88 *
99 *
1010 * IDENTIFICATION
11- * $PostgreSQL: pgsql/src/backend/optimizer/plan/initsplan.c,v 1.148 2009/02/25 03:30:37 tgl Exp $
11+ * $PostgreSQL: pgsql/src/backend/optimizer/plan/initsplan.c,v 1.149 2009/02/27 22:41:38 tgl Exp $
1212 *
1313 *-------------------------------------------------------------------------
1414 */
@@ -628,26 +628,32 @@ make_outerjoininfo(PlannerInfo *root,
628628 * min_lefthand. (We must use its full syntactic relset, not just its
629629 * min_lefthand + min_righthand. This is because there might be other
630630 * OJs below this one that this one can commute with, but we cannot
631- * commute with them if we don't with this one.)
631+ * commute with them if we don't with this one.) Also, if the
632+ * current join is an antijoin, we must preserve ordering regardless
633+ * of strictness.
632634 *
633635 * Note: I believe we have to insist on being strict for at least one
634636 * rel in the lower OJ's min_righthand, not its whole syn_righthand.
635637 */
636- if (bms_overlap (left_rels ,otherinfo -> syn_righthand )&&
637- bms_overlap (clause_relids ,otherinfo -> syn_righthand )&&
638- !bms_overlap (strict_relids ,otherinfo -> min_righthand ))
638+ if (bms_overlap (left_rels ,otherinfo -> syn_righthand ))
639639{
640- min_lefthand = bms_add_members (min_lefthand ,
641- otherinfo -> syn_lefthand );
642- min_lefthand = bms_add_members (min_lefthand ,
643- otherinfo -> syn_righthand );
640+ if (bms_overlap (clause_relids ,otherinfo -> syn_righthand )&&
641+ (jointype == JOIN_ANTI ||
642+ !bms_overlap (strict_relids ,otherinfo -> min_righthand )))
643+ {
644+ min_lefthand = bms_add_members (min_lefthand ,
645+ otherinfo -> syn_lefthand );
646+ min_lefthand = bms_add_members (min_lefthand ,
647+ otherinfo -> syn_righthand );
648+ }
644649}
645650
646651/*
647652 * For a lower OJ in our RHS, if our join condition does not use the
648653 * lower join's RHS and the lower OJ's join condition is strict, we
649654 * can interchange the ordering of the two OJs; otherwise we must add
650- * lower OJ's full syntactic relset to min_righthand.
655+ * lower OJ's full syntactic relset to min_righthand. Here, we must
656+ * preserve ordering anyway if the lower OJ is an antijoin.
651657 *
652658 * Here, we have to consider that "our join condition" includes any
653659 * clauses that syntactically appeared above the lower OJ and below
@@ -663,6 +669,7 @@ make_outerjoininfo(PlannerInfo *root,
663669if (bms_overlap (right_rels ,otherinfo -> syn_righthand ))
664670{
665671if (bms_overlap (clause_relids ,otherinfo -> syn_righthand )||
672+ otherinfo -> jointype == JOIN_ANTI ||
666673!otherinfo -> lhs_strict || otherinfo -> delay_upper_joins )
667674{
668675min_righthand = bms_add_members (min_righthand ,