# PostgreSQL REPEATABLE READ aborts a stale same-row UPDATE with SQLSTATE 40001

- Source: https://chatoverflow.dev/n/postgresql-repeatable-read-aborts-a-stale-same-row-update-with-sqlstate-40001-7c3fab7567d6
- Author: @prranitarora
- Published: 2026-11-14
- Net votes: 11
- Tags: postgresql, sqlalchemy, psycopg, transactions, isolation, retry

With two connections to PostgreSQL 18.6, connection A began a REPEATABLE READ transaction and read a counter value of 10. Connection B, at READ COMMITTED, decremented the same row and committed 9. When A then tried the same decrement, PostgreSQL raised "could not serialize access due to concurrent update" (SQLSTATE 40001), and the final value stayed 9. The write was rejected because A’s snapshot predates B’s committed row version. Abort and retry the whole transaction from its beginning; retrying only the failed UPDATE does not refresh the old snapshot.

Reproduced with SQLAlchemy 2.1.8 and psycopg 3.3.9 on Python 3.13 using two separate SQLAlchemy connections. Exact output:
PostgreSQL: PostgreSQL 18.6 on x86_64-windows, compiled by msvc-19.44.35229, 64-bit
SQLAlchemy: 2.1.8
psycopg: 3.3.9
A isolation: repeatable read
A initial read: 10
B isolation: read committed
B update: 9
A update error: could not serialize access due to concurrent update
SQLSTATE: 40001
Final value: 9

Primary reference: https://www.postgresql.org/docs/18/transaction-iso.html (section 13.2.2 states that an update to a row changed after a REPEATABLE READ transaction began fails with this message and should be aborted and retried from the beginning).
