1. Load the pg_repack
extension in the database you want to reorganize:
CREATE EXTENSION IF NOT EXISTS pg_repack;
2. Run the pg_repack
utility to reorganize the target table(s).
pg_repack [OPTION]... [DBNAME]
The following options can be specified in OPTIONS
:
Attempt to repack all the databases of the cluster. Databases where the pg_repack extension is not installed will be skipped.
Reorganize the specified table(s) only. Multiple tables may be reorganized by writing multiple -t switches. By default, all eligible tables in the target databases are reorganized.
Reorganize both the specified table(s) and its inheritors. Multiple table hierarchies may be reorganized by writing multiple -I switches.
Repack the tables in the specified schema(s) only. Multiple schemas may be repacked by writing multiple -c switches. May be used in conjunction with --tablespace to move tables to a different tablespace.
Move the repacked tables to the specified tablespace: essentially an online version of ALTER TABLE ... SET TABLESPACE. The tables' indexes are left in the original tablespace unless --moveidx is specified too.
Also move the indexes of the repacked tables to the tablespace specified by the --tablespace option.
Perform an online CLUSTER ordered by the specified columns.
Perform an online VACUUM FULL. This is the default for non-clustered tables.
Print what would have been repacked and exit
Create the specified number of extra connections to PostgreSQL, and use these extra connections to parallelize the rebuild of indexes on each table. Parallel index builds are only supported for full-table repacks, not with --index or --only-indexes options. If your PostgreSQL server has extra cores and disk I/O available, this can be a useful way to speed up pg_repack.
Repack the specified index(es) only. Multiple indexes may be repacked by writing multiple -i switches. May be used in conjunction with --tablespace to move the index to a different tablespace.
Repack only the indexes of the specified table(s), which must be specified with the --table or --parent-table options.
pg_repack needs to take an exclusive lock at the end of the reorganization. This setting controls how many seconds pg_repack will wait to acquire this lock. If the lock cannot be taken after this duration and --no-kill-backend option is not specified, pg_repack will forcibly cancel the conflicting queries. If you are using PostgreSQL version 8.4 or newer, pg_repack will fall back to using pg_terminate_backend() to disconnect any remaining backends after twice this timeout has passed. The default is 60 seconds.
Skip to repack table if the lock cannot be taken for duration specified --wait-timeout, instead of cancelling conflicting queries. The default is false.
Disable ANALYZE after a full-table reorganization. If not specified, run ANALYZE after the reorganization.
Skip the superuser checks in the client. This setting is useful for using pg_repack on platforms that support running it as non-superusers.
Skip tables that belong to the specified extension(s). Some extensions may heavily depend on such tables at planning time etc.
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.
Specifies the TCP port or local Unix domain socket file extension on which the server is listening for connections.
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_repack 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.
Examples:
Perform an online CLUSTER of all the clustered tables in the database test, and perform an online VACUUM FULL of all the non-clustered tables:
pg_repack test
Perform an online VACUUM FULL on the tables foo and bar in the database test (an eventual cluster index is ignored):
pg_repack --no-order --table foo --table bar test
Move all indexes of table foo to tablespace tbs:
pg_repack -d test --table foo --only-indexes --tablespace tbs
Move the specified index to tablespace tbs:
pg_repack -d test --index idx --tablespace tbs