Sub topic 12
MVCC: Readers That Do Not Block Writers
Prerequisites
This chapter assumes you know basic SQL, transactions, primary keys, and constraints. You should also understand that two database sessions can run at the same time.
Multiversion concurrency control (MVCC) is a way to manage concurrent access by keeping more than one version of a row. A reader sees a suitable committed version instead of waiting for a writer to finish. This lets many reads continue while updates are in progress.
How MVCC works
With traditional locking, a reader may wait for a writer that holds a lock. With MVCC, an UPDATE creates a newer row version. The old version remains available to transactions whose snapshot should see it.
A snapshot is the transaction's view of which row versions are visible. Visibility depends on whether a version was committed before the snapshot and whether a newer version should replace it.
The exact implementation differs by database. PostgreSQL stores row versions in the table and tracks transaction metadata. Other systems may store versions in separate structures. The shared idea is the same: readers use visibility rules instead of reading uncommitted changes.
A reader and writer at the same time
The following example uses PostgreSQL. Run the setup first, then use two database sessions.
CREATE TABLE accounts (
id integer PRIMARY KEY,
balance numeric(12, 2) NOT NULL
);
INSERT INTO accounts VALUES (1, 70.00);
-- Session A
BEGIN ISOLATION LEVEL REPEATABLE READ;
SELECT balance FROM accounts WHERE id = 1;
-- 70.00
-- Session B, while Session A remains open
BEGIN;
UPDATE accounts SET balance = 50.00 WHERE id = 1;
COMMIT;
-- Session A
SELECT balance FROM accounts WHERE id = 1;
-- Still 70.00
COMMIT;
-- A new transaction sees the latest committed version
SELECT balance FROM accounts WHERE id = 1;
-- 50.00Session B does not wait for Session A's ordinary SELECT. Session A keeps seeing the version that was visible when its transaction began. Under PostgreSQL's READ COMMITTED default, each statement normally gets a new snapshot, so a second SELECT could see 50.00 after Session B commits.
MVCC does not remove every wait. Two updates to the same row can still conflict because the database must prevent lost updates. A writer may wait for another writer, and a locking read such as SELECT ... FOR UPDATE deliberately requests a lock.
Cleanup and trade-offs
Old versions cannot be removed while an active transaction might still need them. A cleanup process, such as PostgreSQL's VACUUM, later reclaims versions that no transaction can see.
MVCC provides good read concurrency, but it has costs:
- Updates create old and new versions, increasing storage and cleanup work.
- Long-running transactions can prevent cleanup and cause table or index growth.
- Snapshot isolation can still allow write skew unless the database uses serializable checks or suitable locks.
- Applications must still handle writer conflicts, deadlocks, and serialization failures.
Exercise
Exercise
Under MVCC, why can a reader see the old value after another transaction has committed an update?
Show answer
The reader uses a snapshot whose visibility rules select the older row version. A later transaction gets a newer snapshot and sees the updated version.