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)
