pg_reorg -- Reorganize tables in PostgreSQL databases without any locks.
The following options can be specified in OPTIONS.
See also "Options" for details.
- Reorg Options
- -a, --all : reorganize all databases
- -o [--order-by] columns [,...]
- -n [--no-order]
- -t [--table] table
- -T [--wait-timeout] seconds
- -Z [--no-analyze]
- Connection Options
- -d, --dbname=DBNAME : database to connect
- -h, --host=HOSTNAME : database server host or socket directory
- -p, --port=PORT : database server port
- -U, --username=USERNAME : user name to connect as
- -w, --no-password : never prompt for password
- -W, --password : force password prompt
- Generic Options
- -e, --echo : echo queries
- -E, --elevel=LEVEL : set output message level
- --help : show the help, then exit
- --version : output version information, then exit
pg_reorg is an utility program to reorganize tables in PostgreSQL databases.
Unlike clusterdb, it doesn't block any selections and updates during reorganization.
You can choose one of the following methods to reorganize.
- Online CLUSTER (ordered by cluster index)
- Ordered by specified columns
- Online VACUUM FULL (packing rows only)
- Only superusers can use the utility.
- Target table must have PRIMARY KEY.
Execute the following command to perform an online CLUSTER of all tables in test database.
$ pg_reorg test
Execute the following command to perform an online VACUUM FULL to foo table in test database.
$ pg_reorg --no-order --table foo -d test
pg_reorg has the following command line options:
Options to order rows.
If not specified, pg_reorg performs an online CLUSTER using cluster indexes.
Only one option can be specified.
You may also specify target tables or databases.
- Do online VACUUM FULL.
- -o columns [,...]
- Do online CLUSTER ordered by specified columns.
- Reorganize table only. If you don't specify this option, all tables in specified databases are reorganized.
pg_reorg needs to take an exclusive lock at the end of the reorganization.
This setting controls how long it wait for acquiring the lock in seconds.
If the lock cannot be taken even after the duration, pg_reorg forces to cancel conflicted queries.
Also, if the server version is 8.4 or newer, pg_reorg forces to disconnect conflicted backends after twice time passed.
The default is 60 seconds.
- Disable ANALYZE after the reorganization.
If not specified, run ANALYZE after the reorganization.
Options to connect to servers.
You cannot use --all and --dbname or --table together.
- Reorganize all databases.
- Specifies the name of the database to be reorganized. If this is not specified and -a (or --all) is not used, the database name is read from the environment variable PGDATABASE. If that is not set, the user name specified for the connection is used.
- -h host
- Specifies the host name of the machine on which the server is running. If the value begins with a slash, it is used as the directory for the Unix domain socket.
- -p port
- Specifies the TCP port or local Unix domain socket file extension on which the server is listening for connections.
- -U username
- User name to connect as.
Never issue a password prompt.
If the server requires password authentication and a password is not available by other means such as a .pgpass file, the connection attempt will fail.
This option can be useful in batch jobs and scripts where no user is present to enter a password.
- Force the program to prompt for a password before connecting to a database.
- This option is never essential, since the program will automatically prompt for a password if the server demands password authentication. However, pg_reorg will waste a connection attempt finding out that the server wants a password. In some cases it is worth typing -W to avoid the extra connection attempt.
- Echo commands sent to server.
- Choose the output message level from DEBUG, INFO, NOTICE, WARNING, ERROR, LOG, FATAL, and PANIC.
The default is INFO.
- Show usage of the program.
- Show the version number of the program.
- Default connection parameters
This utility, like most other PostgreSQL utilities, also uses the environment variables supported by libpq (see Environment Variables).
Error messages are reported when pg_reorg fails.
The following list shows the cause of errors.
You need to cleanup by hand after fatal errors.
To cleanup, execute $PGHOME/share/contrib/uninstall_pg_reorg.sql to the database where the error occured and then execute $PGHOME/share/contrib/pg_reorg.sql. (Do uninstall and reinstall.)
- pg_reorg : reorg database "template1" ... skipped
- pg_reorg is not installed in the database when --all option is specified.
- Do register pg_reorg to the database.
- ERROR: pg_reorg is not installed
- pg_reorg is not installed in the database specified by --dbname.
- Do register pg_reorg to the database.
- ERROR: relation "table" has no primary key
- The target table doesn't have PRIMARY KEY.
- Define PRIMARY KEY to the table. (ALTER TABLE ADD PRIMARY KEY)
- ERROR: relation "table" has no cluster key
- The target table doesn't have CLUSTER KEY.
- Define CLUSTER KEY to the table. (ALTER TABLE CLUSTER)
- pg_reorg : query failed: ERROR: column "col" does not exist
- The target table doesn't have columns specified by --order-by option.
- Specify existing columns.
- ERROR: permission denied for schema reorg
- Permission error.
- pg_reorg must be executed by superusers.
- pg_reorg : query failed: ERROR: trigger "z_reorg_trigger" for relation "tbl" already exists
- The target table already has a trigger named "z_reorg_trigger".
- Delete or rename the trigger.
- pg_reorg : trigger conflicted for tbl
- The target table already has a trigger which follows by "z_reorg_trigger" in alphabetical order.
- Delete or rename the trigger.
pg_reorg has the following restrictions.
Be careful to avoid data corruptions.
pg_reorg cannot reorganize temp tables.
pg_reorg cannot reorganize tables using GiST indexes.
You cannot do DDL commands except VACUUM and ANALYZE during pg_reorg.
In many cases pg_reorg will fail and rollback collectly, but there are some cases which may result in data-corruption .
- TRUNCATE is lost. Deleted rows still exist after pg_reorg.
- CREATE INDEX
- It causes index corruptions.
- ALTER TABLE ... ADD COLUMN
- It causes lost of data. Newly added columns are initialized with NULLs.
- ALTER TABLE ... ALTER COLUMN TYPE
- It causes data corruptions.
- ALTER TABLE ... SET TABLESPACE
- It causes data corruptions by wrong relfilenode.
pg_reorg creates a work table in the reorg schema and sorts the rows in this table.
Then, it updates the system catalogs directly to swap the work table and the original one.
pg_reorg can be built with "make" on UNIX or Linux.
pgxs build framework is used automatically.
Before building, you might need to install postgres packages for developer (postgresql-devel, etc.)
pg_config to your $PATH.
$ cd pg_reorg
$ make install
You can also use Microsoft Visual C++ 2010 to build the program on Windows.
There are project files in the msvc folder.
Start PostgreSQL and execute the script to register functions to your database.
$ pg_ctl start
$ psql -f $PGSHARE/contrib/pg_reorg.sql -d your_database
You can also use CREATE EXTENSION.
$ psql -c "CREATE EXTENSION pg_reorg" -d your_database
- PostgreSQL versions
- PostgreSQL 8.2, 8.3, 8.4, 9.0, 9.1, 9.2, 9.3
- RHEL 5/6
- Requires free disk space twice as large as the target
table(s) and indexes. For example, if the total size of the tables and
indexes to be reorganized is 1GB, an additional 2GB of disk space is
- 1.1.9 (2013-11-21)
- 1.1.8 (2012-12-07)
- Supports PostgreSQL 9.2
- Supports CREATE EXTENSION to install
- 1.1.7 (2011-08-07)
- Bugfix: VIEWs and FUNCTIONs could be corrupted that used a reorganized table which has a dropped column.
- Supports PostgreSQL 9.1 and 9.2dev. (but EXTENSION is not yet)