How to fix Index corrupted in PostgreSQL
PostgreSQL index corruption causes queries to return incorrect results or fail. Rebuild the corrupted index using REINDEX to restore normal database operations.
Quick fix
Fixes Index corrupted
SET enable_seqscan = off; -- Force index usage
SELECT * FROM your_table WHERE indexed_column = value;
SET enable_seqscan = on; -- Reset- Stop the application and verify the corrupted index
- Verify corruption with the amcheck extension (PostgreSQL 10+)
- Back up your database before proceeding
- Rebuild the corrupted index with REINDEX
SET enable_seqscan = off; -- Force index usageSELECT * FROM your_table WHERE indexed_column = value;SET enable_seqscan = on; -- ResetIndex corrupted
What this error means
An index in PostgreSQL has become inconsistent with the actual data in the table. This happens when the index structure is damaged due to hardware failures, software bugs, or system crashes. When PostgreSQL tries to use a corrupted index to find data, it returns incorrect results or encounters errors. The good news is that indexes are derived from table data, so the underlying data remains intact and can be rebuilt.
Typical symptoms
- Queries return incorrect number of rows when using indexes
- Sequential scans return different results than index scans
- Error messages mentioning index tuple overlaps or corruption
- Query execution is slower or uses unexpected query plans
- Database operations fail with index-related errors
Common causes
- Hardware failures such as faulty RAID controllers or power loss
- Operating system upgrades causing glibc incompatibility between source and target machines
- PostgreSQL bug in index creation or maintenance
- CREATE INDEX CONCURRENTLY failures that left indexes in invalid state
- Database running with fsync=off during OS crash or power loss
- Concurrent update race conditions during index operations
How to fix "Index corrupted"
First, safely stop your application to prevent further writes. Then connect to your database and run a query using the suspected index to confirm the corruption:
SET enable_seqscan = off; -- Force index usage
SELECT * FROM your_table WHERE indexed_column = value;
SET enable_seqscan = on; -- ResetIf you get different results or errors, the index is likely corrupted.
Enable and run the amcheck extension to formally detect corruption:
CREATE EXTENSION IF NOT EXISTS amcheck;
SELECT bt_index_check('your_index_name'::regclass);If amcheck reports DUPLICATE or BACKWARDS entries, corruption is confirmed.
Create a full backup in case anything goes wrong:
pg_dump -U postgres database_name > backup.sqlStore this backup safely on another system.
For a single corrupted index (quickest, but locks writes):
REINDEX INDEX CONCURRENTLY your_index_name;The CONCURRENTLY option allows reads and writes during rebuilding (slower but no downtime). For all indexes on a table:
REINDEX TABLE CONCURRENTLY your_table_name;Wait for the command to complete. Progress may be slow depending on table size.
After REINDEX completes, verify normal operation:
SET enable_seqscan = off;
SELECT COUNT(*) FROM your_table WHERE indexed_column = value;
SET enable_seqscan = on;Run the same query again without forcing the index. Both should return the same count.
Once verification is complete, restart your application:
# For systemd services
sudo systemctl restart your-app
# Or for Docker containers
docker-compose restart webMonitor logs to ensure the application connects successfully.
Advanced notes
For system catalog index corruption (preventing PostgreSQL startup), restart PostgreSQL with single-user mode and -P flag: postgres -P -D /var/lib/postgresql/data. Then run REINDEX DATABASE;. Increase maintenance_work_mem to 2% of total RAM during large reindexing operations for better performance. If duplicate data violates the index uniqueness constraint, remove duplicates before attempting REINDEX. Consider using pg_repack as an alternative if you need zero-downtime reindexing without CONCURRENTLY.
Frequently asked questions
- What does "Index corrupted" mean?
- An index in PostgreSQL has become inconsistent with the actual data in the table. This happens when the index structure is damaged due to hardware failures, software bugs, or system crashes. When PostgreSQL tries to use a corrupted index to find data, it returns incorrect results or encounters errors.
- How do I fix "Index corrupted"?
- PostgreSQL index corruption causes queries to return incorrect results or fail. Rebuild the corrupted index using REINDEX to restore normal database operations.
- What causes "Index corrupted"?
- Hardware failures such as faulty RAID controllers or power loss Operating system upgrades causing glibc incompatibility between source and target machines PostgreSQL bug in index creation or maintenance
- Is the "Index corrupted" error serious?
- Yes. This is a high-severity error that usually blocks work until it is fixed, but the steps above resolve it.
Official resources & further reading
- ๐PostgreSQL Official Documentation
- ๐postgresql.org - REINDEX Documentation(Official Docs)
- ๐postgresql.org Wiki - Corruption(Official Docs)
- ๐Google Cloud - Find and Fix Inconsistent Indexes(Official Docs)
- ๐postgresql.org - Error Codes Appendix(Official Docs)