UPSERT inamaanisha "ingiza, au sasisha ikiwa tayari ipo." PostgreSQL inatekeleza hii kwa INSERT ... ON CONFLICT — ingiza safu, lakini ikiwa itakiuka constraint ya kipekee, badala yake sasisha safu iliyopo (au usifanye chochote). Inashughulikia hitaji la kawaida la "ingiza-au-sasisha" kwa njia ya atomic.
Tatizo ambalo UPSERT linatatua
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)
