Question: How do you use the ALTER TABLE command to cluster a table on an index in PostgreSQL?


In PostgreSQL, clustering a table according to an index physically reorders the rows of the table based on the sort order defined by the index. This can improve the performance of data retrieval operations that are aligned with this order. To perform clustering using the ALTER TABLE command, follow these steps:

Step 1: Create an Index

First, ensure that there is an index on the table based on which you want to cluster. If there isn't one, you'll need to create it. For example, suppose we have a table named employees and we want to cluster it based on the department_id column. First, we would create an index as follows:

CREATE INDEX idx_department ON employees(department_id);

Step 2: Cluster the Table

Once the index is created, you can cluster the table on this index using the ALTER TABLE command:

ALTER TABLE employees CLUSTER ON idx_department;

This command tells PostgreSQL to reorder the physical storage of data in the employees table to match the order of the idx_department index.

Important Considerations

  • Clustering is a one-time operation: When you cluster a table, PostgreSQL does not automatically maintain the clustered order after subsequent inserts, updates, or deletes. You will need to re-cluster the table manually if necessary.
  • Performance overhead: Clustering a table can take a significant amount of time and disk I/O, especially for large tables. It also exclusively locks the table, preventing other accesses during the operation.
  • Choosing the right index: Since clustering modifies the physical order of rows, choosing the correct index to cluster on can be crucial. Typically, you should cluster on an index that matches the most common and critical queries' WHERE or ORDER BY clauses.

By clustering a table on an appropriate index, you can optimize query performance that benefits from this specific row order, particularly for range scans or index-only scans.

Was this content helpful?

White Paper

Free System Design on AWS E-Book

Download this early release of O'Reilly's latest cloud infrastructure e-book: System Design on AWS.

Free System Design on AWS E-Book
Start building today

Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement.