<!--
doc/src/sgml/ref/cluster.sgml
PostgreSQL documentation
-->
<refentry id="sql-cluster">
<indexterm zone="sql-cluster">
<primary>CLUSTER</primary>
</indexterm>
<refmeta>
<refentrytitle>CLUSTER</refentrytitle>
<manvolnum>7</manvolnum>
<refmiscinfo>SQL - Language Statements</refmiscinfo>
</refmeta>
<refnamediv>
<refname>CLUSTER</refname>
<refpurpose>cluster a table according to an index</refpurpose>
</refnamediv>
<refsynopsisdiv>
<synopsis>
CLUSTER [ ( <replaceable class="parameter">option</replaceable> [, ...] ) ] [ <replaceable class="parameter">table_name</replaceable> [ USING <replaceable class="parameter">index_name</replaceable> ] ]
<phrase>where <replaceable class="parameter">option</replaceable> can be one of:</phrase>
VERBOSE [ <replaceable class="parameter">boolean</replaceable> ]
</synopsis>
</refsynopsisdiv>
<refsect1>
<title>Description</title>
<para>
<command>CLUSTER</command> instructs <productname>PostgreSQL</productname>
to cluster the table specified
by <replaceable class="parameter">table_name</replaceable>
based on the index specified by
<replaceable class="parameter">index_name</replaceable>. The index must
already have been defined on
<replaceable class="parameter">table_name</replaceable>.
</para>
<para>
When a table is clustered, it is physically reordered
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
updated rows according to their index order. (If one wishes, one can
periodically recluster by issuing the command again. Also, setting
the table's <literal>fillfactor</literal> storage parameter to less than
100% can aid in preserving cluster ordering during updates, since updated
rows are kept on the same page if enough space is available there.)
</para>
<para>
When a table is clustered, <productname>PostgreSQL</productname>
remembers which index it was clustered by. The form
<command>CLUSTER <replaceable class="parameter">table_name</replaceable></command>
reclusters the table using the same index as before. You can also
use the <literal>CLUSTER</literal> or <literal>SET WITHOUT CLUSTER</literal>
forms of <link linkend="sql-altertable"><command>ALTER TABLE</command></link> to set the index to be used for
future cluster operations, or to clear any previous setting.
</para>
<para>
<command>CLUSTER</command> without a
<replaceable class="parameter">table_name</replaceable> reclusters all the
previously-clustered tables in the current database that the calling user
has privileges for. This form of <command>CLUSTER</command> cannot be
executed inside a transaction block.
</para>
<para>
When a table is being clustered, an <literal>ACCESS
EXCLUSIVE</literal> lock is acquired on it. This prevents any other
database operations (both reads and writes) from operating on the
table until the <command>CLUSTER</command> is finished.
</para>
</refsect1>
<refsect1>
<title>Parameters</title>
<variablelist>
<varlistentry>
<term><replaceable class="parameter">table_name</replaceable></term>
<listitem>
<para>
The name (possibly schema-qualified) of a table.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><replaceable class="parameter">index_name</replaceable></term>
<listitem>
<para>
The name of an index.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>VERBOSE</literal></term>
<listitem>
<para>
Prints a progress report as each table is clustered
at <literal>INFO</literal> level.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><replaceable class="parameter">boolean</replaceable></term>
<listitem>
<para>
Specifies whether the selected option should be turned on