Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Fix memory leak in IndexScan node with reordering
authorAlexander Korotkov <akorotkov@postgresql.org>
Mon, 14 Feb 2022 00:26:55 +0000 (03:26 +0300)
committerAlexander Korotkov <akorotkov@postgresql.org>
Mon, 14 Feb 2022 00:32:39 +0000 (03:32 +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 c06d07aa4676a94d8f91d89055b3039639a02d60..982a80a881eb631f9bd808ca8580bb6d7f00e103 100644 (file)
@@ -574,8 +574,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 93dfeea0a08850155843bcf182091a128b8313a3..267f1cea3ab4a5da633660d3b4b0bbb13cb3d89c 100644 (file)
@@ -639,6 +639,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 4af8ea96e62212847c5b60b22cfd59ceef4f4f12..6ca13920888c538c1b2144161834406ae37dae7d 100644 (file)
@@ -255,6 +255,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;