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:
| Anomaly | What happens |
|---|---|
| Dirty read | Read another transaction’s uncommitted change |
| Non-repeatable read | Re-reading a row gives a different value because someone committed an update |
| Phantom read | Re-running a range query returns new or missing rows |
| Lost update | Two read-modify-write cycles overwrite each other |
| Write skew | Two 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:
| Level | PostgreSQL | SQL Server | Oracle |
|---|---|---|---|
| Read Uncommitted | Same as Read Committed | Available (NOLOCK), dirty reads allowed | Not supported |
| Read Committed | Default. Each statement sees a fresh snapshot | Default. Uses shared locks, or row versioning if READ_COMMITTED_SNAPSHOT (RCSI) is on | Default. Statement-level read consistency via undo |
| Repeatable Read | Snapshot taken at the first statement; no phantoms | Locks held to end of transaction | Not offered |
| Serializable | Serializable Snapshot Isolation: may abort with 40001 | Range locks | Transaction-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
NOLOCKhints 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
- Which anomalies can occur under PostgreSQL Read Committed?
- Why does PostgreSQL Repeatable Read prevent phantoms although the standard allows them?
- Describe write skew with an example from your own domain and two ways to prevent it.
References
- PostgreSQL manual, Transaction Isolation: https://www.postgresql.org/docs/current/transaction-iso.html
- Microsoft Learn, SET TRANSACTION ISOLATION LEVEL and Snapshot isolation in SQL Server
- Oracle Database Concepts, Data Concurrency and Consistency
Related
locking-and-mvcc · transactions-acid · pg-mvcc · sqlserver-isolation-rcsi