Long-running transactions
Overview and setup
A long-lived snapshot can delay storage reclamation even when its session is not actively running a query. In PostgreSQL old snapshots may hold back tuple cleanup; investigate transaction age, backend_xmin and idle-in-transaction sessions. In SQLite WAL an old reader can impede checkpoint progress. Long transactions also increase the opportunity for contention and stale decisions.
Use a fresh disposable file database initialized with versions/v57/mvcc-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, disable automatic checkpoints on both connections with PRAGMA wal_autocheckpoint=0;, 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 qty,version FROM mv_item WHERE id=1;Observed rows: [[10, 1]]
3. Session B
UPDATE mv_item SET qty=qty+5,version=version+1 WHERE id=1;4. Session A
UPDATE mv_item SET qty=qty+5,version=version+1 WHERE id=1;Observed error: SQLITE_BUSY_SNAPSHOT
5. Session A
ROLLBACK;6. Session A
BEGIN;7. Session A
SELECT qty,version FROM mv_item WHERE id=1;Observed rows: [[15, 2]]
8. Session A
UPDATE mv_item SET qty=qty+5,version=version+1 WHERE id=1;9. Session A
COMMIT;10. Session B
SELECT qty,version FROM mv_item WHERE id=1;Observed rows: [[20, 3]]
How it works
In the first SQLite schedule A attempts to upgrade an old read snapshot to a writer after B commits; SQLITE_BUSY_SNAPSHOT requires abandoning that snapshot and recomputing. In the second, three separate updates accumulate three WAL frames while A keeps its original view. Ending A permits truncation. This deterministic exercise measures frame progress, not time or throughput. Keep transactions bounded, avoid user interaction inside them and use appropriate monitoring; do not terminate unknown production sessions or force aggressive cleanup without operational review.
Second two-session example
Run each numbered statement in the indicated connection; do not paste the whole schedule into one session.
1. Session A
PRAGMA wal_checkpoint(TRUNCATE);Observed rows: [[0, 0, 0]]
2. Session A
BEGIN;3. Session A
SELECT qty,version FROM mv_item WHERE id=1;Observed rows: [[10, 1]]
4. Session B
UPDATE mv_item SET qty=qty+5,version=version+1 WHERE id=1;5. Session B
UPDATE mv_item SET qty=qty+5,version=version+1 WHERE id=1;6. Session B
UPDATE mv_item SET qty=qty+5,version=version+1 WHERE id=1;7. Session B
PRAGMA wal_checkpoint(PASSIVE);Observed rows: [[0, 3, 0]]
8. Session A
SELECT qty,version FROM mv_item WHERE id=1;Observed rows: [[10, 1]]
9. Session A
COMMIT;10. Session B
PRAGMA wal_checkpoint(TRUNCATE);Observed rows: [[0, 0, 0]]
11. Session A
SELECT qty,version FROM mv_item WHERE id=1;Observed rows: [[25, 4]]
Practice and mistakes
Practice: end A after the first of three B updates and checkpoint before the next update. Predict whether the original snapshot remains. Hint 1: commit releases the reader. Hint 2: a subsequent SELECT obtains a fresh view. Mistake: keeping a read transaction open while waiting for user input.