Home Explore Blog CI



postgresql

4th chunk of `doc/src/sgml/ref/cluster.sgml`
a56e52bfa46d9b95f1c1ffa2c08e35b783cd25c256c106d80000000100000c52
 method, but if the disk space requirement is
    intolerable, you can disable this choice by temporarily setting <xref
    linkend="guc-enable-sort"/> to <literal>off</literal>.
   </para>

   <para>
    It is advisable to set <xref linkend="guc-maintenance-work-mem"/> to
    a reasonably large value (but not more than the amount of RAM you can
    dedicate to the <command>CLUSTER</command> operation) before clustering.
   </para>

   <para>
    Because the planner records statistics about the ordering of
    tables, it is advisable to run <link linkend="sql-analyze"><command>ANALYZE</command></link>
    on the newly clustered table.
    Otherwise, the planner might make poor choices of query plans.
   </para>

   <para>
    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
    <command>CLUSTER</command> without any parameters, so that the desired tables
    are periodically reclustered.
   </para>

  <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>

   <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.  <command>CLUSTER</command> on a
    partitioned table cannot be executed inside a transaction block.
   </para>

 </refsect1>

 <refsect1>
  <title>Examples</title>

  <para>
   Cluster the table <literal>employees</literal> on the basis of
   its index <literal>employees_ind</literal>:
<programlisting>
CLUSTER employees USING employees_ind;
</programlisting>
  </para>

  <para>
   Cluster the <literal>employees</literal> table using the same
   index that was used before:
<programlisting>
CLUSTER employees;
</programlisting>
  </para>

  <para>
   Cluster all tables in the database that have previously been clustered:
<programlisting>
CLUSTER;
</programlisting></para>
 </refsect1>

 <refsect1>
  <title>Compatibility</title>

  <para>
   There is no <command>CLUSTER</command> statement in the SQL standard.
  </para>

  <para>
   The following syntax was used before <productname>PostgreSQL</productname>
   17 and is still supported:
<synopsis>
CLUSTER [ VERBOSE ] [ <replaceable class="parameter">table_name</replaceable> [ USING <replaceable class="parameter">index_name</replaceable> ] ]
</synopsis>
  </para>

  <para>
   The following syntax was used before <productname>PostgreSQL</productname>
   8.3 and is still supported:
<synopsis>
CLUSTER <replaceable class="parameter">index_name</replaceable> ON <replaceable class="parameter">table_name</replaceable>
</synopsis>
  </para>
 </refsect1>

 <refsect1>
  <title>See Also</title>

  <simplelist type="inline">
   <member><xref linkend="app-clusterdb"/></member>
   <member><xref linkend="cluster-progress-reporting"/></member>
  </simplelist>
 </refsect1>
</refentry>

Title: CLUSTER command: Usage, monitoring, partitioning, examples, and compatibility
Summary
This section details how to use the CLUSTER command, including disabling sorting for disk space concerns, setting maintenance memory, and analyzing tables post-clustering. It covers periodic reclustering, progress monitoring via pg_stat_progress_cluster, and clustering partitioned tables. Examples show clustering a specific table with an index, using a previously used index, and clustering all tables in a database. It notes the absence of CLUSTER in the SQL standard and lists compatible syntax from older PostgreSQL versions, as well as related documentation.