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

NAME

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

SYNOPSIS

10       CLUSTER [VERBOSE] tablename [ USING indexname ]
11       CLUSTER [VERBOSE]
12
13

DESCRIPTION

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

PARAMETERS

42       tablename
43              The name (possibly schema-qualified) of a table.
44
45       indexname
46              The name of an index.
47
48       VERBOSE
49              Prints a progress report as each table is clustered.
50

NOTES

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

EXAMPLES

100       Cluster the table employees on the basis of its index employees_ind:
101
102       CLUSTER employees USING employees_ind;
103
104
105       Cluster the employees table using the same index that was used before:
106
107       CLUSTER employees;
108
109
110       Cluster all tables in the database that have previously been clustered:
111
112       CLUSTER;
113
114

COMPATIBILITY

116       There is no CLUSTER statement in the SQL standard.
117
118       The syntax
119
120       CLUSTER indexname ON tablename
121
122       is also supported for compatibility with pre-8.3 PostgreSQL versions.
123

SEE ALSO

125       clusterdb [clusterdb(1)]
126
127
128
129SQL - Language Statements         2014-02-17                        CLUSTER(7)
Impressum