If I saw this query in a pull request, my first question would be “When do we save the checkpoint?”
SELECT id, order_ref FROM order_events WHERE id > $1 ORDER BY id LIMIT 100; The worker sends a batch of orders to a reporting database, waits for the write to succeed, then saves the last ID it sent. Next time around, it picks up from there.
“After the write succeeds” sounds like a good answer. I could easily move on to the next file. But there’s another question worth asking. When we save ID 2, how do we know we’ve already seen ID 1?
Two terminals are enough to see it
Open two psql sessions against the same database. In terminal A, create this table with autocommit on.
CREATE TABLE order_events ( id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY, order_ref text NOT NULL, exported_at timestamptz ); Now start a transaction in A and leave it open after the insert.
BEGIN; INSERT INTO order_events (order_ref) VALUES ('order-A') RETURNING id; -- 1 Switch to terminal B. Leave autocommit on, insert a second order, then run the query the worker would run on its first poll.
INSERT INTO order_events (order_ref) VALUES ('order-B') RETURNING id; -- 2 SELECT id, order_ref FROM order_events WHERE id > 0 ORDER BY id; -- 2 | order-B That’s the whole batch. The worker sends order-B and saves 2.
Now go back to A and run COMMIT. If you open the table, you’ll see both orders. If you run the worker’s next query, WHERE id > 2, you’ll get neither. Restarting the worker just gives you a fresh process reading the same checkpoint.
Both inserts succeed. The worker succeeds too. The mistake is assuming that a higher ID means every lower ID has already committed. Nobody lost a connection. Both writes succeeded. Postgres assigned 1 first, but the transaction holding 2 committed first. Under the default READ COMMITTED isolation level, that was the only order the worker could see when it polled.
What I’d put in the pull request
For this single-worker export, I’d track which rows still need to reach the report.
SELECT id, order_ref FROM order_events WHERE exported_at IS NULL ORDER BY id LIMIT 100; I’d keep ORDER BY id. It’s handy for choosing a batch. The difference is that when order-A finally commits, it still has exported_at IS NULL, so the next poll picks it up.
That leaves one awkward moment. The reporting database accepts the order, then the worker dies before it can set exported_at. On restart it sends the order again. I’d give the reporting table a unique constraint on source_event_id and use ON CONFLICT (source_event_id) DO NOTHING. Then it’s safe to retry that insert and mark the source row as exported after the reporting transaction commits.
The test I’d want in that PR is almost exactly the terminal exercise. Hold A open, commit B, run the worker, commit A, run it again. Then check that the report contains both orders exactly once.
You could insert ten thousand orders one after another and miss this bug. Two orders are enough, as long as you let the second one finish first.