Understanding PostgreSQL Autovacuum Thresholds Using pg_stat_user_tables
PostgreSQL DBAs often see autovacuum and autoanalyze mentioned together, but they are not the same mechanism.
Recently, while testing PostgreSQL autovacuum behavior, I noticed something interesting:
Autoanalyze had executed, but another autovacuum had not yet occurred.
At first glance, this can look confusing because both operations are related to table activity.
The key is to understand what each process is measuring.
1. What is Autoanalyze?
ANALYZE collects statistics about the contents of a table.
PostgreSQL’s query planner uses these statistics to estimate things such as:
- Number of rows
- Data distribution
- Selectivity of predicates
- Which indexes may be useful
- Estimated cost of different execution plans
When enough rows have changed since the previous analyze, PostgreSQL can automatically run ANALYZE.
The important statistic is:
n_mod_since_analyze
You can see it using:
SELECT
relname,
n_live_tup,
n_mod_since_analyze,
last_autoanalyze,
autoanalyze_count
FROM pg_stat_user_tables
WHERE relname = 'sample';
2. My Test Table
In my test environment, the table showed:
relname sample
n_live_tup 59,137
n_dead_tup 0
n_mod_since_analyze 9,898
n_ins_since_vacuum 18,138
autovacuum_count 3
autoanalyze_count 4
The timestamps showed:
last_autovacuum 10:58:39
last_autoanalyze 11:40:40
So PostgreSQL had performed an autoanalyze, while I was investigating why another autovacuum had not yet occurred.
3. How Does Autoanalyze Decide When to Run?
The default autoanalyze threshold is based on:
analyze_threshold
+
(analyze_scale_factor × number_of_rows)
The PostgreSQL defaults are:
analyze_threshold = 50
analyze_scale_factor = 0.10
For my table:
Live rows = 59,137
Therefore:
50 + (0.10 × 59,137)
= 50 + 5,913.7
≈ 5,964
So approximately 5,964 qualifying row changes are required to trigger autoanalyze under the default settings.
My table showed:
n_mod_since_analyze = 9,898
That’s above the calculated threshold.
Therefore:
9,898 > 5,964
Result
Autoanalyze was triggered.
And the statistics confirmed it:
last_autoanalyze = 11:40:40
autoanalyze_count = 4
4. But Autovacuum Is Different
This is where the confusion usually starts.
Autovacuum doesn’t use the same threshold as autoanalyze.
For the traditional dead-tuple trigger, the calculation is:
vacuum_threshold
+
(vacuum_scale_factor × number_of_rows)
Default values:
autovacuum_vacuum_threshold = 50
autovacuum_vacuum_scale_factor = 0.20
For 59,137 rows:
50 + (0.20 × 59,137)
= 50 + 11,827.4
≈ 11,877
So approximately 11,877 dead tuples would be required for the normal dead-tuple-based autovacuum trigger.
But my table showed:
n_dead_tup = 0
Therefore:
0 < 11,877
The dead-tuple threshold had not been reached.
5. Then What About
n_ins_since_vacuum
?
This is where the example becomes interesting.
My table showed:
n_ins_since_vacuum = 18,138
PostgreSQL also has an insert-based autovacuum trigger.
The calculation is:
autovacuum_vacuum_insert_threshold
+
(autovacuum_vacuum_insert_scale_factor × number_of_rows)
With the default values:
autovacuum_vacuum_insert_threshold = 1,000
autovacuum_vacuum_insert_scale_factor = 0.20
For 59,137 rows:
1,000 + (0.20 × 59,137)
= 1,000 + 11,827.4
≈ 12,827
My table had:
n_ins_since_vacuum = 18,138
So:
18,138 > 12,827
This means the insert-based threshold had also been crossed.
However, this does not mean that a VACUUM must execute at that exact moment.
6. Why Doesn’t Crossing the Threshold Mean Immediate VACUUM?
This is an important DBA concept.
Autovacuum is a background process.
Even when a table becomes eligible, the actual execution depends on PostgreSQL’s autovacuum machinery.
Things that can affect when the worker runs include:
- autovacuum_naptime
- Number of available autovacuum workers
- autovacuum_max_workers
- Other tables competing for workers
- Table-specific autovacuum settings
- Worker scheduling
- Database/table activity
Therefore:
Threshold reached ≠ VACUUM starts immediately.
The threshold makes the table eligible for autovacuum. The autovacuum launcher and workers then determine when it gets processed.
7.
n_dead_tup = 0
Does Not Mean Autovacuum Is Useless
Another important point.
Suppose a table receives mostly INSERT operations:
INSERT
INSERT
INSERT
INSERT
...
There may be very few or zero dead tuples.
Therefore:
n_dead_tup = 0
But the table can still become a candidate for insert-based autovacuum.
Why?
Insert-heavy tables can accumulate pages where newly inserted tuples may affect:
- Visibility information
- Freeze-related maintenance
- Visibility map maintenance
- Transaction ID/MultiXact aging considerations
So PostgreSQL has an insert-based autovacuum mechanism rather than relying exclusively on dead tuples.
8. The Most Important Difference
Think about it this way:
|
Operation |
What does it care about? |
|
AUTOANALYZE |
Changes to table data |
|
AUTOVACUUM |
Dead tuples and, depending on configuration/version, inserts |
|
Manual ANALYZE |
Refreshes planner statistics |
|
Manual VACUUM |
Cleans up/reclaims dead-tuple space for reuse and performs related maintenance |
The counters in pg_stat_user_tables help us understand which situation we’re dealing with.
9. Useful PostgreSQL Monitoring Query
I normally start with:
SELECT
relname,
n_live_tup,
n_dead_tup,
n_mod_since_analyze,
n_ins_since_vacuum,
last_autovacuum,
last_autoanalyze,
autovacuum_count,
autoanalyze_count
FROM pg_stat_user_tables
WHERE relname = 'sample';
This gives a quick picture of table activity.
10. Check Your Autovacuum Configuration
Never calculate thresholds based only on the defaults.
Your PostgreSQL server or individual table may have customized settings.
Check the global settings:
SHOW autovacuum;
SHOW autovacuum_naptime;
SHOW autovacuum_vacuum_threshold;
SHOW autovacuum_vacuum_scale_factor;
SHOW autovacuum_vacuum_insert_threshold;
SHOW autovacuum_vacuum_insert_scale_factor;
Then check whether the table has custom settings:
SELECT
relname,
reloptions
FROM pg_class
WHERE relname = 'sample';
A table can have settings such as:
autovacuum_vacuum_scale_factor=0.05
which would completely change your calculation.
11. Check Whether a VACUUM Is Actually Running
You can monitor active vacuum operations with:
SELECT
pid,
datname,
relid::regclass AS table_name,
phase,
heap_blks_total,
heap_blks_scanned,
heap_blks_vacuumed
FROM pg_stat_progress_vacuum;
If a vacuum is currently running, you’ll be able to see its progress.
12. Check Autovacuum Workers
You can also look for autovacuum activity:
SELECT
pid,
usename,
datname,
query,
state,
wait_event_type,
wait_event
FROM pg_stat_activity
WHERE backend_type = 'autovacuum worker';
This is useful when troubleshooting why a table appears eligible but isn’t currently being processed.
13. One Mistake I Initially Made
When looking at the numbers, it’s easy to apply the same formula to both operations.
For example:
50 + (0.20 × rows)
is not the autoanalyze formula.
For the default settings:
Autoanalyze
50 + (0.10 × rows)
Dead-tuple autovacuum
50 + (0.20 × rows)
Insert-based autovacuum
1000 + (0.20 × rows)
They are different mechanisms with different counters.
14. My Practical DBA Approach
When investigating autovacuum behavior, I wouldn’t simply look at:
last_autovacuum
Instead, I would work through these steps:
Step 1 — Check table activity
SELECT
relname,
n_live_tup,
n_dead_tup,
n_mod_since_analyze,
n_ins_since_vacuum,
last_autovacuum,
last_autoanalyze
FROM pg_stat_user_tables
WHERE relname = 'sample';
Step 2 — Get the actual configuration
SHOW autovacuum_vacuum_threshold;
SHOW autovacuum_vacuum_scale_factor;
SHOW autovacuum_vacuum_insert_threshold;
SHOW autovacuum_vacuum_insert_scale_factor;
SHOW autovacuum_analyze_threshold;
SHOW autovacuum_analyze_scale_factor;
Step 3 — Calculate the thresholds
Don’t assume the defaults.
Step 4 — Check table-specific settings
SELECT relname, reloptions
FROM pg_class
WHERE relname = 'sample';
Step 5 — Check whether workers are available
SELECT *
FROM pg_stat_progress_vacuum;
Then investigate worker configuration if necessary.
Final Takeaway
The most important lesson from this experiment is:
PostgreSQL does not decide to ANALYZE and VACUUM based on the same condition.
In my example:
59,137 live rows
↓
├── Autoanalyze
│ ~5,964 changes required
│ 9,898 changes observed
│ ✅ Triggered
│
├── Dead-tuple autovacuum
│ ~11,877 dead tuples required
│ 0 dead tuples observed
│ ❌ Not triggered by this condition
│
└── Insert-based autovacuum
~12,827 inserts required
18,138 inserts observed
→ eligible, but execution depends on autovacuum scheduling/worker availability
This is why pg_stat_user_tables is such a valuable view for PostgreSQL DBAs.
Don’t just look at whether autovacuum ran.
Look at the counters, calculate the threshold, verify the configuration, and then explain the behavior.
Useful references
- PostgreSQL pg_stat_user_tables
- PostgreSQL Autovacuum documentation
- PostgreSQL VACUUM and ANALYZE documentation
- pg_stat_progress_vacuum
Tags:
#PostgreSQL #PostgreSQLDBA #Autovacuum #AutoAnalyze #DatabaseAdministration #DatabasePerformance #Postgres #DBA #SQL #PerformanceTuning #OpenSource #DatabaseEngineering
Comments
Post a Comment