1VACUUM(7) PostgreSQL 11.6 Documentation VACUUM(7)
2
3
4
6 VACUUM - garbage-collect and optionally analyze a database
7
9 VACUUM [ ( option [, ...] ) ] [ table_and_columns [, ...] ]
10 VACUUM [ FULL ] [ FREEZE ] [ VERBOSE ] [ ANALYZE ] [ table_and_columns [, ...] ]
11
12 where option can be one of:
13
14 FULL
15 FREEZE
16 VERBOSE
17 ANALYZE
18 DISABLE_PAGE_SKIPPING
19
20 and table_and_columns is:
21
22 table_name [ ( column_name [, ...] ) ]
23
25 VACUUM reclaims storage occupied by dead tuples. In normal PostgreSQL
26 operation, tuples that are deleted or obsoleted by an update are not
27 physically removed from their table; they remain present until a VACUUM
28 is done. Therefore it's necessary to do VACUUM periodically, especially
29 on frequently-updated tables.
30
31 Without a table_and_columns list, VACUUM processes every table and
32 materialized view in the current database that the current user has
33 permission to vacuum. With a list, VACUUM processes only those
34 table(s).
35
36 VACUUM ANALYZE performs a VACUUM and then an ANALYZE for each selected
37 table. This is a handy combination form for routine maintenance
38 scripts. See ANALYZE(7) for more details about its processing.
39
40 Plain VACUUM (without FULL) simply reclaims space and makes it
41 available for re-use. This form of the command can operate in parallel
42 with normal reading and writing of the table, as an exclusive lock is
43 not obtained. However, extra space is not returned to the operating
44 system (in most cases); it's just kept available for re-use within the
45 same table. VACUUM FULL rewrites the entire contents of the table into
46 a new disk file with no extra space, allowing unused space to be
47 returned to the operating system. This form is much slower and requires
48 an exclusive lock on each table while it is being processed.
49
50 When the option list is surrounded by parentheses, the options can be
51 written in any order. Without parentheses, options must be specified in
52 exactly the order shown above. The parenthesized syntax was added in
53 PostgreSQL 9.0; the unparenthesized syntax is deprecated.
54
56 FULL
57 Selects “full” vacuum, which can reclaim more space, but takes much
58 longer and exclusively locks the table. This method also requires
59 extra disk space, since it writes a new copy of the table and
60 doesn't release the old copy until the operation is complete.
61 Usually this should only be used when a significant amount of space
62 needs to be reclaimed from within the table.
63
64 FREEZE
65 Selects aggressive “freezing” of tuples. Specifying FREEZE is
66 equivalent to performing VACUUM with the vacuum_freeze_min_age and
67 vacuum_freeze_table_age parameters set to zero. Aggressive freezing
68 is always performed when the table is rewritten, so this option is
69 redundant when FULL is specified.
70
71 VERBOSE
72 Prints a detailed vacuum activity report for each table.
73
74 ANALYZE
75 Updates statistics used by the planner to determine the most
76 efficient way to execute a query.
77
78 DISABLE_PAGE_SKIPPING
79 Normally, VACUUM will skip pages based on the visibility map. Pages
80 where all tuples are known to be frozen can always be skipped, and
81 those where all tuples are known to be visible to all transactions
82 may be skipped except when performing an aggressive vacuum.
83 Furthermore, except when performing an aggressive vacuum, some
84 pages may be skipped in order to avoid waiting for other sessions
85 to finish using them. This option disables all page-skipping
86 behavior, and is intended to be used only when the contents of the
87 visibility map are suspect, which should happen only if there is a
88 hardware or software issue causing database corruption.
89
90 table_name
91 The name (optionally schema-qualified) of a specific table or
92 materialized view to vacuum. If the specified table is a
93 partitioned table, all of its leaf partitions are vacuumed.
94
95 column_name
96 The name of a specific column to analyze. Defaults to all columns.
97 If a column list is specified, ANALYZE must also be specified.
98
100 When VERBOSE is specified, VACUUM emits progress messages to indicate
101 which table is currently being processed. Various statistics about the
102 tables are printed as well.
103
105 To vacuum a table, one must ordinarily be the table's owner or a
106 superuser. However, database owners are allowed to vacuum all tables in
107 their databases, except shared catalogs. (The restriction for shared
108 catalogs means that a true database-wide VACUUM can only be performed
109 by a superuser.) VACUUM will skip over any tables that the calling
110 user does not have permission to vacuum.
111
112 VACUUM cannot be executed inside a transaction block.
113
114 For tables with GIN indexes, VACUUM (in any form) also completes any
115 pending index insertions, by moving pending index entries to the
116 appropriate places in the main GIN index structure. See Section 66.4.1
117 for details.
118
119 We recommend that active production databases be vacuumed frequently
120 (at least nightly), in order to remove dead rows. After adding or
121 deleting a large number of rows, it might be a good idea to issue a
122 VACUUM ANALYZE command for the affected table. This will update the
123 system catalogs with the results of all recent changes, and allow the
124 PostgreSQL query planner to make better choices in planning queries.
125
126 The FULL option is not recommended for routine use, but might be useful
127 in special cases. An example is when you have deleted or updated most
128 of the rows in a table and would like the table to physically shrink to
129 occupy less disk space and allow faster table scans. VACUUM FULL will
130 usually shrink the table more than a plain VACUUM would.
131
132 VACUUM causes a substantial increase in I/O traffic, which might cause
133 poor performance for other active sessions. Therefore, it is sometimes
134 advisable to use the cost-based vacuum delay feature. See
135 Section 19.4.4 for details.
136
137 PostgreSQL includes an “autovacuum” facility which can automate routine
138 vacuum maintenance. For more information about automatic and manual
139 vacuuming, see Section 24.1.
140
142 To clean a single table onek, analyze it for the optimizer and print a
143 detailed vacuum activity report:
144
145 VACUUM (VERBOSE, ANALYZE) onek;
146
148 There is no VACUUM statement in the SQL standard.
149
151 vacuumdb(1), Section 19.4.4, Section 24.1.6
152
153
154
155PostgreSQL 11.6 2019 VACUUM(7)