Commits

Tom Lane  committed 472d393

Rethink representation of index clauses' mapping to index columns.

In commit e2c2c2e8b1df7dfdb01e7e6f6191a569ce3c3195 I made use of nested
list structures to show which clauses went with which index columns, but
on reflection that's a data structure that only an old-line Lisp hacker
could love. Worse, it adds unnecessary complication to the many places
that don't much care which clauses go with which index columns. Revert
to the previous arrangement of flat lists of clauses, and instead add a
parallel integer list of column numbers. The places that care about the
pairing can chase both lists with forboth(), while the places that don't
care just examine one list the same as before.

The only real downside to this is that there are now two more lists that
need to be passed to amcostestimate functions in case they care about
column matching (which btcostestimate does, so not passing the info is not
an option). Rather than deal with 11-argument amcostestimate functions,
pass just the IndexPath and expect the functions to extract fields from it.
That gets us down to 7 arguments which is better than 11, and it seems
more future-proof against likely additions to the information we keep
about an index path.

  • Participants
  • Parent commits e2c2c2e

Comments (0)

Files changed (16)

File doc/src/sgml/indexam.sgml

 <programlisting>
 void
 amcostestimate (PlannerInfo *root,
-                IndexOptInfo *index,
-                List *indexQuals,
-                List *indexOrderBys,
+                IndexPath *path,
                 RelOptInfo *outer_rel,
                 Cost *indexStartupCost,
                 Cost *indexTotalCost,
 <programlisting>
 void
 amcostestimate (PlannerInfo *root,
-                IndexOptInfo *index,
-                List *indexQuals,
-                List *indexOrderBys,
+                IndexPath *path,
                 RelOptInfo *outer_rel,
                 Cost *indexStartupCost,
                 Cost *indexTotalCost,
                 double *indexCorrelation);
 </programlisting>
 
-   The first five parameters are inputs:
+   The first three parameters are inputs:
 
    <variablelist>
     <varlistentry>
     </varlistentry>
 
     <varlistentry>
-     <term><parameter>index</></term>
+     <term><parameter>path</></term>
      <listitem>
       <para>
-       The index being considered.
-      </para>
-     </listitem>
-    </varlistentry>
-
-    <varlistentry>
-     <term><parameter>indexQuals</></term>
-     <listitem>
-      <para>
-       List of index qual clauses (implicitly ANDed);
-       a <symbol>NIL</> list indicates no qualifiers are available.
-       Note that the list contains expression trees with RestrictInfo nodes
-       at the top, not ScanKeys.
-      </para>
-     </listitem>
-    </varlistentry>
-
-    <varlistentry>
-     <term><parameter>indexOrderBys</></term>
-     <listitem>
-      <para>
-       List of indexable ORDER BY operators, or <symbol>NIL</> if none.
-       Note that the list contains expression trees, not ScanKeys.
+       The index access path being considered.  All fields except cost and
+       selectivity values are valid.
       </para>
      </listitem>
     </varlistentry>
       <para>
        If the index is being considered for use in a join inner indexscan,
        the planner's information about the outer side of the join.  Otherwise
-       <symbol>NULL</>.  When non-<symbol>NULL</>, some of the qual clauses will be join clauses
+       <symbol>NULL</>.  When non-<symbol>NULL</>, some of the qual clauses
+       will be join clauses for joins
        with this rel rather than being simple restriction clauses.  Also,
        the cost estimator should expect that the index scan will be repeated
        for each row of the outer rel.
    row should usually be taken as <varname>cpu_index_tuple_cost</>.  In
    addition, an appropriate multiple of <varname>cpu_operator_cost</> should
    be charged for any comparison operators invoked during index processing
-   (especially evaluation of the <literal>indexQuals</> themselves).
+   (especially evaluation of the indexquals themselves).
   </para>
 
   <para>
      knowledge, use the standard optimizer function <function>clauselist_selectivity()</function>:
 
 <programlisting>
-*indexSelectivity = clauselist_selectivity(root, indexQuals,
-                                           index-&gt;rel-&gt;relid,
+*indexSelectivity = clauselist_selectivity(root, path-&gt;indexquals,
+                                           path-&gt;indexinfo-&gt;rel-&gt;relid,
                                            JOIN_INNER, NULL);
 </programlisting>
     </para>
      Estimate the number of index rows that will be visited during the
      scan.  For many index types this is the same as <parameter>indexSelectivity</> times
      the number of rows in the index, but it might be more.  (Note that the
-     index's size in pages and rows is available from the <structname>IndexOptInfo</> struct.)
+     index's size in pages and rows is available from the
+     <literal>path-&gt;indexinfo</> struct.)
     </para>
    </step>
 
  * Also, we charge for evaluation of the indexquals at each index row.
  * All the costs are assumed to be paid incrementally during the scan.
  */
-cost_qual_eval(&amp;index_qual_cost, indexQuals, root);
+cost_qual_eval(&amp;index_qual_cost, path-&gt;indexquals, root);
 *indexStartupCost = index_qual_cost.startup;
 *indexTotalCost = seq_page_cost * numIndexPages +
     (cpu_index_tuple_cost + index_qual_cost.per_tuple) * numIndexTuples;

File src/backend/access/nbtree/nbtutils.c

  *
  * The output keys must be sorted by index attribute.  Presently we expect
  * (but verify) that the input keys are already so sorted --- this is done
- * by group_clauses_by_indexkey() in indxpath.c.  Some reordering of the keys
+ * by match_clauses_to_index() in indxpath.c.  Some reordering of the keys
  * within each attribute may be done as a byproduct of the processing here,
  * but no other code depends on that.
  *

File src/backend/nodes/outfuncs.c

 	WRITE_NODE_FIELD(indexinfo);
 	WRITE_NODE_FIELD(indexclauses);
 	WRITE_NODE_FIELD(indexquals);
+	WRITE_NODE_FIELD(indexqualcols);
 	WRITE_NODE_FIELD(indexorderbys);
+	WRITE_NODE_FIELD(indexorderbycols);
 	WRITE_BOOL_FIELD(isjoininner);
 	WRITE_ENUM_FIELD(indexscandir, ScanDirection);
 	WRITE_FLOAT_FIELD(indextotalcost, "%.2f");

File src/backend/optimizer/path/costsize.c

  * results into.  All the input data they need is passed as separate
  * parameters, even though much of it could be extracted from the Path.
  * An exception is made for the cost_XXXjoin() routines, which expect all
- * the non-cost fields of the passed XXXPath to be filled in.
+ * the non-cost fields of the passed XXXPath to be filled in, and similarly
+ * cost_index() assumes the passed IndexPath is valid except for its output
+ * values.
  *
  *
  * Portions Copyright (c) 1996-2011, PostgreSQL Global Development Group
  * cost_index
  *	  Determines and returns the cost of scanning a relation using an index.
  *
- * 'index' is the index to be used
- * 'indexQuals' is a list of lists of applicable qual clauses (implicit AND
- *		semantics, one sub-list per index column)
- * 'indexOrderBys' is a list of lists of lists of ORDER BY expressions for
- *		amcanorderbyop indexes (lists per pathkey and index column)
- * 'indexonly' is true if it's an index-only scan
+ * 'path' describes the indexscan under consideration, and is complete
+ *		except for the fields to be set by this routine
  * 'outer_rel' is the outer relation when we are considering using the index
- *		scan as the inside of a nestloop join (hence, some of the indexQuals
+ *		scan as the inside of a nestloop join (hence, some of the indexquals
  *		are join clauses, and we should expect repeated scans of the index);
  *		NULL for a plain index scan
  *
- * cost_index() takes an IndexPath not just a Path, because it sets a few
- * additional fields of the IndexPath besides startup_cost and total_cost.
- * These fields are needed if the IndexPath is used in a BitmapIndexScan.
+ * In addition to startup_cost and total_cost, cost_index() sets the path's
+ * indextotalcost and indexselectivity fields.  These values are needed if
+ * the IndexPath is used in a BitmapIndexScan.
  *
- * indexQuals is a list of lists of RestrictInfo nodes, but indexOrderBys
- * is a list of lists of lists of bare expressions.
- *
- * NOTE: 'indexQuals' must contain only clauses usable as index restrictions.
- * Any additional quals evaluated as qpquals may reduce the number of returned
- * tuples, but they won't reduce the number of tuples we have to fetch from
- * the table, so they don't reduce the scan cost.
+ * NOTE: path->indexquals must contain only clauses usable as index
+ * restrictions.  Any additional quals evaluated as qpquals may reduce the
+ * number of returned tuples, but they won't reduce the number of tuples
+ * we have to fetch from the table, so they don't reduce the scan cost.
  */
 void
-cost_index(IndexPath *path, PlannerInfo *root,
-		   IndexOptInfo *index,
-		   List *indexQuals,
-		   List *indexOrderBys,
-		   bool indexonly,
-		   RelOptInfo *outer_rel)
+cost_index(IndexPath *path, PlannerInfo *root, RelOptInfo *outer_rel)
 {
+	IndexOptInfo *index = path->indexinfo;
 	RelOptInfo *baserel = index->rel;
+	bool		indexonly = (path->path.pathtype == T_IndexOnlyScan);
 	Cost		startup_cost = 0;
 	Cost		run_cost = 0;
 	Cost		indexStartupCost;
 	 * the fraction of main-table tuples we will have to retrieve) and its
 	 * correlation to the main-table tuple order.
 	 */
-	OidFunctionCall9(index->amcostestimate,
+	OidFunctionCall7(index->amcostestimate,
 					 PointerGetDatum(root),
-					 PointerGetDatum(index),
-					 PointerGetDatum(indexQuals),
-					 PointerGetDatum(indexOrderBys),
+					 PointerGetDatum(path),
 					 PointerGetDatum(outer_rel),
 					 PointerGetDatum(&indexStartupCost),
 					 PointerGetDatum(&indexTotalCost),
 	{
 		QualCost	index_qual_cost;
 
-		cost_qual_eval(&index_qual_cost, indexQuals, root);
+		cost_qual_eval(&index_qual_cost, path->indexquals, root);
 		/* any startup cost still has to be paid ... */
 		cpu_per_tuple -= index_qual_cost.per_tuple;
 	}
  * 'baserel' is the relation to be scanned
  * 'bitmapqual' is a tree of IndexPaths, BitmapAndPaths, and BitmapOrPaths
  * 'outer_rel' is the outer relation when we are considering using the bitmap
- *		scan as the inside of a nestloop join (hence, some of the indexQuals
+ *		scan as the inside of a nestloop join (hence, some of the indexquals
  *		are join clauses, and we should expect repeated scans of the table);
  *		NULL for a plain bitmap scan
  *

File src/backend/optimizer/path/indxpath.c

 static void find_indexpath_quals(Path *bitmapqual, List **quals, List **preds);
 static int	find_list_position(Node *node, List **nodelist);
 static bool check_index_only(RelOptInfo *rel, IndexOptInfo *index);
-static List *group_clauses_by_indexkey(IndexOptInfo *index,
-						  List *clauses, List *outer_clauses,
-						  Relids outer_relids,
-						  SaOpControl saop_control,
-						  bool *found_clause);
+static void match_clauses_to_index(IndexOptInfo *index,
+					   List *clauses, List *outer_clauses,
+					   Relids outer_relids,
+					   SaOpControl saop_control,
+					   List **index_clauses_p,
+					   List **clause_columns_p,
+					   bool *found_clause);
 static bool match_clause_to_indexcol(IndexOptInfo *index,
 						 int indexcol,
 						 RestrictInfo *rinfo,
 							 Oid idxcollation,
 							 RowCompareExpr *clause,
 							 Relids outer_relids);
-static List *match_index_to_pathkeys(IndexOptInfo *index, List *pathkeys);
+static void match_pathkeys_to_index(IndexOptInfo *index, List *pathkeys,
+						List **orderby_clauses_p,
+						List **clause_columns_p);
 static Expr *match_clause_to_ordering_op(IndexOptInfo *index,
 							int indexcol, Expr *clause, Oid pk_opfamily);
 static Relids indexable_outerrelids(PlannerInfo *root, RelOptInfo *rel);
 		IndexOptInfo *index = (IndexOptInfo *) lfirst(ilist);
 		IndexPath  *ipath;
 		List	   *restrictclauses;
+		List	   *restrictclausecols;
 		List	   *orderbyclauses;
+		List	   *orderbyclausecols;
 		List	   *index_pathkeys;
 		List	   *useful_pathkeys;
 		bool		useful_predicate;
 		 * clauses was used (and, if saop_control is SAOP_REQUIRE, it has to
 		 * have been a ScalarArrayOpExpr clause).
 		 */
-		restrictclauses = group_clauses_by_indexkey(index,
-													clauses,
-													outer_clauses,
-													outer_relids,
-													saop_control,
-													&found_clause);
+		match_clauses_to_index(index,
+							   clauses,
+							   outer_clauses,
+							   outer_relids,
+							   saop_control,
+							   &restrictclauses,
+							   &restrictclausecols,
+							   &found_clause);
 
 		/*
 		 * Not all index AMs support scans with no restriction clauses. We
 			useful_pathkeys = truncate_useless_pathkeys(root, rel,
 														index_pathkeys);
 			orderbyclauses = NIL;
+			orderbyclausecols = NIL;
 		}
 		else if (index->amcanorderbyop && possibly_useful_pathkeys &&
 				 istoplevel && outer_rel == NULL && scantype != ST_BITMAPSCAN)
 		{
 			/* see if we can generate ordering operators for query_pathkeys */
-			orderbyclauses = match_index_to_pathkeys(index,
-													 root->query_pathkeys);
+			match_pathkeys_to_index(index, root->query_pathkeys,
+									&orderbyclauses,
+									&orderbyclausecols);
 			if (orderbyclauses)
 				useful_pathkeys = root->query_pathkeys;
 			else
 		{
 			useful_pathkeys = NIL;
 			orderbyclauses = NIL;
+			orderbyclausecols = NIL;
 		}
 
 		/*
 		{
 			ipath = create_index_path(root, index,
 									  restrictclauses,
+									  restrictclausecols,
 									  orderbyclauses,
+									  orderbyclausecols,
 									  useful_pathkeys,
 									  index_is_ordered ?
 									  ForwardScanDirection :
 			{
 				ipath = create_index_path(root, index,
 										  restrictclauses,
+										  restrictclausecols,
+										  NIL,
 										  NIL,
 										  useful_pathkeys,
 										  BackwardScanDirection,
 
 
 /*
- * group_clauses_by_indexkey
+ * match_clauses_to_index
  *	  Find restriction clauses that can be used with an index.
  *
- * Returns a list of sublists of RestrictInfo nodes for clauses that can be
- * used with this index.  Each sublist contains clauses that can be used
- * with one index key (in no particular order); the top list is ordered by
- * index key.  (This is depended on by expand_indexqual_conditions() and
- * fix_indexqual_references().)
+ * Returns a list of RestrictInfo nodes for clauses that can be used with
+ * this index, along with an integer list of the index column numbers
+ * (zero based) that each clause would be used with.  The clauses are
+ * ordered by index key, so that the column numbers form a nondecreasing
+ * sequence.  (This order is depended on by btree and possibly other places.)
+ * NIL lists are returned if there are no matching clauses.
  *
  * We can use clauses from either the current clauses or outer_clauses lists,
  * but *found_clause is set TRUE only if we used at least one clause from
  *
  * If the index has amoptionalkey = false, we give up and return NIL when
  * there are no restriction clauses matching the first index key.  Otherwise,
- * we return NIL if there are no restriction clauses matching any index key.
- * A non-NIL result will have one (possibly empty) sublist for each index key.
- *
- * Example: given an index on (A,B,C), we would return ((C1 C2) () (C3 C4))
- * if we find that clauses C1 and C2 use column A, clauses C3 and C4 use
- * column C, and no clauses use column B.
+ * we return NIL only if there are no restriction clauses matching any index
+ * key.  There could be unused index keys after the first one in any case.
  *
  * Note: in some circumstances we may find the same RestrictInfos coming
- * from multiple places.  Defend against redundant outputs by keeping a side
- * list of already-used clauses (pointer equality should be a good enough
- * check for this).  That also keeps us from matching the same clause to
+ * from multiple places.  Defend against redundant outputs by refusing to
+ * match an already-used clause (pointer equality should be a good enough
+ * check for this).  This also keeps us from matching the same clause to
  * multiple columns of a badly-defined index, which is unlikely to be helpful
  * and is likely to give us an inflated idea of the index's selectivity.
  */
-static List *
-group_clauses_by_indexkey(IndexOptInfo *index,
-						  List *clauses, List *outer_clauses,
-						  Relids outer_relids,
-						  SaOpControl saop_control,
-						  bool *found_clause)
+static void
+match_clauses_to_index(IndexOptInfo *index,
+					   List *clauses, List *outer_clauses,
+					   Relids outer_relids,
+					   SaOpControl saop_control,
+					   List **index_clauses_p,
+					   List **clause_columns_p,
+					   bool *found_clause)
 {
-	List	   *clausegroup_list = NIL;
-	List	   *used_clauses = NIL;
-	bool		found_outer_clause = false;
+	List	   *index_clauses = NIL;
+	List	   *clause_columns = NIL;
 	int			indexcol;
 
-	*found_clause = false;		/* default result */
+	*index_clauses_p = NIL;		/* set default results */
+	*clause_columns_p = NIL;
+	*found_clause = false;
 
 	if (clauses == NIL && outer_clauses == NIL)
-		return NIL;				/* cannot succeed */
+		return;					/* cannot succeed */
 
 	for (indexcol = 0; indexcol < index->ncolumns; indexcol++)
 	{
-		List	   *clausegroup = NIL;
 		ListCell   *l;
 
 		/* check the current clauses */
 			RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
 
 			Assert(IsA(rinfo, RestrictInfo));
-			if (list_member_ptr(used_clauses, rinfo))
+			if (list_member_ptr(index_clauses, rinfo))
 				continue;
 			if (match_clause_to_indexcol(index,
 										 indexcol,
 										 outer_relids,
 										 saop_control))
 			{
-				clausegroup = lappend(clausegroup, rinfo);
-				used_clauses = lappend(used_clauses, rinfo);
+				index_clauses = lappend(index_clauses, rinfo);
+				clause_columns = lappend_int(clause_columns, indexcol);
 				if (saop_control != SAOP_REQUIRE ||
 					IsA(rinfo->clause, ScalarArrayOpExpr))
 					*found_clause = true;
 			RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
 
 			Assert(IsA(rinfo, RestrictInfo));
-			if (list_member_ptr(used_clauses, rinfo))
+			if (list_member_ptr(index_clauses, rinfo))
 				continue;
 			if (match_clause_to_indexcol(index,
 										 indexcol,
 										 outer_relids,
 										 saop_control))
 			{
-				clausegroup = lappend(clausegroup, rinfo);
-				used_clauses = lappend(used_clauses, rinfo);
-				found_outer_clause = true;
+				index_clauses = lappend(index_clauses, rinfo);
+				clause_columns = lappend_int(clause_columns, indexcol);
 			}
 		}
 
 		/*
 		 * If no clauses match this key, check for amoptionalkey restriction.
 		 */
-		if (clausegroup == NIL && !index->amoptionalkey && indexcol == 0)
-			return NIL;
-
-		clausegroup_list = lappend(clausegroup_list, clausegroup);
+		if (index_clauses == NIL && !index->amoptionalkey)
+			return;
 	}
 
-	list_free(used_clauses);
-
-	if (!*found_clause && !found_outer_clause)
-		return NIL;				/* no indexable clauses anywhere */
-
-	return clausegroup_list;
+	*index_clauses_p = index_clauses;
+	*clause_columns_p = clause_columns;
 }
 
 
  ****************************************************************************/
 
 /*
- * match_index_to_pathkeys
+ * match_pathkeys_to_index
  *		Test whether an index can produce output ordered according to the
  *		given pathkeys using "ordering operators".
  *
- * If it can, return a list of lists of lists of ORDER BY expressions,
- * each of the form "indexedcol operator pseudoconstant".  If not, return NIL.
- * (The top list corresponds to pathkeys and the sub-lists to index columns;
- * see comments for indexorderbys in nodes/relation.h.)
+ * If it can, return a list of suitable ORDER BY expressions, each of the form
+ * "indexedcol operator pseudoconstant", along with an integer list of the
+ * index column numbers (zero based) that each clause would be used with.
+ * NIL lists are returned if the ordering is not achievable this way.
+ *
+ * On success, the result list is ordered by pathkeys, and in fact is
+ * one-to-one with the requested pathkeys.
  */
-static List *
-match_index_to_pathkeys(IndexOptInfo *index, List *pathkeys)
+static void
+match_pathkeys_to_index(IndexOptInfo *index, List *pathkeys,
+						List **orderby_clauses_p,
+						List **clause_columns_p)
 {
-	List	   *orderbylists = NIL;
+	List	   *orderby_clauses = NIL;
+	List	   *clause_columns = NIL;
 	ListCell   *lc1;
 
+	*orderby_clauses_p = NIL;		/* set default results */
+	*clause_columns_p = NIL;
+
 	/* Only indexes with the amcanorderbyop property are interesting here */
 	if (!index->amcanorderbyop)
-		return NIL;
+		return;
 
 	foreach(lc1, pathkeys)
 	{
 		/* Pathkey must request default sort order for the target opfamily */
 		if (pathkey->pk_strategy != BTLessStrategyNumber ||
 			pathkey->pk_nulls_first)
-			return NIL;
+			return;
 
 		/* If eclass is volatile, no hope of using an indexscan */
 		if (pathkey->pk_eclass->ec_has_volatile)
-			return NIL;
+			return;
 
 		/* Try to match eclass member expression(s) to index */
 		foreach(lc2, pathkey->pk_eclass->ec_members)
 												   pathkey->pk_opfamily);
 				if (expr)
 				{
-					/*
-					 * Generate list-of-sublists representation to show which
-					 * index column this expression matches.
-					 */
-					List   *sublist = NIL;
-					int		i;
-
-					for (i = 0; i < indexcol; i++)
-						sublist = lappend(sublist, NIL);
-					sublist = lappend(sublist, list_make1(expr));
-					orderbylists = lappend(orderbylists, sublist);
+					orderby_clauses = lappend(orderby_clauses, expr);
+					clause_columns = lappend_int(clause_columns, indexcol);
 					found = true;
 					break;
 				}
 		}
 
 		if (!found)				/* fail if no match for this pathkey */
-			return NIL;
+			return;
 	}
 
-	return orderbylists;		/* success! */
+	*orderby_clauses_p = orderby_clauses;		/* success! */
+	*clause_columns_p = clause_columns;
 }
 
 /*
 
 
 /****************************************************************************
- *				----  PATH CREATION UTILITIES  ----
- ****************************************************************************/
-
-/*
- * flatten_clausegroups_list
- *	  Given a list of lists of RestrictInfos, flatten it to a list
- *	  of RestrictInfos.
- *
- * This is used to flatten out a list of sublists of index clauses (such as
- * the result of group_clauses_by_indexkey()) into a single list, for use
- * where we don't care which clause goes with which index column.  The input
- * list structure mustn't be altered, but it's OK to share copies of the
- * underlying RestrictInfos.
- */
-List *
-flatten_clausegroups_list(List *clausegroups)
-{
-	List	   *allclauses = NIL;
-	ListCell   *l;
-
-	foreach(l, clausegroups)
-		allclauses = list_concat(allclauses, list_copy((List *) lfirst(l)));
-	return allclauses;
-}
-
-/*
- * flatten_indexorderbys_list
- *	  Given a list of lists of lists of ORDER BY expressions, flatten it.
- *
- * This is similar to flatten_clausegroups_list, but is used to flatten the
- * three-list-level result of match_index_to_pathkeys().  We assume the
- * bottom lists each have zero or one member.
- */
-List *
-flatten_indexorderbys_list(List *indexorderbys)
-{
-	List	   *allclauses = NIL;
-	ListCell   *lc1;
-
-	foreach(lc1, indexorderbys)
-	{
-		List	   *sublist = (List *) lfirst(lc1);
-		ListCell   *lc2;
-
-		foreach(lc2, sublist)
-		{
-			List	   *subsublist = (List *) lfirst(lc2);
-
-			if (subsublist == NIL)
-				continue;
-			Assert(list_length(subsublist) == 1);
-			allclauses = lappend(allclauses, (Expr *) linitial(subsublist));
-		}
-	}
-	return allclauses;
-}
-
-
-/****************************************************************************
  *				----  ROUTINES TO CHECK OPERANDS  ----
  ****************************************************************************/
 
  * match_boolean_index_clause() similarly detects clauses that can be
  * converted into boolean equality operators.
  *
- * expand_indexqual_conditions() converts a list of lists of RestrictInfo
- * nodes (with implicit AND semantics across list elements) into a list of
- * lists of clauses that the executor can actually handle.  For operators
- * that are members of the index's opfamily this transformation is a no-op,
- * but clauses recognized by match_special_index_operator() or
- * match_boolean_index_clause() must be converted into one or more "regular"
- * indexqual conditions.
+ * expand_indexqual_conditions() converts a list of RestrictInfo nodes
+ * (with implicit AND semantics across list elements) into a list of clauses
+ * that the executor can actually handle.  For operators that are members of
+ * the index's opfamily this transformation is a no-op, but clauses recognized
+ * by match_special_index_operator() or match_boolean_index_clause() must be
+ * converted into one or more "regular" indexqual conditions.
  *----------
  */
 
 
 /*
  * expand_indexqual_conditions
- *	  Given a list of sublists of RestrictInfo nodes, produce a list of lists
- *	  of index qual clauses.  Standard qual clauses (those in the index's
- *	  opfamily) are passed through unchanged.  Boolean clauses and "special"
- *	  index operators are expanded into clauses that the indexscan machinery
- *	  will know what to do with.  RowCompare clauses are simplified if
- *	  necessary to create a clause that is fully checkable by the index.
- *
- * The input clauses are grouped by index key, and so the output is too.
+ *	  Given a list of RestrictInfo nodes, produce a list of directly usable
+ *	  index qual clauses.
+ *
+ * Standard qual clauses (those in the index's opfamily) are passed through
+ * unchanged.  Boolean clauses and "special" index operators are expanded
+ * into clauses that the indexscan machinery will know what to do with.
+ * RowCompare clauses are simplified if necessary to create a clause that is
+ * fully checkable by the index.
+ *
+ * In addition to the expressions themselves, there are auxiliary lists
+ * of the index column numbers that the clauses are meant to be used with;
+ * we generate an updated column number list for the result.  (This is not
+ * the identical list because one input clause sometimes produces more than
+ * one output clause.)
+ *
+ * The input clauses are sorted by column number, and so the output is too.
  * (This is depended on in various places in both planner and executor.)
  */
-List *
-expand_indexqual_conditions(IndexOptInfo *index, List *clausegroups)
+void
+expand_indexqual_conditions(IndexOptInfo *index,
+							List *indexclauses, List *indexclausecols,
+							List **indexquals_p, List **indexqualcols_p)
 {
-	List	   *resultgroups = NIL;
-	ListCell   *lc;
-	int			indexcol;
-
-	if (clausegroups == NIL)
-		return NIL;
-
-	/* clausegroups must correspond to index columns */
-	Assert(list_length(clausegroups) <= index->ncolumns);
+	List	   *indexquals = NIL;
+	List	   *indexqualcols = NIL;
+	ListCell   *lcc,
+			   *lci;
 
-	indexcol = 0;
-	foreach(lc, clausegroups)
+	forboth(lcc, indexclauses, lci, indexclausecols)
 	{
-		List	   *clausegroup = (List *) lfirst(lc);
+		RestrictInfo *rinfo = (RestrictInfo *) lfirst(lcc);
+		int			indexcol = lfirst_int(lci);
+		Expr	   *clause = rinfo->clause;
 		Oid			curFamily = index->opfamily[indexcol];
 		Oid			curCollation = index->indexcollations[indexcol];
-		List	   *newgroup = NIL;
-		ListCell   *lc2;
 
-		foreach(lc2, clausegroup)
+		/* First check for boolean cases */
+		if (IsBooleanOpfamily(curFamily))
 		{
-			RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc2);
-			Expr	   *clause = rinfo->clause;
+			Expr	   *boolqual;
 
-			/* First check for boolean cases */
-			if (IsBooleanOpfamily(curFamily))
-			{
-				Expr	   *boolqual;
-
-				boolqual = expand_boolean_index_clause((Node *) clause,
-													   indexcol,
-													   index);
-				if (boolqual)
-				{
-					newgroup = lappend(newgroup,
-									   make_simple_restrictinfo(boolqual));
-					continue;
-				}
-			}
-
-			/*
-			 * Else it must be an opclause (usual case), ScalarArrayOp,
-			 * RowCompare, or NullTest
-			 */
-			if (is_opclause(clause))
-			{
-				newgroup = list_concat(newgroup,
-									   expand_indexqual_opclause(rinfo,
-																 curFamily,
-															  curCollation));
-			}
-			else if (IsA(clause, ScalarArrayOpExpr))
-			{
-				/* no extra work at this time */
-				newgroup = lappend(newgroup, rinfo);
-			}
-			else if (IsA(clause, RowCompareExpr))
-			{
-				newgroup = lappend(newgroup,
-								   expand_indexqual_rowcompare(rinfo,
-															   index,
-															   indexcol));
-			}
-			else if (IsA(clause, NullTest))
+			boolqual = expand_boolean_index_clause((Node *) clause,
+												   indexcol,
+												   index);
+			if (boolqual)
 			{
-				Assert(index->amsearchnulls);
-				newgroup = lappend(newgroup,
-								   make_simple_restrictinfo(clause));
+				indexquals = lappend(indexquals,
+									 make_simple_restrictinfo(boolqual));
+				indexqualcols = lappend_int(indexqualcols, indexcol);
+				continue;
 			}
-			else
-				elog(ERROR, "unsupported indexqual type: %d",
-					 (int) nodeTag(clause));
 		}
 
-		resultgroups = lappend(resultgroups, newgroup);
-
-		indexcol++;
+		/*
+		 * Else it must be an opclause (usual case), ScalarArrayOp,
+		 * RowCompare, or NullTest
+		 */
+		if (is_opclause(clause))
+		{
+			indexquals = list_concat(indexquals,
+									 expand_indexqual_opclause(rinfo,
+															   curFamily,
+															   curCollation));
+			/* expand_indexqual_opclause can produce multiple clauses */
+			while (list_length(indexqualcols) < list_length(indexquals))
+				indexqualcols = lappend_int(indexqualcols, indexcol);
+		}
+		else if (IsA(clause, ScalarArrayOpExpr))
+		{
+			/* no extra work at this time */
+			indexquals = lappend(indexquals, rinfo);
+			indexqualcols = lappend_int(indexqualcols, indexcol);
+		}
+		else if (IsA(clause, RowCompareExpr))
+		{
+			indexquals = lappend(indexquals,
+								 expand_indexqual_rowcompare(rinfo,
+															 index,
+															 indexcol));
+			indexqualcols = lappend_int(indexqualcols, indexcol);
+		}
+		else if (IsA(clause, NullTest))
+		{
+			Assert(index->amsearchnulls);
+			indexquals = lappend(indexquals, rinfo);
+			indexqualcols = lappend_int(indexqualcols, indexcol);
+		}
+		else
+			elog(ERROR, "unsupported indexqual type: %d",
+				 (int) nodeTag(clause));
 	}
 
-	return resultgroups;
+	*indexquals_p = indexquals;
+	*indexqualcols_p = indexqualcols;
 }
 
 /*

File src/backend/optimizer/plan/createplan.c

 					 Plan *outer_plan, Plan *inner_plan);
 static Node *replace_nestloop_params(PlannerInfo *root, Node *expr);
 static Node *replace_nestloop_params_mutator(Node *node, PlannerInfo *root);
-static List *fix_indexqual_references(PlannerInfo *root, IndexPath *index_path,
-						 List *indexquals);
-static List *fix_indexorderby_references(PlannerInfo *root, IndexPath *index_path,
-							List *indexorderbys);
+static List *fix_indexqual_references(PlannerInfo *root, IndexPath *index_path);
+static List *fix_indexorderby_references(PlannerInfo *root, IndexPath *index_path);
 static Node *fix_indexqual_operand(Node *node, IndexOptInfo *index, int indexcol);
 static List *get_switched_clauses(List *clauses, Relids outerrelids);
 static List *order_qual_clauses(PlannerInfo *root, List *clauses);
 					  bool indexonly)
 {
 	Scan	   *scan_plan;
+	List	   *indexquals = best_path->indexquals;
 	List	   *indexorderbys = best_path->indexorderbys;
 	Index		baserelid = best_path->path.parent->relid;
 	Oid			indexoid = best_path->indexinfo->indexoid;
 	List	   *qpqual;
-	List	   *indexquals;
 	List	   *stripped_indexquals;
 	List	   *fixed_indexquals;
 	List	   *fixed_indexorderbys;
 	Assert(best_path->path.parent->rtekind == RTE_RELATION);
 
 	/*
-	 * We need to flatten the indexquals list-of-sublists, since most of the
-	 * processing below doesn't care which index column each qual is
-	 * associated with.
-	 */
-	indexquals = flatten_clausegroups_list(best_path->indexquals);
-
-	/*
 	 * Build "stripped" indexquals structure (no RestrictInfos) to pass to
 	 * executor as indexqualorig
 	 */
 
 	/*
 	 * The executor needs a copy with the indexkey on the left of each clause
-	 * and with index Vars substituted for table ones.  Here we use the
-	 * unflattened list so we can conveniently tell which index column each
-	 * clause is for.
+	 * and with index Vars substituted for table ones.
 	 */
-	fixed_indexquals = fix_indexqual_references(root, best_path,
-												best_path->indexquals);
+	fixed_indexquals = fix_indexqual_references(root, best_path);
 
 	/*
 	 * Likewise fix up index attr references in the ORDER BY expressions.
 	 */
-	fixed_indexorderbys = fix_indexorderby_references(root, best_path,
-													  indexorderbys);
-
-	/*
-	 * Also produce a flat list to become the indexorderbyorig.
-	 */
-	indexorderbys = flatten_indexorderbys_list(indexorderbys);
+	fixed_indexorderbys = fix_indexorderby_references(root, best_path);
 
 	/*
 	 * If this is an innerjoin scan, the indexclauses will contain join
 			clamp_row_est(ipath->indexselectivity * ipath->path.parent->tuples);
 		plan->plan_width = 0;	/* meaningless */
 		*qual = get_actual_clauses(ipath->indexclauses);
-		*indexqual = get_actual_clauses(flatten_clausegroups_list(ipath->indexquals));
+		*indexqual = get_actual_clauses(ipath->indexquals);
 		foreach(l, ipath->indexinfo->indpred)
 		{
 			Expr	   *pred = (Expr *) lfirst(l);
  *	  Adjust indexqual clauses to the form the executor's indexqual
  *	  machinery needs.
  *
- * We have five tasks here:
- *	* Flatten the list-of-sublists structure of indexquals into a simple list.
+ * We have four tasks here:
  *	* Remove RestrictInfo nodes from the input clauses.
  *	* Replace any outer-relation Var or PHV nodes with nestloop Params.
  *	  (XXX eventually, that responsibility should go elsewhere?)
  *	* If the index key is on the right, commute the clause to put it on the
  *	  left.
  *
- * The result is a modified copy of the indexquals list --- the
+ * The result is a modified copy of the path's indexquals list --- the
  * original is not changed.  Note also that the copy shares no substructure
  * with the original; this is needed in case there is a subplan in it (we need
  * two separate copies of the subplan tree, or things will go awry).
  */
 static List *
-fix_indexqual_references(PlannerInfo *root, IndexPath *index_path,
-						 List *indexquals)
+fix_indexqual_references(PlannerInfo *root, IndexPath *index_path)
 {
 	IndexOptInfo *index = index_path->indexinfo;
 	List	   *fixed_indexquals;
-	ListCell   *lc1;
-	int			indexcol;
+	ListCell   *lcc,
+			   *lci;
 
 	fixed_indexquals = NIL;
 
-	/* clausegroups must correspond to index columns */
-	Assert(list_length(indexquals) <= index->ncolumns);
-
-	indexcol = 0;
-	foreach(lc1, indexquals)
+	forboth(lcc, index_path->indexquals, lci, index_path->indexqualcols)
 	{
-		List	   *clausegroup = (List *) lfirst(lc1);
-		ListCell   *lc2;
+		RestrictInfo *rinfo = (RestrictInfo *) lfirst(lcc);
+		int			indexcol = lfirst_int(lci);
+		Node	   *clause;
+
+		Assert(IsA(rinfo, RestrictInfo));
+
+		/*
+		 * Replace any outer-relation variables with nestloop params.
+		 *
+		 * This also makes a copy of the clause, so it's safe to modify it
+		 * in-place below.
+		 */
+		clause = replace_nestloop_params(root, (Node *) rinfo->clause);
 
-		foreach(lc2, clausegroup)
+		if (IsA(clause, OpExpr))
 		{
-			RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc2);
-			Node	   *clause;
+			OpExpr	   *op = (OpExpr *) clause;
 
-			Assert(IsA(rinfo, RestrictInfo));
+			if (list_length(op->args) != 2)
+				elog(ERROR, "indexqual clause is not binary opclause");
 
 			/*
-			 * Replace any outer-relation variables with nestloop params.
-			 *
-			 * This also makes a copy of the clause, so it's safe to modify it
-			 * in-place below.
+			 * Check to see if the indexkey is on the right; if so, commute
+			 * the clause.  The indexkey should be the side that refers to
+			 * (only) the base relation.
 			 */
-			clause = replace_nestloop_params(root, (Node *) rinfo->clause);
+			if (!bms_equal(rinfo->left_relids, index->rel->relids))
+				CommuteOpExpr(op);
 
-			if (IsA(clause, OpExpr))
-			{
-				OpExpr	   *op = (OpExpr *) clause;
+			/*
+			 * Now replace the indexkey expression with an index Var.
+			 */
+			linitial(op->args) = fix_indexqual_operand(linitial(op->args),
+													   index,
+													   indexcol);
+		}
+		else if (IsA(clause, RowCompareExpr))
+		{
+			RowCompareExpr *rc = (RowCompareExpr *) clause;
+			Expr	   *newrc;
+			List	   *indexcolnos;
+			bool		var_on_left;
+			ListCell   *lca,
+					   *lcai;
 
-				if (list_length(op->args) != 2)
-					elog(ERROR, "indexqual clause is not binary opclause");
+			/*
+			 * Re-discover which index columns are used in the rowcompare.
+			 */
+			newrc = adjust_rowcompare_for_index(rc,
+												index,
+												indexcol,
+												&indexcolnos,
+												&var_on_left);
 
-				/*
-				 * Check to see if the indexkey is on the right; if so,
-				 * commute the clause. The indexkey should be the side that
-				 * refers to (only) the base relation.
-				 */
-				if (!bms_equal(rinfo->left_relids, index->rel->relids))
-					CommuteOpExpr(op);
+			/*
+			 * Trouble if adjust_rowcompare_for_index thought the
+			 * RowCompareExpr didn't match the index as-is; the clause should
+			 * have gone through that routine already.
+			 */
+			if (newrc != (Expr *) rc)
+				elog(ERROR, "inconsistent results from adjust_rowcompare_for_index");
 
-				/*
-				 * Now replace the indexkey expression with an index Var.
-				 */
-				linitial(op->args) = fix_indexqual_operand(linitial(op->args),
-														   index,
-														   indexcol);
-			}
-			else if (IsA(clause, RowCompareExpr))
-			{
-				RowCompareExpr *rc = (RowCompareExpr *) clause;
-				Expr	   *newrc;
-				List	   *indexcolnos;
-				bool		var_on_left;
-				ListCell   *lca,
-						   *lci;
+			/*
+			 * Check to see if the indexkey is on the right; if so, commute
+			 * the clause.
+			 */
+			if (!var_on_left)
+				CommuteRowCompareExpr(rc);
 
-				/*
-				 * Re-discover which index columns are used in the rowcompare.
-				 */
-				newrc = adjust_rowcompare_for_index(rc,
+			/*
+			 * Now replace the indexkey expressions with index Vars.
+			 */
+			Assert(list_length(rc->largs) == list_length(indexcolnos));
+			forboth(lca, rc->largs, lcai, indexcolnos)
+			{
+				lfirst(lca) = fix_indexqual_operand(lfirst(lca),
 													index,
-													indexcol,
-													&indexcolnos,
-													&var_on_left);
-
-				/*
-				 * Trouble if adjust_rowcompare_for_index thought the
-				 * RowCompareExpr didn't match the index as-is; the clause
-				 * should have gone through that routine already.
-				 */
-				if (newrc != (Expr *) rc)
-					elog(ERROR, "inconsistent results from adjust_rowcompare_for_index");
-
-				/*
-				 * Check to see if the indexkey is on the right; if so,
-				 * commute the clause.
-				 */
-				if (!var_on_left)
-					CommuteRowCompareExpr(rc);
-
-				/*
-				 * Now replace the indexkey expressions with index Vars.
-				 */
-				Assert(list_length(rc->largs) == list_length(indexcolnos));
-				forboth(lca, rc->largs, lci, indexcolnos)
-				{
-					lfirst(lca) = fix_indexqual_operand(lfirst(lca),
-														index,
-														lfirst_int(lci));
-				}
+													lfirst_int(lcai));
 			}
-			else if (IsA(clause, ScalarArrayOpExpr))
-			{
-				ScalarArrayOpExpr *saop = (ScalarArrayOpExpr *) clause;
-
-				/* Never need to commute... */
+		}
+		else if (IsA(clause, ScalarArrayOpExpr))
+		{
+			ScalarArrayOpExpr *saop = (ScalarArrayOpExpr *) clause;
 
-				/* Replace the indexkey expression with an index Var. */
-				linitial(saop->args) = fix_indexqual_operand(linitial(saop->args),
-															 index,
-															 indexcol);
-			}
-			else if (IsA(clause, NullTest))
-			{
-				NullTest   *nt = (NullTest *) clause;
+			/* Never need to commute... */
 
-				/* Replace the indexkey expression with an index Var. */
-				nt->arg = (Expr *) fix_indexqual_operand((Node *) nt->arg,
+			/* Replace the indexkey expression with an index Var. */
+			linitial(saop->args) = fix_indexqual_operand(linitial(saop->args),
 														 index,
 														 indexcol);
-			}
-			else
-				elog(ERROR, "unsupported indexqual type: %d",
-					 (int) nodeTag(clause));
+		}
+		else if (IsA(clause, NullTest))
+		{
+			NullTest   *nt = (NullTest *) clause;
 
-			fixed_indexquals = lappend(fixed_indexquals, clause);
+			/* Replace the indexkey expression with an index Var. */
+			nt->arg = (Expr *) fix_indexqual_operand((Node *) nt->arg,
+													 index,
+													 indexcol);
 		}
+		else
+			elog(ERROR, "unsupported indexqual type: %d",
+				 (int) nodeTag(clause));
 
-		indexcol++;
+		fixed_indexquals = lappend(fixed_indexquals, clause);
 	}
 
 	return fixed_indexquals;
  * is allowed for ordering operators.
  */
 static List *
-fix_indexorderby_references(PlannerInfo *root, IndexPath *index_path,
-							List *indexorderbys)
+fix_indexorderby_references(PlannerInfo *root, IndexPath *index_path)
 {
 	IndexOptInfo *index = index_path->indexinfo;
 	List	   *fixed_indexorderbys;
-	ListCell   *lc1;
+	ListCell   *lcc,
+			   *lci;
 
 	fixed_indexorderbys = NIL;
 
-	foreach(lc1, indexorderbys)
+	forboth(lcc, index_path->indexorderbys, lci, index_path->indexorderbycols)
 	{
-		List	   *percollists = (List *) lfirst(lc1);
-		ListCell   *lc2;
-		int			indexcol;
+		Node	   *clause = (Node *) lfirst(lcc);
+		int			indexcol = lfirst_int(lci);
 
-		/* percollists must correspond to index columns */
-		Assert(list_length(percollists) <= index->ncolumns);
+		/*
+		 * Replace any outer-relation variables with nestloop params.
+		 *
+		 * This also makes a copy of the clause, so it's safe to modify it
+		 * in-place below.
+		 */
+		clause = replace_nestloop_params(root, clause);
 
-		indexcol = 0;
-		foreach(lc2, percollists)
+		if (IsA(clause, OpExpr))
 		{
-			List	   *percollist = (List *) lfirst(lc2);
-
-			if (percollist != NIL)
-			{
-				Node	   *clause = (Node *) linitial(percollist);
-
-				/* Should have only one clause per index column */
-				Assert(list_length(percollist) == 1);
-
-				/*
-				 * Replace any outer-relation variables with nestloop params.
-				 *
-				 * This also makes a copy of the clause, so it's safe to
-				 * modify it in-place below.
-				 */
-				clause = replace_nestloop_params(root, clause);
-
-				if (IsA(clause, OpExpr))
-				{
-					OpExpr	   *op = (OpExpr *) clause;
-
-					if (list_length(op->args) != 2)
-						elog(ERROR, "indexorderby clause is not binary opclause");
-
-					/*
-					 * Now replace the indexkey expression with an index Var.
-					 */
-					linitial(op->args) = fix_indexqual_operand(linitial(op->args),
-															   index,
-															   indexcol);
-				}
-				else
-					elog(ERROR, "unsupported indexorderby type: %d",
-						 (int) nodeTag(clause));
+			OpExpr	   *op = (OpExpr *) clause;
 
-				fixed_indexorderbys = lappend(fixed_indexorderbys, clause);
-			}
+			if (list_length(op->args) != 2)
+				elog(ERROR, "indexorderby clause is not binary opclause");
 
-			indexcol++;
+			/*
+			 * Now replace the indexkey expression with an index Var.
+			 */
+			linitial(op->args) = fix_indexqual_operand(linitial(op->args),
+													   index,
+													   indexcol);
 		}
+		else
+			elog(ERROR, "unsupported indexorderby type: %d",
+				 (int) nodeTag(clause));
+
+		fixed_indexorderbys = lappend(fixed_indexorderbys, clause);
 	}
 
 	return fixed_indexorderbys;

File src/backend/optimizer/plan/planner.c

 
 	/* Estimate the cost of index scan */
 	indexScanPath = create_index_path(root, indexInfo,
-									  NIL, NIL, NIL,
+									  NIL, NIL, NIL, NIL, NIL,
 									  ForwardScanDirection, false, NULL);
 
 	return (seqScanAndSortPath.total_cost < indexScanPath->path.total_cost);

File src/backend/optimizer/util/pathnode.c

  *	  Creates a path node for an index scan.
  *
  * 'index' is a usable index.
- * 'clause_groups' is a list of lists of RestrictInfo nodes
+ * 'indexclauses' is a list of RestrictInfo nodes representing clauses
  *			to be used as index qual conditions in the scan.
- * 'indexorderbys' is a list of lists of lists of bare expressions (not
- *			RestrictInfos) to be used as index ordering operators.
+ * 'indexclausecols' is an integer list of index column numbers (zero based)
+ *			the indexclauses can be used with.
+ * 'indexorderbys' is a list of bare expressions (no RestrictInfos)
+ *			to be used as index ordering operators in the scan.
+ * 'indexorderbycols' is an integer list of index column numbers (zero based)
+ *			the ordering operators can be used with.
  * 'pathkeys' describes the ordering of the path.
  * 'indexscandir' is ForwardScanDirection or BackwardScanDirection
  *			for an ordered index, or NoMovementScanDirection for
 IndexPath *
 create_index_path(PlannerInfo *root,
 				  IndexOptInfo *index,
-				  List *clause_groups,
+				  List *indexclauses,
+				  List *indexclausecols,
 				  List *indexorderbys,
+				  List *indexorderbycols,
 				  List *pathkeys,
 				  ScanDirection indexscandir,
 				  bool indexonly,
 	IndexPath  *pathnode = makeNode(IndexPath);
 	RelOptInfo *rel = index->rel;
 	List	   *indexquals,
-			   *allclauses;
+			   *indexqualcols;
 
 	/*
 	 * For a join inner scan, there's no point in marking the path with any
 	pathnode->path.pathkeys = pathkeys;
 
 	/* Convert clauses to indexquals the executor can handle */
-	indexquals = expand_indexqual_conditions(index, clause_groups);
-
-	/* Flatten the clause-groups list to produce indexclauses list */
-	allclauses = flatten_clausegroups_list(clause_groups);
+	expand_indexqual_conditions(index, indexclauses, indexclausecols,
+								&indexquals, &indexqualcols);
 
 	/* Fill in the pathnode */
 	pathnode->indexinfo = index;
-	pathnode->indexclauses = allclauses;
+	pathnode->indexclauses = indexclauses;
 	pathnode->indexquals = indexquals;
+	pathnode->indexqualcols = indexqualcols;
 	pathnode->indexorderbys = indexorderbys;
+	pathnode->indexorderbycols = indexorderbycols;
 
 	pathnode->isjoininner = (outer_rel != NULL);
 	pathnode->indexscandir = indexscandir;
 		/*
 		 * We must compute the estimated number of output rows for the
 		 * indexscan.  This is less than rel->rows because of the additional
-		 * selectivity of the join clauses.  Since clause_groups may contain
+		 * selectivity of the join clauses.  Since indexclauses may contain
 		 * both restriction and join clauses, we have to do a set union to get
 		 * the full set of clauses that must be considered to compute the
 		 * correct selectivity.  (Without the union operation, we might have
 		 * Note that we force the clauses to be treated as non-join clauses
 		 * during selectivity estimation.
 		 */
-		allclauses = list_union_ptr(rel->baserestrictinfo, allclauses);
+		List	   *allclauses;
+
+		allclauses = list_union_ptr(rel->baserestrictinfo, indexclauses);
 		pathnode->rows = rel->tuples *
 			clauselist_selectivity(root,
 								   allclauses,
 		pathnode->rows = rel->rows;
 	}
 
-	cost_index(pathnode, root, index, indexquals, indexorderbys,
-			   indexonly, outer_rel);
+	cost_index(pathnode, root, outer_rel);
 
 	return pathnode;
 }

File src/backend/utils/adt/selfuncs.c

 
 static void
 genericcostestimate(PlannerInfo *root,
-					IndexOptInfo *index,
-					List *indexQuals,
-					List *indexOrderBys,
+					IndexPath *path,
 					RelOptInfo *outer_rel,
 					double numIndexTuples,
 					Cost *indexStartupCost,
 					Selectivity *indexSelectivity,
 					double *indexCorrelation)
 {
+	IndexOptInfo *index = path->indexinfo;
+	List	   *indexQuals = path->indexquals;
+	List	   *indexOrderBys = path->indexorderbys;
 	double		numIndexPages;
 	double		num_sa_scans;
 	double		num_outer_scans;
 	List	   *selectivityQuals;
 	ListCell   *l;
 
-	/*
-	 * For our purposes here, it doesn't matter which index columns the
-	 * individual quals and order-by expressions go with, so flatten the
-	 * lists for convenience.
-	 */
-	indexQuals = flatten_clausegroups_list(indexQuals);
-	indexOrderBys = flatten_indexorderbys_list(indexOrderBys);
-
 	/*----------
 	 * If the index is partial, AND the index predicate with the explicitly
 	 * given indexquals to produce a more accurate idea of the index
 			if (!predicate_implied_by(oneQual, indexQuals))
 				predExtraQuals = list_concat(predExtraQuals, oneQual);
 		}
-		/* list_concat avoids modifying the indexQuals list */
+		/* list_concat avoids modifying the passed-in indexQuals list */
 		selectivityQuals = list_concat(predExtraQuals, indexQuals);
 	}
 	else
 btcostestimate(PG_FUNCTION_ARGS)
 {
 	PlannerInfo *root = (PlannerInfo *) PG_GETARG_POINTER(0);
-	IndexOptInfo *index = (IndexOptInfo *) PG_GETARG_POINTER(1);
-	List	   *indexQuals = (List *) PG_GETARG_POINTER(2);
-	List	   *indexOrderBys = (List *) PG_GETARG_POINTER(3);
-	RelOptInfo *outer_rel = (RelOptInfo *) PG_GETARG_POINTER(4);
-	Cost	   *indexStartupCost = (Cost *) PG_GETARG_POINTER(5);
-	Cost	   *indexTotalCost = (Cost *) PG_GETARG_POINTER(6);
-	Selectivity *indexSelectivity = (Selectivity *) PG_GETARG_POINTER(7);
-	double	   *indexCorrelation = (double *) PG_GETARG_POINTER(8);
+	IndexPath  *path = (IndexPath *) PG_GETARG_POINTER(1);
+	RelOptInfo *outer_rel = (RelOptInfo *) PG_GETARG_POINTER(2);
+	Cost	   *indexStartupCost = (Cost *) PG_GETARG_POINTER(3);
+	Cost	   *indexTotalCost = (Cost *) PG_GETARG_POINTER(4);
+	Selectivity *indexSelectivity = (Selectivity *) PG_GETARG_POINTER(5);
+	double	   *indexCorrelation = (double *) PG_GETARG_POINTER(6);
+	IndexOptInfo *index = path->indexinfo;
 	Oid			relid;
 	AttrNumber	colnum;
 	VariableStatData vardata;
 	bool		found_saop;
 	bool		found_is_null_op;
 	double		num_sa_scans;
-	ListCell   *lc1;
+	ListCell   *lcc,
+			   *lci;
 
 	/*
 	 * For a btree scan, only leading '=' quals plus inequality quals for the
 	 * the "boundary quals" that determine the starting and stopping points of
 	 * the index scan).  Additional quals can suppress visits to the heap, so
 	 * it's OK to count them in indexSelectivity, but they should not count
-	 * for estimating numIndexTuples.  So we must examine the given indexQuals
-	 * to find out which ones count as boundary quals.
+	 * for estimating numIndexTuples.  So we must examine the given indexquals
+	 * to find out which ones count as boundary quals.  We rely on the
+	 * knowledge that they are given in index column order.
 	 *
 	 * For a RowCompareExpr, we consider only the first column, just as
 	 * rowcomparesel() does.
 	 * considered to act the same as it normally does.
 	 */
 	indexBoundQuals = NIL;
+	indexcol = 0;
 	eqQualHere = false;
 	found_saop = false;
 	found_is_null_op = false;
 	num_sa_scans = 1;
-
-	/* clausegroups must correspond to index columns */
-	Assert(list_length(indexQuals) <= index->ncolumns);
-
-	indexcol = 0;
-	foreach(lc1, indexQuals)
+	forboth(lcc, path->indexquals, lci, path->indexqualcols)
 	{
-		List	   *clausegroup = (List *) lfirst(lc1);
-		ListCell   *lc2;
+		RestrictInfo *rinfo = (RestrictInfo *) lfirst(lcc);
+		Expr	   *clause;
+		Node	   *leftop,
+				   *rightop;
+		Oid			clause_op;
+		int			op_strategy;
+		bool		is_null_op = false;
 
-		eqQualHere = false;
+		if (indexcol != lfirst_int(lci))
+		{
+			/* Beginning of a new column's quals */
+			if (!eqQualHere)
+				break;			/* done if no '=' qual for indexcol */
+			eqQualHere = false;
+			indexcol++;
+			if (indexcol != lfirst_int(lci))
+				break;			/* no quals at all for indexcol */
+		}
+
+		Assert(IsA(rinfo, RestrictInfo));
+		clause = rinfo->clause;
 
-		foreach(lc2, clausegroup)
+		if (IsA(clause, OpExpr))
 		{
-			RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc2);
-			Expr	   *clause;
-			Node	   *leftop,
-					   *rightop;
-			Oid			clause_op;
-			int			op_strategy;
-			bool		is_null_op = false;
-
-			Assert(IsA(rinfo, RestrictInfo));
-			clause = rinfo->clause;
-			if (IsA(clause, OpExpr))
-			{
-				leftop = get_leftop(clause);
-				rightop = get_rightop(clause);
-				clause_op = ((OpExpr *) clause)->opno;
-			}
-			else if (IsA(clause, RowCompareExpr))
-			{
-				RowCompareExpr *rc = (RowCompareExpr *) clause;
+			leftop = get_leftop(clause);
+			rightop = get_rightop(clause);
+			clause_op = ((OpExpr *) clause)->opno;
+		}
+		else if (IsA(clause, RowCompareExpr))
+		{
+			RowCompareExpr *rc = (RowCompareExpr *) clause;
 
-				leftop = (Node *) linitial(rc->largs);
-				rightop = (Node *) linitial(rc->rargs);
-				clause_op = linitial_oid(rc->opnos);
-			}
-			else if (IsA(clause, ScalarArrayOpExpr))
-			{
-				ScalarArrayOpExpr *saop = (ScalarArrayOpExpr *) clause;
+			leftop = (Node *) linitial(rc->largs);
+			rightop = (Node *) linitial(rc->rargs);
+			clause_op = linitial_oid(rc->opnos);
+		}
+		else if (IsA(clause, ScalarArrayOpExpr))
+		{
+			ScalarArrayOpExpr *saop = (ScalarArrayOpExpr *) clause;
 
-				leftop = (Node *) linitial(saop->args);
-				rightop = (Node *) lsecond(saop->args);
-				clause_op = saop->opno;
-				found_saop = true;
-			}
-			else if (IsA(clause, NullTest))
-			{
-				NullTest   *nt = (NullTest *) clause;
+			leftop = (Node *) linitial(saop->args);
+			rightop = (Node *) lsecond(saop->args);
+			clause_op = saop->opno;
+			found_saop = true;
+		}
+		else if (IsA(clause, NullTest))
+		{
+			NullTest   *nt = (NullTest *) clause;
 
-				leftop = (Node *) nt->arg;
-				rightop = NULL;
-				clause_op = InvalidOid;
-				if (nt->nulltesttype == IS_NULL)
-				{
-					found_is_null_op = true;
-					is_null_op = true;
-				}
-			}
-			else
+			leftop = (Node *) nt->arg;
+			rightop = NULL;
+			clause_op = InvalidOid;
+			if (nt->nulltesttype == IS_NULL)
 			{
-				elog(ERROR, "unsupported indexqual type: %d",
-					 (int) nodeTag(clause));
-				continue;			/* keep compiler quiet */
+				found_is_null_op = true;
+				is_null_op = true;
 			}
+		}
+		else
+		{
+			elog(ERROR, "unsupported indexqual type: %d",
+				 (int) nodeTag(clause));
+			continue;			/* keep compiler quiet */
+		}
 
-			if (match_index_to_operand(leftop, indexcol, index))
-			{
-				/* clause_op is correct */
-			}
-			else
-			{
-				Assert(match_index_to_operand(rightop, indexcol, index));
-				/* Must flip operator to get the opfamily member */
-				clause_op = get_commutator(clause_op);
-			}
+		if (match_index_to_operand(leftop, indexcol, index))
+		{
+			/* clause_op is correct */
+		}
+		else
+		{
+			Assert(match_index_to_operand(rightop, indexcol, index));
+			/* Must flip operator to get the opfamily member */
+			clause_op = get_commutator(clause_op);
+		}
 
-			/* check for equality operator */
-			if (OidIsValid(clause_op))
-			{
-				op_strategy = get_op_opfamily_strategy(clause_op,
+		/* check for equality operator */
+		if (OidIsValid(clause_op))
+		{
+			op_strategy = get_op_opfamily_strategy(clause_op,
 												   index->opfamily[indexcol]);
-				Assert(op_strategy != 0);	/* not a member of opfamily?? */
-				if (op_strategy == BTEqualStrategyNumber)
-					eqQualHere = true;
-			}
-			else if (is_null_op)
-			{
-				/* IS NULL is like = for selectivity determination */
+			Assert(op_strategy != 0);	/* not a member of opfamily?? */
+			if (op_strategy == BTEqualStrategyNumber)
 				eqQualHere = true;
-			}
-			/* count up number of SA scans induced by indexBoundQuals only */
-			if (IsA(clause, ScalarArrayOpExpr))
-			{
-				ScalarArrayOpExpr *saop = (ScalarArrayOpExpr *) clause;
-				int		alength = estimate_array_length(lsecond(saop->args));
-
-				if (alength > 1)
-					num_sa_scans *= alength;
-			}
-			indexBoundQuals = lappend(indexBoundQuals, rinfo);
 		}
+		else if (is_null_op)
+		{
+			/* IS NULL is like = for purposes of selectivity determination */
+			eqQualHere = true;
+		}
+		/* count up number of SA scans induced by indexBoundQuals only */
+		if (IsA(clause, ScalarArrayOpExpr))
+		{
+			ScalarArrayOpExpr *saop = (ScalarArrayOpExpr *) clause;
+			int			alength = estimate_array_length(lsecond(saop->args));
 
-		/* Done with this indexcol, continue to next only if it had = qual */
-		if (!eqQualHere)
-			break;
-
-		indexcol++;
+			if (alength > 1)
+				num_sa_scans *= alength;
+		}
+		indexBoundQuals = lappend(indexBoundQuals, rinfo);
 	}
 
 	/*
 	 * NullTest invalidates that theory, even though it sets eqQualHere.
 	 */
 	if (index->unique &&
-		indexcol == index->ncolumns &&
+		indexcol == index->ncolumns - 1 &&
 		eqQualHere &&
 		!found_saop &&
 		!found_is_null_op)
 		numIndexTuples = rint(numIndexTuples / num_sa_scans);
 	}
 
-	genericcostestimate(root, index, indexQuals, indexOrderBys,
-						outer_rel, numIndexTuples,
+	genericcostestimate(root, path, outer_rel,
+						numIndexTuples,
 						indexStartupCost, indexTotalCost,
 						indexSelectivity, indexCorrelation);
 
 hashcostestimate(PG_FUNCTION_ARGS)
 {
 	PlannerInfo *root = (PlannerInfo *) PG_GETARG_POINTER(0);
-	IndexOptInfo *index = (IndexOptInfo *) PG_GETARG_POINTER(1);
-	List	   *indexQuals = (List *) PG_GETARG_POINTER(2);
-	List	   *indexOrderBys = (List *) PG_GETARG_POINTER(3);
-	RelOptInfo *outer_rel = (RelOptInfo *) PG_GETARG_POINTER(4);
-	Cost	   *indexStartupCost = (Cost *) PG_GETARG_POINTER(5);
-	Cost	   *indexTotalCost = (Cost *) PG_GETARG_POINTER(6);
-	Selectivity *indexSelectivity = (Selectivity *) PG_GETARG_POINTER(7);
-	double	   *indexCorrelation = (double *) PG_GETARG_POINTER(8);
-
-	genericcostestimate(root, index, indexQuals, indexOrderBys, outer_rel, 0.0,
+	IndexPath  *path = (IndexPath *) PG_GETARG_POINTER(1);
+	RelOptInfo *outer_rel = (RelOptInfo *) PG_GETARG_POINTER(2);
+	Cost	   *indexStartupCost = (Cost *) PG_GETARG_POINTER(3);
+	Cost	   *indexTotalCost = (Cost *) PG_GETARG_POINTER(4);
+	Selectivity *indexSelectivity = (Selectivity *) PG_GETARG_POINTER(5);
+	double	   *indexCorrelation = (double *) PG_GETARG_POINTER(6);
+
+	genericcostestimate(root, path, outer_rel, 0.0,
 						indexStartupCost, indexTotalCost,
 						indexSelectivity, indexCorrelation);
 
 gistcostestimate(PG_FUNCTION_ARGS)
 {
 	PlannerInfo *root = (PlannerInfo *) PG_GETARG_POINTER(0);
-	IndexOptInfo *index = (IndexOptInfo *) PG_GETARG_POINTER(1);
-	List	   *indexQuals = (List *) PG_GETARG_POINTER(2);
-	List	   *indexOrderBys = (List *) PG_GETARG_POINTER(3);
-	RelOptInfo *outer_rel = (RelOptInfo *) PG_GETARG_POINTER(4);
-	Cost	   *indexStartupCost = (Cost *) PG_GETARG_POINTER(5);
-	Cost	   *indexTotalCost = (Cost *) PG_GETARG_POINTER(6);
-	Selectivity *indexSelectivity = (Selectivity *) PG_GETARG_POINTER(7);
-	double	   *indexCorrelation = (double *) PG_GETARG_POINTER(8);
-
-	genericcostestimate(root, index, indexQuals, indexOrderBys, outer_rel, 0.0,
+	IndexPath  *path = (IndexPath *) PG_GETARG_POINTER(1);
+	RelOptInfo *outer_rel = (RelOptInfo *) PG_GETARG_POINTER(2);
+	Cost	   *indexStartupCost = (Cost *) PG_GETARG_POINTER(3);
+	Cost	   *indexTotalCost = (Cost *) PG_GETARG_POINTER(4);
+	Selectivity *indexSelectivity = (Selectivity *) PG_GETARG_POINTER(5);
+	double	   *indexCorrelation = (double *) PG_GETARG_POINTER(6);
+
+	genericcostestimate(root, path, outer_rel, 0.0,
 						indexStartupCost, indexTotalCost,
 						indexSelectivity, indexCorrelation);
 
 spgcostestimate(PG_FUNCTION_ARGS)
 {
 	PlannerInfo *root = (PlannerInfo *) PG_GETARG_POINTER(0);
-	IndexOptInfo *index = (IndexOptInfo *) PG_GETARG_POINTER(1);
-	List	   *indexQuals = (List *) PG_GETARG_POINTER(2);
-	List	   *indexOrderBys = (List *) PG_GETARG_POINTER(3);
-	RelOptInfo *outer_rel = (RelOptInfo *) PG_GETARG_POINTER(4);
-	Cost	   *indexStartupCost = (Cost *) PG_GETARG_POINTER(5);
-	Cost	   *indexTotalCost = (Cost *) PG_GETARG_POINTER(6);
-	Selectivity *indexSelectivity = (Selectivity *) PG_GETARG_POINTER(7);
-	double	   *indexCorrelation = (double *) PG_GETARG_POINTER(8);
-
-	genericcostestimate(root, index, indexQuals, indexOrderBys, outer_rel, 0.0,
+	IndexPath  *path = (IndexPath *) PG_GETARG_POINTER(1);
+	RelOptInfo *outer_rel = (RelOptInfo *) PG_GETARG_POINTER(2);
+	Cost	   *indexStartupCost = (Cost *) PG_GETARG_POINTER(3);
+	Cost	   *indexTotalCost = (Cost *) PG_GETARG_POINTER(4);
+	Selectivity *indexSelectivity = (Selectivity *) PG_GETARG_POINTER(5);
+	double	   *indexCorrelation = (double *) PG_GETARG_POINTER(6);
+
+	genericcostestimate(root, path, outer_rel, 0.0,
 						indexStartupCost, indexTotalCost,
 						indexSelectivity, indexCorrelation);
 
 gincostestimate(PG_FUNCTION_ARGS)
 {
 	PlannerInfo *root = (PlannerInfo *) PG_GETARG_POINTER(0);
-	IndexOptInfo *index = (IndexOptInfo *) PG_GETARG_POINTER(1);
-	List	   *indexQuals = (List *) PG_GETARG_POINTER(2);
-	List	   *indexOrderBys = (List *) PG_GETARG_POINTER(3);
-	RelOptInfo *outer_rel = (RelOptInfo *) PG_GETARG_POINTER(4);
-	Cost	   *indexStartupCost = (Cost *) PG_GETARG_POINTER(5);
-	Cost	   *indexTotalCost = (Cost *) PG_GETARG_POINTER(6);
-	Selectivity *indexSelectivity = (Selectivity *) PG_GETARG_POINTER(7);
-	double	   *indexCorrelation = (double *) PG_GETARG_POINTER(8);
+	IndexPath  *path = (IndexPath *) PG_GETARG_POINTER(1);
+	RelOptInfo *outer_rel = (RelOptInfo *) PG_GETARG_POINTER(2);
+	Cost	   *indexStartupCost = (Cost *) PG_GETARG_POINTER(3);
+	Cost	   *indexTotalCost = (Cost *) PG_GETARG_POINTER(4);
+	Selectivity *indexSelectivity = (Selectivity *) PG_GETARG_POINTER(5);
+	double	   *indexCorrelation = (double *) PG_GETARG_POINTER(6);
+	IndexOptInfo *index = path->indexinfo;
+	List	   *indexQuals = path->indexquals;
+	List	   *indexOrderBys = path->indexorderbys;
 	ListCell   *l;
 	List	   *selectivityQuals;
 	double		numPages = index->pages,
 	GinStatsData ginStats;
 
 	/*
-	 * For our purposes here, it doesn't matter which index columns the
-	 * individual quals and order-by expressions go with, so flatten the
-	 * lists for convenience.
-	 */
-	indexQuals = flatten_clausegroups_list(indexQuals);
-	indexOrderBys = flatten_indexorderbys_list(indexOrderBys);
-
-	/*
 	 * Obtain statistic information from the meta page
 	 */
 	indexRel = index_open(index->indexoid, AccessShareLock);
 			if (!predicate_implied_by(oneQual, indexQuals))
 				predExtraQuals = list_concat(predExtraQuals, oneQual);
 		}
-		/* list_concat avoids modifying the indexQuals list */
+		/* list_concat avoids modifying the passed-in indexQuals list */
 		selectivityQuals = list_concat(predExtraQuals, indexQuals);
 	}
 	else

File src/include/catalog/catversion.h

  */
 
 /*							yyyymmddN */
-#define CATALOG_VERSION_NO	201112231
+#define CATALOG_VERSION_NO	201112241
 
 #endif

File src/include/catalog/pg_proc.h

 DESCR("btree(internal)");
 DATA(insert OID = 276 (  btcanreturn	   PGNSP PGUID 12 1 0 0 0 f f f t f s 1 0 16 "2281" _null_ _null_ _null_ _null_ btcanreturn _null_ _null_ _null_ ));
 DESCR("btree(internal)");
-DATA(insert OID = 1268 (  btcostestimate   PGNSP PGUID 12 1 0 0 0 f f f t f v 9 0 2278 "2281 2281 2281 2281 2281 2281 2281 2281 2281" _null_ _null_ _null_ _null_ btcostestimate _null_ _null_ _null_ ));
+DATA(insert OID = 1268 (  btcostestimate   PGNSP PGUID 12 1 0 0 0 f f f t f v 7 0 2278 "2281 2281 2281 2281 2281 2281 2281" _null_ _null_ _null_ _null_ btcostestimate _null_ _null_ _null_ ));
 DESCR("btree(internal)");
 DATA(insert OID = 2785 (  btoptions		   PGNSP PGUID 12 1 0 0 0 f f f t f s 2 0 17 "1009 16" _null_ _null_ _null_ _null_  btoptions _null_ _null_ _null_ ));
 DESCR("btree(internal)");
 DESCR("hash(internal)");
 DATA(insert OID = 425 (  hashvacuumcleanup PGNSP PGUID 12 1 0 0 0 f f f t f v 2 0 2281 "2281 2281" _null_ _null_ _null_ _null_ hashvacuumcleanup _null_ _null_ _null_ ));
 DESCR("hash(internal)");
-DATA(insert OID = 438 (  hashcostestimate  PGNSP PGUID 12 1 0 0 0 f f f t f v 9 0 2278 "2281 2281 2281 2281 2281 2281 2281 2281 2281" _null_ _null_ _null_ _null_ hashcostestimate _null_ _null_ _null_ ));
+DATA(insert OID = 438 (  hashcostestimate  PGNSP PGUID 12 1 0 0 0 f f f t f v 7 0 2278 "2281 2281 2281 2281 2281 2281 2281" _null_ _null_ _null_ _null_ hashcostestimate _null_ _null_ _null_ ));
 DESCR("hash(internal)");
 DATA(insert OID = 2786 (  hashoptions	   PGNSP PGUID 12 1 0 0 0 f f f t f s 2 0 17 "1009 16" _null_ _null_ _null_ _null_  hashoptions _null_ _null_ _null_ ));
 DESCR("hash(internal)");
 DESCR("gist(internal)");
 DATA(insert OID = 2561 (  gistvacuumcleanup   PGNSP PGUID 12 1 0 0 0 f f f t f v 2 0 2281 "2281 2281" _null_ _null_ _null_ _null_ gistvacuumcleanup _null_ _null_ _null_ ));
 DESCR("gist(internal)");
-DATA(insert OID = 772 (  gistcostestimate  PGNSP PGUID 12 1 0 0 0 f f f t f v 9 0 2278 "2281 2281 2281 2281 2281 2281 2281 2281 2281" _null_ _null_ _null_ _null_ gistcostestimate _null_ _null_ _null_ ));
+DATA(insert OID = 772 (  gistcostestimate  PGNSP PGUID 12 1 0 0 0 f f f t f v 7 0 2278 "2281 2281 2281 2281 2281 2281 2281" _null_ _null_ _null_ _null_ gistcostestimate _null_ _null_ _null_ ));
 DESCR("gist(internal)");
 DATA(insert OID = 2787 (  gistoptions	   PGNSP PGUID 12 1 0 0 0 f f f t f s 2 0 17 "1009 16" _null_ _null_ _null_ _null_  gistoptions _null_ _null_ _null_ ));
 DESCR("gist(internal)");
 DESCR("gin(internal)");
 DATA(insert OID = 2740 (  ginvacuumcleanup PGNSP PGUID 12 1 0 0 0 f f f t f v 2 0 2281 "2281 2281" _null_ _null_ _null_ _null_ ginvacuumcleanup _null_ _null_ _null_ ));
 DESCR("gin(internal)");
-DATA(insert OID = 2741 (  gincostestimate  PGNSP PGUID 12 1 0 0 0 f f f t f v 9 0 2278 "2281 2281 2281 2281 2281 2281 2281 2281 2281" _null_ _null_ _null_ _null_ gincostestimate _null_ _null_ _null_ ));
+DATA(insert OID = 2741 (  gincostestimate  PGNSP PGUID 12 1 0 0 0 f f f t f v 7 0 2278 "2281 2281 2281 2281 2281 2281 2281" _null_ _null_ _null_ _null_ gincostestimate _null_ _null_ _null_ ));
 DESCR("gin(internal)");
 DATA(insert OID = 2788 (  ginoptions	   PGNSP PGUID 12 1 0 0 0 f f f t f s 2 0 17 "1009 16" _null_ _null_ _null_ _null_  ginoptions _null_ _null_ _null_ ));
 DESCR("gin(internal)");
 DESCR("spgist(internal)");
 DATA(insert OID = 4032 (  spgcanreturn	   PGNSP PGUID 12 1 0 0 0 f f f t f s 1 0 16 "2281" _null_ _null_ _null_ _null_ spgcanreturn _null_ _null_ _null_ ));
 DESCR("spgist(internal)");
-DATA(insert OID = 4013 (  spgcostestimate  PGNSP PGUID 12 1 0 0 0 f f f t f v 9 0 2278 "2281 2281 2281 2281 2281 2281 2281 2281 2281" _null_ _null_ _null_ _null_ spgcostestimate _null_ _null_ _null_ ));
+DATA(insert OID = 4013 (  spgcostestimate  PGNSP PGUID 12 1 0 0 0 f f f t f v 7 0 2278 "2281 2281 2281 2281 2281 2281 2281" _null_ _null_ _null_ _null_ spgcostestimate _null_ _null_ _null_ ));
 DESCR("spgist(internal)");
 DATA(insert OID = 4014 (  spgoptions	   PGNSP PGUID 12 1 0 0 0 f f f t f s 2 0 17 "1009 16" _null_ _null_ _null_ _null_  spgoptions _null_ _null_ _null_ ));
 DESCR("spgist(internal)");

File src/include/nodes/relation.h

  *
  * 'indexclauses' is a list of index qualification clauses, with implicit
  * AND semantics across the list.  Each clause is a RestrictInfo node from
- * the query's WHERE or JOIN conditions.
- *
- * 'indexquals' is a list of sub-lists of the actual index qual conditions
- * that can be used with the index.  There is one possibly-empty sub-list
- * for each index column (but empty sub-lists for trailing columns can be
- * omitted).  The qual conditions are RestrictInfos, and in simple cases
- * are the same RestrictInfos that appear in the flat indexclauses list.
- * But when special indexable operators appear in 'indexclauses', they are
- * replaced by their derived indexscannable conditions in 'indexquals'.
- * Note that an entirely empty indexquals list denotes a full-index scan.
- *
- * 'indexorderbys', if not NIL, is a list of lists of lists of ORDER BY
- * expressions that have been found to be usable as ordering operators for an
- * amcanorderbyop index.  These are not RestrictInfos, just bare expressions,
+ * the query's WHERE or JOIN conditions.  An empty list implies a full
+ * index scan.
+ *
+ * 'indexquals' has the same structure as 'indexclauses', but it contains
+ * the actual index qual conditions that can be used with the index.
+ * In simple cases this is identical to 'indexclauses', but when special
+ * indexable operators appear in 'indexclauses', they are replaced by the
+ * derived indexscannable conditions in 'indexquals'.
+ *
+ * 'indexqualcols' is an integer list of index column numbers (zero-based)
+ * of the same length as 'indexquals', showing which index column each qual
+ * is meant to be used with.  'indexquals' is required to be ordered by
+ * index column, so 'indexqualcols' must form a nondecreasing sequence.
+ * (The order of multiple quals for the same index column is unspecified.)
+ *
+ * 'indexorderbys', if not NIL, is a list of ORDER BY expressions that have
+ * been found to be usable as ordering operators for an amcanorderbyop index.
+ * The list must match the path's pathkeys, ie, one expression per pathkey
+ * in the same order.  These are not RestrictInfos, just bare expressions,
  * since they generally won't yield booleans.  Also, unlike the case for
  * quals, it's guaranteed that each expression has the index key on the left
- * side of the operator.  The top list has one entry per pathkey in the
- * path's pathkeys, and the sub-lists have one sub-sublist per index column.
- * This representation is a bit of overkill, since there will be only one
- * actual expression per pathkey, but it's convenient because each sub-list
- * has the same structure as the indexquals list.
+ * side of the operator.
+ *
+ * 'indexorderbycols' is an integer list of index column numbers (zero-based)
+ * of the same length as 'indexorderbys', showing which index column each
+ * ORDER BY expression is meant to be used with.  (There is no restriction
+ * on which index column each ORDER BY can be used with.)
  *
  * 'isjoininner' is TRUE if the path is a nestloop inner scan (that is,
  * some of the index conditions are join rather than restriction clauses).
 	IndexOptInfo *indexinfo;
 	List	   *indexclauses;
 	List	   *indexquals;
+	List	   *indexqualcols;
 	List	   *indexorderbys;
+	List	   *indexorderbycols;
 	bool		isjoininner;
 	ScanDirection indexscandir;
 	Cost		indextotalcost;

File src/include/optimizer/cost.h

 extern double index_pages_fetched(double tuples_fetched, BlockNumber pages,
 					double index_pages, PlannerInfo *root);
 extern void cost_seqscan(Path *path, PlannerInfo *root, RelOptInfo *baserel);
-extern void cost_index(IndexPath *path, PlannerInfo *root, IndexOptInfo *index,
-		   List *indexQuals, List *indexOrderBys,
-		   bool indexonly, RelOptInfo *outer_rel);
+extern void cost_index(IndexPath *path, PlannerInfo *root,
+					   RelOptInfo *outer_rel);
 extern void cost_bitmap_heap_scan(Path *path, PlannerInfo *root, RelOptInfo *baserel,
 					  Path *bitmapqual, RelOptInfo *outer_rel);
 extern void cost_bitmap_and_node(BitmapAndPath *path, PlannerInfo *root);

File src/include/optimizer/pathnode.h

 extern Path *create_seqscan_path(PlannerInfo *root, RelOptInfo *rel);
 extern IndexPath *create_index_path(PlannerInfo *root,
 				  IndexOptInfo *index,
-				  List *clause_groups,
+				  List *indexclauses,
+				  List *indexclausecols,
 				  List *indexorderbys,
+				  List *indexorderbycols,
 				  List *pathkeys,
 				  ScanDirection indexscandir,
 				  bool indexonly,