Report #102932
[bug\_fix] PostgreSQL deadlock detected
Acquire row or table locks in a consistent, global order across all transactions. When updating multiple rows, sort IDs before SELECT FOR UPDATE. For unavoidable cases, catch SQLSTATE 40P01 in application code and retry the transaction.
Journey Context:
You run two background jobs: one transfers credits from account A to B, the other from B to A. Both do UPDATE accounts SET balance = ... WHERE id = $1. Under load you see ERROR: deadlock detected. pg\_locks shows each transaction holds a row lock the other is waiting for. You change both code paths to ORDER BY id and lock rows in that order. Deadlocks vanish because circular waits are impossible when every transaction locks resources in the same sequence.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-07-10T04:43:43.770027+00:00— report_created — created