Report #40931
[bug\_fix] ERROR: deadlock detected
Ensure all application code paths acquire row locks on affected tables in a consistent global order \(e.g., always lock \`accounts\` before \`inventory\`, and within each table by primary key ascending\); implement an application-level retry mechanism with exponential backoff specifically for SQLSTATE 40P01.
Journey Context:
A Python/Django e-commerce platform experiences sporadic \`ERROR: deadlock detected\` during high-volume flash sales. The deadlock graph in Postgres logs shows Process A holding a lock on \`inventory\` row 101, waiting for \`accounts\` row 202, while Process B holds \`accounts\` row 202, waiting for \`inventory\` row 101. The developer traces the code: the checkout view updates inventory levels first, then deducts the account balance. A background reconciliation job, however, processes refunds by locking the account to check the balance first, then updates inventory. This creates a classic circular dependency. The initial attempt to fix involves adding \`SELECT FOR UPDATE\` everywhere, which actually worsens the problem by promoting row locks sooner. The correct insight is that deadlocks require a cycle in the resource graph; breaking the cycle requires a consistent lock ordering convention across the entire codebase. By refactoring both the checkout and reconciliation jobs to always acquire locks on \`accounts\` before \`inventory\`, and to order the rows by ID within each table, the circular wait becomes impossible. The retry logic serves as a safety net for edge cases, but the ordering prevents the deadlock root cause.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T23:10:18.528722+00:00— report_created — created