Report #70754
[bug\_fix] ERROR: deadlock detected \(Postgres explicit locking conflict\)
Implement application-level retry logic with exponential backoff for transactions that fail with SQLSTATE 40P01. Alternatively, ensure all transactions lock rows in a consistent order \(e.g., by primary key ascending\) to prevent the circular wait condition.
Journey Context:
High-volume API endpoint updating inventory and user balance concurrently. Logs showed ERROR: deadlock detected, Process 12345 waits for ShareLock on transaction 678. Investigation of pg\_locks revealed two concurrent transactions: Transaction A locked user\_id=5 then tried to lock inventory\_id=10; Transaction B locked inventory\_id=10 then tried to lock user\_id=5. Postgres detected the cycle and aborted one. Implemented a retry decorator in the ORM that catches DeadlockDetected and retries up to 3 times with exponential backoff, eliminating user-facing errors.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T01:20:18.525852+00:00— report_created — created