Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes15kdownloads
sql-cluster.html139 linesDownload Raw Back to html
1<?xml version="1.0" encoding="UTF-8" standalone="no"?>2<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>CLUSTER</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><link rel="prev" href="sql-close.html" title="CLOSE" /><link rel="next" href="sql-comment.html" title="COMMENT" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">CLUSTER</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="sql-close.html" title="CLOSE">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="sql-commands.html" title="SQL Commands">Up</a></td><th width="60%" align="center">SQL Commands</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 16.3 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="sql-comment.html" title="COMMENT">Next</a></td></tr></table><hr /></div><div class="refentry" id="SQL-CLUSTER"><div class="titlepage"></div><a id="id-1.9.3.51.1" class="indexterm"></a><div class="refnamediv"><h2><span class="refentrytitle">CLUSTER</span></h2><p>CLUSTER — cluster a table according to an index</p></div><div class="refsynopsisdiv"><h2>Synopsis</h2><pre class="synopsis">3CLUSTER [VERBOSE] <em class="replaceable"><code>table_name</code></em> [ USING <em class="replaceable"><code>index_name</code></em> ]4CLUSTER ( <em class="replaceable"><code>option</code></em> [, ...] ) <em class="replaceable"><code>table_name</code></em> [ USING <em class="replaceable"><code>index_name</code></em> ]5CLUSTER [VERBOSE]6 7<span class="phrase">where <em class="replaceable"><code>option</code></em> can be one of:</span>8 9    VERBOSE [ <em class="replaceable"><code>boolean</code></em> ]10</pre></div><div class="refsect1" id="id-1.9.3.51.5"><h2>Description</h2><p>11   <code class="command">CLUSTER</code> instructs <span class="productname">PostgreSQL</span>12   to cluster the table specified13   by <em class="replaceable"><code>table_name</code></em>14   based on the index specified by15   <em class="replaceable"><code>index_name</code></em>. The index must16   already have been defined on17   <em class="replaceable"><code>table_name</code></em>.18  </p><p>19   When a table is clustered, it is physically reordered20   based on the index information. Clustering is a one-time operation:21   when the table is subsequently updated, the changes are22   not clustered.  That is, no attempt is made to store new or23   updated rows according to their index order.  (If one wishes, one can24   periodically recluster by issuing the command again.  Also, setting25   the table's <code class="literal">fillfactor</code> storage parameter to less than26   100% can aid in preserving cluster ordering during updates, since updated27   rows are kept on the same page if enough space is available there.)28  </p><p>29   When a table is clustered, <span class="productname">PostgreSQL</span>30   remembers which index it was clustered by.  The form31   <code class="command">CLUSTER <em class="replaceable"><code>table_name</code></em></code>32   reclusters the table using the same index as before.  You can also33   use the <code class="literal">CLUSTER</code> or <code class="literal">SET WITHOUT CLUSTER</code>34   forms of <a class="link" href="sql-altertable.html" title="ALTER TABLE"><code class="command">ALTER TABLE</code></a> to set the index to be used for35   future cluster operations, or to clear any previous setting.36  </p><p>37   <code class="command">CLUSTER</code> without a38   <em class="replaceable"><code>table_name</code></em> reclusters all the39   previously-clustered tables in the current database that the calling user40   owns, or all such tables if called by a superuser.  This41   form of <code class="command">CLUSTER</code> cannot be executed inside a transaction42   block.43  </p><p>44   When a table is being clustered, an <code class="literal">ACCESS45   EXCLUSIVE</code> lock is acquired on it. This prevents any other46   database operations (both reads and writes) from operating on the47   table until the <code class="command">CLUSTER</code> is finished.48  </p></div><div class="refsect1" id="id-1.9.3.51.6"><h2>Parameters</h2><div class="variablelist"><dl class="variablelist"><dt><span class="term"><em class="replaceable"><code>table_name</code></em></span></dt><dd><p>49      The name (possibly schema-qualified) of a table.50     </p></dd><dt><span class="term"><em class="replaceable"><code>index_name</code></em></span></dt><dd><p>51      The name of an index.52     </p></dd><dt><span class="term"><code class="literal">VERBOSE</code></span></dt><dd><p>53      Prints a progress report as each table is clustered.54     </p></dd><dt><span class="term"><em class="replaceable"><code>boolean</code></em></span></dt><dd><p>55      Specifies whether the selected option should be turned on or off.56      You can write <code class="literal">TRUE</code>, <code class="literal">ON</code>, or57      <code class="literal">1</code> to enable the option, and <code class="literal">FALSE</code>,58      <code class="literal">OFF</code>, or <code class="literal">0</code> to disable it.  The59      <em class="replaceable"><code>boolean</code></em> value can also60      be omitted, in which case <code class="literal">TRUE</code> is assumed.61     </p></dd></dl></div></div><div class="refsect1" id="id-1.9.3.51.7"><h2>Notes</h2><p>62    In cases where you are accessing single rows randomly63    within a table, the actual order of the data in the64    table is unimportant. However, if you tend to access some65    data more than others, and there is an index that groups66    them together, you will benefit from using <code class="command">CLUSTER</code>.67    If you are requesting a range of indexed values from a table, or a68    single indexed value that has multiple rows that match,69    <code class="command">CLUSTER</code> will help because once the index identifies the70    table page for the first row that matches, all other rows71    that match are probably already on the same table page,72    and so you save disk accesses and speed up the query.73   </p><p>74    <code class="command">CLUSTER</code> can re-sort the table using either an index scan75    on the specified index, or (if the index is a b-tree) a sequential76    scan followed by sorting.  It will attempt to choose the method that77    will be faster, based on planner cost parameters and available statistical78    information.79   </p><p>80    When an index scan is used, a temporary copy of the table is created that81    contains the table data in the index order.  Temporary copies of each82    index on the table are created as well.  Therefore, you need free space on83    disk at least equal to the sum of the table size and the index sizes.84   </p><p>85    When a sequential scan and sort is used, a temporary sort file is86    also created, so that the peak temporary space requirement is as much87    as double the table size, plus the index sizes.  This method is often88    faster than the index scan method, but if the disk space requirement is89    intolerable, you can disable this choice by temporarily setting <a class="xref" href="runtime-config-query.html#GUC-ENABLE-SORT">enable_sort</a> to <code class="literal">off</code>.90   </p><p>91    It is advisable to set <a class="xref" href="runtime-config-resource.html#GUC-MAINTENANCE-WORK-MEM">maintenance_work_mem</a> to92    a reasonably large value (but not more than the amount of RAM you can93    dedicate to the <code class="command">CLUSTER</code> operation) before clustering.94   </p><p>95    Because the planner records statistics about the ordering of96    tables, it is advisable to run <a class="link" href="sql-analyze.html" title="ANALYZE"><code class="command">ANALYZE</code></a>97    on the newly clustered table.98    Otherwise, the planner might make poor choices of query plans.99   </p><p>100    Because <code class="command">CLUSTER</code> remembers which indexes are clustered,101    one can cluster the tables one wants clustered manually the first time,102    then set up a periodic maintenance script that executes103    <code class="command">CLUSTER</code> without any parameters, so that the desired tables104    are periodically reclustered.105   </p><p>106    Each backend running <code class="command">CLUSTER</code> will report its progress107    in the <code class="structname">pg_stat_progress_cluster</code> view. See108    <a class="xref" href="progress-reporting.html#CLUSTER-PROGRESS-REPORTING" title="28.4.2. CLUSTER Progress Reporting">Section 28.4.2</a> for details.109  </p><p>110    Clustering a partitioned table clusters each of its partitions using the111    partition of the specified partitioned index.  When clustering a partitioned112    table, the index may not be omitted.  <code class="command">CLUSTER</code> on a113    partitioned table cannot be executed inside a transaction block.114   </p></div><div class="refsect1" id="id-1.9.3.51.8"><h2>Examples</h2><p>115   Cluster the table <code class="literal">employees</code> on the basis of116   its index <code class="literal">employees_ind</code>:117</p><pre class="programlisting">118CLUSTER employees USING employees_ind;119</pre><p>120  </p><p>121   Cluster the <code class="literal">employees</code> table using the same122   index that was used before:123</p><pre class="programlisting">124CLUSTER employees;125</pre><p>126  </p><p>127   Cluster all tables in the database that have previously been clustered:128</p><pre class="programlisting">129CLUSTER;130</pre></div><div class="refsect1" id="id-1.9.3.51.9"><h2>Compatibility</h2><p>131   There is no <code class="command">CLUSTER</code> statement in the SQL standard.132  </p><p>133   The syntax134</p><pre class="synopsis">135CLUSTER <em class="replaceable"><code>index_name</code></em> ON <em class="replaceable"><code>table_name</code></em>136</pre><p>137  is also supported for compatibility with pre-8.3 <span class="productname">PostgreSQL</span>138  versions.139  </p></div><div class="refsect1" id="id-1.9.3.51.10"><h2>See Also</h2><span class="simplelist"><a class="xref" href="app-clusterdb.html" title="clusterdb"><span class="refentrytitle"><span class="application">clusterdb</span></span></a>, <a class="xref" href="progress-reporting.html#CLUSTER-PROGRESS-REPORTING" title="28.4.2. CLUSTER Progress Reporting">Section 28.4.2</a></span></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="sql-close.html" title="CLOSE">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="sql-commands.html" title="SQL Commands">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="sql-comment.html" title="COMMENT">Next</a></td></tr><tr><td width="40%" align="left" valign="top">CLOSE </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 16.3 Documentation">Home</a></td><td width="40%" align="right" valign="top"> COMMENT</td></tr></table></div></body></html>
codekingpro/portable-devtools · Team Ai