]> begriffs open source - ai-pg/blob - full-docs/man7/VACUUM.7
Convert HTML docs to more streamlined TXT
[ai-pg] / full-docs / man7 / VACUUM.7
1 '\" t
2 .\"     Title: VACUUM
3 .\"    Author: The PostgreSQL Global Development Group
4 .\" Generator: DocBook XSL Stylesheets vsnapshot <http://docbook.sf.net/>
5 .\"      Date: 2025
6 .\"    Manual: PostgreSQL 18.0 Documentation
7 .\"    Source: PostgreSQL 18.0
8 .\"  Language: English
9 .\"
10 .TH "VACUUM" "7" "2025" "PostgreSQL 18.0" "PostgreSQL 18.0 Documentation"
11 .\" -----------------------------------------------------------------
12 .\" * Define some portability stuff
13 .\" -----------------------------------------------------------------
14 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15 .\" http://bugs.debian.org/507673
16 .\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
17 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
18 .ie \n(.g .ds Aq \(aq
19 .el       .ds Aq '
20 .\" -----------------------------------------------------------------
21 .\" * set default formatting
22 .\" -----------------------------------------------------------------
23 .\" disable hyphenation
24 .nh
25 .\" disable justification (adjust text to left margin only)
26 .ad l
27 .\" -----------------------------------------------------------------
28 .\" * MAIN CONTENT STARTS HERE *
29 .\" -----------------------------------------------------------------
30 .SH "NAME"
31 VACUUM \- garbage\-collect and optionally analyze a database
32 .SH "SYNOPSIS"
33 .sp
34 .nf
35 VACUUM [ ( \fIoption\fR [, \&.\&.\&.] ) ] [ \fItable_and_columns\fR [, \&.\&.\&.] ]
36
37 where \fIoption\fR can be one of:
38
39     FULL [ \fIboolean\fR ]
40     FREEZE [ \fIboolean\fR ]
41     VERBOSE [ \fIboolean\fR ]
42     ANALYZE [ \fIboolean\fR ]
43     DISABLE_PAGE_SKIPPING [ \fIboolean\fR ]
44     SKIP_LOCKED [ \fIboolean\fR ]
45     INDEX_CLEANUP { AUTO | ON | OFF }
46     PROCESS_MAIN [ \fIboolean\fR ]
47     PROCESS_TOAST [ \fIboolean\fR ]
48     TRUNCATE [ \fIboolean\fR ]
49     PARALLEL \fIinteger\fR
50     SKIP_DATABASE_STATS [ \fIboolean\fR ]
51     ONLY_DATABASE_STATS [ \fIboolean\fR ]
52     BUFFER_USAGE_LIMIT \fIsize\fR
53
54 and \fItable_and_columns\fR is:
55
56     [ ONLY ] \fItable_name\fR [ * ] [ ( \fIcolumn_name\fR [, \&.\&.\&.] ) ]
57 .fi
58 .SH "DESCRIPTION"
59 .PP
60 \fBVACUUM\fR
61 reclaims storage occupied by dead tuples\&. In normal
62 PostgreSQL
63 operation, tuples that are deleted or obsoleted by an update are not physically removed from their table; they remain present until a
64 \fBVACUUM\fR
65 is done\&. Therefore it\*(Aqs necessary to do
66 \fBVACUUM\fR
67 periodically, especially on frequently\-updated tables\&.
68 .PP
69 Without a
70 \fItable_and_columns\fR
71 list,
72 \fBVACUUM\fR
73 processes every table and materialized view in the current database that the current user has permission to vacuum\&. With a list,
74 \fBVACUUM\fR
75 processes only those table(s)\&.
76 .PP
77 \fBVACUUM ANALYZE\fR
78 performs a
79 \fBVACUUM\fR
80 and then an
81 \fBANALYZE\fR
82 for each selected table\&. This is a handy combination form for routine maintenance scripts\&. See
83 \fBANALYZE\fR(7)
84 for more details about its processing\&.
85 .PP
86 Plain
87 \fBVACUUM\fR
88 (without
89 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\*(Aqs 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
90 parallel vacuum\&. To disable this feature, one can use
91 PARALLEL
92 option and specify parallel workers as zero\&.
93 \fBVACUUM FULL\fR
94 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
95 ACCESS EXCLUSIVE
96 lock on each table while it is being processed\&.
97 .SH "PARAMETERS"
98 .PP
99 FULL
100 .RS 4
101 Selects
102 \(lqfull\(rq
103 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\*(Aqt 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\&.
104 .RE
105 .PP
106 FREEZE
107 .RS 4
108 Selects aggressive
109 \(lqfreezing\(rq
110 of tuples\&. Specifying
111 FREEZE
112 is equivalent to performing
113 \fBVACUUM\fR
114 with the
115 vacuum_freeze_min_age
116 and
117 vacuum_freeze_table_age
118 parameters set to zero\&. Aggressive freezing is always performed when the table is rewritten, so this option is redundant when
119 FULL
120 is specified\&.
121 .RE
122 .PP
123 VERBOSE
124 .RS 4
125 Prints a detailed vacuum activity report for each table at
126 INFO
127 level\&.
128 .RE
129 .PP
130 ANALYZE
131 .RS 4
132 Updates statistics used by the planner to determine the most efficient way to execute a query\&.
133 .RE
134 .PP
135 DISABLE_PAGE_SKIPPING
136 .RS 4
137 Normally,
138 \fBVACUUM\fR
139 will skip pages based on the
140 visibility map\&. 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\&.
141 .RE
142 .PP
143 SKIP_LOCKED
144 .RS 4
145 Specifies that
146 \fBVACUUM\fR
147 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,
148 \fBVACUUM\fR
149 may still block when opening the relation\*(Aqs indexes\&. Additionally,
150 \fBVACUUM ANALYZE\fR
151 may still block when acquiring sample rows from partitions, table inheritance children, and some types of foreign tables\&. Also, while
152 \fBVACUUM\fR
153 ordinarily processes all partitions of specified partitioned tables, this option will cause
154 \fBVACUUM\fR
155 to skip all partitions if there is a conflicting lock on the partitioned table\&.
156 .RE
157 .PP
158 INDEX_CLEANUP
159 .RS 4
160 Normally,
161 \fBVACUUM\fR
162 will skip index vacuuming when there are very few dead tuples in the table\&. The cost of processing all of the table\*(Aqs indexes is expected to greatly exceed the benefit of removing dead index tuples when this happens\&. This option can be used to force
163 \fBVACUUM\fR
164 to process indexes when there are more than zero dead tuples\&. The default is
165 AUTO, which allows
166 \fBVACUUM\fR
167 to skip index vacuuming when appropriate\&. If
168 INDEX_CLEANUP
169 is set to
170 ON,
171 \fBVACUUM\fR
172 will conservatively remove all dead tuples from indexes\&. This may be useful for backwards compatibility with earlier releases of
173 PostgreSQL
174 where this was the standard behavior\&.
175 .sp
176 INDEX_CLEANUP
177 can also be set to
178 OFF
179 to force
180 \fBVACUUM\fR
181 to
182 \fIalways\fR
183 skip index vacuuming, even when there are many dead tuples in the table\&. This may be useful when it is necessary to make
184 \fBVACUUM\fR
185 run as quickly as possible to avoid imminent transaction ID wraparound (see
186 Section\ \&24.1.5)\&. However, the wraparound failsafe mechanism controlled by
187 vacuum_failsafe_age
188 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\&.
189 .sp
190 This option has no effect for tables that have no index and is ignored if the
191 FULL
192 option is used\&. It also has no effect on the transaction ID wraparound failsafe mechanism\&. When triggered it will skip index vacuuming, even when
193 INDEX_CLEANUP
194 is set to
195 ON\&.
196 .RE
197 .PP
198 PROCESS_MAIN
199 .RS 4
200 Specifies that
201 \fBVACUUM\fR
202 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\*(Aqs corresponding
203 TOAST
204 table\&.
205 .RE
206 .PP
207 PROCESS_TOAST
208 .RS 4
209 Specifies that
210 \fBVACUUM\fR
211 should attempt to process the corresponding
212 TOAST
213 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
214 FULL
215 option is used\&.
216 .RE
217 .PP
218 TRUNCATE
219 .RS 4
220 Specifies that
221 \fBVACUUM\fR
222 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
223 vacuum_truncate
224 is set to false or the
225 vacuum_truncate
226 option has been set to false for the table to be vacuumed\&. Setting this option to false may be useful to avoid
227 ACCESS EXCLUSIVE
228 lock on the table that the truncation requires\&. This option is ignored if the
229 FULL
230 option is used\&.
231 .RE
232 .PP
233 PARALLEL
234 .RS 4
235 Perform index vacuum and index cleanup phases of
236 \fBVACUUM\fR
237 in parallel using
238 \fIinteger\fR
239 background workers (for the details of each vacuum phase, please refer to
240 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
241 PARALLEL
242 option if any which is further limited by
243 max_parallel_maintenance_workers\&. An index can participate in parallel vacuum if and only if the size of the index is more than
244 min_parallel_index_scan_size\&. Please note that it is not guaranteed that the number of parallel workers specified in
245 \fIinteger\fR
246 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
247 2
248 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\*(Aqt be used with the
249 FULL
250 option\&.
251 .RE
252 .PP
253 SKIP_DATABASE_STATS
254 .RS 4
255 Specifies that
256 \fBVACUUM\fR
257 should skip updating the database\-wide statistics about oldest unfrozen XIDs\&. Normally
258 \fBVACUUM\fR
259 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
260 \fBVACUUM\fR
261 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
262 \fBVACUUM\fR
263 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
264 VACUUM (ONLY_DATABASE_STATS)
265 afterwards\&.
266 .RE
267 .PP
268 ONLY_DATABASE_STATS
269 .RS 4
270 Specifies that
271 \fBVACUUM\fR
272 should do nothing except update the database\-wide statistics about oldest unfrozen XIDs\&. When this option is specified, the
273 \fItable_and_columns\fR
274 list must be empty, and no other option may be enabled except
275 VERBOSE\&.
276 .RE
277 .PP
278 BUFFER_USAGE_LIMIT
279 .RS 4
280 Specifies the
281 Buffer Access Strategy
282 ring buffer size for
283 \fBVACUUM\fR\&. This size is used to calculate the number of shared buffers which will be reused as part of this strategy\&.
284 0
285 disables use of a
286 Buffer Access Strategy\&. If
287 \fBANALYZE\fR
288 is also specified, the
289 \fBBUFFER_USAGE_LIMIT\fR
290 value is used for both the vacuum and analyze stages\&. This option can\*(Aqt be used with the
291 \fBFULL\fR
292 option except if
293 \fBANALYZE\fR
294 is also specified\&. When this option is not specified,
295 \fBVACUUM\fR
296 uses the value from
297 vacuum_buffer_usage_limit\&. Higher settings can allow
298 \fBVACUUM\fR
299 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
300 128 kB
301 and the maximum value is
302 16 GB\&.
303 .RE
304 .PP
305 \fIboolean\fR
306 .RS 4
307 Specifies whether the selected option should be turned on or off\&. You can write
308 TRUE,
309 ON, or
310 1
311 to enable the option, and
312 FALSE,
313 OFF, or
314 0
315 to disable it\&. The
316 \fIboolean\fR
317 value can also be omitted, in which case
318 TRUE
319 is assumed\&.
320 .RE
321 .PP
322 \fIinteger\fR
323 .RS 4
324 Specifies a non\-negative integer value passed to the selected option\&.
325 .RE
326 .PP
327 \fIsize\fR
328 .RS 4
329 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:
330 B
331 (bytes),
332 kB
333 (kilobytes),
334 MB
335 (megabytes),
336 GB
337 (gigabytes), or
338 TB
339 (terabytes)\&.
340 .RE
341 .PP
342 \fItable_name\fR
343 .RS 4
344 The name (optionally schema\-qualified) of a specific table or materialized view to vacuum\&. If
345 ONLY
346 is specified before the table name, only that table is vacuumed\&. If
347 ONLY
348 is not specified, the table and all its inheritance child tables or partitions (if any) are also vacuumed\&. Optionally,
349 *
350 can be specified after the table name to explicitly indicate that inheritance child tables (or partitions) are to be vacuumed\&.
351 .RE
352 .PP
353 \fIcolumn_name\fR
354 .RS 4
355 The name of a specific column to analyze\&. Defaults to all columns\&. If a column list is specified,
356 ANALYZE
357 must also be specified\&.
358 .RE
359 .SH "OUTPUTS"
360 .PP
361 When
362 VERBOSE
363 is specified,
364 \fBVACUUM\fR
365 emits progress messages to indicate which table is currently being processed\&. Various statistics about the tables are printed as well\&.
366 .SH "NOTES"
367 .PP
368 To vacuum a table, one must ordinarily have the
369 MAINTAIN
370 privilege on the table\&. However, database owners are allowed to vacuum all tables in their databases, except shared catalogs\&.
371 \fBVACUUM\fR
372 will skip over any tables that the calling user does not have permission to vacuum\&.
373 .PP
374 While
375 \fBVACUUM\fR
376 is running, the
377 search_path
378 is temporarily changed to
379 pg_catalog, pg_temp\&.
380 .PP
381 \fBVACUUM\fR
382 cannot be executed inside a transaction block\&.
383 .PP
384 For tables with
385 GIN
386 indexes,
387 \fBVACUUM\fR
388 (in any form) also completes any pending index insertions, by moving pending index entries to the appropriate places in the main
389 GIN
390 index structure\&. See
391 Section\ \&65.4.4.1
392 for details\&.
393 .PP
394 We recommend that all databases be vacuumed regularly in order to remove dead rows\&.
395 PostgreSQL
396 includes an
397 \(lqautovacuum\(rq
398 facility which can automate routine vacuum maintenance\&. For more information about automatic and manual vacuuming, see
399 Section\ \&24.1\&.
400 .PP
401 The
402 \fBFULL\fR
403 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\&.
404 \fBVACUUM FULL\fR
405 will usually shrink the table more than a plain
406 \fBVACUUM\fR
407 would\&.
408 .PP
409 The
410 \fBPARALLEL\fR
411 option is used only for vacuum purposes\&. If this option is specified with the
412 \fBANALYZE\fR
413 option, it does not affect
414 \fBANALYZE\fR\&.
415 .PP
416 \fBVACUUM\fR
417 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
418 Section\ \&19.10.2
419 for details\&.
420 .PP
421 Each backend running
422 \fBVACUUM\fR
423 without the
424 FULL
425 option will report its progress in the
426 pg_stat_progress_vacuum
427 view\&. Backends running
428 \fBVACUUM FULL\fR
429 will instead report their progress in the
430 pg_stat_progress_cluster
431 view\&. See
432 Section\ \&27.4.5
433 and
434 Section\ \&27.4.2
435 for details\&.
436 .SH "EXAMPLES"
437 .PP
438 To clean a single table
439 onek, analyze it for the optimizer and print a detailed vacuum activity report:
440 .sp
441 .if n \{\
442 .RS 4
443 .\}
444 .nf
445 VACUUM (VERBOSE, ANALYZE) onek;
446 .fi
447 .if n \{\
448 .RE
449 .\}
450 .SH "COMPATIBILITY"
451 .PP
452 There is no
453 \fBVACUUM\fR
454 statement in the SQL standard\&.
455 .PP
456 The following syntax was used before
457 PostgreSQL
458 version 9\&.0 and is still supported:
459 .sp
460 .if n \{\
461 .RS 4
462 .\}
463 .nf
464 VACUUM [ FULL ] [ FREEZE ] [ VERBOSE ] [ ANALYZE ] [ \fItable_and_columns\fR [, \&.\&.\&.] ]
465 .fi
466 .if n \{\
467 .RE
468 .\}
469 .sp
470 Note that in this syntax, the options must be specified in exactly the order shown\&.
471 .SH "SEE ALSO"
472 \fBvacuumdb\fR(1), Section\ \&19.10.2, Section\ \&24.1.6, Section\ \&27.4.5, Section\ \&27.4.2