UPSERT betyder "indsæt, eller opdater hvis det allerede findes." PostgreSQL implementerer dette med INSERT ... ON CONFLICT — indsæt en række, men hvis det ville krænke en unik constraint, opdater i stedet den eksisterende række (eller gør ingenting). Det håndterer det almindelige "indsæt-eller-opdater"-behov atomart.
Problemet UPSERT løser
You want to insert a row, but it might already exist (by a unique key):
❌ a plain INSERT fails with a unique-violation error if it exists
❌ check-then-insert (SELECT, then INSERT or UPDATE) has a RACE CONDITION
(another transaction could insert between your check and insert)
→ ON CONFLICT does it atomically in ONE statement (no race condition)
