Fix for aggregate memory leaks from Erik Riedel.
authorBruce Momjian <bruce@momjian.us>
Sat, 20 Mar 1999 01:13:22 +0000 (01:13 +0000)
committerBruce Momjian <bruce@momjian.us>
Sat, 20 Mar 1999 01:13:22 +0000 (01:13 +0000)
src/backend/executor/execMain.c
src/backend/executor/execUtils.c
src/backend/executor/nodeAgg.c
src/backend/executor/nodeResult.c

index 0b924e29d1a5ab055da0a3f93b595b86a43d0a64..9fb60817b586f08550a85399450deca445bbdd96 100644 (file)
@@ -394,8 +394,26 @@ ExecutorEnd(QueryDesc *queryDesc, EState *estate)
 
        EndPlan(queryDesc->plantree, estate);
 
+       /* XXX - clean up some more from ExecutorStart() - er1p */
+       if (NULL == estate->es_snapshot) {
+         /* nothing to free */
+       } else {
+         if (estate->es_snapshot->xcnt > 0) { 
+           pfree(estate->es_snapshot->xip);
+         }
+         pfree(estate->es_snapshot);
+       }
+
+       if (NULL == estate->es_param_exec_vals) {
+         /* nothing to free */
+       } else {
+         pfree(estate->es_param_exec_vals);
+         estate->es_param_exec_vals = NULL;
+       }
+
        /* restore saved refcounts. */
        BufferRefCountRestore(estate->es_refcount);
+
 }
 
 void
@@ -580,7 +598,7 @@ InitPlan(CmdType operation, Query *parseTree, Plan *plan, EState *estate)
        /*
         *      initialize result relation stuff
         */
-
+       
        if (resultRelation != 0 && operation != CMD_SELECT)
        {
                /*
index b38caa113ce84827431d994ac6e9c1860c1e156d..574a03c5ca92c8f9b41d750de2848630c3dce07a 100644 (file)
@@ -272,6 +272,7 @@ ExecAssignResultTypeFromTL(Plan *node, CommonState *commonstate)
 #endif
                i++;
        }
+
        if (len > 0)
        {
                ExecAssignResultType(commonstate,
@@ -368,6 +369,53 @@ ExecFreeProjectionInfo(CommonState *commonstate)
        commonstate->cs_ProjInfo = NULL;
 }
 
+/* ----------------
+ *             ExecFreeExprContext
+ * ----------------
+ */
+void
+ExecFreeExprContext(CommonState *commonstate)
+{
+       ExprContext *econtext;
+
+       /* ----------------
+        *      get expression context.  if NULL then this node has
+        *      none so we just return.
+        * ----------------
+        */
+       econtext = commonstate->cs_ExprContext;
+       if (econtext == NULL)
+               return;
+
+       /* ----------------
+        *      clean up memory used.
+        * ----------------
+        */
+       pfree(econtext);
+       commonstate->cs_ExprContext = NULL;
+}
+
+/* ----------------
+ *             ExecFreeTypeInfo
+ * ----------------
+ */
+void
+ExecFreeTypeInfo(CommonState *commonstate)
+{
+       TupleDesc tupDesc;
+
+       tupDesc = commonstate->cs_ResultTupleSlot->ttc_tupleDescriptor;
+       if (tupDesc == NULL)
+               return;
+
+       /* ----------------
+        *      clean up memory used.
+        * ----------------
+        */
+       FreeTupleDesc(tupDesc);
+       commonstate->cs_ResultTupleSlot->ttc_tupleDescriptor = NULL;
+}
+
 /* ----------------------------------------------------------------
  *             the following scan type support functions are for
  *             those nodes which are stubborn and return tuples in
index d379077af518b8bff183b5c11e05eec5c1a05463..288e16555e796ce3cf835089798aaa5f97c240d7 100644 (file)
@@ -110,6 +110,7 @@ ExecAgg(Agg *node)
                                isNull2 = FALSE;
        bool            qual_result;
 
+       Datum  oldVal = (Datum) NULL;  /* XXX - so that we can save and free on each iteration - er1p */
 
        /* ---------------------
         *      get state info from node
@@ -372,8 +373,10 @@ ExecAgg(Agg *node)
                                                 */
                                                args[0] = value1[aggno];
                                                args[1] = newVal;
+                                               oldVal = value1[aggno]; /* XXX - save so we can free later - er1p */
                                                value1[aggno] = (Datum) fmgr_c(&aggfns->xfn1,
                                                                                   (FmgrValues *) args, &isNull1);
+                                               pfree(oldVal); /* XXX - new, let's free the old datum - er1p */
                                                Assert(!isNull1);
                                        }
                                }
index b61dac142d31fe6ef345cb80b033152d19978174..4e12a1cb9e5427962a63c680acb6ae004d4c8804 100644 (file)
@@ -263,6 +263,8 @@ ExecEndResult(Result *node)
         *                is freed at end-transaction time.  -cim 6/2/91
         * ----------------
         */
+       ExecFreeExprContext(&resstate->cstate); /* XXX - new for us - er1p */
+       ExecFreeTypeInfo(&resstate->cstate); /* XXX - new for us - er1p */
        ExecFreeProjectionInfo(&resstate->cstate);
 
        /* ----------------
@@ -276,6 +278,7 @@ ExecEndResult(Result *node)
         * ----------------
         */
        ExecClearTuple(resstate->cstate.cs_ResultTupleSlot);
+       pfree(resstate); node->resstate = NULL; /* XXX - new for us - er1p */
 }
 
 void