]> granicus.if.org Git - postgresql/commitdiff
Repair planner bug introduced in 8.2 by ability to rearrange outer joins:
authorTom Lane <tgl@sss.pgh.pa.us>
Tue, 22 May 2007 23:24:09 +0000 (23:24 +0000)
committerTom Lane <tgl@sss.pgh.pa.us>
Tue, 22 May 2007 23:24:09 +0000 (23:24 +0000)
in cases where a sub-SELECT inserts a WHERE clause between two outer joins,
that clause may prevent us from re-ordering the two outer joins.  The code
was considering only the joins' own ON-conditions in determining reordering
safety, which is not good enough.  Add a "delay_upper_joins" flag to
OuterJoinInfo to flag that we have detected such a clause and higher-level
outer joins shouldn't be permitted to commute with this one.  (This might
seem overly coarse, but given the current rules for OJ reordering, it's
sufficient AFAICT.)

The failure case is actually pretty narrow: it needs a WHERE clause within
the RHS of a left join that checks the RHS of a lower left join, but is not
strict for that RHS (else we'd have simplified the lower join to a plain
join).  Even then no failure will be manifest unless the planner chooses to
rearrange the join order.

Per bug report from Adam Terrey.

src/backend/nodes/copyfuncs.c
src/backend/nodes/equalfuncs.c
src/backend/nodes/outfuncs.c
src/backend/optimizer/plan/initsplan.c
src/include/nodes/relation.h
src/test/regress/expected/join.out
src/test/regress/expected/join_1.out
src/test/regress/sql/join.sql

index c1858e6746d101f47547a1ab4e67c32898c1f372..a9cb0416c345d2bf7036dfae063c60796fd3b1ac 100644 (file)
@@ -15,7 +15,7 @@
  * Portions Copyright (c) 1994, Regents of the University of California
  *
  * IDENTIFICATION
- *       $PostgreSQL: pgsql/src/backend/nodes/copyfuncs.c,v 1.353 2006/11/05 22:42:08 tgl Exp $
+ *       $PostgreSQL: pgsql/src/backend/nodes/copyfuncs.c,v 1.353.2.1 2007/05/22 23:24:08 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -1318,6 +1318,7 @@ _copyOuterJoinInfo(OuterJoinInfo *from)
        COPY_BITMAPSET_FIELD(min_righthand);
        COPY_SCALAR_FIELD(is_full_join);
        COPY_SCALAR_FIELD(lhs_strict);
+       COPY_SCALAR_FIELD(delay_upper_joins);
 
        return newnode;
 }
index a42afb77a3da11e9c124becb7da6a81fb90e00de..13be0e0f9c84b697b1bc5359f514ba7abc790532 100644 (file)
@@ -18,7 +18,7 @@
  * Portions Copyright (c) 1994, Regents of the University of California
  *
  * IDENTIFICATION
- *       $PostgreSQL: pgsql/src/backend/nodes/equalfuncs.c,v 1.287 2006/11/05 22:42:08 tgl Exp $
+ *       $PostgreSQL: pgsql/src/backend/nodes/equalfuncs.c,v 1.287.2.1 2007/05/22 23:24:08 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -614,6 +614,7 @@ _equalOuterJoinInfo(OuterJoinInfo *a, OuterJoinInfo *b)
        COMPARE_BITMAPSET_FIELD(min_righthand);
        COMPARE_SCALAR_FIELD(is_full_join);
        COMPARE_SCALAR_FIELD(lhs_strict);
+       COMPARE_SCALAR_FIELD(delay_upper_joins);
 
        return true;
 }
index fdcc2cdc5f67d0d6372d80265ec8b9f2e0bf63d2..f80a333b7e2ce91d045e29b80c0536199376fbbb 100644 (file)
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *       $PostgreSQL: pgsql/src/backend/nodes/outfuncs.c,v 1.285.2.1 2007/05/22 01:40:42 tgl Exp $
+ *       $PostgreSQL: pgsql/src/backend/nodes/outfuncs.c,v 1.285.2.2 2007/05/22 23:24:08 tgl Exp $
  *
  * NOTES
  *       Every node type that can appear in stored rules' parsetrees *must*
@@ -1293,6 +1293,7 @@ _outOuterJoinInfo(StringInfo str, OuterJoinInfo *node)
        WRITE_BITMAPSET_FIELD(min_righthand);
        WRITE_BOOL_FIELD(is_full_join);
        WRITE_BOOL_FIELD(lhs_strict);
+       WRITE_BOOL_FIELD(delay_upper_joins);
 }
 
 static void
index ef131fba0eedb03984c75de0ba09acb7ba7df562..0b315f2cd7df324891f67f67019d77be7f59faa0 100644 (file)
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *       $PostgreSQL: pgsql/src/backend/optimizer/plan/initsplan.c,v 1.123.2.4 2007/02/16 20:57:26 tgl Exp $
+ *       $PostgreSQL: pgsql/src/backend/optimizer/plan/initsplan.c,v 1.123.2.5 2007/05/22 23:24:08 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -495,6 +495,9 @@ make_outerjoininfo(PlannerInfo *root,
                                         errmsg("SELECT FOR UPDATE/SHARE cannot be applied to the nullable side of an outer join")));
        }
 
+       /* this always starts out false */
+       ojinfo->delay_upper_joins = false;
+
        /* If it's a full join, no need to be very smart */
        ojinfo->is_full_join = is_full_join;
        if (is_full_join)
@@ -564,10 +567,21 @@ make_outerjoininfo(PlannerInfo *root,
                 * lower join's RHS and the lower OJ's join condition is strict, we
                 * can interchange the ordering of the two OJs, so exclude the lower
                 * RHS from our min_righthand.
+                *
+                * Here, we have to consider that "our join condition" includes
+                * any clauses that syntactically appeared above the lower OJ and
+                * below ours; those are equivalent to degenerate clauses in our
+                * OJ and must be treated as such.  Such clauses obviously can't
+                * reference our LHS, and they must be non-strict for the lower OJ's
+                * RHS (else reduce_outer_joins would have reduced the lower OJ to
+                * a plain join).  Hence the other ways in which we handle clauses
+                * within our join condition are not affected by them.  The net
+                * effect is therefore sufficiently represented by the
+                * delay_upper_joins flag saved for us by distribute_qual_to_rels.
                 */
                if (bms_overlap(ojinfo->min_righthand, otherinfo->min_righthand) &&
                        !bms_overlap(clause_relids, otherinfo->min_righthand) &&
-                       otherinfo->lhs_strict)
+                       otherinfo->lhs_strict && !otherinfo->delay_upper_joins)
                {
                        ojinfo->min_righthand = bms_del_members(ojinfo->min_righthand,
                                                                                                        otherinfo->min_righthand);
@@ -829,6 +843,10 @@ distribute_qual_to_rels(PlannerInfo *root, Node *clause,
                                                /* we'll need another iteration */
                                                found_some = true;
                                        }
+                                       /* set delay_upper_joins if needed */
+                                       if (!ojinfo->is_full_join &&
+                                               bms_overlap(relids, ojinfo->min_lefthand))
+                                               ojinfo->delay_upper_joins = true;
                                }
                        }
                } while (found_some);
index ad31d536e0bedd12314aeb2a97cd317151a7da4b..b1cedcc850a2a2b00107ae2418d94a4b2fe01d3c 100644 (file)
@@ -7,7 +7,7 @@
  * Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/include/nodes/relation.h,v 1.128.2.2 2007/05/22 01:40:42 tgl Exp $
+ * $PostgreSQL: pgsql/src/include/nodes/relation.h,v 1.128.2.3 2007/05/22 23:24:09 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -852,6 +852,12 @@ typedef struct InnerIndexscanInfo
  * It is not valid for either min_lefthand or min_righthand to be empty sets;
  * if they were, this would break the logic that enforces join order.
  *
+ * delay_upper_joins is set TRUE if we detect a pushed-down clause that has
+ * to be evaluated after this join is formed (because it references the RHS).
+ * Any outer joins that have such a clause and this join in their RHS cannot
+ * commute with this join, because that would leave noplace to check the
+ * pushed-down clause.  (We don't track this for FULL JOINs, either.)
+ *
  * Note: OuterJoinInfo directly represents only LEFT JOIN and FULL JOIN;
  * RIGHT JOIN is handled by switching the inputs to make it a LEFT JOIN.
  * We make an OuterJoinInfo for FULL JOINs even though there is no flexibility
@@ -865,6 +871,7 @@ typedef struct OuterJoinInfo
        Relids          min_righthand;  /* base relids in minimum RHS for join */
        bool            is_full_join;   /* it's a FULL OUTER JOIN */
        bool            lhs_strict;             /* joinclause is strict for some LHS rel */
+       bool            delay_upper_joins;      /* can't commute with upper RHS */
 } OuterJoinInfo;
 
 /*
index 245e22dcd0585e164ccba528c4b85af1844ec30e..8b6716def814c2b0ca67e46c8b503c60ec7fbf83 100644 (file)
@@ -2223,3 +2223,30 @@ select tt1.*, tt2.* from tt2 right join tt1 on tt1.joincol = tt2.joincol;
       2 |         |        |        
 (3 rows)
 
+reset enable_hashjoin;
+reset enable_nestloop;
+--
+-- regression test for 8.2 bug with improper re-ordering of left joins
+--
+create temp table tt3(f1 int, f2 text);
+insert into tt3 select x, repeat('xyzzy', 100) from generate_series(1,10000) x;
+create index tt3i on tt3(f1);
+analyze tt3;
+create temp table tt4(f1 int);
+insert into tt4 values (0),(1),(9999);
+analyze tt4;
+SELECT a.f1
+FROM tt4 a
+LEFT JOIN (
+        SELECT b.f1
+        FROM tt3 b LEFT JOIN tt3 c ON (b.f1 = c.f1)
+        WHERE c.f1 IS NULL
+) AS d ON (a.f1 = d.f1)
+WHERE d.f1 IS NULL;
+  f1  
+------
+    0
+    1
+ 9999
+(3 rows)
+
index 9677408707f771045c2d08bd5bc8cb52724b521d..8e7e4de014147c73114e525638f78e23a1d32d43 100644 (file)
@@ -2223,3 +2223,30 @@ select tt1.*, tt2.* from tt2 right join tt1 on tt1.joincol = tt2.joincol;
       2 |         |        |        
 (3 rows)
 
+reset enable_hashjoin;
+reset enable_nestloop;
+--
+-- regression test for 8.2 bug with improper re-ordering of left joins
+--
+create temp table tt3(f1 int, f2 text);
+insert into tt3 select x, repeat('xyzzy', 100) from generate_series(1,10000) x;
+create index tt3i on tt3(f1);
+analyze tt3;
+create temp table tt4(f1 int);
+insert into tt4 values (0),(1),(9999);
+analyze tt4;
+SELECT a.f1
+FROM tt4 a
+LEFT JOIN (
+        SELECT b.f1
+        FROM tt3 b LEFT JOIN tt3 c ON (b.f1 = c.f1)
+        WHERE c.f1 IS NULL
+) AS d ON (a.f1 = d.f1)
+WHERE d.f1 IS NULL;
+  f1  
+------
+    0
+    1
+ 9999
+(3 rows)
+
index 71208d2d2d83822360030d32e33e1808b973de6d..d3433effd11c884817faeb3270518ba993464674 100644 (file)
@@ -399,3 +399,28 @@ set enable_nestloop to off;
 select tt1.*, tt2.* from tt1 left join tt2 on tt1.joincol = tt2.joincol;
 
 select tt1.*, tt2.* from tt2 right join tt1 on tt1.joincol = tt2.joincol;
+
+reset enable_hashjoin;
+reset enable_nestloop;
+
+--
+-- regression test for 8.2 bug with improper re-ordering of left joins
+--
+
+create temp table tt3(f1 int, f2 text);
+insert into tt3 select x, repeat('xyzzy', 100) from generate_series(1,10000) x;
+create index tt3i on tt3(f1);
+analyze tt3;
+
+create temp table tt4(f1 int);
+insert into tt4 values (0),(1),(9999);
+analyze tt4;
+
+SELECT a.f1
+FROM tt4 a
+LEFT JOIN (
+        SELECT b.f1
+        FROM tt3 b LEFT JOIN tt3 c ON (b.f1 = c.f1)
+        WHERE c.f1 IS NULL
+) AS d ON (a.f1 = d.f1)
+WHERE d.f1 IS NULL;