Referencia · §8.24

Generated columns and embedding columns

dml, embedding, §M14.22 E4

Ez az oldal a nucleus saját dokumentációjából készül, ezért angol nyelvű.

c type GENERATED ALWAYS AS (expr) STORED: the expression, over the row’s other columns, is computed on every write of the row — INSERT, COPY, UPDATE (of any column), a foreign key’s cascade, ALTER COLUMN … TYPE of a column it reads — by Writer::complete before the row’s constraints are checked, and stored like any value (indexed, searched, checked). A value is never given: naming the column in an INSERT’s or COPY’s list or an UPDATE’s SET is 428C9; without a list, an INSERT’s values and a COPY’s fields are the columns that are not generated. The expression may not read a generated column, call a volatile function (42P17) or hold a subquery (0A000), nor be in the partitioning; DEFAULT with it is 42601; VIRTUAL is refused (0A000). ALTER TABLE … ADD COLUMN … GENERATED computes it for every row. The table record (version 15 on) keeps a column’s generation after its statistics: 0, 1 <expr> or 2 <expr> <model> <version u32> <next model?>. nc_catalog.columns shows generated (stored, embedding), generation_expr, model, model_version and next_model; pg-compat shows attgenerated = 's', the expression in pg_attrdef, and information_schema.columns.is_generated / generation_expression.

v vector GENERATED BY EMBEDDING (expr) USING m: the vector of the text expr gives, made by model m in the background. When made: the model exists and the maker has USAGE on it; the type is vector of the model’s dimensions (vector alone takes them, 42804 otherwise); the column may be NULL, is not in the key, and the table has a primary key (42P17); the expression calls no model (embed, 0A000: the worker computes the text, not the role that wrote the row, and the column’s model is its USING). A model used by a column or a chunk table is not dropped (2BP01); dropping the table drops its jobs. ADD COLUMN and SET MODEL read the table validated, so a row a writer commits while they run makes them conflict (40001, run again) instead of going without a job. embed(q) without a model, an operator’s operand beside such a column (ORDER BY v <=> embed($1)), is the binder’s embed(q, 'm', 'query'): the query embedded by the column’s own model.

The queue (embedding): 00 17 <table id u32> <column u16> <primary key> → version, enqueued (µs), attempts, not-before (µs), failed, the last error. An insert puts a job for each new row; an update puts one when a column the expression reads changed (compared as values), and moves a row’s job with its key; a delete removes them — all in the writer’s transaction, a job a row and column, so a row written five times while pending is one job. A round (embedding::work) takes up to 256 due jobs (not-before passed, the model’s circuit closed), reads their texts at one snapshot, calls the models (embed_unchecked: the maker’s USAGE was checked when the column was made; the models’ cache first; a batch refused as a whole by an error that will not pass is tried a text at a time), then writes in one transaction a group: for each job still as read, the row’s vector if its text is still the one embedded (else the job is left for the next round), the job removed; a NULL text or a row gone just removes it; a text whose expression fails for a row (a division by zero, say) fails that job, not the round. A commit that conflicts leaves the group’s jobs for the next round, and a round that panics is the round’s error (the driver freed, the server’s worker alive). Failures that may pass (58030, 40001, 57014, 53300, 53400): attempts + 1, not before now + the provider’s Retry-After (models::retry_hint in the error’s hint, models::retry_after reads it) or a full-jitter backoff (a draw in 0 … min(5 min, 1 s × 2ⁿ⁻¹), from the database’s random source). Others: failed at once, tried again in an hour or when the row’s text changes. Five failures in a row that may pass open the model’s circuit (per database, in memory): no calls for 30 s, doubling to 5 min while it reopens. Rounds run after a session’s statements (embedding::after_statement, when a job may be due: a flag set by a put, or the earliest not-before reached; one round at a time a database) unless a background worker serves the database (embedding::serve, started by the nucleus server: rounds while jobs are due, else asleep until a job is put or comes due — woken again once the writer that put it has committed, since a job put is seen only then). A round that embedded or cleared jobs vacuums the queue’s range after its commit (Kv::vacuum_range, RocksDB’s queue pattern as an HNSW index’s delta, §8.18): else every statement that waits for the queue steps over the done jobs’ tombstones until the whole vacuum runs — 12 ms a statement after 100 000 rows were embedded, 1 ms with it (§M14.24 R4).

Pending rows, by the session’s embedding.pending: include (the default: each row’s vector as it is — NULL before its first, the old one while a new one is made), skip (the binder puts NOT embedding_pending(t)(pk…) over each scan of a table with embedding columns: rows with a job, failed ones too, left out), wait (before a query takes its snapshot, rounds until only jobs failed for good are left, sleeping on the database’s clock until a retried job is due; past the statement’s time — statement_timeout, else a minute — 57014; in a transaction whose snapshot is taken, 25001). Kv::sleep waits on the platform’s clock, so the simulation passes the time. nc_catalog.embedding_jobs (table_name, column_name, model, key, state — pending, retrying, failed —, attempts, enqueued, next_attempt, error; the tables the current role may read). Tests: crates/testkit/slt/61_generated_columns.slt, tests/embedding_columns.rs (kept current, outages, backoff, the circuit, Retry-After, the three modes, a refused text, a writer racing a round, a crash at any I/O point of a workload of writes and rounds).

A column moved to another model (§M14.22 E5): ALTER TABLE t ALTER COLUMN v SET MODEL m2 (the table’s owner; USAGE on m2; not on a partitioned table) records m2 as the column’s next model and puts a job a row under the column’s slot with bit 0x8000 set. Rounds take the columns’ own jobs first and fill the rest of their 256 texts with next-model jobs, whose vectors go beside the table: 00 18 <table id> <column u16> <pk> → the components as f32 little endian (empty: a NULL text). While the column moves, a write that changes the text puts both jobs; a key change moves both and the vector beside; a delete removes them. Each round, for a moving column whose next-model jobs are all done (none left, or this round took the last), swap runs one transaction: no next-model job may be left; every row rewritten (dml::replace_rows) with its vector from beside (a row with none — written while the move began — gets NULL and a job for the new model; the scan validated, so a row written meanwhile makes it wait); the column’s dimensions the new model’s, its index methods defined again and built; its model the next, its version + 1; the store beside and the old model’s jobs removed. A conflict leaves it for the next round. SET MODEL to the column’s current model gives a move up (its jobs and store removed); to another, starts over. A model that is a column’s next one is not dropped (2BP01). wait does not wait for next-model jobs; a job failed for good holds the move until it is mended or given up. nc_catalog.embedding_jobs.kind is current or next (model then the next one).