Version cleanup
Overview and setup
Old PostgreSQL tuple versions eventually become unnecessary once no relevant snapshot can see them. Ordinary VACUUM makes reusable space available and helps maintain visibility information and transaction-ID freezing; it usually does not shrink the whole file. Autovacuum automates routine work. VACUUM FULL rewrites the table and requires a stronger lock, so it is not a routine substitute.
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
PRAGMA wal_checkpoint(TRUNCATE);Observed rows: [[0, 0, 0]]
2. Session B
UPDATE mv_item SET qty=qty+5,version=version+1 WHERE id=1;3. Session B
PRAGMA wal_checkpoint(PASSIVE);Observed rows: [[0, 1, 1]]
4. Session B
PRAGMA wal_checkpoint(TRUNCATE);Observed rows: [[0, 0, 0]]
5. Session A
SELECT qty,version FROM mv_item WHERE id=1;Observed rows: [[15, 2]]
How it works
SQLite checkpointing copies WAL pages back to the database; it is not PostgreSQL tuple vacuuming. With automatic checkpoints disabled and a reset WAL, a single update in this tiny fixture produces one frame. PRAGMA wal_checkpoint returns three integers: busy flag, log frames, checkpointed frames. PASSIVE can report zero busy even when not all frames are copied; TRUNCATE with timeout zero reports busy when A pins the old snapshot. After A commits, truncation succeeds. Frame counts depend on setup; do not generalize these exact numbers. SQLite VACUUM is a separate file-rebuild operation.
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
PRAGMA wal_checkpoint(PASSIVE);Observed rows: [[0, 1, 0]]
6. Session B
PRAGMA wal_checkpoint(TRUNCATE);Observed rows: [[1, 1, 0]]
7. Session A
SELECT qty,version FROM mv_item WHERE id=1;Observed rows: [[10, 1]]
8. Session A
COMMIT;9. Session B
PRAGMA wal_checkpoint(TRUNCATE);Observed rows: [[0, 0, 0]]
10. Session A
SELECT qty,version FROM mv_item WHERE id=1;Observed rows: [[15, 2]]
Practice and mistakes
Practice: compare the passive result with the truncate result while A is active. Hint 1: compare columns two and three. Hint 2: finish the reader, then retry. Mistake: equating a zero busy flag with a fully copied WAL.