Repeatable read

On this page 7

Repeatable read is a transaction isolation level defined by the SQL standard that guarantees a transaction sees the same values every time it reads the same row, even if other sessions commit changes to that row in the meantime. In the study of transactions in databases, it sits between read committed and serializable isolation, and it is the default level in MySQL with InnoDB.

The level exists to remove one specific anomaly, the non-repeatable read, while keeping concurrency higher than full serializability allows. How much more it prevents depends heavily on the engine, because vendors implement the same name with locking, multiversion concurrency control or a mix of both.

What the SQL standard requires at repeatable read

The ANSI/ISO SQL standard defines repeatable read by the anomalies it forbids, not by a mechanism. A transaction at this level must never observe a dirty read (uncommitted data from another session) and must never observe a non-repeatable read, where a row read twice returns different committed values. The standard still permits phantom reads, in which a repeated range query returns rows that were not there before.

The table below places repeatable read among the four standard levels and shows which classic anomalies each level is required to prevent.

Isolation levelDirty readNon-repeatable readPhantom read
Read uncommittedPossiblePossiblePossible
Read committedPreventedPossiblePossible
Repeatable readPreventedPreventedPossible
SerializablePreventedPreventedPrevented
Anomalies prevented by each standard isolation level

Researchers have long pointed out that this anomaly-based definition is ambiguous and incomplete. It says nothing explicit about lost updates or write skew, so two products can both claim repeatable read while behaving very differently under concurrent writes.

How engines implement repeatable read

Two broad strategies deliver the guarantee. A lock-based implementation holds shared read locks until commit on every row the transaction has read, so no other session can modify those rows until it finishes. An MVCC implementation instead gives the transaction a consistent snapshot taken at its first read and serves older row versions from undo or version storage, so readers never block writers.

The following table compares how widely used systems interpret the level, which matters far more in practice than the standard text.

EngineMechanismPhantomsConcurrent write conflict
MySQL InnoDBSnapshot for plain reads, next-key locks for locking readsMostly prevented by gap locksUpdates apply to latest committed row
PostgreSQLTransaction-wide snapshotPreventedSerialization failure, retry required
SQL ServerShared locks held to commitPossibleBlocking, possible deadlocks
IBM Db2Called read stability, locks held to commitPossibleBlocking
Oracle DatabaseLevel not offeredNot applicableUse serializable or read committed
Repeatable read in common database engines

PostgreSQL is the clearest example of the gap between name and behavior: its repeatable read is effectively snapshot isolation, which is stronger than the standard demands for phantoms but still allows write skew. InnoDB mixes models, so a plain SELECT reads the snapshot while UPDATE and SELECT … FOR UPDATE read and lock the latest committed version.

Anomalies that repeatable read still allows

Repeatable read does not make concurrent transactions equivalent to a serial order. Several anomalies survive, and which ones survive depends on the engine. Knowing them is the main skill needed to use the level safely.

  • Phantom reads: under lock-based engines, a second range query can return newly inserted rows because only existing rows are locked.
  • Write skew: two transactions read overlapping data, each checks a constraint, and each writes a different row, so the combined result violates the constraint.
  • Lost updates in InnoDB: a read-modify-write cycle based on a snapshot read can overwrite a concurrent change unless the read uses FOR UPDATE.
  • Read-only transaction anomalies: under snapshot-based engines, a read-only report can observe a state that no serial order would produce.

The next table maps these risks to their typical symptom and the standard countermeasure.

RiskTypical symptomCountermeasure
Write skewTwo on-call doctors both go off dutySerializable level or explicit row locks
Lost updateCounter increments disappearAtomic UPDATE or SELECT … FOR UPDATE
Phantom insertDuplicate booking in a time rangeUnique or exclusion constraint, range lock
Serialization failureError SQLSTATE 40001 under loadRetry the whole transaction
Residual risks and how to address them

Performance and concurrency trade-offs

The cost of repeatable read depends on its mechanism. In lock-based engines, holding read locks until commit reduces throughput and raises deadlock frequency, especially when transactions scan many rows. In MVCC engines, readers do not block, but long transactions keep old row versions alive, which inflates undo logs in InnoDB and delays vacuum cleanup in PostgreSQL.

Snapshot-based repeatable read also shifts conflict handling to the application. When two transactions update the same row, PostgreSQL aborts the later one with a serialization failure, so code must be written to retry entire transactions, not single statements. Retry loops are cheap when conflicts are rare and expensive on hot rows.

Choosing repeatable read in practice

Repeatable read is the right choice when a transaction must see a stable, consistent view across several queries and full serializability is too costly. The decision table below summarizes common situations.

SituationRecommended levelReason
Multi-query financial reportRepeatable readAll queries see one consistent snapshot
Consistent logical backup or exportRepeatable readDump reflects a single point in time
Simple single-statement web requestsRead committedLower overhead, fewer retries
Invariants spanning several rowsSerializablePrevents write skew
Hot counters and balancesRead committed with atomic updatesAvoids retry storms
When to choose repeatable read

Set the level per transaction with SET TRANSACTION ISOLATION LEVEL REPEATABLE READ rather than changing a server default blindly. Keep such transactions short, test them under realistic concurrency, and never assume portability: the same code can be safe on one engine and incorrect on another.

The short version

Imagine taking a photograph of a shared spreadsheet when you start a task. For as long as your task runs, every time you look at a row you have already seen, you see it exactly as it was in your photograph, even if colleagues edit the live sheet.

That is the idea of repeatable read: what you have read stays the same until you finish. It protects you from numbers shifting under you, but it does not stop every conflict. Two people working from their own photographs can still make decisions that clash, which is why the strictest setting, serializable, exists.

Where it makes a difference

An accounting team runs an end-of-month report on a PostgreSQL database that keeps processing orders around the clock. The report issues about 40 queries over six minutes: revenue by region, open invoices, refunds and a final reconciliation total. Under read committed, each query saw newer data, so the regional totals did not add up to the grand total and analysts spent hours chasing differences that were not real.

The team wraps the report in a single transaction at repeatable read. Every query now reads the same snapshot, and the sections reconcile to the cent. Because the report only reads, it never triggers serialization failures, and order processing continues without blocking. The one adjustment is operational: the team schedules the report away from vacuum-heavy maintenance windows, since a long-lived snapshot delays cleanup of old row versions.