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)
