VACUUM(7)
โจ VACUUM(7) โจ
PostgreSQL 18.6 Documentation ๐ bestie welcome to the VACUUM page, this is the manual page for VACUUM and honestly it's a whole vibe.
๐ NAME ๐
VACUUM โ garbage-collect and optionally analyze a database ๐งนโจ
๐ธ SYNOPSIS ๐ธ
VACUUM [ ( option [, ...] ) ] [ table_and_columns [, ...] ]
where option can be one of:
FULL [ boolean ]
FREEZE [ boolean ]
VERBOSE [ boolean ]
ANALYZE [ boolean ]
DISABLE_PAGE_SKIPPING [ boolean ]
SKIP_LOCKED [ boolean ]
INDEX_CLEANUP { AUTO | ON | OFF }
PROCESS_MAIN [ boolean ]
PROCESS_TOAST [ boolean ]
TRUNCATE [ boolean ]
PARALLEL integer
SKIP_DATABASE_STATS [ boolean ]
ONLY_DATABASE_STATS [ boolean ]
BUFFER_USAGE_LIMIT size
and table_and_columns is:
[ ONLY ] table_name [ * ] [ ( column_name [, ...] ) ]
๐ฆ DESCRIPTION ๐ฆ
Okay so VACUUM reclaims storage occupied by dead tuples. ๐งโโ๏ธ And when I say "dead tuples" I mean: in normal PostgreSQL operation, tuples that are deleted or obsoleted by an update are not physically removed from their table. They justโฆ stay there. Haunting the place. ๐ป They remain present until a VACUUM is done. So therefore it's necessary to do VACUUM periodically, especially on frequently-updated tables. Not optional bestie, periodic. ๐
Without a table_and_columns list, VACUUM processes every table and materialized view in the current database that the current user has permission to vacuum. ๐ With a list, VACUUM processes only those table(s). Simple as that.
VACUUM ANALYZE performs a VACUUM and then an ANALYZE for each selected table. This is a handy combination form for routine maintenance scripts. ๐ซถ See ANALYZE(7) for more details about its processing.
Plain VACUUM (without FULL) 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 parallel vacuum. To disable this feature, one can use the PARALLEL option and specify parallel workers as zero. โญ
VACUUM FULL 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 ACCESS EXCLUSIVE lock on each table while it is being processed.
๐ ngl the ACCESS EXCLUSIVE lock part is not a joke, it means nobody else gets to touch that table while it's happening. Plan accordingly bestie.
โจ PARAMETERS โจ
okay bestie, this is the full snack menu ๐ every knob, every toggle, every little dial you can hand to VACUUM. we're doing all of them. no skimming, no "eh I'll skip this one", because every single one is here for a reason and you're gonna know all of them. periodt.
FULLโ this picks "full" vacuum, and yeah it can reclaim more space ๐งน but it takes much longer and it exclusively locks the table. it also needs extra disk space, because it writes a brand new copy of the table and doesn't let go of the old copy until the whole operation is done. so basically: only reach for this when there's a significant amount of space that needs to be reclaimed from inside the table. not a casual move, bestie.FREEZEโ this selects aggressive "freezing" of tuples ๐ง. specifyingFREEZEis literally equivalent to runningVACUUMwith thevacuum_freeze_min_ageandvacuum_freeze_table_ageparameters set to zero. and heads up: aggressive freezing always happens when the table gets rewritten anyway, so this option is redundant if you also saidFULL. double-dipping, we love to see it, but it's pointless here.VERBOSEโ prints a detailed vacuum activity report for each table, at INFO level. it's the "tell me everything you're doing" option ๐ขANALYZEโ updates the statistics the planner uses to figure out the most efficient way to execute a query ๐. the planner is the main character of query planning and this is how you feed it.DISABLE_PAGE_SKIPPINGโ so normallyVACUUMskips pages based on the visibility map. pages where all tuples are known to be frozen can always be skipped, and pages where all tuples are known to be visible to all transactions may be skipped โ except when you're doing an aggressive vacuum. on top of that, except when doing an aggressive vacuum, some pages may get skipped just to avoid waiting for other sessions to finish using them. this option turns all of that page-skipping behavior off. and it's meant to be used only when the contents of the visibility map are suspect โ which should only happen if there's a hardware or software issue causing database corruption ๐ so likeโฆ not a vibe. not a casual toggle.SKIP_LOCKEDโ tellsVACUUMnot to wait around for any conflicting locks to be released when it starts working on a relation: if a relation can't be locked immediately without waiting, that relation just gets skipped ๐โโ๏ธ. but read the fine print, bestie: even with this option,VACUUMmay still block when opening the relation's indexes. also,VACUUM ANALYZEmay still block when acquiring sample rows from partitions, table inheritance children, and some types of foreign tables. and also โ whileVACUUMordinarily processes all partitions of specified partitioned tables, this option will make it skip all partitions if there's a conflicting lock on the partitioned table. so it's a "mostly don't wait" not a "never wait" situation.INDEX_CLEANUPโ normally,VACUUMskips 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 those dead index tuples when that happens. this option lets you forceVACUUMto process indexes when there are more than zero dead tuples. the default isAUTO, which letsVACUUMskip index vacuuming when it's appropriate. ifINDEX_CLEANUPis set toON,VACUUMwill conservatively remove all dead tuples from indexes. this may be useful for backwards compatibility with earlier releases of PostgreSQL, where this was the standard behavior.
INDEX_CLEANUP can also be set to OFF to force VACUUM to always skip index vacuuming, even when there are many dead tuples in the table. this may be useful when you need VACUUM to run as quickly as possible to avoid imminent transaction ID wraparound (see Section 24.1.5). however โ the wraparound failsafe mechanism controlled by vacuum_failsafe_age will generally trigger automatically to avoid transaction ID wraparound failure, and that should be preferred. ๐จ if index cleanup isn't 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 can't be removed until index cleanup is completed.
this option has no effect for tables that have no index, and it's ignored if the FULL option is used. it also has no effect on the transaction ID wraparound failsafe mechanism โ when that triggers it will skip index vacuuming, even when INDEX_CLEANUP is set to ON.
PROCESS_MAINโ saysVACUUMshould attempt to process the main relation. this is usually the desired behavior and it's the default. setting this option to false may be useful when you only need to vacuum a relation's corresponding TOAST table.PROCESS_TOASTโ saysVACUUMshould attempt to process the corresponding TOAST table for each relation, if one exists. also usually the desired behavior and also the default. setting it to false may be useful when you only need to vacuum the main relation. and heads up: this option is required when theFULLoption is used.TRUNCATEโ saysVACUUMshould attempt to truncate off any empty pages at the end of the table, and let the disk space for those truncated pages go back to the operating system โป๏ธ. this is normally the desired behavior and it's the default โ unlessvacuum_truncateis set to false, or thevacuum_truncateoption has been set to false for the table being vacuumed. setting this option to false may be useful to avoid the ACCESS EXCLUSIVE lock on the table that the truncation requires. this option is ignored if theFULLoption is used.PARALLELโ performs the index vacuum and index cleanup phases ofVACUUMin parallel, using integer background workers ๐๏ธ (for the details of each vacuum phase, please refer to Table 27.46). 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 thePARALLELoption, if any โ which is further limited bymax_parallel_maintenance_workers. an index can participate in parallel vacuum if and only if the size of the index is more thanmin_parallel_index_scan_size. also please note: it is not guaranteed that the number of parallel workers specified in integer will actually get used during execution. it's 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 2 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 theFULLoption.SKIP_DATABASE_STATSโ saysVACUUMshould skip updating the database-wide statistics about oldest unfrozen XIDs. normallyVACUUMupdates these statistics once at the end of the command. but that can take a while in a database with a very large number of tables, and it'll accomplish nothing unless the table that had contained the oldest unfrozen XID was among those vacuumed. moreover, if multipleVACUUMcommands are issued in parallel, only one of them can update the database-wide statistics at a time. so: if an application intends to issue a series of manyVACUUMcommands, it can be helpful to set this option in all but the last such command โ or set it in all the commands and separately issueVACUUM (ONLY_DATABASE_STATS)afterwards. chef's kiss efficiency ๐ONLY_DATABASE_STATSโ saysVACUUMshould do nothing except update the database-wide statistics about oldest unfrozen XIDs. when this option is specified, thetable_and_columnslist must be empty, and no other option may be enabled exceptVERBOSE.BUFFER_USAGE_LIMITโ specifies the Buffer Access Strategy ring buffer size forVACUUM. this size is used to calculate the number of shared buffers that will be reused as part of this strategy.0disables use of a Buffer Access Strategy. ifANALYZEis also specified, theBUFFER_USAGE_LIMITvalue is used for both the vacuum and the analyze stages. this option can't be used with theFULLoption โ except ifANALYZEis also specified. when this option isn't specified,VACUUMuses the value fromvacuum_buffer_usage_limit. higher settings can letVACUUMrun more quickly, but having too large a setting may cause too many other useful pages to get evicted from shared buffers ๐ญ. the minimum value is 128 kB and the maximum value is 16 GB.booleanโ specifies whether the selected option should be turned on or off. you can writeTRUE,ON, or1to enable the option, andFALSE,OFF, or0to disable it. the boolean value can also be omitted entirely, in which caseTRUEis assumed โจintegerโ specifies a non-negative integer value passed to the selected option ๐ขsizeโ specifies 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:B(bytes),kB(kilobytes),MB(megabytes),GB(gigabytes), orTB(terabytes).table_nameโ the name (optionally schema-qualified) of a specific table or materialized view to vacuum ๐งผ. ifONLYis specified before the table name, only that table is vacuumed. ifONLYisn't specified, the table and all its inheritance child tables or partitions (if any) are also vacuumed. optionally,*can be specified after the table name to explicitly indicate that inheritance child tables (or partitions) are to be vacuumed.column_nameโ the name of a specific column to analyze. defaults to all columns. and if a column list is specified,ANALYZEmust also be specified.
๐ซถ OUTPUTS ๐ซถ
when VERBOSE is specified, VACUUM emits progress messages to tell you which table is currently being processed ๐ฃ. various statistics about the tables get printed as well. it's giving "watch me work" energy and honestly? iconic.
โจ NOTES โจ
ok so before you go vacuuming everything in sight, there's some lore you gotta know ๐
To vacuum a table, you'd ordinarily need the MAINTAIN privilege on that table. But โ plot twist โ database owners can vacuum all tables in their databases, except shared catalogs. And VACUUM will just skip over any tables the calling user doesn't have permission to vacuum. like, it's not gonna fight you about it, it's just gonna walk past them ๐ถโโ๏ธ
While VACUUM is running, the search_path gets temporarily changed to pg_catalog, pg_temp. (it's giving "don't look at me, I'm working" energy)
๐ซ VACUUM cannot be executed inside a transaction block. that's a hard no, bestie.
For tables with GIN indexes, VACUUM (in any form) also completes any pending index insertions, by moving pending index entries to the appropriate places in the main GIN index structure. See Section 65.4.4.1 for details. โจ
We recommend that all databases be vacuumed regularly in order to remove dead rows. ๐งน PostgreSQL includes an "autovacuum" facility which can automate routine vacuum maintenance. For more information about automatic and manual vacuuming, see Section 24.1.
The FULL 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. VACUUM FULL will usually shrink the table more than a plain VACUUM would. ๐ฅ
The PARALLEL option is used only for vacuum purposes. If this option is specified with the ANALYZE option, it does not affect ANALYZE. (side quest only, no crossover episodes)
VACUUM 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 Section 19.10.2 for details.
Each backend running VACUUM without the FULL option will report its progress in the pg_stat_progress_vacuum view. Backends running VACUUM FULL will instead report their progress in the pg_stat_progress_cluster view. See Section 27.4.5 and Section 27.4.2 for details. ๐
๐ธ EXAMPLES ๐ธ
Wanna clean a single table onek, analyze it for the optimizer and print a detailed vacuum activity report? say less:
VACUUM (VERBOSE, ANALYZE) onek;
๐ซ COMPATIBILITY ๐ซ
ngl, there is no VACUUM statement in the SQL standard. it's just not a thing there ๐คทโโ๏ธ
The following syntax was used before PostgreSQL version 9.0 and is still supported:
VACUUM [ FULL ] [ FREEZE ] [ VERBOSE ] [ ANALYZE ] [ table_and_columns [, ...] ]
โ ๏ธ Note that in this syntax, the options must be specified in exactly the order shown. no shuffling, no remixing, the order is the order periodt ๐
๐ SEE ALSO ๐
vacuumdb(1)- Section 19.10.2
- Section 24.1.6
- Section 27.4.5
- Section 27.4.2
and that's the whole page bestie!! ๐ this was PostgreSQL 18.6, dated 2026, section VACUUM(7) โ go forth and vacuum responsibly, ur tables will literally thank u ๐ซถโจ
๐ฆ where this page comes from
- opensuse-leap 16.0: postgresql18 18.6-160000.1.1 ยท PostgreSQL
- opensuse-leap 16.0: postgresql18 18.6-160000.1.1 ยท PostgreSQL
- opensuse-leap 16.0: postgresql18 18.6-160000.1.1 ยท PostgreSQL
- opensuse-leap 16.0: postgresql18 18.6-160000.1.1 ยท PostgreSQL
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms