Sub topic 4
Non-Repeatable Reads
Assume you know basic SQL and can use BEGIN, COMMIT, and ROLLBACK. You should also understand that several database clients can run transactions at the same time.
A non-repeatable read happens when one transaction reads a row twice and gets different committed values each time. Another transaction changed and committed the row between the two reads.
The first read was valid when it happened. The second read is also valid. The problem is that one transaction cannot rely on its earlier result for the rest of its work.
A two-transaction schedule
Suppose an account has a balance of 100. Session A starts a transaction and reads the balance. Session B then changes the balance to 50 and commits. When Session A reads the same row again, it sees 50 instead of 100.
The name refers to the failed repeatability of the read, not to a failed query. The same query text runs twice, but the database state visible to the transaction has changed.
A dirty read is different. A dirty read sees data that another transaction has changed but not committed. A non-repeatable read sees data after the other transaction has committed.
Reproducing the problem
This example uses PostgreSQL's default READ COMMITTED level. Run Session A and Session B in separate database connections.
-- Run once to create the example data.
CREATE TABLE accounts (
id integer PRIMARY KEY,
balance integer NOT NULL
);
INSERT INTO accounts (id, balance) VALUES (1, 100);
-- Session A
BEGIN TRANSACTION ISOLATION LEVEL READ COMMITTED;
SELECT balance FROM accounts WHERE id = 1;
-- Result: 100
-- Pause here and run Session B.
-- Session A, after Session B commits:
SELECT balance FROM accounts WHERE id = 1;
-- Result: 50
COMMIT;
-- Session B
BEGIN;
UPDATE accounts
SET balance = 50
WHERE id = 1;
COMMIT;At READ COMMITTED, each statement normally gets its own view of committed data. The first SELECT sees the commits available when that statement starts. The second SELECT sees the later commit from Session B.
Preventing the change within a transaction
Use REPEATABLE READ when several statements must all work from one consistent view. In PostgreSQL, the transaction takes a snapshot when it begins. Later commits from other transactions are not visible to ordinary reads in that transaction.
-- Session A in PostgreSQL
BEGIN TRANSACTION ISOLATION LEVEL REPEATABLE READ;
SELECT balance FROM accounts WHERE id = 1;
-- Result: 100
-- Session B may update and commit the row here.
SELECT balance FROM accounts WHERE id = 1;
-- Still returns: 100
COMMIT;This consistency has a cost. A transaction may be working from an older value, so its final decision might no longer fit the current state. Stronger isolation can also increase waiting, conflict errors, or retries.
Choosing the right behavior
| Need | Typical choice |
|---|---|
| Each statement may use the latest committed data | READ COMMITTED |
| Several reads must agree during one transaction | REPEATABLE READ or a suitable snapshot mode |
Do not solve every non-repeatable read by raising isolation globally. First ask whether the transaction needs a consistent historical view, or whether it should lock the rows before making a decision.
Exercise
Session A reads a product price of 20 twice in one transaction. Session B changes the price to 25 and commits between those reads. What anomaly occurred, and which isolation level would commonly keep Session A's two reads consistent?
Show answer
This is a non-repeatable read. REPEATABLE READ would commonly give Session A one consistent snapshot for both reads.