]> granicus.if.org Git - postgresql/blob - src/backend/optimizer/path/indxpath.c
1d74497828920995b1fe49699f30e3a79255d0af
[postgresql] / src / backend / optimizer / path / indxpath.c
1 /*-------------------------------------------------------------------------
2  *
3  * indxpath.c
4  *        Routines to determine which indexes are usable for scanning a
5  *        given relation, and create Paths accordingly.
6  *
7  * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
8  * Portions Copyright (c) 1994, Regents of the University of California
9  *
10  *
11  * IDENTIFICATION
12  *        $PostgreSQL: pgsql/src/backend/optimizer/path/indxpath.c,v 1.189 2005/09/22 23:25:07 tgl Exp $
13  *
14  *-------------------------------------------------------------------------
15  */
16 #include "postgres.h"
17
18 #include <math.h>
19
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"
36
37
38 /*
39  * DoneMatchingIndexKeys() - MACRO
40  */
41 #define DoneMatchingIndexKeys(classes)  (classes[0] == InvalidOid)
42
43 #define is_indexable_operator(clause,opclass,indexkey_on_left) \
44         (indexable_operator(clause,opclass,indexkey_on_left) != InvalidOid)
45
46 #define IsBooleanOpclass(opclass) \
47         ((opclass) == BOOL_BTREE_OPS_OID || (opclass) == BOOL_HASH_OPS_OID)
48
49
50 static List *find_usable_indexes(PlannerInfo *root, RelOptInfo *rel,
51                                                                  List *clauses, List *outer_clauses,
52                                                                  bool istoplevel, bool isjoininner,
53                                                                  Relids outer_relids);
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,
59                                                  RestrictInfo *rinfo,
60                                                  Relids outer_relids);
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,
65                                                           Relids outer_relids);
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,
69                                                                    IndexOptInfo *index,
70                                                                    List *restrictclauses,
71                                                                    ScanDirection *indexscandir);
72 static List *identify_ignorable_ordering_cols(PlannerInfo *root,
73                                                                                           IndexOptInfo *index,
74                                                                                           List *restrictclauses);
75 static bool match_index_to_query_keys(PlannerInfo *root,
76                                                                           IndexOptInfo *index,
77                                                                           ScanDirection indexscandir,
78                                                                           List *ignorables);
79 static bool match_boolean_index_clause(Node *clause, int indexcol,
80                                                                            IndexOptInfo *index);
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,
84                                                                                  IndexOptInfo *index);
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,
89                                          Datum rightop);
90 static Datum string_to_datum(const char *str, Oid datatype);
91 static Const *string_to_const(const char *str, Oid datatype);
92
93
94 /*
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).
98  *
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.
103  *
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.
112  *
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.
115  *
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.
121  *
122  * 'rel' is the relation for which we want to generate index paths
123  *
124  * Note: check_partial_indexes() must have been run previously.
125  */
126 void
127 create_index_paths(PlannerInfo *root, RelOptInfo *rel)
128 {
129         List       *indexpaths;
130         List       *bitindexpaths;
131         ListCell   *l;
132
133         /* Skip the whole mess if no indexes */
134         if (rel->indexlist == NIL)
135         {
136                 rel->index_outer_relids = NULL;
137                 return;
138         }
139
140         /*
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.
145          */
146         rel->index_outer_relids = indexable_outerrelids(rel);
147
148         /*
149          * Find all the index paths that are directly usable for this relation
150          * (ie, are valid without considering OR or JOIN clauses).
151          */
152         indexpaths = find_usable_indexes(root, rel,
153                                                                          rel->baserestrictinfo, NIL,
154                                                                          true, false, NULL);
155
156         /*
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.
161          */
162         bitindexpaths = NIL;
163         foreach(l, indexpaths)
164         {
165                 IndexPath  *ipath = (IndexPath *) lfirst(l);
166
167                 add_path(rel, (Path *) ipath);
168
169                 if (ipath->indexselectivity < 1.0 &&
170                         !ScanDirectionIsBackward(ipath->indexscandir))
171                         bitindexpaths = lappend(bitindexpaths, ipath);
172         }
173
174         /*
175          * Generate BitmapOrPaths for any suitable OR-clauses present in the
176          * restriction list.  Add these to bitindexpaths.
177          */
178         indexpaths = generate_bitmap_or_paths(root, rel,
179                                                                                   rel->baserestrictinfo, NIL,
180                                                                                   false, NULL);
181         bitindexpaths = list_concat(bitindexpaths, indexpaths);
182
183         /*
184          * If we found anything usable, generate a BitmapHeapPath for the
185          * most promising combination of bitmap index paths.
186          */
187         if (bitindexpaths != NIL)
188         {
189                 Path       *bitmapqual;
190                 BitmapHeapPath *bpath;
191
192                 bitmapqual = choose_bitmap_and(root, rel, bitindexpaths);
193                 bpath = create_bitmap_heap_path(root, rel, bitmapqual, false);
194                 add_path(rel, (Path *) bpath);
195         }
196 }
197
198
199 /*----------
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.
203  *
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
208  * examples like
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.
217  *
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
221  * clauses.
222  *
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)
232  *
233  * Note: check_partial_indexes() must have been run previously.
234  *----------
235  */
236 static List *
237 find_usable_indexes(PlannerInfo *root, RelOptInfo *rel,
238                                         List *clauses, List *outer_clauses,
239                                         bool istoplevel, bool isjoininner,
240                                         Relids outer_relids)
241 {
242         List       *result = NIL;
243         List       *all_clauses = NIL;          /* not computed till needed */
244         ListCell   *ilist;
245
246         foreach(ilist, rel->indexlist)
247         {
248                 IndexOptInfo *index = (IndexOptInfo *) lfirst(ilist);
249                 IndexPath  *ipath;
250                 List       *restrictclauses;
251                 List       *index_pathkeys;
252                 List       *useful_pathkeys;
253                 bool            useful_predicate;
254                 bool            found_clause;
255                 bool            index_is_ordered;
256
257                 /*
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.
265                  *
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.)
271                  */
272                 useful_predicate = false;
273                 if (index->indpred != NIL)
274                 {
275                         if (index->predOK)
276                         {
277                                 if (istoplevel)
278                                 {
279                                         /* we know predicate was proven from these clauses */
280                                         useful_predicate = true;
281                                 }
282                         }
283                         else
284                         {
285                                 if (istoplevel)
286                                         continue;               /* no point in trying to prove it */
287
288                                 /* Form all_clauses if not done already */
289                                 if (all_clauses == NIL)
290                                         all_clauses = list_concat(list_copy(clauses),
291                                                                                           outer_clauses);
292
293                                 if (!predicate_implied_by(index->indpred, all_clauses))
294                                         continue;               /* can't use it at all */
295
296                                 if (!predicate_implied_by(index->indpred, outer_clauses))
297                                         useful_predicate = true;
298                         }
299                 }
300
301                 /*
302                  * 1. Match the index against the available restriction clauses.
303                  * found_clause is set true only if at least one of the current
304                  * clauses was used.
305                  */
306                 restrictclauses = group_clauses_by_indexkey(index,
307                                                                                                         clauses,
308                                                                                                         outer_clauses,
309                                                                                                         outer_relids,
310                                                                                                         &found_clause);
311
312                 /*
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.
316                  */
317                 if (restrictclauses == NIL && !index->amoptionalkey)
318                         continue;
319
320                 /*
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.
324                  */
325                 index_is_ordered = OidIsValid(index->ordering[0]);
326                 if (istoplevel && index_is_ordered && !isjoininner)
327                 {
328                         index_pathkeys = build_index_pathkeys(root, index,
329                                                                                                   ForwardScanDirection);
330                         useful_pathkeys = truncate_useless_pathkeys(root, rel,
331                                                                                                                 index_pathkeys);
332                 }
333                 else
334                         useful_pathkeys = NIL;
335
336                 /*
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
341                  * clauses.
342                  */
343                 if (found_clause || useful_pathkeys != NIL || useful_predicate)
344                 {
345                         ipath = create_index_path(root, index,
346                                                                           restrictclauses,
347                                                                           useful_pathkeys,
348                                                                           index_is_ordered ?
349                                                                           ForwardScanDirection :
350                                                                           NoMovementScanDirection,
351                                                                           isjoininner);
352                         result = lappend(result, ipath);
353                 }
354
355                 /*
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
359                  * at top level.
360                  */
361                 if (istoplevel && index_is_ordered && !isjoininner &&
362                         root->query_pathkeys != NIL &&
363                         pathkeys_useful_for_ordering(root, useful_pathkeys) == 0)
364                 {
365                         ScanDirection   indexscandir;
366
367                         if (match_variant_ordering(root, index, restrictclauses,
368                                                                            &indexscandir))
369                         {
370                                 ipath = create_index_path(root, index,
371                                                                                   restrictclauses,
372                                                                                   root->query_pathkeys,
373                                                                                   indexscandir,
374                                                                                   false);
375                                 result = lappend(result, ipath);
376                         }
377                 }
378         }
379
380         return result;
381 }
382
383
384 /*
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.
389  *
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.)
393  */
394 List *
395 generate_bitmap_or_paths(PlannerInfo *root, RelOptInfo *rel,
396                                                  List *clauses, List *outer_clauses,
397                                                  bool isjoininner,
398                                                  Relids outer_relids)
399 {
400         List       *result = NIL;
401         List       *all_clauses;
402         ListCell   *l;
403
404         /*
405          * We can use both the current and outer clauses as context for
406          * find_usable_indexes
407          */
408         all_clauses = list_concat(list_copy(clauses), outer_clauses);
409
410         foreach(l, clauses)
411         {
412                 RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
413                 List   *pathlist;
414                 Path   *bitmapqual;
415                 ListCell *j;
416
417                 Assert(IsA(rinfo, RestrictInfo));
418                 /* Ignore RestrictInfos that aren't ORs */
419                 if (!restriction_is_or_clause(rinfo))
420                         continue;
421
422                 /*
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.
425                  */
426                 pathlist = NIL;
427                 foreach(j, ((BoolExpr *) rinfo->orclause)->args)
428                 {
429                         Node   *orarg = (Node *) lfirst(j);
430                         List   *indlist;
431
432                         /* OR arguments should be ANDs or sub-RestrictInfos */
433                         if (and_clause(orarg))
434                         {
435                                 List   *andargs = ((BoolExpr *) orarg)->args;
436
437                                 indlist = find_usable_indexes(root, rel,
438                                                                                           andargs,
439                                                                                           all_clauses,
440                                                                                           false,
441                                                                                           isjoininner,
442                                                                                           outer_relids);
443                                 /* Recurse in case there are sub-ORs */
444                                 indlist = list_concat(indlist,
445                                                                           generate_bitmap_or_paths(root, rel,
446                                                                                                                            andargs,
447                                                                                                                            all_clauses,
448                                                                                                                            isjoininner,
449                                                                                                                            outer_relids));
450                         }
451                         else
452                         {
453                                 Assert(IsA(orarg, RestrictInfo));
454                                 Assert(!restriction_is_or_clause((RestrictInfo *) orarg));
455                                 indlist = find_usable_indexes(root, rel,
456                                                                                           list_make1(orarg),
457                                                                                           all_clauses,
458                                                                                           false,
459                                                                                           isjoininner,
460                                                                                           outer_relids);
461                         }
462                         /*
463                          * If nothing matched this arm, we can't do anything
464                          * with this OR clause.
465                          */
466                         if (indlist == NIL)
467                         {
468                                 pathlist = NIL;
469                                 break;
470                         }
471                         /*
472                          * OK, pick the most promising AND combination,
473                          * and add it to pathlist.
474                          */
475                         bitmapqual = choose_bitmap_and(root, rel, indlist);
476                         pathlist = lappend(pathlist, bitmapqual);
477                 }
478                 /*
479                  * If we have a match for every arm, then turn them
480                  * into a BitmapOrPath, and add to result list.
481                  */
482                 if (pathlist != NIL)
483                 {
484                         bitmapqual = (Path *) create_bitmap_or_path(root, rel, pathlist);
485                         result = lappend(result, bitmapqual);
486                 }
487         }
488
489         return result;
490 }
491
492
493 /*
494  * choose_bitmap_and
495  *              Given a nonempty list of bitmap paths, AND them into one path.
496  *
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.
500  *
501  * The result is either a single one of the inputs, or a BitmapAndPath
502  * combining multiple inputs.
503  */
504 static Path *
505 choose_bitmap_and(PlannerInfo *root, RelOptInfo *rel, List *paths)
506 {
507         int                     npaths = list_length(paths);
508         Path      **patharray;
509         Cost            costsofar;
510         List       *qualsofar;
511         ListCell   *lastcell;
512         int                     i;
513         ListCell   *l;
514
515         Assert(npaths > 0);                                                     /* else caller error */
516         if (npaths == 1)
517                 return (Path *) linitial(paths);                /* easy case */
518
519         /*
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.
526          *
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.)
535          *
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
540          * work harder.
541          *
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
545          * likely to apply.
546          */
547
548         /* Convert list to array so we can apply qsort */
549         patharray = (Path **) palloc(npaths * sizeof(Path *));
550         i = 0;
551         foreach(l, paths)
552         {
553                 patharray[i++] = (Path *) lfirst(l);
554         }
555         qsort(patharray, npaths, sizeof(Path *), bitmap_path_comparator);
556
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);
561         else
562                 qualsofar = NIL;
563         lastcell = list_head(paths);            /* for quick deletions */
564
565         for (i = 1; i < npaths; i++)
566         {
567                 Path   *newpath = patharray[i];
568                 List   *newqual = NIL;
569                 Cost    newcost;
570
571                 if (IsA(newpath, IndexPath))
572                 {
573                         newqual = ((IndexPath *) newpath)->indexclauses;
574                         if (list_difference_ptr(newqual, qualsofar) == NIL)
575                                 continue;               /* redundant */
576                 }
577
578                 paths = lappend(paths, newpath);
579                 newcost = bitmap_and_cost_est(root, rel, paths);
580                 if (newcost < costsofar)
581                 {
582                         costsofar = newcost;
583                         if (newqual)
584                                 qualsofar = list_concat(qualsofar, list_copy(newqual));
585                         lastcell = lnext(lastcell);
586                 }
587                 else
588                 {
589                         paths = list_delete_cell(paths, lnext(lastcell), lastcell);
590                 }
591                 Assert(lnext(lastcell) == NULL);
592         }
593
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);
597 }
598
599 /* qsort comparator to sort in increasing selectivity order */
600 static int
601 bitmap_path_comparator(const void *a, const void *b)
602 {
603         Path       *pa = *(Path * const *) a;
604         Path       *pb = *(Path * const *) b;
605         Cost            acost;
606         Cost            bcost;
607         Selectivity     aselec;
608         Selectivity     bselec;
609
610         cost_bitmap_tree_node(pa, &acost, &aselec);
611         cost_bitmap_tree_node(pb, &bcost, &bselec);
612
613         if (aselec < bselec)
614                 return -1;
615         if (aselec > bselec)
616                 return 1;
617         /* if identical selectivity, sort by cost */
618         if (acost < bcost)
619                 return -1;
620         if (acost > bcost)
621                 return 1;
622         return 0;
623 }
624
625 /*
626  * Estimate the cost of actually executing a BitmapAnd with the given
627  * inputs.
628  */
629 static Cost
630 bitmap_and_cost_est(PlannerInfo *root, RelOptInfo *rel, List *paths)
631 {
632         BitmapAndPath apath;
633         Path            bpath;
634
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);
640
641         /* Now we can do cost_bitmap_heap_scan */
642         cost_bitmap_heap_scan(&bpath, root, rel, (Path *) &apath, false);
643
644         return bpath.total_cost;
645 }
646
647
648 /****************************************************************************
649  *                              ----  ROUTINES TO CHECK RESTRICTIONS  ----
650  ****************************************************************************/
651
652
653 /*
654  * group_clauses_by_indexkey
655  *        Find restriction clauses that can be used with an index.
656  *
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().)
661  *
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.
665  *
666  * outer_relids determines what Vars will be allowed on the other side
667  * of a possible index qual; see match_clause_to_indexcol().
668  *
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.
673  *
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.
677  *
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).
681  */
682 List *
683 group_clauses_by_indexkey(IndexOptInfo *index,
684                                                   List *clauses, List *outer_clauses,
685                                                   Relids outer_relids,
686                                                   bool *found_clause)
687 {
688         List       *clausegroup_list = NIL;
689         bool            found_outer_clause = false;
690         int                     indexcol = 0;
691         Oid                *classes = index->classlist;
692
693         *found_clause = false;          /* default result */
694
695         if (clauses == NIL && outer_clauses == NIL)
696                 return NIL;                             /* cannot succeed */
697
698         do
699         {
700                 Oid                     curClass = classes[0];
701                 List       *clausegroup = NIL;
702                 ListCell   *l;
703
704                 /* check the current clauses */
705                 foreach(l, clauses)
706                 {
707                         RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
708
709                         Assert(IsA(rinfo, RestrictInfo));
710                         if (match_clause_to_indexcol(index,
711                                                                                  indexcol,
712                                                                                  curClass,
713                                                                                  rinfo,
714                                                                                  outer_relids))
715                         {
716                                 clausegroup = list_append_unique_ptr(clausegroup, rinfo);
717                                 *found_clause = true;
718                         }
719                 }
720
721                 /* check the outer clauses */
722                 foreach(l, outer_clauses)
723                 {
724                         RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
725
726                         Assert(IsA(rinfo, RestrictInfo));
727                         if (match_clause_to_indexcol(index,
728                                                                                  indexcol,
729                                                                                  curClass,
730                                                                                  rinfo,
731                                                                                  outer_relids))
732                         {
733                                 clausegroup = list_append_unique_ptr(clausegroup, rinfo);
734                                 found_outer_clause = true;
735                         }
736                 }
737
738                 /*
739                  * If no clauses match this key, check for amoptionalkey restriction.
740                  */
741                 if (clausegroup == NIL && !index->amoptionalkey && indexcol == 0)
742                         return NIL;
743
744                 clausegroup_list = lappend(clausegroup_list, clausegroup);
745
746                 indexcol++;
747                 classes++;
748
749         } while (!DoneMatchingIndexKeys(classes));
750
751         if (!*found_clause && !found_outer_clause)
752                 return NIL;                             /* no indexable clauses anywhere */
753
754         return clausegroup_list;
755 }
756
757
758 /*
759  * match_clause_to_indexcol()
760  *        Determines whether a restriction clause matches a column of an index.
761  *
762  *        To match a normal index, the clause:
763  *
764  *        (1)  must be in the form (indexkey op const) or (const op indexkey);
765  *                 and
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().
769  *
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.
776  *
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.
782  *
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.
788  *
789  *        For boolean indexes, it is also possible to match the clause directly
790  *        to the indexkey; or perhaps the clause is (NOT indexkey).
791  *
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).
796  *
797  * Returns true if the clause can be used with this index key.
798  *
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.
801  */
802 static bool
803 match_clause_to_indexcol(IndexOptInfo *index,
804                                                  int indexcol,
805                                                  Oid opclass,
806                                                  RestrictInfo *rinfo,
807                                                  Relids outer_relids)
808 {
809         Expr       *clause = rinfo->clause;
810         Node       *leftop,
811                            *rightop;
812
813         /* First check for boolean-index cases. */
814         if (IsBooleanOpclass(opclass))
815         {
816                 if (match_boolean_index_clause((Node *) clause, indexcol, index))
817                         return true;
818         }
819
820         /* Else clause must be a binary opclause. */
821         if (!is_opclause(clause))
822                 return false;
823         leftop = get_leftop(clause);
824         rightop = get_rightop(clause);
825         if (!leftop || !rightop)
826                 return false;
827
828         /*
829          * Check for clauses of the form: (indexkey operator constant) or
830          * (constant operator indexkey).  See above notes about const-ness.
831          */
832         if (match_index_to_operand(leftop, indexcol, index) &&
833                 bms_is_subset(rinfo->right_relids, outer_relids) &&
834                 !contain_volatile_functions(rightop))
835         {
836                 if (is_indexable_operator(clause, opclass, true))
837                         return true;
838
839                 /*
840                  * If we didn't find a member of the index's opclass, see whether
841                  * it is a "special" indexable operator.
842                  */
843                 if (match_special_index_operator(clause, opclass, true))
844                         return true;
845                 return false;
846         }
847
848         if (match_index_to_operand(rightop, indexcol, index) &&
849                 bms_is_subset(rinfo->left_relids, outer_relids) &&
850                 !contain_volatile_functions(leftop))
851         {
852                 if (is_indexable_operator(clause, opclass, false))
853                         return true;
854
855                 /*
856                  * If we didn't find a member of the index's opclass, see whether
857                  * it is a "special" indexable operator.
858                  */
859                 if (match_special_index_operator(clause, opclass, false))
860                         return true;
861                 return false;
862         }
863
864         return false;
865 }
866
867 /*
868  * indexable_operator
869  *        Does a binary opclause contain an operator matching the index opclass?
870  *
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.
874  *
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.)
878  */
879 static Oid
880 indexable_operator(Expr *clause, Oid opclass, bool indexkey_on_left)
881 {
882         Oid                     expr_op = ((OpExpr *) clause)->opno;
883         Oid                     commuted_op;
884
885         /* Get the commuted operator if necessary */
886         if (indexkey_on_left)
887                 commuted_op = expr_op;
888         else
889                 commuted_op = get_commutator(expr_op);
890         if (commuted_op == InvalidOid)
891                 return InvalidOid;
892
893         /* OK if the (commuted) operator is a member of the index's opclass */
894         if (op_in_opclass(commuted_op, opclass))
895                 return expr_op;
896
897         return InvalidOid;
898 }
899
900 /****************************************************************************
901  *                              ----  ROUTINES TO DO PARTIAL INDEX PREDICATE TESTS      ----
902  ****************************************************************************/
903
904 /*
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.
908  */
909 void
910 check_partial_indexes(PlannerInfo *root, RelOptInfo *rel)
911 {
912         List       *restrictinfo_list = rel->baserestrictinfo;
913         ListCell   *ilist;
914
915         /*
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
923          *
924          * XXX as of 7.1, equivalence class info *is* available.  Consider
925          * improving this code as foreseen by Nels.
926          */
927
928         foreach(ilist, rel->indexlist)
929         {
930                 IndexOptInfo *index = (IndexOptInfo *) lfirst(ilist);
931
932                 if (index->indpred == NIL)
933                         continue;                       /* ignore non-partial indexes */
934
935                 index->predOK = predicate_implied_by(index->indpred,
936                                                                                          restrictinfo_list);
937         }
938 }
939
940 /****************************************************************************
941  *                              ----  ROUTINES TO CHECK JOIN CLAUSES  ----
942  ****************************************************************************/
943
944 /*
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.
948  */
949 static Relids
950 indexable_outerrelids(RelOptInfo *rel)
951 {
952         Relids          outer_relids = NULL;
953         ListCell   *l;
954
955         /*
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.
958          */
959         foreach(l, rel->joininfo)
960         {
961                 RestrictInfo *joininfo = (RestrictInfo *) lfirst(l);
962                 Relids  other_rels;
963
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);
967                 else
968                         bms_free(other_rels);
969         }
970
971         return outer_relids;
972 }
973
974 /*
975  * matches_any_index
976  *        Workhorse for indexable_outerrelids: see if a joinclause can be
977  *        matched to any index of the given rel.
978  */
979 static bool
980 matches_any_index(RestrictInfo *rinfo, RelOptInfo *rel, Relids outer_relids)
981 {
982         ListCell   *l;
983
984         Assert(IsA(rinfo, RestrictInfo));
985
986         if (restriction_is_or_clause(rinfo))
987         {
988                 foreach(l, ((BoolExpr *) rinfo->orclause)->args)
989                 {
990                         Node   *orarg = (Node *) lfirst(l);
991
992                         /* OR arguments should be ANDs or sub-RestrictInfos */
993                         if (and_clause(orarg))
994                         {
995                                 ListCell   *j;
996
997                                 /* Recurse to examine AND items and sub-ORs */
998                                 foreach(j, ((BoolExpr *) orarg)->args)
999                                 {
1000                                         RestrictInfo *arinfo = (RestrictInfo *) lfirst(j);
1001
1002                                         if (matches_any_index(arinfo, rel, outer_relids))
1003                                                 return true;
1004                                 }
1005                         }
1006                         else
1007                         {
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,
1012                                                                           outer_relids))
1013                                         return true;
1014                         }
1015                 }
1016
1017                 return false;
1018         }
1019
1020         /* Normal case for a simple restriction clause */
1021         foreach(l, rel->indexlist)
1022         {
1023                 IndexOptInfo *index = (IndexOptInfo *) lfirst(l);
1024                 int                     indexcol = 0;
1025                 Oid                *classes = index->classlist;
1026
1027                 do
1028                 {
1029                         Oid                     curClass = classes[0];
1030
1031                         if (match_clause_to_indexcol(index,
1032                                                                                  indexcol,
1033                                                                                  curClass,
1034                                                                                  rinfo,
1035                                                                                  outer_relids))
1036                                 return true;
1037
1038                         indexcol++;
1039                         classes++;
1040                 } while (!DoneMatchingIndexKeys(classes));
1041         }
1042
1043         return false;
1044 }
1045
1046 /*
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.
1051  *
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.
1058  */
1059 Path *
1060 best_inner_indexscan(PlannerInfo *root, RelOptInfo *rel,
1061                                          Relids outer_relids, JoinType jointype)
1062 {
1063         Path       *cheapest;
1064         bool            isouterjoin;
1065         List       *clause_list;
1066         List       *indexpaths;
1067         List       *bitindexpaths;
1068         ListCell   *l;
1069         InnerIndexscanInfo *info;
1070         MemoryContext oldcontext;
1071
1072         /*
1073          * Nestloop only supports inner, left, and IN joins.
1074          */
1075         switch (jointype)
1076         {
1077                 case JOIN_INNER:
1078                 case JOIN_IN:
1079                 case JOIN_UNIQUE_OUTER:
1080                         isouterjoin = false;
1081                         break;
1082                 case JOIN_LEFT:
1083                         isouterjoin = true;
1084                         break;
1085                 default:
1086                         return NULL;
1087         }
1088
1089         /*
1090          * If there are no indexable joinclauses for this rel, exit quickly.
1091          */
1092         if (bms_is_empty(rel->index_outer_relids))
1093                 return NULL;
1094
1095         /*
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.)
1100          */
1101         oldcontext = MemoryContextSwitchTo(GetMemoryChunkContext(rel));
1102
1103         /*
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.
1107          */
1108         outer_relids = bms_intersect(rel->index_outer_relids, outer_relids);
1109         if (bms_is_empty(outer_relids))
1110         {
1111                 bms_free(outer_relids);
1112                 MemoryContextSwitchTo(oldcontext);
1113                 return NULL;
1114         }
1115
1116         /*
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
1121          * innerrel.)
1122          */
1123         foreach(l, rel->index_inner_paths)
1124         {
1125                 info = (InnerIndexscanInfo *) lfirst(l);
1126                 if (bms_equal(info->other_relids, outer_relids) &&
1127                         info->isouterjoin == isouterjoin)
1128                 {
1129                         bms_free(outer_relids);
1130                         MemoryContextSwitchTo(oldcontext);
1131                         return info->best_innerpath;
1132                 }
1133         }
1134
1135         /*
1136          * Find all the relevant restriction and join clauses.
1137          */
1138         clause_list = find_clauses_for_join(root, rel, outer_relids, isouterjoin);
1139
1140         /*
1141          * Find all the index paths that are usable for this join, except for
1142          * stuff involving OR clauses.
1143          */
1144         indexpaths = find_usable_indexes(root, rel,
1145                                                                          clause_list, NIL,
1146                                                                          false, true,
1147                                                                          outer_relids);
1148
1149         /*
1150          * Generate BitmapOrPaths for any suitable OR-clauses present in the
1151          * clause list.
1152          */
1153         bitindexpaths = generate_bitmap_or_paths(root, rel,
1154                                                                                          clause_list, NIL,
1155                                                                                          true,
1156                                                                                          outer_relids);
1157
1158         /*
1159          * Include the regular index paths in bitindexpaths.
1160          */
1161         bitindexpaths = list_concat(bitindexpaths, list_copy(indexpaths));
1162
1163         /*
1164          * If we found anything usable, generate a BitmapHeapPath for the
1165          * most promising combination of bitmap index paths.
1166          */
1167         if (bitindexpaths != NIL)
1168         {
1169                 Path       *bitmapqual;
1170                 BitmapHeapPath *bpath;
1171
1172                 bitmapqual = choose_bitmap_and(root, rel, bitindexpaths);
1173                 bpath = create_bitmap_heap_path(root, rel, bitmapqual, true);
1174                 indexpaths = lappend(indexpaths, bpath);
1175         }
1176
1177         /*
1178          * Now choose the cheapest member of indexpaths.
1179          */
1180         cheapest = NULL;
1181         foreach(l, indexpaths)
1182         {
1183                 Path       *path = (Path *) lfirst(l);
1184
1185                 if (cheapest == NULL ||
1186                         compare_path_costs(path, cheapest, TOTAL_COST) < 0)
1187                         cheapest = path;
1188         }
1189
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);
1196
1197         MemoryContextSwitchTo(oldcontext);
1198
1199         return cheapest;
1200 }
1201
1202 /*
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.
1206  *
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.
1211  */
1212 static List *
1213 find_clauses_for_join(PlannerInfo *root, RelOptInfo *rel,
1214                                           Relids outer_relids, bool isouterjoin)
1215 {
1216         List       *clause_list = NIL;
1217         bool            jfound = false;
1218         Relids          join_relids;
1219         ListCell   *l;
1220
1221         /*
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.
1228          */
1229         foreach(l, rel->baserestrictinfo)
1230         {
1231                 RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
1232
1233                 /* Can't use pushed-down clauses in outer join */
1234                 if (isouterjoin && rinfo->is_pushed_down)
1235                         continue;
1236                 clause_list = lappend(clause_list, rinfo);
1237         }
1238
1239         /* Look for joinclauses that are usable with given outer_relids */
1240         join_relids = bms_union(rel->relids, outer_relids);
1241
1242         foreach(l, rel->joininfo)
1243         {
1244                 RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
1245
1246                 /* Can't use pushed-down clauses in outer join */
1247                 if (isouterjoin && rinfo->is_pushed_down)
1248                         continue;
1249                 if (!bms_is_subset(rinfo->required_relids, join_relids))
1250                         continue;
1251
1252                 clause_list = lappend(clause_list, rinfo);
1253                 jfound = true;
1254         }
1255
1256         bms_free(join_relids);
1257
1258         /* if no join clause was matched then forget it, per comments above */
1259         if (!jfound)
1260                 return NIL;
1261
1262         /*
1263          * We may now have clauses that are known redundant.  Get rid of 'em.
1264          */
1265         if (list_length(clause_list) > 1)
1266         {
1267                 clause_list = remove_redundant_join_clauses(root,
1268                                                                                                         clause_list,
1269                                                                                                         isouterjoin);
1270         }
1271
1272         return clause_list;
1273 }
1274
1275 /****************************************************************************
1276  *                              ----  ROUTINES TO HANDLE PATHKEYS  ----
1277  ****************************************************************************/
1278
1279 /*
1280  * match_variant_ordering
1281  *              Try to match an index's ordering to the query's requested ordering
1282  *
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;
1290  *
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.
1301  *
1302  * Inputs:
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)
1306  *
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.
1310  */
1311 static bool
1312 match_variant_ordering(PlannerInfo *root,
1313                                            IndexOptInfo *index,
1314                                            List *restrictclauses,
1315                                            ScanDirection *indexscandir)
1316 {
1317         List       *ignorables;
1318
1319         /*
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.)
1325          *
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
1328          * won't cope.
1329          */
1330         if (index->relam != BTREE_AM_OID)
1331                 return false;
1332         /*
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.
1336          */
1337         ignorables = identify_ignorable_ordering_cols(root, index,
1338                                                                                                   restrictclauses);
1339         /*
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.
1343          */
1344         if (ignorables &&
1345                 match_index_to_query_keys(root, index, ForwardScanDirection,
1346                                                                   ignorables))
1347         {
1348                 *indexscandir = ForwardScanDirection;
1349                 return true;
1350         }
1351         if (match_index_to_query_keys(root, index, BackwardScanDirection,
1352                                                                   ignorables))
1353         {
1354                 *indexscandir = BackwardScanDirection;
1355                 return true;
1356         }
1357         return false;
1358 }
1359
1360 /*
1361  * identify_ignorable_ordering_cols
1362  *              Determine which index columns can be ignored for ordering purposes
1363  *
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.
1367  */
1368 static List *
1369 identify_ignorable_ordering_cols(PlannerInfo *root,
1370                                                                  IndexOptInfo *index,
1371                                                                  List *restrictclauses)
1372 {
1373         List       *result = NIL;
1374         int                     indexcol = 0;                   /* note this is 0-based */
1375         ListCell   *l;
1376
1377         /* restrictclauses is either NIL or has a sublist per column */
1378         foreach(l, restrictclauses)
1379         {
1380                 List   *sublist = (List *) lfirst(l);
1381                 Oid             opclass = index->classlist[indexcol];
1382                 ListCell *l2;
1383
1384                 foreach(l2, sublist)
1385                 {
1386                         RestrictInfo *rinfo = (RestrictInfo *) lfirst(l2);
1387                         OpExpr     *clause = (OpExpr *) rinfo->clause;
1388                         Oid             clause_op;
1389                         int             op_strategy;
1390                         bool    varonleft;
1391                         bool    ispc;
1392
1393                         /* We know this clause passed match_clause_to_indexcol */
1394
1395                         /* First check for boolean-index cases. */
1396                         if (IsBooleanOpclass(opclass))
1397                         {
1398                                 if (match_boolean_index_clause((Node *) clause, indexcol,
1399                                                                                            index))
1400                                 {
1401                                         /*
1402                                          * The clause means either col = TRUE or col = FALSE;
1403                                          * we do not care which, it's an equality constraint
1404                                          * either way.
1405                                          */
1406                                         result = lappend_int(result, indexcol+1);
1407                                         break;
1408                                 }
1409                         }
1410
1411                         /* Else clause must be a binary opclause. */
1412                         Assert(IsA(clause, OpExpr));
1413
1414                         /* Determine left/right sides and check the operator */
1415                         clause_op = clause->opno;
1416                         if (match_index_to_operand(linitial(clause->args), indexcol,
1417                                                                            index))
1418                         {
1419                                 /* clause_op is correct */
1420                                 varonleft = true;
1421                         }
1422                         else
1423                         {
1424                                 Assert(match_index_to_operand(lsecond(clause->args), indexcol,
1425                                                                                           index));
1426                                 /* Must flip operator to get the opclass member */
1427                                 clause_op = get_commutator(clause_op);
1428                                 varonleft = false;
1429                         }
1430                         if (!OidIsValid(clause_op))
1431                                 continue;               /* ignore non match, per next comment */
1432                         op_strategy = get_op_opclass_strategy(clause_op, opclass);
1433
1434                         /*
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
1440                          * assume no match.
1441                          */
1442                         if (op_strategy != BTEqualStrategyNumber)
1443                                 continue;
1444
1445                         /* Now check that other side is pseudoconstant */
1446                         if (varonleft)
1447                                 ispc = is_pseudo_constant_clause_relids(lsecond(clause->args),
1448                                                                                                                 rinfo->right_relids);
1449                         else
1450                                 ispc = is_pseudo_constant_clause_relids(linitial(clause->args),
1451                                                                                                                 rinfo->left_relids);
1452                         if (ispc)
1453                         {
1454                                 result = lappend_int(result, indexcol+1);
1455                                 break;
1456                         }
1457                 }
1458                 indexcol++;
1459         }
1460         return result;
1461 }
1462
1463 /*
1464  * match_index_to_query_keys
1465  *              Check a single scan direction for "intelligent" match to query keys
1466  *
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
1470  *
1471  * Returns TRUE on successful match (ie, the query_pathkeys can be considered
1472  * to match this index).
1473  */
1474 static bool
1475 match_index_to_query_keys(PlannerInfo *root,
1476                                                   IndexOptInfo *index,
1477                                                   ScanDirection indexscandir,
1478                                                   List *ignorables)
1479 {
1480         List       *index_pathkeys;
1481         ListCell   *index_cell;
1482         int                     index_col;
1483         ListCell   *r;
1484
1485         /* Get the pathkeys that exactly describe the index */
1486         index_pathkeys = build_index_pathkeys(root, index, indexscandir);
1487
1488         /*
1489          * Can we match to the query's requested pathkeys?  The inner loop
1490          * skips over ignorable index columns while trying to match.
1491          */
1492         index_cell = list_head(index_pathkeys);
1493         index_col = 0;
1494
1495         foreach(r, root->query_pathkeys)
1496         {
1497                 List       *rsubkey = (List *) lfirst(r);
1498
1499                 for (;;)
1500                 {
1501                         List   *isubkey;
1502
1503                         if (index_cell == NULL)
1504                                 return false;
1505                         isubkey = (List *) lfirst(index_cell);
1506                         index_cell = lnext(index_cell);
1507                         index_col++;            /* index_col is now 1-based */
1508                         /*
1509                          * Since we are dealing with canonicalized pathkeys, pointer
1510                          * comparison is sufficient to determine a match.
1511                          */
1512                         if (rsubkey == isubkey)
1513                                 break;                  /* matched current query pathkey */
1514
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 */
1518                 }
1519         }
1520
1521         return true;
1522 }
1523
1524 /****************************************************************************
1525  *                              ----  PATH CREATION UTILITIES  ----
1526  ****************************************************************************/
1527
1528 /*
1529  * flatten_clausegroups_list
1530  *        Given a list of lists of RestrictInfos, flatten it to a list
1531  *        of RestrictInfos.
1532  *
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.
1536  */
1537 List *
1538 flatten_clausegroups_list(List *clausegroups)
1539 {
1540         List       *allclauses = NIL;
1541         ListCell   *l;
1542
1543         foreach(l, clausegroups)
1544                 allclauses = list_concat(allclauses, list_copy((List *) lfirst(l)));
1545         return allclauses;
1546 }
1547
1548
1549 /****************************************************************************
1550  *                              ----  ROUTINES TO CHECK OPERANDS  ----
1551  ****************************************************************************/
1552
1553 /*
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.
1557  *
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
1561  */
1562 bool
1563 match_index_to_operand(Node *operand,
1564                                            int indexcol,
1565                                            IndexOptInfo *index)
1566 {
1567         int                     indkey;
1568
1569         /*
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.
1574          */
1575         if (operand && IsA(operand, RelabelType))
1576                 operand = (Node *) ((RelabelType *) operand)->arg;
1577
1578         indkey = index->indexkeys[indexcol];
1579         if (indkey != 0)
1580         {
1581                 /*
1582                  * Simple index column; operand must be a matching Var.
1583                  */
1584                 if (operand && IsA(operand, Var) &&
1585                         index->rel->relid == ((Var *) operand)->varno &&
1586                         indkey == ((Var *) operand)->varattno)
1587                         return true;
1588         }
1589         else
1590         {
1591                 /*
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.)
1595                  */
1596                 ListCell   *indexpr_item;
1597                 int                     i;
1598                 Node       *indexkey;
1599
1600                 indexpr_item = list_head(index->indexprs);
1601                 for (i = 0; i < indexcol; i++)
1602                 {
1603                         if (index->indexkeys[i] == 0)
1604                         {
1605                                 if (indexpr_item == NULL)
1606                                         elog(ERROR, "wrong number of index expressions");
1607                                 indexpr_item = lnext(indexpr_item);
1608                         }
1609                 }
1610                 if (indexpr_item == NULL)
1611                         elog(ERROR, "wrong number of index expressions");
1612                 indexkey = (Node *) lfirst(indexpr_item);
1613
1614                 /*
1615                  * Does it match the operand?  Again, strip any relabeling.
1616                  */
1617                 if (indexkey && IsA(indexkey, RelabelType))
1618                         indexkey = (Node *) ((RelabelType *) indexkey)->arg;
1619
1620                 if (equal(indexkey, operand))
1621                         return true;
1622         }
1623
1624         return false;
1625 }
1626
1627 /****************************************************************************
1628  *                      ----  ROUTINES FOR "SPECIAL" INDEXABLE OPERATORS  ----
1629  ****************************************************************************/
1630
1631 /*----------
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.)
1642  *
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.)
1651  *
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.)
1660  *
1661  * Three routines are provided here:
1662  *
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.
1668  *
1669  * match_boolean_index_clause() similarly detects clauses that can be
1670  * converted into boolean equality operators.
1671  *
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.
1679  *----------
1680  */
1681
1682 /*
1683  * match_boolean_index_clause
1684  *        Recognize restriction clauses that can be matched to a boolean index.
1685  *
1686  * This should be called only when IsBooleanOpclass() recognizes the
1687  * index's operator class.  We check to see if the clause matches the
1688  * index's key.
1689  */
1690 static bool
1691 match_boolean_index_clause(Node *clause,
1692                                                    int indexcol,
1693                                                    IndexOptInfo *index)
1694 {
1695         /* Direct match? */
1696         if (match_index_to_operand(clause, indexcol, index))
1697                 return true;
1698         /* NOT clause? */
1699         if (not_clause(clause))
1700         {
1701                 if (match_index_to_operand((Node *) get_notclausearg((Expr *) clause),
1702                                                                    indexcol, index))
1703                         return true;
1704         }
1705         /*
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.
1709          */
1710         else if (clause && IsA(clause, BooleanTest))
1711         {
1712                 BooleanTest        *btest = (BooleanTest *) clause;
1713
1714                 if (btest->booltesttype == IS_TRUE ||
1715                         btest->booltesttype == IS_FALSE)
1716                         if (match_index_to_operand((Node *) btest->arg,
1717                                                                            indexcol, index))
1718                                 return true;
1719         }
1720         return false;
1721 }
1722
1723 /*
1724  * match_special_index_operator
1725  *        Recognize restriction clauses that can be used to generate
1726  *        additional indexscanable qualifications.
1727  *
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.
1732  */
1733 static bool
1734 match_special_index_operator(Expr *clause, Oid opclass,
1735                                                          bool indexkey_on_left)
1736 {
1737         bool            isIndexable = false;
1738         Node       *rightop;
1739         Oid                     expr_op;
1740         Const      *patt;
1741         Const      *prefix = NULL;
1742         Const      *rest = NULL;
1743
1744         /*
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...
1748          */
1749         if (!indexkey_on_left)
1750                 return false;
1751
1752         /* we know these will succeed */
1753         rightop = get_rightop(clause);
1754         expr_op = ((OpExpr *) clause)->opno;
1755
1756         /* again, required for all current special ops: */
1757         if (!IsA(rightop, Const) ||
1758                 ((Const *) rightop)->constisnull)
1759                 return false;
1760         patt = (Const *) rightop;
1761
1762         switch (expr_op)
1763         {
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;
1770                         break;
1771
1772                 case OID_BYTEA_LIKE_OP:
1773                         isIndexable = pattern_fixed_prefix(patt, Pattern_Type_Like,
1774                                                                   &prefix, &rest) != Pattern_Prefix_None;
1775                         break;
1776
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;
1783                         break;
1784
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;
1791                         break;
1792
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;
1799                         break;
1800
1801                 case OID_INET_SUB_OP:
1802                 case OID_INET_SUBEQ_OP:
1803                 case OID_CIDR_SUB_OP:
1804                 case OID_CIDR_SUBEQ_OP:
1805                         isIndexable = true;
1806                         break;
1807         }
1808
1809         if (prefix)
1810         {
1811                 pfree(DatumGetPointer(prefix->constvalue));
1812                 pfree(prefix);
1813         }
1814
1815         /* done if the expression doesn't look indexable */
1816         if (!isIndexable)
1817                 return false;
1818
1819         /*
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.
1823          *
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.
1827          */
1828         switch (expr_op)
1829         {
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 */
1835                         isIndexable =
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());
1840                         break;
1841
1842                 case OID_BPCHAR_LIKE_OP:
1843                 case OID_BPCHAR_ICLIKE_OP:
1844                 case OID_BPCHAR_REGEXEQ_OP:
1845                 case OID_BPCHAR_ICREGEXEQ_OP:
1846                         isIndexable =
1847                                 (opclass == BPCHAR_PATTERN_BTREE_OPS_OID) ||
1848                                 (opclass == BPCHAR_BTREE_OPS_OID && lc_collate_is_c());
1849                         break;
1850
1851                 case OID_NAME_LIKE_OP:
1852                 case OID_NAME_ICLIKE_OP:
1853                 case OID_NAME_REGEXEQ_OP:
1854                 case OID_NAME_ICREGEXEQ_OP:
1855                         isIndexable =
1856                                 (opclass == NAME_PATTERN_BTREE_OPS_OID) ||
1857                                 (opclass == NAME_BTREE_OPS_OID && lc_collate_is_c());
1858                         break;
1859
1860                 case OID_BYTEA_LIKE_OP:
1861                         isIndexable = (opclass == BYTEA_BTREE_OPS_OID);
1862                         break;
1863
1864                 case OID_INET_SUB_OP:
1865                 case OID_INET_SUBEQ_OP:
1866                         isIndexable = (opclass == INET_BTREE_OPS_OID);
1867                         break;
1868
1869                 case OID_CIDR_SUB_OP:
1870                 case OID_CIDR_SUBEQ_OP:
1871                         isIndexable = (opclass == CIDR_BTREE_OPS_OID);
1872                         break;
1873         }
1874
1875         return isIndexable;
1876 }
1877
1878 /*
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.
1885  *
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
1889  * functions.)
1890  */
1891 List *
1892 expand_indexqual_conditions(IndexOptInfo *index, List *clausegroups)
1893 {
1894         List       *resultquals = NIL;
1895         ListCell   *clausegroup_item;
1896         int                     indexcol = 0;
1897         Oid                *classes = index->classlist;
1898
1899         if (clausegroups == NIL)
1900                 return NIL;
1901
1902         clausegroup_item = list_head(clausegroups);
1903         do
1904         {
1905                 Oid                     curClass = classes[0];
1906                 ListCell   *l;
1907
1908                 foreach(l, (List *) lfirst(clausegroup_item))
1909                 {
1910                         RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
1911
1912                         /* First check for boolean cases */
1913                         if (IsBooleanOpclass(curClass))
1914                         {
1915                                 Expr   *boolqual;
1916
1917                                 boolqual = expand_boolean_index_clause((Node *) rinfo->clause,
1918                                                                                                            indexcol,
1919                                                                                                            index);
1920                                 if (boolqual)
1921                                 {
1922                                         resultquals = lappend(resultquals,
1923                                                                                   make_restrictinfo(boolqual,
1924                                                                                                                         true,
1925                                                                                                                         NULL));
1926                                         continue;
1927                                 }
1928                         }
1929
1930                         resultquals = list_concat(resultquals,
1931                                                                           expand_indexqual_condition(rinfo,
1932                                                                                                                                  curClass));
1933                 }
1934
1935                 clausegroup_item = lnext(clausegroup_item);
1936
1937                 indexcol++;
1938                 classes++;
1939         } while (clausegroup_item != NULL && !DoneMatchingIndexKeys(classes));
1940
1941         Assert(clausegroup_item == NULL);       /* else more groups than indexkeys */
1942
1943         return resultquals;
1944 }
1945
1946 /*
1947  * expand_boolean_index_clause
1948  *        Convert a clause recognized by match_boolean_index_clause into
1949  *        a boolean equality operator clause.
1950  *
1951  * Returns NULL if the clause isn't a boolean index qual.
1952  */
1953 static Expr *
1954 expand_boolean_index_clause(Node *clause,
1955                                                         int indexcol,
1956                                                         IndexOptInfo *index)
1957 {
1958         /* Direct match? */
1959         if (match_index_to_operand(clause, indexcol, index))
1960         {
1961                 /* convert to indexkey = TRUE */
1962                 return make_opclause(BooleanEqualOperator, BOOLOID, false,
1963                                                          (Expr *) clause,
1964                                                          (Expr *) makeBoolConst(true, false));
1965         }
1966         /* NOT clause? */
1967         if (not_clause(clause))
1968         {
1969                 Node   *arg = (Node *) get_notclausearg((Expr *) clause);
1970
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,
1975                                                          (Expr *) arg,
1976                                                          (Expr *) makeBoolConst(false, false));
1977         }
1978         if (clause && IsA(clause, BooleanTest))
1979         {
1980                 BooleanTest        *btest = (BooleanTest *) clause;
1981                 Node   *arg = (Node *) btest->arg;
1982
1983                 /* It must have matched the indexkey */
1984                 Assert(match_index_to_operand(arg, indexcol, index));
1985                 if (btest->booltesttype == IS_TRUE)
1986                 {
1987                         /* convert to indexkey = TRUE */
1988                         return make_opclause(BooleanEqualOperator, BOOLOID, false,
1989                                                                  (Expr *) arg,
1990                                                                  (Expr *) makeBoolConst(true, false));
1991                 }
1992                 if (btest->booltesttype == IS_FALSE)
1993                 {
1994                         /* convert to indexkey = FALSE */
1995                         return make_opclause(BooleanEqualOperator, BOOLOID, false,
1996                                                                  (Expr *) arg,
1997                                                                  (Expr *) makeBoolConst(false, false));
1998                 }
1999                 /* Oops */
2000                 Assert(false);
2001         }
2002
2003         return NULL;
2004 }
2005
2006 /*
2007  * expand_indexqual_condition --- expand a single indexqual condition
2008  *              (other than a boolean-qual case)
2009  *
2010  * The input is a single RestrictInfo, the output a list of RestrictInfos
2011  */
2012 static List *
2013 expand_indexqual_condition(RestrictInfo *rinfo, Oid opclass)
2014 {
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;
2022         Const      *rest = NULL;
2023         Pattern_Prefix_Status pstatus;
2024         List       *result;
2025
2026         switch (expr_op)
2027         {
2028                         /*
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().
2033                          */
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,
2039                                                                                    &prefix, &rest);
2040                         result = prefix_quals(leftop, opclass, prefix, pstatus);
2041                         break;
2042
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,
2048                                                                                    &prefix, &rest);
2049                         result = prefix_quals(leftop, opclass, prefix, pstatus);
2050                         break;
2051
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,
2057                                                                                    &prefix, &rest);
2058                         result = prefix_quals(leftop, opclass, prefix, pstatus);
2059                         break;
2060
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,
2066                                                                                    &prefix, &rest);
2067                         result = prefix_quals(leftop, opclass, prefix, pstatus);
2068                         break;
2069
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,
2075                                                                                   patt->constvalue);
2076                         break;
2077
2078                 default:
2079                         result = list_make1(rinfo);
2080                         break;
2081         }
2082
2083         return result;
2084 }
2085
2086 /*
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.
2091  */
2092 static List *
2093 prefix_quals(Node *leftop, Oid opclass,
2094                          Const *prefix_const, Pattern_Prefix_Status pstatus)
2095 {
2096         List       *result;
2097         Oid                     datatype;
2098         Oid                     oproid;
2099         Expr       *expr;
2100         Const      *greaterstr;
2101
2102         Assert(pstatus != Pattern_Prefix_None);
2103
2104         switch (opclass)
2105         {
2106                 case TEXT_BTREE_OPS_OID:
2107                 case TEXT_PATTERN_BTREE_OPS_OID:
2108                         datatype = TEXTOID;
2109                         break;
2110
2111                 case VARCHAR_BTREE_OPS_OID:
2112                 case VARCHAR_PATTERN_BTREE_OPS_OID:
2113                         datatype = VARCHAROID;
2114                         break;
2115
2116                 case BPCHAR_BTREE_OPS_OID:
2117                 case BPCHAR_PATTERN_BTREE_OPS_OID:
2118                         datatype = BPCHAROID;
2119                         break;
2120
2121                 case NAME_BTREE_OPS_OID:
2122                 case NAME_PATTERN_BTREE_OPS_OID:
2123                         datatype = NAMEOID;
2124                         break;
2125
2126                 case BYTEA_BTREE_OPS_OID:
2127                         datatype = BYTEAOID;
2128                         break;
2129
2130                 default:
2131                         /* shouldn't get here */
2132                         elog(ERROR, "unexpected opclass: %u", opclass);
2133                         return NIL;
2134         }
2135
2136         /*
2137          * If necessary, coerce the prefix constant to the right type. The
2138          * given prefix constant is either text or bytea type.
2139          */
2140         if (prefix_const->consttype != datatype)
2141         {
2142                 char       *prefix;
2143
2144                 switch (prefix_const->consttype)
2145                 {
2146                         case TEXTOID:
2147                                 prefix = DatumGetCString(DirectFunctionCall1(textout,
2148                                                                                           prefix_const->constvalue));
2149                                 break;
2150                         case BYTEAOID:
2151                                 prefix = DatumGetCString(DirectFunctionCall1(byteaout,
2152                                                                                           prefix_const->constvalue));
2153                                 break;
2154                         default:
2155                                 elog(ERROR, "unexpected const type: %u",
2156                                          prefix_const->consttype);
2157                                 return NIL;
2158                 }
2159                 prefix_const = string_to_const(prefix, datatype);
2160                 pfree(prefix);
2161         }
2162
2163         /*
2164          * If we found an exact-match pattern, generate an "=" indexqual.
2165          */
2166         if (pstatus == Pattern_Prefix_Exact)
2167         {
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));
2175                 return result;
2176         }
2177
2178         /*
2179          * Otherwise, we have a nonempty required prefix of the values.
2180          *
2181          * We can always say "x >= prefix".
2182          */
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));
2190
2191         /*-------
2192          * If we can create a string larger than the prefix, we can say
2193          * "x < greaterstr".
2194          *-------
2195          */
2196         greaterstr = make_greater_string(prefix_const);
2197         if (greaterstr)
2198         {
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));
2206         }
2207
2208         return result;
2209 }
2210
2211 /*
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.
2215  */
2216 static List *
2217 network_prefix_quals(Node *leftop, Oid expr_op, Oid opclass, Datum rightop)
2218 {
2219         bool            is_eq;
2220         Oid                     datatype;
2221         Oid                     opr1oid;
2222         Oid                     opr2oid;
2223         Datum           opr1right;
2224         Datum           opr2right;
2225         List       *result;
2226         Expr       *expr;
2227
2228         switch (expr_op)
2229         {
2230                 case OID_INET_SUB_OP:
2231                         datatype = INETOID;
2232                         is_eq = false;
2233                         break;
2234                 case OID_INET_SUBEQ_OP:
2235                         datatype = INETOID;
2236                         is_eq = true;
2237                         break;
2238                 case OID_CIDR_SUB_OP:
2239                         datatype = CIDROID;
2240                         is_eq = false;
2241                         break;
2242                 case OID_CIDR_SUBEQ_OP:
2243                         datatype = CIDROID;
2244                         is_eq = true;
2245                         break;
2246                 default:
2247                         elog(ERROR, "unexpected operator: %u", expr_op);
2248                         return NIL;
2249         }
2250
2251         /*
2252          * create clause "key >= network_scan_first( rightop )", or ">" if the
2253          * operator disallows equality.
2254          */
2255         if (is_eq)
2256         {
2257                 opr1oid = get_opclass_member(opclass, InvalidOid,
2258                                                                          BTGreaterEqualStrategyNumber);
2259                 if (opr1oid == InvalidOid)
2260                         elog(ERROR, "no >= operator for opclass %u", opclass);
2261         }
2262         else
2263         {
2264                 opr1oid = get_opclass_member(opclass, InvalidOid,
2265                                                                          BTGreaterStrategyNumber);
2266                 if (opr1oid == InvalidOid)
2267                         elog(ERROR, "no > operator for opclass %u", opclass);
2268         }
2269
2270         opr1right = network_scan_first(rightop);
2271
2272         expr = make_opclause(opr1oid, BOOLOID, false,
2273                                                  (Expr *) leftop,
2274                                                  (Expr *) makeConst(datatype, -1, opr1right,
2275                                                                                         false, false));
2276         result = list_make1(make_restrictinfo(expr, true, NULL));
2277
2278         /* create clause "key <= network_scan_last( rightop )" */
2279
2280         opr2oid = get_opclass_member(opclass, InvalidOid,
2281                                                                  BTLessEqualStrategyNumber);
2282         if (opr2oid == InvalidOid)
2283                 elog(ERROR, "no <= operator for opclass %u", opclass);
2284
2285         opr2right = network_scan_last(rightop);
2286
2287         expr = make_opclause(opr2oid, BOOLOID, false,
2288                                                  (Expr *) leftop,
2289                                                  (Expr *) makeConst(datatype, -1, opr2right,
2290                                                                                         false, false));
2291         result = lappend(result, make_restrictinfo(expr, true, NULL));
2292
2293         return result;
2294 }
2295
2296 /*
2297  * Handy subroutines for match_special_index_operator() and friends.
2298  */
2299
2300 /*
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.
2304  */
2305 static Datum
2306 string_to_datum(const char *str, Oid datatype)
2307 {
2308         /*
2309          * We cheat a little by assuming that textin() will do for bpchar and
2310          * varchar constants too...
2311          */
2312         if (datatype == NAMEOID)
2313                 return DirectFunctionCall1(namein, CStringGetDatum(str));
2314         else if (datatype == BYTEAOID)
2315                 return DirectFunctionCall1(byteain, CStringGetDatum(str));
2316         else
2317                 return DirectFunctionCall1(textin, CStringGetDatum(str));
2318 }
2319
2320 /*
2321  * Generate a Const node of the appropriate type from a C string.
2322  */
2323 static Const *
2324 string_to_const(const char *str, Oid datatype)
2325 {
2326         Datum           conval = string_to_datum(str, datatype);
2327
2328         return makeConst(datatype, ((datatype == NAMEOID) ? NAMEDATALEN : -1),
2329                                          conval, false, false);
2330 }