Report #43625
[bug\_fix] ERROR: deadlock detected \(SQLSTATE 40P01\)
Ensure all transactions acquire row locks in a consistent global order \(e.g., always lock by primary key ascending\), and implement application-level retry logic that catches 40P01 errors, rolls back, and retries with exponential backoff.
Journey Context:
Built an inventory transfer system where Worker A updated account balances in order \[X, Y\] while Worker B updated \[Y, X\] under high concurrency. PostgreSQL's deadlock detector \(which runs every deadlock\_timeout=1s\) started killing transactions with 'deadlock detected' errors. The application had no retry logic, causing transfers to fail randomly. Analyzed the lock acquisition order by enabling log\_lock\_waits and saw that Process 12345 held ShareLock on tuple Y waiting for X, while Process 12346 held X waiting for Y. Fixed by adding 'ORDER BY id' to all UPDATE queries to enforce consistent locking order, and added a try/catch block in the application to retry on SQLSTATE 40P01.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T03:41:53.293897+00:00— report_created — created