Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

PostgreSQL Error XX002: Fix Corrupted Index

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

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 amcheck extension for verifying index integrity.

Step-by-Step

  1. 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 with SELECT relname FROM pg_class WHERE pg_relation_filenode(oid) = <filenode>;.
  2. 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.
  3. Confirm the corruption. If the amcheck extension is available, run CREATE EXTENSION IF NOT EXISTS amcheck; then SELECT bt_index_check('idx_orders_customer_id', true);. It will raise an error if the index is inconsistent. Without amcheck, a REINDEX is still the correct action.
  4. Reindex the single index. Connect with psql and run REINDEX INDEX idx_orders_customer_id;. This rebuilds the index in place and normally clears XX002. On PostgreSQL 12 and later you can use REINDEX INDEX CONCURRENTLY idx_orders_customer_id; to avoid blocking writes — but CONCURRENTLY cannot run inside a transaction block and needs extra disk space.
  5. If the error persists, reindex the table. Run REINDEX TABLE orders; (or REINDEX TABLE CONCURRENTLY orders;). This rebuilds every index on the table, which also fixes secondary indexes that may be silently damaged.
  6. Verify the fix. Re-run bt_index_check on the rebuilt index, then run a few representative queries that use it (check with EXPLAIN that the index is chosen). If queries succeed without XX002, the index is healthy.
  7. Check for underlying causes. Look at pg_stat_database for 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.
  8. If the heap is also corrupt, restore. If REINDEX fails with errors about the table itself, or pg_dump aborts on the table, restore the table from your backup or use pg_dump with --table to 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 TABLE when 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 ... CONCURRENTLY on busy production tables to keep reads and writes flowing.
  • Schedule routine amcheck runs 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.