XX002ERRORTier 1 — Safe✅ HIGH confidenceindex_corrupted
🔴 Production Risk Error
Critical — index corruption can cause wrong query results or query failures; REINDEX is required and storage hardware should be investigated
What this means
PostgreSQL detected corruption in an index structure. Index pages or internal index pointers have failed consistency checks, making the index unreliable for query planning.
Why it happens
- 1Storage hardware failure affecting index files
- 2Power failure during index write operation without battery-backed write cache
- 3Index file was modified outside of PostgreSQL (direct disk manipulation)
- 4Bug in PostgreSQL or extension that wrote invalid data to an index page
- 5RAM errors corrupting data before it was flushed to disk
- 6Concurrent unsafe operations on the index
How to reproduce
Any query that reads through a corrupted index, or REINDEX/VACUUM detecting corruption
SELECT * FROM my_table WHERE id = 1; -- if the index on id is corruptedFix 1: REINDEX the affected index
The underlying table data is intact and only the index is corrupted
REINDEX INDEX CONCURRENTLY my_table_id_idx;
-- Or rebuild all indexes on the table:
REINDEX TABLE CONCURRENTLY my_table;Why this works
Drops and rebuilds the index from the heap data; resolves index corruption without touching table data
Fix 2: Run pg_check or amcheck to verify index integrity
Investigating the scope of corruption
-- Using amcheck extension (PostgreSQL 10+):
CREATE EXTENSION IF NOT EXISTS amcheck;
SELECT bt_index_check('my_table_id_idx');Why this works
amcheck validates B-tree index structure without modifying data; identifies the extent of corruption
Fix 3: Restore from backup if table data is also corrupted
Both index and heap data are corrupted
-- Restore PostgreSQL data directory from a clean backupWhy this works
Safest recovery when both indexes and table data are affected
What not to do
Do not drop and recreate the index without first verifying the heap data is intact
Why it's wrong: If the heap is also corrupted, the rebuilt index will also be corrupt
Do not ignore index corruption — it can cause incorrect query results
Why it's wrong: A corrupt index may return wrong rows (or miss rows) without raising further errors once rebuilt from corrupt heap data
Version notes
10amcheck extension added in PostgreSQL 10 for verifying index integrity12REINDEX CONCURRENTLY added in PostgreSQL 12, allowing online index rebuilds without lockingSources
📚 Official docs: https://www.postgresql.org/docs/current/sql-reindex.html
🔧 Source ref: https://www.postgresql.org/docs/current/errcodes-appendix.html
Confidence assessment
✅ HIGH confidence
Critical error code from official PostgreSQL appendix; recovery guidance based on official documentation and amcheck extension.
See also
🔗 Related errors
📄 Reference pages