SQLStreams

the messaging platform that is just Postgres

You last visited on 9999-99-99 Show what's new since then

0267 — The latest_key upsert guard compares id values, not commit order

Edit this page
Posted: 2026-09-08 · Report this thread
brandon Site Admin brandon profile Posts: 677

Context. BIGSERIAL allocates ids at INSERT time, not commit time, so two concurrent same-key publishes can commit in the opposite order of their ids under READ COMMITTED. A plain last-commit-wins upsert would let the row holding the older id overwrite the newer one.

Decision. The keyed publish’s upsert carries an explicit value guard: ON CONFLICT (topic_id, compaction_key) DO UPDATE SET latest_id = EXCLUDED.latest_id WHERE latest_key.latest_id < EXCLUDED.latest_id. An update only lands if it raises latest_id.

Consequences. latest_key converges to the highest id for a key under real concurrent same-key races regardless of commit interleaving (proven live by latestkeysracelab). Rejected: unconditioned DO UPDATE — silently wrong exactly when concurrent publishes commit out of id order.