Sub topic 13
Preventing Bloat with `fillfactor` and HOT Updates
This chapter assumes that you understand PostgreSQL tables, indexes, transactions, and the basic purpose of VACUUM. It focuses on reducing future bloat rather than repairing a table that is already bloated.
Why prevention matters
PostgreSQL uses multiversion concurrency control (MVCC). An UPDATE usually creates a new row version instead of changing the existing version in place. The old version becomes a dead tuple after no transaction can see it.
VACUUM can make that old space reusable, but it does not always return the space to the operating system. Frequent updates can also create extra index entries. Preventing unnecessary movement and index changes reduces the amount of cleanup work.
Two useful techniques are:
fillfactor, which leaves free space on each table page.- HOT updates, which avoid creating new index entries when an update can stay on the same page.
How `fillfactor` creates room
A table's fillfactor is the percentage of each new page that PostgreSQL tries to fill when it writes or rewrites the table. The default is usually 100. A setting of 90 leaves approximately 10% free space for later row versions.
The free space is useful when updates make rows larger or when PostgreSQL needs to place a replacement version. It does not guarantee that every update will fit on the original page.
ALTER TABLE public.orders
SET (fillfactor = 90);This setting affects pages written after the change. It does not rearrange existing pages. To apply the layout to the whole table, you need a rewrite such as VACUUM FULL, CLUSTER, or an online repacking operation. Plan that operation separately because it can require significant locks or disk space.
Choose a lower value when a table is updated often and replacement rows commonly need room. A lower value also means more pages and potentially more I/O for reads. Test the setting with real workload data rather than applying the same value to every table.
| Workload | Possible approach |
|---|---|
| Mostly inserts, few updates | Keep the default or use a high fillfactor |
| Frequent updates to rows of changing size | Leave more free space, such as 80–90 |
| Updates only non-indexed columns | Combine suitable fillfactor with HOT updates |
How HOT updates reduce index work
A Heap-Only Tuple (HOT) update is an update where:
- The new row version fits on the same table page.
- No indexed column changes.
Because the new version stays on the same page, PostgreSQL can link the old and new versions without adding a new entry to every affected index. This reduces index growth and makes cleanup cheaper.
For example, an update to an unindexed status column may be HOT:
UPDATE public.orders
SET processing_status = 'shipped'
WHERE order_id = 42;If processing_status has an index, changing it may require new index entries. An index on order_id does not prevent the update from being HOT when order_id remains unchanged.
The following flow shows the decision PostgreSQL makes for an update:
Designing for fewer updates
Prevention also depends on schema and application behavior:
- Avoid indexing columns that change constantly unless queries need those indexes.
- Update only columns whose values actually changed.
- Keep frequently changing attributes separate from large, mostly static rows when that design fits the application.
- Monitor index growth as well as table size.
- Keep autovacuum active, because prevention reduces bloat but does not eliminate dead tuples.
You can inspect whether HOT updates are occurring with table statistics:
SELECT relname,
n_tup_upd,
n_tup_hot_upd,
round(
100.0 * n_tup_hot_upd / nullif(n_tup_upd, 0),
1
) AS hot_update_pct
FROM pg_stat_user_tables
WHERE relname = 'orders';A low HOT percentage does not automatically indicate a problem. The table may lack enough free space, or the workload may legitimately update indexed columns. Use the result with table-bloat and index-bloat measurements.
Exercise
You set fillfactor = 90, but updates still produce few HOT updates. Name two possible causes.
Show answer
The replacement row may not fit in the remaining space on its page, or the update may change an indexed column. Existing pages may also have been written before the fillfactor change.