]> begriffs open source - ai-pg/blob - full-docs/src/sgml/man7/CLUSTER.7
PG 18 docs from https://ftp.postgresql.org/pub/source/v18.0/postgresql-18.0-docs...
[ai-pg] / full-docs / src / sgml / man7 / CLUSTER.7
1 '\" t
2 .\"     Title: CLUSTER
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 "CLUSTER" "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 CLUSTER \- cluster a table according to an index
32 .SH "SYNOPSIS"
33 .sp
34 .nf
35 CLUSTER [ ( \fIoption\fR [, \&.\&.\&.] ) ] [ \fItable_name\fR [ USING \fIindex_name\fR ] ]
36
37 where \fIoption\fR can be one of:
38
39     VERBOSE [ \fIboolean\fR ]
40 .fi
41 .SH "DESCRIPTION"
42 .PP
43 \fBCLUSTER\fR
44 instructs
45 PostgreSQL
46 to cluster the table specified by
47 \fItable_name\fR
48 based on the index specified by
49 \fIindex_name\fR\&. The index must already have been defined on
50 \fItable_name\fR\&.
51 .PP
52 When a table is clustered, it is physically reordered based on the index information\&. Clustering is a one\-time operation: when the table is subsequently updated, the changes are not clustered\&. That is, no attempt is made to store new or updated rows according to their index order\&. (If one wishes, one can periodically recluster by issuing the command again\&. Also, setting the table\*(Aqs
53 fillfactor
54 storage parameter to less than 100% can aid in preserving cluster ordering during updates, since updated rows are kept on the same page if enough space is available there\&.)
55 .PP
56 When a table is clustered,
57 PostgreSQL
58 remembers which index it was clustered by\&. The form
59 \fBCLUSTER \fR\fB\fItable_name\fR\fR
60 reclusters the table using the same index as before\&. You can also use the
61 CLUSTER
62 or
63 SET WITHOUT CLUSTER
64 forms of
65 \fBALTER TABLE\fR
66 to set the index to be used for future cluster operations, or to clear any previous setting\&.
67 .PP
68 \fBCLUSTER\fR
69 without a
70 \fItable_name\fR
71 reclusters all the previously\-clustered tables in the current database that the calling user has privileges for\&. This form of
72 \fBCLUSTER\fR
73 cannot be executed inside a transaction block\&.
74 .PP
75 When a table is being clustered, an
76 ACCESS EXCLUSIVE
77 lock is acquired on it\&. This prevents any other database operations (both reads and writes) from operating on the table until the
78 \fBCLUSTER\fR
79 is finished\&.
80 .SH "PARAMETERS"
81 .PP
82 \fItable_name\fR
83 .RS 4
84 The name (possibly schema\-qualified) of a table\&.
85 .RE
86 .PP
87 \fIindex_name\fR
88 .RS 4
89 The name of an index\&.
90 .RE
91 .PP
92 VERBOSE
93 .RS 4
94 Prints a progress report as each table is clustered at
95 INFO
96 level\&.
97 .RE
98 .PP
99 \fIboolean\fR
100 .RS 4
101 Specifies whether the selected option should be turned on or off\&. You can write
102 TRUE,
103 ON, or
104 1
105 to enable the option, and
106 FALSE,
107 OFF, or
108 0
109 to disable it\&. The
110 \fIboolean\fR
111 value can also be omitted, in which case
112 TRUE
113 is assumed\&.
114 .RE
115 .SH "NOTES"
116 .PP
117 To cluster a table, one must have the
118 MAINTAIN
119 privilege on the table\&.
120 .PP
121 In cases where you are accessing single rows randomly within a table, the actual order of the data in the table is unimportant\&. However, if you tend to access some data more than others, and there is an index that groups them together, you will benefit from using
122 \fBCLUSTER\fR\&. If you are requesting a range of indexed values from a table, or a single indexed value that has multiple rows that match,
123 \fBCLUSTER\fR
124 will help because once the index identifies the table page for the first row that matches, all other rows that match are probably already on the same table page, and so you save disk accesses and speed up the query\&.
125 .PP
126 \fBCLUSTER\fR
127 can re\-sort the table using either an index scan on the specified index, or (if the index is a b\-tree) a sequential scan followed by sorting\&. It will attempt to choose the method that will be faster, based on planner cost parameters and available statistical information\&.
128 .PP
129 While
130 \fBCLUSTER\fR
131 is running, the
132 search_path
133 is temporarily changed to
134 pg_catalog, pg_temp\&.
135 .PP
136 When an index scan is used, a temporary copy of the table is created that contains the table data in the index order\&. Temporary copies of each index on the table are created as well\&. Therefore, you need free space on disk at least equal to the sum of the table size and the index sizes\&.
137 .PP
138 When a sequential scan and sort is used, a temporary sort file is also created, so that the peak temporary space requirement is as much as double the table size, plus the index sizes\&. This method is often faster than the index scan method, but if the disk space requirement is intolerable, you can disable this choice by temporarily setting
139 enable_sort
140 to
141 off\&.
142 .PP
143 It is advisable to set
144 maintenance_work_mem
145 to a reasonably large value (but not more than the amount of RAM you can dedicate to the
146 \fBCLUSTER\fR
147 operation) before clustering\&.
148 .PP
149 Because the planner records statistics about the ordering of tables, it is advisable to run
150 \fBANALYZE\fR
151 on the newly clustered table\&. Otherwise, the planner might make poor choices of query plans\&.
152 .PP
153 Because
154 \fBCLUSTER\fR
155 remembers which indexes are clustered, one can cluster the tables one wants clustered manually the first time, then set up a periodic maintenance script that executes
156 \fBCLUSTER\fR
157 without any parameters, so that the desired tables are periodically reclustered\&.
158 .PP
159 Each backend running
160 \fBCLUSTER\fR
161 will report its progress in the
162 pg_stat_progress_cluster
163 view\&. See
164 Section\ \&27.4.2
165 for details\&.
166 .PP
167 Clustering a partitioned table clusters each of its partitions using the partition of the specified partitioned index\&. When clustering a partitioned table, the index may not be omitted\&.
168 \fBCLUSTER\fR
169 on a partitioned table cannot be executed inside a transaction block\&.
170 .SH "EXAMPLES"
171 .PP
172 Cluster the table
173 employees
174 on the basis of its index
175 employees_ind:
176 .sp
177 .if n \{\
178 .RS 4
179 .\}
180 .nf
181 CLUSTER employees USING employees_ind;
182 .fi
183 .if n \{\
184 .RE
185 .\}
186 .PP
187 Cluster the
188 employees
189 table using the same index that was used before:
190 .sp
191 .if n \{\
192 .RS 4
193 .\}
194 .nf
195 CLUSTER employees;
196 .fi
197 .if n \{\
198 .RE
199 .\}
200 .PP
201 Cluster all tables in the database that have previously been clustered:
202 .sp
203 .if n \{\
204 .RS 4
205 .\}
206 .nf
207 CLUSTER;
208 .fi
209 .if n \{\
210 .RE
211 .\}
212 .SH "COMPATIBILITY"
213 .PP
214 There is no
215 \fBCLUSTER\fR
216 statement in the SQL standard\&.
217 .PP
218 The following syntax was used before
219 PostgreSQL
220 17 and is still supported:
221 .sp
222 .if n \{\
223 .RS 4
224 .\}
225 .nf
226 CLUSTER [ VERBOSE ] [ \fItable_name\fR [ USING \fIindex_name\fR ] ]
227 .fi
228 .if n \{\
229 .RE
230 .\}
231 .PP
232 The following syntax was used before
233 PostgreSQL
234 8\&.3 and is still supported:
235 .sp
236 .if n \{\
237 .RS 4
238 .\}
239 .nf
240 CLUSTER \fIindex_name\fR ON \fItable_name\fR
241 .fi
242 .if n \{\
243 .RE
244 .\}
245 .sp
246 .SH "SEE ALSO"
247 \fBclusterdb\fR(1), Section\ \&27.4.2