1999-07-22 17:09:15 +02:00
|
|
|
<!--
|
2010-09-20 22:08:53 +02:00
|
|
|
doc/src/sgml/ref/cluster.sgml
|
2001-12-08 04:24:40 +01:00
|
|
|
PostgreSQL documentation
|
1999-07-22 17:09:15 +02:00
|
|
|
-->
|
|
|
|
|
2017-10-20 03:16:39 +02:00
|
|
|
<refentry id="sql-cluster">
|
2014-02-24 03:25:35 +01:00
|
|
|
<indexterm zone="sql-cluster">
|
|
|
|
<primary>CLUSTER</primary>
|
|
|
|
</indexterm>
|
|
|
|
|
1999-07-06 19:16:42 +02:00
|
|
|
<refmeta>
|
2010-04-03 09:23:02 +02:00
|
|
|
<refentrytitle>CLUSTER</refentrytitle>
|
2008-11-14 11:22:48 +01:00
|
|
|
<manvolnum>7</manvolnum>
|
1999-07-06 19:16:42 +02:00
|
|
|
<refmiscinfo>SQL - Language Statements</refmiscinfo>
|
|
|
|
</refmeta>
|
2003-04-15 15:25:08 +02:00
|
|
|
|
1999-07-06 19:16:42 +02:00
|
|
|
<refnamediv>
|
2003-04-15 15:25:08 +02:00
|
|
|
<refname>CLUSTER</refname>
|
|
|
|
<refpurpose>cluster a table according to an index</refpurpose>
|
1998-12-29 03:24:47 +01:00
|
|
|
</refnamediv>
|
2003-04-15 15:25:08 +02:00
|
|
|
|
1999-07-06 19:16:42 +02:00
|
|
|
<refsynopsisdiv>
|
2003-04-15 15:25:08 +02:00
|
|
|
<synopsis>
|
2017-10-09 04:00:57 +02:00
|
|
|
CLUSTER [VERBOSE] <replaceable class="parameter">table_name</replaceable> [ USING <replaceable class="parameter">index_name</replaceable> ]
|
2020-12-03 02:13:21 +01:00
|
|
|
CLUSTER ( <replaceable class="parameter">option</replaceable> [, ...] ) <replaceable class="parameter">table_name</replaceable> [ USING <replaceable class="parameter">index_name</replaceable> ]
|
2008-11-24 09:46:04 +01:00
|
|
|
CLUSTER [VERBOSE]
|
2020-12-03 02:13:21 +01:00
|
|
|
|
|
|
|
<phrase>where <replaceable class="parameter">option</replaceable> can be one of:</phrase>
|
|
|
|
|
|
|
|
VERBOSE [ <replaceable class="parameter">boolean</replaceable> ]
|
2003-04-15 15:25:08 +02:00
|
|
|
</synopsis>
|
1999-07-06 19:16:42 +02:00
|
|
|
</refsynopsisdiv>
|
|
|
|
|
2003-04-15 15:25:08 +02:00
|
|
|
<refsect1>
|
|
|
|
<title>Description</title>
|
|
|
|
|
1999-07-06 19:16:42 +02:00
|
|
|
<para>
|
2010-02-07 21:48:13 +01:00
|
|
|
<command>CLUSTER</command> instructs <productname>PostgreSQL</productname>
|
2001-01-14 00:58:55 +01:00
|
|
|
to cluster the table specified
|
2009-09-19 12:23:27 +02:00
|
|
|
by <replaceable class="parameter">table_name</replaceable>
|
1999-07-06 19:16:42 +02:00
|
|
|
based on the index specified by
|
2009-09-19 12:23:27 +02:00
|
|
|
<replaceable class="parameter">index_name</replaceable>. The index must
|
2010-02-07 21:48:13 +01:00
|
|
|
already have been defined on
|
2009-09-19 12:23:27 +02:00
|
|
|
<replaceable class="parameter">table_name</replaceable>.
|
1999-07-06 19:16:42 +02:00
|
|
|
</para>
|
1999-07-22 17:09:15 +02:00
|
|
|
|
1999-07-06 19:16:42 +02:00
|
|
|
<para>
|
2001-01-14 00:58:55 +01:00
|
|
|
When a table is clustered, it is physically reordered
|
2002-08-11 04:43:57 +02:00
|
|
|
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
|
2007-04-08 04:07:35 +02:00
|
|
|
updated rows according to their index order. (If one wishes, one can
|
|
|
|
periodically recluster by issuing the command again. Also, setting
|
2014-11-11 13:08:21 +01:00
|
|
|
the table's <literal>fillfactor</literal> storage parameter to less than
|
2010-02-07 21:48:13 +01:00
|
|
|
100% can aid in preserving cluster ordering during updates, since updated
|
|
|
|
rows are kept on the same page if enough space is available there.)
|
1999-07-06 19:16:42 +02:00
|
|
|
</para>
|
|
|
|
|
2002-11-15 04:09:39 +01:00
|
|
|
<para>
|
|
|
|
When a table is clustered, <productname>PostgreSQL</productname>
|
2007-04-08 04:07:35 +02:00
|
|
|
remembers which index it was clustered by. The form
|
2009-09-19 12:23:27 +02:00
|
|
|
<command>CLUSTER <replaceable class="parameter">table_name</replaceable></command>
|
2010-05-11 18:07:42 +02:00
|
|
|
reclusters the table using the same index as before. You can also
|
|
|
|
use the <literal>CLUSTER</literal> or <literal>SET WITHOUT CLUSTER</literal>
|
Improve <xref> vs. <command> formatting in the documentation
SQL commands are generally marked up as <command>, except when a link
to a reference page is used using <xref>. But the latter doesn't
create monospace markup, so this looks strange especially when a
paragraph contains a mix of links and non-links.
We considered putting <command> in the <refentrytitle> on the target
side, but that creates some formatting side effects elsewhere.
Generally, it seems safer to solve this on the link source side.
We can't put the <xref> inside the <command>; the DTD doesn't allow
this. DocBook 5 would allow the <command> to have the linkend
attribute itself, but we are not there yet.
So to solve this for now, convert the <xref>s to <link> plus
<command>. This gives the correct look and also gives some more
flexibility what we can put into the link text (e.g., subcommands or
other clauses). In the future, these could then be converted to
DocBook 5 style.
I haven't converted absolutely all xrefs to SQL command reference
pages, only those where we care about the appearance of the link text
or where it was otherwise appropriate to make the appearance match a
bit better. Also in some cases, the links where repetitive, so in
those cases the links where just removed and replaced by a plain
<command>. In cases where we just want the link and don't
specifically care about the generated link text (typically phrased
"for further information see <xref ...>") the xref is kept.
Reported-by: Dagfinn Ilmari Mannsåker <ilmari@ilmari.org>
Discussion: https://www.postgresql.org/message-id/flat/87o8pco34z.fsf@wibble.ilmari.org
2020-10-03 16:16:51 +02:00
|
|
|
forms of <link linkend="sql-altertable"><command>ALTER TABLE</command></link> to set the index to be used for
|
2010-05-11 18:07:42 +02:00
|
|
|
future cluster operations, or to clear any previous setting.
|
2002-11-15 04:09:39 +01:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2007-04-08 04:07:35 +02:00
|
|
|
<command>CLUSTER</command> without any parameter reclusters all the
|
|
|
|
previously-clustered tables in the current database that the calling user
|
2022-12-14 02:33:28 +01:00
|
|
|
owns or has the <literal>MAINTAIN</literal> privilege for, or all such tables
|
|
|
|
if called by a superuser or a role with privileges of the
|
|
|
|
<link linkend="predefined-roles-table"><literal>pg_maintain</literal></link>
|
|
|
|
role. This form of <command>CLUSTER</command> cannot be
|
|
|
|
executed inside a transaction block.
|
2002-11-15 04:09:39 +01:00
|
|
|
</para>
|
|
|
|
|
2003-02-19 05:06:28 +01:00
|
|
|
<para>
|
|
|
|
When a table is being clustered, an <literal>ACCESS
|
|
|
|
EXCLUSIVE</literal> lock is acquired on it. This prevents any other
|
2003-11-02 13:59:54 +01:00
|
|
|
database operations (both reads and writes) from operating on the
|
|
|
|
table until the <command>CLUSTER</command> is finished.
|
2003-02-19 05:06:28 +01:00
|
|
|
</para>
|
2003-04-15 15:25:08 +02:00
|
|
|
</refsect1>
|
|
|
|
|
|
|
|
<refsect1>
|
2003-09-12 02:12:47 +02:00
|
|
|
<title>Parameters</title>
|
2003-04-15 15:25:08 +02:00
|
|
|
|
|
|
|
<variablelist>
|
|
|
|
<varlistentry>
|
2017-10-09 04:00:57 +02:00
|
|
|
<term><replaceable class="parameter">table_name</replaceable></term>
|
2003-04-15 15:25:08 +02:00
|
|
|
<listitem>
|
|
|
|
<para>
|
2007-04-08 02:26:34 +02:00
|
|
|
The name (possibly schema-qualified) of a table.
|
2003-04-15 15:25:08 +02:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
<varlistentry>
|
2017-10-09 04:00:57 +02:00
|
|
|
<term><replaceable class="parameter">index_name</replaceable></term>
|
2003-04-15 15:25:08 +02:00
|
|
|
<listitem>
|
|
|
|
<para>
|
2007-04-08 02:26:34 +02:00
|
|
|
The name of an index.
|
2003-04-15 15:25:08 +02:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
2008-11-24 09:46:04 +01:00
|
|
|
|
|
|
|
<varlistentry>
|
|
|
|
<term><literal>VERBOSE</literal></term>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
Prints a progress report as each table is clustered.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
2020-12-03 02:13:21 +01:00
|
|
|
|
|
|
|
<varlistentry>
|
|
|
|
<term><replaceable class="parameter">boolean</replaceable></term>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
Specifies whether the selected option should be turned on or off.
|
|
|
|
You can write <literal>TRUE</literal>, <literal>ON</literal>, or
|
|
|
|
<literal>1</literal> to enable the option, and <literal>FALSE</literal>,
|
|
|
|
<literal>OFF</literal>, or <literal>0</literal> to disable it. The
|
|
|
|
<replaceable class="parameter">boolean</replaceable> value can also
|
|
|
|
be omitted, in which case <literal>TRUE</literal> is assumed.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
2003-04-15 15:25:08 +02:00
|
|
|
</variablelist>
|
|
|
|
</refsect1>
|
|
|
|
|
|
|
|
<refsect1>
|
|
|
|
<title>Notes</title>
|
1999-07-06 19:16:42 +02:00
|
|
|
|
|
|
|
<para>
|
|
|
|
In cases where you are accessing single rows randomly
|
2003-04-15 15:25:08 +02:00
|
|
|
within a table, the actual order of the data in the
|
1999-07-06 19:16:42 +02:00
|
|
|
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 <command>CLUSTER</command>.
|
2003-04-15 15:25:08 +02:00
|
|
|
If you are requesting a range of indexed values from a table, or a
|
1999-07-06 19:16:42 +02:00
|
|
|
single indexed value that has multiple rows that match,
|
|
|
|
<command>CLUSTER</command> will help because once the index identifies the
|
2006-11-04 20:03:51 +01:00
|
|
|
table page for the first row that matches, all other rows
|
|
|
|
that match are probably already on the same table page,
|
2003-04-15 15:25:08 +02:00
|
|
|
and so you save disk accesses and speed up the query.
|
1999-07-06 19:16:42 +02:00
|
|
|
</para>
|
1998-10-30 20:34:40 +01:00
|
|
|
|
1999-07-06 19:16:42 +02:00
|
|
|
<para>
|
2017-10-09 03:44:17 +02:00
|
|
|
<command>CLUSTER</command> can re-sort the table using either an index scan
|
2010-10-08 02:00:28 +02:00
|
|
|
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.
|
2002-08-11 04:43:57 +02:00
|
|
|
</para>
|
|
|
|
|
2002-11-15 04:09:39 +01:00
|
|
|
<para>
|
2011-05-19 00:14:45 +02:00
|
|
|
When an index scan is used, a temporary copy of the table is created that
|
2010-10-08 02:00:28 +02:00
|
|
|
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.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
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
|
2011-05-19 00:14:45 +02:00
|
|
|
faster than the index scan method, but if the disk space requirement is
|
2010-10-08 02:00:28 +02:00
|
|
|
intolerable, you can disable this choice by temporarily setting <xref
|
2017-11-23 15:39:47 +01:00
|
|
|
linkend="guc-enable-sort"/> to <literal>off</literal>.
|
2010-10-08 02:00:28 +02:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2017-11-23 15:39:47 +01:00
|
|
|
It is advisable to set <xref linkend="guc-maintenance-work-mem"/> to
|
2010-10-08 02:00:28 +02:00
|
|
|
a reasonably large value (but not more than the amount of RAM you can
|
2017-10-09 03:44:17 +02:00
|
|
|
dedicate to the <command>CLUSTER</command> operation) before clustering.
|
2002-08-11 04:43:57 +02:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2004-03-23 14:21:41 +01:00
|
|
|
Because the planner records statistics about the ordering of
|
Improve <xref> vs. <command> formatting in the documentation
SQL commands are generally marked up as <command>, except when a link
to a reference page is used using <xref>. But the latter doesn't
create monospace markup, so this looks strange especially when a
paragraph contains a mix of links and non-links.
We considered putting <command> in the <refentrytitle> on the target
side, but that creates some formatting side effects elsewhere.
Generally, it seems safer to solve this on the link source side.
We can't put the <xref> inside the <command>; the DTD doesn't allow
this. DocBook 5 would allow the <command> to have the linkend
attribute itself, but we are not there yet.
So to solve this for now, convert the <xref>s to <link> plus
<command>. This gives the correct look and also gives some more
flexibility what we can put into the link text (e.g., subcommands or
other clauses). In the future, these could then be converted to
DocBook 5 style.
I haven't converted absolutely all xrefs to SQL command reference
pages, only those where we care about the appearance of the link text
or where it was otherwise appropriate to make the appearance match a
bit better. Also in some cases, the links where repetitive, so in
those cases the links where just removed and replaced by a plain
<command>. In cases where we just want the link and don't
specifically care about the generated link text (typically phrased
"for further information see <xref ...>") the xref is kept.
Reported-by: Dagfinn Ilmari Mannsåker <ilmari@ilmari.org>
Discussion: https://www.postgresql.org/message-id/flat/87o8pco34z.fsf@wibble.ilmari.org
2020-10-03 16:16:51 +02:00
|
|
|
tables, it is advisable to run <link linkend="sql-analyze"><command>ANALYZE</command></link>
|
2010-04-03 09:23:02 +02:00
|
|
|
on the newly clustered table.
|
Update reference documentation on may/can/might:
Standard English uses "may", "can", and "might" in different ways:
may - permission, "You may borrow my rake."
can - ability, "I can lift that log."
might - possibility, "It might rain today."
Unfortunately, in conversational English, their use is often mixed, as
in, "You may use this variable to do X", when in fact, "can" is a better
choice. Similarly, "It may crash" is better stated, "It might crash".
2007-02-01 00:26:05 +01:00
|
|
|
Otherwise, the planner might make poor choices of query plans.
|
2002-08-11 04:43:57 +02:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2010-10-08 02:00:28 +02:00
|
|
|
Because <command>CLUSTER</command> 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
|
2017-10-09 03:44:17 +02:00
|
|
|
<command>CLUSTER</command> without any parameters, so that the desired tables
|
2010-10-08 02:00:28 +02:00
|
|
|
are periodically reclustered.
|
2002-08-10 22:43:46 +02:00
|
|
|
</para>
|
2010-10-08 02:00:28 +02:00
|
|
|
|
2021-03-05 06:58:16 +01:00
|
|
|
<para>
|
|
|
|
Each backend running <command>CLUSTER</command> will report its progress
|
|
|
|
in the <structname>pg_stat_progress_cluster</structname> view. See
|
|
|
|
<xref linkend="cluster-progress-reporting"/> for details.
|
|
|
|
</para>
|
2022-04-02 19:08:34 +02:00
|
|
|
|
|
|
|
<para>
|
|
|
|
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.
|
|
|
|
</para>
|
|
|
|
|
1998-12-29 03:24:47 +01:00
|
|
|
</refsect1>
|
1999-07-06 19:16:42 +02:00
|
|
|
|
2003-04-15 15:25:08 +02:00
|
|
|
<refsect1>
|
|
|
|
<title>Examples</title>
|
|
|
|
|
1999-07-06 19:16:42 +02:00
|
|
|
<para>
|
2003-04-15 15:25:08 +02:00
|
|
|
Cluster the table <literal>employees</literal> on the basis of
|
2007-04-08 02:26:34 +02:00
|
|
|
its index <literal>employees_ind</literal>:
|
2003-04-15 15:25:08 +02:00
|
|
|
<programlisting>
|
2007-04-08 02:26:34 +02:00
|
|
|
CLUSTER employees USING employees_ind;
|
2003-04-15 15:25:08 +02:00
|
|
|
</programlisting>
|
|
|
|
</para>
|
|
|
|
|
2002-11-15 04:09:39 +01:00
|
|
|
<para>
|
2004-03-23 14:21:41 +01:00
|
|
|
Cluster the <literal>employees</literal> table using the same
|
2003-02-19 05:06:28 +01:00
|
|
|
index that was used before:
|
2003-04-15 15:25:08 +02:00
|
|
|
<programlisting>
|
2007-04-08 02:26:34 +02:00
|
|
|
CLUSTER employees;
|
2003-04-15 15:25:08 +02:00
|
|
|
</programlisting>
|
|
|
|
</para>
|
|
|
|
|
2002-11-15 04:09:39 +01:00
|
|
|
<para>
|
2005-01-04 01:39:53 +01:00
|
|
|
Cluster all tables in the database that have previously been clustered:
|
2003-04-15 15:25:08 +02:00
|
|
|
<programlisting>
|
2002-11-18 18:12:07 +01:00
|
|
|
CLUSTER;
|
2011-08-07 09:49:45 +02:00
|
|
|
</programlisting></para>
|
1999-07-06 19:16:42 +02:00
|
|
|
</refsect1>
|
|
|
|
|
2003-04-15 15:25:08 +02:00
|
|
|
<refsect1>
|
|
|
|
<title>Compatibility</title>
|
|
|
|
|
|
|
|
<para>
|
2007-04-08 04:07:35 +02:00
|
|
|
There is no <command>CLUSTER</command> statement in the SQL standard.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
The syntax
|
2007-04-08 02:26:34 +02:00
|
|
|
<synopsis>
|
2017-10-09 04:00:57 +02:00
|
|
|
CLUSTER <replaceable class="parameter">index_name</replaceable> ON <replaceable class="parameter">table_name</replaceable>
|
2007-04-08 02:26:34 +02:00
|
|
|
</synopsis>
|
2017-10-09 03:44:17 +02:00
|
|
|
is also supported for compatibility with pre-8.3 <productname>PostgreSQL</productname>
|
2007-04-08 04:07:35 +02:00
|
|
|
versions.
|
2003-04-15 15:25:08 +02:00
|
|
|
</para>
|
1998-05-13 07:34:00 +02:00
|
|
|
</refsect1>
|
2003-02-19 05:06:28 +01:00
|
|
|
|
|
|
|
<refsect1>
|
|
|
|
<title>See Also</title>
|
|
|
|
|
|
|
|
<simplelist type="inline">
|
2017-11-23 15:39:47 +01:00
|
|
|
<member><xref linkend="app-clusterdb"/></member>
|
2021-03-05 06:58:16 +01:00
|
|
|
<member><xref linkend="cluster-progress-reporting"/></member>
|
2003-02-19 05:06:28 +01:00
|
|
|
</simplelist>
|
|
|
|
</refsect1>
|
1999-07-06 19:16:42 +02:00
|
|
|
</refentry>
|