Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes15kdownloads
sql-analyze.html207 linesDownload Raw Back to html
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 16.3 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">3ANALYZE [ ( <em class="replaceable"><code>option</code></em> [, ...] ) ] [ <em class="replaceable"><code>table_and_columns</code></em> [, ...] ]4ANALYZE [ VERBOSE ] [ <em class="replaceable"><code>table_and_columns</code></em> [, ...] ]5 6<span class="phrase">where <em class="replaceable"><code>option</code></em> can be one of:</span>7 8    VERBOSE [ <em class="replaceable"><code>boolean</code></em> ]9    SKIP_LOCKED [ <em class="replaceable"><code>boolean</code></em> ]10    BUFFER_USAGE_LIMIT <em class="replaceable"><code>size</code></em>11 12<span class="phrase">and <em class="replaceable"><code>table_and_columns</code></em> is:</span>13 14    <em class="replaceable"><code>table_name</code></em> [ ( <em class="replaceable"><code>column_name</code></em> [, ...] ) ]15</pre></div><div class="refsect1" id="id-1.9.3.46.5"><h2>Description</h2><p>16   <code class="command">ANALYZE</code> collects statistics about the contents17   of tables in the database, and stores the results in the <a class="link" href="catalog-pg-statistic.html" title="53.51. pg_statistic"><code class="structname">pg_statistic</code></a>18   system catalog.  Subsequently, the query planner uses these19   statistics to help determine the most efficient execution plans for20   queries.21  </p><p>22   Without a <em class="replaceable"><code>table_and_columns</code></em>23   list, <code class="command">ANALYZE</code> processes every table and materialized view24   in the current database that the current user has permission to analyze.25   With a list, <code class="command">ANALYZE</code> processes only those table(s).26   It is further possible to give a list of column names for a table,27   in which case only the statistics for those columns are collected.28  </p><p>29   When the option list is surrounded by parentheses, the options can be30   written in any order.  The parenthesized syntax was added in31   <span class="productname">PostgreSQL</span> 11;  the unparenthesized syntax32   is deprecated.33  </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>34      Enables display of progress messages.35     </p></dd><dt><span class="term"><code class="literal">SKIP_LOCKED</code></span></dt><dd><p>36      Specifies that <code class="command">ANALYZE</code> should not wait for any37      conflicting locks to be released when beginning work on a relation:38      if a relation cannot be locked immediately without waiting, the relation39      is skipped.  Note that even with this option, <code class="command">ANALYZE</code>40      may still block when opening the relation's indexes or when acquiring41      sample rows from partitions, table inheritance children, and some42      types of foreign tables.  Also, while <code class="command">ANALYZE</code>43      ordinarily processes all partitions of specified partitioned tables,44      this option will cause <code class="command">ANALYZE</code> to skip all45      partitions if there is a conflicting lock on the partitioned table.46     </p></dd><dt><span class="term"><code class="literal">BUFFER_USAGE_LIMIT</code></span></dt><dd><p>47      Specifies the48      <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>49      ring buffer size for <code class="command">ANALYZE</code>.  This size is used to50      calculate the number of shared buffers which will be reused as part of51      this strategy.  <code class="literal">0</code> disables use of a52      <code class="literal">Buffer Access Strategy</code>.   When this option is not53      specified, <code class="command">ANALYZE</code> uses the value from54      <a class="xref" href="runtime-config-resource.html#GUC-VACUUM-BUFFER-USAGE-LIMIT">vacuum_buffer_usage_limit</a>.  Higher settings can55      allow <code class="command">ANALYZE</code> to run more quickly, but having too56      large a setting may cause too many other useful pages to be evicted from57      shared buffers.  The minimum value is <code class="literal">128 kB</code> and the58      maximum value is <code class="literal">16 GB</code>.59     </p></dd><dt><span class="term"><em class="replaceable"><code>boolean</code></em></span></dt><dd><p>60      Specifies whether the selected option should be turned on or off.61      You can write <code class="literal">TRUE</code>, <code class="literal">ON</code>, or62      <code class="literal">1</code> to enable the option, and <code class="literal">FALSE</code>,63      <code class="literal">OFF</code>, or <code class="literal">0</code> to disable it.  The64      <em class="replaceable"><code>boolean</code></em> value can also65      be omitted, in which case <code class="literal">TRUE</code> is assumed.66     </p></dd><dt><span class="term"><em class="replaceable"><code>size</code></em></span></dt><dd><p>67      Specifies an amount of memory in kilobytes.  Sizes may also be specified68      as a string containing the numerical size followed by any one of the69      following memory units: <code class="literal">B</code> (bytes),70      <code class="literal">kB</code> (kilobytes), <code class="literal">MB</code> (megabytes),71      <code class="literal">GB</code> (gigabytes), or <code class="literal">TB</code> (terabytes).72     </p></dd><dt><span class="term"><em class="replaceable"><code>table_name</code></em></span></dt><dd><p>73      The name (possibly schema-qualified) of a specific table to74      analyze.  If omitted, all regular tables, partitioned tables, and75      materialized views in the current database are analyzed (but not76      foreign tables).  If the specified table is a partitioned table, both the77      inheritance statistics of the partitioned table as a whole and78      statistics of the individual partitions are updated.79     </p></dd><dt><span class="term"><em class="replaceable"><code>column_name</code></em></span></dt><dd><p>80      The name of a specific column to analyze. Defaults to all columns.81     </p></dd></dl></div></div><div class="refsect1" id="id-1.9.3.46.7"><h2>Outputs</h2><p>82    When <code class="literal">VERBOSE</code> is specified, <code class="command">ANALYZE</code> emits83    progress messages to indicate which table is currently being84    processed.  Various statistics about the tables are printed as well.85   </p></div><div class="refsect1" id="id-1.9.3.46.8"><h2>Notes</h2><p>86   To analyze a table, one must ordinarily be the table's owner or a87   superuser.  However, database owners are allowed to88   analyze all tables in their databases, except shared catalogs.89   (The restriction for shared catalogs means that a true database-wide90   <code class="command">ANALYZE</code> can only be performed by a superuser.)91   <code class="command">ANALYZE</code> will skip over any tables that the calling user92   does not have permission to analyze.93  </p><p>94   Foreign tables are analyzed only when explicitly selected.  Not all95   foreign data wrappers support <code class="command">ANALYZE</code>.  If the table's96   wrapper does not support <code class="command">ANALYZE</code>, the command prints a97   warning and does nothing.98  </p><p>99   In the default <span class="productname">PostgreSQL</span> configuration,100   the autovacuum daemon (see <a class="xref" href="routine-vacuuming.html#AUTOVACUUM" title="25.1.6. The Autovacuum Daemon">Section 25.1.6</a>)101   takes care of automatic analyzing of tables when they are first loaded102   with data, and as they change throughout regular operation.103   When autovacuum is disabled,104   it is a good idea to run <code class="command">ANALYZE</code> periodically, or105   just after making major changes in the contents of a table.  Accurate106   statistics will help the planner to choose the most appropriate query107   plan, and thereby improve the speed of query processing.  A common108   strategy for read-mostly databases is to run <a class="link" href="sql-vacuum.html" title="VACUUM"><code class="command">VACUUM</code></a>109   and <code class="command">ANALYZE</code> once a day during a low-usage time of day.110   (This will not be sufficient if there is heavy update activity.)111  </p><p>112   <code class="command">ANALYZE</code>113   requires only a read lock on the target table, so it can run in114   parallel with other activity on the table.115  </p><p>116   The statistics collected by <code class="command">ANALYZE</code> usually117   include a list of some of the most common values in each column and118   a histogram showing the approximate data distribution in each119   column.  One or both of these can be omitted if120   <code class="command">ANALYZE</code> deems them uninteresting (for example,121   in a unique-key column, there are no common values) or if the122   column data type does not support the appropriate operators.  There123   is more information about the statistics in <a class="xref" href="maintenance.html" title="Chapter 25. Routine Database Maintenance Tasks">Chapter 25</a>.124  </p><p>125   For large tables, <code class="command">ANALYZE</code> takes a random sample126   of the table contents, rather than examining every row.  This127   allows even very large tables to be analyzed in a small amount of128   time.  Note, however, that the statistics are only approximate, and129   will change slightly each time <code class="command">ANALYZE</code> is run,130   even if the actual table contents did not change.  This might result131   in small changes in the planner's estimated costs shown by132   <a class="link" href="sql-explain.html" title="EXPLAIN"><code class="command">EXPLAIN</code></a>.133   In rare situations, this non-determinism will cause the planner's134   choices of query plans to change after <code class="command">ANALYZE</code> is run.135   To avoid this, raise the amount of statistics collected by136   <code class="command">ANALYZE</code>, as described below.137  </p><p>138   The extent of analysis can be controlled by adjusting the139   <a class="xref" href="runtime-config-query.html#GUC-DEFAULT-STATISTICS-TARGET">default_statistics_target</a> configuration variable, or140   on a column-by-column basis by setting the per-column statistics141   target with <a class="link" href="sql-altertable.html" title="ALTER TABLE"><code class="command">ALTER TABLE ... ALTER COLUMN ... SET142   STATISTICS</code></a>.143   The target value sets the144   maximum number of entries in the most-common-value list and the145   maximum number of bins in the histogram.  The default target value146   is 100, but this can be adjusted up or down to trade off accuracy of147   planner estimates against the time taken for148   <code class="command">ANALYZE</code> and the amount of space occupied in149   <code class="literal">pg_statistic</code>.  In particular, setting the150   statistics target to zero disables collection of statistics for151   that column.  It might be useful to do that for columns that are152   never used as part of the <code class="literal">WHERE</code>, <code class="literal">GROUP BY</code>,153   or <code class="literal">ORDER BY</code> clauses of queries, since the planner will154   have no use for statistics on such columns.155  </p><p>156   The largest statistics target among the columns being analyzed determines157   the number of table rows sampled to prepare the statistics.  Increasing158   the target causes a proportional increase in the time and space needed159   to do <code class="command">ANALYZE</code>.160  </p><p>161   One of the values estimated by <code class="command">ANALYZE</code> is the number of162   distinct values that appear in each column.  Because only a subset of the163   rows are examined, this estimate can sometimes be quite inaccurate, even164   with the largest possible statistics target.  If this inaccuracy leads to165   bad query plans, a more accurate value can be determined manually and then166   installed with167   <a class="link" href="sql-altertable.html" title="ALTER TABLE"><code class="command">ALTER TABLE ... ALTER COLUMN ... SET (n_distinct = ...)</code></a>.168  </p><p>169    If the table being analyzed has inheritance children,170    <code class="command">ANALYZE</code> gathers two sets of statistics: one on the rows171    of the parent table only, and a second including rows of both the parent172    table and all of its children.  This second set of statistics is needed when173    planning queries that process the inheritance tree as a whole.  The child174    tables themselves are not individually analyzed in this case.175    The autovacuum daemon, however, will only consider inserts or176    updates on the parent table itself when deciding whether to trigger an177    automatic analyze for that table.  If that table is rarely inserted into178    or updated, the inheritance statistics will not be up to date unless you179    run <code class="command">ANALYZE</code> manually.180  </p><p>181    For partitioned tables, <code class="command">ANALYZE</code> gathers statistics by182    sampling rows from all partitions; in addition, it will recurse into each183    partition and update its statistics.  Each leaf partition is analyzed only184    once, even with multi-level partitioning.  No statistics are collected for185    only the parent table (without data from its partitions), because with186    partitioning it's guaranteed to be empty.187  </p><p>188    The autovacuum daemon does not process partitioned tables, nor does it189    process inheritance parents if only the children are ever modified.190    It is usually necessary to periodically run a manual191    <code class="command">ANALYZE</code> to keep the statistics of the table hierarchy192    up to date.193  </p><p>194    If any child tables or partitions are foreign tables whose foreign195    data wrappers do not support <code class="command">ANALYZE</code>, those tables are196    ignored while gathering inheritance statistics.197  </p><p>198    If the table being analyzed is completely empty, <code class="command">ANALYZE</code>199    will not record new statistics for that table.  Any existing statistics200    will be retained.201  </p><p>202    Each backend running <code class="command">ANALYZE</code> will report its progress203    in the <code class="structname">pg_stat_progress_analyze</code> view. See204    <a class="xref" href="progress-reporting.html#ANALYZE-PROGRESS-REPORTING" title="28.4.1. ANALYZE Progress Reporting">Section 28.4.1</a> for details.205  </p></div><div class="refsect1" id="id-1.9.3.46.9"><h2>Compatibility</h2><p>206   There is no <code class="command">ANALYZE</code> statement in the SQL standard.207  </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-resource.html#RUNTIME-CONFIG-RESOURCE-VACUUM-COST" title="20.4.4. Cost-based Vacuum Delay">Section 20.4.4</a>, <a class="xref" href="routine-vacuuming.html#AUTOVACUUM" title="25.1.6. The Autovacuum Daemon">Section 25.1.6</a>, <a class="xref" href="progress-reporting.html#ANALYZE-PROGRESS-REPORTING" title="28.4.1. ANALYZE Progress Reporting">Section 28.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 16.3 Documentation">Home</a></td><td width="40%" align="right" valign="top"> BEGIN</td></tr></table></div></body></html>
codekingpro/portable-devtools · Team Ai