Fix a conceptual error in my patch of 2007-10-26 that avoided considering
authorTom Lane <tgl@sss.pgh.pa.us>
Fri, 11 Jan 2008 04:02:26 +0000 (04:02 +0000)
committerTom Lane <tgl@sss.pgh.pa.us>
Fri, 11 Jan 2008 04:02:26 +0000 (04:02 +0000)
clauseless joins of relations that have unexploited join clauses.  Rather
than looking at every other base relation in the query, the correct thing is
to examine the other relations in the "initial_rels" list of the current
make_rel_from_joinlist() invocation, because those are what we actually have
the ability to join against.  This might be a subset of the whole query in
cases where join_collapse_limit or from_collapse_limit or full joins have
prevented merging the whole query into a single join problem.  This is a bit
untidy because we have to pass those rels down through a new PlannerInfo
field, but it's necessary.  Per bug #3865 from Oleg Kharin.

src/backend/optimizer/path/allpaths.c
src/backend/optimizer/path/joinrels.c
src/backend/optimizer/plan/planmain.c
src/include/nodes/relation.h

index 4541bf80039b754d723a464f6356c35e394800f7..8d9ce493cf642c0157115b1f8fe4730400bc30fe 100644 (file)
@@ -628,7 +628,12 @@ make_rel_from_joinlist(PlannerInfo *root, List *joinlist)
                /*
                 * Consider the different orders in which we could join the rels,
                 * using either GEQO or regular optimizer.
+                *
+                * We put the initial_rels list into a PlannerInfo field because
+                * has_legal_joinclause() needs to look at it (ugly :-().
                 */
+               root->initial_rels = initial_rels;
+
                if (enable_geqo && levels_needed >= geqo_threshold)
                        return geqo(root, levels_needed, initial_rels);
                else
index 9ec98de3fb7204635901d05e302cb3599ad1741d..e5dccc4754cd93f5f45bcdc911e648b8d203cad9 100644 (file)
@@ -837,33 +837,27 @@ has_join_restriction(PlannerInfo *root, RelOptInfo *rel)
  *             Detect whether the specified relation can legally be joined
  *             to any other rels using join clauses.
  *
- * We consider only joins to single other relations.  This is sufficient
- * to get a "true" result in most real queries, and an occasional erroneous
- * "false" will only cost a bit more planning time.  The reason for this
- * limitation is that considering joins to other joins would require proving
- * that the other join rel can legally be formed, which seems like too much
- * trouble for something that's only a heuristic to save planning time.
+ * We consider only joins to single other relations in the current
+ * initial_rels list.  This is sufficient to get a "true" result in most real
+ * queries, and an occasional erroneous "false" will only cost a bit more
+ * planning time.  The reason for this limitation is that considering joins to
+ * other joins would require proving that the other join rel can legally be
+ * formed, which seems like too much trouble for something that's only a
+ * heuristic to save planning time.  (Note: we must look at initial_rels
+ * and not all of the query, since when we are planning a sub-joinlist we
+ * may be forced to make clauseless joins within initial_rels even though
+ * there are join clauses linking to other parts of the query.)
  */
 static bool
 has_legal_joinclause(PlannerInfo *root, RelOptInfo *rel)
 {
-       Index           rti;
+       ListCell   *lc;
 
-       for (rti = 1; rti < root->simple_rel_array_size; rti++)
+       foreach(lc, root->initial_rels)
        {
-               RelOptInfo *rel2 = root->simple_rel_array[rti];
+               RelOptInfo *rel2 = (RelOptInfo *) lfirst(lc);
 
-               /* there may be empty slots corresponding to non-baserel RTEs */
-               if (rel2 == NULL)
-                       continue;
-
-               Assert(rel2->relid == rti);             /* sanity check on array */
-
-               /* ignore RTEs that are "other rels" */
-               if (rel2->reloptkind != RELOPT_BASEREL)
-                       continue;
-
-               /* ignore RTEs that are already in "rel" */
+               /* ignore rels that are already in "rel" */
                if (bms_overlap(rel->relids, rel2->relids))
                        continue;
 
index 5ccc599abf48ce710db9e922587a3f45d0c47bde..cea989639569693fd9dab4cd04e375c6c7a41574 100644 (file)
@@ -122,6 +122,7 @@ query_planner(PlannerInfo *root, List *tlist, double tuple_fraction,
        root->right_join_clauses = NIL;
        root->full_join_clauses = NIL;
        root->oj_info_list = NIL;
+       root->initial_rels = NIL;
 
        /*
         * Construct RelOptInfo nodes for all base relations in query, and
index 3bcf45fcb98201d77fc22dccd250535fcb5a36db..136d70a1de025e613bca823d8704907de3df4a25 100644 (file)
@@ -118,6 +118,9 @@ typedef struct PlannerInfo
        bool            hasHavingQual;  /* true if havingQual was non-null */
        bool            hasPseudoConstantQuals; /* true if any RestrictInfo has
                                                                                 * pseudoconstant = true */
+
+       /* At the end to avoid breaking existing 8.2 add-ons */
+       List       *initial_rels;       /* RelOptInfos we are now trying to join */
 } PlannerInfo;