REINDEX(7)
REINDEX(7) ๐
okay bestie, welcome to the PostgreSQL 18.6 Documentation glow-up of REINDEX โ yes that's the manual page name, and yes we're doing this together, hand in hand, no cap ๐ซถ
โจ NAME โจ
REINDEX โ rebuild indexes ๐
(like, the whole point is literally in the name, iconic)
๐ SYNOPSIS ๐
REINDEX [ ( option [, ...] ) ] { INDEX | TABLE | SCHEMA } [ CONCURRENTLY ] name
REINDEX [ ( option [, ...] ) ] { DATABASE | SYSTEM } [ CONCURRENTLY ] [ name ]
where option can be one of:
CONCURRENTLY [ boolean ]
TABLESPACE new_tablespace
VERBOSE [ boolean ]
๐ฆ DESCRIPTION ๐ฆ
So here's the tea: REINDEX rebuilds an index using the data stored in that index's table, and it straight-up replaces the old copy of the index. Out with the old, in with the new, periodt. ๐
And like, there are several scenarios where you'd wanna do this:
- An index has become corrupted and no longer contains valid data. Now, in theory this should never happen, but in practice indexes can get corrupted because of software bugs or hardware failures.
REINDEXis your recovery method, bestie. ๐ฉน - An index has become "bloated" โ meaning it's holding a bunch of empty or nearly-empty pages. This can happen with B-tree indexes in PostgreSQL under certain uncommon access patterns.
REINDEXgives you a way to reduce the space consumption of the index by writing a brand-new version of it without all those dead pages. Peep Section 24.2 for more info. ๐ธ - You altered a storage parameter (like
fillfactor) for an index and you wanna make sure the change has fully taken effect. Vibe check passed โ - If an index build fails while using the
CONCURRENTLYoption, that index gets left as "invalid". Invalid indexes are useless ngl, but it can be convenient to useREINDEXto rebuild them. Note that onlyREINDEX INDEXis able to perform a concurrent build on an invalid index. ๐
๐ PARAMETERS ๐
INDEXโ Recreate the specified index. This form ofREINDEXcannot be executed inside a transaction block when used with a partitioned index. ๐ซTABLEโ Recreate all indexes of the specified table. If the table has a secondary "TOAST" table, that gets reindexed too. This form ofREINDEXcannot be executed inside a transaction block when used with a partitioned table. ๐ซSCHEMAโ Recreate all indexes of the specified schema. If a table of this schema has a secondary "TOAST" table, that gets reindexed as well. Indexes on shared system catalogs are also processed. This form ofREINDEXcannot be executed inside a transaction block. ๐ซDATABASEโ Recreate all indexes within the current database, except system catalogs. Indexes on system catalogs are not processed. This form ofREINDEXcannot be executed inside a transaction block. ๐ซSYSTEMโ Recreate all indexes on system catalogs within the current database. Indexes on shared system catalogs are included. Indexes on user tables are not processed. This form ofREINDEXcannot be executed inside a transaction block. ๐ซnameโ The name of the specific index, table, or database to be reindexed. Index and table names can be schema-qualified. Currently,REINDEX DATABASEandREINDEX SYSTEMcan only reindex the current database. Their parameter is optional, and if you give it, it has to match the current database's name. fr fr.CONCURRENTLYโ When you use this option, PostgreSQL will rebuild the index without taking any locks that prevent concurrent inserts, updates, or deletes on the table. Meanwhile a standard index rebuild locks out writes (but not reads) on the table until it's done. There are several caveats to be aware of when you use this option โ see Rebuilding Indexes Concurrently below. ๐
For temporary tables, REINDEX is always non-concurrent, because no other session can access them anyway, and non-concurrent reindex is cheaper. ๐ธ
TABLESPACEโ Specifies that indexes will be rebuilt on a new tablespace.VERBOSEโ Prints a progress report as each index is reindexed, atINFOlevel. ๐ขbooleanโ Specifies whether the selected option should be turned on or off. You can writeTRUE,ON, or1to enable the option, andFALSE,OFF, or0to disable it. The boolean value can also be omitted, in which caseTRUEis assumed. Slay, defaults. โจnew_tablespaceโ The tablespace where indexes will be rebuilt. ๐
๐ฉน NOTES ๐ฉน
Okay bestie, so listen. If you think an index on a user table is acting sus and maybe corrupted, you don't have to panic โ you can literally just rebuild that one index, or all the indexes on that table, with REINDEX INDEX or REINDEX TABLE. Easy. Slay. ๐
But it gets spicy when the corruption is on a system table index. Then it's highkey important that the system hasn't used any of those sus indexes itself. Like, in that kind of scenario you might literally see server processes crashing immediately at start-up because they're relying on the corrupted indexes. ๐ญ To recover safely, the server must be started with the -P option, which stops it from using indexes for system catalog lookups.
So one way to do this: shut down the server and start a single-user PostgreSQL server with -P on its command line. Then you can issue REINDEX DATABASE, REINDEX SYSTEM, REINDEX TABLE, or REINDEX INDEX, depending on how much you wanna reconstruct. If you're unsure, use REINDEX SYSTEM to select reconstruction of all system indexes in the database. Then quit the single-user server session and restart the regular server. Peep the postgres(1) reference page for more info on how to interact with the single-user server interface. ๐
Alternatively, a regular server session can be started with -P included in its command line options. How you do that varies across clients, but in all libpq-based clients you can set the PGOPTIONS environment variable to -P before starting the client. Note that while this method does not require locking out other clients, it might still be wise to prevent other users from connecting to the damaged database until repairs have been completed. ๐ซถ
REINDEX is similar to a drop and recreate of the index in that the index contents are rebuilt from scratch. BUT the locking considerations are rather different. REINDEX locks out writes but not reads of the index's parent table. It also takes an ACCESS EXCLUSIVE lock on the specific index being processed, which will block reads that attempt to use that index. In particular, the query planner tries to take an ACCESS SHARE lock on every index of the table, regardless of the query, and so REINDEX blocks virtually any queries except for some prepared queries whose plan has been cached and which don't use this very index. In contrast, DROP INDEX momentarily takes an ACCESS 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 might be forced into expensive sequential scans. ๐
While REINDEX is running, the search_path is temporarily changed to pg_catalog, pg_temp.
Reindexing a single index or table requires having the MAINTAIN privilege on the table. Note that while REINDEX on a partitioned index or table requires having the MAINTAIN privilege on the partitioned table, such commands skip the privilege checks when processing the individual partitions. Reindexing a schema or database requires being the owner of that schema or database or having privileges of the pg_maintain role. Note specifically that it's thus possible for non-superusers to rebuild indexes of tables owned by other users. However, as a special exception, REINDEX DATABASE, REINDEX SCHEMA, and REINDEX SYSTEM will skip indexes on shared catalogs unless the user has the MAINTAIN privilege on the catalog. โจ
Reindexing partitioned indexes or partitioned tables is supported with REINDEX INDEX or REINDEX TABLE, respectively. Each partition of the specified partitioned relation is reindexed in a separate transaction. Those commands cannot be used inside a transaction block when working on a partitioned table or index. ๐
When using the TABLESPACE clause with REINDEX on a partitioned index or table, only the tablespace references of the leaf partitions are updated. As partitioned indexes are not updated, it is recommended to separately use ALTER TABLE ONLY on them so as any new partitions attached inherit the new tablespace. On failure, it may not have moved all the indexes to the new tablespace. Re-running the command will rebuild all the leaf partitions and move previously-unprocessed indexes to the new tablespace. ๐
If SCHEMA, DATABASE or SYSTEM is used with TABLESPACE, system relations are skipped and a single WARNING will be generated. Indexes on TOAST tables are rebuilt, but not moved to the new tablespace. ๐
๐ฅ Rebuilding Indexes Concurrently ๐ฅ
Rebuilding an index can interfere with regular operation of a database. Normally PostgreSQL locks the table whose index is rebuilt against writes and performs the entire index build with a single scan of the table. Other transactions can still read the table, but if they try to insert, update, or delete rows in the table they will block until the index rebuild is finished. This could have a severe effect if the system is a live production database. Very large tables can take many hours to be indexed, and even for smaller tables, an index rebuild can lock out writers for periods that are unacceptably long for a production system. ๐ฌ
PostgreSQL supports rebuilding indexes with minimum locking of writes. This method is invoked by specifying the CONCURRENTLY option of REINDEX. When this option is used, PostgreSQL must perform two scans of the table for each index that needs to be rebuilt and wait for termination of all existing transactions that could potentially use the index. This method requires more total work than a standard index rebuild and takes significantly longer to complete as it needs to wait for unfinished transactions that might modify the index. However, since it allows normal operations to continue while the index is being rebuilt, this method is useful for rebuilding indexes in a production environment. Of course, the extra CPU, memory and I/O load imposed by the index rebuild may slow down other operations. ๐ข
The following steps occur in a concurrent reindex. Each step is run in a separate transaction. If there are multiple indexes to be rebuilt, then each step loops through all the indexes before moving to the next step.
- A new transient index definition is added to the catalog
pg_index. This definition will be used to replace the old index. ASHARE UPDATE EXCLUSIVElock at session level is taken on the indexes being reindexed as well as their associated tables to prevent any schema modification while processing. - A first pass to build the index is done for each new index. Once the index is built, its flag
pg_index.indisreadyis switched to โtrueโ to make it ready for inserts, making it visible to other sessions once the transaction that performed the build is finished. This step is done in a separate transaction for each index. - Then a second pass is performed to add tuples that were added while the first pass was running. This step is also done in a separate transaction for each index.
- All the constraints that refer to the index are changed to refer to the new index definition, and the names of the indexes are changed. At this point,
pg_index.indisvalidis switched to โtrueโ for the new index and to โfalseโ for the old, and a cache invalidation is done causing all sessions that referenced the old index to be invalidated. - The old indexes have
pg_index.indisreadyswitched to โfalseโ to prevent any new tuple insertions, after waiting for running queries that might reference the old index to complete. - The old indexes are dropped. The
SHARE UPDATE EXCLUSIVEsession locks for the indexes and the table are released.
If a problem arises while rebuilding the indexes, such as a uniqueness violation in a unique index, the REINDEX command will fail but leave behind an โinvalidโ new index in addition to the pre-existing one. This index will be ignored for querying purposes because it might be incomplete; however it will still consume update overhead. The psql \d command will report such an index as INVALID:
postgres=# \d tab
Table "public.tab"
Column | Type | Modifiers
--------+---------+-----------
col | integer |
Indexes:
"idx" btree (col)
"idx_ccnew" btree (col) INVALID
If the index marked INVALID is suffixed _ccnew, then it corresponds to the transient index created during the concurrent operation, and the recommended recovery method is to drop it using DROP INDEX, then attempt REINDEX CONCURRENTLY again. If the invalid index is instead suffixed _ccold, it corresponds to the original index which could not be dropped; the recommended recovery method is to just drop said index, since the rebuild proper has been successful. A nonzero number may be appended to the suffix of the invalid index names to keep them unique, like _ccnew1, _ccold2, etc. ๐
Regular index builds permit other regular index builds on the same table to occur simultaneously, but only one concurrent index build can occur on a table at a time. In both cases, no other types of schema modification on the table are allowed meanwhile. Another difference is that a regular REINDEX TABLE or REINDEX INDEX command can be performed within a transaction block, but REINDEX CONCURRENTLY cannot. ๐ซ
Like any long-running transaction, REINDEX on a table can affect which tuples can be removed by concurrent VACUUM on any other table.
REINDEX SYSTEM does not support CONCURRENTLY since system catalogs cannot be reindexed concurrently.
Furthermore, indexes for exclusion constraints cannot be reindexed concurrently. If such an index is named directly in this command, an error is raised. If a table or database with exclusion constraint indexes is reindexed concurrently, those indexes will be skipped. (It is possible to reindex such indexes without the CONCURRENTLY option.) ๐
Each backend running REINDEX will report its progress in the pg_stat_progress_create_index view. See Section 27.4.4 for details. ๐
๐ EXAMPLES ๐
Rebuild a single index:
REINDEX INDEX my_index;
Rebuild all the indexes on the table my_table:
REINDEX TABLE my_table;
Rebuild all indexes in a particular database, without trusting the system indexes to be valid already:
$ export PGOPTIONS="-P"
$ psql broken_db
...
broken_db=> REINDEX DATABASE broken_db;
broken_db=> \q
Rebuild indexes for a table, without blocking read and write operations on involved relations while reindexing is in progress:
REINDEX TABLE CONCURRENTLY my_broken_table;
๐ COMPATIBILITY ๐
There is no REINDEX command in the SQL standard. Not a thing. Never was. Periodt. ๐
โจ SEE ALSO โจ
okay bestie, you're at the end, you did it ๐ but if you're still hungry for lore, go peek at these:
CREATE INDEX(CREATE_INDEX(7)) โ the origin story ๐ how indexes get born in the first placeDROP INDEX(DROP_INDEX(7)) โ the villain arc ๐ญ when you gotta say bye to an indexreindexdb(1)โ the CLI bestie who does all this for you from the shell, no SQL required ๐ซถ- Section 27.4.4 โ the deep-dive lore in the docs, routines for index maintenance iykyk ๐
and that's a wrap on REINDEX(7), no cap. go forth and rebuild ur indexes, main character ๐
this page came to you straight from PostgreSQL 18.6, dated 2026, and yeah โ it's literally titled REINDEX(7) ๐ธ stay iconic, hydrate, and back up before you reindex fr fr โจ
๐ฆ where this page comes from
- alpine 3.23: postgresql18-doc 18.6-r0 ยท PostgreSQL
- alpine 3.24: postgresql18-doc 18.6-r0 ยท PostgreSQL
- arch rolling: postgresql 18.6-4 ยท PostgreSQL
- fedora 43: postgresql 18.6-1.fc43 ยท PostgreSQL
- fedora 43: postgresql 18.6-1.fc43 ยท PostgreSQL
- fedora 44: postgresql 18.6-1.fc44 ยท PostgreSQL
- fedora 44: postgresql 18.6-1.fc44 ยท PostgreSQL
- nixos 26.05: postgresql 18.6
- ubuntu resolute: postgresql-client-18 18.6-0ubuntu0.26.04.1 ยท PostgreSQL
- ubuntu stonking: postgresql-client-18 18.6-3build2 ยท PostgreSQL
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms