1 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>ANALYZE</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><link rel="prev" href="sql-alterview.html" title="ALTER VIEW" /><link rel="next" href="sql-begin.html" title="BEGIN" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">ANALYZE</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="sql-alterview.html" title="ALTER VIEW">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="sql-commands.html" title="SQL Commands">Up</a></td><th width="60%" align="center">SQL Commands</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="sql-begin.html" title="BEGIN">Next</a></td></tr></table><hr /></div><div class="refentry" id="SQL-ANALYZE"><div class="titlepage"></div><a id="id-1.9.3.46.1" class="indexterm"></a><div class="refnamediv"><h2><span class="refentrytitle">ANALYZE</span></h2><p>ANALYZE — collect statistics about a database</p></div><div class="refsynopsisdiv"><h2>Synopsis</h2><pre class="synopsis">
3 ANALYZE [ ( <em class="replaceable"><code>option</code></em> [, ...] ) ] [ <em class="replaceable"><code>table_and_columns</code></em> [, ...] ]
5 <span class="phrase">where <em class="replaceable"><code>option</code></em> can be one of:</span>
7 VERBOSE [ <em class="replaceable"><code>boolean</code></em> ]
8 SKIP_LOCKED [ <em class="replaceable"><code>boolean</code></em> ]
9 BUFFER_USAGE_LIMIT <em class="replaceable"><code>size</code></em>
11 <span class="phrase">and <em class="replaceable"><code>table_and_columns</code></em> is:</span>
13 [ ONLY ] <em class="replaceable"><code>table_name</code></em> [ * ] [ ( <em class="replaceable"><code>column_name</code></em> [, ...] ) ]
14 </pre></div><div class="refsect1" id="id-1.9.3.46.5"><h2>Description</h2><p>
15 <code class="command">ANALYZE</code> collects statistics about the contents
16 of tables in the database, and stores the results in the <a class="link" href="catalog-pg-statistic.html" title="52.51. pg_statistic"><code class="structname">pg_statistic</code></a>
17 system catalog. Subsequently, the query planner uses these
18 statistics to help determine the most efficient execution plans for
21 Without a <em class="replaceable"><code>table_and_columns</code></em>
22 list, <code class="command">ANALYZE</code> processes every table and materialized view
23 in the current database that the current user has permission to analyze.
24 With a list, <code class="command">ANALYZE</code> processes only those table(s).
25 It is further possible to give a list of column names for a table,
26 in which case only the statistics for those columns are collected.
27 </p></div><div class="refsect1" id="id-1.9.3.46.6"><h2>Parameters</h2><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="literal">VERBOSE</code></span></dt><dd><p>
28 Enables display of progress messages at <code class="literal">INFO</code> level.
29 </p></dd><dt><span class="term"><code class="literal">SKIP_LOCKED</code></span></dt><dd><p>
30 Specifies that <code class="command">ANALYZE</code> should not wait for any
31 conflicting locks to be released when beginning work on a relation:
32 if a relation cannot be locked immediately without waiting, the relation
33 is skipped. Note that even with this option, <code class="command">ANALYZE</code>
34 may still block when opening the relation's indexes or when acquiring
35 sample rows from partitions, table inheritance children, and some
36 types of foreign tables. Also, while <code class="command">ANALYZE</code>
37 ordinarily processes all partitions of specified partitioned tables,
38 this option will cause <code class="command">ANALYZE</code> to skip all
39 partitions if there is a conflicting lock on the partitioned table.
40 </p></dd><dt><span class="term"><code class="literal">BUFFER_USAGE_LIMIT</code></span></dt><dd><p>
42 <a class="glossterm" href="glossary.html#GLOSSARY-BUFFER-ACCESS-STRATEGY"><em class="glossterm"><a class="glossterm" href="glossary.html#GLOSSARY-BUFFER-ACCESS-STRATEGY" title="Buffer Access Strategy">Buffer Access Strategy</a></em></a>
43 ring buffer size for <code class="command">ANALYZE</code>. This size is used to
44 calculate the number of shared buffers which will be reused as part of
45 this strategy. <code class="literal">0</code> disables use of a
46 <code class="literal">Buffer Access Strategy</code>. When this option is not
47 specified, <code class="command">ANALYZE</code> uses the value from
48 <a class="xref" href="runtime-config-resource.html#GUC-VACUUM-BUFFER-USAGE-LIMIT">vacuum_buffer_usage_limit</a>. Higher settings can
49 allow <code class="command">ANALYZE</code> to run more quickly, but having too
50 large a setting may cause too many other useful pages to be evicted from
51 shared buffers. The minimum value is <code class="literal">128 kB</code> and the
52 maximum value is <code class="literal">16 GB</code>.
53 </p></dd><dt><span class="term"><em class="replaceable"><code>boolean</code></em></span></dt><dd><p>
54 Specifies whether the selected option should be turned on or off.
55 You can write <code class="literal">TRUE</code>, <code class="literal">ON</code>, or
56 <code class="literal">1</code> to enable the option, and <code class="literal">FALSE</code>,
57 <code class="literal">OFF</code>, or <code class="literal">0</code> to disable it. The
58 <em class="replaceable"><code>boolean</code></em> value can also
59 be omitted, in which case <code class="literal">TRUE</code> is assumed.
60 </p></dd><dt><span class="term"><em class="replaceable"><code>size</code></em></span></dt><dd><p>
61 Specifies an amount of memory in kilobytes. Sizes may also be specified
62 as a string containing the numerical size followed by any one of the
63 following memory units: <code class="literal">B</code> (bytes),
64 <code class="literal">kB</code> (kilobytes), <code class="literal">MB</code> (megabytes),
65 <code class="literal">GB</code> (gigabytes), or <code class="literal">TB</code> (terabytes).
66 </p></dd><dt><span class="term"><em class="replaceable"><code>table_name</code></em></span></dt><dd><p>
67 The name (possibly schema-qualified) of a specific table to
68 analyze. If omitted, all regular tables, partitioned tables, and
69 materialized views in the current database are analyzed (but not
70 foreign tables). If <code class="literal">ONLY</code> is specified before
71 the table name, only that table is analyzed. If <code class="literal">ONLY</code>
72 is not specified, the table and all its inheritance child tables or
73 partitions (if any) are analyzed. Optionally, <code class="literal">*</code>
74 can be specified after the table name to explicitly indicate that
75 inheritance child tables (or partitions) are to be analyzed.
76 </p></dd><dt><span class="term"><em class="replaceable"><code>column_name</code></em></span></dt><dd><p>
77 The name of a specific column to analyze. Defaults to all columns.
78 </p></dd></dl></div></div><div class="refsect1" id="id-1.9.3.46.7"><h2>Outputs</h2><p>
79 When <code class="literal">VERBOSE</code> is specified, <code class="command">ANALYZE</code> emits
80 progress messages to indicate which table is currently being
81 processed. Various statistics about the tables are printed as well.
82 </p></div><div class="refsect1" id="id-1.9.3.46.8"><h2>Notes</h2><p>
83 To analyze a table, one must ordinarily have the <code class="literal">MAINTAIN</code>
84 privilege on the table. However, database owners are allowed to
85 analyze all tables in their databases, except shared catalogs.
86 <code class="command">ANALYZE</code> will skip over any tables that the calling user
87 does not have permission to analyze.
89 Foreign tables are analyzed only when explicitly selected. Not all
90 foreign data wrappers support <code class="command">ANALYZE</code>. If the table's
91 wrapper does not support <code class="command">ANALYZE</code>, the command prints a
92 warning and does nothing.
94 In the default <span class="productname">PostgreSQL</span> configuration,
95 the autovacuum daemon (see <a class="xref" href="routine-vacuuming.html#AUTOVACUUM" title="24.1.6. The Autovacuum Daemon">Section 24.1.6</a>)
96 takes care of automatic analyzing of tables when they are first loaded
97 with data, and as they change throughout regular operation.
98 When autovacuum is disabled,
99 it is a good idea to run <code class="command">ANALYZE</code> periodically, or
100 just after making major changes in the contents of a table. Accurate
101 statistics will help the planner to choose the most appropriate query
102 plan, and thereby improve the speed of query processing. A common
103 strategy for read-mostly databases is to run <a class="link" href="sql-vacuum.html" title="VACUUM"><code class="command">VACUUM</code></a>
104 and <code class="command">ANALYZE</code> once a day during a low-usage time of day.
105 (This will not be sufficient if there is heavy update activity.)
107 While <code class="command">ANALYZE</code> is running, the <a class="xref" href="runtime-config-client.html#GUC-SEARCH-PATH">search_path</a> is temporarily changed to <code class="literal">pg_catalog,
110 <code class="command">ANALYZE</code>
111 requires only a read lock on the target table, so it can run in
112 parallel with other non-DDL activity on the table.
114 The statistics collected by <code class="command">ANALYZE</code> usually
115 include a list of some of the most common values in each column and
116 a histogram showing the approximate data distribution in each
117 column. One or both of these can be omitted if
118 <code class="command">ANALYZE</code> deems them uninteresting (for example,
119 in a unique-key column, there are no common values) or if the
120 column data type does not support the appropriate operators. There
121 is more information about the statistics in <a class="xref" href="maintenance.html" title="Chapter 24. Routine Database Maintenance Tasks">Chapter 24</a>.
123 For large tables, <code class="command">ANALYZE</code> takes a random sample
124 of the table contents, rather than examining every row. This
125 allows even very large tables to be analyzed in a small amount of
126 time. Note, however, that the statistics are only approximate, and
127 will change slightly each time <code class="command">ANALYZE</code> is run,
128 even if the actual table contents did not change. This might result
129 in small changes in the planner's estimated costs shown by
130 <a class="link" href="sql-explain.html" title="EXPLAIN"><code class="command">EXPLAIN</code></a>.
131 In rare situations, this non-determinism will cause the planner's
132 choices of query plans to change after <code class="command">ANALYZE</code> is run.
133 To avoid this, raise the amount of statistics collected by
134 <code class="command">ANALYZE</code>, as described below.
136 The extent of analysis can be controlled by adjusting the
137 <a class="xref" href="runtime-config-query.html#GUC-DEFAULT-STATISTICS-TARGET">default_statistics_target</a> configuration variable, or
138 on a column-by-column basis by setting the per-column statistics
139 target with <a class="link" href="sql-altertable.html" title="ALTER TABLE"><code class="command">ALTER TABLE ... ALTER COLUMN ... SET
140 STATISTICS</code></a>.
141 The target value sets the
142 maximum number of entries in the most-common-value list and the
143 maximum number of bins in the histogram. The default target value
144 is 100, but this can be adjusted up or down to trade off accuracy of
145 planner estimates against the time taken for
146 <code class="command">ANALYZE</code> and the amount of space occupied in
147 <code class="literal">pg_statistic</code>. In particular, setting the
148 statistics target to zero disables collection of statistics for
149 that column. It might be useful to do that for columns that are
150 never used as part of the <code class="literal">WHERE</code>, <code class="literal">GROUP BY</code>,
151 or <code class="literal">ORDER BY</code> clauses of queries, since the planner will
152 have no use for statistics on such columns.
154 The largest statistics target among the columns being analyzed determines
155 the number of table rows sampled to prepare the statistics. Increasing
156 the target causes a proportional increase in the time and space needed
157 to do <code class="command">ANALYZE</code>.
159 One of the values estimated by <code class="command">ANALYZE</code> is the number of
160 distinct values that appear in each column. Because only a subset of the
161 rows are examined, this estimate can sometimes be quite inaccurate, even
162 with the largest possible statistics target. If this inaccuracy leads to
163 bad query plans, a more accurate value can be determined manually and then
165 <a class="link" href="sql-altertable.html" title="ALTER TABLE"><code class="command">ALTER TABLE ... ALTER COLUMN ... SET (n_distinct = ...)</code></a>.
167 If the table being analyzed has inheritance children,
168 <code class="command">ANALYZE</code> gathers two sets of statistics: one on the rows
169 of the parent table only, and a second including rows of both the parent
170 table and all of its children. This second set of statistics is needed when
171 planning queries that process the inheritance tree as a whole. The
172 autovacuum daemon, however, will only consider inserts or updates on the
173 parent table itself when deciding whether to trigger an automatic analyze
174 for that table. If that table is rarely inserted into or updated, the
175 inheritance statistics will not be up to date unless you run
176 <code class="command">ANALYZE</code> manually. By default,
177 <code class="command">ANALYZE</code> will also recursively collect and update the
178 statistics for each inheritance child table. The <code class="literal">ONLY</code>
179 keyword may be used to disable this.
181 For partitioned tables, <code class="command">ANALYZE</code> gathers statistics by
182 sampling rows from all partitions. By default,
183 <code class="command">ANALYZE</code> will also recursively collect and update the
184 statistics for each partition. The <code class="literal">ONLY</code> keyword may be
185 used to disable this.
187 The autovacuum daemon does not process partitioned tables, nor does it
188 process inheritance parents if only the children are ever modified.
189 It is usually necessary to periodically run a manual
190 <code class="command">ANALYZE</code> to keep the statistics of the table hierarchy
193 If any child tables or partitions are foreign tables whose foreign
194 data wrappers do not support <code class="command">ANALYZE</code>, those tables are
195 ignored while gathering inheritance statistics.
197 If the table being analyzed is completely empty, <code class="command">ANALYZE</code>
198 will not record new statistics for that table. Any existing statistics
201 Each backend running <code class="command">ANALYZE</code> will report its progress
202 in the <code class="structname">pg_stat_progress_analyze</code> view. See
203 <a class="xref" href="progress-reporting.html#ANALYZE-PROGRESS-REPORTING" title="27.4.1. ANALYZE Progress Reporting">Section 27.4.1</a> for details.
204 </p></div><div class="refsect1" id="id-1.9.3.46.9"><h2>Compatibility</h2><p>
205 There is no <code class="command">ANALYZE</code> statement in the SQL standard.
207 The following syntax was used before <span class="productname">PostgreSQL</span>
208 version 11 and is still supported:
209 </p><pre class="synopsis">
210 ANALYZE [ VERBOSE ] [ <em class="replaceable"><code>table_and_columns</code></em> [, ...] ]
212 </p></div><div class="refsect1" id="id-1.9.3.46.10"><h2>See Also</h2><span class="simplelist"><a class="xref" href="sql-vacuum.html" title="VACUUM"><span class="refentrytitle">VACUUM</span></a>, <a class="xref" href="app-vacuumdb.html" title="vacuumdb"><span class="refentrytitle"><span class="application">vacuumdb</span></span></a>, <a class="xref" href="runtime-config-vacuum.html#RUNTIME-CONFIG-RESOURCE-VACUUM-COST" title="19.10.2. Cost-based Vacuum Delay">Section 19.10.2</a>, <a class="xref" href="routine-vacuuming.html#AUTOVACUUM" title="24.1.6. The Autovacuum Daemon">Section 24.1.6</a>, <a class="xref" href="progress-reporting.html#ANALYZE-PROGRESS-REPORTING" title="27.4.1. ANALYZE Progress Reporting">Section 27.4.1</a></span></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="sql-alterview.html" title="ALTER VIEW">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="sql-commands.html" title="SQL Commands">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="sql-begin.html" title="BEGIN">Next</a></td></tr><tr><td width="40%" align="left" valign="top">ALTER VIEW </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="40%" align="right" valign="top"> BEGIN</td></tr></table></div></body></html>