|
- <?xml version="1.0" encoding="UTF-8" standalone="no"?>
- <!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">
- VACUUM [ ( <em class="replaceable"><code>option</code></em> [, ...] ) ] [ <em class="replaceable"><code>table_and_columns</code></em> [, ...] ]
- VACUUM [ FULL ] [ FREEZE ] [ VERBOSE ] [ ANALYZE ] [ <em class="replaceable"><code>table_and_columns</code></em> [, ...] ]
-
- <span class="phrase">where <em class="replaceable"><code>option</code></em> can be one of:</span>
-
- FULL [ <em class="replaceable"><code>boolean</code></em> ]
- FREEZE [ <em class="replaceable"><code>boolean</code></em> ]
- VERBOSE [ <em class="replaceable"><code>boolean</code></em> ]
- ANALYZE [ <em class="replaceable"><code>boolean</code></em> ]
- DISABLE_PAGE_SKIPPING [ <em class="replaceable"><code>boolean</code></em> ]
- SKIP_LOCKED [ <em class="replaceable"><code>boolean</code></em> ]
- INDEX_CLEANUP [ <em class="replaceable"><code>boolean</code></em> ]
- TRUNCATE [ <em class="replaceable"><code>boolean</code></em> ]
-
- <span class="phrase">and <em class="replaceable"><code>table_and_columns</code></em> is:</span>
-
- <em class="replaceable"><code>table_name</code></em> [ ( <em class="replaceable"><code>column_name</code></em> [, ...] ) ]
- </pre></div><div class="refsect1" id="id-1.9.3.183.5"><h2>Description</h2><p>
- <code class="command">VACUUM</code> reclaims storage occupied by dead tuples.
- In normal <span class="productname">PostgreSQL</span> operation, tuples that
- are deleted or obsoleted by an update are not physically removed from
- their table; they remain present until a <code class="command">VACUUM</code> is
- done. Therefore it's necessary to do <code class="command">VACUUM</code>
- periodically, especially on frequently-updated tables.
- </p><p>
- Without a <em class="replaceable"><code>table_and_columns</code></em>
- list, <code class="command">VACUUM</code> processes every table and materialized view
- in the current database that the current user has permission to vacuum.
- With a list, <code class="command">VACUUM</code> processes only those table(s).
- </p><p>
- <code class="command">VACUUM ANALYZE</code> performs a <code class="command">VACUUM</code>
- and then an <code class="command">ANALYZE</code> for each selected table. This
- is a handy combination form for routine maintenance scripts. See
- <a class="xref" href="sql-analyze.html" title="ANALYZE"><span class="refentrytitle">ANALYZE</span></a>
- for more details about its processing.
- </p><p>
- Plain <code class="command">VACUUM</code> (without <code class="literal">FULL</code>) simply reclaims
- space and makes it
- available for re-use. This form of the command can operate in parallel
- with normal reading and writing of the table, as an exclusive lock
- is not obtained. However, extra space is not returned to the operating
- system (in most cases); it's just kept available for re-use within the
- same table. <code class="command">VACUUM FULL</code> rewrites the entire contents
- of the table into a new disk file with no extra space, allowing unused
- space to be returned to the operating system. This form is much slower and
- requires an exclusive lock on each table while it is being processed.
- </p><p>
- When the option list is surrounded by parentheses, the options can be
- written in any order. Without parentheses, options must be specified
- in exactly the order shown above.
- The parenthesized syntax was added in
- <span class="productname">PostgreSQL</span> 9.0; the unparenthesized
- syntax is deprecated.
- </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>
- Selects <span class="quote">“<span class="quote">full</span>”</span> vacuum, which can reclaim more
- space, but takes much longer and exclusively locks the table.
- This method also requires extra disk space, since it writes a
- new copy of the table and doesn't release the old copy until
- the operation is complete. Usually this should only be used when a
- significant amount of space needs to be reclaimed from within the table.
- </p></dd><dt><span class="term"><code class="literal">FREEZE</code></span></dt><dd><p>
- Selects aggressive <span class="quote">“<span class="quote">freezing</span>”</span> of tuples.
- Specifying <code class="literal">FREEZE</code> is equivalent to performing
- <code class="command">VACUUM</code> with the
- <a class="xref" href="runtime-config-client.html#GUC-VACUUM-FREEZE-MIN-AGE">vacuum_freeze_min_age</a> and
- <a class="xref" href="runtime-config-client.html#GUC-VACUUM-FREEZE-TABLE-AGE">vacuum_freeze_table_age</a> parameters
- set to zero. Aggressive freezing is always performed when the
- table is rewritten, so this option is redundant when <code class="literal">FULL</code>
- is specified.
- </p></dd><dt><span class="term"><code class="literal">VERBOSE</code></span></dt><dd><p>
- Prints a detailed vacuum activity report for each table.
- </p></dd><dt><span class="term"><code class="literal">ANALYZE</code></span></dt><dd><p>
- Updates statistics used by the planner to determine the most
- efficient way to execute a query.
- </p></dd><dt><span class="term"><code class="literal">DISABLE_PAGE_SKIPPING</code></span></dt><dd><p>
- 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
- all tuples are known to be frozen can always be skipped, and those
- where all tuples are known to be visible to all transactions may be
- skipped except when performing an aggressive vacuum. Furthermore,
- except when performing an aggressive vacuum, some pages may be skipped
- in order to avoid waiting for other sessions to finish using them.
- This option disables all page-skipping behavior, and is intended to
- be used only when the contents of the visibility map are
- suspect, which should happen only if there is a hardware or software
- issue causing database corruption.
- </p></dd><dt><span class="term"><code class="literal">SKIP_LOCKED</code></span></dt><dd><p>
- Specifies that <code class="command">VACUUM</code> should not wait for any
- conflicting locks to be released when beginning work on a relation:
- if a relation cannot be locked immediately without waiting, the relation
- is skipped. Note that even with this option,
- <code class="command">VACUUM</code> may still block when opening the relation's
- indexes. Additionally, <code class="command">VACUUM ANALYZE</code> may still
- block when acquiring sample rows from partitions, table inheritance
- children, and some types of foreign tables. Also, while
- <code class="command">VACUUM</code> ordinarily processes all partitions of
- specified partitioned tables, this option will cause
- <code class="command">VACUUM</code> to skip all partitions if there is a
- conflicting lock on the partitioned table.
- </p></dd><dt><span class="term"><code class="literal">INDEX_CLEANUP</code></span></dt><dd><p>
- Specifies that <code class="command">VACUUM</code> should attempt to remove
- index entries pointing to dead tuples. This is normally the desired
- behavior and is the default unless the
- <code class="literal">vacuum_index_cleanup</code> option has been set to false
- for the table to be vacuumed. Setting this option to false may be
- useful when it is necessary to make vacuum run as quickly as possible,
- for example to avoid imminent transaction ID wraparound
- (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
- cleanup is not performed regularly, performance may suffer, because
- as the table is modified, indexes will accumulate dead tuples
- and the table itself will accumulate dead line pointers that cannot be
- removed until index cleanup is completed. This option has no effect
- for tables that do not have an index and is ignored if the
- <code class="literal">FULL</code> option is used.
- </p></dd><dt><span class="term"><code class="literal">TRUNCATE</code></span></dt><dd><p>
- Specifies that <code class="command">VACUUM</code> should attempt to
- truncate off any empty pages at the end of the table and allow
- the disk space for the truncated pages to be returned to
- the operating system. This is normally the desired behavior
- and is the default unless the <code class="literal">vacuum_truncate</code>
- option has been set to false for the table to be vacuumed.
- Setting this option to false may be useful to avoid
- <code class="literal">ACCESS EXCLUSIVE</code> lock on the table that
- the truncation requires. This option is ignored if the
- <code class="literal">FULL</code> option is used.
- </p></dd><dt><span class="term"><em class="replaceable"><code>boolean</code></em></span></dt><dd><p>
- Specifies whether the selected option should be turned on or off.
- You can write <code class="literal">TRUE</code>, <code class="literal">ON</code>, or
- <code class="literal">1</code> to enable the option, and <code class="literal">FALSE</code>,
- <code class="literal">OFF</code>, or <code class="literal">0</code> to disable it. The
- <em class="replaceable"><code>boolean</code></em> value can also
- be omitted, in which case <code class="literal">TRUE</code> is assumed.
- </p></dd><dt><span class="term"><em class="replaceable"><code>table_name</code></em></span></dt><dd><p>
- The name (optionally schema-qualified) of a specific table or
- materialized view to vacuum. If the specified table is a partitioned
- table, all of its leaf partitions are vacuumed.
- </p></dd><dt><span class="term"><em class="replaceable"><code>column_name</code></em></span></dt><dd><p>
- The name of a specific column to analyze. Defaults to all columns.
- If a column list is specified, <code class="literal">ANALYZE</code> must also be
- specified.
- </p></dd></dl></div></div><div class="refsect1" id="id-1.9.3.183.7"><h2>Outputs</h2><p>
- When <code class="literal">VERBOSE</code> is specified, <code class="command">VACUUM</code> emits
- progress messages to indicate which table is currently being
- processed. Various statistics about the tables are printed as well.
- </p></div><div class="refsect1" id="id-1.9.3.183.8"><h2>Notes</h2><p>
- To vacuum a table, one must ordinarily be the table's owner or a
- superuser. However, database owners are allowed to
- vacuum all tables in their databases, except shared catalogs.
- (The restriction for shared catalogs means that a true database-wide
- <code class="command">VACUUM</code> can only be performed by a superuser.)
- <code class="command">VACUUM</code> will skip over any tables that the calling user
- does not have permission to vacuum.
- </p><p>
- <code class="command">VACUUM</code> cannot be executed inside a transaction block.
- </p><p>
- For tables with <acronym class="acronym">GIN</acronym> indexes, <code class="command">VACUUM</code> (in
- any form) also completes any pending index insertions, by moving pending
- index entries to the appropriate places in the main <acronym class="acronym">GIN</acronym> index
- 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.
- </p><p>
- We recommend that active production databases be
- vacuumed frequently (at least nightly), in order to
- remove dead rows. After adding or deleting a large number
- of rows, it might be a good idea to issue a <code class="command">VACUUM
- ANALYZE</code> command for the affected table. This will update the
- system catalogs with
- the results of all recent changes, and allow the
- <span class="productname">PostgreSQL</span> query planner to make better
- choices in planning queries.
- </p><p>
- The <code class="option">FULL</code> option is not recommended for routine use,
- but might be useful in special cases. An example is when you have deleted
- or updated most of the rows in a table and would like the table to
- physically shrink to occupy less disk space and allow faster table
- scans. <code class="command">VACUUM FULL</code> will usually shrink the table
- more than a plain <code class="command">VACUUM</code> would.
- </p><p>
- <code class="command">VACUUM</code> causes a substantial increase in I/O traffic,
- which might cause poor performance for other active sessions. Therefore,
- it is sometimes advisable to use the cost-based vacuum delay feature.
- 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.
- </p><p>
- <span class="productname">PostgreSQL</span> includes an <span class="quote">“<span class="quote">autovacuum</span>”</span>
- facility which can automate routine vacuum maintenance. For more
- information about automatic and manual vacuuming, see
- <a class="xref" href="routine-vacuuming.html" title="24.1. Routine Vacuuming">Section 24.1</a>.
- </p></div><div class="refsect1" id="id-1.9.3.183.9"><h2>Examples</h2><p>
- To clean a single table <code class="literal">onek</code>, analyze it for
- the optimizer and print a detailed vacuum activity report:
-
- </p><pre class="programlisting">
- VACUUM (VERBOSE, ANALYZE) onek;
- </pre></div><div class="refsect1" id="id-1.9.3.183.10"><h2>Compatibility</h2><p>
- There is no <code class="command">VACUUM</code> statement in the SQL standard.
- </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>
|