Three Ways to Ingest from Postgres
Concepts covered: Change Data Capture
Postgres supports all 3 strategies from this lesson, and a single database often uses all 3, table by table. The choice for each table comes down to 4 questions: how big is the table and how fast does it change, are rows hard-deleted, how fresh must the copy be, and who will be on call for it.
| Strategy | How it reads | Scale | Hard deletes | Freshness | Team cost |
|---|---|---|---|---|---|
| Nightly full snapshot | `SELECT` the whole table, replace the target | Up to a few million rows | Caught | Once a day | A scheduled job |
| Incremental pull | Bounded window on `updated_at`, keyed upsert | Large tables with an indexed change column | Missed | Run interval plus safety lag | Bookmark store and idempotent sink |
| Log-based CDC | Logical replication slot, snapshot then stream | Any size, high change rates | Caught | Seconds | Slot monitoring, re-snapshot runbook, DBA access |
Three tables in a single database
stores has 1,800 rows and changes a few times a week. A nightly full snapshot copies it in under a second, catches the occasional closed store that someone deletes, and has no bookmark to corrupt.
products has 2 million rows and about 50,000 updates a day, and is never hard-deleted: a discontinued product gets discontinued_at set, which also moves updated_at. The catalog team reads it hourly. An hourly incremental pull with a 5-minute lag meets every requirement, and it can run against a read replica so the primary never sees the query.
orders has 40 million rows and 300,000 changes a day, and the application hard-deletes orders that customers cancel within 10 minutes of placing them. Finance wants revenue within 5 minutes. An incremental pull would keep every cancelled order in revenue forever, and the nightly full copy takes 25 minutes and leaves the warehouse a day behind. Log-based CDC meets both needs, and that is why the team takes on its slot monitoring and re-snapshot runbook.
When the team cannot run CDC
Team capacity can overrule the table. Log-based CDC needs wal_level = logical on the source, a setting that takes a database restart, plus a role with replication privilege and an on-call engineer who knows what a stalled slot does to the primary's disk. A 2-person team without DBA access can serve orders with a hybrid: an incremental pull every 15 minutes, and a nightly job that lists every order_id in the source, compares the list with the replica, and deletes the orders that are gone.
- orders (source, Postgres read replica)
- incremental pull (transform, orchestrator, every 15 min); freshness: 15 min + 5 min lag; idempotency: upsert by order_id
- nightly key diff (transform, orchestrator, 02:00); on error: block if over 1% of keys missing
- orders replica (storage, warehouse)
- revenue reports (consumer, finance)
- orders -> incremental pull
- orders -> nightly key diff
- incremental pull -> orders replica
- nightly key diff -> orders replica
- orders replica -> revenue reports
The hybrid has a price. Revenue can trail the source by 20 minutes instead of 5, since the pull interval stacks on top of the lag. A cancelled order also stays in revenue until the 02:00 diff removes it. Agree on both with finance before launch, or ask the application team for soft deletes so cancellations become updates the pull already sees.
- Do
- Decide per table, weighing size and change rate against delete and freshness needs and who will run it
- Keep full snapshots for small tables even when big ones move to CDC
- Save the bookmark only after the data commits
- Make every sink absorb repeats, whether by dedupe or by a uniqueness or version check
- Alert on retained WAL long before it threatens the source disk
- Avoid
- Run CDC on a 1,800-row table that a full copy refreshes in a second
- Trust updated_at before confirming every writer sets it
- Leave a replication slot behind when its pipeline is retired
- Let a key diff delete a large share of rows without a threshold check
A refunds table has 6 million rows and gains 20,000 a day. Rows are never updated or deleted; corrections arrive as new rows. Analysts need it hourly, and your team has no replication access. Which strategy fits?
- Log-based CDC, because only the log guarantees that every new refund is seen
- Nightly full snapshot, because an append-only table never needs a bookmark
- Incremental pull on created_at, with both bounds fixed and a 5-minute lag
- Trigger-based CDC into a side table, so that every insert is recorded twice
About this lesson
This section is part of the Ingestion Patterns: Intermediate lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.