Sphere
Log in Join
☆ 13

could not serialize access due to concurrent update

Postgres 15, we turned on REPEATABLE READ for a reporting job and now get:

ERROR: could not serialize access due to concurrent update
CONTEXT: SQL statement "UPDATE accounts SET balance = balance + $1 WHERE id = $2"

This never happened under READ COMMITTED. I understand RR gives a stable snapshot, but why does a plain update conflict? Should I just switch back?

⚑ Report

4 replies

☆ 0
R regex ·2026-10-14#1

Under REPEATABLE READ, if a row changed after your snapshot and you try to update it, Postgres aborts with a serialization failure rather than silently updating a stale value. That's the guarantee working as designed. READ COMMITTED would re-evaluate and apply the update to the new row version.

⚑ Report
☆ 0
R regex ·2026-10-15#2

So the right response is to retry the whole transaction on that error? What's the SQLSTATE to catch? I added a counter to verify the retry actually happens under load, and it does fire occasionally. The important part was making sure the transaction fully rolls back before retrying, otherwise the balance update could double-apply.

⚑ Report
☆ 0
R rm ·2026-10-20#3

SQLSTATE 40001 (serialization_failure). Catch it and retry the entire transaction with backoff and jitter. Under SERIALIZABLE there's also 40P01 for deadlocks. Make sure your transaction body is idempotent or fully rolled back before retrying, and cap the retries so you don't loop forever.

⚑ Report
☆ 0
K kp ·2026-10-23#4

Added a retry wrapper on 40001 with a few attempts and jitter. Conflict rate was low so retries almost never fire twice. Kept REPEATABLE READ because the reporting numbers need a consistent snapshot. Switching isolation level would have hidden the real tradeoff.

⚑ Report
Log in to reply.