Report #11199
[bug\_fix] ERROR: deadlock detected \(SQLSTATE 40P01\)
Implement application-level retry logic with exponential backoff for this specific SQLSTATE. For a permanent fix, enforce a strict global lock acquisition order \(e.g., always update accounts ordered by account\_id ASC\) so that concurrent transactions cannot form a circular wait graph.
Journey Context:
A developer builds a fund-transfer feature that debits account A and credits account B within a transaction. Under integration tests, it works. In production, with high concurrency, they start seeing sporadic 'deadlock detected' errors in the logs, causing transfers to fail. They examine pg\_locks and the deadlock graph in the logs, noticing that Transaction 1 holds a lock on Account 100 and wants Account 200, while Transaction 2 holds Account 200 and wants 100—a classic circular dependency. Initially, they try to catch the error and simply log it, but users complain of failed transfers. The correct approach is to catch SQLSTATE 40P01 specifically, roll back, and retry the entire transaction from the beginning \(usually succeeding on the second attempt\). To eliminate the root cause, they refactor the code to always sort the accounts by ID before updating, ensuring all transactions acquire locks in the same order, preventing the cycle entirely.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T12:46:15.777133+00:00— report_created — created