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
CREATE TABLE orders (order_id int PRIMARY KEY, status text NOT NULL);
INSERT INTO orders VALUES (1, 'NEW');CREATE TABLE
INSERT 0 1BEGIN ISOLATION LEVEL REPEATABLE READ;
SELECT status FROM orders WHERE order_id = 1;BEGIN
status
--------
NEW
(1 row)UPDATE orders SET status = 'CANCELLED' WHERE order_id = 1;UPDATE 1UPDATE orders SET status = 'PAID' WHERE order_id = 1;ERROR: 40001: could not serialize access due to concurrent updateRetry 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.
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;BEGIN
status
-----------
CANCELLED
(1 row)
UPDATE 0
COMMITSELECT order_id, status FROM orders; 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
CREATE TABLE doctors (name text PRIMARY KEY, on_call boolean NOT NULL);
INSERT INTO doctors VALUES ('Kim', true), ('Lee', true);CREATE TABLE
INSERT 0 2BEGIN ISOLATION LEVEL SERIALIZABLE;
SELECT count(*) AS on_call FROM doctors WHERE on_call;BEGIN
on_call
---------
2
(1 row)BEGIN ISOLATION LEVEL SERIALIZABLE;
SELECT count(*) AS on_call FROM doctors WHERE on_call;BEGIN
on_call
---------
2
(1 row)UPDATE doctors SET on_call = false WHERE name = 'Kim';UPDATE 1UPDATE doctors SET on_call = false WHERE name = 'Lee';UPDATE 1COMMIT;COMMITCOMMIT;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.
BEGIN ISOLATION LEVEL SERIALIZABLE;
SELECT count(*) AS on_call FROM doctors WHERE on_call;
COMMIT;BEGIN
on_call
---------
1
(1 row)
COMMITSELECT name, on_call FROM doctors ORDER BY name; 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