HOT Updates & fillfactor
The Index That Makes Every UPDATE 5x Heavier
Databasesdatabases / storage & write performance
π¬ Don't take my word for it. Run this.
CREATE TABLE s (
id bigint PRIMARY KEY,
last_seen timestamptz,
payload text
);
INSERT INTO s
SELECT g, now(), md5(g::text)
FROM generate_series(1,200000) g;
CREATE INDEX ON s (last_seen); -- π the trap
UPDATE s SET last_seen = now();
SELECT n_tup_upd, n_tup_hot_upd
FROM pg_stat_user_tables WHERE relname='s';
n_tup_upd | n_tup_hot_upd
-----------+---------------
200000 | 0
Now DROP that index on last_seen and repeat the UPDATE. Same 200k rows:
400000 | 199xxx
Zero vs ~all. One index flipped it.
What HOT actually is
Postgres never updates a row in place β UPDATE writes a NEW tuple version and leaves the old one dead (MVCC).
A Heap-Only Tuple update is the fast path. Postgres takes it when BOTH hold:
- no column used by any index changed, and
- there's free space on the SAME 8 KB page.
Then: new version goes on that page, indexes are untouched, and the dead version can be reclaimed by page pruning on the next read β no autovacuum needed.
Miss the fast path and one UPDATE becomes 1 heap write + an entry in EVERY index on the table (not just the one you changed). 5 indexes = 6 writes, 6x the WAL, 6x the index bloat.
The bit people get wrong
It is NOT "many indexes = slow updates."
It is "the columns you UPDATE must not be indexed."
A 10-index table whose hot column is unindexed does HOT updates all day. A 1-index table where that index covers last_seen does zero. The classic self-inflicted wound: adding an index on updated_at / last_seen / status "for the ORDER BY" on your busiest write table.
Caveat worth knowing: BRIN indexes don't block HOT (they're summarizing, not per-tuple).
fillfactor β the second condition
Even with no indexed column changed, a page with no free space forces the new version elsewhere β index entries again. Default fillfactor is 100 for tables: pages are packed full on load.
ALTER TABLE s SET (fillfactor = 85);
That's it: 15% of each page reserved for future row versions. Two gotchas β it only applies to pages written AFTER the change (existing pages need VACUUM FULL or pg_repack to re-pack), and it costs you ~15% more disk and slightly worse scan density. Worth it on update-heavy tables, pointless on append-only ones.
The diagnostic, one query
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
WHERE n_tup_upd > 10000
ORDER BY n_tup_upd DESC
LIMIT 10;
Rough reading: >90% is healthy. <50% on a high-update table means you're paying index write amplification on every single UPDATE, and it shows up as WAL volume, replication lag, and index bloat long before it shows up as slow queries.
Your exercise
Run that last query on your real database. Take the worst row and answer two things:
- Which indexed column does your UPDATE actually touch? (\d tablename, then compare to your UPDATE's SET list.)
- Is that index earning its keep β or was it added for a dashboard query that runs twice a day?
Bonus: if you can set fillfactor=90 on that table and rewrite it, re-measure hot_pct a day later and see how much of the gap was free space vs the index.