Skip to content

Commit 942a2e9

Browse files
committed
Fix testing of partial-index predicates to work correctly in cases where
varno of index's relation is not 1. This embarrassing oversight pointed out by Dmitry Tkach 12-Jul-02.
1 parent 59097af commit 942a2e9

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

src/backend/optimizer/path/indxpath.c

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
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
*/
@@ -35,6 +35,7 @@
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,
7980
int indexkey, Oid opclass,
8081
Expr *clause, bool join);
8182
static bool pred_test(List *predicate_list, List *restrictinfo_list,
82-
List *joininfo_list);
83+
List *joininfo_list, int relvarno);
8384
static bool pred_test_restrict_list(Expr *predicate, List *restrictinfo_list);
8485
static bool pred_test_recurse_clause(Expr *predicate, Node *clause);
8586
static bool pred_test_recurse_pred(Expr *predicate, Node *clause);
@@ -153,7 +154,8 @@ create_index_paths(Query *root, RelOptInfo *rel)
153154
* predicate test.
154155
*/
155156
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)))
157159
continue;
158160

159161
/*
@@ -957,7 +959,8 @@ indexable_operator(Expr *clause, Oid opclass, bool indexkey_on_left)
957959
* to CNF format). --Nels, Jan '93
958960
*/
959961
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)
961964
{
962965
List *pred;
963966

@@ -980,6 +983,18 @@ pred_test(List *predicate_list, List *restrictinfo_list, List *joininfo_list)
980983
return 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+
983998
foreach(pred, predicate_list)
984999
{
9851000
/*

0 commit comments

Comments
 (0)