{"Entry":{"collection":"sql","key":"vacuum","name":"VACUUM","aliases":["vacuum-1","vacuum"],"metadata":{"aliases":["vacuum-1","vacuum"],"changed_in":["7.2","8.0","8.2","9.0","9.2","9.6","11","12","13","14","16","17","18","19"],"changes":[{"from":"6.5","purpose_changed":false,"renamed":{"from_file":"sql-vacuum-1.htm","to_file":"sql-vacuum.htm"},"sections":{"added":[],"changed":["description"],"removed":[]},"status":"changed","synopsis":null,"to":"7.0"},{"from":"7.0","purpose_changed":false,"renamed":{"from_file":"sql-vacuum.htm","to_file":"sql-vacuum.html"},"sections":{"added":[],"changed":["description"],"removed":[]},"status":"changed","synopsis":null,"to":"7.1"},{"from":"7.1","purpose_changed":true,"renamed":null,"sections":{"added":[],"changed":["description","usage"],"removed":[]},"status":"changed","synopsis":{"added":["VACUUM [ FULL ] [ FREEZE ] [ VERBOSE ] [ table ]","VACUUM [ FULL ] [ FREEZE ] [ VERBOSE ] ANALYZE [ table [ (column [, ...] ) ] ]"],"removed":["VACUUM [ VERBOSE ] [ ANALYZE ] [ table ]"]},"to":"7.2"},{"from":"7.2","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["description","usage"],"removed":[]},"status":"changed","synopsis":null,"to":"7.3"},{"from":"7.3","purpose_changed":false,"renamed":null,"sections":{"added":["parameters","outputs","notes","examples","see_also"],"changed":["description","compatibility"],"removed":["usage"]},"status":"changed","synopsis":null,"to":"7.4"},{"from":"7.4","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":[],"removed":[]},"status":"changed","synopsis":{"added":["VACUUM [ FULL | FREEZE ] [ VERBOSE ] [ table ]","VACUUM [ FULL | FREEZE ] [ VERBOSE ] ANALYZE [ table [ (column [, ...] ) ] ]"],"removed":["VACUUM [ FULL ] [ FREEZE ] [ VERBOSE ] [ table ]","VACUUM [ FULL ] [ FREEZE ] [ VERBOSE ] ANALYZE [ table [ (column [, ...] ) ] ]"]},"to":"8.0"},{"from":"8.0","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["description","notes","see_also"],"removed":[]},"status":"changed","synopsis":null,"to":"8.1"},{"from":"8.1","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["description","parameters","notes"],"removed":[]},"status":"changed","synopsis":{"added":["VACUUM [ FULL ] [ FREEZE ] [ VERBOSE ] [ table ]","VACUUM [ FULL ] [ FREEZE ] [ VERBOSE ] ANALYZE [ table [ (column [, ...] ) ] ]"],"removed":["VACUUM [ FULL | FREEZE ] [ VERBOSE ] [ table ]","VACUUM [ FULL | FREEZE ] [ VERBOSE ] ANALYZE [ table [ (column [, ...] ) ] ]"]},"to":"8.2"},{"from":"8.2","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["description","parameters","notes","see_also"],"removed":[]},"status":"changed","synopsis":null,"to":"8.3"},{"from":"8.3","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["description","parameters","notes","see_also"],"removed":[]},"status":"changed","synopsis":null,"to":"8.4"},{"from":"8.4","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["description","parameters","notes","examples","see_also"],"removed":[]},"status":"changed","synopsis":{"added":["VACUUM [ ( { FULL | FREEZE | VERBOSE | ANALYZE } [, ...] ) ] [ table [ (column [, ...] ) ] ]"],"removed":[]},"to":"9.0"},{"from":"9.0","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["description","parameters","notes"],"removed":[]},"status":"changed","synopsis":null,"to":"9.1"},{"from":"9.1","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["parameters","notes","examples","see_also"],"removed":[]},"status":"changed","synopsis":{"added":["VACUUM [ ( { FULL | FREEZE | VERBOSE | ANALYZE } [, ...] ) ] [ table_name [ (column_name [, ...] ) ] ]","VACUUM [ FULL ] [ FREEZE ] [ VERBOSE ] [ table_name ]","VACUUM [ FULL ] [ FREEZE ] [ VERBOSE ] ANALYZE [ table_name [ (column_name [, ...] ) ] ]"],"removed":["VACUUM [ ( { FULL | FREEZE | VERBOSE | ANALYZE } [, ...] ) ] [ table [ (column [, ...] ) ] ]","VACUUM [ FULL ] [ FREEZE ] [ VERBOSE ] [ table ]","VACUUM [ FULL ] [ FREEZE ] [ VERBOSE ] ANALYZE [ table [ (column [, ...] ) ] ]"]},"to":"9.2"},{"from":"9.2","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["notes"],"removed":[]},"status":"changed","synopsis":null,"to":"9.3"},{"from":"9.3","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["parameters","notes"],"removed":[]},"status":"changed","synopsis":null,"to":"9.4"},{"from":"9.4","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["notes"],"removed":[]},"status":"changed","synopsis":null,"to":"9.5"},{"from":"9.5","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["description","parameters","notes","see_also"],"removed":[]},"status":"changed","synopsis":{"added":["VACUUM [ ( { FULL | FREEZE | VERBOSE | ANALYZE | DISABLE_PAGE_SKIPPING } [, ...] ) ] [ table_name [ (column_name [, ...] ) ] ]"],"removed":[]},"to":"9.6"},{"from":"9.6","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["parameters","notes"],"removed":[]},"status":"changed","synopsis":null,"to":"10"},{"from":"10","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["description","parameters","notes"],"removed":[]},"status":"changed","synopsis":{"added":["VACUUM [ ( option [, ...] ) ] [ table_and_columns [, ...] ]","VACUUM [ FULL ] [ FREEZE ] [ VERBOSE ] [ ANALYZE ] [ table_and_columns [, ...] ]","FULL","DISABLE_PAGE_SKIPPING"],"removed":["VACUUM [ ( { FULL | FREEZE | VERBOSE | ANALYZE | DISABLE_PAGE_SKIPPING } [, ...] ) ] [ table_name [ (column_name [, ...] ) ] ]","VACUUM [ FULL ] [ FREEZE ] [ VERBOSE ] [ table_name ]","VACUUM [ FULL ] [ FREEZE ] [ VERBOSE ] ANALYZE [ table_name [ (column_name [, ...] ) ] ]"]},"to":"11"},{"from":"11","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["parameters"],"removed":[]},"status":"changed","synopsis":{"added":["FULL [ boolean ]","FREEZE [ boolean ]","VERBOSE [ boolean ]","ANALYZE [ boolean ]","DISABLE_PAGE_SKIPPING [ boolean ]","SKIP_LOCKED [ boolean ]","INDEX_CLEANUP [ boolean ]","TRUNCATE [ boolean ]"],"removed":[]},"to":"12"},{"from":"12","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["description","parameters","notes"],"removed":[]},"status":"changed","synopsis":{"added":["PARALLEL integer"],"removed":[]},"to":"13"},{"from":"13","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["parameters","notes","see_also"],"removed":[]},"status":"changed","synopsis":{"added":["INDEX_CLEANUP { AUTO | ON | OFF }","PROCESS_TOAST [ boolean ]"],"removed":[]},"to":"14"},{"from":"14","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["parameters","notes"],"removed":[]},"status":"changed","synopsis":null,"to":"15"},{"from":"15","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["parameters","notes","see_also"],"removed":[]},"status":"changed","synopsis":{"added":["PROCESS_MAIN [ boolean ]","SKIP_DATABASE_STATS [ boolean ]","ONLY_DATABASE_STATS [ boolean ]","BUFFER_USAGE_LIMIT size"],"removed":[]},"to":"16"},{"from":"16","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["description","parameters","notes","compatibility","see_also"],"removed":[]},"status":"changed","synopsis":{"added":[],"removed":["VACUUM [ ( option [, ...] ) ] [ table_and_columns [, ...] ]","VACUUM [ FULL ] [ FREEZE ] [ VERBOSE ] [ ANALYZE ] [ table_and_columns [, ...] ]"]},"to":"17"},{"from":"17","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["parameters","notes","see_also"],"removed":[]},"status":"changed","synopsis":{"added":["[ ONLY ] table_name [ * ] [ ( column_name [, ...] ) ]"],"removed":[]},"to":"18"},{"from":"18","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["description","parameters","notes","see_also"],"removed":[]},"status":"changed","synopsis":{"added":["FULL [ boolean ]"],"removed":["VACUUM [ ( option [, ...] ) ] [ table_and_columns [, ...] ]","FULL [ boolean ]"]},"to":"19"},{"from":"19","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["parameters","outputs","examples"],"removed":[]},"status":"changed","synopsis":null,"to":"20"}],"content_hash":"80c6d5247bc88711036ef9e95bb7a3d4fed27254a3f8b9757eb8d9ae6d9c1a20","editorial":{},"first_version":"6.4","group":"maintenance","imported_at":"2026-09-27T17:57:27.438234+08:00","last_version":"20","name":"VACUUM","object":"","position":15003,"present_in":["6.4","6.5","7.0","7.1","7.2","7.3","7.4","8.0","8.1","8.2","8.3","8.4","9.0","9.1","9.2","9.3","9.4","9.5","9.6","10","11","12","13","14","15","16","17","18","19","20"],"purpose":"garbage-collect and optionally analyze a database","purpose_zh":"","related":[],"slug":"vacuum","source_rev":"b7bd9cda","synopsis":"VACUUM [ ( option [, ...] ) ] [ table_and_columns [, ...] ]\n\nwhere option can be one of:\n\nFREEZE [ boolean ]\nVERBOSE [ boolean ]\nANALYZE [ boolean ]\nDISABLE_PAGE_SKIPPING [ boolean ]\nSKIP_LOCKED [ boolean ]\nINDEX_CLEANUP { AUTO | ON | OFF }\nPROCESS_MAIN [ boolean ]\nPROCESS_TOAST [ boolean ]\nTRUNCATE [ boolean ]\nPARALLEL integer\nSKIP_DATABASE_STATS [ boolean ]\nONLY_DATABASE_STATS [ boolean ]\nBUFFER_USAGE_LIMIT size\nFULL [ boolean ]\n\nand table_and_columns is:\n\n[ ONLY ] table_name [ * ] [ ( column_name [, ...] ) ]","verb":"VACUUM"}},"Definition":{"Collection":"sql","Key":"vacuum","SourceDatabase":"center","Version":"18","SourceTable":"sqlcmd","SourceKey":"vacuum","SourceRevision":"b7bd9cda","Facts":{"anchor":"SQL-VACUUM","file":"sql-vacuum.html","lang":"en","name":"VACUUM","purpose":"garbage-collect and optionally analyze a database","purpose_zh":"","related":[],"sections":[{"html":"\u003cp\u003e\u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e reclaims storage occupied by dead tuples. In normal \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e operation, tuples that are deleted or obsoleted by an update are not physically removed from their table; they remain present until a \u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e is done. Therefore it's necessary to do \u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e periodically, especially on frequently-updated tables.\u003c/p\u003e\u003cp\u003eWithout a \u003cem class=\"replaceable\"\u003e\u003ccode\u003etable_and_columns\u003c/code\u003e\u003c/em\u003e list, \u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e processes every table and materialized view in the current database that the current user has permission to vacuum. With a list, \u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e processes only those table(s).\u003c/p\u003e\u003cp\u003e\u003ccode class=\"command\"\u003eVACUUM ANALYZE\u003c/code\u003e performs a \u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e and then an \u003ccode class=\"command\"\u003eANALYZE\u003c/code\u003e for each selected table. This is a handy combination form for routine maintenance scripts. See \u003ca href=\"/docs/18/sql-analyze.html\" title=\"ANALYZE\"\u003e\u003cspan class=\"refentrytitle\"\u003eANALYZE\u003c/span\u003e\u003c/a\u003e for more details about its processing.\u003c/p\u003e\u003cp\u003ePlain \u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e (without \u003ccode class=\"literal\"\u003eFULL\u003c/code\u003e) 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. It also allows us to leverage multiple CPUs in order to process indexes. This feature is known as \u003cem class=\"firstterm\"\u003eparallel vacuum\u003c/em\u003e. To disable this feature, one can use \u003ccode class=\"literal\"\u003ePARALLEL\u003c/code\u003e option and specify parallel workers as zero. \u003ccode class=\"command\"\u003eVACUUM FULL\u003c/code\u003e 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 \u003ccode class=\"literal\"\u003eACCESS EXCLUSIVE\u003c/code\u003e lock on each table while it is being processed.\u003c/p\u003e","key":"description","title":"Description"},{"html":"\u003cdiv class=\"variablelist\"\u003e\u003cdl class=\"variablelist\"\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eFULL\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eSelects \u003cspan class=\"quote\"\u003e“\u003cspan class=\"quote\"\u003efull\u003c/span\u003e”\u003c/span\u003e 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.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eFREEZE\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eSelects aggressive \u003cspan class=\"quote\"\u003e“\u003cspan class=\"quote\"\u003efreezing\u003c/span\u003e”\u003c/span\u003e of tuples. Specifying \u003ccode class=\"literal\"\u003eFREEZE\u003c/code\u003e is equivalent to performing \u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e with the \u003ca href=\"/docs/18/runtime-config-vacuum.html#GUC-VACUUM-FREEZE-MIN-AGE\"\u003evacuum_freeze_min_age\u003c/a\u003e and \u003ca href=\"/docs/18/runtime-config-vacuum.html#GUC-VACUUM-FREEZE-TABLE-AGE\"\u003evacuum_freeze_table_age\u003c/a\u003e parameters set to zero. Aggressive freezing is always performed when the table is rewritten, so this option is redundant when \u003ccode class=\"literal\"\u003eFULL\u003c/code\u003e is specified.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eVERBOSE\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003ePrints a detailed vacuum activity report for each table at \u003ccode class=\"literal\"\u003eINFO\u003c/code\u003e level.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eANALYZE\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eUpdates statistics used by the planner to determine the most efficient way to execute a query.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eDISABLE_PAGE_SKIPPING\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eNormally, \u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e will skip pages based on the \u003ca href=\"/docs/18/routine-vacuuming.html#VACUUM-FOR-VISIBILITY-MAP\" title=\"24.1.4. Updating the Visibility Map\"\u003evisibility map\u003c/a\u003e. 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.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eSKIP_LOCKED\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eSpecifies that \u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e 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, \u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e may still block when opening the relation's indexes. Additionally, \u003ccode class=\"command\"\u003eVACUUM ANALYZE\u003c/code\u003e may still block when acquiring sample rows from partitions, table inheritance children, and some types of foreign tables. Also, while \u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e ordinarily processes all partitions of specified partitioned tables, this option will cause \u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e to skip all partitions if there is a conflicting lock on the partitioned table.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eINDEX_CLEANUP\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eNormally, \u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e will skip index vacuuming when there are very few dead tuples in the table. The cost of processing all of the table's indexes is expected to greatly exceed the benefit of removing dead index tuples when this happens. This option can be used to force \u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e to process indexes when there are more than zero dead tuples. The default is \u003ccode class=\"literal\"\u003eAUTO\u003c/code\u003e, which allows \u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e to skip index vacuuming when appropriate. If \u003ccode class=\"literal\"\u003eINDEX_CLEANUP\u003c/code\u003e is set to \u003ccode class=\"literal\"\u003eON\u003c/code\u003e, \u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e will conservatively remove all dead tuples from indexes. This may be useful for backwards compatibility with earlier releases of \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e where this was the standard behavior.\u003c/p\u003e\u003cp\u003e\u003ccode class=\"literal\"\u003eINDEX_CLEANUP\u003c/code\u003e can also be set to \u003ccode class=\"literal\"\u003eOFF\u003c/code\u003e to force \u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e to \u003cspan class=\"emphasis\"\u003e\u003cem\u003ealways\u003c/em\u003e\u003c/span\u003e skip index vacuuming, even when there are many dead tuples in the table. This may be useful when it is necessary to make \u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e run as quickly as possible to avoid imminent transaction ID wraparound (see \u003ca href=\"/docs/18/routine-vacuuming.html#VACUUM-FOR-WRAPAROUND\" title=\"24.1.5. Preventing Transaction ID Wraparound Failures\"\u003eSection 24.1.5\u003c/a\u003e). However, the wraparound failsafe mechanism controlled by \u003ca href=\"/docs/18/runtime-config-vacuum.html#GUC-VACUUM-FAILSAFE-AGE\"\u003evacuum_failsafe_age\u003c/a\u003e will generally trigger automatically to avoid transaction ID wraparound failure, and should be preferred. 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.\u003c/p\u003e\u003cp\u003eThis option has no effect for tables that have no index and is ignored if the \u003ccode class=\"literal\"\u003eFULL\u003c/code\u003e option is used. It also has no effect on the transaction ID wraparound failsafe mechanism. When triggered it will skip index vacuuming, even when \u003ccode class=\"literal\"\u003eINDEX_CLEANUP\u003c/code\u003e is set to \u003ccode class=\"literal\"\u003eON\u003c/code\u003e.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003ePROCESS_MAIN\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eSpecifies that \u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e should attempt to process the main relation. This is usually the desired behavior and is the default. Setting this option to false may be useful when it is only necessary to vacuum a relation's corresponding \u003ccode class=\"literal\"\u003eTOAST\u003c/code\u003e table.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003ePROCESS_TOAST\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eSpecifies that \u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e should attempt to process the corresponding \u003ccode class=\"literal\"\u003eTOAST\u003c/code\u003e table for each relation, if one exists. This is usually the desired behavior and is the default. Setting this option to false may be useful when it is only necessary to vacuum the main relation. This option is required when the \u003ccode class=\"literal\"\u003eFULL\u003c/code\u003e option is used.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eTRUNCATE\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eSpecifies that \u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e 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 \u003ca href=\"/docs/18/runtime-config-vacuum.html#GUC-VACUUM-TRUNCATE\"\u003evacuum_truncate\u003c/a\u003e is set to false or the \u003ccode class=\"literal\"\u003evacuum_truncate\u003c/code\u003e option has been set to false for the table to be vacuumed. Setting this option to false may be useful to avoid \u003ccode class=\"literal\"\u003eACCESS EXCLUSIVE\u003c/code\u003e lock on the table that the truncation requires. This option is ignored if the \u003ccode class=\"literal\"\u003eFULL\u003c/code\u003e option is used.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003ePARALLEL\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003ePerform index vacuum and index cleanup phases of \u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e in parallel using \u003cem class=\"replaceable\"\u003e\u003ccode\u003einteger\u003c/code\u003e\u003c/em\u003e background workers (for the details of each vacuum phase, please refer to \u003ca href=\"/docs/18/progress-reporting.html#VACUUM-PHASES\" title=\"Table 27.46. VACUUM Phases\"\u003eTable 27.46\u003c/a\u003e). The number of workers used to perform the operation is equal to the number of indexes on the relation that support parallel vacuum which is limited by the number of workers specified with \u003ccode class=\"literal\"\u003ePARALLEL\u003c/code\u003e option if any which is further limited by \u003ca href=\"/docs/18/runtime-config-resource.html#GUC-MAX-PARALLEL-MAINTENANCE-WORKERS\"\u003emax_parallel_maintenance_workers\u003c/a\u003e. An index can participate in parallel vacuum if and only if the size of the index is more than \u003ca href=\"/docs/18/runtime-config-query.html#GUC-MIN-PARALLEL-INDEX-SCAN-SIZE\"\u003emin_parallel_index_scan_size\u003c/a\u003e. Please note that it is not guaranteed that the number of parallel workers specified in \u003cem class=\"replaceable\"\u003e\u003ccode\u003einteger\u003c/code\u003e\u003c/em\u003e will be used during execution. It is possible for a vacuum to run with fewer workers than specified, or even with no workers at all. Only one worker can be used per index. So parallel workers are launched only when there are at least \u003ccode class=\"literal\"\u003e2\u003c/code\u003e indexes in the table. Workers for vacuum are launched before the start of each phase and exit at the end of the phase. These behaviors might change in a future release. This option can't be used with the \u003ccode class=\"literal\"\u003eFULL\u003c/code\u003e option.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eSKIP_DATABASE_STATS\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eSpecifies that \u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e should skip updating the database-wide statistics about oldest unfrozen XIDs. Normally \u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e will update these statistics once at the end of the command. However, this can take awhile in a database with a very large number of tables, and it will accomplish nothing unless the table that had contained the oldest unfrozen XID was among those vacuumed. Moreover, if multiple \u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e commands are issued in parallel, only one of them can update the database-wide statistics at a time. Therefore, if an application intends to issue a series of many \u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e commands, it can be helpful to set this option in all but the last such command; or set it in all the commands and separately issue \u003ccode class=\"literal\"\u003eVACUUM (ONLY_DATABASE_STATS)\u003c/code\u003e afterwards.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eONLY_DATABASE_STATS\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eSpecifies that \u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e should do nothing except update the database-wide statistics about oldest unfrozen XIDs. When this option is specified, the \u003cem class=\"replaceable\"\u003e\u003ccode\u003etable_and_columns\u003c/code\u003e\u003c/em\u003e list must be empty, and no other option may be enabled except \u003ccode class=\"literal\"\u003eVERBOSE\u003c/code\u003e.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eBUFFER_USAGE_LIMIT\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eSpecifies the \u003ca href=\"/docs/18/glossary.html#GLOSSARY-BUFFER-ACCESS-STRATEGY\"\u003e\u003c/a\u003e\u003ca href=\"/docs/18/glossary.html#GLOSSARY-BUFFER-ACCESS-STRATEGY\" title=\"Buffer Access Strategy\"\u003eBuffer Access Strategy\u003c/a\u003e ring buffer size for \u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e. This size is used to calculate the number of shared buffers which will be reused as part of this strategy. \u003ccode class=\"literal\"\u003e0\u003c/code\u003e disables use of a \u003ccode class=\"literal\"\u003eBuffer Access Strategy\u003c/code\u003e. If \u003ccode class=\"option\"\u003eANALYZE\u003c/code\u003e is also specified, the \u003ccode class=\"option\"\u003eBUFFER_USAGE_LIMIT\u003c/code\u003e value is used for both the vacuum and analyze stages. This option can't be used with the \u003ccode class=\"option\"\u003eFULL\u003c/code\u003e option except if \u003ccode class=\"option\"\u003eANALYZE\u003c/code\u003e is also specified. When this option is not specified, \u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e uses the value from \u003ca href=\"/docs/18/runtime-config-resource.html#GUC-VACUUM-BUFFER-USAGE-LIMIT\"\u003evacuum_buffer_usage_limit\u003c/a\u003e. Higher settings can allow \u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e to run more quickly, but having too large a setting may cause too many other useful pages to be evicted from shared buffers. The minimum value is \u003ccode class=\"literal\"\u003e128 kB\u003c/code\u003e and the maximum value is \u003ccode class=\"literal\"\u003e16 GB\u003c/code\u003e.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003eboolean\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eSpecifies whether the selected option should be turned on or off. You can write \u003ccode class=\"literal\"\u003eTRUE\u003c/code\u003e, \u003ccode class=\"literal\"\u003eON\u003c/code\u003e, or \u003ccode class=\"literal\"\u003e1\u003c/code\u003e to enable the option, and \u003ccode class=\"literal\"\u003eFALSE\u003c/code\u003e, \u003ccode class=\"literal\"\u003eOFF\u003c/code\u003e, or \u003ccode class=\"literal\"\u003e0\u003c/code\u003e to disable it. The \u003cem class=\"replaceable\"\u003e\u003ccode\u003eboolean\u003c/code\u003e\u003c/em\u003e value can also be omitted, in which case \u003ccode class=\"literal\"\u003eTRUE\u003c/code\u003e is assumed.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003einteger\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eSpecifies a non-negative integer value passed to the selected option.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003esize\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eSpecifies an amount of memory in kilobytes. Sizes may also be specified as a string containing the numerical size followed by any one of the following memory units: \u003ccode class=\"literal\"\u003eB\u003c/code\u003e (bytes), \u003ccode class=\"literal\"\u003ekB\u003c/code\u003e (kilobytes), \u003ccode class=\"literal\"\u003eMB\u003c/code\u003e (megabytes), \u003ccode class=\"literal\"\u003eGB\u003c/code\u003e (gigabytes), or \u003ccode class=\"literal\"\u003eTB\u003c/code\u003e (terabytes).\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003etable_name\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThe name (optionally schema-qualified) of a specific table or materialized view to vacuum. If \u003ccode class=\"literal\"\u003eONLY\u003c/code\u003e is specified before the table name, only that table is vacuumed. If \u003ccode class=\"literal\"\u003eONLY\u003c/code\u003e is not specified, the table and all its inheritance child tables or partitions (if any) are also vacuumed. Optionally, \u003ccode class=\"literal\"\u003e*\u003c/code\u003e can be specified after the table name to explicitly indicate that inheritance child tables (or partitions) are to be vacuumed.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003ecolumn_name\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThe name of a specific column to analyze. Defaults to all columns. If a column list is specified, \u003ccode class=\"literal\"\u003eANALYZE\u003c/code\u003e must also be specified.\u003c/p\u003e\u003c/dd\u003e\u003c/dl\u003e\u003c/div\u003e","key":"parameters","title":"Parameters"},{"html":"\u003cp\u003eWhen \u003ccode class=\"literal\"\u003eVERBOSE\u003c/code\u003e is specified, \u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e emits progress messages to indicate which table is currently being processed. Various statistics about the tables are printed as well.\u003c/p\u003e","key":"outputs","title":"Outputs"},{"html":"\u003cp\u003eTo vacuum a table, one must ordinarily have the \u003ccode class=\"literal\"\u003eMAINTAIN\u003c/code\u003e privilege on the table. However, database owners are allowed to vacuum all tables in their databases, except shared catalogs. \u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e will skip over any tables that the calling user does not have permission to vacuum.\u003c/p\u003e\u003cp\u003eWhile \u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e is running, the \u003ca href=\"/docs/18/runtime-config-client.html#GUC-SEARCH-PATH\"\u003esearch_path\u003c/a\u003e is temporarily changed to \u003ccode class=\"literal\"\u003epg_catalog, pg_temp\u003c/code\u003e.\u003c/p\u003e\u003cp\u003e\u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e cannot be executed inside a transaction block.\u003c/p\u003e\u003cp\u003eFor tables with \u003cacronym\u003eGIN\u003c/acronym\u003e indexes, \u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e (in any form) also completes any pending index insertions, by moving pending index entries to the appropriate places in the main \u003cacronym\u003eGIN\u003c/acronym\u003e index structure. See \u003ca href=\"/docs/18/gin.html#GIN-FAST-UPDATE\" title=\"65.4.4.1. GIN Fast Update Technique\"\u003eSection 65.4.4.1\u003c/a\u003e for details.\u003c/p\u003e\u003cp\u003eWe recommend that all databases be vacuumed regularly in order to remove dead rows. \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e includes an \u003cspan class=\"quote\"\u003e“\u003cspan class=\"quote\"\u003eautovacuum\u003c/span\u003e”\u003c/span\u003e facility which can automate routine vacuum maintenance. For more information about automatic and manual vacuuming, see \u003ca href=\"/docs/18/routine-vacuuming.html\" title=\"24.1. Routine Vacuuming\"\u003eSection 24.1\u003c/a\u003e.\u003c/p\u003e\u003cp\u003eThe \u003ccode class=\"option\"\u003eFULL\u003c/code\u003e 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. \u003ccode class=\"command\"\u003eVACUUM FULL\u003c/code\u003e will usually shrink the table more than a plain \u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e would.\u003c/p\u003e\u003cp\u003eThe \u003ccode class=\"option\"\u003ePARALLEL\u003c/code\u003e option is used only for vacuum purposes. If this option is specified with the \u003ccode class=\"option\"\u003eANALYZE\u003c/code\u003e option, it does not affect \u003ccode class=\"option\"\u003eANALYZE\u003c/code\u003e.\u003c/p\u003e\u003cp\u003e\u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e 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. For parallel vacuum, each worker sleeps in proportion to the work done by that worker. See \u003ca href=\"/docs/18/runtime-config-vacuum.html#RUNTIME-CONFIG-RESOURCE-VACUUM-COST\" title=\"19.10.2. Cost-based Vacuum Delay\"\u003eSection 19.10.2\u003c/a\u003e for details.\u003c/p\u003e\u003cp\u003eEach backend running \u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e without the \u003ccode class=\"literal\"\u003eFULL\u003c/code\u003e option will report its progress in the \u003ccode class=\"structname\"\u003epg_stat_progress_vacuum\u003c/code\u003e view. Backends running \u003ccode class=\"command\"\u003eVACUUM FULL\u003c/code\u003e will instead report their progress in the \u003ccode class=\"structname\"\u003epg_stat_progress_cluster\u003c/code\u003e view. See \u003ca href=\"/docs/18/progress-reporting.html#VACUUM-PROGRESS-REPORTING\" title=\"27.4.5. VACUUM Progress Reporting\"\u003eSection 27.4.5\u003c/a\u003e and \u003ca href=\"/docs/18/progress-reporting.html#CLUSTER-PROGRESS-REPORTING\" title=\"27.4.2. CLUSTER Progress Reporting\"\u003eSection 27.4.2\u003c/a\u003e for details.\u003c/p\u003e","key":"notes","title":"Notes"},{"html":"\u003cp\u003eTo clean a single table \u003ccode class=\"literal\"\u003eonek\u003c/code\u003e, analyze it for the optimizer and print a detailed vacuum activity report:\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eVACUUM (VERBOSE, ANALYZE) onek;\n\u003c/pre\u003e","key":"examples","title":"Examples"},{"html":"\u003cp\u003eThere is no \u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e statement in the SQL standard.\u003c/p\u003e\u003cp\u003eThe following syntax was used before \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e version 9.0 and is still supported:\u003c/p\u003e\u003cpre class=\"synopsis\"\u003eVACUUM [ FULL ] [ FREEZE ] [ VERBOSE ] [ ANALYZE ] [ \u003cem class=\"replaceable\"\u003e\u003ccode\u003etable_and_columns\u003c/code\u003e\u003c/em\u003e [, ...] ]\n\u003c/pre\u003e\u003cp\u003eNote that in this syntax, the options must be specified in exactly the order shown.\u003c/p\u003e","key":"compatibility","title":"Compatibility"},{"html":"\u003cspan class=\"simplelist\"\u003e\u003ca href=\"/docs/18/app-vacuumdb.html\" title=\"vacuumdb\"\u003e\u003cspan class=\"refentrytitle\"\u003e\u003cspan class=\"application\"\u003evacuumdb\u003c/span\u003e\u003c/span\u003e\u003c/a\u003e, \u003ca href=\"/docs/18/runtime-config-vacuum.html#RUNTIME-CONFIG-RESOURCE-VACUUM-COST\" title=\"19.10.2. Cost-based Vacuum Delay\"\u003eSection 19.10.2\u003c/a\u003e, \u003ca href=\"/docs/18/routine-vacuuming.html#AUTOVACUUM\" title=\"24.1.6. The Autovacuum Daemon\"\u003eSection 24.1.6\u003c/a\u003e, \u003ca href=\"/docs/18/progress-reporting.html#VACUUM-PROGRESS-REPORTING\" title=\"27.4.5. VACUUM Progress Reporting\"\u003eSection 27.4.5\u003c/a\u003e, \u003ca href=\"/docs/18/progress-reporting.html#CLUSTER-PROGRESS-REPORTING\" title=\"27.4.2. CLUSTER Progress Reporting\"\u003eSection 27.4.2\u003c/a\u003e\u003c/span\u003e","key":"see_also","title":"See Also"}],"sections_same_as":"","slug":"18","synopsis_html":"VACUUM [ ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003eoption\u003c/code\u003e\u003c/em\u003e [, ...] ) ] [ \u003cem class=\"replaceable\"\u003e\u003ccode\u003etable_and_columns\u003c/code\u003e\u003c/em\u003e [, ...] ]\n\n\u003cspan class=\"phrase\"\u003ewhere \u003cem class=\"replaceable\"\u003e\u003ccode\u003eoption\u003c/code\u003e\u003c/em\u003e can be one of:\u003c/span\u003e\n\n    FULL [ \u003cem class=\"replaceable\"\u003e\u003ccode\u003eboolean\u003c/code\u003e\u003c/em\u003e ]\n    FREEZE [ \u003cem class=\"replaceable\"\u003e\u003ccode\u003eboolean\u003c/code\u003e\u003c/em\u003e ]\n    VERBOSE [ \u003cem class=\"replaceable\"\u003e\u003ccode\u003eboolean\u003c/code\u003e\u003c/em\u003e ]\n    ANALYZE [ \u003cem class=\"replaceable\"\u003e\u003ccode\u003eboolean\u003c/code\u003e\u003c/em\u003e ]\n    DISABLE_PAGE_SKIPPING [ \u003cem class=\"replaceable\"\u003e\u003ccode\u003eboolean\u003c/code\u003e\u003c/em\u003e ]\n    SKIP_LOCKED [ \u003cem class=\"replaceable\"\u003e\u003ccode\u003eboolean\u003c/code\u003e\u003c/em\u003e ]\n    INDEX_CLEANUP { AUTO | ON | OFF }\n    PROCESS_MAIN [ \u003cem class=\"replaceable\"\u003e\u003ccode\u003eboolean\u003c/code\u003e\u003c/em\u003e ]\n    PROCESS_TOAST [ \u003cem class=\"replaceable\"\u003e\u003ccode\u003eboolean\u003c/code\u003e\u003c/em\u003e ]\n    TRUNCATE [ \u003cem class=\"replaceable\"\u003e\u003ccode\u003eboolean\u003c/code\u003e\u003c/em\u003e ]\n    PARALLEL \u003cem class=\"replaceable\"\u003e\u003ccode\u003einteger\u003c/code\u003e\u003c/em\u003e\n    SKIP_DATABASE_STATS [ \u003cem class=\"replaceable\"\u003e\u003ccode\u003eboolean\u003c/code\u003e\u003c/em\u003e ]\n    ONLY_DATABASE_STATS [ \u003cem class=\"replaceable\"\u003e\u003ccode\u003eboolean\u003c/code\u003e\u003c/em\u003e ]\n    BUFFER_USAGE_LIMIT \u003cem class=\"replaceable\"\u003e\u003ccode\u003esize\u003c/code\u003e\u003c/em\u003e\n\n\u003cspan class=\"phrase\"\u003eand \u003cem class=\"replaceable\"\u003e\u003ccode\u003etable_and_columns\u003c/code\u003e\u003c/em\u003e is:\u003c/span\u003e\n\n    [ ONLY ] \u003cem class=\"replaceable\"\u003e\u003ccode\u003etable_name\u003c/code\u003e\u003c/em\u003e [ * ] [ ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecolumn_name\u003c/code\u003e\u003c/em\u003e [, ...] ) ]","synopsis_text":"VACUUM [ ( option [, ...] ) ] [ table_and_columns [, ...] ]\n\nwhere option can be one of:\n\nFULL [ boolean ]\nFREEZE [ boolean ]\nVERBOSE [ boolean ]\nANALYZE [ boolean ]\nDISABLE_PAGE_SKIPPING [ boolean ]\nSKIP_LOCKED [ boolean ]\nINDEX_CLEANUP { AUTO | ON | OFF }\nPROCESS_MAIN [ boolean ]\nPROCESS_TOAST [ boolean ]\nTRUNCATE [ boolean ]\nPARALLEL integer\nSKIP_DATABASE_STATS [ boolean ]\nONLY_DATABASE_STATS [ boolean ]\nBUFFER_USAGE_LIMIT size\nand table_and_columns is:\n\n[ ONLY ] table_name [ * ] [ ( column_name [, ...] ) ]"},"ManualEvidence":{},"MeasuredEvidence":{}},"Text":{"Collection":"sql","Key":"vacuum","SourceDatabase":"pgweb","Version":"18","Locale":"zh-Hans","Title":"VACUUM","Summary":"垃圾收集并按需分析数据库","BodyHTML":"\u003cpre\u003eVACUUM [ ( option [, ...] ) ] [ table_and_columns [, ...] ]\n\n其中option可以是下列之一：\n\nFULL [ boolean ]\nFREEZE [ boolean ]\nVERBOSE [ boolean ]\nANALYZE [ boolean ]\nDISABLE_PAGE_SKIPPING [ boolean ]\nSKIP_LOCKED [ boolean ]\nINDEX_CLEANUP { AUTO | ON | OFF }\nPROCESS_MAIN [ boolean ]\nPROCESS_TOAST [ boolean ]\nTRUNCATE [ boolean ]\nPARALLEL integer\nSKIP_DATABASE_STATS [ boolean ]\nONLY_DATABASE_STATS [ boolean ]\nBUFFER_USAGE_LIMIT size\n其中table_and_columns是：\n\n[ ONLY ] table_name [ * ] [ ( column_name [, ...] ) ]\u003c/pre\u003e\u003csection\u003e\u003ch2\u003e描述\u003c/h2\u003e\u003cp\u003e\u003ccode\u003eVACUUM\u003c/code\u003e回收死元组占用的存储空间。在正常的 \u003cspan\u003ePostgreSQL\u003c/span\u003e运行中，被删除或因更新而过时的元组并不会从其表中物理移除；它们会一直保留，直到执行 \u003ccode\u003eVACUUM\u003c/code\u003e。因此有必要定期执行 \u003ccode\u003eVACUUM\u003c/code\u003e，尤其是对频繁更新的表。\u003c/p\u003e\u003cp\u003e如果没有给出\u003cem\u003e\u003ccode\u003etable_and_columns\u003c/code\u003e\u003c/em\u003e 列表，\u003ccode\u003eVACUUM\u003c/code\u003e会处理当前数据库中当前用户有权清理的每个表和物化视图。如果给出了列表，\u003ccode\u003eVACUUM\u003c/code\u003e则只处理其中列出的表。\u003c/p\u003e\u003cp\u003e\u003ccode\u003eVACUUM ANALYZE\u003c/code\u003e会对每个选定表先执行 \u003ccode\u003eVACUUM\u003c/code\u003e，再执行\u003ccode\u003eANALYZE\u003c/code\u003e。这种便捷的组合形式很适合例行维护脚本使用。关于其处理细节，参见\u003ca href=\"/docs/18/sql-analyze.html\" title=\"ANALYZE\" rel=\"nofollow\"\u003e\u003cspan\u003eANALYZE\u003c/span\u003e\u003c/a\u003e。\u003c/p\u003e\u003cp\u003e普通的\u003ccode\u003eVACUUM\u003c/code\u003e（不带\u003ccode\u003eFULL\u003c/code\u003e）只是回收空间并使其可被重用。这种形式的命令可以与表的正常读写并行运行，因为它不会获得独占锁。不过，在大多数情况下，额外空间不会返还给操作系统；它只是保留在同一张表内供再次使用。它还允许利用多个 CPU 来处理索引，这项功能称为\u003cem\u003e并行清理\u003c/em\u003e。如需禁用该功能，可以使用\u003ccode\u003ePARALLEL\u003c/code\u003e选项并将并行工作进程数指定为零。\u003ccode\u003eVACUUM FULL\u003c/code\u003e会把表的全部内容重写到一个没有额外空闲空间的新磁盘文件中，从而让未使用的空间能够返还给操作系统。这种形式要慢得多，并且在处理每个表时都需要 \u003ccode\u003eACCESS EXCLUSIVE\u003c/code\u003e锁。\u003c/p\u003e\u003c/section\u003e\u003csection\u003e\u003ch2\u003e参数\u003c/h2\u003e\u003cdiv\u003e\u003cdl\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eFULL\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e选择\u003cspan\u003e“\u003cspan\u003e完全\u003c/span\u003e”\u003c/span\u003e清理，它可以回收更多空间，但耗时更长，并且会独占锁定该表。这种方法还需要额外的磁盘空间，因为它会写出该表的一个新副本，并且在操作完成之前不会释放旧副本。通常，只有当需要从表内回收大量空间时才应使用这种方法。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eFREEZE\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e选择激进的元组\u003cspan\u003e“\u003cspan\u003e冻结\u003c/span\u003e”\u003c/span\u003e。指定\u003ccode\u003eFREEZE\u003c/code\u003e 等价于执行一个将\u003ca href=\"/docs/18/runtime-config-vacuum.html#GUC-VACUUM-FREEZE-MIN-AGE\" rel=\"nofollow\"\u003evacuum_freeze_min_age\u003c/a\u003e和\u003ca href=\"/docs/18/runtime-config-vacuum.html#GUC-VACUUM-FREEZE-TABLE-AGE\" rel=\"nofollow\"\u003evacuum_freeze_table_age\u003c/a\u003e参数设为零的 \u003ccode\u003eVACUUM\u003c/code\u003e。在表被重写时总会执行激进冻结，因此指定了 \u003ccode\u003eFULL\u003c/code\u003e时，这个选项就是多余的。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eVERBOSE\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e为每个表以\u003ccode\u003eINFO\u003c/code\u003e级别输出详细的清理活动报告。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eANALYZE\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e更新规划器用来确定查询最高效执行方式的统计信息。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eDISABLE_PAGE_SKIPPING\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e通常，\u003ccode\u003eVACUUM\u003c/code\u003e会根据\u003ca href=\"/docs/18/routine-vacuuming.html#VACUUM-FOR-VISIBILITY-MAP\" rel=\"nofollow\"\u003e可见性映射\u003c/a\u003e跳过某些页面。已知其中所有元组都已冻结的页面总是可以跳过，而已知其中所有元组都对所有事务可见的页面，也可以跳过，除非正在执行激进清理。此外，除非正在执行激进清理，为了避免等待其他会话结束对页面的使用，也可能会跳过某些页面。此选项会禁用所有跳页行为，只应在怀疑可见性映射内容存在问题时使用；而这种情况应当只会在硬件或软件问题导致数据库损坏时发生。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eSKIP_LOCKED\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e指定\u003ccode\u003eVACUUM\u003c/code\u003e在开始处理某个关系时，不要等待任何冲突锁被释放：如果某个关系无法在无需等待的情况下立即获得锁，就跳过该关系。请注意，即使使用了此选项，\u003ccode\u003eVACUUM\u003c/code\u003e在打开该关系的索引时仍可能发生阻塞。此外，\u003ccode\u003eVACUUM ANALYZE\u003c/code\u003e在从分区、继承子表和某些类型的外部表获取样本行时，仍然可能发生阻塞。还有，虽然\u003ccode\u003eVACUUM\u003c/code\u003e通常会处理指定分区表的全部分区，但如果该分区表上存在冲突锁，此选项会导致\u003ccode\u003eVACUUM\u003c/code\u003e跳过所有分区。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eINDEX_CLEANUP\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e通常，当表中的死元组很少时，\u003ccode\u003eVACUUM\u003c/code\u003e会跳过索引清理。在这种情况下，处理该表全部索引的代价预计会远高于删除死索引元组带来的收益。当表中存在多于零个死元组时，此选项可用于强制\u003ccode\u003eVACUUM\u003c/code\u003e处理索引。默认值是\u003ccode\u003eAUTO\u003c/code\u003e，这允许\u003ccode\u003eVACUUM\u003c/code\u003e在适当时跳过索引清理。如果\u003ccode\u003eINDEX_CLEANUP\u003c/code\u003e设为\u003ccode\u003eON\u003c/code\u003e，\u003ccode\u003eVACUUM\u003c/code\u003e将保守地从索引中移除所有死元组。这对于兼容 \u003cspan\u003ePostgreSQL\u003c/span\u003e早期版本可能很有用，因为在那些版本中这是标准行为。\u003c/p\u003e\u003cp\u003e\u003ccode\u003eINDEX_CLEANUP\u003c/code\u003e也可以设为\u003ccode\u003eOFF\u003c/code\u003e，以强制\u003ccode\u003eVACUUM\u003c/code\u003e\u003cspan\u003e\u003cem\u003e总是\u003c/em\u003e\u003c/span\u003e跳过索引清理，即使表中有很多死元组也是如此。当需要让\u003ccode\u003eVACUUM\u003c/code\u003e尽可能快地运行，以避免临近的事务 ID 回卷时，这可能会有用（参见\u003ca href=\"/docs/18/routine-vacuuming.html#VACUUM-FOR-WRAPAROUND\" rel=\"nofollow\"\u003e第 24.1.5 节\u003c/a\u003e）。不过，由\u003ca href=\"/docs/18/runtime-config-vacuum.html#GUC-VACUUM-FAILSAFE-AGE\" rel=\"nofollow\"\u003evacuum_failsafe_age\u003c/a\u003e控制的回卷失效保护机制通常会自动触发，以避免事务 ID 回卷失败，因此一般应优先依赖该机制。如果不定期执行索引清理，性能可能会受到影响，因为随着表被修改，索引会积累死元组，而表本身也会积累在索引清理完成前无法移除的死行指针。\u003c/p\u003e\u003cp\u003e对没有索引的表，此选项没有效果；如果使用了\u003ccode\u003eFULL\u003c/code\u003e选项，它也会被忽略。它对事务 ID 回卷失效保护机制同样没有影响。当该机制被触发时，即使 \u003ccode\u003eINDEX_CLEANUP\u003c/code\u003e设为\u003ccode\u003eON\u003c/code\u003e，也会跳过索引清理。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003ePROCESS_MAIN\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e指定\u003ccode\u003eVACUUM\u003c/code\u003e应尝试处理主关系。这通常是期望的行为，也是默认行为。当只需要清理某个关系对应的\u003ccode\u003eTOAST\u003c/code\u003e表时，将此选项设为 false 可能会有用。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003ePROCESS_TOAST\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e指定\u003ccode\u003eVACUUM\u003c/code\u003e应尝试处理每个关系对应的\u003ccode\u003eTOAST\u003c/code\u003e表（如果存在）。这通常是期望的行为，也是默认行为。当只需要清理主关系时，将此选项设为 false 可能会有用。使用\u003ccode\u003eFULL\u003c/code\u003e选项时必须启用此选项。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eTRUNCATE\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e指定\u003ccode\u003eVACUUM\u003c/code\u003e应尝试截去表末尾的空页，使这些被截断页面占用的磁盘空间能够返还给操作系统。通常这是期望的行为，也是默认行为，除非\u003ca href=\"/docs/18/runtime-config-vacuum.html#GUC-VACUUM-TRUNCATE\" rel=\"nofollow\"\u003evacuum_truncate\u003c/a\u003e被设为 false，或者待清理表的 \u003ccode\u003evacuum_truncate\u003c/code\u003e选项被设为 false。将此选项设为 false 有助于避免截断操作所需的表级\u003ccode\u003eACCESS EXCLUSIVE\u003c/code\u003e锁。使用\u003ccode\u003eFULL\u003c/code\u003e选项时，此选项会被忽略。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003ePARALLEL\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e并行执行\u003ccode\u003eVACUUM\u003c/code\u003e的索引清理和索引收尾清理阶段，使用\u003cem\u003e\u003ccode\u003einteger\u003c/code\u003e\u003c/em\u003e个后台工作进程（每个清理阶段的细节参见\u003ca href=\"/docs/18/progress-reporting.html#VACUUM-PHASES\" rel=\"nofollow\"\u003e表 27.46\u003c/a\u003e）。实际用于执行操作的工作进程数量，等于该关系上支持并行清理的索引数量，如果指定了\u003ccode\u003ePARALLEL\u003c/code\u003e选项，则该数量受其中指定的工作进程数限制，并进一步受\u003ca href=\"/docs/18/runtime-config-resource.html#GUC-MAX-PARALLEL-MAINTENANCE-WORKERS\" rel=\"nofollow\"\u003emax_parallel_maintenance_workers\u003c/a\u003e限制。当且仅当索引大小大于\u003ca href=\"/docs/18/runtime-config-query.html#GUC-MIN-PARALLEL-INDEX-SCAN-SIZE\" rel=\"nofollow\"\u003emin_parallel_index_scan_size\u003c/a\u003e时，该索引才能参与并行清理。请注意，执行期间不保证一定会使用\u003cem\u003e\u003ccode\u003einteger\u003c/code\u003e\u003c/em\u003e中指定的并行工作进程数。一次清理可能使用少于指定数量的工作进程，甚至完全不使用工作进程。每个索引最多只能使用一个工作进程，因此只有当表中至少有\u003ccode\u003e2\u003c/code\u003e个索引时才会启动并行工作进程。清理工作进程会在每个阶段开始前启动，并在该阶段结束时退出。这些行为在未来版本中可能会发生变化。此选项不能与\u003ccode\u003eFULL\u003c/code\u003e选项一起使用。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eSKIP_DATABASE_STATS\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e指定\u003ccode\u003eVACUUM\u003c/code\u003e应跳过更新数据库范围内关于最老未冻结 XID 的统计信息。通常，\u003ccode\u003eVACUUM\u003c/code\u003e会在命令结束时更新这些统计信息。不过，在拥有大量表的数据库中，这可能需要一段时间，而且只有当包含最老未冻结 XID 的表也在本次清理范围内时，这么做才有意义。此外，如果多个\u003ccode\u003eVACUUM\u003c/code\u003e命令并行发出，那么同一时间只能有一个命令更新数据库范围统计信息。因此，如果应用程序打算连续发出一系列\u003ccode\u003eVACUUM\u003c/code\u003e命令，那么除了最后一条命令之外，其余命令都可以设置此选项；或者在所有命令上都设置它，然后再单独执行 \u003ccode\u003eVACUUM (ONLY_DATABASE_STATS)\u003c/code\u003e。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eONLY_DATABASE_STATS\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e指定\u003ccode\u003eVACUUM\u003c/code\u003e除了更新数据库范围内关于最老未冻结 XID 的统计信息外，不执行任何其他操作。指定该选项时，\u003cem\u003e\u003ccode\u003etable_and_columns\u003c/code\u003e\u003c/em\u003e列表必须为空，并且除了\u003ccode\u003eVERBOSE\u003c/code\u003e之外不能启用其他任何选项。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eBUFFER_USAGE_LIMIT\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e指定\u003ccode\u003eVACUUM\u003c/code\u003e所使用的\u003ca href=\"/docs/18/glossary.html#GLOSSARY-BUFFER-ACCESS-STRATEGY\" rel=\"nofollow\"\u003e\u003c/a\u003e\u003ca href=\"/docs/18/glossary.html#GLOSSARY-BUFFER-ACCESS-STRATEGY\" title=\"缓冲区访问策略\" rel=\"nofollow\"\u003e缓冲区访问策略（Buffer Access Strategy）\u003c/a\u003e环形缓冲区大小。该大小用于计算在此策略中会被重用的共享缓冲区数量。\u003ccode\u003e0\u003c/code\u003e表示禁用\u003ccode\u003e缓冲区访问策略\u003c/code\u003e。如果同时指定了\u003ccode\u003eANALYZE\u003c/code\u003e，那么\u003ccode\u003eBUFFER_USAGE_LIMIT\u003c/code\u003e的值会同时用于清理阶段和分析阶段。除非也指定了\u003ccode\u003eANALYZE\u003c/code\u003e，否则该选项不能与\u003ccode\u003eFULL\u003c/code\u003e一起使用。如果未指定该选项，\u003ccode\u003eVACUUM\u003c/code\u003e会使用\u003ca href=\"/docs/18/runtime-config-resource.html#GUC-VACUUM-BUFFER-USAGE-LIMIT\" rel=\"nofollow\"\u003evacuum_buffer_usage_limit\u003c/a\u003e中的值。更高的设置可以让 \u003ccode\u003eVACUUM\u003c/code\u003e运行得更快，但设置过大可能会把过多其他有用页面从共享缓冲区中逐出。最小值是\u003ccode\u003e128 kB\u003c/code\u003e，最大值是\u003ccode\u003e16 GB\u003c/code\u003e。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003cem\u003e\u003ccode\u003eboolean\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e指定所选选项是否开启。可以写\u003ccode\u003eTRUE\u003c/code\u003e、\u003ccode\u003eON\u003c/code\u003e或\u003ccode\u003e1\u003c/code\u003e来启用选项，写\u003ccode\u003eFALSE\u003c/code\u003e、\u003ccode\u003eOFF\u003c/code\u003e或\u003ccode\u003e0\u003c/code\u003e来禁用它。也可以省略\u003cem\u003e\u003ccode\u003eboolean\u003c/code\u003e\u003c/em\u003e值，此时假定为\u003ccode\u003eTRUE\u003c/code\u003e。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003cem\u003e\u003ccode\u003einteger\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e指定传递给所选选项的非负整数值。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003cem\u003e\u003ccode\u003esize\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e指定以千字节为单位的内存大小。也可以把大小写成一个字符串，即数值后跟下列任一种内存单位：\u003ccode\u003eB\u003c/code\u003e（字节）、\u003ccode\u003ekB\u003c/code\u003e（千字节）、\u003ccode\u003eMB\u003c/code\u003e（兆字节）、\u003ccode\u003eGB\u003c/code\u003e（吉字节）或\u003ccode\u003eTB\u003c/code\u003e（太字节）。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003cem\u003e\u003ccode\u003etable_name\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e要清理的特定表或物化视图的名称（可选地带模式限定）。如果在表名前指定\u003ccode\u003eONLY\u003c/code\u003e，则只清理该表。如果未指定\u003ccode\u003eONLY\u003c/code\u003e，则还会清理该表及其所有继承子表或分区（如果有）。也可以在表名后显式指定\u003ccode\u003e*\u003c/code\u003e，以明确表示要清理继承子表（或分区）。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003cem\u003e\u003ccode\u003ecolumn_name\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e要分析的特定列名。默认会分析所有列。如果指定了列列表，则也必须指定\u003ccode\u003eANALYZE\u003c/code\u003e。\u003c/p\u003e\u003c/dd\u003e\u003c/dl\u003e\u003c/div\u003e\u003c/section\u003e\u003csection\u003e\u003ch2\u003e输出\u003c/h2\u003e\u003cp\u003e指定\u003ccode\u003eVERBOSE\u003c/code\u003e时，\u003ccode\u003eVACUUM\u003c/code\u003e会输出进度消息，指示当前正在处理哪个表，同时还会打印这些表的各种统计信息。\u003c/p\u003e\u003c/section\u003e\u003csection\u003e\u003ch2\u003e注解\u003c/h2\u003e\u003cp\u003e要清理一个表，通常必须拥有该表上的\u003ccode\u003eMAINTAIN\u003c/code\u003e权限。不过，数据库拥有者可以清理其数据库中的所有表，但共享系统目录除外。\u003ccode\u003eVACUUM\u003c/code\u003e会跳过调用用户无权清理的任何表。\u003c/p\u003e\u003cp\u003e当\u003ccode\u003eVACUUM\u003c/code\u003e运行时，\u003ca href=\"/docs/18/runtime-config-client.html#GUC-SEARCH-PATH\" rel=\"nofollow\"\u003esearch_path\u003c/a\u003e会被临时改为 \u003ccode\u003epg_catalog, pg_temp\u003c/code\u003e。\u003c/p\u003e\u003cp\u003e\u003ccode\u003eVACUUM\u003c/code\u003e不能在一个事务块内被执行。\u003c/p\u003e\u003cp\u003e对于带有\u003cacronym\u003eGIN\u003c/acronym\u003e索引的表，\u003ccode\u003eVACUUM\u003c/code\u003e（任何形式）还会通过把待处理的索引项移动到主 \u003cacronym\u003eGIN\u003c/acronym\u003e索引结构中的适当位置，来完成所有挂起的索引插入。详见\u003ca href=\"/docs/18/gin.html#GIN-FAST-UPDATE\" rel=\"nofollow\"\u003e第 65.4.4.1 节\u003c/a\u003e。\u003c/p\u003e\u003cp\u003e我们建议定期对所有数据库执行清理，以移除死行。\u003cspan\u003ePostgreSQL\u003c/span\u003e提供了一个\u003cspan\u003e“\u003cspan\u003e自动清理（autovacuum）\u003c/span\u003e”\u003c/span\u003e机制，可以自动执行常规清理维护。有关自动与手动清理的更多信息，参见\u003ca href=\"/docs/18/routine-vacuuming.html\" rel=\"nofollow\"\u003e第 24.1 节\u003c/a\u003e。\u003c/p\u003e\u003cp\u003e\u003ccode\u003eFULL\u003c/code\u003e选项不建议在日常场景中使用，但在某些特殊情况下可能很有用。例如，当你删除或更新了表中的绝大多数行，并希望该表在物理上收缩以占用更少磁盘空间、让表扫描更快时，这个选项就比较合适。\u003ccode\u003eVACUUM FULL\u003c/code\u003e通常会比普通 \u003ccode\u003eVACUUM\u003c/code\u003e更大幅度地收缩表。\u003c/p\u003e\u003cp\u003e\u003ccode\u003ePARALLEL\u003c/code\u003e选项只用于清理。如果此选项与\u003ccode\u003eANALYZE\u003c/code\u003e选项一起指定，它不会影响\u003ccode\u003eANALYZE\u003c/code\u003e。\u003c/p\u003e\u003cp\u003e\u003ccode\u003eVACUUM\u003c/code\u003e会显著增加 I/O 流量，这可能导致其他活动会话性能变差。因此，有时建议使用基于代价的清理延迟特性。对于并行清理，每个工作进程的睡眠时长都与该工作进程完成的工作量成比例。详见\u003ca href=\"/docs/18/runtime-config-vacuum.html#RUNTIME-CONFIG-RESOURCE-VACUUM-COST\" rel=\"nofollow\"\u003e第 19.10.2 节\u003c/a\u003e。\u003c/p\u003e\u003cp\u003e每个运行不带\u003ccode\u003eFULL\u003c/code\u003e选项的\u003ccode\u003eVACUUM\u003c/code\u003e的后端，都会在 \u003ccode\u003epg_stat_progress_vacuum\u003c/code\u003e视图中报告其进度。运行\u003ccode\u003eVACUUM FULL\u003c/code\u003e的后端则会改为在 \u003ccode\u003epg_stat_progress_cluster\u003c/code\u003e视图中报告其进度。详见\u003ca href=\"/docs/18/progress-reporting.html#VACUUM-PROGRESS-REPORTING\" rel=\"nofollow\"\u003e第 27.4.5 节\u003c/a\u003e和\u003ca href=\"/docs/18/progress-reporting.html#CLUSTER-PROGRESS-REPORTING\" rel=\"nofollow\"\u003e第 27.4.2 节\u003c/a\u003e。\u003c/p\u003e\u003c/section\u003e\u003csection\u003e\u003ch2\u003e示例\u003c/h2\u003e\u003cp\u003e清理单个表\u003ccode\u003eonek\u003c/code\u003e，对其进行分析以供优化器使用，并打印详细的清理活动报告：\u003c/p\u003e\u003cpre\u003eVACUUM (VERBOSE, ANALYZE) onek;\n\u003c/pre\u003e\u003c/section\u003e\u003csection\u003e\u003ch2\u003e兼容性\u003c/h2\u003e\u003cp\u003e在SQL标准中没有\u003ccode\u003eVACUUM\u003c/code\u003e语句。\u003c/p\u003e\u003cp\u003e下列语法在\u003cspan\u003ePostgreSQL\u003c/span\u003e 9.0版本之前使用，并且目前仍受支持：\u003c/p\u003e\u003cpre\u003eVACUUM [ FULL ] [ FREEZE ] [ VERBOSE ] [ ANALYZE ] [ \u003cem\u003e\u003ccode\u003etable_and_columns\u003c/code\u003e\u003c/em\u003e [, ...] ]\n\u003c/pre\u003e\u003cp\u003e请注意，在这种语法中，选项必须严格按所示顺序指定。\u003c/p\u003e\u003c/section\u003e\u003csection\u003e\u003ch2\u003e另见\u003c/h2\u003e\u003cspan\u003e\u003ca href=\"/docs/18/app-vacuumdb.html\" title=\"vacuumdb\" rel=\"nofollow\"\u003e\u003cspan\u003e\u003cspan\u003evacuumdb\u003c/span\u003e\u003c/span\u003e\u003c/a\u003e, \u003ca href=\"/docs/18/runtime-config-vacuum.html#RUNTIME-CONFIG-RESOURCE-VACUUM-COST\" rel=\"nofollow\"\u003e第 19.10.2 节\u003c/a\u003e, \u003ca href=\"/docs/18/routine-vacuuming.html#AUTOVACUUM\" rel=\"nofollow\"\u003e第 24.1.6 节\u003c/a\u003e, \u003ca href=\"/docs/18/progress-reporting.html#VACUUM-PROGRESS-REPORTING\" rel=\"nofollow\"\u003e第 27.4.5 节\u003c/a\u003e, \u003ca href=\"/docs/18/progress-reporting.html#CLUSTER-PROGRESS-REPORTING\" rel=\"nofollow\"\u003e第 27.4.2 节\u003c/a\u003e\u003c/span\u003e\u003c/section\u003e","SourceRevision":"ca936764","ContentHash":"5439b81a39734f483c8bb314e183d4a4a3c792ef9f59c33d9461a10a5cade9db","Payload":{"purpose_zh":"垃圾收集并按需分析数据库","sections":[{"html":"\u003cp\u003e\u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e回收死元组占用的存储空间。在正常的 \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e运行中，被删除或因更新而过时的元组并不会从其表中物理移除；它们会一直保留，直到执行 \u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e。因此有必要定期执行 \u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e，尤其是对频繁更新的表。\u003c/p\u003e\u003cp\u003e如果没有给出\u003cem class=\"replaceable\"\u003e\u003ccode\u003etable_and_columns\u003c/code\u003e\u003c/em\u003e 列表，\u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e会处理当前数据库中当前用户有权清理的每个表和物化视图。如果给出了列表，\u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e则只处理其中列出的表。\u003c/p\u003e\u003cp\u003e\u003ccode class=\"command\"\u003eVACUUM ANALYZE\u003c/code\u003e会对每个选定表先执行 \u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e，再执行\u003ccode class=\"command\"\u003eANALYZE\u003c/code\u003e。这种便捷的组合形式很适合例行维护脚本使用。关于其处理细节，参见\u003ca href=\"/docs/18/sql-analyze.html\" title=\"ANALYZE\"\u003e\u003cspan class=\"refentrytitle\"\u003eANALYZE\u003c/span\u003e\u003c/a\u003e。\u003c/p\u003e\u003cp\u003e普通的\u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e（不带\u003ccode class=\"literal\"\u003eFULL\u003c/code\u003e）只是回收空间并使其可被重用。这种形式的命令可以与表的正常读写并行运行，因为它不会获得独占锁。不过，在大多数情况下，额外空间不会返还给操作系统；它只是保留在同一张表内供再次使用。它还允许利用多个 CPU 来处理索引，这项功能称为\u003cem class=\"firstterm\"\u003e并行清理\u003c/em\u003e。如需禁用该功能，可以使用\u003ccode class=\"literal\"\u003ePARALLEL\u003c/code\u003e选项并将并行工作进程数指定为零。\u003ccode class=\"command\"\u003eVACUUM FULL\u003c/code\u003e会把表的全部内容重写到一个没有额外空闲空间的新磁盘文件中，从而让未使用的空间能够返还给操作系统。这种形式要慢得多，并且在处理每个表时都需要 \u003ccode class=\"literal\"\u003eACCESS EXCLUSIVE\u003c/code\u003e锁。\u003c/p\u003e","key":"description","title":"描述"},{"html":"\u003cdiv class=\"variablelist\"\u003e\u003cdl class=\"variablelist\"\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eFULL\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e选择\u003cspan class=\"quote\"\u003e“\u003cspan class=\"quote\"\u003e完全\u003c/span\u003e”\u003c/span\u003e清理，它可以回收更多空间，但耗时更长，并且会独占锁定该表。这种方法还需要额外的磁盘空间，因为它会写出该表的一个新副本，并且在操作完成之前不会释放旧副本。通常，只有当需要从表内回收大量空间时才应使用这种方法。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eFREEZE\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e选择激进的元组\u003cspan class=\"quote\"\u003e“\u003cspan class=\"quote\"\u003e冻结\u003c/span\u003e”\u003c/span\u003e。指定\u003ccode class=\"literal\"\u003eFREEZE\u003c/code\u003e 等价于执行一个将\u003ca href=\"/docs/18/runtime-config-vacuum.html#GUC-VACUUM-FREEZE-MIN-AGE\"\u003evacuum_freeze_min_age\u003c/a\u003e和\u003ca href=\"/docs/18/runtime-config-vacuum.html#GUC-VACUUM-FREEZE-TABLE-AGE\"\u003evacuum_freeze_table_age\u003c/a\u003e参数设为零的 \u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e。在表被重写时总会执行激进冻结，因此指定了 \u003ccode class=\"literal\"\u003eFULL\u003c/code\u003e时，这个选项就是多余的。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eVERBOSE\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e为每个表以\u003ccode class=\"literal\"\u003eINFO\u003c/code\u003e级别输出详细的清理活动报告。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eANALYZE\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e更新规划器用来确定查询最高效执行方式的统计信息。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eDISABLE_PAGE_SKIPPING\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e通常，\u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e会根据\u003ca href=\"/docs/18/routine-vacuuming.html#VACUUM-FOR-VISIBILITY-MAP\" title=\"24.1.4. 更新可见性映射\"\u003e可见性映射\u003c/a\u003e跳过某些页面。已知其中所有元组都已冻结的页面总是可以跳过，而已知其中所有元组都对所有事务可见的页面，也可以跳过，除非正在执行激进清理。此外，除非正在执行激进清理，为了避免等待其他会话结束对页面的使用，也可能会跳过某些页面。此选项会禁用所有跳页行为，只应在怀疑可见性映射内容存在问题时使用；而这种情况应当只会在硬件或软件问题导致数据库损坏时发生。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eSKIP_LOCKED\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e指定\u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e在开始处理某个关系时，不要等待任何冲突锁被释放：如果某个关系无法在无需等待的情况下立即获得锁，就跳过该关系。请注意，即使使用了此选项，\u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e在打开该关系的索引时仍可能发生阻塞。此外，\u003ccode class=\"command\"\u003eVACUUM ANALYZE\u003c/code\u003e在从分区、继承子表和某些类型的外部表获取样本行时，仍然可能发生阻塞。还有，虽然\u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e通常会处理指定分区表的全部分区，但如果该分区表上存在冲突锁，此选项会导致\u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e跳过所有分区。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eINDEX_CLEANUP\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e通常，当表中的死元组很少时，\u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e会跳过索引清理。在这种情况下，处理该表全部索引的代价预计会远高于删除死索引元组带来的收益。当表中存在多于零个死元组时，此选项可用于强制\u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e处理索引。默认值是\u003ccode class=\"literal\"\u003eAUTO\u003c/code\u003e，这允许\u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e在适当时跳过索引清理。如果\u003ccode class=\"literal\"\u003eINDEX_CLEANUP\u003c/code\u003e设为\u003ccode class=\"literal\"\u003eON\u003c/code\u003e，\u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e将保守地从索引中移除所有死元组。这对于兼容 \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e早期版本可能很有用，因为在那些版本中这是标准行为。\u003c/p\u003e\u003cp\u003e\u003ccode class=\"literal\"\u003eINDEX_CLEANUP\u003c/code\u003e也可以设为\u003ccode class=\"literal\"\u003eOFF\u003c/code\u003e，以强制\u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e\u003cspan class=\"emphasis\"\u003e\u003cem\u003e总是\u003c/em\u003e\u003c/span\u003e跳过索引清理，即使表中有很多死元组也是如此。当需要让\u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e尽可能快地运行，以避免临近的事务 ID 回卷时，这可能会有用（参见\u003ca href=\"/docs/18/routine-vacuuming.html#VACUUM-FOR-WRAPAROUND\" title=\"24.1.5. 防止事务 ID 回卷失败\"\u003e第 24.1.5 节\u003c/a\u003e）。不过，由\u003ca href=\"/docs/18/runtime-config-vacuum.html#GUC-VACUUM-FAILSAFE-AGE\"\u003evacuum_failsafe_age\u003c/a\u003e控制的回卷失效保护机制通常会自动触发，以避免事务 ID 回卷失败，因此一般应优先依赖该机制。如果不定期执行索引清理，性能可能会受到影响，因为随着表被修改，索引会积累死元组，而表本身也会积累在索引清理完成前无法移除的死行指针。\u003c/p\u003e\u003cp\u003e对没有索引的表，此选项没有效果；如果使用了\u003ccode class=\"literal\"\u003eFULL\u003c/code\u003e选项，它也会被忽略。它对事务 ID 回卷失效保护机制同样没有影响。当该机制被触发时，即使 \u003ccode class=\"literal\"\u003eINDEX_CLEANUP\u003c/code\u003e设为\u003ccode class=\"literal\"\u003eON\u003c/code\u003e，也会跳过索引清理。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003ePROCESS_MAIN\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e指定\u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e应尝试处理主关系。这通常是期望的行为，也是默认行为。当只需要清理某个关系对应的\u003ccode class=\"literal\"\u003eTOAST\u003c/code\u003e表时，将此选项设为 false 可能会有用。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003ePROCESS_TOAST\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e指定\u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e应尝试处理每个关系对应的\u003ccode class=\"literal\"\u003eTOAST\u003c/code\u003e表（如果存在）。这通常是期望的行为，也是默认行为。当只需要清理主关系时，将此选项设为 false 可能会有用。使用\u003ccode class=\"literal\"\u003eFULL\u003c/code\u003e选项时必须启用此选项。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eTRUNCATE\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e指定\u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e应尝试截去表末尾的空页，使这些被截断页面占用的磁盘空间能够返还给操作系统。通常这是期望的行为，也是默认行为，除非\u003ca href=\"/docs/18/runtime-config-vacuum.html#GUC-VACUUM-TRUNCATE\"\u003evacuum_truncate\u003c/a\u003e被设为 false，或者待清理表的 \u003ccode class=\"literal\"\u003evacuum_truncate\u003c/code\u003e选项被设为 false。将此选项设为 false 有助于避免截断操作所需的表级\u003ccode class=\"literal\"\u003eACCESS EXCLUSIVE\u003c/code\u003e锁。使用\u003ccode class=\"literal\"\u003eFULL\u003c/code\u003e选项时，此选项会被忽略。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003ePARALLEL\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e并行执行\u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e的索引清理和索引收尾清理阶段，使用\u003cem class=\"replaceable\"\u003e\u003ccode\u003einteger\u003c/code\u003e\u003c/em\u003e个后台工作进程（每个清理阶段的细节参见\u003ca href=\"/docs/18/progress-reporting.html#VACUUM-PHASES\" title=\"表 27.46. VACUUM 阶段\"\u003e表 27.46\u003c/a\u003e）。实际用于执行操作的工作进程数量，等于该关系上支持并行清理的索引数量，如果指定了\u003ccode class=\"literal\"\u003ePARALLEL\u003c/code\u003e选项，则该数量受其中指定的工作进程数限制，并进一步受\u003ca href=\"/docs/18/runtime-config-resource.html#GUC-MAX-PARALLEL-MAINTENANCE-WORKERS\"\u003emax_parallel_maintenance_workers\u003c/a\u003e限制。当且仅当索引大小大于\u003ca href=\"/docs/18/runtime-config-query.html#GUC-MIN-PARALLEL-INDEX-SCAN-SIZE\"\u003emin_parallel_index_scan_size\u003c/a\u003e时，该索引才能参与并行清理。请注意，执行期间不保证一定会使用\u003cem class=\"replaceable\"\u003e\u003ccode\u003einteger\u003c/code\u003e\u003c/em\u003e中指定的并行工作进程数。一次清理可能使用少于指定数量的工作进程，甚至完全不使用工作进程。每个索引最多只能使用一个工作进程，因此只有当表中至少有\u003ccode class=\"literal\"\u003e2\u003c/code\u003e个索引时才会启动并行工作进程。清理工作进程会在每个阶段开始前启动，并在该阶段结束时退出。这些行为在未来版本中可能会发生变化。此选项不能与\u003ccode class=\"literal\"\u003eFULL\u003c/code\u003e选项一起使用。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eSKIP_DATABASE_STATS\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e指定\u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e应跳过更新数据库范围内关于最老未冻结 XID 的统计信息。通常，\u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e会在命令结束时更新这些统计信息。不过，在拥有大量表的数据库中，这可能需要一段时间，而且只有当包含最老未冻结 XID 的表也在本次清理范围内时，这么做才有意义。此外，如果多个\u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e命令并行发出，那么同一时间只能有一个命令更新数据库范围统计信息。因此，如果应用程序打算连续发出一系列\u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e命令，那么除了最后一条命令之外，其余命令都可以设置此选项；或者在所有命令上都设置它，然后再单独执行 \u003ccode class=\"literal\"\u003eVACUUM (ONLY_DATABASE_STATS)\u003c/code\u003e。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eONLY_DATABASE_STATS\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e指定\u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e除了更新数据库范围内关于最老未冻结 XID 的统计信息外，不执行任何其他操作。指定该选项时，\u003cem class=\"replaceable\"\u003e\u003ccode\u003etable_and_columns\u003c/code\u003e\u003c/em\u003e列表必须为空，并且除了\u003ccode class=\"literal\"\u003eVERBOSE\u003c/code\u003e之外不能启用其他任何选项。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eBUFFER_USAGE_LIMIT\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e指定\u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e所使用的\u003ca href=\"/docs/18/glossary.html#GLOSSARY-BUFFER-ACCESS-STRATEGY\"\u003e\u003c/a\u003e\u003ca href=\"/docs/18/glossary.html#GLOSSARY-BUFFER-ACCESS-STRATEGY\" title=\"缓冲区访问策略\"\u003e缓冲区访问策略（Buffer Access Strategy）\u003c/a\u003e环形缓冲区大小。该大小用于计算在此策略中会被重用的共享缓冲区数量。\u003ccode class=\"literal\"\u003e0\u003c/code\u003e表示禁用\u003ccode class=\"literal\"\u003e缓冲区访问策略\u003c/code\u003e。如果同时指定了\u003ccode class=\"option\"\u003eANALYZE\u003c/code\u003e，那么\u003ccode class=\"option\"\u003eBUFFER_USAGE_LIMIT\u003c/code\u003e的值会同时用于清理阶段和分析阶段。除非也指定了\u003ccode class=\"option\"\u003eANALYZE\u003c/code\u003e，否则该选项不能与\u003ccode class=\"option\"\u003eFULL\u003c/code\u003e一起使用。如果未指定该选项，\u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e会使用\u003ca href=\"/docs/18/runtime-config-resource.html#GUC-VACUUM-BUFFER-USAGE-LIMIT\"\u003evacuum_buffer_usage_limit\u003c/a\u003e中的值。更高的设置可以让 \u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e运行得更快，但设置过大可能会把过多其他有用页面从共享缓冲区中逐出。最小值是\u003ccode class=\"literal\"\u003e128 kB\u003c/code\u003e，最大值是\u003ccode class=\"literal\"\u003e16 GB\u003c/code\u003e。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003eboolean\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e指定所选选项是否开启。可以写\u003ccode class=\"literal\"\u003eTRUE\u003c/code\u003e、\u003ccode class=\"literal\"\u003eON\u003c/code\u003e或\u003ccode class=\"literal\"\u003e1\u003c/code\u003e来启用选项，写\u003ccode class=\"literal\"\u003eFALSE\u003c/code\u003e、\u003ccode class=\"literal\"\u003eOFF\u003c/code\u003e或\u003ccode class=\"literal\"\u003e0\u003c/code\u003e来禁用它。也可以省略\u003cem class=\"replaceable\"\u003e\u003ccode\u003eboolean\u003c/code\u003e\u003c/em\u003e值，此时假定为\u003ccode class=\"literal\"\u003eTRUE\u003c/code\u003e。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003einteger\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e指定传递给所选选项的非负整数值。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003esize\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e指定以千字节为单位的内存大小。也可以把大小写成一个字符串，即数值后跟下列任一种内存单位：\u003ccode class=\"literal\"\u003eB\u003c/code\u003e（字节）、\u003ccode class=\"literal\"\u003ekB\u003c/code\u003e（千字节）、\u003ccode class=\"literal\"\u003eMB\u003c/code\u003e（兆字节）、\u003ccode class=\"literal\"\u003eGB\u003c/code\u003e（吉字节）或\u003ccode class=\"literal\"\u003eTB\u003c/code\u003e（太字节）。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003etable_name\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e要清理的特定表或物化视图的名称（可选地带模式限定）。如果在表名前指定\u003ccode class=\"literal\"\u003eONLY\u003c/code\u003e，则只清理该表。如果未指定\u003ccode class=\"literal\"\u003eONLY\u003c/code\u003e，则还会清理该表及其所有继承子表或分区（如果有）。也可以在表名后显式指定\u003ccode class=\"literal\"\u003e*\u003c/code\u003e，以明确表示要清理继承子表（或分区）。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003ecolumn_name\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e要分析的特定列名。默认会分析所有列。如果指定了列列表，则也必须指定\u003ccode class=\"literal\"\u003eANALYZE\u003c/code\u003e。\u003c/p\u003e\u003c/dd\u003e\u003c/dl\u003e\u003c/div\u003e","key":"parameters","title":"参数"},{"html":"\u003cp\u003e指定\u003ccode class=\"literal\"\u003eVERBOSE\u003c/code\u003e时，\u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e会输出进度消息，指示当前正在处理哪个表，同时还会打印这些表的各种统计信息。\u003c/p\u003e","key":"outputs","title":"输出"},{"html":"\u003cp\u003e要清理一个表，通常必须拥有该表上的\u003ccode class=\"literal\"\u003eMAINTAIN\u003c/code\u003e权限。不过，数据库拥有者可以清理其数据库中的所有表，但共享系统目录除外。\u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e会跳过调用用户无权清理的任何表。\u003c/p\u003e\u003cp\u003e当\u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e运行时，\u003ca href=\"/docs/18/runtime-config-client.html#GUC-SEARCH-PATH\"\u003esearch_path\u003c/a\u003e会被临时改为 \u003ccode class=\"literal\"\u003epg_catalog, pg_temp\u003c/code\u003e。\u003c/p\u003e\u003cp\u003e\u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e不能在一个事务块内被执行。\u003c/p\u003e\u003cp\u003e对于带有\u003cacronym\u003eGIN\u003c/acronym\u003e索引的表，\u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e（任何形式）还会通过把待处理的索引项移动到主 \u003cacronym\u003eGIN\u003c/acronym\u003e索引结构中的适当位置，来完成所有挂起的索引插入。详见\u003ca href=\"/docs/18/gin.html#GIN-FAST-UPDATE\" title=\"65.4.4.1. GIN 快速更新技术\"\u003e第 65.4.4.1 节\u003c/a\u003e。\u003c/p\u003e\u003cp\u003e我们建议定期对所有数据库执行清理，以移除死行。\u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e提供了一个\u003cspan class=\"quote\"\u003e“\u003cspan class=\"quote\"\u003e自动清理（autovacuum）\u003c/span\u003e”\u003c/span\u003e机制，可以自动执行常规清理维护。有关自动与手动清理的更多信息，参见\u003ca href=\"/docs/18/routine-vacuuming.html\" title=\"24.1. 日常清理\"\u003e第 24.1 节\u003c/a\u003e。\u003c/p\u003e\u003cp\u003e\u003ccode class=\"option\"\u003eFULL\u003c/code\u003e选项不建议在日常场景中使用，但在某些特殊情况下可能很有用。例如，当你删除或更新了表中的绝大多数行，并希望该表在物理上收缩以占用更少磁盘空间、让表扫描更快时，这个选项就比较合适。\u003ccode class=\"command\"\u003eVACUUM FULL\u003c/code\u003e通常会比普通 \u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e更大幅度地收缩表。\u003c/p\u003e\u003cp\u003e\u003ccode class=\"option\"\u003ePARALLEL\u003c/code\u003e选项只用于清理。如果此选项与\u003ccode class=\"option\"\u003eANALYZE\u003c/code\u003e选项一起指定，它不会影响\u003ccode class=\"option\"\u003eANALYZE\u003c/code\u003e。\u003c/p\u003e\u003cp\u003e\u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e会显著增加 I/O 流量，这可能导致其他活动会话性能变差。因此，有时建议使用基于代价的清理延迟特性。对于并行清理，每个工作进程的睡眠时长都与该工作进程完成的工作量成比例。详见\u003ca href=\"/docs/18/runtime-config-vacuum.html#RUNTIME-CONFIG-RESOURCE-VACUUM-COST\" title=\"19.10.2. 基于代价的清理延迟\"\u003e第 19.10.2 节\u003c/a\u003e。\u003c/p\u003e\u003cp\u003e每个运行不带\u003ccode class=\"literal\"\u003eFULL\u003c/code\u003e选项的\u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e的后端，都会在 \u003ccode class=\"structname\"\u003epg_stat_progress_vacuum\u003c/code\u003e视图中报告其进度。运行\u003ccode class=\"command\"\u003eVACUUM FULL\u003c/code\u003e的后端则会改为在 \u003ccode class=\"structname\"\u003epg_stat_progress_cluster\u003c/code\u003e视图中报告其进度。详见\u003ca href=\"/docs/18/progress-reporting.html#VACUUM-PROGRESS-REPORTING\" title=\"27.4.5. VACUUM 进度报告\"\u003e第 27.4.5 节\u003c/a\u003e和\u003ca href=\"/docs/18/progress-reporting.html#CLUSTER-PROGRESS-REPORTING\" title=\"27.4.2. CLUSTER 进度报告\"\u003e第 27.4.2 节\u003c/a\u003e。\u003c/p\u003e","key":"notes","title":"注解"},{"html":"\u003cp\u003e清理单个表\u003ccode class=\"literal\"\u003eonek\u003c/code\u003e，对其进行分析以供优化器使用，并打印详细的清理活动报告：\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eVACUUM (VERBOSE, ANALYZE) onek;\n\u003c/pre\u003e","key":"examples","title":"示例"},{"html":"\u003cp\u003e在SQL标准中没有\u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e语句。\u003c/p\u003e\u003cp\u003e下列语法在\u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e 9.0版本之前使用，并且目前仍受支持：\u003c/p\u003e\u003cpre class=\"synopsis\"\u003eVACUUM [ FULL ] [ FREEZE ] [ VERBOSE ] [ ANALYZE ] [ \u003cem class=\"replaceable\"\u003e\u003ccode\u003etable_and_columns\u003c/code\u003e\u003c/em\u003e [, ...] ]\n\u003c/pre\u003e\u003cp\u003e请注意，在这种语法中，选项必须严格按所示顺序指定。\u003c/p\u003e","key":"compatibility","title":"兼容性"},{"html":"\u003cspan class=\"simplelist\"\u003e\u003ca href=\"/docs/18/app-vacuumdb.html\" title=\"vacuumdb\"\u003e\u003cspan class=\"refentrytitle\"\u003e\u003cspan class=\"application\"\u003evacuumdb\u003c/span\u003e\u003c/span\u003e\u003c/a\u003e, \u003ca href=\"/docs/18/runtime-config-vacuum.html#RUNTIME-CONFIG-RESOURCE-VACUUM-COST\" title=\"19.10.2. 基于代价的清理延迟\"\u003e第 19.10.2 节\u003c/a\u003e, \u003ca href=\"/docs/18/routine-vacuuming.html#AUTOVACUUM\" title=\"24.1.6. 自动清理守护进程\"\u003e第 24.1.6 节\u003c/a\u003e, \u003ca href=\"/docs/18/progress-reporting.html#VACUUM-PROGRESS-REPORTING\" title=\"27.4.5. VACUUM 进度报告\"\u003e第 27.4.5 节\u003c/a\u003e, \u003ca href=\"/docs/18/progress-reporting.html#CLUSTER-PROGRESS-REPORTING\" title=\"27.4.2. CLUSTER 进度报告\"\u003e第 27.4.2 节\u003c/a\u003e\u003c/span\u003e","key":"see_also","title":"另见"}],"sections_same_as":"","synopsis_html":"VACUUM [ ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003eoption\u003c/code\u003e\u003c/em\u003e [, ...] ) ] [ \u003cem class=\"replaceable\"\u003e\u003ccode\u003etable_and_columns\u003c/code\u003e\u003c/em\u003e [, ...] ]\n\n\u003cspan class=\"phrase\"\u003e其中\u003cem class=\"replaceable\"\u003e\u003ccode\u003eoption\u003c/code\u003e\u003c/em\u003e可以是下列之一：\u003c/span\u003e\n\n    FULL [ \u003cem class=\"replaceable\"\u003e\u003ccode\u003eboolean\u003c/code\u003e\u003c/em\u003e ]\n    FREEZE [ \u003cem class=\"replaceable\"\u003e\u003ccode\u003eboolean\u003c/code\u003e\u003c/em\u003e ]\n    VERBOSE [ \u003cem class=\"replaceable\"\u003e\u003ccode\u003eboolean\u003c/code\u003e\u003c/em\u003e ]\n    ANALYZE [ \u003cem class=\"replaceable\"\u003e\u003ccode\u003eboolean\u003c/code\u003e\u003c/em\u003e ]\n    DISABLE_PAGE_SKIPPING [ \u003cem class=\"replaceable\"\u003e\u003ccode\u003eboolean\u003c/code\u003e\u003c/em\u003e ]\n    SKIP_LOCKED [ \u003cem class=\"replaceable\"\u003e\u003ccode\u003eboolean\u003c/code\u003e\u003c/em\u003e ]\n    INDEX_CLEANUP { AUTO | ON | OFF }\n    PROCESS_MAIN [ \u003cem class=\"replaceable\"\u003e\u003ccode\u003eboolean\u003c/code\u003e\u003c/em\u003e ]\n    PROCESS_TOAST [ \u003cem class=\"replaceable\"\u003e\u003ccode\u003eboolean\u003c/code\u003e\u003c/em\u003e ]\n    TRUNCATE [ \u003cem class=\"replaceable\"\u003e\u003ccode\u003eboolean\u003c/code\u003e\u003c/em\u003e ]\n    PARALLEL \u003cem class=\"replaceable\"\u003e\u003ccode\u003einteger\u003c/code\u003e\u003c/em\u003e\n    SKIP_DATABASE_STATS [ \u003cem class=\"replaceable\"\u003e\u003ccode\u003eboolean\u003c/code\u003e\u003c/em\u003e ]\n    ONLY_DATABASE_STATS [ \u003cem class=\"replaceable\"\u003e\u003ccode\u003eboolean\u003c/code\u003e\u003c/em\u003e ]\n    BUFFER_USAGE_LIMIT \u003cem class=\"replaceable\"\u003e\u003ccode\u003esize\u003c/code\u003e\u003c/em\u003e\n\n\u003cspan class=\"phrase\"\u003e其中\u003cem class=\"replaceable\"\u003e\u003ccode\u003etable_and_columns\u003c/code\u003e\u003c/em\u003e是：\u003c/span\u003e\n\n    [ ONLY ] \u003cem class=\"replaceable\"\u003e\u003ccode\u003etable_name\u003c/code\u003e\u003c/em\u003e [ * ] [ ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecolumn_name\u003c/code\u003e\u003c/em\u003e [, ...] ) ]","synopsis_text":"VACUUM [ ( option [, ...] ) ] [ table_and_columns [, ...] ]\n\n其中option可以是下列之一：\n\nFULL [ boolean ]\nFREEZE [ boolean ]\nVERBOSE [ boolean ]\nANALYZE [ boolean ]\nDISABLE_PAGE_SKIPPING [ boolean ]\nSKIP_LOCKED [ boolean ]\nINDEX_CLEANUP { AUTO | ON | OFF }\nPROCESS_MAIN [ boolean ]\nPROCESS_TOAST [ boolean ]\nTRUNCATE [ boolean ]\nPARALLEL integer\nSKIP_DATABASE_STATS [ boolean ]\nONLY_DATABASE_STATS [ boolean ]\nBUFFER_USAGE_LIMIT size\n其中table_and_columns是：\n\n[ ONLY ] table_name [ * ] [ ( column_name [, ...] ) ]"}},"RequestedLocale":"zh-Hans","Fallback":false,"Versions":["10","11","12","13","14","15","16","17","18","19","20","6.4","6.5","7.0","7.1","7.2","7.3","7.4","8.0","8.1","8.2","8.3","8.4","9.0","9.1","9.2","9.3","9.4","9.5","9.6"],"Locales":["en","zh-Hans"],"Signatures":null,"Spellings":null,"SQLState":null,"Evidence":null}
