Three Idempotent Write Patterns

The beginner lesson closed on a single rule: replace, don't append. Which way to replace depends on the destination table's shape: is it partitioned by the column that scopes the run, keyed by an entity ID, or neither? It also depends on a single run's unit of work, which is either a complete day of data or a batch of rows that changed since the previous run.

Partition overwrite

Use it when the destination is partitioned by the same column that scopes the run, usually a date column ds, and each run computes the full contents of a single partition. The write swaps the old partition for the new one as a unit. The run for ds = '2026-03-14' produces 41,200 rows. A retry produces the same 41,200 rows and installs them in place of the first attempt's. Every other partition stays as it was.

INSERT INTO fact_bookings
SELECT
    booking_code,
    listing_id,
    guest_id,
    amount_usd,
    ds
FROM staging_bookings
WHERE ds = '2026-03-14'

It is safe only when the run holds the whole partition. Suppose a run carries only the 300 bookings that changed since yesterday. Overwriting with those 300 erases the other 40,900. The price is rewriting all 41,200 rows even when 3 of them changed. That's fine for a daily fact table and wasteful for a large table where a handful of rows move each hour.

MERGE on a key

Use it when the destination holds a row per entity and those rows change over time, such as dim_listing with a row per listing_id. The run's unit is the set of listings that changed. MERGE joins incoming rows to the table on the key. A matched key updates its row, and an unmatched key inserts a new row. Replaying the same 500 changes leaves the table identical, for 2 reasons. Setting a column to the value it already holds changes nothing. And every key now exists, so no insert fires.

MERGE INTO dim_listing AS t
USING listing_changes AS s
ON t.listing_id = s.listing_id
WHEN MATCHED THEN
    UPDATE SET
        nightly_price = s.nightly_price,
        status = s.status
WHEN NOT MATCHED THEN
    INSERT (listing_id, nightly_price, status)
    VALUES (s.listing_id, s.nightly_price, s.status)

It is safe only when the key is unique in both the table and the incoming batch, which is the subject of the next section. The price is a join between the batch and the target on every run, even when the batch is small.

An hourly job reads the bookings that changed in the last hour. It writes them to fact_bookings, which is partitioned by ds, using partition overwrite. What does each run do to the partitions it touches?

DELETE-then-INSERT

Use it when a run owns a set of rows you can name with a predicate, but that set does not line up with a partition. fact_payouts is partitioned by payout_month because readers query by month, while the pipeline runs daily. Overwriting the March partition from a daily run would erase the other 30 days of March. Instead, the run deletes the rows it owns, those whose payout_date is its day, and inserts the recomputed rows. A retry deletes the same set, including anything the failed attempt left behind, and inserts it again.

DELETE FROM fact_payouts
WHERE payout_date = DATE '2026-03-14'
INSERT INTO fact_payouts
SELECT
    payout_id,
    host_id,
    amount_usd,
    payout_date,
    payout_month
FROM staging_payouts
WHERE payout_date = DATE '2026-03-14'

Two conditions keep it safe. First, the DELETE predicate and the INSERT filter must name the same rows. Say the delete matches on payout_date, the insert on created_at, and a payout is created March 14, dated March 15. Every run of March 14 inserts another copy its delete never matches. The March 15 run deletes every copy, inserts none back, and the payout vanishes. Second, a failure between the 2 statements leaves March 14 empty until the rerun, which the last section addresses.

PatternDestination shapeUnit of workCost per runBreaks when
Partition overwritePartitioned by the run's dateA complete partitionRewrites the whole partitionThe run holds part of a partition
MERGEA row per entity keyChanged rowsJoins batch to targetThe key repeats in source or target
DELETE-then-INSERTPartitioned coarser than the run, or not at allRows matching a predicateDelete scan plus insertDelete and insert filters disagree

A daily run computes a day's host earnings. host_earnings holds a row per host per day and is partitioned by earnings_month. Which write keeps a rerun of a single day idempotent?

About this lesson

This section is part of the Idempotent Reruns: Intermediate 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.