Sub topic 7
`maintenance_work_mem` for VACUUM, CREATE INDEX, and autovacuum
Assume you can run basic SQL in psql, edit postgresql.conf, and read a simple EXPLAIN plan. This chapter focuses on maintenance memory, which is separate from memory used by ordinary query operations such as sorts and joins.
What `maintenance_work_mem` controls
maintenance_work_mem is the maximum memory PostgreSQL uses for one maintenance operation. Maintenance operations include:
VACUUMCREATE INDEXALTER TABLEoperations that build indexes or constraints- Some other commands that reorganize or inspect table data
More memory can reduce temporary disk work and speed up large indexes or vacuum operations. It does not give every database connection a permanent allocation. Memory is used when a maintenance command runs.
autovacuum_work_mem controls memory for each autovacuum worker. Its default value is -1, which means that the worker uses maintenance_work_mem.
SELECT name, setting, unit, context, source, pending_restart
FROM pg_settings
WHERE name IN ('maintenance_work_mem', 'autovacuum_work_mem',
'autovacuum_max_workers');The context column shows whether a setting can be changed with a reload or requires a restart. The source column helps identify whether the value came from the system configuration, a role, or another source.
Choosing a starting value
A common starting point is a few hundred megabytes on a server with several gigabytes of RAM. Larger values can help when you regularly create large indexes, but the value must fit your maintenance concurrency.
For example:
ALTER SYSTEM SET maintenance_work_mem = '512MB';
ALTER SYSTEM SET autovacuum_work_mem = '256MB';
SELECT pg_reload_conf();Check the active values after reloading:
SHOW maintenance_work_mem;
SHOW autovacuum_work_mem;Do not calculate memory as though one value covers all autovacuum workers. If four workers can run at once and each can use 256 MB, their combined allowance is roughly 1 GB, before accounting for shared buffers, query memory, PostgreSQL processes, and the operating system.
VACUUM and CREATE INDEX
VACUUM uses maintenance memory for internal cleanup structures. A table with many dead row versions may need more memory or may perform more work in batches when the setting is small.
CREATE INDEX can also benefit from more memory. A larger value may allow more of the index build to remain in memory and reduce temporary files. Test large index builds during a representative maintenance window because the command still consumes CPU and disk bandwidth.
The following query shows table size and dead-row estimates, which help identify candidates for vacuum investigation:
SELECT schemaname,
relname,
pg_size_pretty(pg_total_relation_size(relid)) AS total_size,
n_live_tup,
n_dead_tup,
last_autovacuum
FROM pg_stat_user_tables
ORDER BY n_dead_tup DESC
LIMIT 10;n_dead_tup is an estimate, not an exact count. Use it to find patterns, then inspect the table and its workload.
Autovacuum-specific risks
Autovacuum prevents transaction ID wraparound and removes obsolete row versions. Raising its memory limit can make individual workers faster, but too many workers can create memory and I/O pressure.
If autovacuum falls behind, first check whether the cause is insufficient memory, long-running transactions, aggressive workloads, blocked cleanup, or inadequate worker and cost settings. Increasing memory alone does not solve every vacuum problem.
A safe test
Change one setting, run a representative VACUUM or index build, and record elapsed time, temporary files, disk throughput, and memory pressure. Restore the previous value if the server begins swapping or competing workloads slow down.
Exercise
If autovacuumworkmem is set to -1 and maintenanceworkmem is 512MB, what limit does each autovacuum worker inherit?
Show answer
Each autovacuum worker inherits a 512MB maintenance memory limit. Multiple workers can therefore use much more than 512MB in total.