Per-Table Autovacuum With Reloptions

Table-level reloptions override global autovacuum triggers for hot tables: fixed thresholds instead of size ratios, with RESET to fall back.

SqlConfig Management

Code

snippet
CREATE TABLE hot_metrics (
    id    int GENERATED ALWAYS AS IDENTITY,
    key   text NOT NULL,
    value numeric NOT NULL
);

-- 没有表级覆盖时 reloptions 为空:autovacuum 走全局比例阈值
SELECT reloptions FROM pg_class WHERE relname = 'hot_metrics';

-- 热表体积小、更新极快:把比例阈值换成固定阈值并提高频率
ALTER TABLE hot_metrics SET (
    autovacuum_vacuum_scale_factor = 0,
    autovacuum_vacuum_threshold = 50,
    autovacuum_analyze_scale_factor = 0,
    autovacuum_analyze_threshold = 50
);

-- reloptions 立即反映新参数,autovacuum 下轮评估即按它执行
SELECT reloptions FROM pg_class WHERE relname = 'hot_metrics';

-- 恢复跟随全局默认:RESET 删除表级覆盖,其余参数保留
ALTER TABLE hot_metrics RESET (autovacuum_vacuum_scale_factor, autovacuum_analyze_scale_factor);
SELECT reloptions FROM pg_class WHERE relname = 'hot_metrics';

How the snippet works

What runs here is SELECT / CREATE / ALTER — pg_class are the objects involved.

What the statements actually assemble: a WHERE clause filtering the rows one statement applies to; NOT NULL qualifiers on the required columns; a GENERATED ALWAYS/GENERATED BY clause that computes the column value.

Applying it

Run the statement against the pg_class table.

Statements that matter

SELECT reloptions FROM pg_class WHERE relname = 'hot_metrics';
CREATE TABLE hot_metrics (
    id    int GENERATED ALWAYS AS IDENTITY,
    key   text NOT NULL,
    value numeric NOT NULL
);
ALTER TABLE hot_metrics SET (
    autovacuum_vacuum_scale_factor = 0,
    autovacuum_vacuum_threshold = 50,
    autovacuum_analyze_scale_factor = 0,
    autovacuum_analyze_threshold = 50
);

Interactive Tools for This Topic

FAQ

What are the relations in play here?
The references are `pg_class`.