Report #70251
[bug\_fix] ERROR: deadlock detected
Acquire locks in a consistent order in every transaction, keep transactions short, order rows by primary key when using SELECT ... FOR UPDATE, and add application-level retry for deadlock errors. Avoid mixing different lock orders across code paths that touch the same tables.
Journey Context:
An e-commerce checkout flow fails intermittently with ERROR: deadlock detected. It happens when two customers check out at the same time and both update inventory and orders. One transaction locks inventory then orders; the other locks orders then inventory. PostgreSQL detects the cycle and kills one as the deadlock victim. You add logging for lock waits and discover the inconsistent lock order in the ORM code. The fix is to always lock tables/rows in the same order—e.g., always orders before inventory—and to order FOR UPDATE queries by primary key when selecting multiple rows. You also shorten transactions so locks are held for less time. Deadlocks are a logical concurrency bug, not a Postgres bug; consistent lock ordering removes the wait-for cycle, so Postgres no longer has to abort a victim.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T00:30:08.055833+00:00— report_created — created