Sub topic 8
Updates and Deletes Create Row Versions
This chapter assumes basic SQL, familiarity with psql, and a general idea of transactions. You should know how to create a table, change rows, and query system columns.
PostgreSQL uses multiversion concurrency control (MVCC). MVCC keeps more than one physical version of a row when a transaction changes it. Readers can continue using an older version while another transaction creates a newer one.
An UPDATE creates a new version
An UPDATE usually does not overwrite the existing row bytes in place. PostgreSQL writes a new tuple, or physical row version, and marks the old tuple as no longer current.
The old tuple records the transaction that replaced it in its xmax field. The new tuple records the creating transaction in its xmin field. The old and new versions are connected through their ctid values.
DROP TABLE IF EXISTS accounts;
CREATE TABLE accounts (
id integer PRIMARY KEY,
owner text,
balance numeric
);
INSERT INTO accounts VALUES
(1, 'alice', 100),
(2, 'bob', 200);
BEGIN;
UPDATE accounts
SET balance = balance + 50
WHERE id = 1;
SELECT ctid, xmin, xmax, id, owner, balance
FROM accounts
ORDER BY id;
COMMIT;A normal SELECT shows the version visible to the current transaction. It does not normally show the obsolete version. A page inspection can show both versions before cleanup removes or reuses the old space.
xmin and xmax are transaction identifiers, not timestamps. A tuple with xmax set to zero has no transaction recorded as deleting or replacing it. Visibility also depends on whether the relevant transactions committed and on the snapshot used by the reader.
A DELETE marks a version as dead
A DELETE also avoids immediately erasing the row. It records the deleting transaction in the tuple's xmax. Until that transaction commits, other transactions may still see the row. After commit, newer snapshots treat the tuple as deleted.
If a long-running transaction still needs the old version, PostgreSQL must retain it. This allows readers and writers to work concurrently, but it means that changes can leave dead tuples behind.
Seeing versions on a page
The pageinspect extension exposes tuple metadata for diagnostics. It requires suitable privileges and should be used on a test table or during controlled investigation.
CREATE EXTENSION IF NOT EXISTS pageinspect;
SELECT ctid, xmin, xmax, id, balance
FROM accounts;
UPDATE accounts
SET balance = balance + 25
WHERE id = 1;
SELECT lp, lp_flags, t_xmin, t_xmax, t_ctid
FROM heap_page_items(get_raw_page('accounts', 0))
WHERE lp_flags <> 0;The lp value identifies a line pointer in the page. t_ctid identifies the tuple's physical location using a block number and line-pointer number. After an update, the old tuple's t_ctid may point to the new version. The exact page and pointer values depend on the table's current layout, so do not treat them as permanent row identifiers.
HOT updates and cleanup
When an update changes no indexed column and the new tuple fits on the same page, PostgreSQL may use a HOT (Heap-Only Tuple) update. The new version does not require a new index entry. This reduces index work, but it still creates a row-version chain.
After old versions are no longer needed by any active snapshot, PostgreSQL can prune them from a page. VACUUM also marks dead space as reusable and updates visibility information. Cleanup is therefore separate from the update itself.
Exercise
Why can a table become larger after many UPDATE statements even when its logical row count stays the same?
Show answer
Each UPDATE can create a new physical row version while the old version remains until it is no longer visible to active transactions and cleanup reclaims its space.