Rebuilds indexes.
REINDEX {INDEX | TABLE | DATABASE | SYSTEM} <name>
REINDEX rebuilds an index using the data stored in the index's table, replacing the old copy of the index. There are several scenarios in which to use REINDEX:
REINDEX provides a way to reduce the space consumption of the index by writing a new version of the index without the dead pages.FILLFACTOR storage parameter for an index, and wish to ensure that the change has taken full effect.REINDEX cannot be run inside a transaction block.
REINDEX cannot be run inside a transaction block.
REINDEX DATABASE and
REINDEX SYSTEM can only reindex the current database, so their parameter must match the current database's name.
REINDEX causes locking of system catalog tables, which could affect currently running queries. To avoid disrupting ongoing business operations, schedule the REINDEX operation during a period of low activity.
REINDEX is similar to a drop and recreate of the index in that the index contents are rebuilt from scratch. However, the locking considerations are rather different. REINDEX locks out writes but not reads of the index's parent table. It also takes an exclusive lock on the specific index being processed, which will block reads that attempt to use that index. In contrast, DROP INDEX momentarily takes an exclusive lock on the parent table, blocking both writes and reads. The subsequent CREATE INDEX locks out writes but not reads; since the index is not there, no read will attempt to use it, meaning that there will be no blocking but reads may be forced into expensive sequential scans.
Reindexing a single index or table requires being the owner of that index or table. Reindexing a database requires being the owner of the database (note that the owner can therefore rebuild indexes of tables owned by other users). Of course, superusers can always reindex anything.
REINDEX does not update the reltuples and relpages statistics for the index. To update those statistics, run ANALYZE on the table after reindexing.
Rebuild a single index:
REINDEX INDEX my_index;
Rebuild all the indexes on the table my_table:
REINDEX TABLE my_table;
There is no REINDEX command in the SQL standard.
CREATE INDEX, DROP INDEX, VACUUM
Parent topic: SQL Commands