UPSERT means "insert, or update if it already exists." PostgreSQL implements this with INSERT ... ON CONFLICT — insert a row, but if it would violate a unique constraint, instead update the existing row (or do nothing). It handles the common "insert-or-update" need atomically.
The problem UPSERT solves
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)
