BUG #19930: amcheck might report btree index corruption after recovery of a mid-deletion state

Started by PG Bug reporting form6 days ago3 messagesbugs

Thread notes

1 note#bug
hackorum-bot#bug
hackorum-bot
2 days ago

@global #bug

#bug
Jump to latest
#1PG Bug reporting form
noreply@postgresql.org

The following bug has been logged on the website:

Bug reference: 19930
Logged by: Alexander Lakhin
Email address: exclusion@gmail.com
PostgreSQL version: 19.0
Operating system: Ubuntu 24.04
Description:

In case a multi-level btree deletion is interrupted by crash, amcheck can
report XX002 error:
ERROR: the first child of leftmost target page is not leftmost of its level
in index "xxx"
DETAIL: Target block=xxx child block=xxx target page lsn=X/X.
when executed right after recovery (before VACUUM, etc).

Commit 6ec9e9975 fixed a similar issue but for single-level deletions only.

A demo test to be attached.

#2Alexander Lakhin
exclusion@gmail.com
In reply to: PG Bug reporting form (#1)
Re: BUG #19930: amcheck might report btree index corruption after recovery of a mid-deletion state

22.11.2026 19:00, PG Bug reporting form wrote:

The following bug has been logged on the website:

Bug reference: 19930
...

In case a multi-level btree deletion is interrupted by crash, amcheck can
report XX002 error:
ERROR: the first child of leftmost target page is not leftmost of its level
in index "xxx"
DETAIL: Target block=xxx child block=xxx target page lsn=X/X.
when executed right after recovery (before VACUUM, etc).

Commit 6ec9e9975 fixed a similar issue but for single-level deletions only.

A demo test to be attached.

Please find the demo test attached. It works/fails for me as below:
make -s check -C contrib/amcheck/
...
#   Failed test 'bt_index_parent_check passes at the interrupted deletion'
#   at t/099_pitr_multilevel.pl line 87.
#          got: '3'
#     expected: '0'

#   Failed test 'no XX002 leftmost finding'
#   at t/099_pitr_multilevel.pl line 88.
#                   'psql:<stdin>:1: ERROR:  the first child of leftmost target page is not leftmost of its level in
index "no_index_cleanup_idx"
# DETAIL:  Target block=8 child block=7 target page lsn=0/0311A9C8.'
#           matches '(?^:first child of leftmost target page is not leftmost of its level)'
# Looks like you failed 2 tests of 4.
t/099_pitr_multilevel.pl ....... Dubious, test returned 2 (wstat 512, 0x200)
Failed 2/4 subtests

Best regards,
Alexander

Attachments:

099_pitr_multilevel.plapplication/x-perl; name=099_pitr_multilevel.plDownload
#3Raja Sai pranav
rajasaipranav0@gmail.com
In reply to: Alexander Lakhin (#2)
Re: BUG #19930: amcheck might report btree index corruption after recovery of a mid-deletion state

On Sun, Nov 22, 2026 at 11:30 PM Alexander Lakhin <exclusion@gmail.com>
wrote:

Please find the demo test attached.

Hi Alexander,

Thanks for the test. It fails on master for me too.

The cause is that _bt_mark_page_halfdead() marks only the leaf half-dead.
In a multi-level deletion, the internal pages from the subtree's top parent
down to the leaf stay live; only the downlink to the top parent is gone. In
your test the leaf is block 2, its high key names block 3 as the top
parent, and block 8's first downlink already points to block 7.
bt_leftmost_ignoring_half_dead() walks left from block 7, reaches block 3,
and since block 3 is not half-dead, reports the error.

The attached patch also accepts such an internal page: following its only
downlink at each level must reach a half-dead leaf whose high key names
that page, or a page above it on the same path, as the top parent. Your
test is added as t/007_pitr_multilevel.pl and passes with the patch, and
all amcheck tests pass.

To check that it doesn't hide real corruption, I took a copy of the cluster
in the interrupted state and changed the top parent in the leaf's high key
from 3 to 18. The patched amcheck reports the same error again for that
copy.

The patch applies to REL_18_STABLE; REL_17_STABLE needs a separate version.
I didn't test a subtree with more than one internal level.

Regards,
Sai Pranav

Attachments:

t259654_3
v1-0001-amcheck-Accept-an-interrupted-multi-level-btree-p.patchapplication/octet-stream; name=v1-0001-amcheck-Accept-an-interrupted-multi-level-btree-p.patchDownload+213-2