]> granicus.if.org Git - postgresql/blob - src/backend/optimizer/path/indxpath.c
3fd52d48500cf94de5fac48181b57f4fe331f8aa
[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-2007, 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.215 2007/01/09 02:14:12 tgl Exp $
13  *
14  *-------------------------------------------------------------------------
15  */
16 #include "postgres.h"
17
18 #include <math.h>
19
20 #include "access/skey.h"
21 #include "catalog/pg_am.h"
22 #include "catalog/pg_operator.h"
23 #include "catalog/pg_opfamily.h"
24 #include "catalog/pg_type.h"
25 #include "nodes/makefuncs.h"
26 #include "optimizer/clauses.h"
27 #include "optimizer/cost.h"
28 #include "optimizer/pathnode.h"
29 #include "optimizer/paths.h"
30 #include "optimizer/predtest.h"
31 #include "optimizer/restrictinfo.h"
32 #include "optimizer/var.h"
33 #include "utils/builtins.h"
34 #include "utils/lsyscache.h"
35 #include "utils/memutils.h"
36 #include "utils/pg_locale.h"
37 #include "utils/selfuncs.h"
38
39
40 /*
41  * DoneMatchingIndexKeys() - MACRO
42  */
43 #define DoneMatchingIndexKeys(families) (families[0] == InvalidOid)
44
45 #define IsBooleanOpfamily(opfamily) \
46         ((opfamily) == BOOL_BTREE_FAM_OID || (opfamily) == BOOL_HASH_FAM_OID)
47
48
49 static List *find_usable_indexes(PlannerInfo *root, RelOptInfo *rel,
50                                         List *clauses, List *outer_clauses,
51                                         bool istoplevel, RelOptInfo *outer_rel,
52                                         SaOpControl saop_control);
53 static List *find_saop_paths(PlannerInfo *root, RelOptInfo *rel,
54                                 List *clauses, List *outer_clauses,
55                                 bool istoplevel, RelOptInfo *outer_rel);
56 static Path *choose_bitmap_and(PlannerInfo *root, RelOptInfo *rel,
57                                   List *paths, RelOptInfo *outer_rel);
58 static int      bitmap_path_comparator(const void *a, const void *b);
59 static Cost bitmap_and_cost_est(PlannerInfo *root, RelOptInfo *rel,
60                                         List *paths, RelOptInfo *outer_rel);
61 static List *pull_indexpath_quals(Path *bitmapqual);
62 static bool lists_intersect_ptr(List *list1, List *list2);
63 static bool match_clause_to_indexcol(IndexOptInfo *index,
64                                                  int indexcol, Oid opfamily,
65                                                  RestrictInfo *rinfo,
66                                                  Relids outer_relids,
67                                                  SaOpControl saop_control);
68 static bool is_indexable_operator(Oid expr_op, Oid opfamily,
69                                           bool indexkey_on_left);
70 static bool match_rowcompare_to_indexcol(IndexOptInfo *index,
71                                                          int indexcol,
72                                                          Oid opfamily,
73                                                          RowCompareExpr *clause,
74                                                          Relids outer_relids);
75 static Relids indexable_outerrelids(RelOptInfo *rel);
76 static bool matches_any_index(RestrictInfo *rinfo, RelOptInfo *rel,
77                                   Relids outer_relids);
78 static List *find_clauses_for_join(PlannerInfo *root, RelOptInfo *rel,
79                                           Relids outer_relids, bool isouterjoin);
80 static ScanDirection match_variant_ordering(PlannerInfo *root,
81                                            IndexOptInfo *index,
82                                            List *restrictclauses);
83 static List *identify_ignorable_ordering_cols(PlannerInfo *root,
84                                                                  IndexOptInfo *index,
85                                                                  List *restrictclauses);
86 static bool match_index_to_query_keys(PlannerInfo *root,
87                                                   IndexOptInfo *index,
88                                                   ScanDirection indexscandir,
89                                                   List *ignorables);
90 static bool match_boolean_index_clause(Node *clause, int indexcol,
91                                                    IndexOptInfo *index);
92 static bool match_special_index_operator(Expr *clause, Oid opfamily,
93                                                          bool indexkey_on_left);
94 static Expr *expand_boolean_index_clause(Node *clause, int indexcol,
95                                                         IndexOptInfo *index);
96 static List *expand_indexqual_opclause(RestrictInfo *rinfo, Oid opfamily);
97 static RestrictInfo *expand_indexqual_rowcompare(RestrictInfo *rinfo,
98                                                         IndexOptInfo *index,
99                                                         int indexcol);
100 static List *prefix_quals(Node *leftop, Oid opfamily,
101                          Const *prefix, Pattern_Prefix_Status pstatus);
102 static List *network_prefix_quals(Node *leftop, Oid expr_op, Oid opfamily,
103                                          Datum rightop);
104 static Datum string_to_datum(const char *str, Oid datatype);
105 static Const *string_to_const(const char *str, Oid datatype);
106
107
108 /*
109  * create_index_paths()
110  *        Generate all interesting index paths for the given relation.
111  *        Candidate paths are added to the rel's pathlist (using add_path).
112  *
113  * To be considered for an index scan, an index must match one or more
114  * restriction clauses or join clauses from the query's qual condition,
115  * or match the query's ORDER BY condition, or have a predicate that
116  * matches the query's qual condition.
117  *
118  * There are two basic kinds of index scans.  A "plain" index scan uses
119  * only restriction clauses (possibly none at all) in its indexqual,
120  * so it can be applied in any context.  An "innerjoin" index scan uses
121  * join clauses (plus restriction clauses, if available) in its indexqual.
122  * Therefore it can only be used as the inner relation of a nestloop
123  * join against an outer rel that includes all the other rels mentioned
124  * in its join clauses.  In that context, values for the other rels'
125  * attributes are available and fixed during any one scan of the indexpath.
126  *
127  * An IndexPath is generated and submitted to add_path() for each plain index
128  * scan this routine deems potentially interesting for the current query.
129  *
130  * We also determine the set of other relids that participate in join
131  * clauses that could be used with each index.  The actually best innerjoin
132  * path will be generated for each outer relation later on, but knowing the
133  * set of potential otherrels allows us to identify equivalent outer relations
134  * and avoid repeated computation.
135  *
136  * 'rel' is the relation for which we want to generate index paths
137  *
138  * Note: check_partial_indexes() must have been run previously for this rel.
139  */
140 void
141 create_index_paths(PlannerInfo *root, RelOptInfo *rel)
142 {
143         List       *indexpaths;
144         List       *bitindexpaths;
145         ListCell   *l;
146
147         /* Skip the whole mess if no indexes */
148         if (rel->indexlist == NIL)
149         {
150                 rel->index_outer_relids = NULL;
151                 return;
152         }
153
154         /*
155          * Examine join clauses to see which ones are potentially usable with
156          * indexes of this rel, and generate the set of all other relids that
157          * participate in such join clauses.  We'll use this set later to
158          * recognize outer rels that are equivalent for joining purposes.
159          */
160         rel->index_outer_relids = indexable_outerrelids(rel);
161
162         /*
163          * Find all the index paths that are directly usable for this relation
164          * (ie, are valid without considering OR or JOIN clauses).
165          */
166         indexpaths = find_usable_indexes(root, rel,
167                                                                          rel->baserestrictinfo, NIL,
168                                                                          true, NULL, SAOP_FORBID);
169
170         /*
171          * We can submit them all to add_path.  (This generates access paths for
172          * plain IndexScan plans.)      However, for the next step we will only want
173          * the ones that have some selectivity; we must discard anything that was
174          * generated solely for ordering purposes.
175          */
176         bitindexpaths = NIL;
177         foreach(l, indexpaths)
178         {
179                 IndexPath  *ipath = (IndexPath *) lfirst(l);
180
181                 add_path(rel, (Path *) ipath);
182
183                 if (ipath->indexselectivity < 1.0 &&
184                         !ScanDirectionIsBackward(ipath->indexscandir))
185                         bitindexpaths = lappend(bitindexpaths, ipath);
186         }
187
188         /*
189          * Generate BitmapOrPaths for any suitable OR-clauses present in the
190          * restriction list.  Add these to bitindexpaths.
191          */
192         indexpaths = generate_bitmap_or_paths(root, rel,
193                                                                                   rel->baserestrictinfo, NIL,
194                                                                                   NULL);
195         bitindexpaths = list_concat(bitindexpaths, indexpaths);
196
197         /*
198          * Likewise, generate paths using ScalarArrayOpExpr clauses; these can't
199          * be simple indexscans but they can be used in bitmap scans.
200          */
201         indexpaths = find_saop_paths(root, rel,
202                                                                  rel->baserestrictinfo, NIL,
203                                                                  true, NULL);
204         bitindexpaths = list_concat(bitindexpaths, indexpaths);
205
206         /*
207          * If we found anything usable, generate a BitmapHeapPath for the most
208          * promising combination of bitmap index paths.
209          */
210         if (bitindexpaths != NIL)
211         {
212                 Path       *bitmapqual;
213                 BitmapHeapPath *bpath;
214
215                 bitmapqual = choose_bitmap_and(root, rel, bitindexpaths, NULL);
216                 bpath = create_bitmap_heap_path(root, rel, bitmapqual, NULL);
217                 add_path(rel, (Path *) bpath);
218         }
219 }
220
221
222 /*----------
223  * find_usable_indexes
224  *        Given a list of restriction clauses, find all the potentially usable
225  *        indexes for the given relation, and return a list of IndexPaths.
226  *
227  * The caller actually supplies two lists of restriction clauses: some
228  * "current" ones and some "outer" ones.  Both lists can be used freely
229  * to match keys of the index, but an index must use at least one of the
230  * "current" clauses to be considered usable.  The motivation for this is
231  * examples like
232  *              WHERE (x = 42) AND (... OR (y = 52 AND z = 77) OR ....)
233  * While we are considering the y/z subclause of the OR, we can use "x = 42"
234  * as one of the available index conditions; but we shouldn't match the
235  * subclause to any index on x alone, because such a Path would already have
236  * been generated at the upper level.  So we could use an index on x,y,z
237  * or an index on x,y for the OR subclause, but not an index on just x.
238  * When dealing with a partial index, a match of the index predicate to
239  * one of the "current" clauses also makes the index usable.
240  *
241  * If istoplevel is true (indicating we are considering the top level of a
242  * rel's restriction clauses), we will include indexes in the result that
243  * have an interesting sort order, even if they have no matching restriction
244  * clauses.
245  *
246  * 'rel' is the relation for which we want to generate index paths
247  * 'clauses' is the current list of clauses (RestrictInfo nodes)
248  * 'outer_clauses' is the list of additional upper-level clauses
249  * 'istoplevel' is true if clauses are the rel's top-level restriction list
250  *              (outer_clauses must be NIL when this is true)
251  * 'outer_rel' is the outer side of the join if forming an inner indexscan
252  *              (so some of the given clauses are join clauses); NULL if not
253  * 'saop_control' indicates whether ScalarArrayOpExpr clauses can be used
254  *
255  * Note: check_partial_indexes() must have been run previously.
256  *----------
257  */
258 static List *
259 find_usable_indexes(PlannerInfo *root, RelOptInfo *rel,
260                                         List *clauses, List *outer_clauses,
261                                         bool istoplevel, RelOptInfo *outer_rel,
262                                         SaOpControl saop_control)
263 {
264         Relids          outer_relids = outer_rel ? outer_rel->relids : NULL;
265         List       *result = NIL;
266         List       *all_clauses = NIL;          /* not computed till needed */
267         ListCell   *ilist;
268
269         foreach(ilist, rel->indexlist)
270         {
271                 IndexOptInfo *index = (IndexOptInfo *) lfirst(ilist);
272                 IndexPath  *ipath;
273                 List       *restrictclauses;
274                 List       *index_pathkeys;
275                 List       *useful_pathkeys;
276                 bool            useful_predicate;
277                 bool            found_clause;
278                 bool            index_is_ordered;
279
280                 /*
281                  * Ignore partial indexes that do not match the query.  If a partial
282                  * index is marked predOK then we know it's OK; otherwise, if we are
283                  * at top level we know it's not OK (since predOK is exactly whether
284                  * its predicate could be proven from the toplevel clauses).
285                  * Otherwise, we have to test whether the added clauses are sufficient
286                  * to imply the predicate.      If so, we could use the index in the
287                  * current context.
288                  *
289                  * We set useful_predicate to true iff the predicate was proven using
290                  * the current set of clauses.  This is needed to prevent matching a
291                  * predOK index to an arm of an OR, which would be a legal but
292                  * pointlessly inefficient plan.  (A better plan will be generated by
293                  * just scanning the predOK index alone, no OR.)
294                  */
295                 useful_predicate = false;
296                 if (index->indpred != NIL)
297                 {
298                         if (index->predOK)
299                         {
300                                 if (istoplevel)
301                                 {
302                                         /* we know predicate was proven from these clauses */
303                                         useful_predicate = true;
304                                 }
305                         }
306                         else
307                         {
308                                 if (istoplevel)
309                                         continue;       /* no point in trying to prove it */
310
311                                 /* Form all_clauses if not done already */
312                                 if (all_clauses == NIL)
313                                         all_clauses = list_concat(list_copy(clauses),
314                                                                                           outer_clauses);
315
316                                 if (!predicate_implied_by(index->indpred, all_clauses))
317                                         continue;       /* can't use it at all */
318
319                                 if (!predicate_implied_by(index->indpred, outer_clauses))
320                                         useful_predicate = true;
321                         }
322                 }
323
324                 /*
325                  * 1. Match the index against the available restriction clauses.
326                  * found_clause is set true only if at least one of the current
327                  * clauses was used (and, if saop_control is SAOP_REQUIRE, it has to
328                  * have been a ScalarArrayOpExpr clause).
329                  */
330                 restrictclauses = group_clauses_by_indexkey(index,
331                                                                                                         clauses,
332                                                                                                         outer_clauses,
333                                                                                                         outer_relids,
334                                                                                                         saop_control,
335                                                                                                         &found_clause);
336
337                 /*
338                  * Not all index AMs support scans with no restriction clauses. We
339                  * can't generate a scan over an index with amoptionalkey = false
340                  * unless there's at least one restriction clause.
341                  */
342                 if (restrictclauses == NIL && !index->amoptionalkey)
343                         continue;
344
345                 /*
346                  * 2. Compute pathkeys describing index's ordering, if any, then see
347                  * how many of them are actually useful for this query.  This is not
348                  * relevant unless we are at top level.
349                  */
350                 index_is_ordered = OidIsValid(index->fwdsortop[0]);
351                 if (index_is_ordered && istoplevel && outer_rel == NULL)
352                 {
353                         index_pathkeys = build_index_pathkeys(root, index,
354                                                                                                   ForwardScanDirection,
355                                                                                                   true);
356                         useful_pathkeys = truncate_useless_pathkeys(root, rel,
357                                                                                                                 index_pathkeys);
358                 }
359                 else
360                         useful_pathkeys = NIL;
361
362                 /*
363                  * 3. Generate an indexscan path if there are relevant restriction
364                  * clauses in the current clauses, OR the index ordering is
365                  * potentially useful for later merging or final output ordering, OR
366                  * the index has a predicate that was proven by the current clauses.
367                  */
368                 if (found_clause || useful_pathkeys != NIL || useful_predicate)
369                 {
370                         ipath = create_index_path(root, index,
371                                                                           restrictclauses,
372                                                                           useful_pathkeys,
373                                                                           index_is_ordered ?
374                                                                           ForwardScanDirection :
375                                                                           NoMovementScanDirection,
376                                                                           outer_rel);
377                         result = lappend(result, ipath);
378                 }
379
380                 /*
381                  * 4. If the index is ordered, and there is a requested query ordering
382                  * that we failed to match, consider variant ways of achieving the
383                  * ordering.  Again, this is only interesting at top level.
384                  */
385                 if (index_is_ordered && istoplevel && outer_rel == NULL &&
386                         root->query_pathkeys != NIL &&
387                         pathkeys_useful_for_ordering(root, useful_pathkeys) == 0)
388                 {
389                         ScanDirection scandir;
390
391                         scandir = match_variant_ordering(root, index, restrictclauses);
392                         if (!ScanDirectionIsNoMovement(scandir))
393                         {
394                                 ipath = create_index_path(root, index,
395                                                                                   restrictclauses,
396                                                                                   root->query_pathkeys,
397                                                                                   scandir,
398                                                                                   outer_rel);
399                                 result = lappend(result, ipath);
400                         }
401                 }
402         }
403
404         return result;
405 }
406
407
408 /*
409  * find_saop_paths
410  *              Find all the potential indexpaths that make use of ScalarArrayOpExpr
411  *              clauses.  The executor only supports these in bitmap scans, not
412  *              plain indexscans, so we need to segregate them from the normal case.
413  *              Otherwise, same API as find_usable_indexes().
414  *              Returns a list of IndexPaths.
415  */
416 static List *
417 find_saop_paths(PlannerInfo *root, RelOptInfo *rel,
418                                 List *clauses, List *outer_clauses,
419                                 bool istoplevel, RelOptInfo *outer_rel)
420 {
421         bool            have_saop = false;
422         ListCell   *l;
423
424         /*
425          * Since find_usable_indexes is relatively expensive, don't bother to run
426          * it unless there are some top-level ScalarArrayOpExpr clauses.
427          */
428         foreach(l, clauses)
429         {
430                 RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
431
432                 Assert(IsA(rinfo, RestrictInfo));
433                 if (IsA(rinfo->clause, ScalarArrayOpExpr))
434                 {
435                         have_saop = true;
436                         break;
437                 }
438         }
439         if (!have_saop)
440                 return NIL;
441
442         return find_usable_indexes(root, rel,
443                                                            clauses, outer_clauses,
444                                                            istoplevel, outer_rel,
445                                                            SAOP_REQUIRE);
446 }
447
448
449 /*
450  * generate_bitmap_or_paths
451  *              Look through the list of clauses to find OR clauses, and generate
452  *              a BitmapOrPath for each one we can handle that way.  Return a list
453  *              of the generated BitmapOrPaths.
454  *
455  * outer_clauses is a list of additional clauses that can be assumed true
456  * for the purpose of generating indexquals, but are not to be searched for
457  * ORs.  (See find_usable_indexes() for motivation.)  outer_rel is the outer
458  * side when we are considering a nestloop inner indexpath.
459  */
460 List *
461 generate_bitmap_or_paths(PlannerInfo *root, RelOptInfo *rel,
462                                                  List *clauses, List *outer_clauses,
463                                                  RelOptInfo *outer_rel)
464 {
465         List       *result = NIL;
466         List       *all_clauses;
467         ListCell   *l;
468
469         /*
470          * We can use both the current and outer clauses as context for
471          * find_usable_indexes
472          */
473         all_clauses = list_concat(list_copy(clauses), outer_clauses);
474
475         foreach(l, clauses)
476         {
477                 RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
478                 List       *pathlist;
479                 Path       *bitmapqual;
480                 ListCell   *j;
481
482                 Assert(IsA(rinfo, RestrictInfo));
483                 /* Ignore RestrictInfos that aren't ORs */
484                 if (!restriction_is_or_clause(rinfo))
485                         continue;
486
487                 /*
488                  * We must be able to match at least one index to each of the arms of
489                  * the OR, else we can't use it.
490                  */
491                 pathlist = NIL;
492                 foreach(j, ((BoolExpr *) rinfo->orclause)->args)
493                 {
494                         Node       *orarg = (Node *) lfirst(j);
495                         List       *indlist;
496
497                         /* OR arguments should be ANDs or sub-RestrictInfos */
498                         if (and_clause(orarg))
499                         {
500                                 List       *andargs = ((BoolExpr *) orarg)->args;
501
502                                 indlist = find_usable_indexes(root, rel,
503                                                                                           andargs,
504                                                                                           all_clauses,
505                                                                                           false,
506                                                                                           outer_rel,
507                                                                                           SAOP_ALLOW);
508                                 /* Recurse in case there are sub-ORs */
509                                 indlist = list_concat(indlist,
510                                                                           generate_bitmap_or_paths(root, rel,
511                                                                                                                            andargs,
512                                                                                                                            all_clauses,
513                                                                                                                            outer_rel));
514                         }
515                         else
516                         {
517                                 Assert(IsA(orarg, RestrictInfo));
518                                 Assert(!restriction_is_or_clause((RestrictInfo *) orarg));
519                                 indlist = find_usable_indexes(root, rel,
520                                                                                           list_make1(orarg),
521                                                                                           all_clauses,
522                                                                                           false,
523                                                                                           outer_rel,
524                                                                                           SAOP_ALLOW);
525                         }
526
527                         /*
528                          * If nothing matched this arm, we can't do anything with this OR
529                          * clause.
530                          */
531                         if (indlist == NIL)
532                         {
533                                 pathlist = NIL;
534                                 break;
535                         }
536
537                         /*
538                          * OK, pick the most promising AND combination, and add it to
539                          * pathlist.
540                          */
541                         bitmapqual = choose_bitmap_and(root, rel, indlist, outer_rel);
542                         pathlist = lappend(pathlist, bitmapqual);
543                 }
544
545                 /*
546                  * If we have a match for every arm, then turn them into a
547                  * BitmapOrPath, and add to result list.
548                  */
549                 if (pathlist != NIL)
550                 {
551                         bitmapqual = (Path *) create_bitmap_or_path(root, rel, pathlist);
552                         result = lappend(result, bitmapqual);
553                 }
554         }
555
556         return result;
557 }
558
559
560 /*
561  * choose_bitmap_and
562  *              Given a nonempty list of bitmap paths, AND them into one path.
563  *
564  * This is a nontrivial decision since we can legally use any subset of the
565  * given path set.      We want to choose a good tradeoff between selectivity
566  * and cost of computing the bitmap.
567  *
568  * The result is either a single one of the inputs, or a BitmapAndPath
569  * combining multiple inputs.
570  */
571 static Path *
572 choose_bitmap_and(PlannerInfo *root, RelOptInfo *rel,
573                                   List *paths, RelOptInfo *outer_rel)
574 {
575         int                     npaths = list_length(paths);
576         Path      **patharray;
577         Cost            costsofar;
578         List       *qualsofar;
579         ListCell   *lastcell;
580         int                     i;
581         ListCell   *l;
582
583         Assert(npaths > 0);                     /* else caller error */
584         if (npaths == 1)
585                 return (Path *) linitial(paths);                /* easy case */
586
587         /*
588          * In theory we should consider every nonempty subset of the given paths.
589          * In practice that seems like overkill, given the crude nature of the
590          * estimates, not to mention the possible effects of higher-level AND and
591          * OR clauses.  As a compromise, we sort the paths by selectivity.      We
592          * always take the first, and sequentially add on paths that result in a
593          * lower estimated cost.
594          *
595          * We also make some effort to detect directly redundant input paths, as
596          * can happen if there are multiple possibly usable indexes.  (Another way
597          * it can happen is that best_inner_indexscan will find the same OR join
598          * clauses that create_or_index_quals has pulled OR restriction clauses
599          * out of, and then both versions show up as duplicate paths.)  We
600          * consider an index redundant if any of its index conditions were already
601          * used by earlier indexes.  (We could use predicate_implied_by to have a
602          * more intelligent, but much more expensive, check --- but in most cases
603          * simple pointer equality should suffice, since after all the index
604          * conditions are all coming from the same RestrictInfo lists.)
605          *
606          * You might think the condition for redundancy should be "all index
607          * conditions already used", not "any", but this turns out to be wrong.
608          * For example, if we use an index on A, and then come to an index with
609          * conditions on A and B, the only way that the second index can be later
610          * in the selectivity-order sort is if the condition on B is completely
611          * non-selective.  In any case, we'd surely be drastically misestimating
612          * the selectivity if we count the same condition twice.
613          *
614          * We include index predicate conditions in the redundancy test.  Because
615          * the test is just for pointer equality and not equal(), the effect is
616          * that use of the same partial index in two different AND elements is
617          * considered redundant.  (XXX is this too strong?)
618          *
619          * Note: outputting the selected sub-paths in selectivity order is a good
620          * thing even if we weren't using that as part of the selection method,
621          * because it makes the short-circuit case in MultiExecBitmapAnd() more
622          * likely to apply.
623          */
624
625         /* Convert list to array so we can apply qsort */
626         patharray = (Path **) palloc(npaths * sizeof(Path *));
627         i = 0;
628         foreach(l, paths)
629         {
630                 patharray[i++] = (Path *) lfirst(l);
631         }
632         qsort(patharray, npaths, sizeof(Path *), bitmap_path_comparator);
633
634         paths = list_make1(patharray[0]);
635         costsofar = bitmap_and_cost_est(root, rel, paths, outer_rel);
636         qualsofar = pull_indexpath_quals(patharray[0]);
637         lastcell = list_head(paths);    /* for quick deletions */
638
639         for (i = 1; i < npaths; i++)
640         {
641                 Path       *newpath = patharray[i];
642                 List       *newqual;
643                 Cost            newcost;
644
645                 newqual = pull_indexpath_quals(newpath);
646                 if (lists_intersect_ptr(newqual, qualsofar))
647                         continue;                       /* consider it redundant */
648                 /* tentatively add newpath to paths, so we can estimate cost */
649                 paths = lappend(paths, newpath);
650                 newcost = bitmap_and_cost_est(root, rel, paths, outer_rel);
651                 if (newcost < costsofar)
652                 {
653                         /* keep newpath in paths, update subsidiary variables */
654                         costsofar = newcost;
655                         qualsofar = list_concat(qualsofar, newqual);
656                         lastcell = lnext(lastcell);
657                 }
658                 else
659                 {
660                         /* reject newpath, remove it from paths list */
661                         paths = list_delete_cell(paths, lnext(lastcell), lastcell);
662                 }
663                 Assert(lnext(lastcell) == NULL);
664         }
665
666         if (list_length(paths) == 1)
667                 return (Path *) linitial(paths);                /* no need for AND */
668         return (Path *) create_bitmap_and_path(root, rel, paths);
669 }
670
671 /* qsort comparator to sort in increasing selectivity order */
672 static int
673 bitmap_path_comparator(const void *a, const void *b)
674 {
675         Path       *pa = *(Path *const *) a;
676         Path       *pb = *(Path *const *) b;
677         Cost            acost;
678         Cost            bcost;
679         Selectivity aselec;
680         Selectivity bselec;
681
682         cost_bitmap_tree_node(pa, &acost, &aselec);
683         cost_bitmap_tree_node(pb, &bcost, &bselec);
684
685         /*
686          * If selectivities are the same, sort by cost.  (Note: there used to be
687          * logic here to do "fuzzy comparison", but that's a bad idea because it
688          * fails to be transitive, which will confuse qsort terribly.)
689          */
690         if (aselec < bselec)
691                 return -1;
692         if (aselec > bselec)
693                 return 1;
694
695         if (acost < bcost)
696                 return -1;
697         if (acost > bcost)
698                 return 1;
699
700         return 0;
701 }
702
703 /*
704  * Estimate the cost of actually executing a BitmapAnd with the given
705  * inputs.
706  */
707 static Cost
708 bitmap_and_cost_est(PlannerInfo *root, RelOptInfo *rel,
709                                         List *paths, RelOptInfo *outer_rel)
710 {
711         BitmapAndPath apath;
712         Path            bpath;
713
714         /* Set up a dummy BitmapAndPath */
715         apath.path.type = T_BitmapAndPath;
716         apath.path.parent = rel;
717         apath.bitmapquals = paths;
718         cost_bitmap_and_node(&apath, root);
719
720         /* Now we can do cost_bitmap_heap_scan */
721         cost_bitmap_heap_scan(&bpath, root, rel, (Path *) &apath, outer_rel);
722
723         return bpath.total_cost;
724 }
725
726 /*
727  * pull_indexpath_quals
728  *
729  * Given the Path structure for a plain or bitmap indexscan, extract a list
730  * of all the indexquals and index predicate conditions used in the Path.
731  *
732  * This is sort of a simplified version of make_restrictinfo_from_bitmapqual;
733  * here, we are not trying to produce an accurate representation of the AND/OR
734  * semantics of the Path, but just find out all the base conditions used.
735  *
736  * The result list contains pointers to the expressions used in the Path,
737  * but all the list cells are freshly built, so it's safe to destructively
738  * modify the list (eg, by concat'ing it with other lists).
739  */
740 static List *
741 pull_indexpath_quals(Path *bitmapqual)
742 {
743         List       *result = NIL;
744         ListCell   *l;
745
746         if (IsA(bitmapqual, BitmapAndPath))
747         {
748                 BitmapAndPath *apath = (BitmapAndPath *) bitmapqual;
749
750                 foreach(l, apath->bitmapquals)
751                 {
752                         List       *sublist;
753
754                         sublist = pull_indexpath_quals((Path *) lfirst(l));
755                         result = list_concat(result, sublist);
756                 }
757         }
758         else if (IsA(bitmapqual, BitmapOrPath))
759         {
760                 BitmapOrPath *opath = (BitmapOrPath *) bitmapqual;
761
762                 foreach(l, opath->bitmapquals)
763                 {
764                         List       *sublist;
765
766                         sublist = pull_indexpath_quals((Path *) lfirst(l));
767                         result = list_concat(result, sublist);
768                 }
769         }
770         else if (IsA(bitmapqual, IndexPath))
771         {
772                 IndexPath  *ipath = (IndexPath *) bitmapqual;
773
774                 result = get_actual_clauses(ipath->indexclauses);
775                 result = list_concat(result, list_copy(ipath->indexinfo->indpred));
776         }
777         else
778                 elog(ERROR, "unrecognized node type: %d", nodeTag(bitmapqual));
779
780         return result;
781 }
782
783
784 /*
785  * lists_intersect_ptr
786  *              Detect whether two lists have a nonempty intersection,
787  *              using pointer equality to compare members.
788  *
789  * This possibly should go into list.c, but it doesn't yet have any use
790  * except in choose_bitmap_and.
791  */
792 static bool
793 lists_intersect_ptr(List *list1, List *list2)
794 {
795         ListCell   *cell1;
796
797         foreach(cell1, list1)
798         {
799                 void       *datum1 = lfirst(cell1);
800                 ListCell   *cell2;
801
802                 foreach(cell2, list2)
803                 {
804                         if (lfirst(cell2) == datum1)
805                                 return true;
806                 }
807         }
808
809         return false;
810 }
811
812
813 /****************************************************************************
814  *                              ----  ROUTINES TO CHECK RESTRICTIONS  ----
815  ****************************************************************************/
816
817
818 /*
819  * group_clauses_by_indexkey
820  *        Find restriction clauses that can be used with an index.
821  *
822  * Returns a list of sublists of RestrictInfo nodes for clauses that can be
823  * used with this index.  Each sublist contains clauses that can be used
824  * with one index key (in no particular order); the top list is ordered by
825  * index key.  (This is depended on by expand_indexqual_conditions().)
826  *
827  * We can use clauses from either the current clauses or outer_clauses lists,
828  * but *found_clause is set TRUE only if we used at least one clause from
829  * the "current clauses" list.  See find_usable_indexes() for motivation.
830  *
831  * outer_relids determines what Vars will be allowed on the other side
832  * of a possible index qual; see match_clause_to_indexcol().
833  *
834  * 'saop_control' indicates whether ScalarArrayOpExpr clauses can be used.
835  * When it's SAOP_REQUIRE, *found_clause is set TRUE only if we used at least
836  * one ScalarArrayOpExpr from the current clauses list.
837  *
838  * If the index has amoptionalkey = false, we give up and return NIL when
839  * there are no restriction clauses matching the first index key.  Otherwise,
840  * we return NIL if there are no restriction clauses matching any index key.
841  * A non-NIL result will have one (possibly empty) sublist for each index key.
842  *
843  * Example: given an index on (A,B,C), we would return ((C1 C2) () (C3 C4))
844  * if we find that clauses C1 and C2 use column A, clauses C3 and C4 use
845  * column C, and no clauses use column B.
846  *
847  * Note: in some circumstances we may find the same RestrictInfos coming
848  * from multiple places.  Defend against redundant outputs by using
849  * list_append_unique_ptr (pointer equality should be good enough).
850  */
851 List *
852 group_clauses_by_indexkey(IndexOptInfo *index,
853                                                   List *clauses, List *outer_clauses,
854                                                   Relids outer_relids,
855                                                   SaOpControl saop_control,
856                                                   bool *found_clause)
857 {
858         List       *clausegroup_list = NIL;
859         bool            found_outer_clause = false;
860         int                     indexcol = 0;
861         Oid                *families = index->opfamily;
862
863         *found_clause = false;          /* default result */
864
865         if (clauses == NIL && outer_clauses == NIL)
866                 return NIL;                             /* cannot succeed */
867
868         do
869         {
870                 Oid                     curFamily = families[0];
871                 List       *clausegroup = NIL;
872                 ListCell   *l;
873
874                 /* check the current clauses */
875                 foreach(l, clauses)
876                 {
877                         RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
878
879                         Assert(IsA(rinfo, RestrictInfo));
880                         if (match_clause_to_indexcol(index,
881                                                                                  indexcol,
882                                                                                  curFamily,
883                                                                                  rinfo,
884                                                                                  outer_relids,
885                                                                                  saop_control))
886                         {
887                                 clausegroup = list_append_unique_ptr(clausegroup, rinfo);
888                                 if (saop_control != SAOP_REQUIRE ||
889                                         IsA(rinfo->clause, ScalarArrayOpExpr))
890                                         *found_clause = true;
891                         }
892                 }
893
894                 /* check the outer clauses */
895                 foreach(l, outer_clauses)
896                 {
897                         RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
898
899                         Assert(IsA(rinfo, RestrictInfo));
900                         if (match_clause_to_indexcol(index,
901                                                                                  indexcol,
902                                                                                  curFamily,
903                                                                                  rinfo,
904                                                                                  outer_relids,
905                                                                                  saop_control))
906                         {
907                                 clausegroup = list_append_unique_ptr(clausegroup, rinfo);
908                                 found_outer_clause = true;
909                         }
910                 }
911
912                 /*
913                  * If no clauses match this key, check for amoptionalkey restriction.
914                  */
915                 if (clausegroup == NIL && !index->amoptionalkey && indexcol == 0)
916                         return NIL;
917
918                 clausegroup_list = lappend(clausegroup_list, clausegroup);
919
920                 indexcol++;
921                 families++;
922
923         } while (!DoneMatchingIndexKeys(families));
924
925         if (!*found_clause && !found_outer_clause)
926                 return NIL;                             /* no indexable clauses anywhere */
927
928         return clausegroup_list;
929 }
930
931
932 /*
933  * match_clause_to_indexcol()
934  *        Determines whether a restriction clause matches a column of an index.
935  *
936  *        To match a normal index, the clause:
937  *
938  *        (1)  must be in the form (indexkey op const) or (const op indexkey);
939  *                 and
940  *        (2)  must contain an operator which is in the same family as the index
941  *                 operator for this column, or is a "special" operator as recognized
942  *                 by match_special_index_operator().
943  *
944  *        Our definition of "const" is pretty liberal: we allow Vars belonging
945  *        to the caller-specified outer_relids relations (which had better not
946  *        include the relation whose index is being tested).  outer_relids should
947  *        be NULL when checking simple restriction clauses, and the outer side
948  *        of the join when building a join inner scan.  Other than that, the
949  *        only thing we don't like is volatile functions.
950  *
951  *        Note: in most cases we already know that the clause as a whole uses
952  *        vars from the interesting set of relations.  The reason for the
953  *        outer_relids test is to reject clauses like (a.f1 OP (b.f2 OP a.f3));
954  *        that's not processable by an indexscan nestloop join on A, whereas
955  *        (a.f1 OP (b.f2 OP c.f3)) is.
956  *
957  *        Presently, the executor can only deal with indexquals that have the
958  *        indexkey on the left, so we can only use clauses that have the indexkey
959  *        on the right if we can commute the clause to put the key on the left.
960  *        We do not actually do the commuting here, but we check whether a
961  *        suitable commutator operator is available.
962  *
963  *        It is also possible to match RowCompareExpr clauses to indexes (but
964  *        currently, only btree indexes handle this).  In this routine we will
965  *        report a match if the first column of the row comparison matches the
966  *        target index column.  This is sufficient to guarantee that some index
967  *        condition can be constructed from the RowCompareExpr --- whether the
968  *        remaining columns match the index too is considered in
969  *        expand_indexqual_rowcompare().
970  *
971  *        It is also possible to match ScalarArrayOpExpr clauses to indexes, when
972  *        the clause is of the form "indexkey op ANY (arrayconst)".  Since the
973  *        executor can only handle these in the context of bitmap index scans,
974  *        our caller specifies whether to allow these or not.
975  *
976  *        For boolean indexes, it is also possible to match the clause directly
977  *        to the indexkey; or perhaps the clause is (NOT indexkey).
978  *
979  * 'index' is the index of interest.
980  * 'indexcol' is a column number of 'index' (counting from 0).
981  * 'opfamily' is the corresponding operator family.
982  * 'rinfo' is the clause to be tested (as a RestrictInfo node).
983  * 'saop_control' indicates whether ScalarArrayOpExpr clauses can be used.
984  *
985  * Returns true if the clause can be used with this index key.
986  *
987  * NOTE:  returns false if clause is an OR or AND clause; it is the
988  * responsibility of higher-level routines to cope with those.
989  */
990 static bool
991 match_clause_to_indexcol(IndexOptInfo *index,
992                                                  int indexcol,
993                                                  Oid opfamily,
994                                                  RestrictInfo *rinfo,
995                                                  Relids outer_relids,
996                                                  SaOpControl saop_control)
997 {
998         Expr       *clause = rinfo->clause;
999         Node       *leftop,
1000                            *rightop;
1001         Relids          left_relids;
1002         Relids          right_relids;
1003         Oid                     expr_op;
1004         bool            plain_op;
1005
1006         /*
1007          * Never match pseudoconstants to indexes.      (Normally this could not
1008          * happen anyway, since a pseudoconstant clause couldn't contain a Var,
1009          * but what if someone builds an expression index on a constant? It's not
1010          * totally unreasonable to do so with a partial index, either.)
1011          */
1012         if (rinfo->pseudoconstant)
1013                 return false;
1014
1015         /* First check for boolean-index cases. */
1016         if (IsBooleanOpfamily(opfamily))
1017         {
1018                 if (match_boolean_index_clause((Node *) clause, indexcol, index))
1019                         return true;
1020         }
1021
1022         /*
1023          * Clause must be a binary opclause, or possibly a ScalarArrayOpExpr
1024          * (which is always binary, by definition).  Or it could be a
1025          * RowCompareExpr, which we pass off to match_rowcompare_to_indexcol().
1026          */
1027         if (is_opclause(clause))
1028         {
1029                 leftop = get_leftop(clause);
1030                 rightop = get_rightop(clause);
1031                 if (!leftop || !rightop)
1032                         return false;
1033                 left_relids = rinfo->left_relids;
1034                 right_relids = rinfo->right_relids;
1035                 expr_op = ((OpExpr *) clause)->opno;
1036                 plain_op = true;
1037         }
1038         else if (saop_control != SAOP_FORBID &&
1039                          clause && IsA(clause, ScalarArrayOpExpr))
1040         {
1041                 ScalarArrayOpExpr *saop = (ScalarArrayOpExpr *) clause;
1042
1043                 /* We only accept ANY clauses, not ALL */
1044                 if (!saop->useOr)
1045                         return false;
1046                 leftop = (Node *) linitial(saop->args);
1047                 rightop = (Node *) lsecond(saop->args);
1048                 left_relids = NULL;             /* not actually needed */
1049                 right_relids = pull_varnos(rightop);
1050                 expr_op = saop->opno;
1051                 plain_op = false;
1052         }
1053         else if (clause && IsA(clause, RowCompareExpr))
1054         {
1055                 return match_rowcompare_to_indexcol(index, indexcol, opfamily,
1056                                                                                         (RowCompareExpr *) clause,
1057                                                                                         outer_relids);
1058         }
1059         else
1060                 return false;
1061
1062         /*
1063          * Check for clauses of the form: (indexkey operator constant) or
1064          * (constant operator indexkey).  See above notes about const-ness.
1065          */
1066         if (match_index_to_operand(leftop, indexcol, index) &&
1067                 bms_is_subset(right_relids, outer_relids) &&
1068                 !contain_volatile_functions(rightop))
1069         {
1070                 if (is_indexable_operator(expr_op, opfamily, true))
1071                         return true;
1072
1073                 /*
1074                  * If we didn't find a member of the index's opfamily, see whether it
1075                  * is a "special" indexable operator.
1076                  */
1077                 if (plain_op &&
1078                         match_special_index_operator(clause, opfamily, true))
1079                         return true;
1080                 return false;
1081         }
1082
1083         if (plain_op &&
1084                 match_index_to_operand(rightop, indexcol, index) &&
1085                 bms_is_subset(left_relids, outer_relids) &&
1086                 !contain_volatile_functions(leftop))
1087         {
1088                 if (is_indexable_operator(expr_op, opfamily, false))
1089                         return true;
1090
1091                 /*
1092                  * If we didn't find a member of the index's opfamily, see whether it
1093                  * is a "special" indexable operator.
1094                  */
1095                 if (match_special_index_operator(clause, opfamily, false))
1096                         return true;
1097                 return false;
1098         }
1099
1100         return false;
1101 }
1102
1103 /*
1104  * is_indexable_operator
1105  *        Does the operator match the specified index opfamily?
1106  *
1107  * If the indexkey is on the right, what we actually want to know
1108  * is whether the operator has a commutator operator that matches
1109  * the opfamily.
1110  */
1111 static bool
1112 is_indexable_operator(Oid expr_op, Oid opfamily, bool indexkey_on_left)
1113 {
1114         /* Get the commuted operator if necessary */
1115         if (!indexkey_on_left)
1116         {
1117                 expr_op = get_commutator(expr_op);
1118                 if (expr_op == InvalidOid)
1119                         return false;
1120         }
1121
1122         /* OK if the (commuted) operator is a member of the index's opfamily */
1123         return op_in_opfamily(expr_op, opfamily);
1124 }
1125
1126 /*
1127  * match_rowcompare_to_indexcol()
1128  *        Handles the RowCompareExpr case for match_clause_to_indexcol(),
1129  *        which see for comments.
1130  */
1131 static bool
1132 match_rowcompare_to_indexcol(IndexOptInfo *index,
1133                                                          int indexcol,
1134                                                          Oid opfamily,
1135                                                          RowCompareExpr *clause,
1136                                                          Relids outer_relids)
1137 {
1138         Node       *leftop,
1139                            *rightop;
1140         Oid                     expr_op;
1141
1142         /* Forget it if we're not dealing with a btree index */
1143         if (index->relam != BTREE_AM_OID)
1144                 return false;
1145
1146         /*
1147          * We could do the matching on the basis of insisting that the opfamily
1148          * shown in the RowCompareExpr be the same as the index column's opfamily,
1149          * but that could fail in the presence of reverse-sort opfamilies: it'd
1150          * be a matter of chance whether RowCompareExpr had picked the forward
1151          * or reverse-sort family.  So look only at the operator, and match
1152          * if it is a member of the index's opfamily (after commutation, if the
1153          * indexkey is on the right).  We'll worry later about whether any
1154          * additional operators are matchable to the index.
1155          */
1156         leftop = (Node *) linitial(clause->largs);
1157         rightop = (Node *) linitial(clause->rargs);
1158         expr_op = linitial_oid(clause->opnos);
1159
1160         /*
1161          * These syntactic tests are the same as in match_clause_to_indexcol()
1162          */
1163         if (match_index_to_operand(leftop, indexcol, index) &&
1164                 bms_is_subset(pull_varnos(rightop), outer_relids) &&
1165                 !contain_volatile_functions(rightop))
1166         {
1167                 /* OK, indexkey is on left */
1168         }
1169         else if (match_index_to_operand(rightop, indexcol, index) &&
1170                          bms_is_subset(pull_varnos(leftop), outer_relids) &&
1171                          !contain_volatile_functions(leftop))
1172         {
1173                 /* indexkey is on right, so commute the operator */
1174                 expr_op = get_commutator(expr_op);
1175                 if (expr_op == InvalidOid)
1176                         return false;
1177         }
1178         else
1179                 return false;
1180
1181         /* We're good if the operator is the right type of opfamily member */
1182         switch (get_op_opfamily_strategy(expr_op, opfamily))
1183         {
1184                 case BTLessStrategyNumber:
1185                 case BTLessEqualStrategyNumber:
1186                 case BTGreaterEqualStrategyNumber:
1187                 case BTGreaterStrategyNumber:
1188                         return true;
1189         }
1190
1191         return false;
1192 }
1193
1194
1195 /****************************************************************************
1196  *                              ----  ROUTINES TO DO PARTIAL INDEX PREDICATE TESTS      ----
1197  ****************************************************************************/
1198
1199 /*
1200  * check_partial_indexes
1201  *              Check each partial index of the relation, and mark it predOK or not
1202  *              depending on whether the predicate is satisfied for this query.
1203  */
1204 void
1205 check_partial_indexes(PlannerInfo *root, RelOptInfo *rel)
1206 {
1207         List       *restrictinfo_list = rel->baserestrictinfo;
1208         ListCell   *ilist;
1209
1210         /*
1211          * Note: if Postgres tried to optimize queries by forming equivalence
1212          * classes over equi-joined attributes (i.e., if it recognized that a
1213          * qualification such as "where a.b=c.d and a.b=5" could make use of an
1214          * index on c.d), then we could use that equivalence class info here with
1215          * joininfo lists to do more complete tests for the usability of a partial
1216          * index.  For now, the test only uses restriction clauses (those in
1217          * baserestrictinfo). --Nels, Dec '92
1218          *
1219          * XXX as of 7.1, equivalence class info *is* available.  Consider
1220          * improving this code as foreseen by Nels.
1221          */
1222
1223         foreach(ilist, rel->indexlist)
1224         {
1225                 IndexOptInfo *index = (IndexOptInfo *) lfirst(ilist);
1226
1227                 if (index->indpred == NIL)
1228                         continue;                       /* ignore non-partial indexes */
1229
1230                 index->predOK = predicate_implied_by(index->indpred,
1231                                                                                          restrictinfo_list);
1232         }
1233 }
1234
1235 /****************************************************************************
1236  *                              ----  ROUTINES TO CHECK JOIN CLAUSES  ----
1237  ****************************************************************************/
1238
1239 /*
1240  * indexable_outerrelids
1241  *        Finds all other relids that participate in any indexable join clause
1242  *        for the specified table.      Returns a set of relids.
1243  */
1244 static Relids
1245 indexable_outerrelids(RelOptInfo *rel)
1246 {
1247         Relids          outer_relids = NULL;
1248         ListCell   *l;
1249
1250         /*
1251          * Examine each joinclause in the joininfo list to see if it matches any
1252          * key of any index.  If so, add the clause's other rels to the result.
1253          */
1254         foreach(l, rel->joininfo)
1255         {
1256                 RestrictInfo *joininfo = (RestrictInfo *) lfirst(l);
1257                 Relids          other_rels;
1258
1259                 other_rels = bms_difference(joininfo->required_relids, rel->relids);
1260                 if (matches_any_index(joininfo, rel, other_rels))
1261                         outer_relids = bms_join(outer_relids, other_rels);
1262                 else
1263                         bms_free(other_rels);
1264         }
1265
1266         return outer_relids;
1267 }
1268
1269 /*
1270  * matches_any_index
1271  *        Workhorse for indexable_outerrelids: see if a joinclause can be
1272  *        matched to any index of the given rel.
1273  */
1274 static bool
1275 matches_any_index(RestrictInfo *rinfo, RelOptInfo *rel, Relids outer_relids)
1276 {
1277         ListCell   *l;
1278
1279         Assert(IsA(rinfo, RestrictInfo));
1280
1281         if (restriction_is_or_clause(rinfo))
1282         {
1283                 foreach(l, ((BoolExpr *) rinfo->orclause)->args)
1284                 {
1285                         Node       *orarg = (Node *) lfirst(l);
1286
1287                         /* OR arguments should be ANDs or sub-RestrictInfos */
1288                         if (and_clause(orarg))
1289                         {
1290                                 ListCell   *j;
1291
1292                                 /* Recurse to examine AND items and sub-ORs */
1293                                 foreach(j, ((BoolExpr *) orarg)->args)
1294                                 {
1295                                         RestrictInfo *arinfo = (RestrictInfo *) lfirst(j);
1296
1297                                         if (matches_any_index(arinfo, rel, outer_relids))
1298                                                 return true;
1299                                 }
1300                         }
1301                         else
1302                         {
1303                                 /* Recurse to examine simple clause */
1304                                 Assert(IsA(orarg, RestrictInfo));
1305                                 Assert(!restriction_is_or_clause((RestrictInfo *) orarg));
1306                                 if (matches_any_index((RestrictInfo *) orarg, rel,
1307                                                                           outer_relids))
1308                                         return true;
1309                         }
1310                 }
1311
1312                 return false;
1313         }
1314
1315         /* Normal case for a simple restriction clause */
1316         foreach(l, rel->indexlist)
1317         {
1318                 IndexOptInfo *index = (IndexOptInfo *) lfirst(l);
1319                 int                     indexcol = 0;
1320                 Oid                *families = index->opfamily;
1321
1322                 do
1323                 {
1324                         Oid                     curFamily = families[0];
1325
1326                         if (match_clause_to_indexcol(index,
1327                                                                                  indexcol,
1328                                                                                  curFamily,
1329                                                                                  rinfo,
1330                                                                                  outer_relids,
1331                                                                                  SAOP_ALLOW))
1332                                 return true;
1333
1334                         indexcol++;
1335                         families++;
1336                 } while (!DoneMatchingIndexKeys(families));
1337         }
1338
1339         return false;
1340 }
1341
1342 /*
1343  * best_inner_indexscan
1344  *        Finds the best available inner indexscan for a nestloop join
1345  *        with the given rel on the inside and the given outer_rel outside.
1346  *        May return NULL if there are no possible inner indexscans.
1347  *
1348  * We ignore ordering considerations (since a nestloop's inner scan's order
1349  * is uninteresting).  Also, we consider only total cost when deciding which
1350  * of two possible paths is better --- this assumes that all indexpaths have
1351  * negligible startup cost.  (True today, but someday we might have to think
1352  * harder.)  Therefore, there is only one dimension of comparison and so it's
1353  * sufficient to return a single "best" path.
1354  *
1355  * Note: create_index_paths() must have been run previously for this rel,
1356  * else the result will always be NULL.
1357  */
1358 Path *
1359 best_inner_indexscan(PlannerInfo *root, RelOptInfo *rel,
1360                                          RelOptInfo *outer_rel, JoinType jointype)
1361 {
1362         Relids          outer_relids;
1363         Path       *cheapest;
1364         bool            isouterjoin;
1365         List       *clause_list;
1366         List       *indexpaths;
1367         List       *bitindexpaths;
1368         ListCell   *l;
1369         InnerIndexscanInfo *info;
1370         MemoryContext oldcontext;
1371
1372         /*
1373          * Nestloop only supports inner, left, and IN joins.
1374          */
1375         switch (jointype)
1376         {
1377                 case JOIN_INNER:
1378                 case JOIN_IN:
1379                 case JOIN_UNIQUE_OUTER:
1380                         isouterjoin = false;
1381                         break;
1382                 case JOIN_LEFT:
1383                         isouterjoin = true;
1384                         break;
1385                 default:
1386                         return NULL;
1387         }
1388
1389         /*
1390          * If there are no indexable joinclauses for this rel, exit quickly.
1391          */
1392         if (bms_is_empty(rel->index_outer_relids))
1393                 return NULL;
1394
1395         /*
1396          * Otherwise, we have to do path selection in the memory context of the
1397          * given rel, so that any created path can be safely attached to the rel's
1398          * cache of best inner paths.  (This is not currently an issue for normal
1399          * planning, but it is an issue for GEQO planning.)
1400          */
1401         oldcontext = MemoryContextSwitchTo(GetMemoryChunkContext(rel));
1402
1403         /*
1404          * Intersect the given outer relids with index_outer_relids to find the
1405          * set of outer relids actually relevant for this rel. If there are none,
1406          * again we can fail immediately.
1407          */
1408         outer_relids = bms_intersect(rel->index_outer_relids, outer_rel->relids);
1409         if (bms_is_empty(outer_relids))
1410         {
1411                 bms_free(outer_relids);
1412                 MemoryContextSwitchTo(oldcontext);
1413                 return NULL;
1414         }
1415
1416         /*
1417          * Look to see if we already computed the result for this set of relevant
1418          * outerrels.  (We include the isouterjoin status in the cache lookup key
1419          * for safety.  In practice I suspect this is not necessary because it
1420          * should always be the same for a given innerrel.)
1421          *
1422          * NOTE: because we cache on outer_relids rather than outer_rel->relids,
1423          * we will report the same path and hence path cost for joins with
1424          * different sets of irrelevant rels on the outside.  Now that cost_index
1425          * is sensitive to outer_rel->rows, this is not really right.  However the
1426          * error is probably not large.  Is it worth establishing a separate cache
1427          * entry for each distinct outer_rel->relids set to get this right?
1428          */
1429         foreach(l, rel->index_inner_paths)
1430         {
1431                 info = (InnerIndexscanInfo *) lfirst(l);
1432                 if (bms_equal(info->other_relids, outer_relids) &&
1433                         info->isouterjoin == isouterjoin)
1434                 {
1435                         bms_free(outer_relids);
1436                         MemoryContextSwitchTo(oldcontext);
1437                         return info->best_innerpath;
1438                 }
1439         }
1440
1441         /*
1442          * Find all the relevant restriction and join clauses.
1443          *
1444          * Note: because we include restriction clauses, we will find indexscans
1445          * that could be plain indexscans, ie, they don't require the join context
1446          * at all.      This may seem redundant, but we need to include those scans in
1447          * the input given to choose_bitmap_and() to be sure we find optimal AND
1448          * combinations of join and non-join scans.  Also, even if the "best inner
1449          * indexscan" is just a plain indexscan, it will have a different cost
1450          * estimate because of cache effects.
1451          */
1452         clause_list = find_clauses_for_join(root, rel, outer_relids, isouterjoin);
1453
1454         /*
1455          * Find all the index paths that are usable for this join, except for
1456          * stuff involving OR and ScalarArrayOpExpr clauses.
1457          */
1458         indexpaths = find_usable_indexes(root, rel,
1459                                                                          clause_list, NIL,
1460                                                                          false, outer_rel,
1461                                                                          SAOP_FORBID);
1462
1463         /*
1464          * Generate BitmapOrPaths for any suitable OR-clauses present in the
1465          * clause list.
1466          */
1467         bitindexpaths = generate_bitmap_or_paths(root, rel,
1468                                                                                          clause_list, NIL,
1469                                                                                          outer_rel);
1470
1471         /*
1472          * Likewise, generate paths using ScalarArrayOpExpr clauses; these can't
1473          * be simple indexscans but they can be used in bitmap scans.
1474          */
1475         bitindexpaths = list_concat(bitindexpaths,
1476                                                                 find_saop_paths(root, rel,
1477                                                                                                 clause_list, NIL,
1478                                                                                                 false, outer_rel));
1479
1480         /*
1481          * Include the regular index paths in bitindexpaths.
1482          */
1483         bitindexpaths = list_concat(bitindexpaths, list_copy(indexpaths));
1484
1485         /*
1486          * If we found anything usable, generate a BitmapHeapPath for the most
1487          * promising combination of bitmap index paths.
1488          */
1489         if (bitindexpaths != NIL)
1490         {
1491                 Path       *bitmapqual;
1492                 BitmapHeapPath *bpath;
1493
1494                 bitmapqual = choose_bitmap_and(root, rel, bitindexpaths, outer_rel);
1495                 bpath = create_bitmap_heap_path(root, rel, bitmapqual, outer_rel);
1496                 indexpaths = lappend(indexpaths, bpath);
1497         }
1498
1499         /*
1500          * Now choose the cheapest member of indexpaths.
1501          */
1502         cheapest = NULL;
1503         foreach(l, indexpaths)
1504         {
1505                 Path       *path = (Path *) lfirst(l);
1506
1507                 if (cheapest == NULL ||
1508                         compare_path_costs(path, cheapest, TOTAL_COST) < 0)
1509                         cheapest = path;
1510         }
1511
1512         /* Cache the result --- whether positive or negative */
1513         info = makeNode(InnerIndexscanInfo);
1514         info->other_relids = outer_relids;
1515         info->isouterjoin = isouterjoin;
1516         info->best_innerpath = cheapest;
1517         rel->index_inner_paths = lcons(info, rel->index_inner_paths);
1518
1519         MemoryContextSwitchTo(oldcontext);
1520
1521         return cheapest;
1522 }
1523
1524 /*
1525  * find_clauses_for_join
1526  *        Generate a list of clauses that are potentially useful for
1527  *        scanning rel as the inner side of a nestloop join.
1528  *
1529  * We consider both join and restriction clauses.  Any joinclause that uses
1530  * only otherrels in the specified outer_relids is fair game.  But there must
1531  * be at least one such joinclause in the final list, otherwise we return NIL
1532  * indicating that there isn't any potential win here.
1533  */
1534 static List *
1535 find_clauses_for_join(PlannerInfo *root, RelOptInfo *rel,
1536                                           Relids outer_relids, bool isouterjoin)
1537 {
1538         List       *clause_list = NIL;
1539         Relids          join_relids;
1540         ListCell   *l;
1541
1542         /* Look for joinclauses that are usable with given outer_relids */
1543         join_relids = bms_union(rel->relids, outer_relids);
1544
1545         foreach(l, rel->joininfo)
1546         {
1547                 RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
1548
1549                 /* Can't use pushed-down join clauses in outer join */
1550                 if (isouterjoin && rinfo->is_pushed_down)
1551                         continue;
1552                 if (!bms_is_subset(rinfo->required_relids, join_relids))
1553                         continue;
1554
1555                 clause_list = lappend(clause_list, rinfo);
1556         }
1557
1558         bms_free(join_relids);
1559
1560         /* if no join clause was matched then forget it, per comments above */
1561         if (clause_list == NIL)
1562                 return NIL;
1563
1564         /*
1565          * We can also use any plain restriction clauses for the rel.  We put
1566          * these at the front of the clause list for the convenience of
1567          * remove_redundant_join_clauses, which can never remove non-join clauses
1568          * and hence won't be able to get rid of a non-join clause if it appears
1569          * after a join clause it is redundant with.
1570          */
1571         clause_list = list_concat(list_copy(rel->baserestrictinfo), clause_list);
1572
1573         /*
1574          * We may now have clauses that are known redundant.  Get rid of 'em.
1575          */
1576         if (list_length(clause_list) > 1)
1577         {
1578                 clause_list = remove_redundant_join_clauses(root,
1579                                                                                                         clause_list,
1580                                                                                                         isouterjoin);
1581         }
1582
1583         return clause_list;
1584 }
1585
1586 /****************************************************************************
1587  *                              ----  ROUTINES TO HANDLE PATHKEYS  ----
1588  ****************************************************************************/
1589
1590 /*
1591  * match_variant_ordering
1592  *              Try to match an index's ordering to the query's requested ordering
1593  *
1594  * This is used when the index is ordered but a naive comparison fails to
1595  * match its ordering (pathkeys) to root->query_pathkeys.  It may be that
1596  * we need to scan the index backwards.  Also, a less naive comparison can
1597  * help for both forward and backward indexscans.  Columns of the index
1598  * that have an equality restriction clause can be ignored in the match;
1599  * that is, an index on (x,y) can be considered to match the ordering of
1600  *              ... WHERE x = 42 ORDER BY y;
1601  *
1602  * Note: it would be possible to similarly ignore useless ORDER BY items;
1603  * that is, an index on just y could be considered to match the ordering of
1604  *              ... WHERE x = 42 ORDER BY x, y;
1605  * But proving that this is safe would require finding a btree opfamily
1606  * containing both the = operator and the < or > operator in the ORDER BY
1607  * item.  That's significantly more expensive than what we do here, since
1608  * we'd have to look at restriction clauses unrelated to the current index
1609  * and search for opfamilies without any hint from the index.  The practical
1610  * use-cases seem to be mostly covered by ignoring index columns, so that's
1611  * all we do for now.
1612  *
1613  * Inputs:
1614  * 'index' is the index of interest.
1615  * 'restrictclauses' is the list of sublists of restriction clauses
1616  *              matching the columns of the index (NIL if none)
1617  *
1618  * If able to match the requested query pathkeys, returns either
1619  * ForwardScanDirection or BackwardScanDirection to indicate the proper index
1620  * scan direction.      If no match, returns NoMovementScanDirection.
1621  */
1622 static ScanDirection
1623 match_variant_ordering(PlannerInfo *root,
1624                                            IndexOptInfo *index,
1625                                            List *restrictclauses)
1626 {
1627         List       *ignorables;
1628
1629         /*
1630          * Forget the whole thing if not a btree index; our check for ignorable
1631          * columns assumes we are dealing with btree opfamilies.  (It'd be possible
1632          * to factor out just the try for backwards indexscan, but considering
1633          * that we presently have no orderable indexes except btrees anyway, it's
1634          * hardly worth contorting this code for that case.)
1635          *
1636          * Note: if you remove this, you probably need to put in a check on
1637          * amoptionalkey to prevent possible clauseless scan on an index that
1638          * won't cope.
1639          */
1640         if (index->relam != BTREE_AM_OID)
1641                 return NoMovementScanDirection;
1642
1643         /*
1644          * Figure out which index columns can be optionally ignored because they
1645          * have an equality constraint.  This is the same set for either forward
1646          * or backward scan, so we do it just once.
1647          */
1648         ignorables = identify_ignorable_ordering_cols(root, index,
1649                                                                                                   restrictclauses);
1650
1651         /*
1652          * Try to match to forward scan, then backward scan.  However, we can skip
1653          * the forward-scan case if there are no ignorable columns, because
1654          * find_usable_indexes() would have found the match already.
1655          */
1656         if (ignorables &&
1657                 match_index_to_query_keys(root, index, ForwardScanDirection,
1658                                                                   ignorables))
1659                 return ForwardScanDirection;
1660
1661         if (match_index_to_query_keys(root, index, BackwardScanDirection,
1662                                                                   ignorables))
1663                 return BackwardScanDirection;
1664
1665         return NoMovementScanDirection;
1666 }
1667
1668 /*
1669  * identify_ignorable_ordering_cols
1670  *              Determine which index columns can be ignored for ordering purposes
1671  *
1672  * Returns an integer List of column numbers (1-based) of ignorable
1673  * columns.  The ignorable columns are those that have equality constraints
1674  * against pseudoconstants.
1675  */
1676 static List *
1677 identify_ignorable_ordering_cols(PlannerInfo *root,
1678                                                                  IndexOptInfo *index,
1679                                                                  List *restrictclauses)
1680 {
1681         List       *result = NIL;
1682         int                     indexcol = 0;   /* note this is 0-based */
1683         ListCell   *l;
1684
1685         /* restrictclauses is either NIL or has a sublist per column */
1686         foreach(l, restrictclauses)
1687         {
1688                 List       *sublist = (List *) lfirst(l);
1689                 Oid                     opfamily = index->opfamily[indexcol];
1690                 ListCell   *l2;
1691
1692                 foreach(l2, sublist)
1693                 {
1694                         RestrictInfo *rinfo = (RestrictInfo *) lfirst(l2);
1695                         OpExpr     *clause = (OpExpr *) rinfo->clause;
1696                         Oid                     clause_op;
1697                         int                     op_strategy;
1698                         bool            varonleft;
1699                         bool            ispc;
1700
1701                         /* First check for boolean-index cases. */
1702                         if (IsBooleanOpfamily(opfamily))
1703                         {
1704                                 if (match_boolean_index_clause((Node *) clause, indexcol,
1705                                                                                            index))
1706                                 {
1707                                         /*
1708                                          * The clause means either col = TRUE or col = FALSE; we
1709                                          * do not care which, it's an equality constraint either
1710                                          * way.
1711                                          */
1712                                         result = lappend_int(result, indexcol + 1);
1713                                         break;
1714                                 }
1715                         }
1716
1717                         /* Otherwise, ignore if not a binary opclause */
1718                         if (!is_opclause(clause) || list_length(clause->args) != 2)
1719                                 continue;
1720
1721                         /* Determine left/right sides and check the operator */
1722                         clause_op = clause->opno;
1723                         if (match_index_to_operand(linitial(clause->args), indexcol,
1724                                                                            index))
1725                         {
1726                                 /* clause_op is correct */
1727                                 varonleft = true;
1728                         }
1729                         else
1730                         {
1731                                 Assert(match_index_to_operand(lsecond(clause->args), indexcol,
1732                                                                                           index));
1733                                 /* Must flip operator to get the opfamily member */
1734                                 clause_op = get_commutator(clause_op);
1735                                 varonleft = false;
1736                         }
1737                         if (!OidIsValid(clause_op))
1738                                 continue;               /* ignore non match, per next comment */
1739                         op_strategy = get_op_opfamily_strategy(clause_op, opfamily);
1740
1741                         /*
1742                          * You might expect to see Assert(op_strategy != 0) here, but you
1743                          * won't: the clause might contain a special indexable operator
1744                          * rather than an ordinary opfamily member.     Currently none of the
1745                          * special operators are very likely to expand to an equality
1746                          * operator; we do not bother to check, but just assume no match.
1747                          */
1748                         if (op_strategy != BTEqualStrategyNumber)
1749                                 continue;
1750
1751                         /* Now check that other side is pseudoconstant */
1752                         if (varonleft)
1753                                 ispc = is_pseudo_constant_clause_relids(lsecond(clause->args),
1754                                                                                                                 rinfo->right_relids);
1755                         else
1756                                 ispc = is_pseudo_constant_clause_relids(linitial(clause->args),
1757                                                                                                                 rinfo->left_relids);
1758                         if (ispc)
1759                         {
1760                                 result = lappend_int(result, indexcol + 1);
1761                                 break;
1762                         }
1763                 }
1764                 indexcol++;
1765         }
1766         return result;
1767 }
1768
1769 /*
1770  * match_index_to_query_keys
1771  *              Check a single scan direction for "intelligent" match to query keys
1772  *
1773  * 'index' is the index of interest.
1774  * 'indexscandir' is the scan direction to consider
1775  * 'ignorables' is an integer list of indexes of ignorable index columns
1776  *
1777  * Returns TRUE on successful match (ie, the query_pathkeys can be considered
1778  * to match this index).
1779  */
1780 static bool
1781 match_index_to_query_keys(PlannerInfo *root,
1782                                                   IndexOptInfo *index,
1783                                                   ScanDirection indexscandir,
1784                                                   List *ignorables)
1785 {
1786         List       *index_pathkeys;
1787         ListCell   *index_cell;
1788         int                     index_col;
1789         ListCell   *r;
1790
1791         /* Get the pathkeys that exactly describe the index */
1792         index_pathkeys = build_index_pathkeys(root, index, indexscandir, false);
1793
1794         /*
1795          * Can we match to the query's requested pathkeys?  The inner loop skips
1796          * over ignorable index columns while trying to match.
1797          */
1798         index_cell = list_head(index_pathkeys);
1799         index_col = 0;
1800
1801         foreach(r, root->query_pathkeys)
1802         {
1803                 List       *rsubkey = (List *) lfirst(r);
1804
1805                 for (;;)
1806                 {
1807                         List       *isubkey;
1808
1809                         if (index_cell == NULL)
1810                                 return false;
1811                         isubkey = (List *) lfirst(index_cell);
1812                         index_cell = lnext(index_cell);
1813                         index_col++;            /* index_col is now 1-based */
1814
1815                         /*
1816                          * Since we are dealing with canonicalized pathkeys, pointer
1817                          * comparison is sufficient to determine a match.
1818                          */
1819                         if (rsubkey == isubkey)
1820                                 break;                  /* matched current query pathkey */
1821
1822                         if (!list_member_int(ignorables, index_col))
1823                                 return false;   /* definite failure to match */
1824                         /* otherwise loop around and try to match to next index col */
1825                 }
1826         }
1827
1828         return true;
1829 }
1830
1831 /****************************************************************************
1832  *                              ----  PATH CREATION UTILITIES  ----
1833  ****************************************************************************/
1834
1835 /*
1836  * flatten_clausegroups_list
1837  *        Given a list of lists of RestrictInfos, flatten it to a list
1838  *        of RestrictInfos.
1839  *
1840  * This is used to flatten out the result of group_clauses_by_indexkey()
1841  * to produce an indexclauses list.  The original list structure mustn't
1842  * be altered, but it's OK to share copies of the underlying RestrictInfos.
1843  */
1844 List *
1845 flatten_clausegroups_list(List *clausegroups)
1846 {
1847         List       *allclauses = NIL;
1848         ListCell   *l;
1849
1850         foreach(l, clausegroups)
1851                 allclauses = list_concat(allclauses, list_copy((List *) lfirst(l)));
1852         return allclauses;
1853 }
1854
1855
1856 /****************************************************************************
1857  *                              ----  ROUTINES TO CHECK OPERANDS  ----
1858  ****************************************************************************/
1859
1860 /*
1861  * match_index_to_operand()
1862  *        Generalized test for a match between an index's key
1863  *        and the operand on one side of a restriction or join clause.
1864  *
1865  * operand: the nodetree to be compared to the index
1866  * indexcol: the column number of the index (counting from 0)
1867  * index: the index of interest
1868  */
1869 bool
1870 match_index_to_operand(Node *operand,
1871                                            int indexcol,
1872                                            IndexOptInfo *index)
1873 {
1874         int                     indkey;
1875
1876         /*
1877          * Ignore any RelabelType node above the operand.       This is needed to be
1878          * able to apply indexscanning in binary-compatible-operator cases. Note:
1879          * we can assume there is at most one RelabelType node;
1880          * eval_const_expressions() will have simplified if more than one.
1881          */
1882         if (operand && IsA(operand, RelabelType))
1883                 operand = (Node *) ((RelabelType *) operand)->arg;
1884
1885         indkey = index->indexkeys[indexcol];
1886         if (indkey != 0)
1887         {
1888                 /*
1889                  * Simple index column; operand must be a matching Var.
1890                  */
1891                 if (operand && IsA(operand, Var) &&
1892                         index->rel->relid == ((Var *) operand)->varno &&
1893                         indkey == ((Var *) operand)->varattno)
1894                         return true;
1895         }
1896         else
1897         {
1898                 /*
1899                  * Index expression; find the correct expression.  (This search could
1900                  * be avoided, at the cost of complicating all the callers of this
1901                  * routine; doesn't seem worth it.)
1902                  */
1903                 ListCell   *indexpr_item;
1904                 int                     i;
1905                 Node       *indexkey;
1906
1907                 indexpr_item = list_head(index->indexprs);
1908                 for (i = 0; i < indexcol; i++)
1909                 {
1910                         if (index->indexkeys[i] == 0)
1911                         {
1912                                 if (indexpr_item == NULL)
1913                                         elog(ERROR, "wrong number of index expressions");
1914                                 indexpr_item = lnext(indexpr_item);
1915                         }
1916                 }
1917                 if (indexpr_item == NULL)
1918                         elog(ERROR, "wrong number of index expressions");
1919                 indexkey = (Node *) lfirst(indexpr_item);
1920
1921                 /*
1922                  * Does it match the operand?  Again, strip any relabeling.
1923                  */
1924                 if (indexkey && IsA(indexkey, RelabelType))
1925                         indexkey = (Node *) ((RelabelType *) indexkey)->arg;
1926
1927                 if (equal(indexkey, operand))
1928                         return true;
1929         }
1930
1931         return false;
1932 }
1933
1934 /****************************************************************************
1935  *                      ----  ROUTINES FOR "SPECIAL" INDEXABLE OPERATORS  ----
1936  ****************************************************************************/
1937
1938 /*----------
1939  * These routines handle special optimization of operators that can be
1940  * used with index scans even though they are not known to the executor's
1941  * indexscan machinery.  The key idea is that these operators allow us
1942  * to derive approximate indexscan qual clauses, such that any tuples
1943  * that pass the operator clause itself must also satisfy the simpler
1944  * indexscan condition(s).      Then we can use the indexscan machinery
1945  * to avoid scanning as much of the table as we'd otherwise have to,
1946  * while applying the original operator as a qpqual condition to ensure
1947  * we deliver only the tuples we want.  (In essence, we're using a regular
1948  * index as if it were a lossy index.)
1949  *
1950  * An example of what we're doing is
1951  *                      textfield LIKE 'abc%'
1952  * from which we can generate the indexscanable conditions
1953  *                      textfield >= 'abc' AND textfield < 'abd'
1954  * which allow efficient scanning of an index on textfield.
1955  * (In reality, character set and collation issues make the transformation
1956  * from LIKE to indexscan limits rather harder than one might think ...
1957  * but that's the basic idea.)
1958  *
1959  * Another thing that we do with this machinery is to provide special
1960  * smarts for "boolean" indexes (that is, indexes on boolean columns
1961  * that support boolean equality).      We can transform a plain reference
1962  * to the indexkey into "indexkey = true", or "NOT indexkey" into
1963  * "indexkey = false", so as to make the expression indexable using the
1964  * regular index operators.  (As of Postgres 8.1, we must do this here
1965  * because constant simplification does the reverse transformation;
1966  * without this code there'd be no way to use such an index at all.)
1967  *
1968  * Three routines are provided here:
1969  *
1970  * match_special_index_operator() is just an auxiliary function for
1971  * match_clause_to_indexcol(); after the latter fails to recognize a
1972  * restriction opclause's operator as a member of an index's opfamily,
1973  * it asks match_special_index_operator() whether the clause should be
1974  * considered an indexqual anyway.
1975  *
1976  * match_boolean_index_clause() similarly detects clauses that can be
1977  * converted into boolean equality operators.
1978  *
1979  * expand_indexqual_conditions() converts a list of lists of RestrictInfo
1980  * nodes (with implicit AND semantics across list elements) into
1981  * a list of clauses that the executor can actually handle.  For operators
1982  * that are members of the index's opfamily this transformation is a no-op,
1983  * but clauses recognized by match_special_index_operator() or
1984  * match_boolean_index_clause() must be converted into one or more "regular"
1985  * indexqual conditions.
1986  *----------
1987  */
1988
1989 /*
1990  * match_boolean_index_clause
1991  *        Recognize restriction clauses that can be matched to a boolean index.
1992  *
1993  * This should be called only when IsBooleanOpfamily() recognizes the
1994  * index's operator family.  We check to see if the clause matches the
1995  * index's key.
1996  */
1997 static bool
1998 match_boolean_index_clause(Node *clause,
1999                                                    int indexcol,
2000                                                    IndexOptInfo *index)
2001 {
2002         /* Direct match? */
2003         if (match_index_to_operand(clause, indexcol, index))
2004                 return true;
2005         /* NOT clause? */
2006         if (not_clause(clause))
2007         {
2008                 if (match_index_to_operand((Node *) get_notclausearg((Expr *) clause),
2009                                                                    indexcol, index))
2010                         return true;
2011         }
2012
2013         /*
2014          * Since we only consider clauses at top level of WHERE, we can convert
2015          * indexkey IS TRUE and indexkey IS FALSE to index searches as well. The
2016          * different meaning for NULL isn't important.
2017          */
2018         else if (clause && IsA(clause, BooleanTest))
2019         {
2020                 BooleanTest *btest = (BooleanTest *) clause;
2021
2022                 if (btest->booltesttype == IS_TRUE ||
2023                         btest->booltesttype == IS_FALSE)
2024                         if (match_index_to_operand((Node *) btest->arg,
2025                                                                            indexcol, index))
2026                                 return true;
2027         }
2028         return false;
2029 }
2030
2031 /*
2032  * match_special_index_operator
2033  *        Recognize restriction clauses that can be used to generate
2034  *        additional indexscanable qualifications.
2035  *
2036  * The given clause is already known to be a binary opclause having
2037  * the form (indexkey OP pseudoconst) or (pseudoconst OP indexkey),
2038  * but the OP proved not to be one of the index's opfamily operators.
2039  * Return 'true' if we can do something with it anyway.
2040  */
2041 static bool
2042 match_special_index_operator(Expr *clause, Oid opfamily,
2043                                                          bool indexkey_on_left)
2044 {
2045         bool            isIndexable = false;
2046         Node       *rightop;
2047         Oid                     expr_op;
2048         Const      *patt;
2049         Const      *prefix = NULL;
2050         Const      *rest = NULL;
2051
2052         /*
2053          * Currently, all known special operators require the indexkey on the
2054          * left, but this test could be pushed into the switch statement if some
2055          * are added that do not...
2056          */
2057         if (!indexkey_on_left)
2058                 return false;
2059
2060         /* we know these will succeed */
2061         rightop = get_rightop(clause);
2062         expr_op = ((OpExpr *) clause)->opno;
2063
2064         /* again, required for all current special ops: */
2065         if (!IsA(rightop, Const) ||
2066                 ((Const *) rightop)->constisnull)
2067                 return false;
2068         patt = (Const *) rightop;
2069
2070         switch (expr_op)
2071         {
2072                 case OID_TEXT_LIKE_OP:
2073                 case OID_BPCHAR_LIKE_OP:
2074                 case OID_NAME_LIKE_OP:
2075                         /* the right-hand const is type text for all of these */
2076                         isIndexable = pattern_fixed_prefix(patt, Pattern_Type_Like,
2077                                                                           &prefix, &rest) != Pattern_Prefix_None;
2078                         break;
2079
2080                 case OID_BYTEA_LIKE_OP:
2081                         isIndexable = pattern_fixed_prefix(patt, Pattern_Type_Like,
2082                                                                           &prefix, &rest) != Pattern_Prefix_None;
2083                         break;
2084
2085                 case OID_TEXT_ICLIKE_OP:
2086                 case OID_BPCHAR_ICLIKE_OP:
2087                 case OID_NAME_ICLIKE_OP:
2088                         /* the right-hand const is type text for all of these */
2089                         isIndexable = pattern_fixed_prefix(patt, Pattern_Type_Like_IC,
2090                                                                           &prefix, &rest) != Pattern_Prefix_None;
2091                         break;
2092
2093                 case OID_TEXT_REGEXEQ_OP:
2094                 case OID_BPCHAR_REGEXEQ_OP:
2095                 case OID_NAME_REGEXEQ_OP:
2096                         /* the right-hand const is type text for all of these */
2097                         isIndexable = pattern_fixed_prefix(patt, Pattern_Type_Regex,
2098                                                                           &prefix, &rest) != Pattern_Prefix_None;
2099                         break;
2100
2101                 case OID_TEXT_ICREGEXEQ_OP:
2102                 case OID_BPCHAR_ICREGEXEQ_OP:
2103                 case OID_NAME_ICREGEXEQ_OP:
2104                         /* the right-hand const is type text for all of these */
2105                         isIndexable = pattern_fixed_prefix(patt, Pattern_Type_Regex_IC,
2106                                                                           &prefix, &rest) != Pattern_Prefix_None;
2107                         break;
2108
2109                 case OID_INET_SUB_OP:
2110                 case OID_INET_SUBEQ_OP:
2111                         isIndexable = true;
2112                         break;
2113         }
2114
2115         if (prefix)
2116         {
2117                 pfree(DatumGetPointer(prefix->constvalue));
2118                 pfree(prefix);
2119         }
2120
2121         /* done if the expression doesn't look indexable */
2122         if (!isIndexable)
2123                 return false;
2124
2125         /*
2126          * Must also check that index's opfamily supports the operators we will
2127          * want to apply.  (A hash index, for example, will not support ">=".)
2128          * Currently, only btree supports the operators we need.
2129          *
2130          * We insist on the opfamily being the specific one we expect, else we'd do
2131          * the wrong thing if someone were to make a reverse-sort opfamily with the
2132          * same operators.
2133          */
2134         switch (expr_op)
2135         {
2136                 case OID_TEXT_LIKE_OP:
2137                 case OID_TEXT_ICLIKE_OP:
2138                 case OID_TEXT_REGEXEQ_OP:
2139                 case OID_TEXT_ICREGEXEQ_OP:
2140                         isIndexable =
2141                                 (opfamily == TEXT_PATTERN_BTREE_FAM_OID) ||
2142                                 (opfamily == TEXT_BTREE_FAM_OID && lc_collate_is_c());
2143                         break;
2144
2145                 case OID_BPCHAR_LIKE_OP:
2146                 case OID_BPCHAR_ICLIKE_OP:
2147                 case OID_BPCHAR_REGEXEQ_OP:
2148                 case OID_BPCHAR_ICREGEXEQ_OP:
2149                         isIndexable =
2150                                 (opfamily == BPCHAR_PATTERN_BTREE_FAM_OID) ||
2151                                 (opfamily == BPCHAR_BTREE_FAM_OID && lc_collate_is_c());
2152                         break;
2153
2154                 case OID_NAME_LIKE_OP:
2155                 case OID_NAME_ICLIKE_OP:
2156                 case OID_NAME_REGEXEQ_OP:
2157                 case OID_NAME_ICREGEXEQ_OP:
2158                         isIndexable =
2159                                 (opfamily == NAME_PATTERN_BTREE_FAM_OID) ||
2160                                 (opfamily == NAME_BTREE_FAM_OID && lc_collate_is_c());
2161                         break;
2162
2163                 case OID_BYTEA_LIKE_OP:
2164                         isIndexable = (opfamily == BYTEA_BTREE_FAM_OID);
2165                         break;
2166
2167                 case OID_INET_SUB_OP:
2168                 case OID_INET_SUBEQ_OP:
2169                         isIndexable = (opfamily == NETWORK_BTREE_FAM_OID);
2170                         break;
2171         }
2172
2173         return isIndexable;
2174 }
2175
2176 /*
2177  * expand_indexqual_conditions
2178  *        Given a list of sublists of RestrictInfo nodes, produce a flat list
2179  *        of index qual clauses.  Standard qual clauses (those in the index's
2180  *        opfamily) are passed through unchanged.  Boolean clauses and "special"
2181  *        index operators are expanded into clauses that the indexscan machinery
2182  *        will know what to do with.  RowCompare clauses are simplified if
2183  *        necessary to create a clause that is fully checkable by the index.
2184  *
2185  * The input list is ordered by index key, and so the output list is too.
2186  * (The latter is not depended on by any part of the core planner, I believe,
2187  * but parts of the executor require it, and so do the amcostestimate
2188  * functions.)
2189  */
2190 List *
2191 expand_indexqual_conditions(IndexOptInfo *index, List *clausegroups)
2192 {
2193         List       *resultquals = NIL;
2194         ListCell   *clausegroup_item;
2195         int                     indexcol = 0;
2196         Oid                *families = index->opfamily;
2197
2198         if (clausegroups == NIL)
2199                 return NIL;
2200
2201         clausegroup_item = list_head(clausegroups);
2202         do
2203         {
2204                 Oid                     curFamily = families[0];
2205                 ListCell   *l;
2206
2207                 foreach(l, (List *) lfirst(clausegroup_item))
2208                 {
2209                         RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
2210                         Expr       *clause = rinfo->clause;
2211
2212                         /* First check for boolean cases */
2213                         if (IsBooleanOpfamily(curFamily))
2214                         {
2215                                 Expr       *boolqual;
2216
2217                                 boolqual = expand_boolean_index_clause((Node *) clause,
2218                                                                                                            indexcol,
2219                                                                                                            index);
2220                                 if (boolqual)
2221                                 {
2222                                         resultquals = lappend(resultquals,
2223                                                                                   make_restrictinfo(boolqual,
2224                                                                                                                         true,
2225                                                                                                                         false,
2226                                                                                                                         false,
2227                                                                                                                         NULL));
2228                                         continue;
2229                                 }
2230                         }
2231
2232                         /*
2233                          * Else it must be an opclause (usual case), ScalarArrayOp, or
2234                          * RowCompare
2235                          */
2236                         if (is_opclause(clause))
2237                         {
2238                                 resultquals = list_concat(resultquals,
2239                                                                                   expand_indexqual_opclause(rinfo,
2240                                                                                                                                   curFamily));
2241                         }
2242                         else if (IsA(clause, ScalarArrayOpExpr))
2243                         {
2244                                 /* no extra work at this time */
2245                                 resultquals = lappend(resultquals, rinfo);
2246                         }
2247                         else if (IsA(clause, RowCompareExpr))
2248                         {
2249                                 resultquals = lappend(resultquals,
2250                                                                           expand_indexqual_rowcompare(rinfo,
2251                                                                                                                                   index,
2252                                                                                                                                   indexcol));
2253                         }
2254                         else
2255                                 elog(ERROR, "unsupported indexqual type: %d",
2256                                          (int) nodeTag(clause));
2257                 }
2258
2259                 clausegroup_item = lnext(clausegroup_item);
2260
2261                 indexcol++;
2262                 families++;
2263         } while (clausegroup_item != NULL && !DoneMatchingIndexKeys(families));
2264
2265         Assert(clausegroup_item == NULL);       /* else more groups than indexkeys */
2266
2267         return resultquals;
2268 }
2269
2270 /*
2271  * expand_boolean_index_clause
2272  *        Convert a clause recognized by match_boolean_index_clause into
2273  *        a boolean equality operator clause.
2274  *
2275  * Returns NULL if the clause isn't a boolean index qual.
2276  */
2277 static Expr *
2278 expand_boolean_index_clause(Node *clause,
2279                                                         int indexcol,
2280                                                         IndexOptInfo *index)
2281 {
2282         /* Direct match? */
2283         if (match_index_to_operand(clause, indexcol, index))
2284         {
2285                 /* convert to indexkey = TRUE */
2286                 return make_opclause(BooleanEqualOperator, BOOLOID, false,
2287                                                          (Expr *) clause,
2288                                                          (Expr *) makeBoolConst(true, false));
2289         }
2290         /* NOT clause? */
2291         if (not_clause(clause))
2292         {
2293                 Node       *arg = (Node *) get_notclausearg((Expr *) clause);
2294
2295                 /* It must have matched the indexkey */
2296                 Assert(match_index_to_operand(arg, indexcol, index));
2297                 /* convert to indexkey = FALSE */
2298                 return make_opclause(BooleanEqualOperator, BOOLOID, false,
2299                                                          (Expr *) arg,
2300                                                          (Expr *) makeBoolConst(false, false));
2301         }
2302         if (clause && IsA(clause, BooleanTest))
2303         {
2304                 BooleanTest *btest = (BooleanTest *) clause;
2305                 Node       *arg = (Node *) btest->arg;
2306
2307                 /* It must have matched the indexkey */
2308                 Assert(match_index_to_operand(arg, indexcol, index));
2309                 if (btest->booltesttype == IS_TRUE)
2310                 {
2311                         /* convert to indexkey = TRUE */
2312                         return make_opclause(BooleanEqualOperator, BOOLOID, false,
2313                                                                  (Expr *) arg,
2314                                                                  (Expr *) makeBoolConst(true, false));
2315                 }
2316                 if (btest->booltesttype == IS_FALSE)
2317                 {
2318                         /* convert to indexkey = FALSE */
2319                         return make_opclause(BooleanEqualOperator, BOOLOID, false,
2320                                                                  (Expr *) arg,
2321                                                                  (Expr *) makeBoolConst(false, false));
2322                 }
2323                 /* Oops */
2324                 Assert(false);
2325         }
2326
2327         return NULL;
2328 }
2329
2330 /*
2331  * expand_indexqual_opclause --- expand a single indexqual condition
2332  *              that is an operator clause
2333  *
2334  * The input is a single RestrictInfo, the output a list of RestrictInfos
2335  */
2336 static List *
2337 expand_indexqual_opclause(RestrictInfo *rinfo, Oid opfamily)
2338 {
2339         Expr       *clause = rinfo->clause;
2340
2341         /* we know these will succeed */
2342         Node       *leftop = get_leftop(clause);
2343         Node       *rightop = get_rightop(clause);
2344         Oid                     expr_op = ((OpExpr *) clause)->opno;
2345         Const      *patt = (Const *) rightop;
2346         Const      *prefix = NULL;
2347         Const      *rest = NULL;
2348         Pattern_Prefix_Status pstatus;
2349         List       *result;
2350
2351         switch (expr_op)
2352         {
2353                         /*
2354                          * LIKE and regex operators are not members of any index opfamily,
2355                          * so if we find one in an indexqual list we can assume that it
2356                          * was accepted by match_special_index_operator().
2357                          */
2358                 case OID_TEXT_LIKE_OP:
2359                 case OID_BPCHAR_LIKE_OP:
2360                 case OID_NAME_LIKE_OP:
2361                 case OID_BYTEA_LIKE_OP:
2362                         pstatus = pattern_fixed_prefix(patt, Pattern_Type_Like,
2363                                                                                    &prefix, &rest);
2364                         result = prefix_quals(leftop, opfamily, prefix, pstatus);
2365                         break;
2366
2367                 case OID_TEXT_ICLIKE_OP:
2368                 case OID_BPCHAR_ICLIKE_OP:
2369                 case OID_NAME_ICLIKE_OP:
2370                         /* the right-hand const is type text for all of these */
2371                         pstatus = pattern_fixed_prefix(patt, Pattern_Type_Like_IC,
2372                                                                                    &prefix, &rest);
2373                         result = prefix_quals(leftop, opfamily, prefix, pstatus);
2374                         break;
2375
2376                 case OID_TEXT_REGEXEQ_OP:
2377                 case OID_BPCHAR_REGEXEQ_OP:
2378                 case OID_NAME_REGEXEQ_OP:
2379                         /* the right-hand const is type text for all of these */
2380                         pstatus = pattern_fixed_prefix(patt, Pattern_Type_Regex,
2381                                                                                    &prefix, &rest);
2382                         result = prefix_quals(leftop, opfamily, prefix, pstatus);
2383                         break;
2384
2385                 case OID_TEXT_ICREGEXEQ_OP:
2386                 case OID_BPCHAR_ICREGEXEQ_OP:
2387                 case OID_NAME_ICREGEXEQ_OP:
2388                         /* the right-hand const is type text for all of these */
2389                         pstatus = pattern_fixed_prefix(patt, Pattern_Type_Regex_IC,
2390                                                                                    &prefix, &rest);
2391                         result = prefix_quals(leftop, opfamily, prefix, pstatus);
2392                         break;
2393
2394                 case OID_INET_SUB_OP:
2395                 case OID_INET_SUBEQ_OP:
2396                         result = network_prefix_quals(leftop, expr_op, opfamily,
2397                                                                                   patt->constvalue);
2398                         break;
2399
2400                 default:
2401                         result = list_make1(rinfo);
2402                         break;
2403         }
2404
2405         return result;
2406 }
2407
2408 /*
2409  * expand_indexqual_rowcompare --- expand a single indexqual condition
2410  *              that is a RowCompareExpr
2411  *
2412  * It's already known that the first column of the row comparison matches
2413  * the specified column of the index.  We can use additional columns of the
2414  * row comparison as index qualifications, so long as they match the index
2415  * in the "same direction", ie, the indexkeys are all on the same side of the
2416  * clause and the operators are all the same-type members of the opfamilies.
2417  * If all the columns of the RowCompareExpr match in this way, we just use it
2418  * as-is.  Otherwise, we build a shortened RowCompareExpr (if more than one
2419  * column matches) or a simple OpExpr (if the first-column match is all
2420  * there is).  In these cases the modified clause is always "<=" or ">="
2421  * even when the original was "<" or ">" --- this is necessary to match all
2422  * the rows that could match the original.      (We are essentially building a
2423  * lossy version of the row comparison when we do this.)
2424  */
2425 static RestrictInfo *
2426 expand_indexqual_rowcompare(RestrictInfo *rinfo,
2427                                                         IndexOptInfo *index,
2428                                                         int indexcol)
2429 {
2430         RowCompareExpr *clause = (RowCompareExpr *) rinfo->clause;
2431         bool            var_on_left;
2432         int                     op_strategy;
2433         Oid                     op_lefttype;
2434         Oid                     op_righttype;
2435         bool            op_recheck;
2436         int                     matching_cols;
2437         Oid                     expr_op;
2438         List       *opfamilies;
2439         List       *lefttypes;
2440         List       *righttypes;
2441         List       *new_ops;
2442         ListCell   *largs_cell;
2443         ListCell   *rargs_cell;
2444         ListCell   *opnos_cell;
2445
2446         /* We have to figure out (again) how the first col matches */
2447         var_on_left = match_index_to_operand((Node *) linitial(clause->largs),
2448                                                                                  indexcol, index);
2449         Assert(var_on_left ||
2450                    match_index_to_operand((Node *) linitial(clause->rargs),
2451                                                                   indexcol, index));
2452         expr_op = linitial_oid(clause->opnos);
2453         if (!var_on_left)
2454                 expr_op = get_commutator(expr_op);
2455         get_op_opfamily_properties(expr_op, index->opfamily[indexcol],
2456                                                            &op_strategy,
2457                                                            &op_lefttype,
2458                                                            &op_righttype,
2459                                                            &op_recheck);
2460         /* Build lists of the opfamilies and operator datatypes in case needed */
2461         opfamilies = list_make1_oid(index->opfamily[indexcol]);
2462         lefttypes = list_make1_oid(op_lefttype);
2463         righttypes = list_make1_oid(op_righttype);
2464
2465         /*
2466          * See how many of the remaining columns match some index column in the
2467          * same way.  A note about rel membership tests: we assume that the clause
2468          * as a whole is already known to use only Vars from the indexed relation
2469          * and possibly some acceptable outer relations. So the "other" side of
2470          * any potential index condition is OK as long as it doesn't use Vars from
2471          * the indexed relation.
2472          */
2473         matching_cols = 1;
2474         largs_cell = lnext(list_head(clause->largs));
2475         rargs_cell = lnext(list_head(clause->rargs));
2476         opnos_cell = lnext(list_head(clause->opnos));
2477
2478         while (largs_cell != NULL)
2479         {
2480                 Node       *varop;
2481                 Node       *constop;
2482                 int                     i;
2483
2484                 expr_op = lfirst_oid(opnos_cell);
2485                 if (var_on_left)
2486                 {
2487                         varop = (Node *) lfirst(largs_cell);
2488                         constop = (Node *) lfirst(rargs_cell);
2489                 }
2490                 else
2491                 {
2492                         varop = (Node *) lfirst(rargs_cell);
2493                         constop = (Node *) lfirst(largs_cell);
2494                         /* indexkey is on right, so commute the operator */
2495                         expr_op = get_commutator(expr_op);
2496                         if (expr_op == InvalidOid)
2497                                 break;                  /* operator is not usable */
2498                 }
2499                 if (bms_is_member(index->rel->relid, pull_varnos(constop)))
2500                         break;                          /* no good, Var on wrong side */
2501                 if (contain_volatile_functions(constop))
2502                         break;                          /* no good, volatile comparison value */
2503
2504                 /*
2505                  * The Var side can match any column of the index.      If the user does
2506                  * something weird like having multiple identical index columns, we
2507                  * insist the match be on the first such column, to avoid confusing
2508                  * the executor.
2509                  */
2510                 for (i = 0; i < index->ncolumns; i++)
2511                 {
2512                         if (match_index_to_operand(varop, i, index))
2513                                 break;
2514                 }
2515                 if (i >= index->ncolumns)
2516                         break;                          /* no match found */
2517
2518                 /* Now, do we have the right operator for this column? */
2519                 if (get_op_opfamily_strategy(expr_op, index->opfamily[i])
2520                         != op_strategy)
2521                         break;
2522
2523                 /* Add opfamily and datatypes to lists */
2524                 get_op_opfamily_properties(expr_op, index->opfamily[i],
2525                                                                    &op_strategy,
2526                                                                    &op_lefttype,
2527                                                                    &op_righttype,
2528                                                                    &op_recheck);
2529                 opfamilies = lappend_oid(opfamilies, index->opfamily[i]);
2530                 lefttypes = lappend_oid(lefttypes, op_lefttype);
2531                 righttypes = lappend_oid(righttypes, op_righttype);
2532
2533                 /* This column matches, keep scanning */
2534                 matching_cols++;
2535                 largs_cell = lnext(largs_cell);
2536                 rargs_cell = lnext(rargs_cell);
2537                 opnos_cell = lnext(opnos_cell);
2538         }
2539
2540         /* Return clause as-is if it's all usable as index quals */
2541         if (matching_cols == list_length(clause->opnos))
2542                 return rinfo;
2543
2544         /*
2545          * We have to generate a subset rowcompare (possibly just one OpExpr). The
2546          * painful part of this is changing < to <= or > to >=, so deal with that
2547          * first.
2548          */
2549         if (op_strategy == BTLessEqualStrategyNumber ||
2550                 op_strategy == BTGreaterEqualStrategyNumber)
2551         {
2552                 /* easy, just use the same operators */
2553                 new_ops = list_truncate(list_copy(clause->opnos), matching_cols);
2554         }
2555         else
2556         {
2557                 ListCell   *opfamilies_cell;
2558                 ListCell   *lefttypes_cell;
2559                 ListCell   *righttypes_cell;
2560
2561                 if (op_strategy == BTLessStrategyNumber)
2562                         op_strategy = BTLessEqualStrategyNumber;
2563                 else if (op_strategy == BTGreaterStrategyNumber)
2564                         op_strategy = BTGreaterEqualStrategyNumber;
2565                 else
2566                         elog(ERROR, "unexpected strategy number %d", op_strategy);
2567                 new_ops = NIL;
2568                 lefttypes_cell = list_head(lefttypes);
2569                 righttypes_cell = list_head(righttypes);
2570                 foreach(opfamilies_cell, opfamilies)
2571                 {
2572                         Oid             opfam = lfirst_oid(opfamilies_cell);
2573                         Oid             lefttype = lfirst_oid(lefttypes_cell);
2574                         Oid             righttype = lfirst_oid(righttypes_cell);
2575
2576                         expr_op = get_opfamily_member(opfam, lefttype, righttype,
2577                                                                                   op_strategy);
2578                         if (!OidIsValid(expr_op))       /* should not happen */
2579                                 elog(ERROR, "could not find member %d(%u,%u) of opfamily %u",
2580                                          op_strategy, lefttype, righttype, opfam);
2581                         if (!var_on_left)
2582                         {
2583                                 expr_op = get_commutator(expr_op);
2584                                 if (!OidIsValid(expr_op))               /* should not happen */
2585                                         elog(ERROR, "could not find commutator of member %d(%u,%u) of opfamily %u",
2586                                                  op_strategy, lefttype, righttype, opfam);
2587                         }
2588                         new_ops = lappend_oid(new_ops, expr_op);
2589                 }
2590                 lefttypes_cell = lnext(lefttypes_cell);
2591                 righttypes_cell = lnext(righttypes_cell);
2592         }
2593
2594         /* If we have more than one matching col, create a subset rowcompare */
2595         if (matching_cols > 1)
2596         {
2597                 RowCompareExpr *rc = makeNode(RowCompareExpr);
2598
2599                 if (var_on_left)
2600                         rc->rctype = (RowCompareType) op_strategy;
2601                 else
2602                         rc->rctype = (op_strategy == BTLessEqualStrategyNumber) ?
2603                                 ROWCOMPARE_GE : ROWCOMPARE_LE;
2604                 rc->opnos = new_ops;
2605                 rc->opfamilies = list_truncate(list_copy(clause->opfamilies),
2606                                                                            matching_cols);
2607                 rc->largs = list_truncate((List *) copyObject(clause->largs),
2608                                                                   matching_cols);
2609                 rc->rargs = list_truncate((List *) copyObject(clause->rargs),
2610                                                                   matching_cols);
2611                 return make_restrictinfo((Expr *) rc, true, false, false, NULL);
2612         }
2613         else
2614         {
2615                 Expr       *opexpr;
2616
2617                 opexpr = make_opclause(linitial_oid(new_ops), BOOLOID, false,
2618                                                            copyObject(linitial(clause->largs)),
2619                                                            copyObject(linitial(clause->rargs)));
2620                 return make_restrictinfo(opexpr, true, false, false, NULL);
2621         }
2622 }
2623
2624 /*
2625  * Given a fixed prefix that all the "leftop" values must have,
2626  * generate suitable indexqual condition(s).  opfamily is the index
2627  * operator family; we use it to deduce the appropriate comparison
2628  * operators and operand datatypes.
2629  */
2630 static List *
2631 prefix_quals(Node *leftop, Oid opfamily,
2632                          Const *prefix_const, Pattern_Prefix_Status pstatus)
2633 {
2634         List       *result;
2635         Oid                     datatype;
2636         Oid                     oproid;
2637         Expr       *expr;
2638         Const      *greaterstr;
2639
2640         Assert(pstatus != Pattern_Prefix_None);
2641
2642         switch (opfamily)
2643         {
2644                 case TEXT_BTREE_FAM_OID:
2645                 case TEXT_PATTERN_BTREE_FAM_OID:
2646                         datatype = TEXTOID;
2647                         break;
2648
2649                 case BPCHAR_BTREE_FAM_OID:
2650                 case BPCHAR_PATTERN_BTREE_FAM_OID:
2651                         datatype = BPCHAROID;
2652                         break;
2653
2654                 case NAME_BTREE_FAM_OID:
2655                 case NAME_PATTERN_BTREE_FAM_OID:
2656                         datatype = NAMEOID;
2657                         break;
2658
2659                 case BYTEA_BTREE_FAM_OID:
2660                         datatype = BYTEAOID;
2661                         break;
2662
2663                 default:
2664                         /* shouldn't get here */
2665                         elog(ERROR, "unexpected opfamily: %u", opfamily);
2666                         return NIL;
2667         }
2668
2669         /*
2670          * If necessary, coerce the prefix constant to the right type. The given
2671          * prefix constant is either text or bytea type.
2672          */
2673         if (prefix_const->consttype != datatype)
2674         {
2675                 char       *prefix;
2676
2677                 switch (prefix_const->consttype)
2678                 {
2679                         case TEXTOID:
2680                                 prefix = DatumGetCString(DirectFunctionCall1(textout,
2681                                                                                                   prefix_const->constvalue));
2682                                 break;
2683                         case BYTEAOID:
2684                                 prefix = DatumGetCString(DirectFunctionCall1(byteaout,
2685                                                                                                   prefix_const->constvalue));
2686                                 break;
2687                         default:
2688                                 elog(ERROR, "unexpected const type: %u",
2689                                          prefix_const->consttype);
2690                                 return NIL;
2691                 }
2692                 prefix_const = string_to_const(prefix, datatype);
2693                 pfree(prefix);
2694         }
2695
2696         /*
2697          * If we found an exact-match pattern, generate an "=" indexqual.
2698          */
2699         if (pstatus == Pattern_Prefix_Exact)
2700         {
2701                 oproid = get_opfamily_member(opfamily, datatype, datatype,
2702                                                                          BTEqualStrategyNumber);
2703                 if (oproid == InvalidOid)
2704                         elog(ERROR, "no = operator for opfamily %u", opfamily);
2705                 expr = make_opclause(oproid, BOOLOID, false,
2706                                                          (Expr *) leftop, (Expr *) prefix_const);
2707                 result = list_make1(make_restrictinfo(expr, true, false, false, NULL));
2708                 return result;
2709         }
2710
2711         /*
2712          * Otherwise, we have a nonempty required prefix of the values.
2713          *
2714          * We can always say "x >= prefix".
2715          */
2716         oproid = get_opfamily_member(opfamily, datatype, datatype,
2717                                                                  BTGreaterEqualStrategyNumber);
2718         if (oproid == InvalidOid)
2719                 elog(ERROR, "no >= operator for opfamily %u", opfamily);
2720         expr = make_opclause(oproid, BOOLOID, false,
2721                                                  (Expr *) leftop, (Expr *) prefix_const);
2722         result = list_make1(make_restrictinfo(expr, true, false, false, NULL));
2723
2724         /*-------
2725          * If we can create a string larger than the prefix, we can say
2726          * "x < greaterstr".
2727          *-------
2728          */
2729         greaterstr = make_greater_string(prefix_const);
2730         if (greaterstr)
2731         {
2732                 oproid = get_opfamily_member(opfamily, datatype, datatype,
2733                                                                          BTLessStrategyNumber);
2734                 if (oproid == InvalidOid)
2735                         elog(ERROR, "no < operator for opfamily %u", opfamily);
2736                 expr = make_opclause(oproid, BOOLOID, false,
2737                                                          (Expr *) leftop, (Expr *) greaterstr);
2738                 result = lappend(result,
2739                                                  make_restrictinfo(expr, true, false, false, NULL));
2740         }
2741
2742         return result;
2743 }
2744
2745 /*
2746  * Given a leftop and a rightop, and a inet-family sup/sub operator,
2747  * generate suitable indexqual condition(s).  expr_op is the original
2748  * operator, and opfamily is the index opfamily.
2749  */
2750 static List *
2751 network_prefix_quals(Node *leftop, Oid expr_op, Oid opfamily, Datum rightop)
2752 {
2753         bool            is_eq;
2754         Oid                     datatype;
2755         Oid                     opr1oid;
2756         Oid                     opr2oid;
2757         Datum           opr1right;
2758         Datum           opr2right;
2759         List       *result;
2760         Expr       *expr;
2761
2762         switch (expr_op)
2763         {
2764                 case OID_INET_SUB_OP:
2765                         datatype = INETOID;
2766                         is_eq = false;
2767                         break;
2768                 case OID_INET_SUBEQ_OP:
2769                         datatype = INETOID;
2770                         is_eq = true;
2771                         break;
2772                 default:
2773                         elog(ERROR, "unexpected operator: %u", expr_op);
2774                         return NIL;
2775         }
2776
2777         /*
2778          * create clause "key >= network_scan_first( rightop )", or ">" if the
2779          * operator disallows equality.
2780          */
2781         if (is_eq)
2782         {
2783                 opr1oid = get_opfamily_member(opfamily, datatype, datatype,
2784                                                                           BTGreaterEqualStrategyNumber);
2785                 if (opr1oid == InvalidOid)
2786                         elog(ERROR, "no >= operator for opfamily %u", opfamily);
2787         }
2788         else
2789         {
2790                 opr1oid = get_opfamily_member(opfamily, datatype, datatype,
2791                                                                           BTGreaterStrategyNumber);
2792                 if (opr1oid == InvalidOid)
2793                         elog(ERROR, "no > operator for opfamily %u", opfamily);
2794         }
2795
2796         opr1right = network_scan_first(rightop);
2797
2798         expr = make_opclause(opr1oid, BOOLOID, false,
2799                                                  (Expr *) leftop,
2800                                                  (Expr *) makeConst(datatype, -1, opr1right,
2801                                                                                         false, false));
2802         result = list_make1(make_restrictinfo(expr, true, false, false, NULL));
2803
2804         /* create clause "key <= network_scan_last( rightop )" */
2805
2806         opr2oid = get_opfamily_member(opfamily, datatype, datatype,
2807                                                                   BTLessEqualStrategyNumber);
2808         if (opr2oid == InvalidOid)
2809                 elog(ERROR, "no <= operator for opfamily %u", opfamily);
2810
2811         opr2right = network_scan_last(rightop);
2812
2813         expr = make_opclause(opr2oid, BOOLOID, false,
2814                                                  (Expr *) leftop,
2815                                                  (Expr *) makeConst(datatype, -1, opr2right,
2816                                                                                         false, false));
2817         result = lappend(result,
2818                                          make_restrictinfo(expr, true, false, false, NULL));
2819
2820         return result;
2821 }
2822
2823 /*
2824  * Handy subroutines for match_special_index_operator() and friends.
2825  */
2826
2827 /*
2828  * Generate a Datum of the appropriate type from a C string.
2829  * Note that all of the supported types are pass-by-ref, so the
2830  * returned value should be pfree'd if no longer needed.
2831  */
2832 static Datum
2833 string_to_datum(const char *str, Oid datatype)
2834 {
2835         /*
2836          * We cheat a little by assuming that textin() will do for bpchar and
2837          * varchar constants too...
2838          */
2839         if (datatype == NAMEOID)
2840                 return DirectFunctionCall1(namein, CStringGetDatum(str));
2841         else if (datatype == BYTEAOID)
2842                 return DirectFunctionCall1(byteain, CStringGetDatum(str));
2843         else
2844                 return DirectFunctionCall1(textin, CStringGetDatum(str));
2845 }
2846
2847 /*
2848  * Generate a Const node of the appropriate type from a C string.
2849  */
2850 static Const *
2851 string_to_const(const char *str, Oid datatype)
2852 {
2853         Datum           conval = string_to_datum(str, datatype);
2854
2855         return makeConst(datatype, ((datatype == NAMEOID) ? NAMEDATALEN : -1),
2856                                          conval, false, false);
2857 }