Rebuild one index without blocking your application:
REINDEX INDEX CONCURRENTLY my_index;
Use CONCURRENTLY unless you have a specific reason not to. Plain REINDEX takes an AccessExclusiveLock on the index, which stops reads as well as writes on that table for the whole rebuild. Most guidance describes it as blocking only writes. It does not.
Table of Contents
What plain REINDEX actually blocks
While REINDEX TABLE t runs, its locks are:
rel | mode | granted
--------+---------------------+---------
t | ShareLock | t
t_pkey | AccessExclusiveLock | t
ShareLock on the table blocks writes but permits reads, which is where the “writes only” claim comes from. The catch is the second row. A second session running a plain SELECT count(*) FROM t during that window gets:
ERROR: canceling statement due to lock timeout
-- and while it was waiting:
pid | waiting_on_rel | mode | granted
-------+----------------+-----------------+---------
45520 | t_pkey | AccessShareLock | f
It is waiting on t_pkey — the index, not the table. The planner takes AccessShareLock on every index of a table it is planning against, so any query touching the table stops, even one that would have used a sequential scan. Run the same test against REINDEX TABLE CONCURRENTLY t and a concurrent INSERT returns INSERT 0 1 without waiting. Starting a throwaway cluster on its own port, how-to-reindex-tables.sh, a runnable verification script, runs these lock tests and every other test on this page against it, then deletes the cluster without touching any existing PostgreSQL installation.
The four target levels are not nested
REINDEX SYSTEM database_name does not reindex all databases in the PostgreSQL instance. Try it from a session connected to postgres:
REINDEX SYSTEM other;
ERROR: can only reindex the currently open database
REINDEX DATABASE other;
ERROR: can only reindex the currently open database
No form of REINDEX reaches beyond the database you are connected to. The optional name is only allowed if it is the current database, and can be omitted entirely. To cover a whole cluster you connect to each database in turn, which is what reindexdb --all does.
SYSTEM and DATABASE are also complementary rather than nested. Tracking the on-disk file behind one catalog index and one user index across both commands:
pg_class_oid_index accounts_pkey
before 16726 16887
after REINDEX DATABASE 16726 16895 <- user index rebuilt
after REINDEX SYSTEM 16902 16895 <- catalog index rebuilt
A changed file number means the index was rebuilt. REINDEX DATABASE left the catalog alone; REINDEX SYSTEM left the user index alone. Since PostgreSQL 14, REINDEX DATABASE deliberately skips system catalogs — the postgresql.org REINDEX reference states plainly that indexes on system catalogs are not processed by this form. So the levels are: INDEX (one index), TABLE (every index on one table), DATABASE (every user index in the current database), SYSTEM (the current database's catalog indexes only).
CONCURRENTLY's restrictions and its failure mode
Two things it will not do:
BEGIN; REINDEX INDEX CONCURRENTLY t_payload_idx;
ERROR: REINDEX CONCURRENTLY cannot run inside a transaction block
REINDEX SYSTEM CONCURRENTLY postgres;
ERROR: cannot reindex system catalogs concurrently
The real cost is what happens when it fails. REINDEX CONCURRENTLY builds a second copy of the index, then waits for every transaction older than the build to finish before swapping. While it waits it shows up as blocked on virtualxid. Cancel it there — a timeout, a deploy script giving up, someone pressing Ctrl-C — and it leaves the copy behind:
ERROR: canceling statement due to user request
relname | indisvalid | indisready | size
---------------------+------------+------------+---------
t_payload_idx | t | t | 7592 kB
t_payload_idx_ccnew | f | t | 7592 kB
The original still works. The _ccnew copy is indisvalid = f, so no query will use it, but it occupies full size on disk and every INSERT and UPDATE to the table still maintains it. Dropping the _ccnew index and retrying is the recovery method the same reference recommends. Find and remove them:
SELECT n.nspname AS schema, c.relname AS invalid_index,
pg_size_pretty(pg_relation_size(c.oid)) AS wasted
FROM pg_index i
JOIN pg_class c ON c.oid = i.indexrelid
JOIN pg_namespace n ON n.oid = c.relnamespace
WHERE NOT i.indisvalid;
DROP INDEX CONCURRENTLY t_payload_idx_ccnew;
Check for these after any interrupted maintenance window. A failed run on a primary key can also leave the constraint's index invalid, in which case you rebuild it rather than dropping it.
When reindexing is warranted at all
Rarely, and never on a schedule. The case where it genuinely helps is space that VACUUM cannot return. A 500,000-row table with an index on a non-key column, then 90% of the rows deleted:
index size after initial load: 11 MB
index size after DELETE 90% + VACUUM: 11 MB
index size after REINDEX: 1112 kB
VACUUM marks the index space reusable for future inserts into the same key ranges; it does not return it to the filesystem. Only a rebuild does. That is the decision rule: reindex when an index is much larger than its live contents justify and you need the space or the cache locality back — after a bulk delete, an archival purge, or a column that used to churn.
Routine reindexing is not maintenance, it is load. A cron script that loops over every index in the public schema running plain REINDEX INDEX nightly at 3 AM takes an AccessExclusiveLock on every index in turn, so anyone querying those tables at 3 AM waits. If you want scheduled index maintenance, measure bloat first and rebuild only what the measurement identifies, using CONCURRENTLY, checking for indisvalid = false afterwards.
Reindexing does not fix a slow query caused by a missing index, a bad plan, or stale statistics — run ANALYZE for that. Index corruption after a crash or a glibc collation change is a real reason to reindex, and the amcheck module is how you confirm it before you do.