--- title: 'PostgreSQL 18 Autovacuum and Maintenance Configuration' page_title: 'PostgreSQL 18 Autovacuum and Maintenance Configuration: Dynamic Workers and Improved Controls' page_description: 'Learn about PostgreSQL 18 autovacuum improvements including dynamic worker management, better threshold controls, improved monitoring, and new security features for maintenance operations.' ogImage: '' updatedOn: '2025-08-03T10:30:00+00:00' enableTableOfContents: true previousLink: title: 'PostgreSQL 18 psql Improvements' slug: 'postgresql-18/psql-improvements' --- **Summary**: PostgreSQL 18 improves autovacuum with runtime worker adjustments, caps on dead tuple buildup, detailed performance tracking, and safer process control. These features make database maintenance more efficient and easier to manage in production environments. ## Introduction to Autovacuum Improvements Autovacuum handles critical maintenance tasks in PostgreSQL by removing dead tuples and updating table statistics to keep queries performing well. However, managing autovacuum effectively has several challenges: large tables can accumulate excessive bloat before cleanup starts, worker processes often max out during high-activity periods, and troubleshooting performance issues is difficult without visibility into what's happening. PostgreSQL 18 addresses these issues with several targeted improvements. You can now adjust the number of active workers without restarting the server, set limits on dead tuple accumulation for large tables, track detailed performance metrics for maintenance operations, and manage autovacuum processes without requiring superuser privileges. > **Note**: PostgreSQL 18 is currently in beta. Test these features thoroughly in non-production environments, as some details may change before the final release. ## Dynamic Worker Management PostgreSQL 18 introduces a flexible worker management system that separates resource allocation from active worker control, eliminating the need for server restarts when adjusting autovacuum capacity. ### Understanding Worker Slots and Active Workers The system uses two parameters that work together. The `autovacuum_worker_slots` parameter sets the maximum number of worker slots available, with a default of 16. This parameter reserves shared memory space for potential workers, so changing it requires a server restart. For databases with many small tables that need frequent maintenance, you might increase this to 20: ```sql ALTER SYSTEM SET autovacuum_worker_slots = 20; -- Server restart required ``` The `autovacuum_max_workers` parameter controls how many of those reserved slots are actually used, and this can be adjusted at runtime without any downtime: ```sql -- Increase workers during high-activity periods ALTER SYSTEM SET autovacuum_max_workers = 10; SELECT pg_reload_conf(); -- Reduce workers during quiet periods ALTER SYSTEM SET autovacuum_max_workers = 4; SELECT pg_reload_conf(); ``` Altering `autovacuum_max_workers` dynamically allows you to adapt to changing workloads without interrupting database operations. For more information on these parameters, refer to the [PostgreSQL documentation](https://www.postgresql.org/docs/18/runtime-config-vacuum.html#GUC-AUTOVACUUM-WORKER-SLOTS). ### Practical Worker Management This flexibility allows you to match autovacuum resources to your workload patterns. During peak business hours, you can increase workers to handle higher update volumes. During batch processing windows, you might temporarily scale up to handle the cleanup from large data modifications. The system prevents configuration errors by capping `autovacuum_max_workers` at the `autovacuum_worker_slots` limit. If you try to set more active workers than available slots, PostgreSQL will use the slot limit and issue a warning. To monitor current worker utilization, check `pg_stat_activity` for processes containing "autovacuum" in their query text. If workers are consistently at maximum capacity during busy periods, consider increasing the worker count or optimizing table-specific autovacuum settings. ## Controlling Dead Tuple Accumulation Large tables have always presented challenges for autovacuum because the standard triggering formula can allow millions of dead tuples to accumulate before cleanup begins. ### The Challenge with Large Tables Autovacuum triggers when dead tuples exceed: `autovacuum_vacuum_threshold + (autovacuum_vacuum_scale_factor × table_size)`. With default settings (threshold 50, scale factor 0.2), a 10 million row table needs over 2 million dead tuples before autovacuum runs. For a 100 million row table, this becomes 20 million dead tuples. This accumulation causes several problems: increased storage usage, slower query performance as the system scans through dead tuples, and longer vacuum operations when they finally run. ### Setting Maximum Thresholds PostgreSQL 18 introduces `autovacuum_vacuum_max_threshold` to cap the calculated threshold. The default is 100 million, but you'll typically want to set this much lower: ```sql -- Set a global maximum threshold ALTER SYSTEM SET autovacuum_vacuum_max_threshold = 100000; SELECT pg_reload_conf(); ``` For tables with high update rates, you can set even tighter controls: ```sql -- Keep high-traffic tables cleaner ALTER TABLE user_sessions SET (autovacuum_vacuum_max_threshold = 10000); ALTER TABLE order_items SET (autovacuum_vacuum_max_threshold = 25000); ``` This approach preserves percentage-based scaling for smaller tables while preventing large tables from accumulating excessive bloat. The feature is particularly valuable for applications with mixed table sizes, which describes most real-world databases. ## Improved Monitoring and Performance Tracking PostgreSQL 18 adds detailed timing information to help understand maintenance operation performance and identify optimization opportunities. ### New Timing Metrics The `pg_stat_all_tables` view now includes timing columns that track the total time spent on different maintenance operations: - `total_vacuum_time`: Total time for manual vacuum operations - `total_autovacuum_time`: Total time for automatic vacuum operations - `total_analyze_time`: Total time for manual analyze operations - `total_autoanalyze_time`: Total time for automatic analyze operations These cumulative metrics help identify tables that consume the most maintenance time: ```sql SELECT relname AS table_name, total_autovacuum_time, autovacuum_count, pg_size_pretty(pg_total_relation_size(schemaname||'.'||relname)) as table_size FROM pg_stat_all_tables WHERE autovacuum_count > 0 ORDER BY total_autovacuum_time DESC LIMIT 10; ``` ### Cost Delay Tracking The new `track_cost_delay_timing` parameter provides insight into how much time vacuum operations spend waiting due to cost-based delays: ```sql ALTER SYSTEM SET track_cost_delay_timing = on; SELECT pg_reload_conf(); ``` With this enabled, the `pg_stat_progress_vacuum` view includes a `delay_time` column showing cumulative delay time. If vacuum operations show high delay times relative to work completed, you might need to adjust cost-based vacuum settings for better throughput. ## Secure Autovacuum Process Management PostgreSQL 18 introduces the `pg_signal_autovacuum_worker` predefined role, which allows controlled management of autovacuum processes without requiring superuser privileges. ### Granting Process Management Access This role provides a safer way to manage autovacuum operations for maintenance scenarios: ```sql GRANT pg_signal_autovacuum_worker TO maintenance_admin; ``` Users with this role can terminate autovacuum workers when necessary for DDL operations or maintenance windows, without having broader system access. ### Managing Process Conflicts When you need to run operations that conflict with autovacuum, you can safely terminate the conflicting process: ```sql -- Find autovacuum processes on specific tables SELECT pid, query FROM pg_stat_activity WHERE query LIKE '%autovacuum%' AND query LIKE '%table_name%'; -- Terminate the process if needed SELECT pg_terminate_backend(process_id); ``` This is particularly useful for: - DDL operations that need exclusive access to tables - Maintenance windows where autovacuum might interfere with planned activities - Emergency situations where you need to free up system resources quickly The controlled access approach maintains security while enabling more flexible operational procedures.