1 /*-------------------------------------------------------------------------
4 * Routines to determine which indexes are usable for scanning a
5 * given relation, and create Paths accordingly.
7 * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
12 * $PostgreSQL: pgsql/src/backend/optimizer/path/indxpath.c,v 1.189 2005/09/22 23:25:07 tgl Exp $
14 *-------------------------------------------------------------------------
20 #include "access/skey.h"
21 #include "catalog/pg_opclass.h"
22 #include "catalog/pg_operator.h"
23 #include "catalog/pg_type.h"
24 #include "nodes/makefuncs.h"
25 #include "optimizer/clauses.h"
26 #include "optimizer/cost.h"
27 #include "optimizer/pathnode.h"
28 #include "optimizer/paths.h"
29 #include "optimizer/predtest.h"
30 #include "optimizer/restrictinfo.h"
31 #include "utils/builtins.h"
32 #include "utils/lsyscache.h"
33 #include "utils/memutils.h"
34 #include "utils/pg_locale.h"
35 #include "utils/selfuncs.h"
39 * DoneMatchingIndexKeys() - MACRO
41 #define DoneMatchingIndexKeys(classes) (classes[0] == InvalidOid)
43 #define is_indexable_operator(clause,opclass,indexkey_on_left) \
44 (indexable_operator(clause,opclass,indexkey_on_left) != InvalidOid)
46 #define IsBooleanOpclass(opclass) \
47 ((opclass) == BOOL_BTREE_OPS_OID || (opclass) == BOOL_HASH_OPS_OID)
50 static List *find_usable_indexes(PlannerInfo *root, RelOptInfo *rel,
51 List *clauses, List *outer_clauses,
52 bool istoplevel, bool isjoininner,
54 static Path *choose_bitmap_and(PlannerInfo *root, RelOptInfo *rel, List *paths);
55 static int bitmap_path_comparator(const void *a, const void *b);
56 static Cost bitmap_and_cost_est(PlannerInfo *root, RelOptInfo *rel, List *paths);
57 static bool match_clause_to_indexcol(IndexOptInfo *index,
58 int indexcol, Oid opclass,
61 static Oid indexable_operator(Expr *clause, Oid opclass,
62 bool indexkey_on_left);
63 static Relids indexable_outerrelids(RelOptInfo *rel);
64 static bool matches_any_index(RestrictInfo *rinfo, RelOptInfo *rel,
66 static List *find_clauses_for_join(PlannerInfo *root, RelOptInfo *rel,
67 Relids outer_relids, bool isouterjoin);
68 static bool match_variant_ordering(PlannerInfo *root,
70 List *restrictclauses,
71 ScanDirection *indexscandir);
72 static List *identify_ignorable_ordering_cols(PlannerInfo *root,
74 List *restrictclauses);
75 static bool match_index_to_query_keys(PlannerInfo *root,
77 ScanDirection indexscandir,
79 static bool match_boolean_index_clause(Node *clause, int indexcol,
81 static bool match_special_index_operator(Expr *clause, Oid opclass,
82 bool indexkey_on_left);
83 static Expr *expand_boolean_index_clause(Node *clause, int indexcol,
85 static List *expand_indexqual_condition(RestrictInfo *rinfo, Oid opclass);
86 static List *prefix_quals(Node *leftop, Oid opclass,
87 Const *prefix, Pattern_Prefix_Status pstatus);
88 static List *network_prefix_quals(Node *leftop, Oid expr_op, Oid opclass,
90 static Datum string_to_datum(const char *str, Oid datatype);
91 static Const *string_to_const(const char *str, Oid datatype);
95 * create_index_paths()
96 * Generate all interesting index paths for the given relation.
97 * Candidate paths are added to the rel's pathlist (using add_path).
99 * To be considered for an index scan, an index must match one or more
100 * restriction clauses or join clauses from the query's qual condition,
101 * or match the query's ORDER BY condition, or have a predicate that
102 * matches the query's qual condition.
104 * There are two basic kinds of index scans. A "plain" index scan uses
105 * only restriction clauses (possibly none at all) in its indexqual,
106 * so it can be applied in any context. An "innerjoin" index scan uses
107 * join clauses (plus restriction clauses, if available) in its indexqual.
108 * Therefore it can only be used as the inner relation of a nestloop
109 * join against an outer rel that includes all the other rels mentioned
110 * in its join clauses. In that context, values for the other rels'
111 * attributes are available and fixed during any one scan of the indexpath.
113 * An IndexPath is generated and submitted to add_path() for each plain index
114 * scan this routine deems potentially interesting for the current query.
116 * We also determine the set of other relids that participate in join
117 * clauses that could be used with each index. The actually best innerjoin
118 * path will be generated for each outer relation later on, but knowing the
119 * set of potential otherrels allows us to identify equivalent outer relations
120 * and avoid repeated computation.
122 * 'rel' is the relation for which we want to generate index paths
124 * Note: check_partial_indexes() must have been run previously.
127 create_index_paths(PlannerInfo *root, RelOptInfo *rel)
133 /* Skip the whole mess if no indexes */
134 if (rel->indexlist == NIL)
136 rel->index_outer_relids = NULL;
141 * Examine join clauses to see which ones are potentially usable with
142 * indexes of this rel, and generate the set of all other relids that
143 * participate in such join clauses. We'll use this set later to
144 * recognize outer rels that are equivalent for joining purposes.
146 rel->index_outer_relids = indexable_outerrelids(rel);
149 * Find all the index paths that are directly usable for this relation
150 * (ie, are valid without considering OR or JOIN clauses).
152 indexpaths = find_usable_indexes(root, rel,
153 rel->baserestrictinfo, NIL,
157 * We can submit them all to add_path. (This generates access paths for
158 * plain IndexScan plans.) However, for the next step we will only want
159 * the ones that have some selectivity; we must discard anything that was
160 * generated solely for ordering purposes.
163 foreach(l, indexpaths)
165 IndexPath *ipath = (IndexPath *) lfirst(l);
167 add_path(rel, (Path *) ipath);
169 if (ipath->indexselectivity < 1.0 &&
170 !ScanDirectionIsBackward(ipath->indexscandir))
171 bitindexpaths = lappend(bitindexpaths, ipath);
175 * Generate BitmapOrPaths for any suitable OR-clauses present in the
176 * restriction list. Add these to bitindexpaths.
178 indexpaths = generate_bitmap_or_paths(root, rel,
179 rel->baserestrictinfo, NIL,
181 bitindexpaths = list_concat(bitindexpaths, indexpaths);
184 * If we found anything usable, generate a BitmapHeapPath for the
185 * most promising combination of bitmap index paths.
187 if (bitindexpaths != NIL)
190 BitmapHeapPath *bpath;
192 bitmapqual = choose_bitmap_and(root, rel, bitindexpaths);
193 bpath = create_bitmap_heap_path(root, rel, bitmapqual, false);
194 add_path(rel, (Path *) bpath);
200 * find_usable_indexes
201 * Given a list of restriction clauses, find all the potentially usable
202 * indexes for the given relation, and return a list of IndexPaths.
204 * The caller actually supplies two lists of restriction clauses: some
205 * "current" ones and some "outer" ones. Both lists can be used freely
206 * to match keys of the index, but an index must use at least one of the
207 * "current" clauses to be considered usable. The motivation for this is
209 * WHERE (x = 42) AND (... OR (y = 52 AND z = 77) OR ....)
210 * While we are considering the y/z subclause of the OR, we can use "x = 42"
211 * as one of the available index conditions; but we shouldn't match the
212 * subclause to any index on x alone, because such a Path would already have
213 * been generated at the upper level. So we could use an index on x,y,z
214 * or an index on x,y for the OR subclause, but not an index on just x.
215 * When dealing with a partial index, a match of the index predicate to
216 * one of the "current" clauses also makes the index usable.
218 * If istoplevel is true (indicating we are considering the top level of a
219 * rel's restriction clauses), we will include indexes in the result that
220 * have an interesting sort order, even if they have no matching restriction
223 * 'rel' is the relation for which we want to generate index paths
224 * 'clauses' is the current list of clauses (RestrictInfo nodes)
225 * 'outer_clauses' is the list of additional upper-level clauses
226 * 'istoplevel' is true if clauses are the rel's top-level restriction list
227 * (outer_clauses must be NIL when this is true)
228 * 'isjoininner' is true if forming an inner indexscan (so some of the
229 * given clauses are join clauses)
230 * 'outer_relids' identifies the outer side of the join (pass NULL
231 * if not isjoininner)
233 * Note: check_partial_indexes() must have been run previously.
237 find_usable_indexes(PlannerInfo *root, RelOptInfo *rel,
238 List *clauses, List *outer_clauses,
239 bool istoplevel, bool isjoininner,
243 List *all_clauses = NIL; /* not computed till needed */
246 foreach(ilist, rel->indexlist)
248 IndexOptInfo *index = (IndexOptInfo *) lfirst(ilist);
250 List *restrictclauses;
251 List *index_pathkeys;
252 List *useful_pathkeys;
253 bool useful_predicate;
255 bool index_is_ordered;
258 * Ignore partial indexes that do not match the query. If a partial
259 * index is marked predOK then we know it's OK; otherwise, if we
260 * are at top level we know it's not OK (since predOK is exactly
261 * whether its predicate could be proven from the toplevel clauses).
262 * Otherwise, we have to test whether the added clauses are
263 * sufficient to imply the predicate. If so, we could use
264 * the index in the current context.
266 * We set useful_predicate to true iff the predicate was proven
267 * using the current set of clauses. This is needed to prevent
268 * matching a predOK index to an arm of an OR, which would be
269 * a legal but pointlessly inefficient plan. (A better plan will
270 * be generated by just scanning the predOK index alone, no OR.)
272 useful_predicate = false;
273 if (index->indpred != NIL)
279 /* we know predicate was proven from these clauses */
280 useful_predicate = true;
286 continue; /* no point in trying to prove it */
288 /* Form all_clauses if not done already */
289 if (all_clauses == NIL)
290 all_clauses = list_concat(list_copy(clauses),
293 if (!predicate_implied_by(index->indpred, all_clauses))
294 continue; /* can't use it at all */
296 if (!predicate_implied_by(index->indpred, outer_clauses))
297 useful_predicate = true;
302 * 1. Match the index against the available restriction clauses.
303 * found_clause is set true only if at least one of the current
306 restrictclauses = group_clauses_by_indexkey(index,
313 * Not all index AMs support scans with no restriction clauses.
314 * We can't generate a scan over an index with amoptionalkey = false
315 * unless there's at least one restriction clause.
317 if (restrictclauses == NIL && !index->amoptionalkey)
321 * 2. Compute pathkeys describing index's ordering, if any, then
322 * see how many of them are actually useful for this query. This
323 * is not relevant unless we are at top level.
325 index_is_ordered = OidIsValid(index->ordering[0]);
326 if (istoplevel && index_is_ordered && !isjoininner)
328 index_pathkeys = build_index_pathkeys(root, index,
329 ForwardScanDirection);
330 useful_pathkeys = truncate_useless_pathkeys(root, rel,
334 useful_pathkeys = NIL;
337 * 3. Generate an indexscan path if there are relevant restriction
338 * clauses in the current clauses, OR the index ordering is
339 * potentially useful for later merging or final output ordering,
340 * OR the index has a predicate that was proven by the current
343 if (found_clause || useful_pathkeys != NIL || useful_predicate)
345 ipath = create_index_path(root, index,
349 ForwardScanDirection :
350 NoMovementScanDirection,
352 result = lappend(result, ipath);
356 * 4. If the index is ordered, and there is a requested query
357 * ordering that we failed to match, consider variant ways of
358 * achieving the ordering. Again, this is only interesting
361 if (istoplevel && index_is_ordered && !isjoininner &&
362 root->query_pathkeys != NIL &&
363 pathkeys_useful_for_ordering(root, useful_pathkeys) == 0)
365 ScanDirection indexscandir;
367 if (match_variant_ordering(root, index, restrictclauses,
370 ipath = create_index_path(root, index,
372 root->query_pathkeys,
375 result = lappend(result, ipath);
385 * generate_bitmap_or_paths
386 * Look through the list of clauses to find OR clauses, and generate
387 * a BitmapOrPath for each one we can handle that way. Return a list
388 * of the generated BitmapOrPaths.
390 * outer_clauses is a list of additional clauses that can be assumed true
391 * for the purpose of generating indexquals, but are not to be searched for
392 * ORs. (See find_usable_indexes() for motivation.)
395 generate_bitmap_or_paths(PlannerInfo *root, RelOptInfo *rel,
396 List *clauses, List *outer_clauses,
405 * We can use both the current and outer clauses as context for
406 * find_usable_indexes
408 all_clauses = list_concat(list_copy(clauses), outer_clauses);
412 RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
417 Assert(IsA(rinfo, RestrictInfo));
418 /* Ignore RestrictInfos that aren't ORs */
419 if (!restriction_is_or_clause(rinfo))
423 * We must be able to match at least one index to each of the arms
424 * of the OR, else we can't use it.
427 foreach(j, ((BoolExpr *) rinfo->orclause)->args)
429 Node *orarg = (Node *) lfirst(j);
432 /* OR arguments should be ANDs or sub-RestrictInfos */
433 if (and_clause(orarg))
435 List *andargs = ((BoolExpr *) orarg)->args;
437 indlist = find_usable_indexes(root, rel,
443 /* Recurse in case there are sub-ORs */
444 indlist = list_concat(indlist,
445 generate_bitmap_or_paths(root, rel,
453 Assert(IsA(orarg, RestrictInfo));
454 Assert(!restriction_is_or_clause((RestrictInfo *) orarg));
455 indlist = find_usable_indexes(root, rel,
463 * If nothing matched this arm, we can't do anything
464 * with this OR clause.
472 * OK, pick the most promising AND combination,
473 * and add it to pathlist.
475 bitmapqual = choose_bitmap_and(root, rel, indlist);
476 pathlist = lappend(pathlist, bitmapqual);
479 * If we have a match for every arm, then turn them
480 * into a BitmapOrPath, and add to result list.
484 bitmapqual = (Path *) create_bitmap_or_path(root, rel, pathlist);
485 result = lappend(result, bitmapqual);
495 * Given a nonempty list of bitmap paths, AND them into one path.
497 * This is a nontrivial decision since we can legally use any subset of the
498 * given path set. We want to choose a good tradeoff between selectivity
499 * and cost of computing the bitmap.
501 * The result is either a single one of the inputs, or a BitmapAndPath
502 * combining multiple inputs.
505 choose_bitmap_and(PlannerInfo *root, RelOptInfo *rel, List *paths)
507 int npaths = list_length(paths);
515 Assert(npaths > 0); /* else caller error */
517 return (Path *) linitial(paths); /* easy case */
520 * In theory we should consider every nonempty subset of the given paths.
521 * In practice that seems like overkill, given the crude nature of the
522 * estimates, not to mention the possible effects of higher-level AND and
523 * OR clauses. As a compromise, we sort the paths by selectivity.
524 * We always take the first, and sequentially add on paths that result
525 * in a lower estimated cost.
527 * We also make some effort to detect directly redundant input paths,
528 * as can happen if there are multiple possibly usable indexes. For
529 * this we look only at plain IndexPath inputs, not at sub-OR clauses.
530 * And we consider an index redundant if all its index conditions were
531 * already used by earlier indexes. (We could use predicate_implied_by
532 * to have a more intelligent, but much more expensive, check --- but in
533 * most cases simple pointer equality should suffice, since after all the
534 * index conditions are all coming from the same RestrictInfo lists.)
536 * XXX is there any risk of throwing away a useful partial index here
537 * because we don't explicitly look at indpred? At least in simple
538 * cases, the partial index will sort before competing non-partial
539 * indexes and so it makes the right choice, but perhaps we need to
542 * Note: outputting the selected sub-paths in selectivity order is a good
543 * thing even if we weren't using that as part of the selection method,
544 * because it makes the short-circuit case in MultiExecBitmapAnd() more
548 /* Convert list to array so we can apply qsort */
549 patharray = (Path **) palloc(npaths * sizeof(Path *));
553 patharray[i++] = (Path *) lfirst(l);
555 qsort(patharray, npaths, sizeof(Path *), bitmap_path_comparator);
557 paths = list_make1(patharray[0]);
558 costsofar = bitmap_and_cost_est(root, rel, paths);
559 if (IsA(patharray[0], IndexPath))
560 qualsofar = list_copy(((IndexPath *) patharray[0])->indexclauses);
563 lastcell = list_head(paths); /* for quick deletions */
565 for (i = 1; i < npaths; i++)
567 Path *newpath = patharray[i];
571 if (IsA(newpath, IndexPath))
573 newqual = ((IndexPath *) newpath)->indexclauses;
574 if (list_difference_ptr(newqual, qualsofar) == NIL)
575 continue; /* redundant */
578 paths = lappend(paths, newpath);
579 newcost = bitmap_and_cost_est(root, rel, paths);
580 if (newcost < costsofar)
584 qualsofar = list_concat(qualsofar, list_copy(newqual));
585 lastcell = lnext(lastcell);
589 paths = list_delete_cell(paths, lnext(lastcell), lastcell);
591 Assert(lnext(lastcell) == NULL);
594 if (list_length(paths) == 1)
595 return (Path *) linitial(paths); /* no need for AND */
596 return (Path *) create_bitmap_and_path(root, rel, paths);
599 /* qsort comparator to sort in increasing selectivity order */
601 bitmap_path_comparator(const void *a, const void *b)
603 Path *pa = *(Path * const *) a;
604 Path *pb = *(Path * const *) b;
610 cost_bitmap_tree_node(pa, &acost, &aselec);
611 cost_bitmap_tree_node(pb, &bcost, &bselec);
617 /* if identical selectivity, sort by cost */
626 * Estimate the cost of actually executing a BitmapAnd with the given
630 bitmap_and_cost_est(PlannerInfo *root, RelOptInfo *rel, List *paths)
635 /* Set up a dummy BitmapAndPath */
636 apath.path.type = T_BitmapAndPath;
637 apath.path.parent = rel;
638 apath.bitmapquals = paths;
639 cost_bitmap_and_node(&apath, root);
641 /* Now we can do cost_bitmap_heap_scan */
642 cost_bitmap_heap_scan(&bpath, root, rel, (Path *) &apath, false);
644 return bpath.total_cost;
648 /****************************************************************************
649 * ---- ROUTINES TO CHECK RESTRICTIONS ----
650 ****************************************************************************/
654 * group_clauses_by_indexkey
655 * Find restriction clauses that can be used with an index.
657 * Returns a list of sublists of RestrictInfo nodes for clauses that can be
658 * used with this index. Each sublist contains clauses that can be used
659 * with one index key (in no particular order); the top list is ordered by
660 * index key. (This is depended on by expand_indexqual_conditions().)
662 * We can use clauses from either the current clauses or outer_clauses lists,
663 * but *found_clause is set TRUE only if we used at least one clause from
664 * the "current clauses" list. See find_usable_indexes() for motivation.
666 * outer_relids determines what Vars will be allowed on the other side
667 * of a possible index qual; see match_clause_to_indexcol().
669 * If the index has amoptionalkey = false, we give up and return NIL when
670 * there are no restriction clauses matching the first index key. Otherwise,
671 * we return NIL if there are no restriction clauses matching any index key.
672 * A non-NIL result will have one (possibly empty) sublist for each index key.
674 * Example: given an index on (A,B,C), we would return ((C1 C2) () (C3 C4))
675 * if we find that clauses C1 and C2 use column A, clauses C3 and C4 use
676 * column C, and no clauses use column B.
678 * Note: in some circumstances we may find the same RestrictInfos coming
679 * from multiple places. Defend against redundant outputs by using
680 * list_append_unique_ptr (pointer equality should be good enough).
683 group_clauses_by_indexkey(IndexOptInfo *index,
684 List *clauses, List *outer_clauses,
688 List *clausegroup_list = NIL;
689 bool found_outer_clause = false;
691 Oid *classes = index->classlist;
693 *found_clause = false; /* default result */
695 if (clauses == NIL && outer_clauses == NIL)
696 return NIL; /* cannot succeed */
700 Oid curClass = classes[0];
701 List *clausegroup = NIL;
704 /* check the current clauses */
707 RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
709 Assert(IsA(rinfo, RestrictInfo));
710 if (match_clause_to_indexcol(index,
716 clausegroup = list_append_unique_ptr(clausegroup, rinfo);
717 *found_clause = true;
721 /* check the outer clauses */
722 foreach(l, outer_clauses)
724 RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
726 Assert(IsA(rinfo, RestrictInfo));
727 if (match_clause_to_indexcol(index,
733 clausegroup = list_append_unique_ptr(clausegroup, rinfo);
734 found_outer_clause = true;
739 * If no clauses match this key, check for amoptionalkey restriction.
741 if (clausegroup == NIL && !index->amoptionalkey && indexcol == 0)
744 clausegroup_list = lappend(clausegroup_list, clausegroup);
749 } while (!DoneMatchingIndexKeys(classes));
751 if (!*found_clause && !found_outer_clause)
752 return NIL; /* no indexable clauses anywhere */
754 return clausegroup_list;
759 * match_clause_to_indexcol()
760 * Determines whether a restriction clause matches a column of an index.
762 * To match a normal index, the clause:
764 * (1) must be in the form (indexkey op const) or (const op indexkey);
766 * (2) must contain an operator which is in the same class as the index
767 * operator for this column, or is a "special" operator as recognized
768 * by match_special_index_operator().
770 * Our definition of "const" is pretty liberal: we allow Vars belonging
771 * to the caller-specified outer_relids relations (which had better not
772 * include the relation whose index is being tested). outer_relids should
773 * be NULL when checking simple restriction clauses, and the outer side
774 * of the join when building a join inner scan. Other than that, the
775 * only thing we don't like is volatile functions.
777 * Note: in most cases we already know that the clause as a whole uses
778 * vars from the interesting set of relations. The reason for the
779 * outer_relids test is to reject clauses like (a.f1 OP (b.f2 OP a.f3));
780 * that's not processable by an indexscan nestloop join on A, whereas
781 * (a.f1 OP (b.f2 OP c.f3)) is.
783 * Presently, the executor can only deal with indexquals that have the
784 * indexkey on the left, so we can only use clauses that have the indexkey
785 * on the right if we can commute the clause to put the key on the left.
786 * We do not actually do the commuting here, but we check whether a
787 * suitable commutator operator is available.
789 * For boolean indexes, it is also possible to match the clause directly
790 * to the indexkey; or perhaps the clause is (NOT indexkey).
792 * 'index' is the index of interest.
793 * 'indexcol' is a column number of 'index' (counting from 0).
794 * 'opclass' is the corresponding operator class.
795 * 'rinfo' is the clause to be tested (as a RestrictInfo node).
797 * Returns true if the clause can be used with this index key.
799 * NOTE: returns false if clause is an OR or AND clause; it is the
800 * responsibility of higher-level routines to cope with those.
803 match_clause_to_indexcol(IndexOptInfo *index,
809 Expr *clause = rinfo->clause;
813 /* First check for boolean-index cases. */
814 if (IsBooleanOpclass(opclass))
816 if (match_boolean_index_clause((Node *) clause, indexcol, index))
820 /* Else clause must be a binary opclause. */
821 if (!is_opclause(clause))
823 leftop = get_leftop(clause);
824 rightop = get_rightop(clause);
825 if (!leftop || !rightop)
829 * Check for clauses of the form: (indexkey operator constant) or
830 * (constant operator indexkey). See above notes about const-ness.
832 if (match_index_to_operand(leftop, indexcol, index) &&
833 bms_is_subset(rinfo->right_relids, outer_relids) &&
834 !contain_volatile_functions(rightop))
836 if (is_indexable_operator(clause, opclass, true))
840 * If we didn't find a member of the index's opclass, see whether
841 * it is a "special" indexable operator.
843 if (match_special_index_operator(clause, opclass, true))
848 if (match_index_to_operand(rightop, indexcol, index) &&
849 bms_is_subset(rinfo->left_relids, outer_relids) &&
850 !contain_volatile_functions(leftop))
852 if (is_indexable_operator(clause, opclass, false))
856 * If we didn't find a member of the index's opclass, see whether
857 * it is a "special" indexable operator.
859 if (match_special_index_operator(clause, opclass, false))
869 * Does a binary opclause contain an operator matching the index opclass?
871 * If the indexkey is on the right, what we actually want to know
872 * is whether the operator has a commutator operator that matches
873 * the index's opclass.
875 * Returns the OID of the matching operator, or InvalidOid if no match.
876 * (Formerly, this routine might return a binary-compatible operator
877 * rather than the original one, but that kluge is history.)
880 indexable_operator(Expr *clause, Oid opclass, bool indexkey_on_left)
882 Oid expr_op = ((OpExpr *) clause)->opno;
885 /* Get the commuted operator if necessary */
886 if (indexkey_on_left)
887 commuted_op = expr_op;
889 commuted_op = get_commutator(expr_op);
890 if (commuted_op == InvalidOid)
893 /* OK if the (commuted) operator is a member of the index's opclass */
894 if (op_in_opclass(commuted_op, opclass))
900 /****************************************************************************
901 * ---- ROUTINES TO DO PARTIAL INDEX PREDICATE TESTS ----
902 ****************************************************************************/
905 * check_partial_indexes
906 * Check each partial index of the relation, and mark it predOK or not
907 * depending on whether the predicate is satisfied for this query.
910 check_partial_indexes(PlannerInfo *root, RelOptInfo *rel)
912 List *restrictinfo_list = rel->baserestrictinfo;
916 * Note: if Postgres tried to optimize queries by forming equivalence
917 * classes over equi-joined attributes (i.e., if it recognized that a
918 * qualification such as "where a.b=c.d and a.b=5" could make use of
919 * an index on c.d), then we could use that equivalence class info
920 * here with joininfo lists to do more complete tests for the usability
921 * of a partial index. For now, the test only uses restriction
922 * clauses (those in baserestrictinfo). --Nels, Dec '92
924 * XXX as of 7.1, equivalence class info *is* available. Consider
925 * improving this code as foreseen by Nels.
928 foreach(ilist, rel->indexlist)
930 IndexOptInfo *index = (IndexOptInfo *) lfirst(ilist);
932 if (index->indpred == NIL)
933 continue; /* ignore non-partial indexes */
935 index->predOK = predicate_implied_by(index->indpred,
940 /****************************************************************************
941 * ---- ROUTINES TO CHECK JOIN CLAUSES ----
942 ****************************************************************************/
945 * indexable_outerrelids
946 * Finds all other relids that participate in any indexable join clause
947 * for the specified table. Returns a set of relids.
950 indexable_outerrelids(RelOptInfo *rel)
952 Relids outer_relids = NULL;
956 * Examine each joinclause in the joininfo list to see if it matches any
957 * key of any index. If so, add the clause's other rels to the result.
959 foreach(l, rel->joininfo)
961 RestrictInfo *joininfo = (RestrictInfo *) lfirst(l);
964 other_rels = bms_difference(joininfo->required_relids, rel->relids);
965 if (matches_any_index(joininfo, rel, other_rels))
966 outer_relids = bms_join(outer_relids, other_rels);
968 bms_free(other_rels);
976 * Workhorse for indexable_outerrelids: see if a joinclause can be
977 * matched to any index of the given rel.
980 matches_any_index(RestrictInfo *rinfo, RelOptInfo *rel, Relids outer_relids)
984 Assert(IsA(rinfo, RestrictInfo));
986 if (restriction_is_or_clause(rinfo))
988 foreach(l, ((BoolExpr *) rinfo->orclause)->args)
990 Node *orarg = (Node *) lfirst(l);
992 /* OR arguments should be ANDs or sub-RestrictInfos */
993 if (and_clause(orarg))
997 /* Recurse to examine AND items and sub-ORs */
998 foreach(j, ((BoolExpr *) orarg)->args)
1000 RestrictInfo *arinfo = (RestrictInfo *) lfirst(j);
1002 if (matches_any_index(arinfo, rel, outer_relids))
1008 /* Recurse to examine simple clause */
1009 Assert(IsA(orarg, RestrictInfo));
1010 Assert(!restriction_is_or_clause((RestrictInfo *) orarg));
1011 if (matches_any_index((RestrictInfo *) orarg, rel,
1020 /* Normal case for a simple restriction clause */
1021 foreach(l, rel->indexlist)
1023 IndexOptInfo *index = (IndexOptInfo *) lfirst(l);
1025 Oid *classes = index->classlist;
1029 Oid curClass = classes[0];
1031 if (match_clause_to_indexcol(index,
1040 } while (!DoneMatchingIndexKeys(classes));
1047 * best_inner_indexscan
1048 * Finds the best available inner indexscan for a nestloop join
1049 * with the given rel on the inside and the given outer_relids outside.
1050 * May return NULL if there are no possible inner indexscans.
1052 * We ignore ordering considerations (since a nestloop's inner scan's order
1053 * is uninteresting). Also, we consider only total cost when deciding which
1054 * of two possible paths is better --- this assumes that all indexpaths have
1055 * negligible startup cost. (True today, but someday we might have to think
1056 * harder.) Therefore, there is only one dimension of comparison and so it's
1057 * sufficient to return a single "best" path.
1060 best_inner_indexscan(PlannerInfo *root, RelOptInfo *rel,
1061 Relids outer_relids, JoinType jointype)
1067 List *bitindexpaths;
1069 InnerIndexscanInfo *info;
1070 MemoryContext oldcontext;
1073 * Nestloop only supports inner, left, and IN joins.
1079 case JOIN_UNIQUE_OUTER:
1080 isouterjoin = false;
1090 * If there are no indexable joinclauses for this rel, exit quickly.
1092 if (bms_is_empty(rel->index_outer_relids))
1096 * Otherwise, we have to do path selection in the memory context of
1097 * the given rel, so that any created path can be safely attached to
1098 * the rel's cache of best inner paths. (This is not currently an
1099 * issue for normal planning, but it is an issue for GEQO planning.)
1101 oldcontext = MemoryContextSwitchTo(GetMemoryChunkContext(rel));
1104 * Intersect the given outer_relids with index_outer_relids to find
1105 * the set of outer relids actually relevant for this rel. If there
1106 * are none, again we can fail immediately.
1108 outer_relids = bms_intersect(rel->index_outer_relids, outer_relids);
1109 if (bms_is_empty(outer_relids))
1111 bms_free(outer_relids);
1112 MemoryContextSwitchTo(oldcontext);
1117 * Look to see if we already computed the result for this set of
1118 * relevant outerrels. (We include the isouterjoin status in the
1119 * cache lookup key for safety. In practice I suspect this is not
1120 * necessary because it should always be the same for a given
1123 foreach(l, rel->index_inner_paths)
1125 info = (InnerIndexscanInfo *) lfirst(l);
1126 if (bms_equal(info->other_relids, outer_relids) &&
1127 info->isouterjoin == isouterjoin)
1129 bms_free(outer_relids);
1130 MemoryContextSwitchTo(oldcontext);
1131 return info->best_innerpath;
1136 * Find all the relevant restriction and join clauses.
1138 clause_list = find_clauses_for_join(root, rel, outer_relids, isouterjoin);
1141 * Find all the index paths that are usable for this join, except for
1142 * stuff involving OR clauses.
1144 indexpaths = find_usable_indexes(root, rel,
1150 * Generate BitmapOrPaths for any suitable OR-clauses present in the
1153 bitindexpaths = generate_bitmap_or_paths(root, rel,
1159 * Include the regular index paths in bitindexpaths.
1161 bitindexpaths = list_concat(bitindexpaths, list_copy(indexpaths));
1164 * If we found anything usable, generate a BitmapHeapPath for the
1165 * most promising combination of bitmap index paths.
1167 if (bitindexpaths != NIL)
1170 BitmapHeapPath *bpath;
1172 bitmapqual = choose_bitmap_and(root, rel, bitindexpaths);
1173 bpath = create_bitmap_heap_path(root, rel, bitmapqual, true);
1174 indexpaths = lappend(indexpaths, bpath);
1178 * Now choose the cheapest member of indexpaths.
1181 foreach(l, indexpaths)
1183 Path *path = (Path *) lfirst(l);
1185 if (cheapest == NULL ||
1186 compare_path_costs(path, cheapest, TOTAL_COST) < 0)
1190 /* Cache the result --- whether positive or negative */
1191 info = makeNode(InnerIndexscanInfo);
1192 info->other_relids = outer_relids;
1193 info->isouterjoin = isouterjoin;
1194 info->best_innerpath = cheapest;
1195 rel->index_inner_paths = lcons(info, rel->index_inner_paths);
1197 MemoryContextSwitchTo(oldcontext);
1203 * find_clauses_for_join
1204 * Generate a list of clauses that are potentially useful for
1205 * scanning rel as the inner side of a nestloop join.
1207 * We consider both join and restriction clauses. Any joinclause that uses
1208 * only otherrels in the specified outer_relids is fair game. But there must
1209 * be at least one such joinclause in the final list, otherwise we return NIL
1210 * indicating that there isn't any potential win here.
1213 find_clauses_for_join(PlannerInfo *root, RelOptInfo *rel,
1214 Relids outer_relids, bool isouterjoin)
1216 List *clause_list = NIL;
1217 bool jfound = false;
1222 * We can always use plain restriction clauses for the rel. We
1223 * scan these first because we want them first in the clause
1224 * list for the convenience of remove_redundant_join_clauses,
1225 * which can never remove non-join clauses and hence won't be able
1226 * to get rid of a non-join clause if it appears after a join
1227 * clause it is redundant with.
1229 foreach(l, rel->baserestrictinfo)
1231 RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
1233 /* Can't use pushed-down clauses in outer join */
1234 if (isouterjoin && rinfo->is_pushed_down)
1236 clause_list = lappend(clause_list, rinfo);
1239 /* Look for joinclauses that are usable with given outer_relids */
1240 join_relids = bms_union(rel->relids, outer_relids);
1242 foreach(l, rel->joininfo)
1244 RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
1246 /* Can't use pushed-down clauses in outer join */
1247 if (isouterjoin && rinfo->is_pushed_down)
1249 if (!bms_is_subset(rinfo->required_relids, join_relids))
1252 clause_list = lappend(clause_list, rinfo);
1256 bms_free(join_relids);
1258 /* if no join clause was matched then forget it, per comments above */
1263 * We may now have clauses that are known redundant. Get rid of 'em.
1265 if (list_length(clause_list) > 1)
1267 clause_list = remove_redundant_join_clauses(root,
1275 /****************************************************************************
1276 * ---- ROUTINES TO HANDLE PATHKEYS ----
1277 ****************************************************************************/
1280 * match_variant_ordering
1281 * Try to match an index's ordering to the query's requested ordering
1283 * This is used when the index is ordered but a naive comparison fails to
1284 * match its ordering (pathkeys) to root->query_pathkeys. It may be that
1285 * we need to scan the index backwards. Also, a less naive comparison can
1286 * help for both forward and backward indexscans. Columns of the index
1287 * that have an equality restriction clause can be ignored in the match;
1288 * that is, an index on (x,y) can be considered to match the ordering of
1289 * ... WHERE x = 42 ORDER BY y;
1291 * Note: it would be possible to similarly ignore useless ORDER BY items;
1292 * that is, an index on just y could be considered to match the ordering of
1293 * ... WHERE x = 42 ORDER BY x, y;
1294 * But proving that this is safe would require finding a btree opclass
1295 * containing both the = operator and the < or > operator in the ORDER BY
1296 * item. That's significantly more expensive than what we do here, since
1297 * we'd have to look at restriction clauses unrelated to the current index
1298 * and search for opclasses without any hint from the index. The practical
1299 * use-cases seem to be mostly covered by ignoring index columns, so that's
1300 * all we do for now.
1303 * 'index' is the index of interest.
1304 * 'restrictclauses' is the list of sublists of restriction clauses
1305 * matching the columns of the index (NIL if none)
1307 * Returns TRUE if able to match the requested query pathkeys, FALSE if not.
1308 * In the TRUE case, sets '*indexscandir' to either ForwardScanDirection or
1309 * BackwardScanDirection to indicate the proper scan direction.
1312 match_variant_ordering(PlannerInfo *root,
1313 IndexOptInfo *index,
1314 List *restrictclauses,
1315 ScanDirection *indexscandir)
1320 * Forget the whole thing if not a btree index; our check for ignorable
1321 * columns assumes we are dealing with btree opclasses. (It'd be possible
1322 * to factor out just the try for backwards indexscan, but considering
1323 * that we presently have no orderable indexes except btrees anyway,
1324 * it's hardly worth contorting this code for that case.)
1326 * Note: if you remove this, you probably need to put in a check on
1327 * amoptionalkey to prevent possible clauseless scan on an index that
1330 if (index->relam != BTREE_AM_OID)
1333 * Figure out which index columns can be optionally ignored because
1334 * they have an equality constraint. This is the same set for either
1335 * forward or backward scan, so we do it just once.
1337 ignorables = identify_ignorable_ordering_cols(root, index,
1340 * Try to match to forward scan, then backward scan. However, we can
1341 * skip the forward-scan case if there are no ignorable columns,
1342 * because find_usable_indexes() would have found the match already.
1345 match_index_to_query_keys(root, index, ForwardScanDirection,
1348 *indexscandir = ForwardScanDirection;
1351 if (match_index_to_query_keys(root, index, BackwardScanDirection,
1354 *indexscandir = BackwardScanDirection;
1361 * identify_ignorable_ordering_cols
1362 * Determine which index columns can be ignored for ordering purposes
1364 * Returns an integer List of column numbers (1-based) of ignorable
1365 * columns. The ignorable columns are those that have equality constraints
1366 * against pseudoconstants.
1369 identify_ignorable_ordering_cols(PlannerInfo *root,
1370 IndexOptInfo *index,
1371 List *restrictclauses)
1374 int indexcol = 0; /* note this is 0-based */
1377 /* restrictclauses is either NIL or has a sublist per column */
1378 foreach(l, restrictclauses)
1380 List *sublist = (List *) lfirst(l);
1381 Oid opclass = index->classlist[indexcol];
1384 foreach(l2, sublist)
1386 RestrictInfo *rinfo = (RestrictInfo *) lfirst(l2);
1387 OpExpr *clause = (OpExpr *) rinfo->clause;
1393 /* We know this clause passed match_clause_to_indexcol */
1395 /* First check for boolean-index cases. */
1396 if (IsBooleanOpclass(opclass))
1398 if (match_boolean_index_clause((Node *) clause, indexcol,
1402 * The clause means either col = TRUE or col = FALSE;
1403 * we do not care which, it's an equality constraint
1406 result = lappend_int(result, indexcol+1);
1411 /* Else clause must be a binary opclause. */
1412 Assert(IsA(clause, OpExpr));
1414 /* Determine left/right sides and check the operator */
1415 clause_op = clause->opno;
1416 if (match_index_to_operand(linitial(clause->args), indexcol,
1419 /* clause_op is correct */
1424 Assert(match_index_to_operand(lsecond(clause->args), indexcol,
1426 /* Must flip operator to get the opclass member */
1427 clause_op = get_commutator(clause_op);
1430 if (!OidIsValid(clause_op))
1431 continue; /* ignore non match, per next comment */
1432 op_strategy = get_op_opclass_strategy(clause_op, opclass);
1435 * You might expect to see Assert(op_strategy != 0) here,
1436 * but you won't: the clause might contain a special indexable
1437 * operator rather than an ordinary opclass member. Currently
1438 * none of the special operators are very likely to expand to
1439 * an equality operator; we do not bother to check, but just
1442 if (op_strategy != BTEqualStrategyNumber)
1445 /* Now check that other side is pseudoconstant */
1447 ispc = is_pseudo_constant_clause_relids(lsecond(clause->args),
1448 rinfo->right_relids);
1450 ispc = is_pseudo_constant_clause_relids(linitial(clause->args),
1451 rinfo->left_relids);
1454 result = lappend_int(result, indexcol+1);
1464 * match_index_to_query_keys
1465 * Check a single scan direction for "intelligent" match to query keys
1467 * 'index' is the index of interest.
1468 * 'indexscandir' is the scan direction to consider
1469 * 'ignorables' is an integer list of indexes of ignorable index columns
1471 * Returns TRUE on successful match (ie, the query_pathkeys can be considered
1472 * to match this index).
1475 match_index_to_query_keys(PlannerInfo *root,
1476 IndexOptInfo *index,
1477 ScanDirection indexscandir,
1480 List *index_pathkeys;
1481 ListCell *index_cell;
1485 /* Get the pathkeys that exactly describe the index */
1486 index_pathkeys = build_index_pathkeys(root, index, indexscandir);
1489 * Can we match to the query's requested pathkeys? The inner loop
1490 * skips over ignorable index columns while trying to match.
1492 index_cell = list_head(index_pathkeys);
1495 foreach(r, root->query_pathkeys)
1497 List *rsubkey = (List *) lfirst(r);
1503 if (index_cell == NULL)
1505 isubkey = (List *) lfirst(index_cell);
1506 index_cell = lnext(index_cell);
1507 index_col++; /* index_col is now 1-based */
1509 * Since we are dealing with canonicalized pathkeys, pointer
1510 * comparison is sufficient to determine a match.
1512 if (rsubkey == isubkey)
1513 break; /* matched current query pathkey */
1515 if (!list_member_int(ignorables, index_col))
1516 return false; /* definite failure to match */
1517 /* otherwise loop around and try to match to next index col */
1524 /****************************************************************************
1525 * ---- PATH CREATION UTILITIES ----
1526 ****************************************************************************/
1529 * flatten_clausegroups_list
1530 * Given a list of lists of RestrictInfos, flatten it to a list
1533 * This is used to flatten out the result of group_clauses_by_indexkey()
1534 * to produce an indexclauses list. The original list structure mustn't
1535 * be altered, but it's OK to share copies of the underlying RestrictInfos.
1538 flatten_clausegroups_list(List *clausegroups)
1540 List *allclauses = NIL;
1543 foreach(l, clausegroups)
1544 allclauses = list_concat(allclauses, list_copy((List *) lfirst(l)));
1549 /****************************************************************************
1550 * ---- ROUTINES TO CHECK OPERANDS ----
1551 ****************************************************************************/
1554 * match_index_to_operand()
1555 * Generalized test for a match between an index's key
1556 * and the operand on one side of a restriction or join clause.
1558 * operand: the nodetree to be compared to the index
1559 * indexcol: the column number of the index (counting from 0)
1560 * index: the index of interest
1563 match_index_to_operand(Node *operand,
1565 IndexOptInfo *index)
1570 * Ignore any RelabelType node above the operand. This is needed to
1571 * be able to apply indexscanning in binary-compatible-operator cases.
1572 * Note: we can assume there is at most one RelabelType node;
1573 * eval_const_expressions() will have simplified if more than one.
1575 if (operand && IsA(operand, RelabelType))
1576 operand = (Node *) ((RelabelType *) operand)->arg;
1578 indkey = index->indexkeys[indexcol];
1582 * Simple index column; operand must be a matching Var.
1584 if (operand && IsA(operand, Var) &&
1585 index->rel->relid == ((Var *) operand)->varno &&
1586 indkey == ((Var *) operand)->varattno)
1592 * Index expression; find the correct expression. (This search
1593 * could be avoided, at the cost of complicating all the callers
1594 * of this routine; doesn't seem worth it.)
1596 ListCell *indexpr_item;
1600 indexpr_item = list_head(index->indexprs);
1601 for (i = 0; i < indexcol; i++)
1603 if (index->indexkeys[i] == 0)
1605 if (indexpr_item == NULL)
1606 elog(ERROR, "wrong number of index expressions");
1607 indexpr_item = lnext(indexpr_item);
1610 if (indexpr_item == NULL)
1611 elog(ERROR, "wrong number of index expressions");
1612 indexkey = (Node *) lfirst(indexpr_item);
1615 * Does it match the operand? Again, strip any relabeling.
1617 if (indexkey && IsA(indexkey, RelabelType))
1618 indexkey = (Node *) ((RelabelType *) indexkey)->arg;
1620 if (equal(indexkey, operand))
1627 /****************************************************************************
1628 * ---- ROUTINES FOR "SPECIAL" INDEXABLE OPERATORS ----
1629 ****************************************************************************/
1632 * These routines handle special optimization of operators that can be
1633 * used with index scans even though they are not known to the executor's
1634 * indexscan machinery. The key idea is that these operators allow us
1635 * to derive approximate indexscan qual clauses, such that any tuples
1636 * that pass the operator clause itself must also satisfy the simpler
1637 * indexscan condition(s). Then we can use the indexscan machinery
1638 * to avoid scanning as much of the table as we'd otherwise have to,
1639 * while applying the original operator as a qpqual condition to ensure
1640 * we deliver only the tuples we want. (In essence, we're using a regular
1641 * index as if it were a lossy index.)
1643 * An example of what we're doing is
1644 * textfield LIKE 'abc%'
1645 * from which we can generate the indexscanable conditions
1646 * textfield >= 'abc' AND textfield < 'abd'
1647 * which allow efficient scanning of an index on textfield.
1648 * (In reality, character set and collation issues make the transformation
1649 * from LIKE to indexscan limits rather harder than one might think ...
1650 * but that's the basic idea.)
1652 * Another thing that we do with this machinery is to provide special
1653 * smarts for "boolean" indexes (that is, indexes on boolean columns
1654 * that support boolean equality). We can transform a plain reference
1655 * to the indexkey into "indexkey = true", or "NOT indexkey" into
1656 * "indexkey = false", so as to make the expression indexable using the
1657 * regular index operators. (As of Postgres 8.1, we must do this here
1658 * because constant simplification does the reverse transformation;
1659 * without this code there'd be no way to use such an index at all.)
1661 * Three routines are provided here:
1663 * match_special_index_operator() is just an auxiliary function for
1664 * match_clause_to_indexcol(); after the latter fails to recognize a
1665 * restriction opclause's operator as a member of an index's opclass,
1666 * it asks match_special_index_operator() whether the clause should be
1667 * considered an indexqual anyway.
1669 * match_boolean_index_clause() similarly detects clauses that can be
1670 * converted into boolean equality operators.
1672 * expand_indexqual_conditions() converts a list of lists of RestrictInfo
1673 * nodes (with implicit AND semantics across list elements) into
1674 * a list of clauses that the executor can actually handle. For operators
1675 * that are members of the index's opclass this transformation is a no-op,
1676 * but clauses recognized by match_special_index_operator() or
1677 * match_boolean_index_clause() must be converted into one or more "regular"
1678 * indexqual conditions.
1683 * match_boolean_index_clause
1684 * Recognize restriction clauses that can be matched to a boolean index.
1686 * This should be called only when IsBooleanOpclass() recognizes the
1687 * index's operator class. We check to see if the clause matches the
1691 match_boolean_index_clause(Node *clause,
1693 IndexOptInfo *index)
1696 if (match_index_to_operand(clause, indexcol, index))
1699 if (not_clause(clause))
1701 if (match_index_to_operand((Node *) get_notclausearg((Expr *) clause),
1706 * Since we only consider clauses at top level of WHERE, we can convert
1707 * indexkey IS TRUE and indexkey IS FALSE to index searches as well.
1708 * The different meaning for NULL isn't important.
1710 else if (clause && IsA(clause, BooleanTest))
1712 BooleanTest *btest = (BooleanTest *) clause;
1714 if (btest->booltesttype == IS_TRUE ||
1715 btest->booltesttype == IS_FALSE)
1716 if (match_index_to_operand((Node *) btest->arg,
1724 * match_special_index_operator
1725 * Recognize restriction clauses that can be used to generate
1726 * additional indexscanable qualifications.
1728 * The given clause is already known to be a binary opclause having
1729 * the form (indexkey OP pseudoconst) or (pseudoconst OP indexkey),
1730 * but the OP proved not to be one of the index's opclass operators.
1731 * Return 'true' if we can do something with it anyway.
1734 match_special_index_operator(Expr *clause, Oid opclass,
1735 bool indexkey_on_left)
1737 bool isIndexable = false;
1741 Const *prefix = NULL;
1745 * Currently, all known special operators require the indexkey on the
1746 * left, but this test could be pushed into the switch statement if
1747 * some are added that do not...
1749 if (!indexkey_on_left)
1752 /* we know these will succeed */
1753 rightop = get_rightop(clause);
1754 expr_op = ((OpExpr *) clause)->opno;
1756 /* again, required for all current special ops: */
1757 if (!IsA(rightop, Const) ||
1758 ((Const *) rightop)->constisnull)
1760 patt = (Const *) rightop;
1764 case OID_TEXT_LIKE_OP:
1765 case OID_BPCHAR_LIKE_OP:
1766 case OID_NAME_LIKE_OP:
1767 /* the right-hand const is type text for all of these */
1768 isIndexable = pattern_fixed_prefix(patt, Pattern_Type_Like,
1769 &prefix, &rest) != Pattern_Prefix_None;
1772 case OID_BYTEA_LIKE_OP:
1773 isIndexable = pattern_fixed_prefix(patt, Pattern_Type_Like,
1774 &prefix, &rest) != Pattern_Prefix_None;
1777 case OID_TEXT_ICLIKE_OP:
1778 case OID_BPCHAR_ICLIKE_OP:
1779 case OID_NAME_ICLIKE_OP:
1780 /* the right-hand const is type text for all of these */
1781 isIndexable = pattern_fixed_prefix(patt, Pattern_Type_Like_IC,
1782 &prefix, &rest) != Pattern_Prefix_None;
1785 case OID_TEXT_REGEXEQ_OP:
1786 case OID_BPCHAR_REGEXEQ_OP:
1787 case OID_NAME_REGEXEQ_OP:
1788 /* the right-hand const is type text for all of these */
1789 isIndexable = pattern_fixed_prefix(patt, Pattern_Type_Regex,
1790 &prefix, &rest) != Pattern_Prefix_None;
1793 case OID_TEXT_ICREGEXEQ_OP:
1794 case OID_BPCHAR_ICREGEXEQ_OP:
1795 case OID_NAME_ICREGEXEQ_OP:
1796 /* the right-hand const is type text for all of these */
1797 isIndexable = pattern_fixed_prefix(patt, Pattern_Type_Regex_IC,
1798 &prefix, &rest) != Pattern_Prefix_None;
1801 case OID_INET_SUB_OP:
1802 case OID_INET_SUBEQ_OP:
1803 case OID_CIDR_SUB_OP:
1804 case OID_CIDR_SUBEQ_OP:
1811 pfree(DatumGetPointer(prefix->constvalue));
1815 /* done if the expression doesn't look indexable */
1820 * Must also check that index's opclass supports the operators we will
1821 * want to apply. (A hash index, for example, will not support ">=".)
1822 * Currently, only btree supports the operators we need.
1824 * We insist on the opclass being the specific one we expect, else we'd
1825 * do the wrong thing if someone were to make a reverse-sort opclass
1826 * with the same operators.
1830 case OID_TEXT_LIKE_OP:
1831 case OID_TEXT_ICLIKE_OP:
1832 case OID_TEXT_REGEXEQ_OP:
1833 case OID_TEXT_ICREGEXEQ_OP:
1834 /* text operators will be used for varchar inputs, too */
1836 (opclass == TEXT_PATTERN_BTREE_OPS_OID) ||
1837 (opclass == TEXT_BTREE_OPS_OID && lc_collate_is_c()) ||
1838 (opclass == VARCHAR_PATTERN_BTREE_OPS_OID) ||
1839 (opclass == VARCHAR_BTREE_OPS_OID && lc_collate_is_c());
1842 case OID_BPCHAR_LIKE_OP:
1843 case OID_BPCHAR_ICLIKE_OP:
1844 case OID_BPCHAR_REGEXEQ_OP:
1845 case OID_BPCHAR_ICREGEXEQ_OP:
1847 (opclass == BPCHAR_PATTERN_BTREE_OPS_OID) ||
1848 (opclass == BPCHAR_BTREE_OPS_OID && lc_collate_is_c());
1851 case OID_NAME_LIKE_OP:
1852 case OID_NAME_ICLIKE_OP:
1853 case OID_NAME_REGEXEQ_OP:
1854 case OID_NAME_ICREGEXEQ_OP:
1856 (opclass == NAME_PATTERN_BTREE_OPS_OID) ||
1857 (opclass == NAME_BTREE_OPS_OID && lc_collate_is_c());
1860 case OID_BYTEA_LIKE_OP:
1861 isIndexable = (opclass == BYTEA_BTREE_OPS_OID);
1864 case OID_INET_SUB_OP:
1865 case OID_INET_SUBEQ_OP:
1866 isIndexable = (opclass == INET_BTREE_OPS_OID);
1869 case OID_CIDR_SUB_OP:
1870 case OID_CIDR_SUBEQ_OP:
1871 isIndexable = (opclass == CIDR_BTREE_OPS_OID);
1879 * expand_indexqual_conditions
1880 * Given a list of sublists of RestrictInfo nodes, produce a flat list
1881 * of index qual clauses. Standard qual clauses (those in the index's
1882 * opclass) are passed through unchanged. Boolean clauses and "special"
1883 * index operators are expanded into clauses that the indexscan machinery
1884 * will know what to do with.
1886 * The input list is ordered by index key, and so the output list is too.
1887 * (The latter is not depended on by any part of the core planner, I believe,
1888 * but parts of the executor require it, and so do the amcostestimate
1892 expand_indexqual_conditions(IndexOptInfo *index, List *clausegroups)
1894 List *resultquals = NIL;
1895 ListCell *clausegroup_item;
1897 Oid *classes = index->classlist;
1899 if (clausegroups == NIL)
1902 clausegroup_item = list_head(clausegroups);
1905 Oid curClass = classes[0];
1908 foreach(l, (List *) lfirst(clausegroup_item))
1910 RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
1912 /* First check for boolean cases */
1913 if (IsBooleanOpclass(curClass))
1917 boolqual = expand_boolean_index_clause((Node *) rinfo->clause,
1922 resultquals = lappend(resultquals,
1923 make_restrictinfo(boolqual,
1930 resultquals = list_concat(resultquals,
1931 expand_indexqual_condition(rinfo,
1935 clausegroup_item = lnext(clausegroup_item);
1939 } while (clausegroup_item != NULL && !DoneMatchingIndexKeys(classes));
1941 Assert(clausegroup_item == NULL); /* else more groups than indexkeys */
1947 * expand_boolean_index_clause
1948 * Convert a clause recognized by match_boolean_index_clause into
1949 * a boolean equality operator clause.
1951 * Returns NULL if the clause isn't a boolean index qual.
1954 expand_boolean_index_clause(Node *clause,
1956 IndexOptInfo *index)
1959 if (match_index_to_operand(clause, indexcol, index))
1961 /* convert to indexkey = TRUE */
1962 return make_opclause(BooleanEqualOperator, BOOLOID, false,
1964 (Expr *) makeBoolConst(true, false));
1967 if (not_clause(clause))
1969 Node *arg = (Node *) get_notclausearg((Expr *) clause);
1971 /* It must have matched the indexkey */
1972 Assert(match_index_to_operand(arg, indexcol, index));
1973 /* convert to indexkey = FALSE */
1974 return make_opclause(BooleanEqualOperator, BOOLOID, false,
1976 (Expr *) makeBoolConst(false, false));
1978 if (clause && IsA(clause, BooleanTest))
1980 BooleanTest *btest = (BooleanTest *) clause;
1981 Node *arg = (Node *) btest->arg;
1983 /* It must have matched the indexkey */
1984 Assert(match_index_to_operand(arg, indexcol, index));
1985 if (btest->booltesttype == IS_TRUE)
1987 /* convert to indexkey = TRUE */
1988 return make_opclause(BooleanEqualOperator, BOOLOID, false,
1990 (Expr *) makeBoolConst(true, false));
1992 if (btest->booltesttype == IS_FALSE)
1994 /* convert to indexkey = FALSE */
1995 return make_opclause(BooleanEqualOperator, BOOLOID, false,
1997 (Expr *) makeBoolConst(false, false));
2007 * expand_indexqual_condition --- expand a single indexqual condition
2008 * (other than a boolean-qual case)
2010 * The input is a single RestrictInfo, the output a list of RestrictInfos
2013 expand_indexqual_condition(RestrictInfo *rinfo, Oid opclass)
2015 Expr *clause = rinfo->clause;
2016 /* we know these will succeed */
2017 Node *leftop = get_leftop(clause);
2018 Node *rightop = get_rightop(clause);
2019 Oid expr_op = ((OpExpr *) clause)->opno;
2020 Const *patt = (Const *) rightop;
2021 Const *prefix = NULL;
2023 Pattern_Prefix_Status pstatus;
2029 * LIKE and regex operators are not members of any index
2030 * opclass, so if we find one in an indexqual list we can
2031 * assume that it was accepted by
2032 * match_special_index_operator().
2034 case OID_TEXT_LIKE_OP:
2035 case OID_BPCHAR_LIKE_OP:
2036 case OID_NAME_LIKE_OP:
2037 case OID_BYTEA_LIKE_OP:
2038 pstatus = pattern_fixed_prefix(patt, Pattern_Type_Like,
2040 result = prefix_quals(leftop, opclass, prefix, pstatus);
2043 case OID_TEXT_ICLIKE_OP:
2044 case OID_BPCHAR_ICLIKE_OP:
2045 case OID_NAME_ICLIKE_OP:
2046 /* the right-hand const is type text for all of these */
2047 pstatus = pattern_fixed_prefix(patt, Pattern_Type_Like_IC,
2049 result = prefix_quals(leftop, opclass, prefix, pstatus);
2052 case OID_TEXT_REGEXEQ_OP:
2053 case OID_BPCHAR_REGEXEQ_OP:
2054 case OID_NAME_REGEXEQ_OP:
2055 /* the right-hand const is type text for all of these */
2056 pstatus = pattern_fixed_prefix(patt, Pattern_Type_Regex,
2058 result = prefix_quals(leftop, opclass, prefix, pstatus);
2061 case OID_TEXT_ICREGEXEQ_OP:
2062 case OID_BPCHAR_ICREGEXEQ_OP:
2063 case OID_NAME_ICREGEXEQ_OP:
2064 /* the right-hand const is type text for all of these */
2065 pstatus = pattern_fixed_prefix(patt, Pattern_Type_Regex_IC,
2067 result = prefix_quals(leftop, opclass, prefix, pstatus);
2070 case OID_INET_SUB_OP:
2071 case OID_INET_SUBEQ_OP:
2072 case OID_CIDR_SUB_OP:
2073 case OID_CIDR_SUBEQ_OP:
2074 result = network_prefix_quals(leftop, expr_op, opclass,
2079 result = list_make1(rinfo);
2087 * Given a fixed prefix that all the "leftop" values must have,
2088 * generate suitable indexqual condition(s). opclass is the index
2089 * operator class; we use it to deduce the appropriate comparison
2090 * operators and operand datatypes.
2093 prefix_quals(Node *leftop, Oid opclass,
2094 Const *prefix_const, Pattern_Prefix_Status pstatus)
2102 Assert(pstatus != Pattern_Prefix_None);
2106 case TEXT_BTREE_OPS_OID:
2107 case TEXT_PATTERN_BTREE_OPS_OID:
2111 case VARCHAR_BTREE_OPS_OID:
2112 case VARCHAR_PATTERN_BTREE_OPS_OID:
2113 datatype = VARCHAROID;
2116 case BPCHAR_BTREE_OPS_OID:
2117 case BPCHAR_PATTERN_BTREE_OPS_OID:
2118 datatype = BPCHAROID;
2121 case NAME_BTREE_OPS_OID:
2122 case NAME_PATTERN_BTREE_OPS_OID:
2126 case BYTEA_BTREE_OPS_OID:
2127 datatype = BYTEAOID;
2131 /* shouldn't get here */
2132 elog(ERROR, "unexpected opclass: %u", opclass);
2137 * If necessary, coerce the prefix constant to the right type. The
2138 * given prefix constant is either text or bytea type.
2140 if (prefix_const->consttype != datatype)
2144 switch (prefix_const->consttype)
2147 prefix = DatumGetCString(DirectFunctionCall1(textout,
2148 prefix_const->constvalue));
2151 prefix = DatumGetCString(DirectFunctionCall1(byteaout,
2152 prefix_const->constvalue));
2155 elog(ERROR, "unexpected const type: %u",
2156 prefix_const->consttype);
2159 prefix_const = string_to_const(prefix, datatype);
2164 * If we found an exact-match pattern, generate an "=" indexqual.
2166 if (pstatus == Pattern_Prefix_Exact)
2168 oproid = get_opclass_member(opclass, InvalidOid,
2169 BTEqualStrategyNumber);
2170 if (oproid == InvalidOid)
2171 elog(ERROR, "no = operator for opclass %u", opclass);
2172 expr = make_opclause(oproid, BOOLOID, false,
2173 (Expr *) leftop, (Expr *) prefix_const);
2174 result = list_make1(make_restrictinfo(expr, true, NULL));
2179 * Otherwise, we have a nonempty required prefix of the values.
2181 * We can always say "x >= prefix".
2183 oproid = get_opclass_member(opclass, InvalidOid,
2184 BTGreaterEqualStrategyNumber);
2185 if (oproid == InvalidOid)
2186 elog(ERROR, "no >= operator for opclass %u", opclass);
2187 expr = make_opclause(oproid, BOOLOID, false,
2188 (Expr *) leftop, (Expr *) prefix_const);
2189 result = list_make1(make_restrictinfo(expr, true, NULL));
2192 * If we can create a string larger than the prefix, we can say
2196 greaterstr = make_greater_string(prefix_const);
2199 oproid = get_opclass_member(opclass, InvalidOid,
2200 BTLessStrategyNumber);
2201 if (oproid == InvalidOid)
2202 elog(ERROR, "no < operator for opclass %u", opclass);
2203 expr = make_opclause(oproid, BOOLOID, false,
2204 (Expr *) leftop, (Expr *) greaterstr);
2205 result = lappend(result, make_restrictinfo(expr, true, NULL));
2212 * Given a leftop and a rightop, and a inet-class sup/sub operator,
2213 * generate suitable indexqual condition(s). expr_op is the original
2214 * operator, and opclass is the index opclass.
2217 network_prefix_quals(Node *leftop, Oid expr_op, Oid opclass, Datum rightop)
2230 case OID_INET_SUB_OP:
2234 case OID_INET_SUBEQ_OP:
2238 case OID_CIDR_SUB_OP:
2242 case OID_CIDR_SUBEQ_OP:
2247 elog(ERROR, "unexpected operator: %u", expr_op);
2252 * create clause "key >= network_scan_first( rightop )", or ">" if the
2253 * operator disallows equality.
2257 opr1oid = get_opclass_member(opclass, InvalidOid,
2258 BTGreaterEqualStrategyNumber);
2259 if (opr1oid == InvalidOid)
2260 elog(ERROR, "no >= operator for opclass %u", opclass);
2264 opr1oid = get_opclass_member(opclass, InvalidOid,
2265 BTGreaterStrategyNumber);
2266 if (opr1oid == InvalidOid)
2267 elog(ERROR, "no > operator for opclass %u", opclass);
2270 opr1right = network_scan_first(rightop);
2272 expr = make_opclause(opr1oid, BOOLOID, false,
2274 (Expr *) makeConst(datatype, -1, opr1right,
2276 result = list_make1(make_restrictinfo(expr, true, NULL));
2278 /* create clause "key <= network_scan_last( rightop )" */
2280 opr2oid = get_opclass_member(opclass, InvalidOid,
2281 BTLessEqualStrategyNumber);
2282 if (opr2oid == InvalidOid)
2283 elog(ERROR, "no <= operator for opclass %u", opclass);
2285 opr2right = network_scan_last(rightop);
2287 expr = make_opclause(opr2oid, BOOLOID, false,
2289 (Expr *) makeConst(datatype, -1, opr2right,
2291 result = lappend(result, make_restrictinfo(expr, true, NULL));
2297 * Handy subroutines for match_special_index_operator() and friends.
2301 * Generate a Datum of the appropriate type from a C string.
2302 * Note that all of the supported types are pass-by-ref, so the
2303 * returned value should be pfree'd if no longer needed.
2306 string_to_datum(const char *str, Oid datatype)
2309 * We cheat a little by assuming that textin() will do for bpchar and
2310 * varchar constants too...
2312 if (datatype == NAMEOID)
2313 return DirectFunctionCall1(namein, CStringGetDatum(str));
2314 else if (datatype == BYTEAOID)
2315 return DirectFunctionCall1(byteain, CStringGetDatum(str));
2317 return DirectFunctionCall1(textin, CStringGetDatum(str));
2321 * Generate a Const node of the appropriate type from a C string.
2324 string_to_const(const char *str, Oid datatype)
2326 Datum conval = string_to_datum(str, datatype);
2328 return makeConst(datatype, ((datatype == NAMEOID) ? NAMEDATALEN : -1),
2329 conval, false, false);