Reading preferences
Concept

MVCC and Transaction Isolation

Compare statement and transaction snapshots through a prepared two-session schedule.

database, postgresql, mvcc, isolation, snapshots, transactions

Readers observe snapshots

Multiversion concurrency control (MVCC) allows transactions to work with versions of rows visible to their snapshots. Readers and writers often do not block each other, but writes can still wait on locks and conflicts. The isolation level determines important visibility and anomaly guarantees.

SQL illustration

The following is a prepared schedule, not executable as one script. In a separate, disposable PostgreSQL database, imagine a shared table scores with one committed row (id = 1, points = 10). Session A and session B use different connections. A uses the default READ COMMITTED isolation level. Commands run in the shown order; no other transactions modify the row.

A: BEGIN;
A: SELECT points FROM scores WHERE id = 1;      -- 10
B: BEGIN;
B: UPDATE scores SET points = 20 WHERE id = 1;
B: COMMIT;
A: SELECT points FROM scores WHERE id = 1;      -- 20
A: COMMIT;

The first A query starts before B's commit and sees 10. The second starts after B commits and sees 20. A never reads B's uncommitted update. These are prepared results, not a live database or a simulation of every schedule.

See the reasoning

Follow two session snapshots

See one READ COMMITTED schedule; other interleavings can differ. This is prepared; Intuvo does not run SQL.

A reads

Events in this schedule
  1. Session ASELECT → 10Current event
  2. Session BNot yet
  3. Session BNot yet
  4. Session ANot yet

A's first SELECT starts before B's update commits, so it reads 10.

Isolation has trade-offs

In PostgreSQL, READ UNCOMMITTED behaves like READ COMMITTED; dirty reads are not permitted. Under REPEATABLE READ, A's first relevant statement establishes a stable snapshot, so both reads in this schedule see 10. PostgreSQL's SERIALIZABLE adds checks intended to make successful transactions equivalent to some serial ordering; applications must handle serialization failures and retry the whole transaction safely where appropriate.

Snapshots alone do not enforce every business invariant, eliminate write conflicts, or remove the need for locks. Long transactions can hold old snapshots and affect cleanup. Explore actual workload behaviour and failure handling before relying on an isolation choice.

Reflect on the concept

Four fixed questions to reinforce the reasoning. Answers are not saved or scored.

1. At READ COMMITTED what can A's second SELECT see after B commits?
2. Does PostgreSQL expose another transaction's uncommitted update as a dirty read?
3. What changes in this schedule if A uses REPEATABLE READ before its first SELECT?
4. What must an application using SERIALIZABLE be prepared to handle?

References and further reading