About this article: This article is reviewed against the cited sources. This is source research, not lab validation. Check the stated versions and your own requirements before making changes.
When PostgreSQL returns ERROR: could not serialize access due to concurrent update, the server has rolled your transaction back on purpose. Another transaction committed a change to a row yours also needed, after yours had taken its snapshot. The PostgreSQL manual says an application that gets this message "should abort the current transaction and retry the whole transaction from the beginning". The error is the designed behaviour of the Repeatable Read and Serializable levels, so the fix is a retry in the application, not a server repair.
What the error means
PostgreSQL's default isolation level is Read Committed. The Repeatable Read level is different: a transaction sees only data committed before its first statement, and applications using this level "must be prepared to retry transactions due to serialization failures".
Suppose such a transaction goes to update a row that another transaction has already updated, deleted or locked. It waits for the other transaction to finish. If the other one rolls back, yours proceeds. If it commits and actually updated or deleted the row (not just locked it), yours cannot proceed, because per the manual "a repeatable read transaction cannot modify or lock rows changed by other transactions after the repeatable read transaction began". PostgreSQL then ends your transaction with SQLSTATE 40001, named serialization_failure in Appendix A.
The Serializable level works "exactly the same as Repeatable Read" and adds monitoring for patterns that no one-at-a-time order could explain. When it trips, the message ends differently: ERROR: could not serialize access due to read/write dependencies among transactions. The manual says such messages vary "according to the precise circumstances" but always carry SQLSTATE 40001.
PostgreSQL 17 source (tag REL_17_9) also contains could not serialize access due to concurrent delete, raised in the same situation when the other transaction deleted the row. That is our reading of the code, not wording from the manual.
At Repeatable Read, only updating transactions can hit this: "read-only transactions will never have serialization conflicts", per the manual.
A deadlock is different. It is SQLSTATE 40P01, a cycle of waiting transactions. See PostgreSQL ERROR: deadlock detected: read the log, then fix the lock order.
Likely causes
| Cause | What you see | How to confirm |
|---|---|---|
| Two overlapping transactions update the same row at Repeatable Read | could not serialize access due to concurrent update |
default_transaction_isolation is repeatable read, or the application sets it per transaction |
| Serializable detected a read/write cycle | could not serialize access due to read/write dependencies among transactions |
The isolation level is serializable |
| The other transaction deleted the row | could not serialize access due to concurrent delete (source reading) |
The message ends with delete |
| Transactions stay open too long, widening the overlap | Failures cluster when sessions sit idle in transaction |
pg_stat_activity shows old xact_start values |
| The application does not retry | One failed attempt reaches the user | Application log shows 40001 once, with no second attempt |
Prove the cause before changing anything
Everything here only reads.
1. Read the whole message and the SQLSTATE. The ending of the message (concurrent update, concurrent delete or read/write dependencies) tells you which of the first three rows applies. Most client libraries expose the five-character code; look for 40001.
2. Read the default isolation level.
SHOW default_transaction_isolation;
The manual says the default is "read committed". If the result is repeatable read or serializable, the server or role is set that way. If it is read committed, the application is raising the level for its own transactions with SET TRANSACTION or BEGIN ISOLATION LEVEL. Search its code:
grep -rn -i "isolation" /srv/app01
A good result is a small number of places that set the level on purpose. A bad result is a level set in a shared connection helper, so every transaction pays for it.
3. Look for transactions held open.
SELECT pid, usename, state, xact_start, query
FROM pg_stat_activity
WHERE state LIKE 'idle in transaction%'
ORDER BY xact_start;
The manual describes idle in transaction as "in a transaction, but is not currently executing a query". A good result is no rows, or rows whose xact_start is seconds old. A bad result is a row whose xact_start is minutes old. A long-open transaction keeps its snapshot, so more concurrent changes land after it.
4. Check whether the application retries. Look in its log for what follows a 40001: a second attempt of the same work, or nothing. The manual says "PostgreSQL does not offer an automatic retry facility", so a retry exists only if the application code has one.
Fix
The manual's remedy is a retry. Lowering the isolation level is a second, narrower option that is our own advice, not a step the manual gives for 40001.
Retry the whole transaction
This is what the manual prescribes. It says the retry must include "all logic that decides which SQL to issue and/or which values to use", because the second attempt sees the other transaction's committed change as part of its initial view. Retrying only the failing statement is not enough.
Two further points from the manual. A retry does not guarantee success, and "multiple retries may be needed". The same page says it is "recommendable to just retry serialization_failure errors unconditionally", and that other codes, such as 23505 (unique_violation), need more care "since they might represent persistent error conditions".
Adding that retry is a change to the application's code, made and tested by the application owner. After the failure the session is in an aborted transaction, and a client that keeps sending statements on it meets the next error. See PostgreSQL "ERROR: current transaction is aborted, commands ignored until end of transaction block" (25P02): find the first error.
Shorten the transactions
For Serializable work the manual lists these measures: keep transactions to what integrity needs, declare them READ ONLY where possible, and do not leave connections "idle in transaction" longer than necessary. It adds that idle_in_transaction_session_timeout "may be used to automatically disconnect lingering sessions". Our own reading: transactions that the application holds open are the cause to look at first, and a timeout that disconnects them only hides the habit. Changing a timeout or the application's transaction handling is a change for the application owner and database administrator, with its own save and back-out.
Use Read Committed for transactions that allow it
At Read Committed, a second updater that finds the row already changed waits, then "will attempt to apply its operation to the updated version of the row", re-checking the WHERE clause. The manual's own example is the bank balance update, where "we clearly want the second transaction to start with the updated version of the account's row". The same page warns that Read Committed is unsuitable "for commands that involve complex search conditions", because a command can see an inconsistent snapshot.
Choose it only for a transaction whose logic is safe under those rules, such as a single-row balance change. If the application sets the level in one place in its code, the change is one line there.
First save the current state:
cp -p orders_service.py orders_service.py.2026-12-03
Risk: the next edit lowers the isolation level of one application transaction from Repeatable Read or Serializable to Read Committed. The transaction stops failing with 40001 on this row, and starts to see other transactions' commits mid-way through its own work, so a rule that relied on the stronger level can be broken silently.
Edit the file so that the one transaction begins with BEGIN ISOLATION LEVEL READ COMMITTED; or issues SET TRANSACTION ISOLATION LEVEL READ COMMITTED; as its first statement of the transaction. The SET TRANSACTION page says it "has no effect on any subsequent transactions".
Only if you need to back out: the copy below overwrites the edited file with the saved one, so any other change made to the file since the save is lost. Copy the saved file back, then rerun the application's tests for the transaction.
cp -p orders_service.py.2026-12-03 orders_service.py
Example (illustrative): On app01, a payment service connects to db01 and sets Repeatable Read. Session A begins and reads account 12345, balance 500.00. Session B adds 100.00 and commits, so the balance is 600.00 (500.00 + 100.00). Session A then runs UPDATE accounts SET balance = balance - 50.00 WHERE acctnum = 12345 and receives could not serialize access due to concurrent update. The service rolls back, starts the whole transaction again, reads 600.00 and writes 550.00 (600.00 - 50.00). The second attempt succeeds because B's commit is now part of its snapshot. pg_stat_activity showed no old idle in transaction rows, so the overlap was two short transactions and a retry was the right fix.
Confirm the fix
- A failing transaction is followed by a second attempt in the application log, and the second attempt commits.
- Callers see no 40001 error. A small number of logged first attempts can remain, and is normal at these levels.
SELECT pid, state, xact_start, query FROM pg_stat_activity WHERE state LIKE 'idle in transaction%'returns no old rows.- If you changed an isolation level, the application's own tests for that transaction pass.
What to watch afterwards
- The rate of 40001 in the application log. A rising rate points to higher contention or longer transactions.
- With very high contention, the manual says "it is possible that completion of a transaction may take many attempts". Put a limit on attempts in the code, with a clear error when it is reached (our own advice).
- Serializable only: sequential scans take relation-level predicate locks, which "can result in an increased rate of serialization failures", and so does a predicate lock table that is short of memory. The manual names
max_pred_locks_per_transaction,max_pred_locks_per_relationandmax_pred_locks_per_pagefor the second. - Prepared transactions: the manual says a conflicting prepared transaction may block progress until it commits or rolls back.
Read next
Related articles you may want next.
- TroubleshootingPostgreSQL ERROR: deadlock detected: read the log, then fix the lock orderPostgreSQL 'ERROR: deadlock detected' (40P01): read the server log, find the two statements, fix the lock order and retry the aborted transaction.Read article
- TroubleshootingMySQL ERROR 1213 (40001): Deadlock found when trying to get lock; try restarting transactionMySQL ERROR 1213 (40001) deadlock found: InnoDB rolled back your transaction. Read the deadlock report, fix the lock order, retry the whole transaction.Read article
- TroubleshootingPostgreSQL "ERROR: current transaction is aborted, commands ignored until end of transaction block" (25P02): find the first errorPostgreSQL 'current transaction is aborted, commands ignored until end of transaction block' (25P02)? Find the first error, then ROLLBACK or savepoints.Read article
Risks and limits
- These commands have not been run or lab-tested.
- A retry repeats the transaction's side effects outside the database, such as an email or a call to another service, unless the code separates them. Our own advice: keep those effects after the commit.
- Lowering the isolation level trades errors for possible anomalies. The manual warns that Read Committed is unsuitable "for commands that involve complex search conditions", and makes a similar warning about business rules at Repeatable Read, so explicit locks matter at either level.
- The manual's text is for PostgreSQL 17. Message wording shown from source is for tag REL_17_9.
Sources
- 13.2. Transaction Isolation — PostgreSQL Global Development Group, PostgreSQL 17 documentation, no date shown, accessed 3 December 2026
- 13.5. Serialization Failure Handling — PostgreSQL Global Development Group, PostgreSQL 17 documentation, no date shown, accessed 3 December 2026
- SET TRANSACTION — PostgreSQL Global Development Group, PostgreSQL 17 documentation, no date shown, accessed 3 December 2026
- BEGIN — PostgreSQL Global Development Group, PostgreSQL 17 documentation, no date shown, accessed 3 December 2026
- 19.11. Client Connection Defaults — PostgreSQL Global Development Group, PostgreSQL 17 documentation, no date shown, accessed 3 December 2026
- 27.2. The Cumulative Statistics System — PostgreSQL Global Development Group, PostgreSQL 17 documentation, no date shown, accessed 3 December 2026
- Appendix A. PostgreSQL Error Codes — PostgreSQL Global Development Group, PostgreSQL 17 documentation, no date shown, accessed 3 December 2026
- nodeModifyTable.c — PostgreSQL source repository, tag REL_17_9, no date shown, accessed 3 December 2026
- predicate.c — PostgreSQL source repository, tag REL_17_9, no date shown, accessed 3 December 2026
This article was originally published on .