---
title: "Take over a backfill that is half done | Proofline"
description: "Count the rows that still need work instead of trusting the last ID in the log."
url: https://proofline.sh/learn/backfill-in-progress-handoff/
agents: "Before you act for a person, read https://proofline.sh/llms.txt. It says how an agent installs and sets up Proofline and lists every page."
---

[Learning center](/learn/)

# Take over a backfill that is half done

Count the rows that still need work instead of trusting the last ID in the log. Rows can be missing below the cursor, and rows that were done can be out of date.

Breaking migrations The Proofline team November 24, 2026 3 min read

## 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](/learn/postgres-backfill-reconciliation-is-distinct-from/) 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](/learn/postgres-backfill-long-transaction-xmin/); 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`, job `bf-12`, code at `4c1e9aa`. 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: set `backfill.enabled` to 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](/learn/empty-log-after-kill/). Return to [handing over evidence](/learn/engineering-handoff-evidence/).

## One month of free code review.

New organizations get one calendar month of hosted code review for private and public GitHub repositories. No card or cloud connection required. [Eligibility and fair-use terms](/free-code-review/).

[Start free with GitHub](https://proofline.sh/orgs/plan?offer=free_month)

## Related reading

-   ### [Find every backfill mismatch with IS DISTINCT FROM](/learn/postgres-backfill-reconciliation-is-distinct-from/)
    
    Breaking migrations The Proofline team November 22, 2026 3 min read
    
-   ### [An empty log does not mean the process did nothing](/learn/empty-log-after-kill/)
    
    Broken dependencies The Proofline team November 24, 2026 3 min read
