1CLUSTER()                        SQL Commands                        CLUSTER()
2
3
4

NAME

6       CLUSTER - cluster a table according to an index
7
8

SYNOPSIS

10       CLUSTER indexname ON tablename
11       CLUSTER tablename
12       CLUSTER
13
14

DESCRIPTION

16       CLUSTER  instructs  PostgreSQL to cluster the table specified by table‐
17       name based on the index specified by indexname. The index must  already
18       have been defined on tablename.
19
20       When  a  table  is  clustered,  it is physically reordered based on the
21       index information. Clustering is a one-time operation: when  the  table
22       is  subsequently  updated,  the  changes are not clustered. That is, no
23       attempt is made to store new or updated rows according to  their  index
24       order.  If  one  wishes,  one can periodically recluster by issuing the
25       command again.
26
27       When a table is clustered, PostgreSQL remembers on which index  it  was
28       clustered.  The form CLUSTER tablename reclusters the table on the same
29       index that it was clustered before.
30
31       CLUSTER without any parameter reclusters all the tables in the  current
32       database that the calling user owns, or all tables if called by a supe‐
33       ruser. (Never-clustered tables are not included.) This form of  CLUSTER
34       cannot be executed inside a transaction block.
35
36       When  a  table is being clustered, an ACCESS EXCLUSIVE lock is acquired
37       on it. This prevents any other  database  operations  (both  reads  and
38       writes) from operating on the table until the CLUSTER is finished.
39

PARAMETERS

41       indexname
42              The name of an index.
43
44       tablename
45              The name (possibly schema-qualified) of a table.
46

NOTES

48       CLUSTER loses all visibility information of tuples, which makes the ta‐
49       ble look empty to any snapshot that was taken before the  CLUSTER  com‐
50       mand  finished.  That  makes  CLUSTER unsuitable for applications where
51       transactions that access the table being clustered are run concurrently
52       with  CLUSTER.  This  is  most  visible with serializable transactions,
53       because they take only one snapshot at the beginning  of  the  transac‐
54       tion, but read-committed transactions are also affected.
55
56       In  cases  where you are accessing single rows randomly within a table,
57       the actual order of the data in the table is unimportant.  However,  if
58       you  tend  to  access some data more than others, and there is an index
59       that groups them together, you will benefit from using CLUSTER.  If you
60       are  requesting  a  range  of  indexed values from a table, or a single
61       indexed value that has multiple rows  that  match,  CLUSTER  will  help
62       because once the index identifies the table page for the first row that
63       matches, all other rows that match are probably already on the same ta‐
64       ble page, and so you save disk accesses and speed up the query.
65
66       During  the cluster operation, a temporary copy of the table is created
67       that contains the table data in the index order.  Temporary  copies  of
68       each  index  on the table are created as well. Therefore, you need free
69       space on disk at least equal to the sum of the table size and the index
70       sizes.
71
72       Because  CLUSTER  remembers the clustering information, one can cluster
73       the tables one wants clustered manually the first  time,  and  setup  a
74       timed  event  similar  to  VACUUM  so  that the tables are periodically
75       reclustered.
76
77       Because the planner records statistics about the ordering of tables, it
78       is  advisable to run ANALYZE [analyze(7)] on the newly clustered table.
79       Otherwise, the planner may make poor choices of query plans.
80
81       There is another way to cluster data. The CLUSTER command reorders  the
82       original  table by scanning it using the index you specify. This can be
83       slow on large tables because the rows are fetched  from  the  table  in
84       index  order, and if the table is disordered, the entries are on random
85       pages, so there is one disk page retrieved for every row moved.  (Post‐
86       greSQL has a cache, but the majority of a big table will not fit in the
87       cache.)  The other way to cluster a table is to use
88
89       CREATE TABLE newtable AS
90           SELECT * FROM table ORDER BY columnlist;
91
92       which uses the PostgreSQL sorting code to produce  the  desired  order;
93       this  is  usually  much  faster than an index scan for disordered data.
94       Then you drop the old table, use  ALTER  TABLE  ...  RENAME  to  rename
95       newtable  to  the  old name, and recreate the table's indexes.  The big
96       disadvantage of this approach is that it does not preserve  OIDs,  con‐
97       straints,  foreign  key  relationships,  granted  privileges, and other
98       ancillary properties of the table — all such  items  must  be  manually
99       recreated. Another disadvantage is that this way requires a sort tempo‐
100       rary file about the same size as the table itself, so peak  disk  usage
101       is about three times the table size instead of twice the table size.
102

EXAMPLES

104       Cluster the table employees on the basis of its index emp_ind:
105
106       CLUSTER emp_ind ON emp;
107
108
109       Cluster the employees table using the same index that was used before:
110
111       CLUSTER emp;
112
113
114       Cluster all tables in the database that have previously been clustered:
115
116       CLUSTER;
117
118

COMPATIBILITY

120       There is no CLUSTER statement in the SQL standard.
121

SEE ALSO

123       clusterdb [clusterdb(1)]
124
125
126
127SQL - Language Statements         2008-06-08                         CLUSTER()
Impressum