INSERT … ON CONFLICT (id) DO UPDATE SET col = EXCLUDED.col makes the statement idempotent: re-run safely without duplicate rows. EXCLUDED.col is the value the conflicting insert WOULD have set. Combine with RETURNING to get the persisted row.
Ingest pipeline: insert chunks with id = checksum(chunk). ON CONFLICT (id) DO NOTHING -> idempotent re-ingest.
Profile updates: ON CONFLICT (user_id) DO UPDATE SET last_seen = EXCLUDED.last_seen.
RETURNING id: get the row id in one round-trip.
INSERT (col1, col2)
ON CONFLICT (col1) DO UPDATE SET col2 = EXCLUDED.col2
RETURNING id
ON CONFLICT is the safe primitive for any “insert but don’t duplicate” pipeline.