Re: Incorrect result of bitmap heap scan. - Mailing list pgsql-hackers

From Peter Geoghegan
Subject Re: Incorrect result of bitmap heap scan.
Date
Msg-id CAH2-Wzn9ZGt8mYMrbDMSRSps3t-ro4JNFXbAvk_jwUoKi=gANQ@mail.gmail.com
Whole thread Raw
Responses Re: Incorrect result of bitmap heap scan.
Re: Incorrect result of bitmap heap scan.
List pgsql-hackers
On Mon, Dec 2, 2024 at 10:15 AM Matthias van de Meent
<boekewurm+postgres@gmail.com> wrote:
> The running theory is that bitmap executor nodes incorrectly assume
> that the rows contained in the bitmap all are still present in the
> index, and thus assume they're allowed to only check the visibility
> map to see if the reference contained in the bitmap is visible.
> However, this seems incorrect: Note that index AMs must hold at least
> pins on the index pages that contain their results when those results
> are returned by amgettuple() [0], and that amgetbitmap() doesn't do
> that for all TIDs in the bitmap; thus allowing vacuum to remove TIDs
> from the index (and later, heap) that are still present in the bitmap
> used in the scan.

This theory seems very believable.

We hold onto a leaf page buffer pin for index-only scans as an
interlock against concurrent TID recycling. If we assume for the sake
of argument that the optimization from commit 7c70996e is correct,
then why do we even bother with holding onto the pin during index-only
scans?

In theory we should either do the "buffer pin interlock against TID
recycling" thing everywhere, or nowhere -- how could bitmap scans
possibly be different here?

> I think this might be an oversight when the feature was originally
> committed in 7c70996e (PG11): we don't know when the VM bit was set,
> and the bitmap we're scanning may thus be out-of-date (and should've
> had TIDs removed it it had been an index), so I propose disabling this
> optimization for now, as attached.

I have a hard time imagining any alternative fix that is suitable for
backpatch. Can we save the optimization on the master branch?

Clearly it would be wildly impractical to do the "buffer pin interlock
against TID recycling" thing in the context of bitmap scans. The only
thing that I can think of that might work is a scheme that establishes
a "safe LSN" for a given MVCC snapshot. If the VM page's LSN is later
than the "safe LSN", it's not okay to trust its all-visible bits. At
least not in the context of bitmap index scans that use the
optimization from 7c70996e.

--
Peter Geoghegan



pgsql-hackers by date:

Previous
From: "Tom Turelinckx"
Date:
Subject: Re: RISC-V animals sporadically produce weird memory-related failures
Next
From: Vik Fearing
Date:
Subject: Re: CREATE SCHEMA ... CREATE DOMAIN support