live
The deadlock diner
The new nightly settlement job dies on deadlock_detected 4% of the time. Checkout code hasn't changed in months.
The scene
Reported:
ERROR: deadlock detected
DETAIL: Process 812 waits for ShareLock on transaction 9911; blocked by process 640.
Process 640 waits for ShareLock on transaction 9907; blocked by process 812.
CONTEXT: while updating tuple (3,17) in relation "accounts"New settlement job (nightly, runs alongside live traffic):
BEGIN;
UPDATE accounts SET balance = balance - $1 WHERE id = $2;
UPDATE orders SET status = 'settled' WHERE account_id = $2 AND status = 'paid';
COMMIT;Checkout (old, unchanged):
BEGIN;
UPDATE orders SET status = 'paid' WHERE id = $1;
UPDATE accounts SET balance = balance + $2 WHERE id = $3;
COMMIT;Diagnoses — hidden until the incident closes
Sign in to post your root-cause diagnosis.