PostgreSQL 17.12Lab-verified · 2026-11-30

PostgreSQL 40P01: deadlock detected

Two transactions were each waiting for a lock the other held, so PostgreSQL aborted one of them.

PostgreSQL 17.12
40P01: deadlock detected

Jump to a cause

  1. 01Two sessions update the same rows in opposite order
  2. 02Two sessions read with FOR SHARE, then both UPDATE

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
Setuprun as lab_a
CREATE TABLE accounts (account_id int PRIMARY KEY, balance int);
INSERT INTO accounts VALUES (1, 1000), (2, 1000);
OUTPUT
CREATE TABLE
INSERT 0 2
Reproduce2 sessions open at the same time (A, B), run in this order
Session Arun as lab_a
BEGIN;
UPDATE accounts SET balance = balance - 100 WHERE account_id = 1;
OUTPUT
BEGIN
UPDATE 1
Session Brun as lab_a
BEGIN;
UPDATE accounts SET balance = balance - 50 WHERE account_id = 2;
OUTPUT
BEGIN
UPDATE 1
Session Arun as lab_a⏳ Waiting for the lock…
UPDATE accounts SET balance = balance + 100 WHERE account_id = 2;
Session Brun as lab_a
UPDATE accounts SET balance = balance + 50 WHERE account_id = 1;
OUTPUT
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"
Session A: the waiting statement finishesrun as lab_a
OUTPUT
UPDATE 1

Sorting keys (for example by primary key) before updating them removes most deadlocks in batch jobs and transfers.

After the fix3 sessions open at the same time (A, B, C), run in this order
Session Arun as lab_a
BEGIN;
UPDATE accounts SET balance = balance - 100 WHERE account_id = 1;
OUTPUT
BEGIN
UPDATE 1
Session B · Fixrun as lab_a⏳ Waiting for the lock…
BEGIN;
UPDATE accounts SET balance = balance + 50 WHERE account_id = 1;
Session Arun as lab_a
UPDATE accounts SET balance = balance + 100 WHERE account_id = 2;
COMMIT;
OUTPUT
UPDATE 1
COMMIT
Session B: the waiting statement finishesrun as lab_a
OUTPUT
BEGIN
UPDATE 1
Session B · Fixrun as lab_a
UPDATE accounts SET balance = balance - 50 WHERE account_id = 2;
COMMIT;
OUTPUT
UPDATE 1
COMMIT
Session C · Checkrun as lab_a
SELECT account_id, balance FROM accounts ORDER BY account_id;
OUTPUT
 account_id | balance
------------+---------
          1 |     950
          2 |    1050
(2 rows)

02Two sessions read with FOR SHARE, then both UPDATE

A numbering table: sessions A and B both read the next number with FOR SHARE and both got 1. Share locks do not block each other, but then both tried to UPDATE the row, and each UPDATE needed the other's share lock to go away. B got 40P01 and A's UPDATE went through.

If you read a row in order to change it, use FOR UPDATE. Then session B waited at its SELECT until A committed, read 2, and the final value was 3.

Setup
Setuprun as lab_a
CREATE TABLE seq_no (name text PRIMARY KEY, next_no int);
INSERT INTO seq_no VALUES ('INVOICE', 1);
OUTPUT
CREATE TABLE
INSERT 0 1
Reproduce2 sessions open at the same time (A, B), run in this order
Session Arun as lab_a
BEGIN;
SELECT next_no FROM seq_no WHERE name = 'INVOICE' FOR SHARE;
OUTPUT
BEGIN
 next_no
---------
       1
(1 row)
Session Brun as lab_a
BEGIN;
SELECT next_no FROM seq_no WHERE name = 'INVOICE' FOR SHARE;
OUTPUT
BEGIN
 next_no
---------
       1
(1 row)
Session Arun as lab_a⏳ Waiting for the lock…
UPDATE seq_no SET next_no = next_no + 1 WHERE name = 'INVOICE';
Session Brun as lab_a
UPDATE seq_no SET next_no = next_no + 1 WHERE name = 'INVOICE';
OUTPUT
ERROR:  40P01: deadlock detected
DETAIL:  Process 2626 waits for ShareLock on transaction 6655; blocked by process 2618.
Process 2618 waits for ShareLock on transaction 6656; blocked by process 2626.
HINT:  See server log for query details.
CONTEXT:  while updating tuple (0,1) in relation "seq_no"
Session A: the waiting statement finishesrun as lab_a
OUTPUT
UPDATE 1

Counters, stock checks and other read-then-update code should lock with FOR UPDATE, not FOR SHARE.

After the fix3 sessions open at the same time (A, B, C), run in this order
Session Arun as lab_a
BEGIN;
SELECT next_no FROM seq_no WHERE name = 'INVOICE' FOR UPDATE;
OUTPUT
BEGIN
 next_no
---------
       1
(1 row)
Session B · Fixrun as lab_a⏳ Waiting for the lock…
BEGIN;
SELECT next_no FROM seq_no WHERE name = 'INVOICE' FOR UPDATE;
Session Arun as lab_a
UPDATE seq_no SET next_no = next_no + 1 WHERE name = 'INVOICE';
COMMIT;
OUTPUT
UPDATE 1
COMMIT
Session B: the waiting statement finishesrun as lab_a
OUTPUT
BEGIN
 next_no
---------
       2
(1 row)
Session Brun as lab_a
UPDATE seq_no SET next_no = next_no + 1 WHERE name = 'INVOICE';
COMMIT;
OUTPUT
UPDATE 1
COMMIT
Session C · Checkrun as lab_a
SELECT name, next_no FROM seq_no;
OUTPUT
  name   | next_no
---------+---------
 INVOICE |       3
(1 row)

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