Full answer
Schema and sample rows:
CREATE TABLE counters (id int PRIMARY KEY, views int, label text) WITH (fillfactor = 70);
CREATE INDEX counters_label ON counters (label);
INSERT INTO counters SELECT g, 0, 'item' || g FROM generate_series(1, 1000) g;BEGIN; UPDATE counters SET views = views + 1 WHERE id <= 100; SELECT n_tup_upd, n_tup_hot_upd FROM pg_stat_xact_user_tables WHERE relname = 'counters';Output:
n_tup_upd | n_tup_hot_upd
-----------+---------------
100 | 56
(1 row)UPDATE counters SET label = label || '-x' WHERE id <= 100; SELECT n_tup_upd, n_tup_hot_upd FROM pg_stat_xact_user_tables WHERE relname = 'counters';PostgreSQL returns:
n_tup_upd | n_tup_hot_upd
-----------+---------------
200 | 56
(1 row)COMMIT; SELECT reloptions::text FROM pg_class WHERE relname = 'counters';Result on PostgreSQL 17:
reloptions
-----------------
{fillfactor=70}
(1 row)pg_stat_xact_user_tables reports counts for the current transaction immediately, which makes the effect visible without waiting for the statistics collector. Changing only views, which is not indexed, produced 100 updates, and 56 of them were HOT; for the rest, the page had run out of free space, so the new version went to another page and needed new index entries. Changing label, which has an index, added 100 more updates and no more HOT ones, because each new version needed new entries in counters_label. The fillfactor of 70 left space on each page for the new versions. Practical rules: avoid indexing columns that change constantly, keep hot counters in narrow tables with a lower fillfactor, and watch n_tup_hot_upd against n_tup_upd in pg_stat_user_tables.
CREATE TABLE counters (id int PRIMARY KEY, views int, label text) WITH (fillfactor = 70);
CREATE INDEX counters_label ON counters (label);
INSERT INTO counters SELECT g, 0, 'item' || g FROM generate_series(1, 1000) g;
BEGIN; UPDATE counters SET views = views + 1 WHERE id <= 100; SELECT n_tup_upd, n_tup_hot_upd FROM pg_stat_xact_user_tables WHERE relname = 'counters';
UPDATE counters SET label = label || '-x' WHERE id <= 100; SELECT n_tup_upd, n_tup_hot_upd FROM pg_stat_xact_user_tables WHERE relname = 'counters';
COMMIT; SELECT reloptions::text FROM pg_class WHERE relname = 'counters';Run on PostgreSQL 17.12 (Debian 17.12-0+deb13u1) in a fresh schema, twice, with identical output.
Key points
- HOT updates skip index maintenance
- They require unchanged indexed columns
- They need free space on the page
- Lower fillfactor leaves room
- Monitor the HOT ratio
Common mistakes
- Indexing frequently updated columns
- Using fillfactor 100 on update-heavy tables
Follow-ups
- How does pruning reclaim space within a page?
- What fillfactor would you choose for an append-only table?
Tips
- Compare the two update counts