Sub topic 3
Reclaiming space: pruning, HOT updates, and VACUUM
This chapter assumes you know basic SQL, can run psql, and understand tuples, line pointers, and xmin/xmax from earlier chapters.
Dead tuples and pruning
An UPDATE writes a new row version and leaves the old one behind as a dead tuple. A DELETE only sets xmax on the old version. Dead tuples keep taking space until something reclaims them.
When a page is about to fill, PostgreSQL prunes it. Pruning removes the bodies of dead tuples that no transaction can still see and leaves their line pointers as redirects or unused slots.
HOT updates
A heap-only tuple (HOT) update avoids touching indexes. It applies when the new version fits on the same page and no indexed column changed. The old line pointer then redirects to the new tuple, so index entries stay valid. Keep fillfactor below 100 on frequently updated tables to leave room for HOT.
VACUUM and the free space map
VACUUM removes dead tuples fully, marks their line pointers reusable, and updates two maps:
- The free space map (FSM) records roughly how much free space each page has, so inserts can find room without scanning the table.
- The visibility map (VM) has one bit per page. A set bit means every tuple on the page is visible to all transactions. VACUUM skips those pages, and index-only scans can use them without checking the heap.
CREATE TABLE accounts (id int PRIMARY KEY, balance numeric);
INSERT INTO accounts SELECT g, 100 FROM generate_series(1, 1000) g;
UPDATE accounts SET balance = balance + 1;
VACUUM (VERBOSE) accounts;TOAST: values too large for a page
A page is 8 KB, and a row must fit within a page. When a row exceeds about 2 KB, PostgreSQL first compresses large values. If the row is still too big, it moves values out of line into a companion TOAST table. The main table keeps a small pointer.
Chunks and fetching
The TOAST table stores each value as chunks of roughly 2 KB. Each chunk has a chunk_id and a chunk_seq. To read a value, PostgreSQL uses an index on chunk_id to fetch the chunks in order and reassemble them. Reading only a few columns that are not TOASTed avoids this cost.
flowchart TD
A["Row update"] --> B["Old version becomes dead tuple"]
B --> C{"Fits on same page, no indexed column changed?"}
C -->|Yes| D["HOT update, no index change"]
C -->|No| E["New index entries added"]
D --> F["Pruning or VACUUM frees space"]
E --> F
F --> G["FSM and VM updated"]caption: How an UPDATE leads to dead tuples and reclaimed space. alt: A flowchart. An update creates a dead tuple. If the new version fits on the same page and no indexed column changed, it becomes a HOT update. Otherwise, new index entries are added. Either path leads to pruning or VACUUM, which updates the free space map and visibility map.
Storage strategies and compression
Each column has a storage strategy set by ALTER TABLE ... SET STORAGE:
| Strategy | Compressed | Stored out of line |
|---|---|---|
| PLAIN | No | No |
| EXTENDED | Yes | Yes (default for most types) |
| EXTERNAL | No | Yes |
| MAIN | Yes | Only as a last resort |
Set the compression method with SET COMPRESSION (pglz or lz4, where lz4 needs PostgreSQL 14 or later). Changes apply only to values written afterward.
ALTER TABLE accounts ALTER COLUMN balance SET STORAGE EXTERNAL;
ALTER TABLE accounts ALTER COLUMN balance SET COMPRESSION lz4;Exercise:
Exercise
After running ALTER TABLE ... SET STORAGE EXTERNAL on a column, does an existing large value become uncompressed?
Show answer
No. The setting applies only to values stored afterward. Existing values keep their old storage until the row is rewritten, for example by an UPDATE.