]> granicus.if.org Git - postgresql/blobdiff - src/backend/executor/nodeResult.c
Faster expression evaluation and targetlist projection.
[postgresql] / src / backend / executor / nodeResult.c
index 9bc6342b66839d565e8dd2e1e9a7f5a3b508c51b..a753a53419183cefebf5c3e0ed9fe6a9f8e8ac37 100644 (file)
@@ -1,46 +1,59 @@
 /*-------------------------------------------------------------------------
  *
- * nodeResult.c--
+ * nodeResult.c
  *       support for constant nodes needing special code.
  *
- * Copyright (c) 1994, Regents of the University of California
+ * DESCRIPTION
  *
+ *             Result nodes are used in queries where no relations are scanned.
+ *             Examples of such queries are:
  *
- * DESCRIPTION
+ *                             select 1 * 2
  *
- *             Example: in constant queries where no relations are scanned,
- *             the planner generates result nodes.  Examples of such queries are:
+ *                             insert into emp values ('mike', 15000)
  *
- *                             retrieve (x = 1)
- *             and
- *                             append emp (name = "mike", salary = 15000)
+ *             (Remember that in an INSERT or UPDATE, we need a plan tree that
+ *             generates the new rows.)
  *
- *             Result nodes are also used to optimise queries
- *             with tautological qualifications like:
+ *             Result nodes are also used to optimise queries with constant
+ *             qualifications (ie, quals that do not depend on the scanned data),
+ *             such as:
  *
- *                             retrieve (emp.all) where 2 > 1
+ *                             select * from emp where 2 > 1
  *
  *             In this case, the plan generated is
  *
  *                                             Result  (with 2 > 1 qual)
  *                                             /
- *                                SeqScan (emp.all)
+ *                                SeqScan (emp.*)
+ *
+ *             At runtime, the Result node evaluates the constant qual once,
+ *             which is shown by EXPLAIN as a One-Time Filter.  If it's
+ *             false, we can return an empty result set without running the
+ *             controlled plan at all.  If it's true, we run the controlled
+ *             plan normally and pass back the results.
+ *
+ *
+ * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1994, Regents of the University of California
  *
  * IDENTIFICATION
- *       $Header: /cvsroot/pgsql/src/backend/executor/nodeResult.c,v 1.8 1998/02/26 04:31:31 momjian Exp $
+ *       src/backend/executor/nodeResult.c
  *
  *-------------------------------------------------------------------------
  */
-#include "postgres.h"
 
+#include "postgres.h"
 
 #include "executor/executor.h"
 #include "executor/nodeResult.h"
+#include "utils/memutils.h"
+
 
 /* ----------------------------------------------------------------
  *             ExecResult(node)
  *
- *             returns the tuples from the outer plan which satisify the
+ *             returns the tuples from the outer plan which satisfy the
  *             qualification clause.  Since result nodes with right
  *             subtrees are never planned, we ignore the right subtree
  *             entirely (for now).. -cim 10/7/89
  * ----------------------------------------------------------------
  */
 TupleTableSlot *
-ExecResult(Result *node)
+ExecResult(ResultState *node)
 {
-       ResultState *resstate;
        TupleTableSlot *outerTupleSlot;
-       TupleTableSlot *resultSlot;
-       Plan       *outerPlan;
+       PlanState  *outerPlan;
        ExprContext *econtext;
-       bool            isDone;
-       ProjectionInfo *projInfo;
-
-       /* ----------------
-        *      initialize the result node's state
-        * ----------------
-        */
-       resstate = node->resstate;
 
-       /* ----------------
-        *      get the expression context
-        * ----------------
-        */
-       econtext = resstate->cstate.cs_ExprContext;
+       econtext = node->ps.ps_ExprContext;
 
-       /* ----------------
-        *       check tautological qualifications like (2 > 1)
-        * ----------------
+       /*
+        * check constant qualifications like (2 > 1), if not already done
         */
-       if (resstate->rs_checkqual)
+       if (node->rs_checkqual)
        {
-               bool            qualResult = ExecQual((List *) node->resconstantqual, econtext);
+               bool            qualResult = ExecQual(node->resconstantqual, econtext);
 
-               resstate->rs_checkqual = false;
-               if (qualResult == false)
+               node->rs_checkqual = false;
+               if (!qualResult)
                {
-                       resstate->rs_done = true;
+                       node->rs_done = true;
                        return NULL;
                }
        }
 
-       if (resstate->cstate.cs_TupFromTlist)
-       {
-               ProjectionInfo *projInfo;
-
-               projInfo = resstate->cstate.cs_ProjInfo;
-               resultSlot = ExecProject(projInfo, &isDone);
-               if (!isDone)
-                       return resultSlot;
-       }
-
-       /* ----------------
-        *      retrieve a tuple that satisfy the qual from the outer plan until
-        *      there are no more.
-        *
-        *      if rs_done is 1 then it means that we were asked to return
-        *      a constant tuple and we alread did the last time ExecResult()
-        *      was called, so now we are through.
-        * ----------------
+       /*
+        * Reset per-tuple memory context to free any expression evaluation
+        * storage allocated in the previous tuple cycle.
         */
-       outerPlan = outerPlan(node);
+       ResetExprContext(econtext);
 
-       while (!resstate->rs_done)
+       /*
+        * if rs_done is true then it means that we were asked to return a
+        * constant tuple and we already did the last time ExecResult() was
+        * called, OR that we failed the constant qual check. Either way, now we
+        * are through.
+        */
+       while (!node->rs_done)
        {
+               outerPlan = outerPlanState(node);
 
-               /* ----------------
-                *        get next outer tuple if necessary.
-                * ----------------
-                */
                if (outerPlan != NULL)
                {
-                       outerTupleSlot = ExecProcNode(outerPlan, (Plan *) node);
+                       /*
+                        * retrieve tuples from the outer plan until there are no more.
+                        */
+                       outerTupleSlot = ExecProcNode(outerPlan);
 
                        if (TupIsNull(outerTupleSlot))
                                return NULL;
 
-                       resstate->cstate.cs_OuterTupleSlot = outerTupleSlot;
+                       /*
+                        * prepare to compute projection expressions, which will expect to
+                        * access the input tuples as varno OUTER.
+                        */
+                       econtext->ecxt_outertuple = outerTupleSlot;
                }
                else
                {
-
-                       /* ----------------
-                        *      if we don't have an outer plan, then it's probably
-                        *      the case that we are doing a retrieve or an append
-                        *      with a constant target list, so we should only return
-                        *      the constant tuple once or never if we fail the qual.
-                        * ----------------
+                       /*
+                        * if we don't have an outer plan, then we are just generating the
+                        * results from a constant target list.  Do it only once.
                         */
-                       resstate->rs_done = 1;
+                       node->rs_done = true;
                }
 
-               /* ----------------
-                *        get the information to place into the expr context
-                * ----------------
-                */
-               resstate = node->resstate;
-
-               outerTupleSlot = resstate->cstate.cs_OuterTupleSlot;
-
-               /* ----------------
-                *       fill in the information in the expression context
-                *       XXX gross hack. use outer tuple as scan tuple
-                * ----------------
-                */
-               econtext->ecxt_outertuple = outerTupleSlot;
-               econtext->ecxt_scantuple = outerTupleSlot;
-
-               /* ----------------
-                *       form the result tuple and pass it back using ExecProject()
-                * ----------------
-                */
-               projInfo = resstate->cstate.cs_ProjInfo;
-               resultSlot = ExecProject(projInfo, &isDone);
-               resstate->cstate.cs_TupFromTlist = !isDone;
-               return resultSlot;
+               /* form the result tuple using ExecProject(), and return it */
+               return ExecProject(node->ps.ps_ProjInfo);
        }
 
        return NULL;
 }
 
+/* ----------------------------------------------------------------
+ *             ExecResultMarkPos
+ * ----------------------------------------------------------------
+ */
+void
+ExecResultMarkPos(ResultState *node)
+{
+       PlanState  *outerPlan = outerPlanState(node);
+
+       if (outerPlan != NULL)
+               ExecMarkPos(outerPlan);
+       else
+               elog(DEBUG2, "Result nodes do not support mark/restore");
+}
+
+/* ----------------------------------------------------------------
+ *             ExecResultRestrPos
+ * ----------------------------------------------------------------
+ */
+void
+ExecResultRestrPos(ResultState *node)
+{
+       PlanState  *outerPlan = outerPlanState(node);
+
+       if (outerPlan != NULL)
+               ExecRestrPos(outerPlan);
+       else
+               elog(ERROR, "Result nodes do not support mark/restore");
+}
+
 /* ----------------------------------------------------------------
  *             ExecInitResult
  *
  *             Creates the run-time state information for the result node
- *             produced by the planner and initailizes outer relations
+ *             produced by the planner and initializes outer relations
  *             (child nodes).
  * ----------------------------------------------------------------
  */
-bool
-ExecInitResult(Result *node, EState *estate, Plan *parent)
+ResultState *
+ExecInitResult(Result *node, EState *estate, int eflags)
 {
        ResultState *resstate;
 
-       /* ----------------
-        *      assign execution state to node
-        * ----------------
-        */
-       node->plan.state = estate;
+       /* check for unsupported flags */
+       Assert(!(eflags & (EXEC_FLAG_MARK | EXEC_FLAG_BACKWARD)) ||
+                  outerPlan(node) != NULL);
 
-       /* ----------------
-        *      create new ResultState for node
-        * ----------------
+       /*
+        * create state structure
         */
        resstate = makeNode(ResultState);
+       resstate->ps.plan = (Plan *) node;
+       resstate->ps.state = estate;
+
        resstate->rs_done = false;
        resstate->rs_checkqual = (node->resconstantqual == NULL) ? false : true;
-       node->resstate = resstate;
 
-       /* ----------------
-        *      Miscellanious initialization
+       /*
+        * Miscellaneous initialization
         *
-        *               +      assign node's base_id
-        *               +      assign debugging hooks and
-        *               +      create expression context for node
-        * ----------------
+        * create expression context for node
         */
-       ExecAssignNodeBaseInfo(estate, &resstate->cstate, parent);
-       ExecAssignExprContext(estate, &resstate->cstate);
+       ExecAssignExprContext(estate, &resstate->ps);
 
-#define RESULT_NSLOTS 1
-       /* ----------------
-        *      tuple table initialization
-        * ----------------
+       /*
+        * tuple table initialization
+        */
+       ExecInitResultTupleSlot(estate, &resstate->ps);
+
+       /*
+        * initialize child expressions
         */
-       ExecInitResultTupleSlot(estate, &resstate->cstate);
+       resstate->ps.qual =
+               ExecInitQual(node->plan.qual, (PlanState *) resstate);
+       resstate->resconstantqual =
+               ExecInitQual((List *) node->resconstantqual, (PlanState *) resstate);
 
-       /* ----------------
-        *      then initialize children
-        * ----------------
+       /*
+        * initialize child nodes
         */
-       ExecInitNode(outerPlan(node), estate, (Plan *) node);
+       outerPlanState(resstate) = ExecInitNode(outerPlan(node), estate, eflags);
 
        /*
         * we don't use inner plan
         */
        Assert(innerPlan(node) == NULL);
 
-       /* ----------------
-        *      initialize tuple type and projection info
-        * ----------------
+       /*
+        * initialize tuple type and projection info
         */
-       ExecAssignResultTypeFromTL((Plan *) node, &resstate->cstate);
-       ExecAssignProjectionInfo((Plan *) node, &resstate->cstate);
-
-       return TRUE;
-}
+       ExecAssignResultTypeFromTL(&resstate->ps);
+       ExecAssignProjectionInfo(&resstate->ps, NULL);
 
-int
-ExecCountSlotsResult(Result *node)
-{
-       return ExecCountSlotsNode(outerPlan(node)) + RESULT_NSLOTS;
+       return resstate;
 }
 
 /* ----------------------------------------------------------------
  *             ExecEndResult
  *
- *             fees up storage allocated through C routines
+ *             frees up storage allocated through C routines
  * ----------------------------------------------------------------
  */
 void
-ExecEndResult(Result *node)
+ExecEndResult(ResultState *node)
 {
-       ResultState *resstate;
-
-       resstate = node->resstate;
-
-       /* ----------------
-        *      Free the projection info
-        *
-        *      Note: we don't ExecFreeResultType(resstate)
-        *                because the rule manager depends on the tupType
-        *                returned by ExecMain().  So for now, this
-        *                is freed at end-transaction time.  -cim 6/2/91
-        * ----------------
+       /*
+        * Free the exprcontext
         */
-       ExecFreeProjectionInfo(&resstate->cstate);
+       ExecFreeExprContext(&node->ps);
 
-       /* ----------------
-        *      shut down subplans
-        * ----------------
+       /*
+        * clean out the tuple table
         */
-       ExecEndNode(outerPlan(node), (Plan *) node);
+       ExecClearTuple(node->ps.ps_ResultTupleSlot);
 
-       /* ----------------
-        *      clean out the tuple table
-        * ----------------
+       /*
+        * shut down subplans
         */
-       ExecClearTuple(resstate->cstate.cs_ResultTupleSlot);
+       ExecEndNode(outerPlanState(node));
 }
 
 void
-ExecReScanResult(Result *node, ExprContext *exprCtxt, Plan *parent)
+ExecReScanResult(ResultState *node)
 {
-       ResultState *resstate = node->resstate;
-
-       resstate->rs_done = false;
-       resstate->cstate.cs_TupFromTlist = false;
-       resstate->rs_checkqual = (node->resconstantqual == NULL) ? false : true;
+       node->rs_done = false;
+       node->rs_checkqual = (node->resconstantqual == NULL) ? false : true;
 
        /*
-        * if chgParam of subnode is not null then plan will be re-scanned by
+        * If chgParam of subnode is not null then plan will be re-scanned by
         * first ExecProcNode.
         */
-       if (((Plan *) node)->lefttree &&
-               ((Plan *) node)->lefttree->chgParam == NULL)
-               ExecReScan(((Plan *) node)->lefttree, exprCtxt, (Plan *) node);
-
+       if (node->ps.lefttree &&
+               node->ps.lefttree->chgParam == NULL)
+               ExecReScan(node->ps.lefttree);
 }