Diagnosing Production Deadlocks in PostgreSQL
ERROR: deadlock detected confirms a deadlock, not its cause. How to trace the sessions involved with pg_stat_activity and pg_blocking_pids(), why a retry is not the fix, and how a consistent lock order removes the circular wait.

