PostgreSQL manages deleted and updated records differently from many traditional database systems. When an application updates a row, PostgreSQL creates a new row version instead of immediately replacing the existing one. Similarly, deleted rows remain in the table until PostgreSQL determines that they are no longer required by active transactions.
These obsolete row versions are called dead tuples. If they are not cleaned up regularly, they can increase table size, affect query performance, and consume unnecessary storage.
PostgreSQL uses the autovacuum daemon to handle this maintenance automatically. It runs VACUUM and ANALYZE when tables reach configured thresholds. However, autovacuum does not always keep up with a busy application. Large tables, frequent updates, long-running transactions, limited worker capacity, and aggressive workloads can create maintenance problems.
PostgreSQL 19 introduces additional monitoring capabilities that make these problems easier to investigate. The new pg_stat_autovacuum_scores view shows how autovacuum prioritizes tables, while improvements to vacuum progress reporting provide more information about why a vacuum started and how it is running. PostgreSQL 19 also supports parallel index processing during autovacuum.
This article explains how to use these PostgreSQL 19 features to identify tables that need maintenance, investigate delayed vacuum operations, monitor worker activity, and improve autovacuum configuration.
Understanding How Autovacuum Works
Autovacuum is a background maintenance system that automatically performs two important operations:
- VACUUM removes obsolete row versions when they are no longer needed and makes the associated space available for reuse.
- ANALYZE collects table statistics that help the query planner select efficient execution plans.
The autovacuum launcher periodically checks databases and starts workers when maintenance is required.
For example, consider an application that frequently updates order statuses:
UPDATE orders
SET status = 'SHIPPED'
WHERE order_id = 10501;
PostgreSQL creates a new row version for the updated record. The previous version eventually becomes eligible for cleanup.
If the application updates millions of records, the number of dead tuples can increase quickly. Autovacuum must process these records to prevent unnecessary storage growth.
Autovacuum uses configurable thresholds to decide when a table requires maintenance. It also considers transaction ID and multixact age to protect the database from transaction ID wraparound.
The default configuration works for many applications, but high-traffic systems often need table-specific tuning.
Understanding What's New in PostgreSQL 19
PostgreSQL 19 introduces several improvements related to autovacuum monitoring and execution.
New pg_stat_autovacuum_scores View
The most important monitoring addition is pg_stat_autovacuum_scores.
This view shows the current autovacuum score for each table in the connected database. Higher scores indicate higher processing priority.
Instead of only checking whether a table has many dead tuples, administrators can now inspect the individual components that influence autovacuum prioritization.
The view includes:
| Column | Description |
|---|
| relid | Object identifier of the table |
| schemaname | Schema containing the table |
| relname | Table name |
| score | Highest component score used for prioritization |
| xid_score | Transaction ID age score |
| mxid_score | Multixact ID age score |
| vacuum_score | Dead-tuple vacuum score |
| vacuum_insert_score | Insert-based vacuum score |
| analyze_score | Analyze score |
| do_vacuum | Whether autovacuum would vacuum the table |
| do_analyze | Whether autovacuum would analyze the table |
| for_wraparound | Whether vacuuming is required for wraparound protection |
This view is useful when a table appears to need maintenance but is not being processed as expected.
One important limitation is that the scores represent current information. They may differ from the information available when an autovacuum worker last selected its processing list. Therefore, the view is a diagnostic tool, not a guarantee of the exact next table that will be processed.
Improved Vacuum Progress Information
PostgreSQL 19 extends pg_stat_progress_vacuum with two useful columns:
- started_by identifies why the vacuum operation started.
- mode identifies the type of vacuum operation currently running.
The started_by column can report manual, autovacuum, or autovacuum_wraparound.
The mode column can report normal, aggressive, or failsafe.
This makes it easier to distinguish routine maintenance from operations required to protect the database against transaction ID wraparound.
Parallel Index Processing During Autovacuum
PostgreSQL 19 also introduces autovacuum_max_parallel_workers.
This parameter controls the maximum number of parallel workers that a single autovacuum worker can use for index vacuuming and index cleanup.
Previously, administrators had fewer options for parallelizing index maintenance performed by autovacuum. The new setting provides additional control for databases with large indexes.
The default value is zero, which disables parallel index vacuuming during autovacuum. Administrators can enable it after evaluating the available worker capacity and storage performance.
Finding Tables That Need Autovacuum
The first step in troubleshooting is identifying tables with a large number of dead tuples.
PostgreSQL provides pg_stat_user_tables for this purpose.
Run the following query:
SELECT
schemaname,
relname AS table_name,
n_live_tup,
n_dead_tup,
n_ins_since_vacuum,
last_autovacuum,
autovacuum_count,
total_autovacuum_time
FROM pg_stat_user_tables
ORDER BY n_dead_tup DESC
LIMIT 20;
This query identifies tables with the highest estimated dead-tuple counts.
The important columns include:
- n_live_tup: Estimated number of live rows.
- n_dead_tup: Estimated number of dead rows.
- n_ins_since_vacuum: Estimated number of inserted rows since the last vacuum.
- last_autovacuum: Time of the most recent automatic vacuum.
- autovacuum_count: Number of automatic vacuum operations.
- total_autovacuum_time: Total time spent on automatic vacuum operations.
For example, suppose the orders table has several million live rows and a large number of dead tuples. Its last_autovacuum timestamp may also be several hours old.
This could indicate that the table is generating obsolete records faster than autovacuum can clean them.
However, n_dead_tup is an estimate. A high value should trigger further investigation rather than an immediate assumption that autovacuum is failing.
Using pg_stat_autovacuum_scores to Find Delayed Maintenance
PostgreSQL 19 makes it possible to inspect the actual scoring components used by autovacuum.
Execute the following query:
SELECT
schemaname,
relname AS table_name,
ROUND(score::numeric, 2) AS total_score,
ROUND(vacuum_score::numeric, 2) AS vacuum_score,
ROUND(vacuum_insert_score::numeric, 2)
AS insert_score,
ROUND(analyze_score::numeric, 2)
AS analyze_score,
ROUND(xid_score::numeric, 2) AS xid_score,
do_vacuum,
do_analyze,
for_wraparound
FROM pg_stat_autovacuum_scores
ORDER BY score DESC
LIMIT 20;
This report helps identify tables with high maintenance priority.
For example, consider the following hypothetical situation:
| Table | Vacuum score | Analyze score | Wraparound |
|---|
| orders | 4.8 | 0.9 | No |
| order_items | 3.2 | 1.4 | No |
| audit_logs | 0.7 | 0.5 | No |
| customer_events | 1.1 | 0.8 | No |
The orders table has the highest vacuum score, so it deserves attention first.
The score components help explain why a table is being prioritized. A high xid_score or mxid_score is particularly important because it indicates transaction ID or multixact age pressure.
Do not interpret the total score as a direct measurement of dead tuples or table size. It is a prioritization value calculated from different maintenance requirements.
Checking Whether Autovacuum Is Running
After identifying tables that need maintenance, check the currently running autovacuum workers.
Use pg_stat_activity:
SELECT
pid,
datname,
usename,
state,
wait_event_type,
wait_event,
query_start,
query
FROM pg_stat_activity
WHERE backend_type = 'autovacuum worker'
ORDER BY query_start;
This query helps identify active autovacuum workers and how long they have been running.
If several workers are processing large tables, smaller tables may need to wait until worker capacity becomes available.
Check the current worker configuration:
SHOW autovacuum;
SHOW autovacuum_max_workers;
SHOW autovacuum_worker_slots;
SHOW autovacuum_naptime;
PostgreSQL 19 uses autovacuum_worker_slots to reserve backend slots for autovacuum workers. The maximum worker count is also limited by the available slots.
Increasing autovacuum_max_workers without considering autovacuum_worker_slots may not provide the expected benefit.
Monitoring the Current Vacuum Operation
PostgreSQL provides pg_stat_progress_vacuum to monitor active vacuum operations.
In PostgreSQL 19, this view includes the started_by and mode columns.
Run:
SELECT
p.pid,
p.datname,
p.relid::regclass AS table_name,
p.phase,
p.mode,
p.started_by,
p.heap_blks_total,
p.heap_blks_scanned,
p.heap_blks_vacuumed,
p.indexes_total,
p.indexes_processed,
p.delay_time
FROM pg_stat_progress_vacuum p;
This query provides useful information about the current vacuum operation.
For example, if a table is being vacuumed in aggressive mode, PostgreSQL is performing additional work to freeze old transaction identifiers.
If the started_by value is autovacuum_wraparound, the operation was started to prevent transaction ID or multixact ID wraparound.
The delay_time column reports time spent sleeping because of cost-based vacuum delays when track_cost_delay_timing is enabled.
This is useful when a vacuum operation appears to be progressing slowly because of configured resource throttling.
Identifying Why Autovacuum Is Not Keeping Up
There are several reasons why autovacuum may fall behind.
Too Few Autovacuum Workers
The default autovacuum_max_workers value is three. A database with several large, frequently updated tables may need more worker capacity.
If all workers remain busy with large tables, other eligible tables may wait.
Increasing worker capacity can help, but administrators must also consider CPU, memory, disk I/O, and available worker slots.
Vacuum Thresholds Are Too High
PostgreSQL calculates vacuum eligibility using a base threshold and a scale factor.
The general calculation for update- and delete-based vacuuming is:
Vacuum threshold =
autovacuum_vacuum_threshold
+
autovacuum_vacuum_scale_factor
* number of tuples
The default vacuum scale factor is 0.2, or 20 percent.
For a very large table, this can mean that a substantial number of rows must be updated or deleted before automatic vacuuming starts.
For frequently updated tables, a smaller table-specific scale factor may be more appropriate.
For example:
ALTER TABLE public.orders
SET (
autovacuum_vacuum_scale_factor = 0.02,
autovacuum_vacuum_threshold = 5000
);
This makes vacuuming eligible at a lower dead-tuple threshold.
Always measure the impact before applying such settings to many tables.
Long-Running Transactions
Long-running transactions can prevent PostgreSQL from removing old row versions because those versions may still be visible to an active snapshot.
Find long-running transactions:
SELECT
pid,
usename,
application_name,
state,
xact_start,
now() - xact_start AS transaction_duration,
query
FROM pg_stat_activity
WHERE xact_start IS NOT NULL
ORDER BY xact_start;
Investigate transactions that remain open for unusually long periods.
Applications should commit or roll back transactions promptly rather than keeping database transactions open while performing unrelated processing.
Insufficient Memory for Index Maintenance
Autovacuum also needs memory to collect dead tuple identifiers before cleaning indexes.
If the available memory is insufficient, a vacuum operation may need to repeat index and heap processing cycles.
Review autovacuum_work_mem and the vacuum progress information when large tables have many indexes.
Improving Autovacuum Performance in PostgreSQL 19
After identifying the problem, make targeted configuration changes.
For a database with large, frequently updated tables, administrators can evaluate increasing worker capacity:
ALTER SYSTEM
SET autovacuum_max_workers = 6;
For parallel index processing, PostgreSQL 19 provides:
ALTER SYSTEM
SET autovacuum_max_parallel_workers = 2;
These settings require appropriate server capacity. The worker count and parallel worker count should be tested against the actual workload.
Reload the configuration where supported:
SELECT pg_reload_conf();
Some configuration parameters require a server restart. Check their context before applying changes.
For individual tables, adjust vacuum settings based on update frequency and table size rather than using one global value for every workload.
Also, avoid disabling autovacuum to reduce background activity. PostgreSQL may still launch workers to prevent transaction ID wraparound, and disabling routine maintenance can create more serious problems later.
Best Practices for Production Databases
A reliable autovacuum monitoring process should include:
- Monitor n_dead_tup and last_autovacuum for frequently updated tables.
- Use pg_stat_autovacuum_scores to understand maintenance priority.
- Monitor active workers through pg_stat_activity.
- Inspect pg_stat_progress_vacuum when maintenance takes longer than expected.
- Check long-running transactions that may prevent cleanup.
- Review autovacuum_max_workers and autovacuum_worker_slots together.
- Evaluate parallel index processing for large tables.
- Adjust vacuum thresholds at the table level when necessary.
- Monitor transaction ID age and wraparound warnings.
- Compare maintenance duration and dead-tuple estimates over time.
Remember that PostgreSQL statistics are periodically updated rather than instantly refreshed. When investigating a problem, compare measurements across a representative time interval instead of relying on a single snapshot.
Summary
Autovacuum is essential for maintaining PostgreSQL database health. When it falls behind, dead tuples can accumulate, query performance can suffer, and transaction ID wraparound risks can increase.
PostgreSQL 19 makes autovacuum troubleshooting more practical through the new pg_stat_autovacuum_scores view, improved vacuum progress reporting, and parallel index processing during autovacuum.
Administrators can use these features to identify which tables need maintenance, understand why they are being prioritized, monitor active workers, and investigate long-running vacuum operations.
The most effective approach is to monitor maintenance continuously, identify the actual cause of delays, and adjust worker capacity or table-specific thresholds based on workload measurements. This helps PostgreSQL maintain database health without introducing unnecessary resource consumption or application disruption.
Join the conversation! Your thoughts help the community grow.