Best Practices for Safe PostgreSQL Schema Migrations
When you change a PostgreSQL schema in production, the database has to keep serving queries while you roll out new application versions…
When you run CLUSTER in PostgreSQL 7.4, PostgreSQL rearranges a table's rows on disk to match an existing index. If your queries read a range of indexed values or many rows with the same value, PostgreSQL can often pull those rows from fewer disk pages. PostgreSQL only reorders the table once per run, so later inserts and updates do not keep that layout.
These instructions apply to PostgreSQL 7.4, which is no longer supported. Newer releases change the CLUSTER syntax and how the database carries it out, so check the documentation for your installed version before you run the command.
Before you run CLUSTER, you need an index on the column or columns your queries filter on. In PostgreSQL 7.4, you pass the index name first and the table name second:
CREATE INDEX orders_customer_id_idx ON orders (customer_id);
CLUSTER orders_customer_id_idx ON orders;
ANALYZE orders;
PostgreSQL requires the index to exist on orders before you cluster it. Clustering on customer_id makes sense when your queries often pull all orders for one customer or scan a range of customer IDs. If your queries mostly fetch single rows at random, you are less likely to see a benefit. The PostgreSQL 7.4 CLUSTER documentation notes that fewer disk reads are a possible gain, without promising a speedup.
Run ANALYZE right after you cluster the table. Because PostgreSQL has changed the physical order of the rows on disk, the query planner needs fresh statistics to pick good execution plans, which is why the 7.4 documentation recommends running ANALYZE.
PostgreSQL remembers which index you used for each table. Next time, you can recluster orders without naming the index again:
CLUSTER orders;
If you want to recluster every previously clustered table in the current database, run the command with no arguments:
CLUSTER;
When you run CLUSTER without parameters, PostgreSQL skips tables that have never been clustered. It only reclusters tables that you own, though a superuser can recluster all previously clustered tables in the database.
PostgreSQL stores a table's rows across physical disk pages, while an index sits separately to help locate them. When you run CLUSTER, PostgreSQL rewrites the table so the rows sit on disk in the same order as the chosen index. That order is not permanent, and you still need an ORDER BY clause whenever a query must return rows in a specific order.
As your application inserts and updates rows, the physical layout drifts away from the index order. PostgreSQL 7.4 does not set a schedule for reclustering. You should re-run CLUSTER only when you measure that the table's layout is slowing down the queries you care about.
Do not confuse CLUSTER with VACUUM. In PostgreSQL 7.4, VACUUM reclaims space from deleted or obsolete rows and can refresh planner statistics if you pair it with ANALYZE. CLUSTER physically reorders the table's rows to match an index.
While PostgreSQL 7.4 clusters a table, it holds an ACCESS EXCLUSIVE lock on that table. No other session can read from or write to the table until PostgreSQL finishes, so schedule the work for a maintenance window when you can afford that downtime.
You also need to watch out for open transactions. The 7.4 documentation warns that transactions started before CLUSTER runs, which still read from older database snapshots, can see the table as completely empty once CLUSTER finishes. Both serializable and read-committed transactions can hit this behavior, so make sure you account for active transactions alongside the table lock.
To rewrite the table, PostgreSQL builds a temporary copy of the table along with temporary copies of all its indexes. The manual recommends having free disk space at least equal to the combined size of the table and its indexes. Check your available disk space before you start, and treat that figure as a planning baseline from the manual rather than a guaranteed ceiling on peak disk use.
If you have a large table whose rows are heavily scattered on disk, you can rebuild it in order with CREATE TABLE ... AS SELECT ... ORDER BY ... instead of running CLUSTER through an index scan. The PostgreSQL 7.4 manual notes that rebuilding the table this way is usually faster. However, PostgreSQL will not carry over the original table's constraints or foreign keys, and it will also drop granted privileges and OIDs. You will need to recreate any of those properties that your application relies on.
If you are running a version newer than 7.4, do not copy these 7.4 commands directly. Later releases use different syntax and execution options, as shown in the PostgreSQL 16 documentation. Always check the manual for your installed version.
If you prefer to run reclustering from the shell, PostgreSQL 7.4 includes the clusterdb command-line utility. It runs SQL CLUSTER on tables that have already been clustered. You can target a single table with --table:
clusterdb --table foo xyzzy
Here, clusterdb reclusters table foo in database xyzzy. If you want to cluster previously clustered tables across all databases, the 7.4 clusterdb documentation also provides an --all option.
Give Vroni a GitHub issue, bug report, spec, or rough idea. It reads the repo, plans the change, writes code, runs checks, and works toward a review-ready pull request.
Take a look at vroni.com