Sub topic 4
What Goes Wrong Without Control: Dirty Reads, Lost Updates, and More
Assume you know basic tables, rows, SELECT, and UPDATE statements. Also assume you can read a schedule as an interleaving of operations from several transactions. A transaction is a database operation sequence that should appear to run as one unit. Without concurrency control, interleaving can expose intermediate results or overwrite valid work.
The running example
Suppose an account row contains balance = 1000. Two transactions run at the same time:
T1withdraws 100.T2withdraws 50.
A serial execution runs one complete transaction before the other. A concurrent execution interleaves their operations. The interleaving is safe only if it produces a result equivalent to some correct serial order.
Dirty read
A dirty read occurs when one transaction reads data written by another transaction before that write is committed.
| Schedule | What happens |
|---|---|
| `T1: W(A); T2: R(A); T1: abort` | `T2` reads a value that never becomes permanent |
For example, T1 changes a balance from 1000 to 900. Before T1 commits, T2 reads 900 and approves a purchase based on it. If T1 aborts, the database restores 1000, but T2 used a value that was never valid in the committed database state.
A dirty read can cause incorrect reports, decisions, or updates. Preventing it requires readers to avoid uncommitted writes.
Lost update
A lost update occurs when two transactions read the same old value, calculate new values independently, and then one write overwrites the other.
Consider this schedule:
T1: R(balance)reads 1000.T2: R(balance)reads 1000.T1calculates 900 and writes 900.T2calculates 950 and writes 950.
The final balance is 950. The withdrawal by T1 disappeared. The correct result should be 850.
The problem is the read-modify-write sequence. Each transaction believes it owns the value it read. A database can prevent this with locks, serializable execution, or an atomic update such as balance = balance - 100.
Unrepeatable read
An unrepeatable read occurs when a transaction reads the same row twice and gets different committed values because another transaction updated and committed between the reads.
Example:
T1: R(A)reads 100.T2: W(A); commitchangesAto 120.T1: R(A)reads 120.
T1 cannot rely on a stable view of the row during its work.
Phantom read
A phantom read involves a predicate query, which selects rows matching a condition. A transaction repeats the query and finds additional rows, or finds that matching rows disappeared, because another transaction inserted or deleted rows.
For example, T1 counts all accounts with balance above 10,000 and gets 3. T2 inserts a fourth qualifying account and commits. When T1 repeats the count, it gets 4. The new row is a phantom.
Row locks alone may not prevent phantoms because the new row did not exist when the first query ran. Preventing them may require range or predicate locking, or a suitable multiversion isolation level.
Distinguishing the anomalies
- Dirty read: reads another transaction's uncommitted write.
- Lost update: one write overwrites another transaction's update.
- Unrepeatable read: the same row changes between two reads.
- Phantom read: the set of rows matching a condition changes.
Exercise
In the schedule T1: R(A); T2: W(A); commit; T1: R(A), which anomaly can occur, assuming the two reads return different values?
Show answer
An unrepeatable read. T1 reads the same row twice, but T2 commits a change between those reads.