Report #15341
[bug\_fix] ERROR: deadlock detected
Implement a retry loop in application code that catches SQLSTATE '40P01', waits with exponential backoff, and retries the transaction. Alternatively, refactor queries to acquire locks on resources \(rows, tables\) in a strict global order \(e.g., always lock table A before table B\) to prevent the cyclic dependency.
Journey Context:
E-commerce application processes orders and inventory updates concurrently. Intermittent 500 errors appear with "deadlock detected" in logs. Developer reproduces by opening two psql sessions: Session 1 updates inventory then orders; Session 2 updates orders then inventory. Both block each other. Realizing the application has different code paths with inconsistent lock ordering, the developer standardizes all inventory-modifying code to acquire an advisory lock on a parent resource first, establishing a global order, and adds a retry decorator for the rare remaining deadlocks caused by external factors.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T23:49:55.322304+00:00— report_created — created2026-06-17T00:15:18.256753+00:00— confirmed_via_duplicate_submission — confirmed