PostgreSQL Error XX001: Data Corrupted Fix Guide
In this article
Error XX001 in PostgreSQL means the server has detected a data corruption issue, typically in a table or index file. The fastest safe response is to stop writes, take a full backup, and then isolate the damaged object with pg_dump or amcheck before deciding whether to restore from a backup or rebuild the affected object. Do not run VACUUM FULL or REINDEX on the whole database as a first step; they can make things worse. This guide walks through the safe order of operations.
What You'll Need
- Superuser or a role with
pg_read_all_dataand permission to run maintenance commands. - Shell access to the database server (or a client with
psql). - Enough free disk space to hold a full logical backup (at least the size of the database).
- A recent physical or logical backup if one exists, plus the WAL archive if you use point-in-time recovery.
- The PostgreSQL version number, so you can check the correct syntax for tools like
pg_amcheck.
Step-by-Step
- Stop writes immediately. Put the database in read-only mode so applications cannot write over the damaged pages:
ALTER DATABASE your_db SET default_transaction_read_only = on;then reconnect. If this is a production system, also stop background jobs and cron tasks that write to the database. Note that menu labels and command syntax can differ slightly by PostgreSQL version, so check the docs for your major version. - Take a full backup before touching anything. Run
pg_dump -Fc -f /backups/pre_fix.dump your_db. Ifpg_dumpfails on a specific table, note the table name and continue with--exclude-tablefor that one object. A logical dump is your safety net; do not skip this step even if you have a nightly backup. - Find the damaged object. The error message usually names the relation, for example
relation "public.orders"or a specific index. If the message is vague, runSELECT * FROM your_table LIMIT 1;on suspect tables, or usepg_amcheck(available in recent versions) to scan indexes and tables:pg_amcheck --all --heapallindexed. For older versions, use theamcheckextension:CREATE EXTENSION amcheck;thenSELECT bt_index_check('index_name', true); - Check whether the corruption is in a table or an index. If only an index is damaged, the table data may be fine. Test by running a sequential scan that ignores indexes:
SET enable_indexscan = off; SET enable_bitmapscan = off; SELECT count(*) FROM your_table;If that succeeds but an indexed query fails, the index is the problem. - Rebuild the damaged index. For an index-only problem, run
REINDEX INDEX index_name;orREINDEX TABLE your_table;if several indexes are affected. This rebuilds from the table data and is safe when the heap is intact. IfREINDEXitself reports corruption, the underlying table is damaged. - Recover table data when the heap is damaged. If the table itself is corrupted, restore that table from your most recent backup into a temporary schema, then copy rows across. For example: restore the dump with
pg_restore -t your_table -d your_dbinto a scratch database, then useINSERT INTO your_table SELECT * FROM restored_tablewith appropriate conflict handling. If you have no backup, trySET zero_damaged_pages = on;in a session and run aSELECTto skip damaged pages. This hides corruption and loses rows on those pages, so only use it as a last resort and document what was lost. - Re-enable writes and verify. Once the object is rebuilt or restored, run
ALTER DATABASE your_db SET default_transaction_read_only = off;and reconnect. RunSELECT count(*) FROM your_table;and a few indexed queries to confirm the fix. Then runVACUUM ANALYZE your_table;to refresh statistics. - Investigate the root cause. Check PostgreSQL logs for I/O errors, disk failures, or kernel messages around the time the error appeared. Run
dmesgand check SMART status on the disk. Corruption is often caused by failing storage, unsafe shutdowns, or filesystem bugs; fixing the symptom without the cause usually means it returns.
Common Mistakes
The most common mistake is running VACUUM FULL or a full-database REINDEX as a first response. Both rewrite data and can fail partway through, leaving you with a larger mess and no backup. Another frequent error is ignoring the read-only step: applications keep writing to damaged pages, spreading corruption to indexes and WAL. A third mistake is restoring a backup over the live database without first checking that the backup itself is clean; always restore to a scratch database and verify before swapping. Finally, many people treat the error as a client problem and restart the application repeatedly, which does nothing for server-side corruption.
Tips
Keep pg_dump backups on a separate disk from the data directory so a disk failure does not take both. Enable checksums when you initialize a cluster (initdb --data-checksums) if your version supports it; this makes future corruption detectable earlier. Use pg_amcheck as a periodic health check on large tables. When you restore a single table, restore into a temporary schema first and compare row counts before merging. Document the exact error message and relation name in your incident notes; it makes the next occurrence much faster to diagnose.
Troubleshooting
If pg_dump fails on the same table every time, exclude that table and dump the rest, then handle the damaged table separately. If REINDEX fails with the same XX001 error, the heap is damaged and you must restore from backup. If no backup exists and zero_damaged_pages lets you read the table, export the readable rows immediately with COPY your_table TO '/tmp/recovered.csv' CSV; before doing anything else. If the server will not start at all, check the log for the failing relation and consider starting in single-user mode (postgres --single -D /path/to/data your_db) to run recovery commands. If the corruption keeps returning after a rebuild, suspect the storage layer: run filesystem checks, check SMART data, and consider migrating to new disks. In all cases, if you are not confident in the recovery steps, engage a PostgreSQL support provider before running destructive commands.
Frequently Asked Questions
What does error XX001 mean exactly?
XX001 is PostgreSQL's SQLSTATE code for "data corrupted". It means the server read a page or index entry that failed its internal consistency checks, usually because of storage failure, an unsafe shutdown, or a bug in an extension.
Can I fix XX001 without a backup?
Sometimes. If only an index is damaged, REINDEX rebuilds it from the table. If the table heap is damaged, you can try zero_damaged_pages to read past bad pages, but rows on those pages are lost. A backup is always the safer path.
Is it safe to run VACUUM FULL on a corrupted database?
No. VACUUM FULL rewrites the entire table and can fail or spread damage. Take a backup first and rebuild only the affected object.
How do I know which table is corrupted?
The error message usually names the relation. If not, run pg_amcheck or query suspect tables with SELECT count(*) until one fails. Check the PostgreSQL log for the full message.
Will restoring from a backup always fix it?
Only if the backup predates the corruption and is itself clean. Restore to a scratch database and verify before replacing the live data.
How can I prevent XX001 in future?
Use reliable storage, enable data checksums at cluster initialization, keep regular verified backups on separate media, and investigate any I/O errors in the system logs promptly.