UPSERT pomeni »vstavi ali posodobi, če že obstaja«. PostgreSQL to izvaja z INSERT ... ON CONFLICT — vstaviš vrstico, vendar če bi kršila omejitev edinstvenosti, namesto tega posodobiš obstoječo vrstico (ali ne narediš ničesar). Atomsko obravnava pogosto potrebo »vstavi ali posodobi«.
Problem, ki ga UPSERT rešuje
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)
