Sub topic 13
HOT Updates and Fillfactor
Prerequisites
This chapter assumes you understand basic SQL, transactions, row versions, and the roles of xmin, xmax, and ctid. You should also be able to run SQL in PostgreSQL.
Why HOT updates matter
A normal UPDATE creates a new row version. PostgreSQL must update every index that points to the changed row, even when the indexed columns did not change.
A HOT update is a heap-only tuple update. PostgreSQL creates the new row version on the same heap page as the old version and leaves existing index entries unchanged. The old version points to the new version through its ctid.
A HOT update is possible when both conditions are true:
- The update does not change any column used by an index.
- The new row version fits on the same heap page.
This can reduce index page writes and index bloat. It does not eliminate the old row version. VACUUM still needs to clean up versions that no active snapshot can see.
A worked example
Create a table with an indexed identifier and a frequently changing, unindexed value:
CREATE TABLE sensor_readings (
sensor_id bigint PRIMARY KEY,
reading numeric NOT NULL,
recorded_at timestamptz NOT NULL
) WITH (fillfactor = 70);
INSERT INTO sensor_readings
VALUES (1, 20.5, now());
UPDATE sensor_readings
SET reading = 21.0,
recorded_at = now()
WHERE sensor_id = 1;The primary-key index does not need a new key because sensor_id stayed the same. If the new version fits on the same page, PostgreSQL can use a HOT update.
You can inspect update behavior through the statistics view:
SELECT relname, n_tup_upd, n_tup_hot_upd
FROM pg_stat_user_tables
WHERE relname = 'sensor_readings';The counters are cumulative estimates. n_tup_hot_upd counts HOT updates, while n_tup_upd counts all updates recorded for the table.
Fillfactor reserves space
Fillfactor is the target percentage of each heap page that PostgreSQL fills during ordinary insertion. A table with fillfactor = 70 leaves approximately 30 percent of each page available for later row versions.
The setting is a trade-off:
| Fillfactor choice | Benefit | Cost |
|---|---|---|
| Lower value, such as 70 | More room for HOT updates | More pages and a larger table |
| Higher value, such as 100 | Better initial packing | Fewer same-page HOT updates |
The best value depends on the workload. A table with frequent updates to unindexed columns may benefit from a lower value. An append-only table usually gains little from reserving update space.
Common failure cases
HOT updates cannot occur when an indexed column changes. An index on recorded_at, for example, can prevent HOT updates for the example above. A new version also cannot use another page, so a full page, a large row, or an unsuitable page layout can prevent HOT even when no indexed column changes.
Exercise
Exercise
Why might lowering fillfactor increase HOT updates but also increase storage use?
Show answer
It leaves free space on each heap page for new row versions, which improves same-page updates but requires more pages to store the table.