From 942a2e94fab16e11254d69c1425622fb91fb3628 Mon Sep 17 00:00:00 2001
From: Tom Lane <tgl@sss.pgh.pa.us>
Date: Sat, 13 Jul 2002 19:20:34 +0000
Subject: [PATCH] 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.

---
 src/backend/optimizer/path/indxpath.c | 23 +++++++++++++++++++----
 1 file changed, 19 insertions(+), 4 deletions(-)

diff --git a/src/backend/optimizer/path/indxpath.c b/src/backend/optimizer/path/indxpath.c
index baeed1f34b2..bce39f1166e 100644
--- a/src/backend/optimizer/path/indxpath.c
+++ b/src/backend/optimizer/path/indxpath.c
@@ -9,7 +9,7 @@
  *
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/backend/optimizer/path/indxpath.c,v 1.119 2002/06/20 20:29:29 momjian Exp $
+ *	  $Header: /cvsroot/pgsql/src/backend/optimizer/path/indxpath.c,v 1.120 2002/07/13 19:20:34 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -35,6 +35,7 @@
 #include "parser/parse_coerce.h"
 #include "parser/parse_expr.h"
 #include "parser/parse_oper.h"
+#include "rewrite/rewriteManip.h"
 #include "utils/builtins.h"
 #include "utils/fmgroids.h"
 #include "utils/lsyscache.h"
@@ -79,7 +80,7 @@ static bool match_clause_to_indexkey(RelOptInfo *rel, IndexOptInfo *index,
 						 int indexkey, Oid opclass,
 						 Expr *clause, bool join);
 static bool pred_test(List *predicate_list, List *restrictinfo_list,
-		  List *joininfo_list);
+		  List *joininfo_list, int relvarno);
 static bool pred_test_restrict_list(Expr *predicate, List *restrictinfo_list);
 static bool pred_test_recurse_clause(Expr *predicate, Node *clause);
 static bool pred_test_recurse_pred(Expr *predicate, Node *clause);
@@ -153,7 +154,8 @@ create_index_paths(Query *root, RelOptInfo *rel)
 		 * predicate test.
 		 */
 		if (index->indpred != NIL)
-			if (!pred_test(index->indpred, restrictinfo_list, joininfo_list))
+			if (!pred_test(index->indpred, restrictinfo_list, joininfo_list,
+						   lfirsti(rel->relids)))
 				continue;
 
 		/*
@@ -957,7 +959,8 @@ indexable_operator(Expr *clause, Oid opclass, bool indexkey_on_left)
  *	  to CNF format). --Nels, Jan '93
  */
 static bool
-pred_test(List *predicate_list, List *restrictinfo_list, List *joininfo_list)
+pred_test(List *predicate_list, List *restrictinfo_list, List *joininfo_list,
+		  int relvarno)
 {
 	List	   *pred;
 
@@ -980,6 +983,18 @@ pred_test(List *predicate_list, List *restrictinfo_list, List *joininfo_list)
 		return false;			/* no restriction clauses: the test must
 								 * fail */
 
+	/*
+	 * The predicate as stored in the index definition will use varno 1
+	 * for its Vars referencing the indexed relation.  If the indexed
+	 * relation isn't varno 1 in the query, we must adjust the predicate
+	 * to make the Vars match, else equal() won't work.
+	 */
+	if (relvarno != 1)
+	{
+		predicate_list = copyObject(predicate_list);
+		ChangeVarNodes((Node *) predicate_list, 1, relvarno, 0);
+	}
+
 	foreach(pred, predicate_list)
 	{
 		/*
-- 
GitLab