Sub topic 13
HOT updates and fillfactor
This chapter assumes you know how an UPDATE creates a new row version, how xmin, xmax and ctid identify versions, and what VACUUM reclaims. You also need two psql sessions and a superuser role for the pageinspect extension.
The cost of a plain update
An ordinary UPDATE writes a new row version into the heap. Every index on the table also needs an entry that points to that new version. For a table with five indexes, a one-column change can mean six writes: one heap tuple and five index entries. Each index entry later becomes garbage that VACUUM has to remove.
Most updates change a column that no index uses, such as a balance, a counter or a status text. In that case the index entries would point to a different place but hold exactly the same key values. PostgreSQL has an optimization for this case, called a HOT update. HOT stands for Heap-Only Tuple.
What makes an update HOT
An update is HOT when both of these conditions hold:
- No column used by an index has changed. This includes columns in index expressions and in the
WHEREclause of a partial index. Since PostgreSQL 16, columns used only by summarizing indexes such as BRIN do not count. - The new version fits on the same heap page as the old version.
When both hold, PostgreSQL writes the new version on the same page and creates no new index entries. The existing index entries keep pointing at the first version in the chain. A scan that arrives through the index follows t_ctid links within the page until it reaches a version that is visible to its snapshot.
Seeing a HOT chain
Create a table with an index on a column that the updates will not touch. The fillfactor setting is explained in the next section.
CREATE EXTENSION IF NOT EXISTS pageinspect;
CREATE TABLE accounts_hot (
id int PRIMARY KEY,
balance int NOT NULL,
owner text NOT NULL
) WITH (fillfactor = 80);
CREATE INDEX accounts_hot_owner_idx ON accounts_hot (owner);
INSERT INTO accounts_hot
SELECT g, 100, 'owner' || g FROM generate_series(1, 100) AS g;
SELECT ctid, * FROM accounts_hot WHERE id = 1; -- (0,1)
UPDATE accounts_hot SET balance = 90 WHERE id = 1;
SELECT ctid, * FROM accounts_hot WHERE id = 1; -- same page 0, higher item numberThe page number in ctid is still 0. Only the item number changed. Now look at the page itself:
SELECT lp, lp_flags, t_ctid,
(t_infomask2 & 16384) <> 0 AS hot_updated,
(t_infomask2 & 32768) <> 0 AS heap_only
FROM heap_page_items(get_raw_page('accounts_hot', 0))
WHERE lp = 1 OR (t_infomask2 & 32768) <> 0;You should see item 1 flagged hot_updated, with a t_ctid that points to the new item. The new item is flagged heap_only, which means no index entry points to it directly. The two flags correspond to the HEAP_HOT_UPDATED (0x4000) and HEAP_ONLY_TUPLE (0x8000) bits in t_infomask2.
Counting HOT updates
The statistics view reports how many updates were HOT:
UPDATE accounts_hot SET balance = balance + 1 WHERE id <= 50; -- no indexed column changes
UPDATE accounts_hot SET owner = owner || 'x' WHERE id > 50; -- changes an indexed column
SELECT relname, n_tup_upd, n_tup_hot_upd
FROM pg_stat_user_tables
WHERE relname = 'accounts_hot';The counters are sent to the statistics system asynchronously, so run the SELECT again after a moment if the numbers look low. Here n_tup_hot_upd should be close to 51, which is the single-row update plus the 50 rows from the first batch. The second batch changes owner, which has an index, so none of those 50 updates can be HOT. The ratio n_tup_hot_upd / n_tup_upd is a quick health check for an update-heavy table.
Pruning: cleanup without VACUUM
Dead versions in a HOT chain are cheap to remove because no index entries point at them. Any backend that visits a nearly full page can perform page pruning on its own, without waiting for VACUUM. Pruning removes dead versions that no snapshot can still see. It keeps the first line pointer in the chain and turns it into a redirect (lp_flags = 2) to the surviving version. The index entry stays valid, and the space freed by the pruned tuples is available to later updates on the same page.
Because of this, a table that receives only HOT updates can run for a long time without growing, even though autovacuum has little to do for it.
Fillfactor: leaving room on each page
A HOT update needs free space on the page that holds the old version. The fillfactor storage parameter controls how full PostgreSQL packs a heap page during INSERT. The default for tables is 100, so pages are filled completely and updates often find no room on the page.
ALTER TABLE accounts_hot SET (fillfactor = 70);Keep these points in mind:
- The new value applies only to pages filled after the change. Existing pages keep their current fill level until the table is rewritten, for example by
VACUUM FULLorCLUSTER. Both take an exclusive lock on the table. - Updates can use the reserved space, but plain INSERTs stop filling a page at the fillfactor.
- A lower fillfactor makes the table larger, so sequential scans read more pages and
shared_buffersholds fewer rows.
| Workload | Reasonable fillfactor |
|---|---|
| Append-only or read-mostly | 100 (default) |
| Frequent updates to non-indexed columns | 70 to 90 |
| Heavy updates, small rows, few long transactions | Lower values can pay off, but measure |
Treat these numbers as starting points. Change the value, run a realistic workload, and compare n_tup_hot_upd and table size.
Why HOT updates stop happening
When the HOT ratio is lower than you expect, check these causes in order:
- An indexed column is in the SET list. A common case is an indexed
updated_atcolumn that the application refreshes on every update. Dropping that index or moving it away from the hot path may be worth more than any tuning. Remember that the check looks at columns in the SET list whose value actually changes. An expression index or a partial index predicate also counts as a use of its columns. - The page is full. Fillfactor is 100, or the page is full of versions that pruning cannot remove.
- Old snapshots keep versions alive. Pruning follows the same visibility horizon as VACUUM. A long-running transaction (see the earlier chapter on the xmin horizon) stops pruning from removing recent dead versions. The page fills up and updates fall back to regular updates.
- Unrelated rows share the page. A single hot row cannot take more space than its page has. Many versions of one row, each kept alive by an old snapshot, can fill the page by themselves.
Summary of the mechanism
A HOT update keeps the chain of versions inside one page. The index points to the start of the chain, and the visibility rules from earlier chapters decide which version a scan returns. Pruning shortens the chain and reclaims space without VACUUM, and fillfactor reserves the room that makes this possible.
Exercise
A table orders(id int primary key, status text, updated_at timestamptz) has an index on updated_at and none on status. The application runs UPDATE orders SET status = 'shipped', updated_at = now() WHERE id = $1. n_tup_hot_upd stays at 0. Why, and what are two ways to change that?
Show answer
The statement changes updated_at, which is an indexed column, so no update can be HOT. Each update writes a new index entry in the updated_at index. You can make updates HOT either by removing the index on updated_at (if queries do not need it), or by no longer changing updated_at in the hot path, for example by updating it in a separate, less frequent process. Lowering fillfactor does not help here, because the indexed-column condition fails before page space is checked.
Exercise
You run ALTER TABLE accounts_hot SET (fillfactor = 70) on a table with 10 million existing rows. Is the extra free space now available on the existing pages? How can you get it?
Show answer
No. The setting affects only pages written after the change, so existing pages stay as full as they were. To apply it to existing data you must rewrite the table, for example with VACUUM FULL accounts_hot or CLUSTER, both of which take an exclusive lock for the duration of the rewrite.