The REINDEX command rebuilds one or more indices, replacing the previous version of the index. REINDEX can be used in many scenarios, including the following (from Postgres documentation):
- An index has become corrupted, and no longer contains valid data. Although in theory this should never happen, in practice indexes can become corrupted due to software bugs or hardware failures. REINDEX provides a recovery method.
- An index has become “bloated”, that is it contains many empty or nearly-empty pages. This can occur with B-tree indexes in PostgreSQL under certain uncommon access patterns. REINDEX provides a way to reduce the space consumption of the index by writing a new version of the index without the dead pages.
- You have altered a storage parameter (such as fillfactor) for an index, and wish to ensure that the change has taken full effect.
- An index build with the CONCURRENTLY option failed, leaving an “invalid” index. Such indexes are useless but it can be convenient to use REINDEX to rebuild them. Note that REINDEX will not perform a concurrent build. To build the index without interfering with production you should drop the index and reissue the CREATE INDEX CONCURRENTLY command.
Index Rebuilding is two type:
- Reindexdb Utility
- reindex Sql commnad
1. reindexdb Utility:
reindexdb is a postgresql utility
Recreate index only:
reindexdb -i index_name
Recreate table indexes:
reindexdb -t table_name
Reindex specific databases:
reindexdb -d db_name
Reindex all databases :
Reindex database’s system catalogs:
2.Reindex Sql command:
Any of these can be forced by adding the keyword
FORCE after the command
- Recreate a single index, nijamindex:
REINDEX INDEX nijamindex;
- Recreate all indices in a table, t1
REINDEX TABLE t1;
- Recreate all indices in schema public:
REINDEX SCHEMA public;
- Recreate all indices in database postgres:
REINDEX DATABASE postgres;
- Recreate all indices on system catalogs in database postgres:
REINDEX SYSTEM postgre;