Sub topic 13
Preventing bloat with fillfactor and HOT updates
This chapter assumes you know how MVCC creates dead tuples, how VACUUM and autovacuum reclaim them, and what an index is. The earlier chapters covered cleaning up bloat after it exists. This one covers writing data in a way that creates less of it, or makes it cheaper to clean.
Why updates are the expensive case
In PostgreSQL, an UPDATE does not change a row in place. It writes a new row version (a tuple) and marks the old one as dead once no snapshot can see it. Two costs follow from this:
- The new version needs space, and the old version keeps its space until VACUUM removes it.
- Every index on the table normally needs a new entry that points to the new version.
The second cost is the one that hurts most. A table with six indexes gets six new index entries for each update, even if the update changed only a column that none of those indexes cover. Those entries become dead index entries later, and indexes can only be cleaned by VACUUM. That is a major source of index bloat.
PostgreSQL has an optimization that avoids this work in many cases. It is called a HOT update.
What a HOT update is
HOT stands for Heap-Only Tuple. A HOT update writes the new row version on the same heap page as the old one and creates no new index entries. The old version keeps a link to the new one, and index lookups follow that link inside the page.
An update can be HOT only when both of these conditions hold:
- The update changes no column that is used by any index on the table. This includes columns in expression indexes and in the WHERE clause of partial indexes. (Columns that are only stored in an index with
INCLUDEcount too.) - The page that holds the old version has enough free space for the new version.
If either condition fails, the update is a normal one. It writes new entries in every index.
HOT updates also give you a second benefit. When a page is read or written, PostgreSQL can do a quick "pruning" step on it. Pruning removes dead tuples that were left by earlier HOT updates, without waiting for VACUUM and without touching the indexes. So HOT chains tend to clean up after themselves.
Checking how many updates are HOT
The statistics view pg_stat_user_tables counts total and HOT updates for each table. This query shows the HOT ratio for the busiest tables:
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 > 0
ORDER BY n_tup_upd DESC
LIMIT 10;How to read the result:
- A high
hot_pct(for example above 80%) means most updates already avoid index writes. - A low
hot_pcton a table with many updates is worth investigating. Either an indexed column changes often, or pages have no room.
The counters are cumulative since the last statistics reset, so a table that behaved differently last month can hide a recent change. Compare values over time, or call pg_stat_reset() in a test environment before a workload run.
The two levers: fewer indexes and more room
You can raise the HOT ratio in two ways.
Lever 1: do not index columns that change often
If a column like last_seen_at or status is updated constantly and is part of an index, every update of it is a non-HOT update. Ask whether the index is needed. Check usage with pg_stat_user_indexes:
SELECT relname,
indexrelname,
idx_scan,
pg_size_pretty(pg_relation_size(indexrelid)) AS index_size
FROM pg_stat_user_indexes
WHERE schemaname = 'public'
ORDER BY idx_scan ASC
LIMIT 10;An index with idx_scan = 0 over a long period is a candidate for removal. Confirm first that it does not enforce a constraint (a unique or primary key index) and that the statistics cover a full business cycle, including month-end jobs and reports. Dropping an unused index speeds up every write and removes one source of bloat.
Sometimes the index is needed. In that case, consider moving the frequently changing column to a separate table so that the hot columns and the indexed columns do not share a row.
Lever 2: leave free space with fillfactor
By default, PostgreSQL packs table pages as full as it can when inserting rows (fillfactor 100). A page that is full cannot accept the new version of an updated row, so the update cannot be HOT even when no indexed column changed.
The fillfactor storage parameter tells PostgreSQL to stop filling pages at a given percentage when it inserts rows. The rest is kept as room for later updates.
ALTER TABLE public.orders SET (fillfactor = 90);This leaves 10% free space in each newly written page. Important details:
- The setting affects only pages written after the change. Existing pages stay as full as they were. To apply it to old data you must rewrite the table, for example with
VACUUM FULL,pg_repack, orREPACK(see the chapters on rewrites). - Updates that fill a page past the fillfactor are still allowed. Fillfactor only controls inserts.
- You can check the current value with
SELECT reloptions FROM pg_class WHERE relname = 'orders';. If it returns NULL, the default of 100 applies.
Choosing a fillfactor value
There is no universal number. The trade-off is table size against update efficiency.
| Workload | Suggested fillfactor | Reason |
|---|---|---|
| Append-only or rarely updated | 100 (default) | Free space would be wasted |
| Occasional updates to small columns | 90 to 95 | A little room is enough |
| Frequent updates to non-indexed columns | 70 to 85 | More versions fit on a page before pruning or VACUUM |
| Updates that change indexed columns | 100 | HOT is impossible, so reserved space is wasted |
Lower fillfactor makes the table physically larger. A table at fillfactor 80 uses about 25% more pages than one at 100 for the same rows. That means more disk space, more cache use, and slightly slower sequential scans. Only pay this cost where HOT updates are realistic.
Indexes have a fillfactor too. B-tree indexes default to 90, which already leaves room for new entries. Lowering it further rarely helps with bloat.
Measuring the effect
Do not guess. Change the setting on one table, rewrite it if you need old pages to benefit, then compare the HOT ratio and dead tuple counts after a representative period.
A small experiment shows the idea:
CREATE TABLE hot_demo (
id integer PRIMARY KEY,
counter integer NOT NULL DEFAULT 0,
payload text
) WITH (fillfactor = 100);
INSERT INTO hot_demo
SELECT g, 0, repeat('x', 100)
FROM generate_series(1, 10000) AS g;
UPDATE hot_demo SET counter = counter + 1;
SELECT pg_stat_force_next_flush();
SELECT n_tup_upd, n_tup_hot_upd
FROM pg_stat_user_tables
WHERE relname = 'hot_demo';On PostgreSQL 15 and earlier, pg_stat_force_next_flush() does not exist. Skip that line and wait a second before querying, because statistics are reported asynchronously. On version 15 and earlier it can take up to about a second for counters to appear.
The counter column is not indexed, so the update is eligible for HOT. With fillfactor 100 and rows packed tightly, many of the 10,000 updates will find their page full and be non-HOT. Now repeat with a lower setting:
DROP TABLE hot_demo;
CREATE TABLE hot_demo (
id integer PRIMARY KEY,
counter integer NOT NULL DEFAULT 0,
payload text
) WITH (fillfactor = 70);
INSERT INTO hot_demo
SELECT g, 0, repeat('x', 100)
FROM generate_series(1, 10000) AS g;
UPDATE hot_demo SET counter = counter + 1;
SELECT pg_stat_force_next_flush();
SELECT n_tup_upd, n_tup_hot_upd
FROM pg_stat_user_tables
WHERE relname = 'hot_demo';With fillfactor 70, nearly all updates should be HOT. The exact numbers depend on row width and version, so compare the two runs rather than expecting a specific count.
Edge cases and gotchas
- Long transactions still block cleanup. HOT pruning removes only tuples that no snapshot can see. A transaction that holds an old snapshot keeps HOT chains from being pruned, so they grow. Fixing blockers (see the chapter on cleanup blockers) matters here too.
- TOASTed values. Large values stored out of line in the TOAST table are handled separately. Updating a row without changing the toasted column does not rewrite the toasted data, but a HOT update still needs room on the main heap page for the new row version.
- Updating a column to the same value. An update that sets an indexed column to its current value is still treated as a change by older PostgreSQL versions, so it is not HOT. Newer versions compare the old and new values for some index types, but do not rely on this. Avoid writing unchanged values, for example by adding
WHERE column IS DISTINCT FROM new_value. - Adding an index later can lower the HOT ratio. A new index on a frequently updated column makes updates of that column non-HOT. Check
hot_pctbefore and after you add indexes to busy tables. - Replicas. HOT pruning happens on the primary, and the effect is replicated through WAL. You do not tune it separately on standbys.
- Heavy bulk updates. A single statement that updates every row creates one new version per row. Fillfactor helps little, because the new versions all arrive within one statement and nothing can be pruned yet. Batch such work into smaller transactions with VACUUM between batches, or rewrite the table into a new one.
A prevention checklist
- Find tables with many updates and a low
hot_pct. - For each one, list its indexes and check which updated columns appear in them.
- Drop unused indexes and consider whether a changing column really needs an index.
- Set a lower fillfactor on tables where updates touch only non-indexed columns.
- Rewrite the table if you need existing pages to benefit.
- Re-measure after a representative period and keep the setting only if the HOT ratio and dead tuple growth improved.
Exercise
A table named sessions has 20 million updates, but only 5% of them are HOT. The updates change a column called lastactiveat, which has its own index. Would lowering fillfactor to 80 fix this? What would you try instead?
Show answer
No. HOT requires that no indexed column changes, and lastactiveat is indexed, so every update is non-HOT no matter how much free space the pages have. Lowering fillfactor would only make the table larger. Check whether the index on lastactiveat is used (pgstatuserindexes.idxscan). If it is not needed, drop it, and fillfactor may then help. If it is needed, accept non-HOT updates and rely on autovacuum tuning, or move lastactiveat into a separate narrow table so the main table is not rewritten and indexed on every update.
Exercise
You run ALTER TABLE public.orders SET (fillfactor = 85) on a 50 GB table that has existed for years. The next day the HOT ratio has barely changed. Why, and how do you make the setting apply to the existing data?
Show answer
Fillfactor affects only pages written after the change. Existing pages are still packed to 100%, so updates to rows on those pages still find no room. Rewrite the table so every page is laid out with the new setting. Use VACUUM FULL (blocks all access), pg_repack (online), or REPACK in versions that provide it. Plan for the table to grow by roughly 15% in size.