Sub topic 2
Row versions in the heap
This chapter assumes you know basic SQL and transactions, including BEGIN, COMMIT, and ROLLBACK. It also assumes you can connect to one PostgreSQL database with psql.
A table stores tuple versions
PostgreSQL stores table rows in a heap. A heap is the table's main storage area, organized into fixed-size pages. A row stored in a heap page is called a tuple.
An UPDATE usually does not overwrite the existing tuple in place. PostgreSQL writes a new tuple version instead. The old version remains on the page until VACUUM can reclaim it. This design is central to multiversion concurrency control, or MVCC.
MVCC is PostgreSQL's method for letting transactions read a suitable row version while another transaction writes a newer version. A reader can continue using the old version instead of waiting for the writer to replace it.
An update creates a new physical tuple
Run this example in a test database:
DROP TABLE IF EXISTS accounts;
CREATE TABLE accounts (
id integer PRIMARY KEY,
balance integer NOT NULL
);
INSERT INTO accounts VALUES (1, 100);
SELECT xmin, xmax, ctid, id, balance
FROM accounts;The result includes PostgreSQL system columns:
| Column | Meaning |
|---|---|
| `xmin` | Transaction ID that created this tuple version |
| `xmax` | Transaction ID that deleted or replaced this version; `0` means no such transaction is recorded |
| `ctid` | Physical tuple location, written as `(page, item)` |
The exact values depend on database activity, so do not expect the same transaction IDs or page locations on every system.
Now update the row:
BEGIN;
UPDATE accounts
SET balance = 90
WHERE id = 1;
SELECT xmin, xmax, ctid, id, balance
FROM accounts;
COMMIT;The visible tuple normally has a new ctid and a new xmin. The old tuple has been marked as replaced by the update transaction. A normal query hides that old version because PostgreSQL's visibility rules select the version appropriate for the current snapshot.
ctid is useful while investigating storage, but it is not a permanent row identifier. An update can change it, and operations such as VACUUM FULL can rewrite the table and change many locations.
Why old versions remain
Keeping old tuples allows a transaction that started earlier to continue seeing data from its snapshot. PostgreSQL cannot remove an old version while an active snapshot might still need it.
This creates a cleanup cycle:
- An
UPDATEorDELETEcreates a dead tuple version. - New snapshots stop needing that version.
VACUUMmarks its space reusable.- A later insert or update may use that space.
A table with frequent updates can therefore contain many dead tuples between vacuum runs. The next chapter examines transaction IDs, which PostgreSQL stores in tuple metadata to track these versions.
Exercise
After an UPDATE, why can the old tuple still occupy space even though a normal SELECT returns only the new value?
Show answer
The old tuple may still be needed by an active transaction's snapshot. VACUUM can reclaim it only after no eligible snapshot can see it.