Row versions and visibility
Overview and setup
Multi-version concurrency control (MVCC) lets a transaction read a consistent version of data while other transactions change it. PostgreSQL normally creates a new tuple version for an UPDATE; DELETE marks a version as deleted. Visibility depends on transaction status and the reader’s snapshot, not just on whether a physical tuple exists. An old version may still be visible to an earlier reader.
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 B
SELECT qty,version FROM mv_item WHERE id=1;Observed rows: [[15, 2]]
5. Session A
SELECT qty,version FROM mv_item WHERE id=1;Observed rows: [[10, 1]]
6. Session A
COMMIT;7. Session A
SELECT qty,version FROM mv_item WHERE id=1;Observed rows: [[15, 2]]
How it works
SQLite WAL preserves a reader’s view using database pages and WAL frames, not PostgreSQL heap tuples. In the first schedule B commits quantity 15, but A still sees 10 until its read transaction ends. In the second, A can see a row that B has deleted. A fresh read no longer sees it. Ordinary MVCC visibility is not a permanent audit history or a time-travel query API. See the native PostgreSQL lab file for tuple metadata.
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 qty,version FROM mv_item WHERE id=1;Observed rows: [[10, 1]]
3. Session B
DELETE FROM mv_item WHERE id=1;4. Session B
SELECT qty,version FROM mv_item WHERE id=1;Observed rows: []
5. Session A
SELECT qty,version FROM mv_item WHERE id=1;Observed rows: [[10, 1]]
6. Session A
COMMIT;7. Session A
SELECT qty,version FROM mv_item WHERE id=1;Observed rows: []
Practice and mistakes
Practice: change B’s update to a rollback inside an explicit transaction; predict A’s next read after commit. Hint 1: only committed external writes become visible. Hint 2: begin B, update, then roll back B. Mistake: assuming MVCC removes all write conflicts.