99 *
1010 *
1111 * IDENTIFICATION
12- * $PostgreSQL: pgsql/src/backend/optimizer/path/indxpath.c,v 1.149 2003/11/29 19:51:50 pgsql Exp $
12+ * $PostgreSQL: pgsql/src/backend/optimizer/path/indxpath.c,v 1.150 2003/12/18 00:22:12 tgl Exp $
1313 *
1414 *-------------------------------------------------------------------------
1515 */
@@ -600,14 +600,43 @@ group_clauses_by_indexkey_for_join(Query *root,
600600{
601601Oid curClass = classes [0 ];
602602FastList clausegroup ;
603+ int numsources ;
603604List * i ;
604605
605606FastListInit (& clausegroup );
606607
608+ /*
609+ * We can always use plain restriction clauses for the rel. We scan
610+ * these first because we want them first in the clausegroup list
611+ * for the convenience of remove_redundant_join_clauses, which can
612+ * never remove non-join clauses and hence won't be able to get rid
613+ * of a non-join clause if it appears after a join clause it is
614+ * redundant with.
615+ */
616+ foreach (i ,rel -> baserestrictinfo )
617+ {
618+ RestrictInfo * rinfo = (RestrictInfo * )lfirst (i );
619+
620+ /* Can't use pushed-down clauses in outer join */
621+ if (isouterjoin && rinfo -> ispusheddown )
622+ continue ;
623+
624+ if (match_clause_to_indexcol (rel ,
625+ index ,
626+ indexcol ,
627+ curClass ,
628+ rinfo -> clause ))
629+ FastAppend (& clausegroup ,rinfo );
630+ }
631+
632+ /* found anything in base restrict list? */
633+ numsources = (FastListValue (& clausegroup )!= NIL ) ?1 :0 ;
634+
607635/* Look for joinclauses that are usable with given outer_relids */
608636foreach (i ,rel -> joininfo )
609637{
610638JoinInfo * joininfo = (JoinInfo * )lfirst (i );
639+ bool jfoundhere = false;
611640List * j ;
612641
613642if (!bms_is_subset (joininfo -> unjoined_relids ,outer_relids ))
@@ -628,20 +657,21 @@ group_clauses_by_indexkey_for_join(Query *root,
628657rinfo -> clause ))
629658{
630659FastAppend (& clausegroup ,rinfo );
631- jfound = true;
660+ if (!jfoundhere )
661+ {
662+ jfoundhere = true;
663+ jfound = true;
664+ numsources ++ ;
665+ }
632666}
633667}
634668}
635669
636670/*
637- * If we found join clauses in more than one joininfo list, we may
638- * now have clauses that are known redundant. Get rid of 'em.
639- * (There is no point in looking at restriction clauses, because
640- * remove_redundant_join_clauses will never think they are
641- * redundant, so we do this before adding restriction clauses to
642- * the clause group.)
671+ * If we found clauses in more than one list, we may now have clauses
672+ * that are known redundant. Get rid of 'em.
643673 */
644- if (FastListValue ( & clausegroup ) != NIL )
674+ if (numsources > 1 )
645675{
646676List * nl ;
647677
@@ -651,23 +681,6 @@ group_clauses_by_indexkey_for_join(Query *root,
651681FastListFromList (& clausegroup ,nl );
652682}
653683
654- /* We can also use plain restriction clauses for the rel */
655- foreach (i ,rel -> baserestrictinfo )
656- {
657- RestrictInfo * rinfo = (RestrictInfo * )lfirst (i );
658-
659- /* Can't use pushed-down clauses in outer join */
660- if (isouterjoin && rinfo -> ispusheddown )
661- continue ;
662-
663- if (match_clause_to_indexcol (rel ,
664- index ,
665- indexcol ,
666- curClass ,
667- rinfo -> clause ))
668- FastAppend (& clausegroup ,rinfo );
669- }
670-
671684/*
672685 * If no clauses match this key, we're done; we don't want to look
673686 * at keys to its right.