Fix oversight in previous error-reporting patch; mustn't pfree path string
[PostgreSQL.git] / src / backend / optimizer / path / indxpath.c
blobe7237ce7728b35d9400676cb0edc6518896a18a9
1 /*-------------------------------------------------------------------------
3 * indxpath.c
4 * Routines to determine which indexes are usable for scanning a
5 * given relation, and create Paths accordingly.
7 * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
11 * IDENTIFICATION
12 * $PostgreSQL$
14 *-------------------------------------------------------------------------
16 #include "postgres.h"
18 #include <math.h>
20 #include "access/skey.h"
21 #include "catalog/pg_am.h"
22 #include "catalog/pg_operator.h"
23 #include "catalog/pg_opfamily.h"
24 #include "catalog/pg_type.h"
25 #include "nodes/makefuncs.h"
26 #include "optimizer/clauses.h"
27 #include "optimizer/cost.h"
28 #include "optimizer/pathnode.h"
29 #include "optimizer/paths.h"
30 #include "optimizer/predtest.h"
31 #include "optimizer/restrictinfo.h"
32 #include "optimizer/var.h"
33 #include "utils/builtins.h"
34 #include "utils/lsyscache.h"
35 #include "utils/pg_locale.h"
36 #include "utils/selfuncs.h"
40 * DoneMatchingIndexKeys() - MACRO
42 #define DoneMatchingIndexKeys(families) (families[0] == InvalidOid)
44 #define IsBooleanOpfamily(opfamily) \
45 ((opfamily) == BOOL_BTREE_FAM_OID || (opfamily) == BOOL_HASH_FAM_OID)
48 /* Per-path data used within choose_bitmap_and() */
49 typedef struct
51 Path *path; /* IndexPath, BitmapAndPath, or BitmapOrPath */
52 List *quals; /* the WHERE clauses it uses */
53 List *preds; /* predicates of its partial index(es) */
54 Bitmapset *clauseids; /* quals+preds represented as a bitmapset */
55 } PathClauseUsage;
58 static List *find_usable_indexes(PlannerInfo *root, RelOptInfo *rel,
59 List *clauses, List *outer_clauses,
60 bool istoplevel, RelOptInfo *outer_rel,
61 SaOpControl saop_control);
62 static List *find_saop_paths(PlannerInfo *root, RelOptInfo *rel,
63 List *clauses, List *outer_clauses,
64 bool istoplevel, RelOptInfo *outer_rel);
65 static Path *choose_bitmap_and(PlannerInfo *root, RelOptInfo *rel,
66 List *paths, RelOptInfo *outer_rel);
67 static int path_usage_comparator(const void *a, const void *b);
68 static Cost bitmap_scan_cost_est(PlannerInfo *root, RelOptInfo *rel,
69 Path *ipath, RelOptInfo *outer_rel);
70 static Cost bitmap_and_cost_est(PlannerInfo *root, RelOptInfo *rel,
71 List *paths, RelOptInfo *outer_rel);
72 static PathClauseUsage *classify_index_clause_usage(Path *path,
73 List **clauselist);
74 static void find_indexpath_quals(Path *bitmapqual, List **quals, List **preds);
75 static int find_list_position(Node *node, List **nodelist);
76 static bool match_clause_to_indexcol(IndexOptInfo *index,
77 int indexcol, Oid opfamily,
78 RestrictInfo *rinfo,
79 Relids outer_relids,
80 SaOpControl saop_control);
81 static bool is_indexable_operator(Oid expr_op, Oid opfamily,
82 bool indexkey_on_left);
83 static bool match_rowcompare_to_indexcol(IndexOptInfo *index,
84 int indexcol,
85 Oid opfamily,
86 RowCompareExpr *clause,
87 Relids outer_relids);
88 static Relids indexable_outerrelids(PlannerInfo *root, RelOptInfo *rel);
89 static bool matches_any_index(RestrictInfo *rinfo, RelOptInfo *rel,
90 Relids outer_relids);
91 static List *find_clauses_for_join(PlannerInfo *root, RelOptInfo *rel,
92 Relids outer_relids, bool isouterjoin);
93 static bool match_boolean_index_clause(Node *clause, int indexcol,
94 IndexOptInfo *index);
95 static bool match_special_index_operator(Expr *clause, Oid opfamily,
96 bool indexkey_on_left);
97 static Expr *expand_boolean_index_clause(Node *clause, int indexcol,
98 IndexOptInfo *index);
99 static List *expand_indexqual_opclause(RestrictInfo *rinfo, Oid opfamily);
100 static RestrictInfo *expand_indexqual_rowcompare(RestrictInfo *rinfo,
101 IndexOptInfo *index,
102 int indexcol);
103 static List *prefix_quals(Node *leftop, Oid opfamily,
104 Const *prefix, Pattern_Prefix_Status pstatus);
105 static List *network_prefix_quals(Node *leftop, Oid expr_op, Oid opfamily,
106 Datum rightop);
107 static Datum string_to_datum(const char *str, Oid datatype);
108 static Const *string_to_const(const char *str, Oid datatype);
112 * create_index_paths()
113 * Generate all interesting index paths for the given relation.
114 * Candidate paths are added to the rel's pathlist (using add_path).
116 * To be considered for an index scan, an index must match one or more
117 * restriction clauses or join clauses from the query's qual condition,
118 * or match the query's ORDER BY condition, or have a predicate that
119 * matches the query's qual condition.
121 * There are two basic kinds of index scans. A "plain" index scan uses
122 * only restriction clauses (possibly none at all) in its indexqual,
123 * so it can be applied in any context. An "innerjoin" index scan uses
124 * join clauses (plus restriction clauses, if available) in its indexqual.
125 * Therefore it can only be used as the inner relation of a nestloop
126 * join against an outer rel that includes all the other rels mentioned
127 * in its join clauses. In that context, values for the other rels'
128 * attributes are available and fixed during any one scan of the indexpath.
130 * An IndexPath is generated and submitted to add_path() for each plain index
131 * scan this routine deems potentially interesting for the current query.
133 * We also determine the set of other relids that participate in join
134 * clauses that could be used with each index. The actually best innerjoin
135 * path will be generated for each outer relation later on, but knowing the
136 * set of potential otherrels allows us to identify equivalent outer relations
137 * and avoid repeated computation.
139 * 'rel' is the relation for which we want to generate index paths
141 * Note: check_partial_indexes() must have been run previously for this rel.
143 void
144 create_index_paths(PlannerInfo *root, RelOptInfo *rel)
146 List *indexpaths;
147 List *bitindexpaths;
148 ListCell *l;
150 /* Skip the whole mess if no indexes */
151 if (rel->indexlist == NIL)
153 rel->index_outer_relids = NULL;
154 return;
158 * Examine join clauses to see which ones are potentially usable with
159 * indexes of this rel, and generate the set of all other relids that
160 * participate in such join clauses. We'll use this set later to
161 * recognize outer rels that are equivalent for joining purposes.
163 rel->index_outer_relids = indexable_outerrelids(root, rel);
166 * Find all the index paths that are directly usable for this relation
167 * (ie, are valid without considering OR or JOIN clauses).
169 indexpaths = find_usable_indexes(root, rel,
170 rel->baserestrictinfo, NIL,
171 true, NULL, SAOP_FORBID);
174 * We can submit them all to add_path. (This generates access paths for
175 * plain IndexScan plans.) However, for the next step we will only want
176 * the ones that have some selectivity; we must discard anything that was
177 * generated solely for ordering purposes.
179 bitindexpaths = NIL;
180 foreach(l, indexpaths)
182 IndexPath *ipath = (IndexPath *) lfirst(l);
184 add_path(rel, (Path *) ipath);
186 if (ipath->indexselectivity < 1.0 &&
187 !ScanDirectionIsBackward(ipath->indexscandir))
188 bitindexpaths = lappend(bitindexpaths, ipath);
192 * Generate BitmapOrPaths for any suitable OR-clauses present in the
193 * restriction list. Add these to bitindexpaths.
195 indexpaths = generate_bitmap_or_paths(root, rel,
196 rel->baserestrictinfo, NIL,
197 NULL);
198 bitindexpaths = list_concat(bitindexpaths, indexpaths);
201 * Likewise, generate paths using ScalarArrayOpExpr clauses; these can't
202 * be simple indexscans but they can be used in bitmap scans.
204 indexpaths = find_saop_paths(root, rel,
205 rel->baserestrictinfo, NIL,
206 true, NULL);
207 bitindexpaths = list_concat(bitindexpaths, indexpaths);
210 * If we found anything usable, generate a BitmapHeapPath for the most
211 * promising combination of bitmap index paths.
213 if (bitindexpaths != NIL)
215 Path *bitmapqual;
216 BitmapHeapPath *bpath;
218 bitmapqual = choose_bitmap_and(root, rel, bitindexpaths, NULL);
219 bpath = create_bitmap_heap_path(root, rel, bitmapqual, NULL);
220 add_path(rel, (Path *) bpath);
225 /*----------
226 * find_usable_indexes
227 * Given a list of restriction clauses, find all the potentially usable
228 * indexes for the given relation, and return a list of IndexPaths.
230 * The caller actually supplies two lists of restriction clauses: some
231 * "current" ones and some "outer" ones. Both lists can be used freely
232 * to match keys of the index, but an index must use at least one of the
233 * "current" clauses to be considered usable. The motivation for this is
234 * examples like
235 * WHERE (x = 42) AND (... OR (y = 52 AND z = 77) OR ....)
236 * While we are considering the y/z subclause of the OR, we can use "x = 42"
237 * as one of the available index conditions; but we shouldn't match the
238 * subclause to any index on x alone, because such a Path would already have
239 * been generated at the upper level. So we could use an index on x,y,z
240 * or an index on x,y for the OR subclause, but not an index on just x.
241 * When dealing with a partial index, a match of the index predicate to
242 * one of the "current" clauses also makes the index usable.
244 * If istoplevel is true (indicating we are considering the top level of a
245 * rel's restriction clauses), we will include indexes in the result that
246 * have an interesting sort order, even if they have no matching restriction
247 * clauses.
249 * 'rel' is the relation for which we want to generate index paths
250 * 'clauses' is the current list of clauses (RestrictInfo nodes)
251 * 'outer_clauses' is the list of additional upper-level clauses
252 * 'istoplevel' is true if clauses are the rel's top-level restriction list
253 * (outer_clauses must be NIL when this is true)
254 * 'outer_rel' is the outer side of the join if forming an inner indexscan
255 * (so some of the given clauses are join clauses); NULL if not
256 * 'saop_control' indicates whether ScalarArrayOpExpr clauses can be used
258 * Note: check_partial_indexes() must have been run previously.
259 *----------
261 static List *
262 find_usable_indexes(PlannerInfo *root, RelOptInfo *rel,
263 List *clauses, List *outer_clauses,
264 bool istoplevel, RelOptInfo *outer_rel,
265 SaOpControl saop_control)
267 Relids outer_relids = outer_rel ? outer_rel->relids : NULL;
268 bool possibly_useful_pathkeys = has_useful_pathkeys(root, rel);
269 List *result = NIL;
270 List *all_clauses = NIL; /* not computed till needed */
271 ListCell *ilist;
273 foreach(ilist, rel->indexlist)
275 IndexOptInfo *index = (IndexOptInfo *) lfirst(ilist);
276 IndexPath *ipath;
277 List *restrictclauses;
278 List *index_pathkeys;
279 List *useful_pathkeys;
280 bool useful_predicate;
281 bool found_clause;
282 bool index_is_ordered;
285 * Ignore partial indexes that do not match the query. If a partial
286 * index is marked predOK then we know it's OK; otherwise, if we are
287 * at top level we know it's not OK (since predOK is exactly whether
288 * its predicate could be proven from the toplevel clauses).
289 * Otherwise, we have to test whether the added clauses are sufficient
290 * to imply the predicate. If so, we could use the index in the
291 * current context.
293 * We set useful_predicate to true iff the predicate was proven using
294 * the current set of clauses. This is needed to prevent matching a
295 * predOK index to an arm of an OR, which would be a legal but
296 * pointlessly inefficient plan. (A better plan will be generated by
297 * just scanning the predOK index alone, no OR.)
299 useful_predicate = false;
300 if (index->indpred != NIL)
302 if (index->predOK)
304 if (istoplevel)
306 /* we know predicate was proven from these clauses */
307 useful_predicate = true;
310 else
312 if (istoplevel)
313 continue; /* no point in trying to prove it */
315 /* Form all_clauses if not done already */
316 if (all_clauses == NIL)
317 all_clauses = list_concat(list_copy(clauses),
318 outer_clauses);
320 if (!predicate_implied_by(index->indpred, all_clauses))
321 continue; /* can't use it at all */
323 if (!predicate_implied_by(index->indpred, outer_clauses))
324 useful_predicate = true;
329 * 1. Match the index against the available restriction clauses.
330 * found_clause is set true only if at least one of the current
331 * clauses was used (and, if saop_control is SAOP_REQUIRE, it has to
332 * have been a ScalarArrayOpExpr clause).
334 restrictclauses = group_clauses_by_indexkey(index,
335 clauses,
336 outer_clauses,
337 outer_relids,
338 saop_control,
339 &found_clause);
342 * Not all index AMs support scans with no restriction clauses. We
343 * can't generate a scan over an index with amoptionalkey = false
344 * unless there's at least one restriction clause.
346 if (restrictclauses == NIL && !index->amoptionalkey)
347 continue;
350 * 2. Compute pathkeys describing index's ordering, if any, then see
351 * how many of them are actually useful for this query. This is not
352 * relevant unless we are at top level.
354 index_is_ordered = OidIsValid(index->fwdsortop[0]);
355 if (index_is_ordered && possibly_useful_pathkeys &&
356 istoplevel && outer_rel == NULL)
358 index_pathkeys = build_index_pathkeys(root, index,
359 ForwardScanDirection);
360 useful_pathkeys = truncate_useless_pathkeys(root, rel,
361 index_pathkeys);
363 else
364 useful_pathkeys = NIL;
367 * 3. Generate an indexscan path if there are relevant restriction
368 * clauses in the current clauses, OR the index ordering is
369 * potentially useful for later merging or final output ordering, OR
370 * the index has a predicate that was proven by the current clauses.
372 if (found_clause || useful_pathkeys != NIL || useful_predicate)
374 ipath = create_index_path(root, index,
375 restrictclauses,
376 useful_pathkeys,
377 index_is_ordered ?
378 ForwardScanDirection :
379 NoMovementScanDirection,
380 outer_rel);
381 result = lappend(result, ipath);
385 * 4. If the index is ordered, a backwards scan might be interesting.
386 * Again, this is only interesting at top level.
388 if (index_is_ordered && possibly_useful_pathkeys &&
389 istoplevel && outer_rel == NULL)
391 index_pathkeys = build_index_pathkeys(root, index,
392 BackwardScanDirection);
393 useful_pathkeys = truncate_useless_pathkeys(root, rel,
394 index_pathkeys);
395 if (useful_pathkeys != NIL)
397 ipath = create_index_path(root, index,
398 restrictclauses,
399 useful_pathkeys,
400 BackwardScanDirection,
401 outer_rel);
402 result = lappend(result, ipath);
407 return result;
412 * find_saop_paths
413 * Find all the potential indexpaths that make use of ScalarArrayOpExpr
414 * clauses. The executor only supports these in bitmap scans, not
415 * plain indexscans, so we need to segregate them from the normal case.
416 * Otherwise, same API as find_usable_indexes().
417 * Returns a list of IndexPaths.
419 static List *
420 find_saop_paths(PlannerInfo *root, RelOptInfo *rel,
421 List *clauses, List *outer_clauses,
422 bool istoplevel, RelOptInfo *outer_rel)
424 bool have_saop = false;
425 ListCell *l;
428 * Since find_usable_indexes is relatively expensive, don't bother to run
429 * it unless there are some top-level ScalarArrayOpExpr clauses.
431 foreach(l, clauses)
433 RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
435 Assert(IsA(rinfo, RestrictInfo));
436 if (IsA(rinfo->clause, ScalarArrayOpExpr))
438 have_saop = true;
439 break;
442 if (!have_saop)
443 return NIL;
445 return find_usable_indexes(root, rel,
446 clauses, outer_clauses,
447 istoplevel, outer_rel,
448 SAOP_REQUIRE);
453 * generate_bitmap_or_paths
454 * Look through the list of clauses to find OR clauses, and generate
455 * a BitmapOrPath for each one we can handle that way. Return a list
456 * of the generated BitmapOrPaths.
458 * outer_clauses is a list of additional clauses that can be assumed true
459 * for the purpose of generating indexquals, but are not to be searched for
460 * ORs. (See find_usable_indexes() for motivation.) outer_rel is the outer
461 * side when we are considering a nestloop inner indexpath.
463 List *
464 generate_bitmap_or_paths(PlannerInfo *root, RelOptInfo *rel,
465 List *clauses, List *outer_clauses,
466 RelOptInfo *outer_rel)
468 List *result = NIL;
469 List *all_clauses;
470 ListCell *l;
473 * We can use both the current and outer clauses as context for
474 * find_usable_indexes
476 all_clauses = list_concat(list_copy(clauses), outer_clauses);
478 foreach(l, clauses)
480 RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
481 List *pathlist;
482 Path *bitmapqual;
483 ListCell *j;
485 Assert(IsA(rinfo, RestrictInfo));
486 /* Ignore RestrictInfos that aren't ORs */
487 if (!restriction_is_or_clause(rinfo))
488 continue;
491 * We must be able to match at least one index to each of the arms of
492 * the OR, else we can't use it.
494 pathlist = NIL;
495 foreach(j, ((BoolExpr *) rinfo->orclause)->args)
497 Node *orarg = (Node *) lfirst(j);
498 List *indlist;
500 /* OR arguments should be ANDs or sub-RestrictInfos */
501 if (and_clause(orarg))
503 List *andargs = ((BoolExpr *) orarg)->args;
505 indlist = find_usable_indexes(root, rel,
506 andargs,
507 all_clauses,
508 false,
509 outer_rel,
510 SAOP_ALLOW);
511 /* Recurse in case there are sub-ORs */
512 indlist = list_concat(indlist,
513 generate_bitmap_or_paths(root, rel,
514 andargs,
515 all_clauses,
516 outer_rel));
518 else
520 Assert(IsA(orarg, RestrictInfo));
521 Assert(!restriction_is_or_clause((RestrictInfo *) orarg));
522 indlist = find_usable_indexes(root, rel,
523 list_make1(orarg),
524 all_clauses,
525 false,
526 outer_rel,
527 SAOP_ALLOW);
531 * If nothing matched this arm, we can't do anything with this OR
532 * clause.
534 if (indlist == NIL)
536 pathlist = NIL;
537 break;
541 * OK, pick the most promising AND combination, and add it to
542 * pathlist.
544 bitmapqual = choose_bitmap_and(root, rel, indlist, outer_rel);
545 pathlist = lappend(pathlist, bitmapqual);
549 * If we have a match for every arm, then turn them into a
550 * BitmapOrPath, and add to result list.
552 if (pathlist != NIL)
554 bitmapqual = (Path *) create_bitmap_or_path(root, rel, pathlist);
555 result = lappend(result, bitmapqual);
559 return result;
564 * choose_bitmap_and
565 * Given a nonempty list of bitmap paths, AND them into one path.
567 * This is a nontrivial decision since we can legally use any subset of the
568 * given path set. We want to choose a good tradeoff between selectivity
569 * and cost of computing the bitmap.
571 * The result is either a single one of the inputs, or a BitmapAndPath
572 * combining multiple inputs.
574 static Path *
575 choose_bitmap_and(PlannerInfo *root, RelOptInfo *rel,
576 List *paths, RelOptInfo *outer_rel)
578 int npaths = list_length(paths);
579 PathClauseUsage **pathinfoarray;
580 PathClauseUsage *pathinfo;
581 List *clauselist;
582 List *bestpaths = NIL;
583 Cost bestcost = 0;
584 int i,
586 ListCell *l;
588 Assert(npaths > 0); /* else caller error */
589 if (npaths == 1)
590 return (Path *) linitial(paths); /* easy case */
593 * In theory we should consider every nonempty subset of the given paths.
594 * In practice that seems like overkill, given the crude nature of the
595 * estimates, not to mention the possible effects of higher-level AND and
596 * OR clauses. Moreover, it's completely impractical if there are a large
597 * number of paths, since the work would grow as O(2^N).
599 * As a heuristic, we first check for paths using exactly the same sets of
600 * WHERE clauses + index predicate conditions, and reject all but the
601 * cheapest-to-scan in any such group. This primarily gets rid of indexes
602 * that include the interesting columns but also irrelevant columns. (In
603 * situations where the DBA has gone overboard on creating variant
604 * indexes, this can make for a very large reduction in the number of
605 * paths considered further.)
607 * We then sort the surviving paths with the cheapest-to-scan first, and
608 * for each path, consider using that path alone as the basis for a bitmap
609 * scan. Then we consider bitmap AND scans formed from that path plus
610 * each subsequent (higher-cost) path, adding on a subsequent path if it
611 * results in a reduction in the estimated total scan cost. This means we
612 * consider about O(N^2) rather than O(2^N) path combinations, which is
613 * quite tolerable, especially given than N is usually reasonably small
614 * because of the prefiltering step. The cheapest of these is returned.
616 * We will only consider AND combinations in which no two indexes use the
617 * same WHERE clause. This is a bit of a kluge: it's needed because
618 * costsize.c and clausesel.c aren't very smart about redundant clauses.
619 * They will usually double-count the redundant clauses, producing a
620 * too-small selectivity that makes a redundant AND step look like it
621 * reduces the total cost. Perhaps someday that code will be smarter and
622 * we can remove this limitation. (But note that this also defends
623 * against flat-out duplicate input paths, which can happen because
624 * best_inner_indexscan will find the same OR join clauses that
625 * create_or_index_quals has pulled OR restriction clauses out of.)
627 * For the same reason, we reject AND combinations in which an index
628 * predicate clause duplicates another clause. Here we find it necessary
629 * to be even stricter: we'll reject a partial index if any of its
630 * predicate clauses are implied by the set of WHERE clauses and predicate
631 * clauses used so far. This covers cases such as a condition "x = 42"
632 * used with a plain index, followed by a clauseless scan of a partial
633 * index "WHERE x >= 40 AND x < 50". The partial index has been accepted
634 * only because "x = 42" was present, and so allowing it would partially
635 * double-count selectivity. (We could use predicate_implied_by on
636 * regular qual clauses too, to have a more intelligent, but much more
637 * expensive, check for redundancy --- but in most cases simple equality
638 * seems to suffice.)
642 * Extract clause usage info and detect any paths that use exactly the
643 * same set of clauses; keep only the cheapest-to-scan of any such groups.
644 * The surviving paths are put into an array for qsort'ing.
646 pathinfoarray = (PathClauseUsage **)
647 palloc(npaths * sizeof(PathClauseUsage *));
648 clauselist = NIL;
649 npaths = 0;
650 foreach(l, paths)
652 Path *ipath = (Path *) lfirst(l);
654 pathinfo = classify_index_clause_usage(ipath, &clauselist);
655 for (i = 0; i < npaths; i++)
657 if (bms_equal(pathinfo->clauseids, pathinfoarray[i]->clauseids))
658 break;
660 if (i < npaths)
662 /* duplicate clauseids, keep the cheaper one */
663 Cost ncost;
664 Cost ocost;
665 Selectivity nselec;
666 Selectivity oselec;
668 cost_bitmap_tree_node(pathinfo->path, &ncost, &nselec);
669 cost_bitmap_tree_node(pathinfoarray[i]->path, &ocost, &oselec);
670 if (ncost < ocost)
671 pathinfoarray[i] = pathinfo;
673 else
675 /* not duplicate clauseids, add to array */
676 pathinfoarray[npaths++] = pathinfo;
680 /* If only one surviving path, we're done */
681 if (npaths == 1)
682 return pathinfoarray[0]->path;
684 /* Sort the surviving paths by index access cost */
685 qsort(pathinfoarray, npaths, sizeof(PathClauseUsage *),
686 path_usage_comparator);
689 * For each surviving index, consider it as an "AND group leader", and see
690 * whether adding on any of the later indexes results in an AND path with
691 * cheaper total cost than before. Then take the cheapest AND group.
693 for (i = 0; i < npaths; i++)
695 Cost costsofar;
696 List *qualsofar;
697 Bitmapset *clauseidsofar;
698 ListCell *lastcell;
700 pathinfo = pathinfoarray[i];
701 paths = list_make1(pathinfo->path);
702 costsofar = bitmap_scan_cost_est(root, rel, pathinfo->path, outer_rel);
703 qualsofar = list_concat(list_copy(pathinfo->quals),
704 list_copy(pathinfo->preds));
705 clauseidsofar = bms_copy(pathinfo->clauseids);
706 lastcell = list_head(paths); /* for quick deletions */
708 for (j = i + 1; j < npaths; j++)
710 Cost newcost;
712 pathinfo = pathinfoarray[j];
713 /* Check for redundancy */
714 if (bms_overlap(pathinfo->clauseids, clauseidsofar))
715 continue; /* consider it redundant */
716 if (pathinfo->preds)
718 bool redundant = false;
720 /* we check each predicate clause separately */
721 foreach(l, pathinfo->preds)
723 Node *np = (Node *) lfirst(l);
725 if (predicate_implied_by(list_make1(np), qualsofar))
727 redundant = true;
728 break; /* out of inner foreach loop */
731 if (redundant)
732 continue;
734 /* tentatively add new path to paths, so we can estimate cost */
735 paths = lappend(paths, pathinfo->path);
736 newcost = bitmap_and_cost_est(root, rel, paths, outer_rel);
737 if (newcost < costsofar)
739 /* keep new path in paths, update subsidiary variables */
740 costsofar = newcost;
741 qualsofar = list_concat(qualsofar,
742 list_copy(pathinfo->quals));
743 qualsofar = list_concat(qualsofar,
744 list_copy(pathinfo->preds));
745 clauseidsofar = bms_add_members(clauseidsofar,
746 pathinfo->clauseids);
747 lastcell = lnext(lastcell);
749 else
751 /* reject new path, remove it from paths list */
752 paths = list_delete_cell(paths, lnext(lastcell), lastcell);
754 Assert(lnext(lastcell) == NULL);
757 /* Keep the cheapest AND-group (or singleton) */
758 if (i == 0 || costsofar < bestcost)
760 bestpaths = paths;
761 bestcost = costsofar;
764 /* some easy cleanup (we don't try real hard though) */
765 list_free(qualsofar);
768 if (list_length(bestpaths) == 1)
769 return (Path *) linitial(bestpaths); /* no need for AND */
770 return (Path *) create_bitmap_and_path(root, rel, bestpaths);
773 /* qsort comparator to sort in increasing index access cost order */
774 static int
775 path_usage_comparator(const void *a, const void *b)
777 PathClauseUsage *pa = *(PathClauseUsage *const *) a;
778 PathClauseUsage *pb = *(PathClauseUsage *const *) b;
779 Cost acost;
780 Cost bcost;
781 Selectivity aselec;
782 Selectivity bselec;
784 cost_bitmap_tree_node(pa->path, &acost, &aselec);
785 cost_bitmap_tree_node(pb->path, &bcost, &bselec);
788 * If costs are the same, sort by selectivity.
790 if (acost < bcost)
791 return -1;
792 if (acost > bcost)
793 return 1;
795 if (aselec < bselec)
796 return -1;
797 if (aselec > bselec)
798 return 1;
800 return 0;
804 * Estimate the cost of actually executing a bitmap scan with a single
805 * index path (no BitmapAnd, at least not at this level).
807 static Cost
808 bitmap_scan_cost_est(PlannerInfo *root, RelOptInfo *rel,
809 Path *ipath, RelOptInfo *outer_rel)
811 Path bpath;
813 cost_bitmap_heap_scan(&bpath, root, rel, ipath, outer_rel);
815 return bpath.total_cost;
819 * Estimate the cost of actually executing a BitmapAnd scan with the given
820 * inputs.
822 static Cost
823 bitmap_and_cost_est(PlannerInfo *root, RelOptInfo *rel,
824 List *paths, RelOptInfo *outer_rel)
826 BitmapAndPath apath;
827 Path bpath;
829 /* Set up a dummy BitmapAndPath */
830 apath.path.type = T_BitmapAndPath;
831 apath.path.parent = rel;
832 apath.bitmapquals = paths;
833 cost_bitmap_and_node(&apath, root);
835 /* Now we can do cost_bitmap_heap_scan */
836 cost_bitmap_heap_scan(&bpath, root, rel, (Path *) &apath, outer_rel);
838 return bpath.total_cost;
843 * classify_index_clause_usage
844 * Construct a PathClauseUsage struct describing the WHERE clauses and
845 * index predicate clauses used by the given indexscan path.
846 * We consider two clauses the same if they are equal().
848 * At some point we might want to migrate this info into the Path data
849 * structure proper, but for the moment it's only needed within
850 * choose_bitmap_and().
852 * *clauselist is used and expanded as needed to identify all the distinct
853 * clauses seen across successive calls. Caller must initialize it to NIL
854 * before first call of a set.
856 static PathClauseUsage *
857 classify_index_clause_usage(Path *path, List **clauselist)
859 PathClauseUsage *result;
860 Bitmapset *clauseids;
861 ListCell *lc;
863 result = (PathClauseUsage *) palloc(sizeof(PathClauseUsage));
864 result->path = path;
866 /* Recursively find the quals and preds used by the path */
867 result->quals = NIL;
868 result->preds = NIL;
869 find_indexpath_quals(path, &result->quals, &result->preds);
871 /* Build up a bitmapset representing the quals and preds */
872 clauseids = NULL;
873 foreach(lc, result->quals)
875 Node *node = (Node *) lfirst(lc);
877 clauseids = bms_add_member(clauseids,
878 find_list_position(node, clauselist));
880 foreach(lc, result->preds)
882 Node *node = (Node *) lfirst(lc);
884 clauseids = bms_add_member(clauseids,
885 find_list_position(node, clauselist));
887 result->clauseids = clauseids;
889 return result;
894 * find_indexpath_quals
896 * Given the Path structure for a plain or bitmap indexscan, extract lists
897 * of all the indexquals and index predicate conditions used in the Path.
898 * These are appended to the initial contents of *quals and *preds (hence
899 * caller should initialize those to NIL).
901 * This is sort of a simplified version of make_restrictinfo_from_bitmapqual;
902 * here, we are not trying to produce an accurate representation of the AND/OR
903 * semantics of the Path, but just find out all the base conditions used.
905 * The result lists contain pointers to the expressions used in the Path,
906 * but all the list cells are freshly built, so it's safe to destructively
907 * modify the lists (eg, by concat'ing with other lists).
909 static void
910 find_indexpath_quals(Path *bitmapqual, List **quals, List **preds)
912 if (IsA(bitmapqual, BitmapAndPath))
914 BitmapAndPath *apath = (BitmapAndPath *) bitmapqual;
915 ListCell *l;
917 foreach(l, apath->bitmapquals)
919 find_indexpath_quals((Path *) lfirst(l), quals, preds);
922 else if (IsA(bitmapqual, BitmapOrPath))
924 BitmapOrPath *opath = (BitmapOrPath *) bitmapqual;
925 ListCell *l;
927 foreach(l, opath->bitmapquals)
929 find_indexpath_quals((Path *) lfirst(l), quals, preds);
932 else if (IsA(bitmapqual, IndexPath))
934 IndexPath *ipath = (IndexPath *) bitmapqual;
936 *quals = list_concat(*quals, get_actual_clauses(ipath->indexclauses));
937 *preds = list_concat(*preds, list_copy(ipath->indexinfo->indpred));
939 else
940 elog(ERROR, "unrecognized node type: %d", nodeTag(bitmapqual));
945 * find_list_position
946 * Return the given node's position (counting from 0) in the given
947 * list of nodes. If it's not equal() to any existing list member,
948 * add it at the end, and return that position.
950 static int
951 find_list_position(Node *node, List **nodelist)
953 int i;
954 ListCell *lc;
956 i = 0;
957 foreach(lc, *nodelist)
959 Node *oldnode = (Node *) lfirst(lc);
961 if (equal(node, oldnode))
962 return i;
963 i++;
966 *nodelist = lappend(*nodelist, node);
968 return i;
972 /****************************************************************************
973 * ---- ROUTINES TO CHECK RESTRICTIONS ----
974 ****************************************************************************/
978 * group_clauses_by_indexkey
979 * Find restriction clauses that can be used with an index.
981 * Returns a list of sublists of RestrictInfo nodes for clauses that can be
982 * used with this index. Each sublist contains clauses that can be used
983 * with one index key (in no particular order); the top list is ordered by
984 * index key. (This is depended on by expand_indexqual_conditions().)
986 * We can use clauses from either the current clauses or outer_clauses lists,
987 * but *found_clause is set TRUE only if we used at least one clause from
988 * the "current clauses" list. See find_usable_indexes() for motivation.
990 * outer_relids determines what Vars will be allowed on the other side
991 * of a possible index qual; see match_clause_to_indexcol().
993 * 'saop_control' indicates whether ScalarArrayOpExpr clauses can be used.
994 * When it's SAOP_REQUIRE, *found_clause is set TRUE only if we used at least
995 * one ScalarArrayOpExpr from the current clauses list.
997 * If the index has amoptionalkey = false, we give up and return NIL when
998 * there are no restriction clauses matching the first index key. Otherwise,
999 * we return NIL if there are no restriction clauses matching any index key.
1000 * A non-NIL result will have one (possibly empty) sublist for each index key.
1002 * Example: given an index on (A,B,C), we would return ((C1 C2) () (C3 C4))
1003 * if we find that clauses C1 and C2 use column A, clauses C3 and C4 use
1004 * column C, and no clauses use column B.
1006 * Note: in some circumstances we may find the same RestrictInfos coming
1007 * from multiple places. Defend against redundant outputs by using
1008 * list_append_unique_ptr (pointer equality should be good enough).
1010 List *
1011 group_clauses_by_indexkey(IndexOptInfo *index,
1012 List *clauses, List *outer_clauses,
1013 Relids outer_relids,
1014 SaOpControl saop_control,
1015 bool *found_clause)
1017 List *clausegroup_list = NIL;
1018 bool found_outer_clause = false;
1019 int indexcol = 0;
1020 Oid *families = index->opfamily;
1022 *found_clause = false; /* default result */
1024 if (clauses == NIL && outer_clauses == NIL)
1025 return NIL; /* cannot succeed */
1029 Oid curFamily = families[0];
1030 List *clausegroup = NIL;
1031 ListCell *l;
1033 /* check the current clauses */
1034 foreach(l, clauses)
1036 RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
1038 Assert(IsA(rinfo, RestrictInfo));
1039 if (match_clause_to_indexcol(index,
1040 indexcol,
1041 curFamily,
1042 rinfo,
1043 outer_relids,
1044 saop_control))
1046 clausegroup = list_append_unique_ptr(clausegroup, rinfo);
1047 if (saop_control != SAOP_REQUIRE ||
1048 IsA(rinfo->clause, ScalarArrayOpExpr))
1049 *found_clause = true;
1053 /* check the outer clauses */
1054 foreach(l, outer_clauses)
1056 RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
1058 Assert(IsA(rinfo, RestrictInfo));
1059 if (match_clause_to_indexcol(index,
1060 indexcol,
1061 curFamily,
1062 rinfo,
1063 outer_relids,
1064 saop_control))
1066 clausegroup = list_append_unique_ptr(clausegroup, rinfo);
1067 found_outer_clause = true;
1072 * If no clauses match this key, check for amoptionalkey restriction.
1074 if (clausegroup == NIL && !index->amoptionalkey && indexcol == 0)
1075 return NIL;
1077 clausegroup_list = lappend(clausegroup_list, clausegroup);
1079 indexcol++;
1080 families++;
1082 } while (!DoneMatchingIndexKeys(families));
1084 if (!*found_clause && !found_outer_clause)
1085 return NIL; /* no indexable clauses anywhere */
1087 return clausegroup_list;
1092 * match_clause_to_indexcol()
1093 * Determines whether a restriction clause matches a column of an index.
1095 * To match a normal index, the clause:
1097 * (1) must be in the form (indexkey op const) or (const op indexkey);
1098 * and
1099 * (2) must contain an operator which is in the same family as the index
1100 * operator for this column, or is a "special" operator as recognized
1101 * by match_special_index_operator().
1103 * Our definition of "const" is pretty liberal: we allow Vars belonging
1104 * to the caller-specified outer_relids relations (which had better not
1105 * include the relation whose index is being tested). outer_relids should
1106 * be NULL when checking simple restriction clauses, and the outer side
1107 * of the join when building a join inner scan. Other than that, the
1108 * only thing we don't like is volatile functions.
1110 * Note: in most cases we already know that the clause as a whole uses
1111 * vars from the interesting set of relations. The reason for the
1112 * outer_relids test is to reject clauses like (a.f1 OP (b.f2 OP a.f3));
1113 * that's not processable by an indexscan nestloop join on A, whereas
1114 * (a.f1 OP (b.f2 OP c.f3)) is.
1116 * Presently, the executor can only deal with indexquals that have the
1117 * indexkey on the left, so we can only use clauses that have the indexkey
1118 * on the right if we can commute the clause to put the key on the left.
1119 * We do not actually do the commuting here, but we check whether a
1120 * suitable commutator operator is available.
1122 * It is also possible to match RowCompareExpr clauses to indexes (but
1123 * currently, only btree indexes handle this). In this routine we will
1124 * report a match if the first column of the row comparison matches the
1125 * target index column. This is sufficient to guarantee that some index
1126 * condition can be constructed from the RowCompareExpr --- whether the
1127 * remaining columns match the index too is considered in
1128 * expand_indexqual_rowcompare().
1130 * It is also possible to match ScalarArrayOpExpr clauses to indexes, when
1131 * the clause is of the form "indexkey op ANY (arrayconst)". Since the
1132 * executor can only handle these in the context of bitmap index scans,
1133 * our caller specifies whether to allow these or not.
1135 * For boolean indexes, it is also possible to match the clause directly
1136 * to the indexkey; or perhaps the clause is (NOT indexkey).
1138 * 'index' is the index of interest.
1139 * 'indexcol' is a column number of 'index' (counting from 0).
1140 * 'opfamily' is the corresponding operator family.
1141 * 'rinfo' is the clause to be tested (as a RestrictInfo node).
1142 * 'saop_control' indicates whether ScalarArrayOpExpr clauses can be used.
1144 * Returns true if the clause can be used with this index key.
1146 * NOTE: returns false if clause is an OR or AND clause; it is the
1147 * responsibility of higher-level routines to cope with those.
1149 static bool
1150 match_clause_to_indexcol(IndexOptInfo *index,
1151 int indexcol,
1152 Oid opfamily,
1153 RestrictInfo *rinfo,
1154 Relids outer_relids,
1155 SaOpControl saop_control)
1157 Expr *clause = rinfo->clause;
1158 Node *leftop,
1159 *rightop;
1160 Relids left_relids;
1161 Relids right_relids;
1162 Oid expr_op;
1163 bool plain_op;
1166 * Never match pseudoconstants to indexes. (Normally this could not
1167 * happen anyway, since a pseudoconstant clause couldn't contain a Var,
1168 * but what if someone builds an expression index on a constant? It's not
1169 * totally unreasonable to do so with a partial index, either.)
1171 if (rinfo->pseudoconstant)
1172 return false;
1174 /* First check for boolean-index cases. */
1175 if (IsBooleanOpfamily(opfamily))
1177 if (match_boolean_index_clause((Node *) clause, indexcol, index))
1178 return true;
1182 * Clause must be a binary opclause, or possibly a ScalarArrayOpExpr
1183 * (which is always binary, by definition). Or it could be a
1184 * RowCompareExpr, which we pass off to match_rowcompare_to_indexcol().
1185 * Or, if the index supports it, we can handle IS NULL clauses.
1187 if (is_opclause(clause))
1189 leftop = get_leftop(clause);
1190 rightop = get_rightop(clause);
1191 if (!leftop || !rightop)
1192 return false;
1193 left_relids = rinfo->left_relids;
1194 right_relids = rinfo->right_relids;
1195 expr_op = ((OpExpr *) clause)->opno;
1196 plain_op = true;
1198 else if (saop_control != SAOP_FORBID &&
1199 clause && IsA(clause, ScalarArrayOpExpr))
1201 ScalarArrayOpExpr *saop = (ScalarArrayOpExpr *) clause;
1203 /* We only accept ANY clauses, not ALL */
1204 if (!saop->useOr)
1205 return false;
1206 leftop = (Node *) linitial(saop->args);
1207 rightop = (Node *) lsecond(saop->args);
1208 left_relids = NULL; /* not actually needed */
1209 right_relids = pull_varnos(rightop);
1210 expr_op = saop->opno;
1211 plain_op = false;
1213 else if (clause && IsA(clause, RowCompareExpr))
1215 return match_rowcompare_to_indexcol(index, indexcol, opfamily,
1216 (RowCompareExpr *) clause,
1217 outer_relids);
1219 else if (index->amsearchnulls && IsA(clause, NullTest))
1221 NullTest *nt = (NullTest *) clause;
1223 if (nt->nulltesttype == IS_NULL &&
1224 match_index_to_operand((Node *) nt->arg, indexcol, index))
1225 return true;
1226 return false;
1228 else
1229 return false;
1232 * Check for clauses of the form: (indexkey operator constant) or
1233 * (constant operator indexkey). See above notes about const-ness.
1235 if (match_index_to_operand(leftop, indexcol, index) &&
1236 bms_is_subset(right_relids, outer_relids) &&
1237 !contain_volatile_functions(rightop))
1239 if (is_indexable_operator(expr_op, opfamily, true))
1240 return true;
1243 * If we didn't find a member of the index's opfamily, see whether it
1244 * is a "special" indexable operator.
1246 if (plain_op &&
1247 match_special_index_operator(clause, opfamily, true))
1248 return true;
1249 return false;
1252 if (plain_op &&
1253 match_index_to_operand(rightop, indexcol, index) &&
1254 bms_is_subset(left_relids, outer_relids) &&
1255 !contain_volatile_functions(leftop))
1257 if (is_indexable_operator(expr_op, opfamily, false))
1258 return true;
1261 * If we didn't find a member of the index's opfamily, see whether it
1262 * is a "special" indexable operator.
1264 if (match_special_index_operator(clause, opfamily, false))
1265 return true;
1266 return false;
1269 return false;
1273 * is_indexable_operator
1274 * Does the operator match the specified index opfamily?
1276 * If the indexkey is on the right, what we actually want to know
1277 * is whether the operator has a commutator operator that matches
1278 * the opfamily.
1280 static bool
1281 is_indexable_operator(Oid expr_op, Oid opfamily, bool indexkey_on_left)
1283 /* Get the commuted operator if necessary */
1284 if (!indexkey_on_left)
1286 expr_op = get_commutator(expr_op);
1287 if (expr_op == InvalidOid)
1288 return false;
1291 /* OK if the (commuted) operator is a member of the index's opfamily */
1292 return op_in_opfamily(expr_op, opfamily);
1296 * match_rowcompare_to_indexcol()
1297 * Handles the RowCompareExpr case for match_clause_to_indexcol(),
1298 * which see for comments.
1300 static bool
1301 match_rowcompare_to_indexcol(IndexOptInfo *index,
1302 int indexcol,
1303 Oid opfamily,
1304 RowCompareExpr *clause,
1305 Relids outer_relids)
1307 Node *leftop,
1308 *rightop;
1309 Oid expr_op;
1311 /* Forget it if we're not dealing with a btree index */
1312 if (index->relam != BTREE_AM_OID)
1313 return false;
1316 * We could do the matching on the basis of insisting that the opfamily
1317 * shown in the RowCompareExpr be the same as the index column's opfamily,
1318 * but that could fail in the presence of reverse-sort opfamilies: it'd be
1319 * a matter of chance whether RowCompareExpr had picked the forward or
1320 * reverse-sort family. So look only at the operator, and match if it is
1321 * a member of the index's opfamily (after commutation, if the indexkey is
1322 * on the right). We'll worry later about whether any additional
1323 * operators are matchable to the index.
1325 leftop = (Node *) linitial(clause->largs);
1326 rightop = (Node *) linitial(clause->rargs);
1327 expr_op = linitial_oid(clause->opnos);
1330 * These syntactic tests are the same as in match_clause_to_indexcol()
1332 if (match_index_to_operand(leftop, indexcol, index) &&
1333 bms_is_subset(pull_varnos(rightop), outer_relids) &&
1334 !contain_volatile_functions(rightop))
1336 /* OK, indexkey is on left */
1338 else if (match_index_to_operand(rightop, indexcol, index) &&
1339 bms_is_subset(pull_varnos(leftop), outer_relids) &&
1340 !contain_volatile_functions(leftop))
1342 /* indexkey is on right, so commute the operator */
1343 expr_op = get_commutator(expr_op);
1344 if (expr_op == InvalidOid)
1345 return false;
1347 else
1348 return false;
1350 /* We're good if the operator is the right type of opfamily member */
1351 switch (get_op_opfamily_strategy(expr_op, opfamily))
1353 case BTLessStrategyNumber:
1354 case BTLessEqualStrategyNumber:
1355 case BTGreaterEqualStrategyNumber:
1356 case BTGreaterStrategyNumber:
1357 return true;
1360 return false;
1364 /****************************************************************************
1365 * ---- ROUTINES TO DO PARTIAL INDEX PREDICATE TESTS ----
1366 ****************************************************************************/
1369 * check_partial_indexes
1370 * Check each partial index of the relation, and mark it predOK or not
1371 * depending on whether the predicate is satisfied for this query.
1373 void
1374 check_partial_indexes(PlannerInfo *root, RelOptInfo *rel)
1376 List *restrictinfo_list = rel->baserestrictinfo;
1377 ListCell *ilist;
1379 foreach(ilist, rel->indexlist)
1381 IndexOptInfo *index = (IndexOptInfo *) lfirst(ilist);
1383 if (index->indpred == NIL)
1384 continue; /* ignore non-partial indexes */
1386 index->predOK = predicate_implied_by(index->indpred,
1387 restrictinfo_list);
1391 /****************************************************************************
1392 * ---- ROUTINES TO CHECK JOIN CLAUSES ----
1393 ****************************************************************************/
1396 * indexable_outerrelids
1397 * Finds all other relids that participate in any indexable join clause
1398 * for the specified table. Returns a set of relids.
1400 static Relids
1401 indexable_outerrelids(PlannerInfo *root, RelOptInfo *rel)
1403 Relids outer_relids = NULL;
1404 bool is_child_rel = (rel->reloptkind == RELOPT_OTHER_MEMBER_REL);
1405 ListCell *lc1;
1408 * Examine each joinclause in the joininfo list to see if it matches any
1409 * key of any index. If so, add the clause's other rels to the result.
1411 foreach(lc1, rel->joininfo)
1413 RestrictInfo *joininfo = (RestrictInfo *) lfirst(lc1);
1414 Relids other_rels;
1416 other_rels = bms_difference(joininfo->required_relids, rel->relids);
1417 if (matches_any_index(joininfo, rel, other_rels))
1418 outer_relids = bms_join(outer_relids, other_rels);
1419 else
1420 bms_free(other_rels);
1424 * We also have to look through the query's EquivalenceClasses to see if
1425 * any of them could generate indexable join conditions for this rel.
1427 if (rel->has_eclass_joins)
1429 foreach(lc1, root->eq_classes)
1431 EquivalenceClass *cur_ec = (EquivalenceClass *) lfirst(lc1);
1432 Relids other_rels = NULL;
1433 bool found_index = false;
1434 ListCell *lc2;
1437 * Won't generate joinclauses if const or single-member (the
1438 * latter test covers the volatile case too)
1440 if (cur_ec->ec_has_const || list_length(cur_ec->ec_members) <= 1)
1441 continue;
1444 * Note we don't test ec_broken; if we did, we'd need a separate
1445 * code path to look through ec_sources. Checking the members
1446 * anyway is OK as a possibly-overoptimistic heuristic.
1450 * No point in searching if rel not mentioned in eclass (but we
1451 * can't tell that for a child rel).
1453 if (!is_child_rel &&
1454 !bms_is_subset(rel->relids, cur_ec->ec_relids))
1455 continue;
1458 * Scan members, looking for both an index match and join
1459 * candidates
1461 foreach(lc2, cur_ec->ec_members)
1463 EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc2);
1465 /* Join candidate? */
1466 if (!cur_em->em_is_child &&
1467 !bms_overlap(cur_em->em_relids, rel->relids))
1469 other_rels = bms_add_members(other_rels,
1470 cur_em->em_relids);
1471 continue;
1474 /* Check for index match (only need one) */
1475 if (!found_index &&
1476 bms_equal(cur_em->em_relids, rel->relids) &&
1477 eclass_matches_any_index(cur_ec, cur_em, rel))
1478 found_index = true;
1481 if (found_index)
1482 outer_relids = bms_join(outer_relids, other_rels);
1483 else
1484 bms_free(other_rels);
1488 return outer_relids;
1492 * matches_any_index
1493 * Workhorse for indexable_outerrelids: see if a joinclause can be
1494 * matched to any index of the given rel.
1496 static bool
1497 matches_any_index(RestrictInfo *rinfo, RelOptInfo *rel, Relids outer_relids)
1499 ListCell *l;
1501 Assert(IsA(rinfo, RestrictInfo));
1503 if (restriction_is_or_clause(rinfo))
1505 foreach(l, ((BoolExpr *) rinfo->orclause)->args)
1507 Node *orarg = (Node *) lfirst(l);
1509 /* OR arguments should be ANDs or sub-RestrictInfos */
1510 if (and_clause(orarg))
1512 ListCell *j;
1514 /* Recurse to examine AND items and sub-ORs */
1515 foreach(j, ((BoolExpr *) orarg)->args)
1517 RestrictInfo *arinfo = (RestrictInfo *) lfirst(j);
1519 if (matches_any_index(arinfo, rel, outer_relids))
1520 return true;
1523 else
1525 /* Recurse to examine simple clause */
1526 Assert(IsA(orarg, RestrictInfo));
1527 Assert(!restriction_is_or_clause((RestrictInfo *) orarg));
1528 if (matches_any_index((RestrictInfo *) orarg, rel,
1529 outer_relids))
1530 return true;
1534 return false;
1537 /* Normal case for a simple restriction clause */
1538 foreach(l, rel->indexlist)
1540 IndexOptInfo *index = (IndexOptInfo *) lfirst(l);
1541 int indexcol = 0;
1542 Oid *families = index->opfamily;
1546 Oid curFamily = families[0];
1548 if (match_clause_to_indexcol(index,
1549 indexcol,
1550 curFamily,
1551 rinfo,
1552 outer_relids,
1553 SAOP_ALLOW))
1554 return true;
1556 indexcol++;
1557 families++;
1558 } while (!DoneMatchingIndexKeys(families));
1561 return false;
1565 * eclass_matches_any_index
1566 * Workhorse for indexable_outerrelids: see if an EquivalenceClass member
1567 * can be matched to any index column of the given rel.
1569 * This is also exported for use by find_eclass_clauses_for_index_join.
1571 bool
1572 eclass_matches_any_index(EquivalenceClass *ec, EquivalenceMember *em,
1573 RelOptInfo *rel)
1575 ListCell *l;
1577 foreach(l, rel->indexlist)
1579 IndexOptInfo *index = (IndexOptInfo *) lfirst(l);
1580 int indexcol = 0;
1581 Oid *families = index->opfamily;
1585 Oid curFamily = families[0];
1588 * If it's a btree index, we can reject it if its opfamily isn't
1589 * compatible with the EC, since no clause generated from the
1590 * EC could be used with the index. For non-btree indexes,
1591 * we can't easily tell whether clauses generated from the EC
1592 * could be used with the index, so only check for expression
1593 * match. This might mean we return "true" for a useless index,
1594 * but that will just cause some wasted planner cycles; it's
1595 * better than ignoring useful indexes.
1597 if ((index->relam != BTREE_AM_OID ||
1598 list_member_oid(ec->ec_opfamilies, curFamily)) &&
1599 match_index_to_operand((Node *) em->em_expr, indexcol, index))
1600 return true;
1602 indexcol++;
1603 families++;
1604 } while (!DoneMatchingIndexKeys(families));
1607 return false;
1612 * best_inner_indexscan
1613 * Finds the best available inner indexscans for a nestloop join
1614 * with the given rel on the inside and the given outer_rel outside.
1616 * *cheapest_startup gets the path with least startup cost
1617 * *cheapest_total gets the path with least total cost (often the same path)
1618 * Both are set to NULL if there are no possible inner indexscans.
1620 * We ignore ordering considerations, since a nestloop's inner scan's order
1621 * is uninteresting. Hence startup cost and total cost are the only figures
1622 * of merit to consider.
1624 * Note: create_index_paths() must have been run previously for this rel,
1625 * else the results will always be NULL.
1627 void
1628 best_inner_indexscan(PlannerInfo *root, RelOptInfo *rel,
1629 RelOptInfo *outer_rel, JoinType jointype,
1630 Path **cheapest_startup, Path **cheapest_total)
1632 Relids outer_relids;
1633 bool isouterjoin;
1634 List *clause_list;
1635 List *indexpaths;
1636 List *bitindexpaths;
1637 ListCell *l;
1638 InnerIndexscanInfo *info;
1639 MemoryContext oldcontext;
1641 /* Initialize results for failure returns */
1642 *cheapest_startup = *cheapest_total = NULL;
1645 * Nestloop only supports inner, left, semi, and anti joins.
1647 switch (jointype)
1649 case JOIN_INNER:
1650 isouterjoin = false;
1651 break;
1652 case JOIN_LEFT:
1653 case JOIN_SEMI:
1654 case JOIN_ANTI:
1655 isouterjoin = true;
1656 break;
1657 default:
1658 return;
1662 * If there are no indexable joinclauses for this rel, exit quickly.
1664 if (bms_is_empty(rel->index_outer_relids))
1665 return;
1668 * Otherwise, we have to do path selection in the main planning context,
1669 * so that any created path can be safely attached to the rel's cache of
1670 * best inner paths. (This is not currently an issue for normal planning,
1671 * but it is an issue for GEQO planning.)
1673 oldcontext = MemoryContextSwitchTo(root->planner_cxt);
1676 * Intersect the given outer relids with index_outer_relids to find the
1677 * set of outer relids actually relevant for this rel. If there are none,
1678 * again we can fail immediately.
1680 outer_relids = bms_intersect(rel->index_outer_relids, outer_rel->relids);
1681 if (bms_is_empty(outer_relids))
1683 bms_free(outer_relids);
1684 MemoryContextSwitchTo(oldcontext);
1685 return;
1689 * Look to see if we already computed the result for this set of relevant
1690 * outerrels. (We include the isouterjoin status in the cache lookup key
1691 * for safety. In practice I suspect this is not necessary because it
1692 * should always be the same for a given combination of rels.)
1694 * NOTE: because we cache on outer_relids rather than outer_rel->relids,
1695 * we will report the same paths and hence path cost for joins with
1696 * different sets of irrelevant rels on the outside. Now that cost_index
1697 * is sensitive to outer_rel->rows, this is not really right. However the
1698 * error is probably not large. Is it worth establishing a separate cache
1699 * entry for each distinct outer_rel->relids set to get this right?
1701 foreach(l, rel->index_inner_paths)
1703 info = (InnerIndexscanInfo *) lfirst(l);
1704 if (bms_equal(info->other_relids, outer_relids) &&
1705 info->isouterjoin == isouterjoin)
1707 bms_free(outer_relids);
1708 MemoryContextSwitchTo(oldcontext);
1709 *cheapest_startup = info->cheapest_startup_innerpath;
1710 *cheapest_total = info->cheapest_total_innerpath;
1711 return;
1716 * Find all the relevant restriction and join clauses.
1718 * Note: because we include restriction clauses, we will find indexscans
1719 * that could be plain indexscans, ie, they don't require the join context
1720 * at all. This may seem redundant, but we need to include those scans in
1721 * the input given to choose_bitmap_and() to be sure we find optimal AND
1722 * combinations of join and non-join scans. Also, even if the "best inner
1723 * indexscan" is just a plain indexscan, it will have a different cost
1724 * estimate because of cache effects.
1726 clause_list = find_clauses_for_join(root, rel, outer_relids, isouterjoin);
1729 * Find all the index paths that are usable for this join, except for
1730 * stuff involving OR and ScalarArrayOpExpr clauses.
1732 indexpaths = find_usable_indexes(root, rel,
1733 clause_list, NIL,
1734 false, outer_rel,
1735 SAOP_FORBID);
1738 * Generate BitmapOrPaths for any suitable OR-clauses present in the
1739 * clause list.
1741 bitindexpaths = generate_bitmap_or_paths(root, rel,
1742 clause_list, NIL,
1743 outer_rel);
1746 * Likewise, generate paths using ScalarArrayOpExpr clauses; these can't
1747 * be simple indexscans but they can be used in bitmap scans.
1749 bitindexpaths = list_concat(bitindexpaths,
1750 find_saop_paths(root, rel,
1751 clause_list, NIL,
1752 false, outer_rel));
1755 * Include the regular index paths in bitindexpaths.
1757 bitindexpaths = list_concat(bitindexpaths, list_copy(indexpaths));
1760 * If we found anything usable, generate a BitmapHeapPath for the most
1761 * promising combination of bitmap index paths.
1763 if (bitindexpaths != NIL)
1765 Path *bitmapqual;
1766 BitmapHeapPath *bpath;
1768 bitmapqual = choose_bitmap_and(root, rel, bitindexpaths, outer_rel);
1769 bpath = create_bitmap_heap_path(root, rel, bitmapqual, outer_rel);
1770 indexpaths = lappend(indexpaths, bpath);
1774 * Now choose the cheapest members of indexpaths.
1776 if (indexpaths != NIL)
1778 *cheapest_startup = *cheapest_total = (Path *) linitial(indexpaths);
1780 for_each_cell(l, lnext(list_head(indexpaths)))
1782 Path *path = (Path *) lfirst(l);
1784 if (compare_path_costs(path, *cheapest_startup, STARTUP_COST) < 0)
1785 *cheapest_startup = path;
1786 if (compare_path_costs(path, *cheapest_total, TOTAL_COST) < 0)
1787 *cheapest_total = path;
1791 /* Cache the results --- whether positive or negative */
1792 info = makeNode(InnerIndexscanInfo);
1793 info->other_relids = outer_relids;
1794 info->isouterjoin = isouterjoin;
1795 info->cheapest_startup_innerpath = *cheapest_startup;
1796 info->cheapest_total_innerpath = *cheapest_total;
1797 rel->index_inner_paths = lcons(info, rel->index_inner_paths);
1799 MemoryContextSwitchTo(oldcontext);
1803 * find_clauses_for_join
1804 * Generate a list of clauses that are potentially useful for
1805 * scanning rel as the inner side of a nestloop join.
1807 * We consider both join and restriction clauses. Any joinclause that uses
1808 * only otherrels in the specified outer_relids is fair game. But there must
1809 * be at least one such joinclause in the final list, otherwise we return NIL
1810 * indicating that there isn't any potential win here.
1812 static List *
1813 find_clauses_for_join(PlannerInfo *root, RelOptInfo *rel,
1814 Relids outer_relids, bool isouterjoin)
1816 List *clause_list = NIL;
1817 Relids join_relids;
1818 ListCell *l;
1821 * Look for joinclauses that are usable with given outer_relids. Note
1822 * we'll take anything that's applicable to the join whether it has
1823 * anything to do with an index or not; since we're only building a list,
1824 * it's not worth filtering more finely here.
1826 join_relids = bms_union(rel->relids, outer_relids);
1828 foreach(l, rel->joininfo)
1830 RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
1832 /* Can't use pushed-down join clauses in outer join */
1833 if (isouterjoin && rinfo->is_pushed_down)
1834 continue;
1835 if (!bms_is_subset(rinfo->required_relids, join_relids))
1836 continue;
1838 clause_list = lappend(clause_list, rinfo);
1841 bms_free(join_relids);
1844 * Also check to see if any EquivalenceClasses can produce a relevant
1845 * joinclause. Since all such clauses are effectively pushed-down, this
1846 * doesn't apply to outer joins.
1848 if (!isouterjoin && rel->has_eclass_joins)
1849 clause_list = list_concat(clause_list,
1850 find_eclass_clauses_for_index_join(root,
1851 rel,
1852 outer_relids));
1854 /* If no join clause was matched then forget it, per comments above */
1855 if (clause_list == NIL)
1856 return NIL;
1858 /* We can also use any plain restriction clauses for the rel */
1859 clause_list = list_concat(list_copy(rel->baserestrictinfo), clause_list);
1861 return clause_list;
1865 /****************************************************************************
1866 * ---- PATH CREATION UTILITIES ----
1867 ****************************************************************************/
1870 * flatten_clausegroups_list
1871 * Given a list of lists of RestrictInfos, flatten it to a list
1872 * of RestrictInfos.
1874 * This is used to flatten out the result of group_clauses_by_indexkey()
1875 * to produce an indexclauses list. The original list structure mustn't
1876 * be altered, but it's OK to share copies of the underlying RestrictInfos.
1878 List *
1879 flatten_clausegroups_list(List *clausegroups)
1881 List *allclauses = NIL;
1882 ListCell *l;
1884 foreach(l, clausegroups)
1885 allclauses = list_concat(allclauses, list_copy((List *) lfirst(l)));
1886 return allclauses;
1890 /****************************************************************************
1891 * ---- ROUTINES TO CHECK OPERANDS ----
1892 ****************************************************************************/
1895 * match_index_to_operand()
1896 * Generalized test for a match between an index's key
1897 * and the operand on one side of a restriction or join clause.
1899 * operand: the nodetree to be compared to the index
1900 * indexcol: the column number of the index (counting from 0)
1901 * index: the index of interest
1903 bool
1904 match_index_to_operand(Node *operand,
1905 int indexcol,
1906 IndexOptInfo *index)
1908 int indkey;
1911 * Ignore any RelabelType node above the operand. This is needed to be
1912 * able to apply indexscanning in binary-compatible-operator cases. Note:
1913 * we can assume there is at most one RelabelType node;
1914 * eval_const_expressions() will have simplified if more than one.
1916 if (operand && IsA(operand, RelabelType))
1917 operand = (Node *) ((RelabelType *) operand)->arg;
1919 indkey = index->indexkeys[indexcol];
1920 if (indkey != 0)
1923 * Simple index column; operand must be a matching Var.
1925 if (operand && IsA(operand, Var) &&
1926 index->rel->relid == ((Var *) operand)->varno &&
1927 indkey == ((Var *) operand)->varattno)
1928 return true;
1930 else
1933 * Index expression; find the correct expression. (This search could
1934 * be avoided, at the cost of complicating all the callers of this
1935 * routine; doesn't seem worth it.)
1937 ListCell *indexpr_item;
1938 int i;
1939 Node *indexkey;
1941 indexpr_item = list_head(index->indexprs);
1942 for (i = 0; i < indexcol; i++)
1944 if (index->indexkeys[i] == 0)
1946 if (indexpr_item == NULL)
1947 elog(ERROR, "wrong number of index expressions");
1948 indexpr_item = lnext(indexpr_item);
1951 if (indexpr_item == NULL)
1952 elog(ERROR, "wrong number of index expressions");
1953 indexkey = (Node *) lfirst(indexpr_item);
1956 * Does it match the operand? Again, strip any relabeling.
1958 if (indexkey && IsA(indexkey, RelabelType))
1959 indexkey = (Node *) ((RelabelType *) indexkey)->arg;
1961 if (equal(indexkey, operand))
1962 return true;
1965 return false;
1968 /****************************************************************************
1969 * ---- ROUTINES FOR "SPECIAL" INDEXABLE OPERATORS ----
1970 ****************************************************************************/
1972 /*----------
1973 * These routines handle special optimization of operators that can be
1974 * used with index scans even though they are not known to the executor's
1975 * indexscan machinery. The key idea is that these operators allow us
1976 * to derive approximate indexscan qual clauses, such that any tuples
1977 * that pass the operator clause itself must also satisfy the simpler
1978 * indexscan condition(s). Then we can use the indexscan machinery
1979 * to avoid scanning as much of the table as we'd otherwise have to,
1980 * while applying the original operator as a qpqual condition to ensure
1981 * we deliver only the tuples we want. (In essence, we're using a regular
1982 * index as if it were a lossy index.)
1984 * An example of what we're doing is
1985 * textfield LIKE 'abc%'
1986 * from which we can generate the indexscanable conditions
1987 * textfield >= 'abc' AND textfield < 'abd'
1988 * which allow efficient scanning of an index on textfield.
1989 * (In reality, character set and collation issues make the transformation
1990 * from LIKE to indexscan limits rather harder than one might think ...
1991 * but that's the basic idea.)
1993 * Another thing that we do with this machinery is to provide special
1994 * smarts for "boolean" indexes (that is, indexes on boolean columns
1995 * that support boolean equality). We can transform a plain reference
1996 * to the indexkey into "indexkey = true", or "NOT indexkey" into
1997 * "indexkey = false", so as to make the expression indexable using the
1998 * regular index operators. (As of Postgres 8.1, we must do this here
1999 * because constant simplification does the reverse transformation;
2000 * without this code there'd be no way to use such an index at all.)
2002 * Three routines are provided here:
2004 * match_special_index_operator() is just an auxiliary function for
2005 * match_clause_to_indexcol(); after the latter fails to recognize a
2006 * restriction opclause's operator as a member of an index's opfamily,
2007 * it asks match_special_index_operator() whether the clause should be
2008 * considered an indexqual anyway.
2010 * match_boolean_index_clause() similarly detects clauses that can be
2011 * converted into boolean equality operators.
2013 * expand_indexqual_conditions() converts a list of lists of RestrictInfo
2014 * nodes (with implicit AND semantics across list elements) into
2015 * a list of clauses that the executor can actually handle. For operators
2016 * that are members of the index's opfamily this transformation is a no-op,
2017 * but clauses recognized by match_special_index_operator() or
2018 * match_boolean_index_clause() must be converted into one or more "regular"
2019 * indexqual conditions.
2020 *----------
2024 * match_boolean_index_clause
2025 * Recognize restriction clauses that can be matched to a boolean index.
2027 * This should be called only when IsBooleanOpfamily() recognizes the
2028 * index's operator family. We check to see if the clause matches the
2029 * index's key.
2031 static bool
2032 match_boolean_index_clause(Node *clause,
2033 int indexcol,
2034 IndexOptInfo *index)
2036 /* Direct match? */
2037 if (match_index_to_operand(clause, indexcol, index))
2038 return true;
2039 /* NOT clause? */
2040 if (not_clause(clause))
2042 if (match_index_to_operand((Node *) get_notclausearg((Expr *) clause),
2043 indexcol, index))
2044 return true;
2048 * Since we only consider clauses at top level of WHERE, we can convert
2049 * indexkey IS TRUE and indexkey IS FALSE to index searches as well. The
2050 * different meaning for NULL isn't important.
2052 else if (clause && IsA(clause, BooleanTest))
2054 BooleanTest *btest = (BooleanTest *) clause;
2056 if (btest->booltesttype == IS_TRUE ||
2057 btest->booltesttype == IS_FALSE)
2058 if (match_index_to_operand((Node *) btest->arg,
2059 indexcol, index))
2060 return true;
2062 return false;
2066 * match_special_index_operator
2067 * Recognize restriction clauses that can be used to generate
2068 * additional indexscanable qualifications.
2070 * The given clause is already known to be a binary opclause having
2071 * the form (indexkey OP pseudoconst) or (pseudoconst OP indexkey),
2072 * but the OP proved not to be one of the index's opfamily operators.
2073 * Return 'true' if we can do something with it anyway.
2075 static bool
2076 match_special_index_operator(Expr *clause, Oid opfamily,
2077 bool indexkey_on_left)
2079 bool isIndexable = false;
2080 Node *rightop;
2081 Oid expr_op;
2082 Const *patt;
2083 Const *prefix = NULL;
2084 Const *rest = NULL;
2087 * Currently, all known special operators require the indexkey on the
2088 * left, but this test could be pushed into the switch statement if some
2089 * are added that do not...
2091 if (!indexkey_on_left)
2092 return false;
2094 /* we know these will succeed */
2095 rightop = get_rightop(clause);
2096 expr_op = ((OpExpr *) clause)->opno;
2098 /* again, required for all current special ops: */
2099 if (!IsA(rightop, Const) ||
2100 ((Const *) rightop)->constisnull)
2101 return false;
2102 patt = (Const *) rightop;
2104 switch (expr_op)
2106 case OID_TEXT_LIKE_OP:
2107 case OID_BPCHAR_LIKE_OP:
2108 case OID_NAME_LIKE_OP:
2109 /* the right-hand const is type text for all of these */
2110 isIndexable = pattern_fixed_prefix(patt, Pattern_Type_Like,
2111 &prefix, &rest) != Pattern_Prefix_None;
2112 break;
2114 case OID_BYTEA_LIKE_OP:
2115 isIndexable = pattern_fixed_prefix(patt, Pattern_Type_Like,
2116 &prefix, &rest) != Pattern_Prefix_None;
2117 break;
2119 case OID_TEXT_ICLIKE_OP:
2120 case OID_BPCHAR_ICLIKE_OP:
2121 case OID_NAME_ICLIKE_OP:
2122 /* the right-hand const is type text for all of these */
2123 isIndexable = pattern_fixed_prefix(patt, Pattern_Type_Like_IC,
2124 &prefix, &rest) != Pattern_Prefix_None;
2125 break;
2127 case OID_TEXT_REGEXEQ_OP:
2128 case OID_BPCHAR_REGEXEQ_OP:
2129 case OID_NAME_REGEXEQ_OP:
2130 /* the right-hand const is type text for all of these */
2131 isIndexable = pattern_fixed_prefix(patt, Pattern_Type_Regex,
2132 &prefix, &rest) != Pattern_Prefix_None;
2133 break;
2135 case OID_TEXT_ICREGEXEQ_OP:
2136 case OID_BPCHAR_ICREGEXEQ_OP:
2137 case OID_NAME_ICREGEXEQ_OP:
2138 /* the right-hand const is type text for all of these */
2139 isIndexable = pattern_fixed_prefix(patt, Pattern_Type_Regex_IC,
2140 &prefix, &rest) != Pattern_Prefix_None;
2141 break;
2143 case OID_INET_SUB_OP:
2144 case OID_INET_SUBEQ_OP:
2145 isIndexable = true;
2146 break;
2149 if (prefix)
2151 pfree(DatumGetPointer(prefix->constvalue));
2152 pfree(prefix);
2155 /* done if the expression doesn't look indexable */
2156 if (!isIndexable)
2157 return false;
2160 * Must also check that index's opfamily supports the operators we will
2161 * want to apply. (A hash index, for example, will not support ">=".)
2162 * Currently, only btree supports the operators we need.
2164 * We insist on the opfamily being the specific one we expect, else we'd
2165 * do the wrong thing if someone were to make a reverse-sort opfamily with
2166 * the same operators.
2168 switch (expr_op)
2170 case OID_TEXT_LIKE_OP:
2171 case OID_TEXT_ICLIKE_OP:
2172 case OID_TEXT_REGEXEQ_OP:
2173 case OID_TEXT_ICREGEXEQ_OP:
2174 isIndexable =
2175 (opfamily == TEXT_PATTERN_BTREE_FAM_OID) ||
2176 (opfamily == TEXT_BTREE_FAM_OID && lc_collate_is_c());
2177 break;
2179 case OID_BPCHAR_LIKE_OP:
2180 case OID_BPCHAR_ICLIKE_OP:
2181 case OID_BPCHAR_REGEXEQ_OP:
2182 case OID_BPCHAR_ICREGEXEQ_OP:
2183 isIndexable =
2184 (opfamily == BPCHAR_PATTERN_BTREE_FAM_OID) ||
2185 (opfamily == BPCHAR_BTREE_FAM_OID && lc_collate_is_c());
2186 break;
2188 case OID_NAME_LIKE_OP:
2189 case OID_NAME_ICLIKE_OP:
2190 case OID_NAME_REGEXEQ_OP:
2191 case OID_NAME_ICREGEXEQ_OP:
2192 /* name uses locale-insensitive sorting */
2193 isIndexable = (opfamily == NAME_BTREE_FAM_OID);
2194 break;
2196 case OID_BYTEA_LIKE_OP:
2197 isIndexable = (opfamily == BYTEA_BTREE_FAM_OID);
2198 break;
2200 case OID_INET_SUB_OP:
2201 case OID_INET_SUBEQ_OP:
2202 isIndexable = (opfamily == NETWORK_BTREE_FAM_OID);
2203 break;
2206 return isIndexable;
2210 * expand_indexqual_conditions
2211 * Given a list of sublists of RestrictInfo nodes, produce a flat list
2212 * of index qual clauses. Standard qual clauses (those in the index's
2213 * opfamily) are passed through unchanged. Boolean clauses and "special"
2214 * index operators are expanded into clauses that the indexscan machinery
2215 * will know what to do with. RowCompare clauses are simplified if
2216 * necessary to create a clause that is fully checkable by the index.
2218 * The input list is ordered by index key, and so the output list is too.
2219 * (The latter is not depended on by any part of the core planner, I believe,
2220 * but parts of the executor require it, and so do the amcostestimate
2221 * functions.)
2223 List *
2224 expand_indexqual_conditions(IndexOptInfo *index, List *clausegroups)
2226 List *resultquals = NIL;
2227 ListCell *clausegroup_item;
2228 int indexcol = 0;
2229 Oid *families = index->opfamily;
2231 if (clausegroups == NIL)
2232 return NIL;
2234 clausegroup_item = list_head(clausegroups);
2237 Oid curFamily = families[0];
2238 ListCell *l;
2240 foreach(l, (List *) lfirst(clausegroup_item))
2242 RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
2243 Expr *clause = rinfo->clause;
2245 /* First check for boolean cases */
2246 if (IsBooleanOpfamily(curFamily))
2248 Expr *boolqual;
2250 boolqual = expand_boolean_index_clause((Node *) clause,
2251 indexcol,
2252 index);
2253 if (boolqual)
2255 resultquals = lappend(resultquals,
2256 make_restrictinfo(boolqual,
2257 true,
2258 false,
2259 false,
2260 NULL));
2261 continue;
2266 * Else it must be an opclause (usual case), ScalarArrayOp,
2267 * RowCompare, or NullTest
2269 if (is_opclause(clause))
2271 resultquals = list_concat(resultquals,
2272 expand_indexqual_opclause(rinfo,
2273 curFamily));
2275 else if (IsA(clause, ScalarArrayOpExpr))
2277 /* no extra work at this time */
2278 resultquals = lappend(resultquals, rinfo);
2280 else if (IsA(clause, RowCompareExpr))
2282 resultquals = lappend(resultquals,
2283 expand_indexqual_rowcompare(rinfo,
2284 index,
2285 indexcol));
2287 else if (IsA(clause, NullTest))
2289 Assert(index->amsearchnulls);
2290 resultquals = lappend(resultquals,
2291 make_restrictinfo(clause,
2292 true,
2293 false,
2294 false,
2295 NULL));
2297 else
2298 elog(ERROR, "unsupported indexqual type: %d",
2299 (int) nodeTag(clause));
2302 clausegroup_item = lnext(clausegroup_item);
2304 indexcol++;
2305 families++;
2306 } while (clausegroup_item != NULL && !DoneMatchingIndexKeys(families));
2308 Assert(clausegroup_item == NULL); /* else more groups than indexkeys */
2310 return resultquals;
2314 * expand_boolean_index_clause
2315 * Convert a clause recognized by match_boolean_index_clause into
2316 * a boolean equality operator clause.
2318 * Returns NULL if the clause isn't a boolean index qual.
2320 static Expr *
2321 expand_boolean_index_clause(Node *clause,
2322 int indexcol,
2323 IndexOptInfo *index)
2325 /* Direct match? */
2326 if (match_index_to_operand(clause, indexcol, index))
2328 /* convert to indexkey = TRUE */
2329 return make_opclause(BooleanEqualOperator, BOOLOID, false,
2330 (Expr *) clause,
2331 (Expr *) makeBoolConst(true, false));
2333 /* NOT clause? */
2334 if (not_clause(clause))
2336 Node *arg = (Node *) get_notclausearg((Expr *) clause);
2338 /* It must have matched the indexkey */
2339 Assert(match_index_to_operand(arg, indexcol, index));
2340 /* convert to indexkey = FALSE */
2341 return make_opclause(BooleanEqualOperator, BOOLOID, false,
2342 (Expr *) arg,
2343 (Expr *) makeBoolConst(false, false));
2345 if (clause && IsA(clause, BooleanTest))
2347 BooleanTest *btest = (BooleanTest *) clause;
2348 Node *arg = (Node *) btest->arg;
2350 /* It must have matched the indexkey */
2351 Assert(match_index_to_operand(arg, indexcol, index));
2352 if (btest->booltesttype == IS_TRUE)
2354 /* convert to indexkey = TRUE */
2355 return make_opclause(BooleanEqualOperator, BOOLOID, false,
2356 (Expr *) arg,
2357 (Expr *) makeBoolConst(true, false));
2359 if (btest->booltesttype == IS_FALSE)
2361 /* convert to indexkey = FALSE */
2362 return make_opclause(BooleanEqualOperator, BOOLOID, false,
2363 (Expr *) arg,
2364 (Expr *) makeBoolConst(false, false));
2366 /* Oops */
2367 Assert(false);
2370 return NULL;
2374 * expand_indexqual_opclause --- expand a single indexqual condition
2375 * that is an operator clause
2377 * The input is a single RestrictInfo, the output a list of RestrictInfos.
2379 * In the base case this is just list_make1(), but we have to be prepared to
2380 * expand special cases that were accepted by match_special_index_operator().
2382 static List *
2383 expand_indexqual_opclause(RestrictInfo *rinfo, Oid opfamily)
2385 Expr *clause = rinfo->clause;
2387 /* we know these will succeed */
2388 Node *leftop = get_leftop(clause);
2389 Node *rightop = get_rightop(clause);
2390 Oid expr_op = ((OpExpr *) clause)->opno;
2391 Const *patt = (Const *) rightop;
2392 Const *prefix = NULL;
2393 Const *rest = NULL;
2394 Pattern_Prefix_Status pstatus;
2397 * LIKE and regex operators are not members of any btree index opfamily,
2398 * but they can be members of opfamilies for more exotic index types such
2399 * as GIN. Therefore, we should only do expansion if the operator is
2400 * actually not in the opfamily. But checking that requires a syscache
2401 * lookup, so it's best to first see if the operator is one we are
2402 * interested in.
2404 switch (expr_op)
2406 case OID_TEXT_LIKE_OP:
2407 case OID_BPCHAR_LIKE_OP:
2408 case OID_NAME_LIKE_OP:
2409 case OID_BYTEA_LIKE_OP:
2410 if (!op_in_opfamily(expr_op, opfamily))
2412 pstatus = pattern_fixed_prefix(patt, Pattern_Type_Like,
2413 &prefix, &rest);
2414 return prefix_quals(leftop, opfamily, prefix, pstatus);
2416 break;
2418 case OID_TEXT_ICLIKE_OP:
2419 case OID_BPCHAR_ICLIKE_OP:
2420 case OID_NAME_ICLIKE_OP:
2421 if (!op_in_opfamily(expr_op, opfamily))
2423 /* the right-hand const is type text for all of these */
2424 pstatus = pattern_fixed_prefix(patt, Pattern_Type_Like_IC,
2425 &prefix, &rest);
2426 return prefix_quals(leftop, opfamily, prefix, pstatus);
2428 break;
2430 case OID_TEXT_REGEXEQ_OP:
2431 case OID_BPCHAR_REGEXEQ_OP:
2432 case OID_NAME_REGEXEQ_OP:
2433 if (!op_in_opfamily(expr_op, opfamily))
2435 /* the right-hand const is type text for all of these */
2436 pstatus = pattern_fixed_prefix(patt, Pattern_Type_Regex,
2437 &prefix, &rest);
2438 return prefix_quals(leftop, opfamily, prefix, pstatus);
2440 break;
2442 case OID_TEXT_ICREGEXEQ_OP:
2443 case OID_BPCHAR_ICREGEXEQ_OP:
2444 case OID_NAME_ICREGEXEQ_OP:
2445 if (!op_in_opfamily(expr_op, opfamily))
2447 /* the right-hand const is type text for all of these */
2448 pstatus = pattern_fixed_prefix(patt, Pattern_Type_Regex_IC,
2449 &prefix, &rest);
2450 return prefix_quals(leftop, opfamily, prefix, pstatus);
2452 break;
2454 case OID_INET_SUB_OP:
2455 case OID_INET_SUBEQ_OP:
2456 if (!op_in_opfamily(expr_op, opfamily))
2458 return network_prefix_quals(leftop, expr_op, opfamily,
2459 patt->constvalue);
2461 break;
2464 /* Default case: just make a list of the unmodified indexqual */
2465 return list_make1(rinfo);
2469 * expand_indexqual_rowcompare --- expand a single indexqual condition
2470 * that is a RowCompareExpr
2472 * It's already known that the first column of the row comparison matches
2473 * the specified column of the index. We can use additional columns of the
2474 * row comparison as index qualifications, so long as they match the index
2475 * in the "same direction", ie, the indexkeys are all on the same side of the
2476 * clause and the operators are all the same-type members of the opfamilies.
2477 * If all the columns of the RowCompareExpr match in this way, we just use it
2478 * as-is. Otherwise, we build a shortened RowCompareExpr (if more than one
2479 * column matches) or a simple OpExpr (if the first-column match is all
2480 * there is). In these cases the modified clause is always "<=" or ">="
2481 * even when the original was "<" or ">" --- this is necessary to match all
2482 * the rows that could match the original. (We are essentially building a
2483 * lossy version of the row comparison when we do this.)
2485 static RestrictInfo *
2486 expand_indexqual_rowcompare(RestrictInfo *rinfo,
2487 IndexOptInfo *index,
2488 int indexcol)
2490 RowCompareExpr *clause = (RowCompareExpr *) rinfo->clause;
2491 bool var_on_left;
2492 int op_strategy;
2493 Oid op_lefttype;
2494 Oid op_righttype;
2495 int matching_cols;
2496 Oid expr_op;
2497 List *opfamilies;
2498 List *lefttypes;
2499 List *righttypes;
2500 List *new_ops;
2501 ListCell *largs_cell;
2502 ListCell *rargs_cell;
2503 ListCell *opnos_cell;
2505 /* We have to figure out (again) how the first col matches */
2506 var_on_left = match_index_to_operand((Node *) linitial(clause->largs),
2507 indexcol, index);
2508 Assert(var_on_left ||
2509 match_index_to_operand((Node *) linitial(clause->rargs),
2510 indexcol, index));
2511 expr_op = linitial_oid(clause->opnos);
2512 if (!var_on_left)
2513 expr_op = get_commutator(expr_op);
2514 get_op_opfamily_properties(expr_op, index->opfamily[indexcol],
2515 &op_strategy,
2516 &op_lefttype,
2517 &op_righttype);
2518 /* Build lists of the opfamilies and operator datatypes in case needed */
2519 opfamilies = list_make1_oid(index->opfamily[indexcol]);
2520 lefttypes = list_make1_oid(op_lefttype);
2521 righttypes = list_make1_oid(op_righttype);
2524 * See how many of the remaining columns match some index column in the
2525 * same way. A note about rel membership tests: we assume that the clause
2526 * as a whole is already known to use only Vars from the indexed relation
2527 * and possibly some acceptable outer relations. So the "other" side of
2528 * any potential index condition is OK as long as it doesn't use Vars from
2529 * the indexed relation.
2531 matching_cols = 1;
2532 largs_cell = lnext(list_head(clause->largs));
2533 rargs_cell = lnext(list_head(clause->rargs));
2534 opnos_cell = lnext(list_head(clause->opnos));
2536 while (largs_cell != NULL)
2538 Node *varop;
2539 Node *constop;
2540 int i;
2542 expr_op = lfirst_oid(opnos_cell);
2543 if (var_on_left)
2545 varop = (Node *) lfirst(largs_cell);
2546 constop = (Node *) lfirst(rargs_cell);
2548 else
2550 varop = (Node *) lfirst(rargs_cell);
2551 constop = (Node *) lfirst(largs_cell);
2552 /* indexkey is on right, so commute the operator */
2553 expr_op = get_commutator(expr_op);
2554 if (expr_op == InvalidOid)
2555 break; /* operator is not usable */
2557 if (bms_is_member(index->rel->relid, pull_varnos(constop)))
2558 break; /* no good, Var on wrong side */
2559 if (contain_volatile_functions(constop))
2560 break; /* no good, volatile comparison value */
2563 * The Var side can match any column of the index. If the user does
2564 * something weird like having multiple identical index columns, we
2565 * insist the match be on the first such column, to avoid confusing
2566 * the executor.
2568 for (i = 0; i < index->ncolumns; i++)
2570 if (match_index_to_operand(varop, i, index))
2571 break;
2573 if (i >= index->ncolumns)
2574 break; /* no match found */
2576 /* Now, do we have the right operator for this column? */
2577 if (get_op_opfamily_strategy(expr_op, index->opfamily[i])
2578 != op_strategy)
2579 break;
2581 /* Add opfamily and datatypes to lists */
2582 get_op_opfamily_properties(expr_op, index->opfamily[i],
2583 &op_strategy,
2584 &op_lefttype,
2585 &op_righttype);
2586 opfamilies = lappend_oid(opfamilies, index->opfamily[i]);
2587 lefttypes = lappend_oid(lefttypes, op_lefttype);
2588 righttypes = lappend_oid(righttypes, op_righttype);
2590 /* This column matches, keep scanning */
2591 matching_cols++;
2592 largs_cell = lnext(largs_cell);
2593 rargs_cell = lnext(rargs_cell);
2594 opnos_cell = lnext(opnos_cell);
2597 /* Return clause as-is if it's all usable as index quals */
2598 if (matching_cols == list_length(clause->opnos))
2599 return rinfo;
2602 * We have to generate a subset rowcompare (possibly just one OpExpr). The
2603 * painful part of this is changing < to <= or > to >=, so deal with that
2604 * first.
2606 if (op_strategy == BTLessEqualStrategyNumber ||
2607 op_strategy == BTGreaterEqualStrategyNumber)
2609 /* easy, just use the same operators */
2610 new_ops = list_truncate(list_copy(clause->opnos), matching_cols);
2612 else
2614 ListCell *opfamilies_cell;
2615 ListCell *lefttypes_cell;
2616 ListCell *righttypes_cell;
2618 if (op_strategy == BTLessStrategyNumber)
2619 op_strategy = BTLessEqualStrategyNumber;
2620 else if (op_strategy == BTGreaterStrategyNumber)
2621 op_strategy = BTGreaterEqualStrategyNumber;
2622 else
2623 elog(ERROR, "unexpected strategy number %d", op_strategy);
2624 new_ops = NIL;
2625 lefttypes_cell = list_head(lefttypes);
2626 righttypes_cell = list_head(righttypes);
2627 foreach(opfamilies_cell, opfamilies)
2629 Oid opfam = lfirst_oid(opfamilies_cell);
2630 Oid lefttype = lfirst_oid(lefttypes_cell);
2631 Oid righttype = lfirst_oid(righttypes_cell);
2633 expr_op = get_opfamily_member(opfam, lefttype, righttype,
2634 op_strategy);
2635 if (!OidIsValid(expr_op)) /* should not happen */
2636 elog(ERROR, "could not find member %d(%u,%u) of opfamily %u",
2637 op_strategy, lefttype, righttype, opfam);
2638 if (!var_on_left)
2640 expr_op = get_commutator(expr_op);
2641 if (!OidIsValid(expr_op)) /* should not happen */
2642 elog(ERROR, "could not find commutator of member %d(%u,%u) of opfamily %u",
2643 op_strategy, lefttype, righttype, opfam);
2645 new_ops = lappend_oid(new_ops, expr_op);
2646 lefttypes_cell = lnext(lefttypes_cell);
2647 righttypes_cell = lnext(righttypes_cell);
2651 /* If we have more than one matching col, create a subset rowcompare */
2652 if (matching_cols > 1)
2654 RowCompareExpr *rc = makeNode(RowCompareExpr);
2656 if (var_on_left)
2657 rc->rctype = (RowCompareType) op_strategy;
2658 else
2659 rc->rctype = (op_strategy == BTLessEqualStrategyNumber) ?
2660 ROWCOMPARE_GE : ROWCOMPARE_LE;
2661 rc->opnos = new_ops;
2662 rc->opfamilies = list_truncate(list_copy(clause->opfamilies),
2663 matching_cols);
2664 rc->largs = list_truncate((List *) copyObject(clause->largs),
2665 matching_cols);
2666 rc->rargs = list_truncate((List *) copyObject(clause->rargs),
2667 matching_cols);
2668 return make_restrictinfo((Expr *) rc, true, false, false, NULL);
2670 else
2672 Expr *opexpr;
2674 opexpr = make_opclause(linitial_oid(new_ops), BOOLOID, false,
2675 copyObject(linitial(clause->largs)),
2676 copyObject(linitial(clause->rargs)));
2677 return make_restrictinfo(opexpr, true, false, false, NULL);
2682 * Given a fixed prefix that all the "leftop" values must have,
2683 * generate suitable indexqual condition(s). opfamily is the index
2684 * operator family; we use it to deduce the appropriate comparison
2685 * operators and operand datatypes.
2687 static List *
2688 prefix_quals(Node *leftop, Oid opfamily,
2689 Const *prefix_const, Pattern_Prefix_Status pstatus)
2691 List *result;
2692 Oid datatype;
2693 Oid oproid;
2694 Expr *expr;
2695 FmgrInfo ltproc;
2696 Const *greaterstr;
2698 Assert(pstatus != Pattern_Prefix_None);
2700 switch (opfamily)
2702 case TEXT_BTREE_FAM_OID:
2703 case TEXT_PATTERN_BTREE_FAM_OID:
2704 datatype = TEXTOID;
2705 break;
2707 case BPCHAR_BTREE_FAM_OID:
2708 case BPCHAR_PATTERN_BTREE_FAM_OID:
2709 datatype = BPCHAROID;
2710 break;
2712 case NAME_BTREE_FAM_OID:
2713 datatype = NAMEOID;
2714 break;
2716 case BYTEA_BTREE_FAM_OID:
2717 datatype = BYTEAOID;
2718 break;
2720 default:
2721 /* shouldn't get here */
2722 elog(ERROR, "unexpected opfamily: %u", opfamily);
2723 return NIL;
2727 * If necessary, coerce the prefix constant to the right type. The given
2728 * prefix constant is either text or bytea type.
2730 if (prefix_const->consttype != datatype)
2732 char *prefix;
2734 switch (prefix_const->consttype)
2736 case TEXTOID:
2737 prefix = TextDatumGetCString(prefix_const->constvalue);
2738 break;
2739 case BYTEAOID:
2740 prefix = DatumGetCString(DirectFunctionCall1(byteaout,
2741 prefix_const->constvalue));
2742 break;
2743 default:
2744 elog(ERROR, "unexpected const type: %u",
2745 prefix_const->consttype);
2746 return NIL;
2748 prefix_const = string_to_const(prefix, datatype);
2749 pfree(prefix);
2753 * If we found an exact-match pattern, generate an "=" indexqual.
2755 if (pstatus == Pattern_Prefix_Exact)
2757 oproid = get_opfamily_member(opfamily, datatype, datatype,
2758 BTEqualStrategyNumber);
2759 if (oproid == InvalidOid)
2760 elog(ERROR, "no = operator for opfamily %u", opfamily);
2761 expr = make_opclause(oproid, BOOLOID, false,
2762 (Expr *) leftop, (Expr *) prefix_const);
2763 result = list_make1(make_restrictinfo(expr, true, false, false, NULL));
2764 return result;
2768 * Otherwise, we have a nonempty required prefix of the values.
2770 * We can always say "x >= prefix".
2772 oproid = get_opfamily_member(opfamily, datatype, datatype,
2773 BTGreaterEqualStrategyNumber);
2774 if (oproid == InvalidOid)
2775 elog(ERROR, "no >= operator for opfamily %u", opfamily);
2776 expr = make_opclause(oproid, BOOLOID, false,
2777 (Expr *) leftop, (Expr *) prefix_const);
2778 result = list_make1(make_restrictinfo(expr, true, false, false, NULL));
2780 /*-------
2781 * If we can create a string larger than the prefix, we can say
2782 * "x < greaterstr".
2783 *-------
2785 oproid = get_opfamily_member(opfamily, datatype, datatype,
2786 BTLessStrategyNumber);
2787 if (oproid == InvalidOid)
2788 elog(ERROR, "no < operator for opfamily %u", opfamily);
2789 fmgr_info(get_opcode(oproid), &ltproc);
2790 greaterstr = make_greater_string(prefix_const, &ltproc);
2791 if (greaterstr)
2793 expr = make_opclause(oproid, BOOLOID, false,
2794 (Expr *) leftop, (Expr *) greaterstr);
2795 result = lappend(result,
2796 make_restrictinfo(expr, true, false, false, NULL));
2799 return result;
2803 * Given a leftop and a rightop, and a inet-family sup/sub operator,
2804 * generate suitable indexqual condition(s). expr_op is the original
2805 * operator, and opfamily is the index opfamily.
2807 static List *
2808 network_prefix_quals(Node *leftop, Oid expr_op, Oid opfamily, Datum rightop)
2810 bool is_eq;
2811 Oid datatype;
2812 Oid opr1oid;
2813 Oid opr2oid;
2814 Datum opr1right;
2815 Datum opr2right;
2816 List *result;
2817 Expr *expr;
2819 switch (expr_op)
2821 case OID_INET_SUB_OP:
2822 datatype = INETOID;
2823 is_eq = false;
2824 break;
2825 case OID_INET_SUBEQ_OP:
2826 datatype = INETOID;
2827 is_eq = true;
2828 break;
2829 default:
2830 elog(ERROR, "unexpected operator: %u", expr_op);
2831 return NIL;
2835 * create clause "key >= network_scan_first( rightop )", or ">" if the
2836 * operator disallows equality.
2838 if (is_eq)
2840 opr1oid = get_opfamily_member(opfamily, datatype, datatype,
2841 BTGreaterEqualStrategyNumber);
2842 if (opr1oid == InvalidOid)
2843 elog(ERROR, "no >= operator for opfamily %u", opfamily);
2845 else
2847 opr1oid = get_opfamily_member(opfamily, datatype, datatype,
2848 BTGreaterStrategyNumber);
2849 if (opr1oid == InvalidOid)
2850 elog(ERROR, "no > operator for opfamily %u", opfamily);
2853 opr1right = network_scan_first(rightop);
2855 expr = make_opclause(opr1oid, BOOLOID, false,
2856 (Expr *) leftop,
2857 (Expr *) makeConst(datatype, -1, -1, opr1right,
2858 false, false));
2859 result = list_make1(make_restrictinfo(expr, true, false, false, NULL));
2861 /* create clause "key <= network_scan_last( rightop )" */
2863 opr2oid = get_opfamily_member(opfamily, datatype, datatype,
2864 BTLessEqualStrategyNumber);
2865 if (opr2oid == InvalidOid)
2866 elog(ERROR, "no <= operator for opfamily %u", opfamily);
2868 opr2right = network_scan_last(rightop);
2870 expr = make_opclause(opr2oid, BOOLOID, false,
2871 (Expr *) leftop,
2872 (Expr *) makeConst(datatype, -1, -1, opr2right,
2873 false, false));
2874 result = lappend(result,
2875 make_restrictinfo(expr, true, false, false, NULL));
2877 return result;
2881 * Handy subroutines for match_special_index_operator() and friends.
2885 * Generate a Datum of the appropriate type from a C string.
2886 * Note that all of the supported types are pass-by-ref, so the
2887 * returned value should be pfree'd if no longer needed.
2889 static Datum
2890 string_to_datum(const char *str, Oid datatype)
2893 * We cheat a little by assuming that CStringGetTextDatum() will do for
2894 * bpchar and varchar constants too...
2896 if (datatype == NAMEOID)
2897 return DirectFunctionCall1(namein, CStringGetDatum(str));
2898 else if (datatype == BYTEAOID)
2899 return DirectFunctionCall1(byteain, CStringGetDatum(str));
2900 else
2901 return CStringGetTextDatum(str);
2905 * Generate a Const node of the appropriate type from a C string.
2907 static Const *
2908 string_to_const(const char *str, Oid datatype)
2910 Datum conval = string_to_datum(str, datatype);
2912 return makeConst(datatype, -1,
2913 ((datatype == NAMEOID) ? NAMEDATALEN : -1),
2914 conval, false, false);