Clustering an index
CLUSTER works by making a whole new copy of the table, sorted by the index you asked to traverse the original in. It's as if you had inserted them in that order in the first place. Once built, the original copy of the data is dropped. CLUSTER
requires an exclusive lock on the table and enough disk space to hold the second, reorganized copy—essentially, twice the amount of disk space, temporarily. You should run ANALYZE
afterwards in order to update the statistics associated with this table, to reflect its new ordering.
Clustering is useful for getting faster results from range-based queries, when selecting a set of values that is between two end points (with the beginning or end being considered an end point in that context). It can generally speed up access to fragmented data that might have been introduced from any number of sources.
Clustering is a one-time act. Future insertion does not respect the clustered order, even though the database does...