pgsql: Fix bitmapheapscan incorrect recheck of NULL tuples

From: Melanie Plageman <melanieplageman(at)gmail(dot)com>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Fix bitmapheapscan incorrect recheck of NULL tuples
Date: 2025-03-24 20:43:38
Message-ID: E1twoe2-000pJL-0h@gemulon.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Fix bitmapheapscan incorrect recheck of NULL tuples

The bitmap heap scan skip fetch optimization skips fetching the heap
block when a page is set all-visible in the visibility map and no
columns from the table are needed to satisfy the query.

2b73a8cd33b and c3953226a07 changed the control flow of bitmap heap scan
to use the read stream API. The read stream API returns buffers
containing blocks to the user. To make this work with the skip fetch
optimization, we keep a count of the empty tuples we need to emit for
all the blocks skipped and only emit the empty tuples after processing
the next block fetched from the heap or at the end of the scan.

It's incorrect to recheck NULL tuples, so we must set `recheck` to false
before yielding control back to BitmapHeapNext(). This was done before
emitting any remaining empty tuples at the end of the scan but not for
empty tuples emitted during the scan. This meant that if a page fetched
from the heap did require recheck and set `recheck` to true and then we
emitted empty tuples for subsequent blocks, we would get wrong results.

Fix this by always setting `recheck` to false before emitting empty
tuples.

Reported-by: Alexander Lakhin <exclusion(at)gmail(dot)com>
Tested-by: Andres Freund <andres(at)anarazel(dot)de>
Discussion: https://postgr.es/m/496f7acd-881c-4df3-9bd3-8f8534dfec26%40gmail.com

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/aea916fe555a351fe20bf31d56f5f0d027d9db61

Modified Files
--------------
src/backend/access/heap/heapam_handler.c | 28 +++++++++++++++++++++-------
src/test/regress/expected/bitmapops.out | 14 ++++++++++++--
src/test/regress/sql/bitmapops.sql | 10 ++++++++--
3 files changed, 41 insertions(+), 11 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Nathan Bossart 2025-03-24 20:47:13 pgsql: Expand comment for isset_offset.
Previous Message Álvaro Herrera 2025-03-24 16:38:33 pgsql: Fix typo