Three Ways to Detect That Data Changed
Run timestamp polling, a snapshot diff and log-based change data capture side by side on one PostgreSQL table, see which changes each one misses, and compare them by latency, load on the source, delete handling and correctness.
Contentsand lesson path
On this page
- Why it matters
- A small table to watch
- One hour of changes, three answers
- Latency, load, deletes and correctness
- Check your understanding
- Apply it
- Review prompt
Lessons of Change Data Capture
- Three Ways to Detect That Data Changed Concept · 20 min
- Logical Decoding and the Write-Ahead Log Concept · 20 min
- Lab: PostgreSQL to Kafka with Debezium Lab · 30 min
- Worked Example: Applying Changes in Order with MERGE Worked example · 25 min
- Snapshots, Schema Changes and Slot Lag Concept · 20 min
- Check: CDC Failure Modes Knowledge check · 8 questions
What you will learn
- Compare snapshot diffs, timestamp-based extraction and log-based CDC by latency, load on the source, delete handling and correctness.
Prerequisites: Messaging & Event Streaming
01. Why it matters
Most pipelines do not copy a whole database every time. They copy what changed since the last run, which is called incremental extraction. The hard part is not the copy. It is knowing, reliably, what "changed" means: a new row, a new value in an old row, or a row that is no longer there.
There are three common ways to find out. Timestamp polling asks the source for rows whose updated_at column is newer than the last value you loaded. A snapshot diff copies the whole table and compares it with the previous copy. Log-based change data capture (CDC) reads the database's own record of every committed change, the transaction log. They differ in four ways that matter in production: how quickly a change reaches the target (latency), how much work the source database does (load), whether deletes are seen, and whether every change is seen in the right order (correctness).
In this lesson you run all three against the same table and the same hour of changes, and compare what each one reports. The incremental loads lesson of the ETL skill already showed one weakness of timestamps, late commits; here the focus is on what the methods can and cannot see at all.
02. A small table to watch
You need Docker. The example uses the postgres:17-alpine image (it was run with PostgreSQL 17.11) and keeps its data in memory with --tmpfs, so nothing is left on disk when you remove the container. The server starts with wal_level=logical, which the third method needs; the next lesson explains that setting. Start it and wait until it accepts connections:
mkdir cdc-demo && cd cdc-demo
docker run -d --name cdc-demo --tmpfs /var/lib/postgresql/data \
-e POSTGRES_PASSWORD=demo postgres:17-alpine -c wal_level=logical
until docker exec cdc-demo pg_isready -h 127.0.0.1 -U postgres >/dev/null; do
sleep 1
doneThe table holds three orders at 09:00. At 10:00 you prepare all three detectors at once. For the snapshot diff you keep a copy of the table, snapshot_1000. For log-based capture you create a replication slot, a named bookmark in the transaction log that remembers which changes a reader has not taken yet. It uses test_decoding, a demonstration output plugin that ships with PostgreSQL and prints changes as text. For polling you note the watermark, the highest updated_at loaded so far.
CREATE TABLE orders (
id int PRIMARY KEY,
status text NOT NULL,
amount numeric(8,2) NOT NULL,
updated_at timestamptz NOT NULL
);
INSERT INTO orders VALUES
(1, 'placed', 20.00, '2026-11-02 09:00+00'),
(2, 'placed', 35.00, '2026-11-02 09:05+00'),
(3, 'placed', 12.50, '2026-11-02 09:10+00');
-- 10:00: get the three detectors ready.
CREATE TABLE snapshot_1000 AS SELECT * FROM orders; -- for the diff
SELECT slot_name FROM pg_create_logical_replication_slot('detect', 'test_decoding');
SELECT max(updated_at) AS watermark FROM orders; -- for pollingdocker exec -i cdc-demo psql -U postgres -X -q < setup.sql slot_name
-----------
detect
(1 row)
watermark
------------------------
2026-11-02 09:10:00+00
(1 row)03. One hour of changes, three answers
Between 10:00 and 11:00 the application changes the table six times, each statement in its own transaction. Order 1 is paid and later shipped. Order 2 is deleted. Order 4 is created. Order 5 is created and deleted again within the hour, for example a test order or a fraud check that cancelled it. Like many applications, this one sets updated_at itself on every write.
-- Between 10:00 and 11:00 the application does this, one statement at a time.
UPDATE orders SET status = 'paid', updated_at = '2026-11-02 10:05+00' WHERE id = 1;
INSERT INTO orders VALUES (4, 'placed', 80.00, '2026-11-02 10:20+00');
DELETE FROM orders WHERE id = 2;
INSERT INTO orders VALUES (5, 'placed', 9.99, '2026-11-02 10:30+00');
UPDATE orders SET status = 'shipped', updated_at = '2026-11-02 10:40+00' WHERE id = 1;
DELETE FROM orders WHERE id = 5;At 11:00 each detector reports what changed. Polling selects rows above the watermark. The diff joins the old copy and the current table with a FULL JOIN, so that a row present on only one side shows up as an insert or a delete. The slot hands over every change it has kept since 10:00. The regexp_replace only shortens the text lines for the page; it removes the table name and the updated_at value.
\echo 1. Timestamp polling
SELECT id, status FROM orders
WHERE updated_at > '2026-11-02 09:10+00'
ORDER BY id;
\echo 2. Snapshot diff
SELECT coalesce(n.id, o.id) AS id,
CASE WHEN o.id IS NULL THEN 'insert'
WHEN n.id IS NULL THEN 'delete'
ELSE 'update' END AS change,
n.status
FROM snapshot_1000 o
FULL JOIN orders n ON n.id = o.id
WHERE o.id IS NULL OR n.id IS NULL
OR (o.status, o.amount) IS DISTINCT FROM (n.status, n.amount)
ORDER BY 1;
\echo 3. Logical decoding
SELECT regexp_replace(data, '^table public\.orders: | updated_at.*$', '', 'g')
AS change
FROM pg_logical_slot_get_changes('detect', NULL, NULL, 'include-xids', '0')
WHERE data LIKE 'table%';docker exec -i cdc-demo psql -U postgres -X -q < changes.sql
docker exec -i cdc-demo psql -U postgres -X -q < detect.sql1. Timestamp polling
id | status
----+---------
1 | shipped
4 | placed
(2 rows)
2. Snapshot diff
id | change | status
----+--------+---------
1 | update | shipped
2 | delete |
4 | insert | placed
(3 rows)
3. Logical decoding
change
--------------------------------------------------------------------
UPDATE: id[integer]:1 status[text]:'paid' amount[numeric]:20.00
INSERT: id[integer]:4 status[text]:'placed' amount[numeric]:80.00
DELETE: id[integer]:2
INSERT: id[integer]:5 status[text]:'placed' amount[numeric]:9.99
UPDATE: id[integer]:1 status[text]:'shipped' amount[numeric]:20.00
DELETE: id[integer]:5
(6 rows)Read the three answers against the six changes.
- Polling found two rows. It cannot see the deletes of orders 2 and 5, because a deleted row no longer exists to match the filter. It also shows order 1 only as
shipped: thepaidstate existed for 35 minutes but was overwritten before the query ran. - The diff found three changes, including the delete of order 2. It still cannot see order 5, which was neither in the 10:00 copy nor in the 11:00 table, and it also misses the
paidstate. Like polling, it reports the state at two moments and guesses the changes between them. - The slot returned all six changes, in the order they were committed. A delete arrives with the key of the row that was removed, which is exactly what a downstream table needs to remove it too.
04. Latency, load, deletes and correctness
The output answers the delete and correctness questions. Latency and load depend on how you run each method, so think them through.
Latency. Polling and diffs are only as fresh as their schedule: an hourly job means changes up to an hour old. Running polling every minute shortens that, but each run is another query. A log reader can stream changes seconds after commit, because the database writes the log anyway and the reader only follows it.
Load on the source. A polling query is cheap when updated_at has an index and few rows changed; without the index, every run scans the table. A snapshot diff reads the whole table every time, so its cost grows with the size of the table, not with the amount of change. That is fine for a 3,000-row reference table and too slow for a billion-row one. Running either query against a read replica moves the load off the primary, but it closes none of the gaps described below. Log-based CDC adds no queries against the tables, but it is not free. The log must contain extra detail (wal_level=logical), and a slot keeps log files on the server's disk until its reader confirms them, which a later lesson shows can fill the disk.
Deletes. Only the diff and the log see hard deletes. Polling can see them only if the application never really deletes, and instead sets a deleted_at column, a soft delete. That is a rule every writer must follow, including the person who runs a manual DELETE during an incident.
Correctness. Polling trusts updated_at. A writer that forgets to set it, a clock that is behind, or a transaction that commits after the extract with an earlier timestamp all cause silent gaps. A diff is correct about the end state, but it loses intermediate versions and the order of changes. The log records every committed change, in commit order, and it groups the changes of one transaction together. Changes that were rolled back never appear.
A fourth approach sits between these: database triggers that copy each change into a change table, which a job then reads. Triggers see deletes and intermediate versions, but they add work to every write on the source, and every new table needs a new trigger. Log-based CDC avoids both problems by reading the log the database already writes.
In practice, log-based CDC is the usual choice for large or busy tables that need fresh data and deletes. Snapshot diffs remain useful for small tables from sources that give no log access and have no trustworthy updated_at, and as a periodic check that a CDC pipeline has not drifted. Polling is a reasonable start when deletes are soft, writers are disciplined, and an hour of delay is acceptable.
Keep the cdc-demo container for the next lesson. The detect slot has served its purpose, so drop it now: an unused slot keeps log files on the server.
docker exec cdc-demo psql -U postgres -X -q -At \
-c "SELECT pg_drop_replication_slot('detect')" \
-c "SELECT count(*) AS slots_left FROM pg_replication_slots"
005. Check your understanding
06. Apply it
The skill practice captures changes from an orders table and proves that a replica matches it row for row, deletes included. Before you build it, write down for your own orders table: how often it changes, whether rows are hard-deleted, whether every writer sets updated_at, and how fresh the replica must be. Then decide which of the three methods you would use, and which one you would keep as a periodic check. Keep the note; the lab in this skill builds the log-based version.
07. Review prompt
Without looking back, explain: why timestamp polling cannot see hard deletes; why a snapshot diff sees deletes but still misses some changes; what a replication slot remembers for its reader; and how the three methods compare on latency and on load on the source.
References
Your progress
Reading state
Sign in to save that you finished reading this lesson.
Prove this skill
8 questions · about 6 minutes · scored automatically. Marking a lesson as read never demonstrates a skill.