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.

Kindcommand
Example LanguageSQL

📝 Syntax

ANALYZE [VERBOSE] [<table> [(<column>, ...)]]

-- examples
ANALYZE orders;
ANALYZE (VERBOSE) orders;
VACUUM (ANALYZE) orders;

⚙️ Parameters

VERBOSEPrints 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

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.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

Content reviewed by MOTO · Found an error? Report it

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏