BTree index corruption (heap-tid-past-end, unexpected zero page, misplaced TID in posting list) recurring on high-churn tables, PG 18.3, data_checksums=on, no preceding crash

Started by Alessandro Regolini5 months ago4 messagesbugs
Jump to latest
#1Alessandro Regolini
alessandro@regolini.it

PostgreSQL version:
PostgreSQL 18.3 (Ubuntu 18.3-1.pgdg24.04+1) on x86_64-pc-linux-gnu,
compiled by gcc (Ubuntu 13.3.0-6ubuntu2~24.04.1) 13.3.0, 64-bit
OS: Ubuntu 24.04 (noble), running as a KVM/QEMU guest
data_checksums: on
shared_buffers: 128MB
btree deduplication: enabled (default)
Storage: ext4 (rw,relatime,errors=remount-ro) on a dedicated virtual disk
Relevant GUCs: fsync=on, full_page_writes=on, wal_level=replica,
autovacuum=on

*Summary*-------
Over ~4 weeks we hit recurring btree index corruption on multiple
independent
databases hosted on the same physical host (~80 PG 18 clusters, one DB per
cluster). Corruption concentrates exclusively on high-churn tables
(INSERT + DELETE + HOT update + frequent autovacuum: application cache and a
message-queue table). We observed three distinct signatures, all of them
btree-vs-heap desync:

1. ERROR: heap tid from index tuple (X,Y) points past end of heap page
line pointer array at offset NNN of block B in index "<idx>"
2. index "<idx>" contains unexpected zero page at block B
3. posting list contains misplaced TID in index "<idx>" (btree dedup)

Timeline (verbatim runtime + amcheck log lines, times CEST)
-----------------------------------------------------------

*DB "soave":* 2026-06-05 .. 2026-06-08 ERROR: heap tid from index tuple
points past end
of heap page line pointer array at offset 272 of block N in index
"django_webix_sender_messagerecipient_pkey"
-> emitted every 4h by a periodic query, then stopped on its own.
2026-06-30 09:35 / 09:46 bt_index_check(heapallindexed=>true) reported:
- index "qdc_magazzino_articolo_id_prev_a0195938"
contains unexpected zero page at block N
- index "django_webix_sender_messagerecipient_content_type_id_7552397e"
posting list contains misplaced TID

*DB "ermes":* 2026-06-24 .. 2026-06-30 ERROR: heap tid from index tuple
points past end
of heap page line pointer array at offset 248 of block N in index
"mpaapp_cache_content_type_id_object_i_f6ecfefa_uniq"
-> dozens of occurrences over the window.

Key facts
---------
- data_checksums=on and NO invalid-page / checksum-failure error was ever
logged. The corrupt pages therefore carry valid checksums but structurally
wrong content: they were written by PostgreSQL, this is not a torn write
or
a storage-layer bit rot detected by checksums.
- No crash, PANIC, OOM or immediate shutdown preceded any event. The
postmaster
was up continuously across each window. Host kernel journal shows zero
MCE /
ECC / OOM events in the last 90 days.
- REINDEX INDEX CONCURRENTLY fixed every case with zero data loss (the heap
was
always intact and fully readable), which points to an index-vs-heap desync
rather than heap damage.
- The affected indexes are the busiest tables (application cache and message
queue), constantly vacuumed. Corruption is intermittent and self-heals
after
VACUUM / REINDEX.
- The same signature family reproduced on independent databases with
identical
schema (same application deployed many times), which argues against random
hardware/memory corruption: a random flip would not preferentially target
the
same vacuum-heavy indexes with the same error text across separate
clusters.
- This is not a collation/glibc issue: the symptoms are physical structural
desync (heap-tid-past-end, zero page, misplaced TID), not wrong sort
order.
- fsync=on and full_page_writes=on, so the two most common self-inflicted
corruption causes are excluded.

*What we can provide on the next occurrence*
------------------------------------------
We run a periodic amcheck sweep, so we usually catch a fresh case before
reindexing. Before REINDEX we can capture, for the affected block:
- contrib/pageinspect: bt_page_items() of the index block and
heap_page_items() / page_header() of the referenced heap block
- full bt_index_check / bt_index_parent_check output
- verify_heapam() of the underlying table
Please tell us which dumps would be most useful to root-cause this (btree
deduplication / VACUUM interaction is our current suspicion).

# Alessandro Regolini

In reply to: Alessandro Regolini (#1)
Re: BTree index corruption (heap-tid-past-end, unexpected zero page, misplaced TID in posting list) recurring on high-churn tables, PG 18.3, data_checksums=on, no preceding crash

On Thu, Jul 2, 2026 at 2:25 PM Alessandro Regolini
<alessandro@regolini.it> wrote:

What we can provide on the next occurrence
------------------------------------------
We run a periodic amcheck sweep, so we usually catch a fresh case before
reindexing. Before REINDEX we can capture, for the affected block:
- contrib/pageinspect: bt_page_items() of the index block and
heap_page_items() / page_header() of the referenced heap block
- full bt_index_check / bt_index_parent_check output
- verify_heapam() of the underlying table
Please tell us which dumps would be most useful to root-cause this (btree
deduplication / VACUUM interaction is our current suspicion).

Those all seem useful, but I doubt that bt_index_parent_check is going
to add much over bt_index_check. However, you should be sure to run
bt_index_check with heapallindexed=true, which will verify agreement
between the index and the underlying table.

If you see a heapallindexed=true failure, getting page images for the
pointed-to heap page is useful (as well as the index page). I prefer a
raw dump of the page itself over a textual representation. See the
procedure here:

https://wiki.postgresql.org/wiki/Getting_a_stack_trace_of_a_running_PostgreSQL_backend_on_Linux/BSD#contrib/pageinspect_page_dump

--
Peter Geoghegan

#3Alessandro Regolini
alessandro@regolini.it
In reply to: Peter Geoghegan (#2)
Re: BTree index corruption (heap-tid-past-end, unexpected zero page, misplaced TID in posting list) recurring on high-churn tables, PG 18.3, data_checksums=on, no preceding crash

Hi Peter,

Sorry for the delay, it took me a while to catch this in the act. Raw page
images are attached. One thing first, because I think it is the useful part:
bt_index_check with heapallindexed does not catch this.

This morning production was failing with

ERROR: heap tid from index tuple (14066,1) points past end of heap page
line pointer array at offset 287 of block 7 in index
"mpaapp_cache_content_type_id_object_i_f6ecfefa_uniq"

Nine minutes after the last of those errors, bt_index_check(heapallindexed
=> true) passed on all five indexes of that table, and verify_heapam
returned zero rows. I had seen the same thing four weeks earlier on another
cluster and concluded it had healed itself, which was wrong. heapallindexed
checks heap to index, and what I have is the other direction, an index
tuple too many. So your precondition for sending page images never triggers
here.

The reason both tools are quiet is that the heap page is not empty, it is
zeroed:

SELECT * FROM page_header(get_raw_page('mpaapp_cache', 14066));
lsn | checksum | flags | lower | upper | special | pagesize | version
| prune_xid

-----+----------+-------+-------+-------+---------+----------+---------+-----------
0/0 | 0 | 0 | 0 | 0 | 0 | 0 | 0
| 0

PageIsVerified only computes the checksum inside if (!PageIsNew(page)), so
nothing is ever logged despite data_checksums being on, and
PageGetMaxOffsetNumber returns 0 so verify_heapam never enters its tuple
loop. I found these pages only by reading every block with
pg_read_binary_file and comparing against 8192 zero bytes.

Exactly two blocks in that relation are zeroed, 14066 and 14282, out of
14750. Neighbouring blocks have valid headers, so the damage is 8192 bytes
wide and sits on the block boundary, same as the 2010 "unexpected
zero-pages" thread. Index entries still pointing into them:
pkey 11, the unique index 3 (all on index block 7, all dead=t), two others
2 each. At 11.5 rows per page that suggests page 14066 held 11 live rows.

It cannot clear itself: those entries are already LP_DEAD, and the only
path that would remove them, _bt_delete_or_dedup_one_page from
_bt_findinsertloc, dies in index_delete_check_htid first. So every insert
landing on that leaf page fails again, about 1100 statements in sixteen
hours, always the same heap tid.
My original guess was wrong, by the way. No posting list tuples are
involved and dedup does not seem related, the heap page is simply gone.
PostgreSQL 18.3 (Ubuntu 18.3-1.pgdg24.04+1), checksums on, full_page_writes
on, fsync on, wal_log_hints off, KVM/QEMU guest under Proxmox. No crash or
restart before any occurrence. Heavily churned table, 2.1M updates of which
2.0M HOT, 638k deletes, 156k live rows. I scanned the other 77 clusters on
this host and found no comparable case.
Attached, taken with the wiki procedure while the errors were ongoing:

dump_block_7_index.page index block 7 of the unique index
dump_block_14066.page heap block 14066, all zero
dump_block_14282.page heap block 14282, all zero

I have not reindexed, the cluster is still in this state, so tell me if
anything else is
worth pulling out before I repair it.

Regards,

# Alessandro Regolini

Il giorno gio 2 lug 2026 alle ore 20:33 Peter Geoghegan <pg@bowt.ie> ha
scritto:

Show quoted text

On Thu, Jul 2, 2026 at 2:25 PM Alessandro Regolini
<alessandro@regolini.it> wrote:

What we can provide on the next occurrence
------------------------------------------
We run a periodic amcheck sweep, so we usually catch a fresh case before
reindexing. Before REINDEX we can capture, for the affected block:
- contrib/pageinspect: bt_page_items() of the index block and
heap_page_items() / page_header() of the referenced heap block
- full bt_index_check / bt_index_parent_check output
- verify_heapam() of the underlying table
Please tell us which dumps would be most useful to root-cause this (btree
deduplication / VACUUM interaction is our current suspicion).

Those all seem useful, but I doubt that bt_index_parent_check is going
to add much over bt_index_check. However, you should be sure to run
bt_index_check with heapallindexed=true, which will verify agreement
between the index and the underlying table.

If you see a heapallindexed=true failure, getting page images for the
pointed-to heap page is useful (as well as the index page). I prefer a
raw dump of the page itself over a textual representation. See the
procedure here:

https://wiki.postgresql.org/wiki/Getting_a_stack_trace_of_a_running_PostgreSQL_backend_on_Linux/BSD#contrib/pageinspect_page_dump

--
Peter Geoghegan

Attachments:

dump_block_14282.pageapplication/octet-stream; name=dump_block_14282.pageDownload
dump_block_14066.pageapplication/octet-stream; name=dump_block_14066.pageDownload
dump_block_7_index.pageapplication/octet-stream; name=dump_block_7_index.pageDownload
In reply to: Alessandro Regolini (#3)
Re: BTree index corruption (heap-tid-past-end, unexpected zero page, misplaced TID in posting list) recurring on high-churn tables, PG 18.3, data_checksums=on, no preceding crash

On Mon, Aug 10, 2026 at 3:44 AM Alessandro Regolini
<alessandro@regolini.it> wrote:

This morning production was failing with

ERROR: heap tid from index tuple (14066,1) points past end of heap page line pointer array at offset 287 of block 7 in index "mpaapp_cache_content_type_id_object_i_f6ecfefa_uniq"

Unfortunately, those symptoms are extremely broad. The underlying
cause could be almost anything.

There are no known bugs in 18.3 that match the symptoms you're
describing -- nothing that seems remotely relevant changed in 18. The
most likely explanation is storage-level corruption, possibly caused
by a broken backup tool.

Nine minutes after the last of those errors, bt_index_check(heapallindexed => true) passed on all five indexes of that table, and verify_heapam returned zero rows. I had seen the same thing four weeks earlier on another cluster and concluded it had healed itself, which was wrong. heapallindexed checks heap to index, and what I have is the other direction, an index
tuple too many. So your precondition for sending page images never triggers here.

Right, amcheck can only detect structural inconsistencies within an
index (which sometimes happens to you), and, with heapallindexed,
where an index tuple points to something in the heap whose indexed
columns don't match what is stored in the index. The logic for
heapallindexed piggy-backs on the code that REINDEX uses, and verifies
that a fingerprint of the would-be index has tuples that were present
in the index. As you say, if there are "extra" index tuples in the
index it won't be caught -- heapallindexed effectively assumes those
are dead tuples, that the simulated REINDEX correctly excluded from
its imaginary/fingerprint-only new index.

--
Peter Geoghegan