diff options
author | Melanie Plageman <melanieplageman@gmail.com> | 2025-03-24 16:40:59 -0400 |
---|---|---|
committer | Melanie Plageman <melanieplageman@gmail.com> | 2025-03-24 16:40:59 -0400 |
commit | aea916fe555a351fe20bf31d56f5f0d027d9db61 (patch) | |
tree | 4c32330d4ed62148382de7624ff3190b292ce112 /src/backend/access/heap/heapam_handler.c | |
parent | 0e3e0ec06b995f6809f315752cbf5ff67902e095 (diff) | |
download | postgresql-aea916fe555a351fe20bf31d56f5f0d027d9db61.tar.gz postgresql-aea916fe555a351fe20bf31d56f5f0d027d9db61.zip |
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@gmail.com>
Tested-by: Andres Freund <andres@anarazel.de>
Discussion: https://postgr.es/m/496f7acd-881c-4df3-9bd3-8f8534dfec26%40gmail.com
Diffstat (limited to 'src/backend/access/heap/heapam_handler.c')
-rw-r--r-- | src/backend/access/heap/heapam_handler.c | 28 |
1 files changed, 21 insertions, 7 deletions
diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index 4da4dc84580..24d3765aa20 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -2147,6 +2147,19 @@ heapam_scan_bitmap_next_tuple(TableScanDesc scan, */ ExecStoreAllNullTuple(slot); bscan->rs_empty_tuples_pending--; + + /* + * We do not recheck all NULL tuples. Because the streaming read + * API only yields TBMIterateResults for blocks actually fetched + * from the heap, we must unset `recheck` ourselves here to ensure + * correct results. + * + * Our read stream callback accrues a count of empty tuples to + * emit and then emits them after emitting tuples from the next + * fetched block. If no blocks need fetching, we'll emit the + * accrued count at the end of the scan. + */ + *recheck = false; return true; } @@ -2510,13 +2523,14 @@ BitmapHeapScanNextBlock(TableScanDesc scan, } /* - * Bitmap is exhausted. Time to emit empty tuples if relevant. We emit - * all empty tuples at the end instead of emitting them per block we - * skip fetching. This is necessary because the streaming read API - * will only return TBMIterateResults for blocks actually fetched. - * When we skip fetching a block, we keep track of how many empty - * tuples to emit at the end of the BitmapHeapScan. We do not recheck - * all NULL tuples. + * The bitmap is exhausted. Now emit any remaining empty tuples. The + * read stream API only returns TBMIterateResults for blocks actually + * fetched from the heap. Our callback will accrue a count of empty + * tuples to emit for all blocks we skipped fetching. So, if we skip + * fetching heap blocks at the end of the relation (or no heap blocks + * are fetched) we need to ensure we emit empty tuples before ending + * the scan. We don't recheck empty tuples so ensure `recheck` is + * unset. */ *recheck = false; return bscan->rs_empty_tuples_pending > 0; |