Sub topic 9
Reclaiming Space: Pruning, HOT Updates, and VACUUM
This chapter assumes basic SQL, familiarity with psql, a general idea of transactions, and basic knowledge of files and bytes on disk. It also assumes you understand that PostgreSQL uses multiversion concurrency control (MVCC), which keeps multiple versions of a row so transactions can see a consistent snapshot.
Why updates leave old row versions
An UPDATE usually does not overwrite a row in place. PostgreSQL creates a new row version and marks the old version as no longer current. A DELETE marks the existing version as deleted. The old versions are called dead tuples after no transaction can see them.
This design lets readers continue without waiting for writers. It also means that an active table can accumulate dead tuples:
CREATE TABLE accounts (
id integer PRIMARY KEY,
balance numeric
);
INSERT INTO accounts VALUES (1, 100);
UPDATE accounts
SET balance = balance + 50
WHERE id = 1;
DELETE FROM accounts
WHERE id = 1;The old versions still occupy space on their heap page. PostgreSQL can reuse that space, but it does not immediately remove the table's data file or return the space to the operating system.
Page pruning
Page pruning is a local cleanup operation. PostgreSQL can perform it when it needs to modify a page. It examines chains of row versions on that page and removes or redirects versions that are no longer visible to any transaction.
Pruning is opportunistic. It normally affects one page rather than scanning the entire table. It can make free space available for another tuple on that same page.
A version chain might look like this:
The old tuple's line pointer may be redirected to the newest version, marked unused, or otherwise adjusted according to the chain's state. Pruning does not depend on the regular table-wide work of VACUUM, although it uses the same visibility rules.
HOT updates
A Heap-Only Tuple, or HOT, update is an optimization for updates that do not change any indexed column and can place the new version on the same heap page.
For example, if balance is not indexed, this update may be HOT:
UPDATE accounts
SET balance = balance + 10
WHERE id = 1;The index entry still points to the original heap location. PostgreSQL follows the tuple chain from that location to find the current version. Because no new index entry is needed, a HOT update reduces index growth and write work.
HOT is not guaranteed. PostgreSQL needs free space on the original page, and changing an indexed column prevents the optimization.
VACUUM
VACUUM scans a table and identifies dead tuples that are no longer needed by any transaction. It makes their space reusable, updates the free space map (FSM), and can update the visibility map (VM). The FSM records pages with available space. The VM records pages whose tuples are known to be visible to all transactions.
VACUUM accounts;
SELECT
relname,
n_live_tup,
n_dead_tup,
last_vacuum,
last_autovacuum
FROM pg_stat_user_tables
WHERE relname = 'accounts';Regular VACUUM usually does not shrink the table file. It prepares existing pages for reuse. VACUUM FULL rewrites the table into a smaller file, but it requires stronger locking and can require substantial temporary disk space.
Autovacuum runs VACUUM automatically when update and delete activity crosses configured thresholds. Monitor n_dead_tup, transaction age, and table growth when cleanup appears delayed.
Choosing the cleanup operation
| Operation | Scope | Main result |
|---|---|---|
| Page pruning | Individual heap pages | Makes obsolete space reusable locally |
| Regular VACUUM | Whole table | Reclaims dead tuples for reuse and updates FSM/VM |
| VACUUM FULL | Whole table rewrite | Shrinks the file but needs stronger locking |
Exercise
Exercise
Why can an UPDATE that changes only a non-indexed column still fail to become a HOT update?
Show answer
HOT requires the new tuple version to fit on the same heap page. If that page has insufficient free space, PostgreSQL must use a regular update instead.