9
9
*
10
10
*
11
11
* 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 $
13
13
*
14
14
*-------------------------------------------------------------------------
15
15
*/
35
35
#include "parser/parse_coerce.h"
36
36
#include "parser/parse_expr.h"
37
37
#include "parser/parse_oper.h"
38
+ #include "rewrite/rewriteManip.h"
38
39
#include "utils/builtins.h"
39
40
#include "utils/fmgroids.h"
40
41
#include "utils/lsyscache.h"
@@ -79,7 +80,7 @@ static bool match_clause_to_indexkey(RelOptInfo *rel, IndexOptInfo *index,
79
80
int indexkey ,Oid opclass ,
80
81
Expr * clause ,bool join );
81
82
static bool pred_test (List * predicate_list ,List * restrictinfo_list ,
82
- List * joininfo_list );
83
+ List * joininfo_list , int relvarno );
83
84
static bool pred_test_restrict_list (Expr * predicate ,List * restrictinfo_list );
84
85
static bool pred_test_recurse_clause (Expr * predicate ,Node * clause );
85
86
static bool pred_test_recurse_pred (Expr * predicate ,Node * clause );
@@ -153,7 +154,8 @@ create_index_paths(Query *root, RelOptInfo *rel)
153
154
* predicate test.
154
155
*/
155
156
if (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 )))
157
159
continue ;
158
160
159
161
/*
@@ -957,7 +959,8 @@ indexable_operator(Expr *clause, Oid opclass, bool indexkey_on_left)
957
959
* to CNF format). --Nels, Jan '93
958
960
*/
959
961
static 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 )
961
964
{
962
965
List * pred ;
963
966
@@ -980,6 +983,18 @@ pred_test(List *predicate_list, List *restrictinfo_list, List *joininfo_list)
980
983
return false;/* no restriction clauses: the test must
981
984
* fail */
982
985
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
+
983
998
foreach (pred ,predicate_list )
984
999
{
985
1000
/*