Debian Codegraph @

doc/src/sgml/ref/cluster.sgml

raw
1 <!--
2 doc/src/sgml/ref/cluster.sgml
3 PostgreSQL documentation
4 -->
5
6 <refentry id="sql-cluster">
7 <indexterm zone="sql-cluster">
8 <primary>CLUSTER</primary>
9 </indexterm>
10
11 <refmeta>
12 <refentrytitle>CLUSTER</refentrytitle>
13 <manvolnum>7</manvolnum>
14 <refmiscinfo>SQL - Language Statements</refmiscinfo>
15 </refmeta>
16
17 <refnamediv>
18 <refname>CLUSTER</refname>
19 <refpurpose>cluster a table according to an index</refpurpose>
20 </refnamediv>
21
22 <refsynopsisdiv>
23 <synopsis>
24 CLUSTER [ ( <replaceable class="parameter">option</replaceable> [, ...] ) ] [ <replaceable class="parameter">table_name</replaceable> [ USING <replaceable class="parameter">index_name</replaceable> ] ]
25
26 <phrase>where <replaceable class="parameter">option</replaceable> can be one of:</phrase>
27
28 VERBOSE [ <replaceable class="parameter">boolean</replaceable> ]
29 </synopsis>
30 </refsynopsisdiv>
31
32 <refsect1>
33 <title>Description</title>
34
35 <para>
36 <command>CLUSTER</command> instructs <productname>PostgreSQL</productname>
37 to cluster the table specified
38 by <replaceable class="parameter">table_name</replaceable>
39 based on the index specified by
40 <replaceable class="parameter">index_name</replaceable>. The index must
41 already have been defined on
42 <replaceable class="parameter">table_name</replaceable>.
43 </para>
44
45 <para>
46 When a table is clustered, it is physically reordered
47 based on the index information. Clustering is a one-time operation:
48 when the table is subsequently updated, the changes are
49 not clustered. That is, no attempt is made to store new or
50 updated rows according to their index order. (If one wishes, one can
51 periodically recluster by issuing the command again. Also, setting
52 the table's <literal>fillfactor</literal> storage parameter to less than
53 100% can aid in preserving cluster ordering during updates, since updated
54 rows are kept on the same page if enough space is available there.)
55 </para>
56
57 <para>
58 When a table is clustered, <productname>PostgreSQL</productname>
59 remembers which index it was clustered by. The form
60 <command>CLUSTER <replaceable class="parameter">table_name</replaceable></command>
61 reclusters the table using the same index as before. You can also
62 use the <literal>CLUSTER</literal> or <literal>SET WITHOUT CLUSTER</literal>
63 forms of <link linkend="sql-altertable"><command>ALTER TABLE</command></link> to set the index to be used for
64 future cluster operations, or to clear any previous setting.
65 </para>
66
67 <para>
68 <command>CLUSTER</command> without a
69 <replaceable class="parameter">table_name</replaceable> reclusters all the
70 previously-clustered tables in the current database that the calling user
71 has privileges for. This form of <command>CLUSTER</command> cannot be
72 executed inside a transaction block.
73 </para>
74
75 <para>
76 When a table is being clustered, an <literal>ACCESS
77 EXCLUSIVE</literal> lock is acquired on it. This prevents any other
78 database operations (both reads and writes) from operating on the
79 table until the <command>CLUSTER</command> is finished.
80 </para>
81 </refsect1>
82
83 <refsect1>
84 <title>Parameters</title>
85
86 <variablelist>
87 <varlistentry>
88 <term><replaceable class="parameter">table_name</replaceable></term>
89 <listitem>
90 <para>
91 The name (possibly schema-qualified) of a table.
92 </para>
93 </listitem>
94 </varlistentry>
95
96 <varlistentry>
97 <term><replaceable class="parameter">index_name</replaceable></term>
98 <listitem>
99 <para>
100 The name of an index.
101 </para>
102 </listitem>
103 </varlistentry>
104
105 <varlistentry>
106 <term><literal>VERBOSE</literal></term>
107 <listitem>
108 <para>
109 Prints a progress report as each table is clustered
110 at <literal>INFO</literal> level.
111 </para>
112 </listitem>
113 </varlistentry>
114
115 <varlistentry>
116 <term><replaceable class="parameter">boolean</replaceable></term>
117 <listitem>
118 <para>
119 Specifies whether the selected option should be turned on or off.
120 You can write <literal>TRUE</literal>, <literal>ON</literal>, or
121 <literal>1</literal> to enable the option, and <literal>FALSE</literal>,
122 <literal>OFF</literal>, or <literal>0</literal> to disable it. The
123 <replaceable class="parameter">boolean</replaceable> value can also
124 be omitted, in which case <literal>TRUE</literal> is assumed.
125 </para>
126 </listitem>
127 </varlistentry>
128 </variablelist>
129 </refsect1>
130
131 <refsect1>
132 <title>Notes</title>
133
134 <para>
135 To cluster a table, one must have the <literal>MAINTAIN</literal> privilege
136 on the table.
137 </para>
138
139 <para>
140 In cases where you are accessing single rows randomly
141 within a table, the actual order of the data in the
142 table is unimportant. However, if you tend to access some
143 data more than others, and there is an index that groups
144 them together, you will benefit from using <command>CLUSTER</command>.
145 If you are requesting a range of indexed values from a table, or a
146 single indexed value that has multiple rows that match,
147 <command>CLUSTER</command> will help because once the index identifies the
148 table page for the first row that matches, all other rows
149 that match are probably already on the same table page,
150 and so you save disk accesses and speed up the query.
151 </para>
152
153 <para>
154 <command>CLUSTER</command> can re-sort the table using either an index scan
155 on the specified index, or (if the index is a b-tree) a sequential
156 scan followed by sorting. It will attempt to choose the method that
157 will be faster, based on planner cost parameters and available statistical
158 information.
159 </para>
160
161 <para>
162 While <command>CLUSTER</command> is running, the <xref
163 linkend="guc-search-path"/> is temporarily changed to <literal>pg_catalog,
164 pg_temp</literal>.
165 </para>
166
167 <para>
168 When an index scan is used, a temporary copy of the table is created that
169 contains the table data in the index order. Temporary copies of each
170 index on the table are created as well. Therefore, you need free space on
171 disk at least equal to the sum of the table size and the index sizes.
172 </para>
173
174 <para>
175 When a sequential scan and sort is used, a temporary sort file is
176 also created, so that the peak temporary space requirement is as much
177 as double the table size, plus the index sizes. This method is often
178 faster than the index scan method, but if the disk space requirement is
179 intolerable, you can disable this choice by temporarily setting <xref
180 linkend="guc-enable-sort"/> to <literal>off</literal>.
181 </para>
182
183 <para>
184 It is advisable to set <xref linkend="guc-maintenance-work-mem"/> to
185 a reasonably large value (but not more than the amount of RAM you can
186 dedicate to the <command>CLUSTER</command> operation) before clustering.
187 </para>
188
189 <para>
190 Because the planner records statistics about the ordering of
191 tables, it is advisable to run <link linkend="sql-analyze"><command>ANALYZE</command></link>
192 on the newly clustered table.
193 Otherwise, the planner might make poor choices of query plans.
194 </para>
195
196 <para>
197 Because <command>CLUSTER</command> remembers which indexes are clustered,
198 one can cluster the tables one wants clustered manually the first time,
199 then set up a periodic maintenance script that executes
200 <command>CLUSTER</command> without any parameters, so that the desired tables
201 are periodically reclustered.
202 </para>
203
204 <para>
205 Each backend running <command>CLUSTER</command> will report its progress
206 in the <structname>pg_stat_progress_cluster</structname> view. See
207 <xref linkend="cluster-progress-reporting"/> for details.
208 </para>
209
210 <para>
211 Clustering a partitioned table clusters each of its partitions using the
212 partition of the specified partitioned index. When clustering a partitioned
213 table, the index may not be omitted. <command>CLUSTER</command> on a
214 partitioned table cannot be executed inside a transaction block.
215 </para>
216
217 </refsect1>
218
219 <refsect1>
220 <title>Examples</title>
221
222 <para>
223 Cluster the table <literal>employees</literal> on the basis of
224 its index <literal>employees_ind</literal>:
225 <programlisting>
226 CLUSTER employees USING employees_ind;
227 </programlisting>
228 </para>
229
230 <para>
231 Cluster the <literal>employees</literal> table using the same
232 index that was used before:
233 <programlisting>
234 CLUSTER employees;
235 </programlisting>
236 </para>
237
238 <para>
239 Cluster all tables in the database that have previously been clustered:
240 <programlisting>
241 CLUSTER;
242 </programlisting></para>
243 </refsect1>
244
245 <refsect1>
246 <title>Compatibility</title>
247
248 <para>
249 There is no <command>CLUSTER</command> statement in the SQL standard.
250 </para>
251
252 <para>
253 The following syntax was used before <productname>PostgreSQL</productname>
254 17 and is still supported:
255 <synopsis>
256 CLUSTER [ VERBOSE ] [ <replaceable class="parameter">table_name</replaceable> [ USING <replaceable class="parameter">index_name</replaceable> ] ]
257 </synopsis>
258 </para>
259
260 <para>
261 The following syntax was used before <productname>PostgreSQL</productname>
262 8.3 and is still supported:
263 <synopsis>
264 CLUSTER <replaceable class="parameter">index_name</replaceable> ON <replaceable class="parameter">table_name</replaceable>
265 </synopsis>
266 </para>
267 </refsect1>
268
269 <refsect1>
270 <title>See Also</title>
271
272 <simplelist type="inline">
273 <member><xref linkend="app-clusterdb"/></member>
274 <member><xref linkend="cluster-progress-reporting"/></member>
275 </simplelist>
276 </refsect1>
277 </refentry>