UPSERT એટલે "insert કરો, અથવા જો તે પહેલેથી અસ્તિત્વમાં હોય તો update કરો." PostgreSQL આને INSERT ... ON CONFLICT વડે અમલમાં મૂકે છે — એક row insert કરો, પરંતુ જો તે unique constraint નું ઉલ્લંઘન કરે છે તો તેના બદલે હાલની row ને update કરો (અથવા કશું કરશો નહીં). તે સામાન્ય "insert-or-update" જરૂરિયાત ને atomically હેન્ડલ કરે છે.
UPSERT જે સમસ્યા સુલझાવે છે
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)
