Video in progress

What is a HOT update, and how does fillfactor help it?

In a HOT (Heap-Only Tuple) update, if none of the updated columns is in any index and the page has room, the new version is written to the same page and the indexes aren't updated at all. Lowering fillfactor leaves free space on the pages and raises the share of HOT updates.

📖 Answer

In a regular UPDATE the new version gets a new ctid, and a new entry is added to every index on the table. With 6 indexes that is 6 extra writes, plus extra work for WAL and later for VACUUM.

HOT works when two conditions are met:

  1. none of the changed columns takes part in any index (including a partial index condition or an expression);
  2. the new version fits on the same page as the old version.

Then the index keeps pointing at the old entry, and inside the page there is a chain from the old version to the new one. Later, while reading the page, the old versions get cleaned up (page pruning), without waiting for a full VACUUM.

Checking the HOT share:

SELECT relname, n_tup_upd, n_tup_hot_upd,
       round(100.0 * n_tup_hot_upd / nullif(n_tup_upd, 0), 1) AS hot_pct
FROM pg_stat_user_tables
ORDER BY n_tup_upd DESC LIMIT 10;

For a table that is updated a lot:

ALTER TABLE session_state SET (fillfactor = 80);
-- affects only newly written pages; for existing data use pg_repack or VACUUM FULL

The practical lesson: putting an index on an updated_at column kills HOT completely on a frequently updated table. In one project, dropping the last_seen_at index noticeably reduced the write load. Before adding any new index, think about which column it makes "hot".