From f7430af9342c8d02b6d0edde50c2035672808d1a Mon Sep 17 00:00:00 2001
From: Bruce Momjian <bruce@momjian.us>
Date: Mon, 8 Mar 1999 14:01:57 +0000
Subject: [PATCH] Fix OR index selectivity problem.

---
 src/backend/optimizer/path/orindxpath.c | 43 +++++++++++--------------
 src/backend/optimizer/util/plancat.c    |  3 +-
 2 files changed, 19 insertions(+), 27 deletions(-)

diff --git a/src/backend/optimizer/path/orindxpath.c b/src/backend/optimizer/path/orindxpath.c
index 5566954f595..833d00c82fa 100644
--- a/src/backend/optimizer/path/orindxpath.c
+++ b/src/backend/optimizer/path/orindxpath.c
@@ -7,7 +7,7 @@
  *
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/backend/optimizer/path/orindxpath.c,v 1.22 1999/03/08 13:35:50 momjian Exp $
+ *	  $Header: /cvsroot/pgsql/src/backend/optimizer/path/orindxpath.c,v 1.23 1999/03/08 14:01:55 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -33,8 +33,7 @@
 
 static void
 best_or_subclause_indices(Query *root, RelOptInfo *rel, List *subclauses,
-List *indices, List *examined_indexids, Cost subcost, List *selectivities,
-						  List **indexids, Cost *cost, List **selecs);
+List *indices, List **indexids, Cost *cost, Cost *selec);
 static void best_or_subclause_index(Query *root, RelOptInfo *rel, Expr *subclause,
 				   List *indices, int *indexid, Cost *cost, Cost *selec);
 
@@ -87,20 +86,17 @@ create_or_index_paths(Query *root,
 			if (index_flag)
 			{					/* used to be a lisp every function */
 				IndexPath  *pathnode = makeNode(IndexPath);
-				List	   *indexids;
+				List	   *indexids = NIL;
 				Cost		cost;
-				List	   *selecs;
+				Cost	    selec;
 
 				best_or_subclause_indices(root,
 										  rel,
 										  clausenode->clause->args,
 										  clausenode->indexids,
-										  NIL,
-										  (Cost) 0,
-										  NIL,
 										  &indexids,
 										  &cost,
-										  &selecs);
+										  &selec);
 
 				pathnode->path.pathtype = T_IndexScan;
 				pathnode->path.parent = rel;
@@ -132,7 +128,7 @@ create_or_index_paths(Query *root,
 					((Path *) pathnode)->path_cost += xfunc_get_path_cost((Path) pathnode);
 				}
 #endif
-				clausenode->selectivity = (Cost) floatVal(lfirst(selecs));
+				clausenode->selectivity = (Cost) selec;
 				t_list = lappend(t_list, pathnode);
 			}
 		}
@@ -154,8 +150,7 @@ create_or_index_paths(Query *root,
  * 'examined_indexids' is a list of those index ids to be used with
  *		subclauses that have already been examined
  * 'subcost' is the cost of using the indices in 'examined_indexids'
- * 'selectivities' is a list of the selectivities of subclauses that
- *		have already been examined
+ * 'selec' is a list of all subclauses that have already been examined
  *
  * Returns a list of the indexids, cost, and selectivities of each
  * subclause, e.g., ((i1 i2 i3) cost (s1 s2 s3)), where 'i' is an OID,
@@ -166,15 +161,15 @@ best_or_subclause_indices(Query *root,
 						  RelOptInfo *rel,
 						  List *subclauses,
 						  List *indices,
-						  List *examined_indexids,
-						  Cost subcost,
-						  List *selectivities,
 						  List **indexids,		/* return value */
-						  Cost *cost,	/* return value */
-						  List **selecs)		/* return value */
+						  Cost *cost,			/* return value */
+						  Cost *selec)			/* return value */
 {
-	List	   *slist;
+	List		*slist;
 
+	*selec = (Cost) 0.0;
+	*cost = (Cost) 0.0;
+	
 	foreach(slist, subclauses)
 	{
 		int			best_indexid;
@@ -184,17 +179,15 @@ best_or_subclause_indices(Query *root,
 		best_or_subclause_index(root, rel, lfirst(slist), lfirst(indices),
 								&best_indexid, &best_cost, &best_selec);
 
-		examined_indexids = lappendi(examined_indexids, best_indexid);
-		subcost += best_cost;
-		selectivities = lappend(selectivities, makeFloat(best_selec));
+		*indexids = lappendi(*indexids, best_indexid);
+		*cost += best_cost;
+		*selec += best_selec;
+		if (*selec > (Cost) 1.0)
+			*selec = (Cost) 1.0;
 
 		indices = lnext(indices);
 	}
 
-	*indexids = examined_indexids;
-	*cost = subcost;
-	*selecs = selectivities;
-
 	return;
 }
 
diff --git a/src/backend/optimizer/util/plancat.c b/src/backend/optimizer/util/plancat.c
index b1d43db2ab7..ed168222e9b 100644
--- a/src/backend/optimizer/util/plancat.c
+++ b/src/backend/optimizer/util/plancat.c
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/backend/optimizer/util/plancat.c,v 1.26 1999/02/21 03:48:54 scrappy Exp $
+ *	  $Header: /cvsroot/pgsql/src/backend/optimizer/util/plancat.c,v 1.27 1999/03/08 14:01:57 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -545,7 +545,6 @@ IndexSelectivity(Oid indexrelid,
 	select = 1.0;
 	for (n = 0; n < nIndexKeys; ++n)
 	{
-
 		/*
 		 * Find the AM class for this key.
 		 *
-- 
GitLab