Postgres “could not serialize access due to concurrent update” (40001) – retry or redesign

By Abheesht Tripathi · Published

ERROR:  could not serialize access due to concurrent update   (SQLSTATE 40001)
ERROR:  could not serialize access due to read/write dependencies among transactions
HINT:  The transaction might succeed if retried.

What it means

Your transaction runs at REPEATABLE READ or SERIALIZABLE isolation. At those levels every statement sees the database as it was when the transaction took its snapshot. If you then try to update, delete or lock (FOR UPDATE) a row that another transaction changed and committed after that snapshot, Postgres can't apply your change without breaking the promise, so it aborts your transaction with SQLSTATE 40001.

The second message is specific to SERIALIZABLE: Postgres found a pattern of reads and writes between concurrent transactions that could not have happened in any one-at-a-time order, so it cancelled one of them.

This is not a bug. The PostgreSQL documentation says applications using these levels must be prepared to retry transactions that fail with serialization errors.

Where the higher isolation level comes from

Fix 1: retry the whole transaction

If you need the stronger guarantee, keep it and add a retry around the complete unit of work. Re-run everything, including the reads, because the decision you made from them may change:

import { Prisma } from '@prisma/client';

async function serializable<T>(fn: (tx: Prisma.TransactionClient) => Promise<T>, attempts = 5): Promise<T> {
  for (let i = 1; ; i++) {
    try {
      return await prisma.$transaction(fn, {
        isolationLevel: Prisma.TransactionIsolationLevel.Serializable,
      });
    } catch (e) {
      const retryable = e instanceof Prisma.PrismaClientKnownRequestError && e.code === 'P2034';
      if (!retryable || i >= attempts) throw e;
      await new Promise((r) => setTimeout(r, 20 * 2 ** i + Math.random() * 20));
    }
  }
}

With node-postgres or supabase-js, check for code === '40001' instead. Don't send emails or call external APIs inside the transaction; a retry would repeat them.

Fix 2: drop to READ COMMITTED and make the write atomic

Many “read, check, write” flows don't need serializable isolation at all. Put the check in the UPDATE itself, so the row is re-checked under its lock:

-- take 1 seat only if one is left; returns no row when sold out
update public.events
set seats_left = seats_left - 1
where id = $1 and seats_left > 0
returning seats_left;

Or lock the row first with select … for update at READ COMMITTED: concurrent transactions wait for each other instead of failing.

begin;
select balance from public.wallets where user_id = $1 for update;
-- decide in code using the locked, current balance
update public.wallets set balance = balance - $2 where user_id = $1;
commit;

Fix 3: reduce conflicts

Keep serializable transactions short and touching as few rows as possible. Under SERIALIZABLE, reads that use an index conflict with fewer rows than sequential scans, so the right indexes also reduce false conflicts.

How to verify

Run a load test with concurrent requests on the same row (for example 50 parallel seat bookings). With retries: no 40001 reaches the user and the final count is exact. With the atomic update: no errors at all, and seats_left never goes below zero.

Related guides

More in Postgres errors and performance · all guides