Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 754c610

Browse files
Fix bitmap table scan crash on iterator release
1a0da34 replaced Bitmap Table Scan's individual private and shared iterators with a unified iterator. It neglected, however, to check if the iterator had already been cleaned up before doing so on rescan. Add this check both on rescan and end scan to be safe. Reported-by: Richard Guo Author: Richard Guo Discussion: https://postgr.es/m/CAMbWs48nrhcLY1kcd-u9oD%2B6yiS631F_8Fx8ZGsO-BYDwH%2Bbyw%40mail.gmail.com
1 parent 31b0a8f commit 754c610

File tree

4 files changed

+61
-5
lines changed

4 files changed

+61
-5
lines changed

src/backend/executor/nodeBitmapHeapscan.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -572,9 +572,11 @@ ExecReScanBitmapHeapScan(BitmapHeapScanState *node)
572572
if (scan)
573573
{
574574
/*
575-
* End iteration on iterators saved in scan descriptor.
575+
* End iteration on iterators saved in scan descriptor if they have
576+
* not already been cleaned up.
576577
*/
577-
tbm_end_iterate(&scan->st.rs_tbmiterator);
578+
if (!tbm_exhausted(&scan->st.rs_tbmiterator))
579+
tbm_end_iterate(&scan->st.rs_tbmiterator);
578580

579581
/* rescan to release any page pin */
580582
table_rescan(node->ss.ss_currentScanDesc, NULL);
@@ -654,9 +656,11 @@ ExecEndBitmapHeapScan(BitmapHeapScanState *node)
654656
if (scanDesc)
655657
{
656658
/*
657-
* End iteration on iterators saved in scan descriptor.
659+
* End iteration on iterators saved in scan descriptor if they have
660+
* not already been cleaned up.
658661
*/
659-
tbm_end_iterate(&scanDesc->st.rs_tbmiterator);
662+
if (!tbm_exhausted(&scanDesc->st.rs_tbmiterator))
663+
tbm_end_iterate(&scanDesc->st.rs_tbmiterator);
660664

661665
/*
662666
* close table scan

src/backend/nodes/tidbitmap.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1594,7 +1594,7 @@ tbm_begin_iterate(TIDBitmap *tbm, dsa_area *dsa, dsa_pointer dsp)
15941594
void
15951595
tbm_end_iterate(TBMIterator *iterator)
15961596
{
1597-
Assert(iterator);
1597+
Assert(iterator && !tbm_exhausted(iterator));
15981598

15991599
if (iterator->shared)
16001600
tbm_end_shared_iterate(iterator->i.shared_iterator);

src/test/regress/expected/join.out

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8182,3 +8182,38 @@ SELECT t1.a FROM skip_fetch t1 LEFT JOIN skip_fetch t2 ON t2.a = 1 WHERE t2.a IS
81828182

81838183
RESET enable_indexonlyscan;
81848184
RESET enable_seqscan;
8185+
-- Test BitmapHeapScan with a rescan releases resources correctly
8186+
SET enable_seqscan = off;
8187+
SET enable_indexscan = off;
8188+
CREATE TEMP TABLE rescan_bhs (a INT);
8189+
INSERT INTO rescan_bhs VALUES (1), (2);
8190+
CREATE INDEX ON rescan_bhs (a);
8191+
EXPLAIN (COSTS OFF)
8192+
SELECT * FROM rescan_bhs t1 LEFT JOIN rescan_bhs t2 ON t1.a IN
8193+
(SELECT a FROM rescan_bhs t3 WHERE t2.a > 1);
8194+
QUERY PLAN
8195+
-----------------------------------------------------------
8196+
Nested Loop Left Join
8197+
Join Filter: (ANY (t1.a = (SubPlan 1).col1))
8198+
-> Bitmap Heap Scan on rescan_bhs t1
8199+
-> Bitmap Index Scan on rescan_bhs_a_idx
8200+
-> Materialize
8201+
-> Bitmap Heap Scan on rescan_bhs t2
8202+
-> Bitmap Index Scan on rescan_bhs_a_idx
8203+
SubPlan 1
8204+
-> Result
8205+
One-Time Filter: (t2.a > 1)
8206+
-> Bitmap Heap Scan on rescan_bhs t3
8207+
-> Bitmap Index Scan on rescan_bhs_a_idx
8208+
(12 rows)
8209+
8210+
SELECT * FROM rescan_bhs t1 LEFT JOIN rescan_bhs t2 ON t1.a IN
8211+
(SELECT a FROM rescan_bhs t3 WHERE t2.a > 1);
8212+
a | a
8213+
---+---
8214+
1 | 2
8215+
2 | 2
8216+
(2 rows)
8217+
8218+
RESET enable_seqscan;
8219+
RESET enable_indexscan;

src/test/regress/sql/join.sql

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3016,3 +3016,20 @@ SELECT t1.a FROM skip_fetch t1 LEFT JOIN skip_fetch t2 ON t2.a = 1 WHERE t2.a IS
30163016

30173017
RESET enable_indexonlyscan;
30183018
RESET enable_seqscan;
3019+
3020+
-- Test BitmapHeapScan with a rescan releases resources correctly
3021+
SET enable_seqscan = off;
3022+
SET enable_indexscan = off;
3023+
3024+
CREATE TEMP TABLE rescan_bhs (a INT);
3025+
INSERT INTO rescan_bhs VALUES (1), (2);
3026+
CREATE INDEX ON rescan_bhs (a);
3027+
3028+
EXPLAIN (COSTS OFF)
3029+
SELECT * FROM rescan_bhs t1 LEFT JOIN rescan_bhs t2 ON t1.a IN
3030+
(SELECT a FROM rescan_bhs t3 WHERE t2.a > 1);
3031+
SELECT * FROM rescan_bhs t1 LEFT JOIN rescan_bhs t2 ON t1.a IN
3032+
(SELECT a FROM rescan_bhs t3 WHERE t2.a > 1);
3033+
3034+
RESET enable_seqscan;
3035+
RESET enable_indexscan;

0 commit comments

Comments
 (0)