2 doc/src/sgml/ref/cluster.sgml
3 PostgreSQL documentation
6 <refentry id="SQL-CLUSTER">
8 <refentrytitle>CLUSTER</refentrytitle>
9 <manvolnum>7</manvolnum>
10 <refmiscinfo>SQL - Language Statements</refmiscinfo>
14 <refname>CLUSTER</refname>
15 <refpurpose>cluster a table according to an index</refpurpose>
18 <indexterm zone="sql-cluster">
19 <primary>CLUSTER</primary>
24 CLUSTER [VERBOSE] <replaceable class="PARAMETER">table_name</replaceable> [ USING <replaceable class="PARAMETER">index_name</replaceable> ]
30 <title>Description</title>
33 <command>CLUSTER</command> instructs <productname>PostgreSQL</productname>
34 to cluster the table specified
35 by <replaceable class="parameter">table_name</replaceable>
36 based on the index specified by
37 <replaceable class="parameter">index_name</replaceable>. The index must
38 already have been defined on
39 <replaceable class="parameter">table_name</replaceable>.
43 When a table is clustered, it is physically reordered
44 based on the index information. Clustering is a one-time operation:
45 when the table is subsequently updated, the changes are
46 not clustered. That is, no attempt is made to store new or
47 updated rows according to their index order. (If one wishes, one can
48 periodically recluster by issuing the command again. Also, setting
49 the table's <literal>FILLFACTOR</literal> storage parameter to less than
50 100% can aid in preserving cluster ordering during updates, since updated
51 rows are kept on the same page if enough space is available there.)
55 When a table is clustered, <productname>PostgreSQL</productname>
56 remembers which index it was clustered by. The form
57 <command>CLUSTER <replaceable class="parameter">table_name</replaceable></command>
58 reclusters the table using the same index as before. You can also
59 use the <literal>CLUSTER</literal> or <literal>SET WITHOUT CLUSTER</literal>
60 forms of <xref linkend="SQL-ALTERTABLE"> to set the index to be used for
61 future cluster operations, or to clear any previous setting.
65 <command>CLUSTER</command> without any parameter reclusters all the
66 previously-clustered tables in the current database that the calling user
67 owns, or all such tables if called by a superuser. This
68 form of <command>CLUSTER</command> cannot be executed inside a transaction
73 When a table is being clustered, an <literal>ACCESS
74 EXCLUSIVE</literal> lock is acquired on it. This prevents any other
75 database operations (both reads and writes) from operating on the
76 table until the <command>CLUSTER</command> is finished.
81 <title>Parameters</title>
85 <term><replaceable class="PARAMETER">table_name</replaceable></term>
88 The name (possibly schema-qualified) of a table.
94 <term><replaceable class="PARAMETER">index_name</replaceable></term>
103 <term><literal>VERBOSE</literal></term>
106 Prints a progress report as each table is clustered.
117 In cases where you are accessing single rows randomly
118 within a table, the actual order of the data in the
119 table is unimportant. However, if you tend to access some
120 data more than others, and there is an index that groups
121 them together, you will benefit from using <command>CLUSTER</command>.
122 If you are requesting a range of indexed values from a table, or a
123 single indexed value that has multiple rows that match,
124 <command>CLUSTER</command> will help because once the index identifies the
125 table page for the first row that matches, all other rows
126 that match are probably already on the same table page,
127 and so you save disk accesses and speed up the query.
131 <command>CLUSTER</> can re-sort the table using either an index scan
132 on the specified index, or (if the index is a b-tree) a sequential
133 scan followed by sorting. It will attempt to choose the method that
134 will be faster, based on planner cost parameters and available statistical
139 When an index scan is used, a temporary copy of the table is created that
140 contains the table data in the index order. Temporary copies of each
141 index on the table are created as well. Therefore, you need free space on
142 disk at least equal to the sum of the table size and the index sizes.
146 When a sequential scan and sort is used, a temporary sort file is
147 also created, so that the peak temporary space requirement is as much
148 as double the table size, plus the index sizes. This method is often
149 faster than the index scan method, but if the disk space requirement is
150 intolerable, you can disable this choice by temporarily setting <xref
151 linkend="guc-enable-sort"> to <literal>off</>.
155 It is advisable to set <xref linkend="guc-maintenance-work-mem"> to
156 a reasonably large value (but not more than the amount of RAM you can
157 dedicate to the <command>CLUSTER</> operation) before clustering.
161 Because the planner records statistics about the ordering of
162 tables, it is advisable to run <xref linkend="sql-analyze">
163 on the newly clustered table.
164 Otherwise, the planner might make poor choices of query plans.
168 Because <command>CLUSTER</command> remembers which indexes are clustered,
169 one can cluster the tables one wants clustered manually the first time,
170 then set up a periodic maintenance script that executes
171 <command>CLUSTER</> without any parameters, so that the desired tables
172 are periodically reclustered.
178 <title>Examples</title>
181 Cluster the table <literal>employees</literal> on the basis of
182 its index <literal>employees_ind</literal>:
184 CLUSTER employees USING employees_ind;
189 Cluster the <literal>employees</literal> table using the same
190 index that was used before:
197 Cluster all tables in the database that have previously been clustered:
200 </programlisting></para>
204 <title>Compatibility</title>
207 There is no <command>CLUSTER</command> statement in the SQL standard.
213 CLUSTER <replaceable class="PARAMETER">index_name</replaceable> ON <replaceable class="PARAMETER">table_name</replaceable>
215 is also supported for compatibility with pre-8.3 <productname>PostgreSQL</>
221 <title>See Also</title>
223 <simplelist type="inline">
224 <member><xref linkend="app-clusterdb"></member>