Report #74822
[bug\_fix] ERROR: deadlock detected
Enforce a strict global ordering when acquiring row locks \(e.g., always update inventory items ordered by primary key ASC\), or use SELECT FOR UPDATE SKIP LOCKED to avoid contention. If deadlocks occur, catch SQLSTATE 40P01 and retry the transaction. Root cause is circular wait-for dependencies between transactions holding and requesting row locks.
Journey Context:
During a flash sale, the Django application logs show 'ERROR: deadlock detected' on the checkout endpoint. The deadlock graph in Postgres logs shows Process 123 locks row \(1,2\) then waits for row \(3,4\), while Process 456 locks row \(3,4\) then waits for \(1,2\). Code review reveals two different checkout paths: one deducts inventory in item ID order, the other deducts in the order the user added to cart. This creates a cycle when two users buy overlapping items in different orders. The fix requires canonicalizing the update order or implementing idempotent retry logic.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T08:11:07.833356+00:00— report_created — created