REINDEX

Name

REINDEX -- rebuild indexes

Synopsis

REINDEX { DATABASE | TABLE | INDEX } name [ FORCE ]

Description

REINDEX rebuilds an index based on the data stored in the table, replacing the old copy of the index. There are two main reasons to use REINDEX:

If you suspect corruption of an index on a user table, you can simply rebuild that index, or all indexes on the table, using REINDEX INDEX or REINDEX TABLE. Another approach to dealing with a corrupted user-table index is just to drop and recreate it. This may in fact be preferable if you would like to maintain some semblance of normal operation on the table meanwhile. REINDEX acquires exclusive lock on the table, while CREATE INDEX only locks out writes not reads of the table.

Things are more difficult if you need to recover from corruption of an index on a system table. In this case it's important for the system to not have used any of the suspect indexes itself. (Indeed, in this sort of scenario you may find that server processes are crashing immediately at start-up, due to reliance on the corrupted indexes.) To recover safely, the server must be shut down and a stand-alone PostgreSQL server must be started instead with the command-line options -O and -P. (These options allow system table modifications and prevent use of system indexes, respectively.) Then, REINDEX DATABASE, REINDEX TABLE, or REINDEX INDEX can be issued, depending on how much you want to reconstruct. If in doubt, use REINDEX DATABASE FORCE to force reconstruction of all system indexes in the database. Then quit the standalone server session and restart the real server.

See the postgres reference page for more information about how to interact with the stand-alone server interface.

Parameters

DATABASE

Recreate all system indexes of a specified database. Indexes on user tables are not included. This form of REINDEX can only be used in stand-alone mode (see above).

TABLE

Recreate all indexes of a specified table.

INDEX

Recreate a specified index.

name

The name of the specific database, table, or index to be reindexed. Table and index names may be schema-qualified.

FORCE

Force rebuild of system indexes. Without this key word, REINDEX skips system indexes that are not marked invalid. FORCE is irrelevant for REINDEX INDEX or when reindexing user indexes.

Examples

Recreate the indexes on the table my_table:

REINDEX TABLE my_table;

Rebuild a single index:

REINDEX INDEX my_index;

Rebuild all system indexes (this will only work in a stand-alone server session):

REINDEX DATABASE my_database FORCE;

Compatibility

There is no REINDEX command in the SQL standard.