Sub topic 1
Why PostgreSQL Keeps Multiple Versions of a Row
Prerequisites
This chapter assumes basic SQL, including SELECT, INSERT, UPDATE, and DELETE. You should also understand BEGIN, COMMIT, and ROLLBACK, and know how to connect to one PostgreSQL database from two psql sessions.
The core idea: readers and writers can proceed together
PostgreSQL uses multiversion concurrency control (MVCC). MVCC is a concurrency method that keeps more than one version of a row so that a query can read a consistent version while another transaction changes the same logical row.
An UPDATE does not overwrite a row in place in the usual sense. PostgreSQL writes a new physical row version and marks the old version as no longer current. A DELETE marks the existing version as deleted. The old version remains temporarily because a transaction that started earlier might still need to read it.
This design avoids making readers wait for most writers. It also avoids making writers wait for readers that only need to inspect data.
Row versions live in the heap
PostgreSQL stores table rows in heap pages. A physical row representation is called a tuple. A logical row, such as the account with id = 1, can therefore have several tuples over time.
Run this example in a test database:
CREATE TEMP TABLE accounts (
id integer PRIMARY KEY,
balance integer NOT NULL
);
INSERT INTO accounts VALUES (1, 100);
SELECT xmin, xmax, ctid, id, balance
FROM accounts;PostgreSQL adds hidden system columns to each table. xmin identifies the transaction that created a tuple. xmax identifies a transaction that deleted or replaced it. A value of zero in xmax usually means that no deleting transaction has been recorded. ctid identifies the tuple's current physical location as a page and item number.
Now update the account:
BEGIN;
UPDATE accounts
SET balance = 90
WHERE id = 1;
SELECT xmin, xmax, ctid, id, balance
FROM accounts;
COMMIT;The visible tuple now has the new balance. Its ctid commonly differs from the earlier value, because the update created a new tuple. The old tuple still occupies space until PostgreSQL can safely remove or reuse it.
| Logical row | Physical versions over time |
|---|---|
| `id = 1` | `(balance = 100)` then `(balance = 90)` |
Why the old version matters
Suppose Session A begins a transaction and reads the account. Session B then updates and commits it. If Session A runs under an isolation level that preserves its original view, it may continue seeing the old tuple. PostgreSQL can provide that result because the old tuple has not yet been removed.
A snapshot is the transaction's view of which transaction IDs were committed, active, or not yet started at a particular point. PostgreSQL checks each tuple's transaction metadata against that snapshot. Visibility therefore depends on both the tuple version and the reader's snapshot.
The benefit is less reader-writer blocking. The cost is extra storage, tuple visibility checks, and cleanup work from VACUUM and autovacuum.
Exercise
Why does PostgreSQL create a new tuple for an UPDATE instead of simply replacing the old tuple?
Show answer
The old tuple may still be visible to a transaction that started before the update. Keeping it lets PostgreSQL provide a consistent snapshot without blocking that reader.