Sub topic 13
SQL Isolation Levels and the Anomalies They Allow
Prerequisites and working model
This chapter assumes you know tables, rows, SELECT, UPDATE, and transactions. It also assumes you can read a schedule as an interleaving of operations from different transactions.
An isolation level controls which effects of one transaction can be observed by another transaction while both run concurrently. Stronger isolation usually reduces anomalies, but it can also reduce concurrency or cause transactions to wait or retry.
The main anomalies
A dirty read occurs when a transaction reads data written by another transaction that has not committed. If the writer later rolls back, the reader used a value that never became permanent.
A nonrepeatable read occurs when a transaction reads the same row twice and gets different committed values because another transaction updated and committed between the reads.
A phantom read occurs when a transaction repeats a predicate query and finds a different set of rows. Another transaction inserted, deleted, or changed rows so that they now match the predicate.
A lost update occurs when two transactions read the same value and then write results based on that old value. One write overwrites the other.
For example, suppose an account balance is 500:
-- Session A
BEGIN TRANSACTION ISOLATION LEVEL READ COMMITTED;
SELECT balance FROM account WHERE id = 1;
-- returns 500
-- Session B
BEGIN;
UPDATE account SET balance = 450 WHERE id = 1;
COMMIT;
-- Session A
SELECT balance FROM account WHERE id = 1;
-- may now return 450
COMMIT;Session A experienced a nonrepeatable read. Under READ COMMITTED, each statement normally sees data committed before that statement began.
Isolation levels
The SQL standard defines four named levels. Database products can implement them differently, especially when they use multiversion concurrency control (MVCC), which keeps several committed versions of a row.
| Isolation level | Dirty reads | Nonrepeatable reads | Phantoms | Typical behavior |
|---|---|---|---|---|
| `READ UNCOMMITTED` | Allowed | Allowed | Allowed | May read uncommitted changes |
| `READ COMMITTED` | Prevented | Allowed | Allowed | Each statement sees committed data |
| `REPEATABLE READ` | Prevented | Prevented | Product-dependent | Transaction keeps a stable row view |
| `SERIALIZABLE` | Prevented | Prevented | Prevented | Result matches some serial execution |
READ UNCOMMITTED provides the weakest protection. It can be useful only when approximate data is acceptable.
READ COMMITTED is common for general workloads. It prevents dirty reads, but a transaction must not assume that a later query sees the same database state.
REPEATABLE READ keeps rows already read stable for the transaction. Some systems also prevent phantoms; others require extra locking or use snapshot behavior that differs from the SQL standard.
SERIALIZABLE gives the strongest guarantee. The database may lock rows and ranges, detect conflicts, or abort a transaction with a serialization error. Applications must be prepared to retry aborted transactions.
Choosing an isolation level
Choose the weakest level that preserves the correctness of the operation. A report may tolerate changing values, while transferring money requires protection against conflicting updates. For a critical read-modify-write operation, use a locking technique such as SELECT ... FOR UPDATE, an atomic update, or serializable execution.
Always check the documentation for the specific database system. Isolation names do not guarantee identical behavior across products.
Exercise
Which anomaly is possible when a transaction reads the same row twice and another transaction commits an update between the two reads?
Show answer
A nonrepeatable read. READ COMMITTED prevents dirty reads but normally allows this anomaly.