]> granicus.if.org Git - postgresql/blobdiff - src/backend/optimizer/util/relnode.c
Desultory de-FastList-ification. RelOptInfo.reltargetlist is back to
[postgresql] / src / backend / optimizer / util / relnode.c
index b4764ab6f8cd08295a6913ccf680c9d7c5cb9a47..36e688135a8cb9c1ea1c5f2b3ce1c7e96eacd414 100644 (file)
@@ -3,12 +3,12 @@
  * relnode.c
  *       Relation-node lookup/construction routines
  *
- * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
  *
  * IDENTIFICATION
- *       $Header: /cvsroot/pgsql/src/backend/optimizer/util/relnode.c,v 1.32 2001/02/16 00:03:08 tgl Exp $
+ *       $PostgreSQL: pgsql/src/backend/optimizer/util/relnode.c,v 1.59 2004/06/01 03:03:02 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
 #include "optimizer/joininfo.h"
 #include "optimizer/pathnode.h"
 #include "optimizer/plancat.h"
+#include "optimizer/restrictinfo.h"
 #include "optimizer/tlist.h"
 #include "parser/parsetree.h"
 
 
-static List *new_join_tlist(List *tlist, int first_resdomno);
-static List *build_joinrel_restrictlist(RelOptInfo *joinrel,
+static RelOptInfo *make_base_rel(Query *root, int relid);
+static void build_joinrel_tlist(Query *root, RelOptInfo *joinrel);
+static List *build_joinrel_restrictlist(Query *root,
+                                                  RelOptInfo *joinrel,
                                                   RelOptInfo *outer_rel,
-                                                  RelOptInfo *inner_rel);
+                                                  RelOptInfo *inner_rel,
+                                                  JoinType jointype);
 static void build_joinrel_joinlist(RelOptInfo *joinrel,
                                           RelOptInfo *outer_rel,
                                           RelOptInfo *inner_rel);
@@ -36,26 +40,30 @@ static void subbuild_joinrel_joinlist(RelOptInfo *joinrel,
 
 
 /*
- * get_base_rel
- *       Returns relation entry corresponding to 'relid', creating a new one
- *       if necessary.  This is for base relations.
+ * build_base_rel
+ *       Construct a new base relation RelOptInfo, and put it in the query's
+ *       base_rel_list.
  */
-RelOptInfo *
-get_base_rel(Query *root, int relid)
+void
+build_base_rel(Query *root, int relid)
 {
-       List       *baserels;
+       ListCell   *l;
        RelOptInfo *rel;
 
-       foreach(baserels, root->base_rel_list)
+       /* Rel should not exist already */
+       foreach(l, root->base_rel_list)
        {
-               rel = (RelOptInfo *) lfirst(baserels);
+               rel = (RelOptInfo *) lfirst(l);
+               if (relid == rel->relid)
+                       elog(ERROR, "rel already exists");
+       }
 
-               /*
-                * We know length(rel->relids) == 1 for all members of
-                * base_rel_list
-                */
-               if (lfirsti(rel->relids) == relid)
-                       return rel;
+       /* It should not exist as an "other" rel, either */
+       foreach(l, root->other_rel_list)
+       {
+               rel = (RelOptInfo *) lfirst(l);
+               if (relid == rel->relid)
+                       elog(ERROR, "rel already exists as \"other\" rel");
        }
 
        /* No existing RelOptInfo for this base rel, so make a new one */
@@ -63,6 +71,45 @@ get_base_rel(Query *root, int relid)
 
        /* and add it to the list */
        root->base_rel_list = lcons(rel, root->base_rel_list);
+}
+
+/*
+ * build_other_rel
+ *       Returns relation entry corresponding to 'relid', creating a new one
+ *       if necessary.  This is for 'other' relations, which are much like
+ *       base relations except that they live in a different list.
+ */
+RelOptInfo *
+build_other_rel(Query *root, int relid)
+{
+       ListCell   *l;
+       RelOptInfo *rel;
+
+       /* Already made? */
+       foreach(l, root->other_rel_list)
+       {
+               rel = (RelOptInfo *) lfirst(l);
+               if (relid == rel->relid)
+                       return rel;
+       }
+
+       /* It should not exist as a base rel */
+       foreach(l, root->base_rel_list)
+       {
+               rel = (RelOptInfo *) lfirst(l);
+               if (relid == rel->relid)
+                       elog(ERROR, "rel already exists as base rel");
+       }
+
+       /* No existing RelOptInfo for this other rel, so make a new one */
+       rel = make_base_rel(root, relid);
+
+       /* presently, must be an inheritance child rel */
+       Assert(rel->reloptkind == RELOPT_BASEREL);
+       rel->reloptkind = RELOPT_OTHER_CHILD_REL;
+
+       /* and add it to the list */
+       root->other_rel_list = lcons(rel, root->other_rel_list);
 
        return rel;
 }
@@ -71,74 +118,112 @@ get_base_rel(Query *root, int relid)
  * make_base_rel
  *       Construct a base-relation RelOptInfo for the specified rangetable index.
  *
- * This is split out of get_base_rel so that inheritance-tree processing can
- * construct baserel nodes for child tables.  We need a RelOptInfo so we can
- * plan a suitable access path for each child table, but we do NOT want to
- * enter the child nodes into base_rel_list.  In most contexts, get_base_rel
- * should be called instead.
+ * Common code for build_base_rel and build_other_rel.
  */
-RelOptInfo *
+static RelOptInfo *
 make_base_rel(Query *root, int relid)
 {
        RelOptInfo *rel = makeNode(RelOptInfo);
-       Oid                     relationObjectId;
+       RangeTblEntry *rte = rt_fetch(relid, root->rtable);
 
-       rel->relids = makeListi1(relid);
+       rel->reloptkind = RELOPT_BASEREL;
+       rel->relids = bms_make_singleton(relid);
        rel->rows = 0;
        rel->width = 0;
-       rel->targetlist = NIL;
+       rel->reltargetlist = NIL;
        rel->pathlist = NIL;
        rel->cheapest_startup_path = NULL;
        rel->cheapest_total_path = NULL;
-       rel->pruneable = true;
-       rel->issubquery = false;
-       rel->indexed = false;
+       rel->cheapest_unique_path = NULL;
+       rel->relid = relid;
+       rel->rtekind = rte->rtekind;
+       /* min_attr, max_attr, attr_needed, attr_widths are set below */
+       rel->indexlist = NIL;
        rel->pages = 0;
        rel->tuples = 0;
        rel->subplan = NULL;
        rel->baserestrictinfo = NIL;
-       rel->baserestrictcost = 0;
-       rel->outerjoinset = NIL;
+       rel->baserestrictcost.startup = 0;
+       rel->baserestrictcost.per_tuple = 0;
+       rel->outerjoinset = NULL;
        rel->joininfo = NIL;
-       rel->innerjoin = NIL;
+       rel->index_outer_relids = NULL;
+       rel->index_inner_paths = NIL;
 
-       /* Check rtable to see if it's a plain relation or a subquery */
-       relationObjectId = getrelid(relid, root->rtable);
+       /* Check type of rtable entry */
+       switch (rte->rtekind)
+       {
+               case RTE_RELATION:
+                       /* Table --- retrieve statistics from the system catalogs */
+                       get_relation_info(rte->relid, rel);
+                       break;
+               case RTE_SUBQUERY:
+               case RTE_FUNCTION:
+                       /* Subquery or function --- need only set up attr range */
+                       /* Note: 0 is included in range to support whole-row Vars */
+                       rel->min_attr = 0;
+                       rel->max_attr = list_length(rte->eref->colnames);
+                       break;
+               default:
+                       elog(ERROR, "unrecognized RTE kind: %d",
+                                (int) rte->rtekind);
+                       break;
+       }
 
-       if (relationObjectId != InvalidOid)
+       Assert(rel->max_attr >= rel->min_attr);
+       rel->attr_needed = (Relids *)
+               palloc0((rel->max_attr - rel->min_attr + 1) * sizeof(Relids));
+       rel->attr_widths = (int32 *)
+               palloc0((rel->max_attr - rel->min_attr + 1) * sizeof(int32));
+
+       return rel;
+}
+
+/*
+ * find_base_rel
+ *       Find a base or other relation entry, which must already exist
+ *       (since we'd have no idea which list to add it to).
+ */
+RelOptInfo *
+find_base_rel(Query *root, int relid)
+{
+       ListCell   *l;
+       RelOptInfo *rel;
+
+       foreach(l, root->base_rel_list)
        {
-               /* Plain relation --- retrieve statistics from the system catalogs */
-               relation_info(relationObjectId,
-                                         &rel->indexed, &rel->pages, &rel->tuples);
+               rel = (RelOptInfo *) lfirst(l);
+               if (relid == rel->relid)
+                       return rel;
        }
-       else
+
+       foreach(l, root->other_rel_list)
        {
-               /* subquery --- mark it as such for later processing */
-               rel->issubquery = true;
+               rel = (RelOptInfo *) lfirst(l);
+               if (relid == rel->relid)
+                       return rel;
        }
 
-       return rel;
+       elog(ERROR, "no relation entry for relid %d", relid);
+
+       return NULL;                            /* keep compiler quiet */
 }
 
 /*
  * find_join_rel
- *       Returns relation entry corresponding to 'relids' (a list of RT indexes),
+ *       Returns relation entry corresponding to 'relids' (a set of RT indexes),
  *       or NULL if none exists.  This is for join relations.
- *
- * Note: there is probably no good reason for this to be called from
- * anywhere except get_join_rel, but keep it as a separate routine
- * just in case.
  */
-static RelOptInfo *
+RelOptInfo *
 find_join_rel(Query *root, Relids relids)
 {
-       List       *joinrels;
+       ListCell   *l;
 
-       foreach(joinrels, root->join_rel_list)
+       foreach(l, root->join_rel_list)
        {
-               RelOptInfo *rel = (RelOptInfo *) lfirst(joinrels);
+               RelOptInfo *rel = (RelOptInfo *) lfirst(l);
 
-               if (sameseti(rel->relids, relids))
+               if (bms_equal(rel->relids, relids))
                        return rel;
        }
 
@@ -146,10 +231,11 @@ find_join_rel(Query *root, Relids relids)
 }
 
 /*
- * get_join_rel
+ * build_join_rel
  *       Returns relation entry corresponding to the union of two given rels,
  *       creating a new relation entry if none already exists.
  *
+ * 'joinrelids' is the Relids set that uniquely identifies the join
  * 'outer_rel' and 'inner_rel' are relation nodes for the relations to be
  *             joined
  * 'jointype': type of join (inner/outer)
@@ -161,41 +247,33 @@ find_join_rel(Query *root, Relids relids)
  * duplicated calculation of the restrictlist...
  */
 RelOptInfo *
-get_join_rel(Query *root,
-                        RelOptInfo *outer_rel,
-                        RelOptInfo *inner_rel,
-                        JoinType jointype,
-                        List **restrictlist_ptr)
+build_join_rel(Query *root,
+                          Relids joinrelids,
+                          RelOptInfo *outer_rel,
+                          RelOptInfo *inner_rel,
+                          JoinType jointype,
+                          List **restrictlist_ptr)
 {
-       List       *joinrelids;
        RelOptInfo *joinrel;
        List       *restrictlist;
-       List       *new_outer_tlist;
-       List       *new_inner_tlist;
-
-       /* We should never try to join two overlapping sets of rels. */
-       Assert(nonoverlap_setsi(outer_rel->relids, inner_rel->relids));
 
        /*
         * See if we already have a joinrel for this set of base rels.
-        *
-        * nconc(listCopy(x), y) is an idiom for making a new list without
-        * changing either input list.
         */
-       joinrelids = nconc(listCopy(outer_rel->relids), inner_rel->relids);
        joinrel = find_join_rel(root, joinrelids);
 
        if (joinrel)
        {
-
                /*
                 * Yes, so we only need to figure the restrictlist for this
                 * particular pair of component relations.
                 */
                if (restrictlist_ptr)
-                       *restrictlist_ptr = build_joinrel_restrictlist(joinrel,
+                       *restrictlist_ptr = build_joinrel_restrictlist(root,
+                                                                                                                  joinrel,
                                                                                                                   outer_rel,
-                                                                                                                  inner_rel);
+                                                                                                                  inner_rel,
+                                                                                                                  jointype);
                return joinrel;
        }
 
@@ -203,49 +281,50 @@ get_join_rel(Query *root,
         * Nope, so make one.
         */
        joinrel = makeNode(RelOptInfo);
-       joinrel->relids = joinrelids;
+       joinrel->reloptkind = RELOPT_JOINREL;
+       joinrel->relids = bms_copy(joinrelids);
        joinrel->rows = 0;
        joinrel->width = 0;
-       joinrel->targetlist = NIL;
+       joinrel->reltargetlist = NIL;
        joinrel->pathlist = NIL;
        joinrel->cheapest_startup_path = NULL;
        joinrel->cheapest_total_path = NULL;
-       joinrel->pruneable = true;
-       joinrel->issubquery = false;
-       joinrel->indexed = false;
+       joinrel->cheapest_unique_path = NULL;
+       joinrel->relid = 0;                     /* indicates not a baserel */
+       joinrel->rtekind = RTE_JOIN;
+       joinrel->min_attr = 0;
+       joinrel->max_attr = 0;
+       joinrel->attr_needed = NULL;
+       joinrel->attr_widths = NULL;
+       joinrel->indexlist = NIL;
        joinrel->pages = 0;
        joinrel->tuples = 0;
        joinrel->subplan = NULL;
        joinrel->baserestrictinfo = NIL;
-       joinrel->baserestrictcost = 0;
-       joinrel->outerjoinset = NIL;
+       joinrel->baserestrictcost.startup = 0;
+       joinrel->baserestrictcost.per_tuple = 0;
+       joinrel->outerjoinset = NULL;
        joinrel->joininfo = NIL;
-       joinrel->innerjoin = NIL;
+       joinrel->index_outer_relids = NULL;
+       joinrel->index_inner_paths = NIL;
 
        /*
-        * Create a new tlist by removing irrelevant elements from both tlists
-        * of the outer and inner join relations and then merging the results
-        * together.
-        *
-        * NOTE: the tlist order for a join rel will depend on which pair of
-        * outer and inner rels we first try to build it from.  But the
-        * contents should be the same regardless.
-        *
-        * XXX someday: consider pruning vars from the join's targetlist if they
-        * are needed only to evaluate restriction clauses of this join, and
-        * will never be accessed at higher levels of the plantree.
+        * Create a new tlist containing just the vars that need to be output
+        * from this join (ie, are needed for higher joinclauses or final
+        * output).
         */
-       new_outer_tlist = new_join_tlist(outer_rel->targetlist, 1);
-       new_inner_tlist = new_join_tlist(inner_rel->targetlist,
-                                                                        length(new_outer_tlist) + 1);
-       joinrel->targetlist = nconc(new_outer_tlist, new_inner_tlist);
+       build_joinrel_tlist(root, joinrel);
 
        /*
         * Construct restrict and join clause lists for the new joinrel. (The
         * caller might or might not need the restrictlist, but I need it
         * anyway for set_joinrel_size_estimates().)
         */
-       restrictlist = build_joinrel_restrictlist(joinrel, outer_rel, inner_rel);
+       restrictlist = build_joinrel_restrictlist(root,
+                                                                                         joinrel,
+                                                                                         outer_rel,
+                                                                                         inner_rel,
+                                                                                         jointype);
        if (restrictlist_ptr)
                *restrictlist_ptr = restrictlist;
        build_joinrel_joinlist(joinrel, outer_rel, inner_rel);
@@ -265,42 +344,51 @@ get_join_rel(Query *root,
 }
 
 /*
- * new_join_tlist
- *       Builds a join relation's target list by keeping those elements that
- *       will be in the final target list and any other elements that are still
- *       needed for future joins.      For a target list entry to still be needed
- *       for future joins, its 'joinlist' field must not be empty after removal
- *       of all relids in 'other_relids'.
+ * build_joinrel_tlist
+ *       Builds a join relation's target list.
  *
- *       XXX the above comment refers to code that is long dead and gone;
- *       we don't keep track of joinlists for individual targetlist entries
- *       anymore.      For now, all vars present in either input tlist will be
- *       emitted in the join's tlist.
+ * The join's targetlist includes all Vars of its member relations that
+ * will still be needed above the join.
  *
- * 'tlist' is the target list of one of the join relations
- * 'first_resdomno' is the resdom number to use for the first created
- *                             target list entry
+ * In a former lifetime, this just merged the tlists of the two member
+ * relations first presented.  While we could still do that, working from
+ * lists of Vars would mean doing a find_base_rel lookup for each Var.
+ * It seems more efficient to scan the list of base rels and collect the
+ * needed vars directly from there.
  *
- * Returns the new target list.
+ * We also compute the expected width of the join's output, making use
+ * of data that was cached at the baserel level by set_rel_width().
  */
-static List *
-new_join_tlist(List *tlist,
-                          int first_resdomno)
+static void
+build_joinrel_tlist(Query *root, RelOptInfo *joinrel)
 {
-       int                     resdomno = first_resdomno - 1;
-       List       *t_list = NIL;
-       List       *i;
+       Relids          relids = joinrel->relids;
+       ListCell   *rels;
+
+       joinrel->reltargetlist = NIL;
+       joinrel->width = 0;
 
-       foreach(i, tlist)
+       foreach(rels, root->base_rel_list)
        {
-               TargetEntry *xtl = lfirst(i);
+               RelOptInfo *baserel = (RelOptInfo *) lfirst(rels);
+               ListCell   *vars;
 
-               resdomno += 1;
-               t_list = lappend(t_list,
-                                                create_tl_element(get_expr(xtl), resdomno));
-       }
+               if (!bms_is_member(baserel->relid, relids))
+                       continue;
 
-       return t_list;
+               foreach(vars, baserel->reltargetlist)
+               {
+                       Var                *var = (Var *) lfirst(vars);
+                       int                     ndx = var->varattno - baserel->min_attr;
+
+                       if (bms_nonempty_difference(baserel->attr_needed[ndx], relids))
+                       {
+                               joinrel->reltargetlist = lappend(joinrel->reltargetlist, var);
+                               Assert(baserel->attr_widths[ndx] > 0);
+                               joinrel->width += baserel->attr_widths[ndx];
+                       }
+               }
+       }
 }
 
 /*
@@ -326,9 +414,15 @@ new_join_tlist(List *tlist,
  *       join paths made from this pair of sub-relations.      (It will not need to
  *       be considered further up the join tree.)
  *
+ *       When building a restriction list, we eliminate redundant clauses.
+ *       We don't try to do that for join clause lists, since the join clauses
+ *       aren't really doing anything, just waiting to become part of higher
+ *       levels' restriction lists.
+ *
  * 'joinrel' is a join relation node
  * 'outer_rel' and 'inner_rel' are a pair of relations that can be joined
  *             to form joinrel.
+ * 'jointype' is the type of join used.
  *
  * build_joinrel_restrictlist() returns a list of relevant restrictinfos,
  * whereas build_joinrel_joinlist() stores its results in the joinrel's
@@ -340,19 +434,37 @@ new_join_tlist(List *tlist,
  * the original nodes in the lists made for the join relation.
  */
 static List *
-build_joinrel_restrictlist(RelOptInfo *joinrel,
+build_joinrel_restrictlist(Query *root,
+                                                  RelOptInfo *joinrel,
                                                   RelOptInfo *outer_rel,
-                                                  RelOptInfo *inner_rel)
+                                                  RelOptInfo *inner_rel,
+                                                  JoinType jointype)
 {
+       List       *result;
+       List       *rlist;
+
+       /*
+        * Collect all the clauses that syntactically belong at this level.
+        */
+       rlist = list_concat(subbuild_joinrel_restrictlist(joinrel,
+                                                                                                         outer_rel->joininfo),
+                                               subbuild_joinrel_restrictlist(joinrel,
+                                                                                                         inner_rel->joininfo));
 
        /*
-        * We must eliminate duplicates, since we will see the same clauses
-        * arriving from both input relations...
+        * Eliminate duplicate and redundant clauses.
+        *
+        * We must eliminate duplicates, since we will see many of the same
+        * clauses arriving from both input relations.  Also, if a clause is a
+        * mergejoinable clause, it's possible that it is redundant with
+        * previous clauses (see optimizer/README for discussion).      We detect
+        * that case and omit the redundant clause from the result list.
         */
-       return set_union(subbuild_joinrel_restrictlist(joinrel,
-                                                                                                  outer_rel->joininfo),
-                                        subbuild_joinrel_restrictlist(joinrel,
-                                                                                                  inner_rel->joininfo));
+       result = remove_redundant_join_clauses(root, rlist, jointype);
+
+       list_free(rlist);
+
+       return result;
 }
 
 static void
@@ -369,15 +481,14 @@ subbuild_joinrel_restrictlist(RelOptInfo *joinrel,
                                                          List *joininfo_list)
 {
        List       *restrictlist = NIL;
-       List       *xjoininfo;
+       ListCell   *xjoininfo;
 
        foreach(xjoininfo, joininfo_list)
        {
                JoinInfo   *joininfo = (JoinInfo *) lfirst(xjoininfo);
 
-               if (is_subseti(joininfo->unjoined_relids, joinrel->relids))
+               if (bms_is_subset(joininfo->unjoined_relids, joinrel->relids))
                {
-
                        /*
                         * Clauses in this JoinInfo list become restriction clauses
                         * for the joinrel, since they refer to no outside rels.
@@ -385,12 +496,11 @@ subbuild_joinrel_restrictlist(RelOptInfo *joinrel,
                         * We must copy the list to avoid disturbing the input relation,
                         * but we can use a shallow copy.
                         */
-                       restrictlist = nconc(restrictlist,
-                                                                listCopy(joininfo->jinfo_restrictinfo));
+                       restrictlist = list_concat(restrictlist,
+                                                                          list_copy(joininfo->jinfo_restrictinfo));
                }
                else
                {
-
                        /*
                         * These clauses are still join clauses at this level, so we
                         * ignore them in this routine.
@@ -405,38 +515,42 @@ static void
 subbuild_joinrel_joinlist(RelOptInfo *joinrel,
                                                  List *joininfo_list)
 {
-       List       *xjoininfo;
+       ListCell   *xjoininfo;
 
        foreach(xjoininfo, joininfo_list)
        {
                JoinInfo   *joininfo = (JoinInfo *) lfirst(xjoininfo);
                Relids          new_unjoined_relids;
 
-               new_unjoined_relids = set_differencei(joininfo->unjoined_relids,
-                                                                                         joinrel->relids);
-               if (new_unjoined_relids == NIL)
+               new_unjoined_relids = bms_difference(joininfo->unjoined_relids,
+                                                                                        joinrel->relids);
+               if (bms_is_empty(new_unjoined_relids))
                {
-
                        /*
                         * Clauses in this JoinInfo list become restriction clauses
                         * for the joinrel, since they refer to no outside rels. So we
                         * can ignore them in this routine.
                         */
+                       bms_free(new_unjoined_relids);
                }
                else
                {
-
                        /*
                         * These clauses are still join clauses at this level, so find
                         * or make the appropriate JoinInfo item for the joinrel, and
-                        * add the clauses to it (eliminating duplicates).
+                        * add the clauses to it, eliminating duplicates.  (Since
+                        * RestrictInfo nodes are normally multiply-linked rather than
+                        * copied, pointer equality should be a sufficient test.  If
+                        * two equal() nodes should happen to sneak in, no great harm
+                        * is done --- they'll be detected by redundant-clause testing
+                        * when they reach a restriction list.)
                         */
                        JoinInfo   *new_joininfo;
 
-                       new_joininfo = find_joininfo_node(joinrel, new_unjoined_relids);
+                       new_joininfo = make_joininfo_node(joinrel, new_unjoined_relids);
                        new_joininfo->jinfo_restrictinfo =
-                               set_union(new_joininfo->jinfo_restrictinfo,
-                                                 joininfo->jinfo_restrictinfo);
+                               list_union_ptr(new_joininfo->jinfo_restrictinfo,
+                                                          joininfo->jinfo_restrictinfo);
                }
        }
 }