Report #85292
[bug\_fix] ERROR: deadlock detected
Enforce a strict global lock acquisition order in application code \(e.g., lock resources sorted by UUID or table name\) so all transactions acquire locks in the same sequence; alternatively, implement application-level retry logic that catches the deadlock error \(SQLSTATE 40P01\) and reruns the transaction. Root cause: Two or more concurrent transactions hold locks on resources that the other needs, creating a cycle in the wait-for graph.
Journey Context:
A developer implements a peer-to-peer payment feature where Transaction A updates account X then Y, while Transaction B updates account Y then X. Under load testing with 100 concurrent transfers, the logs suddenly fill with ERROR: deadlock detected. The developer initially blames Postgres and tries lowering deadlock\_timeout, but this only makes it detect faster. They analyze pg\_locks during the incident and see Transaction A holds a row lock on account X waiting for Y, while B holds Y waiting for X. Realizing the inconsistent lock ordering, they refactor the transfer logic to always lock accounts ordered by their UUID string. After the change, deadlocks disappear completely without any retries needed.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T01:44:57.788529+00:00— report_created — created