Report #86970
[bug\_fix] ERROR: deadlock detected
Acquire locks on multiple rows, tables, or resources in the same order everywhere in the application. Keep transactions short, avoid user interaction inside transactions, and add an application-level retry loop with exponential backoff for deadlock-detected errors.
Journey Context:
Two concurrent checkout flows update inventory and order totals in opposite order: transaction A locks product row 7 then tries to lock product row 9, while transaction B locks row 9 then tries to lock row 7. PostgreSQL waits briefly, detects the circular dependency, and aborts one transaction with 'ERROR: deadlock detected'. The logs show the two conflicting PIDs and queries. The developer first tries increasing lock\_timeout, but that just fails faster instead of preventing the cycle. The permanent fix is to make every code path lock rows in a consistent order \(e.g., by ascending product id\) and to wrap the operation in a small retry loop, because deadlock detection is correct behavior when lock ordering is inconsistent.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T04:33:52.733558+00:00— report_created — created