1CLUSTER(7) PostgreSQL 12.6 Documentation CLUSTER(7)
2
3
4
6 CLUSTER - cluster a table according to an index
7
9 CLUSTER [VERBOSE] table_name [ USING index_name ]
10 CLUSTER [VERBOSE]
11
13 CLUSTER instructs PostgreSQL to cluster the table specified by
14 table_name based on the index specified by index_name. The index must
15 already have been defined on table_name.
16
17 When a table is clustered, it is physically reordered based on the
18 index information. Clustering is a one-time operation: when the table
19 is subsequently updated, the changes are not clustered. That is, no
20 attempt is made to store new or updated rows according to their index
21 order. (If one wishes, one can periodically recluster by issuing the
22 command again. Also, setting the table's fillfactor storage parameter
23 to less than 100% can aid in preserving cluster ordering during
24 updates, since updated rows are kept on the same page if enough space
25 is available there.)
26
27 When a table is clustered, PostgreSQL remembers which index it was
28 clustered by. The form CLUSTER table_name reclusters the table using
29 the same index as before. You can also use the CLUSTER or SET WITHOUT
30 CLUSTER forms of ALTER TABLE (ALTER_TABLE(7)) to set the index to be
31 used for future cluster operations, or to clear any previous setting.
32
33 CLUSTER without any parameter reclusters all the previously-clustered
34 tables in the current database that the calling user owns, or all such
35 tables if called by a superuser. This form of CLUSTER cannot be
36 executed inside a transaction block.
37
38 When a table is being clustered, an ACCESS EXCLUSIVE lock is acquired
39 on it. This prevents any other database operations (both reads and
40 writes) from operating on the table until the CLUSTER is finished.
41
43 table_name
44 The name (possibly schema-qualified) of a table.
45
46 index_name
47 The name of an index.
48
49 VERBOSE
50 Prints a progress report as each table is clustered.
51
53 In cases where you are accessing single rows randomly within a table,
54 the actual order of the data in the table is unimportant. However, if
55 you tend to access some data more than others, and there is an index
56 that groups them together, you will benefit from using CLUSTER. If you
57 are requesting a range of indexed values from a table, or a single
58 indexed value that has multiple rows that match, CLUSTER will help
59 because once the index identifies the table page for the first row that
60 matches, all other rows that match are probably already on the same
61 table page, and so you save disk accesses and speed up the query.
62
63 CLUSTER can re-sort the table using either an index scan on the
64 specified index, or (if the index is a b-tree) a sequential scan
65 followed by sorting. It will attempt to choose the method that will be
66 faster, based on planner cost parameters and available statistical
67 information.
68
69 When an index scan is used, a temporary copy of the table is created
70 that contains the table data in the index order. Temporary copies of
71 each index on the table are created as well. Therefore, you need free
72 space on disk at least equal to the sum of the table size and the index
73 sizes.
74
75 When a sequential scan and sort is used, a temporary sort file is also
76 created, so that the peak temporary space requirement is as much as
77 double the table size, plus the index sizes. This method is often
78 faster than the index scan method, but if the disk space requirement is
79 intolerable, you can disable this choice by temporarily setting
80 enable_sort to off.
81
82 It is advisable to set maintenance_work_mem to a reasonably large value
83 (but not more than the amount of RAM you can dedicate to the CLUSTER
84 operation) before clustering.
85
86 Because the planner records statistics about the ordering of tables, it
87 is advisable to run ANALYZE(7) on the newly clustered table. Otherwise,
88 the planner might make poor choices of query plans.
89
90 Because CLUSTER remembers which indexes are clustered, one can cluster
91 the tables one wants clustered manually the first time, then set up a
92 periodic maintenance script that executes CLUSTER without any
93 parameters, so that the desired tables are periodically reclustered.
94
96 Cluster the table employees on the basis of its index employees_ind:
97
98 CLUSTER employees USING employees_ind;
99
100 Cluster the employees table using the same index that was used before:
101
102 CLUSTER employees;
103
104 Cluster all tables in the database that have previously been clustered:
105
106 CLUSTER;
107
109 There is no CLUSTER statement in the SQL standard.
110
111 The syntax
112
113 CLUSTER index_name ON table_name
114
115 is also supported for compatibility with pre-8.3 PostgreSQL versions.
116
118 clusterdb(1)
119
120
121
122PostgreSQL 12.6 2021 CLUSTER(7)