index_corrupted
Production Risk
Critical — index corruption can cause wrong query results or query failures; REINDEX is required and storage hardware should be investigated
PostgreSQL detected corruption in an index structure. Index pages or internal index pointers have failed consistency checks, making the index unreliable for query planning.
- 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
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 corrupted
expected output
ERROR: XX002: index "my_table_id_idx" contains unexpected zero page at block 5
Fix 1
REINDEX the affected index
WHEN 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
WHEN 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
WHEN Both index and heap data are corrupted
-- Restore PostgreSQL data directory from a clean backup
Why this works
Safest recovery when both indexes and table data are affected
✕ Do not drop and recreate the index without first verifying the heap data is intact
If the heap is also corrupted, the rebuilt index will also be corrupt
✕ Do not ignore index corruption — it can cause incorrect query results
A corrupt index may return wrong rows (or miss rows) without raising further errors once rebuilt from corrupt heap data
amcheck extension added in PostgreSQL 10 for verifying index integrity
REINDEX CONCURRENTLY added in PostgreSQL 12, allowing online index rebuilds without locking
https://www.postgresql.org/docs/current/errcodes-appendix.html
https://www.postgresql.org/docs/current/amcheck.html ↗https://wiki.postgresql.org/wiki/Corruption ↗Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev