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.
Code
-- 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
SQL DDL to TypeScript ConverterConvert SQL CREATE TABLE DDL statements into strict TypeScript interfaces with nullable, array, enum, JSONB, and Date or ISO string column mappings.PostgreSQL EXPLAIN AnalyzerAnalyze PostgreSQL EXPLAIN ANALYZE text or FORMAT JSON plans locally to flag Seq Scans, slow nodes, row misestimates, disk sorts, and index hints.Multi-Mode Escape & Unescape CodecEscape and unescape strings safely across JSON, JavaScript, SQL single-quoted literals, POSIX shell single quotes, and RFC 4180 CSV cells in your browser.SQL FormatterReformat SQL by uppercasing reserved words and breaking lines before SELECT, FROM, WHERE, and other clauses.
FAQ
- Which tables appear in this script?
- `hot_counters` — referenced directly, no join needed.
Report a problem with this exampleOpens a pre-filled issue on GitHub.