Report #78857
[bug\_fix] ERROR: deadlock detected \(SQLSTATE 40P01\)
Retry the failed transaction immediately \(exponential backoff\), or refactor code to always acquire locks in the same order \(e.g., always update table A before table B\). Root cause is circular wait where Transaction 1 holds lock on Row X and wants Row Y, while Transaction 2 holds Row Y and wants X.
Journey Context:
High-concurrency e-commerce checkout suddenly failing with deadlock errors. Logs showed two concurrent requests updating inventory and order tables in opposite orders. Request A updated inventory then orders; Request B updated orders then inventory. When they hit the same SKUs simultaneously, PostgreSQL detected the cycle and killed one transaction. Fixed by wrapping updates in a retry loop \(3 attempts\) and reordering all inventory operations to happen before order inserts.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T14:57:09.909516+00:00— report_created — created