Sub topic 1
MVCC: Why Readers Do Not Block Writers
Prerequisites
This chapter assumes that you know basic SQL, including SELECT and UPDATE, and understand BEGIN, COMMIT, and ROLLBACK. You should also be able to connect to PostgreSQL from two concurrent sessions.
The MVCC working model
PostgreSQL uses multiversion concurrency control, or MVCC. MVCC keeps multiple versions of a row so that each transaction can read a consistent view of the database.
When a transaction runs a normal SELECT, PostgreSQL gives it a snapshot. A snapshot is the transaction's view of which row versions are visible. If another transaction later updates a row, the reader can continue using the older visible version while the writer creates a newer version.
This is why an ordinary reader usually does not block an ordinary writer, and a writer usually does not block an ordinary reader.
flowchart TD
A[Transaction A starts a SELECT] --> B[Reads visible row version]
C[Transaction B updates the row] --> D[Creates a new row version]
B --> E[A continues reading its snapshot]
D --> F[B commits]
F --> G[Later snapshots can see the new version]What happens during an update
An UPDATE does not simply overwrite the bytes that a reader is using. PostgreSQL creates a new row version and marks the old version as updated. The old version remains available to transactions whose snapshots require it.
Try this setup first:
CREATE TABLE accounts (
id integer PRIMARY KEY,
balance numeric NOT NULL
);
INSERT INTO accounts (id, balance)
VALUES (1, 100), (2, 100);Use two sessions. In session A, start a transaction and update account 1:
BEGIN;
UPDATE accounts
SET balance = balance - 10
WHERE id = 1;Before committing session A, run this in session B:
SELECT id, balance
FROM accounts
WHERE id = 1;Session B can normally return the previously committed balance, 100. It does not wait for session A's uncommitted update. Session A then commits:
COMMIT;A new query in session B can see the committed balance, 90.
The behavior changes when session B also tries to update the same row:
BEGIN;
UPDATE accounts
SET balance = balance + 5
WHERE id = 1;This update waits. Both transactions want to modify the same row, so PostgreSQL uses a row-level lock to serialize the changes. The waiting transaction proceeds after the first transaction commits or rolls back.
| Operation | Usual interaction with a concurrent update |
|---|---|
| Ordinary `SELECT` | Reads a visible version without waiting for the row update |
| `UPDATE` of the same row | Waits for the other update to finish |
| `SELECT ... FOR UPDATE` | Reads and locks the row for later modification |
| `ALTER TABLE` | May wait for conflicting table-level locks |
When readers still matter
A long-running reader can prevent PostgreSQL from removing old row versions. PostgreSQL's vacuum process needs to preserve versions that an old snapshot might still read. If a transaction remains open for a long time, dead row versions can accumulate, increasing table size and slowing queries.
Readers can also block operations that require stronger table locks. For example, many schema changes need an ACCESS EXCLUSIVE lock. An open transaction that has touched the table may delay that operation, even if its query was only a SELECT.
Use explicit row locks when a transaction must read a value and then safely act on that same row:
BEGIN;
SELECT balance
FROM accounts
WHERE id = 1
FOR UPDATE;
UPDATE accounts
SET balance = balance - 10
WHERE id = 1;
COMMIT;The FOR UPDATE clause makes the intention explicit. Other transactions trying to update or lock that row must wait.
Exercise
Why can an ordinary SELECT read account 1 while another transaction has an uncommitted UPDATE on account 1, but a second UPDATE must wait?
Show answer
MVCC lets the SELECT use the last committed row version from its snapshot. The second UPDATE needs to change the same row, so PostgreSQL uses a row-level lock to serialize the writers.