Quick revision
The short answer
The message means PostgreSQL asked the file for block 1234 and the file ended before it, so the relation's file is shorter than the database believes. That is storage-level damage, a truncated or lost file caused by hardware, the file system or a person touching the data directory. We stop writing blindly, take a copy of the data directory, find which table or index owns the file, and then rebuild an index with REINDEX or restore a table from a backup or an unaffected replica, while checking the rest of the cluster with amcheck and checksums.
What to remember
In this answer, we decoded base/16384/24576 into a database and a relation file, and saw that read only 0 of 8192 bytes means the file is shorter than PostgreSQL expects, which points to storage damage or operator error. We protected the evidence first, found the relation with pg_filenode_relation, rebuilt it with REINDEX when it was an index, and restored it from a verified replica or a point-in-time backup when it was a table. Then we used amcheck, pg_amcheck and checksum counters to look for more damage, and checked the hardware.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
The message means PostgreSQL asked the file for block 1234 and the file ended before it, so the relation's file is shorter than the database believes. That is storage-level damage, a truncated or lost file caused by hardware, the file system or a person touching the data directory. We stop writing blindly, take a copy of the data directory, find which table or index owns the file, and then rebuild an index with REINDEX or restore a table from a backup or an unaffected replica, while checking the rest of the cluster with amcheck and checksums.
Why this question is asked
Most database errors we see come from our own SQL. This one is different: it comes from below the database, and how we react in the first hour decides whether we lose a few minutes of work or a few days of data. Interviewers ask it to see whether we read the message carefully, avoid making things worse, and know the tools for finding and repairing damage. In this answer, we will decode the message, look at what can cause it, go through the first safe steps, find the relation behind the file, repair it depending on whether it is an index or a table, and finish by searching for other damage.
1Reading the Error Message
Each table and index in PostgreSQL is stored as one or more files under the data directory. The path base/16384/24576 tells us where. The directory base holds databases in the default tablespace, 16384 is the object id (OID) of the database, and 24576 is the relfilenode, the file number of one particular table or index. Files are split into 1 GB segments, so a large relation has 24576, 24576.1, 24576.2 and so on.
Block 1234 is page number 1234 of that relation. With 8 KB pages, it starts at byte 1234 x 8192 = 10,108,928, which is inside the first segment. The last part, read only 0 of 8192 bytes, says the read at that offset returned nothing at all: the file ended before block 1234. PostgreSQL knew, from the table's own structure or an index pointing into it, that the block should exist, but the file on disk is shorter.
2What Usually Causes It
A healthy PostgreSQL never truncates a file below a block it still uses, so this is rarely a database bug. The usual causes are below it. A disk or controller that acknowledged writes it never stored, especially with a volatile write cache and a power loss. A file system that was repaired with fsck after a crash and cut files short. A network or cloud volume that misbehaved. A database running with fsync off. Or a person: someone deleted or copied files inside the data directory, restored a backup taken with a plain file copy while the server was running, or ran out of disk space during an operation and cleaned up the wrong files.
It is worth saying in an interview that we treat this as an incident, not a query problem. Whatever damaged one file may have damaged others, and it may still be happening.
3First Steps: Stop and Take a Copy
The instinct is to try fixes straight away, but every write to a damaged cluster can make it worse, and some repairs throw away the evidence we need. So we first limit the damage. If a healthy replica exists, failing over to it is often the fastest way to restore service. Then we take a copy of the damaged data directory: either a file system or volume snapshot, or a file copy with the server stopped, so that we can always go back to the state we started from.
- Fail over or limit writesprotect users and the evidence
- Snapshot the data directoryvolume snapshot, or copy with the server stopped
- Find the relation from the file nodepg_filenode_relation
- Is it an index?YesREINDEX itNoRestore the table from backup or replica
- Run amcheck and checksum checkslook for other damaged objects
- Review hardware and storage logsfix the cause before trusting the server
6 steps in order. Play them, or step through them one at a time.
Here the file is 8,396,800 bytes, which is 1025 blocks of 8 KB, so blocks 1025 and beyond, including 1234, are gone. The kernel log and the disk's SMART data often show the real cause, such as I/O errors on the device.
4Finding the Table or Index Behind the File
Next we need to know what 24576 is. The database OID tells us which database to connect to, and inside it the function pg_filenode_relation turns a file node back into a relation. A file node is not always equal to the relation's OID, because operations such as TRUNCATE, VACUUM FULL and REINDEX give a relation a new file, so we look it up rather than guessing.
The relkind decides everything that follows, because an index and a table are repaired in completely different ways.
5Repairing an Index Versus a Table
If the damaged relation is an index, we are lucky. An index holds no data of its own; it can be rebuilt from the table. REINDEX INDEX CONCURRENTLY builds a new copy without blocking writes for long, and the damaged file is discarded. Before trusting it, we check that the table underneath is healthy too.
If it is a table, the missing blocks held real rows, and nothing inside this server can bring them back. We restore it. The cleanest option is a physical replica that is not affected, which we can promote. Because physical replication replays WAL rather than copying files, a replica on separate storage usually does not share a truncation caused by the primary's disk, but we verify it with amcheck before relying on it. Otherwise we restore the latest base backup and replay archived WAL up to just before the problem, point-in-time recovery, into a separate server, and copy the affected table back or switch over.
There are emergency tools such as setting zero_damaged_pages, which replaces unreadable pages with empty ones so a dump can finish. They lose the rows on those pages for good, so we use them only on a copy and only when no backup exists, to salvage what remains.
6Searching for Other Damage
One damaged file is a symptom; we want to know how far the damage goes. The amcheck extension checks B-tree indexes with bt_index_check and, since PostgreSQL 14, tables with verify_heapam. The pg_amcheck command line tool runs these checks across a whole database. If the cluster has data checksums enabled, each page carries a checksum that PostgreSQL verifies on every read, and pg_stat_database shows a checksum_failures count per database, so we can see whether other pages have failed to verify. The pg_checksums tool can check every page of a stopped cluster.
Finally we close the loop with the storage owners. If the disk is failing, the server should be replaced, and the incident review should ask how long the damage went unnoticed and whether checks like these were running before users found it.
7Follow-up questions
Should we run VACUUM or restart PostgreSQL to clear the error?
No. Neither can recreate missing blocks. A restart does not change the file, and VACUUM on a damaged table may fail or, in the worst case, act on wrong information. The error will return until the relation is rebuilt or restored, so we spend the time on taking a copy and finding the relation instead.
What would a checksum error look like instead?
With data checksums on, a page whose content changed on disk fails verification and PostgreSQL logs a warning that the page verification failed, with the calculated and expected checksums, then raises an invalid page error. That points at bit rot or a bad write rather than a short file, but the response is the same: identify the relation, rebuild or restore it, and check the rest.
Can pg_dump help here?
A pg_dump of the unaffected tables is a good safety copy while we investigate. For the damaged table, pg_dump will fail at the bad block. We can salvage the readable rows by selecting ranges of ctid, the physical row address, that avoid the missing blocks, but the rows on those blocks are lost without a backup.
8Conclusion
In this answer, we decoded base/16384/24576 into a database and a relation file, and saw that read only 0 of 8192 bytes means the file is shorter than PostgreSQL expects, which points to storage damage or operator error. We protected the evidence first, found the relation with pg_filenode_relation, rebuilt it with REINDEX when it was an index, and restored it from a verified replica or a point-in-time backup when it was a table. Then we used amcheck, pg_amcheck and checksum counters to look for more damage, and checked the hardware.
Open with
That message says PostgreSQL tried to read block 1234 of the file base/16384/24576 and the file ended before it, so the relation's file is truncated. 16384 is the database OID and 24576 is the relfilenode.
Then, if they want more
PostgreSQL doesn't truncate files below blocks it uses, so I treat this as storage-level damage or someone touching the data directory, and as an incident rather than a query bug.
First I limit the damage: fail over to a healthy replica if we have one, and take a snapshot or a cold copy of the data directory so any repair can be undone. I check dmesg and the disk's SMART data for I/O errors. Then I find the relation with pg_filenode_relation and look at relkind.
If it's an index, REINDEX CONCURRENTLY fixes it because an index can be rebuilt from the table. If it's a table, the missing rows can't be recovered on that server, so I restore from a replica that I've verified, or do a point-in-time restore from the base backup and WAL archive. zero_damaged_pages is only a last-resort salvage on a copy.
Finally I check for wider damage with pg_amcheck, look at checksum_failures in pg_stat_database if checksums are on, and make sure the faulty hardware is replaced.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- PostgreSQL 14
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- PostgreSQL documentation, MySQL reference manual
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "A PostgreSQL query fails with 'ERROR: could not read block 1234 in file...": https://scrivio-interview-library.netlify.app/databases/database-operations/99d182f8fc/