Cache eval cost of qualification expressions in RestrictInfo nodes to
authorTom Lane <tgl@sss.pgh.pa.us>
Tue, 12 Dec 2000 23:33:34 +0000 (23:33 +0000)
committerTom Lane <tgl@sss.pgh.pa.us>
Tue, 12 Dec 2000 23:33:34 +0000 (23:33 +0000)
avoid repeated evaluations in cost_qual_eval().  This turns out to save
a useful fraction of planning time.  No change to external representation
of RestrictInfo --- although that node type doesn't appear in stored
rules anyway.

src/backend/nodes/copyfuncs.c
src/backend/nodes/equalfuncs.c
src/backend/nodes/readfuncs.c
src/backend/optimizer/path/costsize.c
src/backend/optimizer/plan/initsplan.c
src/backend/optimizer/prep/prepunion.c
src/include/nodes/relation.h

index 8bca4f84219dcd8f0208b284329c02503f77e858..96288f675eaf529586bc281667c041095abfac08 100644 (file)
@@ -1418,6 +1418,7 @@ _copyRestrictInfo(RestrictInfo *from)
         * ----------------
         */
        Node_Copy(from, newnode, clause);
+       newnode->eval_cost = from->eval_cost;
        newnode->ispusheddown = from->ispusheddown;
        Node_Copy(from, newnode, subclauseindices);
        newnode->mergejoinoperator = from->mergejoinoperator;
index dec01f0b1fbeb556c84afef0143ba1dde19048a9..c95109578f0b6dc5f999ea7a6f0197d84c55f09d 100644 (file)
@@ -514,6 +514,10 @@ _equalRestrictInfo(RestrictInfo *a, RestrictInfo *b)
 {
        if (!equal(a->clause, b->clause))
                return false;
+       /*
+        * ignore eval_cost, since it may not be set yet, and should be
+        * derivable from the clause anyway
+        */
        if (a->ispusheddown != b->ispusheddown)
                return false;
        if (!equal(a->subclauseindices, b->subclauseindices))
index 339e1dc830f549711abe6ac5d89d5b779427f746..ed58f2ff33f85c35673b63152f86382348af3a8e 100644 (file)
@@ -1846,6 +1846,9 @@ _readRestrictInfo(void)
        token = lsptok(NULL, &length);          /* now read it */
        local_node->hashjoinoperator = (Oid) atol(token);
 
+       /* eval_cost is not part of saved representation; compute on first use */
+       local_node->eval_cost = -1;
+
        return local_node;
 }
 
index 70414fdac9689a2a47eea46ed3c087ffb3c107e7..cebe6a28681e832407c5bca4f8c272dacc7ab974 100644 (file)
@@ -672,8 +672,38 @@ Cost
 cost_qual_eval(List *quals)
 {
        Cost            total = 0;
+       List       *l;
 
-       cost_qual_eval_walker((Node *) quals, &total);
+       /* We don't charge any cost for the implicit ANDing at top level ... */
+
+       foreach(l, quals)
+       {
+               Node   *qual = (Node *) lfirst(l);
+
+               /*
+                * RestrictInfo nodes contain an eval_cost field reserved for this
+                * routine's use, so that it's not necessary to evaluate the qual
+                * clause's cost more than once.  If the clause's cost hasn't been
+                * computed yet, the field will contain -1.
+                */
+               if (qual && IsA(qual, RestrictInfo))
+               {
+                       RestrictInfo *restrictinfo = (RestrictInfo *) qual;
+
+                       if (restrictinfo->eval_cost < 0)
+                       {
+                               restrictinfo->eval_cost = 0;
+                               cost_qual_eval_walker((Node *) restrictinfo->clause,
+                                                                         &restrictinfo->eval_cost);
+                       }
+                       total += restrictinfo->eval_cost;
+               }
+               else
+               {
+                       /* If it's a bare expression, must always do it the hard way */
+                       cost_qual_eval_walker(qual, &total);
+               }
+       }
        return total;
 }
 
@@ -748,18 +778,6 @@ cost_qual_eval_walker(Node *node, Cost *total)
                }
                /* fall through to examine args of Expr node */
        }
-
-       /*
-        * expression_tree_walker doesn't know what to do with RestrictInfo
-        * nodes, but we just want to recurse through them.
-        */
-       if (IsA(node, RestrictInfo))
-       {
-               RestrictInfo *restrictinfo = (RestrictInfo *) node;
-
-               return cost_qual_eval_walker((Node *) restrictinfo->clause, total);
-       }
-       /* Otherwise, recurse. */
        return expression_tree_walker(node, cost_qual_eval_walker,
                                                                  (void *) total);
 }
index 120634657d74b4712a7e480b2a5c844411196dee..c45ca098d623474364fe862122203f2a2dc2650f 100644 (file)
@@ -338,6 +338,7 @@ distribute_qual_to_rels(Query *root, Node *clause,
        bool            can_be_equijoin;
 
        restrictinfo->clause = (Expr *) clause;
+       restrictinfo->eval_cost = -1; /* not computed until needed */
        restrictinfo->subclauseindices = NIL;
        restrictinfo->mergejoinoperator = InvalidOid;
        restrictinfo->left_sortop = InvalidOid;
index 10cb2f12daa224056517e4b3f06fe329a9e2dab1..472d83363ed5cca0865c4618a1baae9838f64443 100644 (file)
@@ -652,7 +652,7 @@ adjust_inherited_attrs_mutator(Node *node,
        /*
         * We have to process RestrictInfo nodes specially: we do NOT want to
         * copy the original subclauseindices list, since the new rel may have
-        * different indices.  The list will be rebuilt during planning anyway.
+        * different indices.  The list will be rebuilt during later planning.
         */
        if (IsA(node, RestrictInfo))
        {
@@ -666,6 +666,7 @@ adjust_inherited_attrs_mutator(Node *node,
                        adjust_inherited_attrs_mutator((Node *) oldinfo->clause, context);
 
                newinfo->subclauseindices = NIL;
+               newinfo->eval_cost = -1; /* reset this too */
 
                return (Node *) newinfo;
        }
index 0e278fa9623eecb88a8b0243d4f77f4d13ac603f..e21e8cba3ed7a1e9ee3c97a36f906c345047ac1c 100644 (file)
@@ -373,9 +373,9 @@ typedef JoinPath NestPath;
  * A mergejoin path has these fields.
  *
  * path_mergeclauses lists the clauses (in the form of RestrictInfos)
- * that will be used in the merge.     (Before 7.0, this was a list of
- * bare clause expressions, but we can save on list memory by leaving
- * it in the form of a RestrictInfo list.)
+ * that will be used in the merge.     (Before 7.0, this was a list of bare
+ * clause expressions, but we can save on list memory and cost_qual_eval
+ * work by leaving it in the form of a RestrictInfo list.)
  *
  * Note that the mergeclauses are a subset of the parent relation's
  * restriction-clause list.  Any join clauses that are not mergejoinable
@@ -491,6 +491,8 @@ typedef struct RestrictInfo
 
        Expr       *clause;                     /* the represented clause of WHERE or JOIN */
 
+       Cost            eval_cost;              /* eval cost of clause; -1 if not yet set */
+
        bool            ispusheddown;   /* TRUE if clause was pushed down in level */
 
        /* only used if clause is an OR clause: */