1 /*-------------------------------------------------------------------------
2  *
3  * indxpath.c
4  *	  Routines to determine which indexes are usable for scanning a
5  *	  given relation, and create Paths accordingly.
6  *
7  * Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
8  * Portions Copyright (c) 1994, Regents of the University of California
9  *
10  *
11  * IDENTIFICATION
12  *	  src/backend/optimizer/path/indxpath.c
13  *
14  *-------------------------------------------------------------------------
15  */
16 #include "postgres.h"
17 
18 #include <math.h>
19 
20 #include "access/stratnum.h"
21 #include "access/sysattr.h"
22 #include "catalog/pg_am.h"
23 #include "catalog/pg_operator.h"
24 #include "catalog/pg_opfamily.h"
25 #include "catalog/pg_type.h"
26 #include "nodes/makefuncs.h"
27 #include "nodes/nodeFuncs.h"
28 #include "nodes/supportnodes.h"
29 #include "optimizer/cost.h"
30 #include "optimizer/optimizer.h"
31 #include "optimizer/pathnode.h"
32 #include "optimizer/paths.h"
33 #include "optimizer/prep.h"
34 #include "optimizer/restrictinfo.h"
35 #include "utils/lsyscache.h"
36 #include "utils/selfuncs.h"
37 
38 
39 /* XXX see PartCollMatchesExprColl */
40 #define IndexCollMatchesExprColl(idxcollation, exprcollation) \
41 	((idxcollation) == InvalidOid || (idxcollation) == (exprcollation))
42 
43 /* Whether we are looking for plain indexscan, bitmap scan, or either */
44 typedef enum
45 {
46 	ST_INDEXSCAN,				/* must support amgettuple */
47 	ST_BITMAPSCAN,				/* must support amgetbitmap */
48 	ST_ANYSCAN					/* either is okay */
49 } ScanTypeControl;
50 
51 /* Data structure for collecting qual clauses that match an index */
52 typedef struct
53 {
54 	bool		nonempty;		/* True if lists are not all empty */
55 	/* Lists of IndexClause nodes, one list per index column */
56 	List	   *indexclauses[INDEX_MAX_KEYS];
57 } IndexClauseSet;
58 
59 /* Per-path data used within choose_bitmap_and() */
60 typedef struct
61 {
62 	Path	   *path;			/* IndexPath, BitmapAndPath, or BitmapOrPath */
63 	List	   *quals;			/* the WHERE clauses it uses */
64 	List	   *preds;			/* predicates of its partial index(es) */
65 	Bitmapset  *clauseids;		/* quals+preds represented as a bitmapset */
66 	bool		unclassifiable; /* has too many quals+preds to process? */
67 } PathClauseUsage;
68 
69 /* Callback argument for ec_member_matches_indexcol */
70 typedef struct
71 {
72 	IndexOptInfo *index;		/* index we're considering */
73 	int			indexcol;		/* index column we want to match to */
74 } ec_member_matches_arg;
75 
76 
77 static void consider_index_join_clauses(PlannerInfo *root, RelOptInfo *rel,
78 										IndexOptInfo *index,
79 										IndexClauseSet *rclauseset,
80 										IndexClauseSet *jclauseset,
81 										IndexClauseSet *eclauseset,
82 										List **bitindexpaths);
83 static void consider_index_join_outer_rels(PlannerInfo *root, RelOptInfo *rel,
84 										   IndexOptInfo *index,
85 										   IndexClauseSet *rclauseset,
86 										   IndexClauseSet *jclauseset,
87 										   IndexClauseSet *eclauseset,
88 										   List **bitindexpaths,
89 										   List *indexjoinclauses,
90 										   int considered_clauses,
91 										   List **considered_relids);
92 static void get_join_index_paths(PlannerInfo *root, RelOptInfo *rel,
93 								 IndexOptInfo *index,
94 								 IndexClauseSet *rclauseset,
95 								 IndexClauseSet *jclauseset,
96 								 IndexClauseSet *eclauseset,
97 								 List **bitindexpaths,
98 								 Relids relids,
99 								 List **considered_relids);
100 static bool eclass_already_used(EquivalenceClass *parent_ec, Relids oldrelids,
101 								List *indexjoinclauses);
102 static bool bms_equal_any(Relids relids, List *relids_list);
103 static void get_index_paths(PlannerInfo *root, RelOptInfo *rel,
104 							IndexOptInfo *index, IndexClauseSet *clauses,
105 							List **bitindexpaths);
106 static List *build_index_paths(PlannerInfo *root, RelOptInfo *rel,
107 							   IndexOptInfo *index, IndexClauseSet *clauses,
108 							   bool useful_predicate,
109 							   ScanTypeControl scantype,
110 							   bool *skip_nonnative_saop,
111 							   bool *skip_lower_saop);
112 static List *build_paths_for_OR(PlannerInfo *root, RelOptInfo *rel,
113 								List *clauses, List *other_clauses);
114 static List *generate_bitmap_or_paths(PlannerInfo *root, RelOptInfo *rel,
115 									  List *clauses, List *other_clauses);
116 static Path *choose_bitmap_and(PlannerInfo *root, RelOptInfo *rel,
117 							   List *paths);
118 static int	path_usage_comparator(const void *a, const void *b);
119 static Cost bitmap_scan_cost_est(PlannerInfo *root, RelOptInfo *rel,
120 								 Path *ipath);
121 static Cost bitmap_and_cost_est(PlannerInfo *root, RelOptInfo *rel,
122 								List *paths);
123 static PathClauseUsage *classify_index_clause_usage(Path *path,
124 													List **clauselist);
125 static void find_indexpath_quals(Path *bitmapqual, List **quals, List **preds);
126 static int	find_list_position(Node *node, List **nodelist);
127 static bool check_index_only(RelOptInfo *rel, IndexOptInfo *index);
128 static double get_loop_count(PlannerInfo *root, Index cur_relid, Relids outer_relids);
129 static double adjust_rowcount_for_semijoins(PlannerInfo *root,
130 											Index cur_relid,
131 											Index outer_relid,
132 											double rowcount);
133 static double approximate_joinrel_size(PlannerInfo *root, Relids relids);
134 static void match_restriction_clauses_to_index(PlannerInfo *root,
135 											   IndexOptInfo *index,
136 											   IndexClauseSet *clauseset);
137 static void match_join_clauses_to_index(PlannerInfo *root,
138 										RelOptInfo *rel, IndexOptInfo *index,
139 										IndexClauseSet *clauseset,
140 										List **joinorclauses);
141 static void match_eclass_clauses_to_index(PlannerInfo *root,
142 										  IndexOptInfo *index,
143 										  IndexClauseSet *clauseset);
144 static void match_clauses_to_index(PlannerInfo *root,
145 								   List *clauses,
146 								   IndexOptInfo *index,
147 								   IndexClauseSet *clauseset);
148 static void match_clause_to_index(PlannerInfo *root,
149 								  RestrictInfo *rinfo,
150 								  IndexOptInfo *index,
151 								  IndexClauseSet *clauseset);
152 static IndexClause *match_clause_to_indexcol(PlannerInfo *root,
153 											 RestrictInfo *rinfo,
154 											 int indexcol,
155 											 IndexOptInfo *index);
156 static IndexClause *match_boolean_index_clause(PlannerInfo *root,
157 											   RestrictInfo *rinfo,
158 											   int indexcol, IndexOptInfo *index);
159 static IndexClause *match_opclause_to_indexcol(PlannerInfo *root,
160 											   RestrictInfo *rinfo,
161 											   int indexcol,
162 											   IndexOptInfo *index);
163 static IndexClause *match_funcclause_to_indexcol(PlannerInfo *root,
164 												 RestrictInfo *rinfo,
165 												 int indexcol,
166 												 IndexOptInfo *index);
167 static IndexClause *get_index_clause_from_support(PlannerInfo *root,
168 												  RestrictInfo *rinfo,
169 												  Oid funcid,
170 												  int indexarg,
171 												  int indexcol,
172 												  IndexOptInfo *index);
173 static IndexClause *match_saopclause_to_indexcol(PlannerInfo *root,
174 												 RestrictInfo *rinfo,
175 												 int indexcol,
176 												 IndexOptInfo *index);
177 static IndexClause *match_rowcompare_to_indexcol(PlannerInfo *root,
178 												 RestrictInfo *rinfo,
179 												 int indexcol,
180 												 IndexOptInfo *index);
181 static IndexClause *expand_indexqual_rowcompare(PlannerInfo *root,
182 												RestrictInfo *rinfo,
183 												int indexcol,
184 												IndexOptInfo *index,
185 												Oid expr_op,
186 												bool var_on_left);
187 static void match_pathkeys_to_index(IndexOptInfo *index, List *pathkeys,
188 									List **orderby_clauses_p,
189 									List **clause_columns_p);
190 static Expr *match_clause_to_ordering_op(IndexOptInfo *index,
191 										 int indexcol, Expr *clause, Oid pk_opfamily);
192 static bool ec_member_matches_indexcol(PlannerInfo *root, RelOptInfo *rel,
193 									   EquivalenceClass *ec, EquivalenceMember *em,
194 									   void *arg);
195 
196 
197 /*
198  * create_index_paths()
199  *	  Generate all interesting index paths for the given relation.
200  *	  Candidate paths are added to the rel's pathlist (using add_path).
201  *
202  * To be considered for an index scan, an index must match one or more
203  * restriction clauses or join clauses from the query's qual condition,
204  * or match the query's ORDER BY condition, or have a predicate that
205  * matches the query's qual condition.
206  *
207  * There are two basic kinds of index scans.  A "plain" index scan uses
208  * only restriction clauses (possibly none at all) in its indexqual,
209  * so it can be applied in any context.  A "parameterized" index scan uses
210  * join clauses (plus restriction clauses, if available) in its indexqual.
211  * When joining such a scan to one of the relations supplying the other
212  * variables used in its indexqual, the parameterized scan must appear as
213  * the inner relation of a nestloop join; it can't be used on the outer side,
214  * nor in a merge or hash join.  In that context, values for the other rels'
215  * attributes are available and fixed during any one scan of the indexpath.
216  *
217  * An IndexPath is generated and submitted to add_path() for each plain or
218  * parameterized index scan this routine deems potentially interesting for
219  * the current query.
220  *
221  * 'rel' is the relation for which we want to generate index paths
222  *
223  * Note: check_index_predicates() must have been run previously for this rel.
224  *
225  * Note: in cases involving LATERAL references in the relation's tlist, it's
226  * possible that rel->lateral_relids is nonempty.  Currently, we include
227  * lateral_relids into the parameterization reported for each path, but don't
228  * take it into account otherwise.  The fact that any such rels *must* be
229  * available as parameter sources perhaps should influence our choices of
230  * index quals ... but for now, it doesn't seem worth troubling over.
231  * In particular, comments below about "unparameterized" paths should be read
232  * as meaning "unparameterized so far as the indexquals are concerned".
233  */
234 void
create_index_paths(PlannerInfo * root,RelOptInfo * rel)235 create_index_paths(PlannerInfo *root, RelOptInfo *rel)
236 {
237 	List	   *indexpaths;
238 	List	   *bitindexpaths;
239 	List	   *bitjoinpaths;
240 	List	   *joinorclauses;
241 	IndexClauseSet rclauseset;
242 	IndexClauseSet jclauseset;
243 	IndexClauseSet eclauseset;
244 	ListCell   *lc;
245 
246 	/* Skip the whole mess if no indexes */
247 	if (rel->indexlist == NIL)
248 		return;
249 
250 	/* Bitmap paths are collected and then dealt with at the end */
251 	bitindexpaths = bitjoinpaths = joinorclauses = NIL;
252 
253 	/* Examine each index in turn */
254 	foreach(lc, rel->indexlist)
255 	{
256 		IndexOptInfo *index = (IndexOptInfo *) lfirst(lc);
257 
258 		/* Protect limited-size array in IndexClauseSets */
259 		Assert(index->nkeycolumns <= INDEX_MAX_KEYS);
260 
261 		/*
262 		 * Ignore partial indexes that do not match the query.
263 		 * (generate_bitmap_or_paths() might be able to do something with
264 		 * them, but that's of no concern here.)
265 		 */
266 		if (index->indpred != NIL && !index->predOK)
267 			continue;
268 
269 		/*
270 		 * Identify the restriction clauses that can match the index.
271 		 */
272 		MemSet(&rclauseset, 0, sizeof(rclauseset));
273 		match_restriction_clauses_to_index(root, index, &rclauseset);
274 
275 		/*
276 		 * Build index paths from the restriction clauses.  These will be
277 		 * non-parameterized paths.  Plain paths go directly to add_path(),
278 		 * bitmap paths are added to bitindexpaths to be handled below.
279 		 */
280 		get_index_paths(root, rel, index, &rclauseset,
281 						&bitindexpaths);
282 
283 		/*
284 		 * Identify the join clauses that can match the index.  For the moment
285 		 * we keep them separate from the restriction clauses.  Note that this
286 		 * step finds only "loose" join clauses that have not been merged into
287 		 * EquivalenceClasses.  Also, collect join OR clauses for later.
288 		 */
289 		MemSet(&jclauseset, 0, sizeof(jclauseset));
290 		match_join_clauses_to_index(root, rel, index,
291 									&jclauseset, &joinorclauses);
292 
293 		/*
294 		 * Look for EquivalenceClasses that can generate joinclauses matching
295 		 * the index.
296 		 */
297 		MemSet(&eclauseset, 0, sizeof(eclauseset));
298 		match_eclass_clauses_to_index(root, index,
299 									  &eclauseset);
300 
301 		/*
302 		 * If we found any plain or eclass join clauses, build parameterized
303 		 * index paths using them.
304 		 */
305 		if (jclauseset.nonempty || eclauseset.nonempty)
306 			consider_index_join_clauses(root, rel, index,
307 										&rclauseset,
308 										&jclauseset,
309 										&eclauseset,
310 										&bitjoinpaths);
311 	}
312 
313 	/*
314 	 * Generate BitmapOrPaths for any suitable OR-clauses present in the
315 	 * restriction list.  Add these to bitindexpaths.
316 	 */
317 	indexpaths = generate_bitmap_or_paths(root, rel,
318 										  rel->baserestrictinfo, NIL);
319 	bitindexpaths = list_concat(bitindexpaths, indexpaths);
320 
321 	/*
322 	 * Likewise, generate BitmapOrPaths for any suitable OR-clauses present in
323 	 * the joinclause list.  Add these to bitjoinpaths.
324 	 */
325 	indexpaths = generate_bitmap_or_paths(root, rel,
326 										  joinorclauses, rel->baserestrictinfo);
327 	bitjoinpaths = list_concat(bitjoinpaths, indexpaths);
328 
329 	/*
330 	 * If we found anything usable, generate a BitmapHeapPath for the most
331 	 * promising combination of restriction bitmap index paths.  Note there
332 	 * will be only one such path no matter how many indexes exist.  This
333 	 * should be sufficient since there's basically only one figure of merit
334 	 * (total cost) for such a path.
335 	 */
336 	if (bitindexpaths != NIL)
337 	{
338 		Path	   *bitmapqual;
339 		BitmapHeapPath *bpath;
340 
341 		bitmapqual = choose_bitmap_and(root, rel, bitindexpaths);
342 		bpath = create_bitmap_heap_path(root, rel, bitmapqual,
343 										rel->lateral_relids, 1.0, 0);
344 		add_path(rel, (Path *) bpath);
345 
346 		/* create a partial bitmap heap path */
347 		if (rel->consider_parallel && rel->lateral_relids == NULL)
348 			create_partial_bitmap_paths(root, rel, bitmapqual);
349 	}
350 
351 	/*
352 	 * Likewise, if we found anything usable, generate BitmapHeapPaths for the
353 	 * most promising combinations of join bitmap index paths.  Our strategy
354 	 * is to generate one such path for each distinct parameterization seen
355 	 * among the available bitmap index paths.  This may look pretty
356 	 * expensive, but usually there won't be very many distinct
357 	 * parameterizations.  (This logic is quite similar to that in
358 	 * consider_index_join_clauses, but we're working with whole paths not
359 	 * individual clauses.)
360 	 */
361 	if (bitjoinpaths != NIL)
362 	{
363 		List	   *all_path_outers;
364 		ListCell   *lc;
365 
366 		/* Identify each distinct parameterization seen in bitjoinpaths */
367 		all_path_outers = NIL;
368 		foreach(lc, bitjoinpaths)
369 		{
370 			Path	   *path = (Path *) lfirst(lc);
371 			Relids		required_outer = PATH_REQ_OUTER(path);
372 
373 			if (!bms_equal_any(required_outer, all_path_outers))
374 				all_path_outers = lappend(all_path_outers, required_outer);
375 		}
376 
377 		/* Now, for each distinct parameterization set ... */
378 		foreach(lc, all_path_outers)
379 		{
380 			Relids		max_outers = (Relids) lfirst(lc);
381 			List	   *this_path_set;
382 			Path	   *bitmapqual;
383 			Relids		required_outer;
384 			double		loop_count;
385 			BitmapHeapPath *bpath;
386 			ListCell   *lcp;
387 
388 			/* Identify all the bitmap join paths needing no more than that */
389 			this_path_set = NIL;
390 			foreach(lcp, bitjoinpaths)
391 			{
392 				Path	   *path = (Path *) lfirst(lcp);
393 
394 				if (bms_is_subset(PATH_REQ_OUTER(path), max_outers))
395 					this_path_set = lappend(this_path_set, path);
396 			}
397 
398 			/*
399 			 * Add in restriction bitmap paths, since they can be used
400 			 * together with any join paths.
401 			 */
402 			this_path_set = list_concat(this_path_set, bitindexpaths);
403 
404 			/* Select best AND combination for this parameterization */
405 			bitmapqual = choose_bitmap_and(root, rel, this_path_set);
406 
407 			/* And push that path into the mix */
408 			required_outer = PATH_REQ_OUTER(bitmapqual);
409 			loop_count = get_loop_count(root, rel->relid, required_outer);
410 			bpath = create_bitmap_heap_path(root, rel, bitmapqual,
411 											required_outer, loop_count, 0);
412 			add_path(rel, (Path *) bpath);
413 		}
414 	}
415 }
416 
417 /*
418  * consider_index_join_clauses
419  *	  Given sets of join clauses for an index, decide which parameterized
420  *	  index paths to build.
421  *
422  * Plain indexpaths are sent directly to add_path, while potential
423  * bitmap indexpaths are added to *bitindexpaths for later processing.
424  *
425  * 'rel' is the index's heap relation
426  * 'index' is the index for which we want to generate paths
427  * 'rclauseset' is the collection of indexable restriction clauses
428  * 'jclauseset' is the collection of indexable simple join clauses
429  * 'eclauseset' is the collection of indexable clauses from EquivalenceClasses
430  * '*bitindexpaths' is the list to add bitmap paths to
431  */
432 static void
consider_index_join_clauses(PlannerInfo * root,RelOptInfo * rel,IndexOptInfo * index,IndexClauseSet * rclauseset,IndexClauseSet * jclauseset,IndexClauseSet * eclauseset,List ** bitindexpaths)433 consider_index_join_clauses(PlannerInfo *root, RelOptInfo *rel,
434 							IndexOptInfo *index,
435 							IndexClauseSet *rclauseset,
436 							IndexClauseSet *jclauseset,
437 							IndexClauseSet *eclauseset,
438 							List **bitindexpaths)
439 {
440 	int			considered_clauses = 0;
441 	List	   *considered_relids = NIL;
442 	int			indexcol;
443 
444 	/*
445 	 * The strategy here is to identify every potentially useful set of outer
446 	 * rels that can provide indexable join clauses.  For each such set,
447 	 * select all the join clauses available from those outer rels, add on all
448 	 * the indexable restriction clauses, and generate plain and/or bitmap
449 	 * index paths for that set of clauses.  This is based on the assumption
450 	 * that it's always better to apply a clause as an indexqual than as a
451 	 * filter (qpqual); which is where an available clause would end up being
452 	 * applied if we omit it from the indexquals.
453 	 *
454 	 * This looks expensive, but in most practical cases there won't be very
455 	 * many distinct sets of outer rels to consider.  As a safety valve when
456 	 * that's not true, we use a heuristic: limit the number of outer rel sets
457 	 * considered to a multiple of the number of clauses considered.  (We'll
458 	 * always consider using each individual join clause, though.)
459 	 *
460 	 * For simplicity in selecting relevant clauses, we represent each set of
461 	 * outer rels as a maximum set of clause_relids --- that is, the indexed
462 	 * relation itself is also included in the relids set.  considered_relids
463 	 * lists all relids sets we've already tried.
464 	 */
465 	for (indexcol = 0; indexcol < index->nkeycolumns; indexcol++)
466 	{
467 		/* Consider each applicable simple join clause */
468 		considered_clauses += list_length(jclauseset->indexclauses[indexcol]);
469 		consider_index_join_outer_rels(root, rel, index,
470 									   rclauseset, jclauseset, eclauseset,
471 									   bitindexpaths,
472 									   jclauseset->indexclauses[indexcol],
473 									   considered_clauses,
474 									   &considered_relids);
475 		/* Consider each applicable eclass join clause */
476 		considered_clauses += list_length(eclauseset->indexclauses[indexcol]);
477 		consider_index_join_outer_rels(root, rel, index,
478 									   rclauseset, jclauseset, eclauseset,
479 									   bitindexpaths,
480 									   eclauseset->indexclauses[indexcol],
481 									   considered_clauses,
482 									   &considered_relids);
483 	}
484 }
485 
486 /*
487  * consider_index_join_outer_rels
488  *	  Generate parameterized paths based on clause relids in the clause list.
489  *
490  * Workhorse for consider_index_join_clauses; see notes therein for rationale.
491  *
492  * 'rel', 'index', 'rclauseset', 'jclauseset', 'eclauseset', and
493  *		'bitindexpaths' as above
494  * 'indexjoinclauses' is a list of IndexClauses for join clauses
495  * 'considered_clauses' is the total number of clauses considered (so far)
496  * '*considered_relids' is a list of all relids sets already considered
497  */
498 static void
consider_index_join_outer_rels(PlannerInfo * root,RelOptInfo * rel,IndexOptInfo * index,IndexClauseSet * rclauseset,IndexClauseSet * jclauseset,IndexClauseSet * eclauseset,List ** bitindexpaths,List * indexjoinclauses,int considered_clauses,List ** considered_relids)499 consider_index_join_outer_rels(PlannerInfo *root, RelOptInfo *rel,
500 							   IndexOptInfo *index,
501 							   IndexClauseSet *rclauseset,
502 							   IndexClauseSet *jclauseset,
503 							   IndexClauseSet *eclauseset,
504 							   List **bitindexpaths,
505 							   List *indexjoinclauses,
506 							   int considered_clauses,
507 							   List **considered_relids)
508 {
509 	ListCell   *lc;
510 
511 	/* Examine relids of each joinclause in the given list */
512 	foreach(lc, indexjoinclauses)
513 	{
514 		IndexClause *iclause = (IndexClause *) lfirst(lc);
515 		Relids		clause_relids = iclause->rinfo->clause_relids;
516 		EquivalenceClass *parent_ec = iclause->rinfo->parent_ec;
517 		int			num_considered_relids;
518 
519 		/* If we already tried its relids set, no need to do so again */
520 		if (bms_equal_any(clause_relids, *considered_relids))
521 			continue;
522 
523 		/*
524 		 * Generate the union of this clause's relids set with each
525 		 * previously-tried set.  This ensures we try this clause along with
526 		 * every interesting subset of previous clauses.  However, to avoid
527 		 * exponential growth of planning time when there are many clauses,
528 		 * limit the number of relid sets accepted to 10 * considered_clauses.
529 		 *
530 		 * Note: get_join_index_paths appends entries to *considered_relids,
531 		 * but we do not need to visit such newly-added entries within this
532 		 * loop, so we don't use foreach() here.  No real harm would be done
533 		 * if we did visit them, since the subset check would reject them; but
534 		 * it would waste some cycles.
535 		 */
536 		num_considered_relids = list_length(*considered_relids);
537 		for (int pos = 0; pos < num_considered_relids; pos++)
538 		{
539 			Relids		oldrelids = (Relids) list_nth(*considered_relids, pos);
540 
541 			/*
542 			 * If either is a subset of the other, no new set is possible.
543 			 * This isn't a complete test for redundancy, but it's easy and
544 			 * cheap.  get_join_index_paths will check more carefully if we
545 			 * already generated the same relids set.
546 			 */
547 			if (bms_subset_compare(clause_relids, oldrelids) != BMS_DIFFERENT)
548 				continue;
549 
550 			/*
551 			 * If this clause was derived from an equivalence class, the
552 			 * clause list may contain other clauses derived from the same
553 			 * eclass.  We should not consider that combining this clause with
554 			 * one of those clauses generates a usefully different
555 			 * parameterization; so skip if any clause derived from the same
556 			 * eclass would already have been included when using oldrelids.
557 			 */
558 			if (parent_ec &&
559 				eclass_already_used(parent_ec, oldrelids,
560 									indexjoinclauses))
561 				continue;
562 
563 			/*
564 			 * If the number of relid sets considered exceeds our heuristic
565 			 * limit, stop considering combinations of clauses.  We'll still
566 			 * consider the current clause alone, though (below this loop).
567 			 */
568 			if (list_length(*considered_relids) >= 10 * considered_clauses)
569 				break;
570 
571 			/* OK, try the union set */
572 			get_join_index_paths(root, rel, index,
573 								 rclauseset, jclauseset, eclauseset,
574 								 bitindexpaths,
575 								 bms_union(clause_relids, oldrelids),
576 								 considered_relids);
577 		}
578 
579 		/* Also try this set of relids by itself */
580 		get_join_index_paths(root, rel, index,
581 							 rclauseset, jclauseset, eclauseset,
582 							 bitindexpaths,
583 							 clause_relids,
584 							 considered_relids);
585 	}
586 }
587 
588 /*
589  * get_join_index_paths
590  *	  Generate index paths using clauses from the specified outer relations.
591  *	  In addition to generating paths, relids is added to *considered_relids
592  *	  if not already present.
593  *
594  * Workhorse for consider_index_join_clauses; see notes therein for rationale.
595  *
596  * 'rel', 'index', 'rclauseset', 'jclauseset', 'eclauseset',
597  *		'bitindexpaths', 'considered_relids' as above
598  * 'relids' is the current set of relids to consider (the target rel plus
599  *		one or more outer rels)
600  */
601 static void
get_join_index_paths(PlannerInfo * root,RelOptInfo * rel,IndexOptInfo * index,IndexClauseSet * rclauseset,IndexClauseSet * jclauseset,IndexClauseSet * eclauseset,List ** bitindexpaths,Relids relids,List ** considered_relids)602 get_join_index_paths(PlannerInfo *root, RelOptInfo *rel,
603 					 IndexOptInfo *index,
604 					 IndexClauseSet *rclauseset,
605 					 IndexClauseSet *jclauseset,
606 					 IndexClauseSet *eclauseset,
607 					 List **bitindexpaths,
608 					 Relids relids,
609 					 List **considered_relids)
610 {
611 	IndexClauseSet clauseset;
612 	int			indexcol;
613 
614 	/* If we already considered this relids set, don't repeat the work */
615 	if (bms_equal_any(relids, *considered_relids))
616 		return;
617 
618 	/* Identify indexclauses usable with this relids set */
619 	MemSet(&clauseset, 0, sizeof(clauseset));
620 
621 	for (indexcol = 0; indexcol < index->nkeycolumns; indexcol++)
622 	{
623 		ListCell   *lc;
624 
625 		/* First find applicable simple join clauses */
626 		foreach(lc, jclauseset->indexclauses[indexcol])
627 		{
628 			IndexClause *iclause = (IndexClause *) lfirst(lc);
629 
630 			if (bms_is_subset(iclause->rinfo->clause_relids, relids))
631 				clauseset.indexclauses[indexcol] =
632 					lappend(clauseset.indexclauses[indexcol], iclause);
633 		}
634 
635 		/*
636 		 * Add applicable eclass join clauses.  The clauses generated for each
637 		 * column are redundant (cf generate_implied_equalities_for_column),
638 		 * so we need at most one.  This is the only exception to the general
639 		 * rule of using all available index clauses.
640 		 */
641 		foreach(lc, eclauseset->indexclauses[indexcol])
642 		{
643 			IndexClause *iclause = (IndexClause *) lfirst(lc);
644 
645 			if (bms_is_subset(iclause->rinfo->clause_relids, relids))
646 			{
647 				clauseset.indexclauses[indexcol] =
648 					lappend(clauseset.indexclauses[indexcol], iclause);
649 				break;
650 			}
651 		}
652 
653 		/* Add restriction clauses */
654 		clauseset.indexclauses[indexcol] =
655 			list_concat(clauseset.indexclauses[indexcol],
656 						rclauseset->indexclauses[indexcol]);
657 
658 		if (clauseset.indexclauses[indexcol] != NIL)
659 			clauseset.nonempty = true;
660 	}
661 
662 	/* We should have found something, else caller passed silly relids */
663 	Assert(clauseset.nonempty);
664 
665 	/* Build index path(s) using the collected set of clauses */
666 	get_index_paths(root, rel, index, &clauseset, bitindexpaths);
667 
668 	/*
669 	 * Remember we considered paths for this set of relids.
670 	 */
671 	*considered_relids = lappend(*considered_relids, relids);
672 }
673 
674 /*
675  * eclass_already_used
676  *		True if any join clause usable with oldrelids was generated from
677  *		the specified equivalence class.
678  */
679 static bool
eclass_already_used(EquivalenceClass * parent_ec,Relids oldrelids,List * indexjoinclauses)680 eclass_already_used(EquivalenceClass *parent_ec, Relids oldrelids,
681 					List *indexjoinclauses)
682 {
683 	ListCell   *lc;
684 
685 	foreach(lc, indexjoinclauses)
686 	{
687 		IndexClause *iclause = (IndexClause *) lfirst(lc);
688 		RestrictInfo *rinfo = iclause->rinfo;
689 
690 		if (rinfo->parent_ec == parent_ec &&
691 			bms_is_subset(rinfo->clause_relids, oldrelids))
692 			return true;
693 	}
694 	return false;
695 }
696 
697 /*
698  * bms_equal_any
699  *		True if relids is bms_equal to any member of relids_list
700  *
701  * Perhaps this should be in bitmapset.c someday.
702  */
703 static bool
bms_equal_any(Relids relids,List * relids_list)704 bms_equal_any(Relids relids, List *relids_list)
705 {
706 	ListCell   *lc;
707 
708 	foreach(lc, relids_list)
709 	{
710 		if (bms_equal(relids, (Relids) lfirst(lc)))
711 			return true;
712 	}
713 	return false;
714 }
715 
716 
717 /*
718  * get_index_paths
719  *	  Given an index and a set of index clauses for it, construct IndexPaths.
720  *
721  * Plain indexpaths are sent directly to add_path, while potential
722  * bitmap indexpaths are added to *bitindexpaths for later processing.
723  *
724  * This is a fairly simple frontend to build_index_paths().  Its reason for
725  * existence is mainly to handle ScalarArrayOpExpr quals properly.  If the
726  * index AM supports them natively, we should just include them in simple
727  * index paths.  If not, we should exclude them while building simple index
728  * paths, and then make a separate attempt to include them in bitmap paths.
729  * Furthermore, we should consider excluding lower-order ScalarArrayOpExpr
730  * quals so as to create ordered paths.
731  */
732 static void
get_index_paths(PlannerInfo * root,RelOptInfo * rel,IndexOptInfo * index,IndexClauseSet * clauses,List ** bitindexpaths)733 get_index_paths(PlannerInfo *root, RelOptInfo *rel,
734 				IndexOptInfo *index, IndexClauseSet *clauses,
735 				List **bitindexpaths)
736 {
737 	List	   *indexpaths;
738 	bool		skip_nonnative_saop = false;
739 	bool		skip_lower_saop = false;
740 	ListCell   *lc;
741 
742 	/*
743 	 * Build simple index paths using the clauses.  Allow ScalarArrayOpExpr
744 	 * clauses only if the index AM supports them natively, and skip any such
745 	 * clauses for index columns after the first (so that we produce ordered
746 	 * paths if possible).
747 	 */
748 	indexpaths = build_index_paths(root, rel,
749 								   index, clauses,
750 								   index->predOK,
751 								   ST_ANYSCAN,
752 								   &skip_nonnative_saop,
753 								   &skip_lower_saop);
754 
755 	/*
756 	 * If we skipped any lower-order ScalarArrayOpExprs on an index with an AM
757 	 * that supports them, then try again including those clauses.  This will
758 	 * produce paths with more selectivity but no ordering.
759 	 */
760 	if (skip_lower_saop)
761 	{
762 		indexpaths = list_concat(indexpaths,
763 								 build_index_paths(root, rel,
764 												   index, clauses,
765 												   index->predOK,
766 												   ST_ANYSCAN,
767 												   &skip_nonnative_saop,
768 												   NULL));
769 	}
770 
771 	/*
772 	 * Submit all the ones that can form plain IndexScan plans to add_path. (A
773 	 * plain IndexPath can represent either a plain IndexScan or an
774 	 * IndexOnlyScan, but for our purposes here that distinction does not
775 	 * matter.  However, some of the indexes might support only bitmap scans,
776 	 * and those we mustn't submit to add_path here.)
777 	 *
778 	 * Also, pick out the ones that are usable as bitmap scans.  For that, we
779 	 * must discard indexes that don't support bitmap scans, and we also are
780 	 * only interested in paths that have some selectivity; we should discard
781 	 * anything that was generated solely for ordering purposes.
782 	 */
783 	foreach(lc, indexpaths)
784 	{
785 		IndexPath  *ipath = (IndexPath *) lfirst(lc);
786 
787 		if (index->amhasgettuple)
788 			add_path(rel, (Path *) ipath);
789 
790 		if (index->amhasgetbitmap &&
791 			(ipath->path.pathkeys == NIL ||
792 			 ipath->indexselectivity < 1.0))
793 			*bitindexpaths = lappend(*bitindexpaths, ipath);
794 	}
795 
796 	/*
797 	 * If there were ScalarArrayOpExpr clauses that the index can't handle
798 	 * natively, generate bitmap scan paths relying on executor-managed
799 	 * ScalarArrayOpExpr.
800 	 */
801 	if (skip_nonnative_saop)
802 	{
803 		indexpaths = build_index_paths(root, rel,
804 									   index, clauses,
805 									   false,
806 									   ST_BITMAPSCAN,
807 									   NULL,
808 									   NULL);
809 		*bitindexpaths = list_concat(*bitindexpaths, indexpaths);
810 	}
811 }
812 
813 /*
814  * build_index_paths
815  *	  Given an index and a set of index clauses for it, construct zero
816  *	  or more IndexPaths. It also constructs zero or more partial IndexPaths.
817  *
818  * We return a list of paths because (1) this routine checks some cases
819  * that should cause us to not generate any IndexPath, and (2) in some
820  * cases we want to consider both a forward and a backward scan, so as
821  * to obtain both sort orders.  Note that the paths are just returned
822  * to the caller and not immediately fed to add_path().
823  *
824  * At top level, useful_predicate should be exactly the index's predOK flag
825  * (ie, true if it has a predicate that was proven from the restriction
826  * clauses).  When working on an arm of an OR clause, useful_predicate
827  * should be true if the predicate required the current OR list to be proven.
828  * Note that this routine should never be called at all if the index has an
829  * unprovable predicate.
830  *
831  * scantype indicates whether we want to create plain indexscans, bitmap
832  * indexscans, or both.  When it's ST_BITMAPSCAN, we will not consider
833  * index ordering while deciding if a Path is worth generating.
834  *
835  * If skip_nonnative_saop is non-NULL, we ignore ScalarArrayOpExpr clauses
836  * unless the index AM supports them directly, and we set *skip_nonnative_saop
837  * to true if we found any such clauses (caller must initialize the variable
838  * to false).  If it's NULL, we do not ignore ScalarArrayOpExpr clauses.
839  *
840  * If skip_lower_saop is non-NULL, we ignore ScalarArrayOpExpr clauses for
841  * non-first index columns, and we set *skip_lower_saop to true if we found
842  * any such clauses (caller must initialize the variable to false).  If it's
843  * NULL, we do not ignore non-first ScalarArrayOpExpr clauses, but they will
844  * result in considering the scan's output to be unordered.
845  *
846  * 'rel' is the index's heap relation
847  * 'index' is the index for which we want to generate paths
848  * 'clauses' is the collection of indexable clauses (IndexClause nodes)
849  * 'useful_predicate' indicates whether the index has a useful predicate
850  * 'scantype' indicates whether we need plain or bitmap scan support
851  * 'skip_nonnative_saop' indicates whether to accept SAOP if index AM doesn't
852  * 'skip_lower_saop' indicates whether to accept non-first-column SAOP
853  */
854 static List *
build_index_paths(PlannerInfo * root,RelOptInfo * rel,IndexOptInfo * index,IndexClauseSet * clauses,bool useful_predicate,ScanTypeControl scantype,bool * skip_nonnative_saop,bool * skip_lower_saop)855 build_index_paths(PlannerInfo *root, RelOptInfo *rel,
856 				  IndexOptInfo *index, IndexClauseSet *clauses,
857 				  bool useful_predicate,
858 				  ScanTypeControl scantype,
859 				  bool *skip_nonnative_saop,
860 				  bool *skip_lower_saop)
861 {
862 	List	   *result = NIL;
863 	IndexPath  *ipath;
864 	List	   *index_clauses;
865 	Relids		outer_relids;
866 	double		loop_count;
867 	List	   *orderbyclauses;
868 	List	   *orderbyclausecols;
869 	List	   *index_pathkeys;
870 	List	   *useful_pathkeys;
871 	bool		found_lower_saop_clause;
872 	bool		pathkeys_possibly_useful;
873 	bool		index_is_ordered;
874 	bool		index_only_scan;
875 	int			indexcol;
876 
877 	/*
878 	 * Check that index supports the desired scan type(s)
879 	 */
880 	switch (scantype)
881 	{
882 		case ST_INDEXSCAN:
883 			if (!index->amhasgettuple)
884 				return NIL;
885 			break;
886 		case ST_BITMAPSCAN:
887 			if (!index->amhasgetbitmap)
888 				return NIL;
889 			break;
890 		case ST_ANYSCAN:
891 			/* either or both are OK */
892 			break;
893 	}
894 
895 	/*
896 	 * 1. Combine the per-column IndexClause lists into an overall list.
897 	 *
898 	 * In the resulting list, clauses are ordered by index key, so that the
899 	 * column numbers form a nondecreasing sequence.  (This order is depended
900 	 * on by btree and possibly other places.)  The list can be empty, if the
901 	 * index AM allows that.
902 	 *
903 	 * found_lower_saop_clause is set true if we accept a ScalarArrayOpExpr
904 	 * index clause for a non-first index column.  This prevents us from
905 	 * assuming that the scan result is ordered.  (Actually, the result is
906 	 * still ordered if there are equality constraints for all earlier
907 	 * columns, but it seems too expensive and non-modular for this code to be
908 	 * aware of that refinement.)
909 	 *
910 	 * We also build a Relids set showing which outer rels are required by the
911 	 * selected clauses.  Any lateral_relids are included in that, but not
912 	 * otherwise accounted for.
913 	 */
914 	index_clauses = NIL;
915 	found_lower_saop_clause = false;
916 	outer_relids = bms_copy(rel->lateral_relids);
917 	for (indexcol = 0; indexcol < index->nkeycolumns; indexcol++)
918 	{
919 		ListCell   *lc;
920 
921 		foreach(lc, clauses->indexclauses[indexcol])
922 		{
923 			IndexClause *iclause = (IndexClause *) lfirst(lc);
924 			RestrictInfo *rinfo = iclause->rinfo;
925 
926 			/* We might need to omit ScalarArrayOpExpr clauses */
927 			if (IsA(rinfo->clause, ScalarArrayOpExpr))
928 			{
929 				if (!index->amsearcharray)
930 				{
931 					if (skip_nonnative_saop)
932 					{
933 						/* Ignore because not supported by index */
934 						*skip_nonnative_saop = true;
935 						continue;
936 					}
937 					/* Caller had better intend this only for bitmap scan */
938 					Assert(scantype == ST_BITMAPSCAN);
939 				}
940 				if (indexcol > 0)
941 				{
942 					if (skip_lower_saop)
943 					{
944 						/* Caller doesn't want to lose index ordering */
945 						*skip_lower_saop = true;
946 						continue;
947 					}
948 					found_lower_saop_clause = true;
949 				}
950 			}
951 
952 			/* OK to include this clause */
953 			index_clauses = lappend(index_clauses, iclause);
954 			outer_relids = bms_add_members(outer_relids,
955 										   rinfo->clause_relids);
956 		}
957 
958 		/*
959 		 * If no clauses match the first index column, check for amoptionalkey
960 		 * restriction.  We can't generate a scan over an index with
961 		 * amoptionalkey = false unless there's at least one index clause.
962 		 * (When working on columns after the first, this test cannot fail. It
963 		 * is always okay for columns after the first to not have any
964 		 * clauses.)
965 		 */
966 		if (index_clauses == NIL && !index->amoptionalkey)
967 			return NIL;
968 	}
969 
970 	/* We do not want the index's rel itself listed in outer_relids */
971 	outer_relids = bms_del_member(outer_relids, rel->relid);
972 	/* Enforce convention that outer_relids is exactly NULL if empty */
973 	if (bms_is_empty(outer_relids))
974 		outer_relids = NULL;
975 
976 	/* Compute loop_count for cost estimation purposes */
977 	loop_count = get_loop_count(root, rel->relid, outer_relids);
978 
979 	/*
980 	 * 2. Compute pathkeys describing index's ordering, if any, then see how
981 	 * many of them are actually useful for this query.  This is not relevant
982 	 * if we are only trying to build bitmap indexscans, nor if we have to
983 	 * assume the scan is unordered.
984 	 */
985 	pathkeys_possibly_useful = (scantype != ST_BITMAPSCAN &&
986 								!found_lower_saop_clause &&
987 								has_useful_pathkeys(root, rel));
988 	index_is_ordered = (index->sortopfamily != NULL);
989 	if (index_is_ordered && pathkeys_possibly_useful)
990 	{
991 		index_pathkeys = build_index_pathkeys(root, index,
992 											  ForwardScanDirection);
993 		useful_pathkeys = truncate_useless_pathkeys(root, rel,
994 													index_pathkeys);
995 		orderbyclauses = NIL;
996 		orderbyclausecols = NIL;
997 	}
998 	else if (index->amcanorderbyop && pathkeys_possibly_useful)
999 	{
1000 		/* see if we can generate ordering operators for query_pathkeys */
1001 		match_pathkeys_to_index(index, root->query_pathkeys,
1002 								&orderbyclauses,
1003 								&orderbyclausecols);
1004 		if (orderbyclauses)
1005 			useful_pathkeys = root->query_pathkeys;
1006 		else
1007 			useful_pathkeys = NIL;
1008 	}
1009 	else
1010 	{
1011 		useful_pathkeys = NIL;
1012 		orderbyclauses = NIL;
1013 		orderbyclausecols = NIL;
1014 	}
1015 
1016 	/*
1017 	 * 3. Check if an index-only scan is possible.  If we're not building
1018 	 * plain indexscans, this isn't relevant since bitmap scans don't support
1019 	 * index data retrieval anyway.
1020 	 */
1021 	index_only_scan = (scantype != ST_BITMAPSCAN &&
1022 					   check_index_only(rel, index));
1023 
1024 	/*
1025 	 * 4. Generate an indexscan path if there are relevant restriction clauses
1026 	 * in the current clauses, OR the index ordering is potentially useful for
1027 	 * later merging or final output ordering, OR the index has a useful
1028 	 * predicate, OR an index-only scan is possible.
1029 	 */
1030 	if (index_clauses != NIL || useful_pathkeys != NIL || useful_predicate ||
1031 		index_only_scan)
1032 	{
1033 		ipath = create_index_path(root, index,
1034 								  index_clauses,
1035 								  orderbyclauses,
1036 								  orderbyclausecols,
1037 								  useful_pathkeys,
1038 								  index_is_ordered ?
1039 								  ForwardScanDirection :
1040 								  NoMovementScanDirection,
1041 								  index_only_scan,
1042 								  outer_relids,
1043 								  loop_count,
1044 								  false);
1045 		result = lappend(result, ipath);
1046 
1047 		/*
1048 		 * If appropriate, consider parallel index scan.  We don't allow
1049 		 * parallel index scan for bitmap index scans.
1050 		 */
1051 		if (index->amcanparallel &&
1052 			rel->consider_parallel && outer_relids == NULL &&
1053 			scantype != ST_BITMAPSCAN)
1054 		{
1055 			ipath = create_index_path(root, index,
1056 									  index_clauses,
1057 									  orderbyclauses,
1058 									  orderbyclausecols,
1059 									  useful_pathkeys,
1060 									  index_is_ordered ?
1061 									  ForwardScanDirection :
1062 									  NoMovementScanDirection,
1063 									  index_only_scan,
1064 									  outer_relids,
1065 									  loop_count,
1066 									  true);
1067 
1068 			/*
1069 			 * if, after costing the path, we find that it's not worth using
1070 			 * parallel workers, just free it.
1071 			 */
1072 			if (ipath->path.parallel_workers > 0)
1073 				add_partial_path(rel, (Path *) ipath);
1074 			else
1075 				pfree(ipath);
1076 		}
1077 	}
1078 
1079 	/*
1080 	 * 5. If the index is ordered, a backwards scan might be interesting.
1081 	 */
1082 	if (index_is_ordered && pathkeys_possibly_useful)
1083 	{
1084 		index_pathkeys = build_index_pathkeys(root, index,
1085 											  BackwardScanDirection);
1086 		useful_pathkeys = truncate_useless_pathkeys(root, rel,
1087 													index_pathkeys);
1088 		if (useful_pathkeys != NIL)
1089 		{
1090 			ipath = create_index_path(root, index,
1091 									  index_clauses,
1092 									  NIL,
1093 									  NIL,
1094 									  useful_pathkeys,
1095 									  BackwardScanDirection,
1096 									  index_only_scan,
1097 									  outer_relids,
1098 									  loop_count,
1099 									  false);
1100 			result = lappend(result, ipath);
1101 
1102 			/* If appropriate, consider parallel index scan */
1103 			if (index->amcanparallel &&
1104 				rel->consider_parallel && outer_relids == NULL &&
1105 				scantype != ST_BITMAPSCAN)
1106 			{
1107 				ipath = create_index_path(root, index,
1108 										  index_clauses,
1109 										  NIL,
1110 										  NIL,
1111 										  useful_pathkeys,
1112 										  BackwardScanDirection,
1113 										  index_only_scan,
1114 										  outer_relids,
1115 										  loop_count,
1116 										  true);
1117 
1118 				/*
1119 				 * if, after costing the path, we find that it's not worth
1120 				 * using parallel workers, just free it.
1121 				 */
1122 				if (ipath->path.parallel_workers > 0)
1123 					add_partial_path(rel, (Path *) ipath);
1124 				else
1125 					pfree(ipath);
1126 			}
1127 		}
1128 	}
1129 
1130 	return result;
1131 }
1132 
1133 /*
1134  * build_paths_for_OR
1135  *	  Given a list of restriction clauses from one arm of an OR clause,
1136  *	  construct all matching IndexPaths for the relation.
1137  *
1138  * Here we must scan all indexes of the relation, since a bitmap OR tree
1139  * can use multiple indexes.
1140  *
1141  * The caller actually supplies two lists of restriction clauses: some
1142  * "current" ones and some "other" ones.  Both lists can be used freely
1143  * to match keys of the index, but an index must use at least one of the
1144  * "current" clauses to be considered usable.  The motivation for this is
1145  * examples like
1146  *		WHERE (x = 42) AND (... OR (y = 52 AND z = 77) OR ....)
1147  * While we are considering the y/z subclause of the OR, we can use "x = 42"
1148  * as one of the available index conditions; but we shouldn't match the
1149  * subclause to any index on x alone, because such a Path would already have
1150  * been generated at the upper level.  So we could use an index on x,y,z
1151  * or an index on x,y for the OR subclause, but not an index on just x.
1152  * When dealing with a partial index, a match of the index predicate to
1153  * one of the "current" clauses also makes the index usable.
1154  *
1155  * 'rel' is the relation for which we want to generate index paths
1156  * 'clauses' is the current list of clauses (RestrictInfo nodes)
1157  * 'other_clauses' is the list of additional upper-level clauses
1158  */
1159 static List *
build_paths_for_OR(PlannerInfo * root,RelOptInfo * rel,List * clauses,List * other_clauses)1160 build_paths_for_OR(PlannerInfo *root, RelOptInfo *rel,
1161 				   List *clauses, List *other_clauses)
1162 {
1163 	List	   *result = NIL;
1164 	List	   *all_clauses = NIL;	/* not computed till needed */
1165 	ListCell   *lc;
1166 
1167 	foreach(lc, rel->indexlist)
1168 	{
1169 		IndexOptInfo *index = (IndexOptInfo *) lfirst(lc);
1170 		IndexClauseSet clauseset;
1171 		List	   *indexpaths;
1172 		bool		useful_predicate;
1173 
1174 		/* Ignore index if it doesn't support bitmap scans */
1175 		if (!index->amhasgetbitmap)
1176 			continue;
1177 
1178 		/*
1179 		 * Ignore partial indexes that do not match the query.  If a partial
1180 		 * index is marked predOK then we know it's OK.  Otherwise, we have to
1181 		 * test whether the added clauses are sufficient to imply the
1182 		 * predicate. If so, we can use the index in the current context.
1183 		 *
1184 		 * We set useful_predicate to true iff the predicate was proven using
1185 		 * the current set of clauses.  This is needed to prevent matching a
1186 		 * predOK index to an arm of an OR, which would be a legal but
1187 		 * pointlessly inefficient plan.  (A better plan will be generated by
1188 		 * just scanning the predOK index alone, no OR.)
1189 		 */
1190 		useful_predicate = false;
1191 		if (index->indpred != NIL)
1192 		{
1193 			if (index->predOK)
1194 			{
1195 				/* Usable, but don't set useful_predicate */
1196 			}
1197 			else
1198 			{
1199 				/* Form all_clauses if not done already */
1200 				if (all_clauses == NIL)
1201 					all_clauses = list_concat_copy(clauses, other_clauses);
1202 
1203 				if (!predicate_implied_by(index->indpred, all_clauses, false))
1204 					continue;	/* can't use it at all */
1205 
1206 				if (!predicate_implied_by(index->indpred, other_clauses, false))
1207 					useful_predicate = true;
1208 			}
1209 		}
1210 
1211 		/*
1212 		 * Identify the restriction clauses that can match the index.
1213 		 */
1214 		MemSet(&clauseset, 0, sizeof(clauseset));
1215 		match_clauses_to_index(root, clauses, index, &clauseset);
1216 
1217 		/*
1218 		 * If no matches so far, and the index predicate isn't useful, we
1219 		 * don't want it.
1220 		 */
1221 		if (!clauseset.nonempty && !useful_predicate)
1222 			continue;
1223 
1224 		/*
1225 		 * Add "other" restriction clauses to the clauseset.
1226 		 */
1227 		match_clauses_to_index(root, other_clauses, index, &clauseset);
1228 
1229 		/*
1230 		 * Construct paths if possible.
1231 		 */
1232 		indexpaths = build_index_paths(root, rel,
1233 									   index, &clauseset,
1234 									   useful_predicate,
1235 									   ST_BITMAPSCAN,
1236 									   NULL,
1237 									   NULL);
1238 		result = list_concat(result, indexpaths);
1239 	}
1240 
1241 	return result;
1242 }
1243 
1244 /*
1245  * generate_bitmap_or_paths
1246  *		Look through the list of clauses to find OR clauses, and generate
1247  *		a BitmapOrPath for each one we can handle that way.  Return a list
1248  *		of the generated BitmapOrPaths.
1249  *
1250  * other_clauses is a list of additional clauses that can be assumed true
1251  * for the purpose of generating indexquals, but are not to be searched for
1252  * ORs.  (See build_paths_for_OR() for motivation.)
1253  */
1254 static List *
generate_bitmap_or_paths(PlannerInfo * root,RelOptInfo * rel,List * clauses,List * other_clauses)1255 generate_bitmap_or_paths(PlannerInfo *root, RelOptInfo *rel,
1256 						 List *clauses, List *other_clauses)
1257 {
1258 	List	   *result = NIL;
1259 	List	   *all_clauses;
1260 	ListCell   *lc;
1261 
1262 	/*
1263 	 * We can use both the current and other clauses as context for
1264 	 * build_paths_for_OR; no need to remove ORs from the lists.
1265 	 */
1266 	all_clauses = list_concat_copy(clauses, other_clauses);
1267 
1268 	foreach(lc, clauses)
1269 	{
1270 		RestrictInfo *rinfo = lfirst_node(RestrictInfo, lc);
1271 		List	   *pathlist;
1272 		Path	   *bitmapqual;
1273 		ListCell   *j;
1274 
1275 		/* Ignore RestrictInfos that aren't ORs */
1276 		if (!restriction_is_or_clause(rinfo))
1277 			continue;
1278 
1279 		/*
1280 		 * We must be able to match at least one index to each of the arms of
1281 		 * the OR, else we can't use it.
1282 		 */
1283 		pathlist = NIL;
1284 		foreach(j, ((BoolExpr *) rinfo->orclause)->args)
1285 		{
1286 			Node	   *orarg = (Node *) lfirst(j);
1287 			List	   *indlist;
1288 
1289 			/* OR arguments should be ANDs or sub-RestrictInfos */
1290 			if (is_andclause(orarg))
1291 			{
1292 				List	   *andargs = ((BoolExpr *) orarg)->args;
1293 
1294 				indlist = build_paths_for_OR(root, rel,
1295 											 andargs,
1296 											 all_clauses);
1297 
1298 				/* Recurse in case there are sub-ORs */
1299 				indlist = list_concat(indlist,
1300 									  generate_bitmap_or_paths(root, rel,
1301 															   andargs,
1302 															   all_clauses));
1303 			}
1304 			else
1305 			{
1306 				RestrictInfo *rinfo = castNode(RestrictInfo, orarg);
1307 				List	   *orargs;
1308 
1309 				Assert(!restriction_is_or_clause(rinfo));
1310 				orargs = list_make1(rinfo);
1311 
1312 				indlist = build_paths_for_OR(root, rel,
1313 											 orargs,
1314 											 all_clauses);
1315 			}
1316 
1317 			/*
1318 			 * If nothing matched this arm, we can't do anything with this OR
1319 			 * clause.
1320 			 */
1321 			if (indlist == NIL)
1322 			{
1323 				pathlist = NIL;
1324 				break;
1325 			}
1326 
1327 			/*
1328 			 * OK, pick the most promising AND combination, and add it to
1329 			 * pathlist.
1330 			 */
1331 			bitmapqual = choose_bitmap_and(root, rel, indlist);
1332 			pathlist = lappend(pathlist, bitmapqual);
1333 		}
1334 
1335 		/*
1336 		 * If we have a match for every arm, then turn them into a
1337 		 * BitmapOrPath, and add to result list.
1338 		 */
1339 		if (pathlist != NIL)
1340 		{
1341 			bitmapqual = (Path *) create_bitmap_or_path(root, rel, pathlist);
1342 			result = lappend(result, bitmapqual);
1343 		}
1344 	}
1345 
1346 	return result;
1347 }
1348 
1349 
1350 /*
1351  * choose_bitmap_and
1352  *		Given a nonempty list of bitmap paths, AND them into one path.
1353  *
1354  * This is a nontrivial decision since we can legally use any subset of the
1355  * given path set.  We want to choose a good tradeoff between selectivity
1356  * and cost of computing the bitmap.
1357  *
1358  * The result is either a single one of the inputs, or a BitmapAndPath
1359  * combining multiple inputs.
1360  */
1361 static Path *
choose_bitmap_and(PlannerInfo * root,RelOptInfo * rel,List * paths)1362 choose_bitmap_and(PlannerInfo *root, RelOptInfo *rel, List *paths)
1363 {
1364 	int			npaths = list_length(paths);
1365 	PathClauseUsage **pathinfoarray;
1366 	PathClauseUsage *pathinfo;
1367 	List	   *clauselist;
1368 	List	   *bestpaths = NIL;
1369 	Cost		bestcost = 0;
1370 	int			i,
1371 				j;
1372 	ListCell   *l;
1373 
1374 	Assert(npaths > 0);			/* else caller error */
1375 	if (npaths == 1)
1376 		return (Path *) linitial(paths);	/* easy case */
1377 
1378 	/*
1379 	 * In theory we should consider every nonempty subset of the given paths.
1380 	 * In practice that seems like overkill, given the crude nature of the
1381 	 * estimates, not to mention the possible effects of higher-level AND and
1382 	 * OR clauses.  Moreover, it's completely impractical if there are a large
1383 	 * number of paths, since the work would grow as O(2^N).
1384 	 *
1385 	 * As a heuristic, we first check for paths using exactly the same sets of
1386 	 * WHERE clauses + index predicate conditions, and reject all but the
1387 	 * cheapest-to-scan in any such group.  This primarily gets rid of indexes
1388 	 * that include the interesting columns but also irrelevant columns.  (In
1389 	 * situations where the DBA has gone overboard on creating variant
1390 	 * indexes, this can make for a very large reduction in the number of
1391 	 * paths considered further.)
1392 	 *
1393 	 * We then sort the surviving paths with the cheapest-to-scan first, and
1394 	 * for each path, consider using that path alone as the basis for a bitmap
1395 	 * scan.  Then we consider bitmap AND scans formed from that path plus
1396 	 * each subsequent (higher-cost) path, adding on a subsequent path if it
1397 	 * results in a reduction in the estimated total scan cost. This means we
1398 	 * consider about O(N^2) rather than O(2^N) path combinations, which is
1399 	 * quite tolerable, especially given than N is usually reasonably small
1400 	 * because of the prefiltering step.  The cheapest of these is returned.
1401 	 *
1402 	 * We will only consider AND combinations in which no two indexes use the
1403 	 * same WHERE clause.  This is a bit of a kluge: it's needed because
1404 	 * costsize.c and clausesel.c aren't very smart about redundant clauses.
1405 	 * They will usually double-count the redundant clauses, producing a
1406 	 * too-small selectivity that makes a redundant AND step look like it
1407 	 * reduces the total cost.  Perhaps someday that code will be smarter and
1408 	 * we can remove this limitation.  (But note that this also defends
1409 	 * against flat-out duplicate input paths, which can happen because
1410 	 * match_join_clauses_to_index will find the same OR join clauses that
1411 	 * extract_restriction_or_clauses has pulled OR restriction clauses out
1412 	 * of.)
1413 	 *
1414 	 * For the same reason, we reject AND combinations in which an index
1415 	 * predicate clause duplicates another clause.  Here we find it necessary
1416 	 * to be even stricter: we'll reject a partial index if any of its
1417 	 * predicate clauses are implied by the set of WHERE clauses and predicate
1418 	 * clauses used so far.  This covers cases such as a condition "x = 42"
1419 	 * used with a plain index, followed by a clauseless scan of a partial
1420 	 * index "WHERE x >= 40 AND x < 50".  The partial index has been accepted
1421 	 * only because "x = 42" was present, and so allowing it would partially
1422 	 * double-count selectivity.  (We could use predicate_implied_by on
1423 	 * regular qual clauses too, to have a more intelligent, but much more
1424 	 * expensive, check for redundancy --- but in most cases simple equality
1425 	 * seems to suffice.)
1426 	 */
1427 
1428 	/*
1429 	 * Extract clause usage info and detect any paths that use exactly the
1430 	 * same set of clauses; keep only the cheapest-to-scan of any such groups.
1431 	 * The surviving paths are put into an array for qsort'ing.
1432 	 */
1433 	pathinfoarray = (PathClauseUsage **)
1434 		palloc(npaths * sizeof(PathClauseUsage *));
1435 	clauselist = NIL;
1436 	npaths = 0;
1437 	foreach(l, paths)
1438 	{
1439 		Path	   *ipath = (Path *) lfirst(l);
1440 
1441 		pathinfo = classify_index_clause_usage(ipath, &clauselist);
1442 
1443 		/* If it's unclassifiable, treat it as distinct from all others */
1444 		if (pathinfo->unclassifiable)
1445 		{
1446 			pathinfoarray[npaths++] = pathinfo;
1447 			continue;
1448 		}
1449 
1450 		for (i = 0; i < npaths; i++)
1451 		{
1452 			if (!pathinfoarray[i]->unclassifiable &&
1453 				bms_equal(pathinfo->clauseids, pathinfoarray[i]->clauseids))
1454 				break;
1455 		}
1456 		if (i < npaths)
1457 		{
1458 			/* duplicate clauseids, keep the cheaper one */
1459 			Cost		ncost;
1460 			Cost		ocost;
1461 			Selectivity nselec;
1462 			Selectivity oselec;
1463 
1464 			cost_bitmap_tree_node(pathinfo->path, &ncost, &nselec);
1465 			cost_bitmap_tree_node(pathinfoarray[i]->path, &ocost, &oselec);
1466 			if (ncost < ocost)
1467 				pathinfoarray[i] = pathinfo;
1468 		}
1469 		else
1470 		{
1471 			/* not duplicate clauseids, add to array */
1472 			pathinfoarray[npaths++] = pathinfo;
1473 		}
1474 	}
1475 
1476 	/* If only one surviving path, we're done */
1477 	if (npaths == 1)
1478 		return pathinfoarray[0]->path;
1479 
1480 	/* Sort the surviving paths by index access cost */
1481 	qsort(pathinfoarray, npaths, sizeof(PathClauseUsage *),
1482 		  path_usage_comparator);
1483 
1484 	/*
1485 	 * For each surviving index, consider it as an "AND group leader", and see
1486 	 * whether adding on any of the later indexes results in an AND path with
1487 	 * cheaper total cost than before.  Then take the cheapest AND group.
1488 	 *
1489 	 * Note: paths that are either clauseless or unclassifiable will have
1490 	 * empty clauseids, so that they will not be rejected by the clauseids
1491 	 * filter here, nor will they cause later paths to be rejected by it.
1492 	 */
1493 	for (i = 0; i < npaths; i++)
1494 	{
1495 		Cost		costsofar;
1496 		List	   *qualsofar;
1497 		Bitmapset  *clauseidsofar;
1498 
1499 		pathinfo = pathinfoarray[i];
1500 		paths = list_make1(pathinfo->path);
1501 		costsofar = bitmap_scan_cost_est(root, rel, pathinfo->path);
1502 		qualsofar = list_concat_copy(pathinfo->quals, pathinfo->preds);
1503 		clauseidsofar = bms_copy(pathinfo->clauseids);
1504 
1505 		for (j = i + 1; j < npaths; j++)
1506 		{
1507 			Cost		newcost;
1508 
1509 			pathinfo = pathinfoarray[j];
1510 			/* Check for redundancy */
1511 			if (bms_overlap(pathinfo->clauseids, clauseidsofar))
1512 				continue;		/* consider it redundant */
1513 			if (pathinfo->preds)
1514 			{
1515 				bool		redundant = false;
1516 
1517 				/* we check each predicate clause separately */
1518 				foreach(l, pathinfo->preds)
1519 				{
1520 					Node	   *np = (Node *) lfirst(l);
1521 
1522 					if (predicate_implied_by(list_make1(np), qualsofar, false))
1523 					{
1524 						redundant = true;
1525 						break;	/* out of inner foreach loop */
1526 					}
1527 				}
1528 				if (redundant)
1529 					continue;
1530 			}
1531 			/* tentatively add new path to paths, so we can estimate cost */
1532 			paths = lappend(paths, pathinfo->path);
1533 			newcost = bitmap_and_cost_est(root, rel, paths);
1534 			if (newcost < costsofar)
1535 			{
1536 				/* keep new path in paths, update subsidiary variables */
1537 				costsofar = newcost;
1538 				qualsofar = list_concat(qualsofar, pathinfo->quals);
1539 				qualsofar = list_concat(qualsofar, pathinfo->preds);
1540 				clauseidsofar = bms_add_members(clauseidsofar,
1541 												pathinfo->clauseids);
1542 			}
1543 			else
1544 			{
1545 				/* reject new path, remove it from paths list */
1546 				paths = list_truncate(paths, list_length(paths) - 1);
1547 			}
1548 		}
1549 
1550 		/* Keep the cheapest AND-group (or singleton) */
1551 		if (i == 0 || costsofar < bestcost)
1552 		{
1553 			bestpaths = paths;
1554 			bestcost = costsofar;
1555 		}
1556 
1557 		/* some easy cleanup (we don't try real hard though) */
1558 		list_free(qualsofar);
1559 	}
1560 
1561 	if (list_length(bestpaths) == 1)
1562 		return (Path *) linitial(bestpaths);	/* no need for AND */
1563 	return (Path *) create_bitmap_and_path(root, rel, bestpaths);
1564 }
1565 
1566 /* qsort comparator to sort in increasing index access cost order */
1567 static int
path_usage_comparator(const void * a,const void * b)1568 path_usage_comparator(const void *a, const void *b)
1569 {
1570 	PathClauseUsage *pa = *(PathClauseUsage *const *) a;
1571 	PathClauseUsage *pb = *(PathClauseUsage *const *) b;
1572 	Cost		acost;
1573 	Cost		bcost;
1574 	Selectivity aselec;
1575 	Selectivity bselec;
1576 
1577 	cost_bitmap_tree_node(pa->path, &acost, &aselec);
1578 	cost_bitmap_tree_node(pb->path, &bcost, &bselec);
1579 
1580 	/*
1581 	 * If costs are the same, sort by selectivity.
1582 	 */
1583 	if (acost < bcost)
1584 		return -1;
1585 	if (acost > bcost)
1586 		return 1;
1587 
1588 	if (aselec < bselec)
1589 		return -1;
1590 	if (aselec > bselec)
1591 		return 1;
1592 
1593 	return 0;
1594 }
1595 
1596 /*
1597  * Estimate the cost of actually executing a bitmap scan with a single
1598  * index path (which could be a BitmapAnd or BitmapOr node).
1599  */
1600 static Cost
bitmap_scan_cost_est(PlannerInfo * root,RelOptInfo * rel,Path * ipath)1601 bitmap_scan_cost_est(PlannerInfo *root, RelOptInfo *rel, Path *ipath)
1602 {
1603 	BitmapHeapPath bpath;
1604 
1605 	/* Set up a dummy BitmapHeapPath */
1606 	bpath.path.type = T_BitmapHeapPath;
1607 	bpath.path.pathtype = T_BitmapHeapScan;
1608 	bpath.path.parent = rel;
1609 	bpath.path.pathtarget = rel->reltarget;
1610 	bpath.path.param_info = ipath->param_info;
1611 	bpath.path.pathkeys = NIL;
1612 	bpath.bitmapqual = ipath;
1613 
1614 	/*
1615 	 * Check the cost of temporary path without considering parallelism.
1616 	 * Parallel bitmap heap path will be considered at later stage.
1617 	 */
1618 	bpath.path.parallel_workers = 0;
1619 
1620 	/* Now we can do cost_bitmap_heap_scan */
1621 	cost_bitmap_heap_scan(&bpath.path, root, rel,
1622 						  bpath.path.param_info,
1623 						  ipath,
1624 						  get_loop_count(root, rel->relid,
1625 										 PATH_REQ_OUTER(ipath)));
1626 
1627 	return bpath.path.total_cost;
1628 }
1629 
1630 /*
1631  * Estimate the cost of actually executing a BitmapAnd scan with the given
1632  * inputs.
1633  */
1634 static Cost
bitmap_and_cost_est(PlannerInfo * root,RelOptInfo * rel,List * paths)1635 bitmap_and_cost_est(PlannerInfo *root, RelOptInfo *rel, List *paths)
1636 {
1637 	BitmapAndPath *apath;
1638 
1639 	/*
1640 	 * Might as well build a real BitmapAndPath here, as the work is slightly
1641 	 * too complicated to be worth repeating just to save one palloc.
1642 	 */
1643 	apath = create_bitmap_and_path(root, rel, paths);
1644 
1645 	return bitmap_scan_cost_est(root, rel, (Path *) apath);
1646 }
1647 
1648 
1649 /*
1650  * classify_index_clause_usage
1651  *		Construct a PathClauseUsage struct describing the WHERE clauses and
1652  *		index predicate clauses used by the given indexscan path.
1653  *		We consider two clauses the same if they are equal().
1654  *
1655  * At some point we might want to migrate this info into the Path data
1656  * structure proper, but for the moment it's only needed within
1657  * choose_bitmap_and().
1658  *
1659  * *clauselist is used and expanded as needed to identify all the distinct
1660  * clauses seen across successive calls.  Caller must initialize it to NIL
1661  * before first call of a set.
1662  */
1663 static PathClauseUsage *
classify_index_clause_usage(Path * path,List ** clauselist)1664 classify_index_clause_usage(Path *path, List **clauselist)
1665 {
1666 	PathClauseUsage *result;
1667 	Bitmapset  *clauseids;
1668 	ListCell   *lc;
1669 
1670 	result = (PathClauseUsage *) palloc(sizeof(PathClauseUsage));
1671 	result->path = path;
1672 
1673 	/* Recursively find the quals and preds used by the path */
1674 	result->quals = NIL;
1675 	result->preds = NIL;
1676 	find_indexpath_quals(path, &result->quals, &result->preds);
1677 
1678 	/*
1679 	 * Some machine-generated queries have outlandish numbers of qual clauses.
1680 	 * To avoid getting into O(N^2) behavior even in this preliminary
1681 	 * classification step, we want to limit the number of entries we can
1682 	 * accumulate in *clauselist.  Treat any path with more than 100 quals +
1683 	 * preds as unclassifiable, which will cause calling code to consider it
1684 	 * distinct from all other paths.
1685 	 */
1686 	if (list_length(result->quals) + list_length(result->preds) > 100)
1687 	{
1688 		result->clauseids = NULL;
1689 		result->unclassifiable = true;
1690 		return result;
1691 	}
1692 
1693 	/* Build up a bitmapset representing the quals and preds */
1694 	clauseids = NULL;
1695 	foreach(lc, result->quals)
1696 	{
1697 		Node	   *node = (Node *) lfirst(lc);
1698 
1699 		clauseids = bms_add_member(clauseids,
1700 								   find_list_position(node, clauselist));
1701 	}
1702 	foreach(lc, result->preds)
1703 	{
1704 		Node	   *node = (Node *) lfirst(lc);
1705 
1706 		clauseids = bms_add_member(clauseids,
1707 								   find_list_position(node, clauselist));
1708 	}
1709 	result->clauseids = clauseids;
1710 	result->unclassifiable = false;
1711 
1712 	return result;
1713 }
1714 
1715 
1716 /*
1717  * find_indexpath_quals
1718  *
1719  * Given the Path structure for a plain or bitmap indexscan, extract lists
1720  * of all the index clauses and index predicate conditions used in the Path.
1721  * These are appended to the initial contents of *quals and *preds (hence
1722  * caller should initialize those to NIL).
1723  *
1724  * Note we are not trying to produce an accurate representation of the AND/OR
1725  * semantics of the Path, but just find out all the base conditions used.
1726  *
1727  * The result lists contain pointers to the expressions used in the Path,
1728  * but all the list cells are freshly built, so it's safe to destructively
1729  * modify the lists (eg, by concat'ing with other lists).
1730  */
1731 static void
find_indexpath_quals(Path * bitmapqual,List ** quals,List ** preds)1732 find_indexpath_quals(Path *bitmapqual, List **quals, List **preds)
1733 {
1734 	if (IsA(bitmapqual, BitmapAndPath))
1735 	{
1736 		BitmapAndPath *apath = (BitmapAndPath *) bitmapqual;
1737 		ListCell   *l;
1738 
1739 		foreach(l, apath->bitmapquals)
1740 		{
1741 			find_indexpath_quals((Path *) lfirst(l), quals, preds);
1742 		}
1743 	}
1744 	else if (IsA(bitmapqual, BitmapOrPath))
1745 	{
1746 		BitmapOrPath *opath = (BitmapOrPath *) bitmapqual;
1747 		ListCell   *l;
1748 
1749 		foreach(l, opath->bitmapquals)
1750 		{
1751 			find_indexpath_quals((Path *) lfirst(l), quals, preds);
1752 		}
1753 	}
1754 	else if (IsA(bitmapqual, IndexPath))
1755 	{
1756 		IndexPath  *ipath = (IndexPath *) bitmapqual;
1757 		ListCell   *l;
1758 
1759 		foreach(l, ipath->indexclauses)
1760 		{
1761 			IndexClause *iclause = (IndexClause *) lfirst(l);
1762 
1763 			*quals = lappend(*quals, iclause->rinfo->clause);
1764 		}
1765 		*preds = list_concat(*preds, ipath->indexinfo->indpred);
1766 	}
1767 	else
1768 		elog(ERROR, "unrecognized node type: %d", nodeTag(bitmapqual));
1769 }
1770 
1771 
1772 /*
1773  * find_list_position
1774  *		Return the given node's position (counting from 0) in the given
1775  *		list of nodes.  If it's not equal() to any existing list member,
1776  *		add it at the end, and return that position.
1777  */
1778 static int
find_list_position(Node * node,List ** nodelist)1779 find_list_position(Node *node, List **nodelist)
1780 {
1781 	int			i;
1782 	ListCell   *lc;
1783 
1784 	i = 0;
1785 	foreach(lc, *nodelist)
1786 	{
1787 		Node	   *oldnode = (Node *) lfirst(lc);
1788 
1789 		if (equal(node, oldnode))
1790 			return i;
1791 		i++;
1792 	}
1793 
1794 	*nodelist = lappend(*nodelist, node);
1795 
1796 	return i;
1797 }
1798 
1799 
1800 /*
1801  * check_index_only
1802  *		Determine whether an index-only scan is possible for this index.
1803  */
1804 static bool
check_index_only(RelOptInfo * rel,IndexOptInfo * index)1805 check_index_only(RelOptInfo *rel, IndexOptInfo *index)
1806 {
1807 	bool		result;
1808 	Bitmapset  *attrs_used = NULL;
1809 	Bitmapset  *index_canreturn_attrs = NULL;
1810 	Bitmapset  *index_cannotreturn_attrs = NULL;
1811 	ListCell   *lc;
1812 	int			i;
1813 
1814 	/* Index-only scans must be enabled */
1815 	if (!enable_indexonlyscan)
1816 		return false;
1817 
1818 	/*
1819 	 * Check that all needed attributes of the relation are available from the
1820 	 * index.
1821 	 */
1822 
1823 	/*
1824 	 * First, identify all the attributes needed for joins or final output.
1825 	 * Note: we must look at rel's targetlist, not the attr_needed data,
1826 	 * because attr_needed isn't computed for inheritance child rels.
1827 	 */
1828 	pull_varattnos((Node *) rel->reltarget->exprs, rel->relid, &attrs_used);
1829 
1830 	/*
1831 	 * Add all the attributes used by restriction clauses; but consider only
1832 	 * those clauses not implied by the index predicate, since ones that are
1833 	 * so implied don't need to be checked explicitly in the plan.
1834 	 *
1835 	 * Note: attributes used only in index quals would not be needed at
1836 	 * runtime either, if we are certain that the index is not lossy.  However
1837 	 * it'd be complicated to account for that accurately, and it doesn't
1838 	 * matter in most cases, since we'd conclude that such attributes are
1839 	 * available from the index anyway.
1840 	 */
1841 	foreach(lc, index->indrestrictinfo)
1842 	{
1843 		RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
1844 
1845 		pull_varattnos((Node *) rinfo->clause, rel->relid, &attrs_used);
1846 	}
1847 
1848 	/*
1849 	 * Construct a bitmapset of columns that the index can return back in an
1850 	 * index-only scan.  If there are multiple index columns containing the
1851 	 * same attribute, all of them must be capable of returning the value,
1852 	 * since we might recheck operators on any of them.  (Potentially we could
1853 	 * be smarter about that, but it's such a weird situation that it doesn't
1854 	 * seem worth spending a lot of sweat on.)
1855 	 */
1856 	for (i = 0; i < index->ncolumns; i++)
1857 	{
1858 		int			attno = index->indexkeys[i];
1859 
1860 		/*
1861 		 * For the moment, we just ignore index expressions.  It might be nice
1862 		 * to do something with them, later.
1863 		 */
1864 		if (attno == 0)
1865 			continue;
1866 
1867 		if (index->canreturn[i])
1868 			index_canreturn_attrs =
1869 				bms_add_member(index_canreturn_attrs,
1870 							   attno - FirstLowInvalidHeapAttributeNumber);
1871 		else
1872 			index_cannotreturn_attrs =
1873 				bms_add_member(index_cannotreturn_attrs,
1874 							   attno - FirstLowInvalidHeapAttributeNumber);
1875 	}
1876 
1877 	index_canreturn_attrs = bms_del_members(index_canreturn_attrs,
1878 											index_cannotreturn_attrs);
1879 
1880 	/* Do we have all the necessary attributes? */
1881 	result = bms_is_subset(attrs_used, index_canreturn_attrs);
1882 
1883 	bms_free(attrs_used);
1884 	bms_free(index_canreturn_attrs);
1885 	bms_free(index_cannotreturn_attrs);
1886 
1887 	return result;
1888 }
1889 
1890 /*
1891  * get_loop_count
1892  *		Choose the loop count estimate to use for costing a parameterized path
1893  *		with the given set of outer relids.
1894  *
1895  * Since we produce parameterized paths before we've begun to generate join
1896  * relations, it's impossible to predict exactly how many times a parameterized
1897  * path will be iterated; we don't know the size of the relation that will be
1898  * on the outside of the nestloop.  However, we should try to account for
1899  * multiple iterations somehow in costing the path.  The heuristic embodied
1900  * here is to use the rowcount of the smallest other base relation needed in
1901  * the join clauses used by the path.  (We could alternatively consider the
1902  * largest one, but that seems too optimistic.)  This is of course the right
1903  * answer for single-other-relation cases, and it seems like a reasonable
1904  * zero-order approximation for multiway-join cases.
1905  *
1906  * In addition, we check to see if the other side of each join clause is on
1907  * the inside of some semijoin that the current relation is on the outside of.
1908  * If so, the only way that a parameterized path could be used is if the
1909  * semijoin RHS has been unique-ified, so we should use the number of unique
1910  * RHS rows rather than using the relation's raw rowcount.
1911  *
1912  * Note: for this to work, allpaths.c must establish all baserel size
1913  * estimates before it begins to compute paths, or at least before it
1914  * calls create_index_paths().
1915  */
1916 static double
get_loop_count(PlannerInfo * root,Index cur_relid,Relids outer_relids)1917 get_loop_count(PlannerInfo *root, Index cur_relid, Relids outer_relids)
1918 {
1919 	double		result;
1920 	int			outer_relid;
1921 
1922 	/* For a non-parameterized path, just return 1.0 quickly */
1923 	if (outer_relids == NULL)
1924 		return 1.0;
1925 
1926 	result = 0.0;
1927 	outer_relid = -1;
1928 	while ((outer_relid = bms_next_member(outer_relids, outer_relid)) >= 0)
1929 	{
1930 		RelOptInfo *outer_rel;
1931 		double		rowcount;
1932 
1933 		/* Paranoia: ignore bogus relid indexes */
1934 		if (outer_relid >= root->simple_rel_array_size)
1935 			continue;
1936 		outer_rel = root->simple_rel_array[outer_relid];
1937 		if (outer_rel == NULL)
1938 			continue;
1939 		Assert(outer_rel->relid == outer_relid);	/* sanity check on array */
1940 
1941 		/* Other relation could be proven empty, if so ignore */
1942 		if (IS_DUMMY_REL(outer_rel))
1943 			continue;
1944 
1945 		/* Otherwise, rel's rows estimate should be valid by now */
1946 		Assert(outer_rel->rows > 0);
1947 
1948 		/* Check to see if rel is on the inside of any semijoins */
1949 		rowcount = adjust_rowcount_for_semijoins(root,
1950 												 cur_relid,
1951 												 outer_relid,
1952 												 outer_rel->rows);
1953 
1954 		/* Remember smallest row count estimate among the outer rels */
1955 		if (result == 0.0 || result > rowcount)
1956 			result = rowcount;
1957 	}
1958 	/* Return 1.0 if we found no valid relations (shouldn't happen) */
1959 	return (result > 0.0) ? result : 1.0;
1960 }
1961 
1962 /*
1963  * Check to see if outer_relid is on the inside of any semijoin that cur_relid
1964  * is on the outside of.  If so, replace rowcount with the estimated number of
1965  * unique rows from the semijoin RHS (assuming that's smaller, which it might
1966  * not be).  The estimate is crude but it's the best we can do at this stage
1967  * of the proceedings.
1968  */
1969 static double
adjust_rowcount_for_semijoins(PlannerInfo * root,Index cur_relid,Index outer_relid,double rowcount)1970 adjust_rowcount_for_semijoins(PlannerInfo *root,
1971 							  Index cur_relid,
1972 							  Index outer_relid,
1973 							  double rowcount)
1974 {
1975 	ListCell   *lc;
1976 
1977 	foreach(lc, root->join_info_list)
1978 	{
1979 		SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) lfirst(lc);
1980 
1981 		if (sjinfo->jointype == JOIN_SEMI &&
1982 			bms_is_member(cur_relid, sjinfo->syn_lefthand) &&
1983 			bms_is_member(outer_relid, sjinfo->syn_righthand))
1984 		{
1985 			/* Estimate number of unique-ified rows */
1986 			double		nraw;
1987 			double		nunique;
1988 
1989 			nraw = approximate_joinrel_size(root, sjinfo->syn_righthand);
1990 			nunique = estimate_num_groups(root,
1991 										  sjinfo->semi_rhs_exprs,
1992 										  nraw,
1993 										  NULL,
1994 										  NULL);
1995 			if (rowcount > nunique)
1996 				rowcount = nunique;
1997 		}
1998 	}
1999 	return rowcount;
2000 }
2001 
2002 /*
2003  * Make an approximate estimate of the size of a joinrel.
2004  *
2005  * We don't have enough info at this point to get a good estimate, so we
2006  * just multiply the base relation sizes together.  Fortunately, this is
2007  * the right answer anyway for the most common case with a single relation
2008  * on the RHS of a semijoin.  Also, estimate_num_groups() has only a weak
2009  * dependency on its input_rows argument (it basically uses it as a clamp).
2010  * So we might be able to get a fairly decent end result even with a severe
2011  * overestimate of the RHS's raw size.
2012  */
2013 static double
approximate_joinrel_size(PlannerInfo * root,Relids relids)2014 approximate_joinrel_size(PlannerInfo *root, Relids relids)
2015 {
2016 	double		rowcount = 1.0;
2017 	int			relid;
2018 
2019 	relid = -1;
2020 	while ((relid = bms_next_member(relids, relid)) >= 0)
2021 	{
2022 		RelOptInfo *rel;
2023 
2024 		/* Paranoia: ignore bogus relid indexes */
2025 		if (relid >= root->simple_rel_array_size)
2026 			continue;
2027 		rel = root->simple_rel_array[relid];
2028 		if (rel == NULL)
2029 			continue;
2030 		Assert(rel->relid == relid);	/* sanity check on array */
2031 
2032 		/* Relation could be proven empty, if so ignore */
2033 		if (IS_DUMMY_REL(rel))
2034 			continue;
2035 
2036 		/* Otherwise, rel's rows estimate should be valid by now */
2037 		Assert(rel->rows > 0);
2038 
2039 		/* Accumulate product */
2040 		rowcount *= rel->rows;
2041 	}
2042 	return rowcount;
2043 }
2044 
2045 
2046 /****************************************************************************
2047  *				----  ROUTINES TO CHECK QUERY CLAUSES  ----
2048  ****************************************************************************/
2049 
2050 /*
2051  * match_restriction_clauses_to_index
2052  *	  Identify restriction clauses for the rel that match the index.
2053  *	  Matching clauses are added to *clauseset.
2054  */
2055 static void
match_restriction_clauses_to_index(PlannerInfo * root,IndexOptInfo * index,IndexClauseSet * clauseset)2056 match_restriction_clauses_to_index(PlannerInfo *root,
2057 								   IndexOptInfo *index,
2058 								   IndexClauseSet *clauseset)
2059 {
2060 	/* We can ignore clauses that are implied by the index predicate */
2061 	match_clauses_to_index(root, index->indrestrictinfo, index, clauseset);
2062 }
2063 
2064 /*
2065  * match_join_clauses_to_index
2066  *	  Identify join clauses for the rel that match the index.
2067  *	  Matching clauses are added to *clauseset.
2068  *	  Also, add any potentially usable join OR clauses to *joinorclauses.
2069  */
2070 static void
match_join_clauses_to_index(PlannerInfo * root,RelOptInfo * rel,IndexOptInfo * index,IndexClauseSet * clauseset,List ** joinorclauses)2071 match_join_clauses_to_index(PlannerInfo *root,
2072 							RelOptInfo *rel, IndexOptInfo *index,
2073 							IndexClauseSet *clauseset,
2074 							List **joinorclauses)
2075 {
2076 	ListCell   *lc;
2077 
2078 	/* Scan the rel's join clauses */
2079 	foreach(lc, rel->joininfo)
2080 	{
2081 		RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
2082 
2083 		/* Check if clause can be moved to this rel */
2084 		if (!join_clause_is_movable_to(rinfo, rel))
2085 			continue;
2086 
2087 		/* Potentially usable, so see if it matches the index or is an OR */
2088 		if (restriction_is_or_clause(rinfo))
2089 			*joinorclauses = lappend(*joinorclauses, rinfo);
2090 		else
2091 			match_clause_to_index(root, rinfo, index, clauseset);
2092 	}
2093 }
2094 
2095 /*
2096  * match_eclass_clauses_to_index
2097  *	  Identify EquivalenceClass join clauses for the rel that match the index.
2098  *	  Matching clauses are added to *clauseset.
2099  */
2100 static void
match_eclass_clauses_to_index(PlannerInfo * root,IndexOptInfo * index,IndexClauseSet * clauseset)2101 match_eclass_clauses_to_index(PlannerInfo *root, IndexOptInfo *index,
2102 							  IndexClauseSet *clauseset)
2103 {
2104 	int			indexcol;
2105 
2106 	/* No work if rel is not in any such ECs */
2107 	if (!index->rel->has_eclass_joins)
2108 		return;
2109 
2110 	for (indexcol = 0; indexcol < index->nkeycolumns; indexcol++)
2111 	{
2112 		ec_member_matches_arg arg;
2113 		List	   *clauses;
2114 
2115 		/* Generate clauses, skipping any that join to lateral_referencers */
2116 		arg.index = index;
2117 		arg.indexcol = indexcol;
2118 		clauses = generate_implied_equalities_for_column(root,
2119 														 index->rel,
2120 														 ec_member_matches_indexcol,
2121 														 (void *) &arg,
2122 														 index->rel->lateral_referencers);
2123 
2124 		/*
2125 		 * We have to check whether the results actually do match the index,
2126 		 * since for non-btree indexes the EC's equality operators might not
2127 		 * be in the index opclass (cf ec_member_matches_indexcol).
2128 		 */
2129 		match_clauses_to_index(root, clauses, index, clauseset);
2130 	}
2131 }
2132 
2133 /*
2134  * match_clauses_to_index
2135  *	  Perform match_clause_to_index() for each clause in a list.
2136  *	  Matching clauses are added to *clauseset.
2137  */
2138 static void
match_clauses_to_index(PlannerInfo * root,List * clauses,IndexOptInfo * index,IndexClauseSet * clauseset)2139 match_clauses_to_index(PlannerInfo *root,
2140 					   List *clauses,
2141 					   IndexOptInfo *index,
2142 					   IndexClauseSet *clauseset)
2143 {
2144 	ListCell   *lc;
2145 
2146 	foreach(lc, clauses)
2147 	{
2148 		RestrictInfo *rinfo = lfirst_node(RestrictInfo, lc);
2149 
2150 		match_clause_to_index(root, rinfo, index, clauseset);
2151 	}
2152 }
2153 
2154 /*
2155  * match_clause_to_index
2156  *	  Test whether a qual clause can be used with an index.
2157  *
2158  * If the clause is usable, add an IndexClause entry for it to the appropriate
2159  * list in *clauseset.  (*clauseset must be initialized to zeroes before first
2160  * call.)
2161  *
2162  * Note: in some circumstances we may find the same RestrictInfos coming from
2163  * multiple places.  Defend against redundant outputs by refusing to add a
2164  * clause twice (pointer equality should be a good enough check for this).
2165  *
2166  * Note: it's possible that a badly-defined index could have multiple matching
2167  * columns.  We always select the first match if so; this avoids scenarios
2168  * wherein we get an inflated idea of the index's selectivity by using the
2169  * same clause multiple times with different index columns.
2170  */
2171 static void
match_clause_to_index(PlannerInfo * root,RestrictInfo * rinfo,IndexOptInfo * index,IndexClauseSet * clauseset)2172 match_clause_to_index(PlannerInfo *root,
2173 					  RestrictInfo *rinfo,
2174 					  IndexOptInfo *index,
2175 					  IndexClauseSet *clauseset)
2176 {
2177 	int			indexcol;
2178 
2179 	/*
2180 	 * Never match pseudoconstants to indexes.  (Normally a match could not
2181 	 * happen anyway, since a pseudoconstant clause couldn't contain a Var,
2182 	 * but what if someone builds an expression index on a constant? It's not
2183 	 * totally unreasonable to do so with a partial index, either.)
2184 	 */
2185 	if (rinfo->pseudoconstant)
2186 		return;
2187 
2188 	/*
2189 	 * If clause can't be used as an indexqual because it must wait till after
2190 	 * some lower-security-level restriction clause, reject it.
2191 	 */
2192 	if (!restriction_is_securely_promotable(rinfo, index->rel))
2193 		return;
2194 
2195 	/* OK, check each index key column for a match */
2196 	for (indexcol = 0; indexcol < index->nkeycolumns; indexcol++)
2197 	{
2198 		IndexClause *iclause;
2199 		ListCell   *lc;
2200 
2201 		/* Ignore duplicates */
2202 		foreach(lc, clauseset->indexclauses[indexcol])
2203 		{
2204 			IndexClause *iclause = (IndexClause *) lfirst(lc);
2205 
2206 			if (iclause->rinfo == rinfo)
2207 				return;
2208 		}
2209 
2210 		/* OK, try to match the clause to the index column */
2211 		iclause = match_clause_to_indexcol(root,
2212 										   rinfo,
2213 										   indexcol,
2214 										   index);
2215 		if (iclause)
2216 		{
2217 			/* Success, so record it */
2218 			clauseset->indexclauses[indexcol] =
2219 				lappend(clauseset->indexclauses[indexcol], iclause);
2220 			clauseset->nonempty = true;
2221 			return;
2222 		}
2223 	}
2224 }
2225 
2226 /*
2227  * match_clause_to_indexcol()
2228  *	  Determine whether a restriction clause matches a column of an index,
2229  *	  and if so, build an IndexClause node describing the details.
2230  *
2231  *	  To match an index normally, an operator clause:
2232  *
2233  *	  (1)  must be in the form (indexkey op const) or (const op indexkey);
2234  *		   and
2235  *	  (2)  must contain an operator which is in the index's operator family
2236  *		   for this column; and
2237  *	  (3)  must match the collation of the index, if collation is relevant.
2238  *
2239  *	  Our definition of "const" is exceedingly liberal: we allow anything that
2240  *	  doesn't involve a volatile function or a Var of the index's relation.
2241  *	  In particular, Vars belonging to other relations of the query are
2242  *	  accepted here, since a clause of that form can be used in a
2243  *	  parameterized indexscan.  It's the responsibility of higher code levels
2244  *	  to manage restriction and join clauses appropriately.
2245  *
2246  *	  Note: we do need to check for Vars of the index's relation on the
2247  *	  "const" side of the clause, since clauses like (a.f1 OP (b.f2 OP a.f3))
2248  *	  are not processable by a parameterized indexscan on a.f1, whereas
2249  *	  something like (a.f1 OP (b.f2 OP c.f3)) is.
2250  *
2251  *	  Presently, the executor can only deal with indexquals that have the
2252  *	  indexkey on the left, so we can only use clauses that have the indexkey
2253  *	  on the right if we can commute the clause to put the key on the left.
2254  *	  We handle that by generating an IndexClause with the correctly-commuted
2255  *	  opclause as a derived indexqual.
2256  *
2257  *	  If the index has a collation, the clause must have the same collation.
2258  *	  For collation-less indexes, we assume it doesn't matter; this is
2259  *	  necessary for cases like "hstore ? text", wherein hstore's operators
2260  *	  don't care about collation but the clause will get marked with a
2261  *	  collation anyway because of the text argument.  (This logic is
2262  *	  embodied in the macro IndexCollMatchesExprColl.)
2263  *
2264  *	  It is also possible to match RowCompareExpr clauses to indexes (but
2265  *	  currently, only btree indexes handle this).
2266  *
2267  *	  It is also possible to match ScalarArrayOpExpr clauses to indexes, when
2268  *	  the clause is of the form "indexkey op ANY (arrayconst)".
2269  *
2270  *	  For boolean indexes, it is also possible to match the clause directly
2271  *	  to the indexkey; or perhaps the clause is (NOT indexkey).
2272  *
2273  *	  And, last but not least, some operators and functions can be processed
2274  *	  to derive (typically lossy) indexquals from a clause that isn't in
2275  *	  itself indexable.  If we see that any operand of an OpExpr or FuncExpr
2276  *	  matches the index key, and the function has a planner support function
2277  *	  attached to it, we'll invoke the support function to see if such an
2278  *	  indexqual can be built.
2279  *
2280  * 'rinfo' is the clause to be tested (as a RestrictInfo node).
2281  * 'indexcol' is a column number of 'index' (counting from 0).
2282  * 'index' is the index of interest.
2283  *
2284  * Returns an IndexClause if the clause can be used with this index key,
2285  * or NULL if not.
2286  *
2287  * NOTE:  returns NULL if clause is an OR or AND clause; it is the
2288  * responsibility of higher-level routines to cope with those.
2289  */
2290 static IndexClause *
match_clause_to_indexcol(PlannerInfo * root,RestrictInfo * rinfo,int indexcol,IndexOptInfo * index)2291 match_clause_to_indexcol(PlannerInfo *root,
2292 						 RestrictInfo *rinfo,
2293 						 int indexcol,
2294 						 IndexOptInfo *index)
2295 {
2296 	IndexClause *iclause;
2297 	Expr	   *clause = rinfo->clause;
2298 	Oid			opfamily;
2299 
2300 	Assert(indexcol < index->nkeycolumns);
2301 
2302 	/*
2303 	 * Historically this code has coped with NULL clauses.  That's probably
2304 	 * not possible anymore, but we might as well continue to cope.
2305 	 */
2306 	if (clause == NULL)
2307 		return NULL;
2308 
2309 	/* First check for boolean-index cases. */
2310 	opfamily = index->opfamily[indexcol];
2311 	if (IsBooleanOpfamily(opfamily))
2312 	{
2313 		iclause = match_boolean_index_clause(root, rinfo, indexcol, index);
2314 		if (iclause)
2315 			return iclause;
2316 	}
2317 
2318 	/*
2319 	 * Clause must be an opclause, funcclause, ScalarArrayOpExpr, or
2320 	 * RowCompareExpr.  Or, if the index supports it, we can handle IS
2321 	 * NULL/NOT NULL clauses.
2322 	 */
2323 	if (IsA(clause, OpExpr))
2324 	{
2325 		return match_opclause_to_indexcol(root, rinfo, indexcol, index);
2326 	}
2327 	else if (IsA(clause, FuncExpr))
2328 	{
2329 		return match_funcclause_to_indexcol(root, rinfo, indexcol, index);
2330 	}
2331 	else if (IsA(clause, ScalarArrayOpExpr))
2332 	{
2333 		return match_saopclause_to_indexcol(root, rinfo, indexcol, index);
2334 	}
2335 	else if (IsA(clause, RowCompareExpr))
2336 	{
2337 		return match_rowcompare_to_indexcol(root, rinfo, indexcol, index);
2338 	}
2339 	else if (index->amsearchnulls && IsA(clause, NullTest))
2340 	{
2341 		NullTest   *nt = (NullTest *) clause;
2342 
2343 		if (!nt->argisrow &&
2344 			match_index_to_operand((Node *) nt->arg, indexcol, index))
2345 		{
2346 			iclause = makeNode(IndexClause);
2347 			iclause->rinfo = rinfo;
2348 			iclause->indexquals = list_make1(rinfo);
2349 			iclause->lossy = false;
2350 			iclause->indexcol = indexcol;
2351 			iclause->indexcols = NIL;
2352 			return iclause;
2353 		}
2354 	}
2355 
2356 	return NULL;
2357 }
2358 
2359 /*
2360  * match_boolean_index_clause
2361  *	  Recognize restriction clauses that can be matched to a boolean index.
2362  *
2363  * The idea here is that, for an index on a boolean column that supports the
2364  * BooleanEqualOperator, we can transform a plain reference to the indexkey
2365  * into "indexkey = true", or "NOT indexkey" into "indexkey = false", etc,
2366  * so as to make the expression indexable using the index's "=" operator.
2367  * Since Postgres 8.1, we must do this because constant simplification does
2368  * the reverse transformation; without this code there'd be no way to use
2369  * such an index at all.
2370  *
2371  * This should be called only when IsBooleanOpfamily() recognizes the
2372  * index's operator family.  We check to see if the clause matches the
2373  * index's key, and if so, build a suitable IndexClause.
2374  */
2375 static IndexClause *
match_boolean_index_clause(PlannerInfo * root,RestrictInfo * rinfo,int indexcol,IndexOptInfo * index)2376 match_boolean_index_clause(PlannerInfo *root,
2377 						   RestrictInfo *rinfo,
2378 						   int indexcol,
2379 						   IndexOptInfo *index)
2380 {
2381 	Node	   *clause = (Node *) rinfo->clause;
2382 	Expr	   *op = NULL;
2383 
2384 	/* Direct match? */
2385 	if (match_index_to_operand(clause, indexcol, index))
2386 	{
2387 		/* convert to indexkey = TRUE */
2388 		op = make_opclause(BooleanEqualOperator, BOOLOID, false,
2389 						   (Expr *) clause,
2390 						   (Expr *) makeBoolConst(true, false),
2391 						   InvalidOid, InvalidOid);
2392 	}
2393 	/* NOT clause? */
2394 	else if (is_notclause(clause))
2395 	{
2396 		Node	   *arg = (Node *) get_notclausearg((Expr *) clause);
2397 
2398 		if (match_index_to_operand(arg, indexcol, index))
2399 		{
2400 			/* convert to indexkey = FALSE */
2401 			op = make_opclause(BooleanEqualOperator, BOOLOID, false,
2402 							   (Expr *) arg,
2403 							   (Expr *) makeBoolConst(false, false),
2404 							   InvalidOid, InvalidOid);
2405 		}
2406 	}
2407 
2408 	/*
2409 	 * Since we only consider clauses at top level of WHERE, we can convert
2410 	 * indexkey IS TRUE and indexkey IS FALSE to index searches as well.  The
2411 	 * different meaning for NULL isn't important.
2412 	 */
2413 	else if (clause && IsA(clause, BooleanTest))
2414 	{
2415 		BooleanTest *btest = (BooleanTest *) clause;
2416 		Node	   *arg = (Node *) btest->arg;
2417 
2418 		if (btest->booltesttype == IS_TRUE &&
2419 			match_index_to_operand(arg, indexcol, index))
2420 		{
2421 			/* convert to indexkey = TRUE */
2422 			op = make_opclause(BooleanEqualOperator, BOOLOID, false,
2423 							   (Expr *) arg,
2424 							   (Expr *) makeBoolConst(true, false),
2425 							   InvalidOid, InvalidOid);
2426 		}
2427 		else if (btest->booltesttype == IS_FALSE &&
2428 				 match_index_to_operand(arg, indexcol, index))
2429 		{
2430 			/* convert to indexkey = FALSE */
2431 			op = make_opclause(BooleanEqualOperator, BOOLOID, false,
2432 							   (Expr *) arg,
2433 							   (Expr *) makeBoolConst(false, false),
2434 							   InvalidOid, InvalidOid);
2435 		}
2436 	}
2437 
2438 	/*
2439 	 * If we successfully made an operator clause from the given qual, we must
2440 	 * wrap it in an IndexClause.  It's not lossy.
2441 	 */
2442 	if (op)
2443 	{
2444 		IndexClause *iclause = makeNode(IndexClause);
2445 
2446 		iclause->rinfo = rinfo;
2447 		iclause->indexquals = list_make1(make_simple_restrictinfo(root, op));
2448 		iclause->lossy = false;
2449 		iclause->indexcol = indexcol;
2450 		iclause->indexcols = NIL;
2451 		return iclause;
2452 	}
2453 
2454 	return NULL;
2455 }
2456 
2457 /*
2458  * match_opclause_to_indexcol()
2459  *	  Handles the OpExpr case for match_clause_to_indexcol(),
2460  *	  which see for comments.
2461  */
2462 static IndexClause *
match_opclause_to_indexcol(PlannerInfo * root,RestrictInfo * rinfo,int indexcol,IndexOptInfo * index)2463 match_opclause_to_indexcol(PlannerInfo *root,
2464 						   RestrictInfo *rinfo,
2465 						   int indexcol,
2466 						   IndexOptInfo *index)
2467 {
2468 	IndexClause *iclause;
2469 	OpExpr	   *clause = (OpExpr *) rinfo->clause;
2470 	Node	   *leftop,
2471 			   *rightop;
2472 	Oid			expr_op;
2473 	Oid			expr_coll;
2474 	Index		index_relid;
2475 	Oid			opfamily;
2476 	Oid			idxcollation;
2477 
2478 	/*
2479 	 * Only binary operators need apply.  (In theory, a planner support
2480 	 * function could do something with a unary operator, but it seems
2481 	 * unlikely to be worth the cycles to check.)
2482 	 */
2483 	if (list_length(clause->args) != 2)
2484 		return NULL;
2485 
2486 	leftop = (Node *) linitial(clause->args);
2487 	rightop = (Node *) lsecond(clause->args);
2488 	expr_op = clause->opno;
2489 	expr_coll = clause->inputcollid;
2490 
2491 	index_relid = index->rel->relid;
2492 	opfamily = index->opfamily[indexcol];
2493 	idxcollation = index->indexcollations[indexcol];
2494 
2495 	/*
2496 	 * Check for clauses of the form: (indexkey operator constant) or
2497 	 * (constant operator indexkey).  See match_clause_to_indexcol's notes
2498 	 * about const-ness.
2499 	 *
2500 	 * Note that we don't ask the support function about clauses that don't
2501 	 * have one of these forms.  Again, in principle it might be possible to
2502 	 * do something, but it seems unlikely to be worth the cycles to check.
2503 	 */
2504 	if (match_index_to_operand(leftop, indexcol, index) &&
2505 		!bms_is_member(index_relid, rinfo->right_relids) &&
2506 		!contain_volatile_functions(rightop))
2507 	{
2508 		if (IndexCollMatchesExprColl(idxcollation, expr_coll) &&
2509 			op_in_opfamily(expr_op, opfamily))
2510 		{
2511 			iclause = makeNode(IndexClause);
2512 			iclause->rinfo = rinfo;
2513 			iclause->indexquals = list_make1(rinfo);
2514 			iclause->lossy = false;
2515 			iclause->indexcol = indexcol;
2516 			iclause->indexcols = NIL;
2517 			return iclause;
2518 		}
2519 
2520 		/*
2521 		 * If we didn't find a member of the index's opfamily, try the support
2522 		 * function for the operator's underlying function.
2523 		 */
2524 		set_opfuncid(clause);	/* make sure we have opfuncid */
2525 		return get_index_clause_from_support(root,
2526 											 rinfo,
2527 											 clause->opfuncid,
2528 											 0, /* indexarg on left */
2529 											 indexcol,
2530 											 index);
2531 	}
2532 
2533 	if (match_index_to_operand(rightop, indexcol, index) &&
2534 		!bms_is_member(index_relid, rinfo->left_relids) &&
2535 		!contain_volatile_functions(leftop))
2536 	{
2537 		if (IndexCollMatchesExprColl(idxcollation, expr_coll))
2538 		{
2539 			Oid			comm_op = get_commutator(expr_op);
2540 
2541 			if (OidIsValid(comm_op) &&
2542 				op_in_opfamily(comm_op, opfamily))
2543 			{
2544 				RestrictInfo *commrinfo;
2545 
2546 				/* Build a commuted OpExpr and RestrictInfo */
2547 				commrinfo = commute_restrictinfo(rinfo, comm_op);
2548 
2549 				/* Make an IndexClause showing that as a derived qual */
2550 				iclause = makeNode(IndexClause);
2551 				iclause->rinfo = rinfo;
2552 				iclause->indexquals = list_make1(commrinfo);
2553 				iclause->lossy = false;
2554 				iclause->indexcol = indexcol;
2555 				iclause->indexcols = NIL;
2556 				return iclause;
2557 			}
2558 		}
2559 
2560 		/*
2561 		 * If we didn't find a member of the index's opfamily, try the support
2562 		 * function for the operator's underlying function.
2563 		 */
2564 		set_opfuncid(clause);	/* make sure we have opfuncid */
2565 		return get_index_clause_from_support(root,
2566 											 rinfo,
2567 											 clause->opfuncid,
2568 											 1, /* indexarg on right */
2569 											 indexcol,
2570 											 index);
2571 	}
2572 
2573 	return NULL;
2574 }
2575 
2576 /*
2577  * match_funcclause_to_indexcol()
2578  *	  Handles the FuncExpr case for match_clause_to_indexcol(),
2579  *	  which see for comments.
2580  */
2581 static IndexClause *
match_funcclause_to_indexcol(PlannerInfo * root,RestrictInfo * rinfo,int indexcol,IndexOptInfo * index)2582 match_funcclause_to_indexcol(PlannerInfo *root,
2583 							 RestrictInfo *rinfo,
2584 							 int indexcol,
2585 							 IndexOptInfo *index)
2586 {
2587 	FuncExpr   *clause = (FuncExpr *) rinfo->clause;
2588 	int			indexarg;
2589 	ListCell   *lc;
2590 
2591 	/*
2592 	 * We have no built-in intelligence about function clauses, but if there's
2593 	 * a planner support function, it might be able to do something.  But, to
2594 	 * cut down on wasted planning cycles, only call the support function if
2595 	 * at least one argument matches the target index column.
2596 	 *
2597 	 * Note that we don't insist on the other arguments being pseudoconstants;
2598 	 * the support function has to check that.  This is to allow cases where
2599 	 * only some of the other arguments need to be included in the indexqual.
2600 	 */
2601 	indexarg = 0;
2602 	foreach(lc, clause->args)
2603 	{
2604 		Node	   *op = (Node *) lfirst(lc);
2605 
2606 		if (match_index_to_operand(op, indexcol, index))
2607 		{
2608 			return get_index_clause_from_support(root,
2609 												 rinfo,
2610 												 clause->funcid,
2611 												 indexarg,
2612 												 indexcol,
2613 												 index);
2614 		}
2615 
2616 		indexarg++;
2617 	}
2618 
2619 	return NULL;
2620 }
2621 
2622 /*
2623  * get_index_clause_from_support()
2624  *		If the function has a planner support function, try to construct
2625  *		an IndexClause using indexquals created by the support function.
2626  */
2627 static IndexClause *
get_index_clause_from_support(PlannerInfo * root,RestrictInfo * rinfo,Oid funcid,int indexarg,int indexcol,IndexOptInfo * index)2628 get_index_clause_from_support(PlannerInfo *root,
2629 							  RestrictInfo *rinfo,
2630 							  Oid funcid,
2631 							  int indexarg,
2632 							  int indexcol,
2633 							  IndexOptInfo *index)
2634 {
2635 	Oid			prosupport = get_func_support(funcid);
2636 	SupportRequestIndexCondition req;
2637 	List	   *sresult;
2638 
2639 	if (!OidIsValid(prosupport))
2640 		return NULL;
2641 
2642 	req.type = T_SupportRequestIndexCondition;
2643 	req.root = root;
2644 	req.funcid = funcid;
2645 	req.node = (Node *) rinfo->clause;
2646 	req.indexarg = indexarg;
2647 	req.index = index;
2648 	req.indexcol = indexcol;
2649 	req.opfamily = index->opfamily[indexcol];
2650 	req.indexcollation = index->indexcollations[indexcol];
2651 
2652 	req.lossy = true;			/* default assumption */
2653 
2654 	sresult = (List *)
2655 		DatumGetPointer(OidFunctionCall1(prosupport,
2656 										 PointerGetDatum(&req)));
2657 
2658 	if (sresult != NIL)
2659 	{
2660 		IndexClause *iclause = makeNode(IndexClause);
2661 		List	   *indexquals = NIL;
2662 		ListCell   *lc;
2663 
2664 		/*
2665 		 * The support function API says it should just give back bare
2666 		 * clauses, so here we must wrap each one in a RestrictInfo.
2667 		 */
2668 		foreach(lc, sresult)
2669 		{
2670 			Expr	   *clause = (Expr *) lfirst(lc);
2671 
2672 			indexquals = lappend(indexquals,
2673 								 make_simple_restrictinfo(root, clause));
2674 		}
2675 
2676 		iclause->rinfo = rinfo;
2677 		iclause->indexquals = indexquals;
2678 		iclause->lossy = req.lossy;
2679 		iclause->indexcol = indexcol;
2680 		iclause->indexcols = NIL;
2681 
2682 		return iclause;
2683 	}
2684 
2685 	return NULL;
2686 }
2687 
2688 /*
2689  * match_saopclause_to_indexcol()
2690  *	  Handles the ScalarArrayOpExpr case for match_clause_to_indexcol(),
2691  *	  which see for comments.
2692  */
2693 static IndexClause *
match_saopclause_to_indexcol(PlannerInfo * root,RestrictInfo * rinfo,int indexcol,IndexOptInfo * index)2694 match_saopclause_to_indexcol(PlannerInfo *root,
2695 							 RestrictInfo *rinfo,
2696 							 int indexcol,
2697 							 IndexOptInfo *index)
2698 {
2699 	ScalarArrayOpExpr *saop = (ScalarArrayOpExpr *) rinfo->clause;
2700 	Node	   *leftop,
2701 			   *rightop;
2702 	Relids		right_relids;
2703 	Oid			expr_op;
2704 	Oid			expr_coll;
2705 	Index		index_relid;
2706 	Oid			opfamily;
2707 	Oid			idxcollation;
2708 
2709 	/* We only accept ANY clauses, not ALL */
2710 	if (!saop->useOr)
2711 		return NULL;
2712 	leftop = (Node *) linitial(saop->args);
2713 	rightop = (Node *) lsecond(saop->args);
2714 	right_relids = pull_varnos(root, rightop);
2715 	expr_op = saop->opno;
2716 	expr_coll = saop->inputcollid;
2717 
2718 	index_relid = index->rel->relid;
2719 	opfamily = index->opfamily[indexcol];
2720 	idxcollation = index->indexcollations[indexcol];
2721 
2722 	/*
2723 	 * We must have indexkey on the left and a pseudo-constant array argument.
2724 	 */
2725 	if (match_index_to_operand(leftop, indexcol, index) &&
2726 		!bms_is_member(index_relid, right_relids) &&
2727 		!contain_volatile_functions(rightop))
2728 	{
2729 		if (IndexCollMatchesExprColl(idxcollation, expr_coll) &&
2730 			op_in_opfamily(expr_op, opfamily))
2731 		{
2732 			IndexClause *iclause = makeNode(IndexClause);
2733 
2734 			iclause->rinfo = rinfo;
2735 			iclause->indexquals = list_make1(rinfo);
2736 			iclause->lossy = false;
2737 			iclause->indexcol = indexcol;
2738 			iclause->indexcols = NIL;
2739 			return iclause;
2740 		}
2741 
2742 		/*
2743 		 * We do not currently ask support functions about ScalarArrayOpExprs,
2744 		 * though in principle we could.
2745 		 */
2746 	}
2747 
2748 	return NULL;
2749 }
2750 
2751 /*
2752  * match_rowcompare_to_indexcol()
2753  *	  Handles the RowCompareExpr case for match_clause_to_indexcol(),
2754  *	  which see for comments.
2755  *
2756  * In this routine we check whether the first column of the row comparison
2757  * matches the target index column.  This is sufficient to guarantee that some
2758  * index condition can be constructed from the RowCompareExpr --- the rest
2759  * is handled by expand_indexqual_rowcompare().
2760  */
2761 static IndexClause *
match_rowcompare_to_indexcol(PlannerInfo * root,RestrictInfo * rinfo,int indexcol,IndexOptInfo * index)2762 match_rowcompare_to_indexcol(PlannerInfo *root,
2763 							 RestrictInfo *rinfo,
2764 							 int indexcol,
2765 							 IndexOptInfo *index)
2766 {
2767 	RowCompareExpr *clause = (RowCompareExpr *) rinfo->clause;
2768 	Index		index_relid;
2769 	Oid			opfamily;
2770 	Oid			idxcollation;
2771 	Node	   *leftop,
2772 			   *rightop;
2773 	bool		var_on_left;
2774 	Oid			expr_op;
2775 	Oid			expr_coll;
2776 
2777 	/* Forget it if we're not dealing with a btree index */
2778 	if (index->relam != BTREE_AM_OID)
2779 		return NULL;
2780 
2781 	index_relid = index->rel->relid;
2782 	opfamily = index->opfamily[indexcol];
2783 	idxcollation = index->indexcollations[indexcol];
2784 
2785 	/*
2786 	 * We could do the matching on the basis of insisting that the opfamily
2787 	 * shown in the RowCompareExpr be the same as the index column's opfamily,
2788 	 * but that could fail in the presence of reverse-sort opfamilies: it'd be
2789 	 * a matter of chance whether RowCompareExpr had picked the forward or
2790 	 * reverse-sort family.  So look only at the operator, and match if it is
2791 	 * a member of the index's opfamily (after commutation, if the indexkey is
2792 	 * on the right).  We'll worry later about whether any additional
2793 	 * operators are matchable to the index.
2794 	 */
2795 	leftop = (Node *) linitial(clause->largs);
2796 	rightop = (Node *) linitial(clause->rargs);
2797 	expr_op = linitial_oid(clause->opnos);
2798 	expr_coll = linitial_oid(clause->inputcollids);
2799 
2800 	/* Collations must match, if relevant */
2801 	if (!IndexCollMatchesExprColl(idxcollation, expr_coll))
2802 		return NULL;
2803 
2804 	/*
2805 	 * These syntactic tests are the same as in match_opclause_to_indexcol()
2806 	 */
2807 	if (match_index_to_operand(leftop, indexcol, index) &&
2808 		!bms_is_member(index_relid, pull_varnos(root, rightop)) &&
2809 		!contain_volatile_functions(rightop))
2810 	{
2811 		/* OK, indexkey is on left */
2812 		var_on_left = true;
2813 	}
2814 	else if (match_index_to_operand(rightop, indexcol, index) &&
2815 			 !bms_is_member(index_relid, pull_varnos(root, leftop)) &&
2816 			 !contain_volatile_functions(leftop))
2817 	{
2818 		/* indexkey is on right, so commute the operator */
2819 		expr_op = get_commutator(expr_op);
2820 		if (expr_op == InvalidOid)
2821 			return NULL;
2822 		var_on_left = false;
2823 	}
2824 	else
2825 		return NULL;
2826 
2827 	/* We're good if the operator is the right type of opfamily member */
2828 	switch (get_op_opfamily_strategy(expr_op, opfamily))
2829 	{
2830 		case BTLessStrategyNumber:
2831 		case BTLessEqualStrategyNumber:
2832 		case BTGreaterEqualStrategyNumber:
2833 		case BTGreaterStrategyNumber:
2834 			return expand_indexqual_rowcompare(root,
2835 											   rinfo,
2836 											   indexcol,
2837 											   index,
2838 											   expr_op,
2839 											   var_on_left);
2840 	}
2841 
2842 	return NULL;
2843 }
2844 
2845 /*
2846  * expand_indexqual_rowcompare --- expand a single indexqual condition
2847  *		that is a RowCompareExpr
2848  *
2849  * It's already known that the first column of the row comparison matches
2850  * the specified column of the index.  We can use additional columns of the
2851  * row comparison as index qualifications, so long as they match the index
2852  * in the "same direction", ie, the indexkeys are all on the same side of the
2853  * clause and the operators are all the same-type members of the opfamilies.
2854  *
2855  * If all the columns of the RowCompareExpr match in this way, we just use it
2856  * as-is, except for possibly commuting it to put the indexkeys on the left.
2857  *
2858  * Otherwise, we build a shortened RowCompareExpr (if more than one
2859  * column matches) or a simple OpExpr (if the first-column match is all
2860  * there is).  In these cases the modified clause is always "<=" or ">="
2861  * even when the original was "<" or ">" --- this is necessary to match all
2862  * the rows that could match the original.  (We are building a lossy version
2863  * of the row comparison when we do this, so we set lossy = true.)
2864  *
2865  * Note: this is really just the last half of match_rowcompare_to_indexcol,
2866  * but we split it out for comprehensibility.
2867  */
2868 static IndexClause *
expand_indexqual_rowcompare(PlannerInfo * root,RestrictInfo * rinfo,int indexcol,IndexOptInfo * index,Oid expr_op,bool var_on_left)2869 expand_indexqual_rowcompare(PlannerInfo *root,
2870 							RestrictInfo *rinfo,
2871 							int indexcol,
2872 							IndexOptInfo *index,
2873 							Oid expr_op,
2874 							bool var_on_left)
2875 {
2876 	IndexClause *iclause = makeNode(IndexClause);
2877 	RowCompareExpr *clause = (RowCompareExpr *) rinfo->clause;
2878 	int			op_strategy;
2879 	Oid			op_lefttype;
2880 	Oid			op_righttype;
2881 	int			matching_cols;
2882 	List	   *expr_ops;
2883 	List	   *opfamilies;
2884 	List	   *lefttypes;
2885 	List	   *righttypes;
2886 	List	   *new_ops;
2887 	List	   *var_args;
2888 	List	   *non_var_args;
2889 
2890 	iclause->rinfo = rinfo;
2891 	iclause->indexcol = indexcol;
2892 
2893 	if (var_on_left)
2894 	{
2895 		var_args = clause->largs;
2896 		non_var_args = clause->rargs;
2897 	}
2898 	else
2899 	{
2900 		var_args = clause->rargs;
2901 		non_var_args = clause->largs;
2902 	}
2903 
2904 	get_op_opfamily_properties(expr_op, index->opfamily[indexcol], false,
2905 							   &op_strategy,
2906 							   &op_lefttype,
2907 							   &op_righttype);
2908 
2909 	/* Initialize returned list of which index columns are used */
2910 	iclause->indexcols = list_make1_int(indexcol);
2911 
2912 	/* Build lists of ops, opfamilies and operator datatypes in case needed */
2913 	expr_ops = list_make1_oid(expr_op);
2914 	opfamilies = list_make1_oid(index->opfamily[indexcol]);
2915 	lefttypes = list_make1_oid(op_lefttype);
2916 	righttypes = list_make1_oid(op_righttype);
2917 
2918 	/*
2919 	 * See how many of the remaining columns match some index column in the
2920 	 * same way.  As in match_clause_to_indexcol(), the "other" side of any
2921 	 * potential index condition is OK as long as it doesn't use Vars from the
2922 	 * indexed relation.
2923 	 */
2924 	matching_cols = 1;
2925 
2926 	while (matching_cols < list_length(var_args))
2927 	{
2928 		Node	   *varop = (Node *) list_nth(var_args, matching_cols);
2929 		Node	   *constop = (Node *) list_nth(non_var_args, matching_cols);
2930 		int			i;
2931 
2932 		expr_op = list_nth_oid(clause->opnos, matching_cols);
2933 		if (!var_on_left)
2934 		{
2935 			/* indexkey is on right, so commute the operator */
2936 			expr_op = get_commutator(expr_op);
2937 			if (expr_op == InvalidOid)
2938 				break;			/* operator is not usable */
2939 		}
2940 		if (bms_is_member(index->rel->relid, pull_varnos(root, constop)))
2941 			break;				/* no good, Var on wrong side */
2942 		if (contain_volatile_functions(constop))
2943 			break;				/* no good, volatile comparison value */
2944 
2945 		/*
2946 		 * The Var side can match any key column of the index.
2947 		 */
2948 		for (i = 0; i < index->nkeycolumns; i++)
2949 		{
2950 			if (match_index_to_operand(varop, i, index) &&
2951 				get_op_opfamily_strategy(expr_op,
2952 										 index->opfamily[i]) == op_strategy &&
2953 				IndexCollMatchesExprColl(index->indexcollations[i],
2954 										 list_nth_oid(clause->inputcollids,
2955 													  matching_cols)))
2956 				break;
2957 		}
2958 		if (i >= index->nkeycolumns)
2959 			break;				/* no match found */
2960 
2961 		/* Add column number to returned list */
2962 		iclause->indexcols = lappend_int(iclause->indexcols, i);
2963 
2964 		/* Add operator info to lists */
2965 		get_op_opfamily_properties(expr_op, index->opfamily[i], false,
2966 								   &op_strategy,
2967 								   &op_lefttype,
2968 								   &op_righttype);
2969 		expr_ops = lappend_oid(expr_ops, expr_op);
2970 		opfamilies = lappend_oid(opfamilies, index->opfamily[i]);
2971 		lefttypes = lappend_oid(lefttypes, op_lefttype);
2972 		righttypes = lappend_oid(righttypes, op_righttype);
2973 
2974 		/* This column matches, keep scanning */
2975 		matching_cols++;
2976 	}
2977 
2978 	/* Result is non-lossy if all columns are usable as index quals */
2979 	iclause->lossy = (matching_cols != list_length(clause->opnos));
2980 
2981 	/*
2982 	 * We can use rinfo->clause as-is if we have var on left and it's all
2983 	 * usable as index quals.
2984 	 */
2985 	if (var_on_left && !iclause->lossy)
2986 		iclause->indexquals = list_make1(rinfo);
2987 	else
2988 	{
2989 		/*
2990 		 * We have to generate a modified rowcompare (possibly just one
2991 		 * OpExpr).  The painful part of this is changing < to <= or > to >=,
2992 		 * so deal with that first.
2993 		 */
2994 		if (!iclause->lossy)
2995 		{
2996 			/* very easy, just use the commuted operators */
2997 			new_ops = expr_ops;
2998 		}
2999 		else if (op_strategy == BTLessEqualStrategyNumber ||
3000 				 op_strategy == BTGreaterEqualStrategyNumber)
3001 		{
3002 			/* easy, just use the same (possibly commuted) operators */
3003 			new_ops = list_truncate(expr_ops, matching_cols);
3004 		}
3005 		else
3006 		{
3007 			ListCell   *opfamilies_cell;
3008 			ListCell   *lefttypes_cell;
3009 			ListCell   *righttypes_cell;
3010 
3011 			if (op_strategy == BTLessStrategyNumber)
3012 				op_strategy = BTLessEqualStrategyNumber;
3013 			else if (op_strategy == BTGreaterStrategyNumber)
3014 				op_strategy = BTGreaterEqualStrategyNumber;
3015 			else
3016 				elog(ERROR, "unexpected strategy number %d", op_strategy);
3017 			new_ops = NIL;
3018 			forthree(opfamilies_cell, opfamilies,
3019 					 lefttypes_cell, lefttypes,
3020 					 righttypes_cell, righttypes)
3021 			{
3022 				Oid			opfam = lfirst_oid(opfamilies_cell);
3023 				Oid			lefttype = lfirst_oid(lefttypes_cell);
3024 				Oid			righttype = lfirst_oid(righttypes_cell);
3025 
3026 				expr_op = get_opfamily_member(opfam, lefttype, righttype,
3027 											  op_strategy);
3028 				if (!OidIsValid(expr_op))	/* should not happen */
3029 					elog(ERROR, "missing operator %d(%u,%u) in opfamily %u",
3030 						 op_strategy, lefttype, righttype, opfam);
3031 				new_ops = lappend_oid(new_ops, expr_op);
3032 			}
3033 		}
3034 
3035 		/* If we have more than one matching col, create a subset rowcompare */
3036 		if (matching_cols > 1)
3037 		{
3038 			RowCompareExpr *rc = makeNode(RowCompareExpr);
3039 
3040 			rc->rctype = (RowCompareType) op_strategy;
3041 			rc->opnos = new_ops;
3042 			rc->opfamilies = list_truncate(list_copy(clause->opfamilies),
3043 										   matching_cols);
3044 			rc->inputcollids = list_truncate(list_copy(clause->inputcollids),
3045 											 matching_cols);
3046 			rc->largs = list_truncate(copyObject(var_args),
3047 									  matching_cols);
3048 			rc->rargs = list_truncate(copyObject(non_var_args),
3049 									  matching_cols);
3050 			iclause->indexquals = list_make1(make_simple_restrictinfo(root,
3051 																	  (Expr *) rc));
3052 		}
3053 		else
3054 		{
3055 			Expr	   *op;
3056 
3057 			/* We don't report an index column list in this case */
3058 			iclause->indexcols = NIL;
3059 
3060 			op = make_opclause(linitial_oid(new_ops), BOOLOID, false,
3061 							   copyObject(linitial(var_args)),
3062 							   copyObject(linitial(non_var_args)),
3063 							   InvalidOid,
3064 							   linitial_oid(clause->inputcollids));
3065 			iclause->indexquals = list_make1(make_simple_restrictinfo(root, op));
3066 		}
3067 	}
3068 
3069 	return iclause;
3070 }
3071 
3072 
3073 /****************************************************************************
3074  *				----  ROUTINES TO CHECK ORDERING OPERATORS	----
3075  ****************************************************************************/
3076 
3077 /*
3078  * match_pathkeys_to_index
3079  *		Test whether an index can produce output ordered according to the
3080  *		given pathkeys using "ordering operators".
3081  *
3082  * If it can, return a list of suitable ORDER BY expressions, each of the form
3083  * "indexedcol operator pseudoconstant", along with an integer list of the
3084  * index column numbers (zero based) that each clause would be used with.
3085  * NIL lists are returned if the ordering is not achievable this way.
3086  *
3087  * On success, the result list is ordered by pathkeys, and in fact is
3088  * one-to-one with the requested pathkeys.
3089  */
3090 static void
match_pathkeys_to_index(IndexOptInfo * index,List * pathkeys,List ** orderby_clauses_p,List ** clause_columns_p)3091 match_pathkeys_to_index(IndexOptInfo *index, List *pathkeys,
3092 						List **orderby_clauses_p,
3093 						List **clause_columns_p)
3094 {
3095 	List	   *orderby_clauses = NIL;
3096 	List	   *clause_columns = NIL;
3097 	ListCell   *lc1;
3098 
3099 	*orderby_clauses_p = NIL;	/* set default results */
3100 	*clause_columns_p = NIL;
3101 
3102 	/* Only indexes with the amcanorderbyop property are interesting here */
3103 	if (!index->amcanorderbyop)
3104 		return;
3105 
3106 	foreach(lc1, pathkeys)
3107 	{
3108 		PathKey    *pathkey = (PathKey *) lfirst(lc1);
3109 		bool		found = false;
3110 		ListCell   *lc2;
3111 
3112 		/*
3113 		 * Note: for any failure to match, we just return NIL immediately.
3114 		 * There is no value in matching just some of the pathkeys.
3115 		 */
3116 
3117 		/* Pathkey must request default sort order for the target opfamily */
3118 		if (pathkey->pk_strategy != BTLessStrategyNumber ||
3119 			pathkey->pk_nulls_first)
3120 			return;
3121 
3122 		/* If eclass is volatile, no hope of using an indexscan */
3123 		if (pathkey->pk_eclass->ec_has_volatile)
3124 			return;
3125 
3126 		/*
3127 		 * Try to match eclass member expression(s) to index.  Note that child
3128 		 * EC members are considered, but only when they belong to the target
3129 		 * relation.  (Unlike regular members, the same expression could be a
3130 		 * child member of more than one EC.  Therefore, the same index could
3131 		 * be considered to match more than one pathkey list, which is OK
3132 		 * here.  See also get_eclass_for_sort_expr.)
3133 		 */
3134 		foreach(lc2, pathkey->pk_eclass->ec_members)
3135 		{
3136 			EquivalenceMember *member = (EquivalenceMember *) lfirst(lc2);
3137 			int			indexcol;
3138 
3139 			/* No possibility of match if it references other relations */
3140 			if (!bms_equal(member->em_relids, index->rel->relids))
3141 				continue;
3142 
3143 			/*
3144 			 * We allow any column of the index to match each pathkey; they
3145 			 * don't have to match left-to-right as you might expect.  This is
3146 			 * correct for GiST, and it doesn't matter for SP-GiST because
3147 			 * that doesn't handle multiple columns anyway, and no other
3148 			 * existing AMs support amcanorderbyop.  We might need different
3149 			 * logic in future for other implementations.
3150 			 */
3151 			for (indexcol = 0; indexcol < index->nkeycolumns; indexcol++)
3152 			{
3153 				Expr	   *expr;
3154 
3155 				expr = match_clause_to_ordering_op(index,
3156 												   indexcol,
3157 												   member->em_expr,
3158 												   pathkey->pk_opfamily);
3159 				if (expr)
3160 				{
3161 					orderby_clauses = lappend(orderby_clauses, expr);
3162 					clause_columns = lappend_int(clause_columns, indexcol);
3163 					found = true;
3164 					break;
3165 				}
3166 			}
3167 
3168 			if (found)			/* don't want to look at remaining members */
3169 				break;
3170 		}
3171 
3172 		if (!found)				/* fail if no match for this pathkey */
3173 			return;
3174 	}
3175 
3176 	*orderby_clauses_p = orderby_clauses;	/* success! */
3177 	*clause_columns_p = clause_columns;
3178 }
3179 
3180 /*
3181  * match_clause_to_ordering_op
3182  *	  Determines whether an ordering operator expression matches an
3183  *	  index column.
3184  *
3185  *	  This is similar to, but simpler than, match_clause_to_indexcol.
3186  *	  We only care about simple OpExpr cases.  The input is a bare
3187  *	  expression that is being ordered by, which must be of the form
3188  *	  (indexkey op const) or (const op indexkey) where op is an ordering
3189  *	  operator for the column's opfamily.
3190  *
3191  * 'index' is the index of interest.
3192  * 'indexcol' is a column number of 'index' (counting from 0).
3193  * 'clause' is the ordering expression to be tested.
3194  * 'pk_opfamily' is the btree opfamily describing the required sort order.
3195  *
3196  * Note that we currently do not consider the collation of the ordering
3197  * operator's result.  In practical cases the result type will be numeric
3198  * and thus have no collation, and it's not very clear what to match to
3199  * if it did have a collation.  The index's collation should match the
3200  * ordering operator's input collation, not its result.
3201  *
3202  * If successful, return 'clause' as-is if the indexkey is on the left,
3203  * otherwise a commuted copy of 'clause'.  If no match, return NULL.
3204  */
3205 static Expr *
match_clause_to_ordering_op(IndexOptInfo * index,int indexcol,Expr * clause,Oid pk_opfamily)3206 match_clause_to_ordering_op(IndexOptInfo *index,
3207 							int indexcol,
3208 							Expr *clause,
3209 							Oid pk_opfamily)
3210 {
3211 	Oid			opfamily;
3212 	Oid			idxcollation;
3213 	Node	   *leftop,
3214 			   *rightop;
3215 	Oid			expr_op;
3216 	Oid			expr_coll;
3217 	Oid			sortfamily;
3218 	bool		commuted;
3219 
3220 	Assert(indexcol < index->nkeycolumns);
3221 
3222 	opfamily = index->opfamily[indexcol];
3223 	idxcollation = index->indexcollations[indexcol];
3224 
3225 	/*
3226 	 * Clause must be a binary opclause.
3227 	 */
3228 	if (!is_opclause(clause))
3229 		return NULL;
3230 	leftop = get_leftop(clause);
3231 	rightop = get_rightop(clause);
3232 	if (!leftop || !rightop)
3233 		return NULL;
3234 	expr_op = ((OpExpr *) clause)->opno;
3235 	expr_coll = ((OpExpr *) clause)->inputcollid;
3236 
3237 	/*
3238 	 * We can forget the whole thing right away if wrong collation.
3239 	 */
3240 	if (!IndexCollMatchesExprColl(idxcollation, expr_coll))
3241 		return NULL;
3242 
3243 	/*
3244 	 * Check for clauses of the form: (indexkey operator constant) or
3245 	 * (constant operator indexkey).
3246 	 */
3247 	if (match_index_to_operand(leftop, indexcol, index) &&
3248 		!contain_var_clause(rightop) &&
3249 		!contain_volatile_functions(rightop))
3250 	{
3251 		commuted = false;
3252 	}
3253 	else if (match_index_to_operand(rightop, indexcol, index) &&
3254 			 !contain_var_clause(leftop) &&
3255 			 !contain_volatile_functions(leftop))
3256 	{
3257 		/* Might match, but we need a commuted operator */
3258 		expr_op = get_commutator(expr_op);
3259 		if (expr_op == InvalidOid)
3260 			return NULL;
3261 		commuted = true;
3262 	}
3263 	else
3264 		return NULL;
3265 
3266 	/*
3267 	 * Is the (commuted) operator an ordering operator for the opfamily? And
3268 	 * if so, does it yield the right sorting semantics?
3269 	 */
3270 	sortfamily = get_op_opfamily_sortfamily(expr_op, opfamily);
3271 	if (sortfamily != pk_opfamily)
3272 		return NULL;
3273 
3274 	/* We have a match.  Return clause or a commuted version thereof. */
3275 	if (commuted)
3276 	{
3277 		OpExpr	   *newclause = makeNode(OpExpr);
3278 
3279 		/* flat-copy all the fields of clause */
3280 		memcpy(newclause, clause, sizeof(OpExpr));
3281 
3282 		/* commute it */
3283 		newclause->opno = expr_op;
3284 		newclause->opfuncid = InvalidOid;
3285 		newclause->args = list_make2(rightop, leftop);
3286 
3287 		clause = (Expr *) newclause;
3288 	}
3289 
3290 	return clause;
3291 }
3292 
3293 
3294 /****************************************************************************
3295  *				----  ROUTINES TO DO PARTIAL INDEX PREDICATE TESTS	----
3296  ****************************************************************************/
3297 
3298 /*
3299  * check_index_predicates
3300  *		Set the predicate-derived IndexOptInfo fields for each index
3301  *		of the specified relation.
3302  *
3303  * predOK is set true if the index is partial and its predicate is satisfied
3304  * for this query, ie the query's WHERE clauses imply the predicate.
3305  *
3306  * indrestrictinfo is set to the relation's baserestrictinfo list less any
3307  * conditions that are implied by the index's predicate.  (Obviously, for a
3308  * non-partial index, this is the same as baserestrictinfo.)  Such conditions
3309  * can be dropped from the plan when using the index, in certain cases.
3310  *
3311  * At one time it was possible for this to get re-run after adding more
3312  * restrictions to the rel, thus possibly letting us prove more indexes OK.
3313  * That doesn't happen any more (at least not in the core code's usage),
3314  * but this code still supports it in case extensions want to mess with the
3315  * baserestrictinfo list.  We assume that adding more restrictions can't make
3316  * an index not predOK.  We must recompute indrestrictinfo each time, though,
3317  * to make sure any newly-added restrictions get into it if needed.
3318  */
3319 void
check_index_predicates(PlannerInfo * root,RelOptInfo * rel)3320 check_index_predicates(PlannerInfo *root, RelOptInfo *rel)
3321 {
3322 	List	   *clauselist;
3323 	bool		have_partial;
3324 	bool		is_target_rel;
3325 	Relids		otherrels;
3326 	ListCell   *lc;
3327 
3328 	/* Indexes are available only on base or "other" member relations. */
3329 	Assert(IS_SIMPLE_REL(rel));
3330 
3331 	/*
3332 	 * Initialize the indrestrictinfo lists to be identical to
3333 	 * baserestrictinfo, and check whether there are any partial indexes.  If
3334 	 * not, this is all we need to do.
3335 	 */
3336 	have_partial = false;
3337 	foreach(lc, rel->indexlist)
3338 	{
3339 		IndexOptInfo *index = (IndexOptInfo *) lfirst(lc);
3340 
3341 		index->indrestrictinfo = rel->baserestrictinfo;
3342 		if (index->indpred)
3343 			have_partial = true;
3344 	}
3345 	if (!have_partial)
3346 		return;
3347 
3348 	/*
3349 	 * Construct a list of clauses that we can assume true for the purpose of
3350 	 * proving the index(es) usable.  Restriction clauses for the rel are
3351 	 * always usable, and so are any join clauses that are "movable to" this
3352 	 * rel.  Also, we can consider any EC-derivable join clauses (which must
3353 	 * be "movable to" this rel, by definition).
3354 	 */
3355 	clauselist = list_copy(rel->baserestrictinfo);
3356 
3357 	/* Scan the rel's join clauses */
3358 	foreach(lc, rel->joininfo)
3359 	{
3360 		RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
3361 
3362 		/* Check if clause can be moved to this rel */
3363 		if (!join_clause_is_movable_to(rinfo, rel))
3364 			continue;
3365 
3366 		clauselist = lappend(clauselist, rinfo);
3367 	}
3368 
3369 	/*
3370 	 * Add on any equivalence-derivable join clauses.  Computing the correct
3371 	 * relid sets for generate_join_implied_equalities is slightly tricky
3372 	 * because the rel could be a child rel rather than a true baserel, and in
3373 	 * that case we must remove its parents' relid(s) from all_baserels.
3374 	 */
3375 	if (rel->reloptkind == RELOPT_OTHER_MEMBER_REL)
3376 		otherrels = bms_difference(root->all_baserels,
3377 								   find_childrel_parents(root, rel));
3378 	else
3379 		otherrels = bms_difference(root->all_baserels, rel->relids);
3380 
3381 	if (!bms_is_empty(otherrels))
3382 		clauselist =
3383 			list_concat(clauselist,
3384 						generate_join_implied_equalities(root,
3385 														 bms_union(rel->relids,
3386 																   otherrels),
3387 														 otherrels,
3388 														 rel));
3389 
3390 	/*
3391 	 * Normally we remove quals that are implied by a partial index's
3392 	 * predicate from indrestrictinfo, indicating that they need not be
3393 	 * checked explicitly by an indexscan plan using this index.  However, if
3394 	 * the rel is a target relation of UPDATE/DELETE/SELECT FOR UPDATE, we
3395 	 * cannot remove such quals from the plan, because they need to be in the
3396 	 * plan so that they will be properly rechecked by EvalPlanQual testing.
3397 	 * Some day we might want to remove such quals from the main plan anyway
3398 	 * and pass them through to EvalPlanQual via a side channel; but for now,
3399 	 * we just don't remove implied quals at all for target relations.
3400 	 */
3401 	is_target_rel = (bms_is_member(rel->relid, root->all_result_relids) ||
3402 					 get_plan_rowmark(root->rowMarks, rel->relid) != NULL);
3403 
3404 	/*
3405 	 * Now try to prove each index predicate true, and compute the
3406 	 * indrestrictinfo lists for partial indexes.  Note that we compute the
3407 	 * indrestrictinfo list even for non-predOK indexes; this might seem
3408 	 * wasteful, but we may be able to use such indexes in OR clauses, cf
3409 	 * generate_bitmap_or_paths().
3410 	 */
3411 	foreach(lc, rel->indexlist)
3412 	{
3413 		IndexOptInfo *index = (IndexOptInfo *) lfirst(lc);
3414 		ListCell   *lcr;
3415 
3416 		if (index->indpred == NIL)
3417 			continue;			/* ignore non-partial indexes here */
3418 
3419 		if (!index->predOK)		/* don't repeat work if already proven OK */
3420 			index->predOK = predicate_implied_by(index->indpred, clauselist,
3421 												 false);
3422 
3423 		/* If rel is an update target, leave indrestrictinfo as set above */
3424 		if (is_target_rel)
3425 			continue;
3426 
3427 		/* Else compute indrestrictinfo as the non-implied quals */
3428 		index->indrestrictinfo = NIL;
3429 		foreach(lcr, rel->baserestrictinfo)
3430 		{
3431 			RestrictInfo *rinfo = (RestrictInfo *) lfirst(lcr);
3432 
3433 			/* predicate_implied_by() assumes first arg is immutable */
3434 			if (contain_mutable_functions((Node *) rinfo->clause) ||
3435 				!predicate_implied_by(list_make1(rinfo->clause),
3436 									  index->indpred, false))
3437 				index->indrestrictinfo = lappend(index->indrestrictinfo, rinfo);
3438 		}
3439 	}
3440 }
3441 
3442 /****************************************************************************
3443  *				----  ROUTINES TO CHECK EXTERNALLY-VISIBLE CONDITIONS  ----
3444  ****************************************************************************/
3445 
3446 /*
3447  * ec_member_matches_indexcol
3448  *	  Test whether an EquivalenceClass member matches an index column.
3449  *
3450  * This is a callback for use by generate_implied_equalities_for_column.
3451  */
3452 static bool
ec_member_matches_indexcol(PlannerInfo * root,RelOptInfo * rel,EquivalenceClass * ec,EquivalenceMember * em,void * arg)3453 ec_member_matches_indexcol(PlannerInfo *root, RelOptInfo *rel,
3454 						   EquivalenceClass *ec, EquivalenceMember *em,
3455 						   void *arg)
3456 {
3457 	IndexOptInfo *index = ((ec_member_matches_arg *) arg)->index;
3458 	int			indexcol = ((ec_member_matches_arg *) arg)->indexcol;
3459 	Oid			curFamily;
3460 	Oid			curCollation;
3461 
3462 	Assert(indexcol < index->nkeycolumns);
3463 
3464 	curFamily = index->opfamily[indexcol];
3465 	curCollation = index->indexcollations[indexcol];
3466 
3467 	/*
3468 	 * If it's a btree index, we can reject it if its opfamily isn't
3469 	 * compatible with the EC, since no clause generated from the EC could be
3470 	 * used with the index.  For non-btree indexes, we can't easily tell
3471 	 * whether clauses generated from the EC could be used with the index, so
3472 	 * don't check the opfamily.  This might mean we return "true" for a
3473 	 * useless EC, so we have to recheck the results of
3474 	 * generate_implied_equalities_for_column; see
3475 	 * match_eclass_clauses_to_index.
3476 	 */
3477 	if (index->relam == BTREE_AM_OID &&
3478 		!list_member_oid(ec->ec_opfamilies, curFamily))
3479 		return false;
3480 
3481 	/* We insist on collation match for all index types, though */
3482 	if (!IndexCollMatchesExprColl(curCollation, ec->ec_collation))
3483 		return false;
3484 
3485 	return match_index_to_operand((Node *) em->em_expr, indexcol, index);
3486 }
3487 
3488 /*
3489  * relation_has_unique_index_for
3490  *	  Determine whether the relation provably has at most one row satisfying
3491  *	  a set of equality conditions, because the conditions constrain all
3492  *	  columns of some unique index.
3493  *
3494  * The conditions can be represented in either or both of two ways:
3495  * 1. A list of RestrictInfo nodes, where the caller has already determined
3496  * that each condition is a mergejoinable equality with an expression in
3497  * this relation on one side, and an expression not involving this relation
3498  * on the other.  The transient outer_is_left flag is used to identify which
3499  * side we should look at: left side if outer_is_left is false, right side
3500  * if it is true.
3501  * 2. A list of expressions in this relation, and a corresponding list of
3502  * equality operators. The caller must have already checked that the operators
3503  * represent equality.  (Note: the operators could be cross-type; the
3504  * expressions should correspond to their RHS inputs.)
3505  *
3506  * The caller need only supply equality conditions arising from joins;
3507  * this routine automatically adds in any usable baserestrictinfo clauses.
3508  * (Note that the passed-in restrictlist will be destructively modified!)
3509  */
3510 bool
relation_has_unique_index_for(PlannerInfo * root,RelOptInfo * rel,List * restrictlist,List * exprlist,List * oprlist)3511 relation_has_unique_index_for(PlannerInfo *root, RelOptInfo *rel,
3512 							  List *restrictlist,
3513 							  List *exprlist, List *oprlist)
3514 {
3515 	ListCell   *ic;
3516 
3517 	Assert(list_length(exprlist) == list_length(oprlist));
3518 
3519 	/* Short-circuit if no indexes... */
3520 	if (rel->indexlist == NIL)
3521 		return false;
3522 
3523 	/*
3524 	 * Examine the rel's restriction clauses for usable var = const clauses
3525 	 * that we can add to the restrictlist.
3526 	 */
3527 	foreach(ic, rel->baserestrictinfo)
3528 	{
3529 		RestrictInfo *restrictinfo = (RestrictInfo *) lfirst(ic);
3530 
3531 		/*
3532 		 * Note: can_join won't be set for a restriction clause, but
3533 		 * mergeopfamilies will be if it has a mergejoinable operator and
3534 		 * doesn't contain volatile functions.
3535 		 */
3536 		if (restrictinfo->mergeopfamilies == NIL)
3537 			continue;			/* not mergejoinable */
3538 
3539 		/*
3540 		 * The clause certainly doesn't refer to anything but the given rel.
3541 		 * If either side is pseudoconstant then we can use it.
3542 		 */
3543 		if (bms_is_empty(restrictinfo->left_relids))
3544 		{
3545 			/* righthand side is inner */
3546 			restrictinfo->outer_is_left = true;
3547 		}
3548 		else if (bms_is_empty(restrictinfo->right_relids))
3549 		{
3550 			/* lefthand side is inner */
3551 			restrictinfo->outer_is_left = false;
3552 		}
3553 		else
3554 			continue;
3555 
3556 		/* OK, add to list */
3557 		restrictlist = lappend(restrictlist, restrictinfo);
3558 	}
3559 
3560 	/* Short-circuit the easy case */
3561 	if (restrictlist == NIL && exprlist == NIL)
3562 		return false;
3563 
3564 	/* Examine each index of the relation ... */
3565 	foreach(ic, rel->indexlist)
3566 	{
3567 		IndexOptInfo *ind = (IndexOptInfo *) lfirst(ic);
3568 		int			c;
3569 
3570 		/*
3571 		 * If the index is not unique, or not immediately enforced, or if it's
3572 		 * a partial index that doesn't match the query, it's useless here.
3573 		 */
3574 		if (!ind->unique || !ind->immediate ||
3575 			(ind->indpred != NIL && !ind->predOK))
3576 			continue;
3577 
3578 		/*
3579 		 * Try to find each index column in the lists of conditions.  This is
3580 		 * O(N^2) or worse, but we expect all the lists to be short.
3581 		 */
3582 		for (c = 0; c < ind->nkeycolumns; c++)
3583 		{
3584 			bool		matched = false;
3585 			ListCell   *lc;
3586 			ListCell   *lc2;
3587 
3588 			foreach(lc, restrictlist)
3589 			{
3590 				RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
3591 				Node	   *rexpr;
3592 
3593 				/*
3594 				 * The condition's equality operator must be a member of the
3595 				 * index opfamily, else it is not asserting the right kind of
3596 				 * equality behavior for this index.  We check this first
3597 				 * since it's probably cheaper than match_index_to_operand().
3598 				 */
3599 				if (!list_member_oid(rinfo->mergeopfamilies, ind->opfamily[c]))
3600 					continue;
3601 
3602 				/*
3603 				 * XXX at some point we may need to check collations here too.
3604 				 * For the moment we assume all collations reduce to the same
3605 				 * notion of equality.
3606 				 */
3607 
3608 				/* OK, see if the condition operand matches the index key */
3609 				if (rinfo->outer_is_left)
3610 					rexpr = get_rightop(rinfo->clause);
3611 				else
3612 					rexpr = get_leftop(rinfo->clause);
3613 
3614 				if (match_index_to_operand(rexpr, c, ind))
3615 				{
3616 					matched = true; /* column is unique */
3617 					break;
3618 				}
3619 			}
3620 
3621 			if (matched)
3622 				continue;
3623 
3624 			forboth(lc, exprlist, lc2, oprlist)
3625 			{
3626 				Node	   *expr = (Node *) lfirst(lc);
3627 				Oid			opr = lfirst_oid(lc2);
3628 
3629 				/* See if the expression matches the index key */
3630 				if (!match_index_to_operand(expr, c, ind))
3631 					continue;
3632 
3633 				/*
3634 				 * The equality operator must be a member of the index
3635 				 * opfamily, else it is not asserting the right kind of
3636 				 * equality behavior for this index.  We assume the caller
3637 				 * determined it is an equality operator, so we don't need to
3638 				 * check any more tightly than this.
3639 				 */
3640 				if (!op_in_opfamily(opr, ind->opfamily[c]))
3641 					continue;
3642 
3643 				/*
3644 				 * XXX at some point we may need to check collations here too.
3645 				 * For the moment we assume all collations reduce to the same
3646 				 * notion of equality.
3647 				 */
3648 
3649 				matched = true; /* column is unique */
3650 				break;
3651 			}
3652 
3653 			if (!matched)
3654 				break;			/* no match; this index doesn't help us */
3655 		}
3656 
3657 		/* Matched all key columns of this index? */
3658 		if (c == ind->nkeycolumns)
3659 			return true;
3660 	}
3661 
3662 	return false;
3663 }
3664 
3665 /*
3666  * indexcol_is_bool_constant_for_query
3667  *
3668  * If an index column is constrained to have a constant value by the query's
3669  * WHERE conditions, then it's irrelevant for sort-order considerations.
3670  * Usually that means we have a restriction clause WHERE indexcol = constant,
3671  * which gets turned into an EquivalenceClass containing a constant, which
3672  * is recognized as redundant by build_index_pathkeys().  But if the index
3673  * column is a boolean variable (or expression), then we are not going to
3674  * see WHERE indexcol = constant, because expression preprocessing will have
3675  * simplified that to "WHERE indexcol" or "WHERE NOT indexcol".  So we are not
3676  * going to have a matching EquivalenceClass (unless the query also contains
3677  * "ORDER BY indexcol").  To allow such cases to work the same as they would
3678  * for non-boolean values, this function is provided to detect whether the
3679  * specified index column matches a boolean restriction clause.
3680  */
3681 bool
indexcol_is_bool_constant_for_query(PlannerInfo * root,IndexOptInfo * index,int indexcol)3682 indexcol_is_bool_constant_for_query(PlannerInfo *root,
3683 									IndexOptInfo *index,
3684 									int indexcol)
3685 {
3686 	ListCell   *lc;
3687 
3688 	/* If the index isn't boolean, we can't possibly get a match */
3689 	if (!IsBooleanOpfamily(index->opfamily[indexcol]))
3690 		return false;
3691 
3692 	/* Check each restriction clause for the index's rel */
3693 	foreach(lc, index->rel->baserestrictinfo)
3694 	{
3695 		RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
3696 
3697 		/*
3698 		 * As in match_clause_to_indexcol, never match pseudoconstants to
3699 		 * indexes.  (It might be semantically okay to do so here, but the
3700 		 * odds of getting a match are negligible, so don't waste the cycles.)
3701 		 */
3702 		if (rinfo->pseudoconstant)
3703 			continue;
3704 
3705 		/* See if we can match the clause's expression to the index column */
3706 		if (match_boolean_index_clause(root, rinfo, indexcol, index))
3707 			return true;
3708 	}
3709 
3710 	return false;
3711 }
3712 
3713 
3714 /****************************************************************************
3715  *				----  ROUTINES TO CHECK OPERANDS  ----
3716  ****************************************************************************/
3717 
3718 /*
3719  * match_index_to_operand()
3720  *	  Generalized test for a match between an index's key
3721  *	  and the operand on one side of a restriction or join clause.
3722  *
3723  * operand: the nodetree to be compared to the index
3724  * indexcol: the column number of the index (counting from 0)
3725  * index: the index of interest
3726  *
3727  * Note that we aren't interested in collations here; the caller must check
3728  * for a collation match, if it's dealing with an operator where that matters.
3729  *
3730  * This is exported for use in selfuncs.c.
3731  */
3732 bool
match_index_to_operand(Node * operand,int indexcol,IndexOptInfo * index)3733 match_index_to_operand(Node *operand,
3734 					   int indexcol,
3735 					   IndexOptInfo *index)
3736 {
3737 	int			indkey;
3738 
3739 	/*
3740 	 * Ignore any RelabelType node above the operand.   This is needed to be
3741 	 * able to apply indexscanning in binary-compatible-operator cases. Note:
3742 	 * we can assume there is at most one RelabelType node;
3743 	 * eval_const_expressions() will have simplified if more than one.
3744 	 */
3745 	if (operand && IsA(operand, RelabelType))
3746 		operand = (Node *) ((RelabelType *) operand)->arg;
3747 
3748 	indkey = index->indexkeys[indexcol];
3749 	if (indkey != 0)
3750 	{
3751 		/*
3752 		 * Simple index column; operand must be a matching Var.
3753 		 */
3754 		if (operand && IsA(operand, Var) &&
3755 			index->rel->relid == ((Var *) operand)->varno &&
3756 			indkey == ((Var *) operand)->varattno)
3757 			return true;
3758 	}
3759 	else
3760 	{
3761 		/*
3762 		 * Index expression; find the correct expression.  (This search could
3763 		 * be avoided, at the cost of complicating all the callers of this
3764 		 * routine; doesn't seem worth it.)
3765 		 */
3766 		ListCell   *indexpr_item;
3767 		int			i;
3768 		Node	   *indexkey;
3769 
3770 		indexpr_item = list_head(index->indexprs);
3771 		for (i = 0; i < indexcol; i++)
3772 		{
3773 			if (index->indexkeys[i] == 0)
3774 			{
3775 				if (indexpr_item == NULL)
3776 					elog(ERROR, "wrong number of index expressions");
3777 				indexpr_item = lnext(index->indexprs, indexpr_item);
3778 			}
3779 		}
3780 		if (indexpr_item == NULL)
3781 			elog(ERROR, "wrong number of index expressions");
3782 		indexkey = (Node *) lfirst(indexpr_item);
3783 
3784 		/*
3785 		 * Does it match the operand?  Again, strip any relabeling.
3786 		 */
3787 		if (indexkey && IsA(indexkey, RelabelType))
3788 			indexkey = (Node *) ((RelabelType *) indexkey)->arg;
3789 
3790 		if (equal(indexkey, operand))
3791 			return true;
3792 	}
3793 
3794 	return false;
3795 }
3796 
3797 /*
3798  * is_pseudo_constant_for_index()
3799  *	  Test whether the given expression can be used as an indexscan
3800  *	  comparison value.
3801  *
3802  * An indexscan comparison value must not contain any volatile functions,
3803  * and it can't contain any Vars of the index's own table.  Vars of
3804  * other tables are okay, though; in that case we'd be producing an
3805  * indexqual usable in a parameterized indexscan.  This is, therefore,
3806  * a weaker condition than is_pseudo_constant_clause().
3807  *
3808  * This function is exported for use by planner support functions,
3809  * which will have available the IndexOptInfo, but not any RestrictInfo
3810  * infrastructure.  It is making the same test made by functions above
3811  * such as match_opclause_to_indexcol(), but those rely where possible
3812  * on RestrictInfo information about variable membership.
3813  *
3814  * expr: the nodetree to be checked
3815  * index: the index of interest
3816  */
3817 bool
is_pseudo_constant_for_index(PlannerInfo * root,Node * expr,IndexOptInfo * index)3818 is_pseudo_constant_for_index(PlannerInfo *root, Node *expr, IndexOptInfo *index)
3819 {
3820 	/* pull_varnos is cheaper than volatility check, so do that first */
3821 	if (bms_is_member(index->rel->relid, pull_varnos(root, expr)))
3822 		return false;			/* no good, contains Var of table */
3823 	if (contain_volatile_functions(expr))
3824 		return false;			/* no good, volatile comparison value */
3825 	return true;
3826 }
3827