Fillfactor And HOT Updates

Leave free space on pages with fillfactor so updates that touch no indexed column stay on-page as HOT updates and skip index maintenance entirely.

SqlData Integrity

Code

snippet
-- fillfactor:页内预留空闲空间,让 UPDATE 尽量留在同一页(HOT update)
-- HOT(Heap-Only Tuple)链不更新索引:前提是被更新的列不在任何索引里
CREATE TABLE hot_counters (
  id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
  value integer NOT NULL,
  note text
) WITH (fillfactor = 70);

-- 索引列是 id 与 value:只更新 note 才能走 HOT 链
UPDATE hot_counters SET note = 'tick-' || id WHERE id % 2 = 0;

-- 索引也能设 fillfactor:btree 默认 90,留出指向新元组的空间
CREATE INDEX idx_hot_counters_value
  ON hot_counters (value)
  WITH (fillfactor = 60);

What the code does

Several statements share this file — UPDATE / CREATE, with hot_counters among the objects involved.

Breaking the statements into their working parts: a WHERE clause narrowing which rows one statement affects; NOT NULL qualifiers on the required columns; a PRIMARY KEY that enforces row identity; a GENERATED ALWAYS/GENERATED BY clause that computes the column value.

Applying it

Run the statement against the hot_counters table.

Statements that matter

CREATE TABLE hot_counters (
  id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
  value integer NOT NULL,
  note text
) WITH (fillfactor = 70);
CREATE INDEX idx_hot_counters_value
  ON hot_counters (value)
  WITH (fillfactor = 60);
UPDATE hot_counters SET note = 'tick-' || id WHERE id % 2 = 0;

Interactive Tools for This Topic

FAQ

Which tables appear in this script?
`hot_counters` — referenced directly, no join needed.