Serialization retries
Overview and setup
A serialization failure means the transaction must be attempted again using a fresh view, not merely replaying the last SQL statement. PostgreSQL signals serialization failure with SQLSTATE 40001, potentially at commit. SQLite SQLITE_BUSY_SNAPSHOT is a distinct engine-specific stale-snapshot error.
Use a fresh disposable file database initialized with versions/v55/isolation-fixture.sql. Enable WAL with PRAGMA journal_mode=WAL; before opening two independent private-cache connections A and B. Use driver autocommit, no implicit transaction, zero busy timeout, and fully fetch each query result. Follow steps in order; the included Python validator automates this. Never use production data.
First two-session example
Run each numbered statement in the indicated connection; do not paste the whole schedule into one session.
1. Session A
BEGIN;2. Session A
SELECT cents FROM iso_account WHERE id=1;Observed rows: [[100]]
3. Session B
UPDATE iso_account SET cents=120 WHERE id=1;4. Session A
UPDATE iso_account SET cents=110 WHERE id=1;Observed error: SQLITE_BUSY_SNAPSHOT
5. Session A
ROLLBACK;6. Session A
BEGIN;7. Session A
SELECT cents FROM iso_account WHERE id=1;Observed rows: [[120]]
8. Session A
UPDATE iso_account SET cents=130 WHERE id=1;9. Session A
COMMIT;10. Session B
SELECT cents FROM iso_account WHERE id=1;Observed rows: [[130]]
How it works
The first schedule deliberately computes a stale 110, then rolls back after the error and recomputes 130 from the new 120. The second retries the whole business decision: the fresh count is one, so the caller skips the off-call update. A production retry loop should bound attempts, use backoff with jitter for transient contention, roll back failed work, and avoid repeating external side effects (use idempotency/outbox where appropriate). Do not retry every integrity error blindly.
Second two-session example
Run each numbered statement in the indicated connection; do not paste the whole schedule into one session.
1. Session A
BEGIN;2. Session A
SELECT SUM(on_call) FROM iso_shift;Observed rows: [[2]]
3. Session B
UPDATE iso_shift SET on_call=0 WHERE id=2;4. Session A
UPDATE iso_shift SET on_call=0 WHERE id=1;Observed error: SQLITE_BUSY_SNAPSHOT
5. Session A
ROLLBACK;6. Session A
BEGIN;7. Session A
SELECT SUM(on_call) FROM iso_shift;Observed rows: [[1]]
8. Session A
COMMIT;9. Session B
SELECT SUM(on_call) FROM iso_shift;Observed rows: [[1]]
Practice and mistakes
Practice: write client pseudocode for up to three attempts including COMMIT inside the error handler scope. Hint 1: each attempt begins with fresh reads. Hint 2: only publish external effects after confirmed commit, with a deduplication strategy. Mistake: retrying the final UPDATE with stale values or treating an unknown commit outcome as a confirmed rollback.