Practical technical guides / Core IT: data engineering (incremental extraction)

An updated_at Watermark Skips Rows That Commit Late: Incremental Extracts From PostgreSQL Measured

On PostgreSQL 18.4 a row stamped before an extract and committed after it was never selected. Lookback, a watermark capped at the oldest open transaction, or CDC.

By Sandeep Belgavi · Published 2026-11-08

A late commit hides below the watermark
Original explanatory diagram; synthetic values where shown.

On PostgreSQL 18.4 a row stamped before an extract and committed after it was never selected. Lookback, a watermark capped at the oldest open transaction, or CDC.

A row that was updated but never extracted

Hansini maintains the job that copies changed help-desk cases from PostgreSQL into the analytics warehouse at Belgavi's Book Store. Every 30 seconds it runs SELECT ... WHERE updated_at > :watermark ORDER BY updated_at, loads the rows, and saves the largest updated_at it saw as the next watermark. A finance analyst found a refund that was approved in the application but never appeared in the warehouse. The row's updated_at was older than the watermark at the moment it became visible, so no later extract would ever select it.

This guide reproduces that on PostgreSQL 18.4, explains why it happens under ordinary transactions, and compares three fixes with what each costs. The interactive lab replays the same strategies over timelines you can edit.

Why a timestamp is not a commit order

updated_at is written inside the transaction that changes the row. With a default of now(), PostgreSQL uses the transaction's start time; now() is documented as the start time of the current transaction, constant within it. Even clock_timestamp(), the statement-time alternative, is still earlier than the commit. Other sessions cannot see the row until the transaction commits, which under the default Read Committed isolation level is exactly when a later statement in another session can see it.

So two transactions can commit in the opposite order to their timestamps:

  1. Transaction A starts at t = 10 s, updates case 1 with updated_at = 10, and stays open while an agent step finishes.
  2. Transaction B starts at t = 40 s, updates case 2 with updated_at = 40, and commits at once.
  3. The extract at t = 60 s sees case 2 only, and advances the watermark to 40.
  4. A commits at t = 75 s. Case 1 now has updated_at = 10, below the watermark.
  5. Every later extract asks for updated_at > 40. Case 1 is never selected.

What PostgreSQL 18.4 did

The companion probe runs those steps with two writing sessions and an extractor, on a scratch PostgreSQL 18.4 server with psycopg 3.2.10:

StepResult
A updates case 1, not committedA's row has updated_at = T
1.2 s later, B updates case 2 and commitsB's row has updated_at = T + 1.21 s
extract 1 (A still open)returns case 2 only; naive watermark becomes T + 1.21 s
A commitscase 1 keeps updated_at = T
extract 2, naive updated_at > watermarkreturns nothing; case 1 is lost
extract 2, 5 s lookbackreturns cases 1 and 2 (case 2 again)
extract 2, watermark capped at the oldest open transactionreturns cases 1 and 2 (case 2 again)
logical decoding slot, test_decodingcase 2's update, then case 1's: commit order

The probe found the oldest open writing transaction with pg_stat_activity, using xact_start for sessions that have an assigned transaction id (backend_xid IS NOT NULL). That column is documented; reading other users' sessions in it needs suitable privileges, such as the pg_read_all_stats role.

Three fixes and what they cost

Lookback. Query updated_at > watermark - L and de-duplicate by primary key and updated_at downstream. It is the most common fix because it is one line. It is only correct if L is longer than the longest time between a row's timestamp and its commit, and the boundary is strict: in the lab's late_commit_lookback_short preset, a 30 s lookback with a watermark of 40 asks for updated_at > 10, and the row stamped exactly 10 is still missed. Every extract re-reads the last L seconds, so the cost is duplicates proportional to the write rate times L. You also cannot know L for certain; an idle-in-transaction session can hold a row for hours.

Cap the watermark at the oldest open transaction. Before saving the new watermark, find the start of the oldest transaction that has written and is still open, and never advance past it:

SELECT min(xact_start)
FROM pg_stat_activity
WHERE backend_xid IS NOT NULL
  AND pid <> pg_backend_pid();
top = max(row.updated_at for row in batch) if batch else watermark
oldest = fetch_oldest_open_write_start()          # the query above, or None
new_watermark = top if oldest is None else min(top, oldest - TICK)
watermark = max(watermark, new_watermark)          # never move backwards

This is correct whenever updated_at is set from the database clock inside the transaction, because every uncommitted row then has a stamp at or after its transaction's start. It fails in two ways the lab shows. An idle transaction pins the watermark: with one session left open from t = 5 s, every extract re-reads everything since then (28 repeated reads in idle_transaction_capped). And a stamp from the application's clock can be older than the transaction that writes it, which the cap cannot see (app_clock_stamp_capped). Set idle_in_transaction_session_timeout so the first problem has an upper bound.

Read changes in commit order. Logical decoding delivers committed changes in commit order, each once, from a replication slot. Debezium and similar tools build on it. In the probe, the test_decoding output listed case 2's update before case 1's, which is exactly the order a watermark cannot reconstruct. The costs are operational: wal_level = logical, a replication slot that retains WAL until it is consumed, monitoring for a slot that falls behind, and a consumer that handles schema changes.

StrategyMisses late commitsRepeated readsExtra moving parts
naive watermarkyesnonenone
lookback Lonly if lag exceeds Labout write rate times Lde-duplication
capped at oldest open transactionno, if stamps come from the database inside the transactiongrows while a transaction stays openprivilege to read pg_stat_activity, an idle timeout
commit order (logical decoding)nononeslot, WAL retention, a consumer

Choosing

If the table is written only by short transactions that stamp updated_at with now() and commit within a second, a lookback of a minute with de-duplication is a reasonable, cheap choice, provided you also alert on any transaction older than the lookback. If agents or batch steps can hold transactions open, or you cannot bound transaction length, use the cap. If you need every change, including deletes, in order, use change data capture and stop treating updated_at as a cursor at all. Deletes are invisible to all three timestamp strategies; soft deletes or a separate tombstone table are needed if you stay with polling.

A related fix is to keep transactions short in the first place. An agent step that holds a database transaction open while it waits for a model response is the usual cause of the long gap between stamp and commit. Do the model call first, then open the transaction, write and commit.

Failure modes to check

  • Ties. Two rows with the same updated_at straddle a batch limit, and > skips the second. Page with (updated_at, id) > (:ts, :id) and order by both.
  • Clock changes. A stamp from a server whose clock stepped backwards looks like a late commit. Database-side stamps from one primary avoid mixing clocks.
  • Replica reads. Extracting from a replica adds replication lag to the gap; the cap must then be read from the primary.
  • Silent progress. A naive extract that loses rows reports success. Reconcile counts per day between source and warehouse.

Verifying it

Run the probe against a scratch database (it drops and creates a table and a slot) and keep its output with your extractor's tests. The companion model's tests check that the naive watermark never delivers the late commit, that a 30 s lookback misses it and 31 s does not, that the cap catches it with one repeated read, that commit order delivers each change once, and that an idle transaction and an application-clock stamp break the cap in the ways described. In production, the daily reconciliation count is the test that keeps running.

Interactive experiment

Watermark extracts: late commits, lookback and the oldest open transaction. Synthetic browser-local fixtures, adjustable inputs, decision traces and JSON export.

Primary references and authorship

Original explanation and synthetic examples by Sandeep Belgavi. Companion code: Sandeep-BA/agent-decision-lab/examples/python/watermark_late_commit. Views are my own and do not represent any employer, past or present.