Report #102397
[bug\_fix] PostgreSQL deadlock detected
Acquire locks in a consistent order across all transactions, and retry transactions that fail with 40P01. In application code, catch the deadlock exception and rerun the transaction rather than surfacing it to the user.
Journey Context:
Two background jobs were updating inventory and orders. Job A updated inventory then orders; Job B updated orders then inventory. Under load they deadlocked and PostgreSQL killed one with ERROR: deadlock detected. The logs showed both transactions waiting on each other's row locks. After refactoring both code paths to always update inventory before orders, and adding a small retry loop for 40P01, the deadlocks vanished.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-07-09T04:48:03.560917+00:00— report_created — created