Skip to content

Sources

Postgres CDC

The postgres_cdc source: settings, Databricks and Parquet targets, what a load guarantees, the slot and WAL, schema changes, and what plan refuses.

A postgres_cdc source captures a Postgres database’s changes. The first sqlload load copies every table under a snapshot; each later load applies the inserts, updates and deletes committed since, from a logical replication slot, and stops. It loads into DuckDB, Snowflake and Databricks, and into files targets as a changelog. To set one up, and for what the server, user and tables need, follow Capture Postgres changes. This page is the reference.

A postgres_cdc source takes a postgres connection, include and exclude, and cdc_mode, deletes, slot, publication and batch_interval; see Postgres CDC settings for their defaults. Column settings work as for other sources. A captured table takes no write_strategy, cursor_column, batch_size or state; merge matches rows on the primary key. unnest turns lists in json and jsonb columns into child tables, except into files targets; see Nested data.

--reload refuses captured tables; --resnapshot copies them again (see Reloading).

A Databricks target takes merge (soft or hard deletes) or changelog, with everything that works for DuckDB and Snowflake. With the source from the guide:

default_target = "lakehouse"
[connections.lakehouse]
kind = "databricks"
host = "${ENV:DATABRICKS_HOST}"
client_id = "${ENV:DATABRICKS_CLIENT_ID}"
client_secret = "${ENV:DATABRICKS_CLIENT_SECRET}"
warehouse_id = "${ENV:DATABRICKS_WAREHOUSE_ID}"
catalog = "main"
volume = "main.raw.stage"
[targets.lakehouse]
connection = "lakehouse"
loader_schema = "raw"
$ sqlload plan -v
…
Loads (2) sqlload load
├── raw.app__customers snapshot copy, then apply changes, soft deletes (key: customer_id)
│ ├── source postgres_cdc app public.customers slot sqlload_app
│ └── route read as Arrow -> Parquet -> PUT to /Volumes/main/raw/stage -> COPY INTO
└── raw.app__orders snapshot copy, then apply changes, soft deletes (key: order_id)
├── source postgres_cdc app public.orders slot sqlload_app
└── route read as Arrow -> Parquet -> PUT to /Volumes/main/raw/stage -> COPY INTO
…

With cdc_mode = "changelog" a table shows snapshot copy, then append changes (changelog); with deletes = "hard", apply changes, hard deletes.

Each table’s batch is written by one statement, so readers never see a truncated table emptied. If sqlload is killed while that statement runs on the warehouse, it may still finish; the restarted load’s replay then relies on Delta’s conflict detection, which the tests don’t cover.

A files target, local or S3, takes cdc_mode = "changelog". With the source from the guide and a local folder:

default_target = "files"
[connections.lake]
kind = "filesystem"
root = "lake"
[targets.files]
connection = "lake"
[sources.app]
kind = "postgres_cdc"
connection = "app"
cdc_mode = "changelog"
# ...the rest as in the guide
$ sqlload plan -v
…
└── app__orders snapshot copy, then append changes (changelog)
├── source postgres_cdc app public.orders slot sqlload_app
├── files lake/app/orders/ parquet (zstd)
├── schema first files, 12 columns
└── note each batch of changes adds new files; a snapshot copy (the first load, a table joining the source, or --resnapshot) writes its files, then deletes every older file in the folder

Each load adds Parquet files to the folder of each table it changed. After the first load and the changes from step 4, read the folder with union_by_name, since files written after a column was added hold it and older ones don’t:

SELECT order_id, status, _sqlload_op
FROM read_parquet('lake/app/orders/*.parquet', union_by_name = true)
WHERE order_id IN (1, 2, 1001)
ORDER BY order_id, _sqlload_lsn;
order_id status _sqlload_op
1 paid snapshot
1 shipped update
2 shipped snapshot
2 NULL delete
1001 placed insert
  • Each change is in exactly one row, across crashes. Files of a load that stopped part-way never count, and are deleted at the table’s next write.
  • A snapshot copy (the plan’s note line) deletes the folder’s history, and a reader in between sees both. One written in more files than the target’s max_footer_reads (default 100, at about a million rows a file) stops the next plan until you raise it.
  • Snapshot records live in _sqlload/cdc/<source>/<slot>/ under the fixed folders of the source’s destination_path, else the target’s path: here lake/app/_sqlload/cdc/app/sqlload_app/. A table’s own destination_path doesn’t move them.
  • A table that rejoins the source while its folder holds files is refused: delete the folder, or run --resnapshot.
  • Under --follow, batch_interval defaults to 1m. Nothing compacts the files.

merge and a {cursor} placeholder in the path are refused at plan:

└── app__customers, app__orders
└── reason a files target takes kind = "postgres_cdc" only with cdc_mode = "changelog": Parquet
files are append-only, so changes cannot be merged into rows
└── app__customers, app__orders
└── reason a postgres_cdc table writes one changelog, not partition folders; remove the cursor
placeholder from its path
  • A batch holds whole transactions.
  • The slot confirms a batch only after every table it touched has committed, and a change whose _sqlload_lsn a table already holds is skipped. A load that stops part-way is safe to run again: nothing is lost or applied twice.
  • Each table converges to the source as of the last confirmed position; tables aren’t consistent with each other at every moment.
  • A transaction or batch over about 256 MiB of changes spills to the temporary folder (TMPDIR; use a disk, not a memory-backed tmpfs).

Postgres keeps WAL from the slot’s last confirmed position, so WAL builds up between loads. Every load confirms what it applied, even one that found no changes, so Postgres can free it. To see how much each slot holds:

SELECT slot_name, wal_status,
pg_size_pretty(pg_current_wal_lsn() - confirmed_flush_lsn)
AS not_loaded,
pg_size_pretty(pg_current_wal_lsn() - restart_lsn) AS wal_kept
FROM pg_replication_slots
WHERE slot_name LIKE 'sqlload_%';

After a load, not_loaded is 0 bytes; wal_kept shrinks at the first load after Postgres’s next checkpoint.

If loads stop running, the slot keeps WAL until the disk fills. Cap it with Postgres’s max_slot_wal_keep_size. A slot past the cap is marked lost, and the next load stops:

failed raw.app__orders slot sqlload_app lost WAL it had not confirmed, so changes are missing; run `sqlload load --resnapshot` to drop and recreate it and copy every table again

SQLLoad drops a slot only under --resnapshot. When you remove a source, drop its slot and publication yourself, or they keep holding WAL:

SELECT pg_drop_replication_slot('sqlload_app');
DROP PUBLICATION app_changes;

plan and apply add new columns as for other sources (see on_schema_change).

Change in Postgres What loads
A column added without a default The new column, with values from rows changed after it was added.
A column dropped It stays with its values; new rows get NULL. A value written just before the drop, in the same load, isn’t loaded.
A type on_schema_change widens, such as numeric(12,2) to numeric(14,2) The widened column, altered in place.

A change from before a column was added leaves it out: merge keeps the row’s current value, and changelog writes NULL and lists the column in _sqlload_unchanged. Three transactions, one after another, into a changelog table:

UPDATE customers SET country = 'DE' WHERE customer_id = 3;
ALTER TABLE customers ADD COLUMN vip boolean;
UPDATE customers SET vip = true WHERE customer_id = 3;
customer_id country vip _sqlload_op _sqlload_unchanged
3 DE NULL update ,vip,
3 DE true update NULL

These stop the load, with --resnapshot as the fix, as no change carries the values the table holds:

  • a column added with a default, or as an identity or generated column;
  • a renamed column, or one loaded again after it stopped loading;
  • a primary key change under merge (changelog loads it);
  • a column dropped and another added while changes to the dropped one are unloaded, which looks like a rename. Run a load between the two.
failed raw.app__customers column tier of app__customers was added with a default (or as an identity or generated column), which fills existing rows without changes to stream; run `sqlload load --resnapshot` to copy every table again, or exclude the column

A column added while a load runs stops it before anything is written; run it again. Under --follow, it plans again and carries on. A generated column, or one left out of the publication’s column list, never streams, so a table that loads one is refused: add it to exclude_columns.

A source’s tables share its slot, so it loads whole. Selecting some of them is refused:

$ sqlload load app__orders
Errors (1)
└── raw.app__orders
└── reason this selection leaves out public.customers of source app, whose changes share its
one slot and would be skipped; select the whole source, or move those tables to
another postgres_cdc source
…

So are a table without a primary key, and a BigQuery or ClickHouse target:

Errors (1)
└── raw.app__events
└── reason public.events has no primary key; kind = "postgres_cdc" captures tables with one in
this release (publishing a table without a replica identity makes Postgres refuse
its updates and deletes); add a primary key, or exclude it
Errors (1)
└── raw.app__customers, raw.app__orders
└── reason kind = "postgres_cdc" loads into DuckDB, Snowflake and Databricks targets, and into
files targets as a changelog, in this release; not BigQuery or ClickHouse

For files targets, see Into Parquet files.

The test suite runs against Postgres 15, 16 and 17. Live rounds into DuckDB, Snowflake (through an S3 external stage) and a Databricks SQL warehouse, under merge and changelog, matched Postgres after snapshots, inserts, updates leaving a TOASTed value out, deletes, truncates, a spilled transaction, schema changes, load --follow and loads stopped part-way.