A job fails halfway and gets retried, or someone backfills a day. Whether that's safe depends on how the load writes. I ran a small test (SQLite 3.53.1, so only an illustration) with the same scenario for three strategies.
Source data for 2026-11-01: three rows with amounts 10, 20, 30 (sum 60). Later the source is corrected: id 2 becomes 25, and id 3 is removed.
A. Plain append, run twice:
(6, 120) -- rows, sum
Every retry duplicates the data. Anything downstream that sums or counts is now wrong.
B. Upsert by primary key (INSERT ... ON CONFLICT(id) DO UPDATE):
run twice: (3, 60)
after source fix (id3 gone): (3, 65)
Safe to re-run, and id 2 is updated. But id 3 is still there, since an upsert never deletes. The total is now 65 when the source says 35.
C. Partition overwrite (delete the day's rows, then insert, in one transaction):
run twice: (3, 60)
after source fix: (2, 35)
Re-runs are safe, and the result matches the corrected source, including the deletion.
def load(rows, day):
with db: # one transaction
db.execute("delete from t where day=?", (day,))
db.executemany("insert into t values(?,?,?)", rows)
What this does not show, and where I'd be careful:
- Overwrite needs a natural partition (a day, a batch id). If there isn't one, it's harder.
- The delete+insert must be atomic. If your engine or tool doesn't give you a transaction (or an equivalent like swapping a partition), a failure between the two steps leaves the day empty.
- Upsert can be right when the source never deletes, or when you handle deletes explicitly (soft-delete flags, tombstones, a separate reconciliation step).
- This is a toy in one database. Warehouses and lake tables have their own mechanics (MERGE, partition overwrite, snapshots), so check yours.
My takeaway: decide how a re-run behaves before the first failure, and test it by running the job twice on purpose.
How do you handle deletes at the source in an upsert-based pipeline? And do you have a standard test, like "run twice and compare counts"?