<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML ><HEAD ><TITLE >CLUSTER</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK REV="MADE" HREF="mailto:pgsql-docs@postgresql.org"><LINK REL="HOME" TITLE="PostgreSQL 8.4.12 Documentation" HREF="index.html"><LINK REL="UP" TITLE="SQL Commands" HREF="sql-commands.html"><LINK REL="PREVIOUS" TITLE="CLOSE" HREF="sql-close.html"><LINK REL="NEXT" TITLE="COMMENT" HREF="sql-comment.html"><LINK REL="STYLESHEET" TYPE="text/css" HREF="stylesheet.css"><META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=ISO-8859-1"><META NAME="creation" CONTENT="2012-05-31T23:30:11"></HEAD ><BODY CLASS="REFENTRY" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="5" ALIGN="center" VALIGN="bottom" >PostgreSQL 8.4.12 Documentation</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="top" ><A HREF="sql-close.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="10%" ALIGN="left" VALIGN="top" ><A HREF="sql-close.html" >Fast Backward</A ></TD ><TD WIDTH="60%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="top" ><A HREF="sql-comment.html" >Fast Forward</A ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="top" ><A HREF="sql-comment.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="SQL-CLUSTER" ></A >CLUSTER</H1 ><DIV CLASS="REFNAMEDIV" ><A NAME="AEN55247" ></A ><H2 >Name</H2 >CLUSTER -- cluster a table according to an index</DIV ><A NAME="AEN55250" ></A ><DIV CLASS="REFSYNOPSISDIV" ><A NAME="AEN55252" ></A ><H2 >Synopsis</H2 ><PRE CLASS="SYNOPSIS" >CLUSTER [VERBOSE] <TT CLASS="REPLACEABLE" ><I >tablename</I ></TT > [ USING <TT CLASS="REPLACEABLE" ><I >indexname</I ></TT > ] CLUSTER [VERBOSE]</PRE ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN55256" ></A ><H2 >Description</H2 ><P > <TT CLASS="COMMAND" >CLUSTER</TT > instructs <SPAN CLASS="PRODUCTNAME" >PostgreSQL</SPAN > to cluster the table specified by <TT CLASS="REPLACEABLE" ><I >tablename</I ></TT > based on the index specified by <TT CLASS="REPLACEABLE" ><I >indexname</I ></TT >. The index must already have been defined on <TT CLASS="REPLACEABLE" ><I >tablename</I ></TT >. </P ><P > 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 <TT CLASS="LITERAL" >FILLFACTOR</TT > storage parameter to less than 100% can aid in preserving cluster ordering during updates, since updated rows are preferentially kept on the same page.) </P ><P > When a table is clustered, <SPAN CLASS="PRODUCTNAME" >PostgreSQL</SPAN > remembers which index it was clustered by. The form <TT CLASS="COMMAND" >CLUSTER <TT CLASS="REPLACEABLE" ><I >tablename</I ></TT ></TT > reclusters the table using the same index as before. </P ><P > <TT CLASS="COMMAND" >CLUSTER</TT > without any parameter reclusters all the previously-clustered tables in the current database that the calling user owns, or all such tables if called by a superuser. This form of <TT CLASS="COMMAND" >CLUSTER</TT > cannot be executed inside a transaction block. </P ><P > When a table is being clustered, an <TT CLASS="LITERAL" >ACCESS EXCLUSIVE</TT > lock is acquired on it. This prevents any other database operations (both reads and writes) from operating on the table until the <TT CLASS="COMMAND" >CLUSTER</TT > is finished. </P ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN55276" ></A ><H2 >Parameters</H2 ><P ></P ><DIV CLASS="VARIABLELIST" ><DL ><DT ><TT CLASS="REPLACEABLE" ><I >tablename</I ></TT ></DT ><DD ><P > The name (possibly schema-qualified) of a table. </P ></DD ><DT ><TT CLASS="REPLACEABLE" ><I >indexname</I ></TT ></DT ><DD ><P > The name of an index. </P ></DD ><DT ><TT CLASS="LITERAL" >VERBOSE</TT ></DT ><DD ><P > Prints a progress report as each table is clustered. </P ></DD ></DL ></DIV ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN55294" ></A ><H2 >Notes</H2 ><P > In cases where you are accessing single rows randomly within a table, the actual order of the data in the 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 <TT CLASS="COMMAND" >CLUSTER</TT >. If you are requesting a range of indexed values from a table, or a single indexed value that has multiple rows that match, <TT CLASS="COMMAND" >CLUSTER</TT > will help because once the index identifies the table page for the first row that matches, all other rows that match are probably already on the same table page, and so you save disk accesses and speed up the query. </P ><P > During the cluster operation, a temporary copy of the table is created that 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. </P ><P > Because <TT CLASS="COMMAND" >CLUSTER</TT > remembers the clustering information, one can cluster the tables one wants clustered manually the first time, and setup a timed event similar to <TT CLASS="COMMAND" >VACUUM</TT > so that the tables are periodically reclustered. </P ><P > Because the planner records statistics about the ordering of tables, it is advisable to run <A HREF="sql-analyze.html" ><I >ANALYZE</I ></A > on the newly clustered table. Otherwise, the planner might make poor choices of query plans. </P ><P > There is another way to cluster data. The <TT CLASS="COMMAND" >CLUSTER</TT > command reorders the original table by scanning it using the index you specify. This can be slow on large tables because the rows are fetched from the table in index order, and if the table is disordered, the entries are on random pages, so there is one disk page retrieved for every row moved. (<SPAN CLASS="PRODUCTNAME" >PostgreSQL</SPAN > has a cache, but the majority of a big table will not fit in the cache.) The other way to cluster a table is to use: </P><PRE CLASS="PROGRAMLISTING" >CREATE TABLE <TT CLASS="REPLACEABLE" ><I >newtable</I ></TT > AS SELECT * FROM <TT CLASS="REPLACEABLE" ><I >table</I ></TT > ORDER BY <TT CLASS="REPLACEABLE" ><I >columnlist</I ></TT >;</PRE ><P> which uses the <SPAN CLASS="PRODUCTNAME" >PostgreSQL</SPAN > sorting code to produce the desired order; this is usually much faster than an index scan for disordered data. Then you drop the old table, use <TT CLASS="COMMAND" >ALTER TABLE ... RENAME</TT > to rename <TT CLASS="REPLACEABLE" ><I >newtable</I ></TT > to the old name, and recreate the table's indexes. The big disadvantage of this approach is that it does not preserve OIDs, constraints, foreign key relationships, granted privileges, and other ancillary properties of the table — all such items must be manually recreated. Another disadvantage is that this way requires a sort temporary file about the same size as the table itself, so peak disk usage is about three times the table size instead of twice the table size. </P ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN55315" ></A ><H2 >Examples</H2 ><P > Cluster the table <TT CLASS="LITERAL" >employees</TT > on the basis of its index <TT CLASS="LITERAL" >employees_ind</TT >: </P><PRE CLASS="PROGRAMLISTING" >CLUSTER employees USING employees_ind;</PRE ><P> </P ><P > Cluster the <TT CLASS="LITERAL" >employees</TT > table using the same index that was used before: </P><PRE CLASS="PROGRAMLISTING" >CLUSTER employees;</PRE ><P> </P ><P > Cluster all tables in the database that have previously been clustered: </P><PRE CLASS="PROGRAMLISTING" >CLUSTER;</PRE ><P> </P ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN55326" ></A ><H2 >Compatibility</H2 ><P > There is no <TT CLASS="COMMAND" >CLUSTER</TT > statement in the SQL standard. </P ><P > The syntax </P><PRE CLASS="SYNOPSIS" >CLUSTER <TT CLASS="REPLACEABLE" ><I >indexname</I ></TT > ON <TT CLASS="REPLACEABLE" ><I >tablename</I ></TT ></PRE ><P> is also supported for compatibility with pre-8.3 <SPAN CLASS="PRODUCTNAME" >PostgreSQL</SPAN > versions. </P ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN55335" ></A ><H2 >See Also</H2 ><A HREF="app-clusterdb.html" ><I ><I >clusterdb</I ></I ></A ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="sql-close.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="sql-comment.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >CLOSE</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="sql-commands.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >COMMENT</TD ></TR ></TABLE ></DIV ></BODY ></HTML >