Lab-verifiedReproduced and fixed on PostgreSQL 17.12 · 2026-11-30 · container image postgres:17
A deadlock means transaction A waits for a lock held by B while B waits for a lock held by A. PostgreSQL detects this and fails one of them with SQLSTATE 40P01.
In both experiments the error went to session B, the session that closed the loop, and the DETAIL lines named both processes: Process 623 waits for ShareLock on transaction 4818; blocked by process 615. and the reverse. Right after, session A's waiting UPDATE finished. The aborted transaction has to be rolled back and run again.
01Two sessions update the same rows in opposite order
A money transfer: session A updated account 1 then account 2, session B updated account 2 then account 1. A's second UPDATE waited for B; when B asked for account 1, B got ERROR: 40P01: deadlock detected with the two processes in DETAIL and CONTEXT: while updating tuple (0,1) in relation "accounts". A's UPDATE then completed.
The fix is to lock rows in the same order everywhere. In the retry both sessions started with account 1: B waited for A's COMMIT and then continued. Both transfers committed and the balances became 950 and 1050.
Setup
CREATE TABLE accounts (account_id int PRIMARY KEY, balance int);
INSERT INTO accounts VALUES (1, 1000), (2, 1000);CREATE TABLE
INSERT 0 2BEGIN;
UPDATE accounts SET balance = balance - 100 WHERE account_id = 1;BEGIN
UPDATE 1BEGIN;
UPDATE accounts SET balance = balance - 50 WHERE account_id = 2;BEGIN
UPDATE 1UPDATE accounts SET balance = balance + 100 WHERE account_id = 2;UPDATE accounts SET balance = balance + 50 WHERE account_id = 1;ERROR: 40P01: deadlock detected
DETAIL: Process 2457 waits for ShareLock on transaction 6629; blocked by process 2449.
Process 2449 waits for ShareLock on transaction 6630; blocked by process 2457.
HINT: See server log for query details.
CONTEXT: while updating tuple (0,1) in relation "accounts"UPDATE 1Sorting keys (for example by primary key) before updating them removes most deadlocks in batch jobs and transfers.
BEGIN;
UPDATE accounts SET balance = balance - 100 WHERE account_id = 1;BEGIN
UPDATE 1BEGIN;
UPDATE accounts SET balance = balance + 50 WHERE account_id = 1;UPDATE accounts SET balance = balance + 100 WHERE account_id = 2;
COMMIT;UPDATE 1
COMMITBEGIN
UPDATE 1UPDATE accounts SET balance = balance - 50 WHERE account_id = 2;
COMMIT;UPDATE 1
COMMITSELECT account_id, balance FROM accounts ORDER BY account_id; account_id | balance
------------+---------
1 | 950
2 | 1050
(2 rows)FAQ
How do I find which queries caused a PostgreSQL deadlock?
The error's DETAIL lists the process ids and the transactions each was waiting for, and the HINT says See server log for query details. The server log has the statements of both processes.
Which transaction is aborted on deadlock detected?
In both experiments here it was session B, the one whose request completed the cycle. Session A's waiting statement finished right after.
How do I fix deadlock detected in PostgreSQL?
Lock rows in the same order in every transaction, and use SELECT ... FOR UPDATE instead of FOR SHARE when you will update the row. Both fixes are shown above with the rerun output. Retry the whole transaction in code when 40P01 still happens.
Is 40P01 the same as lock timeout 55P03?
No. 55P03 means a statement gave up waiting because of lock_timeout or NOWAIT. 40P01 means two transactions were waiting for each other and could never finish.
How we verify
Each scenario starts from fresh database users. A script runs the setup, confirms the exact error code, applies the fix and runs the statement again. The output shown is what the database printed, saved by the script.
lab_a and lab_b are ordinary roles created for the test, each owning a schema of the same name; postgres is the superuser. PostgreSQL 17.12