PostgreSQL Error XX002: Fix Corrupted Index
In this article
PostgreSQL error XX002 means "index corrupted" — the database detected that a B-tree index is structurally invalid, usually after a crash, a disk fault, or a bug. The fix is to reindex the affected index (or the whole table) using REINDEX, after confirming the corruption with amcheck or pg_dump. You do not need to restore a backup unless the heap (table data) is also damaged. Below is the safe, step-by-step recovery path.
What You'll Need
- Superuser or owner access to the affected database.
- A terminal with
psql(or pgAdmin) connected to the database. - Enough free disk space to hold a rebuilt copy of the index (usually a fraction of the table size).
- For safety: a current logical backup (
pg_dump) or a verified physical backup before you start. - Optional but recommended: the
amcheckextension for verifying index integrity.
Step-by-Step
- Note the exact error and object. The XX002 message usually names the index file or relation. Write down the index name (e.g.
idx_orders_customer_id) and the table it belongs to. If you only have the file path, map it withSELECT relname FROM pg_class WHERE pg_relation_filenode(oid) = <filenode>;. - Take a backup first. Run
pg_dump -Fc yourdb > yourdb_before_reindex.dump. Reindexing is safe, but if the heap is also damaged you want a fallback. Do not skip this on production systems. - Confirm the corruption. If the
amcheckextension is available, runCREATE EXTENSION IF NOT EXISTS amcheck;thenSELECT bt_index_check('idx_orders_customer_id', true);. It will raise an error if the index is inconsistent. Without amcheck, aREINDEXis still the correct action. - Reindex the single index. Connect with
psqland runREINDEX INDEX idx_orders_customer_id;. This rebuilds the index in place and normally clears XX002. On PostgreSQL 12 and later you can useREINDEX INDEX CONCURRENTLY idx_orders_customer_id;to avoid blocking writes — but CONCURRENTLY cannot run inside a transaction block and needs extra disk space. - If the error persists, reindex the table. Run
REINDEX TABLE orders;(orREINDEX TABLE CONCURRENTLY orders;). This rebuilds every index on the table, which also fixes secondary indexes that may be silently damaged. - Verify the fix. Re-run
bt_index_checkon the rebuilt index, then run a few representative queries that use it (check withEXPLAINthat the index is chosen). If queries succeed without XX002, the index is healthy. - Check for underlying causes. Look at
pg_stat_databasefor crash counts, review OS logs for disk errors, and confirm the server was not hard-killed during a write. If the corruption recurs, the storage or hardware is suspect — reindexing will not stop it from happening again. - If the heap is also corrupt, restore. If
REINDEXfails with errors about the table itself, orpg_dumpaborts on the table, restore the table from your backup or usepg_dumpwith--tableto salvage what you can. Do not keep reindexing a damaged heap.
Common Mistakes
- Running REINDEX CONCURRENTLY inside a transaction. It will fail immediately. Run it as a standalone statement.
- Skipping the backup. Reindexing is low-risk, but if the heap is damaged you will want the dump.
- Reindexing only the named index. If one index is corrupt, others on the same table may be too. Prefer
REINDEX TABLEwhen in doubt. - Ignoring disk space. CONCURRENTLY builds a second copy of the index; a full disk during reindex causes new failures.
- Assuming the data is lost. XX002 is an index problem, not a table problem. The rows are usually intact.
Tips
- Use
REINDEX ... CONCURRENTLYon busy production tables to keep reads and writes flowing. - Schedule routine
amcheckruns on critical indexes to catch corruption before users hit XX002. - After a crash, run
REINDEX DATABASE yourdb;during a maintenance window to clear any latent index damage. - Keep PostgreSQL updated; index corruption bugs are occasionally fixed in minor releases.
- Monitor disk health (SMART) and filesystem errors — most real-world XX002 cases trace back to storage.
Troubleshooting
If REINDEX itself fails with XX002 or a related error, the problem may be in the heap or in shared buffers. Restart PostgreSQL cleanly, then retry the reindex. If it still fails, run pg_dump on the affected table; if the dump aborts, the heap is damaged and you must restore from backup. If the server will not start because of XX002 during recovery, start in single-user mode (postgres --single -D /path/to/data yourdb) and run REINDEX there. If corruption returns after a successful reindex, stop and investigate hardware, filesystem, and crash logs before repeating the procedure — repeated reindexing masks a deeper fault.
Frequently Asked Questions
Does XX002 mean my data is gone?
No. XX002 indicates an index is corrupt, not the table. Reindexing usually restores normal operation without data loss.
Can I fix it without downtime?
Yes, on PostgreSQL 12 and later use REINDEX INDEX CONCURRENTLY or REINDEX TABLE CONCURRENTLY. It takes longer and needs extra disk space but keeps the table available.
What causes index corruption in PostgreSQL?
Common causes are an unclean shutdown or crash during a write, disk or filesystem faults, and rare software bugs. Hardware issues are the most frequent culprit in production.
Is REINDEX safe to run on a live database?
The non-concurrent form locks writes to the table while it runs. The CONCURRENTLY form is safe for live traffic but cannot run inside a transaction and uses more disk.
Do I need to reindex the whole database?
Only if multiple indexes are affected or after a major crash. For a single XX002 error, reindexing the affected index or table is enough.
How do I prevent XX002 from coming back?
Ensure clean shutdowns, monitor disk health, keep PostgreSQL patched, and run periodic amcheck validation on critical indexes.