Skip to content

Guides

Capture Postgres changes (CDC)

Set up a postgres_cdc source: prepare Postgres, load the snapshot and the changes after it, keep streaming, add tables and re-snapshot.

A postgres_cdc source captures a Postgres database’s changes. The first sqlload load copies every table under a snapshot. Each later load reads the inserts, updates and deletes committed since, from a logical replication slot, applies them, and stops. This guide sets one up into DuckDB. For its settings, other targets and guarantees, see Postgres CDC.

  • The server runs with wal_level = logical. Changing it needs a restart.
  • The connection’s user has the REPLICATION attribute and can SELECT the tables.
  • A publication of exactly the source’s tables; one that publishes others stops the load. When it doesn’t exist, SQLLoad creates it (sqlload_<source> unless publication names one), which needs a user that owns the tables and has CREATE on the database. Otherwise create it as the owner and name it with publication; SQLLoad then only checks it.
  • Every table has a primary key and a replica identity other than NOTHING.
  • TLS is set on the connection as for any Postgres source, and covers replication too. See TLS.
  • The target is DuckDB, Snowflake, Databricks (see Into Databricks), or a files target with cdc_mode = "changelog" (see Into Parquet files). plan refuses BigQuery, ClickHouse and Postgres targets.

This example streams a shop database into DuckDB, as a user that doesn’t own the tables.

Save this as seed.sql. orders has a text[] column, tags:

CREATE TABLE customers (
customer_id bigint PRIMARY KEY,
email text NOT NULL,
country text
);
CREATE TABLE orders (
order_id bigint PRIMARY KEY,
customer_id bigint NOT NULL REFERENCES customers,
status text NOT NULL,
tags text[],
total numeric(12,2) NOT NULL
);
INSERT INTO customers
SELECT i, 'customer' || i || '@example.com',
(ARRAY['GB', 'FR', 'DE', 'US'])[1 + i % 4]
FROM generate_series(1, 50) AS i;
INSERT INTO orders
SELECT i, 1 + i % 50, (ARRAY['placed', 'paid', 'shipped'])[1 + i % 3],
ARRAY['web'], round(10 + i * 1.37, 2)
FROM generate_series(1, 1000) AS i;

Start Postgres with logical decoding on, and load the data:

docker run -d --name app-db -e POSTGRES_PASSWORD=secret -p 5432:5432 postgres:17 -c wal_level=logical
docker exec -i app-db psql -q -U postgres < seed.sql

As the tables’ owner, create the loader’s user and a publication of the tables:

CREATE ROLE loader LOGIN REPLICATION PASSWORD 'secret';
GRANT SELECT ON ALL TABLES IN SCHEMA public TO loader;
CREATE PUBLICATION app_changes FOR TABLE customers, orders;
default_target = "local"
[connections.app]
kind = "postgres"
host = "localhost"
user = "loader"
password = "${ENV:PGPASSWORD}"
database = "postgres"
[connections.warehouse]
kind = "duckdb"
database = "shop.duckdb"
[targets.local]
connection = "warehouse"
loader_schema = "raw"
[sources.app]
kind = "postgres_cdc"
connection = "app"
description = "The shop's app database, streamed from a replication slot."
include = ["public.*"]
publication = "app_changes"
[sources.app.tables."public.customers"]
description = "Customers, one row per customer."
[sources.app.tables."public.orders"]
description = "Orders, one row per order."
columns = { tags = "text" }

The connection is an ordinary postgres connection. Array and json columns must be pinned to text, as tags is here; jsonb needs nothing. Without the pin, plan stops:

Errors (1)
└── raw.app__orders
└── reason tags: kind = "postgres_cdc" streams text[] values as the source wrote them, which
the snapshot normalises as jsonb; pin it with columns = { tags = "text" }, or
exclude it
export PGPASSWORD=secret
$ sqlload plan -v
Plan ready 2 selected, 2 objects to create, 2 to load
to DuckDB local
Objects (2) sqlload apply
├── raw.app__customers create table
│ ├── customer_id int -> BIGINT
│ ├── email text -> VARCHAR
│ ├── country text -> VARCHAR
│ └── + _sqlload_lsn, _op, _committed_at, _deleted_at, _load_id, _loaded_at
└── raw.app__orders create table
├── order_id int -> BIGINT
├── customer_id int -> BIGINT
├── status text -> VARCHAR
├── tags text -> VARCHAR
├── total decimal(12,2) -> DECIMAL(12,2)
└── + _sqlload_lsn, _op, _committed_at, _deleted_at, _load_id, _loaded_at
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 -> DuckDB takes Arrow
└── 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 -> DuckDB takes Arrow
✓ Plan complete 2 selected, 2 objects to create, 2 to load

sqlload apply creates the tables. The first sqlload load creates the slot, sqlload_app, and copies every table:

$ sqlload load
✓ Plan complete 2 to load (sqlload plan shows the tree)
loaded raw.app__customers 50 rows 0.4s
loaded raw.app__orders 1,000 rows 0.4s
✓ Completed 2 loaded, 1,050 rows, 0.4s
├── slowest raw.app__customers 0.4s
└── peak memory 78.4 MiB

After an insert, an update and a delete in Postgres:

INSERT INTO orders VALUES (1001, 7, 'placed', ARRAY['app'], 25.00);
UPDATE orders SET status = 'shipped' WHERE order_id = 1;
DELETE FROM orders WHERE order_id = 2;

the next load reads just those three changes:

$ sqlload load
✓ Plan complete 2 to load (sqlload plan shows the tree)
loaded raw.app__customers 0 rows 0.2s
loaded raw.app__orders 3 rows 0.2s
✓ Completed 2 loaded, 3 rows, 0.3s
├── slowest raw.app__customers 0.2s
└── peak memory 84.6 MiB

In raw.app__orders, the deleted order is kept and marked:

order_id status tags _sqlload_op _sqlload_deleted_at set
1 shipped {web} update no
2 shipped {web} delete yes
1001 placed {app} insert no

Run sqlload load on a schedule; each run stops at the changes committed before it started. To load changes as they arrive, use --follow.

sqlload load SOURCE --follow keeps streaming after it catches up, on one replication session. It writes a batch every batch_interval (default 10s, or 1m into files), at 100,000 changes, or when it stops; a transaction still open on the server waits until it commits. It runs until --max-duration passes or it gets Ctrl-C or SIGTERM:

sqlload load app --follow # until Ctrl-C or SIGTERM
sqlload load app --follow --max-duration 10m

Here an order is inserted and then updated while it follows, then Ctrl-C stops it:

$ sqlload load app --follow
✓ Plan complete 2 to load (sqlload plan shows the tree)
2026-11-15T03:47:51.265378Z INFO sqlload::cdc::main::follow_cdc: following
2026-11-15T03:48:01.402075Z INFO sqlload::cdc::_helpers::stream: batch written source="app" changes=2 tables=1 lsn=0/1572DF0 behind_bytes=56 behind_secs=2
written raw.app__orders 1 row 2s behind
2026-11-15T03:48:10.656949Z INFO sqlload::command::_helpers::loader_commands: stopping once the batch or copy under way is written; press Ctrl-C again to stop now
2026-11-15T03:48:10.660993Z INFO sqlload::cdc::main::follow_cdc: stopped following rows=1
loaded raw.app__customers 0 rows 19.4s
loaded raw.app__orders 1 row 19.4s
✓ Completed 2 loaded, 1 row, 19.5s
├── slowest raw.app__customers 19.4s
└── peak memory 84.3 MiB

behind_bytes and behind_secs say how far each batch is behind the server. The two changes to one order load as one row under merge.

  • Stopping. --max-duration, or a first Ctrl-C or SIGTERM, writes what it has read (and finishes a copy under way), then exits 0. A second signal stops at once with exit code 130; the next load skips what was already written.

  • Starting again at once. A new load waits up to 30 seconds for the slot, then fails naming the server process holding it: another load, or a lost connection Postgres hasn’t noticed yet (wal_sender_timeout).

  • While idle it sends Snowflake or Databricks no queries, so the warehouse can suspend, and still confirms the server’s position, so the slot keeps no WAL for other tables’ writes. Each batch written resumes the warehouse, and a longer batch_interval writes fewer, larger batches.

  • A column added is planned again and added to the destination, and the stream carries on:

    2026-11-07T15:03:20.136978Z INFO sqlload::cdc::_helpers::stream: re-planning gained=public.customers gained column vip
    2026-11-07T15:03:20.160941Z INFO sqlload::cdc::_helpers::stream: applied line=altered raw.app__customers add column vip bool -> BOOLEAN
    2026-11-07T15:03:20.161020Z INFO sqlload::cdc::main::follow_cdc: following

    The same column gained again, with nothing written in between, stops it.

  • Plan errors. It starts only when every selected table plans, as the slot would move past the changes of the rest. Leave them out with --exclude <source>, or with the failing tables in the source’s exclude. A re-plan that finds errors, such as a lost primary key, stops it.

  • Any other error exits 1 without confirming anything unwritten. Run it under a supervisor that restarts it.

  • A table that joins include is copied at the next re-plan or restart; see Adding tables.

  • One source per process. Select one postgres_cdc source and nothing else. plan --follow, apply --follow, --max-duration without --follow, and --follow with --reload, the cursor options, --sync-deletes, --fail-fast, --events, --events-file or --memory-per-table are refused. --resnapshot applies once, at the start.

Every row carries _sqlload_lsn (its transaction’s commit LSN), _sqlload_op (snapshot, insert, update, delete or truncate) and _sqlload_committed_at.

  • merge keeps one row per key. A soft delete sets _sqlload_deleted_at, and a truncate soft-deletes every older row. A TOASTed value the change left out keeps its current value.
  • changelog appends every change. _sqlload_seq is its position in the transaction, and _sqlload_unchanged lists the values the change left out.

With cdc_mode = "changelog", two updates to order 5 after the snapshot leave three rows:

order_id status _sqlload_op _sqlload_seq
5 shipped snapshot 0
5 paid update 1
5 shipped update 1

A table that include matches after the first load is copied on its own at the start of the next load, while the others keep streaming. The copy needs one free slot under max_replication_slots. Here the owner creates refunds:

CREATE TABLE refunds (
refund_id bigint PRIMARY KEY,
order_id bigint NOT NULL REFERENCES orders,
amount numeric(12,2) NOT NULL
);
INSERT INTO refunds SELECT i, i * 10, 5.00 FROM generate_series(1, 20) AS i;
GRANT SELECT ON refunds TO loader;

plan shows it as a copy:

$ sqlload plan -v
…
└── raw.app__refunds snapshot copy, then apply changes, soft deletes (key: refund_id)
└── source postgres_cdc app public.refunds slot sqlload_app
…

SQLLoad adds the table to its own publication, sqlload_<source>, which needs a user that owns it. A publication named with publication is yours to change, even one SQLLoad created, so until it publishes the table, the load stops:

$ sqlload load
…
failed raw.app__customers publication app_changes does not publish "public"."refunds"; add them with ALTER PUBLICATION "app_changes" ADD TABLE "public"."refunds"
…
✗ Completed with failures 3 failed, 0.1s
…

After ALTER PUBLICATION app_changes ADD TABLE refunds; and sqlload apply, the next load copies refunds and applies the other tables’ changes:

$ sqlload load
…
loaded raw.app__customers 0 rows 0.2s
loaded raw.app__orders 1 row 0.2s
loaded raw.app__refunds 20 rows 0.2s
…

A table that leaves include needs --resnapshot. If it joins again while its destination table holds rows captured before that, the load stops rather than replace them without asking:

failed raw.app__refunds rows captured before slot sqlload_app was recreated are still in app__refunds; copying those tables alone would replace them without asking, so run `sqlload load --resnapshot` to copy every table again (it asks first), or drop the tables from the destination to copy them alone

To copy it alone, drop the destination table (on a files target, its folder) and run sqlload apply. A table whose destination is empty or gone is copied alone without asking.

A load stops and names --resnapshot as the fix when the slot is lost or gone (see The slot and WAL), after a schema change the stream can’t carry, or when a table leaves include.

sqlload load --resnapshot drops and recreates the slot and copies every table again, replacing what it holds: a changelog table loses its history, and a merge table its soft-deleted rows. SQLLoad’s own publication, sqlload_<source>, is reset to include. A first snapshot that stopped part-way resumes by itself; a --resnapshot that stopped part-way needs --resnapshot again. plan --resnapshot shows what it would do:

$ sqlload plan --resnapshot
…
Warnings (2)
├── app__customers
│ └── --resnapshot drops and recreates slot sqlload_app, then copies every table of app again
└── app__orders
└── --resnapshot drops and recreates slot sqlload_app, then copies every table of app again
✓ Plan complete 2 to load

On a terminal, load --resnapshot asks first. Elsewhere it needs --yes:

$ sqlload load --resnapshot
error: --resnapshot rebuilds app__customers, app__orders from the whole source, replacing what they hold; confirm it in a terminal, or add --yes
$ sqlload load --resnapshot --yes
…
loaded raw.app__customers 50 rows 0.3s
loaded raw.app__orders 1,000 rows 0.3s
…

--reload refuses captured tables; --resnapshot copies them again. A bare load --reload still rebuilds other sources’ tables, but exits 1. Here shop is a postgres source next to app:

$ sqlload load --reload --yes
Errors (1)
└── raw.app__customers, raw.app__orders
└── reason a postgres_cdc table is copied again with --resnapshot, not --reload
✗ Plan failed 1 object to create, 1 to load, 1 error
created raw.shop__products 4 columns
loaded raw.shop__products 30 rows 0.0s
…

To reload only the other tables, leave the source out with sqlload load --reload --yes --exclude app.