From 3d6e538edfea2c610d1fd866dcea04c1927306bc Mon Sep 17 00:00:00 2001
From: Tom Lane <tgl@sss.pgh.pa.us>
Date: Fri, 5 Nov 2004 20:45:10 +0000
Subject: [PATCH] pred_test() logic was being too narrow-minded about where it
 might find RestrictInfo nodes in the query expression.  Per example from
 James Robinson.

---
 src/backend/optimizer/path/indxpath.c | 20 ++++++++++++--------
 1 file changed, 12 insertions(+), 8 deletions(-)

diff --git a/src/backend/optimizer/path/indxpath.c b/src/backend/optimizer/path/indxpath.c
index 2a0c3d1c5d6..961b382e683 100644
--- a/src/backend/optimizer/path/indxpath.c
+++ b/src/backend/optimizer/path/indxpath.c
@@ -9,7 +9,7 @@
  *
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/backend/optimizer/path/indxpath.c,v 1.165 2004/10/11 22:56:56 tgl Exp $
+ *	  $PostgreSQL: pgsql/src/backend/optimizer/path/indxpath.c,v 1.166 2004/11/05 20:45:10 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -848,13 +848,9 @@ pred_test_restrict_list(Expr *predicate, List *restrictinfo_list)
 
 	foreach(item, restrictinfo_list)
 	{
-		RestrictInfo *restrictinfo = (RestrictInfo *) lfirst(item);
-
-		Assert(IsA(restrictinfo, RestrictInfo));
-
 		/* if any clause implies the predicate, return true */
 		if (pred_test_recurse_restrict(predicate,
-									   (Node *) restrictinfo->clause))
+									   (Node *) lfirst(item)))
 			return true;
 	}
 	return false;
@@ -865,7 +861,8 @@ pred_test_restrict_list(Expr *predicate, List *restrictinfo_list)
  * pred_test_recurse_restrict
  *	  Does the "predicate inclusion test" for one element of a predicate
  *	  expression.  Here we recursively deal with the possibility that the
- *	  restriction-list element is itself an AND or OR structure.
+ *	  restriction-list element is itself an AND or OR structure; also,
+ *	  we strip off RestrictInfo nodes to find bare predicate expressions.
  */
 static bool
 pred_test_recurse_restrict(Expr *predicate, Node *clause)
@@ -874,7 +871,14 @@ pred_test_recurse_restrict(Expr *predicate, Node *clause)
 	ListCell   *item;
 
 	Assert(clause != NULL);
-	if (or_clause(clause))
+	if (IsA(clause, RestrictInfo))
+	{
+		RestrictInfo *restrictinfo = (RestrictInfo *) clause;
+
+		return pred_test_recurse_restrict(predicate,
+										  (Node *) restrictinfo->clause);
+	}
+	else if (or_clause(clause))
 	{
 		items = ((BoolExpr *) clause)->args;
 		foreach(item, items)
-- 
GitLab