Ensure set-returning functions in the targetlist of a plan node will be
authorTom Lane <tgl@sss.pgh.pa.us>
Thu, 18 Dec 2003 20:21:53 +0000 (20:21 +0000)
committerTom Lane <tgl@sss.pgh.pa.us>
Thu, 18 Dec 2003 20:21:53 +0000 (20:21 +0000)
shut down cleanly if the plan node is ReScanned before the SRFs are run
to completion.  This fixes the problem for SQL-language functions, but
still need work on functions using the SRF_XXX() macros.

src/backend/executor/execAmi.c
src/backend/executor/execUtils.c
src/include/executor/executor.h

index 49af79f060bc2e4c0c6e6cf6a58efbc66c42702e..ecaaaf3a76011455616f6ea037b7f9dff6c3e13f 100644 (file)
 #include "executor/nodeUnique.h"
 
 
-/* ----------------------------------------------------------------
- *             ExecReScan
+/*
+ * ExecReScan
+ *             Reset a plan node so that its output can be re-scanned.
+ *
+ * Note that if the plan node has parameters that have changed value,
+ * the output might be different from last time.
  *
- *             takes the new expression context as an argument, so that
- *             index scans needn't have their scan keys updated separately
- *             - marcel 09/20/94
- * ----------------------------------------------------------------
+ * The second parameter is currently only used to pass a NestLoop plan's
+ * econtext down to its inner child plan, in case that is an indexscan that
+ * needs access to variables of the current outer tuple.  (The handling of
+ * this parameter is currently pretty inconsistent: some callers pass NULL
+ * and some pass down their parent's value; so don't rely on it in other
+ * situations.  It'd probably be better to remove the whole thing and use
+ * the generalized parameter mechanism instead.)
  */
 void
 ExecReScan(PlanState *node, ExprContext *exprCtxt)
@@ -85,6 +92,11 @@ ExecReScan(PlanState *node, ExprContext *exprCtxt)
                        UpdateChangedParamSet(node->righttree, node->chgParam);
        }
 
+       /* Shut down any SRFs in the plan node's targetlist */
+       if (node->ps_ExprContext)
+               ReScanExprContext(node->ps_ExprContext);
+
+       /* And do node-type-specific processing */
        switch (nodeTag(node))
        {
                case T_ResultState:
index adb6ade06260adc5bddb4381d3efa69b07e0deae..4e2e0eb6aee529e889ba94b4a3b5f5eb6f320027 100644 (file)
@@ -18,6 +18,7 @@
  *             FreeExecutorState
  *             CreateExprContext
  *             FreeExprContext
+ *             ReScanExprContext
  *
  *             ExecAssignExprContext   Common code for plan node init routines.
  *             ExecAssignResultType
@@ -352,6 +353,24 @@ FreeExprContext(ExprContext *econtext)
        pfree(econtext);
 }
 
+/*
+ * ReScanExprContext
+ *
+ *             Reset an expression context in preparation for a rescan of its
+ *             plan node.  This requires calling any registered shutdown callbacks,
+ *             since any partially complete set-returning-functions must be canceled.
+ *
+ * Note we make no assumption about the caller's memory context.
+ */
+void
+ReScanExprContext(ExprContext *econtext)
+{
+       /* Call any registered callbacks */
+       ShutdownExprContext(econtext);
+       /* And clean up the memory used */
+       MemoryContextReset(econtext->ecxt_per_tuple_memory);
+}
+
 /*
  * Build a per-output-tuple ExprContext for an EState.
  *
index 105966540e370372353fe7edc54785710096b8c0..259d614918abee43621937a9a474c18b4614712e 100644 (file)
@@ -201,6 +201,7 @@ extern EState *CreateExecutorState(void);
 extern void FreeExecutorState(EState *estate);
 extern ExprContext *CreateExprContext(EState *estate);
 extern void FreeExprContext(ExprContext *econtext);
+extern void ReScanExprContext(ExprContext *econtext);
 
 #define ResetExprContext(econtext) \
        MemoryContextReset((econtext)->ecxt_per_tuple_memory)