clusterdb
Recluster PostgreSQL database tables
TLDR
Cluster a specific database
Cluster all databases
Cluster a specific table in a database
SYNOPSIS
clusterdb [OPTION]... [--dbname=]DBNAME [TABLE [TABLE...]]
PARAMETERS
-a, --all
Cluster all tables in all databases
-d DBNAME, --dbname=DBNAME
Database to cluster
-e, --echo-errors
Echo failed SQL commands
-h HOSTNAME, --host=HOSTNAME
Database server host or socket directory
-l, --list
List available tables for clustering
-p PORT, --port=PORT
Database server port number
-q, --quiet
Suppress all output messages
-t TABLE, --table=TABLE
Cluster only specific table(s)
-U NAME, --username=NAME
Connect as specified user
-v, --verbose
Enable verbose output
-V, --version
Display version information
-W, --password
Force password prompt
-?, --help
Show help and exit
-H CONNINFO, --conninfo=CONNINFO
PostgreSQL connection string
DESCRIPTION
clusterdb is a command-line utility in PostgreSQL that reorganizes tables by clustering rows based on a specified index. This process physically sorts heap tuples in index order, improving query performance for index scans and reclaiming space from deleted or updated rows (dead tuples). Unlike VACUUM, which only marks space as reusable, CLUSTER compacts the table.
It wraps the SQL CLUSTER command, allowing clustering of one or more tables in a database, or all tables across all databases. clusterdb acquires an exclusive lock on each table, making it suitable for maintenance windows rather than live production use on large tables.
Specify a database with -d or --dbname, tables with -t, or use -a for all. It supports standard PostgreSQL connection options like host, port, and user. Output includes progress unless suppressed with -q. Ideal after bulk deletes or updates to optimize storage and speed.
CAVEATS
Acquires exclusive table locks, blocking writes; avoid on large production tables without downtime. Requires index on table; skips tables without suitable indexes. Can be I/O intensive.
EXAMPLES
clusterdb mydb clusters all tables in mydb.
clusterdb -h localhost -U postgres -t users -t orders mydb clusters specific tables.
clusterdb -a clusters all tables in all databases.
NOTES
Each table clusters on its primary index unless specified via SQL. Combine with ANALYZE for full optimization.
HISTORY
Introduced in PostgreSQL 7.3 (2002) alongside the CLUSTER SQL command (from 7.1). Evolved with libpq enhancements; current in PostgreSQL 16+ for optimized maintenance.


