PostgreSQL error: could not serialize access

ERROR:  could not serialize access due to concurrent update

-- oppure, a livello Serializable:
ERROR:  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.

This error only exists at the Repeatable Read and Serializable isolation levels, and it is those levels working as designed: PostgreSQL could not produce a result consistent with the guarantees you asked for, so it aborted your transaction instead of silently returning something wrong.

What this error means

The two variants correspond to the two levels:

Either way the transaction was rolled back and, as the HINT says, would likely succeed if simply run again. SQLSTATE is 40001 in both cases.

Common causes

How to diagnose it

How to fix it

🔍 Related reading: Transaction isolation levels in practice — what each level guarantees, write skew, and a full section on retrying correctly.