Header And Logo

PostgreSQL
| The world's most advanced open source database.

indxpath.c

Go to the documentation of this file.
00001 /*-------------------------------------------------------------------------
00002  *
00003  * indxpath.c
00004  *    Routines to determine which indexes are usable for scanning a
00005  *    given relation, and create Paths accordingly.
00006  *
00007  * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
00008  * Portions Copyright (c) 1994, Regents of the University of California
00009  *
00010  *
00011  * IDENTIFICATION
00012  *    src/backend/optimizer/path/indxpath.c
00013  *
00014  *-------------------------------------------------------------------------
00015  */
00016 #include "postgres.h"
00017 
00018 #include <math.h>
00019 
00020 #include "access/skey.h"
00021 #include "access/sysattr.h"
00022 #include "catalog/pg_am.h"
00023 #include "catalog/pg_collation.h"
00024 #include "catalog/pg_operator.h"
00025 #include "catalog/pg_opfamily.h"
00026 #include "catalog/pg_type.h"
00027 #include "nodes/makefuncs.h"
00028 #include "optimizer/clauses.h"
00029 #include "optimizer/cost.h"
00030 #include "optimizer/pathnode.h"
00031 #include "optimizer/paths.h"
00032 #include "optimizer/predtest.h"
00033 #include "optimizer/restrictinfo.h"
00034 #include "optimizer/var.h"
00035 #include "utils/builtins.h"
00036 #include "utils/bytea.h"
00037 #include "utils/lsyscache.h"
00038 #include "utils/pg_locale.h"
00039 #include "utils/selfuncs.h"
00040 
00041 
00042 #define IsBooleanOpfamily(opfamily) \
00043     ((opfamily) == BOOL_BTREE_FAM_OID || (opfamily) == BOOL_HASH_FAM_OID)
00044 
00045 #define IndexCollMatchesExprColl(idxcollation, exprcollation) \
00046     ((idxcollation) == InvalidOid || (idxcollation) == (exprcollation))
00047 
00048 /* Whether to use ScalarArrayOpExpr to build index qualifications */
00049 typedef enum
00050 {
00051     SAOP_PER_AM,                /* Use ScalarArrayOpExpr if amsearcharray */
00052     SAOP_ALLOW,                 /* Use ScalarArrayOpExpr for all indexes */
00053     SAOP_REQUIRE                /* Require ScalarArrayOpExpr to be used */
00054 } SaOpControl;
00055 
00056 /* Whether we are looking for plain indexscan, bitmap scan, or either */
00057 typedef enum
00058 {
00059     ST_INDEXSCAN,               /* must support amgettuple */
00060     ST_BITMAPSCAN,              /* must support amgetbitmap */
00061     ST_ANYSCAN                  /* either is okay */
00062 } ScanTypeControl;
00063 
00064 /* Data structure for collecting qual clauses that match an index */
00065 typedef struct
00066 {
00067     bool        nonempty;       /* True if lists are not all empty */
00068     /* Lists of RestrictInfos, one per index column */
00069     List       *indexclauses[INDEX_MAX_KEYS];
00070 } IndexClauseSet;
00071 
00072 /* Per-path data used within choose_bitmap_and() */
00073 typedef struct
00074 {
00075     Path       *path;           /* IndexPath, BitmapAndPath, or BitmapOrPath */
00076     List       *quals;          /* the WHERE clauses it uses */
00077     List       *preds;          /* predicates of its partial index(es) */
00078     Bitmapset  *clauseids;      /* quals+preds represented as a bitmapset */
00079 } PathClauseUsage;
00080 
00081 /* Callback argument for ec_member_matches_indexcol */
00082 typedef struct
00083 {
00084     IndexOptInfo *index;        /* index we're considering */
00085     int         indexcol;       /* index column we want to match to */
00086 } ec_member_matches_arg;
00087 
00088 
00089 static void consider_index_join_clauses(PlannerInfo *root, RelOptInfo *rel,
00090                             IndexOptInfo *index,
00091                             IndexClauseSet *rclauseset,
00092                             IndexClauseSet *jclauseset,
00093                             IndexClauseSet *eclauseset,
00094                             List **bitindexpaths);
00095 static void consider_index_join_outer_rels(PlannerInfo *root, RelOptInfo *rel,
00096                                IndexOptInfo *index,
00097                                IndexClauseSet *rclauseset,
00098                                IndexClauseSet *jclauseset,
00099                                IndexClauseSet *eclauseset,
00100                                List **bitindexpaths,
00101                                List *indexjoinclauses,
00102                                int considered_clauses,
00103                                List **considered_relids);
00104 static void get_join_index_paths(PlannerInfo *root, RelOptInfo *rel,
00105                      IndexOptInfo *index,
00106                      IndexClauseSet *rclauseset,
00107                      IndexClauseSet *jclauseset,
00108                      IndexClauseSet *eclauseset,
00109                      List **bitindexpaths,
00110                      Relids relids,
00111                      List **considered_relids);
00112 static bool eclass_already_used(EquivalenceClass *parent_ec, Relids oldrelids,
00113                     List *indexjoinclauses);
00114 static bool bms_equal_any(Relids relids, List *relids_list);
00115 static void get_index_paths(PlannerInfo *root, RelOptInfo *rel,
00116                 IndexOptInfo *index, IndexClauseSet *clauses,
00117                 List **bitindexpaths);
00118 static List *build_index_paths(PlannerInfo *root, RelOptInfo *rel,
00119                   IndexOptInfo *index, IndexClauseSet *clauses,
00120                   bool useful_predicate,
00121                   SaOpControl saop_control, ScanTypeControl scantype);
00122 static List *build_paths_for_OR(PlannerInfo *root, RelOptInfo *rel,
00123                    List *clauses, List *other_clauses);
00124 static List *drop_indexable_join_clauses(RelOptInfo *rel, List *clauses);
00125 static Path *choose_bitmap_and(PlannerInfo *root, RelOptInfo *rel,
00126                   List *paths);
00127 static int  path_usage_comparator(const void *a, const void *b);
00128 static Cost bitmap_scan_cost_est(PlannerInfo *root, RelOptInfo *rel,
00129                      Path *ipath);
00130 static Cost bitmap_and_cost_est(PlannerInfo *root, RelOptInfo *rel,
00131                     List *paths);
00132 static PathClauseUsage *classify_index_clause_usage(Path *path,
00133                             List **clauselist);
00134 static Relids get_bitmap_tree_required_outer(Path *bitmapqual);
00135 static void find_indexpath_quals(Path *bitmapqual, List **quals, List **preds);
00136 static int  find_list_position(Node *node, List **nodelist);
00137 static bool check_index_only(RelOptInfo *rel, IndexOptInfo *index);
00138 static double get_loop_count(PlannerInfo *root, Relids outer_relids);
00139 static void match_restriction_clauses_to_index(RelOptInfo *rel,
00140                                    IndexOptInfo *index,
00141                                    IndexClauseSet *clauseset);
00142 static void match_join_clauses_to_index(PlannerInfo *root,
00143                             RelOptInfo *rel, IndexOptInfo *index,
00144                             Relids lateral_referencers,
00145                             IndexClauseSet *clauseset,
00146                             List **joinorclauses);
00147 static void match_eclass_clauses_to_index(PlannerInfo *root,
00148                               IndexOptInfo *index,
00149                               Relids lateral_referencers,
00150                               IndexClauseSet *clauseset);
00151 static void match_clauses_to_index(IndexOptInfo *index,
00152                        List *clauses,
00153                        IndexClauseSet *clauseset);
00154 static void match_clause_to_index(IndexOptInfo *index,
00155                       RestrictInfo *rinfo,
00156                       IndexClauseSet *clauseset);
00157 static bool match_clause_to_indexcol(IndexOptInfo *index,
00158                          int indexcol,
00159                          RestrictInfo *rinfo);
00160 static bool is_indexable_operator(Oid expr_op, Oid opfamily,
00161                       bool indexkey_on_left);
00162 static bool match_rowcompare_to_indexcol(IndexOptInfo *index,
00163                              int indexcol,
00164                              Oid opfamily,
00165                              Oid idxcollation,
00166                              RowCompareExpr *clause);
00167 static void match_pathkeys_to_index(IndexOptInfo *index, List *pathkeys,
00168                         List **orderby_clauses_p,
00169                         List **clause_columns_p);
00170 static Expr *match_clause_to_ordering_op(IndexOptInfo *index,
00171                             int indexcol, Expr *clause, Oid pk_opfamily);
00172 static bool ec_member_matches_indexcol(PlannerInfo *root, RelOptInfo *rel,
00173                            EquivalenceClass *ec, EquivalenceMember *em,
00174                            void *arg);
00175 static bool match_boolean_index_clause(Node *clause, int indexcol,
00176                            IndexOptInfo *index);
00177 static bool match_special_index_operator(Expr *clause,
00178                              Oid opfamily, Oid idxcollation,
00179                              bool indexkey_on_left);
00180 static Expr *expand_boolean_index_clause(Node *clause, int indexcol,
00181                             IndexOptInfo *index);
00182 static List *expand_indexqual_opclause(RestrictInfo *rinfo,
00183                           Oid opfamily, Oid idxcollation);
00184 static RestrictInfo *expand_indexqual_rowcompare(RestrictInfo *rinfo,
00185                             IndexOptInfo *index,
00186                             int indexcol);
00187 static List *prefix_quals(Node *leftop, Oid opfamily, Oid collation,
00188              Const *prefix, Pattern_Prefix_Status pstatus);
00189 static List *network_prefix_quals(Node *leftop, Oid expr_op, Oid opfamily,
00190                      Datum rightop);
00191 static Datum string_to_datum(const char *str, Oid datatype);
00192 static Const *string_to_const(const char *str, Oid datatype);
00193 
00194 
00195 /*
00196  * create_index_paths()
00197  *    Generate all interesting index paths for the given relation.
00198  *    Candidate paths are added to the rel's pathlist (using add_path).
00199  *
00200  * To be considered for an index scan, an index must match one or more
00201  * restriction clauses or join clauses from the query's qual condition,
00202  * or match the query's ORDER BY condition, or have a predicate that
00203  * matches the query's qual condition.
00204  *
00205  * There are two basic kinds of index scans.  A "plain" index scan uses
00206  * only restriction clauses (possibly none at all) in its indexqual,
00207  * so it can be applied in any context.  A "parameterized" index scan uses
00208  * join clauses (plus restriction clauses, if available) in its indexqual.
00209  * When joining such a scan to one of the relations supplying the other
00210  * variables used in its indexqual, the parameterized scan must appear as
00211  * the inner relation of a nestloop join; it can't be used on the outer side,
00212  * nor in a merge or hash join.  In that context, values for the other rels'
00213  * attributes are available and fixed during any one scan of the indexpath.
00214  *
00215  * An IndexPath is generated and submitted to add_path() for each plain or
00216  * parameterized index scan this routine deems potentially interesting for
00217  * the current query.
00218  *
00219  * 'rel' is the relation for which we want to generate index paths
00220  *
00221  * Note: check_partial_indexes() must have been run previously for this rel.
00222  *
00223  * Note: in corner cases involving LATERAL appendrel children, it's possible
00224  * that rel->lateral_relids is nonempty.  Currently, we include lateral_relids
00225  * into the parameterization reported for each path, but don't take it into
00226  * account otherwise.  The fact that any such rels *must* be available as
00227  * parameter sources perhaps should influence our choices of index quals ...
00228  * but for now, it doesn't seem worth troubling over.  In particular, comments
00229  * below about "unparameterized" paths should be read as meaning
00230  * "unparameterized so far as the indexquals are concerned".
00231  */
00232 void
00233 create_index_paths(PlannerInfo *root, RelOptInfo *rel)
00234 {
00235     List       *indexpaths;
00236     List       *bitindexpaths;
00237     List       *bitjoinpaths;
00238     List       *joinorclauses;
00239     Relids      lateral_referencers;
00240     IndexClauseSet rclauseset;
00241     IndexClauseSet jclauseset;
00242     IndexClauseSet eclauseset;
00243     ListCell   *lc;
00244 
00245     /* Skip the whole mess if no indexes */
00246     if (rel->indexlist == NIL)
00247         return;
00248 
00249     /*
00250      * If there are any rels that have LATERAL references to this one, we
00251      * cannot use join quals referencing them as index quals for this one,
00252      * since such rels would have to be on the inside not the outside of a
00253      * nestloop join relative to this one.  Create a Relids set listing all
00254      * such rels, for use in checks of potential join clauses.
00255      */
00256     lateral_referencers = NULL;
00257     foreach(lc, root->lateral_info_list)
00258     {
00259         LateralJoinInfo *ljinfo = (LateralJoinInfo *) lfirst(lc);
00260 
00261         if (bms_is_member(rel->relid, ljinfo->lateral_lhs))
00262             lateral_referencers = bms_add_member(lateral_referencers,
00263                                                  ljinfo->lateral_rhs);
00264     }
00265 
00266     /* Bitmap paths are collected and then dealt with at the end */
00267     bitindexpaths = bitjoinpaths = joinorclauses = NIL;
00268 
00269     /* Examine each index in turn */
00270     foreach(lc, rel->indexlist)
00271     {
00272         IndexOptInfo *index = (IndexOptInfo *) lfirst(lc);
00273 
00274         /* Protect limited-size array in IndexClauseSets */
00275         Assert(index->ncolumns <= INDEX_MAX_KEYS);
00276 
00277         /*
00278          * Ignore partial indexes that do not match the query.
00279          * (generate_bitmap_or_paths() might be able to do something with
00280          * them, but that's of no concern here.)
00281          */
00282         if (index->indpred != NIL && !index->predOK)
00283             continue;
00284 
00285         /*
00286          * Identify the restriction clauses that can match the index.
00287          */
00288         MemSet(&rclauseset, 0, sizeof(rclauseset));
00289         match_restriction_clauses_to_index(rel, index, &rclauseset);
00290 
00291         /*
00292          * Build index paths from the restriction clauses.  These will be
00293          * non-parameterized paths.  Plain paths go directly to add_path(),
00294          * bitmap paths are added to bitindexpaths to be handled below.
00295          */
00296         get_index_paths(root, rel, index, &rclauseset,
00297                         &bitindexpaths);
00298 
00299         /*
00300          * Identify the join clauses that can match the index.  For the moment
00301          * we keep them separate from the restriction clauses.  Note that this
00302          * step finds only "loose" join clauses that have not been merged into
00303          * EquivalenceClasses.  Also, collect join OR clauses for later.
00304          */
00305         MemSet(&jclauseset, 0, sizeof(jclauseset));
00306         match_join_clauses_to_index(root, rel, index, lateral_referencers,
00307                                     &jclauseset, &joinorclauses);
00308 
00309         /*
00310          * Look for EquivalenceClasses that can generate joinclauses matching
00311          * the index.
00312          */
00313         MemSet(&eclauseset, 0, sizeof(eclauseset));
00314         match_eclass_clauses_to_index(root, index, lateral_referencers,
00315                                       &eclauseset);
00316 
00317         /*
00318          * If we found any plain or eclass join clauses, build parameterized
00319          * index paths using them.
00320          */
00321         if (jclauseset.nonempty || eclauseset.nonempty)
00322             consider_index_join_clauses(root, rel, index,
00323                                         &rclauseset,
00324                                         &jclauseset,
00325                                         &eclauseset,
00326                                         &bitjoinpaths);
00327     }
00328 
00329     /*
00330      * Generate BitmapOrPaths for any suitable OR-clauses present in the
00331      * restriction list.  Add these to bitindexpaths.
00332      */
00333     indexpaths = generate_bitmap_or_paths(root, rel,
00334                                           rel->baserestrictinfo, NIL,
00335                                           false);
00336     bitindexpaths = list_concat(bitindexpaths, indexpaths);
00337 
00338     /*
00339      * Likewise, generate BitmapOrPaths for any suitable OR-clauses present in
00340      * the joinclause list.  Add these to bitjoinpaths.
00341      */
00342     indexpaths = generate_bitmap_or_paths(root, rel,
00343                                         joinorclauses, rel->baserestrictinfo,
00344                                           false);
00345     bitjoinpaths = list_concat(bitjoinpaths, indexpaths);
00346 
00347     /*
00348      * If we found anything usable, generate a BitmapHeapPath for the most
00349      * promising combination of restriction bitmap index paths.  Note there
00350      * will be only one such path no matter how many indexes exist.  This
00351      * should be sufficient since there's basically only one figure of merit
00352      * (total cost) for such a path.
00353      */
00354     if (bitindexpaths != NIL)
00355     {
00356         Path       *bitmapqual;
00357         BitmapHeapPath *bpath;
00358 
00359         bitmapqual = choose_bitmap_and(root, rel, bitindexpaths);
00360         bpath = create_bitmap_heap_path(root, rel, bitmapqual,
00361                                         rel->lateral_relids, 1.0);
00362         add_path(rel, (Path *) bpath);
00363     }
00364 
00365     /*
00366      * Likewise, if we found anything usable, generate BitmapHeapPaths for the
00367      * most promising combinations of join bitmap index paths.  Our strategy
00368      * is to generate one such path for each distinct parameterization seen
00369      * among the available bitmap index paths.  This may look pretty
00370      * expensive, but usually there won't be very many distinct
00371      * parameterizations.  (This logic is quite similar to that in
00372      * consider_index_join_clauses, but we're working with whole paths not
00373      * individual clauses.)
00374      */
00375     if (bitjoinpaths != NIL)
00376     {
00377         List       *path_outer;
00378         List       *all_path_outers;
00379         ListCell   *lc;
00380 
00381         /*
00382          * path_outer holds the parameterization of each path in bitjoinpaths
00383          * (to save recalculating that several times), while all_path_outers
00384          * holds all distinct parameterization sets.
00385          */
00386         path_outer = all_path_outers = NIL;
00387         foreach(lc, bitjoinpaths)
00388         {
00389             Path       *path = (Path *) lfirst(lc);
00390             Relids      required_outer;
00391 
00392             required_outer = get_bitmap_tree_required_outer(path);
00393             path_outer = lappend(path_outer, required_outer);
00394             if (!bms_equal_any(required_outer, all_path_outers))
00395                 all_path_outers = lappend(all_path_outers, required_outer);
00396         }
00397 
00398         /* Now, for each distinct parameterization set ... */
00399         foreach(lc, all_path_outers)
00400         {
00401             Relids      max_outers = (Relids) lfirst(lc);
00402             List       *this_path_set;
00403             Path       *bitmapqual;
00404             Relids      required_outer;
00405             double      loop_count;
00406             BitmapHeapPath *bpath;
00407             ListCell   *lcp;
00408             ListCell   *lco;
00409 
00410             /* Identify all the bitmap join paths needing no more than that */
00411             this_path_set = NIL;
00412             forboth(lcp, bitjoinpaths, lco, path_outer)
00413             {
00414                 Path       *path = (Path *) lfirst(lcp);
00415                 Relids      p_outers = (Relids) lfirst(lco);
00416 
00417                 if (bms_is_subset(p_outers, max_outers))
00418                     this_path_set = lappend(this_path_set, path);
00419             }
00420 
00421             /*
00422              * Add in restriction bitmap paths, since they can be used
00423              * together with any join paths.
00424              */
00425             this_path_set = list_concat(this_path_set, bitindexpaths);
00426 
00427             /* Select best AND combination for this parameterization */
00428             bitmapqual = choose_bitmap_and(root, rel, this_path_set);
00429 
00430             /* And push that path into the mix */
00431             required_outer = get_bitmap_tree_required_outer(bitmapqual);
00432             loop_count = get_loop_count(root, required_outer);
00433             bpath = create_bitmap_heap_path(root, rel, bitmapqual,
00434                                             required_outer, loop_count);
00435             add_path(rel, (Path *) bpath);
00436         }
00437     }
00438 }
00439 
00440 /*
00441  * consider_index_join_clauses
00442  *    Given sets of join clauses for an index, decide which parameterized
00443  *    index paths to build.
00444  *
00445  * Plain indexpaths are sent directly to add_path, while potential
00446  * bitmap indexpaths are added to *bitindexpaths for later processing.
00447  *
00448  * 'rel' is the index's heap relation
00449  * 'index' is the index for which we want to generate paths
00450  * 'rclauseset' is the collection of indexable restriction clauses
00451  * 'jclauseset' is the collection of indexable simple join clauses
00452  * 'eclauseset' is the collection of indexable clauses from EquivalenceClasses
00453  * '*bitindexpaths' is the list to add bitmap paths to
00454  */
00455 static void
00456 consider_index_join_clauses(PlannerInfo *root, RelOptInfo *rel,
00457                             IndexOptInfo *index,
00458                             IndexClauseSet *rclauseset,
00459                             IndexClauseSet *jclauseset,
00460                             IndexClauseSet *eclauseset,
00461                             List **bitindexpaths)
00462 {
00463     int         considered_clauses = 0;
00464     List       *considered_relids = NIL;
00465     int         indexcol;
00466 
00467     /*
00468      * The strategy here is to identify every potentially useful set of outer
00469      * rels that can provide indexable join clauses.  For each such set,
00470      * select all the join clauses available from those outer rels, add on all
00471      * the indexable restriction clauses, and generate plain and/or bitmap
00472      * index paths for that set of clauses.  This is based on the assumption
00473      * that it's always better to apply a clause as an indexqual than as a
00474      * filter (qpqual); which is where an available clause would end up being
00475      * applied if we omit it from the indexquals.
00476      *
00477      * This looks expensive, but in most practical cases there won't be very
00478      * many distinct sets of outer rels to consider.  As a safety valve when
00479      * that's not true, we use a heuristic: limit the number of outer rel sets
00480      * considered to a multiple of the number of clauses considered.  (We'll
00481      * always consider using each individual join clause, though.)
00482      *
00483      * For simplicity in selecting relevant clauses, we represent each set of
00484      * outer rels as a maximum set of clause_relids --- that is, the indexed
00485      * relation itself is also included in the relids set.  considered_relids
00486      * lists all relids sets we've already tried.
00487      */
00488     for (indexcol = 0; indexcol < index->ncolumns; indexcol++)
00489     {
00490         /* Consider each applicable simple join clause */
00491         considered_clauses += list_length(jclauseset->indexclauses[indexcol]);
00492         consider_index_join_outer_rels(root, rel, index,
00493                                        rclauseset, jclauseset, eclauseset,
00494                                        bitindexpaths,
00495                                        jclauseset->indexclauses[indexcol],
00496                                        considered_clauses,
00497                                        &considered_relids);
00498         /* Consider each applicable eclass join clause */
00499         considered_clauses += list_length(eclauseset->indexclauses[indexcol]);
00500         consider_index_join_outer_rels(root, rel, index,
00501                                        rclauseset, jclauseset, eclauseset,
00502                                        bitindexpaths,
00503                                        eclauseset->indexclauses[indexcol],
00504                                        considered_clauses,
00505                                        &considered_relids);
00506     }
00507 }
00508 
00509 /*
00510  * consider_index_join_outer_rels
00511  *    Generate parameterized paths based on clause relids in the clause list.
00512  *
00513  * Workhorse for consider_index_join_clauses; see notes therein for rationale.
00514  *
00515  * 'rel', 'index', 'rclauseset', 'jclauseset', 'eclauseset', and
00516  *      'bitindexpaths' as above
00517  * 'indexjoinclauses' is a list of RestrictInfos for join clauses
00518  * 'considered_clauses' is the total number of clauses considered (so far)
00519  * '*considered_relids' is a list of all relids sets already considered
00520  */
00521 static void
00522 consider_index_join_outer_rels(PlannerInfo *root, RelOptInfo *rel,
00523                                IndexOptInfo *index,
00524                                IndexClauseSet *rclauseset,
00525                                IndexClauseSet *jclauseset,
00526                                IndexClauseSet *eclauseset,
00527                                List **bitindexpaths,
00528                                List *indexjoinclauses,
00529                                int considered_clauses,
00530                                List **considered_relids)
00531 {
00532     ListCell   *lc;
00533 
00534     /* Examine relids of each joinclause in the given list */
00535     foreach(lc, indexjoinclauses)
00536     {
00537         RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
00538         Relids      clause_relids = rinfo->clause_relids;
00539         ListCell   *lc2;
00540 
00541         /* If we already tried its relids set, no need to do so again */
00542         if (bms_equal_any(clause_relids, *considered_relids))
00543             continue;
00544 
00545         /*
00546          * Generate the union of this clause's relids set with each
00547          * previously-tried set.  This ensures we try this clause along with
00548          * every interesting subset of previous clauses.  However, to avoid
00549          * exponential growth of planning time when there are many clauses,
00550          * limit the number of relid sets accepted to 10 * considered_clauses.
00551          *
00552          * Note: get_join_index_paths adds entries to *considered_relids, but
00553          * it prepends them to the list, so that we won't visit new entries
00554          * during the inner foreach loop.  No real harm would be done if we
00555          * did, since the subset check would reject them; but it would waste
00556          * some cycles.
00557          */
00558         foreach(lc2, *considered_relids)
00559         {
00560             Relids  oldrelids = (Relids) lfirst(lc2);
00561 
00562             /*
00563              * If either is a subset of the other, no new set is possible.
00564              * This isn't a complete test for redundancy, but it's easy and
00565              * cheap.  get_join_index_paths will check more carefully if we
00566              * already generated the same relids set.
00567              */
00568             if (bms_subset_compare(clause_relids, oldrelids) != BMS_DIFFERENT)
00569                 continue;
00570 
00571             /*
00572              * If this clause was derived from an equivalence class, the
00573              * clause list may contain other clauses derived from the same
00574              * eclass.  We should not consider that combining this clause with
00575              * one of those clauses generates a usefully different
00576              * parameterization; so skip if any clause derived from the same
00577              * eclass would already have been included when using oldrelids.
00578              */
00579             if (rinfo->parent_ec &&
00580                 eclass_already_used(rinfo->parent_ec, oldrelids,
00581                                     indexjoinclauses))
00582                 continue;
00583 
00584             /*
00585              * If the number of relid sets considered exceeds our heuristic
00586              * limit, stop considering combinations of clauses.  We'll still
00587              * consider the current clause alone, though (below this loop).
00588              */
00589             if (list_length(*considered_relids) >= 10 * considered_clauses)
00590                 break;
00591 
00592             /* OK, try the union set */
00593             get_join_index_paths(root, rel, index,
00594                                  rclauseset, jclauseset, eclauseset,
00595                                  bitindexpaths,
00596                                  bms_union(clause_relids, oldrelids),
00597                                  considered_relids);
00598         }
00599 
00600         /* Also try this set of relids by itself */
00601         get_join_index_paths(root, rel, index,
00602                              rclauseset, jclauseset, eclauseset,
00603                              bitindexpaths,
00604                              clause_relids,
00605                              considered_relids);
00606     }
00607 }
00608 
00609 /*
00610  * get_join_index_paths
00611  *    Generate index paths using clauses from the specified outer relations.
00612  *    In addition to generating paths, relids is added to *considered_relids
00613  *    if not already present.
00614  *
00615  * Workhorse for consider_index_join_clauses; see notes therein for rationale.
00616  *
00617  * 'rel', 'index', 'rclauseset', 'jclauseset', 'eclauseset',
00618  *      'bitindexpaths', 'considered_relids' as above
00619  * 'relids' is the current set of relids to consider (the target rel plus
00620  *      one or more outer rels)
00621  */
00622 static void
00623 get_join_index_paths(PlannerInfo *root, RelOptInfo *rel,
00624                      IndexOptInfo *index,
00625                      IndexClauseSet *rclauseset,
00626                      IndexClauseSet *jclauseset,
00627                      IndexClauseSet *eclauseset,
00628                      List **bitindexpaths,
00629                      Relids relids,
00630                      List **considered_relids)
00631 {
00632     IndexClauseSet clauseset;
00633     int         indexcol;
00634 
00635     /* If we already considered this relids set, don't repeat the work */
00636     if (bms_equal_any(relids, *considered_relids))
00637         return;
00638 
00639     /* Identify indexclauses usable with this relids set */
00640     MemSet(&clauseset, 0, sizeof(clauseset));
00641 
00642     for (indexcol = 0; indexcol < index->ncolumns; indexcol++)
00643     {
00644         ListCell   *lc;
00645 
00646         /* First find applicable simple join clauses */
00647         foreach(lc, jclauseset->indexclauses[indexcol])
00648         {
00649             RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
00650 
00651             if (bms_is_subset(rinfo->clause_relids, relids))
00652                 clauseset.indexclauses[indexcol] =
00653                     lappend(clauseset.indexclauses[indexcol], rinfo);
00654         }
00655 
00656         /*
00657          * Add applicable eclass join clauses.  The clauses generated for each
00658          * column are redundant (cf generate_implied_equalities_for_column),
00659          * so we need at most one.  This is the only exception to the general
00660          * rule of using all available index clauses.
00661          */
00662         foreach(lc, eclauseset->indexclauses[indexcol])
00663         {
00664             RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
00665 
00666             if (bms_is_subset(rinfo->clause_relids, relids))
00667             {
00668                 clauseset.indexclauses[indexcol] =
00669                     lappend(clauseset.indexclauses[indexcol], rinfo);
00670                 break;
00671             }
00672         }
00673 
00674         /* Add restriction clauses (this is nondestructive to rclauseset) */
00675         clauseset.indexclauses[indexcol] =
00676             list_concat(clauseset.indexclauses[indexcol],
00677                         rclauseset->indexclauses[indexcol]);
00678 
00679         if (clauseset.indexclauses[indexcol] != NIL)
00680             clauseset.nonempty = true;
00681     }
00682 
00683     /* We should have found something, else caller passed silly relids */
00684     Assert(clauseset.nonempty);
00685 
00686     /* Build index path(s) using the collected set of clauses */
00687     get_index_paths(root, rel, index, &clauseset, bitindexpaths);
00688 
00689     /*
00690      * Remember we considered paths for this set of relids.  We use lcons not
00691      * lappend to avoid confusing the loop in consider_index_join_outer_rels.
00692      */
00693     *considered_relids = lcons(relids, *considered_relids);
00694 }
00695 
00696 /*
00697  * eclass_already_used
00698  *      True if any join clause usable with oldrelids was generated from
00699  *      the specified equivalence class.
00700  */
00701 static bool
00702 eclass_already_used(EquivalenceClass *parent_ec, Relids oldrelids,
00703                     List *indexjoinclauses)
00704 {
00705     ListCell   *lc;
00706 
00707     foreach(lc, indexjoinclauses)
00708     {
00709         RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
00710 
00711         if (rinfo->parent_ec == parent_ec &&
00712             bms_is_subset(rinfo->clause_relids, oldrelids))
00713             return true;
00714     }
00715     return false;
00716 }
00717 
00718 /*
00719  * bms_equal_any
00720  *      True if relids is bms_equal to any member of relids_list
00721  *
00722  * Perhaps this should be in bitmapset.c someday.
00723  */
00724 static bool
00725 bms_equal_any(Relids relids, List *relids_list)
00726 {
00727     ListCell   *lc;
00728 
00729     foreach(lc, relids_list)
00730     {
00731         if (bms_equal(relids, (Relids) lfirst(lc)))
00732             return true;
00733     }
00734     return false;
00735 }
00736 
00737 
00738 /*
00739  * get_index_paths
00740  *    Given an index and a set of index clauses for it, construct IndexPaths.
00741  *
00742  * Plain indexpaths are sent directly to add_path, while potential
00743  * bitmap indexpaths are added to *bitindexpaths for later processing.
00744  *
00745  * This is a fairly simple frontend to build_index_paths().  Its reason for
00746  * existence is mainly to handle ScalarArrayOpExpr quals properly.  If the
00747  * index AM supports them natively, we should just include them in simple
00748  * index paths.  If not, we should exclude them while building simple index
00749  * paths, and then make a separate attempt to include them in bitmap paths.
00750  */
00751 static void
00752 get_index_paths(PlannerInfo *root, RelOptInfo *rel,
00753                 IndexOptInfo *index, IndexClauseSet *clauses,
00754                 List **bitindexpaths)
00755 {
00756     List       *indexpaths;
00757     ListCell   *lc;
00758 
00759     /*
00760      * Build simple index paths using the clauses.  Allow ScalarArrayOpExpr
00761      * clauses only if the index AM supports them natively.
00762      */
00763     indexpaths = build_index_paths(root, rel,
00764                                    index, clauses,
00765                                    index->predOK,
00766                                    SAOP_PER_AM, ST_ANYSCAN);
00767 
00768     /*
00769      * Submit all the ones that can form plain IndexScan plans to add_path. (A
00770      * plain IndexPath can represent either a plain IndexScan or an
00771      * IndexOnlyScan, but for our purposes here that distinction does not
00772      * matter.  However, some of the indexes might support only bitmap scans,
00773      * and those we mustn't submit to add_path here.)
00774      *
00775      * Also, pick out the ones that are usable as bitmap scans.  For that, we
00776      * must discard indexes that don't support bitmap scans, and we also are
00777      * only interested in paths that have some selectivity; we should discard
00778      * anything that was generated solely for ordering purposes.
00779      */
00780     foreach(lc, indexpaths)
00781     {
00782         IndexPath  *ipath = (IndexPath *) lfirst(lc);
00783 
00784         if (index->amhasgettuple)
00785             add_path(rel, (Path *) ipath);
00786 
00787         if (index->amhasgetbitmap &&
00788             (ipath->path.pathkeys == NIL ||
00789              ipath->indexselectivity < 1.0))
00790             *bitindexpaths = lappend(*bitindexpaths, ipath);
00791     }
00792 
00793     /*
00794      * If the index doesn't handle ScalarArrayOpExpr clauses natively, check
00795      * to see if there are any such clauses, and if so generate bitmap scan
00796      * paths relying on executor-managed ScalarArrayOpExpr.
00797      */
00798     if (!index->amsearcharray)
00799     {
00800         indexpaths = build_index_paths(root, rel,
00801                                        index, clauses,
00802                                        false,
00803                                        SAOP_REQUIRE, ST_BITMAPSCAN);
00804         *bitindexpaths = list_concat(*bitindexpaths, indexpaths);
00805     }
00806 }
00807 
00808 /*
00809  * build_index_paths
00810  *    Given an index and a set of index clauses for it, construct zero
00811  *    or more IndexPaths.
00812  *
00813  * We return a list of paths because (1) this routine checks some cases
00814  * that should cause us to not generate any IndexPath, and (2) in some
00815  * cases we want to consider both a forward and a backward scan, so as
00816  * to obtain both sort orders.  Note that the paths are just returned
00817  * to the caller and not immediately fed to add_path().
00818  *
00819  * At top level, useful_predicate should be exactly the index's predOK flag
00820  * (ie, true if it has a predicate that was proven from the restriction
00821  * clauses).  When working on an arm of an OR clause, useful_predicate
00822  * should be true if the predicate required the current OR list to be proven.
00823  * Note that this routine should never be called at all if the index has an
00824  * unprovable predicate.
00825  *
00826  * saop_control indicates whether ScalarArrayOpExpr clauses can be used.
00827  * When it's SAOP_REQUIRE, index paths are created only if we found at least
00828  * one ScalarArrayOpExpr clause.
00829  *
00830  * scantype indicates whether we want to create plain indexscans, bitmap
00831  * indexscans, or both.  When it's ST_BITMAPSCAN, we will not consider
00832  * index ordering while deciding if a Path is worth generating.
00833  *
00834  * 'rel' is the index's heap relation
00835  * 'index' is the index for which we want to generate paths
00836  * 'clauses' is the collection of indexable clauses (RestrictInfo nodes)
00837  * 'useful_predicate' indicates whether the index has a useful predicate
00838  * 'saop_control' indicates whether ScalarArrayOpExpr clauses can be used
00839  * 'scantype' indicates whether we need plain or bitmap scan support
00840  */
00841 static List *
00842 build_index_paths(PlannerInfo *root, RelOptInfo *rel,
00843                   IndexOptInfo *index, IndexClauseSet *clauses,
00844                   bool useful_predicate,
00845                   SaOpControl saop_control, ScanTypeControl scantype)
00846 {
00847     List       *result = NIL;
00848     IndexPath  *ipath;
00849     List       *index_clauses;
00850     List       *clause_columns;
00851     Relids      outer_relids;
00852     double      loop_count;
00853     List       *orderbyclauses;
00854     List       *orderbyclausecols;
00855     List       *index_pathkeys;
00856     List       *useful_pathkeys;
00857     bool        found_clause;
00858     bool        found_lower_saop_clause;
00859     bool        pathkeys_possibly_useful;
00860     bool        index_is_ordered;
00861     bool        index_only_scan;
00862     int         indexcol;
00863 
00864     /*
00865      * Check that index supports the desired scan type(s)
00866      */
00867     switch (scantype)
00868     {
00869         case ST_INDEXSCAN:
00870             if (!index->amhasgettuple)
00871                 return NIL;
00872             break;
00873         case ST_BITMAPSCAN:
00874             if (!index->amhasgetbitmap)
00875                 return NIL;
00876             break;
00877         case ST_ANYSCAN:
00878             /* either or both are OK */
00879             break;
00880     }
00881 
00882     /*
00883      * 1. Collect the index clauses into a single list.
00884      *
00885      * We build a list of RestrictInfo nodes for clauses to be used with this
00886      * index, along with an integer list of the index column numbers (zero
00887      * based) that each clause should be used with.  The clauses are ordered
00888      * by index key, so that the column numbers form a nondecreasing sequence.
00889      * (This order is depended on by btree and possibly other places.)  The
00890      * lists can be empty, if the index AM allows that.
00891      *
00892      * found_clause is set true only if there's at least one index clause; and
00893      * if saop_control is SAOP_REQUIRE, it has to be a ScalarArrayOpExpr
00894      * clause.
00895      *
00896      * found_lower_saop_clause is set true if there's a ScalarArrayOpExpr
00897      * index clause for a non-first index column.  This prevents us from
00898      * assuming that the scan result is ordered.  (Actually, the result is
00899      * still ordered if there are equality constraints for all earlier
00900      * columns, but it seems too expensive and non-modular for this code to be
00901      * aware of that refinement.)
00902      *
00903      * We also build a Relids set showing which outer rels are required by the
00904      * selected clauses.  Any lateral_relids are included in that, but not
00905      * otherwise accounted for.
00906      */
00907     index_clauses = NIL;
00908     clause_columns = NIL;
00909     found_clause = false;
00910     found_lower_saop_clause = false;
00911     outer_relids = bms_copy(rel->lateral_relids);
00912     for (indexcol = 0; indexcol < index->ncolumns; indexcol++)
00913     {
00914         ListCell   *lc;
00915 
00916         foreach(lc, clauses->indexclauses[indexcol])
00917         {
00918             RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
00919 
00920             if (IsA(rinfo->clause, ScalarArrayOpExpr))
00921             {
00922                 /* Ignore if not supported by index */
00923                 if (saop_control == SAOP_PER_AM && !index->amsearcharray)
00924                     continue;
00925                 found_clause = true;
00926                 if (indexcol > 0)
00927                     found_lower_saop_clause = true;
00928             }
00929             else
00930             {
00931                 if (saop_control != SAOP_REQUIRE)
00932                     found_clause = true;
00933             }
00934             index_clauses = lappend(index_clauses, rinfo);
00935             clause_columns = lappend_int(clause_columns, indexcol);
00936             outer_relids = bms_add_members(outer_relids,
00937                                            rinfo->clause_relids);
00938         }
00939 
00940         /*
00941          * If no clauses match the first index column, check for amoptionalkey
00942          * restriction.  We can't generate a scan over an index with
00943          * amoptionalkey = false unless there's at least one index clause.
00944          * (When working on columns after the first, this test cannot fail. It
00945          * is always okay for columns after the first to not have any
00946          * clauses.)
00947          */
00948         if (index_clauses == NIL && !index->amoptionalkey)
00949             return NIL;
00950     }
00951 
00952     /* We do not want the index's rel itself listed in outer_relids */
00953     outer_relids = bms_del_member(outer_relids, rel->relid);
00954     /* Enforce convention that outer_relids is exactly NULL if empty */
00955     if (bms_is_empty(outer_relids))
00956         outer_relids = NULL;
00957 
00958     /* Compute loop_count for cost estimation purposes */
00959     loop_count = get_loop_count(root, outer_relids);
00960 
00961     /*
00962      * 2. Compute pathkeys describing index's ordering, if any, then see how
00963      * many of them are actually useful for this query.  This is not relevant
00964      * if we are only trying to build bitmap indexscans, nor if we have to
00965      * assume the scan is unordered.
00966      */
00967     pathkeys_possibly_useful = (scantype != ST_BITMAPSCAN &&
00968                                 !found_lower_saop_clause &&
00969                                 has_useful_pathkeys(root, rel));
00970     index_is_ordered = (index->sortopfamily != NULL);
00971     if (index_is_ordered && pathkeys_possibly_useful)
00972     {
00973         index_pathkeys = build_index_pathkeys(root, index,
00974                                               ForwardScanDirection);
00975         useful_pathkeys = truncate_useless_pathkeys(root, rel,
00976                                                     index_pathkeys);
00977         orderbyclauses = NIL;
00978         orderbyclausecols = NIL;
00979     }
00980     else if (index->amcanorderbyop && pathkeys_possibly_useful)
00981     {
00982         /* see if we can generate ordering operators for query_pathkeys */
00983         match_pathkeys_to_index(index, root->query_pathkeys,
00984                                 &orderbyclauses,
00985                                 &orderbyclausecols);
00986         if (orderbyclauses)
00987             useful_pathkeys = root->query_pathkeys;
00988         else
00989             useful_pathkeys = NIL;
00990     }
00991     else
00992     {
00993         useful_pathkeys = NIL;
00994         orderbyclauses = NIL;
00995         orderbyclausecols = NIL;
00996     }
00997 
00998     /*
00999      * 3. Check if an index-only scan is possible.  If we're not building
01000      * plain indexscans, this isn't relevant since bitmap scans don't support
01001      * index data retrieval anyway.
01002      */
01003     index_only_scan = (scantype != ST_BITMAPSCAN &&
01004                        check_index_only(rel, index));
01005 
01006     /*
01007      * 4. Generate an indexscan path if there are relevant restriction clauses
01008      * in the current clauses, OR the index ordering is potentially useful for
01009      * later merging or final output ordering, OR the index has a useful
01010      * predicate, OR an index-only scan is possible.
01011      */
01012     if (found_clause || useful_pathkeys != NIL || useful_predicate ||
01013         index_only_scan)
01014     {
01015         ipath = create_index_path(root, index,
01016                                   index_clauses,
01017                                   clause_columns,
01018                                   orderbyclauses,
01019                                   orderbyclausecols,
01020                                   useful_pathkeys,
01021                                   index_is_ordered ?
01022                                   ForwardScanDirection :
01023                                   NoMovementScanDirection,
01024                                   index_only_scan,
01025                                   outer_relids,
01026                                   loop_count);
01027         result = lappend(result, ipath);
01028     }
01029 
01030     /*
01031      * 5. If the index is ordered, a backwards scan might be interesting.
01032      */
01033     if (index_is_ordered && pathkeys_possibly_useful)
01034     {
01035         index_pathkeys = build_index_pathkeys(root, index,
01036                                               BackwardScanDirection);
01037         useful_pathkeys = truncate_useless_pathkeys(root, rel,
01038                                                     index_pathkeys);
01039         if (useful_pathkeys != NIL)
01040         {
01041             ipath = create_index_path(root, index,
01042                                       index_clauses,
01043                                       clause_columns,
01044                                       NIL,
01045                                       NIL,
01046                                       useful_pathkeys,
01047                                       BackwardScanDirection,
01048                                       index_only_scan,
01049                                       outer_relids,
01050                                       loop_count);
01051             result = lappend(result, ipath);
01052         }
01053     }
01054 
01055     return result;
01056 }
01057 
01058 /*
01059  * build_paths_for_OR
01060  *    Given a list of restriction clauses from one arm of an OR clause,
01061  *    construct all matching IndexPaths for the relation.
01062  *
01063  * Here we must scan all indexes of the relation, since a bitmap OR tree
01064  * can use multiple indexes.
01065  *
01066  * The caller actually supplies two lists of restriction clauses: some
01067  * "current" ones and some "other" ones.  Both lists can be used freely
01068  * to match keys of the index, but an index must use at least one of the
01069  * "current" clauses to be considered usable.  The motivation for this is
01070  * examples like
01071  *      WHERE (x = 42) AND (... OR (y = 52 AND z = 77) OR ....)
01072  * While we are considering the y/z subclause of the OR, we can use "x = 42"
01073  * as one of the available index conditions; but we shouldn't match the
01074  * subclause to any index on x alone, because such a Path would already have
01075  * been generated at the upper level.  So we could use an index on x,y,z
01076  * or an index on x,y for the OR subclause, but not an index on just x.
01077  * When dealing with a partial index, a match of the index predicate to
01078  * one of the "current" clauses also makes the index usable.
01079  *
01080  * 'rel' is the relation for which we want to generate index paths
01081  * 'clauses' is the current list of clauses (RestrictInfo nodes)
01082  * 'other_clauses' is the list of additional upper-level clauses
01083  */
01084 static List *
01085 build_paths_for_OR(PlannerInfo *root, RelOptInfo *rel,
01086                    List *clauses, List *other_clauses)
01087 {
01088     List       *result = NIL;
01089     List       *all_clauses = NIL;      /* not computed till needed */
01090     ListCell   *lc;
01091 
01092     foreach(lc, rel->indexlist)
01093     {
01094         IndexOptInfo *index = (IndexOptInfo *) lfirst(lc);
01095         IndexClauseSet clauseset;
01096         List       *indexpaths;
01097         bool        useful_predicate;
01098 
01099         /* Ignore index if it doesn't support bitmap scans */
01100         if (!index->amhasgetbitmap)
01101             continue;
01102 
01103         /*
01104          * Ignore partial indexes that do not match the query.  If a partial
01105          * index is marked predOK then we know it's OK.  Otherwise, we have to
01106          * test whether the added clauses are sufficient to imply the
01107          * predicate. If so, we can use the index in the current context.
01108          *
01109          * We set useful_predicate to true iff the predicate was proven using
01110          * the current set of clauses.  This is needed to prevent matching a
01111          * predOK index to an arm of an OR, which would be a legal but
01112          * pointlessly inefficient plan.  (A better plan will be generated by
01113          * just scanning the predOK index alone, no OR.)
01114          */
01115         useful_predicate = false;
01116         if (index->indpred != NIL)
01117         {
01118             if (index->predOK)
01119             {
01120                 /* Usable, but don't set useful_predicate */
01121             }
01122             else
01123             {
01124                 /* Form all_clauses if not done already */
01125                 if (all_clauses == NIL)
01126                     all_clauses = list_concat(list_copy(clauses),
01127                                               other_clauses);
01128 
01129                 if (!predicate_implied_by(index->indpred, all_clauses))
01130                     continue;   /* can't use it at all */
01131 
01132                 if (!predicate_implied_by(index->indpred, other_clauses))
01133                     useful_predicate = true;
01134             }
01135         }
01136 
01137         /*
01138          * Identify the restriction clauses that can match the index.
01139          */
01140         MemSet(&clauseset, 0, sizeof(clauseset));
01141         match_clauses_to_index(index, clauses, &clauseset);
01142 
01143         /*
01144          * If no matches so far, and the index predicate isn't useful, we
01145          * don't want it.
01146          */
01147         if (!clauseset.nonempty && !useful_predicate)
01148             continue;
01149 
01150         /*
01151          * Add "other" restriction clauses to the clauseset.
01152          */
01153         match_clauses_to_index(index, other_clauses, &clauseset);
01154 
01155         /*
01156          * Construct paths if possible.
01157          */
01158         indexpaths = build_index_paths(root, rel,
01159                                        index, &clauseset,
01160                                        useful_predicate,
01161                                        SAOP_ALLOW, ST_BITMAPSCAN);
01162         result = list_concat(result, indexpaths);
01163     }
01164 
01165     return result;
01166 }
01167 
01168 /*
01169  * generate_bitmap_or_paths
01170  *      Look through the list of clauses to find OR clauses, and generate
01171  *      a BitmapOrPath for each one we can handle that way.  Return a list
01172  *      of the generated BitmapOrPaths.
01173  *
01174  * other_clauses is a list of additional clauses that can be assumed true
01175  * for the purpose of generating indexquals, but are not to be searched for
01176  * ORs.  (See build_paths_for_OR() for motivation.)
01177  *
01178  * If restriction_only is true, ignore OR elements that are join clauses.
01179  * When using this feature it is caller's responsibility that neither clauses
01180  * nor other_clauses contain any join clauses that are not ORs, as we do not
01181  * re-filter those lists.
01182  */
01183 List *
01184 generate_bitmap_or_paths(PlannerInfo *root, RelOptInfo *rel,
01185                          List *clauses, List *other_clauses,
01186                          bool restriction_only)
01187 {
01188     List       *result = NIL;
01189     List       *all_clauses;
01190     ListCell   *lc;
01191 
01192     /*
01193      * We can use both the current and other clauses as context for
01194      * build_paths_for_OR; no need to remove ORs from the lists.
01195      */
01196     all_clauses = list_concat(list_copy(clauses), other_clauses);
01197 
01198     foreach(lc, clauses)
01199     {
01200         RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
01201         List       *pathlist;
01202         Path       *bitmapqual;
01203         ListCell   *j;
01204 
01205         Assert(IsA(rinfo, RestrictInfo));
01206         /* Ignore RestrictInfos that aren't ORs */
01207         if (!restriction_is_or_clause(rinfo))
01208             continue;
01209 
01210         /*
01211          * We must be able to match at least one index to each of the arms of
01212          * the OR, else we can't use it.
01213          */
01214         pathlist = NIL;
01215         foreach(j, ((BoolExpr *) rinfo->orclause)->args)
01216         {
01217             Node       *orarg = (Node *) lfirst(j);
01218             List       *indlist;
01219 
01220             /* OR arguments should be ANDs or sub-RestrictInfos */
01221             if (and_clause(orarg))
01222             {
01223                 List       *andargs = ((BoolExpr *) orarg)->args;
01224 
01225                 if (restriction_only)
01226                     andargs = drop_indexable_join_clauses(rel, andargs);
01227 
01228                 indlist = build_paths_for_OR(root, rel,
01229                                              andargs,
01230                                              all_clauses);
01231 
01232                 /* Recurse in case there are sub-ORs */
01233                 indlist = list_concat(indlist,
01234                                       generate_bitmap_or_paths(root, rel,
01235                                                                andargs,
01236                                                                all_clauses,
01237                                                           restriction_only));
01238             }
01239             else
01240             {
01241                 List       *orargs;
01242 
01243                 Assert(IsA(orarg, RestrictInfo));
01244                 Assert(!restriction_is_or_clause((RestrictInfo *) orarg));
01245                 orargs = list_make1(orarg);
01246 
01247                 if (restriction_only)
01248                     orargs = drop_indexable_join_clauses(rel, orargs);
01249 
01250                 indlist = build_paths_for_OR(root, rel,
01251                                              orargs,
01252                                              all_clauses);
01253             }
01254 
01255             /*
01256              * If nothing matched this arm, we can't do anything with this OR
01257              * clause.
01258              */
01259             if (indlist == NIL)
01260             {
01261                 pathlist = NIL;
01262                 break;
01263             }
01264 
01265             /*
01266              * OK, pick the most promising AND combination, and add it to
01267              * pathlist.
01268              */
01269             bitmapqual = choose_bitmap_and(root, rel, indlist);
01270             pathlist = lappend(pathlist, bitmapqual);
01271         }
01272 
01273         /*
01274          * If we have a match for every arm, then turn them into a
01275          * BitmapOrPath, and add to result list.
01276          */
01277         if (pathlist != NIL)
01278         {
01279             bitmapqual = (Path *) create_bitmap_or_path(root, rel, pathlist);
01280             result = lappend(result, bitmapqual);
01281         }
01282     }
01283 
01284     return result;
01285 }
01286 
01287 /*
01288  * drop_indexable_join_clauses
01289  *      Remove any indexable join clauses from the list.
01290  *
01291  * This is a helper for generate_bitmap_or_paths().  We leave OR clauses
01292  * in the list whether they are joins or not, since we might be able to
01293  * extract a restriction item from an OR list.  It's safe to leave such
01294  * clauses in the list because match_clauses_to_index() will ignore them,
01295  * so there's no harm in passing such clauses to build_paths_for_OR().
01296  */
01297 static List *
01298 drop_indexable_join_clauses(RelOptInfo *rel, List *clauses)
01299 {
01300     List       *result = NIL;
01301     ListCell   *lc;
01302 
01303     foreach(lc, clauses)
01304     {
01305         RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
01306 
01307         Assert(IsA(rinfo, RestrictInfo));
01308         if (restriction_is_or_clause(rinfo) ||
01309             bms_is_subset(rinfo->clause_relids, rel->relids))
01310             result = lappend(result, rinfo);
01311     }
01312     return result;
01313 }
01314 
01315 
01316 /*
01317  * choose_bitmap_and
01318  *      Given a nonempty list of bitmap paths, AND them into one path.
01319  *
01320  * This is a nontrivial decision since we can legally use any subset of the
01321  * given path set.  We want to choose a good tradeoff between selectivity
01322  * and cost of computing the bitmap.
01323  *
01324  * The result is either a single one of the inputs, or a BitmapAndPath
01325  * combining multiple inputs.
01326  */
01327 static Path *
01328 choose_bitmap_and(PlannerInfo *root, RelOptInfo *rel, List *paths)
01329 {
01330     int         npaths = list_length(paths);
01331     PathClauseUsage **pathinfoarray;
01332     PathClauseUsage *pathinfo;
01333     List       *clauselist;
01334     List       *bestpaths = NIL;
01335     Cost        bestcost = 0;
01336     int         i,
01337                 j;
01338     ListCell   *l;
01339 
01340     Assert(npaths > 0);         /* else caller error */
01341     if (npaths == 1)
01342         return (Path *) linitial(paths);        /* easy case */
01343 
01344     /*
01345      * In theory we should consider every nonempty subset of the given paths.
01346      * In practice that seems like overkill, given the crude nature of the
01347      * estimates, not to mention the possible effects of higher-level AND and
01348      * OR clauses.  Moreover, it's completely impractical if there are a large
01349      * number of paths, since the work would grow as O(2^N).
01350      *
01351      * As a heuristic, we first check for paths using exactly the same sets of
01352      * WHERE clauses + index predicate conditions, and reject all but the
01353      * cheapest-to-scan in any such group.  This primarily gets rid of indexes
01354      * that include the interesting columns but also irrelevant columns.  (In
01355      * situations where the DBA has gone overboard on creating variant
01356      * indexes, this can make for a very large reduction in the number of
01357      * paths considered further.)
01358      *
01359      * We then sort the surviving paths with the cheapest-to-scan first, and
01360      * for each path, consider using that path alone as the basis for a bitmap
01361      * scan.  Then we consider bitmap AND scans formed from that path plus
01362      * each subsequent (higher-cost) path, adding on a subsequent path if it
01363      * results in a reduction in the estimated total scan cost. This means we
01364      * consider about O(N^2) rather than O(2^N) path combinations, which is
01365      * quite tolerable, especially given than N is usually reasonably small
01366      * because of the prefiltering step.  The cheapest of these is returned.
01367      *
01368      * We will only consider AND combinations in which no two indexes use the
01369      * same WHERE clause.  This is a bit of a kluge: it's needed because
01370      * costsize.c and clausesel.c aren't very smart about redundant clauses.
01371      * They will usually double-count the redundant clauses, producing a
01372      * too-small selectivity that makes a redundant AND step look like it
01373      * reduces the total cost.  Perhaps someday that code will be smarter and
01374      * we can remove this limitation.  (But note that this also defends
01375      * against flat-out duplicate input paths, which can happen because
01376      * match_join_clauses_to_index will find the same OR join clauses that
01377      * create_or_index_quals has pulled OR restriction clauses out of.)
01378      *
01379      * For the same reason, we reject AND combinations in which an index
01380      * predicate clause duplicates another clause.  Here we find it necessary
01381      * to be even stricter: we'll reject a partial index if any of its
01382      * predicate clauses are implied by the set of WHERE clauses and predicate
01383      * clauses used so far.  This covers cases such as a condition "x = 42"
01384      * used with a plain index, followed by a clauseless scan of a partial
01385      * index "WHERE x >= 40 AND x < 50".  The partial index has been accepted
01386      * only because "x = 42" was present, and so allowing it would partially
01387      * double-count selectivity.  (We could use predicate_implied_by on
01388      * regular qual clauses too, to have a more intelligent, but much more
01389      * expensive, check for redundancy --- but in most cases simple equality
01390      * seems to suffice.)
01391      */
01392 
01393     /*
01394      * Extract clause usage info and detect any paths that use exactly the
01395      * same set of clauses; keep only the cheapest-to-scan of any such groups.
01396      * The surviving paths are put into an array for qsort'ing.
01397      */
01398     pathinfoarray = (PathClauseUsage **)
01399         palloc(npaths * sizeof(PathClauseUsage *));
01400     clauselist = NIL;
01401     npaths = 0;
01402     foreach(l, paths)
01403     {
01404         Path       *ipath = (Path *) lfirst(l);
01405 
01406         pathinfo = classify_index_clause_usage(ipath, &clauselist);
01407         for (i = 0; i < npaths; i++)
01408         {
01409             if (bms_equal(pathinfo->clauseids, pathinfoarray[i]->clauseids))
01410                 break;
01411         }
01412         if (i < npaths)
01413         {
01414             /* duplicate clauseids, keep the cheaper one */
01415             Cost        ncost;
01416             Cost        ocost;
01417             Selectivity nselec;
01418             Selectivity oselec;
01419 
01420             cost_bitmap_tree_node(pathinfo->path, &ncost, &nselec);
01421             cost_bitmap_tree_node(pathinfoarray[i]->path, &ocost, &oselec);
01422             if (ncost < ocost)
01423                 pathinfoarray[i] = pathinfo;
01424         }
01425         else
01426         {
01427             /* not duplicate clauseids, add to array */
01428             pathinfoarray[npaths++] = pathinfo;
01429         }
01430     }
01431 
01432     /* If only one surviving path, we're done */
01433     if (npaths == 1)
01434         return pathinfoarray[0]->path;
01435 
01436     /* Sort the surviving paths by index access cost */
01437     qsort(pathinfoarray, npaths, sizeof(PathClauseUsage *),
01438           path_usage_comparator);
01439 
01440     /*
01441      * For each surviving index, consider it as an "AND group leader", and see
01442      * whether adding on any of the later indexes results in an AND path with
01443      * cheaper total cost than before.  Then take the cheapest AND group.
01444      */
01445     for (i = 0; i < npaths; i++)
01446     {
01447         Cost        costsofar;
01448         List       *qualsofar;
01449         Bitmapset  *clauseidsofar;
01450         ListCell   *lastcell;
01451 
01452         pathinfo = pathinfoarray[i];
01453         paths = list_make1(pathinfo->path);
01454         costsofar = bitmap_scan_cost_est(root, rel, pathinfo->path);
01455         qualsofar = list_concat(list_copy(pathinfo->quals),
01456                                 list_copy(pathinfo->preds));
01457         clauseidsofar = bms_copy(pathinfo->clauseids);
01458         lastcell = list_head(paths);    /* for quick deletions */
01459 
01460         for (j = i + 1; j < npaths; j++)
01461         {
01462             Cost        newcost;
01463 
01464             pathinfo = pathinfoarray[j];
01465             /* Check for redundancy */
01466             if (bms_overlap(pathinfo->clauseids, clauseidsofar))
01467                 continue;       /* consider it redundant */
01468             if (pathinfo->preds)
01469             {
01470                 bool        redundant = false;
01471 
01472                 /* we check each predicate clause separately */
01473                 foreach(l, pathinfo->preds)
01474                 {
01475                     Node       *np = (Node *) lfirst(l);
01476 
01477                     if (predicate_implied_by(list_make1(np), qualsofar))
01478                     {
01479                         redundant = true;
01480                         break;  /* out of inner foreach loop */
01481                     }
01482                 }
01483                 if (redundant)
01484                     continue;
01485             }
01486             /* tentatively add new path to paths, so we can estimate cost */
01487             paths = lappend(paths, pathinfo->path);
01488             newcost = bitmap_and_cost_est(root, rel, paths);
01489             if (newcost < costsofar)
01490             {
01491                 /* keep new path in paths, update subsidiary variables */
01492                 costsofar = newcost;
01493                 qualsofar = list_concat(qualsofar,
01494                                         list_copy(pathinfo->quals));
01495                 qualsofar = list_concat(qualsofar,
01496                                         list_copy(pathinfo->preds));
01497                 clauseidsofar = bms_add_members(clauseidsofar,
01498                                                 pathinfo->clauseids);
01499                 lastcell = lnext(lastcell);
01500             }
01501             else
01502             {
01503                 /* reject new path, remove it from paths list */
01504                 paths = list_delete_cell(paths, lnext(lastcell), lastcell);
01505             }
01506             Assert(lnext(lastcell) == NULL);
01507         }
01508 
01509         /* Keep the cheapest AND-group (or singleton) */
01510         if (i == 0 || costsofar < bestcost)
01511         {
01512             bestpaths = paths;
01513             bestcost = costsofar;
01514         }
01515 
01516         /* some easy cleanup (we don't try real hard though) */
01517         list_free(qualsofar);
01518     }
01519 
01520     if (list_length(bestpaths) == 1)
01521         return (Path *) linitial(bestpaths);    /* no need for AND */
01522     return (Path *) create_bitmap_and_path(root, rel, bestpaths);
01523 }
01524 
01525 /* qsort comparator to sort in increasing index access cost order */
01526 static int
01527 path_usage_comparator(const void *a, const void *b)
01528 {
01529     PathClauseUsage *pa = *(PathClauseUsage *const *) a;
01530     PathClauseUsage *pb = *(PathClauseUsage *const *) b;
01531     Cost        acost;
01532     Cost        bcost;
01533     Selectivity aselec;
01534     Selectivity bselec;
01535 
01536     cost_bitmap_tree_node(pa->path, &acost, &aselec);
01537     cost_bitmap_tree_node(pb->path, &bcost, &bselec);
01538 
01539     /*
01540      * If costs are the same, sort by selectivity.
01541      */
01542     if (acost < bcost)
01543         return -1;
01544     if (acost > bcost)
01545         return 1;
01546 
01547     if (aselec < bselec)
01548         return -1;
01549     if (aselec > bselec)
01550         return 1;
01551 
01552     return 0;
01553 }
01554 
01555 /*
01556  * Estimate the cost of actually executing a bitmap scan with a single
01557  * index path (no BitmapAnd, at least not at this level; but it could be
01558  * a BitmapOr).
01559  */
01560 static Cost
01561 bitmap_scan_cost_est(PlannerInfo *root, RelOptInfo *rel, Path *ipath)
01562 {
01563     BitmapHeapPath bpath;
01564     Relids      required_outer;
01565 
01566     /* Identify required outer rels, in case it's a parameterized scan */
01567     required_outer = get_bitmap_tree_required_outer(ipath);
01568 
01569     /* Set up a dummy BitmapHeapPath */
01570     bpath.path.type = T_BitmapHeapPath;
01571     bpath.path.pathtype = T_BitmapHeapScan;
01572     bpath.path.parent = rel;
01573     bpath.path.param_info = get_baserel_parampathinfo(root, rel,
01574                                                       required_outer);
01575     bpath.path.pathkeys = NIL;
01576     bpath.bitmapqual = ipath;
01577 
01578     cost_bitmap_heap_scan(&bpath.path, root, rel,
01579                           bpath.path.param_info,
01580                           ipath,
01581                           get_loop_count(root, required_outer));
01582 
01583     return bpath.path.total_cost;
01584 }
01585 
01586 /*
01587  * Estimate the cost of actually executing a BitmapAnd scan with the given
01588  * inputs.
01589  */
01590 static Cost
01591 bitmap_and_cost_est(PlannerInfo *root, RelOptInfo *rel, List *paths)
01592 {
01593     BitmapAndPath apath;
01594     BitmapHeapPath bpath;
01595     Relids      required_outer;
01596 
01597     /* Set up a dummy BitmapAndPath */
01598     apath.path.type = T_BitmapAndPath;
01599     apath.path.pathtype = T_BitmapAnd;
01600     apath.path.parent = rel;
01601     apath.path.param_info = NULL;       /* not used in bitmap trees */
01602     apath.path.pathkeys = NIL;
01603     apath.bitmapquals = paths;
01604     cost_bitmap_and_node(&apath, root);
01605 
01606     /* Identify required outer rels, in case it's a parameterized scan */
01607     required_outer = get_bitmap_tree_required_outer((Path *) &apath);
01608 
01609     /* Set up a dummy BitmapHeapPath */
01610     bpath.path.type = T_BitmapHeapPath;
01611     bpath.path.pathtype = T_BitmapHeapScan;
01612     bpath.path.parent = rel;
01613     bpath.path.param_info = get_baserel_parampathinfo(root, rel,
01614                                                       required_outer);
01615     bpath.path.pathkeys = NIL;
01616     bpath.bitmapqual = (Path *) &apath;
01617 
01618     /* Now we can do cost_bitmap_heap_scan */
01619     cost_bitmap_heap_scan(&bpath.path, root, rel,
01620                           bpath.path.param_info,
01621                           (Path *) &apath,
01622                           get_loop_count(root, required_outer));
01623 
01624     return bpath.path.total_cost;
01625 }
01626 
01627 
01628 /*
01629  * classify_index_clause_usage
01630  *      Construct a PathClauseUsage struct describing the WHERE clauses and
01631  *      index predicate clauses used by the given indexscan path.
01632  *      We consider two clauses the same if they are equal().
01633  *
01634  * At some point we might want to migrate this info into the Path data
01635  * structure proper, but for the moment it's only needed within
01636  * choose_bitmap_and().
01637  *
01638  * *clauselist is used and expanded as needed to identify all the distinct
01639  * clauses seen across successive calls.  Caller must initialize it to NIL
01640  * before first call of a set.
01641  */
01642 static PathClauseUsage *
01643 classify_index_clause_usage(Path *path, List **clauselist)
01644 {
01645     PathClauseUsage *result;
01646     Bitmapset  *clauseids;
01647     ListCell   *lc;
01648 
01649     result = (PathClauseUsage *) palloc(sizeof(PathClauseUsage));
01650     result->path = path;
01651 
01652     /* Recursively find the quals and preds used by the path */
01653     result->quals = NIL;
01654     result->preds = NIL;
01655     find_indexpath_quals(path, &result->quals, &result->preds);
01656 
01657     /* Build up a bitmapset representing the quals and preds */
01658     clauseids = NULL;
01659     foreach(lc, result->quals)
01660     {
01661         Node       *node = (Node *) lfirst(lc);
01662 
01663         clauseids = bms_add_member(clauseids,
01664                                    find_list_position(node, clauselist));
01665     }
01666     foreach(lc, result->preds)
01667     {
01668         Node       *node = (Node *) lfirst(lc);
01669 
01670         clauseids = bms_add_member(clauseids,
01671                                    find_list_position(node, clauselist));
01672     }
01673     result->clauseids = clauseids;
01674 
01675     return result;
01676 }
01677 
01678 
01679 /*
01680  * get_bitmap_tree_required_outer
01681  *      Find the required outer rels for a bitmap tree (index/and/or)
01682  *
01683  * We don't associate any particular parameterization with a BitmapAnd or
01684  * BitmapOr node; however, the IndexPaths have parameterization info, in
01685  * their capacity as standalone access paths.  The parameterization required
01686  * for the bitmap heap scan node is the union of rels referenced in the
01687  * child IndexPaths.
01688  */
01689 static Relids
01690 get_bitmap_tree_required_outer(Path *bitmapqual)
01691 {
01692     Relids      result = NULL;
01693     ListCell   *lc;
01694 
01695     if (IsA(bitmapqual, IndexPath))
01696     {
01697         return bms_copy(PATH_REQ_OUTER(bitmapqual));
01698     }
01699     else if (IsA(bitmapqual, BitmapAndPath))
01700     {
01701         foreach(lc, ((BitmapAndPath *) bitmapqual)->bitmapquals)
01702         {
01703             result = bms_join(result,
01704                         get_bitmap_tree_required_outer((Path *) lfirst(lc)));
01705         }
01706     }
01707     else if (IsA(bitmapqual, BitmapOrPath))
01708     {
01709         foreach(lc, ((BitmapOrPath *) bitmapqual)->bitmapquals)
01710         {
01711             result = bms_join(result,
01712                         get_bitmap_tree_required_outer((Path *) lfirst(lc)));
01713         }
01714     }
01715     else
01716         elog(ERROR, "unrecognized node type: %d", nodeTag(bitmapqual));
01717 
01718     return result;
01719 }
01720 
01721 
01722 /*
01723  * find_indexpath_quals
01724  *
01725  * Given the Path structure for a plain or bitmap indexscan, extract lists
01726  * of all the indexquals and index predicate conditions used in the Path.
01727  * These are appended to the initial contents of *quals and *preds (hence
01728  * caller should initialize those to NIL).
01729  *
01730  * This is sort of a simplified version of make_restrictinfo_from_bitmapqual;
01731  * here, we are not trying to produce an accurate representation of the AND/OR
01732  * semantics of the Path, but just find out all the base conditions used.
01733  *
01734  * The result lists contain pointers to the expressions used in the Path,
01735  * but all the list cells are freshly built, so it's safe to destructively
01736  * modify the lists (eg, by concat'ing with other lists).
01737  */
01738 static void
01739 find_indexpath_quals(Path *bitmapqual, List **quals, List **preds)
01740 {
01741     if (IsA(bitmapqual, BitmapAndPath))
01742     {
01743         BitmapAndPath *apath = (BitmapAndPath *) bitmapqual;
01744         ListCell   *l;
01745 
01746         foreach(l, apath->bitmapquals)
01747         {
01748             find_indexpath_quals((Path *) lfirst(l), quals, preds);
01749         }
01750     }
01751     else if (IsA(bitmapqual, BitmapOrPath))
01752     {
01753         BitmapOrPath *opath = (BitmapOrPath *) bitmapqual;
01754         ListCell   *l;
01755 
01756         foreach(l, opath->bitmapquals)
01757         {
01758             find_indexpath_quals((Path *) lfirst(l), quals, preds);
01759         }
01760     }
01761     else if (IsA(bitmapqual, IndexPath))
01762     {
01763         IndexPath  *ipath = (IndexPath *) bitmapqual;
01764 
01765         *quals = list_concat(*quals, get_actual_clauses(ipath->indexclauses));
01766         *preds = list_concat(*preds, list_copy(ipath->indexinfo->indpred));
01767     }
01768     else
01769         elog(ERROR, "unrecognized node type: %d", nodeTag(bitmapqual));
01770 }
01771 
01772 
01773 /*
01774  * find_list_position
01775  *      Return the given node's position (counting from 0) in the given
01776  *      list of nodes.  If it's not equal() to any existing list member,
01777  *      add it at the end, and return that position.
01778  */
01779 static int
01780 find_list_position(Node *node, List **nodelist)
01781 {
01782     int         i;
01783     ListCell   *lc;
01784 
01785     i = 0;
01786     foreach(lc, *nodelist)
01787     {
01788         Node       *oldnode = (Node *) lfirst(lc);
01789 
01790         if (equal(node, oldnode))
01791             return i;
01792         i++;
01793     }
01794 
01795     *nodelist = lappend(*nodelist, node);
01796 
01797     return i;
01798 }
01799 
01800 
01801 /*
01802  * check_index_only
01803  *      Determine whether an index-only scan is possible for this index.
01804  */
01805 static bool
01806 check_index_only(RelOptInfo *rel, IndexOptInfo *index)
01807 {
01808     bool        result;
01809     Bitmapset  *attrs_used = NULL;
01810     Bitmapset  *index_attrs = NULL;
01811     ListCell   *lc;
01812     int         i;
01813 
01814     /* Index-only scans must be enabled, and index must be capable of them */
01815     if (!enable_indexonlyscan)
01816         return false;
01817     if (!index->canreturn)
01818         return false;
01819 
01820     /*
01821      * Check that all needed attributes of the relation are available from the
01822      * index.
01823      *
01824      * XXX this is overly conservative for partial indexes, since we will
01825      * consider attributes involved in the index predicate as required even
01826      * though the predicate won't need to be checked at runtime.  (The same is
01827      * true for attributes used only in index quals, if we are certain that
01828      * the index is not lossy.)  However, it would be quite expensive to
01829      * determine that accurately at this point, so for now we take the easy
01830      * way out.
01831      */
01832 
01833     /*
01834      * Add all the attributes needed for joins or final output.  Note: we must
01835      * look at reltargetlist, not the attr_needed data, because attr_needed
01836      * isn't computed for inheritance child rels.
01837      */
01838     pull_varattnos((Node *) rel->reltargetlist, rel->relid, &attrs_used);
01839 
01840     /* Add all the attributes used by restriction clauses. */
01841     foreach(lc, rel->baserestrictinfo)
01842     {
01843         RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
01844 
01845         pull_varattnos((Node *) rinfo->clause, rel->relid, &attrs_used);
01846     }
01847 
01848     /* Construct a bitmapset of columns stored in the index. */
01849     for (i = 0; i < index->ncolumns; i++)
01850     {
01851         int         attno = index->indexkeys[i];
01852 
01853         /*
01854          * For the moment, we just ignore index expressions.  It might be nice
01855          * to do something with them, later.
01856          */
01857         if (attno == 0)
01858             continue;
01859 
01860         index_attrs =
01861             bms_add_member(index_attrs,
01862                            attno - FirstLowInvalidHeapAttributeNumber);
01863     }
01864 
01865     /* Do we have all the necessary attributes? */
01866     result = bms_is_subset(attrs_used, index_attrs);
01867 
01868     bms_free(attrs_used);
01869     bms_free(index_attrs);
01870 
01871     return result;
01872 }
01873 
01874 /*
01875  * get_loop_count
01876  *      Choose the loop count estimate to use for costing a parameterized path
01877  *      with the given set of outer relids.
01878  *
01879  * Since we produce parameterized paths before we've begun to generate join
01880  * relations, it's impossible to predict exactly how many times a parameterized
01881  * path will be iterated; we don't know the size of the relation that will be
01882  * on the outside of the nestloop.  However, we should try to account for
01883  * multiple iterations somehow in costing the path.  The heuristic embodied
01884  * here is to use the rowcount of the smallest other base relation needed in
01885  * the join clauses used by the path.  (We could alternatively consider the
01886  * largest one, but that seems too optimistic.)  This is of course the right
01887  * answer for single-other-relation cases, and it seems like a reasonable
01888  * zero-order approximation for multiway-join cases.
01889  *
01890  * Note: for this to work, allpaths.c must establish all baserel size
01891  * estimates before it begins to compute paths, or at least before it
01892  * calls create_index_paths().
01893  */
01894 static double
01895 get_loop_count(PlannerInfo *root, Relids outer_relids)
01896 {
01897     double      result = 1.0;
01898 
01899     /* For a non-parameterized path, just return 1.0 quickly */
01900     if (outer_relids != NULL)
01901     {
01902         int         relid;
01903 
01904         /* Need a working copy since bms_first_member is destructive */
01905         outer_relids = bms_copy(outer_relids);
01906         while ((relid = bms_first_member(outer_relids)) >= 0)
01907         {
01908             RelOptInfo *outer_rel;
01909 
01910             /* Paranoia: ignore bogus relid indexes */
01911             if (relid >= root->simple_rel_array_size)
01912                 continue;
01913             outer_rel = root->simple_rel_array[relid];
01914             if (outer_rel == NULL)
01915                 continue;
01916             Assert(outer_rel->relid == relid);  /* sanity check on array */
01917 
01918             /* Other relation could be proven empty, if so ignore */
01919             if (IS_DUMMY_REL(outer_rel))
01920                 continue;
01921 
01922             /* Otherwise, rel's rows estimate should be valid by now */
01923             Assert(outer_rel->rows > 0);
01924 
01925             /* Remember smallest row count estimate among the outer rels */
01926             if (result == 1.0 || result > outer_rel->rows)
01927                 result = outer_rel->rows;
01928         }
01929         bms_free(outer_relids);
01930     }
01931     return result;
01932 }
01933 
01934 
01935 /****************************************************************************
01936  *              ----  ROUTINES TO CHECK QUERY CLAUSES  ----
01937  ****************************************************************************/
01938 
01939 /*
01940  * match_restriction_clauses_to_index
01941  *    Identify restriction clauses for the rel that match the index.
01942  *    Matching clauses are added to *clauseset.
01943  */
01944 static void
01945 match_restriction_clauses_to_index(RelOptInfo *rel, IndexOptInfo *index,
01946                                    IndexClauseSet *clauseset)
01947 {
01948     match_clauses_to_index(index, rel->baserestrictinfo, clauseset);
01949 }
01950 
01951 /*
01952  * match_join_clauses_to_index
01953  *    Identify join clauses for the rel that match the index.
01954  *    Matching clauses are added to *clauseset.
01955  *    Also, add any potentially usable join OR clauses to *joinorclauses.
01956  */
01957 static void
01958 match_join_clauses_to_index(PlannerInfo *root,
01959                             RelOptInfo *rel, IndexOptInfo *index,
01960                             Relids lateral_referencers,
01961                             IndexClauseSet *clauseset,
01962                             List **joinorclauses)
01963 {
01964     ListCell   *lc;
01965 
01966     /* Scan the rel's join clauses */
01967     foreach(lc, rel->joininfo)
01968     {
01969         RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
01970 
01971         /* Check if clause can be moved to this rel */
01972         if (!join_clause_is_movable_to(rinfo, rel->relid))
01973             continue;
01974 
01975         /* Not useful if it conflicts with any LATERAL references */
01976         if (bms_overlap(rinfo->clause_relids, lateral_referencers))
01977             continue;
01978 
01979         /* Potentially usable, so see if it matches the index or is an OR */
01980         if (restriction_is_or_clause(rinfo))
01981             *joinorclauses = lappend(*joinorclauses, rinfo);
01982         else
01983             match_clause_to_index(index, rinfo, clauseset);
01984     }
01985 }
01986 
01987 /*
01988  * match_eclass_clauses_to_index
01989  *    Identify EquivalenceClass join clauses for the rel that match the index.
01990  *    Matching clauses are added to *clauseset.
01991  */
01992 static void
01993 match_eclass_clauses_to_index(PlannerInfo *root, IndexOptInfo *index,
01994                               Relids lateral_referencers,
01995                               IndexClauseSet *clauseset)
01996 {
01997     int         indexcol;
01998 
01999     /* No work if rel is not in any such ECs */
02000     if (!index->rel->has_eclass_joins)
02001         return;
02002 
02003     for (indexcol = 0; indexcol < index->ncolumns; indexcol++)
02004     {
02005         ec_member_matches_arg arg;
02006         List       *clauses;
02007 
02008         /* Generate clauses, skipping any that join to lateral_referencers */
02009         arg.index = index;
02010         arg.indexcol = indexcol;
02011         clauses = generate_implied_equalities_for_column(root,
02012                                                          index->rel,
02013                                                   ec_member_matches_indexcol,
02014                                                          (void *) &arg,
02015                                                          lateral_referencers);
02016 
02017         /*
02018          * We have to check whether the results actually do match the index,
02019          * since for non-btree indexes the EC's equality operators might not
02020          * be in the index opclass (cf ec_member_matches_indexcol).
02021          */
02022         match_clauses_to_index(index, clauses, clauseset);
02023     }
02024 }
02025 
02026 /*
02027  * match_clauses_to_index
02028  *    Perform match_clause_to_index() for each clause in a list.
02029  *    Matching clauses are added to *clauseset.
02030  */
02031 static void
02032 match_clauses_to_index(IndexOptInfo *index,
02033                        List *clauses,
02034                        IndexClauseSet *clauseset)
02035 {
02036     ListCell   *lc;
02037 
02038     foreach(lc, clauses)
02039     {
02040         RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
02041 
02042         Assert(IsA(rinfo, RestrictInfo));
02043         match_clause_to_index(index, rinfo, clauseset);
02044     }
02045 }
02046 
02047 /*
02048  * match_clause_to_index
02049  *    Test whether a qual clause can be used with an index.
02050  *
02051  * If the clause is usable, add it to the appropriate list in *clauseset.
02052  * *clauseset must be initialized to zeroes before first call.
02053  *
02054  * Note: in some circumstances we may find the same RestrictInfos coming from
02055  * multiple places.  Defend against redundant outputs by refusing to add a
02056  * clause twice (pointer equality should be a good enough check for this).
02057  *
02058  * Note: it's possible that a badly-defined index could have multiple matching
02059  * columns.  We always select the first match if so; this avoids scenarios
02060  * wherein we get an inflated idea of the index's selectivity by using the
02061  * same clause multiple times with different index columns.
02062  */
02063 static void
02064 match_clause_to_index(IndexOptInfo *index,
02065                       RestrictInfo *rinfo,
02066                       IndexClauseSet *clauseset)
02067 {
02068     int         indexcol;
02069 
02070     for (indexcol = 0; indexcol < index->ncolumns; indexcol++)
02071     {
02072         if (match_clause_to_indexcol(index,
02073                                      indexcol,
02074                                      rinfo))
02075         {
02076             clauseset->indexclauses[indexcol] =
02077                 list_append_unique_ptr(clauseset->indexclauses[indexcol],
02078                                        rinfo);
02079             clauseset->nonempty = true;
02080             return;
02081         }
02082     }
02083 }
02084 
02085 /*
02086  * match_clause_to_indexcol()
02087  *    Determines whether a restriction clause matches a column of an index.
02088  *
02089  *    To match an index normally, the clause:
02090  *
02091  *    (1)  must be in the form (indexkey op const) or (const op indexkey);
02092  *         and
02093  *    (2)  must contain an operator which is in the same family as the index
02094  *         operator for this column, or is a "special" operator as recognized
02095  *         by match_special_index_operator();
02096  *         and
02097  *    (3)  must match the collation of the index, if collation is relevant.
02098  *
02099  *    Our definition of "const" is exceedingly liberal: we allow anything that
02100  *    doesn't involve a volatile function or a Var of the index's relation.
02101  *    In particular, Vars belonging to other relations of the query are
02102  *    accepted here, since a clause of that form can be used in a
02103  *    parameterized indexscan.  It's the responsibility of higher code levels
02104  *    to manage restriction and join clauses appropriately.
02105  *
02106  *    Note: we do need to check for Vars of the index's relation on the
02107  *    "const" side of the clause, since clauses like (a.f1 OP (b.f2 OP a.f3))
02108  *    are not processable by a parameterized indexscan on a.f1, whereas
02109  *    something like (a.f1 OP (b.f2 OP c.f3)) is.
02110  *
02111  *    Presently, the executor can only deal with indexquals that have the
02112  *    indexkey on the left, so we can only use clauses that have the indexkey
02113  *    on the right if we can commute the clause to put the key on the left.
02114  *    We do not actually do the commuting here, but we check whether a
02115  *    suitable commutator operator is available.
02116  *
02117  *    If the index has a collation, the clause must have the same collation.
02118  *    For collation-less indexes, we assume it doesn't matter; this is
02119  *    necessary for cases like "hstore ? text", wherein hstore's operators
02120  *    don't care about collation but the clause will get marked with a
02121  *    collation anyway because of the text argument.  (This logic is
02122  *    embodied in the macro IndexCollMatchesExprColl.)
02123  *
02124  *    It is also possible to match RowCompareExpr clauses to indexes (but
02125  *    currently, only btree indexes handle this).  In this routine we will
02126  *    report a match if the first column of the row comparison matches the
02127  *    target index column.  This is sufficient to guarantee that some index
02128  *    condition can be constructed from the RowCompareExpr --- whether the
02129  *    remaining columns match the index too is considered in
02130  *    adjust_rowcompare_for_index().
02131  *
02132  *    It is also possible to match ScalarArrayOpExpr clauses to indexes, when
02133  *    the clause is of the form "indexkey op ANY (arrayconst)".
02134  *
02135  *    For boolean indexes, it is also possible to match the clause directly
02136  *    to the indexkey; or perhaps the clause is (NOT indexkey).
02137  *
02138  * 'index' is the index of interest.
02139  * 'indexcol' is a column number of 'index' (counting from 0).
02140  * 'rinfo' is the clause to be tested (as a RestrictInfo node).
02141  *
02142  * Returns true if the clause can be used with this index key.
02143  *
02144  * NOTE:  returns false if clause is an OR or AND clause; it is the
02145  * responsibility of higher-level routines to cope with those.
02146  */
02147 static bool
02148 match_clause_to_indexcol(IndexOptInfo *index,
02149                          int indexcol,
02150                          RestrictInfo *rinfo)
02151 {
02152     Expr       *clause = rinfo->clause;
02153     Index       index_relid = index->rel->relid;
02154     Oid         opfamily = index->opfamily[indexcol];
02155     Oid         idxcollation = index->indexcollations[indexcol];
02156     Node       *leftop,
02157                *rightop;
02158     Relids      left_relids;
02159     Relids      right_relids;
02160     Oid         expr_op;
02161     Oid         expr_coll;
02162     bool        plain_op;
02163 
02164     /*
02165      * Never match pseudoconstants to indexes.  (Normally this could not
02166      * happen anyway, since a pseudoconstant clause couldn't contain a Var,
02167      * but what if someone builds an expression index on a constant? It's not
02168      * totally unreasonable to do so with a partial index, either.)
02169      */
02170     if (rinfo->pseudoconstant)
02171         return false;
02172 
02173     /* First check for boolean-index cases. */
02174     if (IsBooleanOpfamily(opfamily))
02175     {
02176         if (match_boolean_index_clause((Node *) clause, indexcol, index))
02177             return true;
02178     }
02179 
02180     /*
02181      * Clause must be a binary opclause, or possibly a ScalarArrayOpExpr
02182      * (which is always binary, by definition).  Or it could be a
02183      * RowCompareExpr, which we pass off to match_rowcompare_to_indexcol().
02184      * Or, if the index supports it, we can handle IS NULL/NOT NULL clauses.
02185      */
02186     if (is_opclause(clause))
02187     {
02188         leftop = get_leftop(clause);
02189         rightop = get_rightop(clause);
02190         if (!leftop || !rightop)
02191             return false;
02192         left_relids = rinfo->left_relids;
02193         right_relids = rinfo->right_relids;
02194         expr_op = ((OpExpr *) clause)->opno;
02195         expr_coll = ((OpExpr *) clause)->inputcollid;
02196         plain_op = true;
02197     }
02198     else if (clause && IsA(clause, ScalarArrayOpExpr))
02199     {
02200         ScalarArrayOpExpr *saop = (ScalarArrayOpExpr *) clause;
02201 
02202         /* We only accept ANY clauses, not ALL */
02203         if (!saop->useOr)
02204             return false;
02205         leftop = (Node *) linitial(saop->args);
02206         rightop = (Node *) lsecond(saop->args);
02207         left_relids = NULL;     /* not actually needed */
02208         right_relids = pull_varnos(rightop);
02209         expr_op = saop->opno;
02210         expr_coll = saop->inputcollid;
02211         plain_op = false;
02212     }
02213     else if (clause && IsA(clause, RowCompareExpr))
02214     {
02215         return match_rowcompare_to_indexcol(index, indexcol,
02216                                             opfamily, idxcollation,
02217                                             (RowCompareExpr *) clause);
02218     }
02219     else if (index->amsearchnulls && IsA(clause, NullTest))
02220     {
02221         NullTest   *nt = (NullTest *) clause;
02222 
02223         if (!nt->argisrow &&
02224             match_index_to_operand((Node *) nt->arg, indexcol, index))
02225             return true;
02226         return false;
02227     }
02228     else
02229         return false;
02230 
02231     /*
02232      * Check for clauses of the form: (indexkey operator constant) or
02233      * (constant operator indexkey).  See above notes about const-ness.
02234      */
02235     if (match_index_to_operand(leftop, indexcol, index) &&
02236         !bms_is_member(index_relid, right_relids) &&
02237         !contain_volatile_functions(rightop))
02238     {
02239         if (IndexCollMatchesExprColl(idxcollation, expr_coll) &&
02240             is_indexable_operator(expr_op, opfamily, true))
02241             return true;
02242 
02243         /*
02244          * If we didn't find a member of the index's opfamily, see whether it
02245          * is a "special" indexable operator.
02246          */
02247         if (plain_op &&
02248             match_special_index_operator(clause, opfamily,
02249                                          idxcollation, true))
02250             return true;
02251         return false;
02252     }
02253 
02254     if (plain_op &&
02255         match_index_to_operand(rightop, indexcol, index) &&
02256         !bms_is_member(index_relid, left_relids) &&
02257         !contain_volatile_functions(leftop))
02258     {
02259         if (IndexCollMatchesExprColl(idxcollation, expr_coll) &&
02260             is_indexable_operator(expr_op, opfamily, false))
02261             return true;
02262 
02263         /*
02264          * If we didn't find a member of the index's opfamily, see whether it
02265          * is a "special" indexable operator.
02266          */
02267         if (match_special_index_operator(clause, opfamily,
02268                                          idxcollation, false))
02269             return true;
02270         return false;
02271     }
02272 
02273     return false;
02274 }
02275 
02276 /*
02277  * is_indexable_operator
02278  *    Does the operator match the specified index opfamily?
02279  *
02280  * If the indexkey is on the right, what we actually want to know
02281  * is whether the operator has a commutator operator that matches
02282  * the opfamily.
02283  */
02284 static bool
02285 is_indexable_operator(Oid expr_op, Oid opfamily, bool indexkey_on_left)
02286 {
02287     /* Get the commuted operator if necessary */
02288     if (!indexkey_on_left)
02289     {
02290         expr_op = get_commutator(expr_op);
02291         if (expr_op == InvalidOid)
02292             return false;
02293     }
02294 
02295     /* OK if the (commuted) operator is a member of the index's opfamily */
02296     return op_in_opfamily(expr_op, opfamily);
02297 }
02298 
02299 /*
02300  * match_rowcompare_to_indexcol()
02301  *    Handles the RowCompareExpr case for match_clause_to_indexcol(),
02302  *    which see for comments.
02303  */
02304 static bool
02305 match_rowcompare_to_indexcol(IndexOptInfo *index,
02306                              int indexcol,
02307                              Oid opfamily,
02308                              Oid idxcollation,
02309                              RowCompareExpr *clause)
02310 {
02311     Index       index_relid = index->rel->relid;
02312     Node       *leftop,
02313                *rightop;
02314     Oid         expr_op;
02315     Oid         expr_coll;
02316 
02317     /* Forget it if we're not dealing with a btree index */
02318     if (index->relam != BTREE_AM_OID)
02319         return false;
02320 
02321     /*
02322      * We could do the matching on the basis of insisting that the opfamily
02323      * shown in the RowCompareExpr be the same as the index column's opfamily,
02324      * but that could fail in the presence of reverse-sort opfamilies: it'd be
02325      * a matter of chance whether RowCompareExpr had picked the forward or
02326      * reverse-sort family.  So look only at the operator, and match if it is
02327      * a member of the index's opfamily (after commutation, if the indexkey is
02328      * on the right).  We'll worry later about whether any additional
02329      * operators are matchable to the index.
02330      */
02331     leftop = (Node *) linitial(clause->largs);
02332     rightop = (Node *) linitial(clause->rargs);
02333     expr_op = linitial_oid(clause->opnos);
02334     expr_coll = linitial_oid(clause->inputcollids);
02335 
02336     /* Collations must match, if relevant */
02337     if (!IndexCollMatchesExprColl(idxcollation, expr_coll))
02338         return false;
02339 
02340     /*
02341      * These syntactic tests are the same as in match_clause_to_indexcol()
02342      */
02343     if (match_index_to_operand(leftop, indexcol, index) &&
02344         !bms_is_member(index_relid, pull_varnos(rightop)) &&
02345         !contain_volatile_functions(rightop))
02346     {
02347         /* OK, indexkey is on left */
02348     }
02349     else if (match_index_to_operand(rightop, indexcol, index) &&
02350              !bms_is_member(index_relid, pull_varnos(leftop)) &&
02351              !contain_volatile_functions(leftop))
02352     {
02353         /* indexkey is on right, so commute the operator */
02354         expr_op = get_commutator(expr_op);
02355         if (expr_op == InvalidOid)
02356             return false;
02357     }
02358     else
02359         return false;
02360 
02361     /* We're good if the operator is the right type of opfamily member */
02362     switch (get_op_opfamily_strategy(expr_op, opfamily))
02363     {
02364         case BTLessStrategyNumber:
02365         case BTLessEqualStrategyNumber:
02366         case BTGreaterEqualStrategyNumber:
02367         case BTGreaterStrategyNumber:
02368             return true;
02369     }
02370 
02371     return false;
02372 }
02373 
02374 
02375 /****************************************************************************
02376  *              ----  ROUTINES TO CHECK ORDERING OPERATORS  ----
02377  ****************************************************************************/
02378 
02379 /*
02380  * match_pathkeys_to_index
02381  *      Test whether an index can produce output ordered according to the
02382  *      given pathkeys using "ordering operators".
02383  *
02384  * If it can, return a list of suitable ORDER BY expressions, each of the form
02385  * "indexedcol operator pseudoconstant", along with an integer list of the
02386  * index column numbers (zero based) that each clause would be used with.
02387  * NIL lists are returned if the ordering is not achievable this way.
02388  *
02389  * On success, the result list is ordered by pathkeys, and in fact is
02390  * one-to-one with the requested pathkeys.
02391  */
02392 static void
02393 match_pathkeys_to_index(IndexOptInfo *index, List *pathkeys,
02394                         List **orderby_clauses_p,
02395                         List **clause_columns_p)
02396 {
02397     List       *orderby_clauses = NIL;
02398     List       *clause_columns = NIL;
02399     ListCell   *lc1;
02400 
02401     *orderby_clauses_p = NIL;   /* set default results */
02402     *clause_columns_p = NIL;
02403 
02404     /* Only indexes with the amcanorderbyop property are interesting here */
02405     if (!index->amcanorderbyop)
02406         return;
02407 
02408     foreach(lc1, pathkeys)
02409     {
02410         PathKey    *pathkey = (PathKey *) lfirst(lc1);
02411         bool        found = false;
02412         ListCell   *lc2;
02413 
02414         /*
02415          * Note: for any failure to match, we just return NIL immediately.
02416          * There is no value in matching just some of the pathkeys.
02417          */
02418 
02419         /* Pathkey must request default sort order for the target opfamily */
02420         if (pathkey->pk_strategy != BTLessStrategyNumber ||
02421             pathkey->pk_nulls_first)
02422             return;
02423 
02424         /* If eclass is volatile, no hope of using an indexscan */
02425         if (pathkey->pk_eclass->ec_has_volatile)
02426             return;
02427 
02428         /*
02429          * Try to match eclass member expression(s) to index.  Note that child
02430          * EC members are considered, but only when they belong to the target
02431          * relation.  (Unlike regular members, the same expression could be a
02432          * child member of more than one EC.  Therefore, the same index could
02433          * be considered to match more than one pathkey list, which is OK
02434          * here.  See also get_eclass_for_sort_expr.)
02435          */
02436         foreach(lc2, pathkey->pk_eclass->ec_members)
02437         {
02438             EquivalenceMember *member = (EquivalenceMember *) lfirst(lc2);
02439             int         indexcol;
02440 
02441             /* No possibility of match if it references other relations */
02442             if (!bms_equal(member->em_relids, index->rel->relids))
02443                 continue;
02444 
02445             /*
02446              * We allow any column of the index to match each pathkey; they
02447              * don't have to match left-to-right as you might expect.  This is
02448              * correct for GiST, which is the sole existing AM supporting
02449              * amcanorderbyop.  We might need different logic in future for
02450              * other implementations.
02451              */
02452             for (indexcol = 0; indexcol < index->ncolumns; indexcol++)
02453             {
02454                 Expr       *expr;
02455 
02456                 expr = match_clause_to_ordering_op(index,
02457                                                    indexcol,
02458                                                    member->em_expr,
02459                                                    pathkey->pk_opfamily);
02460                 if (expr)
02461                 {
02462                     orderby_clauses = lappend(orderby_clauses, expr);
02463                     clause_columns = lappend_int(clause_columns, indexcol);
02464                     found = true;
02465                     break;
02466                 }
02467             }
02468 
02469             if (found)          /* don't want to look at remaining members */
02470                 break;
02471         }
02472 
02473         if (!found)             /* fail if no match for this pathkey */
02474             return;
02475     }
02476 
02477     *orderby_clauses_p = orderby_clauses;       /* success! */
02478     *clause_columns_p = clause_columns;
02479 }
02480 
02481 /*
02482  * match_clause_to_ordering_op
02483  *    Determines whether an ordering operator expression matches an
02484  *    index column.
02485  *
02486  *    This is similar to, but simpler than, match_clause_to_indexcol.
02487  *    We only care about simple OpExpr cases.  The input is a bare
02488  *    expression that is being ordered by, which must be of the form
02489  *    (indexkey op const) or (const op indexkey) where op is an ordering
02490  *    operator for the column's opfamily.
02491  *
02492  * 'index' is the index of interest.
02493  * 'indexcol' is a column number of 'index' (counting from 0).
02494  * 'clause' is the ordering expression to be tested.
02495  * 'pk_opfamily' is the btree opfamily describing the required sort order.
02496  *
02497  * Note that we currently do not consider the collation of the ordering
02498  * operator's result.  In practical cases the result type will be numeric
02499  * and thus have no collation, and it's not very clear what to match to
02500  * if it did have a collation.  The index's collation should match the
02501  * ordering operator's input collation, not its result.
02502  *
02503  * If successful, return 'clause' as-is if the indexkey is on the left,
02504  * otherwise a commuted copy of 'clause'.  If no match, return NULL.
02505  */
02506 static Expr *
02507 match_clause_to_ordering_op(IndexOptInfo *index,
02508                             int indexcol,
02509                             Expr *clause,
02510                             Oid pk_opfamily)
02511 {
02512     Oid         opfamily = index->opfamily[indexcol];
02513     Oid         idxcollation = index->indexcollations[indexcol];
02514     Node       *leftop,
02515                *rightop;
02516     Oid         expr_op;
02517     Oid         expr_coll;
02518     Oid         sortfamily;
02519     bool        commuted;
02520 
02521     /*
02522      * Clause must be a binary opclause.
02523      */
02524     if (!is_opclause(clause))
02525         return NULL;
02526     leftop = get_leftop(clause);
02527     rightop = get_rightop(clause);
02528     if (!leftop || !rightop)
02529         return NULL;
02530     expr_op = ((OpExpr *) clause)->opno;
02531     expr_coll = ((OpExpr *) clause)->inputcollid;
02532 
02533     /*
02534      * We can forget the whole thing right away if wrong collation.
02535      */
02536     if (!IndexCollMatchesExprColl(idxcollation, expr_coll))
02537         return NULL;
02538 
02539     /*
02540      * Check for clauses of the form: (indexkey operator constant) or
02541      * (constant operator indexkey).
02542      */
02543     if (match_index_to_operand(leftop, indexcol, index) &&
02544         !contain_var_clause(rightop) &&
02545         !contain_volatile_functions(rightop))
02546     {
02547         commuted = false;
02548     }
02549     else if (match_index_to_operand(rightop, indexcol, index) &&
02550              !contain_var_clause(leftop) &&
02551              !contain_volatile_functions(leftop))
02552     {
02553         /* Might match, but we need a commuted operator */
02554         expr_op = get_commutator(expr_op);
02555         if (expr_op == InvalidOid)
02556             return NULL;
02557         commuted = true;
02558     }
02559     else
02560         return NULL;
02561 
02562     /*
02563      * Is the (commuted) operator an ordering operator for the opfamily? And
02564      * if so, does it yield the right sorting semantics?
02565      */
02566     sortfamily = get_op_opfamily_sortfamily(expr_op, opfamily);
02567     if (sortfamily != pk_opfamily)
02568         return NULL;
02569 
02570     /* We have a match.  Return clause or a commuted version thereof. */
02571     if (commuted)
02572     {
02573         OpExpr     *newclause = makeNode(OpExpr);
02574 
02575         /* flat-copy all the fields of clause */
02576         memcpy(newclause, clause, sizeof(OpExpr));
02577 
02578         /* commute it */
02579         newclause->opno = expr_op;
02580         newclause->opfuncid = InvalidOid;
02581         newclause->args = list_make2(rightop, leftop);
02582 
02583         clause = (Expr *) newclause;
02584     }
02585 
02586     return clause;
02587 }
02588 
02589 
02590 /****************************************************************************
02591  *              ----  ROUTINES TO DO PARTIAL INDEX PREDICATE TESTS  ----
02592  ****************************************************************************/
02593 
02594 /*
02595  * check_partial_indexes
02596  *      Check each partial index of the relation, and mark it predOK if
02597  *      the index's predicate is satisfied for this query.
02598  *
02599  * Note: it is possible for this to get re-run after adding more restrictions
02600  * to the rel; so we might be able to prove more indexes OK.  We assume that
02601  * adding more restrictions can't make an index not OK.
02602  */
02603 void
02604 check_partial_indexes(PlannerInfo *root, RelOptInfo *rel)
02605 {
02606     List       *clauselist;
02607     bool        have_partial;
02608     Relids      otherrels;
02609     ListCell   *lc;
02610 
02611     /*
02612      * Frequently, there will be no partial indexes, so first check to make
02613      * sure there's something useful to do here.
02614      */
02615     have_partial = false;
02616     foreach(lc, rel->indexlist)
02617     {
02618         IndexOptInfo *index = (IndexOptInfo *) lfirst(lc);
02619 
02620         if (index->indpred == NIL)
02621             continue;           /* ignore non-partial indexes */
02622 
02623         if (index->predOK)
02624             continue;           /* don't repeat work if already proven OK */
02625 
02626         have_partial = true;
02627         break;
02628     }
02629     if (!have_partial)
02630         return;
02631 
02632     /*
02633      * Construct a list of clauses that we can assume true for the purpose
02634      * of proving the index(es) usable.  Restriction clauses for the rel are
02635      * always usable, and so are any join clauses that are "movable to" this
02636      * rel.  Also, we can consider any EC-derivable join clauses (which must
02637      * be "movable to" this rel, by definition).
02638      */
02639     clauselist = list_copy(rel->baserestrictinfo);
02640 
02641     /* Scan the rel's join clauses */
02642     foreach(lc, rel->joininfo)
02643     {
02644         RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
02645 
02646         /* Check if clause can be moved to this rel */
02647         if (!join_clause_is_movable_to(rinfo, rel->relid))
02648             continue;
02649 
02650         clauselist = lappend(clauselist, rinfo);
02651     }
02652 
02653     /*
02654      * Add on any equivalence-derivable join clauses.  Computing the correct
02655      * relid sets for generate_join_implied_equalities is slightly tricky
02656      * because the rel could be a child rel rather than a true baserel, and
02657      * in that case we must remove its parent's relid from all_baserels.
02658      */
02659     if (rel->reloptkind == RELOPT_OTHER_MEMBER_REL)
02660     {
02661         /* Lookup parent->child translation data */
02662         AppendRelInfo *appinfo = find_childrel_appendrelinfo(root, rel);
02663 
02664         otherrels = bms_difference(root->all_baserels,
02665                                    bms_make_singleton(appinfo->parent_relid));
02666     }
02667     else
02668         otherrels = bms_difference(root->all_baserels, rel->relids);
02669 
02670     if (!bms_is_empty(otherrels))
02671         clauselist =
02672             list_concat(clauselist,
02673                         generate_join_implied_equalities(root,
02674                                                          bms_union(rel->relids,
02675                                                                    otherrels),
02676                                                          otherrels,
02677                                                          rel));
02678 
02679     /* Now try to prove each index predicate true */
02680     foreach(lc, rel->indexlist)
02681     {
02682         IndexOptInfo *index = (IndexOptInfo *) lfirst(lc);
02683 
02684         if (index->indpred == NIL)
02685             continue;           /* ignore non-partial indexes */
02686 
02687         if (index->predOK)
02688             continue;           /* don't repeat work if already proven OK */
02689 
02690         index->predOK = predicate_implied_by(index->indpred, clauselist);
02691     }
02692 }
02693 
02694 /****************************************************************************
02695  *              ----  ROUTINES TO CHECK EXTERNALLY-VISIBLE CONDITIONS  ----
02696  ****************************************************************************/
02697 
02698 /*
02699  * ec_member_matches_indexcol
02700  *    Test whether an EquivalenceClass member matches an index column.
02701  *
02702  * This is a callback for use by generate_implied_equalities_for_column.
02703  */
02704 static bool
02705 ec_member_matches_indexcol(PlannerInfo *root, RelOptInfo *rel,
02706                            EquivalenceClass *ec, EquivalenceMember *em,
02707                            void *arg)
02708 {
02709     IndexOptInfo *index = ((ec_member_matches_arg *) arg)->index;
02710     int         indexcol = ((ec_member_matches_arg *) arg)->indexcol;
02711     Oid         curFamily = index->opfamily[indexcol];
02712     Oid         curCollation = index->indexcollations[indexcol];
02713 
02714     /*
02715      * If it's a btree index, we can reject it if its opfamily isn't
02716      * compatible with the EC, since no clause generated from the EC could be
02717      * used with the index.  For non-btree indexes, we can't easily tell
02718      * whether clauses generated from the EC could be used with the index, so
02719      * don't check the opfamily.  This might mean we return "true" for a
02720      * useless EC, so we have to recheck the results of
02721      * generate_implied_equalities_for_column; see
02722      * match_eclass_clauses_to_index.
02723      */
02724     if (index->relam == BTREE_AM_OID &&
02725         !list_member_oid(ec->ec_opfamilies, curFamily))
02726         return false;
02727 
02728     /* We insist on collation match for all index types, though */
02729     if (!IndexCollMatchesExprColl(curCollation, ec->ec_collation))
02730         return false;
02731 
02732     return match_index_to_operand((Node *) em->em_expr, indexcol, index);
02733 }
02734 
02735 /*
02736  * relation_has_unique_index_for
02737  *    Determine whether the relation provably has at most one row satisfying
02738  *    a set of equality conditions, because the conditions constrain all
02739  *    columns of some unique index.
02740  *
02741  * The conditions can be represented in either or both of two ways:
02742  * 1. A list of RestrictInfo nodes, where the caller has already determined
02743  * that each condition is a mergejoinable equality with an expression in
02744  * this relation on one side, and an expression not involving this relation
02745  * on the other.  The transient outer_is_left flag is used to identify which
02746  * side we should look at: left side if outer_is_left is false, right side
02747  * if it is true.
02748  * 2. A list of expressions in this relation, and a corresponding list of
02749  * equality operators. The caller must have already checked that the operators
02750  * represent equality.  (Note: the operators could be cross-type; the
02751  * expressions should correspond to their RHS inputs.)
02752  *
02753  * The caller need only supply equality conditions arising from joins;
02754  * this routine automatically adds in any usable baserestrictinfo clauses.
02755  * (Note that the passed-in restrictlist will be destructively modified!)
02756  */
02757 bool
02758 relation_has_unique_index_for(PlannerInfo *root, RelOptInfo *rel,
02759                               List *restrictlist,
02760                               List *exprlist, List *oprlist)
02761 {
02762     ListCell   *ic;
02763 
02764     Assert(list_length(exprlist) == list_length(oprlist));
02765 
02766     /* Short-circuit if no indexes... */
02767     if (rel->indexlist == NIL)
02768         return false;
02769 
02770     /*
02771      * Examine the rel's restriction clauses for usable var = const clauses
02772      * that we can add to the restrictlist.
02773      */
02774     foreach(ic, rel->baserestrictinfo)
02775     {
02776         RestrictInfo *restrictinfo = (RestrictInfo *) lfirst(ic);
02777 
02778         /*
02779          * Note: can_join won't be set for a restriction clause, but
02780          * mergeopfamilies will be if it has a mergejoinable operator and
02781          * doesn't contain volatile functions.
02782          */
02783         if (restrictinfo->mergeopfamilies == NIL)
02784             continue;           /* not mergejoinable */
02785 
02786         /*
02787          * The clause certainly doesn't refer to anything but the given rel.
02788          * If either side is pseudoconstant then we can use it.
02789          */
02790         if (bms_is_empty(restrictinfo->left_relids))
02791         {
02792             /* righthand side is inner */
02793             restrictinfo->outer_is_left = true;
02794         }
02795         else if (bms_is_empty(restrictinfo->right_relids))
02796         {
02797             /* lefthand side is inner */
02798             restrictinfo->outer_is_left = false;
02799         }
02800         else
02801             continue;
02802 
02803         /* OK, add to list */
02804         restrictlist = lappend(restrictlist, restrictinfo);
02805     }
02806 
02807     /* Short-circuit the easy case */
02808     if (restrictlist == NIL && exprlist == NIL)
02809         return false;
02810 
02811     /* Examine each index of the relation ... */
02812     foreach(ic, rel->indexlist)
02813     {
02814         IndexOptInfo *ind = (IndexOptInfo *) lfirst(ic);
02815         int         c;
02816 
02817         /*
02818          * If the index is not unique, or not immediately enforced, or if it's
02819          * a partial index that doesn't match the query, it's useless here.
02820          */
02821         if (!ind->unique || !ind->immediate ||
02822             (ind->indpred != NIL && !ind->predOK))
02823             continue;
02824 
02825         /*
02826          * Try to find each index column in the lists of conditions.  This is
02827          * O(N^2) or worse, but we expect all the lists to be short.
02828          */
02829         for (c = 0; c < ind->ncolumns; c++)
02830         {
02831             bool        matched = false;
02832             ListCell   *lc;
02833             ListCell   *lc2;
02834 
02835             foreach(lc, restrictlist)
02836             {
02837                 RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
02838                 Node       *rexpr;
02839 
02840                 /*
02841                  * The condition's equality operator must be a member of the
02842                  * index opfamily, else it is not asserting the right kind of
02843                  * equality behavior for this index.  We check this first
02844                  * since it's probably cheaper than match_index_to_operand().
02845                  */
02846                 if (!list_member_oid(rinfo->mergeopfamilies, ind->opfamily[c]))
02847                     continue;
02848 
02849                 /*
02850                  * XXX at some point we may need to check collations here too.
02851                  * For the moment we assume all collations reduce to the same
02852                  * notion of equality.
02853                  */
02854 
02855                 /* OK, see if the condition operand matches the index key */
02856                 if (rinfo->outer_is_left)
02857                     rexpr = get_rightop(rinfo->clause);
02858                 else
02859                     rexpr = get_leftop(rinfo->clause);
02860 
02861                 if (match_index_to_operand(rexpr, c, ind))
02862                 {
02863                     matched = true;     /* column is unique */
02864                     break;
02865                 }
02866             }
02867 
02868             if (matched)
02869                 continue;
02870 
02871             forboth(lc, exprlist, lc2, oprlist)
02872             {
02873                 Node       *expr = (Node *) lfirst(lc);
02874                 Oid         opr = lfirst_oid(lc2);
02875 
02876                 /* See if the expression matches the index key */
02877                 if (!match_index_to_operand(expr, c, ind))
02878                     continue;
02879 
02880                 /*
02881                  * The equality operator must be a member of the index
02882                  * opfamily, else it is not asserting the right kind of
02883                  * equality behavior for this index.  We assume the caller
02884                  * determined it is an equality operator, so we don't need to
02885                  * check any more tightly than this.
02886                  */
02887                 if (!op_in_opfamily(opr, ind->opfamily[c]))
02888                     continue;
02889 
02890                 /*
02891                  * XXX at some point we may need to check collations here too.
02892                  * For the moment we assume all collations reduce to the same
02893                  * notion of equality.
02894                  */
02895 
02896                 matched = true; /* column is unique */
02897                 break;
02898             }
02899 
02900             if (!matched)
02901                 break;          /* no match; this index doesn't help us */
02902         }
02903 
02904         /* Matched all columns of this index? */
02905         if (c == ind->ncolumns)
02906             return true;
02907     }
02908 
02909     return false;
02910 }
02911 
02912 
02913 /****************************************************************************
02914  *              ----  ROUTINES TO CHECK OPERANDS  ----
02915  ****************************************************************************/
02916 
02917 /*
02918  * match_index_to_operand()
02919  *    Generalized test for a match between an index's key
02920  *    and the operand on one side of a restriction or join clause.
02921  *
02922  * operand: the nodetree to be compared to the index
02923  * indexcol: the column number of the index (counting from 0)
02924  * index: the index of interest
02925  *
02926  * Note that we aren't interested in collations here; the caller must check
02927  * for a collation match, if it's dealing with an operator where that matters.
02928  *
02929  * This is exported for use in selfuncs.c.
02930  */
02931 bool
02932 match_index_to_operand(Node *operand,
02933                        int indexcol,
02934                        IndexOptInfo *index)
02935 {
02936     int         indkey;
02937 
02938     /*
02939      * Ignore any RelabelType node above the operand.   This is needed to be
02940      * able to apply indexscanning in binary-compatible-operator cases. Note:
02941      * we can assume there is at most one RelabelType node;
02942      * eval_const_expressions() will have simplified if more than one.
02943      */
02944     if (operand && IsA(operand, RelabelType))
02945         operand = (Node *) ((RelabelType *) operand)->arg;
02946 
02947     indkey = index->indexkeys[indexcol];
02948     if (indkey != 0)
02949     {
02950         /*
02951          * Simple index column; operand must be a matching Var.
02952          */
02953         if (operand && IsA(operand, Var) &&
02954             index->rel->relid == ((Var *) operand)->varno &&
02955             indkey == ((Var *) operand)->varattno)
02956             return true;
02957     }
02958     else
02959     {
02960         /*
02961          * Index expression; find the correct expression.  (This search could
02962          * be avoided, at the cost of complicating all the callers of this
02963          * routine; doesn't seem worth it.)
02964          */
02965         ListCell   *indexpr_item;
02966         int         i;
02967         Node       *indexkey;
02968 
02969         indexpr_item = list_head(index->indexprs);
02970         for (i = 0; i < indexcol; i++)
02971         {
02972             if (index->indexkeys[i] == 0)
02973             {
02974                 if (indexpr_item == NULL)
02975                     elog(ERROR, "wrong number of index expressions");
02976                 indexpr_item = lnext(indexpr_item);
02977             }
02978         }
02979         if (indexpr_item == NULL)
02980             elog(ERROR, "wrong number of index expressions");
02981         indexkey = (Node *) lfirst(indexpr_item);
02982 
02983         /*
02984          * Does it match the operand?  Again, strip any relabeling.
02985          */
02986         if (indexkey && IsA(indexkey, RelabelType))
02987             indexkey = (Node *) ((RelabelType *) indexkey)->arg;
02988 
02989         if (equal(indexkey, operand))
02990             return true;
02991     }
02992 
02993     return false;
02994 }
02995 
02996 /****************************************************************************
02997  *          ----  ROUTINES FOR "SPECIAL" INDEXABLE OPERATORS  ----
02998  ****************************************************************************/
02999 
03000 /*
03001  * These routines handle special optimization of operators that can be
03002  * used with index scans even though they are not known to the executor's
03003  * indexscan machinery.  The key idea is that these operators allow us
03004  * to derive approximate indexscan qual clauses, such that any tuples
03005  * that pass the operator clause itself must also satisfy the simpler
03006  * indexscan condition(s).  Then we can use the indexscan machinery
03007  * to avoid scanning as much of the table as we'd otherwise have to,
03008  * while applying the original operator as a qpqual condition to ensure
03009  * we deliver only the tuples we want.  (In essence, we're using a regular
03010  * index as if it were a lossy index.)
03011  *
03012  * An example of what we're doing is
03013  *          textfield LIKE 'abc%'
03014  * from which we can generate the indexscanable conditions
03015  *          textfield >= 'abc' AND textfield < 'abd'
03016  * which allow efficient scanning of an index on textfield.
03017  * (In reality, character set and collation issues make the transformation
03018  * from LIKE to indexscan limits rather harder than one might think ...
03019  * but that's the basic idea.)
03020  *
03021  * Another thing that we do with this machinery is to provide special
03022  * smarts for "boolean" indexes (that is, indexes on boolean columns
03023  * that support boolean equality).  We can transform a plain reference
03024  * to the indexkey into "indexkey = true", or "NOT indexkey" into
03025  * "indexkey = false", so as to make the expression indexable using the
03026  * regular index operators.  (As of Postgres 8.1, we must do this here
03027  * because constant simplification does the reverse transformation;
03028  * without this code there'd be no way to use such an index at all.)
03029  *
03030  * Three routines are provided here:
03031  *
03032  * match_special_index_operator() is just an auxiliary function for
03033  * match_clause_to_indexcol(); after the latter fails to recognize a
03034  * restriction opclause's operator as a member of an index's opfamily,
03035  * it asks match_special_index_operator() whether the clause should be
03036  * considered an indexqual anyway.
03037  *
03038  * match_boolean_index_clause() similarly detects clauses that can be
03039  * converted into boolean equality operators.
03040  *
03041  * expand_indexqual_conditions() converts a list of RestrictInfo nodes
03042  * (with implicit AND semantics across list elements) into a list of clauses
03043  * that the executor can actually handle.  For operators that are members of
03044  * the index's opfamily this transformation is a no-op, but clauses recognized
03045  * by match_special_index_operator() or match_boolean_index_clause() must be
03046  * converted into one or more "regular" indexqual conditions.
03047  */
03048 
03049 /*
03050  * match_boolean_index_clause
03051  *    Recognize restriction clauses that can be matched to a boolean index.
03052  *
03053  * This should be called only when IsBooleanOpfamily() recognizes the
03054  * index's operator family.  We check to see if the clause matches the
03055  * index's key.
03056  */
03057 static bool
03058 match_boolean_index_clause(Node *clause,
03059                            int indexcol,
03060                            IndexOptInfo *index)
03061 {
03062     /* Direct match? */
03063     if (match_index_to_operand(clause, indexcol, index))
03064         return true;
03065     /* NOT clause? */
03066     if (not_clause(clause))
03067     {
03068         if (match_index_to_operand((Node *) get_notclausearg((Expr *) clause),
03069                                    indexcol, index))
03070             return true;
03071     }
03072 
03073     /*
03074      * Since we only consider clauses at top level of WHERE, we can convert
03075      * indexkey IS TRUE and indexkey IS FALSE to index searches as well. The
03076      * different meaning for NULL isn't important.
03077      */
03078     else if (clause && IsA(clause, BooleanTest))
03079     {
03080         BooleanTest *btest = (BooleanTest *) clause;
03081 
03082         if (btest->booltesttype == IS_TRUE ||
03083             btest->booltesttype == IS_FALSE)
03084             if (match_index_to_operand((Node *) btest->arg,
03085                                        indexcol, index))
03086                 return true;
03087     }
03088     return false;
03089 }
03090 
03091 /*
03092  * match_special_index_operator
03093  *    Recognize restriction clauses that can be used to generate
03094  *    additional indexscanable qualifications.
03095  *
03096  * The given clause is already known to be a binary opclause having
03097  * the form (indexkey OP pseudoconst) or (pseudoconst OP indexkey),
03098  * but the OP proved not to be one of the index's opfamily operators.
03099  * Return 'true' if we can do something with it anyway.
03100  */
03101 static bool
03102 match_special_index_operator(Expr *clause, Oid opfamily, Oid idxcollation,
03103                              bool indexkey_on_left)
03104 {
03105     bool        isIndexable = false;
03106     Node       *rightop;
03107     Oid         expr_op;
03108     Oid         expr_coll;
03109     Const      *patt;
03110     Const      *prefix = NULL;
03111     Pattern_Prefix_Status pstatus = Pattern_Prefix_None;
03112 
03113     /*
03114      * Currently, all known special operators require the indexkey on the
03115      * left, but this test could be pushed into the switch statement if some
03116      * are added that do not...
03117      */
03118     if (!indexkey_on_left)
03119         return false;
03120 
03121     /* we know these will succeed */
03122     rightop = get_rightop(clause);
03123     expr_op = ((OpExpr *) clause)->opno;
03124     expr_coll = ((OpExpr *) clause)->inputcollid;
03125 
03126     /* again, required for all current special ops: */
03127     if (!IsA(rightop, Const) ||
03128         ((Const *) rightop)->constisnull)
03129         return false;
03130     patt = (Const *) rightop;
03131 
03132     switch (expr_op)
03133     {
03134         case OID_TEXT_LIKE_OP:
03135         case OID_BPCHAR_LIKE_OP:
03136         case OID_NAME_LIKE_OP:
03137             /* the right-hand const is type text for all of these */
03138             pstatus = pattern_fixed_prefix(patt, Pattern_Type_Like, expr_coll,
03139                                            &prefix, NULL);
03140             isIndexable = (pstatus != Pattern_Prefix_None);
03141             break;
03142 
03143         case OID_BYTEA_LIKE_OP:
03144             pstatus = pattern_fixed_prefix(patt, Pattern_Type_Like, expr_coll,
03145                                            &prefix, NULL);
03146             isIndexable = (pstatus != Pattern_Prefix_None);
03147             break;
03148 
03149         case OID_TEXT_ICLIKE_OP:
03150         case OID_BPCHAR_ICLIKE_OP:
03151         case OID_NAME_ICLIKE_OP:
03152             /* the right-hand const is type text for all of these */
03153             pstatus = pattern_fixed_prefix(patt, Pattern_Type_Like_IC, expr_coll,
03154                                            &prefix, NULL);
03155             isIndexable = (pstatus != Pattern_Prefix_None);
03156             break;
03157 
03158         case OID_TEXT_REGEXEQ_OP:
03159         case OID_BPCHAR_REGEXEQ_OP:
03160         case OID_NAME_REGEXEQ_OP:
03161             /* the right-hand const is type text for all of these */
03162             pstatus = pattern_fixed_prefix(patt, Pattern_Type_Regex, expr_coll,
03163                                            &prefix, NULL);
03164             isIndexable = (pstatus != Pattern_Prefix_None);
03165             break;
03166 
03167         case OID_TEXT_ICREGEXEQ_OP:
03168         case OID_BPCHAR_ICREGEXEQ_OP:
03169         case OID_NAME_ICREGEXEQ_OP:
03170             /* the right-hand const is type text for all of these */
03171             pstatus = pattern_fixed_prefix(patt, Pattern_Type_Regex_IC, expr_coll,
03172                                            &prefix, NULL);
03173             isIndexable = (pstatus != Pattern_Prefix_None);
03174             break;
03175 
03176         case OID_INET_SUB_OP:
03177         case OID_INET_SUBEQ_OP:
03178             isIndexable = true;
03179             break;
03180     }
03181 
03182     if (prefix)
03183     {
03184         pfree(DatumGetPointer(prefix->constvalue));
03185         pfree(prefix);
03186     }
03187 
03188     /* done if the expression doesn't look indexable */
03189     if (!isIndexable)
03190         return false;
03191 
03192     /*
03193      * Must also check that index's opfamily supports the operators we will
03194      * want to apply.  (A hash index, for example, will not support ">=".)
03195      * Currently, only btree and spgist support the operators we need.
03196      *
03197      * Note: actually, in the Pattern_Prefix_Exact case, we only need "=" so a
03198      * hash index would work.  Currently it doesn't seem worth checking for
03199      * that, however.
03200      *
03201      * We insist on the opfamily being the specific one we expect, else we'd
03202      * do the wrong thing if someone were to make a reverse-sort opfamily with
03203      * the same operators.
03204      *
03205      * The non-pattern opclasses will not sort the way we need in most non-C
03206      * locales.  We can use such an index anyway for an exact match (simple
03207      * equality), but not for prefix-match cases.  Note that here we are
03208      * looking at the index's collation, not the expression's collation --
03209      * this test is *not* dependent on the LIKE/regex operator's collation.
03210      */
03211     switch (expr_op)
03212     {
03213         case OID_TEXT_LIKE_OP:
03214         case OID_TEXT_ICLIKE_OP:
03215         case OID_TEXT_REGEXEQ_OP:
03216         case OID_TEXT_ICREGEXEQ_OP:
03217             isIndexable =
03218                 (opfamily == TEXT_PATTERN_BTREE_FAM_OID) ||
03219                 (opfamily == TEXT_SPGIST_FAM_OID) ||
03220                 (opfamily == TEXT_BTREE_FAM_OID &&
03221                  (pstatus == Pattern_Prefix_Exact ||
03222                   lc_collate_is_c(idxcollation)));
03223             break;
03224 
03225         case OID_BPCHAR_LIKE_OP:
03226         case OID_BPCHAR_ICLIKE_OP:
03227         case OID_BPCHAR_REGEXEQ_OP:
03228         case OID_BPCHAR_ICREGEXEQ_OP:
03229             isIndexable =
03230                 (opfamily == BPCHAR_PATTERN_BTREE_FAM_OID) ||
03231                 (opfamily == BPCHAR_BTREE_FAM_OID &&
03232                  (pstatus == Pattern_Prefix_Exact ||
03233                   lc_collate_is_c(idxcollation)));
03234             break;
03235 
03236         case OID_NAME_LIKE_OP:
03237         case OID_NAME_ICLIKE_OP:
03238         case OID_NAME_REGEXEQ_OP:
03239         case OID_NAME_ICREGEXEQ_OP:
03240             /* name uses locale-insensitive sorting */
03241             isIndexable = (opfamily == NAME_BTREE_FAM_OID);
03242             break;
03243 
03244         case OID_BYTEA_LIKE_OP:
03245             isIndexable = (opfamily == BYTEA_BTREE_FAM_OID);
03246             break;
03247 
03248         case OID_INET_SUB_OP:
03249         case OID_INET_SUBEQ_OP:
03250             isIndexable = (opfamily == NETWORK_BTREE_FAM_OID);
03251             break;
03252     }
03253 
03254     return isIndexable;
03255 }
03256 
03257 /*
03258  * expand_indexqual_conditions
03259  *    Given a list of RestrictInfo nodes, produce a list of directly usable
03260  *    index qual clauses.
03261  *
03262  * Standard qual clauses (those in the index's opfamily) are passed through
03263  * unchanged.  Boolean clauses and "special" index operators are expanded
03264  * into clauses that the indexscan machinery will know what to do with.
03265  * RowCompare clauses are simplified if necessary to create a clause that is
03266  * fully checkable by the index.
03267  *
03268  * In addition to the expressions themselves, there are auxiliary lists
03269  * of the index column numbers that the clauses are meant to be used with;
03270  * we generate an updated column number list for the result.  (This is not
03271  * the identical list because one input clause sometimes produces more than
03272  * one output clause.)
03273  *
03274  * The input clauses are sorted by column number, and so the output is too.
03275  * (This is depended on in various places in both planner and executor.)
03276  */
03277 void
03278 expand_indexqual_conditions(IndexOptInfo *index,
03279                             List *indexclauses, List *indexclausecols,
03280                             List **indexquals_p, List **indexqualcols_p)
03281 {
03282     List       *indexquals = NIL;
03283     List       *indexqualcols = NIL;
03284     ListCell   *lcc,
03285                *lci;
03286 
03287     forboth(lcc, indexclauses, lci, indexclausecols)
03288     {
03289         RestrictInfo *rinfo = (RestrictInfo *) lfirst(lcc);
03290         int         indexcol = lfirst_int(lci);
03291         Expr       *clause = rinfo->clause;
03292         Oid         curFamily = index->opfamily[indexcol];
03293         Oid         curCollation = index->indexcollations[indexcol];
03294 
03295         /* First check for boolean cases */
03296         if (IsBooleanOpfamily(curFamily))
03297         {
03298             Expr       *boolqual;
03299 
03300             boolqual = expand_boolean_index_clause((Node *) clause,
03301                                                    indexcol,
03302                                                    index);
03303             if (boolqual)
03304             {
03305                 indexquals = lappend(indexquals,
03306                                      make_simple_restrictinfo(boolqual));
03307                 indexqualcols = lappend_int(indexqualcols, indexcol);
03308                 continue;
03309             }
03310         }
03311 
03312         /*
03313          * Else it must be an opclause (usual case), ScalarArrayOp,
03314          * RowCompare, or NullTest
03315          */
03316         if (is_opclause(clause))
03317         {
03318             indexquals = list_concat(indexquals,
03319                                      expand_indexqual_opclause(rinfo,
03320                                                                curFamily,
03321                                                                curCollation));
03322             /* expand_indexqual_opclause can produce multiple clauses */
03323             while (list_length(indexqualcols) < list_length(indexquals))
03324                 indexqualcols = lappend_int(indexqualcols, indexcol);
03325         }
03326         else if (IsA(clause, ScalarArrayOpExpr))
03327         {
03328             /* no extra work at this time */
03329             indexquals = lappend(indexquals, rinfo);
03330             indexqualcols = lappend_int(indexqualcols, indexcol);
03331         }
03332         else if (IsA(clause, RowCompareExpr))
03333         {
03334             indexquals = lappend(indexquals,
03335                                  expand_indexqual_rowcompare(rinfo,
03336                                                              index,
03337                                                              indexcol));
03338             indexqualcols = lappend_int(indexqualcols, indexcol);
03339         }
03340         else if (IsA(clause, NullTest))
03341         {
03342             Assert(index->amsearchnulls);
03343             indexquals = lappend(indexquals, rinfo);
03344             indexqualcols = lappend_int(indexqualcols, indexcol);
03345         }
03346         else
03347             elog(ERROR, "unsupported indexqual type: %d",
03348                  (int) nodeTag(clause));
03349     }
03350 
03351     *indexquals_p = indexquals;
03352     *indexqualcols_p = indexqualcols;
03353 }
03354 
03355 /*
03356  * expand_boolean_index_clause
03357  *    Convert a clause recognized by match_boolean_index_clause into
03358  *    a boolean equality operator clause.
03359  *
03360  * Returns NULL if the clause isn't a boolean index qual.
03361  */
03362 static Expr *
03363 expand_boolean_index_clause(Node *clause,
03364                             int indexcol,
03365                             IndexOptInfo *index)
03366 {
03367     /* Direct match? */
03368     if (match_index_to_operand(clause, indexcol, index))
03369     {
03370         /* convert to indexkey = TRUE */
03371         return make_opclause(BooleanEqualOperator, BOOLOID, false,
03372                              (Expr *) clause,
03373                              (Expr *) makeBoolConst(true, false),
03374                              InvalidOid, InvalidOid);
03375     }
03376     /* NOT clause? */
03377     if (not_clause(clause))
03378     {
03379         Node       *arg = (Node *) get_notclausearg((Expr *) clause);
03380 
03381         /* It must have matched the indexkey */
03382         Assert(match_index_to_operand(arg, indexcol, index));
03383         /* convert to indexkey = FALSE */
03384         return make_opclause(BooleanEqualOperator, BOOLOID, false,
03385                              (Expr *) arg,
03386                              (Expr *) makeBoolConst(false, false),
03387                              InvalidOid, InvalidOid);
03388     }
03389     if (clause && IsA(clause, BooleanTest))
03390     {
03391         BooleanTest *btest = (BooleanTest *) clause;
03392         Node       *arg = (Node *) btest->arg;
03393 
03394         /* It must have matched the indexkey */
03395         Assert(match_index_to_operand(arg, indexcol, index));
03396         if (btest->booltesttype == IS_TRUE)
03397         {
03398             /* convert to indexkey = TRUE */
03399             return make_opclause(BooleanEqualOperator, BOOLOID, false,
03400                                  (Expr *) arg,
03401                                  (Expr *) makeBoolConst(true, false),
03402                                  InvalidOid, InvalidOid);
03403         }
03404         if (btest->booltesttype == IS_FALSE)
03405         {
03406             /* convert to indexkey = FALSE */
03407             return make_opclause(BooleanEqualOperator, BOOLOID, false,
03408                                  (Expr *) arg,
03409                                  (Expr *) makeBoolConst(false, false),
03410                                  InvalidOid, InvalidOid);
03411         }
03412         /* Oops */
03413         Assert(false);
03414     }
03415 
03416     return NULL;
03417 }
03418 
03419 /*
03420  * expand_indexqual_opclause --- expand a single indexqual condition
03421  *      that is an operator clause
03422  *
03423  * The input is a single RestrictInfo, the output a list of RestrictInfos.
03424  *
03425  * In the base case this is just list_make1(), but we have to be prepared to
03426  * expand special cases that were accepted by match_special_index_operator().
03427  */
03428 static List *
03429 expand_indexqual_opclause(RestrictInfo *rinfo, Oid opfamily, Oid idxcollation)
03430 {
03431     Expr       *clause = rinfo->clause;
03432 
03433     /* we know these will succeed */
03434     Node       *leftop = get_leftop(clause);
03435     Node       *rightop = get_rightop(clause);
03436     Oid         expr_op = ((OpExpr *) clause)->opno;
03437     Oid         expr_coll = ((OpExpr *) clause)->inputcollid;
03438     Const      *patt = (Const *) rightop;
03439     Const      *prefix = NULL;
03440     Pattern_Prefix_Status pstatus;
03441 
03442     /*
03443      * LIKE and regex operators are not members of any btree index opfamily,
03444      * but they can be members of opfamilies for more exotic index types such
03445      * as GIN.  Therefore, we should only do expansion if the operator is
03446      * actually not in the opfamily.  But checking that requires a syscache
03447      * lookup, so it's best to first see if the operator is one we are
03448      * interested in.
03449      */
03450     switch (expr_op)
03451     {
03452         case OID_TEXT_LIKE_OP:
03453         case OID_BPCHAR_LIKE_OP:
03454         case OID_NAME_LIKE_OP:
03455         case OID_BYTEA_LIKE_OP:
03456             if (!op_in_opfamily(expr_op, opfamily))
03457             {
03458                 pstatus = pattern_fixed_prefix(patt, Pattern_Type_Like, expr_coll,
03459                                                &prefix, NULL);
03460                 return prefix_quals(leftop, opfamily, idxcollation, prefix, pstatus);
03461             }
03462             break;
03463 
03464         case OID_TEXT_ICLIKE_OP:
03465         case OID_BPCHAR_ICLIKE_OP:
03466         case OID_NAME_ICLIKE_OP:
03467             if (!op_in_opfamily(expr_op, opfamily))
03468             {
03469                 /* the right-hand const is type text for all of these */
03470                 pstatus = pattern_fixed_prefix(patt, Pattern_Type_Like_IC, expr_coll,
03471                                                &prefix, NULL);
03472                 return prefix_quals(leftop, opfamily, idxcollation, prefix, pstatus);
03473             }
03474             break;
03475 
03476         case OID_TEXT_REGEXEQ_OP:
03477         case OID_BPCHAR_REGEXEQ_OP:
03478         case OID_NAME_REGEXEQ_OP:
03479             if (!op_in_opfamily(expr_op, opfamily))
03480             {
03481                 /* the right-hand const is type text for all of these */
03482                 pstatus = pattern_fixed_prefix(patt, Pattern_Type_Regex, expr_coll,
03483                                                &prefix, NULL);
03484                 return prefix_quals(leftop, opfamily, idxcollation, prefix, pstatus);
03485             }
03486             break;
03487 
03488         case OID_TEXT_ICREGEXEQ_OP:
03489         case OID_BPCHAR_ICREGEXEQ_OP:
03490         case OID_NAME_ICREGEXEQ_OP:
03491             if (!op_in_opfamily(expr_op, opfamily))
03492             {
03493                 /* the right-hand const is type text for all of these */
03494                 pstatus = pattern_fixed_prefix(patt, Pattern_Type_Regex_IC, expr_coll,
03495                                                &prefix, NULL);
03496                 return prefix_quals(leftop, opfamily, idxcollation, prefix, pstatus);
03497             }
03498             break;
03499 
03500         case OID_INET_SUB_OP:
03501         case OID_INET_SUBEQ_OP:
03502             if (!op_in_opfamily(expr_op, opfamily))
03503             {
03504                 return network_prefix_quals(leftop, expr_op, opfamily,
03505                                             patt->constvalue);
03506             }
03507             break;
03508     }
03509 
03510     /* Default case: just make a list of the unmodified indexqual */
03511     return list_make1(rinfo);
03512 }
03513 
03514 /*
03515  * expand_indexqual_rowcompare --- expand a single indexqual condition
03516  *      that is a RowCompareExpr
03517  *
03518  * This is a thin wrapper around adjust_rowcompare_for_index; we export the
03519  * latter so that createplan.c can use it to re-discover which columns of the
03520  * index are used by a row comparison indexqual.
03521  */
03522 static RestrictInfo *
03523 expand_indexqual_rowcompare(RestrictInfo *rinfo,
03524                             IndexOptInfo *index,
03525                             int indexcol)
03526 {
03527     RowCompareExpr *clause = (RowCompareExpr *) rinfo->clause;
03528     Expr       *newclause;
03529     List       *indexcolnos;
03530     bool        var_on_left;
03531 
03532     newclause = adjust_rowcompare_for_index(clause,
03533                                             index,
03534                                             indexcol,
03535                                             &indexcolnos,
03536                                             &var_on_left);
03537 
03538     /*
03539      * If we didn't have to change the RowCompareExpr, return the original
03540      * RestrictInfo.
03541      */
03542     if (newclause == (Expr *) clause)
03543         return rinfo;
03544 
03545     /* Else we need a new RestrictInfo */
03546     return make_simple_restrictinfo(newclause);
03547 }
03548 
03549 /*
03550  * adjust_rowcompare_for_index --- expand a single indexqual condition
03551  *      that is a RowCompareExpr
03552  *
03553  * It's already known that the first column of the row comparison matches
03554  * the specified column of the index.  We can use additional columns of the
03555  * row comparison as index qualifications, so long as they match the index
03556  * in the "same direction", ie, the indexkeys are all on the same side of the
03557  * clause and the operators are all the same-type members of the opfamilies.
03558  * If all the columns of the RowCompareExpr match in this way, we just use it
03559  * as-is.  Otherwise, we build a shortened RowCompareExpr (if more than one
03560  * column matches) or a simple OpExpr (if the first-column match is all
03561  * there is).  In these cases the modified clause is always "<=" or ">="
03562  * even when the original was "<" or ">" --- this is necessary to match all
03563  * the rows that could match the original.  (We are essentially building a
03564  * lossy version of the row comparison when we do this.)
03565  *
03566  * *indexcolnos receives an integer list of the index column numbers (zero
03567  * based) used in the resulting expression.  The reason we need to return
03568  * that is that if the index is selected for use, createplan.c will need to
03569  * call this again to extract that list.  (This is a bit grotty, but row
03570  * comparison indexquals aren't used enough to justify finding someplace to
03571  * keep the information in the Path representation.)  Since createplan.c
03572  * also needs to know which side of the RowCompareExpr is the index side,
03573  * we also return *var_on_left_p rather than re-deducing that there.
03574  */
03575 Expr *
03576 adjust_rowcompare_for_index(RowCompareExpr *clause,
03577                             IndexOptInfo *index,
03578                             int indexcol,
03579                             List **indexcolnos,
03580                             bool *var_on_left_p)
03581 {
03582     bool        var_on_left;
03583     int         op_strategy;
03584     Oid         op_lefttype;
03585     Oid         op_righttype;
03586     int         matching_cols;
03587     Oid         expr_op;
03588     List       *opfamilies;
03589     List       *lefttypes;
03590     List       *righttypes;
03591     List       *new_ops;
03592     ListCell   *largs_cell;
03593     ListCell   *rargs_cell;
03594     ListCell   *opnos_cell;
03595     ListCell   *collids_cell;
03596 
03597     /* We have to figure out (again) how the first col matches */
03598     var_on_left = match_index_to_operand((Node *) linitial(clause->largs),
03599                                          indexcol, index);
03600     Assert(var_on_left ||
03601            match_index_to_operand((Node *) linitial(clause->rargs),
03602                                   indexcol, index));
03603     *var_on_left_p = var_on_left;
03604 
03605     expr_op = linitial_oid(clause->opnos);
03606     if (!var_on_left)
03607         expr_op = get_commutator(expr_op);
03608     get_op_opfamily_properties(expr_op, index->opfamily[indexcol], false,
03609                                &op_strategy,
03610                                &op_lefttype,
03611                                &op_righttype);
03612 
03613     /* Initialize returned list of which index columns are used */
03614     *indexcolnos = list_make1_int(indexcol);
03615 
03616     /* Build lists of the opfamilies and operator datatypes in case needed */
03617     opfamilies = list_make1_oid(index->opfamily[indexcol]);
03618     lefttypes = list_make1_oid(op_lefttype);
03619     righttypes = list_make1_oid(op_righttype);
03620 
03621     /*
03622      * See how many of the remaining columns match some index column in the
03623      * same way.  As in match_clause_to_indexcol(), the "other" side of any
03624      * potential index condition is OK as long as it doesn't use Vars from the
03625      * indexed relation.
03626      */
03627     matching_cols = 1;
03628     largs_cell = lnext(list_head(clause->largs));
03629     rargs_cell = lnext(list_head(clause->rargs));
03630     opnos_cell = lnext(list_head(clause->opnos));
03631     collids_cell = lnext(list_head(clause->inputcollids));
03632 
03633     while (largs_cell != NULL)
03634     {
03635         Node       *varop;
03636         Node       *constop;
03637         int         i;
03638 
03639         expr_op = lfirst_oid(opnos_cell);
03640         if (var_on_left)
03641         {
03642             varop = (Node *) lfirst(largs_cell);
03643             constop = (Node *) lfirst(rargs_cell);
03644         }
03645         else
03646         {
03647             varop = (Node *) lfirst(rargs_cell);
03648             constop = (Node *) lfirst(largs_cell);
03649             /* indexkey is on right, so commute the operator */
03650             expr_op = get_commutator(expr_op);
03651             if (expr_op == InvalidOid)
03652                 break;          /* operator is not usable */
03653         }
03654         if (bms_is_member(index->rel->relid, pull_varnos(constop)))
03655             break;              /* no good, Var on wrong side */
03656         if (contain_volatile_functions(constop))
03657             break;              /* no good, volatile comparison value */
03658 
03659         /*
03660          * The Var side can match any column of the index.
03661          */
03662         for (i = 0; i < index->ncolumns; i++)
03663         {
03664             if (match_index_to_operand(varop, i, index) &&
03665                 get_op_opfamily_strategy(expr_op,
03666                                          index->opfamily[i]) == op_strategy &&
03667                 IndexCollMatchesExprColl(index->indexcollations[i],
03668                                          lfirst_oid(collids_cell)))
03669                 break;
03670         }
03671         if (i >= index->ncolumns)
03672             break;              /* no match found */
03673 
03674         /* Add column number to returned list */
03675         *indexcolnos = lappend_int(*indexcolnos, i);
03676 
03677         /* Add opfamily and datatypes to lists */
03678         get_op_opfamily_properties(expr_op, index->opfamily[i], false,
03679                                    &op_strategy,
03680                                    &op_lefttype,
03681                                    &op_righttype);
03682         opfamilies = lappend_oid(opfamilies, index->opfamily[i]);
03683         lefttypes = lappend_oid(lefttypes, op_lefttype);
03684         righttypes = lappend_oid(righttypes, op_righttype);
03685 
03686         /* This column matches, keep scanning */
03687         matching_cols++;
03688         largs_cell = lnext(largs_cell);
03689         rargs_cell = lnext(rargs_cell);
03690         opnos_cell = lnext(opnos_cell);
03691         collids_cell = lnext(collids_cell);
03692     }
03693 
03694     /* Return clause as-is if it's all usable as index quals */
03695     if (matching_cols == list_length(clause->opnos))
03696         return (Expr *) clause;
03697 
03698     /*
03699      * We have to generate a subset rowcompare (possibly just one OpExpr). The
03700      * painful part of this is changing < to <= or > to >=, so deal with that
03701      * first.
03702      */
03703     if (op_strategy == BTLessEqualStrategyNumber ||
03704         op_strategy == BTGreaterEqualStrategyNumber)
03705     {
03706         /* easy, just use the same operators */
03707         new_ops = list_truncate(list_copy(clause->opnos), matching_cols);
03708     }
03709     else
03710     {
03711         ListCell   *opfamilies_cell;
03712         ListCell   *lefttypes_cell;
03713         ListCell   *righttypes_cell;
03714 
03715         if (op_strategy == BTLessStrategyNumber)
03716             op_strategy = BTLessEqualStrategyNumber;
03717         else if (op_strategy == BTGreaterStrategyNumber)
03718             op_strategy = BTGreaterEqualStrategyNumber;
03719         else
03720             elog(ERROR, "unexpected strategy number %d", op_strategy);
03721         new_ops = NIL;
03722         lefttypes_cell = list_head(lefttypes);
03723         righttypes_cell = list_head(righttypes);
03724         foreach(opfamilies_cell, opfamilies)
03725         {
03726             Oid         opfam = lfirst_oid(opfamilies_cell);
03727             Oid         lefttype = lfirst_oid(lefttypes_cell);
03728             Oid         righttype = lfirst_oid(righttypes_cell);
03729 
03730             expr_op = get_opfamily_member(opfam, lefttype, righttype,
03731                                           op_strategy);
03732             if (!OidIsValid(expr_op))   /* should not happen */
03733                 elog(ERROR, "could not find member %d(%u,%u) of opfamily %u",
03734                      op_strategy, lefttype, righttype, opfam);
03735             if (!var_on_left)
03736             {
03737                 expr_op = get_commutator(expr_op);
03738                 if (!OidIsValid(expr_op))       /* should not happen */
03739                     elog(ERROR, "could not find commutator of member %d(%u,%u) of opfamily %u",
03740                          op_strategy, lefttype, righttype, opfam);
03741             }
03742             new_ops = lappend_oid(new_ops, expr_op);
03743             lefttypes_cell = lnext(lefttypes_cell);
03744             righttypes_cell = lnext(righttypes_cell);
03745         }
03746     }
03747 
03748     /* If we have more than one matching col, create a subset rowcompare */
03749     if (matching_cols > 1)
03750     {
03751         RowCompareExpr *rc = makeNode(RowCompareExpr);
03752 
03753         if (var_on_left)
03754             rc->rctype = (RowCompareType) op_strategy;
03755         else
03756             rc->rctype = (op_strategy == BTLessEqualStrategyNumber) ?
03757                 ROWCOMPARE_GE : ROWCOMPARE_LE;
03758         rc->opnos = new_ops;
03759         rc->opfamilies = list_truncate(list_copy(clause->opfamilies),
03760                                        matching_cols);
03761         rc->inputcollids = list_truncate(list_copy(clause->inputcollids),
03762                                          matching_cols);
03763         rc->largs = list_truncate((List *) copyObject(clause->largs),
03764                                   matching_cols);
03765         rc->rargs = list_truncate((List *) copyObject(clause->rargs),
03766                                   matching_cols);
03767         return (Expr *) rc;
03768     }
03769     else
03770     {
03771         return make_opclause(linitial_oid(new_ops), BOOLOID, false,
03772                              copyObject(linitial(clause->largs)),
03773                              copyObject(linitial(clause->rargs)),
03774                              InvalidOid,
03775                              linitial_oid(clause->inputcollids));
03776     }
03777 }
03778 
03779 /*
03780  * Given a fixed prefix that all the "leftop" values must have,
03781  * generate suitable indexqual condition(s).  opfamily is the index
03782  * operator family; we use it to deduce the appropriate comparison
03783  * operators and operand datatypes.  collation is the input collation to use.
03784  */
03785 static List *
03786 prefix_quals(Node *leftop, Oid opfamily, Oid collation,
03787              Const *prefix_const, Pattern_Prefix_Status pstatus)
03788 {
03789     List       *result;
03790     Oid         datatype;
03791     Oid         oproid;
03792     Expr       *expr;
03793     FmgrInfo    ltproc;
03794     Const      *greaterstr;
03795 
03796     Assert(pstatus != Pattern_Prefix_None);
03797 
03798     switch (opfamily)
03799     {
03800         case TEXT_BTREE_FAM_OID:
03801         case TEXT_PATTERN_BTREE_FAM_OID:
03802         case TEXT_SPGIST_FAM_OID:
03803             datatype = TEXTOID;
03804             break;
03805 
03806         case BPCHAR_BTREE_FAM_OID:
03807         case BPCHAR_PATTERN_BTREE_FAM_OID:
03808             datatype = BPCHAROID;
03809             break;
03810 
03811         case NAME_BTREE_FAM_OID:
03812             datatype = NAMEOID;
03813             break;
03814 
03815         case BYTEA_BTREE_FAM_OID:
03816             datatype = BYTEAOID;
03817             break;
03818 
03819         default:
03820             /* shouldn't get here */
03821             elog(ERROR, "unexpected opfamily: %u", opfamily);
03822             return NIL;
03823     }
03824 
03825     /*
03826      * If necessary, coerce the prefix constant to the right type. The given
03827      * prefix constant is either text or bytea type.
03828      */
03829     if (prefix_const->consttype != datatype)
03830     {
03831         char       *prefix;
03832 
03833         switch (prefix_const->consttype)
03834         {
03835             case TEXTOID:
03836                 prefix = TextDatumGetCString(prefix_const->constvalue);
03837                 break;
03838             case BYTEAOID:
03839                 prefix = DatumGetCString(DirectFunctionCall1(byteaout,
03840                                                   prefix_const->constvalue));
03841                 break;
03842             default:
03843                 elog(ERROR, "unexpected const type: %u",
03844                      prefix_const->consttype);
03845                 return NIL;
03846         }
03847         prefix_const = string_to_const(prefix, datatype);
03848         pfree(prefix);
03849     }
03850 
03851     /*
03852      * If we found an exact-match pattern, generate an "=" indexqual.
03853      */
03854     if (pstatus == Pattern_Prefix_Exact)
03855     {
03856         oproid = get_opfamily_member(opfamily, datatype, datatype,
03857                                      BTEqualStrategyNumber);
03858         if (oproid == InvalidOid)
03859             elog(ERROR, "no = operator for opfamily %u", opfamily);
03860         expr = make_opclause(oproid, BOOLOID, false,
03861                              (Expr *) leftop, (Expr *) prefix_const,
03862                              InvalidOid, collation);
03863         result = list_make1(make_simple_restrictinfo(expr));
03864         return result;
03865     }
03866 
03867     /*
03868      * Otherwise, we have a nonempty required prefix of the values.
03869      *
03870      * We can always say "x >= prefix".
03871      */
03872     oproid = get_opfamily_member(opfamily, datatype, datatype,
03873                                  BTGreaterEqualStrategyNumber);
03874     if (oproid == InvalidOid)
03875         elog(ERROR, "no >= operator for opfamily %u", opfamily);
03876     expr = make_opclause(oproid, BOOLOID, false,
03877                          (Expr *) leftop, (Expr *) prefix_const,
03878                          InvalidOid, collation);
03879     result = list_make1(make_simple_restrictinfo(expr));
03880 
03881     /*-------
03882      * If we can create a string larger than the prefix, we can say
03883      * "x < greaterstr".  NB: we rely on make_greater_string() to generate
03884      * a guaranteed-greater string, not just a probably-greater string.
03885      * In general this is only guaranteed in C locale, so we'd better be
03886      * using a C-locale index collation.
03887      *-------
03888      */
03889     oproid = get_opfamily_member(opfamily, datatype, datatype,
03890                                  BTLessStrategyNumber);
03891     if (oproid == InvalidOid)
03892         elog(ERROR, "no < operator for opfamily %u", opfamily);
03893     fmgr_info(get_opcode(oproid), &ltproc);
03894     greaterstr = make_greater_string(prefix_const, &ltproc, collation);
03895     if (greaterstr)
03896     {
03897         expr = make_opclause(oproid, BOOLOID, false,
03898                              (Expr *) leftop, (Expr *) greaterstr,
03899                              InvalidOid, collation);
03900         result = lappend(result, make_simple_restrictinfo(expr));
03901     }
03902 
03903     return result;
03904 }
03905 
03906 /*
03907  * Given a leftop and a rightop, and a inet-family sup/sub operator,
03908  * generate suitable indexqual condition(s).  expr_op is the original
03909  * operator, and opfamily is the index opfamily.
03910  */
03911 static List *
03912 network_prefix_quals(Node *leftop, Oid expr_op, Oid opfamily, Datum rightop)
03913 {
03914     bool        is_eq;
03915     Oid         datatype;
03916     Oid         opr1oid;
03917     Oid         opr2oid;
03918     Datum       opr1right;
03919     Datum       opr2right;
03920     List       *result;
03921     Expr       *expr;
03922 
03923     switch (expr_op)
03924     {
03925         case OID_INET_SUB_OP:
03926             datatype = INETOID;
03927             is_eq = false;
03928             break;
03929         case OID_INET_SUBEQ_OP:
03930             datatype = INETOID;
03931             is_eq = true;
03932             break;
03933         default:
03934             elog(ERROR, "unexpected operator: %u", expr_op);
03935             return NIL;
03936     }
03937 
03938     /*
03939      * create clause "key >= network_scan_first( rightop )", or ">" if the
03940      * operator disallows equality.
03941      */
03942     if (is_eq)
03943     {
03944         opr1oid = get_opfamily_member(opfamily, datatype, datatype,
03945                                       BTGreaterEqualStrategyNumber);
03946         if (opr1oid == InvalidOid)
03947             elog(ERROR, "no >= operator for opfamily %u", opfamily);
03948     }
03949     else
03950     {
03951         opr1oid = get_opfamily_member(opfamily, datatype, datatype,
03952                                       BTGreaterStrategyNumber);
03953         if (opr1oid == InvalidOid)
03954             elog(ERROR, "no > operator for opfamily %u", opfamily);
03955     }
03956 
03957     opr1right = network_scan_first(rightop);
03958 
03959     expr = make_opclause(opr1oid, BOOLOID, false,
03960                          (Expr *) leftop,
03961                          (Expr *) makeConst(datatype, -1,
03962                                             InvalidOid, /* not collatable */
03963                                             -1, opr1right,
03964                                             false, false),
03965                          InvalidOid, InvalidOid);
03966     result = list_make1(make_simple_restrictinfo(expr));
03967 
03968     /* create clause "key <= network_scan_last( rightop )" */
03969 
03970     opr2oid = get_opfamily_member(opfamily, datatype, datatype,
03971                                   BTLessEqualStrategyNumber);
03972     if (opr2oid == InvalidOid)
03973         elog(ERROR, "no <= operator for opfamily %u", opfamily);
03974 
03975     opr2right = network_scan_last(rightop);
03976 
03977     expr = make_opclause(opr2oid, BOOLOID, false,
03978                          (Expr *) leftop,
03979                          (Expr *) makeConst(datatype, -1,
03980                                             InvalidOid, /* not collatable */
03981                                             -1, opr2right,
03982                                             false, false),
03983                          InvalidOid, InvalidOid);
03984     result = lappend(result, make_simple_restrictinfo(expr));
03985 
03986     return result;
03987 }
03988 
03989 /*
03990  * Handy subroutines for match_special_index_operator() and friends.
03991  */
03992 
03993 /*
03994  * Generate a Datum of the appropriate type from a C string.
03995  * Note that all of the supported types are pass-by-ref, so the
03996  * returned value should be pfree'd if no longer needed.
03997  */
03998 static Datum
03999 string_to_datum(const char *str, Oid datatype)
04000 {
04001     /*
04002      * We cheat a little by assuming that CStringGetTextDatum() will do for
04003      * bpchar and varchar constants too...
04004      */
04005     if (datatype == NAMEOID)
04006         return DirectFunctionCall1(namein, CStringGetDatum(str));
04007     else if (datatype == BYTEAOID)
04008         return DirectFunctionCall1(byteain, CStringGetDatum(str));
04009     else
04010         return CStringGetTextDatum(str);
04011 }
04012 
04013 /*
04014  * Generate a Const node of the appropriate type from a C string.
04015  */
04016 static Const *
04017 string_to_const(const char *str, Oid datatype)
04018 {
04019     Datum       conval = string_to_datum(str, datatype);
04020     Oid         collation;
04021     int         constlen;
04022 
04023     /*
04024      * We only need to support a few datatypes here, so hard-wire properties
04025      * instead of incurring the expense of catalog lookups.
04026      */
04027     switch (datatype)
04028     {
04029         case TEXTOID:
04030         case VARCHAROID:
04031         case BPCHAROID:
04032             collation = DEFAULT_COLLATION_OID;
04033             constlen = -1;
04034             break;
04035 
04036         case NAMEOID:
04037             collation = InvalidOid;
04038             constlen = NAMEDATALEN;
04039             break;
04040 
04041         case BYTEAOID:
04042             collation = InvalidOid;
04043             constlen = -1;
04044             break;
04045 
04046         default:
04047             elog(ERROR, "unexpected datatype in string_to_const: %u",
04048                  datatype);
04049             return NULL;
04050     }
04051 
04052     return makeConst(datatype, -1, collation, constlen,
04053                      conval, false, false);
04054 }