Referencia · §8.44
Changefeeds
changefeed, §M16.5 S3
Ez az oldal a nucleus saját dokumentációjából készül, ezért angol nyelvű.
A changefeed is a stream (§8.43) that every write of its table appends to, in the writing transaction:
CREATE CHANGEFEED account_changes ON accounts WITH (before, retention = '30 days');
UPDATE accounts SET balance = balance + 10 WHERE id = 2;
SELECT seq, op, key, after, before FROM account_changes;
-- 3 | update | {"id": 2} | {"id": 2, "owner": "Bob", "balance": 60} | {"id": 2, "owner": "Bob", "balance": 50}
CONSUME op, key, after FROM account_changes GROUP search_index LIMIT 500;
- Its entries:
seq,time(the commit’s, as any stream’s),op(insert,update,delete),key(the row’s primary key as ajsonobject;{"rowid": n}for a table without one),after(the row as ajsonobject;NULLfor a delete) andbefore(the row it replaced or deleted — onlyWITH (before); otherwiseNULL). A row whose key anUPDATEchanges is adeleteunder the old key and aninsertunder the new. Upserts give aninsertor anupdatefor each row written, none for a rowDO NOTHINGpassed over;TRUNCATEand expiry give adeleteper row. The rows there before the feed was made are not its changes. - Every committed change, in commit order, none rolled back: the
table’s writers’ storage is wrapped (
changefeed::wrap, indml::Writeranddelete_rows), so a row’s write and its entry are one transaction’s — a statement that fails, aROLLBACK, aROLLBACK TO SAVEPOINTtake both back — and the entry is numbered at the commit (§8.43). The table keeps its feeds’ ids (Table::changefeeds); a writer reads the table’s definition validated at its commit, so a write begun before a feed was made fails on it (40001) and runs again with it. The transaction making the feed validates the table’s whole key range (KvTxn::validate_range): a write committed after its snapshot by a writer that saw no feed fails that commit instead. So what the feed’s making transaction reads of the table and what the feed holds after it are, between them, every change — a copy made in that transaction and the feed replayed onto it give the table. The key-only fast delete is not used for a table with feeds. Changes made by statements that rewrite a table’s rows without changing them (another storage, a column retyped) are not changes. - A stream: consumer groups,
stream_wait, retention,ALTER STREAMand the catalog as any stream’s; inserting into it is refused (0A000).nc_catalog.streams.changefeed_ofnames its table. - Made by its table’s owner (with
CREATEon the schema): its entries show every row written. Not yet on a partitioned table or a partition, a chunk table, a stream, or a table with subject columns (their copies in a feed would outliveFORGET SUBJECT). - Dropped:
DROP CHANGEFEED f(orDROP STREAM); its table is dropped only with its feeds —DROP TABLE tis refused (2BP01) while it has one,DROP TABLE t CASCADEdrops them first.nucleus dump --schemawritesCREATE CHANGEFEED f ON t WITH (…).
Tests: slt/98_changefeeds.slt (the entries of each kind of write,
rollbacks and savepoints, a key changed, upserts, TRUNCATE, a failed
statement, a table without a key, refusals, dropping; every storage path
the scripts run), crates/sql/tests/changefeeds.rs (a feed made while
four writers insert, update, delete and upsert, its table copied in the
same transaction, replayed onto the copy in seq order gives the table
— row, column and LSM storage, three seeds each; a write between the
feed’s snapshot and its commit fails the commit).