Why the cursor is not the state#
The fixture is a table t with 9,000 rows whose IDs, up to 10,000, have gaps. A backfill copies v into v2 in ID order and has finished up to 5,000. Then two ordinary things happen: a row with ID 10 arrives, and an old release changes v on row 101.
SELECT count(*) FILTER (WHERE v2 IS NULL AND id <= 5000) AS below,
count(*) FILTER (WHERE v2 IS NULL AND id > 5000) AS above,
count(*) FILTER (WHERE v2 IS NOT NULL AND v2 IS DISTINCT FROM v) AS different
FROM t;
-- below | above | different
-- 1 | 4500 | 1
A backfill that resumes at 5,001 leaves both rows wrong and then reports that it is complete. Run with PostgreSQL 18.
A row can appear below the cursor when a sequence value is taken early and committed late, when IDs are assigned by clients, or when a row is restored. A done row goes stale when something still writes only the old column.
Read four counts before you resume#
| Count | Query | Meaning |
|---|---|---|
| Remaining above the cursor | v2 IS NULL AND id > C | Normal work |
| Remaining below | v2 IS NULL AND id <= C | Gaps. Find out why before resuming |
| Different | v2 IS DISTINCT FROM v where v2 is set | A writer that does not know the new column |
| Legitimately empty | v IS NULL | Must not be counted as remaining |
The last one depends on the column's meaning. If the source can be NULL, "is NULL" does not mean "not done", and the count never reaches zero. Use a separate marker, or compare with IS DISTINCT FROM as the reconciliation guide shows.
Run the four inside one REPEATABLE READ transaction, so that they describe the same snapshot. On a large table that is a long read, which has costs of its own for vacuum; use a replica or sample by ID range if that is a problem.
Find out what is still running#
The previous owner's process may not have stopped. Look before you start a second one:
SELECT pid, application_name, state, xact_start, left(query, 80)
FROM pg_stat_activity
WHERE query ILIKE '%update t set v2%';
Your own query appears in the result. An idle gap between batches shows no match, so check the scheduler too. Two workers are harmless only if each batch claims its rows.
The note to leave#
Backfill
t.v2, jobbf-12, code at4c1e9aa. Stopped 09:40 UTC after the batch ending at ID 5000, committed. Batch size 500, pause 200 ms, chosen because replica lag rose above 5 s at 2,000. New release writes both columns since 08:10 UTC. Below the cursor: 1 remaining, not explained. Stop: setbackfill.enabledto false. Resume:bf run --after 5000, and the gaps separately.
The batch size without its reason gets raised by the next person in a hurry.
Worked example#
This example is fictional. Ben inherits that note. He looks at the one row below the cursor, and finds that a partner import still runs the old code. Resuming would finish the backfill and leave the import producing gaps. He deploys the import first, runs the gap query until it stays at zero for a day of imports, and then resumes. The constraint that the backfill prepares is added last.
If the job's log ends early, its buffer may be missing. Return to handing over evidence.