← Back to PostgreSQL, page 4

postgresql-0069 · Hard senior

Why do some UPDATEs bloat indexes while others do not? Show HOT updates and the role of fillfactor.

A heap-only tuple (HOT) update writes the new row version on the same page and skips index updates, which is possible only when no indexed column changes and the page has free space. Updates to indexed columns, or on full pages, must insert new index entries, causing index bloat and more WAL. A lower fillfactor leaves room on each page for HOT updates on frequently updated tables.

Full answer

Schema and sample rows:

sql
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;
sql
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:

text
 n_tup_upd | n_tup_hot_upd
-----------+---------------
 100       | 56
(1 row)
sql
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:

text
 n_tup_upd | n_tup_hot_upd
-----------+---------------
 200       | 56
(1 row)
sql
COMMIT; SELECT reloptions::text FROM pg_class WHERE relname = 'counters';

Result on PostgreSQL 17:

text
    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.

sql
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