PostgreSQL 17.12Lab-verified · 2026-11-30

PostgreSQL 40001: could not serialize access due to concurrent update

Under REPEATABLE READ or SERIALIZABLE, another transaction's committed change conflicts with yours, so PostgreSQL cancels your transaction.

PostgreSQL 17.12
40001: could not serialize access due to concurrent update

Jump to a cause

  1. 01REPEATABLE READ: the row was updated and committed by another session
  2. 02SERIALIZABLE: two transactions read the same data and each changed a different row

Lab-verifiedReproduced and fixed on PostgreSQL 17.12 · 2026-11-30 · container image postgres:17

SQLSTATE 40001 (serialization_failure) only appears at the REPEATABLE READ and SERIALIZABLE isolation levels. PostgreSQL cancels one transaction instead of letting it work on outdated data. It is not a bug in your SQL: the expected handling is to roll back and run the whole transaction again.

The server even says so in the HINT: The transaction might succeed if retried. Both cases below were reproduced with two real sessions on PostgreSQL 17.

01REPEATABLE READ: the row was updated and committed by another session

Session A started a REPEATABLE READ transaction and read the order as NEW. Session B then changed it to CANCELLED and committed. When A tried to update the same row, PostgreSQL refused with could not serialize access due to concurrent update, because A's snapshot no longer matches the committed row.

Setup
Setuprun as lab_a
CREATE TABLE orders (order_id int PRIMARY KEY, status text NOT NULL);
INSERT INTO orders VALUES (1, 'NEW');
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 ISOLATION LEVEL REPEATABLE READ;
SELECT status FROM orders WHERE order_id = 1;
OUTPUT
BEGIN
 status
--------
 NEW
(1 row)
Session Brun as lab_a
UPDATE orders SET status = 'CANCELLED' WHERE order_id = 1;
OUTPUT
UPDATE 1
Session Arun as lab_a
UPDATE orders SET status = 'PAID' WHERE order_id = 1;
OUTPUT
ERROR:  40001: could not serialize access due to concurrent update

Retry the whole transaction from BEGIN. The new snapshot sees CANCELLED, and the update with AND status = 'NEW' changes 0 rows, so the cancelled order is not marked as paid.

After the fix2 sessions open at the same time (A, C), run in this order
Session A · Fixrun as lab_a
BEGIN ISOLATION LEVEL REPEATABLE READ;
SELECT status FROM orders WHERE order_id = 1;
UPDATE orders SET status = 'PAID' WHERE order_id = 1 AND status = 'NEW';
COMMIT;
OUTPUT
BEGIN
  status
-----------
 CANCELLED
(1 row)

UPDATE 0
COMMIT
Session C · Checkrun as lab_a
SELECT order_id, status FROM orders;
OUTPUT
 order_id |  status
----------+-----------
        1 | CANCELLED
(1 row)

02SERIALIZABLE: two transactions read the same data and each changed a different row

Both sessions checked that 2 doctors were on call, and each took a different doctor off call. Neither touched the other's row, so there was no lock wait. A committed first; B's COMMIT failed with read/write dependencies among transactions.

Setup
Setuprun as lab_a
CREATE TABLE doctors (name text PRIMARY KEY, on_call boolean NOT NULL);
INSERT INTO doctors VALUES ('Kim', true), ('Lee', true);
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 ISOLATION LEVEL SERIALIZABLE;
SELECT count(*) AS on_call FROM doctors WHERE on_call;
OUTPUT
BEGIN
 on_call
---------
       2
(1 row)
Session Brun as lab_a
BEGIN ISOLATION LEVEL SERIALIZABLE;
SELECT count(*) AS on_call FROM doctors WHERE on_call;
OUTPUT
BEGIN
 on_call
---------
       2
(1 row)
Session Arun as lab_a
UPDATE doctors SET on_call = false WHERE name = 'Kim';
OUTPUT
UPDATE 1
Session Brun as lab_a
UPDATE doctors SET on_call = false WHERE name = 'Lee';
OUTPUT
UPDATE 1
Session Arun as lab_a
COMMIT;
OUTPUT
COMMIT
Session Brun as lab_a
COMMIT;
OUTPUT
ERROR:  40001: could not serialize access due to read/write dependencies among transactions
DETAIL:  Reason code: Canceled on identification as a pivot, during commit attempt.
HINT:  The transaction might succeed if retried.

Re-run B's transaction from the start. It now counts only 1 doctor on call, so the application should stop before taking Lee off call. The final state keeps Lee on call.

After the fix2 sessions open at the same time (B, C), run in this order
Session B · Fixrun as lab_a
BEGIN ISOLATION LEVEL SERIALIZABLE;
SELECT count(*) AS on_call FROM doctors WHERE on_call;
COMMIT;
OUTPUT
BEGIN
 on_call
---------
       1
(1 row)

COMMIT
Session C · Checkrun as lab_a
SELECT name, on_call FROM doctors ORDER BY name;
OUTPUT
 name | on_call
------+---------
 Kim  | f
 Lee  | t
(2 rows)

FAQ

How should an application handle SQLSTATE 40001?

Catch it, roll back, and run the entire transaction again from BEGIN (including the reads). Re-running only the failed statement does not help, because the transaction is already aborted.

Why do I get 40001 when no row is locked?

In SERIALIZABLE, PostgreSQL also tracks what each transaction read. In the doctors example the two sessions updated different rows, but each one's read depended on the other's write, so one was cancelled at COMMIT.

Does 40001 happen at READ COMMITTED?

These serialization errors come from REPEATABLE READ and SERIALIZABLE; both examples here set the level with BEGIN ISOLATION LEVEL .... Deadlocks are a different code, 40P01.

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