Snapshot isolation

On this page 7

Snapshot isolation is a transaction isolation guarantee under which every transaction reads data from a consistent snapshot of the database as it existed at the moment the transaction started, and commits only if no concurrent transaction has already committed a write to the same items. In databases, it is a central concept of transactions because it gives readers a stable view without blocking writers, and writers without blocking readers.

Snapshot isolation prevents dirty reads, non-repeatable reads and most phantom reads, yet it is not fully serializable. Its best-known gap is write skew, an anomaly in which two transactions read overlapping data, write to different rows and together violate a constraint that each one checked individually.

How snapshot isolation works internally

Snapshot isolation is almost always implemented with multiversion concurrency control (MVCC). Instead of overwriting a row in place, an update creates a new row version tagged with the identifier or timestamp of the writing transaction, while older versions remain available to transactions that started earlier.

When a transaction begins, the engine records a start timestamp or a list of transactions that were still active. Every read then selects the newest version committed before that point, so the transaction sees a frozen, internally consistent state regardless of what other sessions commit later.

Writes are checked at commit time or at the moment of writing. Under the first-committer-wins rule, if another transaction has committed a change to the same row after the snapshot was taken, the later writer is aborted with a serialization or update-conflict error. Some engines apply a first-updater-wins variant, blocking the second writer until the first commits or rolls back.

The table below summarizes the moving parts of a typical implementation.

ComponentPurposeTypical form
Row versionsKeep older states readableVersion chains in heap or undo log
Snapshot timestampDefines what a transaction can seeTransaction ID list or commit sequence number
Visibility ruleSelects the correct version per readNewest version committed before snapshot
Write conflict checkPrevents lost updatesFirst-committer-wins or first-updater-wins
Garbage collectionRemoves versions no snapshot needsVacuum, purge or version store cleanup
Components of a snapshot isolation implementation

Anomalies prevented and permitted

Snapshot isolation blocks the classic read anomalies because a transaction never sees uncommitted data and never sees data change underneath it. It also prevents the lost update anomaly on a single row, because two concurrent writers to the same row cannot both commit.

The guarantee breaks down when transactions write disjoint sets of rows based on overlapping reads. Neither writer conflicts with the other at the row level, so both commit, yet the combined result could never have arisen from any serial order. Read-only transactions can also observe a read-only anomaly, seeing a state that is inconsistent with any serial schedule.

AnomalyPreventedReason
Dirty readYesOnly committed versions are visible
Non-repeatable readYesSnapshot does not change during the transaction
Phantom readYes, for readsNew rows are invisible to the old snapshot
Lost updateYesWrite conflict check aborts the second writer
Write skewNoDisjoint writes produce no row-level conflict
Read-only anomalyNoReader can see an order no serial schedule allows
Anomaly behavior under snapshot isolation

Snapshot isolation compared with standard isolation levels

The ANSI SQL standard defines isolation levels by the anomalies they forbid, and snapshot isolation does not map cleanly onto any of them. It is stronger than read committed and, in most respects, stronger than the standard’s repeatable read, yet weaker than true serializability. This mismatch has caused real confusion in product naming.

PostgreSQL implements snapshot isolation under the name Repeatable Read, and Oracle Database historically labeled its snapshot-based mode Serializable even though it permits write skew. SQL Server exposes it explicitly as the SNAPSHOT isolation level, which must be enabled per database. The comparison below shows where snapshot isolation sits.

Isolation levelRead viewWrite skew possibleBlocking between readers and writers
Read committedNew snapshot per statementYesNone with MVCC
Repeatable read (locking)Locks held on read rowsYes, via phantomsReaders block writers
Snapshot isolationOne snapshot per transactionYesNone
Serializable snapshot isolationOne snapshot plus dependency trackingNoNone, aborts instead
Two-phase locking serializableLocks plus predicate locksNoReaders and writers block each other
Position of snapshot isolation among isolation levels

Handling write skew in practice

Applications that run under snapshot isolation must identify transactions whose correctness depends on reading one set of rows and writing another. Common cases include on-call scheduling, seat or room booking, balance checks across multiple accounts and uniqueness rules that are not backed by a constraint.

  • Materialize the conflict by writing to a shared row, such as a counter or a lock record, so concurrent transactions collide.
  • Use explicit row locks with SELECT ... FOR UPDATE on the rows that the decision depends on.
  • Enforce the rule with a database constraint, such as a unique or exclusion constraint, rather than application logic.
  • Switch the critical transactions to serializable isolation where the engine supports serializable snapshot isolation.

Serializable snapshot isolation, used by PostgreSQL at its Serializable level, keeps the non-blocking behavior of snapshots but tracks read-write dependencies between concurrent transactions and aborts one when a dangerous cycle appears. Applications must therefore be ready to retry aborted transactions, which is good practice under plain snapshot isolation as well.

Costs and operational trade-offs

The main benefit of snapshot isolation is high read concurrency: long reports and analytical queries can run alongside heavy write traffic without lock contention. The price is paid in storage, cleanup work and abort handling.

EffectCauseMitigation
Version storage growthOld versions kept for active snapshotsKeep transactions short
Table and index bloatCleanup delayed by long transactionsMonitor oldest snapshot age
Update conflict errorsFirst-committer-wins abortsRetry logic in the application
Stale reads in long transactionsSnapshot fixed at startRestart long readers periodically
Operational effects of snapshot isolation

A single forgotten session left idle in transaction can hold back garbage collection for the entire database, so monitoring the age of the oldest active snapshot is a routine operational duty wherever snapshot isolation is used.

In simple terms

Imagine that each person working with a shared spreadsheet receives a private photograph of it at the moment they start. They read from that photograph, so nothing shifts under them while they work, and nobody has to wait for anybody else. Snapshot isolation gives every transaction that kind of photograph.

When someone tries to save changes, the system checks whether another person already changed the same cells in the meantime. If so, the later save is refused and must be tried again. The weakness is that two people can change different cells that together break a shared rule, and the system will not notice.

A practical example

A hospital scheduling system requires that at least one doctor stays on call each night. Two doctors, both on call for the same shift, open the app at the same moment to request leave. Each transaction reads the schedule, sees two doctors on call and concludes that removing one is safe.

Under snapshot isolation, each transaction updates only its own row, so there is no write conflict and both commits succeed, leaving the shift with nobody on call. The development team fixed the defect by running the leave request at the Serializable level in PostgreSQL and adding automatic retries. In load tests with 500 concurrent requests, the system then aborted the second transaction in every conflicting pair, and the coverage rule held without any noticeable slowdown for ordinary reads.