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.
Code
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
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.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.Dotenv File ParserParse .env file content into a JSON object, handling comments, export prefixes, single quotes, and double-quoted escape sequences.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.
FAQ
- What are the relations in play here?
- The references are `pg_class`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.