3 .\" Author: The PostgreSQL Global Development Group
4 .\" Generator: DocBook XSL Stylesheets vsnapshot <http://docbook.sf.net/>
6 .\" Manual: PostgreSQL 18.0 Documentation
7 .\" Source: PostgreSQL 18.0
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 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20 .\" -----------------------------------------------------------------
21 .\" * set default formatting
22 .\" -----------------------------------------------------------------
23 .\" disable hyphenation
25 .\" disable justification (adjust text to left margin only)
27 .\" -----------------------------------------------------------------
28 .\" * MAIN CONTENT STARTS HERE *
29 .\" -----------------------------------------------------------------
31 CLUSTER \- cluster a table according to an index
35 CLUSTER [ ( \fIoption\fR [, \&.\&.\&.] ) ] [ \fItable_name\fR [ USING \fIindex_name\fR ] ]
37 where \fIoption\fR can be one of:
39 VERBOSE [ \fIboolean\fR ]
46 to cluster the table specified by
48 based on the index specified by
49 \fIindex_name\fR\&. The index must already have been defined on
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
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\&.)
56 When a table is clustered,
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
66 to set the index to be used for future cluster operations, or to clear any previous setting\&.
71 reclusters all the previously\-clustered tables in the current database that the calling user has privileges for\&. This form of
73 cannot be executed inside a transaction block\&.
75 When a table is being clustered, an
77 lock is acquired on it\&. This prevents any other database operations (both reads and writes) from operating on the table until the
84 The name (possibly schema\-qualified) of a table\&.
89 The name of an index\&.
94 Prints a progress report as each table is clustered at
101 Specifies whether the selected option should be turned on or off\&. You can write
105 to enable the option, and
111 value can also be omitted, in which case
117 To cluster a table, one must have the
119 privilege on the table\&.
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,
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\&.
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\&.
133 is temporarily changed to
134 pg_catalog, pg_temp\&.
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\&.
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
143 It is advisable to set
145 to a reasonably large value (but not more than the amount of RAM you can dedicate to the
147 operation) before clustering\&.
149 Because the planner records statistics about the ordering of tables, it is advisable to run
151 on the newly clustered table\&. Otherwise, the planner might make poor choices of query plans\&.
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
157 without any parameters, so that the desired tables are periodically reclustered\&.
161 will report its progress in the
162 pg_stat_progress_cluster
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\&.
169 on a partitioned table cannot be executed inside a transaction block\&.
174 on the basis of its index
181 CLUSTER employees USING employees_ind;
189 table using the same index that was used before:
201 Cluster all tables in the database that have previously been clustered:
216 statement in the SQL standard\&.
218 The following syntax was used before
220 17 and is still supported:
226 CLUSTER [ VERBOSE ] [ \fItable_name\fR [ USING \fIindex_name\fR ] ]
232 The following syntax was used before
234 8\&.3 and is still supported:
240 CLUSTER \fIindex_name\fR ON \fItable_name\fR
247 \fBclusterdb\fR(1), Section\ \&27.4.2