Enable CDC
Enable CDC per connection using a PRAGMA:Capture Modes
| Mode | Description |
|---|---|
id | Capture only the primary key/rowid of changed rows |
before | Capture row state before changes (for updates/deletes) |
after | Capture row state after changes (for inserts/updates) |
full | Capture both before and after states, plus per-column update details |
CDC Table
Changes are stored in theturso_cdc table by default. You can specify a custom table name:
| Column | Type | Description |
|---|---|---|
change_id | INTEGER | Auto-incrementing unique identifier |
change_time | INTEGER | Timestamp (Unix epoch) |
change_txn_id | INTEGER | Transaction ID (groups rows into transactions) |
change_type | INTEGER | 1 = INSERT, 0 = UPDATE, -1 = DELETE, 2 = COMMIT |
table_name | TEXT | Name of the table that was changed |
id | varies | Primary key/rowid of the changed row |
before | BLOB | Row data before the change (modes: before, full) |
after | BLOB | Row data after the change (modes: after, full) |
updates | BLOB | Per-column change details (mode: full) |
Querying Changes
Decoding Binary Records
Thebefore, after, and updates columns store data in a binary format. Use the built-in helper functions to decode them:
| Function | Description |
|---|---|
table_columns_json_array(table_name) | Returns a JSON array of column names for a table (e.g. ["id","name","email"]) |
bin_record_json_object(columns_json, blob) | Decodes a binary record into a JSON object using the given column names |
Transactions
CDC respects transaction boundaries. Changes are only recorded when a transaction commits. If a transaction rolls back, no CDC entries are created. All rows from the same transaction share the samechange_txn_id, with a single COMMIT record (change_type = 2) at the end:
Schema Changes
CDC also tracks DDL operations (CREATE TABLE, DROP TABLE, CREATE INDEX, etc.) as changes to thesqlite_schema table: