Sub topic 1
How MVCC Keeps Old Row Versions
Prerequisites
This chapter assumes basic SQL, familiarity with PostgreSQL clients such as psql, and a general understanding of transactions, commits, and isolation levels.
The MVCC working model
PostgreSQL uses MVCC, or multi-version concurrency control. MVCC lets readers and writers work at the same time by keeping multiple versions of a row.
A transaction reads from a snapshot. A snapshot is the transaction's view of which other transactions have committed. PostgreSQL checks each row version against that snapshot before returning it.
A row version is sometimes called a tuple. PostgreSQL records transaction information with each tuple, including:
- The transaction that created the tuple.
- The transaction that replaced or deleted the tuple.
- Whether those transactions committed.
This information lets PostgreSQL decide whether a tuple is visible to a particular reader.
What UPDATE does
An UPDATE does not normally overwrite a row in place. Instead, PostgreSQL writes a new row version and marks the previous version as no longer current.
For example:
DROP TABLE IF EXISTS accounts;
CREATE TABLE accounts (
account_id integer PRIMARY KEY,
balance numeric NOT NULL
);
INSERT INTO accounts (account_id, balance)
VALUES (1, 100);
BEGIN;
UPDATE accounts
SET balance = 125
WHERE account_id = 1;
COMMIT;
SELECT account_id, balance
FROM accounts;The final query returns 125, but the table may still contain the older version with balance = 100. That older version is useful if a transaction that started earlier still needs to see the old value.
The row versions form a logical chain:
The old version is not immediately removed because an active transaction might still need it. PostgreSQL must preserve a consistent view for that transaction.
What DELETE does
A DELETE follows a similar pattern. PostgreSQL marks the existing tuple as deleted by the deleting transaction. It does not immediately erase every byte from the table file.
If no active snapshot can see the deleted tuple, PostgreSQL can later remove it during VACUUM. Until then, the tuple is called a dead tuple.
A dead tuple is no longer visible to normal queries, but it still occupies space. Repeated updates and deletes can therefore leave many dead tuples behind.
Why readers do not block writers
Suppose one transaction updates a row while another transaction reads it. The reader can use the older visible version instead of waiting for the writer to finish. After the writer commits, later snapshots see the new version.
This design improves concurrency, but it transfers work to cleanup. PostgreSQL must eventually determine that old versions are invisible to every transaction and reclaim their space.
The table's physical file usually does not become smaller after ordinary VACUUM. Instead, VACUUM marks freed space for reuse by later inserts and updates. Removing dead tuples and shrinking the file are separate operations.
Exercise
Exercise
An application updates every row in a table once per hour. The table contains one million logical rows, but its file keeps growing. Why can this happen even though the row count stays constant?
Show answer
Each UPDATE creates a new row version and leaves the previous version for cleanup. Until VACUUM removes those dead tuples and marks their space reusable, repeated updates can increase the table's physical size.