Syntax
Parameters
WITH options
Notes
CREATE CDC TABLErecords metadata only.ALTER CDC TABLE ... ATTACH STREAMnames the stream that feeds the table, and ingestion starts withALTER CDC TABLE ... RESUME. One stream feeds one CDC table.- CDC tables are created and ingested on single-node Engines, and any Engine can query them.
CREATE CDC TABLEandALTER CDC TABLE ... RESUMEreturn an error on an Engine with more than one node.SUSPEND,ATTACH STREAM, andDETACH STREAMare still accepted, so a CDC table on an Engine that was resized can be stopped and detached. - Merge-key columns are implicitly
NOT NULL. - In
readmode, merge-key columns must beBOOLEAN,INTEGER,BIGINT,DATE,TIMESTAMP,TIMESTAMPTZ,NUMERIC, orTEXT(NUMERIConly as a single-column key). Usemerge_mode = 'write'for other key types. - Column names beginning with
$or__are reserved for CDC bookkeeping. merge_modecannot be changed after creation.- In
readmode, direct DML,TRUNCATE, column-levelALTER TABLE, and aggregating indexes are rejected; renaming the table is allowed.writetables behave like ordinary tables. - A
PRIMARY INDEXexpression is computed for every row the table writes, a delete included. A delete storesNULLin each nullable column and a placeholder in eachNOT NULLcolumn outside the merge key (0,'',FALSE,1970-01-01, an empty array), so an expression over such a column sees that value too:CAST(code AS INT)fails on the''of a deleted row.CREATE CDC TABLErejects an expression that fails on that row unless it also reads a merge-key column, whose value a delete keeps. Use forms that cannot fail, such asTRY_CAST. - A change the expression fails on is retried until the retry budget runs out, and then the attachment stops with
state = 'error'and the reason ininformation_schema.cdc_ingests. To recover, replace the expression, either withALTER TABLE ... SET PRIMARY INDEXto columns or by recreating the table, then detach the stream and start a new binding. ALTER TABLE ... SET PRIMARY INDEXtakes column names and changes the order data is written in from then on, as on any table.ALTER TABLE ... DROP PRIMARY INDEXis rejected: a CDC table always has a primary index.
Examples
Mirror a Postgres table
Mirror a MongoDB collection
A MongoDB stream’s columns are always_id, _shard_key and doc, so the table’s are too. The table is keyed by _id and _shard_key:
Sort a MongoDB collection by a document field
WHERE JSON_VALUE(doc.customer.city) = 'Berlin', uses the primary index to skip the tablets and granules that cannot hold 'Berlin'.
Sort the table for analytics instead of by key
ALTER CDC TABLE
Attaches a stream to a CDC table and controls its ingest worker:-
ATTACH STREAMtakes a stream whose columns match the table’s. Attaching the stream the table was detached from continues where it stopped; attaching a stream that has not loaded a table before reloads the table from that stream’s snapshot on the nextRESUME. A stream that has already loaded a table does not load another one, so mirroring into a new table needs a new stream. -
DETACH STREAMstops the worker and unlinks the stream. The table stays queryable at its last state. -
RESUMEbinds the ingest worker to the Engine that runs the statement and persists the enabled state. A worker that an earlierRESUMEstarted on another Engine stops after the statement it is running finishes (for an initial load, after the whole load) and after any cascade already in progress. After an Engine restart, runRESUMEagain on the Engine where ingestion should continue. The firstRESUMEbackfills the table from a source snapshot before streaming.RESUMEis rejected while the attachment is inerror(see CDC tables); the error includes the recorded reason. -
RESUMErequires a single-node Engine. On an Engine with more than one node it returns an error and starts nothing. -
SUSPENDstops the worker without a final consolidation. For Postgres sources, the idle replication slot retains WAL on the source while suspended. On an attachment inerror,SUSPENDhas no effect; a worker still applying what it had staged continues until it is done.
DROP TABLE
DROP TABLE <table_name> drops a CDC table together with its hidden staging storage and stops its ingest worker. Dropping the table does not drop the stream; see DROP STREAM for source-side cleanup.
Related
- CDC tables: semantics, merge modes, monitoring.
- CREATE STREAM: define the change feed.
information_schema.cdc_ingests: ingest state, progress, and errors.