From 49e94fce8e26c94475eea232c37c6e22c6837016 Mon Sep 17 00:00:00 2001
From: Tomas Vondra <tomas.vondra@postgresql.org>
Date: Tue, 19 Dec 2023 13:59:07 +0100
Subject: [PATCH v20231219 2/2] reworks

---
 src/backend/commands/explain.c          |   4 +-
 src/backend/executor/nodeIndexscan.c    |  18 ++--
 src/backend/optimizer/path/costsize.c   |  22 -----
 src/backend/optimizer/path/indxpath.c   | 109 ++++++++++++------------
 src/backend/optimizer/plan/createplan.c |  43 ++++++++--
 src/backend/optimizer/plan/setrefs.c    |  11 ++-
 src/include/nodes/execnodes.h           |  24 +++++-
 src/include/nodes/plannodes.h           |  16 +++-
 8 files changed, 144 insertions(+), 103 deletions(-)

diff --git a/src/backend/commands/explain.c b/src/backend/commands/explain.c
index a30708dec9e..db5a8bd0436 100644
--- a/src/backend/commands/explain.c
+++ b/src/backend/commands/explain.c
@@ -1786,8 +1786,10 @@ ExplainNode(PlanState *planstate, List *ancestors,
 		case T_IndexScan:
 			show_scan_qual(((IndexScan *) plan)->indexqualorig,
 						   "Index Cond", planstate, ancestors, es);
-			show_scan_qual(((IndexScan *) plan)->indexfiltersorig,
+			show_scan_qual(((IndexScan *) plan)->indexfilterorig,
 						   "Index Filter", planstate, ancestors, es);
+			show_scan_qual(((IndexScan *) plan)->indexfilterqual,
+						   "Index Filter (non-index)", planstate, ancestors, es);
 			if (((IndexScan *) plan)->indexqualorig)
 				show_instrumentation_count("Rows Removed by Index Recheck", 2,
 										   planstate, es);
diff --git a/src/backend/executor/nodeIndexscan.c b/src/backend/executor/nodeIndexscan.c
index 1ce8a7c19f3..4fb768741ae 100644
--- a/src/backend/executor/nodeIndexscan.c
+++ b/src/backend/executor/nodeIndexscan.c
@@ -122,7 +122,7 @@ IndexNext(IndexScanState *node)
 		node->iss_ScanDesc = scandesc;
 
 		/* Set it up for index-only filters, if there are any. */
-		if (node->indexfilters != NULL)
+		if (node->indexfilter != NULL)
 		{
 			node->iss_ScanDesc->xs_want_itup = true;
 			node->iss_VMBuffer = InvalidBuffer;
@@ -182,7 +182,7 @@ IndexNext(IndexScanState *node)
 			 *
 			 * https://www.postgresql.org/message-id/N1xaIrU29uk5YxLyW55MGk5fz9s6V2FNtj54JRaVlFbPixD5z8sJ07Ite5CvbWwik8ZvDG07oSTN-usENLVMq2UAcizVTEd5b-o16ZGDIIU%3D%40yamlcoder.me
 			 */
-			if (node->indexfilters != NULL)
+			if (node->indexfilter != NULL)
 			{
 				ItemPointer tid = &scandesc->xs_heaptid;
 
@@ -243,7 +243,7 @@ IndexNext(IndexScanState *node)
 					econtext->ecxt_scantuple = node->iss_TableSlot;
 
 					/* check the filters pushed to the index-tuple level */
-					if (!ExecQual(node->indexfilters, econtext))
+					if (!ExecQual(node->indexfilter, econtext))
 					{
 						InstrCountFiltered2(node, 1);
 						continue;
@@ -277,7 +277,7 @@ IndexNext(IndexScanState *node)
 		if (!filter_checked)
 		{
 			econtext->ecxt_scantuple = slot;
-			if (!ExecQual(node->indexfiltersorig, econtext))
+			if (!ExecQual(node->indexfilterorig, econtext))
 			{
 				InstrCountFiltered2(node, 1);
 				continue;
@@ -1099,10 +1099,12 @@ ExecInitIndexScan(IndexScan *node, EState *estate, int eflags)
 		ExecInitQual(node->scan.plan.qual, (PlanState *) indexstate);
 	indexstate->indexqualorig =
 		ExecInitQual(node->indexqualorig, (PlanState *) indexstate);
-	indexstate->indexfilters =
-		ExecInitQual(node->indexfilters, (PlanState *) indexstate);
-	indexstate->indexfiltersorig =
-		ExecInitQual(node->indexfiltersorig, (PlanState *) indexstate);
+	indexstate->indexfilter =
+		ExecInitQual(node->indexfilter, (PlanState *) indexstate);
+	indexstate->indexfilterorig =
+		ExecInitQual(node->indexfilterorig, (PlanState *) indexstate);
+	indexstate->indexfilterqual =
+		ExecInitQual(node->indexfilterqual, (PlanState *) indexstate);
 	indexstate->indexorderbyorig =
 		ExecInitExprList(node->indexorderbyorig, (PlanState *) indexstate);
 
diff --git a/src/backend/optimizer/path/costsize.c b/src/backend/optimizer/path/costsize.c
index fef7d62dab8..0fc0585d01d 100644
--- a/src/backend/optimizer/path/costsize.c
+++ b/src/backend/optimizer/path/costsize.c
@@ -637,28 +637,6 @@ cost_index(IndexPath *path, PlannerInfo *root, double loop_count,
 	/* estimate number of main-table tuples fetched */
 	tuples_fetched = clamp_row_est(indexSelectivity * baserel->tuples);
 
-	/*
-	 * If some filters can be evaluated on the index tuple, account for that.
-	 * We need to scan all tuples from pages that are not all-visible, and
-	 * for the remaining tuples we fetch those not eliminated by the filter.
-	 *
-	 * XXX Does this need to worry about path->path.param_info?
-	 *
-	 * XXX All of this seems overly manual / ad-hoc, surely there's a place
-	 * where we already do this in a more elegant manner?
-	 */
-	if (path->indexfilters != NIL)
-	{
-		Selectivity sel;
-
-		sel = clauselist_selectivity(root, path->indexfilters, baserel->relid,
-									 JOIN_INNER, NULL);
-
-		tuples_fetched *= (1.0 - baserel->allvisfrac) + (baserel->allvisfrac) * sel;
-
-		tuples_fetched = clamp_row_est(tuples_fetched);
-	}
-
 	/* fetch estimated page costs for tablespace containing table */
 	get_tablespace_page_costs(baserel->reltablespace,
 							  &spc_random_page_cost,
diff --git a/src/backend/optimizer/path/indxpath.c b/src/backend/optimizer/path/indxpath.c
index aec05e1a087..6f6465ea7b6 100644
--- a/src/backend/optimizer/path/indxpath.c
+++ b/src/backend/optimizer/path/indxpath.c
@@ -51,9 +51,11 @@ typedef enum
 /* Data structure for collecting qual clauses that match an index */
 typedef struct
 {
-	bool		nonempty;		/* True if lists are not all empty */
+	bool		nonempty;		/* True if lists are not all empty (does not
+								 * consider the indexfilters list). */
 	/* Lists of IndexClause nodes, one list per index column */
 	List	   *indexclauses[INDEX_MAX_KEYS];
+	List	   *indexfilters;
 } IndexClauseSet;
 
 /* Per-path data used within choose_bitmap_and() */
@@ -101,11 +103,9 @@ static bool eclass_already_used(EquivalenceClass *parent_ec, Relids oldrelids,
 								List *indexjoinclauses);
 static void get_index_paths(PlannerInfo *root, RelOptInfo *rel,
 							IndexOptInfo *index, IndexClauseSet *clauses,
-							List *filters, List **bitindexpaths);
+							List **bitindexpaths);
 static List *build_index_paths(PlannerInfo *root, RelOptInfo *rel,
-							   IndexOptInfo *index,
-							   IndexClauseSet *clauses,
-							   List *filters,
+							   IndexOptInfo *index, IndexClauseSet *clauses,
 							   bool useful_predicate,
 							   ScanTypeControl scantype,
 							   bool *skip_nonnative_saop,
@@ -135,8 +135,7 @@ static double adjust_rowcount_for_semijoins(PlannerInfo *root,
 static double approximate_joinrel_size(PlannerInfo *root, Relids relids);
 static void match_restriction_clauses_to_index(PlannerInfo *root,
 											   IndexOptInfo *index,
-											   IndexClauseSet *clauseset,
-											   List **filters);
+											   IndexClauseSet *clauseset);
 static void match_join_clauses_to_index(PlannerInfo *root,
 										RelOptInfo *rel, IndexOptInfo *index,
 										IndexClauseSet *clauseset,
@@ -147,13 +146,11 @@ static void match_eclass_clauses_to_index(PlannerInfo *root,
 static void match_clauses_to_index(PlannerInfo *root,
 								   List *clauses,
 								   IndexOptInfo *index,
-								   IndexClauseSet *clauseset,
-								   List **filters);
+								   IndexClauseSet *clauseset);
 static void match_clause_to_index(PlannerInfo *root,
 								  RestrictInfo *rinfo,
 								  IndexOptInfo *index,
-								  IndexClauseSet *clauseset,
-								  List **filters);
+								  IndexClauseSet *clauseset);
 static IndexClause *match_clause_to_indexcol(PlannerInfo *root,
 											 RestrictInfo *rinfo,
 											 int indexcol,
@@ -250,7 +247,6 @@ create_index_paths(PlannerInfo *root, RelOptInfo *rel)
 	IndexClauseSet rclauseset;
 	IndexClauseSet jclauseset;
 	IndexClauseSet eclauseset;
-	List	   *rfilters;
 	ListCell   *lc;
 
 	/* Skip the whole mess if no indexes */
@@ -280,15 +276,14 @@ create_index_paths(PlannerInfo *root, RelOptInfo *rel)
 		 * Identify the restriction clauses that can match the index.
 		 */
 		MemSet(&rclauseset, 0, sizeof(rclauseset));
-		rfilters = NIL;
-		match_restriction_clauses_to_index(root, index, &rclauseset, &rfilters);
+		match_restriction_clauses_to_index(root, index, &rclauseset);
 
 		/*
 		 * Build index paths from the restriction clauses.  These will be
 		 * non-parameterized paths.  Plain paths go directly to add_path(),
 		 * bitmap paths are added to bitindexpaths to be handled below.
 		 */
-		get_index_paths(root, rel, index, &rclauseset, rfilters,
+		get_index_paths(root, rel, index, &rclauseset,
 						&bitindexpaths);
 
 		/*
@@ -312,8 +307,6 @@ create_index_paths(PlannerInfo *root, RelOptInfo *rel)
 		/*
 		 * If we found any plain or eclass join clauses, build parameterized
 		 * index paths using them.
-		 *
-		 * XXX Maybe pass the filters too?
 		 */
 		if (jclauseset.nonempty || eclauseset.nonempty)
 			consider_index_join_clauses(root, rel, index,
@@ -675,7 +668,7 @@ get_join_index_paths(PlannerInfo *root, RelOptInfo *rel,
 	Assert(clauseset.nonempty);
 
 	/* Build index path(s) using the collected set of clauses */
-	get_index_paths(root, rel, index, &clauseset, NULL, bitindexpaths);
+	get_index_paths(root, rel, index, &clauseset, bitindexpaths);
 
 	/*
 	 * Remember we considered paths for this set of relids.
@@ -725,7 +718,7 @@ eclass_already_used(EquivalenceClass *parent_ec, Relids oldrelids,
 static void
 get_index_paths(PlannerInfo *root, RelOptInfo *rel,
 				IndexOptInfo *index, IndexClauseSet *clauses,
-				List *filters, List **bitindexpaths)
+				List **bitindexpaths)
 {
 	List	   *indexpaths;
 	bool		skip_nonnative_saop = false;
@@ -739,7 +732,7 @@ get_index_paths(PlannerInfo *root, RelOptInfo *rel,
 	 * paths if possible).
 	 */
 	indexpaths = build_index_paths(root, rel,
-								   index, clauses, filters,
+								   index, clauses,
 								   index->predOK,
 								   ST_ANYSCAN,
 								   &skip_nonnative_saop,
@@ -754,7 +747,7 @@ get_index_paths(PlannerInfo *root, RelOptInfo *rel,
 	{
 		indexpaths = list_concat(indexpaths,
 								 build_index_paths(root, rel,
-												   index, clauses, filters,
+												   index, clauses,
 												   index->predOK,
 												   ST_ANYSCAN,
 												   &skip_nonnative_saop,
@@ -794,7 +787,7 @@ get_index_paths(PlannerInfo *root, RelOptInfo *rel,
 	if (skip_nonnative_saop)
 	{
 		indexpaths = build_index_paths(root, rel,
-									   index, clauses, filters,
+									   index, clauses,
 									   false,
 									   ST_BITMAPSCAN,
 									   NULL,
@@ -848,7 +841,6 @@ static List *
 build_index_paths(PlannerInfo *root, RelOptInfo *rel,
 				  IndexOptInfo *index,
 				  IndexClauseSet *clauses,
-				  List *filters,
 				  bool useful_predicate,
 				  ScanTypeControl scantype,
 				  bool *skip_nonnative_saop,
@@ -966,13 +958,16 @@ build_index_paths(PlannerInfo *root, RelOptInfo *rel,
 	/*
 	 * If we have index-only filters, combine the clauses into a simple list and
 	 * add the relids to outer relids.
+	 *
+	 * XXX Shouldn't we try to order the clauses in some particular order, say by
+	 * procost / selectivity? It's difficult, though.
 	 */
 	index_filters = NIL;
-	if (filters)
+	if (clauses->indexfilters)
 	{
 		ListCell   *lc;
 
-		foreach(lc, filters)
+		foreach(lc, clauses->indexfilters)
 		{
 			RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
 
@@ -1187,7 +1182,6 @@ build_paths_for_OR(PlannerInfo *root, RelOptInfo *rel,
 	{
 		IndexOptInfo *index = (IndexOptInfo *) lfirst(lc);
 		IndexClauseSet clauseset;
-		List	   *filters;
 		List	   *indexpaths;
 		bool		useful_predicate;
 
@@ -1232,14 +1226,14 @@ build_paths_for_OR(PlannerInfo *root, RelOptInfo *rel,
 		 * Identify the restriction clauses that can match the index.
 		 */
 		MemSet(&clauseset, 0, sizeof(clauseset));
-		filters = NIL;
-		match_clauses_to_index(root, clauses, index, &clauseset, &filters);
+		match_clauses_to_index(root, clauses, index, &clauseset);
 
 		/*
 		 * If no matches so far, and the index predicate isn't useful, we
 		 * don't want it.
 		 *
-		 * XXX Maybe this should check the filterset too?
+		 * XXX Maybe this should check the index filters too? nonempty=true only
+		 * deals with index clauses, it does not consider filters.
 		 */
 		if (!clauseset.nonempty && !useful_predicate)
 			continue;
@@ -1247,13 +1241,13 @@ build_paths_for_OR(PlannerInfo *root, RelOptInfo *rel,
 		/*
 		 * Add "other" restriction clauses to the clauseset.
 		 */
-		match_clauses_to_index(root, other_clauses, index, &clauseset, &filters);
+		match_clauses_to_index(root, other_clauses, index, &clauseset);
 
 		/*
 		 * Construct paths if possible.
 		 */
 		indexpaths = build_index_paths(root, rel,
-									   index, &clauseset, filters,
+									   index, &clauseset,
 									   useful_predicate,
 									   ST_BITMAPSCAN,
 									   NULL,
@@ -1900,6 +1894,14 @@ check_index_only(RelOptInfo *rel, IndexOptInfo *index)
 /*
  * check_index_filter
  *		Determine whether a clause can be executed directly on the index tuple.
+ *
+ * This checks the clause references only columns that can be returned by the
+ * index, same as in index-only scan.
+ *
+ * XXX It's a bit annoying we build the bitmap from index columns over and over,
+ * so if there are multiple clauses to check, we will do that multiple times.
+ * However, it's not that expensive, there usually are only few such clauses, so
+ * it doesn't seem worth worrying about.
  */
 static bool
 check_index_filter(RelOptInfo *rel, IndexOptInfo *index, Node *clause)
@@ -1909,7 +1911,7 @@ check_index_filter(RelOptInfo *rel, IndexOptInfo *index, Node *clause)
 	Bitmapset  *index_canreturn_attrs = NULL;
 	int			i;
 
-	/* Index-only scans must be enabled */
+	/* Index-only filters must be enabled */
 	if (!enable_indexonlyfilter)
 		return false;
 
@@ -2121,11 +2123,10 @@ approximate_joinrel_size(PlannerInfo *root, Relids relids)
 static void
 match_restriction_clauses_to_index(PlannerInfo *root,
 								   IndexOptInfo *index,
-								   IndexClauseSet *clauseset,
-								   List **filters)
+								   IndexClauseSet *clauseset)
 {
 	/* We can ignore clauses that are implied by the index predicate */
-	match_clauses_to_index(root, index->indrestrictinfo, index, clauseset, filters);
+	match_clauses_to_index(root, index->indrestrictinfo, index, clauseset);
 }
 
 /*
@@ -2133,8 +2134,6 @@ match_restriction_clauses_to_index(PlannerInfo *root,
  *	  Identify join clauses for the rel that match the index.
  *	  Matching clauses are added to *clauseset.
  *	  Also, add any potentially usable join OR clauses to *joinorclauses.
- *
- * FIXME Maybe this should fill the filterset too?
  */
 static void
 match_join_clauses_to_index(PlannerInfo *root,
@@ -2157,7 +2156,7 @@ match_join_clauses_to_index(PlannerInfo *root,
 		if (restriction_is_or_clause(rinfo))
 			*joinorclauses = lappend(*joinorclauses, rinfo);
 		else
-			match_clause_to_index(root, rinfo, index, clauseset, NULL);
+			match_clause_to_index(root, rinfo, index, clauseset);
 	}
 }
 
@@ -2165,8 +2164,6 @@ match_join_clauses_to_index(PlannerInfo *root,
  * match_eclass_clauses_to_index
  *	  Identify EquivalenceClass join clauses for the rel that match the index.
  *	  Matching clauses are added to *clauseset.
- *
- * XXX Maybe this should fill the filterset too?
  */
 static void
 match_eclass_clauses_to_index(PlannerInfo *root, IndexOptInfo *index,
@@ -2197,7 +2194,7 @@ match_eclass_clauses_to_index(PlannerInfo *root, IndexOptInfo *index,
 		 * since for non-btree indexes the EC's equality operators might not
 		 * be in the index opclass (cf ec_member_matches_indexcol).
 		 */
-		match_clauses_to_index(root, clauses, index, clauseset, NULL);
+		match_clauses_to_index(root, clauses, index, clauseset);
 	}
 }
 
@@ -2210,8 +2207,7 @@ static void
 match_clauses_to_index(PlannerInfo *root,
 					   List *clauses,
 					   IndexOptInfo *index,
-					   IndexClauseSet *clauseset,
-					   List **filters)
+					   IndexClauseSet *clauseset)
 {
 	ListCell   *lc;
 
@@ -2219,7 +2215,7 @@ match_clauses_to_index(PlannerInfo *root,
 	{
 		RestrictInfo *rinfo = lfirst_node(RestrictInfo, lc);
 
-		match_clause_to_index(root, rinfo, index, clauseset, filters);
+		match_clause_to_index(root, rinfo, index, clauseset);
 	}
 }
 
@@ -2244,8 +2240,7 @@ static void
 match_clause_to_index(PlannerInfo *root,
 					  RestrictInfo *rinfo,
 					  IndexOptInfo *index,
-					  IndexClauseSet *clauseset,
-					  List **filters)
+					  IndexClauseSet *clauseset)
 {
 	int			indexcol;
 
@@ -2295,19 +2290,15 @@ match_clause_to_index(PlannerInfo *root,
 		}
 	}
 
-	/* if filterset is NULL, we're done */
-	if (!filters)
-		return;
-
 	/*
-	 * We didn't record the clause as a regular index clause, so see if
-	 * we can evaluate it as an index filter.
+	 * If we got here, it means we didn't record the clause as a regular index
+	 * clause, but maybe we could still evaluate it as an index-only filter,
+	 * without fetching the heap tuple.
+	 *
+	 * FIXME maybe check/prevent duplicates, like above?
 	 */
 	if ((rinfo = match_filter_to_index(root, rinfo, index)) != NULL)
-	{
-		/* FIXME maybe check/prevent duplicates, like above? */
-		*filters = lappend(*filters, rinfo);
-	}
+		clauseset->indexfilters = lappend(clauseset->indexfilters, rinfo);
 }
 
 /*
@@ -2453,12 +2444,18 @@ match_filter_to_index(PlannerInfo *root,
 	/*
 	 * Historically this code has coped with NULL clauses.  That's probably
 	 * not possible anymore, but we might as well continue to cope.
+	 *
+	 * XXX This code is new, the comment comes from match_clause_to_indexcol.
+	 * Not sure if we need to handle NULL clauses here. Maybe not.
 	 */
 	if (clause == NULL)
 		return NULL;
 
 	/*
 	 * Can the clause be evaluated only using the index tuple?
+	 *
+	 * XXX Maybe we should just do the check_index_filter here, if it's the only
+	 * thing we need to do. There's no point in having yet another function.
 	 */
 	if (!check_index_filter(index->rel, index, (Node *) rinfo->clause))
 		return NULL;
diff --git a/src/backend/optimizer/plan/createplan.c b/src/backend/optimizer/plan/createplan.c
index cddc87d4fb8..ebdcaef8dab 100644
--- a/src/backend/optimizer/plan/createplan.c
+++ b/src/backend/optimizer/plan/createplan.c
@@ -188,7 +188,8 @@ static SampleScan *make_samplescan(List *qptlist, List *qpqual, Index scanrelid,
 								   TableSampleClause *tsc);
 static IndexScan *make_indexscan(List *qptlist, List *qpqual, Index scanrelid,
 								 Oid indexid, List *indexqual, List *indexqualorig,
-								 List *indexfilters, List *indexfiltersorig,
+								 List *indexfilter, List *indexfilterorig,
+								 List *indexfilterqual,
 								 List *indexorderby, List *indexorderbyorig,
 								 List *indexorderbyops,
 								 ScanDirection indexscandir);
@@ -3014,6 +3015,7 @@ create_indexscan_plan(PlannerInfo *root,
 {
 	Scan	   *scan_plan;
 	List	   *indexclauses = best_path->indexclauses;
+	List	   *indexfilters = best_path->indexfilters;
 	List	   *indexorderbys = best_path->indexorderbys;
 	Index		baserelid = best_path->path.parent->relid;
 	IndexOptInfo *indexinfo = best_path->indexinfo;
@@ -3026,6 +3028,7 @@ create_indexscan_plan(PlannerInfo *root,
 	List	   *fixed_indexorderbys;
 	List	   *indexorderbyops = NIL;
 	ListCell   *l;
+	List	   *filterqual;
 
 	/* it should be a base rel... */
 	Assert(baserelid > 0);
@@ -3045,7 +3048,7 @@ create_indexscan_plan(PlannerInfo *root,
 							 &fixed_indexquals);
 
 	/*
-	 * Extract the index qual expressions (stripped of RestrictInfos) from the
+	 * Extract the index filter expressions (stripped of RestrictInfos) from the
 	 * IndexClauses list, and prepare a copy with index Vars substituted for
 	 * table Vars.  (This step also does replace_nestloop_params on the
 	 * fixed_indexquals.)
@@ -3088,6 +3091,7 @@ create_indexscan_plan(PlannerInfo *root,
 	 * extract_nonindex_conditions() in costsize.c.
 	 */
 	qpqual = NIL;
+	filterqual = NIL;
 	foreach(l, scan_clauses)
 	{
 		RestrictInfo *rinfo = lfirst_node(RestrictInfo, l);
@@ -3101,14 +3105,34 @@ create_indexscan_plan(PlannerInfo *root,
 								 false))
 			continue;			/* provably implied by indexquals */
 		qpqual = lappend(qpqual, rinfo);
+
+		/*
+		 * Maybe add it to the non-index quals, i.e. those that need to be
+		 * evaluated on the heap tuple. But only if we decided to not evaluate
+		 * it on the index directly.
+		 */
+		if (list_member_ptr(indexfilters, rinfo))
+			continue;
+
+		filterqual = lappend(filterqual, rinfo);
 	}
 
 	/* Sort clauses into best execution order */
-	qpqual = order_qual_clauses(root, qpqual);
+	qpqual = order_qual_clauses(root, qpqual);	/* XXX */
 
 	/* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
 	qpqual = extract_actual_clauses(qpqual, false);
 
+	/*
+	 * Do the same cost reordering and reduction with non-index filters.
+	 *
+	 * XXX This is a bit strange/wrong, because it happens after we already
+	 * split the clauses into index and non-index part. So if we want to look
+	 * at the cost and stop pushing stuff down, this is probably too late.
+	 */
+	filterqual = order_qual_clauses(root, filterqual);
+	filterqual = extract_actual_clauses(filterqual, false);
+
 	/*
 	 * We have to replace any outer-relation variables with nestloop params in
 	 * the indexqualorig, qpqual, and indexorderbyorig expressions.  A bit
@@ -3126,6 +3150,8 @@ create_indexscan_plan(PlannerInfo *root,
 			replace_nestloop_params(root, (Node *) stripped_indexfilters);
 		qpqual = (List *)
 			replace_nestloop_params(root, (Node *) qpqual);
+		filterqual = (List *)
+			replace_nestloop_params(root, (Node *) filterqual);
 		indexorderbys = (List *)
 			replace_nestloop_params(root, (Node *) indexorderbys);
 	}
@@ -3202,6 +3228,7 @@ create_indexscan_plan(PlannerInfo *root,
 											stripped_indexquals,
 											fixed_indexfilters,
 											stripped_indexfilters,
+											filterqual,	/* non-index quals */
 											fixed_indexorderbys,
 											indexorderbys,
 											indexorderbyops,
@@ -5595,8 +5622,9 @@ make_indexscan(List *qptlist,
 			   Oid indexid,
 			   List *indexqual,
 			   List *indexqualorig,
-			   List *indexfilters,
-			   List *indexfiltersorig,
+			   List *indexfilter,
+			   List *indexfilterorig,
+			   List *indexfilterqual,
 			   List *indexorderby,
 			   List *indexorderbyorig,
 			   List *indexorderbyops,
@@ -5613,8 +5641,9 @@ make_indexscan(List *qptlist,
 	node->indexid = indexid;
 	node->indexqual = indexqual;
 	node->indexqualorig = indexqualorig;
-	node->indexfilters = indexfilters;
-	node->indexfiltersorig = indexfiltersorig;
+	node->indexfilter = indexfilter;
+	node->indexfilterorig = indexfilterorig;
+	node->indexfilterqual = indexfilterqual;
 	node->indexorderby = indexorderby;
 	node->indexorderbyorig = indexorderbyorig;
 	node->indexorderbyops = indexorderbyops;
diff --git a/src/backend/optimizer/plan/setrefs.c b/src/backend/optimizer/plan/setrefs.c
index 9d48de543ab..c735d996ebd 100644
--- a/src/backend/optimizer/plan/setrefs.c
+++ b/src/backend/optimizer/plan/setrefs.c
@@ -667,11 +667,14 @@ set_plan_refs(PlannerInfo *root, Plan *plan, int rtoffset)
 				splan->indexqualorig =
 					fix_scan_list(root, splan->indexqualorig,
 								  rtoffset, NUM_EXEC_QUAL(plan));
-				splan->indexfilters =
-					fix_scan_list(root, splan->indexfilters,
+				splan->indexfilter =
+					fix_scan_list(root, splan->indexfilter,
 								  rtoffset, 1);
-				splan->indexfiltersorig =
-					fix_scan_list(root, splan->indexfiltersorig,
+				splan->indexfilterorig =
+					fix_scan_list(root, splan->indexfilterorig,
+								  rtoffset, NUM_EXEC_QUAL(plan));
+				splan->indexfilterqual =
+					fix_scan_list(root, splan->indexfilterqual,
 								  rtoffset, NUM_EXEC_QUAL(plan));
 				splan->indexorderby =
 					fix_scan_list(root, splan->indexorderby,
diff --git a/src/include/nodes/execnodes.h b/src/include/nodes/execnodes.h
index 6b50d3808a1..e9bb9922be1 100644
--- a/src/include/nodes/execnodes.h
+++ b/src/include/nodes/execnodes.h
@@ -1571,9 +1571,27 @@ typedef struct IndexScanState
 	Relation	iss_RelationDesc;
 	struct IndexScanDescData *iss_ScanDesc;
 
-	/* index-only filters */
-	ExprState	   *indexfilters;
-	ExprState	   *indexfiltersorig;
+	/*
+	 * index-only filters
+	 *
+	 * The "qual" (in PlanState) contains all the original quals, including
+	 * those that might be evaluated on the index tuple only. This is needed
+	 * when the optimization can't be used (pages that are not all-visible).
+	 * With the optimization enabled we also split the lists into two parts,
+	 * one for clauses evaluated on the index tuple, the other for clauses
+	 * that need to be evaluated on the heap tuple.
+	 *
+	 * indexfilter/indexfilterorig contains the quals to be evaluated on the
+	 * index tuple. indexfilterqual contains quals to be evaluated on the
+	 * original heap tuple.
+	 *
+	 * XXX The indexfilterqual name is a bit strange/misleading, as it seems
+	 * like "index filter quals" but it's "non-index filter quals".
+	 */
+	ExprState  *indexfilter;			/* evaluate on index tuple */
+	ExprState  *indexfilterorig;
+	ExprState  *indexfilterqual;		/* evaluate on heap tuple */
+
 	TupleTableSlot *iss_TableSlot;
 	Buffer			iss_VMBuffer;
 	IndexInfo	   *iss_IndexInfo;
diff --git a/src/include/nodes/plannodes.h b/src/include/nodes/plannodes.h
index 0b0909e209f..7241d3b3490 100644
--- a/src/include/nodes/plannodes.h
+++ b/src/include/nodes/plannodes.h
@@ -425,6 +425,17 @@ typedef struct SampleScan
  * by Var nodes identifying the index columns (their varno is INDEX_VAR and
  * their varattno is the index column number).
  *
+ * indexfilter and indexfilterorig resembles indexqual/indexqualorig, but it
+ * contains quals that are not regular index quals, but still can be evaluated
+ * on the index tuple (in a way similar to what index-only scans do). The
+ * expressions may be more complex (not simple OpExpr etc.), but the index
+ * keys are replaced by Var nodes identifying the index column etc.
+ *
+ * indexfilterqual contains quals that can't be evaluated on the index tuple
+ * (due to referencing a column not included in the index). In principle we
+ * could simply evaluate the quals from the Plan node, but that contains all
+ * quals, including those already evaluated on the index tuple.
+ *
  * indexorderbyorig is similarly the original form of any ORDER BY expressions
  * that are being implemented by the index, while indexorderby is modified to
  * have index column Vars on the left-hand side.  Here, multiple expressions
@@ -450,8 +461,9 @@ typedef struct IndexScan
 	Oid			indexid;		/* OID of index to scan */
 	List	   *indexqual;		/* list of index quals (usually OpExprs) */
 	List	   *indexqualorig;	/* the same in original form */
-	List	   *indexfilters;	/* quals for included columns */
-	List	   *indexfiltersorig;	/* the same in original form */
+	List	   *indexfilter;	/* quals for index-only filters */
+	List	   *indexfilterorig;	/* the same in original form */
+	List	   *indexfilterqual;	/* quals for non-index-only filters */
 	List	   *indexorderby;	/* list of index ORDER BY exprs */
 	List	   *indexorderbyorig;	/* the same in original form */
 	List	   *indexorderbyops;	/* OIDs of sort ops for ORDER BY exprs */
-- 
2.41.0

