Scrivio Interview library
DebuggingIntermediate7 min read · updated 12 November 2026

A nightly job extracts orders updated since the last run using WHERE updated_at > :lastRun, and the warehouse is missing some orders every few days. Find likely causes.

The query WHERE updated_at > :lastRun only finds rows whose timestamp is visible and later than the mark when the job runs, and several common situations break that. A long transaction can commit after the job ran with an updated_at earlier than the new mark, some writers do not set updated_at at all, application and database clocks or time zones can disagree, and deletes never appear in a timestamp query. We fix it by reading an overlapping window with idempotent upserts, using database-generated timestamps, or switching to log-based change data capture.

Why this question is asked

Incremental extraction by timestamp is the first approach almost every team builds, because it is one WHERE clause. It also produces one of the most frustrating bugs in data work: a few rows missing every few days, never the same ones, with no error anywhere. Interviewers ask it to see whether we understand transactions and visibility well enough to explain the gap, and whether we know the reliable alternatives. In this answer, we will recap how the job works, reproduce the main cause with two sessions, go through the other causes, and then look at three fixes.

1How the Job Works

Each night, the job remembers the time it last ran, called the high-water mark. It reads all orders with updated_at after that mark, upserts them into the warehouse, and stores the new mark, usually the time the job started or the largest updated_at it saw. The idea is that every change since the last run has a newer timestamp and will be picked up exactly once.

That idea assumes that a row's timestamp is set at the moment the row becomes visible, that every writer sets it, and that all clocks agree. Each assumption fails in practice.

2Cause 1: Long Transactions Commit Behind the Mark

The most common cause is a transaction that sets updated_at early and commits late. In PostgreSQL, now() returns the time the transaction started, and even clock_timestamp() returns the time of the statement, not of the commit. Until the transaction commits, other sessions, including our job, cannot see the row.

SQL
-- 01:59:50 Session A (a slow batch in the shop)
BEGIN;
UPDATE orders SET status = 'PAID', updated_at = now() WHERE id = 501; -- updated_at = 01:59:50
-- ... more work, takes 30 seconds ...
-- 02:00:00 Session B (the nightly job)
SELECT * FROM orders WHERE updated_at > '2026-11-05 02:00:00'; -- order 501 not visible yet
-- job stores lastRun = '2026-11-06 02:00:00'
-- 02:00:20 Session A
COMMIT; -- order 501 is now visible with updated_at = 01:59:50
-- next night: WHERE updated_at > '2026-11-06 02:00:00' -- 01:59:50 is older, 501 is skipped forever
Step throughA row that commits behind the high-water mark
Shop batchOrders DBNightly job
01:59:50 update order 501, updated_at 01:59:50
02:00:00 select updated_at > last mark
501 not visible: not committed
store new mark 02:00:00
02:00:20 commit
next night: updated_at > 02:00:00
501 is older than the mark, skipped

7 messages in order. Play them, or step through them one at a time.

Notice that nothing was wrong with either session on its own. The job saw a consistent snapshot, and the batch wrote a correct timestamp. The row simply became visible after the mark had moved past its timestamp. The longer the transactions in the source, the more rows fall into this gap, which is why it happens every few days, on nights when a slow batch overlaps the job.

3Cause 2: Writers That Do Not Maintain updated_at

The timestamp is only as good as the code that sets it. Often the application's ORM sets updated_at, for example through a Hibernate @UpdateTimestamp or a JPA @PreUpdate callback, but other writers bypass the ORM: a nightly refund script using plain JDBC, a support engineer's manual UPDATE to fix an address, a bulk import tool, or a stored procedure. Those rows change without a new timestamp and the job never sees them. A database trigger that sets updated_at on every update closes this gap for all writers, but teams often forget it on newer tables.

4Cause 3: Clocks and Time Zones

If the application servers set updated_at from their own clocks, a server whose clock runs two seconds behind writes timestamps from the past, and rows can land behind the mark just like in cause 1. Time zones make it worse. If updated_at is a timestamp without time zone filled with local time on some servers and UTC on others, or the job compares it with a mark in a different zone, a whole hour of changes can be skipped or read twice, typically around daylight saving changes. Using timestamptz, filling it in the database, and keeping the mark in UTC removes this class of problems.

5Cause 4: Deletes Are Invisible

A deleted row has no updated_at, because it no longer exists. A timestamp query can never return it, so the warehouse keeps orders that were deleted in the source. Strictly, this produces extra rows rather than missing ones, but it is part of the same weakness and usually shows up in the same reconciliation. Soft deletes, a deleted_at column set instead of a real delete, make deletes visible to timestamp queries, at the cost of changing the application.

6Fix 1: Overlap the Window and Make Loads Idempotent

The quickest fix keeps the timestamp approach but reads further back than the mark. If no transaction in the source runs longer than, say, 10 minutes, we read from lastRun minus a safety margin of an hour. Rows from the overlap are read twice, so the load must be an upsert by primary key, not an insert.

SQL
-- extract with an overlap
SELECT * FROM orders
WHERE updated_at > :lastRun - interval '1 hour';
-- load idempotently into the warehouse
MERGE INTO wh.orders t
USING staging.orders s ON t.order_id = s.order_id
WHEN MATCHED AND s.updated_at >= t.updated_at THEN
UPDATE SET status = s.status, total = s.total, updated_at = s.updated_at
WHEN NOT MATCHED THEN
INSERT (order_id, status, total, updated_at) VALUES (s.order_id, s.status, s.total, s.updated_at);

The overlap must be longer than the longest transaction we can expect, and we should check that with monitoring of transaction age in the source. This reduces missing rows dramatically, but it does not fix writers that skip updated_at or capture deletes.

7Fix 2 and Fix 3: Database Timestamps and Log-Based CDC

The second fix is to make the database own the timestamp: a BEFORE UPDATE trigger sets updated_at for every writer, using the database's single clock, in timestamptz. That removes causes 2 and 3.

The most complete fix is to stop asking the table what changed and read the database's transaction log instead. Log-based change data capture, for example Debezium reading PostgreSQL's WAL through a replication slot, receives every insert, update and delete in commit order, after it commits, with no dependence on timestamps or on writers' discipline. The long transaction from cause 1 simply appears when it commits. It needs more infrastructure, typically Kafka Connect and a replication slot to monitor, which is why it is usually adopted once timestamp extracts start losing data.

SQL
CREATE OR REPLACE FUNCTION set_updated_at() RETURNS trigger AS $$
BEGIN
NEW.updated_at := clock_timestamp();
RETURN NEW;
END $$ LANGUAGE plpgsql;
CREATE TRIGGER orders_updated_at BEFORE UPDATE ON orders
FOR EACH ROW EXECUTE FUNCTION set_updated_at();

8Follow-up questions

Why not use the largest updated_at the job saw as the next mark instead of the run time?

It helps with clock differences between the job and the database, but not with long transactions: a row can still commit later with a timestamp below that maximum. Any timestamp-based mark has this gap, which is why an overlap or CDC is needed.

How would you find which orders are missing today?

Compare keys between the source and the warehouse for a period, for example by counting orders per day on both sides and then listing ids that exist only in the source for days that differ. Looking at the missing rows' updated_at and the job's run times usually confirms the long transaction pattern.

Could PostgreSQL's xmin or commit timestamps help?

With track_commit_timestamp enabled, PostgreSQL records commit times that can be queried with pg_xact_commit_timestamp, which avoids the start-versus-commit gap. It adds overhead and complexity, and wraparound of transaction ids makes xmin awkward as a long-term mark, so most teams move to log-based CDC instead.

9Conclusion

In this answer, we saw why WHERE updated_at > :lastRun misses rows. Long transactions commit after the job has moved its mark past their timestamps, some writers never update the column, clocks and time zones disagree, and deletes cannot appear in a timestamp query at all. We can reduce the gap with an overlapping window and idempotent MERGE loads, close more of it with database-generated timestamps through a trigger, and remove it fully with log-based change data capture.

What to say in the interviewAbout 80 seconds aloud

Open with

The first thing I'd suspect is long transactions. updated_at is set when the statement runs, but the row only becomes visible when the transaction commits. If a batch sets updated_at at 01:59:50 and commits at 02:00:20, the job at 02:00:00 can't see it, moves the mark to 02:00, and next night the row is older than the mark, so it's skipped forever.

Then, if they want more

That fits a pattern of a few rows every few days. Other causes I'd check: writers that don't maintain updated_at, like scripts with plain JDBC or manual fixes that bypass the ORM; clock skew or time zone mix-ups if application servers set the timestamp; and deletes, which a timestamp query can never see.

To confirm, I'd compare ids per day between source and warehouse and look at the missing rows' timestamps against job run times. For fixes, the quick one is to read with an overlap, say last run minus an hour, longer than any transaction, and load with MERGE by primary key so duplicates are harmless.

I'd also make the database own the timestamp with a trigger and timestamptz in UTC. The real fix, especially if we care about deletes, is log-based CDC like Debezium, which reads committed changes from the WAL in commit order.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "A nightly job extracts orders updated since the last run using WHERE updated_at >...": https://scrivio-interview-library.netlify.app/databases/analytics-and-cdc/4ae414acb7/