Re: [GSoC 2026] - B-tree Index Bloat Reduction - Approach & Questions

From: Yao Feng <fengyao0087(at)gmail(dot)com>
To: Salma Elsayed <salmasayed182003(at)gmail(dot)com>
Cc: pgsql-hackers(at)lists(dot)postgresql(dot)org
Subject: Re: [GSoC 2026] - B-tree Index Bloat Reduction - Approach & Questions
Date: 2026-12-04 11:14:00
Message-ID: CAAU0Mh0Y2z3RBJtDpX0=jd77zka05q_8pUVD5Chif6M9AL_L5A@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi Salma,

While reviewing and testing v5, I found two concurrency bugs.
I have attached a reproducer script and two patches as proposed
fixes for your reference.

1. Silent data loss in parallel forward scans (btps_skipMABlkno race)

In _bt_readpage(), a forward parallel scan calls _bt_parallel_release()
before scanning tuples, and later calls _bt_parallel_set_skip_ma() when
crossing the merge boundary. If another worker has already moved on to
the next merge group (MA2), that late write overwrites btps_skipMABlkno
back to MA1. A worker that then seizes a remaining split page of MA2's
group sees MA1 != MA2, enables merge recovery, and skips tuples that
were never read (72 rows silently lost in the attached reproducer).

0001 removes the post-release _bt_parallel_set_skip_ma() calls in
_bt_readpage(), avoids calling _bt_parallel_get_skip_ma() when
needMergeRecovery is already set on backward return to MA, resets
btps_skipMABlkno on fresh descents to non-merged pages, and asserts
btps_pageStatus == BTPARALLEL_ADVANCING on access.

2. Leftmost leaf page can be merged away (and bare Assert(P_ISLEAF))

Between _bt_mergescan() releasing share locks and _bt_mergepage()
acquiring exclusive locks, a concurrent VACUUM can unlink the original
leftmost leaf page so L becomes P_LEFTMOST (or recycle either block as
a non-leaf page). Because _bt_mergepage() does not recheck
P_LEFTMOST(leftopaque), it merges the new leftmost page into a
BTP_MERGED_AWAY tombstone, which also causes bt_index_parent_check() to
fail. Both functions also have bare Assert(P_ISLEAF(...)) checks on
relocked buffers.

0002 replaces those asserts with explicit !P_ISLEAF(...) and
P_LEFTMOST(leftopaque) checks in _bt_mergescan() and _bt_mergepage(),
and teaches amcheck to reject leftmost BTP_MERGED_AWAY pages.

With repro_btree_merge_v5_bugs.py, both issues reproduce 5/5 on v5
(--enable-cassert --enable-injection-points) and pass 5/5 with 0001 and
0002 applied.

Hope this helps.

Best regards,
Yao Feng

Attachment Content-Type Size
repro_btree_merge_v5_bugs.py text/x-python 14.1 KB
0001-nbtree-avoid-overwriting-btps_skipMABlkno-after-para.patch application/x-patch 5.5 KB
0002-nbtree-recheck-P_LEFTMOST-and-P_ISLEAF-in-merge-scan.patch application/x-patch 5.5 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Fujii Masao 2026-12-04 11:15:27 Re: Assertion failure in WAL summarizer
Previous Message Ashutosh Bapat 2026-12-04 11:07:40 Re: Changing shared_buffers without restart