1a0da347a7ac98db 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
if (scan)
{
/*
- * End iteration on iterators saved in scan descriptor.
+ * End iteration on iterators saved in scan descriptor if they have
+ * not already been cleaned up.
*/
- tbm_end_iterate(&scan->st.rs_tbmiterator);
+ if (!tbm_exhausted(&scan->st.rs_tbmiterator))
+ tbm_end_iterate(&scan->st.rs_tbmiterator);
/* rescan to release any page pin */
table_rescan(node->ss.ss_currentScanDesc, NULL);
if (scanDesc)
{
/*
- * End iteration on iterators saved in scan descriptor.
+ * End iteration on iterators saved in scan descriptor if they have
+ * not already been cleaned up.
*/
- tbm_end_iterate(&scanDesc->st.rs_tbmiterator);
+ if (!tbm_exhausted(&scanDesc->st.rs_tbmiterator))
+ tbm_end_iterate(&scanDesc->st.rs_tbmiterator);
/*
* close table scan
void
tbm_end_iterate(TBMIterator *iterator)
{
- Assert(iterator);
+ Assert(iterator && !tbm_exhausted(iterator));
if (iterator->shared)
tbm_end_shared_iterate(iterator->i.shared_iterator);
RESET enable_indexonlyscan;
RESET enable_seqscan;
+-- Test BitmapHeapScan with a rescan releases resources correctly
+SET enable_seqscan = off;
+SET enable_indexscan = off;
+CREATE TEMP TABLE rescan_bhs (a INT);
+INSERT INTO rescan_bhs VALUES (1), (2);
+CREATE INDEX ON rescan_bhs (a);
+EXPLAIN (COSTS OFF)
+SELECT * FROM rescan_bhs t1 LEFT JOIN rescan_bhs t2 ON t1.a IN
+ (SELECT a FROM rescan_bhs t3 WHERE t2.a > 1);
+ QUERY PLAN
+-----------------------------------------------------------
+ Nested Loop Left Join
+ Join Filter: (ANY (t1.a = (SubPlan 1).col1))
+ -> Bitmap Heap Scan on rescan_bhs t1
+ -> Bitmap Index Scan on rescan_bhs_a_idx
+ -> Materialize
+ -> Bitmap Heap Scan on rescan_bhs t2
+ -> Bitmap Index Scan on rescan_bhs_a_idx
+ SubPlan 1
+ -> Result
+ One-Time Filter: (t2.a > 1)
+ -> Bitmap Heap Scan on rescan_bhs t3
+ -> Bitmap Index Scan on rescan_bhs_a_idx
+(12 rows)
+
+SELECT * FROM rescan_bhs t1 LEFT JOIN rescan_bhs t2 ON t1.a IN
+ (SELECT a FROM rescan_bhs t3 WHERE t2.a > 1);
+ a | a
+---+---
+ 1 | 2
+ 2 | 2
+(2 rows)
+
+RESET enable_seqscan;
+RESET enable_indexscan;
RESET enable_indexonlyscan;
RESET enable_seqscan;
+
+-- Test BitmapHeapScan with a rescan releases resources correctly
+SET enable_seqscan = off;
+SET enable_indexscan = off;
+
+CREATE TEMP TABLE rescan_bhs (a INT);
+INSERT INTO rescan_bhs VALUES (1), (2);
+CREATE INDEX ON rescan_bhs (a);
+
+EXPLAIN (COSTS OFF)
+SELECT * FROM rescan_bhs t1 LEFT JOIN rescan_bhs t2 ON t1.a IN
+ (SELECT a FROM rescan_bhs t3 WHERE t2.a > 1);
+SELECT * FROM rescan_bhs t1 LEFT JOIN rescan_bhs t2 ON t1.a IN
+ (SELECT a FROM rescan_bhs t3 WHERE t2.a > 1);
+
+RESET enable_seqscan;
+RESET enable_indexscan;