VACUUM(7)

πŸ“¦ version 16.15, in opensuse-leap 16.0

🌸 NAME 🌸

okay bestie, this is the VACUUM(7) page from the PostgreSQL 16.15 Documentation, and the whole vibe is: VACUUM β€” garbage-collect and optionally analyze a database. πŸ’–

✨ SYNOPSIS ✨

the syntax situation, exactly as it is:

VACUUM [ ( option [, ...] ) ] [ table_and_columns [, ...] ]
VACUUM [ FULL ] [ FREEZE ] [ VERBOSE ] [ ANALYZE ] [ 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:

    table_name [ ( column_name [, ...] ) ]

πŸ’… DESCRIPTION πŸ’…

VACUUM reclaims storage occupied by dead tuples. periodt. In normal PostgreSQL operation, tuples that are deleted or obsoleted by an update are not physically removed from their table β€” they remain present until a VACUUM is done. So yeah, it’s necessary to do VACUUM periodically, especially on frequently-updated tables that are always in the group chat. πŸ”₯

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). πŸ¦‹

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. 😭

When the option list is surrounded by parentheses, the options can be written in any order. Without parentheses, options must be specified in exactly the order shown above. The parenthesized syntax was added in PostgreSQL 9.0; the unparenthesized syntax is deprecated.

⚠️ deprecated alert: the parenthesized syntax was added in PostgreSQL 9.0, and the unparenthesized syntax is deprecated β€” it’s giving legacy, bestie. πŸ’€

✨ PARAMETERS ✨

Okay bestie, this is the part where the vacuum clean gives you options β€” and there are a LOT of them, so buckle up πŸ’…


  • FULL β€” picks the β€œfull” vacuum. It reclaims more space but takes much longer and exclusively locks the table. It also needs extra disk space, because it writes a whole new copy of the table and doesn't let go of the old copy until it's finished. Basically: only reach for this when you genuinely need to claw back a significant amount of space from inside the table. Like a full closet purge vs. just tidying the top shelf 🧹
  • FREEZE β€” picks aggressive β€œfreezing” of tuples ❄️. Specifying FREEZE is equivalent to running VACUUM with the vacuum_freeze_min_age and vacuum_freeze_table_age parameters set to zero. Heads up though: aggressive freezing always happens when the table is rewritten, so this option is redundant when FULL is specified β€” double-dipping, no cap.
  • VERBOSE β€” prints a detailed vacuum activity report for each table πŸ—’οΈβœ¨
  • ANALYZE β€” updates the statistics the planner uses to figure out the most efficient way to execute a query πŸ“Š
  • DISABLE_PAGE_SKIPPING β€” okay 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. Also, except during an aggressive vacuum, some pages may be skipped to avoid waiting for other sessions to finish using them. This option disables all page-skipping behavior, 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 casual Tuesday vibe.
  • SKIP_LOCKED β€” says VACUUM shouldn't wait 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 gets skipped. ⚠️ But note: 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 one more: while VACUUM ordinarily processes all partitions of specified partitioned tables, this option will cause VACUUM to skip all partitions if there's a conflicting lock on the partitioned table. So it's giving "avoid the wait" but not "avoid the wait entirely", iykyk 😭
  • INDEX_CLEANUP β€” normally, VACUUM skips index vacuuming when there are very few dead tuples in the table. Why? Because the cost of processing all of the table's indexes is expected to greatly exceed the benefit of removing dead index tuples in that scenario. This option can force VACUUM to process indexes when there are more than zero dead tuples. The default is AUTO, which lets VACUUM skip index vacuuming when 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 to make VACUUM run as quickly as possible to avoid imminent transaction ID wraparound (see Section 25.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. So like, skipping it is a ✨short-term✨ win.

This option has no effect for tables that have no index, and is ignored if the FULL option is used. It also has no effect on the transaction ID wraparound failsafe mechanism β€” when triggered, 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 what you want, 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 what you want, also the default 🫢. Setting this option to false may be useful when you only need to vacuum the main relation. 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 is the default β€” unless 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 that truncation requires. This option is ignored if the FULL option is used.
  • PARALLEL β€” perform the index vacuum and index cleanup phases of VACUUM in parallel using integer background workers (for the deets on each vacuum phase, please refer to Table 28.45 πŸ“–). The number of workers used 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.
Please note: it is not guaranteed that the number of parallel workers specified in integer will be used during execution 😭. A vacuum can totally 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 will accomplish nothing unless the table that had contained the oldest unfrozen XID was among those vacuumed. On top of that, 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. Big brain move 🧠✨
  • 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. That's it, that's the whole job πŸ’…
  • BUFFER_USAGE_LIMIT β€” specifies the Buffer Access Strategy ring buffer size for VACUUM. This size is used to calculate the number of shared buffers which 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 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 be 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, in which case TRUE is assumed. Slay, defaults 🌸
  • integer β€” specifies a non-negative integer value passed to the selected option. No negatives, bestie 🚫
  • 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 these 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 the specified table is a partitioned table, all of its leaf partitions are vacuumed πŸƒ
  • column_name β€” the name of a specific column to analyze. Defaults to all columns. If a column list is specified, ANALYZE must also be specified. Important ‼️

🌈 OUTPUTS 🌈

When VERBOSE is specified, VACUUM emits progress messages to indicate which table is currently being processed πŸ‘€. Various statistics about the tables are printed as well. It's giving "live commentary while cleaning", and honestly? We love a transparent queen πŸ’–

πŸ’… NOTES πŸ’…

okay bestie, permission check first 🫢 to vacuum a table you ordinarily gotta be that table's owner or a superuser. but here's the cute loophole: database owners are allowed to vacuum all tables in their databases β€” except shared catalogs. and yeah, that shared-catalog restriction means a true database-wide VACUUM can only be performed by a superuser πŸ‘‘. also VACUUM will just skip over any tables the calling user doesn't have permission to vacuum, like, no drama, it simply moves on πŸ’β€β™€οΈ

🚨 important vibe check: VACUUM cannot be executed inside a transaction block. like, at all. don't try it.

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 70.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 β€” literally your cleanup bestie doing the work for you. for more information about automatic and manual vacuuming, see Section 25.1.


now, the FULL option: not recommended for routine use πŸ™ˆ but it 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 β€” it's giving deep clean vs. tidy up. the way that it actually moves the furniture 😭

the PARALLEL option is used only for vacuum purposes. if this option is specified with the ANALYZE option, it does not affect ANALYZE β€” so don't expect parallel analyze, that's not the vibe ✨

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 20.4.4 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 28.4.5 and Section 28.4.2 for details. progress tracking is so main character energy fr fr πŸ’…


🌸 EXAMPLES 🌸

to clean a single table onek, analyze it for the optimizer and print a detailed vacuum activity report:

VACUUM (VERBOSE, ANALYZE) onek;

slay, that's the whole example. one line, iconic behavior ✨


πŸ¦‹ COMPATIBILITY πŸ¦‹

there is no VACUUM statement in the SQL standard. nope, not a thing, PostgreSQL said "I'll make my own" πŸ’…


⭐ SEE ALSO ⭐

  • vacuumdb(1)
  • Section 20.4.4
  • Section 25.1.6
  • Section 28.4.5
  • Section 28.4.2

and that's the whole page bestie, you ate that πŸŽ€ go forth and vacuum responsibly πŸ’–

🌈 this gem is from PostgreSQL 16.15, dated 2026, and it's the VACUUM(7) manual page. mwah, see u next time 😘
πŸ“¦ where this page comes from