Sub topic 11
Preventing older source data from overwriting newer data
This chapter assumes you can write a PostgreSQL INSERT ... ON CONFLICT DO UPDATE keyed on an external ID, and that your source system gives each record a last-modified timestamp. It builds on the upsert statement from earlier chapters.
The problem: records arrive out of order
A plain upsert always applies the incoming row. That is wrong when loads can arrive out of order. Suppose a retry replays an export from Monday after Tuesday's export has already been loaded. The Monday data overwrites the newer Tuesday data, and the table now holds old values with no error to warn you.
The fix is a stale guard. The update runs only when the incoming record is newer than the stored one.
Store the source's timestamp
The guard needs the time the source last changed the record, not the time your pipeline loaded it. Add a column for it:
ALTER TABLE customers ADD COLUMN source_updated_at timestamptz;Use the source's own clock value. The load time would make every replay look new.
Add a WHERE clause to DO UPDATE
PostgreSQL lets DO UPDATE carry a WHERE condition. If the condition is false for the conflicting row, the update is skipped and the stored row stays as it is.
INSERT INTO customers (source_system, external_id, name, email, source_updated_at)
VALUES ('crm', 'C-1001', 'Ada King', '[email protected]', '2026-11-30T12:00:00Z')
ON CONFLICT (source_system, external_id)
DO UPDATE SET name = EXCLUDED.name,
email = EXCLUDED.email,
source_updated_at = EXCLUDED.source_updated_at
WHERE customers.source_updated_at IS NULL
OR customers.source_updated_at < EXCLUDED.source_updated_at;In the WHERE clause, customers.source_updated_at is the stored value and EXCLUDED.source_updated_at is the incoming one. The IS NULL branch lets rows loaded before the column existed be updated once. Without it, a comparison against NULL is never true, and those rows would stay frozen.
What the guard changes
The guard changes three things you may rely on elsewhere in the pipeline.
- Idempotency holds. Replaying the same record has an equal timestamp, so the
<comparison is false and nothing changes. - Ties are skipped. With
<, a record with the same timestamp but different content does not apply. Use<=if you want the last-arriving record to win on ties. RETURNING idcan return no row. A skipped update produces no row, so code that expects an id from every upsert must handle an empty result. If you need the id regardless, look it up with a separateSELECTon the external ID.
Choosing the comparison column
| Source provides | Compare on | Note |
|---|---|---|
| Last-modified timestamp | `source_updated_at` | Watch for time zones and tie handling |
| Monotonic version number | A `source_version` integer | Avoids clock problems |
| Neither | Load order only | No stale protection. Process files in order. |
Common errors
- Storing the load time in the guard column, so every replay looks newer.
- Forgetting the
IS NULLcase, which freezes old rows. - Writing
customers.source_updated_atwithout the table name insideDO UPDATE, which fails with an ambiguous column reference. - Treating a skipped update as a failure. It is the intended result.
Exercise
A batch replay contains record C-1001 with sourceupdatedat = 2026-11-29T08:00:00Z. The stored row has 2026-11-30T12:00:00Z. The upsert uses the guard from this chapter. What happens to the row, and what does RETURNING id return?
Show answer
The row is not changed, because the stored timestamp is newer, so the WHERE condition is false. RETURNING id returns no row, so the calling code must treat an empty result as "skipped", not as an error.