gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

196 line
16KB

  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>VACUUM</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 V1.79.1" /><link rel="prev" href="sql-update.html" title="UPDATE" /><link rel="next" href="sql-values.html" title="VALUES" /></head><body><div xmlns="http://www.w3.org/TR/xhtml1/transitional" class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">VACUUM</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="sql-update.html" title="UPDATE">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 12.4 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="sql-values.html" title="VALUES">Next</a></td></tr></table><hr></hr></div><div class="refentry" id="SQL-VACUUM"><div class="titlepage"></div><a id="id-1.9.3.183.1" class="indexterm"></a><div class="refnamediv"><h2><span class="refentrytitle">VACUUM</span></h2><p>VACUUM — garbage-collect and optionally analyze a database</p></div><div class="refsynopsisdiv"><h2>Synopsis</h2><pre class="synopsis">
  3. VACUUM [ ( <em class="replaceable"><code>option</code></em> [, ...] ) ] [ <em class="replaceable"><code>table_and_columns</code></em> [, ...] ]
  4. VACUUM [ FULL ] [ FREEZE ] [ VERBOSE ] [ ANALYZE ] [ <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>
  6. FULL [ <em class="replaceable"><code>boolean</code></em> ]
  7. FREEZE [ <em class="replaceable"><code>boolean</code></em> ]
  8. VERBOSE [ <em class="replaceable"><code>boolean</code></em> ]
  9. ANALYZE [ <em class="replaceable"><code>boolean</code></em> ]
  10. DISABLE_PAGE_SKIPPING [ <em class="replaceable"><code>boolean</code></em> ]
  11. SKIP_LOCKED [ <em class="replaceable"><code>boolean</code></em> ]
  12. INDEX_CLEANUP [ <em class="replaceable"><code>boolean</code></em> ]
  13. TRUNCATE [ <em class="replaceable"><code>boolean</code></em> ]
  14. <span class="phrase">and <em class="replaceable"><code>table_and_columns</code></em> is:</span>
  15. <em class="replaceable"><code>table_name</code></em> [ ( <em class="replaceable"><code>column_name</code></em> [, ...] ) ]
  16. </pre></div><div class="refsect1" id="id-1.9.3.183.5"><h2>Description</h2><p>
  17. <code class="command">VACUUM</code> reclaims storage occupied by dead tuples.
  18. In normal <span class="productname">PostgreSQL</span> operation, tuples that
  19. are deleted or obsoleted by an update are not physically removed from
  20. their table; they remain present until a <code class="command">VACUUM</code> is
  21. done. Therefore it's necessary to do <code class="command">VACUUM</code>
  22. periodically, especially on frequently-updated tables.
  23. </p><p>
  24. Without a <em class="replaceable"><code>table_and_columns</code></em>
  25. list, <code class="command">VACUUM</code> processes every table and materialized view
  26. in the current database that the current user has permission to vacuum.
  27. With a list, <code class="command">VACUUM</code> processes only those table(s).
  28. </p><p>
  29. <code class="command">VACUUM ANALYZE</code> performs a <code class="command">VACUUM</code>
  30. and then an <code class="command">ANALYZE</code> for each selected table. This
  31. is a handy combination form for routine maintenance scripts. See
  32. <a class="xref" href="sql-analyze.html" title="ANALYZE"><span class="refentrytitle">ANALYZE</span></a>
  33. for more details about its processing.
  34. </p><p>
  35. Plain <code class="command">VACUUM</code> (without <code class="literal">FULL</code>) simply reclaims
  36. space and makes it
  37. available for re-use. This form of the command can operate in parallel
  38. with normal reading and writing of the table, as an exclusive lock
  39. is not obtained. However, extra space is not returned to the operating
  40. system (in most cases); it's just kept available for re-use within the
  41. same table. <code class="command">VACUUM FULL</code> rewrites the entire contents
  42. of the table into a new disk file with no extra space, allowing unused
  43. space to be returned to the operating system. This form is much slower and
  44. requires an exclusive lock on each table while it is being processed.
  45. </p><p>
  46. When the option list is surrounded by parentheses, the options can be
  47. written in any order. Without parentheses, options must be specified
  48. in exactly the order shown above.
  49. The parenthesized syntax was added in
  50. <span class="productname">PostgreSQL</span> 9.0; the unparenthesized
  51. syntax is deprecated.
  52. </p></div><div class="refsect1" id="id-1.9.3.183.6"><h2>Parameters</h2><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="literal">FULL</code></span></dt><dd><p>
  53. Selects <span class="quote">“<span class="quote">full</span>”</span> vacuum, which can reclaim more
  54. space, but takes much longer and exclusively locks the table.
  55. This method also requires extra disk space, since it writes a
  56. new copy of the table and doesn't release the old copy until
  57. the operation is complete. Usually this should only be used when a
  58. significant amount of space needs to be reclaimed from within the table.
  59. </p></dd><dt><span class="term"><code class="literal">FREEZE</code></span></dt><dd><p>
  60. Selects aggressive <span class="quote">“<span class="quote">freezing</span>”</span> of tuples.
  61. Specifying <code class="literal">FREEZE</code> is equivalent to performing
  62. <code class="command">VACUUM</code> with the
  63. <a class="xref" href="runtime-config-client.html#GUC-VACUUM-FREEZE-MIN-AGE">vacuum_freeze_min_age</a> and
  64. <a class="xref" href="runtime-config-client.html#GUC-VACUUM-FREEZE-TABLE-AGE">vacuum_freeze_table_age</a> parameters
  65. set to zero. Aggressive freezing is always performed when the
  66. table is rewritten, so this option is redundant when <code class="literal">FULL</code>
  67. is specified.
  68. </p></dd><dt><span class="term"><code class="literal">VERBOSE</code></span></dt><dd><p>
  69. Prints a detailed vacuum activity report for each table.
  70. </p></dd><dt><span class="term"><code class="literal">ANALYZE</code></span></dt><dd><p>
  71. Updates statistics used by the planner to determine the most
  72. efficient way to execute a query.
  73. </p></dd><dt><span class="term"><code class="literal">DISABLE_PAGE_SKIPPING</code></span></dt><dd><p>
  74. Normally, <code class="command">VACUUM</code> will skip pages based on the <a class="link" href="routine-vacuuming.html#VACUUM-FOR-VISIBILITY-MAP" title="24.1.4. Updating the Visibility Map">visibility map</a>. Pages where
  75. all tuples are known to be frozen can always be skipped, and those
  76. where all tuples are known to be visible to all transactions may be
  77. skipped except when performing an aggressive vacuum. Furthermore,
  78. except when performing an aggressive vacuum, some pages may be skipped
  79. in order to avoid waiting for other sessions to finish using them.
  80. This option disables all page-skipping behavior, and is intended to
  81. be used only when the contents of the visibility map are
  82. suspect, which should happen only if there is a hardware or software
  83. issue causing database corruption.
  84. </p></dd><dt><span class="term"><code class="literal">SKIP_LOCKED</code></span></dt><dd><p>
  85. Specifies that <code class="command">VACUUM</code> should not wait for any
  86. conflicting locks to be released when beginning work on a relation:
  87. if a relation cannot be locked immediately without waiting, the relation
  88. is skipped. Note that even with this option,
  89. <code class="command">VACUUM</code> may still block when opening the relation's
  90. indexes. Additionally, <code class="command">VACUUM ANALYZE</code> may still
  91. block when acquiring sample rows from partitions, table inheritance
  92. children, and some types of foreign tables. Also, while
  93. <code class="command">VACUUM</code> ordinarily processes all partitions of
  94. specified partitioned tables, this option will cause
  95. <code class="command">VACUUM</code> to skip all partitions if there is a
  96. conflicting lock on the partitioned table.
  97. </p></dd><dt><span class="term"><code class="literal">INDEX_CLEANUP</code></span></dt><dd><p>
  98. Specifies that <code class="command">VACUUM</code> should attempt to remove
  99. index entries pointing to dead tuples. This is normally the desired
  100. behavior and is the default unless the
  101. <code class="literal">vacuum_index_cleanup</code> option has been set to false
  102. for the table to be vacuumed. Setting this option to false may be
  103. useful when it is necessary to make vacuum run as quickly as possible,
  104. for example to avoid imminent transaction ID wraparound
  105. (see <a class="xref" href="routine-vacuuming.html#VACUUM-FOR-WRAPAROUND" title="24.1.5. Preventing Transaction ID Wraparound Failures">Section 24.1.5</a>). However, if index
  106. cleanup is not performed regularly, performance may suffer, because
  107. as the table is modified, indexes will accumulate dead tuples
  108. and the table itself will accumulate dead line pointers that cannot be
  109. removed until index cleanup is completed. This option has no effect
  110. for tables that do not have an index and is ignored if the
  111. <code class="literal">FULL</code> option is used.
  112. </p></dd><dt><span class="term"><code class="literal">TRUNCATE</code></span></dt><dd><p>
  113. Specifies that <code class="command">VACUUM</code> should attempt to
  114. truncate off any empty pages at the end of the table and allow
  115. the disk space for the truncated pages to be returned to
  116. the operating system. This is normally the desired behavior
  117. and is the default unless the <code class="literal">vacuum_truncate</code>
  118. option has been set to false for the table to be vacuumed.
  119. Setting this option to false may be useful to avoid
  120. <code class="literal">ACCESS EXCLUSIVE</code> lock on the table that
  121. the truncation requires. This option is ignored if the
  122. <code class="literal">FULL</code> option is used.
  123. </p></dd><dt><span class="term"><em class="replaceable"><code>boolean</code></em></span></dt><dd><p>
  124. Specifies whether the selected option should be turned on or off.
  125. You can write <code class="literal">TRUE</code>, <code class="literal">ON</code>, or
  126. <code class="literal">1</code> to enable the option, and <code class="literal">FALSE</code>,
  127. <code class="literal">OFF</code>, or <code class="literal">0</code> to disable it. The
  128. <em class="replaceable"><code>boolean</code></em> value can also
  129. be omitted, in which case <code class="literal">TRUE</code> is assumed.
  130. </p></dd><dt><span class="term"><em class="replaceable"><code>table_name</code></em></span></dt><dd><p>
  131. The name (optionally schema-qualified) of a specific table or
  132. materialized view to vacuum. If the specified table is a partitioned
  133. table, all of its leaf partitions are vacuumed.
  134. </p></dd><dt><span class="term"><em class="replaceable"><code>column_name</code></em></span></dt><dd><p>
  135. The name of a specific column to analyze. Defaults to all columns.
  136. If a column list is specified, <code class="literal">ANALYZE</code> must also be
  137. specified.
  138. </p></dd></dl></div></div><div class="refsect1" id="id-1.9.3.183.7"><h2>Outputs</h2><p>
  139. When <code class="literal">VERBOSE</code> is specified, <code class="command">VACUUM</code> emits
  140. progress messages to indicate which table is currently being
  141. processed. Various statistics about the tables are printed as well.
  142. </p></div><div class="refsect1" id="id-1.9.3.183.8"><h2>Notes</h2><p>
  143. To vacuum a table, one must ordinarily be the table's owner or a
  144. superuser. However, database owners are allowed to
  145. vacuum all tables in their databases, except shared catalogs.
  146. (The restriction for shared catalogs means that a true database-wide
  147. <code class="command">VACUUM</code> can only be performed by a superuser.)
  148. <code class="command">VACUUM</code> will skip over any tables that the calling user
  149. does not have permission to vacuum.
  150. </p><p>
  151. <code class="command">VACUUM</code> cannot be executed inside a transaction block.
  152. </p><p>
  153. For tables with <acronym class="acronym">GIN</acronym> indexes, <code class="command">VACUUM</code> (in
  154. any form) also completes any pending index insertions, by moving pending
  155. index entries to the appropriate places in the main <acronym class="acronym">GIN</acronym> index
  156. structure. See <a class="xref" href="gin-implementation.html#GIN-FAST-UPDATE" title="66.4.1. GIN Fast Update Technique">Section 66.4.1</a> for details.
  157. </p><p>
  158. We recommend that active production databases be
  159. vacuumed frequently (at least nightly), in order to
  160. remove dead rows. After adding or deleting a large number
  161. of rows, it might be a good idea to issue a <code class="command">VACUUM
  162. ANALYZE</code> command for the affected table. This will update the
  163. system catalogs with
  164. the results of all recent changes, and allow the
  165. <span class="productname">PostgreSQL</span> query planner to make better
  166. choices in planning queries.
  167. </p><p>
  168. The <code class="option">FULL</code> option is not recommended for routine use,
  169. but might be useful in special cases. An example is when you have deleted
  170. or updated most of the rows in a table and would like the table to
  171. physically shrink to occupy less disk space and allow faster table
  172. scans. <code class="command">VACUUM FULL</code> will usually shrink the table
  173. more than a plain <code class="command">VACUUM</code> would.
  174. </p><p>
  175. <code class="command">VACUUM</code> causes a substantial increase in I/O traffic,
  176. which might cause poor performance for other active sessions. Therefore,
  177. it is sometimes advisable to use the cost-based vacuum delay feature.
  178. See <a class="xref" href="runtime-config-resource.html#RUNTIME-CONFIG-RESOURCE-VACUUM-COST" title="19.4.4. Cost-based Vacuum Delay">Section 19.4.4</a> for details.
  179. </p><p>
  180. <span class="productname">PostgreSQL</span> includes an <span class="quote">“<span class="quote">autovacuum</span>”</span>
  181. facility which can automate routine vacuum maintenance. For more
  182. information about automatic and manual vacuuming, see
  183. <a class="xref" href="routine-vacuuming.html" title="24.1. Routine Vacuuming">Section 24.1</a>.
  184. </p></div><div class="refsect1" id="id-1.9.3.183.9"><h2>Examples</h2><p>
  185. To clean a single table <code class="literal">onek</code>, analyze it for
  186. the optimizer and print a detailed vacuum activity report:
  187. </p><pre class="programlisting">
  188. VACUUM (VERBOSE, ANALYZE) onek;
  189. </pre></div><div class="refsect1" id="id-1.9.3.183.10"><h2>Compatibility</h2><p>
  190. There is no <code class="command">VACUUM</code> statement in the SQL standard.
  191. </p></div><div class="refsect1" id="id-1.9.3.183.11"><h2>See Also</h2><span class="simplelist"><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="19.4.4. Cost-based Vacuum Delay">Section 19.4.4</a>, <a class="xref" href="routine-vacuuming.html#AUTOVACUUM" title="24.1.6. The Autovacuum Daemon">Section 24.1.6</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-update.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="sql-commands.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="sql-values.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">UPDATE </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> VALUES</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1