@@ -42,7 +42,7 @@ static List *select_mergejoin_clauses(PlannerInfo *root,
4242RelOptInfo * innerrel ,
4343List * restrictlist ,
4444JoinType jointype ,
45- bool * have_nonmergeable_clause );
45+ bool * mergejoin_allowed );
4646
4747
4848/*
@@ -78,7 +78,7 @@ add_paths_to_joinrel(PlannerInfo *root,
7878List * restrictlist )
7979{
8080List * mergeclause_list = NIL ;
81- bool have_nonmergeable_clause = false ;
81+ bool mergejoin_allowed = true ;
8282
8383/*
8484 * Find potential mergejoin clauses. We can skip this if we are not
@@ -93,13 +93,13 @@ add_paths_to_joinrel(PlannerInfo *root,
9393innerrel ,
9494restrictlist ,
9595jointype ,
96- & have_nonmergeable_clause );
96+ & mergejoin_allowed );
9797
9898/*
9999 * 1. Consider mergejoin paths where both relations must be explicitly
100100 * sorted. Skip this if we can't mergejoin.
101101 */
102- if (! have_nonmergeable_clause )
102+ if (mergejoin_allowed )
103103sort_inner_and_outer (root ,joinrel ,outerrel ,innerrel ,
104104restrictlist ,mergeclause_list ,jointype ,sjinfo );
105105
@@ -108,9 +108,9 @@ add_paths_to_joinrel(PlannerInfo *root,
108108 * sorted. This includes both nestloops and mergejoins where the outer
109109 * path is already ordered. Again, skip this if we can't mergejoin.
110110 * (That's okay because we know that nestloop can't handle right/full
111- * joins at all, so it wouldn't work inthose cases either.)
111+ * joins at all, so it wouldn't work inthe prohibited cases either.)
112112 */
113- if (! have_nonmergeable_clause )
113+ if (mergejoin_allowed )
114114match_unsorted_outer (root ,joinrel ,outerrel ,innerrel ,
115115restrictlist ,mergeclause_list ,jointype ,sjinfo );
116116
@@ -127,7 +127,7 @@ add_paths_to_joinrel(PlannerInfo *root,
127127 * those made by match_unsorted_outer when add_paths_to_joinrel() is
128128 * invoked with the two rels given in the other order.
129129 */
130- if (! have_nonmergeable_clause )
130+ if (mergejoin_allowed )
131131match_unsorted_inner (root ,joinrel ,outerrel ,innerrel ,
132132restrictlist ,mergeclause_list ,jointype ,sjinfo );
133133#endif
@@ -927,10 +927,14 @@ best_appendrel_indexscan(PlannerInfo *root, RelOptInfo *rel,
927927 * Select mergejoin clauses that are usable for a particular join.
928928 * Returns a list of RestrictInfo nodes for those clauses.
929929 *
930- * *have_nonmergeable_clause is set TRUE if this is a right/full join and
931- * there are nonmergejoinable join clauses. The executor's mergejoin
932- * machinery cannot handle such cases, so we have to avoid generating a
933- * mergejoin plan.
930+ * *mergejoin_allowed is normally set to TRUE, but it is set to FALSE if
931+ * this is a right/full join and there are nonmergejoinable join clauses.
932+ * The executor's mergejoin machinery cannot handle such cases, so we have
933+ * to avoid generating a mergejoin plan. (Note that this flag does NOT
934+ * consider whether there are actually any mergejoinable clauses. This is
935+ * correct because in some cases we need to build a clauseless mergejoin.
936+ * Simply returning NIL is therefore not enough to distinguish safe from
937+ * unsafe cases.)
934938 *
935939 * We also mark each selected RestrictInfo to show which side is currently
936940 * being considered as outer. These are transient markings that are only
@@ -947,22 +951,21 @@ select_mergejoin_clauses(PlannerInfo *root,
947951RelOptInfo * innerrel ,
948952List * restrictlist ,
949953JoinType jointype ,
950- bool * have_nonmergeable_clause )
954+ bool * mergejoin_allowed )
951955{
952956List * result_list = NIL ;
953957bool isouterjoin = IS_OUTER_JOIN (jointype );
958+ bool have_nonmergeable_joinclause = false;
954959ListCell * l ;
955960
956- * have_nonmergeable_clause = false;
957-
958961foreach (l ,restrictlist )
959962{
960963RestrictInfo * restrictinfo = (RestrictInfo * )lfirst (l );
961964
962965/*
963966 * If processing an outer join, only use its own join clauses in the
964967 * merge. For inner joins we can use pushed-down clauses too. (Note:
965- * we don't sethave_nonmergeable_clause here because pushed-down
968+ * we don't sethave_nonmergeable_joinclause here because pushed-down
966969 * clauses will become otherquals not joinquals.)
967970 */
968971if (isouterjoin && restrictinfo -> is_pushed_down )
@@ -979,7 +982,7 @@ select_mergejoin_clauses(PlannerInfo *root,
979982 * FALSE.)
980983 */
981984if (!restrictinfo -> clause || !IsA (restrictinfo -> clause ,Const ))
982- * have_nonmergeable_clause = true;
985+ have_nonmergeable_joinclause = true;
983986continue ;/* not mergejoinable */
984987}
985988
@@ -988,7 +991,7 @@ select_mergejoin_clauses(PlannerInfo *root,
988991 */
989992if (!clause_sides_match_join (restrictinfo ,outerrel ,innerrel ))
990993{
991- * have_nonmergeable_clause = true;
994+ have_nonmergeable_joinclause = true;
992995continue ;/* no good for these input relations */
993996}
994997
@@ -1017,26 +1020,24 @@ select_mergejoin_clauses(PlannerInfo *root,
10171020if (EC_MUST_BE_REDUNDANT (restrictinfo -> left_ec )||
10181021EC_MUST_BE_REDUNDANT (restrictinfo -> right_ec ))
10191022{
1020- * have_nonmergeable_clause = true;
1023+ have_nonmergeable_joinclause = true;
10211024continue ;/* can't handle redundant eclasses */
10221025}
10231026
10241027result_list = lappend (result_list ,restrictinfo );
10251028}
10261029
10271030/*
1028- * If it is not a right/full join then we don't need to insist on all the
1029- * joinclauses being mergejoinable, so reset the flag. This simplifies
1030- * the logic in add_paths_to_joinrel.
1031+ * Report whether mergejoin is allowed (see comment at top of function).
10311032 */
10321033switch (jointype )
10331034{
10341035case JOIN_RIGHT :
10351036case JOIN_FULL :
1037+ * mergejoin_allowed = !have_nonmergeable_joinclause ;
10361038break ;
10371039default :
1038- /* otherwise, it's OK to have nonmergeable join quals */
1039- * have_nonmergeable_clause = false;
1040+ * mergejoin_allowed = true;
10401041break ;
10411042}
10421043