1CLUSTER(7)               PostgreSQL 15.4 Documentation              CLUSTER(7)
2
3
4

NAME

6       CLUSTER - cluster a table according to an index
7

SYNOPSIS

9       CLUSTER [VERBOSE] table_name [ USING index_name ]
10       CLUSTER ( option [, ...] ) table_name [ USING index_name ]
11       CLUSTER [VERBOSE]
12
13       where option can be one of:
14
15           VERBOSE [ boolean ]
16

DESCRIPTION

18       CLUSTER instructs PostgreSQL to cluster the table specified by
19       table_name based on the index specified by index_name. The index must
20       already have been defined on table_name.
21
22       When a table is clustered, it is physically reordered based on the
23       index information. Clustering is a one-time operation: when the table
24       is subsequently updated, the changes are not clustered. That is, no
25       attempt is made to store new or updated rows according to their index
26       order. (If one wishes, one can periodically recluster by issuing the
27       command again. Also, setting the table's fillfactor storage parameter
28       to less than 100% can aid in preserving cluster ordering during
29       updates, since updated rows are kept on the same page if enough space
30       is available there.)
31
32       When a table is clustered, PostgreSQL remembers which index it was
33       clustered by. The form CLUSTER table_name reclusters the table using
34       the same index as before. You can also use the CLUSTER or SET WITHOUT
35       CLUSTER forms of ALTER TABLE to set the index to be used for future
36       cluster operations, or to clear any previous setting.
37
38       CLUSTER without any parameter reclusters all the previously-clustered
39       tables in the current database that the calling user owns, or all such
40       tables if called by a superuser. This form of CLUSTER cannot be
41       executed inside a transaction block.
42
43       When a table is being clustered, an ACCESS EXCLUSIVE lock is acquired
44       on it. This prevents any other database operations (both reads and
45       writes) from operating on the table until the CLUSTER is finished.
46

PARAMETERS

48       table_name
49           The name (possibly schema-qualified) of a table.
50
51       index_name
52           The name of an index.
53
54       VERBOSE
55           Prints a progress report as each table is clustered.
56
57       boolean
58           Specifies whether the selected option should be turned on or off.
59           You can write TRUE, ON, or 1 to enable the option, and FALSE, OFF,
60           or 0 to disable it. The boolean value can also be omitted, in which
61           case TRUE is assumed.
62

NOTES

64       In cases where you are accessing single rows randomly within a table,
65       the actual order of the data in the table is unimportant. However, if
66       you tend to access some data more than others, and there is an index
67       that groups them together, you will benefit from using CLUSTER. If you
68       are requesting a range of indexed values from a table, or a single
69       indexed value that has multiple rows that match, CLUSTER will help
70       because once the index identifies the table page for the first row that
71       matches, all other rows that match are probably already on the same
72       table page, and so you save disk accesses and speed up the query.
73
74       CLUSTER can re-sort the table using either an index scan on the
75       specified index, or (if the index is a b-tree) a sequential scan
76       followed by sorting. It will attempt to choose the method that will be
77       faster, based on planner cost parameters and available statistical
78       information.
79
80       When an index scan is used, a temporary copy of the table is created
81       that contains the table data in the index order. Temporary copies of
82       each index on the table are created as well. Therefore, you need free
83       space on disk at least equal to the sum of the table size and the index
84       sizes.
85
86       When a sequential scan and sort is used, a temporary sort file is also
87       created, so that the peak temporary space requirement is as much as
88       double the table size, plus the index sizes. This method is often
89       faster than the index scan method, but if the disk space requirement is
90       intolerable, you can disable this choice by temporarily setting
91       enable_sort to off.
92
93       It is advisable to set maintenance_work_mem to a reasonably large value
94       (but not more than the amount of RAM you can dedicate to the CLUSTER
95       operation) before clustering.
96
97       Because the planner records statistics about the ordering of tables, it
98       is advisable to run ANALYZE on the newly clustered table. Otherwise,
99       the planner might make poor choices of query plans.
100
101       Because CLUSTER remembers which indexes are clustered, one can cluster
102       the tables one wants clustered manually the first time, then set up a
103       periodic maintenance script that executes CLUSTER without any
104       parameters, so that the desired tables are periodically reclustered.
105
106       Each backend running CLUSTER will report its progress in the
107       pg_stat_progress_cluster view. See Section 28.4.4 for details.
108
109       Clustering a partitioned table clusters each of its partitions using
110       the partition of the specified partitioned index. When clustering a
111       partitioned table, the index may not be omitted.
112

EXAMPLES

114       Cluster the table employees on the basis of its index employees_ind:
115
116           CLUSTER employees USING employees_ind;
117
118       Cluster the employees table using the same index that was used before:
119
120           CLUSTER employees;
121
122       Cluster all tables in the database that have previously been clustered:
123
124           CLUSTER;
125

COMPATIBILITY

127       There is no CLUSTER statement in the SQL standard.
128
129       The syntax
130
131           CLUSTER index_name ON table_name
132
133       is also supported for compatibility with pre-8.3 PostgreSQL versions.
134

SEE ALSO

136       clusterdb(1), Section 28.4.4
137
138
139
140PostgreSQL 15.4                      2023                           CLUSTER(7)
Impressum