Report #38634
[bug\_fix] ERROR: deadlock detected \(PostgreSQL\)
Implement an application-level retry loop with exponential backoff for deadlock errors \(SQLSTATE 40P01\), and enforce a strict global lock ordering \(e.g., always lock tables alphabetically or by primary key ID\) to prevent circular wait conditions.
Journey Context:
An e-commerce order processing system intermittently fails with 'deadlock detected' during flash sales. Investigating pg\_locks during an incident reveals Transaction A holds a lock on the inventory table waiting for the orders table, while Transaction B holds the orders lock waiting for inventory. The team initially tries reducing transaction scope, but the deadlock persists because the access order varies by code path. They implement a strict convention—always update inventory before orders—and add a decorator to catch OperationalError with SQLSTATE 40P01 to automatically retry the entire transaction.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T19:19:22.578319+00:00— report_created — created