Fix memory leak in IndexScan node with reordering
authorAlexander Korotkov <[email protected]>
Mon, 14 Feb 2022 00:26:55 +0000 (03:26 +0300)
committerAlexander Korotkov <[email protected]>
Mon, 14 Feb 2022 01:04:28 +0000 (04:04 +0300)
Fix ExecReScanIndexScan() to free the referenced tuples while emptying the
priority queue.  Backpatch to all supported versions.

Discussion: https://postgr.es/m/CAHqSB9gECMENBQmpbv5rvmT3HTaORmMK3Ukg73DsX5H7EJV7jw%40mail.gmail.com
Author: Aliaksandr Kalenik
Reviewed-by: Tom Lane, Alexander Korotkov
Backpatch-through: 10

src/backend/executor/nodeIndexscan.c
src/test/regress/expected/create_index.out
src/test/regress/sql/create_index.sql

index 9ccdab6d0716a5c672460caf6084ac62f3a2219b..2458cf3a29103274a81d8140dd9c26084a9bb1a9 100644 (file)
@@ -600,8 +600,12 @@ ExecReScanIndexScan(IndexScanState *node)
    /* flush the reorder queue */
    if (node->iss_ReorderQueue)
    {
+       HeapTuple   tuple;
        while (!pairingheap_is_empty(node->iss_ReorderQueue))
-           reorderqueue_pop(node);
+       {
+           tuple = reorderqueue_pop(node);
+           heap_freetuple(tuple);
+       }
    }
 
    /* reset index scan */
index c0a30a0396f0fcda1915a8bc1a41d713573a7f34..4723df9e08b68bd5db2cfacb318d984633db046e 100644 (file)
@@ -1230,6 +1230,33 @@ SELECT circle_center(f1), round(radius(f1)) as radius FROM gcircle_tbl ORDER BY
  (751.5,2655)   |     20
 (10 rows)
 
+EXPLAIN (COSTS OFF)
+SELECT point(x,x), (SELECT f1 FROM gpolygon_tbl ORDER BY f1 <-> point(x,x) LIMIT 1) as c FROM generate_series(0,10,1) x;
+                                         QUERY PLAN                                         
+--------------------------------------------------------------------------------------------
+ Function Scan on generate_series x
+   SubPlan 1
+     ->  Limit
+           ->  Index Scan using ggpolygonind on gpolygon_tbl
+                 Order By: (f1 <-> point((x.x)::double precision, (x.x)::double precision))
+(5 rows)
+
+SELECT point(x,x), (SELECT f1 FROM gpolygon_tbl ORDER BY f1 <-> point(x,x) LIMIT 1) as c FROM generate_series(0,10,1) x;
+  point  |                     c                     
+---------+-------------------------------------------
+ (0,0)   | ((240,359),(240,455),(337,455),(337,359))
+ (1,1)   | ((240,359),(240,455),(337,455),(337,359))
+ (2,2)   | ((240,359),(240,455),(337,455),(337,359))
+ (3,3)   | ((240,359),(240,455),(337,455),(337,359))
+ (4,4)   | ((240,359),(240,455),(337,455),(337,359))
+ (5,5)   | ((240,359),(240,455),(337,455),(337,359))
+ (6,6)   | ((240,359),(240,455),(337,455),(337,359))
+ (7,7)   | ((240,359),(240,455),(337,455),(337,359))
+ (8,8)   | ((240,359),(240,455),(337,455),(337,359))
+ (9,9)   | ((240,359),(240,455),(337,455),(337,359))
+ (10,10) | ((240,359),(240,455),(337,455),(337,359))
+(11 rows)
+
 -- Now check the results from bitmap indexscan
 SET enable_seqscan = OFF;
 SET enable_indexscan = OFF;
index 9a18bfa458ec20fef2c87fa98158ba0a3ff84413..bb4f24ef771418da5b8c48aa7a9787efa370ac0b 100644 (file)
@@ -449,6 +449,10 @@ EXPLAIN (COSTS OFF)
 SELECT circle_center(f1), round(radius(f1)) as radius FROM gcircle_tbl ORDER BY f1 <-> '(200,300)'::point LIMIT 10;
 SELECT circle_center(f1), round(radius(f1)) as radius FROM gcircle_tbl ORDER BY f1 <-> '(200,300)'::point LIMIT 10;
 
+EXPLAIN (COSTS OFF)
+SELECT point(x,x), (SELECT f1 FROM gpolygon_tbl ORDER BY f1 <-> point(x,x) LIMIT 1) as c FROM generate_series(0,10,1) x;
+SELECT point(x,x), (SELECT f1 FROM gpolygon_tbl ORDER BY f1 <-> point(x,x) LIMIT 1) as c FROM generate_series(0,10,1) x;
+
 -- Now check the results from bitmap indexscan
 SET enable_seqscan = OFF;
 SET enable_indexscan = OFF;