Report #9927
[bug\_fix] ERROR: deadlock detected
Catch SQLSTATE 40P01 in application code and automatically retry the transaction with exponential backoff. Prevention: ensure all transactions acquire row locks in a consistent global order \(e.g., sort record IDs before updating\). Root cause: circular wait between two transactions where each holds a lock the other needs.
Journey Context:
Your e-commerce app deadlocks during Black Friday. Logs show ERROR: deadlock detected with a detail showing Process 12345 waits for ShareLock on transaction 67890. You analyze the trace: Transaction A updates inventory for product\_id=5 then product\_id=10, while Transaction B updates product\_id=10 then product\_id=5. When they intersect, PostgreSQL detects the cycle and aborts one. You refactor the inventory service to always sort product IDs ascending before executing updates, ensuring a global lock order. You also add a @retry\_on\_deadlock decorator that catches 40P01 and retries. Deadlocks drop to zero.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T09:22:38.896349+00:00— report_created — created