Sub topic 8
Autoanalyze: when statistics refresh themselves
Assume you know basic SQL, can run psql, and can read simple EXPLAIN output. This chapter focuses on how PostgreSQL decides to refresh planner statistics automatically.
Why autoanalyze matters
The PostgreSQL cost-based planner estimates how many rows each query step will process. It uses statistics collected by ANALYZE. If those statistics describe an old version of a table, the planner can choose a poor plan.
Autoanalyze is the automatic form of ANALYZE. The autovacuum launcher periodically checks tables and schedules work for autovacuum workers. A worker analyzes a table when enough rows have been inserted, updated, or deleted since the last analysis.
Autoanalyze does not replace vacuuming. Vacuum reclaims space from dead row versions and helps maintain table health. Analyze updates planner statistics. A table can need one operation, the other, or both.
The autoanalyze threshold
For a normal table, PostgreSQL uses a threshold based on the table's size:
analyze threshold =
autovacuum_analyze_threshold
+ autovacuum_analyze_scale_factor × estimated table row countThe usual defaults are:
autovacuum_analyze_threshold: 50 changed rowsautovacuum_analyze_scale_factor: 0.1, or 10 percent
For a table estimated to contain 100,000 rows, autoanalyze normally becomes eligible after about 10,050 qualifying changes.
The change counter includes inserts, updates, and deletes. Eligibility does not mean that analysis starts immediately. The autovacuum launcher must discover the table, and a worker must become available.
You can inspect the current settings:
SHOW autovacuum;
SHOW autovacuum_analyze_threshold;
SHOW autovacuum_analyze_scale_factor;Check whether statistics are stale
The statistics view pg_stat_user_tables records analysis times and the number of changes since the last analysis:
SELECT relname,
n_live_tup,
n_mod_since_analyze,
last_analyze,
last_autoanalyze
FROM pg_stat_user_tables
WHERE relname = 'orders';n_mod_since_analyze is an estimate, not an exact audit log. A large value compared with the table size suggests that the planner may be using stale statistics.
You can run ANALYZE yourself when a query needs accurate statistics immediately:
ANALYZE orders;For maintenance that performs both operations, use:
VACUUM (ANALYZE) orders;VACUUM (ANALYZE) can take longer because it performs vacuum work as well as collecting statistics.
Tune a table with unusual change patterns
A table that changes rapidly may reach the global threshold too slowly for its workload. A small, high-value table may also need more frequent analysis. Set a table-level storage parameter:
ALTER TABLE orders
SET (autovacuum_analyze_scale_factor = 0.02);
ALTER TABLE orders
SET (autovacuum_analyze_threshold = 100);
SELECT relname, reloptions
FROM pg_class
WHERE relname = 'orders';With these settings, the threshold is approximately 100 plus 2 percent of the estimated row count.
Use a lower scale factor for tables whose data distribution changes often. Avoid setting it to zero without a reason. Very frequent analysis consumes I/O and worker time, especially on busy systems.
A practical diagnosis
Use this sequence when estimates look wrong:
- Run
EXPLAIN (ANALYZE)and compare estimated rows with actual rows. - Check
n_mod_since_analyze,last_analyze, andlast_autoanalyze. - Run
ANALYZEmanually and repeat the query. - If the estimate improves, tune the table's autoanalyze settings.
- If it does not improve, inspect statistics targets or extended statistics. The problem may be a distribution or column-correlation issue rather than timing.
Exercise
A table has about 200,000 rows, and its default analyze settings are unchanged. Approximately how many changed rows make it eligible for autoanalyze?
Show answer
About 20,050 rows: 50 plus 10 percent of 200,000.