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