UPSERT అంటే "insert చేయండి, లేదా అది ఇప్పటికే ఉనికిలో ఉంటే update చేయండి." PostgreSQL ఇందుకు INSERT ... ON CONFLICT తో ఆచరణ చేస్తుంది — ఒక row insert చేయండి, కానీ అది unique constraint ను ఉల్లంఘిస్తే, బదులుగా existing row ని update చేయండి (లేదా ఏమీ చేయవద్దు). ఇది సాధారణ "insert-or-update" అవసరాన్ని అణువుగా నిర్వహిస్తుంది.
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)
