VACUUM(7)
πΈ 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 βοΈ. SpecifyingFREEZEis equivalent to runningVACUUMwith thevacuum_freeze_min_ageandvacuum_freeze_table_ageparameters set to zero. Heads up though: aggressive freezing always happens when the table is rewritten, so this option is redundant whenFULLis 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 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. 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β saysVACUUMshouldn'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,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 one more: whileVACUUMordinarily processes all partitions of specified partitioned tables, this option will causeVACUUMto 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,VACUUMskips 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 forceVACUUMto process indexes when there are more than zero dead tuples. The default isAUTO, which letsVACUUMskip 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β saysVACUUMshould 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β saysVACUUMshould 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 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 is the default β unless thevacuum_truncateoption has been set to false for the table being vacuumed. Setting this option to false may be useful to avoid theACCESS EXCLUSIVElock that truncation requires. This option is ignored if theFULLoption is used.PARALLELβ perform the index vacuum and index cleanup phases ofVACUUMin 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 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.
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β 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 will accomplish nothing unless the table that had contained the oldest unfrozen XID was among those vacuumed. On top of that, 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. Big brain move π§ β¨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. That's it, that's the whole job πBUFFER_USAGE_LIMITβ specifies the Buffer Access Strategy ring buffer size forVACUUM. This size is used to calculate the number of shared buffers which 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 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 be evicted from shared buffers π«£. The minimum value is128 kBand the maximum value is16 GB.- boolean β specifies whether the selected option should be turned on or off. You can write
TRUE,ON, or1to enable the option, andFALSE,OFF, or0to disable it. The boolean value can also be omitted, in which caseTRUEis 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), orTB(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,
ANALYZEmust 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
- opensuse-leap 16.0: postgresql16 16.15-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql16 16.15-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql16 16.15-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql16 16.15-160000.1.1 Β· PostgreSQL
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms