ANALYZE (Collect Statistics)
Last updated: 2026-11-04
ANALYZE (Collect Statistics)
ANALYZE scans a table and updates the statistics the planner relies on: row counts, the distribution of values per column, most-common values, and column correlations. The planner turns those into cost estimates, so stale statistics can make it pick a bad plan. PostgreSQL's autovacuum does this automatically, but it is still worth running ANALYZE by hand after a bulk load.
| Kind | command |
|---|---|
| Example Language | SQL |
📝 Syntax
ANALYZE [VERBOSE] [<table> [(<column>, ...)]]
-- examples
ANALYZE orders;
ANALYZE (VERBOSE) orders;
VACUUM (ANALYZE) orders;
⚙️ Parameters
| VERBOSE | Prints collected samples and other detail |
|---|---|
| <table> | Table to analyze; all tables if omitted |
| <column> | Restricts the analysis to these columns |
Returns:No return value. Once the statistics are refreshed the planner uses them on the next query.
▶ Example
Runs real PostgreSQL 18 right in your browser (PGlite WASM, nothing to install). Press ▶ Run and the result appears in the panel below.
Output:
DROP TABLE
CREATE TABLE
INSERT 0 1000
ANALYZE
QUERY PLAN
---------------------------------------------------------------------------------------------------
Seq Scan on orders (cost=0.00..19.50 rows=1 width=17) (actual time=0.042..0.113 rows=1.00 loops=1)
Filter: (customer = 'cust42'::text)
Rows Removed by Filter: 999
Buffers: shared hit=7
Planning:
Buffers: shared hit=28
Planning Time: 0.133 ms
Execution Time: 0.226 ms
📌 Key Points
- ANALYZE only reads data and never changes it, and costs far less than VACUUM.
- Stale statistics are a common cause of slow queries — compare estimated rows with actual rows in EXPLAIN to spot it.
- ANALYZE VERBOSE prints the collected samples, which helps when investigating bad statistics.
- autovacuum_analyze_scale_factor sets when automatic analysis kicks in, which is often too slow after a bulk load.
- Restrict the work to a single column with ANALYZE table (column);
- VACUUM ANALYZE does both space reclamation and statistics in one pass — the usual combination.
- The actual time, Planning Time, and Execution Time in the sample output are real timings from that run and fluctuate with the machine and load, so treat them as indicative; the plan shape and row counts are the stable part.
💡 Related Cases
More case studies coming soon.
📚 Related Reference
❓ FAQ
QWhat is the difference between ANALYZE and VACUUM?
AANALYZE refreshes statistics so the planner estimates better; VACUUM reclaims the space left by dead tuples from UPDATE and DELETE. VACUUM ANALYZE does both.
QWhen should I run ANALYZE manually?
ARight after a bulk insert or bulk update, before autovacuum catches up — the planner then works with an accurate distribution right away.