Reads Can Be Non-Idempotent Too
A partition overwrite or a MERGE guarantees that the write replaces. It says nothing about what it replaces the data with. If the query returns different rows when it runs again, the rerun faithfully installs a different answer. A run is idempotent only when its inputs are fixed, and the most common moving input is the clock.
SELECT
booking_code,
amount_usd,
booked_at
FROM raw_bookings
WHERE booked_at >= current_timestamp - INTERVAL '1' DAYThe scheduled run starts at 02:00 on March 15 and covers the day leading up to that moment. It fails, and the on-call engineer retries it at 09:40. The retry's window slides forward with it. It drops the early morning of March 14 and picks up the morning of March 15, which belongs to the next run.
- 7h 40m
The fix is to hand the run its time bounds. Each scheduled run has a logical date, which is the period the run is responsible for. The logical date is fixed when the run is created and is unrelated to when the run actually starts. The orchestrator passes the start and end of that period into the run, and the query filters on them. The run for March 14 then reads March 14 whether it executes at 02:00, at 09:40 or months later.
SELECT
booking_code,
amount_usd,
booked_at
FROM raw_bookings
WHERE booked_at >= TIMESTAMP '2026-03-14 00:00:00'
AND booked_at < TIMESTAMP '2026-03-15 00:00:00'This job writes yesterday's bookings per listing into a ds partition. It always reads whole days, yet a recomputation of last month comes out wrong. Which change fixes it?
SELECT
listing_id,
count(*) AS bookings
FROM raw_bookings
WHERE booked_at >= date_trunc('day', current_timestamp - INTERVAL '1' DAY)
AND booked_at < date_trunc('day', current_timestamp)
GROUP BY listing_id- Replace both current_timestamp bounds with the run's logical-date bounds
- Truncate to the hour instead of the day for finer windows
- Filter on landed_at instead of booked_at
- Switch the write from partition overwrite to MERGE
Event time and processing time
A booking row usually carries 2 timestamps. Event time is when the thing happened in the world: the guest pressed Book at 23:58 on March 14. Processing time is when the pipeline received or handled the row: it landed in the raw table at 00:07 on March 15. The 2 timestamps differ by seconds on a good day. They differ by hours when a phone was offline or an upstream export ran late.
| booking_code | booked_at (event) | landed_at (processing) | Day by event time | Day by processing time |
|---|---|---|---|---|
| HMX41 | 2026-03-14 10:12 | 2026-03-14 10:12 | March 14 | March 14 |
| HMX57 | 2026-03-14 23:58 | 2026-03-15 00:07 | March 14 | March 15 |
| HMX63 | 2026-03-14 18:30 | 2026-03-15 06:45 | March 14 | March 15 |
A report of bookings made on March 14 means event time, so it groups on booked_at. Grouping on landed_at would credit HMX57 and HMX63 to March 15, and the totals would shift with every slow export. Selecting rows is a second choice. Event-time bounds, as in the query above, read March 14's raw rows, late arrivals included. landed_at bounds read only rows new since the last run, but those can span several event days, so the run must rewrite each event-time partition they touch.
Rerun the March 14 event-time partition a week later, and it may hold 3 more bookings, because rows that landed late are now in the raw table. That is correct, because idempotency means the same inputs give the same output. When more facts about March 14 exist, the rerun reflects them, and running it twice more changes nothing further.
Finance asks for the number of bookings made per day. A transform groups by date(landed_at). On the night an upstream export runs 6 hours late, what happens to the totals?
- Nothing changes, since every booking lands eventually
- Both days come up short, since late rows are dropped
- One day is low and the next day is high by the same bookings
- The run fails, since landed_at is later than booked_at
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.