LCOV - code coverage report
Current view: top level - src/backend/optimizer/path - indxpath.c (source / functions) Hit Total Coverage
Test: PostgreSQL 18devel Lines: 1180 1254 94.1 %
Date: 2025-04-01 14:15:22 Functions: 47 48 97.9 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * indxpath.c
       4             :  *    Routines to determine which indexes are usable for scanning a
       5             :  *    given relation, and create Paths accordingly.
       6             :  *
       7             :  * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
       8             :  * Portions Copyright (c) 1994, Regents of the University of California
       9             :  *
      10             :  *
      11             :  * IDENTIFICATION
      12             :  *    src/backend/optimizer/path/indxpath.c
      13             :  *
      14             :  *-------------------------------------------------------------------------
      15             :  */
      16             : #include "postgres.h"
      17             : 
      18             : #include <math.h>
      19             : 
      20             : #include "access/stratnum.h"
      21             : #include "access/sysattr.h"
      22             : #include "catalog/pg_am.h"
      23             : #include "catalog/pg_amop.h"
      24             : #include "catalog/pg_operator.h"
      25             : #include "catalog/pg_opfamily.h"
      26             : #include "catalog/pg_type.h"
      27             : #include "nodes/makefuncs.h"
      28             : #include "nodes/nodeFuncs.h"
      29             : #include "nodes/supportnodes.h"
      30             : #include "optimizer/cost.h"
      31             : #include "optimizer/optimizer.h"
      32             : #include "optimizer/pathnode.h"
      33             : #include "optimizer/paths.h"
      34             : #include "optimizer/prep.h"
      35             : #include "optimizer/restrictinfo.h"
      36             : #include "utils/array.h"
      37             : #include "utils/lsyscache.h"
      38             : #include "utils/selfuncs.h"
      39             : #include "utils/syscache.h"
      40             : 
      41             : 
      42             : /* XXX see PartCollMatchesExprColl */
      43             : #define IndexCollMatchesExprColl(idxcollation, exprcollation) \
      44             :     ((idxcollation) == InvalidOid || (idxcollation) == (exprcollation))
      45             : 
      46             : /* Whether we are looking for plain indexscan, bitmap scan, or either */
      47             : typedef enum
      48             : {
      49             :     ST_INDEXSCAN,               /* must support amgettuple */
      50             :     ST_BITMAPSCAN,              /* must support amgetbitmap */
      51             :     ST_ANYSCAN,                 /* either is okay */
      52             : } ScanTypeControl;
      53             : 
      54             : /* Data structure for collecting qual clauses that match an index */
      55             : typedef struct
      56             : {
      57             :     bool        nonempty;       /* True if lists are not all empty */
      58             :     /* Lists of IndexClause nodes, one list per index column */
      59             :     List       *indexclauses[INDEX_MAX_KEYS];
      60             : } IndexClauseSet;
      61             : 
      62             : /* Per-path data used within choose_bitmap_and() */
      63             : typedef struct
      64             : {
      65             :     Path       *path;           /* IndexPath, BitmapAndPath, or BitmapOrPath */
      66             :     List       *quals;          /* the WHERE clauses it uses */
      67             :     List       *preds;          /* predicates of its partial index(es) */
      68             :     Bitmapset  *clauseids;      /* quals+preds represented as a bitmapset */
      69             :     bool        unclassifiable; /* has too many quals+preds to process? */
      70             : } PathClauseUsage;
      71             : 
      72             : /* Callback argument for ec_member_matches_indexcol */
      73             : typedef struct
      74             : {
      75             :     IndexOptInfo *index;        /* index we're considering */
      76             :     int         indexcol;       /* index column we want to match to */
      77             : } ec_member_matches_arg;
      78             : 
      79             : 
      80             : static void consider_index_join_clauses(PlannerInfo *root, RelOptInfo *rel,
      81             :                                         IndexOptInfo *index,
      82             :                                         IndexClauseSet *rclauseset,
      83             :                                         IndexClauseSet *jclauseset,
      84             :                                         IndexClauseSet *eclauseset,
      85             :                                         List **bitindexpaths);
      86             : static void consider_index_join_outer_rels(PlannerInfo *root, RelOptInfo *rel,
      87             :                                            IndexOptInfo *index,
      88             :                                            IndexClauseSet *rclauseset,
      89             :                                            IndexClauseSet *jclauseset,
      90             :                                            IndexClauseSet *eclauseset,
      91             :                                            List **bitindexpaths,
      92             :                                            List *indexjoinclauses,
      93             :                                            int considered_clauses,
      94             :                                            List **considered_relids);
      95             : static void get_join_index_paths(PlannerInfo *root, RelOptInfo *rel,
      96             :                                  IndexOptInfo *index,
      97             :                                  IndexClauseSet *rclauseset,
      98             :                                  IndexClauseSet *jclauseset,
      99             :                                  IndexClauseSet *eclauseset,
     100             :                                  List **bitindexpaths,
     101             :                                  Relids relids,
     102             :                                  List **considered_relids);
     103             : static bool eclass_already_used(EquivalenceClass *parent_ec, Relids oldrelids,
     104             :                                 List *indexjoinclauses);
     105             : static void get_index_paths(PlannerInfo *root, RelOptInfo *rel,
     106             :                             IndexOptInfo *index, IndexClauseSet *clauses,
     107             :                             List **bitindexpaths);
     108             : static List *build_index_paths(PlannerInfo *root, RelOptInfo *rel,
     109             :                                IndexOptInfo *index, IndexClauseSet *clauses,
     110             :                                bool useful_predicate,
     111             :                                ScanTypeControl scantype,
     112             :                                bool *skip_nonnative_saop);
     113             : static List *build_paths_for_OR(PlannerInfo *root, RelOptInfo *rel,
     114             :                                 List *clauses, List *other_clauses);
     115             : static List *generate_bitmap_or_paths(PlannerInfo *root, RelOptInfo *rel,
     116             :                                       List *clauses, List *other_clauses);
     117             : static Path *choose_bitmap_and(PlannerInfo *root, RelOptInfo *rel,
     118             :                                List *paths);
     119             : static int  path_usage_comparator(const void *a, const void *b);
     120             : static Cost bitmap_scan_cost_est(PlannerInfo *root, RelOptInfo *rel,
     121             :                                  Path *ipath);
     122             : static Cost bitmap_and_cost_est(PlannerInfo *root, RelOptInfo *rel,
     123             :                                 List *paths);
     124             : static PathClauseUsage *classify_index_clause_usage(Path *path,
     125             :                                                     List **clauselist);
     126             : static void find_indexpath_quals(Path *bitmapqual, List **quals, List **preds);
     127             : static int  find_list_position(Node *node, List **nodelist);
     128             : static bool check_index_only(RelOptInfo *rel, IndexOptInfo *index);
     129             : static double get_loop_count(PlannerInfo *root, Index cur_relid, Relids outer_relids);
     130             : static double adjust_rowcount_for_semijoins(PlannerInfo *root,
     131             :                                             Index cur_relid,
     132             :                                             Index outer_relid,
     133             :                                             double rowcount);
     134             : static double approximate_joinrel_size(PlannerInfo *root, Relids relids);
     135             : static void match_restriction_clauses_to_index(PlannerInfo *root,
     136             :                                                IndexOptInfo *index,
     137             :                                                IndexClauseSet *clauseset);
     138             : static void match_join_clauses_to_index(PlannerInfo *root,
     139             :                                         RelOptInfo *rel, IndexOptInfo *index,
     140             :                                         IndexClauseSet *clauseset,
     141             :                                         List **joinorclauses);
     142             : static void match_eclass_clauses_to_index(PlannerInfo *root,
     143             :                                           IndexOptInfo *index,
     144             :                                           IndexClauseSet *clauseset);
     145             : static void match_clauses_to_index(PlannerInfo *root,
     146             :                                    List *clauses,
     147             :                                    IndexOptInfo *index,
     148             :                                    IndexClauseSet *clauseset);
     149             : static void match_clause_to_index(PlannerInfo *root,
     150             :                                   RestrictInfo *rinfo,
     151             :                                   IndexOptInfo *index,
     152             :                                   IndexClauseSet *clauseset);
     153             : static IndexClause *match_clause_to_indexcol(PlannerInfo *root,
     154             :                                              RestrictInfo *rinfo,
     155             :                                              int indexcol,
     156             :                                              IndexOptInfo *index);
     157             : static bool IsBooleanOpfamily(Oid opfamily);
     158             : static IndexClause *match_boolean_index_clause(PlannerInfo *root,
     159             :                                                RestrictInfo *rinfo,
     160             :                                                int indexcol, IndexOptInfo *index);
     161             : static IndexClause *match_opclause_to_indexcol(PlannerInfo *root,
     162             :                                                RestrictInfo *rinfo,
     163             :                                                int indexcol,
     164             :                                                IndexOptInfo *index);
     165             : static IndexClause *match_funcclause_to_indexcol(PlannerInfo *root,
     166             :                                                  RestrictInfo *rinfo,
     167             :                                                  int indexcol,
     168             :                                                  IndexOptInfo *index);
     169             : static IndexClause *get_index_clause_from_support(PlannerInfo *root,
     170             :                                                   RestrictInfo *rinfo,
     171             :                                                   Oid funcid,
     172             :                                                   int indexarg,
     173             :                                                   int indexcol,
     174             :                                                   IndexOptInfo *index);
     175             : static IndexClause *match_saopclause_to_indexcol(PlannerInfo *root,
     176             :                                                  RestrictInfo *rinfo,
     177             :                                                  int indexcol,
     178             :                                                  IndexOptInfo *index);
     179             : static IndexClause *match_rowcompare_to_indexcol(PlannerInfo *root,
     180             :                                                  RestrictInfo *rinfo,
     181             :                                                  int indexcol,
     182             :                                                  IndexOptInfo *index);
     183             : static IndexClause *match_orclause_to_indexcol(PlannerInfo *root,
     184             :                                                RestrictInfo *rinfo,
     185             :                                                int indexcol,
     186             :                                                IndexOptInfo *index);
     187             : static IndexClause *expand_indexqual_rowcompare(PlannerInfo *root,
     188             :                                                 RestrictInfo *rinfo,
     189             :                                                 int indexcol,
     190             :                                                 IndexOptInfo *index,
     191             :                                                 Oid expr_op,
     192             :                                                 bool var_on_left);
     193             : static void match_pathkeys_to_index(IndexOptInfo *index, List *pathkeys,
     194             :                                     List **orderby_clauses_p,
     195             :                                     List **clause_columns_p);
     196             : static Expr *match_clause_to_ordering_op(IndexOptInfo *index,
     197             :                                          int indexcol, Expr *clause, Oid pk_opfamily);
     198             : static bool ec_member_matches_indexcol(PlannerInfo *root, RelOptInfo *rel,
     199             :                                        EquivalenceClass *ec, EquivalenceMember *em,
     200             :                                        void *arg);
     201             : 
     202             : 
     203             : /*
     204             :  * create_index_paths()
     205             :  *    Generate all interesting index paths for the given relation.
     206             :  *    Candidate paths are added to the rel's pathlist (using add_path).
     207             :  *
     208             :  * To be considered for an index scan, an index must match one or more
     209             :  * restriction clauses or join clauses from the query's qual condition,
     210             :  * or match the query's ORDER BY condition, or have a predicate that
     211             :  * matches the query's qual condition.
     212             :  *
     213             :  * There are two basic kinds of index scans.  A "plain" index scan uses
     214             :  * only restriction clauses (possibly none at all) in its indexqual,
     215             :  * so it can be applied in any context.  A "parameterized" index scan uses
     216             :  * join clauses (plus restriction clauses, if available) in its indexqual.
     217             :  * When joining such a scan to one of the relations supplying the other
     218             :  * variables used in its indexqual, the parameterized scan must appear as
     219             :  * the inner relation of a nestloop join; it can't be used on the outer side,
     220             :  * nor in a merge or hash join.  In that context, values for the other rels'
     221             :  * attributes are available and fixed during any one scan of the indexpath.
     222             :  *
     223             :  * An IndexPath is generated and submitted to add_path() for each plain or
     224             :  * parameterized index scan this routine deems potentially interesting for
     225             :  * the current query.
     226             :  *
     227             :  * 'rel' is the relation for which we want to generate index paths
     228             :  *
     229             :  * Note: check_index_predicates() must have been run previously for this rel.
     230             :  *
     231             :  * Note: in cases involving LATERAL references in the relation's tlist, it's
     232             :  * possible that rel->lateral_relids is nonempty.  Currently, we include
     233             :  * lateral_relids into the parameterization reported for each path, but don't
     234             :  * take it into account otherwise.  The fact that any such rels *must* be
     235             :  * available as parameter sources perhaps should influence our choices of
     236             :  * index quals ... but for now, it doesn't seem worth troubling over.
     237             :  * In particular, comments below about "unparameterized" paths should be read
     238             :  * as meaning "unparameterized so far as the indexquals are concerned".
     239             :  */
     240             : void
     241      384248 : create_index_paths(PlannerInfo *root, RelOptInfo *rel)
     242             : {
     243             :     List       *indexpaths;
     244             :     List       *bitindexpaths;
     245             :     List       *bitjoinpaths;
     246             :     List       *joinorclauses;
     247             :     IndexClauseSet rclauseset;
     248             :     IndexClauseSet jclauseset;
     249             :     IndexClauseSet eclauseset;
     250             :     ListCell   *lc;
     251             : 
     252             :     /* Skip the whole mess if no indexes */
     253      384248 :     if (rel->indexlist == NIL)
     254       68426 :         return;
     255             : 
     256             :     /* Bitmap paths are collected and then dealt with at the end */
     257      315822 :     bitindexpaths = bitjoinpaths = joinorclauses = NIL;
     258             : 
     259             :     /* Examine each index in turn */
     260      981702 :     foreach(lc, rel->indexlist)
     261             :     {
     262      665880 :         IndexOptInfo *index = (IndexOptInfo *) lfirst(lc);
     263             : 
     264             :         /* Protect limited-size array in IndexClauseSets */
     265             :         Assert(index->nkeycolumns <= INDEX_MAX_KEYS);
     266             : 
     267             :         /*
     268             :          * Ignore partial indexes that do not match the query.
     269             :          * (generate_bitmap_or_paths() might be able to do something with
     270             :          * them, but that's of no concern here.)
     271             :          */
     272      665880 :         if (index->indpred != NIL && !index->predOK)
     273         496 :             continue;
     274             : 
     275             :         /*
     276             :          * Identify the restriction clauses that can match the index.
     277             :          */
     278    22623056 :         MemSet(&rclauseset, 0, sizeof(rclauseset));
     279      665384 :         match_restriction_clauses_to_index(root, index, &rclauseset);
     280             : 
     281             :         /*
     282             :          * Build index paths from the restriction clauses.  These will be
     283             :          * non-parameterized paths.  Plain paths go directly to add_path(),
     284             :          * bitmap paths are added to bitindexpaths to be handled below.
     285             :          */
     286      665384 :         get_index_paths(root, rel, index, &rclauseset,
     287             :                         &bitindexpaths);
     288             : 
     289             :         /*
     290             :          * Identify the join clauses that can match the index.  For the moment
     291             :          * we keep them separate from the restriction clauses.  Note that this
     292             :          * step finds only "loose" join clauses that have not been merged into
     293             :          * EquivalenceClasses.  Also, collect join OR clauses for later.
     294             :          */
     295    22623056 :         MemSet(&jclauseset, 0, sizeof(jclauseset));
     296      665384 :         match_join_clauses_to_index(root, rel, index,
     297             :                                     &jclauseset, &joinorclauses);
     298             : 
     299             :         /*
     300             :          * Look for EquivalenceClasses that can generate joinclauses matching
     301             :          * the index.
     302             :          */
     303    22623056 :         MemSet(&eclauseset, 0, sizeof(eclauseset));
     304      665384 :         match_eclass_clauses_to_index(root, index,
     305             :                                       &eclauseset);
     306             : 
     307             :         /*
     308             :          * If we found any plain or eclass join clauses, build parameterized
     309             :          * index paths using them.
     310             :          */
     311      665384 :         if (jclauseset.nonempty || eclauseset.nonempty)
     312      126982 :             consider_index_join_clauses(root, rel, index,
     313             :                                         &rclauseset,
     314             :                                         &jclauseset,
     315             :                                         &eclauseset,
     316             :                                         &bitjoinpaths);
     317             :     }
     318             : 
     319             :     /*
     320             :      * Generate BitmapOrPaths for any suitable OR-clauses present in the
     321             :      * restriction list.  Add these to bitindexpaths.
     322             :      */
     323      315822 :     indexpaths = generate_bitmap_or_paths(root, rel,
     324             :                                           rel->baserestrictinfo, NIL);
     325      315822 :     bitindexpaths = list_concat(bitindexpaths, indexpaths);
     326             : 
     327             :     /*
     328             :      * Likewise, generate BitmapOrPaths for any suitable OR-clauses present in
     329             :      * the joinclause list.  Add these to bitjoinpaths.
     330             :      */
     331      315822 :     indexpaths = generate_bitmap_or_paths(root, rel,
     332             :                                           joinorclauses, rel->baserestrictinfo);
     333      315822 :     bitjoinpaths = list_concat(bitjoinpaths, indexpaths);
     334             : 
     335             :     /*
     336             :      * If we found anything usable, generate a BitmapHeapPath for the most
     337             :      * promising combination of restriction bitmap index paths.  Note there
     338             :      * will be only one such path no matter how many indexes exist.  This
     339             :      * should be sufficient since there's basically only one figure of merit
     340             :      * (total cost) for such a path.
     341             :      */
     342      315822 :     if (bitindexpaths != NIL)
     343             :     {
     344             :         Path       *bitmapqual;
     345             :         BitmapHeapPath *bpath;
     346             : 
     347      190718 :         bitmapqual = choose_bitmap_and(root, rel, bitindexpaths);
     348      190718 :         bpath = create_bitmap_heap_path(root, rel, bitmapqual,
     349             :                                         rel->lateral_relids, 1.0, 0);
     350      190718 :         add_path(rel, (Path *) bpath);
     351             : 
     352             :         /* create a partial bitmap heap path */
     353      190718 :         if (rel->consider_parallel && rel->lateral_relids == NULL)
     354      136284 :             create_partial_bitmap_paths(root, rel, bitmapqual);
     355             :     }
     356             : 
     357             :     /*
     358             :      * Likewise, if we found anything usable, generate BitmapHeapPaths for the
     359             :      * most promising combinations of join bitmap index paths.  Our strategy
     360             :      * is to generate one such path for each distinct parameterization seen
     361             :      * among the available bitmap index paths.  This may look pretty
     362             :      * expensive, but usually there won't be very many distinct
     363             :      * parameterizations.  (This logic is quite similar to that in
     364             :      * consider_index_join_clauses, but we're working with whole paths not
     365             :      * individual clauses.)
     366             :      */
     367      315822 :     if (bitjoinpaths != NIL)
     368             :     {
     369             :         List       *all_path_outers;
     370             : 
     371             :         /* Identify each distinct parameterization seen in bitjoinpaths */
     372      115206 :         all_path_outers = NIL;
     373      257202 :         foreach(lc, bitjoinpaths)
     374             :         {
     375      141996 :             Path       *path = (Path *) lfirst(lc);
     376      141996 :             Relids      required_outer = PATH_REQ_OUTER(path);
     377             : 
     378      141996 :             all_path_outers = list_append_unique(all_path_outers,
     379             :                                                  required_outer);
     380             :         }
     381             : 
     382             :         /* Now, for each distinct parameterization set ... */
     383      249420 :         foreach(lc, all_path_outers)
     384             :         {
     385      134214 :             Relids      max_outers = (Relids) lfirst(lc);
     386             :             List       *this_path_set;
     387             :             Path       *bitmapqual;
     388             :             Relids      required_outer;
     389             :             double      loop_count;
     390             :             BitmapHeapPath *bpath;
     391             :             ListCell   *lcp;
     392             : 
     393             :             /* Identify all the bitmap join paths needing no more than that */
     394      134214 :             this_path_set = NIL;
     395      329140 :             foreach(lcp, bitjoinpaths)
     396             :             {
     397      194926 :                 Path       *path = (Path *) lfirst(lcp);
     398             : 
     399      194926 :                 if (bms_is_subset(PATH_REQ_OUTER(path), max_outers))
     400      148870 :                     this_path_set = lappend(this_path_set, path);
     401             :             }
     402             : 
     403             :             /*
     404             :              * Add in restriction bitmap paths, since they can be used
     405             :              * together with any join paths.
     406             :              */
     407      134214 :             this_path_set = list_concat(this_path_set, bitindexpaths);
     408             : 
     409             :             /* Select best AND combination for this parameterization */
     410      134214 :             bitmapqual = choose_bitmap_and(root, rel, this_path_set);
     411             : 
     412             :             /* And push that path into the mix */
     413      134214 :             required_outer = PATH_REQ_OUTER(bitmapqual);
     414      134214 :             loop_count = get_loop_count(root, rel->relid, required_outer);
     415      134214 :             bpath = create_bitmap_heap_path(root, rel, bitmapqual,
     416             :                                             required_outer, loop_count, 0);
     417      134214 :             add_path(rel, (Path *) bpath);
     418             :         }
     419             :     }
     420             : }
     421             : 
     422             : /*
     423             :  * consider_index_join_clauses
     424             :  *    Given sets of join clauses for an index, decide which parameterized
     425             :  *    index paths to build.
     426             :  *
     427             :  * Plain indexpaths are sent directly to add_path, while potential
     428             :  * bitmap indexpaths are added to *bitindexpaths for later processing.
     429             :  *
     430             :  * 'rel' is the index's heap relation
     431             :  * 'index' is the index for which we want to generate paths
     432             :  * 'rclauseset' is the collection of indexable restriction clauses
     433             :  * 'jclauseset' is the collection of indexable simple join clauses
     434             :  * 'eclauseset' is the collection of indexable clauses from EquivalenceClasses
     435             :  * '*bitindexpaths' is the list to add bitmap paths to
     436             :  */
     437             : static void
     438      126982 : consider_index_join_clauses(PlannerInfo *root, RelOptInfo *rel,
     439             :                             IndexOptInfo *index,
     440             :                             IndexClauseSet *rclauseset,
     441             :                             IndexClauseSet *jclauseset,
     442             :                             IndexClauseSet *eclauseset,
     443             :                             List **bitindexpaths)
     444             : {
     445      126982 :     int         considered_clauses = 0;
     446      126982 :     List       *considered_relids = NIL;
     447             :     int         indexcol;
     448             : 
     449             :     /*
     450             :      * The strategy here is to identify every potentially useful set of outer
     451             :      * rels that can provide indexable join clauses.  For each such set,
     452             :      * select all the join clauses available from those outer rels, add on all
     453             :      * the indexable restriction clauses, and generate plain and/or bitmap
     454             :      * index paths for that set of clauses.  This is based on the assumption
     455             :      * that it's always better to apply a clause as an indexqual than as a
     456             :      * filter (qpqual); which is where an available clause would end up being
     457             :      * applied if we omit it from the indexquals.
     458             :      *
     459             :      * This looks expensive, but in most practical cases there won't be very
     460             :      * many distinct sets of outer rels to consider.  As a safety valve when
     461             :      * that's not true, we use a heuristic: limit the number of outer rel sets
     462             :      * considered to a multiple of the number of clauses considered.  (We'll
     463             :      * always consider using each individual join clause, though.)
     464             :      *
     465             :      * For simplicity in selecting relevant clauses, we represent each set of
     466             :      * outer rels as a maximum set of clause_relids --- that is, the indexed
     467             :      * relation itself is also included in the relids set.  considered_relids
     468             :      * lists all relids sets we've already tried.
     469             :      */
     470      315812 :     for (indexcol = 0; indexcol < index->nkeycolumns; indexcol++)
     471             :     {
     472             :         /* Consider each applicable simple join clause */
     473      188830 :         considered_clauses += list_length(jclauseset->indexclauses[indexcol]);
     474      188830 :         consider_index_join_outer_rels(root, rel, index,
     475             :                                        rclauseset, jclauseset, eclauseset,
     476             :                                        bitindexpaths,
     477             :                                        jclauseset->indexclauses[indexcol],
     478             :                                        considered_clauses,
     479             :                                        &considered_relids);
     480             :         /* Consider each applicable eclass join clause */
     481      188830 :         considered_clauses += list_length(eclauseset->indexclauses[indexcol]);
     482      188830 :         consider_index_join_outer_rels(root, rel, index,
     483             :                                        rclauseset, jclauseset, eclauseset,
     484             :                                        bitindexpaths,
     485             :                                        eclauseset->indexclauses[indexcol],
     486             :                                        considered_clauses,
     487             :                                        &considered_relids);
     488             :     }
     489      126982 : }
     490             : 
     491             : /*
     492             :  * consider_index_join_outer_rels
     493             :  *    Generate parameterized paths based on clause relids in the clause list.
     494             :  *
     495             :  * Workhorse for consider_index_join_clauses; see notes therein for rationale.
     496             :  *
     497             :  * 'rel', 'index', 'rclauseset', 'jclauseset', 'eclauseset', and
     498             :  *      'bitindexpaths' as above
     499             :  * 'indexjoinclauses' is a list of IndexClauses for join clauses
     500             :  * 'considered_clauses' is the total number of clauses considered (so far)
     501             :  * '*considered_relids' is a list of all relids sets already considered
     502             :  */
     503             : static void
     504      377660 : consider_index_join_outer_rels(PlannerInfo *root, RelOptInfo *rel,
     505             :                                IndexOptInfo *index,
     506             :                                IndexClauseSet *rclauseset,
     507             :                                IndexClauseSet *jclauseset,
     508             :                                IndexClauseSet *eclauseset,
     509             :                                List **bitindexpaths,
     510             :                                List *indexjoinclauses,
     511             :                                int considered_clauses,
     512             :                                List **considered_relids)
     513             : {
     514             :     ListCell   *lc;
     515             : 
     516             :     /* Examine relids of each joinclause in the given list */
     517      520118 :     foreach(lc, indexjoinclauses)
     518             :     {
     519      142458 :         IndexClause *iclause = (IndexClause *) lfirst(lc);
     520      142458 :         Relids      clause_relids = iclause->rinfo->clause_relids;
     521      142458 :         EquivalenceClass *parent_ec = iclause->rinfo->parent_ec;
     522             :         int         num_considered_relids;
     523             : 
     524             :         /* If we already tried its relids set, no need to do so again */
     525      142458 :         if (list_member(*considered_relids, clause_relids))
     526        3812 :             continue;
     527             : 
     528             :         /*
     529             :          * Generate the union of this clause's relids set with each
     530             :          * previously-tried set.  This ensures we try this clause along with
     531             :          * every interesting subset of previous clauses.  However, to avoid
     532             :          * exponential growth of planning time when there are many clauses,
     533             :          * limit the number of relid sets accepted to 10 * considered_clauses.
     534             :          *
     535             :          * Note: get_join_index_paths appends entries to *considered_relids,
     536             :          * but we do not need to visit such newly-added entries within this
     537             :          * loop, so we don't use foreach() here.  No real harm would be done
     538             :          * if we did visit them, since the subset check would reject them; but
     539             :          * it would waste some cycles.
     540             :          */
     541      138646 :         num_considered_relids = list_length(*considered_relids);
     542      150676 :         for (int pos = 0; pos < num_considered_relids; pos++)
     543             :         {
     544       12030 :             Relids      oldrelids = (Relids) list_nth(*considered_relids, pos);
     545             : 
     546             :             /*
     547             :              * If either is a subset of the other, no new set is possible.
     548             :              * This isn't a complete test for redundancy, but it's easy and
     549             :              * cheap.  get_join_index_paths will check more carefully if we
     550             :              * already generated the same relids set.
     551             :              */
     552       12030 :             if (bms_subset_compare(clause_relids, oldrelids) != BMS_DIFFERENT)
     553          24 :                 continue;
     554             : 
     555             :             /*
     556             :              * If this clause was derived from an equivalence class, the
     557             :              * clause list may contain other clauses derived from the same
     558             :              * eclass.  We should not consider that combining this clause with
     559             :              * one of those clauses generates a usefully different
     560             :              * parameterization; so skip if any clause derived from the same
     561             :              * eclass would already have been included when using oldrelids.
     562             :              */
     563       23850 :             if (parent_ec &&
     564       11844 :                 eclass_already_used(parent_ec, oldrelids,
     565             :                                     indexjoinclauses))
     566        8640 :                 continue;
     567             : 
     568             :             /*
     569             :              * If the number of relid sets considered exceeds our heuristic
     570             :              * limit, stop considering combinations of clauses.  We'll still
     571             :              * consider the current clause alone, though (below this loop).
     572             :              */
     573        3366 :             if (list_length(*considered_relids) >= 10 * considered_clauses)
     574           0 :                 break;
     575             : 
     576             :             /* OK, try the union set */
     577        3366 :             get_join_index_paths(root, rel, index,
     578             :                                  rclauseset, jclauseset, eclauseset,
     579             :                                  bitindexpaths,
     580             :                                  bms_union(clause_relids, oldrelids),
     581             :                                  considered_relids);
     582             :         }
     583             : 
     584             :         /* Also try this set of relids by itself */
     585      138646 :         get_join_index_paths(root, rel, index,
     586             :                              rclauseset, jclauseset, eclauseset,
     587             :                              bitindexpaths,
     588             :                              clause_relids,
     589             :                              considered_relids);
     590             :     }
     591      377660 : }
     592             : 
     593             : /*
     594             :  * get_join_index_paths
     595             :  *    Generate index paths using clauses from the specified outer relations.
     596             :  *    In addition to generating paths, relids is added to *considered_relids
     597             :  *    if not already present.
     598             :  *
     599             :  * Workhorse for consider_index_join_clauses; see notes therein for rationale.
     600             :  *
     601             :  * 'rel', 'index', 'rclauseset', 'jclauseset', 'eclauseset',
     602             :  *      'bitindexpaths', 'considered_relids' as above
     603             :  * 'relids' is the current set of relids to consider (the target rel plus
     604             :  *      one or more outer rels)
     605             :  */
     606             : static void
     607      142012 : get_join_index_paths(PlannerInfo *root, RelOptInfo *rel,
     608             :                      IndexOptInfo *index,
     609             :                      IndexClauseSet *rclauseset,
     610             :                      IndexClauseSet *jclauseset,
     611             :                      IndexClauseSet *eclauseset,
     612             :                      List **bitindexpaths,
     613             :                      Relids relids,
     614             :                      List **considered_relids)
     615             : {
     616             :     IndexClauseSet clauseset;
     617             :     int         indexcol;
     618             : 
     619             :     /* If we already considered this relids set, don't repeat the work */
     620      142012 :     if (list_member(*considered_relids, relids))
     621           0 :         return;
     622             : 
     623             :     /* Identify indexclauses usable with this relids set */
     624     4828408 :     MemSet(&clauseset, 0, sizeof(clauseset));
     625             : 
     626      359430 :     for (indexcol = 0; indexcol < index->nkeycolumns; indexcol++)
     627             :     {
     628             :         ListCell   *lc;
     629             : 
     630             :         /* First find applicable simple join clauses */
     631      250920 :         foreach(lc, jclauseset->indexclauses[indexcol])
     632             :         {
     633       33502 :             IndexClause *iclause = (IndexClause *) lfirst(lc);
     634             : 
     635       33502 :             if (bms_is_subset(iclause->rinfo->clause_relids, relids))
     636       33076 :                 clauseset.indexclauses[indexcol] =
     637       33076 :                     lappend(clauseset.indexclauses[indexcol], iclause);
     638             :         }
     639             : 
     640             :         /*
     641             :          * Add applicable eclass join clauses.  The clauses generated for each
     642             :          * column are redundant (cf generate_implied_equalities_for_column),
     643             :          * so we need at most one.  This is the only exception to the general
     644             :          * rule of using all available index clauses.
     645             :          */
     646      235240 :         foreach(lc, eclauseset->indexclauses[indexcol])
     647             :         {
     648      133864 :             IndexClause *iclause = (IndexClause *) lfirst(lc);
     649             : 
     650      133864 :             if (bms_is_subset(iclause->rinfo->clause_relids, relids))
     651             :             {
     652      116042 :                 clauseset.indexclauses[indexcol] =
     653      116042 :                     lappend(clauseset.indexclauses[indexcol], iclause);
     654      116042 :                 break;
     655             :             }
     656             :         }
     657             : 
     658             :         /* Add restriction clauses */
     659      217418 :         clauseset.indexclauses[indexcol] =
     660      217418 :             list_concat(clauseset.indexclauses[indexcol],
     661      217418 :                         rclauseset->indexclauses[indexcol]);
     662             : 
     663      217418 :         if (clauseset.indexclauses[indexcol] != NIL)
     664      174498 :             clauseset.nonempty = true;
     665             :     }
     666             : 
     667             :     /* We should have found something, else caller passed silly relids */
     668             :     Assert(clauseset.nonempty);
     669             : 
     670             :     /* Build index path(s) using the collected set of clauses */
     671      142012 :     get_index_paths(root, rel, index, &clauseset, bitindexpaths);
     672             : 
     673             :     /*
     674             :      * Remember we considered paths for this set of relids.
     675             :      */
     676      142012 :     *considered_relids = lappend(*considered_relids, relids);
     677             : }
     678             : 
     679             : /*
     680             :  * eclass_already_used
     681             :  *      True if any join clause usable with oldrelids was generated from
     682             :  *      the specified equivalence class.
     683             :  */
     684             : static bool
     685       11844 : eclass_already_used(EquivalenceClass *parent_ec, Relids oldrelids,
     686             :                     List *indexjoinclauses)
     687             : {
     688             :     ListCell   *lc;
     689             : 
     690       15510 :     foreach(lc, indexjoinclauses)
     691             :     {
     692       12306 :         IndexClause *iclause = (IndexClause *) lfirst(lc);
     693       12306 :         RestrictInfo *rinfo = iclause->rinfo;
     694             : 
     695       24612 :         if (rinfo->parent_ec == parent_ec &&
     696       12306 :             bms_is_subset(rinfo->clause_relids, oldrelids))
     697        8640 :             return true;
     698             :     }
     699        3204 :     return false;
     700             : }
     701             : 
     702             : 
     703             : /*
     704             :  * get_index_paths
     705             :  *    Given an index and a set of index clauses for it, construct IndexPaths.
     706             :  *
     707             :  * Plain indexpaths are sent directly to add_path, while potential
     708             :  * bitmap indexpaths are added to *bitindexpaths for later processing.
     709             :  *
     710             :  * This is a fairly simple frontend to build_index_paths().  Its reason for
     711             :  * existence is mainly to handle ScalarArrayOpExpr quals properly.  If the
     712             :  * index AM supports them natively, we should just include them in simple
     713             :  * index paths.  If not, we should exclude them while building simple index
     714             :  * paths, and then make a separate attempt to include them in bitmap paths.
     715             :  */
     716             : static void
     717      807396 : get_index_paths(PlannerInfo *root, RelOptInfo *rel,
     718             :                 IndexOptInfo *index, IndexClauseSet *clauses,
     719             :                 List **bitindexpaths)
     720             : {
     721             :     List       *indexpaths;
     722      807396 :     bool        skip_nonnative_saop = false;
     723             :     ListCell   *lc;
     724             : 
     725             :     /*
     726             :      * Build simple index paths using the clauses.  Allow ScalarArrayOpExpr
     727             :      * clauses only if the index AM supports them natively.
     728             :      */
     729      807396 :     indexpaths = build_index_paths(root, rel,
     730             :                                    index, clauses,
     731      807396 :                                    index->predOK,
     732             :                                    ST_ANYSCAN,
     733             :                                    &skip_nonnative_saop);
     734             : 
     735             :     /*
     736             :      * Submit all the ones that can form plain IndexScan plans to add_path. (A
     737             :      * plain IndexPath can represent either a plain IndexScan or an
     738             :      * IndexOnlyScan, but for our purposes here that distinction does not
     739             :      * matter.  However, some of the indexes might support only bitmap scans,
     740             :      * and those we mustn't submit to add_path here.)
     741             :      *
     742             :      * Also, pick out the ones that are usable as bitmap scans.  For that, we
     743             :      * must discard indexes that don't support bitmap scans, and we also are
     744             :      * only interested in paths that have some selectivity; we should discard
     745             :      * anything that was generated solely for ordering purposes.
     746             :      */
     747     1290692 :     foreach(lc, indexpaths)
     748             :     {
     749      483296 :         IndexPath  *ipath = (IndexPath *) lfirst(lc);
     750             : 
     751      483296 :         if (index->amhasgettuple)
     752      469896 :             add_path(rel, (Path *) ipath);
     753             : 
     754      483296 :         if (index->amhasgetbitmap &&
     755      483296 :             (ipath->path.pathkeys == NIL ||
     756      299250 :              ipath->indexselectivity < 1.0))
     757      356282 :             *bitindexpaths = lappend(*bitindexpaths, ipath);
     758             :     }
     759             : 
     760             :     /*
     761             :      * If there were ScalarArrayOpExpr clauses that the index can't handle
     762             :      * natively, generate bitmap scan paths relying on executor-managed
     763             :      * ScalarArrayOpExpr.
     764             :      */
     765      807396 :     if (skip_nonnative_saop)
     766             :     {
     767          32 :         indexpaths = build_index_paths(root, rel,
     768             :                                        index, clauses,
     769             :                                        false,
     770             :                                        ST_BITMAPSCAN,
     771             :                                        NULL);
     772          32 :         *bitindexpaths = list_concat(*bitindexpaths, indexpaths);
     773             :     }
     774      807396 : }
     775             : 
     776             : /*
     777             :  * build_index_paths
     778             :  *    Given an index and a set of index clauses for it, construct zero
     779             :  *    or more IndexPaths. It also constructs zero or more partial IndexPaths.
     780             :  *
     781             :  * We return a list of paths because (1) this routine checks some cases
     782             :  * that should cause us to not generate any IndexPath, and (2) in some
     783             :  * cases we want to consider both a forward and a backward scan, so as
     784             :  * to obtain both sort orders.  Note that the paths are just returned
     785             :  * to the caller and not immediately fed to add_path().
     786             :  *
     787             :  * At top level, useful_predicate should be exactly the index's predOK flag
     788             :  * (ie, true if it has a predicate that was proven from the restriction
     789             :  * clauses).  When working on an arm of an OR clause, useful_predicate
     790             :  * should be true if the predicate required the current OR list to be proven.
     791             :  * Note that this routine should never be called at all if the index has an
     792             :  * unprovable predicate.
     793             :  *
     794             :  * scantype indicates whether we want to create plain indexscans, bitmap
     795             :  * indexscans, or both.  When it's ST_BITMAPSCAN, we will not consider
     796             :  * index ordering while deciding if a Path is worth generating.
     797             :  *
     798             :  * If skip_nonnative_saop is non-NULL, we ignore ScalarArrayOpExpr clauses
     799             :  * unless the index AM supports them directly, and we set *skip_nonnative_saop
     800             :  * to true if we found any such clauses (caller must initialize the variable
     801             :  * to false).  If it's NULL, we do not ignore ScalarArrayOpExpr clauses.
     802             :  *
     803             :  * 'rel' is the index's heap relation
     804             :  * 'index' is the index for which we want to generate paths
     805             :  * 'clauses' is the collection of indexable clauses (IndexClause nodes)
     806             :  * 'useful_predicate' indicates whether the index has a useful predicate
     807             :  * 'scantype' indicates whether we need plain or bitmap scan support
     808             :  * 'skip_nonnative_saop' indicates whether to accept SAOP if index AM doesn't
     809             :  */
     810             : static List *
     811      810416 : build_index_paths(PlannerInfo *root, RelOptInfo *rel,
     812             :                   IndexOptInfo *index, IndexClauseSet *clauses,
     813             :                   bool useful_predicate,
     814             :                   ScanTypeControl scantype,
     815             :                   bool *skip_nonnative_saop)
     816             : {
     817      810416 :     List       *result = NIL;
     818             :     IndexPath  *ipath;
     819             :     List       *index_clauses;
     820             :     Relids      outer_relids;
     821             :     double      loop_count;
     822             :     List       *orderbyclauses;
     823             :     List       *orderbyclausecols;
     824             :     List       *index_pathkeys;
     825             :     List       *useful_pathkeys;
     826             :     bool        pathkeys_possibly_useful;
     827             :     bool        index_is_ordered;
     828             :     bool        index_only_scan;
     829             :     int         indexcol;
     830             : 
     831             :     Assert(skip_nonnative_saop != NULL || scantype == ST_BITMAPSCAN);
     832             : 
     833             :     /*
     834             :      * Check that index supports the desired scan type(s)
     835             :      */
     836      810416 :     switch (scantype)
     837             :     {
     838           0 :         case ST_INDEXSCAN:
     839           0 :             if (!index->amhasgettuple)
     840           0 :                 return NIL;
     841           0 :             break;
     842        3020 :         case ST_BITMAPSCAN:
     843        3020 :             if (!index->amhasgetbitmap)
     844           0 :                 return NIL;
     845        3020 :             break;
     846      807396 :         case ST_ANYSCAN:
     847             :             /* either or both are OK */
     848      807396 :             break;
     849             :     }
     850             : 
     851             :     /*
     852             :      * 1. Combine the per-column IndexClause lists into an overall list.
     853             :      *
     854             :      * In the resulting list, clauses are ordered by index key, so that the
     855             :      * column numbers form a nondecreasing sequence.  (This order is depended
     856             :      * on by btree and possibly other places.)  The list can be empty, if the
     857             :      * index AM allows that.
     858             :      *
     859             :      * We also build a Relids set showing which outer rels are required by the
     860             :      * selected clauses.  Any lateral_relids are included in that, but not
     861             :      * otherwise accounted for.
     862             :      */
     863      810416 :     index_clauses = NIL;
     864      810416 :     outer_relids = bms_copy(rel->lateral_relids);
     865     2315746 :     for (indexcol = 0; indexcol < index->nkeycolumns; indexcol++)
     866             :     {
     867             :         ListCell   *lc;
     868             : 
     869     1922034 :         foreach(lc, clauses->indexclauses[indexcol])
     870             :         {
     871      416374 :             IndexClause *iclause = (IndexClause *) lfirst(lc);
     872      416374 :             RestrictInfo *rinfo = iclause->rinfo;
     873             : 
     874      416374 :             if (skip_nonnative_saop && !index->amsearcharray &&
     875       21266 :                 IsA(rinfo->clause, ScalarArrayOpExpr))
     876             :             {
     877             :                 /*
     878             :                  * Caller asked us to generate IndexPaths that omit any
     879             :                  * ScalarArrayOpExpr clauses when the underlying index AM
     880             :                  * lacks native support.
     881             :                  *
     882             :                  * We must omit this clause (and tell caller about it).
     883             :                  */
     884          32 :                 *skip_nonnative_saop = true;
     885          32 :                 continue;
     886             :             }
     887             : 
     888             :             /* OK to include this clause */
     889      416342 :             index_clauses = lappend(index_clauses, iclause);
     890      416342 :             outer_relids = bms_add_members(outer_relids,
     891      416342 :                                            rinfo->clause_relids);
     892             :         }
     893             : 
     894             :         /*
     895             :          * If no clauses match the first index column, check for amoptionalkey
     896             :          * restriction.  We can't generate a scan over an index with
     897             :          * amoptionalkey = false unless there's at least one index clause.
     898             :          * (When working on columns after the first, this test cannot fail. It
     899             :          * is always okay for columns after the first to not have any
     900             :          * clauses.)
     901             :          */
     902     1505660 :         if (index_clauses == NIL && !index->amoptionalkey)
     903         330 :             return NIL;
     904             :     }
     905             : 
     906             :     /* We do not want the index's rel itself listed in outer_relids */
     907      810086 :     outer_relids = bms_del_member(outer_relids, rel->relid);
     908             : 
     909             :     /* Compute loop_count for cost estimation purposes */
     910      810086 :     loop_count = get_loop_count(root, rel->relid, outer_relids);
     911             : 
     912             :     /*
     913             :      * 2. Compute pathkeys describing index's ordering, if any, then see how
     914             :      * many of them are actually useful for this query.  This is not relevant
     915             :      * if we are only trying to build bitmap indexscans.
     916             :      */
     917     1617152 :     pathkeys_possibly_useful = (scantype != ST_BITMAPSCAN &&
     918      807066 :                                 has_useful_pathkeys(root, rel));
     919      810086 :     index_is_ordered = (index->sortopfamily != NULL);
     920      810086 :     if (index_is_ordered && pathkeys_possibly_useful)
     921             :     {
     922      598500 :         index_pathkeys = build_index_pathkeys(root, index,
     923             :                                               ForwardScanDirection);
     924      598500 :         useful_pathkeys = truncate_useless_pathkeys(root, rel,
     925             :                                                     index_pathkeys);
     926      598500 :         orderbyclauses = NIL;
     927      598500 :         orderbyclausecols = NIL;
     928             :     }
     929      211586 :     else if (index->amcanorderbyop && pathkeys_possibly_useful)
     930             :     {
     931             :         /*
     932             :          * See if we can generate ordering operators for query_pathkeys or at
     933             :          * least some prefix thereof.  Matching to just a prefix of the
     934             :          * query_pathkeys will allow an incremental sort to be considered on
     935             :          * the index's partially sorted results.
     936             :          */
     937        1078 :         match_pathkeys_to_index(index, root->query_pathkeys,
     938             :                                 &orderbyclauses,
     939             :                                 &orderbyclausecols);
     940        1078 :         if (list_length(root->query_pathkeys) == list_length(orderbyclauses))
     941         472 :             useful_pathkeys = root->query_pathkeys;
     942             :         else
     943         606 :             useful_pathkeys = list_copy_head(root->query_pathkeys,
     944             :                                              list_length(orderbyclauses));
     945             :     }
     946             :     else
     947             :     {
     948      210508 :         useful_pathkeys = NIL;
     949      210508 :         orderbyclauses = NIL;
     950      210508 :         orderbyclausecols = NIL;
     951             :     }
     952             : 
     953             :     /*
     954             :      * 3. Check if an index-only scan is possible.  If we're not building
     955             :      * plain indexscans, this isn't relevant since bitmap scans don't support
     956             :      * index data retrieval anyway.
     957             :      */
     958     1617152 :     index_only_scan = (scantype != ST_BITMAPSCAN &&
     959      807066 :                        check_index_only(rel, index));
     960             : 
     961             :     /*
     962             :      * 4. Generate an indexscan path if there are relevant restriction clauses
     963             :      * in the current clauses, OR the index ordering is potentially useful for
     964             :      * later merging or final output ordering, OR the index has a useful
     965             :      * predicate, OR an index-only scan is possible.
     966             :      */
     967      810086 :     if (index_clauses != NIL || useful_pathkeys != NIL || useful_predicate ||
     968             :         index_only_scan)
     969             :     {
     970      485756 :         ipath = create_index_path(root, index,
     971             :                                   index_clauses,
     972             :                                   orderbyclauses,
     973             :                                   orderbyclausecols,
     974             :                                   useful_pathkeys,
     975             :                                   ForwardScanDirection,
     976             :                                   index_only_scan,
     977             :                                   outer_relids,
     978             :                                   loop_count,
     979             :                                   false);
     980      485756 :         result = lappend(result, ipath);
     981             : 
     982             :         /*
     983             :          * If appropriate, consider parallel index scan.  We don't allow
     984             :          * parallel index scan for bitmap index scans.
     985             :          */
     986      485756 :         if (index->amcanparallel &&
     987      465374 :             rel->consider_parallel && outer_relids == NULL &&
     988             :             scantype != ST_BITMAPSCAN)
     989             :         {
     990      251816 :             ipath = create_index_path(root, index,
     991             :                                       index_clauses,
     992             :                                       orderbyclauses,
     993             :                                       orderbyclausecols,
     994             :                                       useful_pathkeys,
     995             :                                       ForwardScanDirection,
     996             :                                       index_only_scan,
     997             :                                       outer_relids,
     998             :                                       loop_count,
     999             :                                       true);
    1000             : 
    1001             :             /*
    1002             :              * if, after costing the path, we find that it's not worth using
    1003             :              * parallel workers, just free it.
    1004             :              */
    1005      251816 :             if (ipath->path.parallel_workers > 0)
    1006        9844 :                 add_partial_path(rel, (Path *) ipath);
    1007             :             else
    1008      241972 :                 pfree(ipath);
    1009             :         }
    1010             :     }
    1011             : 
    1012             :     /*
    1013             :      * 5. If the index is ordered, a backwards scan might be interesting.
    1014             :      */
    1015      810086 :     if (index_is_ordered && pathkeys_possibly_useful)
    1016             :     {
    1017      598500 :         index_pathkeys = build_index_pathkeys(root, index,
    1018             :                                               BackwardScanDirection);
    1019      598500 :         useful_pathkeys = truncate_useless_pathkeys(root, rel,
    1020             :                                                     index_pathkeys);
    1021      598500 :         if (useful_pathkeys != NIL)
    1022             :         {
    1023         560 :             ipath = create_index_path(root, index,
    1024             :                                       index_clauses,
    1025             :                                       NIL,
    1026             :                                       NIL,
    1027             :                                       useful_pathkeys,
    1028             :                                       BackwardScanDirection,
    1029             :                                       index_only_scan,
    1030             :                                       outer_relids,
    1031             :                                       loop_count,
    1032             :                                       false);
    1033         560 :             result = lappend(result, ipath);
    1034             : 
    1035             :             /* If appropriate, consider parallel index scan */
    1036         560 :             if (index->amcanparallel &&
    1037         560 :                 rel->consider_parallel && outer_relids == NULL &&
    1038             :                 scantype != ST_BITMAPSCAN)
    1039             :             {
    1040         470 :                 ipath = create_index_path(root, index,
    1041             :                                           index_clauses,
    1042             :                                           NIL,
    1043             :                                           NIL,
    1044             :                                           useful_pathkeys,
    1045             :                                           BackwardScanDirection,
    1046             :                                           index_only_scan,
    1047             :                                           outer_relids,
    1048             :                                           loop_count,
    1049             :                                           true);
    1050             : 
    1051             :                 /*
    1052             :                  * if, after costing the path, we find that it's not worth
    1053             :                  * using parallel workers, just free it.
    1054             :                  */
    1055         470 :                 if (ipath->path.parallel_workers > 0)
    1056         168 :                     add_partial_path(rel, (Path *) ipath);
    1057             :                 else
    1058         302 :                     pfree(ipath);
    1059             :             }
    1060             :         }
    1061             :     }
    1062             : 
    1063      810086 :     return result;
    1064             : }
    1065             : 
    1066             : /*
    1067             :  * build_paths_for_OR
    1068             :  *    Given a list of restriction clauses from one arm of an OR clause,
    1069             :  *    construct all matching IndexPaths for the relation.
    1070             :  *
    1071             :  * Here we must scan all indexes of the relation, since a bitmap OR tree
    1072             :  * can use multiple indexes.
    1073             :  *
    1074             :  * The caller actually supplies two lists of restriction clauses: some
    1075             :  * "current" ones and some "other" ones.  Both lists can be used freely
    1076             :  * to match keys of the index, but an index must use at least one of the
    1077             :  * "current" clauses to be considered usable.  The motivation for this is
    1078             :  * examples like
    1079             :  *      WHERE (x = 42) AND (... OR (y = 52 AND z = 77) OR ....)
    1080             :  * While we are considering the y/z subclause of the OR, we can use "x = 42"
    1081             :  * as one of the available index conditions; but we shouldn't match the
    1082             :  * subclause to any index on x alone, because such a Path would already have
    1083             :  * been generated at the upper level.  So we could use an index on x,y,z
    1084             :  * or an index on x,y for the OR subclause, but not an index on just x.
    1085             :  * When dealing with a partial index, a match of the index predicate to
    1086             :  * one of the "current" clauses also makes the index usable.
    1087             :  *
    1088             :  * 'rel' is the relation for which we want to generate index paths
    1089             :  * 'clauses' is the current list of clauses (RestrictInfo nodes)
    1090             :  * 'other_clauses' is the list of additional upper-level clauses
    1091             :  */
    1092             : static List *
    1093       11106 : build_paths_for_OR(PlannerInfo *root, RelOptInfo *rel,
    1094             :                    List *clauses, List *other_clauses)
    1095             : {
    1096       11106 :     List       *result = NIL;
    1097       11106 :     List       *all_clauses = NIL;  /* not computed till needed */
    1098             :     ListCell   *lc;
    1099             : 
    1100       39058 :     foreach(lc, rel->indexlist)
    1101             :     {
    1102       27952 :         IndexOptInfo *index = (IndexOptInfo *) lfirst(lc);
    1103             :         IndexClauseSet clauseset;
    1104             :         List       *indexpaths;
    1105             :         bool        useful_predicate;
    1106             : 
    1107             :         /* Ignore index if it doesn't support bitmap scans */
    1108       27952 :         if (!index->amhasgetbitmap)
    1109       24964 :             continue;
    1110             : 
    1111             :         /*
    1112             :          * Ignore partial indexes that do not match the query.  If a partial
    1113             :          * index is marked predOK then we know it's OK.  Otherwise, we have to
    1114             :          * test whether the added clauses are sufficient to imply the
    1115             :          * predicate. If so, we can use the index in the current context.
    1116             :          *
    1117             :          * We set useful_predicate to true iff the predicate was proven using
    1118             :          * the current set of clauses.  This is needed to prevent matching a
    1119             :          * predOK index to an arm of an OR, which would be a legal but
    1120             :          * pointlessly inefficient plan.  (A better plan will be generated by
    1121             :          * just scanning the predOK index alone, no OR.)
    1122             :          */
    1123       27952 :         useful_predicate = false;
    1124       27952 :         if (index->indpred != NIL)
    1125             :         {
    1126         168 :             if (index->predOK)
    1127             :             {
    1128             :                 /* Usable, but don't set useful_predicate */
    1129             :             }
    1130             :             else
    1131             :             {
    1132             :                 /* Form all_clauses if not done already */
    1133         144 :                 if (all_clauses == NIL)
    1134          60 :                     all_clauses = list_concat_copy(clauses, other_clauses);
    1135             : 
    1136         144 :                 if (!predicate_implied_by(index->indpred, all_clauses, false))
    1137          96 :                     continue;   /* can't use it at all */
    1138             : 
    1139          48 :                 if (!predicate_implied_by(index->indpred, other_clauses, false))
    1140          48 :                     useful_predicate = true;
    1141             :             }
    1142             :         }
    1143             : 
    1144             :         /*
    1145             :          * Identify the restriction clauses that can match the index.
    1146             :          */
    1147      947104 :         MemSet(&clauseset, 0, sizeof(clauseset));
    1148       27856 :         match_clauses_to_index(root, clauses, index, &clauseset);
    1149             : 
    1150             :         /*
    1151             :          * If no matches so far, and the index predicate isn't useful, we
    1152             :          * don't want it.
    1153             :          */
    1154       27856 :         if (!clauseset.nonempty && !useful_predicate)
    1155       24868 :             continue;
    1156             : 
    1157             :         /*
    1158             :          * Add "other" restriction clauses to the clauseset.
    1159             :          */
    1160        2988 :         match_clauses_to_index(root, other_clauses, index, &clauseset);
    1161             : 
    1162             :         /*
    1163             :          * Construct paths if possible.
    1164             :          */
    1165        2988 :         indexpaths = build_index_paths(root, rel,
    1166             :                                        index, &clauseset,
    1167             :                                        useful_predicate,
    1168             :                                        ST_BITMAPSCAN,
    1169             :                                        NULL);
    1170        2988 :         result = list_concat(result, indexpaths);
    1171             :     }
    1172             : 
    1173       11106 :     return result;
    1174             : }
    1175             : 
    1176             : /*
    1177             :  * Utility structure used to group similar OR-clause arguments in
    1178             :  * group_similar_or_args().  It represents information about the OR-clause
    1179             :  * argument and its matching index key.
    1180             :  */
    1181             : typedef struct
    1182             : {
    1183             :     int         indexnum;       /* index of the matching index, or -1 if no
    1184             :                                  * matching index */
    1185             :     int         colnum;         /* index of the matching column, or -1 if no
    1186             :                                  * matching index */
    1187             :     Oid         opno;           /* OID of the OpClause operator, or InvalidOid
    1188             :                                  * if not an OpExpr */
    1189             :     Oid         inputcollid;    /* OID of the OpClause input collation */
    1190             :     int         argindex;       /* index of the clause in the list of
    1191             :                                  * arguments */
    1192             :     int         groupindex;     /* value of argindex for the fist clause in
    1193             :                                  * the group of similar clauses */
    1194             : } OrArgIndexMatch;
    1195             : 
    1196             : /*
    1197             :  * Comparison function for OrArgIndexMatch which provides sort order placing
    1198             :  * similar OR-clause arguments together.
    1199             :  */
    1200             : static int
    1201        6820 : or_arg_index_match_cmp(const void *a, const void *b)
    1202             : {
    1203        6820 :     const OrArgIndexMatch *match_a = (const OrArgIndexMatch *) a;
    1204        6820 :     const OrArgIndexMatch *match_b = (const OrArgIndexMatch *) b;
    1205             : 
    1206        6820 :     if (match_a->indexnum < match_b->indexnum)
    1207        1286 :         return -1;
    1208        5534 :     else if (match_a->indexnum > match_b->indexnum)
    1209        2916 :         return 1;
    1210             : 
    1211        2618 :     if (match_a->colnum < match_b->colnum)
    1212         830 :         return -1;
    1213        1788 :     else if (match_a->colnum > match_b->colnum)
    1214          24 :         return 1;
    1215             : 
    1216        1764 :     if (match_a->opno < match_b->opno)
    1217          18 :         return -1;
    1218        1746 :     else if (match_a->opno > match_b->opno)
    1219          42 :         return 1;
    1220             : 
    1221        1704 :     if (match_a->inputcollid < match_b->inputcollid)
    1222           0 :         return -1;
    1223        1704 :     else if (match_a->inputcollid > match_b->inputcollid)
    1224           0 :         return 1;
    1225             : 
    1226        1704 :     if (match_a->argindex < match_b->argindex)
    1227        1626 :         return -1;
    1228          78 :     else if (match_a->argindex > match_b->argindex)
    1229          78 :         return 1;
    1230             : 
    1231           0 :     return 0;
    1232             : }
    1233             : 
    1234             : /*
    1235             :  * Another comparison function for OrArgIndexMatch.  It sorts groups together
    1236             :  * using groupindex.  The group items are then sorted by argindex.
    1237             :  */
    1238             : static int
    1239        6922 : or_arg_index_match_cmp_group(const void *a, const void *b)
    1240             : {
    1241        6922 :     const OrArgIndexMatch *match_a = (const OrArgIndexMatch *) a;
    1242        6922 :     const OrArgIndexMatch *match_b = (const OrArgIndexMatch *) b;
    1243             : 
    1244        6922 :     if (match_a->groupindex < match_b->groupindex)
    1245        3364 :         return -1;
    1246        3558 :     else if (match_a->groupindex > match_b->groupindex)
    1247        3114 :         return 1;
    1248             : 
    1249         444 :     if (match_a->argindex < match_b->argindex)
    1250         444 :         return -1;
    1251           0 :     else if (match_a->argindex > match_b->argindex)
    1252           0 :         return 1;
    1253             : 
    1254           0 :     return 0;
    1255             : }
    1256             : 
    1257             : /*
    1258             :  * group_similar_or_args
    1259             :  *      Transform incoming OR-restrictinfo into a list of sub-restrictinfos,
    1260             :  *      each of them containing a subset of similar OR-clause arguments from
    1261             :  *      the source rinfo.
    1262             :  *
    1263             :  * Similar OR-clause arguments are of the form "indexkey op constant" having
    1264             :  * the same indexkey, operator, and collation.  Constant may comprise either
    1265             :  * Const or Param.  It may be employed later, during the
    1266             :  * match_clause_to_indexcol() to transform the whole OR-sub-rinfo to an SAOP
    1267             :  * clause.
    1268             :  *
    1269             :  * Returns the processed list of OR-clause arguments.
    1270             :  */
    1271             : static List *
    1272        9414 : group_similar_or_args(PlannerInfo *root, RelOptInfo *rel, RestrictInfo *rinfo)
    1273             : {
    1274             :     int         n;
    1275             :     int         i;
    1276             :     int         group_start;
    1277             :     OrArgIndexMatch *matches;
    1278        9414 :     bool        matched = false;
    1279             :     ListCell   *lc;
    1280             :     ListCell   *lc2;
    1281             :     List       *orargs;
    1282        9414 :     List       *result = NIL;
    1283        9414 :     Index       relid = rel->relid;
    1284             : 
    1285             :     Assert(IsA(rinfo->orclause, BoolExpr));
    1286        9414 :     orargs = ((BoolExpr *) rinfo->orclause)->args;
    1287        9414 :     n = list_length(orargs);
    1288             : 
    1289             :     /*
    1290             :      * To avoid N^2 behavior, take utility pass along the list of OR-clause
    1291             :      * arguments.  For each argument, fill the OrArgIndexMatch structure,
    1292             :      * which will be used to sort these arguments at the next step.
    1293             :      */
    1294        9414 :     i = -1;
    1295        9414 :     matches = (OrArgIndexMatch *) palloc(sizeof(OrArgIndexMatch) * n);
    1296       31520 :     foreach(lc, orargs)
    1297             :     {
    1298       22106 :         Node       *arg = lfirst(lc);
    1299             :         RestrictInfo *argrinfo;
    1300             :         OpExpr     *clause;
    1301             :         Oid         opno;
    1302             :         Node       *leftop,
    1303             :                    *rightop;
    1304             :         Node       *nonConstExpr;
    1305             :         int         indexnum;
    1306             :         int         colnum;
    1307             : 
    1308       22106 :         i++;
    1309       22106 :         matches[i].argindex = i;
    1310       22106 :         matches[i].groupindex = i;
    1311       22106 :         matches[i].indexnum = -1;
    1312       22106 :         matches[i].colnum = -1;
    1313       22106 :         matches[i].opno = InvalidOid;
    1314       22106 :         matches[i].inputcollid = InvalidOid;
    1315             : 
    1316       22106 :         if (!IsA(arg, RestrictInfo))
    1317        2216 :             continue;
    1318             : 
    1319       19890 :         argrinfo = castNode(RestrictInfo, arg);
    1320             : 
    1321             :         /* Only operator clauses can match  */
    1322       19890 :         if (!IsA(argrinfo->clause, OpExpr))
    1323        8888 :             continue;
    1324             : 
    1325       11002 :         clause = (OpExpr *) argrinfo->clause;
    1326       11002 :         opno = clause->opno;
    1327             : 
    1328             :         /* Only binary operators can match  */
    1329       11002 :         if (list_length(clause->args) != 2)
    1330           0 :             continue;
    1331             : 
    1332             :         /*
    1333             :          * Ignore any RelabelType node above the operands.  This is needed to
    1334             :          * be able to apply indexscanning in binary-compatible-operator cases.
    1335             :          * Note: we can assume there is at most one RelabelType node;
    1336             :          * eval_const_expressions() will have simplified if more than one.
    1337             :          */
    1338       11002 :         leftop = get_leftop(clause);
    1339       11002 :         if (IsA(leftop, RelabelType))
    1340         204 :             leftop = (Node *) ((RelabelType *) leftop)->arg;
    1341             : 
    1342       11002 :         rightop = get_rightop(clause);
    1343       11002 :         if (IsA(rightop, RelabelType))
    1344         764 :             rightop = (Node *) ((RelabelType *) rightop)->arg;
    1345             : 
    1346             :         /*
    1347             :          * Check for clauses of the form: (indexkey operator constant) or
    1348             :          * (constant operator indexkey).  But we don't know a particular index
    1349             :          * yet.  Therefore, we try to distinguish the potential index key and
    1350             :          * constant first, then search for a matching index key among all
    1351             :          * indexes.
    1352             :          */
    1353       11002 :         if (bms_is_member(relid, argrinfo->right_relids) &&
    1354        1880 :             !bms_is_member(relid, argrinfo->left_relids) &&
    1355        1808 :             !contain_volatile_functions(leftop))
    1356             :         {
    1357        1808 :             opno = get_commutator(opno);
    1358             : 
    1359        1808 :             if (!OidIsValid(opno))
    1360             :             {
    1361             :                 /* commutator doesn't exist, we can't reverse the order */
    1362           0 :                 continue;
    1363             :             }
    1364        1808 :             nonConstExpr = rightop;
    1365             :         }
    1366        9194 :         else if (bms_is_member(relid, argrinfo->left_relids) &&
    1367        7344 :                  !bms_is_member(relid, argrinfo->right_relids) &&
    1368        7272 :                  !contain_volatile_functions(rightop))
    1369             :         {
    1370        7272 :             nonConstExpr = leftop;
    1371             :         }
    1372             :         else
    1373             :         {
    1374        1922 :             continue;
    1375             :         }
    1376             : 
    1377             :         /*
    1378             :          * Match non-constant part to the index key.  It's possible that a
    1379             :          * single non-constant part matches multiple index keys.  It's OK, we
    1380             :          * just stop with first matching index key.  Given that this choice is
    1381             :          * determined the same for every clause, we will group similar clauses
    1382             :          * together anyway.
    1383             :          */
    1384        9080 :         indexnum = 0;
    1385       19772 :         foreach(lc2, rel->indexlist)
    1386             :         {
    1387       16174 :             IndexOptInfo *index = (IndexOptInfo *) lfirst(lc2);
    1388             : 
    1389             :             /*
    1390             :              * Ignore index if it doesn't support bitmap scans or SAOP
    1391             :              * clauses.
    1392             :              */
    1393       16174 :             if (!index->amhasgetbitmap || !index->amsearcharray)
    1394          54 :                 continue;
    1395             : 
    1396       36582 :             for (colnum = 0; colnum < index->nkeycolumns; colnum++)
    1397             :             {
    1398       25944 :                 if (match_index_to_operand(nonConstExpr, colnum, index))
    1399             :                 {
    1400        5482 :                     matches[i].indexnum = indexnum;
    1401        5482 :                     matches[i].colnum = colnum;
    1402        5482 :                     matches[i].opno = opno;
    1403        5482 :                     matches[i].inputcollid = clause->inputcollid;
    1404        5482 :                     matched = true;
    1405        5482 :                     break;
    1406             :                 }
    1407             :             }
    1408             : 
    1409             :             /*
    1410             :              * Stop looping through the indexes, if we managed to match
    1411             :              * nonConstExpr to any index column.
    1412             :              */
    1413       16120 :             if (matches[i].indexnum >= 0)
    1414        5482 :                 break;
    1415       10638 :             indexnum++;
    1416             :         }
    1417             :     }
    1418             : 
    1419             :     /*
    1420             :      * Fast-path check: if no clause is matching to the index column, we can
    1421             :      * just give up at this stage and return the clause list as-is.
    1422             :      */
    1423        9414 :     if (!matched)
    1424             :     {
    1425        5630 :         pfree(matches);
    1426        5630 :         return orargs;
    1427             :     }
    1428             : 
    1429             :     /*
    1430             :      * Sort clauses to make similar clauses go together.  But at the same
    1431             :      * time, we would like to change the order of clauses as little as
    1432             :      * possible.  To do so, we reorder each group of similar clauses so that
    1433             :      * the first item of the group stays in place, and all the other items are
    1434             :      * moved after it.  So, if there are no similar clauses, the order of
    1435             :      * clauses stays the same.  When there are some groups, required
    1436             :      * reordering happens while the rest of the clauses remain in their
    1437             :      * places.  That is achieved by assigning a 'groupindex' to each clause:
    1438             :      * the number of the first item in the group in the original clause list.
    1439             :      */
    1440        3784 :     qsort(matches, n, sizeof(OrArgIndexMatch), or_arg_index_match_cmp);
    1441             : 
    1442             :     /* Assign groupindex to the sorted clauses */
    1443        9086 :     for (i = 1; i < n; i++)
    1444             :     {
    1445             :         /*
    1446             :          * When two clauses are similar and should belong to the same group,
    1447             :          * copy the 'groupindex' from the previous clause.  Given we are
    1448             :          * considering clauses in direct order, all the clauses would have a
    1449             :          * 'groupindex' equal to the 'groupindex' of the first clause in the
    1450             :          * group.
    1451             :          */
    1452        5302 :         if (matches[i].indexnum == matches[i - 1].indexnum &&
    1453        2456 :             matches[i].colnum == matches[i - 1].colnum &&
    1454        1614 :             matches[i].opno == matches[i - 1].opno &&
    1455        1566 :             matches[i].inputcollid == matches[i - 1].inputcollid &&
    1456        1566 :             matches[i].indexnum != -1)
    1457         444 :             matches[i].groupindex = matches[i - 1].groupindex;
    1458             :     }
    1459             : 
    1460             :     /* Re-sort clauses first by groupindex then by argindex */
    1461        3784 :     qsort(matches, n, sizeof(OrArgIndexMatch), or_arg_index_match_cmp_group);
    1462             : 
    1463             :     /*
    1464             :      * Group similar clauses into single sub-restrictinfo. Side effect: the
    1465             :      * resulting list of restrictions will be sorted by indexnum and colnum.
    1466             :      */
    1467        3784 :     group_start = 0;
    1468       12870 :     for (i = 1; i <= n; i++)
    1469             :     {
    1470             :         /* Check if it's a group boundary */
    1471        9086 :         if (group_start >= 0 &&
    1472        5302 :             (i == n ||
    1473        5302 :              matches[i].indexnum != matches[group_start].indexnum ||
    1474        2384 :              matches[i].colnum != matches[group_start].colnum ||
    1475        1560 :              matches[i].opno != matches[group_start].opno ||
    1476        1518 :              matches[i].inputcollid != matches[group_start].inputcollid ||
    1477        1518 :              matches[i].indexnum == -1))
    1478             :         {
    1479             :             /*
    1480             :              * One clause in group: add it "as is" to the upper-level OR.
    1481             :              */
    1482        8642 :             if (i - group_start == 1)
    1483             :             {
    1484        8330 :                 result = lappend(result,
    1485             :                                  list_nth(orargs,
    1486        8330 :                                           matches[group_start].argindex));
    1487             :             }
    1488             :             else
    1489             :             {
    1490             :                 /*
    1491             :                  * Two or more clauses in a group: create a nested OR.
    1492             :                  */
    1493         312 :                 List       *args = NIL;
    1494         312 :                 List       *rargs = NIL;
    1495             :                 RestrictInfo *subrinfo;
    1496             :                 int         j;
    1497             : 
    1498             :                 Assert(i - group_start >= 2);
    1499             : 
    1500             :                 /* Construct the list of nested OR arguments */
    1501        1068 :                 for (j = group_start; j < i; j++)
    1502             :                 {
    1503         756 :                     Node       *arg = list_nth(orargs, matches[j].argindex);
    1504             : 
    1505         756 :                     rargs = lappend(rargs, arg);
    1506         756 :                     if (IsA(arg, RestrictInfo))
    1507         756 :                         args = lappend(args, ((RestrictInfo *) arg)->clause);
    1508             :                     else
    1509           0 :                         args = lappend(args, arg);
    1510             :                 }
    1511             : 
    1512             :                 /* Construct the nested OR and wrap it with RestrictInfo */
    1513         312 :                 subrinfo = make_plain_restrictinfo(root,
    1514             :                                                    make_orclause(args),
    1515             :                                                    make_orclause(rargs),
    1516         312 :                                                    rinfo->is_pushed_down,
    1517         312 :                                                    rinfo->has_clone,
    1518         312 :                                                    rinfo->is_clone,
    1519         312 :                                                    rinfo->pseudoconstant,
    1520             :                                                    rinfo->security_level,
    1521             :                                                    rinfo->required_relids,
    1522             :                                                    rinfo->incompatible_relids,
    1523             :                                                    rinfo->outer_relids);
    1524         312 :                 result = lappend(result, subrinfo);
    1525             :             }
    1526             : 
    1527        8642 :             group_start = i;
    1528             :         }
    1529             :     }
    1530        3784 :     pfree(matches);
    1531        3784 :     return result;
    1532             : }
    1533             : 
    1534             : /*
    1535             :  * make_bitmap_paths_for_or_group
    1536             :  *      Generate bitmap paths for a group of similar OR-clause arguments
    1537             :  *      produced by group_similar_or_args().
    1538             :  *
    1539             :  * This function considers two cases: (1) matching a group of clauses to
    1540             :  * the index as a whole, and (2) matching the individual clauses one-by-one.
    1541             :  * (1) typically comprises an optimal solution.  If not, (2) typically
    1542             :  * comprises fair alternative.
    1543             :  *
    1544             :  * Ideally, we could consider all arbitrary splits of arguments into
    1545             :  * subgroups, but that could lead to unacceptable computational complexity.
    1546             :  * This is why we only consider two cases of above.
    1547             :  */
    1548             : static List *
    1549         306 : make_bitmap_paths_for_or_group(PlannerInfo *root, RelOptInfo *rel,
    1550             :                                RestrictInfo *ri, List *other_clauses)
    1551             : {
    1552         306 :     List       *jointlist = NIL;
    1553         306 :     List       *splitlist = NIL;
    1554             :     ListCell   *lc;
    1555             :     List       *orargs;
    1556         306 :     List       *args = ((BoolExpr *) ri->orclause)->args;
    1557         306 :     Cost        jointcost = 0.0,
    1558         306 :                 splitcost = 0.0;
    1559             :     Path       *bitmapqual;
    1560             :     List       *indlist;
    1561             : 
    1562             :     /*
    1563             :      * First, try to match the whole group to the one index.
    1564             :      */
    1565         306 :     orargs = list_make1(ri);
    1566         306 :     indlist = build_paths_for_OR(root, rel,
    1567             :                                  orargs,
    1568             :                                  other_clauses);
    1569         306 :     if (indlist != NIL)
    1570             :     {
    1571         300 :         bitmapqual = choose_bitmap_and(root, rel, indlist);
    1572         300 :         jointcost = bitmapqual->total_cost;
    1573         300 :         jointlist = list_make1(bitmapqual);
    1574             :     }
    1575             : 
    1576             :     /*
    1577             :      * If we manage to find a bitmap scan, which uses the group of OR-clause
    1578             :      * arguments as a whole, we can skip matching OR-clause arguments
    1579             :      * one-by-one as long as there are no other clauses, which can bring more
    1580             :      * efficiency to one-by-one case.
    1581             :      */
    1582         306 :     if (jointlist != NIL && other_clauses == NIL)
    1583          84 :         return jointlist;
    1584             : 
    1585             :     /*
    1586             :      * Also try to match all containing clauses one-by-one.
    1587             :      */
    1588         768 :     foreach(lc, args)
    1589             :     {
    1590         552 :         orargs = list_make1(lfirst(lc));
    1591             : 
    1592         552 :         indlist = build_paths_for_OR(root, rel,
    1593             :                                      orargs,
    1594             :                                      other_clauses);
    1595             : 
    1596         552 :         if (indlist == NIL)
    1597             :         {
    1598           6 :             splitlist = NIL;
    1599           6 :             break;
    1600             :         }
    1601             : 
    1602         546 :         bitmapqual = choose_bitmap_and(root, rel, indlist);
    1603         546 :         splitcost += bitmapqual->total_cost;
    1604         546 :         splitlist = lappend(splitlist, bitmapqual);
    1605             :     }
    1606             : 
    1607             :     /*
    1608             :      * Pick the best option.
    1609             :      */
    1610         222 :     if (splitlist == NIL)
    1611           6 :         return jointlist;
    1612         216 :     else if (jointlist == NIL)
    1613           0 :         return splitlist;
    1614             :     else
    1615         216 :         return (jointcost < splitcost) ? jointlist : splitlist;
    1616             : }
    1617             : 
    1618             : 
    1619             : /*
    1620             :  * generate_bitmap_or_paths
    1621             :  *      Look through the list of clauses to find OR clauses, and generate
    1622             :  *      a BitmapOrPath for each one we can handle that way.  Return a list
    1623             :  *      of the generated BitmapOrPaths.
    1624             :  *
    1625             :  * other_clauses is a list of additional clauses that can be assumed true
    1626             :  * for the purpose of generating indexquals, but are not to be searched for
    1627             :  * ORs.  (See build_paths_for_OR() for motivation.)
    1628             :  */
    1629             : static List *
    1630      632960 : generate_bitmap_or_paths(PlannerInfo *root, RelOptInfo *rel,
    1631             :                          List *clauses, List *other_clauses)
    1632             : {
    1633      632960 :     List       *result = NIL;
    1634             :     List       *all_clauses;
    1635             :     ListCell   *lc;
    1636             : 
    1637             :     /*
    1638             :      * We can use both the current and other clauses as context for
    1639             :      * build_paths_for_OR; no need to remove ORs from the lists.
    1640             :      */
    1641      632960 :     all_clauses = list_concat_copy(clauses, other_clauses);
    1642             : 
    1643      987988 :     foreach(lc, clauses)
    1644             :     {
    1645      355028 :         RestrictInfo *rinfo = lfirst_node(RestrictInfo, lc);
    1646             :         List       *pathlist;
    1647             :         Path       *bitmapqual;
    1648             :         ListCell   *j;
    1649             :         List       *groupedArgs;
    1650      355028 :         List       *inner_other_clauses = NIL;
    1651             : 
    1652             :         /* Ignore RestrictInfos that aren't ORs */
    1653      355028 :         if (!restriction_is_or_clause(rinfo))
    1654      345614 :             continue;
    1655             : 
    1656             :         /*
    1657             :          * We must be able to match at least one index to each of the arms of
    1658             :          * the OR, else we can't use it.
    1659             :          */
    1660        9414 :         pathlist = NIL;
    1661             : 
    1662             :         /*
    1663             :          * Group the similar OR-clause arguments into dedicated RestrictInfos,
    1664             :          * because each of those RestrictInfos has a chance to match the index
    1665             :          * as a whole.
    1666             :          */
    1667        9414 :         groupedArgs = group_similar_or_args(root, rel, rinfo);
    1668             : 
    1669        9414 :         if (groupedArgs != ((BoolExpr *) rinfo->orclause)->args)
    1670             :         {
    1671             :             /*
    1672             :              * Some parts of the rinfo were probably grouped.  In this case,
    1673             :              * we have a set of sub-rinfos that together are an exact
    1674             :              * duplicate of rinfo.  Thus, we need to remove the rinfo from
    1675             :              * other clauses. match_clauses_to_index detects duplicated
    1676             :              * iclauses by comparing pointers to original rinfos that would be
    1677             :              * different.  So, we must delete rinfo to avoid de-facto
    1678             :              * duplicated clauses in the index clauses list.
    1679             :              */
    1680        3784 :             inner_other_clauses = list_delete(list_copy(all_clauses), rinfo);
    1681             :         }
    1682             : 
    1683       11526 :         foreach(j, groupedArgs)
    1684             :         {
    1685       10554 :             Node       *orarg = (Node *) lfirst(j);
    1686             :             List       *indlist;
    1687             : 
    1688             :             /* OR arguments should be ANDs or sub-RestrictInfos */
    1689       10554 :             if (is_andclause(orarg))
    1690             :             {
    1691        1316 :                 List       *andargs = ((BoolExpr *) orarg)->args;
    1692             : 
    1693        1316 :                 indlist = build_paths_for_OR(root, rel,
    1694             :                                              andargs,
    1695             :                                              all_clauses);
    1696             : 
    1697             :                 /* Recurse in case there are sub-ORs */
    1698        1316 :                 indlist = list_concat(indlist,
    1699        1316 :                                       generate_bitmap_or_paths(root, rel,
    1700             :                                                                andargs,
    1701             :                                                                all_clauses));
    1702             :             }
    1703        9238 :             else if (restriction_is_or_clause(castNode(RestrictInfo, orarg)))
    1704             :             {
    1705         306 :                 RestrictInfo *ri = castNode(RestrictInfo, orarg);
    1706             : 
    1707             :                 /*
    1708             :                  * Generate bitmap paths for the group of similar OR-clause
    1709             :                  * arguments.
    1710             :                  */
    1711         306 :                 indlist = make_bitmap_paths_for_or_group(root,
    1712             :                                                          rel, ri,
    1713             :                                                          inner_other_clauses);
    1714             : 
    1715         306 :                 if (indlist == NIL)
    1716             :                 {
    1717           6 :                     pathlist = NIL;
    1718           6 :                     break;
    1719             :                 }
    1720             :                 else
    1721             :                 {
    1722         300 :                     pathlist = list_concat(pathlist, indlist);
    1723         300 :                     continue;
    1724             :                 }
    1725             :             }
    1726             :             else
    1727             :             {
    1728        8932 :                 RestrictInfo *ri = castNode(RestrictInfo, orarg);
    1729             :                 List       *orargs;
    1730             : 
    1731        8932 :                 orargs = list_make1(ri);
    1732             : 
    1733        8932 :                 indlist = build_paths_for_OR(root, rel,
    1734             :                                              orargs,
    1735             :                                              all_clauses);
    1736             :             }
    1737             : 
    1738             :             /*
    1739             :              * If nothing matched this arm, we can't do anything with this OR
    1740             :              * clause.
    1741             :              */
    1742       10248 :             if (indlist == NIL)
    1743             :             {
    1744        8436 :                 pathlist = NIL;
    1745        8436 :                 break;
    1746             :             }
    1747             : 
    1748             :             /*
    1749             :              * OK, pick the most promising AND combination, and add it to
    1750             :              * pathlist.
    1751             :              */
    1752        1812 :             bitmapqual = choose_bitmap_and(root, rel, indlist);
    1753        1812 :             pathlist = lappend(pathlist, bitmapqual);
    1754             :         }
    1755             : 
    1756        9414 :         if (inner_other_clauses != NIL)
    1757        2098 :             list_free(inner_other_clauses);
    1758             : 
    1759             :         /*
    1760             :          * If we have a match for every arm, then turn them into a
    1761             :          * BitmapOrPath, and add to result list.
    1762             :          */
    1763        9414 :         if (pathlist != NIL)
    1764             :         {
    1765         972 :             bitmapqual = (Path *) create_bitmap_or_path(root, rel, pathlist);
    1766         972 :             result = lappend(result, bitmapqual);
    1767             :         }
    1768             :     }
    1769             : 
    1770      632960 :     return result;
    1771             : }
    1772             : 
    1773             : 
    1774             : /*
    1775             :  * choose_bitmap_and
    1776             :  *      Given a nonempty list of bitmap paths, AND them into one path.
    1777             :  *
    1778             :  * This is a nontrivial decision since we can legally use any subset of the
    1779             :  * given path set.  We want to choose a good tradeoff between selectivity
    1780             :  * and cost of computing the bitmap.
    1781             :  *
    1782             :  * The result is either a single one of the inputs, or a BitmapAndPath
    1783             :  * combining multiple inputs.
    1784             :  */
    1785             : static Path *
    1786      327590 : choose_bitmap_and(PlannerInfo *root, RelOptInfo *rel, List *paths)
    1787             : {
    1788      327590 :     int         npaths = list_length(paths);
    1789             :     PathClauseUsage **pathinfoarray;
    1790             :     PathClauseUsage *pathinfo;
    1791             :     List       *clauselist;
    1792      327590 :     List       *bestpaths = NIL;
    1793      327590 :     Cost        bestcost = 0;
    1794             :     int         i,
    1795             :                 j;
    1796             :     ListCell   *l;
    1797             : 
    1798             :     Assert(npaths > 0);          /* else caller error */
    1799      327590 :     if (npaths == 1)
    1800      251326 :         return (Path *) linitial(paths);    /* easy case */
    1801             : 
    1802             :     /*
    1803             :      * In theory we should consider every nonempty subset of the given paths.
    1804             :      * In practice that seems like overkill, given the crude nature of the
    1805             :      * estimates, not to mention the possible effects of higher-level AND and
    1806             :      * OR clauses.  Moreover, it's completely impractical if there are a large
    1807             :      * number of paths, since the work would grow as O(2^N).
    1808             :      *
    1809             :      * As a heuristic, we first check for paths using exactly the same sets of
    1810             :      * WHERE clauses + index predicate conditions, and reject all but the
    1811             :      * cheapest-to-scan in any such group.  This primarily gets rid of indexes
    1812             :      * that include the interesting columns but also irrelevant columns.  (In
    1813             :      * situations where the DBA has gone overboard on creating variant
    1814             :      * indexes, this can make for a very large reduction in the number of
    1815             :      * paths considered further.)
    1816             :      *
    1817             :      * We then sort the surviving paths with the cheapest-to-scan first, and
    1818             :      * for each path, consider using that path alone as the basis for a bitmap
    1819             :      * scan.  Then we consider bitmap AND scans formed from that path plus
    1820             :      * each subsequent (higher-cost) path, adding on a subsequent path if it
    1821             :      * results in a reduction in the estimated total scan cost. This means we
    1822             :      * consider about O(N^2) rather than O(2^N) path combinations, which is
    1823             :      * quite tolerable, especially given than N is usually reasonably small
    1824             :      * because of the prefiltering step.  The cheapest of these is returned.
    1825             :      *
    1826             :      * We will only consider AND combinations in which no two indexes use the
    1827             :      * same WHERE clause.  This is a bit of a kluge: it's needed because
    1828             :      * costsize.c and clausesel.c aren't very smart about redundant clauses.
    1829             :      * They will usually double-count the redundant clauses, producing a
    1830             :      * too-small selectivity that makes a redundant AND step look like it
    1831             :      * reduces the total cost.  Perhaps someday that code will be smarter and
    1832             :      * we can remove this limitation.  (But note that this also defends
    1833             :      * against flat-out duplicate input paths, which can happen because
    1834             :      * match_join_clauses_to_index will find the same OR join clauses that
    1835             :      * extract_restriction_or_clauses has pulled OR restriction clauses out
    1836             :      * of.)
    1837             :      *
    1838             :      * For the same reason, we reject AND combinations in which an index
    1839             :      * predicate clause duplicates another clause.  Here we find it necessary
    1840             :      * to be even stricter: we'll reject a partial index if any of its
    1841             :      * predicate clauses are implied by the set of WHERE clauses and predicate
    1842             :      * clauses used so far.  This covers cases such as a condition "x = 42"
    1843             :      * used with a plain index, followed by a clauseless scan of a partial
    1844             :      * index "WHERE x >= 40 AND x < 50".  The partial index has been accepted
    1845             :      * only because "x = 42" was present, and so allowing it would partially
    1846             :      * double-count selectivity.  (We could use predicate_implied_by on
    1847             :      * regular qual clauses too, to have a more intelligent, but much more
    1848             :      * expensive, check for redundancy --- but in most cases simple equality
    1849             :      * seems to suffice.)
    1850             :      */
    1851             : 
    1852             :     /*
    1853             :      * Extract clause usage info and detect any paths that use exactly the
    1854             :      * same set of clauses; keep only the cheapest-to-scan of any such groups.
    1855             :      * The surviving paths are put into an array for qsort'ing.
    1856             :      */
    1857             :     pathinfoarray = (PathClauseUsage **)
    1858       76264 :         palloc(npaths * sizeof(PathClauseUsage *));
    1859       76264 :     clauselist = NIL;
    1860       76264 :     npaths = 0;
    1861      251384 :     foreach(l, paths)
    1862             :     {
    1863      175120 :         Path       *ipath = (Path *) lfirst(l);
    1864             : 
    1865      175120 :         pathinfo = classify_index_clause_usage(ipath, &clauselist);
    1866             : 
    1867             :         /* If it's unclassifiable, treat it as distinct from all others */
    1868      175120 :         if (pathinfo->unclassifiable)
    1869             :         {
    1870           0 :             pathinfoarray[npaths++] = pathinfo;
    1871           0 :             continue;
    1872             :         }
    1873             : 
    1874      273618 :         for (i = 0; i < npaths; i++)
    1875             :         {
    1876      242860 :             if (!pathinfoarray[i]->unclassifiable &&
    1877      121430 :                 bms_equal(pathinfo->clauseids, pathinfoarray[i]->clauseids))
    1878       22932 :                 break;
    1879             :         }
    1880      175120 :         if (i < npaths)
    1881             :         {
    1882             :             /* duplicate clauseids, keep the cheaper one */
    1883             :             Cost        ncost;
    1884             :             Cost        ocost;
    1885             :             Selectivity nselec;
    1886             :             Selectivity oselec;
    1887             : 
    1888       22932 :             cost_bitmap_tree_node(pathinfo->path, &ncost, &nselec);
    1889       22932 :             cost_bitmap_tree_node(pathinfoarray[i]->path, &ocost, &oselec);
    1890       22932 :             if (ncost < ocost)
    1891        5190 :                 pathinfoarray[i] = pathinfo;
    1892             :         }
    1893             :         else
    1894             :         {
    1895             :             /* not duplicate clauseids, add to array */
    1896      152188 :             pathinfoarray[npaths++] = pathinfo;
    1897             :         }
    1898             :     }
    1899             : 
    1900             :     /* If only one surviving path, we're done */
    1901       76264 :     if (npaths == 1)
    1902       14492 :         return pathinfoarray[0]->path;
    1903             : 
    1904             :     /* Sort the surviving paths by index access cost */
    1905       61772 :     qsort(pathinfoarray, npaths, sizeof(PathClauseUsage *),
    1906             :           path_usage_comparator);
    1907             : 
    1908             :     /*
    1909             :      * For each surviving index, consider it as an "AND group leader", and see
    1910             :      * whether adding on any of the later indexes results in an AND path with
    1911             :      * cheaper total cost than before.  Then take the cheapest AND group.
    1912             :      *
    1913             :      * Note: paths that are either clauseless or unclassifiable will have
    1914             :      * empty clauseids, so that they will not be rejected by the clauseids
    1915             :      * filter here, nor will they cause later paths to be rejected by it.
    1916             :      */
    1917      199468 :     for (i = 0; i < npaths; i++)
    1918             :     {
    1919             :         Cost        costsofar;
    1920             :         List       *qualsofar;
    1921             :         Bitmapset  *clauseidsofar;
    1922             : 
    1923      137696 :         pathinfo = pathinfoarray[i];
    1924      137696 :         paths = list_make1(pathinfo->path);
    1925      137696 :         costsofar = bitmap_scan_cost_est(root, rel, pathinfo->path);
    1926      137696 :         qualsofar = list_concat_copy(pathinfo->quals, pathinfo->preds);
    1927      137696 :         clauseidsofar = bms_copy(pathinfo->clauseids);
    1928             : 
    1929      228210 :         for (j = i + 1; j < npaths; j++)
    1930             :         {
    1931             :             Cost        newcost;
    1932             : 
    1933       90514 :             pathinfo = pathinfoarray[j];
    1934             :             /* Check for redundancy */
    1935       90514 :             if (bms_overlap(pathinfo->clauseids, clauseidsofar))
    1936       41508 :                 continue;       /* consider it redundant */
    1937       49006 :             if (pathinfo->preds)
    1938             :             {
    1939          24 :                 bool        redundant = false;
    1940             : 
    1941             :                 /* we check each predicate clause separately */
    1942          24 :                 foreach(l, pathinfo->preds)
    1943             :                 {
    1944          24 :                     Node       *np = (Node *) lfirst(l);
    1945             : 
    1946          24 :                     if (predicate_implied_by(list_make1(np), qualsofar, false))
    1947             :                     {
    1948          24 :                         redundant = true;
    1949          24 :                         break;  /* out of inner foreach loop */
    1950             :                     }
    1951             :                 }
    1952          24 :                 if (redundant)
    1953          24 :                     continue;
    1954             :             }
    1955             :             /* tentatively add new path to paths, so we can estimate cost */
    1956       48982 :             paths = lappend(paths, pathinfo->path);
    1957       48982 :             newcost = bitmap_and_cost_est(root, rel, paths);
    1958       48982 :             if (newcost < costsofar)
    1959             :             {
    1960             :                 /* keep new path in paths, update subsidiary variables */
    1961         272 :                 costsofar = newcost;
    1962         272 :                 qualsofar = list_concat(qualsofar, pathinfo->quals);
    1963         272 :                 qualsofar = list_concat(qualsofar, pathinfo->preds);
    1964         272 :                 clauseidsofar = bms_add_members(clauseidsofar,
    1965         272 :                                                 pathinfo->clauseids);
    1966             :             }
    1967             :             else
    1968             :             {
    1969             :                 /* reject new path, remove it from paths list */
    1970       48710 :                 paths = list_truncate(paths, list_length(paths) - 1);
    1971             :             }
    1972             :         }
    1973             : 
    1974             :         /* Keep the cheapest AND-group (or singleton) */
    1975      137696 :         if (i == 0 || costsofar < bestcost)
    1976             :         {
    1977       65520 :             bestpaths = paths;
    1978       65520 :             bestcost = costsofar;
    1979             :         }
    1980             : 
    1981             :         /* some easy cleanup (we don't try real hard though) */
    1982      137696 :         list_free(qualsofar);
    1983             :     }
    1984             : 
    1985       61772 :     if (list_length(bestpaths) == 1)
    1986       61524 :         return (Path *) linitial(bestpaths);    /* no need for AND */
    1987         248 :     return (Path *) create_bitmap_and_path(root, rel, bestpaths);
    1988             : }
    1989             : 
    1990             : /* qsort comparator to sort in increasing index access cost order */
    1991             : static int
    1992       85464 : path_usage_comparator(const void *a, const void *b)
    1993             : {
    1994       85464 :     PathClauseUsage *pa = *(PathClauseUsage *const *) a;
    1995       85464 :     PathClauseUsage *pb = *(PathClauseUsage *const *) b;
    1996             :     Cost        acost;
    1997             :     Cost        bcost;
    1998             :     Selectivity aselec;
    1999             :     Selectivity bselec;
    2000             : 
    2001       85464 :     cost_bitmap_tree_node(pa->path, &acost, &aselec);
    2002       85464 :     cost_bitmap_tree_node(pb->path, &bcost, &bselec);
    2003             : 
    2004             :     /*
    2005             :      * If costs are the same, sort by selectivity.
    2006             :      */
    2007       85464 :     if (acost < bcost)
    2008       48594 :         return -1;
    2009       36870 :     if (acost > bcost)
    2010       25482 :         return 1;
    2011             : 
    2012       11388 :     if (aselec < bselec)
    2013        3926 :         return -1;
    2014        7462 :     if (aselec > bselec)
    2015        1094 :         return 1;
    2016             : 
    2017        6368 :     return 0;
    2018             : }
    2019             : 
    2020             : /*
    2021             :  * Estimate the cost of actually executing a bitmap scan with a single
    2022             :  * index path (which could be a BitmapAnd or BitmapOr node).
    2023             :  */
    2024             : static Cost
    2025      186678 : bitmap_scan_cost_est(PlannerInfo *root, RelOptInfo *rel, Path *ipath)
    2026             : {
    2027             :     BitmapHeapPath bpath;
    2028             : 
    2029             :     /* Set up a dummy BitmapHeapPath */
    2030      186678 :     bpath.path.type = T_BitmapHeapPath;
    2031      186678 :     bpath.path.pathtype = T_BitmapHeapScan;
    2032      186678 :     bpath.path.parent = rel;
    2033      186678 :     bpath.path.pathtarget = rel->reltarget;
    2034      186678 :     bpath.path.param_info = ipath->param_info;
    2035      186678 :     bpath.path.pathkeys = NIL;
    2036      186678 :     bpath.bitmapqual = ipath;
    2037             : 
    2038             :     /*
    2039             :      * Check the cost of temporary path without considering parallelism.
    2040             :      * Parallel bitmap heap path will be considered at later stage.
    2041             :      */
    2042      186678 :     bpath.path.parallel_workers = 0;
    2043             : 
    2044             :     /* Now we can do cost_bitmap_heap_scan */
    2045      186678 :     cost_bitmap_heap_scan(&bpath.path, root, rel,
    2046             :                           bpath.path.param_info,
    2047             :                           ipath,
    2048             :                           get_loop_count(root, rel->relid,
    2049      186678 :                                          PATH_REQ_OUTER(ipath)));
    2050             : 
    2051      186678 :     return bpath.path.total_cost;
    2052             : }
    2053             : 
    2054             : /*
    2055             :  * Estimate the cost of actually executing a BitmapAnd scan with the given
    2056             :  * inputs.
    2057             :  */
    2058             : static Cost
    2059       48982 : bitmap_and_cost_est(PlannerInfo *root, RelOptInfo *rel, List *paths)
    2060             : {
    2061             :     BitmapAndPath *apath;
    2062             : 
    2063             :     /*
    2064             :      * Might as well build a real BitmapAndPath here, as the work is slightly
    2065             :      * too complicated to be worth repeating just to save one palloc.
    2066             :      */
    2067       48982 :     apath = create_bitmap_and_path(root, rel, paths);
    2068             : 
    2069       48982 :     return bitmap_scan_cost_est(root, rel, (Path *) apath);
    2070             : }
    2071             : 
    2072             : 
    2073             : /*
    2074             :  * classify_index_clause_usage
    2075             :  *      Construct a PathClauseUsage struct describing the WHERE clauses and
    2076             :  *      index predicate clauses used by the given indexscan path.
    2077             :  *      We consider two clauses the same if they are equal().
    2078             :  *
    2079             :  * At some point we might want to migrate this info into the Path data
    2080             :  * structure proper, but for the moment it's only needed within
    2081             :  * choose_bitmap_and().
    2082             :  *
    2083             :  * *clauselist is used and expanded as needed to identify all the distinct
    2084             :  * clauses seen across successive calls.  Caller must initialize it to NIL
    2085             :  * before first call of a set.
    2086             :  */
    2087             : static PathClauseUsage *
    2088      175120 : classify_index_clause_usage(Path *path, List **clauselist)
    2089             : {
    2090             :     PathClauseUsage *result;
    2091             :     Bitmapset  *clauseids;
    2092             :     ListCell   *lc;
    2093             : 
    2094      175120 :     result = (PathClauseUsage *) palloc(sizeof(PathClauseUsage));
    2095      175120 :     result->path = path;
    2096             : 
    2097             :     /* Recursively find the quals and preds used by the path */
    2098      175120 :     result->quals = NIL;
    2099      175120 :     result->preds = NIL;
    2100      175120 :     find_indexpath_quals(path, &result->quals, &result->preds);
    2101             : 
    2102             :     /*
    2103             :      * Some machine-generated queries have outlandish numbers of qual clauses.
    2104             :      * To avoid getting into O(N^2) behavior even in this preliminary
    2105             :      * classification step, we want to limit the number of entries we can
    2106             :      * accumulate in *clauselist.  Treat any path with more than 100 quals +
    2107             :      * preds as unclassifiable, which will cause calling code to consider it
    2108             :      * distinct from all other paths.
    2109             :      */
    2110      175120 :     if (list_length(result->quals) + list_length(result->preds) > 100)
    2111             :     {
    2112           0 :         result->clauseids = NULL;
    2113           0 :         result->unclassifiable = true;
    2114           0 :         return result;
    2115             :     }
    2116             : 
    2117             :     /* Build up a bitmapset representing the quals and preds */
    2118      175120 :     clauseids = NULL;
    2119      400982 :     foreach(lc, result->quals)
    2120             :     {
    2121      225862 :         Node       *node = (Node *) lfirst(lc);
    2122             : 
    2123      225862 :         clauseids = bms_add_member(clauseids,
    2124             :                                    find_list_position(node, clauselist));
    2125             :     }
    2126      175414 :     foreach(lc, result->preds)
    2127             :     {
    2128         294 :         Node       *node = (Node *) lfirst(lc);
    2129             : 
    2130         294 :         clauseids = bms_add_member(clauseids,
    2131             :                                    find_list_position(node, clauselist));
    2132             :     }
    2133      175120 :     result->clauseids = clauseids;
    2134      175120 :     result->unclassifiable = false;
    2135             : 
    2136      175120 :     return result;
    2137             : }
    2138             : 
    2139             : 
    2140             : /*
    2141             :  * find_indexpath_quals
    2142             :  *
    2143             :  * Given the Path structure for a plain or bitmap indexscan, extract lists
    2144             :  * of all the index clauses and index predicate conditions used in the Path.
    2145             :  * These are appended to the initial contents of *quals and *preds (hence
    2146             :  * caller should initialize those to NIL).
    2147             :  *
    2148             :  * Note we are not trying to produce an accurate representation of the AND/OR
    2149             :  * semantics of the Path, but just find out all the base conditions used.
    2150             :  *
    2151             :  * The result lists contain pointers to the expressions used in the Path,
    2152             :  * but all the list cells are freshly built, so it's safe to destructively
    2153             :  * modify the lists (eg, by concat'ing with other lists).
    2154             :  */
    2155             : static void
    2156      177238 : find_indexpath_quals(Path *bitmapqual, List **quals, List **preds)
    2157             : {
    2158      177238 :     if (IsA(bitmapqual, BitmapAndPath))
    2159             :     {
    2160           0 :         BitmapAndPath *apath = (BitmapAndPath *) bitmapqual;
    2161             :         ListCell   *l;
    2162             : 
    2163           0 :         foreach(l, apath->bitmapquals)
    2164             :         {
    2165           0 :             find_indexpath_quals((Path *) lfirst(l), quals, preds);
    2166             :         }
    2167             :     }
    2168      177238 :     else if (IsA(bitmapqual, BitmapOrPath))
    2169             :     {
    2170        1158 :         BitmapOrPath *opath = (BitmapOrPath *) bitmapqual;
    2171             :         ListCell   *l;
    2172             : 
    2173        3276 :         foreach(l, opath->bitmapquals)
    2174             :         {
    2175        2118 :             find_indexpath_quals((Path *) lfirst(l), quals, preds);
    2176             :         }
    2177             :     }
    2178      176080 :     else if (IsA(bitmapqual, IndexPath))
    2179             :     {
    2180      176080 :         IndexPath  *ipath = (IndexPath *) bitmapqual;
    2181             :         ListCell   *l;
    2182             : 
    2183      401942 :         foreach(l, ipath->indexclauses)
    2184             :         {
    2185      225862 :             IndexClause *iclause = (IndexClause *) lfirst(l);
    2186             : 
    2187      225862 :             *quals = lappend(*quals, iclause->rinfo->clause);
    2188             :         }
    2189      176080 :         *preds = list_concat(*preds, ipath->indexinfo->indpred);
    2190             :     }
    2191             :     else
    2192           0 :         elog(ERROR, "unrecognized node type: %d", nodeTag(bitmapqual));
    2193      177238 : }
    2194             : 
    2195             : 
    2196             : /*
    2197             :  * find_list_position
    2198             :  *      Return the given node's position (counting from 0) in the given
    2199             :  *      list of nodes.  If it's not equal() to any existing list member,
    2200             :  *      add it at the end, and return that position.
    2201             :  */
    2202             : static int
    2203      226156 : find_list_position(Node *node, List **nodelist)
    2204             : {
    2205             :     int         i;
    2206             :     ListCell   *lc;
    2207             : 
    2208      226156 :     i = 0;
    2209      355448 :     foreach(lc, *nodelist)
    2210             :     {
    2211      196488 :         Node       *oldnode = (Node *) lfirst(lc);
    2212             : 
    2213      196488 :         if (equal(node, oldnode))
    2214       67196 :             return i;
    2215      129292 :         i++;
    2216             :     }
    2217             : 
    2218      158960 :     *nodelist = lappend(*nodelist, node);
    2219             : 
    2220      158960 :     return i;
    2221             : }
    2222             : 
    2223             : 
    2224             : /*
    2225             :  * check_index_only
    2226             :  *      Determine whether an index-only scan is possible for this index.
    2227             :  */
    2228             : static bool
    2229      807066 : check_index_only(RelOptInfo *rel, IndexOptInfo *index)
    2230             : {
    2231             :     bool        result;
    2232      807066 :     Bitmapset  *attrs_used = NULL;
    2233      807066 :     Bitmapset  *index_canreturn_attrs = NULL;
    2234             :     ListCell   *lc;
    2235             :     int         i;
    2236             : 
    2237             :     /* Index-only scans must be enabled */
    2238      807066 :     if (!enable_indexonlyscan)
    2239        3686 :         return false;
    2240             : 
    2241             :     /*
    2242             :      * Check that all needed attributes of the relation are available from the
    2243             :      * index.
    2244             :      */
    2245             : 
    2246             :     /*
    2247             :      * First, identify all the attributes needed for joins or final output.
    2248             :      * Note: we must look at rel's targetlist, not the attr_needed data,
    2249             :      * because attr_needed isn't computed for inheritance child rels.
    2250             :      */
    2251      803380 :     pull_varattnos((Node *) rel->reltarget->exprs, rel->relid, &attrs_used);
    2252             : 
    2253             :     /*
    2254             :      * Add all the attributes used by restriction clauses; but consider only
    2255             :      * those clauses not implied by the index predicate, since ones that are
    2256             :      * so implied don't need to be checked explicitly in the plan.
    2257             :      *
    2258             :      * Note: attributes used only in index quals would not be needed at
    2259             :      * runtime either, if we are certain that the index is not lossy.  However
    2260             :      * it'd be complicated to account for that accurately, and it doesn't
    2261             :      * matter in most cases, since we'd conclude that such attributes are
    2262             :      * available from the index anyway.
    2263             :      */
    2264     1661492 :     foreach(lc, index->indrestrictinfo)
    2265             :     {
    2266      858112 :         RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
    2267             : 
    2268      858112 :         pull_varattnos((Node *) rinfo->clause, rel->relid, &attrs_used);
    2269             :     }
    2270             : 
    2271             :     /*
    2272             :      * Construct a bitmapset of columns that the index can return back in an
    2273             :      * index-only scan.
    2274             :      */
    2275     2299750 :     for (i = 0; i < index->ncolumns; i++)
    2276             :     {
    2277     1496370 :         int         attno = index->indexkeys[i];
    2278             : 
    2279             :         /*
    2280             :          * For the moment, we just ignore index expressions.  It might be nice
    2281             :          * to do something with them, later.
    2282             :          */
    2283     1496370 :         if (attno == 0)
    2284        3274 :             continue;
    2285             : 
    2286     1493096 :         if (index->canreturn[i])
    2287             :             index_canreturn_attrs =
    2288     1217946 :                 bms_add_member(index_canreturn_attrs,
    2289             :                                attno - FirstLowInvalidHeapAttributeNumber);
    2290             :     }
    2291             : 
    2292             :     /* Do we have all the necessary attributes? */
    2293      803380 :     result = bms_is_subset(attrs_used, index_canreturn_attrs);
    2294             : 
    2295      803380 :     bms_free(attrs_used);
    2296      803380 :     bms_free(index_canreturn_attrs);
    2297             : 
    2298      803380 :     return result;
    2299             : }
    2300             : 
    2301             : /*
    2302             :  * get_loop_count
    2303             :  *      Choose the loop count estimate to use for costing a parameterized path
    2304             :  *      with the given set of outer relids.
    2305             :  *
    2306             :  * Since we produce parameterized paths before we've begun to generate join
    2307             :  * relations, it's impossible to predict exactly how many times a parameterized
    2308             :  * path will be iterated; we don't know the size of the relation that will be
    2309             :  * on the outside of the nestloop.  However, we should try to account for
    2310             :  * multiple iterations somehow in costing the path.  The heuristic embodied
    2311             :  * here is to use the rowcount of the smallest other base relation needed in
    2312             :  * the join clauses used by the path.  (We could alternatively consider the
    2313             :  * largest one, but that seems too optimistic.)  This is of course the right
    2314             :  * answer for single-other-relation cases, and it seems like a reasonable
    2315             :  * zero-order approximation for multiway-join cases.
    2316             :  *
    2317             :  * In addition, we check to see if the other side of each join clause is on
    2318             :  * the inside of some semijoin that the current relation is on the outside of.
    2319             :  * If so, the only way that a parameterized path could be used is if the
    2320             :  * semijoin RHS has been unique-ified, so we should use the number of unique
    2321             :  * RHS rows rather than using the relation's raw rowcount.
    2322             :  *
    2323             :  * Note: for this to work, allpaths.c must establish all baserel size
    2324             :  * estimates before it begins to compute paths, or at least before it
    2325             :  * calls create_index_paths().
    2326             :  */
    2327             : static double
    2328     1130978 : get_loop_count(PlannerInfo *root, Index cur_relid, Relids outer_relids)
    2329             : {
    2330             :     double      result;
    2331             :     int         outer_relid;
    2332             : 
    2333             :     /* For a non-parameterized path, just return 1.0 quickly */
    2334     1130978 :     if (outer_relids == NULL)
    2335      768386 :         return 1.0;
    2336             : 
    2337      362592 :     result = 0.0;
    2338      362592 :     outer_relid = -1;
    2339      735950 :     while ((outer_relid = bms_next_member(outer_relids, outer_relid)) >= 0)
    2340             :     {
    2341             :         RelOptInfo *outer_rel;
    2342             :         double      rowcount;
    2343             : 
    2344             :         /* Paranoia: ignore bogus relid indexes */
    2345      373358 :         if (outer_relid >= root->simple_rel_array_size)
    2346           0 :             continue;
    2347      373358 :         outer_rel = root->simple_rel_array[outer_relid];
    2348      373358 :         if (outer_rel == NULL)
    2349         254 :             continue;
    2350             :         Assert(outer_rel->relid == outer_relid); /* sanity check on array */
    2351             : 
    2352             :         /* Other relation could be proven empty, if so ignore */
    2353      373104 :         if (IS_DUMMY_REL(outer_rel))
    2354          24 :             continue;
    2355             : 
    2356             :         /* Otherwise, rel's rows estimate should be valid by now */
    2357             :         Assert(outer_rel->rows > 0);
    2358             : 
    2359             :         /* Check to see if rel is on the inside of any semijoins */
    2360      373080 :         rowcount = adjust_rowcount_for_semijoins(root,
    2361             :                                                  cur_relid,
    2362             :                                                  outer_relid,
    2363             :                                                  outer_rel->rows);
    2364             : 
    2365             :         /* Remember smallest row count estimate among the outer rels */
    2366      373080 :         if (result == 0.0 || result > rowcount)
    2367      369516 :             result = rowcount;
    2368             :     }
    2369             :     /* Return 1.0 if we found no valid relations (shouldn't happen) */
    2370      362592 :     return (result > 0.0) ? result : 1.0;
    2371             : }
    2372             : 
    2373             : /*
    2374             :  * Check to see if outer_relid is on the inside of any semijoin that cur_relid
    2375             :  * is on the outside of.  If so, replace rowcount with the estimated number of
    2376             :  * unique rows from the semijoin RHS (assuming that's smaller, which it might
    2377             :  * not be).  The estimate is crude but it's the best we can do at this stage
    2378             :  * of the proceedings.
    2379             :  */
    2380             : static double
    2381      373080 : adjust_rowcount_for_semijoins(PlannerInfo *root,
    2382             :                               Index cur_relid,
    2383             :                               Index outer_relid,
    2384             :                               double rowcount)
    2385             : {
    2386             :     ListCell   *lc;
    2387             : 
    2388      578594 :     foreach(lc, root->join_info_list)
    2389             :     {
    2390      205514 :         SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) lfirst(lc);
    2391             : 
    2392      212670 :         if (sjinfo->jointype == JOIN_SEMI &&
    2393       10146 :             bms_is_member(cur_relid, sjinfo->syn_lefthand) &&
    2394        2990 :             bms_is_member(outer_relid, sjinfo->syn_righthand))
    2395             :         {
    2396             :             /* Estimate number of unique-ified rows */
    2397             :             double      nraw;
    2398             :             double      nunique;
    2399             : 
    2400        1020 :             nraw = approximate_joinrel_size(root, sjinfo->syn_righthand);
    2401        1020 :             nunique = estimate_num_groups(root,
    2402             :                                           sjinfo->semi_rhs_exprs,
    2403             :                                           nraw,
    2404             :                                           NULL,
    2405             :                                           NULL);
    2406        1020 :             if (rowcount > nunique)
    2407         346 :                 rowcount = nunique;
    2408             :         }
    2409             :     }
    2410      373080 :     return rowcount;
    2411             : }
    2412             : 
    2413             : /*
    2414             :  * Make an approximate estimate of the size of a joinrel.
    2415             :  *
    2416             :  * We don't have enough info at this point to get a good estimate, so we
    2417             :  * just multiply the base relation sizes together.  Fortunately, this is
    2418             :  * the right answer anyway for the most common case with a single relation
    2419             :  * on the RHS of a semijoin.  Also, estimate_num_groups() has only a weak
    2420             :  * dependency on its input_rows argument (it basically uses it as a clamp).
    2421             :  * So we might be able to get a fairly decent end result even with a severe
    2422             :  * overestimate of the RHS's raw size.
    2423             :  */
    2424             : static double
    2425        1020 : approximate_joinrel_size(PlannerInfo *root, Relids relids)
    2426             : {
    2427        1020 :     double      rowcount = 1.0;
    2428             :     int         relid;
    2429             : 
    2430        1020 :     relid = -1;
    2431        2196 :     while ((relid = bms_next_member(relids, relid)) >= 0)
    2432             :     {
    2433             :         RelOptInfo *rel;
    2434             : 
    2435             :         /* Paranoia: ignore bogus relid indexes */
    2436        1176 :         if (relid >= root->simple_rel_array_size)
    2437           0 :             continue;
    2438        1176 :         rel = root->simple_rel_array[relid];
    2439        1176 :         if (rel == NULL)
    2440           0 :             continue;
    2441             :         Assert(rel->relid == relid); /* sanity check on array */
    2442             : 
    2443             :         /* Relation could be proven empty, if so ignore */
    2444        1176 :         if (IS_DUMMY_REL(rel))
    2445           0 :             continue;
    2446             : 
    2447             :         /* Otherwise, rel's rows estimate should be valid by now */
    2448             :         Assert(rel->rows > 0);
    2449             : 
    2450             :         /* Accumulate product */
    2451        1176 :         rowcount *= rel->rows;
    2452             :     }
    2453        1020 :     return rowcount;
    2454             : }
    2455             : 
    2456             : 
    2457             : /****************************************************************************
    2458             :  *              ----  ROUTINES TO CHECK QUERY CLAUSES  ----
    2459             :  ****************************************************************************/
    2460             : 
    2461             : /*
    2462             :  * match_restriction_clauses_to_index
    2463             :  *    Identify restriction clauses for the rel that match the index.
    2464             :  *    Matching clauses are added to *clauseset.
    2465             :  */
    2466             : static void
    2467      665384 : match_restriction_clauses_to_index(PlannerInfo *root,
    2468             :                                    IndexOptInfo *index,
    2469             :                                    IndexClauseSet *clauseset)
    2470             : {
    2471             :     /* We can ignore clauses that are implied by the index predicate */
    2472      665384 :     match_clauses_to_index(root, index->indrestrictinfo, index, clauseset);
    2473      665384 : }
    2474             : 
    2475             : /*
    2476             :  * match_join_clauses_to_index
    2477             :  *    Identify join clauses for the rel that match the index.
    2478             :  *    Matching clauses are added to *clauseset.
    2479             :  *    Also, add any potentially usable join OR clauses to *joinorclauses.
    2480             :  *    They also might be processed by match_clause_to_index() as a whole.
    2481             :  */
    2482             : static void
    2483      665384 : match_join_clauses_to_index(PlannerInfo *root,
    2484             :                             RelOptInfo *rel, IndexOptInfo *index,
    2485             :                             IndexClauseSet *clauseset,
    2486             :                             List **joinorclauses)
    2487             : {
    2488             :     ListCell   *lc;
    2489             : 
    2490             :     /* Scan the rel's join clauses */
    2491      904414 :     foreach(lc, rel->joininfo)
    2492             :     {
    2493      239030 :         RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
    2494             : 
    2495             :         /* Check if clause can be moved to this rel */
    2496      239030 :         if (!join_clause_is_movable_to(rinfo, rel))
    2497      141798 :             continue;
    2498             : 
    2499             :         /*
    2500             :          * Potentially usable, so see if it matches the index or is an OR. Use
    2501             :          * list_append_unique_ptr() here to avoid possible duplicates when
    2502             :          * processing the same clauses with different indexes.
    2503             :          */
    2504       97232 :         if (restriction_is_or_clause(rinfo))
    2505       12696 :             *joinorclauses = list_append_unique_ptr(*joinorclauses, rinfo);
    2506             : 
    2507       97232 :         match_clause_to_index(root, rinfo, index, clauseset);
    2508             :     }
    2509      665384 : }
    2510             : 
    2511             : /*
    2512             :  * match_eclass_clauses_to_index
    2513             :  *    Identify EquivalenceClass join clauses for the rel that match the index.
    2514             :  *    Matching clauses are added to *clauseset.
    2515             :  */
    2516             : static void
    2517      665384 : match_eclass_clauses_to_index(PlannerInfo *root, IndexOptInfo *index,
    2518             :                               IndexClauseSet *clauseset)
    2519             : {
    2520             :     int         indexcol;
    2521             : 
    2522             :     /* No work if rel is not in any such ECs */
    2523      665384 :     if (!index->rel->has_eclass_joins)
    2524      390712 :         return;
    2525             : 
    2526      718778 :     for (indexcol = 0; indexcol < index->nkeycolumns; indexcol++)
    2527             :     {
    2528             :         ec_member_matches_arg arg;
    2529             :         List       *clauses;
    2530             : 
    2531             :         /* Generate clauses, skipping any that join to lateral_referencers */
    2532      444106 :         arg.index = index;
    2533      444106 :         arg.indexcol = indexcol;
    2534      444106 :         clauses = generate_implied_equalities_for_column(root,
    2535             :                                                          index->rel,
    2536             :                                                          ec_member_matches_indexcol,
    2537             :                                                          &arg,
    2538      444106 :                                                          index->rel->lateral_referencers);
    2539             : 
    2540             :         /*
    2541             :          * We have to check whether the results actually do match the index,
    2542             :          * since for non-btree indexes the EC's equality operators might not
    2543             :          * be in the index opclass (cf ec_member_matches_indexcol).
    2544             :          */
    2545      444106 :         match_clauses_to_index(root, clauses, index, clauseset);
    2546             :     }
    2547             : }
    2548             : 
    2549             : /*
    2550             :  * match_clauses_to_index
    2551             :  *    Perform match_clause_to_index() for each clause in a list.
    2552             :  *    Matching clauses are added to *clauseset.
    2553             :  */
    2554             : static void
    2555     1140334 : match_clauses_to_index(PlannerInfo *root,
    2556             :                        List *clauses,
    2557             :                        IndexOptInfo *index,
    2558             :                        IndexClauseSet *clauseset)
    2559             : {
    2560             :     ListCell   *lc;
    2561             : 
    2562     2053010 :     foreach(lc, clauses)
    2563             :     {
    2564      912676 :         RestrictInfo *rinfo = lfirst_node(RestrictInfo, lc);
    2565             : 
    2566      912676 :         match_clause_to_index(root, rinfo, index, clauseset);
    2567             :     }
    2568     1140334 : }
    2569             : 
    2570             : /*
    2571             :  * match_clause_to_index
    2572             :  *    Test whether a qual clause can be used with an index.
    2573             :  *
    2574             :  * If the clause is usable, add an IndexClause entry for it to the appropriate
    2575             :  * list in *clauseset.  (*clauseset must be initialized to zeroes before first
    2576             :  * call.)
    2577             :  *
    2578             :  * Note: in some circumstances we may find the same RestrictInfos coming from
    2579             :  * multiple places.  Defend against redundant outputs by refusing to add a
    2580             :  * clause twice (pointer equality should be a good enough check for this).
    2581             :  *
    2582             :  * Note: it's possible that a badly-defined index could have multiple matching
    2583             :  * columns.  We always select the first match if so; this avoids scenarios
    2584             :  * wherein we get an inflated idea of the index's selectivity by using the
    2585             :  * same clause multiple times with different index columns.
    2586             :  */
    2587             : static void
    2588     1009908 : match_clause_to_index(PlannerInfo *root,
    2589             :                       RestrictInfo *rinfo,
    2590             :                       IndexOptInfo *index,
    2591             :                       IndexClauseSet *clauseset)
    2592             : {
    2593             :     int         indexcol;
    2594             : 
    2595             :     /*
    2596             :      * Never match pseudoconstants to indexes.  (Normally a match could not
    2597             :      * happen anyway, since a pseudoconstant clause couldn't contain a Var,
    2598             :      * but what if someone builds an expression index on a constant? It's not
    2599             :      * totally unreasonable to do so with a partial index, either.)
    2600             :      */
    2601     1009908 :     if (rinfo->pseudoconstant)
    2602       12948 :         return;
    2603             : 
    2604             :     /*
    2605             :      * If clause can't be used as an indexqual because it must wait till after
    2606             :      * some lower-security-level restriction clause, reject it.
    2607             :      */
    2608      996960 :     if (!restriction_is_securely_promotable(rinfo, index->rel))
    2609         474 :         return;
    2610             : 
    2611             :     /* OK, check each index key column for a match */
    2612     2200258 :     for (indexcol = 0; indexcol < index->nkeycolumns; indexcol++)
    2613             :     {
    2614             :         IndexClause *iclause;
    2615             :         ListCell   *lc;
    2616             : 
    2617             :         /* Ignore duplicates */
    2618     1660506 :         foreach(lc, clauseset->indexclauses[indexcol])
    2619             :         {
    2620       73122 :             iclause = (IndexClause *) lfirst(lc);
    2621             : 
    2622       73122 :             if (iclause->rinfo == rinfo)
    2623           0 :                 return;
    2624             :         }
    2625             : 
    2626             :         /* OK, try to match the clause to the index column */
    2627     1587384 :         iclause = match_clause_to_indexcol(root,
    2628             :                                            rinfo,
    2629             :                                            indexcol,
    2630             :                                            index);
    2631     1587384 :         if (iclause)
    2632             :         {
    2633             :             /* Success, so record it */
    2634      383612 :             clauseset->indexclauses[indexcol] =
    2635      383612 :                 lappend(clauseset->indexclauses[indexcol], iclause);
    2636      383612 :             clauseset->nonempty = true;
    2637      383612 :             return;
    2638             :         }
    2639             :     }
    2640             : }
    2641             : 
    2642             : /*
    2643             :  * match_clause_to_indexcol()
    2644             :  *    Determine whether a restriction clause matches a column of an index,
    2645             :  *    and if so, build an IndexClause node describing the details.
    2646             :  *
    2647             :  *    To match an index normally, an operator clause:
    2648             :  *
    2649             :  *    (1)  must be in the form (indexkey op const) or (const op indexkey);
    2650             :  *         and
    2651             :  *    (2)  must contain an operator which is in the index's operator family
    2652             :  *         for this column; and
    2653             :  *    (3)  must match the collation of the index, if collation is relevant.
    2654             :  *
    2655             :  *    Our definition of "const" is exceedingly liberal: we allow anything that
    2656             :  *    doesn't involve a volatile function or a Var of the index's relation.
    2657             :  *    In particular, Vars belonging to other relations of the query are
    2658             :  *    accepted here, since a clause of that form can be used in a
    2659             :  *    parameterized indexscan.  It's the responsibility of higher code levels
    2660             :  *    to manage restriction and join clauses appropriately.
    2661             :  *
    2662             :  *    Note: we do need to check for Vars of the index's relation on the
    2663             :  *    "const" side of the clause, since clauses like (a.f1 OP (b.f2 OP a.f3))
    2664             :  *    are not processable by a parameterized indexscan on a.f1, whereas
    2665             :  *    something like (a.f1 OP (b.f2 OP c.f3)) is.
    2666             :  *
    2667             :  *    Presently, the executor can only deal with indexquals that have the
    2668             :  *    indexkey on the left, so we can only use clauses that have the indexkey
    2669             :  *    on the right if we can commute the clause to put the key on the left.
    2670             :  *    We handle that by generating an IndexClause with the correctly-commuted
    2671             :  *    opclause as a derived indexqual.
    2672             :  *
    2673             :  *    If the index has a collation, the clause must have the same collation.
    2674             :  *    For collation-less indexes, we assume it doesn't matter; this is
    2675             :  *    necessary for cases like "hstore ? text", wherein hstore's operators
    2676             :  *    don't care about collation but the clause will get marked with a
    2677             :  *    collation anyway because of the text argument.  (This logic is
    2678             :  *    embodied in the macro IndexCollMatchesExprColl.)
    2679             :  *
    2680             :  *    It is also possible to match RowCompareExpr clauses to indexes (but
    2681             :  *    currently, only btree indexes handle this).
    2682             :  *
    2683             :  *    It is also possible to match ScalarArrayOpExpr clauses to indexes, when
    2684             :  *    the clause is of the form "indexkey op ANY (arrayconst)".
    2685             :  *
    2686             :  *    It is also possible to match a list of OR clauses if it might be
    2687             :  *    transformed into a single ScalarArrayOpExpr clause.  On success,
    2688             :  *    the returning index clause will contain a transformed clause.
    2689             :  *
    2690             :  *    For boolean indexes, it is also possible to match the clause directly
    2691             :  *    to the indexkey; or perhaps the clause is (NOT indexkey).
    2692             :  *
    2693             :  *    And, last but not least, some operators and functions can be processed
    2694             :  *    to derive (typically lossy) indexquals from a clause that isn't in
    2695             :  *    itself indexable.  If we see that any operand of an OpExpr or FuncExpr
    2696             :  *    matches the index key, and the function has a planner support function
    2697             :  *    attached to it, we'll invoke the support function to see if such an
    2698             :  *    indexqual can be built.
    2699             :  *
    2700             :  * 'rinfo' is the clause to be tested (as a RestrictInfo node).
    2701             :  * 'indexcol' is a column number of 'index' (counting from 0).
    2702             :  * 'index' is the index of interest.
    2703             :  *
    2704             :  * Returns an IndexClause if the clause can be used with this index key,
    2705             :  * or NULL if not.
    2706             :  *
    2707             :  * NOTE:  This routine always returns NULL if the clause is an AND clause.
    2708             :  * Higher-level routines deal with OR and AND clauses. OR clause can be
    2709             :  * matched as a whole by match_orclause_to_indexcol() though.
    2710             :  */
    2711             : static IndexClause *
    2712     1587384 : match_clause_to_indexcol(PlannerInfo *root,
    2713             :                          RestrictInfo *rinfo,
    2714             :                          int indexcol,
    2715             :                          IndexOptInfo *index)
    2716             : {
    2717             :     IndexClause *iclause;
    2718     1587384 :     Expr       *clause = rinfo->clause;
    2719             :     Oid         opfamily;
    2720             : 
    2721             :     Assert(indexcol < index->nkeycolumns);
    2722             : 
    2723             :     /*
    2724             :      * Historically this code has coped with NULL clauses.  That's probably
    2725             :      * not possible anymore, but we might as well continue to cope.
    2726             :      */
    2727     1587384 :     if (clause == NULL)
    2728           0 :         return NULL;
    2729             : 
    2730             :     /* First check for boolean-index cases. */
    2731     1587384 :     opfamily = index->opfamily[indexcol];
    2732     1587384 :     if (IsBooleanOpfamily(opfamily))
    2733             :     {
    2734         446 :         iclause = match_boolean_index_clause(root, rinfo, indexcol, index);
    2735         446 :         if (iclause)
    2736         294 :             return iclause;
    2737             :     }
    2738             : 
    2739             :     /*
    2740             :      * Clause must be an opclause, funcclause, ScalarArrayOpExpr,
    2741             :      * RowCompareExpr, or OR-clause that could be converted to SAOP.  Or, if
    2742             :      * the index supports it, we can handle IS NULL/NOT NULL clauses.
    2743             :      */
    2744     1587090 :     if (IsA(clause, OpExpr))
    2745             :     {
    2746     1303986 :         return match_opclause_to_indexcol(root, rinfo, indexcol, index);
    2747             :     }
    2748      283104 :     else if (IsA(clause, FuncExpr))
    2749             :     {
    2750       35724 :         return match_funcclause_to_indexcol(root, rinfo, indexcol, index);
    2751             :     }
    2752      247380 :     else if (IsA(clause, ScalarArrayOpExpr))
    2753             :     {
    2754       74142 :         return match_saopclause_to_indexcol(root, rinfo, indexcol, index);
    2755             :     }
    2756      173238 :     else if (IsA(clause, RowCompareExpr))
    2757             :     {
    2758         432 :         return match_rowcompare_to_indexcol(root, rinfo, indexcol, index);
    2759             :     }
    2760      172806 :     else if (restriction_is_or_clause(rinfo))
    2761             :     {
    2762       45584 :         return match_orclause_to_indexcol(root, rinfo, indexcol, index);
    2763             :     }
    2764      127222 :     else if (index->amsearchnulls && IsA(clause, NullTest))
    2765             :     {
    2766       14874 :         NullTest   *nt = (NullTest *) clause;
    2767             : 
    2768       29748 :         if (!nt->argisrow &&
    2769       14874 :             match_index_to_operand((Node *) nt->arg, indexcol, index))
    2770             :         {
    2771        1442 :             iclause = makeNode(IndexClause);
    2772        1442 :             iclause->rinfo = rinfo;
    2773        1442 :             iclause->indexquals = list_make1(rinfo);
    2774        1442 :             iclause->lossy = false;
    2775        1442 :             iclause->indexcol = indexcol;
    2776        1442 :             iclause->indexcols = NIL;
    2777        1442 :             return iclause;
    2778             :         }
    2779             :     }
    2780             : 
    2781      125780 :     return NULL;
    2782             : }
    2783             : 
    2784             : /*
    2785             :  * IsBooleanOpfamily
    2786             :  *    Detect whether an opfamily supports boolean equality as an operator.
    2787             :  *
    2788             :  * If the opfamily OID is in the range of built-in objects, we can rely
    2789             :  * on hard-wired knowledge of which built-in opfamilies support this.
    2790             :  * For extension opfamilies, there's no choice but to do a catcache lookup.
    2791             :  */
    2792             : static bool
    2793     2160972 : IsBooleanOpfamily(Oid opfamily)
    2794             : {
    2795     2160972 :     if (opfamily < FirstNormalObjectId)
    2796     2157766 :         return IsBuiltinBooleanOpfamily(opfamily);
    2797             :     else
    2798        3206 :         return op_in_opfamily(BooleanEqualOperator, opfamily);
    2799             : }
    2800             : 
    2801             : /*
    2802             :  * match_boolean_index_clause
    2803             :  *    Recognize restriction clauses that can be matched to a boolean index.
    2804             :  *
    2805             :  * The idea here is that, for an index on a boolean column that supports the
    2806             :  * BooleanEqualOperator, we can transform a plain reference to the indexkey
    2807             :  * into "indexkey = true", or "NOT indexkey" into "indexkey = false", etc,
    2808             :  * so as to make the expression indexable using the index's "=" operator.
    2809             :  * Since Postgres 8.1, we must do this because constant simplification does
    2810             :  * the reverse transformation; without this code there'd be no way to use
    2811             :  * such an index at all.
    2812             :  *
    2813             :  * This should be called only when IsBooleanOpfamily() recognizes the
    2814             :  * index's operator family.  We check to see if the clause matches the
    2815             :  * index's key, and if so, build a suitable IndexClause.
    2816             :  */
    2817             : static IndexClause *
    2818        1706 : match_boolean_index_clause(PlannerInfo *root,
    2819             :                            RestrictInfo *rinfo,
    2820             :                            int indexcol,
    2821             :                            IndexOptInfo *index)
    2822             : {
    2823        1706 :     Node       *clause = (Node *) rinfo->clause;
    2824        1706 :     Expr       *op = NULL;
    2825             : 
    2826             :     /* Direct match? */
    2827        1706 :     if (match_index_to_operand(clause, indexcol, index))
    2828             :     {
    2829             :         /* convert to indexkey = TRUE */
    2830         274 :         op = make_opclause(BooleanEqualOperator, BOOLOID, false,
    2831             :                            (Expr *) clause,
    2832         274 :                            (Expr *) makeBoolConst(true, false),
    2833             :                            InvalidOid, InvalidOid);
    2834             :     }
    2835             :     /* NOT clause? */
    2836        1432 :     else if (is_notclause(clause))
    2837             :     {
    2838        1208 :         Node       *arg = (Node *) get_notclausearg((Expr *) clause);
    2839             : 
    2840        1208 :         if (match_index_to_operand(arg, indexcol, index))
    2841             :         {
    2842             :             /* convert to indexkey = FALSE */
    2843        1208 :             op = make_opclause(BooleanEqualOperator, BOOLOID, false,
    2844             :                                (Expr *) arg,
    2845        1208 :                                (Expr *) makeBoolConst(false, false),
    2846             :                                InvalidOid, InvalidOid);
    2847             :         }
    2848             :     }
    2849             : 
    2850             :     /*
    2851             :      * Since we only consider clauses at top level of WHERE, we can convert
    2852             :      * indexkey IS TRUE and indexkey IS FALSE to index searches as well.  The
    2853             :      * different meaning for NULL isn't important.
    2854             :      */
    2855         224 :     else if (clause && IsA(clause, BooleanTest))
    2856             :     {
    2857          36 :         BooleanTest *btest = (BooleanTest *) clause;
    2858          36 :         Node       *arg = (Node *) btest->arg;
    2859             : 
    2860          54 :         if (btest->booltesttype == IS_TRUE &&
    2861          18 :             match_index_to_operand(arg, indexcol, index))
    2862             :         {
    2863             :             /* convert to indexkey = TRUE */
    2864          18 :             op = make_opclause(BooleanEqualOperator, BOOLOID, false,
    2865             :                                (Expr *) arg,
    2866          18 :                                (Expr *) makeBoolConst(true, false),
    2867             :                                InvalidOid, InvalidOid);
    2868             :         }
    2869          36 :         else if (btest->booltesttype == IS_FALSE &&
    2870          18 :                  match_index_to_operand(arg, indexcol, index))
    2871             :         {
    2872             :             /* convert to indexkey = FALSE */
    2873          18 :             op = make_opclause(BooleanEqualOperator, BOOLOID, false,
    2874             :                                (Expr *) arg,
    2875          18 :                                (Expr *) makeBoolConst(false, false),
    2876             :                                InvalidOid, InvalidOid);
    2877             :         }
    2878             :     }
    2879             : 
    2880             :     /*
    2881             :      * If we successfully made an operator clause from the given qual, we must
    2882             :      * wrap it in an IndexClause.  It's not lossy.
    2883             :      */
    2884        1706 :     if (op)
    2885             :     {
    2886        1518 :         IndexClause *iclause = makeNode(IndexClause);
    2887             : 
    2888        1518 :         iclause->rinfo = rinfo;
    2889        1518 :         iclause->indexquals = list_make1(make_simple_restrictinfo(root, op));
    2890        1518 :         iclause->lossy = false;
    2891        1518 :         iclause->indexcol = indexcol;
    2892        1518 :         iclause->indexcols = NIL;
    2893        1518 :         return iclause;
    2894             :     }
    2895             : 
    2896         188 :     return NULL;
    2897             : }
    2898             : 
    2899             : /*
    2900             :  * match_opclause_to_indexcol()
    2901             :  *    Handles the OpExpr case for match_clause_to_indexcol(),
    2902             :  *    which see for comments.
    2903             :  */
    2904             : static IndexClause *
    2905     1303986 : match_opclause_to_indexcol(PlannerInfo *root,
    2906             :                            RestrictInfo *rinfo,
    2907             :                            int indexcol,
    2908             :                            IndexOptInfo *index)
    2909             : {
    2910             :     IndexClause *iclause;
    2911     1303986 :     OpExpr     *clause = (OpExpr *) rinfo->clause;
    2912             :     Node       *leftop,
    2913             :                *rightop;
    2914             :     Oid         expr_op;
    2915             :     Oid         expr_coll;
    2916             :     Index       index_relid;
    2917             :     Oid         opfamily;
    2918             :     Oid         idxcollation;
    2919             : 
    2920             :     /*
    2921             :      * Only binary operators need apply.  (In theory, a planner support
    2922             :      * function could do something with a unary operator, but it seems
    2923             :      * unlikely to be worth the cycles to check.)
    2924             :      */
    2925     1303986 :     if (list_length(clause->args) != 2)
    2926           0 :         return NULL;
    2927             : 
    2928     1303986 :     leftop = (Node *) linitial(clause->args);
    2929     1303986 :     rightop = (Node *) lsecond(clause->args);
    2930     1303986 :     expr_op = clause->opno;
    2931     1303986 :     expr_coll = clause->inputcollid;
    2932             : 
    2933     1303986 :     index_relid = index->rel->relid;
    2934     1303986 :     opfamily = index->opfamily[indexcol];
    2935     1303986 :     idxcollation = index->indexcollations[indexcol];
    2936             : 
    2937             :     /*
    2938             :      * Check for clauses of the form: (indexkey operator constant) or
    2939             :      * (constant operator indexkey).  See match_clause_to_indexcol's notes
    2940             :      * about const-ness.
    2941             :      *
    2942             :      * Note that we don't ask the support function about clauses that don't
    2943             :      * have one of these forms.  Again, in principle it might be possible to
    2944             :      * do something, but it seems unlikely to be worth the cycles to check.
    2945             :      */
    2946     1303986 :     if (match_index_to_operand(leftop, indexcol, index) &&
    2947      323804 :         !bms_is_member(index_relid, rinfo->right_relids) &&
    2948      323630 :         !contain_volatile_functions(rightop))
    2949             :     {
    2950      640712 :         if (IndexCollMatchesExprColl(idxcollation, expr_coll) &&
    2951      317082 :             op_in_opfamily(expr_op, opfamily))
    2952             :         {
    2953      310218 :             iclause = makeNode(IndexClause);
    2954      310218 :             iclause->rinfo = rinfo;
    2955      310218 :             iclause->indexquals = list_make1(rinfo);
    2956      310218 :             iclause->lossy = false;
    2957      310218 :             iclause->indexcol = indexcol;
    2958      310218 :             iclause->indexcols = NIL;
    2959      310218 :             return iclause;
    2960             :         }
    2961             : 
    2962             :         /*
    2963             :          * If we didn't find a member of the index's opfamily, try the support
    2964             :          * function for the operator's underlying function.
    2965             :          */
    2966       13412 :         set_opfuncid(clause);   /* make sure we have opfuncid */
    2967       13412 :         return get_index_clause_from_support(root,
    2968             :                                              rinfo,
    2969             :                                              clause->opfuncid,
    2970             :                                              0, /* indexarg on left */
    2971             :                                              indexcol,
    2972             :                                              index);
    2973             :     }
    2974             : 
    2975      980356 :     if (match_index_to_operand(rightop, indexcol, index) &&
    2976       63442 :         !bms_is_member(index_relid, rinfo->left_relids) &&
    2977       63316 :         !contain_volatile_functions(leftop))
    2978             :     {
    2979       63316 :         if (IndexCollMatchesExprColl(idxcollation, expr_coll))
    2980             :         {
    2981       63304 :             Oid         comm_op = get_commutator(expr_op);
    2982             : 
    2983      126608 :             if (OidIsValid(comm_op) &&
    2984       63304 :                 op_in_opfamily(comm_op, opfamily))
    2985             :             {
    2986             :                 RestrictInfo *commrinfo;
    2987             : 
    2988             :                 /* Build a commuted OpExpr and RestrictInfo */
    2989       62838 :                 commrinfo = commute_restrictinfo(rinfo, comm_op);
    2990             : 
    2991             :                 /* Make an IndexClause showing that as a derived qual */
    2992       62838 :                 iclause = makeNode(IndexClause);
    2993       62838 :                 iclause->rinfo = rinfo;
    2994       62838 :                 iclause->indexquals = list_make1(commrinfo);
    2995       62838 :                 iclause->lossy = false;
    2996       62838 :                 iclause->indexcol = indexcol;
    2997       62838 :                 iclause->indexcols = NIL;
    2998       62838 :                 return iclause;
    2999             :             }
    3000             :         }
    3001             : 
    3002             :         /*
    3003             :          * If we didn't find a member of the index's opfamily, try the support
    3004             :          * function for the operator's underlying function.
    3005             :          */
    3006         478 :         set_opfuncid(clause);   /* make sure we have opfuncid */
    3007         478 :         return get_index_clause_from_support(root,
    3008             :                                              rinfo,
    3009             :                                              clause->opfuncid,
    3010             :                                              1, /* indexarg on right */
    3011             :                                              indexcol,
    3012             :                                              index);
    3013             :     }
    3014             : 
    3015      917040 :     return NULL;
    3016             : }
    3017             : 
    3018             : /*
    3019             :  * match_funcclause_to_indexcol()
    3020             :  *    Handles the FuncExpr case for match_clause_to_indexcol(),
    3021             :  *    which see for comments.
    3022             :  */
    3023             : static IndexClause *
    3024       35724 : match_funcclause_to_indexcol(PlannerInfo *root,
    3025             :                              RestrictInfo *rinfo,
    3026             :                              int indexcol,
    3027             :                              IndexOptInfo *index)
    3028             : {
    3029       35724 :     FuncExpr   *clause = (FuncExpr *) rinfo->clause;
    3030             :     int         indexarg;
    3031             :     ListCell   *lc;
    3032             : 
    3033             :     /*
    3034             :      * We have no built-in intelligence about function clauses, but if there's
    3035             :      * a planner support function, it might be able to do something.  But, to
    3036             :      * cut down on wasted planning cycles, only call the support function if
    3037             :      * at least one argument matches the target index column.
    3038             :      *
    3039             :      * Note that we don't insist on the other arguments being pseudoconstants;
    3040             :      * the support function has to check that.  This is to allow cases where
    3041             :      * only some of the other arguments need to be included in the indexqual.
    3042             :      */
    3043       35724 :     indexarg = 0;
    3044       85664 :     foreach(lc, clause->args)
    3045             :     {
    3046       57088 :         Node       *op = (Node *) lfirst(lc);
    3047             : 
    3048       57088 :         if (match_index_to_operand(op, indexcol, index))
    3049             :         {
    3050        7148 :             return get_index_clause_from_support(root,
    3051             :                                                  rinfo,
    3052             :                                                  clause->funcid,
    3053             :                                                  indexarg,
    3054             :                                                  indexcol,
    3055             :                                                  index);
    3056             :         }
    3057             : 
    3058       49940 :         indexarg++;
    3059             :     }
    3060             : 
    3061       28576 :     return NULL;
    3062             : }
    3063             : 
    3064             : /*
    3065             :  * get_index_clause_from_support()
    3066             :  *      If the function has a planner support function, try to construct
    3067             :  *      an IndexClause using indexquals created by the support function.
    3068             :  */
    3069             : static IndexClause *
    3070       21038 : get_index_clause_from_support(PlannerInfo *root,
    3071             :                               RestrictInfo *rinfo,
    3072             :                               Oid funcid,
    3073             :                               int indexarg,
    3074             :                               int indexcol,
    3075             :                               IndexOptInfo *index)
    3076             : {
    3077       21038 :     Oid         prosupport = get_func_support(funcid);
    3078             :     SupportRequestIndexCondition req;
    3079             :     List       *sresult;
    3080             : 
    3081       21038 :     if (!OidIsValid(prosupport))
    3082       13016 :         return NULL;
    3083             : 
    3084        8022 :     req.type = T_SupportRequestIndexCondition;
    3085        8022 :     req.root = root;
    3086        8022 :     req.funcid = funcid;
    3087        8022 :     req.node = (Node *) rinfo->clause;
    3088        8022 :     req.indexarg = indexarg;
    3089        8022 :     req.index = index;
    3090        8022 :     req.indexcol = indexcol;
    3091        8022 :     req.opfamily = index->opfamily[indexcol];
    3092        8022 :     req.indexcollation = index->indexcollations[indexcol];
    3093             : 
    3094        8022 :     req.lossy = true;           /* default assumption */
    3095             : 
    3096             :     sresult = (List *)
    3097        8022 :         DatumGetPointer(OidFunctionCall1(prosupport,
    3098             :                                          PointerGetDatum(&req)));
    3099             : 
    3100        8022 :     if (sresult != NIL)
    3101             :     {
    3102        1390 :         IndexClause *iclause = makeNode(IndexClause);
    3103        1390 :         List       *indexquals = NIL;
    3104             :         ListCell   *lc;
    3105             : 
    3106             :         /*
    3107             :          * The support function API says it should just give back bare
    3108             :          * clauses, so here we must wrap each one in a RestrictInfo.
    3109             :          */
    3110        4092 :         foreach(lc, sresult)
    3111             :         {
    3112        2702 :             Expr       *clause = (Expr *) lfirst(lc);
    3113             : 
    3114        2702 :             indexquals = lappend(indexquals,
    3115        2702 :                                  make_simple_restrictinfo(root, clause));
    3116             :         }
    3117             : 
    3118        1390 :         iclause->rinfo = rinfo;
    3119        1390 :         iclause->indexquals = indexquals;
    3120        1390 :         iclause->lossy = req.lossy;
    3121        1390 :         iclause->indexcol = indexcol;
    3122        1390 :         iclause->indexcols = NIL;
    3123             : 
    3124        1390 :         return iclause;
    3125             :     }
    3126             : 
    3127        6632 :     return NULL;
    3128             : }
    3129             : 
    3130             : /*
    3131             :  * match_saopclause_to_indexcol()
    3132             :  *    Handles the ScalarArrayOpExpr case for match_clause_to_indexcol(),
    3133             :  *    which see for comments.
    3134             :  */
    3135             : static IndexClause *
    3136       74142 : match_saopclause_to_indexcol(PlannerInfo *root,
    3137             :                              RestrictInfo *rinfo,
    3138             :                              int indexcol,
    3139             :                              IndexOptInfo *index)
    3140             : {
    3141       74142 :     ScalarArrayOpExpr *saop = (ScalarArrayOpExpr *) rinfo->clause;
    3142             :     Node       *leftop,
    3143             :                *rightop;
    3144             :     Relids      right_relids;
    3145             :     Oid         expr_op;
    3146             :     Oid         expr_coll;
    3147             :     Index       index_relid;
    3148             :     Oid         opfamily;
    3149             :     Oid         idxcollation;
    3150             : 
    3151             :     /* We only accept ANY clauses, not ALL */
    3152       74142 :     if (!saop->useOr)
    3153       13852 :         return NULL;
    3154       60290 :     leftop = (Node *) linitial(saop->args);
    3155       60290 :     rightop = (Node *) lsecond(saop->args);
    3156       60290 :     right_relids = pull_varnos(root, rightop);
    3157       60290 :     expr_op = saop->opno;
    3158       60290 :     expr_coll = saop->inputcollid;
    3159             : 
    3160       60290 :     index_relid = index->rel->relid;
    3161       60290 :     opfamily = index->opfamily[indexcol];
    3162       60290 :     idxcollation = index->indexcollations[indexcol];
    3163             : 
    3164             :     /*
    3165             :      * We must have indexkey on the left and a pseudo-constant array argument.
    3166             :      */
    3167       60290 :     if (match_index_to_operand(leftop, indexcol, index) &&
    3168        6248 :         !bms_is_member(index_relid, right_relids) &&
    3169        6248 :         !contain_volatile_functions(rightop))
    3170             :     {
    3171       12490 :         if (IndexCollMatchesExprColl(idxcollation, expr_coll) &&
    3172        6242 :             op_in_opfamily(expr_op, opfamily))
    3173             :         {
    3174        6230 :             IndexClause *iclause = makeNode(IndexClause);
    3175             : 
    3176        6230 :             iclause->rinfo = rinfo;
    3177        6230 :             iclause->indexquals = list_make1(rinfo);
    3178        6230 :             iclause->lossy = false;
    3179        6230 :             iclause->indexcol = indexcol;
    3180        6230 :             iclause->indexcols = NIL;
    3181        6230 :             return iclause;
    3182             :         }
    3183             : 
    3184             :         /*
    3185             :          * We do not currently ask support functions about ScalarArrayOpExprs,
    3186             :          * though in principle we could.
    3187             :          */
    3188             :     }
    3189             : 
    3190       54060 :     return NULL;
    3191             : }
    3192             : 
    3193             : /*
    3194             :  * match_rowcompare_to_indexcol()
    3195             :  *    Handles the RowCompareExpr case for match_clause_to_indexcol(),
    3196             :  *    which see for comments.
    3197             :  *
    3198             :  * In this routine we check whether the first column of the row comparison
    3199             :  * matches the target index column.  This is sufficient to guarantee that some
    3200             :  * index condition can be constructed from the RowCompareExpr --- the rest
    3201             :  * is handled by expand_indexqual_rowcompare().
    3202             :  */
    3203             : static IndexClause *
    3204         432 : match_rowcompare_to_indexcol(PlannerInfo *root,
    3205             :                              RestrictInfo *rinfo,
    3206             :                              int indexcol,
    3207             :                              IndexOptInfo *index)
    3208             : {
    3209         432 :     RowCompareExpr *clause = (RowCompareExpr *) rinfo->clause;
    3210             :     Index       index_relid;
    3211             :     Oid         opfamily;
    3212             :     Oid         idxcollation;
    3213             :     Node       *leftop,
    3214             :                *rightop;
    3215             :     bool        var_on_left;
    3216             :     Oid         expr_op;
    3217             :     Oid         expr_coll;
    3218             : 
    3219             :     /* Forget it if we're not dealing with a btree index */
    3220         432 :     if (index->relam != BTREE_AM_OID)
    3221           0 :         return NULL;
    3222             : 
    3223         432 :     index_relid = index->rel->relid;
    3224         432 :     opfamily = index->opfamily[indexcol];
    3225         432 :     idxcollation = index->indexcollations[indexcol];
    3226             : 
    3227             :     /*
    3228             :      * We could do the matching on the basis of insisting that the opfamily
    3229             :      * shown in the RowCompareExpr be the same as the index column's opfamily,
    3230             :      * but that could fail in the presence of reverse-sort opfamilies: it'd be
    3231             :      * a matter of chance whether RowCompareExpr had picked the forward or
    3232             :      * reverse-sort family.  So look only at the operator, and match if it is
    3233             :      * a member of the index's opfamily (after commutation, if the indexkey is
    3234             :      * on the right).  We'll worry later about whether any additional
    3235             :      * operators are matchable to the index.
    3236             :      */
    3237         432 :     leftop = (Node *) linitial(clause->largs);
    3238         432 :     rightop = (Node *) linitial(clause->rargs);
    3239         432 :     expr_op = linitial_oid(clause->opnos);
    3240         432 :     expr_coll = linitial_oid(clause->inputcollids);
    3241             : 
    3242             :     /* Collations must match, if relevant */
    3243         432 :     if (!IndexCollMatchesExprColl(idxcollation, expr_coll))
    3244           0 :         return NULL;
    3245             : 
    3246             :     /*
    3247             :      * These syntactic tests are the same as in match_opclause_to_indexcol()
    3248             :      */
    3249         432 :     if (match_index_to_operand(leftop, indexcol, index) &&
    3250         126 :         !bms_is_member(index_relid, pull_varnos(root, rightop)) &&
    3251         126 :         !contain_volatile_functions(rightop))
    3252             :     {
    3253             :         /* OK, indexkey is on left */
    3254         126 :         var_on_left = true;
    3255             :     }
    3256         306 :     else if (match_index_to_operand(rightop, indexcol, index) &&
    3257          24 :              !bms_is_member(index_relid, pull_varnos(root, leftop)) &&
    3258          24 :              !contain_volatile_functions(leftop))
    3259             :     {
    3260             :         /* indexkey is on right, so commute the operator */
    3261          24 :         expr_op = get_commutator(expr_op);
    3262          24 :         if (expr_op == InvalidOid)
    3263           0 :             return NULL;
    3264          24 :         var_on_left = false;
    3265             :     }
    3266             :     else
    3267         282 :         return NULL;
    3268             : 
    3269             :     /* We're good if the operator is the right type of opfamily member */
    3270         150 :     switch (get_op_opfamily_strategy(expr_op, opfamily))
    3271             :     {
    3272         150 :         case BTLessStrategyNumber:
    3273             :         case BTLessEqualStrategyNumber:
    3274             :         case BTGreaterEqualStrategyNumber:
    3275             :         case BTGreaterStrategyNumber:
    3276         150 :             return expand_indexqual_rowcompare(root,
    3277             :                                                rinfo,
    3278             :                                                indexcol,
    3279             :                                                index,
    3280             :                                                expr_op,
    3281             :                                                var_on_left);
    3282             :     }
    3283             : 
    3284           0 :     return NULL;
    3285             : }
    3286             : 
    3287             : /*
    3288             :  * match_orclause_to_indexcol()
    3289             :  *    Handles the OR-expr case for match_clause_to_indexcol() in the case
    3290             :  *    when it could be transformed to ScalarArrayOpExpr.
    3291             :  *
    3292             :  * In this routine, we attempt to transform a list of OR-clause args into a
    3293             :  * single SAOP expression matching the target index column.  On success,
    3294             :  * return an IndexClause, containing the transformed expression or NULL,
    3295             :  * if failed.
    3296             :  */
    3297             : static IndexClause *
    3298       45584 : match_orclause_to_indexcol(PlannerInfo *root,
    3299             :                            RestrictInfo *rinfo,
    3300             :                            int indexcol,
    3301             :                            IndexOptInfo *index)
    3302             : {
    3303             :     ListCell   *lc;
    3304       45584 :     BoolExpr   *orclause = (BoolExpr *) rinfo->orclause;
    3305       45584 :     Node       *indexExpr = NULL;
    3306       45584 :     List       *consts = NIL;
    3307       45584 :     Node       *arrayNode = NULL;
    3308       45584 :     ScalarArrayOpExpr *saopexpr = NULL;
    3309       45584 :     Oid         matchOpno = InvalidOid;
    3310             :     IndexClause *iclause;
    3311       45584 :     Oid         consttype = InvalidOid;
    3312       45584 :     Oid         arraytype = InvalidOid;
    3313       45584 :     Oid         inputcollid = InvalidOid;
    3314       45584 :     bool        firstTime = true;
    3315       45584 :     bool        haveNonConst = false;
    3316       45584 :     Index       indexRelid = index->rel->relid;
    3317             : 
    3318             :     Assert(IsA(orclause, BoolExpr));
    3319             :     Assert(orclause->boolop == OR_EXPR);
    3320             : 
    3321             :     /* Ignore index if it doesn't support SAOP clauses */
    3322       45584 :     if (!index->amsearcharray)
    3323         106 :         return NULL;
    3324             : 
    3325             :     /*
    3326             :      * Try to convert a list of OR-clauses to a single SAOP expression. Each
    3327             :      * OR entry must be in the form: (indexkey operator constant) or (constant
    3328             :      * operator indexkey).  Operators of all the entries must match.  To be
    3329             :      * effective, give up on the first non-matching entry.  Exit is
    3330             :      * implemented as a break from the loop, which is catched afterwards.
    3331             :      */
    3332       49858 :     foreach(lc, orclause->args)
    3333             :     {
    3334             :         RestrictInfo *subRinfo;
    3335             :         OpExpr     *subClause;
    3336             :         Oid         opno;
    3337             :         Node       *leftop,
    3338             :                    *rightop;
    3339             :         Node       *constExpr;
    3340             : 
    3341       48808 :         if (!IsA(lfirst(lc), RestrictInfo))
    3342        4742 :             break;
    3343             : 
    3344       44066 :         subRinfo = (RestrictInfo *) lfirst(lc);
    3345             : 
    3346             :         /* Only operator clauses can match  */
    3347       44066 :         if (!IsA(subRinfo->clause, OpExpr))
    3348       15786 :             break;
    3349             : 
    3350       28280 :         subClause = (OpExpr *) subRinfo->clause;
    3351       28280 :         opno = subClause->opno;
    3352             : 
    3353             :         /* Only binary operators can match  */
    3354       28280 :         if (list_length(subClause->args) != 2)
    3355           0 :             break;
    3356             : 
    3357             :         /*
    3358             :          * The parameters below must match between sub-rinfo and its parent as
    3359             :          * make_restrictinfo() fills them with the same values, and further
    3360             :          * modifications are also the same for the whole subtree.  However,
    3361             :          * still make a sanity check.
    3362             :          */
    3363             :         Assert(subRinfo->is_pushed_down == rinfo->is_pushed_down);
    3364             :         Assert(subRinfo->is_clone == rinfo->is_clone);
    3365             :         Assert(subRinfo->security_level == rinfo->security_level);
    3366             :         Assert(bms_equal(subRinfo->incompatible_relids, rinfo->incompatible_relids));
    3367             :         Assert(bms_equal(subRinfo->outer_relids, rinfo->outer_relids));
    3368             : 
    3369             :         /*
    3370             :          * Also, check that required_relids in sub-rinfo is subset of parent's
    3371             :          * required_relids.
    3372             :          */
    3373             :         Assert(bms_is_subset(subRinfo->required_relids, rinfo->required_relids));
    3374             : 
    3375             :         /* Only the operator returning a boolean suit the transformation. */
    3376       28280 :         if (get_op_rettype(opno) != BOOLOID)
    3377           0 :             break;
    3378             : 
    3379             :         /*
    3380             :          * Check for clauses of the form: (indexkey operator constant) or
    3381             :          * (constant operator indexkey).  See match_clause_to_indexcol's notes
    3382             :          * about const-ness.
    3383             :          */
    3384       28280 :         leftop = (Node *) linitial(subClause->args);
    3385       28280 :         rightop = (Node *) lsecond(subClause->args);
    3386       28280 :         if (match_index_to_operand(leftop, indexcol, index) &&
    3387        6140 :             !bms_is_member(indexRelid, subRinfo->right_relids) &&
    3388        6110 :             !contain_volatile_functions(rightop))
    3389             :         {
    3390        6110 :             indexExpr = leftop;
    3391        6110 :             constExpr = rightop;
    3392             :         }
    3393       22170 :         else if (match_index_to_operand(rightop, indexcol, index) &&
    3394         170 :                  !bms_is_member(indexRelid, subRinfo->left_relids) &&
    3395         164 :                  !contain_volatile_functions(leftop))
    3396             :         {
    3397         164 :             opno = get_commutator(opno);
    3398         164 :             if (!OidIsValid(opno))
    3399             :             {
    3400             :                 /* commutator doesn't exist, we can't reverse the order */
    3401           0 :                 break;
    3402             :             }
    3403         164 :             indexExpr = rightop;
    3404         164 :             constExpr = leftop;
    3405             :         }
    3406             :         else
    3407             :         {
    3408             :             break;
    3409             :         }
    3410             : 
    3411             :         /*
    3412             :          * Ignore any RelabelType node above the operands.  This is needed to
    3413             :          * be able to apply indexscanning in binary-compatible-operator cases.
    3414             :          * Note: we can assume there is at most one RelabelType node;
    3415             :          * eval_const_expressions() will have simplified if more than one.
    3416             :          */
    3417        6274 :         if (IsA(constExpr, RelabelType))
    3418           0 :             constExpr = (Node *) ((RelabelType *) constExpr)->arg;
    3419        6274 :         if (IsA(indexExpr, RelabelType))
    3420          12 :             indexExpr = (Node *) ((RelabelType *) indexExpr)->arg;
    3421             : 
    3422             :         /* Forbid transformation for composite types, records. */
    3423       12548 :         if (type_is_rowtype(exprType(constExpr)) ||
    3424        6274 :             type_is_rowtype(exprType(indexExpr)))
    3425             :             break;
    3426             : 
    3427             :         /*
    3428             :          * Save information about the operator, type, and collation for the
    3429             :          * first matching qual.  Then, check that subsequent quals match the
    3430             :          * first.
    3431             :          */
    3432        6274 :         if (firstTime)
    3433             :         {
    3434        4534 :             matchOpno = opno;
    3435        4534 :             consttype = exprType(constExpr);
    3436        4534 :             arraytype = get_array_type(consttype);
    3437        4534 :             inputcollid = subClause->inputcollid;
    3438             : 
    3439             :             /*
    3440             :              * Check that the operator is presented in the opfamily and that
    3441             :              * the expression collation matches the index collation.  Also,
    3442             :              * there must be an array type to construct an array later.
    3443             :              */
    3444        4534 :             if (!IndexCollMatchesExprColl(index->indexcollations[indexcol], inputcollid) ||
    3445        4408 :                 !op_in_opfamily(matchOpno, index->opfamily[indexcol]) ||
    3446             :                 !OidIsValid(arraytype))
    3447             :                 break;
    3448        2766 :             firstTime = false;
    3449             :         }
    3450             :         else
    3451             :         {
    3452        1740 :             if (opno != matchOpno ||
    3453        3228 :                 inputcollid != subClause->inputcollid ||
    3454        1614 :                 consttype != exprType(constExpr))
    3455             :                 break;
    3456             :         }
    3457             : 
    3458             :         /*
    3459             :          * Check if our list of constants in match_clause_to_indexcol's
    3460             :          * understanding of const-ness have something other than Const.
    3461             :          */
    3462        4380 :         if (!IsA(constExpr, Const))
    3463         344 :             haveNonConst = true;
    3464        4380 :         consts = lappend(consts, constExpr);
    3465             :     }
    3466             : 
    3467             :     /*
    3468             :      * Catch the break from the loop above.  Normally, a foreach() loop ends
    3469             :      * up with a NULL list cell.  A non-NULL list cell indicates a break from
    3470             :      * the foreach() loop.  Free the consts list and return NULL then.
    3471             :      */
    3472       45478 :     if (lc != NULL)
    3473             :     {
    3474       44428 :         list_free(consts);
    3475       44428 :         return NULL;
    3476             :     }
    3477             : 
    3478             :     /*
    3479             :      * Assemble an array from the list of constants.  It seems more profitable
    3480             :      * to build a const array.  But in the presence of other nodes, we don't
    3481             :      * have a specific value here and must employ an ArrayExpr instead.
    3482             :      */
    3483        1050 :     if (haveNonConst)
    3484             :     {
    3485          96 :         ArrayExpr  *arrayExpr = makeNode(ArrayExpr);
    3486             : 
    3487             :         /* array_collid will be set by parse_collate.c */
    3488          96 :         arrayExpr->element_typeid = consttype;
    3489          96 :         arrayExpr->array_typeid = arraytype;
    3490          96 :         arrayExpr->multidims = false;
    3491          96 :         arrayExpr->elements = consts;
    3492          96 :         arrayExpr->location = -1;
    3493             : 
    3494          96 :         arrayNode = (Node *) arrayExpr;
    3495             :     }
    3496             :     else
    3497             :     {
    3498             :         int16       typlen;
    3499             :         bool        typbyval;
    3500             :         char        typalign;
    3501             :         Datum      *elems;
    3502         954 :         int         i = 0;
    3503             :         ArrayType  *arrayConst;
    3504             : 
    3505         954 :         get_typlenbyvalalign(consttype, &typlen, &typbyval, &typalign);
    3506             : 
    3507         954 :         elems = (Datum *) palloc(sizeof(Datum) * list_length(consts));
    3508        4254 :         foreach_node(Const, value, consts)
    3509             :         {
    3510             :             Assert(!value->constisnull);
    3511             : 
    3512        2346 :             elems[i++] = value->constvalue;
    3513             :         }
    3514             : 
    3515         954 :         arrayConst = construct_array(elems, i, consttype,
    3516             :                                      typlen, typbyval, typalign);
    3517         954 :         arrayNode = (Node *) makeConst(arraytype, -1, inputcollid,
    3518             :                                        -1, PointerGetDatum(arrayConst),
    3519             :                                        false, false);
    3520             : 
    3521         954 :         pfree(elems);
    3522         954 :         list_free(consts);
    3523             :     }
    3524             : 
    3525             :     /* Build the SAOP expression node */
    3526        1050 :     saopexpr = makeNode(ScalarArrayOpExpr);
    3527        1050 :     saopexpr->opno = matchOpno;
    3528        1050 :     saopexpr->opfuncid = get_opcode(matchOpno);
    3529        1050 :     saopexpr->hashfuncid = InvalidOid;
    3530        1050 :     saopexpr->negfuncid = InvalidOid;
    3531        1050 :     saopexpr->useOr = true;
    3532        1050 :     saopexpr->inputcollid = inputcollid;
    3533        1050 :     saopexpr->args = list_make2(indexExpr, arrayNode);
    3534        1050 :     saopexpr->location = -1;
    3535             : 
    3536             :     /*
    3537             :      * Finally, build an IndexClause based on the SAOP node.  Use
    3538             :      * make_simple_restrictinfo() to get RestrictInfo with clean selectivity
    3539             :      * estimations, because they may differ from the estimation made for an OR
    3540             :      * clause.  Although it is not a lossy expression, keep the original rinfo
    3541             :      * in iclause->rinfo as prescribed.
    3542             :      */
    3543        1050 :     iclause = makeNode(IndexClause);
    3544        1050 :     iclause->rinfo = rinfo;
    3545        1050 :     iclause->indexquals = list_make1(make_simple_restrictinfo(root,
    3546             :                                                               &saopexpr->xpr));
    3547        1050 :     iclause->lossy = false;
    3548        1050 :     iclause->indexcol = indexcol;
    3549        1050 :     iclause->indexcols = NIL;
    3550        1050 :     return iclause;
    3551             : }
    3552             : 
    3553             : /*
    3554             :  * expand_indexqual_rowcompare --- expand a single indexqual condition
    3555             :  *      that is a RowCompareExpr
    3556             :  *
    3557             :  * It's already known that the first column of the row comparison matches
    3558             :  * the specified column of the index.  We can use additional columns of the
    3559             :  * row comparison as index qualifications, so long as they match the index
    3560             :  * in the "same direction", ie, the indexkeys are all on the same side of the
    3561             :  * clause and the operators are all the same-type members of the opfamilies.
    3562             :  *
    3563             :  * If all the columns of the RowCompareExpr match in this way, we just use it
    3564             :  * as-is, except for possibly commuting it to put the indexkeys on the left.
    3565             :  *
    3566             :  * Otherwise, we build a shortened RowCompareExpr (if more than one
    3567             :  * column matches) or a simple OpExpr (if the first-column match is all
    3568             :  * there is).  In these cases the modified clause is always "<=" or ">="
    3569             :  * even when the original was "<" or ">" --- this is necessary to match all
    3570             :  * the rows that could match the original.  (We are building a lossy version
    3571             :  * of the row comparison when we do this, so we set lossy = true.)
    3572             :  *
    3573             :  * Note: this is really just the last half of match_rowcompare_to_indexcol,
    3574             :  * but we split it out for comprehensibility.
    3575             :  */
    3576             : static IndexClause *
    3577         150 : expand_indexqual_rowcompare(PlannerInfo *root,
    3578             :                             RestrictInfo *rinfo,
    3579             :                             int indexcol,
    3580             :                             IndexOptInfo *index,
    3581             :                             Oid expr_op,
    3582             :                             bool var_on_left)
    3583             : {
    3584         150 :     IndexClause *iclause = makeNode(IndexClause);
    3585         150 :     RowCompareExpr *clause = (RowCompareExpr *) rinfo->clause;
    3586             :     int         op_strategy;
    3587             :     Oid         op_lefttype;
    3588             :     Oid         op_righttype;
    3589             :     int         matching_cols;
    3590             :     List       *expr_ops;
    3591             :     List       *opfamilies;
    3592             :     List       *lefttypes;
    3593             :     List       *righttypes;
    3594             :     List       *new_ops;
    3595             :     List       *var_args;
    3596             :     List       *non_var_args;
    3597             : 
    3598         150 :     iclause->rinfo = rinfo;
    3599         150 :     iclause->indexcol = indexcol;
    3600             : 
    3601         150 :     if (var_on_left)
    3602             :     {
    3603         126 :         var_args = clause->largs;
    3604         126 :         non_var_args = clause->rargs;
    3605             :     }
    3606             :     else
    3607             :     {
    3608          24 :         var_args = clause->rargs;
    3609          24 :         non_var_args = clause->largs;
    3610             :     }
    3611             : 
    3612         150 :     get_op_opfamily_properties(expr_op, index->opfamily[indexcol], false,
    3613             :                                &op_strategy,
    3614             :                                &op_lefttype,
    3615             :                                &op_righttype);
    3616             : 
    3617             :     /* Initialize returned list of which index columns are used */
    3618         150 :     iclause->indexcols = list_make1_int(indexcol);
    3619             : 
    3620             :     /* Build lists of ops, opfamilies and operator datatypes in case needed */
    3621         150 :     expr_ops = list_make1_oid(expr_op);
    3622         150 :     opfamilies = list_make1_oid(index->opfamily[indexcol]);
    3623         150 :     lefttypes = list_make1_oid(op_lefttype);
    3624         150 :     righttypes = list_make1_oid(op_righttype);
    3625             : 
    3626             :     /*
    3627             :      * See how many of the remaining columns match some index column in the
    3628             :      * same way.  As in match_clause_to_indexcol(), the "other" side of any
    3629             :      * potential index condition is OK as long as it doesn't use Vars from the
    3630             :      * indexed relation.
    3631             :      */
    3632         150 :     matching_cols = 1;
    3633             : 
    3634         282 :     while (matching_cols < list_length(var_args))
    3635             :     {
    3636         186 :         Node       *varop = (Node *) list_nth(var_args, matching_cols);
    3637         186 :         Node       *constop = (Node *) list_nth(non_var_args, matching_cols);
    3638             :         int         i;
    3639             : 
    3640         186 :         expr_op = list_nth_oid(clause->opnos, matching_cols);
    3641         186 :         if (!var_on_left)
    3642             :         {
    3643             :             /* indexkey is on right, so commute the operator */
    3644          24 :             expr_op = get_commutator(expr_op);
    3645          24 :             if (expr_op == InvalidOid)
    3646           0 :                 break;          /* operator is not usable */
    3647             :         }
    3648         186 :         if (bms_is_member(index->rel->relid, pull_varnos(root, constop)))
    3649           0 :             break;              /* no good, Var on wrong side */
    3650         186 :         if (contain_volatile_functions(constop))
    3651           0 :             break;              /* no good, volatile comparison value */
    3652             : 
    3653             :         /*
    3654             :          * The Var side can match any key column of the index.
    3655             :          */
    3656         444 :         for (i = 0; i < index->nkeycolumns; i++)
    3657             :         {
    3658         390 :             if (match_index_to_operand(varop, i, index) &&
    3659         132 :                 get_op_opfamily_strategy(expr_op,
    3660         132 :                                          index->opfamily[i]) == op_strategy &&
    3661         132 :                 IndexCollMatchesExprColl(index->indexcollations[i],
    3662             :                                          list_nth_oid(clause->inputcollids,
    3663             :                                                       matching_cols)))
    3664             :                 break;
    3665             :         }
    3666         186 :         if (i >= index->nkeycolumns)
    3667          54 :             break;              /* no match found */
    3668             : 
    3669             :         /* Add column number to returned list */
    3670         132 :         iclause->indexcols = lappend_int(iclause->indexcols, i);
    3671             : 
    3672             :         /* Add operator info to lists */
    3673         132 :         get_op_opfamily_properties(expr_op, index->opfamily[i], false,
    3674             :                                    &op_strategy,
    3675             :                                    &op_lefttype,
    3676             :                                    &op_righttype);
    3677         132 :         expr_ops = lappend_oid(expr_ops, expr_op);
    3678         132 :         opfamilies = lappend_oid(opfamilies, index->opfamily[i]);
    3679         132 :         lefttypes = lappend_oid(lefttypes, op_lefttype);
    3680         132 :         righttypes = lappend_oid(righttypes, op_righttype);
    3681             : 
    3682             :         /* This column matches, keep scanning */
    3683         132 :         matching_cols++;
    3684             :     }
    3685             : 
    3686             :     /* Result is non-lossy if all columns are usable as index quals */
    3687         150 :     iclause->lossy = (matching_cols != list_length(clause->opnos));
    3688             : 
    3689             :     /*
    3690             :      * We can use rinfo->clause as-is if we have var on left and it's all
    3691             :      * usable as index quals.
    3692             :      */
    3693         150 :     if (var_on_left && !iclause->lossy)
    3694          84 :         iclause->indexquals = list_make1(rinfo);
    3695             :     else
    3696             :     {
    3697             :         /*
    3698             :          * We have to generate a modified rowcompare (possibly just one
    3699             :          * OpExpr).  The painful part of this is changing < to <= or > to >=,
    3700             :          * so deal with that first.
    3701             :          */
    3702          66 :         if (!iclause->lossy)
    3703             :         {
    3704             :             /* very easy, just use the commuted operators */
    3705          12 :             new_ops = expr_ops;
    3706             :         }
    3707          54 :         else if (op_strategy == BTLessEqualStrategyNumber ||
    3708          54 :                  op_strategy == BTGreaterEqualStrategyNumber)
    3709             :         {
    3710             :             /* easy, just use the same (possibly commuted) operators */
    3711           0 :             new_ops = list_truncate(expr_ops, matching_cols);
    3712             :         }
    3713             :         else
    3714             :         {
    3715             :             ListCell   *opfamilies_cell;
    3716             :             ListCell   *lefttypes_cell;
    3717             :             ListCell   *righttypes_cell;
    3718             : 
    3719          54 :             if (op_strategy == BTLessStrategyNumber)
    3720          30 :                 op_strategy = BTLessEqualStrategyNumber;
    3721          24 :             else if (op_strategy == BTGreaterStrategyNumber)
    3722          24 :                 op_strategy = BTGreaterEqualStrategyNumber;
    3723             :             else
    3724           0 :                 elog(ERROR, "unexpected strategy number %d", op_strategy);
    3725          54 :             new_ops = NIL;
    3726         144 :             forthree(opfamilies_cell, opfamilies,
    3727             :                      lefttypes_cell, lefttypes,
    3728             :                      righttypes_cell, righttypes)
    3729             :             {
    3730          90 :                 Oid         opfam = lfirst_oid(opfamilies_cell);
    3731          90 :                 Oid         lefttype = lfirst_oid(lefttypes_cell);
    3732          90 :                 Oid         righttype = lfirst_oid(righttypes_cell);
    3733             : 
    3734          90 :                 expr_op = get_opfamily_member(opfam, lefttype, righttype,
    3735             :                                               op_strategy);
    3736          90 :                 if (!OidIsValid(expr_op))   /* should not happen */
    3737           0 :                     elog(ERROR, "missing operator %d(%u,%u) in opfamily %u",
    3738             :                          op_strategy, lefttype, righttype, opfam);
    3739          90 :                 new_ops = lappend_oid(new_ops, expr_op);
    3740             :             }
    3741             :         }
    3742             : 
    3743             :         /* If we have more than one matching col, create a subset rowcompare */
    3744          66 :         if (matching_cols > 1)
    3745             :         {
    3746          48 :             RowCompareExpr *rc = makeNode(RowCompareExpr);
    3747             : 
    3748          48 :             rc->cmptype = (CompareType) op_strategy;
    3749          48 :             rc->opnos = new_ops;
    3750          48 :             rc->opfamilies = list_copy_head(clause->opfamilies,
    3751             :                                             matching_cols);
    3752          48 :             rc->inputcollids = list_copy_head(clause->inputcollids,
    3753             :                                               matching_cols);
    3754          48 :             rc->largs = list_copy_head(var_args, matching_cols);
    3755          48 :             rc->rargs = list_copy_head(non_var_args, matching_cols);
    3756          48 :             iclause->indexquals = list_make1(make_simple_restrictinfo(root,
    3757             :                                                                       (Expr *) rc));
    3758             :         }
    3759             :         else
    3760             :         {
    3761             :             Expr       *op;
    3762             : 
    3763             :             /* We don't report an index column list in this case */
    3764          18 :             iclause->indexcols = NIL;
    3765             : 
    3766          18 :             op = make_opclause(linitial_oid(new_ops), BOOLOID, false,
    3767          18 :                                copyObject(linitial(var_args)),
    3768          18 :                                copyObject(linitial(non_var_args)),
    3769             :                                InvalidOid,
    3770          18 :                                linitial_oid(clause->inputcollids));
    3771          18 :             iclause->indexquals = list_make1(make_simple_restrictinfo(root, op));
    3772             :         }
    3773             :     }
    3774             : 
    3775         150 :     return iclause;
    3776             : }
    3777             : 
    3778             : 
    3779             : /****************************************************************************
    3780             :  *              ----  ROUTINES TO CHECK ORDERING OPERATORS  ----
    3781             :  ****************************************************************************/
    3782             : 
    3783             : /*
    3784             :  * match_pathkeys_to_index
    3785             :  *      For the given 'index' and 'pathkeys', output a list of suitable ORDER
    3786             :  *      BY expressions, each of the form "indexedcol operator pseudoconstant",
    3787             :  *      along with an integer list of the index column numbers (zero based)
    3788             :  *      that each clause would be used with.
    3789             :  *
    3790             :  * This attempts to find an ORDER BY and index column number for all items in
    3791             :  * the pathkey list, however, if we're unable to match any given pathkey to an
    3792             :  * index column, we return just the ones matched by the function so far.  This
    3793             :  * allows callers who are interested in partial matches to get them.  Callers
    3794             :  * can determine a partial match vs a full match by checking the outputted
    3795             :  * list lengths.  A full match will have one item in the output lists for each
    3796             :  * item in the given 'pathkeys' list.
    3797             :  */
    3798             : static void
    3799        1078 : match_pathkeys_to_index(IndexOptInfo *index, List *pathkeys,
    3800             :                         List **orderby_clauses_p,
    3801             :                         List **clause_columns_p)
    3802             : {
    3803             :     ListCell   *lc1;
    3804             : 
    3805        1078 :     *orderby_clauses_p = NIL;   /* set default results */
    3806        1078 :     *clause_columns_p = NIL;
    3807             : 
    3808             :     /* Only indexes with the amcanorderbyop property are interesting here */
    3809        1078 :     if (!index->amcanorderbyop)
    3810           0 :         return;
    3811             : 
    3812        1552 :     foreach(lc1, pathkeys)
    3813             :     {
    3814        1080 :         PathKey    *pathkey = (PathKey *) lfirst(lc1);
    3815        1080 :         bool        found = false;
    3816             :         ListCell   *lc2;
    3817             : 
    3818             : 
    3819             :         /* Pathkey must request default sort order for the target opfamily */
    3820        1080 :         if (pathkey->pk_strategy != BTLessStrategyNumber ||
    3821        1046 :             pathkey->pk_nulls_first)
    3822         606 :             return;
    3823             : 
    3824             :         /* If eclass is volatile, no hope of using an indexscan */
    3825        1046 :         if (pathkey->pk_eclass->ec_has_volatile)
    3826           0 :             return;
    3827             : 
    3828             :         /*
    3829             :          * Try to match eclass member expression(s) to index.  Note that child
    3830             :          * EC members are considered, but only when they belong to the target
    3831             :          * relation.  (Unlike regular members, the same expression could be a
    3832             :          * child member of more than one EC.  Therefore, the same index could
    3833             :          * be considered to match more than one pathkey list, which is OK
    3834             :          * here.  See also get_eclass_for_sort_expr.)
    3835             :          */
    3836        1682 :         foreach(lc2, pathkey->pk_eclass->ec_members)
    3837             :         {
    3838        1110 :             EquivalenceMember *member = (EquivalenceMember *) lfirst(lc2);
    3839             :             int         indexcol;
    3840             : 
    3841             :             /* No possibility of match if it references other relations */
    3842        1110 :             if (!bms_equal(member->em_relids, index->rel->relids))
    3843          64 :                 continue;
    3844             : 
    3845             :             /*
    3846             :              * We allow any column of the index to match each pathkey; they
    3847             :              * don't have to match left-to-right as you might expect.  This is
    3848             :              * correct for GiST, and it doesn't matter for SP-GiST because
    3849             :              * that doesn't handle multiple columns anyway, and no other
    3850             :              * existing AMs support amcanorderbyop.  We might need different
    3851             :              * logic in future for other implementations.
    3852             :              */
    3853        1906 :             for (indexcol = 0; indexcol < index->nkeycolumns; indexcol++)
    3854             :             {
    3855             :                 Expr       *expr;
    3856             : 
    3857        1334 :                 expr = match_clause_to_ordering_op(index,
    3858             :                                                    indexcol,
    3859             :                                                    member->em_expr,
    3860             :                                                    pathkey->pk_opfamily);
    3861        1334 :                 if (expr)
    3862             :                 {
    3863         474 :                     *orderby_clauses_p = lappend(*orderby_clauses_p, expr);
    3864         474 :                     *clause_columns_p = lappend_int(*clause_columns_p, indexcol);
    3865         474 :                     found = true;
    3866         474 :                     break;
    3867             :                 }
    3868             :             }
    3869             : 
    3870        1046 :             if (found)          /* don't want to look at remaining members */
    3871         474 :                 break;
    3872             :         }
    3873             : 
    3874             :         /*
    3875             :          * Return the matches found so far when this pathkey couldn't be
    3876             :          * matched to the index.
    3877             :          */
    3878        1046 :         if (!found)
    3879         572 :             return;
    3880             :     }
    3881             : }
    3882             : 
    3883             : /*
    3884             :  * match_clause_to_ordering_op
    3885             :  *    Determines whether an ordering operator expression matches an
    3886             :  *    index column.
    3887             :  *
    3888             :  *    This is similar to, but simpler than, match_clause_to_indexcol.
    3889             :  *    We only care about simple OpExpr cases.  The input is a bare
    3890             :  *    expression that is being ordered by, which must be of the form
    3891             :  *    (indexkey op const) or (const op indexkey) where op is an ordering
    3892             :  *    operator for the column's opfamily.
    3893             :  *
    3894             :  * 'index' is the index of interest.
    3895             :  * 'indexcol' is a column number of 'index' (counting from 0).
    3896             :  * 'clause' is the ordering expression to be tested.
    3897             :  * 'pk_opfamily' is the btree opfamily describing the required sort order.
    3898             :  *
    3899             :  * Note that we currently do not consider the collation of the ordering
    3900             :  * operator's result.  In practical cases the result type will be numeric
    3901             :  * and thus have no collation, and it's not very clear what to match to
    3902             :  * if it did have a collation.  The index's collation should match the
    3903             :  * ordering operator's input collation, not its result.
    3904             :  *
    3905             :  * If successful, return 'clause' as-is if the indexkey is on the left,
    3906             :  * otherwise a commuted copy of 'clause'.  If no match, return NULL.
    3907             :  */
    3908             : static Expr *
    3909        1334 : match_clause_to_ordering_op(IndexOptInfo *index,
    3910             :                             int indexcol,
    3911             :                             Expr *clause,
    3912             :                             Oid pk_opfamily)
    3913             : {
    3914             :     Oid         opfamily;
    3915             :     Oid         idxcollation;
    3916             :     Node       *leftop,
    3917             :                *rightop;
    3918             :     Oid         expr_op;
    3919             :     Oid         expr_coll;
    3920             :     Oid         sortfamily;
    3921             :     bool        commuted;
    3922             : 
    3923             :     Assert(indexcol < index->nkeycolumns);
    3924             : 
    3925        1334 :     opfamily = index->opfamily[indexcol];
    3926        1334 :     idxcollation = index->indexcollations[indexcol];
    3927             : 
    3928             :     /*
    3929             :      * Clause must be a binary opclause.
    3930             :      */
    3931        1334 :     if (!is_opclause(clause))
    3932         860 :         return NULL;
    3933         474 :     leftop = get_leftop(clause);
    3934         474 :     rightop = get_rightop(clause);
    3935         474 :     if (!leftop || !rightop)
    3936           0 :         return NULL;
    3937         474 :     expr_op = ((OpExpr *) clause)->opno;
    3938         474 :     expr_coll = ((OpExpr *) clause)->inputcollid;
    3939             : 
    3940             :     /*
    3941             :      * We can forget the whole thing right away if wrong collation.
    3942             :      */
    3943         474 :     if (!IndexCollMatchesExprColl(idxcollation, expr_coll))
    3944           0 :         return NULL;
    3945             : 
    3946             :     /*
    3947             :      * Check for clauses of the form: (indexkey operator constant) or
    3948             :      * (constant operator indexkey).
    3949             :      */
    3950         474 :     if (match_index_to_operand(leftop, indexcol, index) &&
    3951         450 :         !contain_var_clause(rightop) &&
    3952         450 :         !contain_volatile_functions(rightop))
    3953             :     {
    3954         450 :         commuted = false;
    3955             :     }
    3956          24 :     else if (match_index_to_operand(rightop, indexcol, index) &&
    3957          24 :              !contain_var_clause(leftop) &&
    3958          24 :              !contain_volatile_functions(leftop))
    3959             :     {
    3960             :         /* Might match, but we need a commuted operator */
    3961          24 :         expr_op = get_commutator(expr_op);
    3962          24 :         if (expr_op == InvalidOid)
    3963           0 :             return NULL;
    3964          24 :         commuted = true;
    3965             :     }
    3966             :     else
    3967           0 :         return NULL;
    3968             : 
    3969             :     /*
    3970             :      * Is the (commuted) operator an ordering operator for the opfamily? And
    3971             :      * if so, does it yield the right sorting semantics?
    3972             :      */
    3973         474 :     sortfamily = get_op_opfamily_sortfamily(expr_op, opfamily);
    3974         474 :     if (sortfamily != pk_opfamily)
    3975           0 :         return NULL;
    3976             : 
    3977             :     /* We have a match.  Return clause or a commuted version thereof. */
    3978         474 :     if (commuted)
    3979             :     {
    3980          24 :         OpExpr     *newclause = makeNode(OpExpr);
    3981             : 
    3982             :         /* flat-copy all the fields of clause */
    3983          24 :         memcpy(newclause, clause, sizeof(OpExpr));
    3984             : 
    3985             :         /* commute it */
    3986          24 :         newclause->opno = expr_op;
    3987          24 :         newclause->opfuncid = InvalidOid;
    3988          24 :         newclause->args = list_make2(rightop, leftop);
    3989             : 
    3990          24 :         clause = (Expr *) newclause;
    3991             :     }
    3992             : 
    3993         474 :     return clause;
    3994             : }
    3995             : 
    3996             : 
    3997             : /****************************************************************************
    3998             :  *              ----  ROUTINES TO DO PARTIAL INDEX PREDICATE TESTS  ----
    3999             :  ****************************************************************************/
    4000             : 
    4001             : /*
    4002             :  * check_index_predicates
    4003             :  *      Set the predicate-derived IndexOptInfo fields for each index
    4004             :  *      of the specified relation.
    4005             :  *
    4006             :  * predOK is set true if the index is partial and its predicate is satisfied
    4007             :  * for this query, ie the query's WHERE clauses imply the predicate.
    4008             :  *
    4009             :  * indrestrictinfo is set to the relation's baserestrictinfo list less any
    4010             :  * conditions that are implied by the index's predicate.  (Obviously, for a
    4011             :  * non-partial index, this is the same as baserestrictinfo.)  Such conditions
    4012             :  * can be dropped from the plan when using the index, in certain cases.
    4013             :  *
    4014             :  * At one time it was possible for this to get re-run after adding more
    4015             :  * restrictions to the rel, thus possibly letting us prove more indexes OK.
    4016             :  * That doesn't happen any more (at least not in the core code's usage),
    4017             :  * but this code still supports it in case extensions want to mess with the
    4018             :  * baserestrictinfo list.  We assume that adding more restrictions can't make
    4019             :  * an index not predOK.  We must recompute indrestrictinfo each time, though,
    4020             :  * to make sure any newly-added restrictions get into it if needed.
    4021             :  */
    4022             : void
    4023      384982 : check_index_predicates(PlannerInfo *root, RelOptInfo *rel)
    4024             : {
    4025             :     List       *clauselist;
    4026             :     bool        have_partial;
    4027             :     bool        is_target_rel;
    4028             :     Relids      otherrels;
    4029             :     ListCell   *lc;
    4030             : 
    4031             :     /* Indexes are available only on base or "other" member relations. */
    4032             :     Assert(IS_SIMPLE_REL(rel));
    4033             : 
    4034             :     /*
    4035             :      * Initialize the indrestrictinfo lists to be identical to
    4036             :      * baserestrictinfo, and check whether there are any partial indexes.  If
    4037             :      * not, this is all we need to do.
    4038             :      */
    4039      384982 :     have_partial = false;
    4040     1051072 :     foreach(lc, rel->indexlist)
    4041             :     {
    4042      666090 :         IndexOptInfo *index = (IndexOptInfo *) lfirst(lc);
    4043             : 
    4044      666090 :         index->indrestrictinfo = rel->baserestrictinfo;
    4045      666090 :         if (index->indpred)
    4046         984 :             have_partial = true;
    4047             :     }
    4048      384982 :     if (!have_partial)
    4049      384322 :         return;
    4050             : 
    4051             :     /*
    4052             :      * Construct a list of clauses that we can assume true for the purpose of
    4053             :      * proving the index(es) usable.  Restriction clauses for the rel are
    4054             :      * always usable, and so are any join clauses that are "movable to" this
    4055             :      * rel.  Also, we can consider any EC-derivable join clauses (which must
    4056             :      * be "movable to" this rel, by definition).
    4057             :      */
    4058         660 :     clauselist = list_copy(rel->baserestrictinfo);
    4059             : 
    4060             :     /* Scan the rel's join clauses */
    4061         660 :     foreach(lc, rel->joininfo)
    4062             :     {
    4063           0 :         RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
    4064             : 
    4065             :         /* Check if clause can be moved to this rel */
    4066           0 :         if (!join_clause_is_movable_to(rinfo, rel))
    4067           0 :             continue;
    4068             : 
    4069           0 :         clauselist = lappend(clauselist, rinfo);
    4070             :     }
    4071             : 
    4072             :     /*
    4073             :      * Add on any equivalence-derivable join clauses.  Computing the correct
    4074             :      * relid sets for generate_join_implied_equalities is slightly tricky
    4075             :      * because the rel could be a child rel rather than a true baserel, and in
    4076             :      * that case we must subtract its parents' relid(s) from all_query_rels.
    4077             :      * Additionally, we mustn't consider clauses that are only computable
    4078             :      * after outer joins that can null the rel.
    4079             :      */
    4080         660 :     if (rel->reloptkind == RELOPT_OTHER_MEMBER_REL)
    4081          72 :         otherrels = bms_difference(root->all_query_rels,
    4082          72 :                                    find_childrel_parents(root, rel));
    4083             :     else
    4084         588 :         otherrels = bms_difference(root->all_query_rels, rel->relids);
    4085         660 :     otherrels = bms_del_members(otherrels, rel->nulling_relids);
    4086             : 
    4087         660 :     if (!bms_is_empty(otherrels))
    4088             :         clauselist =
    4089          88 :             list_concat(clauselist,
    4090          88 :                         generate_join_implied_equalities(root,
    4091          88 :                                                          bms_union(rel->relids,
    4092             :                                                                    otherrels),
    4093             :                                                          otherrels,
    4094             :                                                          rel,
    4095             :                                                          NULL));
    4096             : 
    4097             :     /*
    4098             :      * Normally we remove quals that are implied by a partial index's
    4099             :      * predicate from indrestrictinfo, indicating that they need not be
    4100             :      * checked explicitly by an indexscan plan using this index.  However, if
    4101             :      * the rel is a target relation of UPDATE/DELETE/MERGE/SELECT FOR UPDATE,
    4102             :      * we cannot remove such quals from the plan, because they need to be in
    4103             :      * the plan so that they will be properly rechecked by EvalPlanQual
    4104             :      * testing.  Some day we might want to remove such quals from the main
    4105             :      * plan anyway and pass them through to EvalPlanQual via a side channel;
    4106             :      * but for now, we just don't remove implied quals at all for target
    4107             :      * relations.
    4108             :      */
    4109        1208 :     is_target_rel = (bms_is_member(rel->relid, root->all_result_relids) ||
    4110         548 :                      get_plan_rowmark(root->rowMarks, rel->relid) != NULL);
    4111             : 
    4112             :     /*
    4113             :      * Now try to prove each index predicate true, and compute the
    4114             :      * indrestrictinfo lists for partial indexes.  Note that we compute the
    4115             :      * indrestrictinfo list even for non-predOK indexes; this might seem
    4116             :      * wasteful, but we may be able to use such indexes in OR clauses, cf
    4117             :      * generate_bitmap_or_paths().
    4118             :      */
    4119        2030 :     foreach(lc, rel->indexlist)
    4120             :     {
    4121        1370 :         IndexOptInfo *index = (IndexOptInfo *) lfirst(lc);
    4122             :         ListCell   *lcr;
    4123             : 
    4124        1370 :         if (index->indpred == NIL)
    4125         386 :             continue;           /* ignore non-partial indexes here */
    4126             : 
    4127         984 :         if (!index->predOK)      /* don't repeat work if already proven OK */
    4128         984 :             index->predOK = predicate_implied_by(index->indpred, clauselist,
    4129             :                                                  false);
    4130             : 
    4131             :         /* If rel is an update target, leave indrestrictinfo as set above */
    4132         984 :         if (is_target_rel)
    4133         172 :             continue;
    4134             : 
    4135             :         /* Else compute indrestrictinfo as the non-implied quals */
    4136         812 :         index->indrestrictinfo = NIL;
    4137        1914 :         foreach(lcr, rel->baserestrictinfo)
    4138             :         {
    4139        1102 :             RestrictInfo *rinfo = (RestrictInfo *) lfirst(lcr);
    4140             : 
    4141             :             /* predicate_implied_by() assumes first arg is immutable */
    4142        1102 :             if (contain_mutable_functions((Node *) rinfo->clause) ||
    4143        1102 :                 !predicate_implied_by(list_make1(rinfo->clause),
    4144             :                                       index->indpred, false))
    4145         782 :                 index->indrestrictinfo = lappend(index->indrestrictinfo, rinfo);
    4146             :         }
    4147             :     }
    4148             : }
    4149             : 
    4150             : /****************************************************************************
    4151             :  *              ----  ROUTINES TO CHECK EXTERNALLY-VISIBLE CONDITIONS  ----
    4152             :  ****************************************************************************/
    4153             : 
    4154             : /*
    4155             :  * ec_member_matches_indexcol
    4156             :  *    Test whether an EquivalenceClass member matches an index column.
    4157             :  *
    4158             :  * This is a callback for use by generate_implied_equalities_for_column.
    4159             :  */
    4160             : static bool
    4161      392032 : ec_member_matches_indexcol(PlannerInfo *root, RelOptInfo *rel,
    4162             :                            EquivalenceClass *ec, EquivalenceMember *em,
    4163             :                            void *arg)
    4164             : {
    4165      392032 :     IndexOptInfo *index = ((ec_member_matches_arg *) arg)->index;
    4166      392032 :     int         indexcol = ((ec_member_matches_arg *) arg)->indexcol;
    4167             :     Oid         curFamily;
    4168             :     Oid         curCollation;
    4169             : 
    4170             :     Assert(indexcol < index->nkeycolumns);
    4171             : 
    4172      392032 :     curFamily = index->opfamily[indexcol];
    4173      392032 :     curCollation = index->indexcollations[indexcol];
    4174             : 
    4175             :     /*
    4176             :      * If it's a btree index, we can reject it if its opfamily isn't
    4177             :      * compatible with the EC, since no clause generated from the EC could be
    4178             :      * used with the index.  For non-btree indexes, we can't easily tell
    4179             :      * whether clauses generated from the EC could be used with the index, so
    4180             :      * don't check the opfamily.  This might mean we return "true" for a
    4181             :      * useless EC, so we have to recheck the results of
    4182             :      * generate_implied_equalities_for_column; see
    4183             :      * match_eclass_clauses_to_index.
    4184             :      */
    4185      392032 :     if (index->relam == BTREE_AM_OID &&
    4186      391990 :         !list_member_oid(ec->ec_opfamilies, curFamily))
    4187      121648 :         return false;
    4188             : 
    4189             :     /* We insist on collation match for all index types, though */
    4190      270384 :     if (!IndexCollMatchesExprColl(curCollation, ec->ec_collation))
    4191          14 :         return false;
    4192             : 
    4193      270370 :     return match_index_to_operand((Node *) em->em_expr, indexcol, index);
    4194             : }
    4195             : 
    4196             : /*
    4197             :  * relation_has_unique_index_for
    4198             :  *    Determine whether the relation provably has at most one row satisfying
    4199             :  *    a set of equality conditions, because the conditions constrain all
    4200             :  *    columns of some unique index.
    4201             :  *
    4202             :  * The conditions can be represented in either or both of two ways:
    4203             :  * 1. A list of RestrictInfo nodes, where the caller has already determined
    4204             :  * that each condition is a mergejoinable equality with an expression in
    4205             :  * this relation on one side, and an expression not involving this relation
    4206             :  * on the other.  The transient outer_is_left flag is used to identify which
    4207             :  * side we should look at: left side if outer_is_left is false, right side
    4208             :  * if it is true.
    4209             :  * 2. A list of expressions in this relation, and a corresponding list of
    4210             :  * equality operators. The caller must have already checked that the operators
    4211             :  * represent equality.  (Note: the operators could be cross-type; the
    4212             :  * expressions should correspond to their RHS inputs.)
    4213             :  *
    4214             :  * The caller need only supply equality conditions arising from joins;
    4215             :  * this routine automatically adds in any usable baserestrictinfo clauses.
    4216             :  * (Note that the passed-in restrictlist will be destructively modified!)
    4217             :  */
    4218             : bool
    4219         936 : relation_has_unique_index_for(PlannerInfo *root, RelOptInfo *rel,
    4220             :                               List *restrictlist,
    4221             :                               List *exprlist, List *oprlist)
    4222             : {
    4223         936 :     return relation_has_unique_index_ext(root, rel, restrictlist,
    4224             :                                          exprlist, oprlist, NULL);
    4225             : }
    4226             : 
    4227             : /*
    4228             :  * relation_has_unique_index_ext
    4229             :  *    Same as relation_has_unique_index_for(), but supports extra_clauses
    4230             :  *    parameter.  If extra_clauses isn't NULL, return baserestrictinfo clauses
    4231             :  *    which were used to derive uniqueness.
    4232             :  */
    4233             : bool
    4234      193306 : relation_has_unique_index_ext(PlannerInfo *root, RelOptInfo *rel,
    4235             :                               List *restrictlist,
    4236             :                               List *exprlist, List *oprlist,
    4237             :                               List **extra_clauses)
    4238             : {
    4239             :     ListCell   *ic;
    4240             : 
    4241             :     Assert(list_length(exprlist) == list_length(oprlist));
    4242             : 
    4243             :     /* Short-circuit if no indexes... */
    4244      193306 :     if (rel->indexlist == NIL)
    4245         466 :         return false;
    4246             : 
    4247             :     /*
    4248             :      * Examine the rel's restriction clauses for usable var = const clauses
    4249             :      * that we can add to the restrictlist.
    4250             :      */
    4251      319840 :     foreach(ic, rel->baserestrictinfo)
    4252             :     {
    4253      127000 :         RestrictInfo *restrictinfo = (RestrictInfo *) lfirst(ic);
    4254             : 
    4255             :         /*
    4256             :          * Note: can_join won't be set for a restriction clause, but
    4257             :          * mergeopfamilies will be if it has a mergejoinable operator and
    4258             :          * doesn't contain volatile functions.
    4259             :          */
    4260      127000 :         if (restrictinfo->mergeopfamilies == NIL)
    4261       56612 :             continue;           /* not mergejoinable */
    4262             : 
    4263             :         /*
    4264             :          * The clause certainly doesn't refer to anything but the given rel.
    4265             :          * If either side is pseudoconstant then we can use it.
    4266             :          */
    4267       70388 :         if (bms_is_empty(restrictinfo->left_relids))
    4268             :         {
    4269             :             /* righthand side is inner */
    4270          58 :             restrictinfo->outer_is_left = true;
    4271             :         }
    4272       70330 :         else if (bms_is_empty(restrictinfo->right_relids))
    4273             :         {
    4274             :             /* lefthand side is inner */
    4275       70204 :             restrictinfo->outer_is_left = false;
    4276             :         }
    4277             :         else
    4278         126 :             continue;
    4279             : 
    4280             :         /* OK, add to list */
    4281       70262 :         restrictlist = lappend(restrictlist, restrictinfo);
    4282             :     }
    4283             : 
    4284             :     /* Short-circuit the easy case */
    4285      192840 :     if (restrictlist == NIL && exprlist == NIL)
    4286         978 :         return false;
    4287             : 
    4288             :     /* Examine each index of the relation ... */
    4289      480330 :     foreach(ic, rel->indexlist)
    4290             :     {
    4291      402326 :         IndexOptInfo *ind = (IndexOptInfo *) lfirst(ic);
    4292             :         int         c;
    4293      402326 :         List       *exprs = NIL;
    4294             : 
    4295             :         /*
    4296             :          * If the index is not unique, or not immediately enforced, or if it's
    4297             :          * a partial index, it's useless here.  We're unable to make use of
    4298             :          * predOK partial unique indexes due to the fact that
    4299             :          * check_index_predicates() also makes use of join predicates to
    4300             :          * determine if the partial index is usable. Here we need proofs that
    4301             :          * hold true before any joins are evaluated.
    4302             :          */
    4303      402326 :         if (!ind->unique || !ind->immediate || ind->indpred != NIL)
    4304      106004 :             continue;
    4305             : 
    4306             :         /*
    4307             :          * Try to find each index column in the lists of conditions.  This is
    4308             :          * O(N^2) or worse, but we expect all the lists to be short.
    4309             :          */
    4310      494088 :         for (c = 0; c < ind->nkeycolumns; c++)
    4311             :         {
    4312      380230 :             bool        matched = false;
    4313             :             ListCell   *lc;
    4314             :             ListCell   *lc2;
    4315             : 
    4316      707598 :             foreach(lc, restrictlist)
    4317             :             {
    4318      525134 :                 RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
    4319             :                 Node       *rexpr;
    4320             : 
    4321             :                 /*
    4322             :                  * The condition's equality operator must be a member of the
    4323             :                  * index opfamily, else it is not asserting the right kind of
    4324             :                  * equality behavior for this index.  We check this first
    4325             :                  * since it's probably cheaper than match_index_to_operand().
    4326             :                  */
    4327      525134 :                 if (!list_member_oid(rinfo->mergeopfamilies, ind->opfamily[c]))
    4328      150620 :                     continue;
    4329             : 
    4330             :                 /*
    4331             :                  * XXX at some point we may need to check collations here too.
    4332             :                  * For the moment we assume all collations reduce to the same
    4333             :                  * notion of equality.
    4334             :                  */
    4335             : 
    4336             :                 /* OK, see if the condition operand matches the index key */
    4337      374514 :                 if (rinfo->outer_is_left)
    4338      165412 :                     rexpr = get_rightop(rinfo->clause);
    4339             :                 else
    4340      209102 :                     rexpr = get_leftop(rinfo->clause);
    4341             : 
    4342      374514 :                 if (match_index_to_operand(rexpr, c, ind))
    4343             :                 {
    4344      197766 :                     matched = true; /* column is unique */
    4345             : 
    4346      197766 :                     if (bms_membership(rinfo->clause_relids) == BMS_SINGLETON)
    4347             :                     {
    4348             :                         MemoryContext oldMemCtx =
    4349       49568 :                             MemoryContextSwitchTo(root->planner_cxt);
    4350             : 
    4351             :                         /*
    4352             :                          * Add filter clause into a list allowing caller to
    4353             :                          * know if uniqueness have made not only by join
    4354             :                          * clauses.
    4355             :                          */
    4356             :                         Assert(bms_is_empty(rinfo->left_relids) ||
    4357             :                                bms_is_empty(rinfo->right_relids));
    4358       49568 :                         if (extra_clauses)
    4359         144 :                             exprs = lappend(exprs, rinfo);
    4360       49568 :                         MemoryContextSwitchTo(oldMemCtx);
    4361             :                     }
    4362             : 
    4363      197766 :                     break;
    4364             :                 }
    4365             :             }
    4366             : 
    4367      380230 :             if (matched)
    4368      197766 :                 continue;
    4369             : 
    4370      182622 :             forboth(lc, exprlist, lc2, oprlist)
    4371             :             {
    4372         158 :                 Node       *expr = (Node *) lfirst(lc);
    4373         158 :                 Oid         opr = lfirst_oid(lc2);
    4374             : 
    4375             :                 /* See if the expression matches the index key */
    4376         158 :                 if (!match_index_to_operand(expr, c, ind))
    4377         158 :                     continue;
    4378             : 
    4379             :                 /*
    4380             :                  * The equality operator must be a member of the index
    4381             :                  * opfamily, else it is not asserting the right kind of
    4382             :                  * equality behavior for this index.  We assume the caller
    4383             :                  * determined it is an equality operator, so we don't need to
    4384             :                  * check any more tightly than this.
    4385             :                  */
    4386           0 :                 if (!op_in_opfamily(opr, ind->opfamily[c]))
    4387           0 :                     continue;
    4388             : 
    4389             :                 /*
    4390             :                  * XXX at some point we may need to check collations here too.
    4391             :                  * For the moment we assume all collations reduce to the same
    4392             :                  * notion of equality.
    4393             :                  */
    4394             : 
    4395           0 :                 matched = true; /* column is unique */
    4396           0 :                 break;
    4397             :             }
    4398             : 
    4399      182464 :             if (!matched)
    4400      182464 :                 break;          /* no match; this index doesn't help us */
    4401             :         }
    4402             : 
    4403             :         /* Matched all key columns of this index? */
    4404      296322 :         if (c == ind->nkeycolumns)
    4405             :         {
    4406      113858 :             if (extra_clauses)
    4407         642 :                 *extra_clauses = exprs;
    4408      113858 :             return true;
    4409             :         }
    4410             :     }
    4411             : 
    4412       78004 :     return false;
    4413             : }
    4414             : 
    4415             : /*
    4416             :  * indexcol_is_bool_constant_for_query
    4417             :  *
    4418             :  * If an index column is constrained to have a constant value by the query's
    4419             :  * WHERE conditions, then it's irrelevant for sort-order considerations.
    4420             :  * Usually that means we have a restriction clause WHERE indexcol = constant,
    4421             :  * which gets turned into an EquivalenceClass containing a constant, which
    4422             :  * is recognized as redundant by build_index_pathkeys().  But if the index
    4423             :  * column is a boolean variable (or expression), then we are not going to
    4424             :  * see WHERE indexcol = constant, because expression preprocessing will have
    4425             :  * simplified that to "WHERE indexcol" or "WHERE NOT indexcol".  So we are not
    4426             :  * going to have a matching EquivalenceClass (unless the query also contains
    4427             :  * "ORDER BY indexcol").  To allow such cases to work the same as they would
    4428             :  * for non-boolean values, this function is provided to detect whether the
    4429             :  * specified index column matches a boolean restriction clause.
    4430             :  */
    4431             : bool
    4432      573588 : indexcol_is_bool_constant_for_query(PlannerInfo *root,
    4433             :                                     IndexOptInfo *index,
    4434             :                                     int indexcol)
    4435             : {
    4436             :     ListCell   *lc;
    4437             : 
    4438             :     /* If the index isn't boolean, we can't possibly get a match */
    4439      573588 :     if (!IsBooleanOpfamily(index->opfamily[indexcol]))
    4440      566124 :         return false;
    4441             : 
    4442             :     /* Check each restriction clause for the index's rel */
    4443        7500 :     foreach(lc, index->rel->baserestrictinfo)
    4444             :     {
    4445        1260 :         RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
    4446             : 
    4447             :         /*
    4448             :          * As in match_clause_to_indexcol, never match pseudoconstants to
    4449             :          * indexes.  (It might be semantically okay to do so here, but the
    4450             :          * odds of getting a match are negligible, so don't waste the cycles.)
    4451             :          */
    4452        1260 :         if (rinfo->pseudoconstant)
    4453           0 :             continue;
    4454             : 
    4455             :         /* See if we can match the clause's expression to the index column */
    4456        1260 :         if (match_boolean_index_clause(root, rinfo, indexcol, index))
    4457        1224 :             return true;
    4458             :     }
    4459             : 
    4460        6240 :     return false;
    4461             : }
    4462             : 
    4463             : 
    4464             : /****************************************************************************
    4465             :  *              ----  ROUTINES TO CHECK OPERANDS  ----
    4466             :  ****************************************************************************/
    4467             : 
    4468             : /*
    4469             :  * match_index_to_operand()
    4470             :  *    Generalized test for a match between an index's key
    4471             :  *    and the operand on one side of a restriction or join clause.
    4472             :  *
    4473             :  * operand: the nodetree to be compared to the index
    4474             :  * indexcol: the column number of the index (counting from 0)
    4475             :  * index: the index of interest
    4476             :  *
    4477             :  * Note that we aren't interested in collations here; the caller must check
    4478             :  * for a collation match, if it's dealing with an operator where that matters.
    4479             :  *
    4480             :  * This is exported for use in selfuncs.c.
    4481             :  */
    4482             : bool
    4483     3374544 : match_index_to_operand(Node *operand,
    4484             :                        int indexcol,
    4485             :                        IndexOptInfo *index)
    4486             : {
    4487             :     int         indkey;
    4488             : 
    4489             :     /*
    4490             :      * Ignore any RelabelType node above the operand.   This is needed to be
    4491             :      * able to apply indexscanning in binary-compatible-operator cases. Note:
    4492             :      * we can assume there is at most one RelabelType node;
    4493             :      * eval_const_expressions() will have simplified if more than one.
    4494             :      */
    4495     3374544 :     if (operand && IsA(operand, RelabelType))
    4496       23272 :         operand = (Node *) ((RelabelType *) operand)->arg;
    4497             : 
    4498     3374544 :     indkey = index->indexkeys[indexcol];
    4499     3374544 :     if (indkey != 0)
    4500             :     {
    4501             :         /*
    4502             :          * Simple index column; operand must be a matching Var.
    4503             :          */
    4504     3368558 :         if (operand && IsA(operand, Var) &&
    4505     2502608 :             index->rel->relid == ((Var *) operand)->varno &&
    4506     2313386 :             indkey == ((Var *) operand)->varattno &&
    4507      832336 :             ((Var *) operand)->varnullingrels == NULL)
    4508      831494 :             return true;
    4509             :     }
    4510             :     else
    4511             :     {
    4512             :         /*
    4513             :          * Index expression; find the correct expression.  (This search could
    4514             :          * be avoided, at the cost of complicating all the callers of this
    4515             :          * routine; doesn't seem worth it.)
    4516             :          */
    4517             :         ListCell   *indexpr_item;
    4518             :         int         i;
    4519             :         Node       *indexkey;
    4520             : 
    4521        5986 :         indexpr_item = list_head(index->indexprs);
    4522        5986 :         for (i = 0; i < indexcol; i++)
    4523             :         {
    4524           0 :             if (index->indexkeys[i] == 0)
    4525             :             {
    4526           0 :                 if (indexpr_item == NULL)
    4527           0 :                     elog(ERROR, "wrong number of index expressions");
    4528           0 :                 indexpr_item = lnext(index->indexprs, indexpr_item);
    4529             :             }
    4530             :         }
    4531        5986 :         if (indexpr_item == NULL)
    4532           0 :             elog(ERROR, "wrong number of index expressions");
    4533        5986 :         indexkey = (Node *) lfirst(indexpr_item);
    4534             : 
    4535             :         /*
    4536             :          * Does it match the operand?  Again, strip any relabeling.
    4537             :          */
    4538        5986 :         if (indexkey && IsA(indexkey, RelabelType))
    4539          10 :             indexkey = (Node *) ((RelabelType *) indexkey)->arg;
    4540             : 
    4541        5986 :         if (equal(indexkey, operand))
    4542        2164 :             return true;
    4543             :     }
    4544             : 
    4545     2540886 :     return false;
    4546             : }
    4547             : 
    4548             : /*
    4549             :  * is_pseudo_constant_for_index()
    4550             :  *    Test whether the given expression can be used as an indexscan
    4551             :  *    comparison value.
    4552             :  *
    4553             :  * An indexscan comparison value must not contain any volatile functions,
    4554             :  * and it can't contain any Vars of the index's own table.  Vars of
    4555             :  * other tables are okay, though; in that case we'd be producing an
    4556             :  * indexqual usable in a parameterized indexscan.  This is, therefore,
    4557             :  * a weaker condition than is_pseudo_constant_clause().
    4558             :  *
    4559             :  * This function is exported for use by planner support functions,
    4560             :  * which will have available the IndexOptInfo, but not any RestrictInfo
    4561             :  * infrastructure.  It is making the same test made by functions above
    4562             :  * such as match_opclause_to_indexcol(), but those rely where possible
    4563             :  * on RestrictInfo information about variable membership.
    4564             :  *
    4565             :  * expr: the nodetree to be checked
    4566             :  * index: the index of interest
    4567             :  */
    4568             : bool
    4569           0 : is_pseudo_constant_for_index(PlannerInfo *root, Node *expr, IndexOptInfo *index)
    4570             : {
    4571             :     /* pull_varnos is cheaper than volatility check, so do that first */
    4572           0 :     if (bms_is_member(index->rel->relid, pull_varnos(root, expr)))
    4573           0 :         return false;           /* no good, contains Var of table */
    4574           0 :     if (contain_volatile_functions(expr))
    4575           0 :         return false;           /* no good, volatile comparison value */
    4576           0 :     return true;
    4577             : }

Generated by: LCOV version 1.14