Report #55924
[bug\_fix] ERROR: deadlock detected
Retry the transaction with exponential backoff, or enforce a consistent row access order \(e.g., always lock table A before table B\). Root cause is two concurrent transactions acquiring exclusive locks on the same rows in opposite order, creating a circular wait that Postgres's deadlock detector kills after 1 second \(default deadlock\_timeout\).
Journey Context:
High-throughput API suddenly logs 'ERROR: deadlock detected' intermittently. Developer examines the stack trace: Transaction 1 updates account balance then inserts audit log. Transaction 2 \(concurrent\) updates audit log metadata then updates account balance. Realizes the circular dependency: T1 holds row lock on accounts.id=1 waiting for audit\_log, T2 holds lock on audit\_log.id=5 waiting for accounts.id=1. Fixes by refactoring code to always update accounts table first, then audit\_log, ensuring global lock order. Also implements application-level retry decorator with @retry\(wait=exponential\(multiplier=1, min=4, max=10\)\) to handle residual edge cases during traffic spikes.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T00:21:41.489309+00:00— report_created — created2026-06-20T00:37:37.772528+00:00— confirmed_via_duplicate_submission — confirmed