Replace, Do Not Append
Concepts covered: Idempotency
One day, one partition
A daily batch pipeline has a natural unit of work: one run produces one day of rows. The write pattern in this section gives that unit a matching place in the table. A partition is the slice of a table that holds all the rows sharing one value of a chosen column. When daily_orders is partitioned by order_date, each day's rows sit together as one slice, and the warehouse can replace one slice without touching the others.
| Partition (order_date) | Rows | Revenue |
|---|---|---|
| 2026-03-12 | 3 | $95 |
| 2026-03-13 | 5 | $180 |
| 2026-03-14 | 4 | $150 |
After 3 clean nights the table holds 12 rows in 3 partitions, one per day.
Clear the day, then write the day
Partition overwrite is a write that replaces the entire contents of one partition with the rows the current run produced. Whatever sat in that partition before is gone afterwards, whether that was nothing, half of a failed load, a complete load, or a load computed with a bug. Its effect equals 2 statements run in order.
DELETE FROM daily_orders
WHERE order_date = DATE '2026-03-14'INSERT INTO daily_orders
SELECT
order_id,
order_date,
amount
FROM orders
WHERE order_date = DATE '2026-03-14'Many warehouses and lake tables offer overwrite as a single write option that performs both steps together. The table ends in the same state either way.
Replaying the failed night
Run the night of the dropped connection again, this time with overwrite, and follow the 2026-03-14 partition.
| Step | What happens | Rows for 2026-03-14 | Revenue shown |
|---|---|---|---|
| Attempt 1 clears | The partition is already empty | 0 | $0 |
| Attempt 1 writes | 2 orders land, then the connection drops | 2 | $75 |
| Retry clears | The 2 leftover rows are removed | 0 | $0 |
| Retry writes | All 4 orders land | 4 | $150 |
| A third run, for any reason | Clears 4 rows, writes the same 4 | 4 | $150 |
The retry never needed to know how far attempt 1 got. It emptied the partition and wrote the full day, so the leftover rows couldn't survive. Check the last 2 rows of the table against the 2-run test: 4 rows and $150 after one complete run, 4 rows and $150 after the next. The partitions for 2026-03-12 and 2026-03-13 were never touched and still hold their 8 rows.
Why this is the default for daily batch
What partition overwrite gives a daily pipeline
- No memory needed: a run does not have to know whether earlier attempts wrote nothing, some rows, or all of them.
- Matching units: one run owns one day and one day is one partition, so a run replaces only its own output.
- Other days stay safe: the clear is scoped to a single date, so rerunning one day cannot disturb another.
- Reruns repair mistakes: after a bug in the task is fixed, rerunning 2026-03-14 swaps that day's wrong rows for right ones.
Overwrite has a cost. It rewrites the whole day even when a single order changed, so it does more writing than an append. For a daily batch table the trade is usually worth making: one day of rows is a bounded amount of work, and a table that can be rerun without inspection saves far more time than the extra writes cost.
- Do
- Partition a daily table by the date each run is responsible for.
- Replace the whole partition on every run, including the first.
- Scope the clear and the write to the same single date.
- Avoid
- Append to a table that a retry or a rerun can reach.
- Skip the load when rows already exist: a half-loaded day passes that check.
- Clear more than the run is about to write back.
daily_orders holds 3 partitions: 2026-03-12 with 3 rows, 2026-03-13 with 5 rows, and 2026-03-14 with 4 rows. A pricing bug is fixed and the task for 2026-03-14 is rerun with partition overwrite, producing 4 corrected rows. What does the table hold afterwards?
- 16 rows: the 12 already there, plus 4 corrected rows added to the 2026-03-14 partition.
- 12 rows: 3 and 5 in the untouched partitions, and 4 corrected rows in place of the old 4.
- 4 rows: the overwrite clears the whole table, then writes the corrected rows for 2026-03-14.
- 8 rows: the 2026-03-14 partition is cleared, and its corrected rows arrive on the next night.
Overwrite takes the question 'what did the last attempt leave behind?' and makes its answer irrelevant. That matters most at the hours when nobody is awake to answer it.
About this lesson
This section is part of the Idempotent Reruns: Beginner lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.