Skip to content

Commit c76665e

Browse files
committed
Fix memory leak in IndexScan node with reordering
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
1 parent ae27b1a commit c76665e

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

src/backend/executor/nodeIndexscan.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -574,8 +574,12 @@ ExecReScanIndexScan(IndexScanState *node)
574574
/* flush the reorder queue */
575575
if (node->iss_ReorderQueue)
576576
{
577+
HeapTuple tuple;
577578
while (!pairingheap_is_empty(node->iss_ReorderQueue))
578-
reorderqueue_pop(node);
579+
{
580+
tuple = reorderqueue_pop(node);
581+
heap_freetuple(tuple);
582+
}
579583
}
580584

581585
/* reset index scan */

src/test/regress/expected/create_index.out

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,33 @@ SELECT circle_center(f1), round(radius(f1)) as radius FROM gcircle_tbl ORDER BY
643643
(751.5,2655) | 20
644644
(10 rows)
645645

646+
EXPLAIN (COSTS OFF)
647+
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;
648+
QUERY PLAN
649+
--------------------------------------------------------------------------------------------
650+
Function Scan on generate_series x
651+
SubPlan 1
652+
-> Limit
653+
-> Index Scan using ggpolygonind on gpolygon_tbl
654+
Order By: (f1 <-> point((x.x)::double precision, (x.x)::double precision))
655+
(5 rows)
656+
657+
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;
658+
point | c
659+
---------+-------------------------------------------
660+
(0,0) | ((240,359),(240,455),(337,455),(337,359))
661+
(1,1) | ((240,359),(240,455),(337,455),(337,359))
662+
(2,2) | ((240,359),(240,455),(337,455),(337,359))
663+
(3,3) | ((240,359),(240,455),(337,455),(337,359))
664+
(4,4) | ((240,359),(240,455),(337,455),(337,359))
665+
(5,5) | ((240,359),(240,455),(337,455),(337,359))
666+
(6,6) | ((240,359),(240,455),(337,455),(337,359))
667+
(7,7) | ((240,359),(240,455),(337,455),(337,359))
668+
(8,8) | ((240,359),(240,455),(337,455),(337,359))
669+
(9,9) | ((240,359),(240,455),(337,455),(337,359))
670+
(10,10) | ((240,359),(240,455),(337,455),(337,359))
671+
(11 rows)
672+
646673
-- Now check the results from bitmap indexscan
647674
SET enable_seqscan = OFF;
648675
SET enable_indexscan = OFF;

src/test/regress/sql/create_index.sql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,10 @@ EXPLAIN (COSTS OFF)
255255
SELECT circle_center(f1), round(radius(f1)) as radius FROM gcircle_tbl ORDER BY f1 <-> '(200,300)'::point LIMIT 10;
256256
SELECT circle_center(f1), round(radius(f1)) as radius FROM gcircle_tbl ORDER BY f1 <-> '(200,300)'::point LIMIT 10;
257257

258+
EXPLAIN (COSTS OFF)
259+
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;
260+
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;
261+
258262
-- Now check the results from bitmap indexscan
259263
SET enable_seqscan = OFF;
260264
SET enable_indexscan = OFF;

0 commit comments

Comments
 (0)