LESSON 03 · Snapshot isolation

Read from one point in time

Snapshot isolation gives each transaction a frozen view of committed data, built by keeping several versions of each row.

SYSTEM VIEW

The interleaving

Diagram view
Seats read—
Revenue read—
Row versions—

Watch a nightly job read a concert’s sales while a purchase commits, then replay it with each read from one snapshot.

  1. A nightly job reconciles a concert’s sales at read committed, reading sold_seats and then revenue as two statements. Its first read returns 480 seats, the latest committed value.

    Isolation
    read committed

    What changed

    • Job reads 480 seats
  2. Before the job’s second read, a purchase of 4 seats at $50 commits. Concert sales now holds 484 seats and $24,200 of revenue, and both values are committed.

    Purchase
    4 × $50 = $200

    What changed

    • Sales: 484 seats, $24,200
  3. The job reads revenue: $24,200. It expects 480 × $50 = $24,000 and raises a false alarm over the $200 gap. Nothing uncommitted was seen; its two reads came from different moments. This is read skew.

    Expects
    480 × $50 = $24,000
    Reads
    $24,200

    What changed

    • Job reads $24,200
    • False alarm over $200
  4. Replay the job with snapshot isolation. It runs as Txn 100, and its snapshot holds only what had committed when the snapshot was taken: 480 seats and $24,000. Its first read returns 480.

    Isolation
    snapshot (MVCC)
    Txn 100 sees
    480 · $24,000

    What changed

    • Snapshot isolation
    • Txn 100’s snapshot
    • Txn 100 reads 480 seats
  5. Txn 102 buys the same 4 seats. Instead of overwriting, it writes a new version tagged 102, holding 484 seats and $24,200, and commits without waiting. The old version stays, so the row now holds two.

    Old version
    480 · $24,000
    Version 102
    484 · $24,200

    What changed

    • Version 102: 484, $24,200
    • Row holds 2 versions
    • Txn 102 commits
  6. Txn 100 reads revenue. Txn 102 hadn’t committed when the snapshot was taken, so Txn 100 skips version 102 and reads $24,000, which matches 480 × $50. It took no read locks and blocked no purchase.

    Matches
    480 × $50 = $24,000
    Read locks
    none

    What changed

    • Txn 100 skips version 102
    • Txn 100 reads $24,000
    • The $200 gap
  7. The cost is old versions. The old version has stayed since Txn 102 committed, and cleanup, which Postgres calls vacuum, can’t remove it while Txn 100’s snapshot may read it. A snapshot held open for hours delays cleanup, so busy tables bloat.

    Old version
    kept for Txn 100
    Open for hours
    busy tables bloat

    What changed

    • Old version kept
    • Vacuum can’t remove it
  8. Txn 100 commits, so no snapshot needs the old version, and vacuum removes it: the row is back to one version. Postgres offers this level as repeatable read, and some anomalies remain, as a later lesson shows.

    Postgres calls it
    repeatable read

    What changed

    • Txn 100 commits
    • Old version

Space plays · ← → step · Home resets

THE IDEA

How it works

A nightly job reconciles a concert’s sales by reading sold_seats and then revenue, as two statements at read committed. It reads 480 seats. A purchase of 4 seats at $50 then commits. It reads revenue of $24,200, when 480 seats at $50 should be $24,000, and raises a false alarm over the $200 gap. Nothing is wrong, and no uncommitted data was seen. This anomaly is read skew: reads within one transaction come from different moments. Backups and long analytics queries are especially exposed.

Snapshot isolation fixes it by letting a transaction read only what had committed when its snapshot was taken. It is usually built with multi-version concurrency control (MVCC): an update writes a new row version tagged with the writing transaction’s ID instead of replacing the old one. A reader ignores any version written by a transaction that had not committed at snapshot time, and uses the newest version left.

Replay the job as transaction 100. Its snapshot holds 480 seats and $24,000. Transaction 102 commits new versions, 484 and $24,200. Transaction 100 still reads 480 and $24,000, so its answer is consistent, and it took no read locks and blocked no purchase. Readers and writers do not block each other.

The cost is old versions. They must stay until no snapshot needs them, and then a cleanup process removes them, which Postgres calls vacuum. One snapshot held open for hours delays that cleanup, so busy tables bloat. Some anomalies also remain, as a later lesson shows. Names vary too: Postgres offers this level as repeatable read.

DECISION POINT

A monthly export runs for four hours inside one snapshot transaction on your primary database. While it runs, the busiest table’s disk use climbs and queries slow down. A teammate suggests splitting the export into many short transactions. What would you decide?

Write a sentence or two to unlock the guidance. Committing first is what makes it stick.

Keep in mind

  • Read skew is seeing committed data from different moments in one transaction.
  • MVCC keeps row versions, so a snapshot reader and a writer do not block each other.
  • Long-open snapshots delay cleanup of old versions and bloat busy tables.
CHECK YOUR UNDERSTANDING

Put it into practice.

1 OF 3
ApplyQUESTION 01

Transaction 100 takes its snapshot while transaction 102 is still running. Transaction 102 then commits an update to a row, and 100 reads that row. What does 100 see?

Choose an answer. You’ll see the explanation once you get it right.

Next lesson

0 of 3 checkpoints answered correctly to complete this lesson.