PostgreSQL REINDEX

In this section, we are going to understand the working of the PostgreSQL REINDEX command, which is used to recreate multiple indexes and see the difference between the REINDEX command, drop and create index command.

What is the PostgreSQL REINDEX command?

In reality, the PostgreSQL index can be altered and no longer having valid data because of software issues or hardware failures. Therefore, we can use the REINDEX command to improve the index.

In other words, we can say the REINDEX command is used to rebuild one or more indexes.

Syntax of PostgreSQL REINDEX command

The illustration of the REINDEX command is as follows:

In the above illustration, we have used the following keywords, which are as shown in the below table:

ParametersDescription
REINDEXIt is a keyword, which is used to recreate the various indexes.
VERBOSEIf we involve the VERBOSE keyword, then the command shows a progress report as soon as all the indexes are re-indexed. And it is an optional keyword.

Now, we will see the following illustrations, which are most commonly used in the REINDEX command.

  • To recreate a single index

We can define the index name after the REINDEX INDEX clause to rebuild the single index, as shown in the following syntax:

  • To rebuild all the indexes of a table

We can use the TABLE keyword and describe the name of the table to rebuild

each of the indexes of a table, as shown in the below syntax:

  • To rebuild the indices in a schema

We can use the SCHEMA keyword after the schema name if we need to reform all the indices in a schema, as shown in the below illustration:

  • To recreate all indices in a specific database

If we want to rebuild or recreate all the indices in a particular database, we can define the database name after the REINDEX DATABASE clause, as we can see in the following syntax:

  • To rebuild all the indices on a system catalog

We can use the below illustration to recreate all indices on system catalog in a particular database:

REINDEX vs. DROP INDEX and CREATE INDEX commands

The REINDEX command rebuilds the index contents from the basic, which has the same result as drop and recreate the index.

But, the locking mechanisms between the re-index, drop index, and the create index are dissimilar.

Let see the dissimilarity between REINDEX, DROP index and the Create index.

The REINDEX command

  • The REINDEX command locks the writes but not reads of the table where the index belongs.

DROP index and Create Index commands

Firstly, we will use the DROP INDEX command, which helps us to locate both writes and reads of the table to which the index has its place by obtaining an exclusive lock on the table.

After that, we have used the CREATE INDEX command to lock out writes but not reads from the index's first table. Though, the reads might be exclusive throughout the formation of the index.

Overview

In the PostgreSQL Reindex section, we have learned the REINDEX command to drop and rebuild several indices.






Latest Courses