The Hidden Deadlock in Concurrent Upserts: Why ON CONFLICT DO UPDATE Isn't Always Safe
PostgreSQL's INSERT ... ON CONFLICT DO UPDATE is commonly treated as an atomic, concurrency-safe upsert. Under high concurrent write load with multi-row batches or multiple unique indexes, it can trigger silent 40P01 deadlocks. Here is how Postgres locks index tuples and how to eliminate the deadlock.