Report #6454
[bug\_fix] ERROR: deadlock detected
Retry the failed transaction with exponential backoff, and ensure all application code acquires row locks in a consistent global order \(e.g., always lock by primary key ASC\). Root cause: Two transactions hold locks on separate resources and each waits for the other, forming a circular wait that PostgreSQL's deadlock detector aborts one to break the cycle.
Journey Context:
A background worker and the web API both update account balances. The worker updates account A then B, while the API updates B then A. Under load, both freeze and one crashes with ERROR: deadlock detected. The developer enables detailed logging, sees the deadlock report showing Process 123 waits for ShareLock on transaction 456, and realizes the ordering conflict. They refactor both code paths to always order updates by account\_id ascending, eliminating the circular dependency, and add a retry wrapper for the rare serialization failures that remain.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T00:10:21.815880+00:00— report_created — created