Sub topic 13
SQL Isolation Levels and the Anomalies They Allow
This chapter assumes you know transactions, ACID, schedules, and the concurrency anomalies (dirty read, unrepeatable read, phantom, lost update). It also assumes you know shared and exclusive locks and strict two-phase locking (2PL). Here we connect those ideas to the four isolation levels that SQL defines, and to what real systems do.
Why isolation levels exist
Full serializability is safe but costly. Transactions wait on locks, deadlocks occur, and throughput drops. Many applications can tolerate some anomalies in exchange for speed. SQL therefore lets each transaction declare how much interference from other transactions it accepts. This setting is the isolation level.
The levels are defined by which anomalies they forbid. The standard names three:
- Dirty read: T1 reads a value written by T2 before T2 commits. If T2 aborts, T1 has read data that never existed.
- Unrepeatable (non-repeatable) read: T1 reads a row twice and gets different values because T2 updated and committed the row in between.
- Phantom read: T1 runs the same range query twice (for example
WHERE salary > 50000) and the second run returns extra or missing rows because T2 inserted or deleted matching rows.
The standard table
This table is the most common GATE question in this area. Learn it exactly.
| Isolation level | Dirty read | Unrepeatable read | Phantom |
|---|---|---|---|
| Read Uncommitted | Possible | Possible | Possible |
| Read Committed | Not possible | Possible | Possible |
| Repeatable Read | Not possible | Not possible | Possible |
| Serializable | Not possible | Not possible | Not possible |
Each level forbids everything the level above it forbids, plus one more anomaly. Only Serializable guarantees that the outcome equals some serial execution.
How locking implements each level
In a lock-based system the levels differ in how long locks are held and what they cover. Exclusive locks on written items are held until commit in every level, which is what stops dirty writes.
| Level | Read locks | Write locks |
|---|---|---|
| Read Uncommitted | None | Exclusive, held to commit |
| Read Committed | Shared, released right after the read (short) | Exclusive, held to commit |
| Repeatable Read | Shared on rows, held to commit | Exclusive, held to commit |
| Serializable | Shared on rows plus range or predicate locks, held to commit | Exclusive, held to commit |
Work through the reasoning:
- Read Uncommitted takes no shared lock, so a read never waits for a writer's exclusive lock. That is why it can see uncommitted data.
- Read Committed takes a shared lock for each read, so it must wait for any uncommitted writer. It then releases the lock at once. A later writer can change the row, so a second read may differ.
- Repeatable Read keeps row read locks until commit. No one can change a row you have read. But the lock covers only rows that existed when you scanned. A new row inserted into your range is not locked, so phantoms appear.
- Serializable adds locks on the range or predicate (index-range or gap locks), which block inserts that would match your query. Together with long read and write locks, this is strict 2PL plus phantom protection.
The long read locks of Repeatable Read and Serializable make them two-phase. Read Committed is not two-phase, because it releases read locks before it finishes acquiring others.
Example: a non-repeatable read
The following runs in PostgreSQL. It shows the difference between Read Committed (the default) and Repeatable Read.
-- Setup
CREATE TABLE account (id INT PRIMARY KEY, balance INT);
INSERT INTO account VALUES (1, 500);
-- Session 1 (Read Committed) -- Session 2
BEGIN ISOLATION LEVEL READ COMMITTED;
SELECT balance FROM account WHERE id = 1; -- 500
BEGIN;
UPDATE account SET balance = 400 WHERE id = 1;
COMMIT;
SELECT balance FROM account WHERE id = 1; -- 400 (unrepeatable read)
COMMIT;If Session 1 had started with BEGIN ISOLATION LEVEL REPEATABLE READ;, the second SELECT would still return 500, because the transaction reads from a snapshot taken at its first query.
Use SET TRANSACTION ISOLATION LEVEL SERIALIZABLE; as the first statement of a transaction to request a level in standard SQL.
Phantoms versus unrepeatable reads
Students often confuse these two. The difference is what changed.
- An unrepeatable read involves an existing row whose value changed (an
UPDATE, or a delete of a row you already read). - A phantom involves the set of rows matching a predicate changing because of an
INSERT(or a delete or update that moves rows in or out of the predicate).
Row locks fix the first and cannot fix the second. You cannot lock a row that does not exist yet. That is why Repeatable Read, by the standard, still permits phantoms.
Where the standard falls short
The ANSI definitions have known gaps. These matter in practice and appear in "conceptual" questions.
Lost updates and write skew
The standard table does not list lost update, but it is real. Two transactions read balance = 500, each compute a new value, and each write. The second write overwrites the first. This can happen at Read Uncommitted and Read Committed when the code reads and then writes in separate statements. The fix is either a locking read (SELECT ... FOR UPDATE, which holds an exclusive lock until commit) or a higher level.
Write skew is a subtler anomaly. Two transactions read overlapping data, then each updates a different row, and the combination breaks an invariant. For example, a hospital rule says at least one doctor must stay on call. Two on-call doctors each check "is another doctor on call?" and both go off call. Each transaction wrote a different row, so no write-write conflict is detected.
Snapshot isolation is not Serializable
Many systems implement Repeatable Read using snapshot isolation, a multiversion scheme (covered in the later MVCC chapter). Each transaction reads from a consistent snapshot, and conflicting writers to the same row are resolved by aborting one ("first committer wins").
Snapshot isolation has these properties:
- It prevents dirty reads, unrepeatable reads, and (for plain snapshot reads) phantoms. It is therefore stronger than the standard Repeatable Read.
- It still allows write skew, so it is not serializable.
| System | Repeatable Read behavior | Serializable behavior |
|---|---|---|
| PostgreSQL | Snapshot isolation; no phantoms, write skew possible | Serializable Snapshot Isolation, which detects dangerous patterns and aborts one transaction |
| MySQL (InnoDB) | Snapshot reads plus gap locks for locking reads | Reads become locking reads (shared locks) |
| Oracle | No Repeatable Read level offered | Snapshot-based, still allows write skew |
The exact behavior depends on the product version, so treat the table as a guide. GATE questions use the standard table, not product behavior.
Dirty writes
All levels, including Read Uncommitted, must prevent dirty writes: overwriting another transaction's uncommitted write. Without this, rollback becomes impossible, because restoring a before-image could erase another transaction's change.
Choosing a level
Use these criteria:
- Read Committed suits most short transactions where each statement needs only committed data. Beware read-modify-write patterns.
- Repeatable Read suits reports that read the same data several times and need a stable view.
- Serializable suits invariants spanning several rows, such as the on-call example. The cost is aborts. Your application must catch a serialization failure and retry the whole transaction.
- Read Uncommitted is rarely useful. Approximate counts are one use. Several systems silently treat it as Read Committed.
GATE shortcuts
- If a question asks which anomaly a level allows, use the standard table, not product behavior.
- Dirty read means reading uncommitted data. Unrepeatable read means the same row differs. Phantom means the row set differs.
- A level that holds read locks to commit, but takes no range locks, allows phantoms.
- Strict 2PL on rows alone gives Repeatable Read behavior. Add predicate locking to get Serializable.
Exercise
A transaction T1 at Repeatable Read (lock-based, row locks only) runs SELECT COUNT(*) FROM emp WHERE dept = 'HR' twice. Between the two runs, T2 inserts a new HR employee and commits. T1 gets different counts. (a) Which anomaly is this? (b) Why did T1's read locks not prevent it? (c) What is the weakest standard level that prevents it?
Show answer
(a) A phantom read. (b) T1 held shared locks only on the rows it had read. The new row did not exist yet, so no lock covered it and T2's insert was allowed. (c) Serializable, which adds range or predicate locks so inserts matching the query condition must wait.
Exercise
Under which standard isolation levels can a dirty read occur, and under which levels can an unrepeatable read occur?
Show answer
A dirty read can occur only at Read Uncommitted. An unrepeatable read can occur at Read Uncommitted and Read Committed.
Exercise
Two on-call doctors each run: read the count of on-call doctors (2), and if it is greater than 1, set their own row to off-call. Both commit under snapshot isolation. Name the anomaly and say whether snapshot isolation prevents it.
Show answer
This is write skew. Snapshot isolation does not prevent it, because the two transactions write different rows and so have no write-write conflict. Running the transactions at true Serializable, or using a locking read on the shared rows, prevents it.