Cookbook recipe

What ANALYZE collects and why plans depend on it

Applies to PostgreSQL 13–17 Last reviewed May 2026 Grounded in source
Estimated investigation4 min

Scenario

After a big data load, queries suddenly pick terrible plans. The statistics are stale — ANALYZE fixes the planner's view of your data. Diagnose it See when stats were last gathered: SELECT relname, last_analyze, last_autoanalyze FROM…

Investigation Path

After a big data load, queries suddenly pick terrible plans. The statistics are stale — ANALYZE fixes the planner’s view of your data.

Diagnose it

See when stats were last gathered:

SELECT relname, last_analyze, last_autoanalyze
FROM pg_stat_user_tables ORDER BY last_analyze NULLS FIRST;

Why it happens

The planner estimates costs from statistics: row counts, most-common values, histograms, and distinct counts. After bulk changes those stats are stale, so estimates — and the chosen plans — go wrong until ANALYZE refreshes them.

This is a Pro lesson

Get every Learning Pathway and cookbook recipe — grounded in PostgreSQL source code, with diagnostics, fixes, and prevention for each topic.

Continue this lesson to learn:

  • How to fix it
  • Prevent it next time
  • Related & next steps
  • All 36 Learning Pathway lessons
  • 170+ cookbook recipes
  • Source-grounded diagnostics & fixes

Secure checkout Cancel anytime Source-grounded

Career Impact

This scenario builds production judgment and operational confidence under pressure.

Open Career Dashboard →

Keep going

Related & next steps

Was this helpful?

← All cookbook recipes