99 *
1010 *
1111 * IDENTIFICATION
12- * $Header: /cvsroot/pgsql/src/backend/optimizer/path/indxpath.c,v 1.119 2002/06/20 20:29:29 momjian Exp $
12+ * $Header: /cvsroot/pgsql/src/backend/optimizer/path/indxpath.c,v 1.120 2002/07/13 19: 20:34 tgl Exp $
1313 *
1414 *-------------------------------------------------------------------------
1515 */
3535#include "parser/parse_coerce.h"
3636#include "parser/parse_expr.h"
3737#include "parser/parse_oper.h"
38+ #include "rewrite/rewriteManip.h"
3839#include "utils/builtins.h"
3940#include "utils/fmgroids.h"
4041#include "utils/lsyscache.h"
@@ -79,7 +80,7 @@ static bool match_clause_to_indexkey(RelOptInfo *rel, IndexOptInfo *index,
7980int indexkey ,Oid opclass ,
8081Expr * clause ,bool join );
8182static bool pred_test (List * predicate_list ,List * restrictinfo_list ,
82- List * joininfo_list );
83+ List * joininfo_list , int relvarno );
8384static bool pred_test_restrict_list (Expr * predicate ,List * restrictinfo_list );
8485static bool pred_test_recurse_clause (Expr * predicate ,Node * clause );
8586static bool pred_test_recurse_pred (Expr * predicate ,Node * clause );
@@ -153,7 +154,8 @@ create_index_paths(Query *root, RelOptInfo *rel)
153154 * predicate test.
154155 */
155156if (index -> indpred != NIL )
156- if (!pred_test (index -> indpred ,restrictinfo_list ,joininfo_list ))
157+ if (!pred_test (index -> indpred ,restrictinfo_list ,joininfo_list ,
158+ lfirsti (rel -> relids )))
157159continue ;
158160
159161/*
@@ -957,7 +959,8 @@ indexable_operator(Expr *clause, Oid opclass, bool indexkey_on_left)
957959 * to CNF format). --Nels, Jan '93
958960 */
959961static bool
960- pred_test (List * predicate_list ,List * restrictinfo_list ,List * joininfo_list )
962+ pred_test (List * predicate_list ,List * restrictinfo_list ,List * joininfo_list ,
963+ int relvarno )
961964{
962965List * pred ;
963966
@@ -980,6 +983,18 @@ pred_test(List *predicate_list, List *restrictinfo_list, List *joininfo_list)
980983return false;/* no restriction clauses: the test must
981984 * fail */
982985
986+ /*
987+ * The predicate as stored in the index definition will use varno 1
988+ * for its Vars referencing the indexed relation. If the indexed
989+ * relation isn't varno 1 in the query, we must adjust the predicate
990+ * to make the Vars match, else equal() won't work.
991+ */
992+ if (relvarno != 1 )
993+ {
994+ predicate_list = copyObject (predicate_list );
995+ ChangeVarNodes ((Node * )predicate_list ,1 ,relvarno ,0 );
996+ }
997+
983998foreach (pred ,predicate_list )
984999{
9851000/*