aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2012-06-21 17:26:13 -0400
committerTom Lane <tgl@sss.pgh.pa.us>2012-06-21 17:26:13 -0400
commit66567ab2c674b958ac7fec47f0c830018245e04a (patch)
tree0728ca52a2f98cd9cfa5e1e9e034e31a66982fb6 /src
parente1a1e6e19840576e563c5c92bb4cbad621e23170 (diff)
downloadpostgresql-66567ab2c674b958ac7fec47f0c830018245e04a.tar.gz
postgresql-66567ab2c674b958ac7fec47f0c830018245e04a.zip
Fix memory leak in ARRAY(SELECT ...) subqueries.
Repeated execution of an uncorrelated ARRAY_SUBLINK sub-select (which I think can only happen if the sub-select is embedded in a larger, correlated subquery) would leak memory for the duration of the query, due to not reclaiming the array generated in the previous execution. Per bug #6698 from Armando Miraglia. Diagnosis and fix idea by Heikki, patch itself by me. This has been like this all along, so back-patch to all supported versions.
Diffstat (limited to 'src')
-rw-r--r--src/backend/executor/nodeSubplan.c18
-rw-r--r--src/include/nodes/execnodes.h1
2 files changed, 14 insertions, 5 deletions
diff --git a/src/backend/executor/nodeSubplan.c b/src/backend/executor/nodeSubplan.c
index da31820e2d9..4b8746f4f26 100644
--- a/src/backend/executor/nodeSubplan.c
+++ b/src/backend/executor/nodeSubplan.c
@@ -668,6 +668,7 @@ ExecInitSubPlan(SubPlan *subplan, PlanState *parent)
* initialize my state
*/
sstate->curTuple = NULL;
+ sstate->curArray = PointerGetDatum(NULL);
sstate->projLeft = NULL;
sstate->projRight = NULL;
sstate->hashtable = NULL;
@@ -994,16 +995,23 @@ ExecSetParamPlan(SubPlanState *node, ExprContext *econtext)
int paramid = linitial_int(subplan->setParam);
ParamExecData *prm = &(econtext->ecxt_param_exec_vals[paramid]);
- prm->execPlan = NULL;
- /* We build the result in query context so it won't disappear */
+ /*
+ * We build the result array in query context so it won't disappear;
+ * to avoid leaking memory across repeated calls, we have to remember
+ * the latest value, much as for curTuple above.
+ */
+ if (node->curArray != PointerGetDatum(NULL))
+ pfree(DatumGetPointer(node->curArray));
if (astate != NULL)
- prm->value = makeArrayResult(astate,
- econtext->ecxt_per_query_memory);
+ node->curArray = makeArrayResult(astate,
+ econtext->ecxt_per_query_memory);
else
{
MemoryContextSwitchTo(econtext->ecxt_per_query_memory);
- prm->value = PointerGetDatum(construct_empty_array(subplan->firstColType));
+ node->curArray = PointerGetDatum(construct_empty_array(subplan->firstColType));
}
+ prm->execPlan = NULL;
+ prm->value = node->curArray;
prm->isnull = false;
}
else if (!found)
diff --git a/src/include/nodes/execnodes.h b/src/include/nodes/execnodes.h
index 6fe8c2303a2..aa391699871 100644
--- a/src/include/nodes/execnodes.h
+++ b/src/include/nodes/execnodes.h
@@ -708,6 +708,7 @@ typedef struct SubPlanState
ExprState *testexpr; /* state of combining expression */
List *args; /* states of argument expression(s) */
HeapTuple curTuple; /* copy of most recent tuple from subplan */
+ Datum curArray; /* most recent array from ARRAY() subplan */
/* these are used when hashing the subselect's output: */
ProjectionInfo *projLeft; /* for projecting lefthand exprs */
ProjectionInfo *projRight; /* for projecting subselect output */