Find stale planner statistics in PostgreSQL
The planner is only as good as its statistics. This shows which tables have changed the most since stats were last collected.
The task
PostgreSQL tracks n_mod_since_analyze per table: the estimated number of rows inserted, updated or deleted since statistics were last collected. When the statistics lag far behind the data, the planner misjudges row counts, and that produces the classic bad plans: the wrong join order, a nested loop over a million rows it expected to be ten, or a hash table sized for a distribution that no longer exists. A common case is a column of dates where the histogram ends last week, so the planner thinks queries for today match almost nothing.
The snippet
Written for PostgreSQL. Versions for SQL Server are under Variations.
SELECT relname AS table_name,
n_mod_since_analyze,
n_live_tup,
ROUND(100.0 * n_mod_since_analyze / NULLIF(n_live_tup, 0), 1) AS pct_changed,
last_analyze, last_autoanalyze,
GREATEST(last_analyze, last_autoanalyze) AS last_stats
FROM pg_stat_user_tables
WHERE n_mod_since_analyze > 0
ORDER BY n_mod_since_analyze DESC
LIMIT 25;
Как это работает
n_mod_since_analyze is the number of rows inserted, updated or deleted since the last ANALYZE or autoanalyze, from the cumulative statistics system. pct_changed relates it to n_live_tup, so a 1,000-row change reads as trivial on a 10-million-row table and as most of the table on a 2,000-row one; it can exceed 100 when the same rows change repeatedly. GREATEST of last_analyze and last_autoanalyze gives the effective last-stats timestamp; NULL on both means the table has never been analyzed, a strong candidate for immediate action, often right after a bulk load.
Variations
Tables never analyzed at all
SELECT relname, n_live_tup, last_analyze, last_autoanalyze
FROM pg_stat_user_tables
WHERE last_analyze IS NULL AND last_autoanalyze IS NULL
ORDER BY n_live_tup DESC;
SQL Server: statistics update age
SQL ServerSELECT OBJECT_SCHEMA_NAME(s.object_id) AS schema_name,
OBJECT_NAME(s.object_id) AS table_name, s.name AS stats_name,
sp.last_updated, sp.rows, sp.modification_counter,
CAST(100.0 * sp.modification_counter / NULLIF(sp.rows, 0) AS DECIMAL(8,1)) AS pct_modified
FROM sys.stats s
CROSS APPLY sys.dm_db_stats_properties(s.object_id, s.stats_id) sp
WHERE OBJECT_NAME(s.object_id) IS NOT NULL
ORDER BY sp.modification_counter DESC;
Questions that come up
Does autovacuum automatically analyze too?
Yes. The autovacuum daemon analyzes a table once n_mod_since_analyze exceeds autovacuum_analyze_threshold plus autovacuum_analyze_scale_factor times the row count, 50 rows plus 10 percent by default. On a 500-million-row table that is 50 million changes, which can take weeks, so large tables often get a lower per-table autovacuum_analyze_scale_factor. Run ANALYZE yourself right after a bulk load, before the queries that depend on it, because autovacuum gets to it only later.
I just ran ANALYZE but the plan is still bad. Why?
ANALYZE samples about 300 times default_statistics_target rows (30,000 by default) and builds per-column statistics, which leaves several gaps. It assumes columns are independent, so city = 'Paris' AND country = 'France' is underestimated until you create extended statistics (CREATE STATISTICS ... ON city, country). A skewed column may need a higher per-column statistics target. And a prepared statement can switch to a generic plan after five executions, built without knowing the parameter values. Compare estimated and actual rows in EXPLAIN (ANALYZE) to see which estimate is off.
Keep reading
Run these with autocomplete that knows your tables
SQLly is a native SQL workbench for macOS, Windows and Linux. Paste a snippet into a live connection and you get schema-aware IntelliSense, a plan viewer and guards against unsafe edits.