VACUUM(7)

๐Ÿ“ฆ version 18.6, in opensuse-leap 16.0

โœจ 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 ๐ŸงŠ. specifying FREEZE is literally equivalent to running VACUUM with the vacuum_freeze_min_age and vacuum_freeze_table_age parameters set to zero. and heads up: aggressive freezing always happens when the table gets rewritten anyway, so this option is redundant if you also said FULL. 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 normally VACUUM skips 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 โ€” tells VACUUM not 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, VACUUM may still block when opening the relation's indexes. also, VACUUM ANALYZE may still block when acquiring sample rows from partitions, table inheritance children, and some types of foreign tables. and also โ€” while VACUUM ordinarily 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, VACUUM skips 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 force VACUUM to process indexes when there are more than zero dead tuples. the default is AUTO, which lets VACUUM skip index vacuuming when it's appropriate. if INDEX_CLEANUP is set to ON, VACUUM will 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 โ€” says VACUUM should 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 โ€” says VACUUM should 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 the FULL option is used.
  • TRUNCATE โ€” says VACUUM should 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 โ€” unless vacuum_truncate is set to false, or the vacuum_truncate option 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 the FULL option is used.
  • PARALLEL โ€” performs the index vacuum and index cleanup phases of VACUUM in 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 the PARALLEL option, if any โ€” which is further limited by max_parallel_maintenance_workers. an index can participate in parallel vacuum if and only if the size of the index is more than min_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 the FULL option.
  • SKIP_DATABASE_STATS โ€” says VACUUM should skip updating the database-wide statistics about oldest unfrozen XIDs. normally VACUUM updates 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 multiple VACUUM commands 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 many VACUUM 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 VACUUM (ONLY_DATABASE_STATS) afterwards. chef's kiss efficiency ๐Ÿ’…
  • ONLY_DATABASE_STATS โ€” says VACUUM should do nothing except update the database-wide statistics about oldest unfrozen XIDs. when this option is specified, the table_and_columns list must be empty, and no other option may be enabled except VERBOSE.
  • BUFFER_USAGE_LIMIT โ€” specifies the Buffer Access Strategy ring buffer size for VACUUM. this size is used to calculate the number of shared buffers that will be reused as part of this strategy. 0 disables use of a Buffer Access Strategy. if ANALYZE is also specified, the BUFFER_USAGE_LIMIT value is used for both the vacuum and the analyze stages. this option can't be used with the FULL option โ€” except if ANALYZE is also specified. when this option isn't specified, VACUUM uses the value from vacuum_buffer_usage_limit. higher settings can let VACUUM run 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 write TRUE, ON, or 1 to enable the option, and FALSE, OFF, or 0 to disable it. the boolean value can also be omitted entirely, in which case TRUE is 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), or TB (terabytes).
  • table_name โ€” the name (optionally schema-qualified) of a specific table or materialized view to vacuum ๐Ÿงผ. if ONLY is specified before the table name, only that table is vacuumed. if ONLY isn'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, ANALYZE must 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