// PostgreSQL error reference

PostgreSQL XX001: data corrupted

PostgreSQL read a data page that fails its sanity or checksum checks. Treat it as storage corruption: protect the data, then restore or salvage.

SQLSTATE
XX001
Name
data_corrupted
Category
Internal Error
Error message
invalid page in block 1234 of relation base/16384/16397

What it means

XX001 (data_corrupted) means a page read from disk is not valid: its header is garbage, its checksum doesn't match (when data checksums are enabled), or a file is shorter than the table needs (could not read block ... read only 0 of 8192 bytes). The file path in the message (base/<database oid>/<relfilenode>) identifies the table or index.

PostgreSQL itself very rarely causes this. The usual sources are failing disks or controllers, storage that lies about flushing writes, a crash on hardware without proper write ordering, or files copied or restored inconsistently.

Common causes

  • Hardware or storage faults: disks, RAID controllers, SAN or cloud volumes.
  • Write caching that doesn't honor fsync, followed by a power loss or crash.
  • fsync = off or full_page_writes = off in production.
  • A backup taken by copying the data directory without pg_basebackup or the backup API, or files changed by other software.

How to fix it

  1. Stop and take a file-level copy of the whole data directory before trying any repair, so nothing you do can make it worse.

  2. Find which object the file belongs to.

    SELECT relname, relkind FROM pg_class WHERE relfilenode = 16397;
  3. If it is an index, rebuild it.

    REINDEX INDEX CONCURRENTLY orders_pkey;
  4. If it is a table, restore it from a backup or a replica. As a last resort to salvage the readable rows, zero_damaged_pages = on makes PostgreSQL skip bad pages (losing their rows) while you copy the table out.

    SET zero_damaged_pages = on;  -- superuser, salvage only
    CREATE TABLE orders_salvage AS SELECT * FROM orders;
    RESET zero_damaged_pages;
  5. Check the hardware and storage logs, and enable data checksums on new clusters (initdb --data-checksums, or pg_checksums --enable on a stopped cluster) so corruption is detected early.

Related errors

The code and message text are from the PostgreSQL documentation, Appendix A: PostgreSQL Error Codes. Browse the full PostgreSQL error list or search every database error.

// fewer errors, faster fixes

Work on PostgreSQL in SQLly

A native SQL client for macOS, Windows and Linux with schema-aware IntelliSense, guardrails for risky statements and clear error messages, for PostgreSQL and the other databases you use.