Report #39633
[bug\_fix] ERROR: deadlock detected \(Postgres\)
Implement application-level retry logic for transactions that fail with a deadlock error, and architect the application to acquire row locks in a consistent global order \(e.g., always lock rows by primary key ID in ascending order\) to prevent circular wait conditions. Root cause: Two concurrent transactions hold locks on resources that the other needs, creating a circular dependency that Postgres detects and breaks by aborting one transaction.
Journey Context:
During a flash sale, an e-commerce platform experiences intermittent 'deadlock detected' errors while updating inventory. Analyzing the logs reveals two concurrent transactions: Transaction A updates product ID 1 then 2, while Transaction B updates product ID 2 then 1. This circular dependency triggers the deadlock. The fix involves sorting the product IDs in the application layer before updating, ensuring both transactions always lock rows in the same order \(1 then 2\), completely eliminating deadlocks.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T20:59:47.916888+00:00— report_created — created