UPSERT significa "insertar, o actualizar si ya existe". PostgreSQL implementa esto con INSERT ... ON CONFLICT — inserta una fila, pero si violaría una restricción única, en su lugar actualiza la fila existente (o no hace nada). Maneja la necesidad común de "insertar-o-actualizar" de forma atómica.
Por qué es importante
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)
