Overview

Isolation controls what a transaction can see of other transactions running at the same time. Stronger isolation prevents more anomalies but costs concurrency (more blocking or more aborts).

Core concepts

Anomalies, in increasing subtlety:

AnomalyWhat happens
Dirty readRead another transaction’s uncommitted change
Non-repeatable readRe-reading a row gives a different value because someone committed an update
Phantom readRe-running a range query returns new or missing rows
Lost updateTwo read-modify-write cycles overwrite each other
Write skewTwo transactions read overlapping data, write different rows, and together break an invariant (e.g. two doctors both go off call)

The SQL standard levels and what the three engines actually do:

LevelPostgreSQLSQL ServerOracle
Read UncommittedSame as Read CommittedAvailable (NOLOCK), dirty reads allowedNot supported
Read CommittedDefault. Each statement sees a fresh snapshotDefault. Uses shared locks, or row versioning if READ_COMMITTED_SNAPSHOT (RCSI) is onDefault. Statement-level read consistency via undo
Repeatable ReadSnapshot taken at the first statement; no phantomsLocks held to end of transactionNot offered
SerializableSerializable Snapshot Isolation: may abort with 40001Range locksTransaction-level snapshot; can raise ORA-08177
Snapshot(Repeatable Read is a snapshot)ALLOW_SNAPSHOT_ISOLATION option(Serializable is a snapshot)

Readers do not block writers in PostgreSQL or Oracle; in SQL Server they do under plain Read Committed, which is why many installations enable RCSI (locking-and-mvcc).

Worked example

Lost update and two ways to prevent it:

-- both sessions: read, compute in the app, write back  -> one update is lost
SELECT bal FROM acct WHERE id = 1;
UPDATE acct SET bal = 70 WHERE id = 1;
 
-- fix 1: do the arithmetic in the statement
UPDATE acct SET bal = bal - 30 WHERE id = 1;
 
-- fix 2: lock the row first
SELECT bal FROM acct WHERE id = 1 FOR UPDATE;   -- SQL Server: WITH (UPDLOCK)

Write skew needs SERIALIZABLE (or an explicit lock on a shared row, or a constraint that encodes the invariant) and the application must be ready to retry.

Pitfalls and production notes

  • NOLOCK hints in SQL Server can return duplicated, missing or uncommitted rows; prefer RCSI or snapshot isolation.
  • Higher isolation in lock-based engines increases deadlocks; in snapshot-based engines it increases serialization failures. Both require retry logic (transactions-acid).
  • Pick the level per transaction when you need it; do not raise the global default without measuring.
  • Isolation applies inside one database. Across services you need idempotency and compensation.

Practice questions

  1. Which anomalies can occur under PostgreSQL Read Committed?
  2. Why does PostgreSQL Repeatable Read prevent phantoms although the standard allows them?
  3. Describe write skew with an example from your own domain and two ways to prevent it.

References

locking-and-mvcc · transactions-acid · pg-mvcc · sqlserver-isolation-rcsi