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.
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?
Splitting lets cleanup proceed but gives up the single point-in-time view, so totals can disagree between chunks unless each chunk filters on a fixed cutoff such as rows created before the export began. A better first move is usually to run the export on a replica or a restored backup, so its long snapshot does not hold back cleanup on the primary. Replicas have their own limits with long queries, so check how yours behaves.
Use this as a guide for self review; written answers are not graded.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.