LCOV - code coverage report
Current view: top level - src/backend/optimizer/util - plancat.c (source / functions) Hit Total Coverage
Test: PostgreSQL 14devel Lines: 681 732 93.0 %
Date: 2020-08-05 07:06:24 Functions: 24 24 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * plancat.c
       4             :  *     routines for accessing the system catalogs
       5             :  *
       6             :  *
       7             :  * Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
       8             :  * Portions Copyright (c) 1994, Regents of the University of California
       9             :  *
      10             :  *
      11             :  * IDENTIFICATION
      12             :  *    src/backend/optimizer/util/plancat.c
      13             :  *
      14             :  *-------------------------------------------------------------------------
      15             :  */
      16             : #include "postgres.h"
      17             : 
      18             : #include <math.h>
      19             : 
      20             : #include "access/genam.h"
      21             : #include "access/htup_details.h"
      22             : #include "access/nbtree.h"
      23             : #include "access/sysattr.h"
      24             : #include "access/table.h"
      25             : #include "access/tableam.h"
      26             : #include "access/transam.h"
      27             : #include "access/xlog.h"
      28             : #include "catalog/catalog.h"
      29             : #include "catalog/dependency.h"
      30             : #include "catalog/heap.h"
      31             : #include "catalog/pg_am.h"
      32             : #include "catalog/pg_proc.h"
      33             : #include "catalog/pg_statistic_ext.h"
      34             : #include "foreign/fdwapi.h"
      35             : #include "miscadmin.h"
      36             : #include "nodes/makefuncs.h"
      37             : #include "nodes/supportnodes.h"
      38             : #include "optimizer/clauses.h"
      39             : #include "optimizer/cost.h"
      40             : #include "optimizer/optimizer.h"
      41             : #include "optimizer/plancat.h"
      42             : #include "optimizer/prep.h"
      43             : #include "parser/parse_relation.h"
      44             : #include "parser/parsetree.h"
      45             : #include "partitioning/partdesc.h"
      46             : #include "rewrite/rewriteManip.h"
      47             : #include "statistics/statistics.h"
      48             : #include "storage/bufmgr.h"
      49             : #include "utils/builtins.h"
      50             : #include "utils/lsyscache.h"
      51             : #include "utils/partcache.h"
      52             : #include "utils/rel.h"
      53             : #include "utils/snapmgr.h"
      54             : #include "utils/syscache.h"
      55             : 
      56             : /* GUC parameter */
      57             : int         constraint_exclusion = CONSTRAINT_EXCLUSION_PARTITION;
      58             : 
      59             : /* Hook for plugins to get control in get_relation_info() */
      60             : get_relation_info_hook_type get_relation_info_hook = NULL;
      61             : 
      62             : 
      63             : static void get_relation_foreign_keys(PlannerInfo *root, RelOptInfo *rel,
      64             :                                       Relation relation, bool inhparent);
      65             : static bool infer_collation_opclass_match(InferenceElem *elem, Relation idxRel,
      66             :                                           List *idxExprs);
      67             : static List *get_relation_constraints(PlannerInfo *root,
      68             :                                       Oid relationObjectId, RelOptInfo *rel,
      69             :                                       bool include_noinherit,
      70             :                                       bool include_notnull,
      71             :                                       bool include_partition);
      72             : static List *build_index_tlist(PlannerInfo *root, IndexOptInfo *index,
      73             :                                Relation heapRelation);
      74             : static List *get_relation_statistics(RelOptInfo *rel, Relation relation);
      75             : static void set_relation_partition_info(PlannerInfo *root, RelOptInfo *rel,
      76             :                                         Relation relation);
      77             : static PartitionScheme find_partition_scheme(PlannerInfo *root, Relation rel);
      78             : static void set_baserel_partition_key_exprs(Relation relation,
      79             :                                             RelOptInfo *rel);
      80             : static void set_baserel_partition_constraint(Relation relation,
      81             :                                              RelOptInfo *rel);
      82             : 
      83             : 
      84             : /*
      85             :  * get_relation_info -
      86             :  *    Retrieves catalog information for a given relation.
      87             :  *
      88             :  * Given the Oid of the relation, return the following info into fields
      89             :  * of the RelOptInfo struct:
      90             :  *
      91             :  *  min_attr    lowest valid AttrNumber
      92             :  *  max_attr    highest valid AttrNumber
      93             :  *  indexlist   list of IndexOptInfos for relation's indexes
      94             :  *  statlist    list of StatisticExtInfo for relation's statistic objects
      95             :  *  serverid    if it's a foreign table, the server OID
      96             :  *  fdwroutine  if it's a foreign table, the FDW function pointers
      97             :  *  pages       number of pages
      98             :  *  tuples      number of tuples
      99             :  *  rel_parallel_workers user-defined number of parallel workers
     100             :  *
     101             :  * Also, add information about the relation's foreign keys to root->fkey_list.
     102             :  *
     103             :  * Also, initialize the attr_needed[] and attr_widths[] arrays.  In most
     104             :  * cases these are left as zeroes, but sometimes we need to compute attr
     105             :  * widths here, and we may as well cache the results for costsize.c.
     106             :  *
     107             :  * If inhparent is true, all we need to do is set up the attr arrays:
     108             :  * the RelOptInfo actually represents the appendrel formed by an inheritance
     109             :  * tree, and so the parent rel's physical size and index information isn't
     110             :  * important for it.
     111             :  */
     112             : void
     113      282604 : get_relation_info(PlannerInfo *root, Oid relationObjectId, bool inhparent,
     114             :                   RelOptInfo *rel)
     115             : {
     116      282604 :     Index       varno = rel->relid;
     117             :     Relation    relation;
     118             :     bool        hasindex;
     119      282604 :     List       *indexinfos = NIL;
     120             : 
     121             :     /*
     122             :      * We need not lock the relation since it was already locked, either by
     123             :      * the rewriter or when expand_inherited_rtentry() added it to the query's
     124             :      * rangetable.
     125             :      */
     126      282604 :     relation = table_open(relationObjectId, NoLock);
     127             : 
     128             :     /* Temporary and unlogged relations are inaccessible during recovery. */
     129      282604 :     if (!RelationNeedsWAL(relation) && RecoveryInProgress())
     130           0 :         ereport(ERROR,
     131             :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
     132             :                  errmsg("cannot access temporary or unlogged relations during recovery")));
     133             : 
     134      282604 :     rel->min_attr = FirstLowInvalidHeapAttributeNumber + 1;
     135      282604 :     rel->max_attr = RelationGetNumberOfAttributes(relation);
     136      282604 :     rel->reltablespace = RelationGetForm(relation)->reltablespace;
     137             : 
     138             :     Assert(rel->max_attr >= rel->min_attr);
     139      282604 :     rel->attr_needed = (Relids *)
     140      282604 :         palloc0((rel->max_attr - rel->min_attr + 1) * sizeof(Relids));
     141      282604 :     rel->attr_widths = (int32 *)
     142      282604 :         palloc0((rel->max_attr - rel->min_attr + 1) * sizeof(int32));
     143             : 
     144             :     /*
     145             :      * Estimate relation size --- unless it's an inheritance parent, in which
     146             :      * case the size we want is not the rel's own size but the size of its
     147             :      * inheritance tree.  That will be computed in set_append_rel_size().
     148             :      */
     149      282604 :     if (!inhparent)
     150      248276 :         estimate_rel_size(relation, rel->attr_widths - rel->min_attr,
     151             :                           &rel->pages, &rel->tuples, &rel->allvisfrac);
     152             : 
     153             :     /* Retrieve the parallel_workers reloption, or -1 if not set. */
     154      282604 :     rel->rel_parallel_workers = RelationGetParallelWorkers(relation, -1);
     155             : 
     156             :     /*
     157             :      * Make list of indexes.  Ignore indexes on system catalogs if told to.
     158             :      * Don't bother with indexes for an inheritance parent, either.
     159             :      */
     160      282604 :     if (inhparent ||
     161      248276 :         (IgnoreSystemIndexes && IsSystemRelation(relation)))
     162       34328 :         hasindex = false;
     163             :     else
     164      248276 :         hasindex = relation->rd_rel->relhasindex;
     165             : 
     166      282604 :     if (hasindex)
     167             :     {
     168             :         List       *indexoidlist;
     169             :         LOCKMODE    lmode;
     170             :         ListCell   *l;
     171             : 
     172      206790 :         indexoidlist = RelationGetIndexList(relation);
     173             : 
     174             :         /*
     175             :          * For each index, we get the same type of lock that the executor will
     176             :          * need, and do not release it.  This saves a couple of trips to the
     177             :          * shared lock manager while not creating any real loss of
     178             :          * concurrency, because no schema changes could be happening on the
     179             :          * index while we hold lock on the parent rel, and no lock type used
     180             :          * for queries blocks any other kind of index operation.
     181             :          */
     182      206790 :         lmode = root->simple_rte_array[varno]->rellockmode;
     183             : 
     184      623986 :         foreach(l, indexoidlist)
     185             :         {
     186      417196 :             Oid         indexoid = lfirst_oid(l);
     187             :             Relation    indexRelation;
     188             :             Form_pg_index index;
     189             :             IndexAmRoutine *amroutine;
     190             :             IndexOptInfo *info;
     191             :             int         ncolumns,
     192             :                         nkeycolumns;
     193             :             int         i;
     194             : 
     195             :             /*
     196             :              * Extract info from the relation descriptor for the index.
     197             :              */
     198      417196 :             indexRelation = index_open(indexoid, lmode);
     199      417196 :             index = indexRelation->rd_index;
     200             : 
     201             :             /*
     202             :              * Ignore invalid indexes, since they can't safely be used for
     203             :              * queries.  Note that this is OK because the data structure we
     204             :              * are constructing is only used by the planner --- the executor
     205             :              * still needs to insert into "invalid" indexes, if they're marked
     206             :              * indisready.
     207             :              */
     208      417196 :             if (!index->indisvalid)
     209             :             {
     210          18 :                 index_close(indexRelation, NoLock);
     211          18 :                 continue;
     212             :             }
     213             : 
     214             :             /*
     215             :              * Ignore partitioned indexes, since they are not usable for
     216             :              * queries.
     217             :              */
     218      417178 :             if (indexRelation->rd_rel->relkind == RELKIND_PARTITIONED_INDEX)
     219             :             {
     220           4 :                 index_close(indexRelation, NoLock);
     221           4 :                 continue;
     222             :             }
     223             : 
     224             :             /*
     225             :              * If the index is valid, but cannot yet be used, ignore it; but
     226             :              * mark the plan we are generating as transient. See
     227             :              * src/backend/access/heap/README.HOT for discussion.
     228             :              */
     229      417174 :             if (index->indcheckxmin &&
     230          80 :                 !TransactionIdPrecedes(HeapTupleHeaderGetXmin(indexRelation->rd_indextuple->t_data),
     231             :                                        TransactionXmin))
     232             :             {
     233           2 :                 root->glob->transientPlan = true;
     234           2 :                 index_close(indexRelation, NoLock);
     235           2 :                 continue;
     236             :             }
     237             : 
     238      417172 :             info = makeNode(IndexOptInfo);
     239             : 
     240      417172 :             info->indexoid = index->indexrelid;
     241      417172 :             info->reltablespace =
     242      417172 :                 RelationGetForm(indexRelation)->reltablespace;
     243      417172 :             info->rel = rel;
     244      417172 :             info->ncolumns = ncolumns = index->indnatts;
     245      417172 :             info->nkeycolumns = nkeycolumns = index->indnkeyatts;
     246             : 
     247      417172 :             info->indexkeys = (int *) palloc(sizeof(int) * ncolumns);
     248      417172 :             info->indexcollations = (Oid *) palloc(sizeof(Oid) * nkeycolumns);
     249      417172 :             info->opfamily = (Oid *) palloc(sizeof(Oid) * nkeycolumns);
     250      417172 :             info->opcintype = (Oid *) palloc(sizeof(Oid) * nkeycolumns);
     251      417172 :             info->canreturn = (bool *) palloc(sizeof(bool) * ncolumns);
     252             : 
     253     1167690 :             for (i = 0; i < ncolumns; i++)
     254             :             {
     255      750518 :                 info->indexkeys[i] = index->indkey.values[i];
     256      750518 :                 info->canreturn[i] = index_can_return(indexRelation, i + 1);
     257             :             }
     258             : 
     259     1167390 :             for (i = 0; i < nkeycolumns; i++)
     260             :             {
     261      750218 :                 info->opfamily[i] = indexRelation->rd_opfamily[i];
     262      750218 :                 info->opcintype[i] = indexRelation->rd_opcintype[i];
     263      750218 :                 info->indexcollations[i] = indexRelation->rd_indcollation[i];
     264             :             }
     265             : 
     266      417172 :             info->relam = indexRelation->rd_rel->relam;
     267             : 
     268             :             /* We copy just the fields we need, not all of rd_indam */
     269      417172 :             amroutine = indexRelation->rd_indam;
     270      417172 :             info->amcanorderbyop = amroutine->amcanorderbyop;
     271      417172 :             info->amoptionalkey = amroutine->amoptionalkey;
     272      417172 :             info->amsearcharray = amroutine->amsearcharray;
     273      417172 :             info->amsearchnulls = amroutine->amsearchnulls;
     274      417172 :             info->amcanparallel = amroutine->amcanparallel;
     275      417172 :             info->amhasgettuple = (amroutine->amgettuple != NULL);
     276      834344 :             info->amhasgetbitmap = amroutine->amgetbitmap != NULL &&
     277      417172 :                 relation->rd_tableam->scan_bitmap_next_block != NULL;
     278      417172 :             info->amcostestimate = amroutine->amcostestimate;
     279             :             Assert(info->amcostestimate != NULL);
     280             : 
     281             :             /* Fetch index opclass options */
     282      417172 :             info->opclassoptions = RelationGetIndexAttOptions(indexRelation, true);
     283             : 
     284             :             /*
     285             :              * Fetch the ordering information for the index, if any.
     286             :              */
     287      417172 :             if (info->relam == BTREE_AM_OID)
     288             :             {
     289             :                 /*
     290             :                  * If it's a btree index, we can use its opfamily OIDs
     291             :                  * directly as the sort ordering opfamily OIDs.
     292             :                  */
     293             :                 Assert(amroutine->amcanorder);
     294             : 
     295      407924 :                 info->sortopfamily = info->opfamily;
     296      407924 :                 info->reverse_sort = (bool *) palloc(sizeof(bool) * nkeycolumns);
     297      407924 :                 info->nulls_first = (bool *) palloc(sizeof(bool) * nkeycolumns);
     298             : 
     299     1033352 :                 for (i = 0; i < nkeycolumns; i++)
     300             :                 {
     301      625428 :                     int16       opt = indexRelation->rd_indoption[i];
     302             : 
     303      625428 :                     info->reverse_sort[i] = (opt & INDOPTION_DESC) != 0;
     304      625428 :                     info->nulls_first[i] = (opt & INDOPTION_NULLS_FIRST) != 0;
     305             :                 }
     306             :             }
     307        9248 :             else if (amroutine->amcanorder)
     308             :             {
     309             :                 /*
     310             :                  * Otherwise, identify the corresponding btree opfamilies by
     311             :                  * trying to map this index's "<" operators into btree.  Since
     312             :                  * "<" uniquely defines the behavior of a sort order, this is
     313             :                  * a sufficient test.
     314             :                  *
     315             :                  * XXX This method is rather slow and also requires the
     316             :                  * undesirable assumption that the other index AM numbers its
     317             :                  * strategies the same as btree.  It'd be better to have a way
     318             :                  * to explicitly declare the corresponding btree opfamily for
     319             :                  * each opfamily of the other index type.  But given the lack
     320             :                  * of current or foreseeable amcanorder index types, it's not
     321             :                  * worth expending more effort on now.
     322             :                  */
     323           0 :                 info->sortopfamily = (Oid *) palloc(sizeof(Oid) * nkeycolumns);
     324           0 :                 info->reverse_sort = (bool *) palloc(sizeof(bool) * nkeycolumns);
     325           0 :                 info->nulls_first = (bool *) palloc(sizeof(bool) * nkeycolumns);
     326             : 
     327           0 :                 for (i = 0; i < nkeycolumns; i++)
     328             :                 {
     329           0 :                     int16       opt = indexRelation->rd_indoption[i];
     330             :                     Oid         ltopr;
     331             :                     Oid         btopfamily;
     332             :                     Oid         btopcintype;
     333             :                     int16       btstrategy;
     334             : 
     335           0 :                     info->reverse_sort[i] = (opt & INDOPTION_DESC) != 0;
     336           0 :                     info->nulls_first[i] = (opt & INDOPTION_NULLS_FIRST) != 0;
     337             : 
     338           0 :                     ltopr = get_opfamily_member(info->opfamily[i],
     339           0 :                                                 info->opcintype[i],
     340           0 :                                                 info->opcintype[i],
     341             :                                                 BTLessStrategyNumber);
     342           0 :                     if (OidIsValid(ltopr) &&
     343           0 :                         get_ordering_op_properties(ltopr,
     344             :                                                    &btopfamily,
     345             :                                                    &btopcintype,
     346           0 :                                                    &btstrategy) &&
     347           0 :                         btopcintype == info->opcintype[i] &&
     348           0 :                         btstrategy == BTLessStrategyNumber)
     349             :                     {
     350             :                         /* Successful mapping */
     351           0 :                         info->sortopfamily[i] = btopfamily;
     352             :                     }
     353             :                     else
     354             :                     {
     355             :                         /* Fail ... quietly treat index as unordered */
     356           0 :                         info->sortopfamily = NULL;
     357           0 :                         info->reverse_sort = NULL;
     358           0 :                         info->nulls_first = NULL;
     359           0 :                         break;
     360             :                     }
     361             :                 }
     362             :             }
     363             :             else
     364             :             {
     365        9248 :                 info->sortopfamily = NULL;
     366        9248 :                 info->reverse_sort = NULL;
     367        9248 :                 info->nulls_first = NULL;
     368             :             }
     369             : 
     370             :             /*
     371             :              * Fetch the index expressions and predicate, if any.  We must
     372             :              * modify the copies we obtain from the relcache to have the
     373             :              * correct varno for the parent relation, so that they match up
     374             :              * correctly against qual clauses.
     375             :              */
     376      417172 :             info->indexprs = RelationGetIndexExpressions(indexRelation);
     377      417172 :             info->indpred = RelationGetIndexPredicate(indexRelation);
     378      417172 :             if (info->indexprs && varno != 1)
     379        1056 :                 ChangeVarNodes((Node *) info->indexprs, 1, varno, 0);
     380      417172 :             if (info->indpred && varno != 1)
     381          84 :                 ChangeVarNodes((Node *) info->indpred, 1, varno, 0);
     382             : 
     383             :             /* Build targetlist using the completed indexprs data */
     384      417172 :             info->indextlist = build_index_tlist(root, info, relation);
     385             : 
     386      417172 :             info->indrestrictinfo = NIL; /* set later, in indxpath.c */
     387      417172 :             info->predOK = false;    /* set later, in indxpath.c */
     388      417172 :             info->unique = index->indisunique;
     389      417172 :             info->immediate = index->indimmediate;
     390      417172 :             info->hypothetical = false;
     391             : 
     392             :             /*
     393             :              * Estimate the index size.  If it's not a partial index, we lock
     394             :              * the number-of-tuples estimate to equal the parent table; if it
     395             :              * is partial then we have to use the same methods as we would for
     396             :              * a table, except we can be sure that the index is not larger
     397             :              * than the table.
     398             :              */
     399      417172 :             if (info->indpred == NIL)
     400             :             {
     401      416492 :                 info->pages = RelationGetNumberOfBlocks(indexRelation);
     402      416492 :                 info->tuples = rel->tuples;
     403             :             }
     404             :             else
     405             :             {
     406             :                 double      allvisfrac; /* dummy */
     407             : 
     408         680 :                 estimate_rel_size(indexRelation, NULL,
     409             :                                   &info->pages, &info->tuples, &allvisfrac);
     410         680 :                 if (info->tuples > rel->tuples)
     411           0 :                     info->tuples = rel->tuples;
     412             :             }
     413             : 
     414      417172 :             if (info->relam == BTREE_AM_OID)
     415             :             {
     416             :                 /* For btrees, get tree height while we have the index open */
     417      407924 :                 info->tree_height = _bt_getrootheight(indexRelation);
     418             :             }
     419             :             else
     420             :             {
     421             :                 /* For other index types, just set it to "unknown" for now */
     422        9248 :                 info->tree_height = -1;
     423             :             }
     424             : 
     425      417172 :             index_close(indexRelation, NoLock);
     426             : 
     427             :             /*
     428             :              * We've historically used lcons() here.  It'd make more sense to
     429             :              * use lappend(), but that causes the planner to change behavior
     430             :              * in cases where two indexes seem equally attractive.  For now,
     431             :              * stick with lcons() --- few tables should have so many indexes
     432             :              * that the O(N^2) behavior of lcons() is really a problem.
     433             :              */
     434      417172 :             indexinfos = lcons(info, indexinfos);
     435             :         }
     436             : 
     437      206790 :         list_free(indexoidlist);
     438             :     }
     439             : 
     440      282604 :     rel->indexlist = indexinfos;
     441             : 
     442      282604 :     rel->statlist = get_relation_statistics(rel, relation);
     443             : 
     444             :     /* Grab foreign-table info using the relcache, while we have it */
     445      282604 :     if (relation->rd_rel->relkind == RELKIND_FOREIGN_TABLE)
     446             :     {
     447        1846 :         rel->serverid = GetForeignServerIdByRelId(RelationGetRelid(relation));
     448        1846 :         rel->fdwroutine = GetFdwRoutineForRelation(relation, true);
     449             :     }
     450             :     else
     451             :     {
     452      280758 :         rel->serverid = InvalidOid;
     453      280758 :         rel->fdwroutine = NULL;
     454             :     }
     455             : 
     456             :     /* Collect info about relation's foreign keys, if relevant */
     457      282594 :     get_relation_foreign_keys(root, rel, relation, inhparent);
     458             : 
     459             :     /*
     460             :      * Collect info about relation's partitioning scheme, if any. Only
     461             :      * inheritance parents may be partitioned.
     462             :      */
     463      282594 :     if (inhparent && relation->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
     464        9760 :         set_relation_partition_info(root, rel, relation);
     465             : 
     466      282594 :     table_close(relation, NoLock);
     467             : 
     468             :     /*
     469             :      * Allow a plugin to editorialize on the info we obtained from the
     470             :      * catalogs.  Actions might include altering the assumed relation size,
     471             :      * removing an index, or adding a hypothetical index to the indexlist.
     472             :      */
     473      282594 :     if (get_relation_info_hook)
     474           0 :         (*get_relation_info_hook) (root, relationObjectId, inhparent, rel);
     475      282594 : }
     476             : 
     477             : /*
     478             :  * get_relation_foreign_keys -
     479             :  *    Retrieves foreign key information for a given relation.
     480             :  *
     481             :  * ForeignKeyOptInfos for relevant foreign keys are created and added to
     482             :  * root->fkey_list.  We do this now while we have the relcache entry open.
     483             :  * We could sometimes avoid making useless ForeignKeyOptInfos if we waited
     484             :  * until all RelOptInfos have been built, but the cost of re-opening the
     485             :  * relcache entries would probably exceed any savings.
     486             :  */
     487             : static void
     488      282594 : get_relation_foreign_keys(PlannerInfo *root, RelOptInfo *rel,
     489             :                           Relation relation, bool inhparent)
     490             : {
     491      282594 :     List       *rtable = root->parse->rtable;
     492             :     List       *cachedfkeys;
     493             :     ListCell   *lc;
     494             : 
     495             :     /*
     496             :      * If it's not a baserel, we don't care about its FKs.  Also, if the query
     497             :      * references only a single relation, we can skip the lookup since no FKs
     498             :      * could satisfy the requirements below.
     499             :      */
     500      540888 :     if (rel->reloptkind != RELOPT_BASEREL ||
     501      258294 :         list_length(rtable) < 2)
     502      151826 :         return;
     503             : 
     504             :     /*
     505             :      * If it's the parent of an inheritance tree, ignore its FKs.  We could
     506             :      * make useful FK-based deductions if we found that all members of the
     507             :      * inheritance tree have equivalent FK constraints, but detecting that
     508             :      * would require code that hasn't been written.
     509             :      */
     510      130768 :     if (inhparent)
     511        2866 :         return;
     512             : 
     513             :     /*
     514             :      * Extract data about relation's FKs from the relcache.  Note that this
     515             :      * list belongs to the relcache and might disappear in a cache flush, so
     516             :      * we must not do any further catalog access within this function.
     517             :      */
     518      127902 :     cachedfkeys = RelationGetFKeyList(relation);
     519             : 
     520             :     /*
     521             :      * Figure out which FKs are of interest for this query, and create
     522             :      * ForeignKeyOptInfos for them.  We want only FKs that reference some
     523             :      * other RTE of the current query.  In queries containing self-joins,
     524             :      * there might be more than one other RTE for a referenced table, and we
     525             :      * should make a ForeignKeyOptInfo for each occurrence.
     526             :      *
     527             :      * Ideally, we would ignore RTEs that correspond to non-baserels, but it's
     528             :      * too hard to identify those here, so we might end up making some useless
     529             :      * ForeignKeyOptInfos.  If so, match_foreign_keys_to_quals() will remove
     530             :      * them again.
     531             :      */
     532      129564 :     foreach(lc, cachedfkeys)
     533             :     {
     534        1662 :         ForeignKeyCacheInfo *cachedfk = (ForeignKeyCacheInfo *) lfirst(lc);
     535             :         Index       rti;
     536             :         ListCell   *lc2;
     537             : 
     538             :         /* conrelid should always be that of the table we're considering */
     539             :         Assert(cachedfk->conrelid == RelationGetRelid(relation));
     540             : 
     541             :         /* Scan to find other RTEs matching confrelid */
     542        1662 :         rti = 0;
     543        7520 :         foreach(lc2, rtable)
     544             :         {
     545        5858 :             RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc2);
     546             :             ForeignKeyOptInfo *info;
     547             : 
     548        5858 :             rti++;
     549             :             /* Ignore if not the correct table */
     550        5858 :             if (rte->rtekind != RTE_RELATION ||
     551        4152 :                 rte->relid != cachedfk->confrelid)
     552        4612 :                 continue;
     553             :             /* Ignore if it's an inheritance parent; doesn't really match */
     554        1246 :             if (rte->inh)
     555         102 :                 continue;
     556             :             /* Ignore self-referential FKs; we only care about joins */
     557        1144 :             if (rti == rel->relid)
     558          40 :                 continue;
     559             : 
     560             :             /* OK, let's make an entry */
     561        1104 :             info = makeNode(ForeignKeyOptInfo);
     562        1104 :             info->con_relid = rel->relid;
     563        1104 :             info->ref_relid = rti;
     564        1104 :             info->nkeys = cachedfk->nkeys;
     565        1104 :             memcpy(info->conkey, cachedfk->conkey, sizeof(info->conkey));
     566        1104 :             memcpy(info->confkey, cachedfk->confkey, sizeof(info->confkey));
     567        1104 :             memcpy(info->conpfeqop, cachedfk->conpfeqop, sizeof(info->conpfeqop));
     568             :             /* zero out fields to be filled by match_foreign_keys_to_quals */
     569        1104 :             info->nmatched_ec = 0;
     570        1104 :             info->nmatched_rcols = 0;
     571        1104 :             info->nmatched_ri = 0;
     572        1104 :             memset(info->eclass, 0, sizeof(info->eclass));
     573        1104 :             memset(info->rinfos, 0, sizeof(info->rinfos));
     574             : 
     575        1104 :             root->fkey_list = lappend(root->fkey_list, info);
     576             :         }
     577             :     }
     578             : }
     579             : 
     580             : /*
     581             :  * infer_arbiter_indexes -
     582             :  *    Determine the unique indexes used to arbitrate speculative insertion.
     583             :  *
     584             :  * Uses user-supplied inference clause expressions and predicate to match a
     585             :  * unique index from those defined and ready on the heap relation (target).
     586             :  * An exact match is required on columns/expressions (although they can appear
     587             :  * in any order).  However, the predicate given by the user need only restrict
     588             :  * insertion to a subset of some part of the table covered by some particular
     589             :  * unique index (in particular, a partial unique index) in order to be
     590             :  * inferred.
     591             :  *
     592             :  * The implementation does not consider which B-Tree operator class any
     593             :  * particular available unique index attribute uses, unless one was specified
     594             :  * in the inference specification. The same is true of collations.  In
     595             :  * particular, there is no system dependency on the default operator class for
     596             :  * the purposes of inference.  If no opclass (or collation) is specified, then
     597             :  * all matching indexes (that may or may not match the default in terms of
     598             :  * each attribute opclass/collation) are used for inference.
     599             :  */
     600             : List *
     601         984 : infer_arbiter_indexes(PlannerInfo *root)
     602             : {
     603         984 :     OnConflictExpr *onconflict = root->parse->onConflict;
     604             : 
     605             :     /* Iteration state */
     606             :     RangeTblEntry *rte;
     607             :     Relation    relation;
     608         984 :     Oid         indexOidFromConstraint = InvalidOid;
     609             :     List       *indexList;
     610             :     ListCell   *l;
     611             : 
     612             :     /* Normalized inference attributes and inference expressions: */
     613         984 :     Bitmapset  *inferAttrs = NULL;
     614         984 :     List       *inferElems = NIL;
     615             : 
     616             :     /* Results */
     617         984 :     List       *results = NIL;
     618             : 
     619             :     /*
     620             :      * Quickly return NIL for ON CONFLICT DO NOTHING without an inference
     621             :      * specification or named constraint.  ON CONFLICT DO UPDATE statements
     622             :      * must always provide one or the other (but parser ought to have caught
     623             :      * that already).
     624             :      */
     625         984 :     if (onconflict->arbiterElems == NIL &&
     626         156 :         onconflict->constraint == InvalidOid)
     627         124 :         return NIL;
     628             : 
     629             :     /*
     630             :      * We need not lock the relation since it was already locked, either by
     631             :      * the rewriter or when expand_inherited_rtentry() added it to the query's
     632             :      * rangetable.
     633             :      */
     634         860 :     rte = rt_fetch(root->parse->resultRelation, root->parse->rtable);
     635             : 
     636         860 :     relation = table_open(rte->relid, NoLock);
     637             : 
     638             :     /*
     639             :      * Build normalized/BMS representation of plain indexed attributes, as
     640             :      * well as a separate list of expression items.  This simplifies matching
     641             :      * the cataloged definition of indexes.
     642             :      */
     643        1884 :     foreach(l, onconflict->arbiterElems)
     644             :     {
     645        1024 :         InferenceElem *elem = (InferenceElem *) lfirst(l);
     646             :         Var        *var;
     647             :         int         attno;
     648             : 
     649        1024 :         if (!IsA(elem->expr, Var))
     650             :         {
     651             :             /* If not a plain Var, just shove it in inferElems for now */
     652         116 :             inferElems = lappend(inferElems, elem->expr);
     653         116 :             continue;
     654             :         }
     655             : 
     656         908 :         var = (Var *) elem->expr;
     657         908 :         attno = var->varattno;
     658             : 
     659         908 :         if (attno == 0)
     660           0 :             ereport(ERROR,
     661             :                     (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
     662             :                      errmsg("whole row unique index inference specifications are not supported")));
     663             : 
     664         908 :         inferAttrs = bms_add_member(inferAttrs,
     665             :                                     attno - FirstLowInvalidHeapAttributeNumber);
     666             :     }
     667             : 
     668             :     /*
     669             :      * Lookup named constraint's index.  This is not immediately returned
     670             :      * because some additional sanity checks are required.
     671             :      */
     672         860 :     if (onconflict->constraint != InvalidOid)
     673             :     {
     674          32 :         indexOidFromConstraint = get_constraint_index(onconflict->constraint);
     675             : 
     676          32 :         if (indexOidFromConstraint == InvalidOid)
     677           0 :             ereport(ERROR,
     678             :                     (errcode(ERRCODE_WRONG_OBJECT_TYPE),
     679             :                      errmsg("constraint in ON CONFLICT clause has no associated index")));
     680             :     }
     681             : 
     682             :     /*
     683             :      * Using that representation, iterate through the list of indexes on the
     684             :      * target relation to try and find a match
     685             :      */
     686         860 :     indexList = RelationGetIndexList(relation);
     687             : 
     688        1960 :     foreach(l, indexList)
     689             :     {
     690        1132 :         Oid         indexoid = lfirst_oid(l);
     691             :         Relation    idxRel;
     692             :         Form_pg_index idxForm;
     693             :         Bitmapset  *indexedAttrs;
     694             :         List       *idxExprs;
     695             :         List       *predExprs;
     696             :         AttrNumber  natt;
     697             :         ListCell   *el;
     698             : 
     699             :         /*
     700             :          * Extract info from the relation descriptor for the index.  Obtain
     701             :          * the same lock type that the executor will ultimately use.
     702             :          *
     703             :          * Let executor complain about !indimmediate case directly, because
     704             :          * enforcement needs to occur there anyway when an inference clause is
     705             :          * omitted.
     706             :          */
     707        1132 :         idxRel = index_open(indexoid, rte->rellockmode);
     708        1132 :         idxForm = idxRel->rd_index;
     709             : 
     710        1132 :         if (!idxForm->indisvalid)
     711           4 :             goto next;
     712             : 
     713             :         /*
     714             :          * Note that we do not perform a check against indcheckxmin (like e.g.
     715             :          * get_relation_info()) here to eliminate candidates, because
     716             :          * uniqueness checking only cares about the most recently committed
     717             :          * tuple versions.
     718             :          */
     719             : 
     720             :         /*
     721             :          * Look for match on "ON constraint_name" variant, which may not be
     722             :          * unique constraint.  This can only be a constraint name.
     723             :          */
     724        1128 :         if (indexOidFromConstraint == idxForm->indexrelid)
     725             :         {
     726          32 :             if (!idxForm->indisunique && onconflict->action == ONCONFLICT_UPDATE)
     727           4 :                 ereport(ERROR,
     728             :                         (errcode(ERRCODE_WRONG_OBJECT_TYPE),
     729             :                          errmsg("ON CONFLICT DO UPDATE not supported with exclusion constraints")));
     730             : 
     731          28 :             results = lappend_oid(results, idxForm->indexrelid);
     732          28 :             list_free(indexList);
     733          28 :             index_close(idxRel, NoLock);
     734          28 :             table_close(relation, NoLock);
     735          28 :             return results;
     736             :         }
     737        1096 :         else if (indexOidFromConstraint != InvalidOid)
     738             :         {
     739             :             /* No point in further work for index in named constraint case */
     740          12 :             goto next;
     741             :         }
     742             : 
     743             :         /*
     744             :          * Only considering conventional inference at this point (not named
     745             :          * constraints), so index under consideration can be immediately
     746             :          * skipped if it's not unique
     747             :          */
     748        1084 :         if (!idxForm->indisunique)
     749           4 :             goto next;
     750             : 
     751             :         /* Build BMS representation of plain (non expression) index attrs */
     752        1080 :         indexedAttrs = NULL;
     753        2520 :         for (natt = 0; natt < idxForm->indnkeyatts; natt++)
     754             :         {
     755        1440 :             int         attno = idxRel->rd_index->indkey.values[natt];
     756             : 
     757        1440 :             if (attno != 0)
     758        1228 :                 indexedAttrs = bms_add_member(indexedAttrs,
     759             :                                               attno - FirstLowInvalidHeapAttributeNumber);
     760             :         }
     761             : 
     762             :         /* Non-expression attributes (if any) must match */
     763        1080 :         if (!bms_equal(indexedAttrs, inferAttrs))
     764         240 :             goto next;
     765             : 
     766             :         /* Expression attributes (if any) must match */
     767         840 :         idxExprs = RelationGetIndexExpressions(idxRel);
     768        1908 :         foreach(el, onconflict->arbiterElems)
     769             :         {
     770        1100 :             InferenceElem *elem = (InferenceElem *) lfirst(el);
     771             : 
     772             :             /*
     773             :              * Ensure that collation/opclass aspects of inference expression
     774             :              * element match.  Even though this loop is primarily concerned
     775             :              * with matching expressions, it is a convenient point to check
     776             :              * this for both expressions and ordinary (non-expression)
     777             :              * attributes appearing as inference elements.
     778             :              */
     779        1100 :             if (!infer_collation_opclass_match(elem, idxRel, idxExprs))
     780          32 :                 goto next;
     781             : 
     782             :             /*
     783             :              * Plain Vars don't factor into count of expression elements, and
     784             :              * the question of whether or not they satisfy the index
     785             :              * definition has already been considered (they must).
     786             :              */
     787        1076 :             if (IsA(elem->expr, Var))
     788         960 :                 continue;
     789             : 
     790             :             /*
     791             :              * Might as well avoid redundant check in the rare cases where
     792             :              * infer_collation_opclass_match() is required to do real work.
     793             :              * Otherwise, check that element expression appears in cataloged
     794             :              * index definition.
     795             :              */
     796         116 :             if (elem->infercollid != InvalidOid ||
     797         204 :                 elem->inferopclass != InvalidOid ||
     798         100 :                 list_member(idxExprs, elem->expr))
     799         108 :                 continue;
     800             : 
     801           8 :             goto next;
     802             :         }
     803             : 
     804             :         /*
     805             :          * Now that all inference elements were matched, ensure that the
     806             :          * expression elements from inference clause are not missing any
     807             :          * cataloged expressions.  This does the right thing when unique
     808             :          * indexes redundantly repeat the same attribute, or if attributes
     809             :          * redundantly appear multiple times within an inference clause.
     810             :          */
     811         808 :         if (list_difference(idxExprs, inferElems) != NIL)
     812          36 :             goto next;
     813             : 
     814             :         /*
     815             :          * If it's a partial index, its predicate must be implied by the ON
     816             :          * CONFLICT's WHERE clause.
     817             :          */
     818         772 :         predExprs = RelationGetIndexPredicate(idxRel);
     819             : 
     820         772 :         if (!predicate_implied_by(predExprs, (List *) onconflict->arbiterWhere, false))
     821          24 :             goto next;
     822             : 
     823         748 :         results = lappend_oid(results, idxForm->indexrelid);
     824        1100 : next:
     825        1100 :         index_close(idxRel, NoLock);
     826             :     }
     827             : 
     828         828 :     list_free(indexList);
     829         828 :     table_close(relation, NoLock);
     830             : 
     831         828 :     if (results == NIL)
     832         116 :         ereport(ERROR,
     833             :                 (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
     834             :                  errmsg("there is no unique or exclusion constraint matching the ON CONFLICT specification")));
     835             : 
     836         712 :     return results;
     837             : }
     838             : 
     839             : /*
     840             :  * infer_collation_opclass_match - ensure infer element opclass/collation match
     841             :  *
     842             :  * Given unique index inference element from inference specification, if
     843             :  * collation was specified, or if opclass was specified, verify that there is
     844             :  * at least one matching indexed attribute (occasionally, there may be more).
     845             :  * Skip this in the common case where inference specification does not include
     846             :  * collation or opclass (instead matching everything, regardless of cataloged
     847             :  * collation/opclass of indexed attribute).
     848             :  *
     849             :  * At least historically, Postgres has not offered collations or opclasses
     850             :  * with alternative-to-default notions of equality, so these additional
     851             :  * criteria should only be required infrequently.
     852             :  *
     853             :  * Don't give up immediately when an inference element matches some attribute
     854             :  * cataloged as indexed but not matching additional opclass/collation
     855             :  * criteria.  This is done so that the implementation is as forgiving as
     856             :  * possible of redundancy within cataloged index attributes (or, less
     857             :  * usefully, within inference specification elements).  If collations actually
     858             :  * differ between apparently redundantly indexed attributes (redundant within
     859             :  * or across indexes), then there really is no redundancy as such.
     860             :  *
     861             :  * Note that if an inference element specifies an opclass and a collation at
     862             :  * once, both must match in at least one particular attribute within index
     863             :  * catalog definition in order for that inference element to be considered
     864             :  * inferred/satisfied.
     865             :  */
     866             : static bool
     867        1100 : infer_collation_opclass_match(InferenceElem *elem, Relation idxRel,
     868             :                               List *idxExprs)
     869             : {
     870             :     AttrNumber  natt;
     871        1100 :     Oid         inferopfamily = InvalidOid; /* OID of opclass opfamily */
     872        1100 :     Oid         inferopcinputtype = InvalidOid; /* OID of opclass input type */
     873        1100 :     int         nplain = 0;     /* # plain attrs observed */
     874             : 
     875             :     /*
     876             :      * If inference specification element lacks collation/opclass, then no
     877             :      * need to check for exact match.
     878             :      */
     879        1100 :     if (elem->infercollid == InvalidOid && elem->inferopclass == InvalidOid)
     880        1024 :         return true;
     881             : 
     882             :     /*
     883             :      * Lookup opfamily and input type, for matching indexes
     884             :      */
     885          76 :     if (elem->inferopclass)
     886             :     {
     887          56 :         inferopfamily = get_opclass_family(elem->inferopclass);
     888          56 :         inferopcinputtype = get_opclass_input_type(elem->inferopclass);
     889             :     }
     890             : 
     891         164 :     for (natt = 1; natt <= idxRel->rd_att->natts; natt++)
     892             :     {
     893         140 :         Oid         opfamily = idxRel->rd_opfamily[natt - 1];
     894         140 :         Oid         opcinputtype = idxRel->rd_opcintype[natt - 1];
     895         140 :         Oid         collation = idxRel->rd_indcollation[natt - 1];
     896         140 :         int         attno = idxRel->rd_index->indkey.values[natt - 1];
     897             : 
     898         140 :         if (attno != 0)
     899         112 :             nplain++;
     900             : 
     901         140 :         if (elem->inferopclass != InvalidOid &&
     902          44 :             (inferopfamily != opfamily || inferopcinputtype != opcinputtype))
     903             :         {
     904             :             /* Attribute needed to match opclass, but didn't */
     905          60 :             continue;
     906             :         }
     907             : 
     908          80 :         if (elem->infercollid != InvalidOid &&
     909          56 :             elem->infercollid != collation)
     910             :         {
     911             :             /* Attribute needed to match collation, but didn't */
     912          24 :             continue;
     913             :         }
     914             : 
     915             :         /* If one matching index att found, good enough -- return true */
     916          56 :         if (IsA(elem->expr, Var))
     917             :         {
     918          36 :             if (((Var *) elem->expr)->varattno == attno)
     919          36 :                 return true;
     920             :         }
     921          20 :         else if (attno == 0)
     922             :         {
     923          20 :             Node       *nattExpr = list_nth(idxExprs, (natt - 1) - nplain);
     924             : 
     925             :             /*
     926             :              * Note that unlike routines like match_index_to_operand() we
     927             :              * don't need to care about RelabelType.  Neither the index
     928             :              * definition nor the inference clause should contain them.
     929             :              */
     930          20 :             if (equal(elem->expr, nattExpr))
     931          16 :                 return true;
     932             :         }
     933             :     }
     934             : 
     935          24 :     return false;
     936             : }
     937             : 
     938             : /*
     939             :  * estimate_rel_size - estimate # pages and # tuples in a table or index
     940             :  *
     941             :  * We also estimate the fraction of the pages that are marked all-visible in
     942             :  * the visibility map, for use in estimation of index-only scans.
     943             :  *
     944             :  * If attr_widths isn't NULL, it points to the zero-index entry of the
     945             :  * relation's attr_widths[] cache; we fill this in if we have need to compute
     946             :  * the attribute widths for estimation purposes.
     947             :  */
     948             : void
     949      270820 : estimate_rel_size(Relation rel, int32 *attr_widths,
     950             :                   BlockNumber *pages, double *tuples, double *allvisfrac)
     951             : {
     952             :     BlockNumber curpages;
     953             :     BlockNumber relpages;
     954             :     double      reltuples;
     955             :     BlockNumber relallvisible;
     956             :     double      density;
     957             : 
     958      270820 :     switch (rel->rd_rel->relkind)
     959             :     {
     960      267848 :         case RELKIND_RELATION:
     961             :         case RELKIND_MATVIEW:
     962             :         case RELKIND_TOASTVALUE:
     963      267848 :             table_relation_estimate_size(rel, attr_widths, pages, tuples,
     964             :                                          allvisfrac);
     965      267848 :             break;
     966             : 
     967         680 :         case RELKIND_INDEX:
     968             : 
     969             :             /*
     970             :              * XXX: It'd probably be good to move this into a callback,
     971             :              * individual index types e.g. know if they have a metapage.
     972             :              */
     973             : 
     974             :             /* it has storage, ok to call the smgr */
     975         680 :             curpages = RelationGetNumberOfBlocks(rel);
     976             : 
     977             :             /* coerce values in pg_class to more desirable types */
     978         680 :             relpages = (BlockNumber) rel->rd_rel->relpages;
     979         680 :             reltuples = (double) rel->rd_rel->reltuples;
     980         680 :             relallvisible = (BlockNumber) rel->rd_rel->relallvisible;
     981             : 
     982             :             /* report estimated # pages */
     983         680 :             *pages = curpages;
     984             :             /* quick exit if rel is clearly empty */
     985         680 :             if (curpages == 0)
     986             :             {
     987           0 :                 *tuples = 0;
     988           0 :                 *allvisfrac = 0;
     989           0 :                 break;
     990             :             }
     991             :             /* coerce values in pg_class to more desirable types */
     992         680 :             relpages = (BlockNumber) rel->rd_rel->relpages;
     993         680 :             reltuples = (double) rel->rd_rel->reltuples;
     994         680 :             relallvisible = (BlockNumber) rel->rd_rel->relallvisible;
     995             : 
     996             :             /*
     997             :              * Discount the metapage while estimating the number of tuples.
     998             :              * This is a kluge because it assumes more than it ought to about
     999             :              * index structure.  Currently it's OK for btree, hash, and GIN
    1000             :              * indexes but suspect for GiST indexes.
    1001             :              */
    1002         680 :             if (relpages > 0)
    1003             :             {
    1004         680 :                 curpages--;
    1005         680 :                 relpages--;
    1006             :             }
    1007             : 
    1008             :             /* estimate number of tuples from previous tuple density */
    1009         680 :             if (relpages > 0)
    1010         412 :                 density = reltuples / (double) relpages;
    1011             :             else
    1012             :             {
    1013             :                 /*
    1014             :                  * When we have no data because the relation was truncated,
    1015             :                  * estimate tuple width from attribute datatypes.  We assume
    1016             :                  * here that the pages are completely full, which is OK for
    1017             :                  * tables (since they've presumably not been VACUUMed yet) but
    1018             :                  * is probably an overestimate for indexes.  Fortunately
    1019             :                  * get_relation_info() can clamp the overestimate to the
    1020             :                  * parent table's size.
    1021             :                  *
    1022             :                  * Note: this code intentionally disregards alignment
    1023             :                  * considerations, because (a) that would be gilding the lily
    1024             :                  * considering how crude the estimate is, and (b) it creates
    1025             :                  * platform dependencies in the default plans which are kind
    1026             :                  * of a headache for regression testing.
    1027             :                  *
    1028             :                  * XXX: Should this logic be more index specific?
    1029             :                  */
    1030             :                 int32       tuple_width;
    1031             : 
    1032         268 :                 tuple_width = get_rel_data_width(rel, attr_widths);
    1033         268 :                 tuple_width += MAXALIGN(SizeofHeapTupleHeader);
    1034         268 :                 tuple_width += sizeof(ItemIdData);
    1035             :                 /* note: integer division is intentional here */
    1036         268 :                 density = (BLCKSZ - SizeOfPageHeaderData) / tuple_width;
    1037             :             }
    1038         680 :             *tuples = rint(density * (double) curpages);
    1039             : 
    1040             :             /*
    1041             :              * We use relallvisible as-is, rather than scaling it up like we
    1042             :              * do for the pages and tuples counts, on the theory that any
    1043             :              * pages added since the last VACUUM are most likely not marked
    1044             :              * all-visible.  But costsize.c wants it converted to a fraction.
    1045             :              */
    1046         680 :             if (relallvisible == 0 || curpages <= 0)
    1047         680 :                 *allvisfrac = 0;
    1048           0 :             else if ((double) relallvisible >= curpages)
    1049           0 :                 *allvisfrac = 1;
    1050             :             else
    1051           0 :                 *allvisfrac = (double) relallvisible / curpages;
    1052         680 :             break;
    1053             : 
    1054         416 :         case RELKIND_SEQUENCE:
    1055             :             /* Sequences always have a known size */
    1056         416 :             *pages = 1;
    1057         416 :             *tuples = 1;
    1058         416 :             *allvisfrac = 0;
    1059         416 :             break;
    1060        1846 :         case RELKIND_FOREIGN_TABLE:
    1061             :             /* Just use whatever's in pg_class */
    1062        1846 :             *pages = rel->rd_rel->relpages;
    1063        1846 :             *tuples = rel->rd_rel->reltuples;
    1064        1846 :             *allvisfrac = 0;
    1065        1846 :             break;
    1066          30 :         default:
    1067             :             /* else it has no disk storage; probably shouldn't get here? */
    1068          30 :             *pages = 0;
    1069          30 :             *tuples = 0;
    1070          30 :             *allvisfrac = 0;
    1071          30 :             break;
    1072             :     }
    1073      270820 : }
    1074             : 
    1075             : 
    1076             : /*
    1077             :  * get_rel_data_width
    1078             :  *
    1079             :  * Estimate the average width of (the data part of) the relation's tuples.
    1080             :  *
    1081             :  * If attr_widths isn't NULL, it points to the zero-index entry of the
    1082             :  * relation's attr_widths[] cache; use and update that cache as appropriate.
    1083             :  *
    1084             :  * Currently we ignore dropped columns.  Ideally those should be included
    1085             :  * in the result, but we haven't got any way to get info about them; and
    1086             :  * since they might be mostly NULLs, treating them as zero-width is not
    1087             :  * necessarily the wrong thing anyway.
    1088             :  */
    1089             : int32
    1090      103042 : get_rel_data_width(Relation rel, int32 *attr_widths)
    1091             : {
    1092      103042 :     int32       tuple_width = 0;
    1093             :     int         i;
    1094             : 
    1095      491230 :     for (i = 1; i <= RelationGetNumberOfAttributes(rel); i++)
    1096             :     {
    1097      388188 :         Form_pg_attribute att = TupleDescAttr(rel->rd_att, i - 1);
    1098             :         int32       item_width;
    1099             : 
    1100      388188 :         if (att->attisdropped)
    1101        1648 :             continue;
    1102             : 
    1103             :         /* use previously cached data, if any */
    1104      386540 :         if (attr_widths != NULL && attr_widths[i] > 0)
    1105             :         {
    1106        4152 :             tuple_width += attr_widths[i];
    1107        4152 :             continue;
    1108             :         }
    1109             : 
    1110             :         /* This should match set_rel_width() in costsize.c */
    1111      382388 :         item_width = get_attavgwidth(RelationGetRelid(rel), i);
    1112      382388 :         if (item_width <= 0)
    1113             :         {
    1114      381518 :             item_width = get_typavgwidth(att->atttypid, att->atttypmod);
    1115             :             Assert(item_width > 0);
    1116             :         }
    1117      382388 :         if (attr_widths != NULL)
    1118      325976 :             attr_widths[i] = item_width;
    1119      382388 :         tuple_width += item_width;
    1120             :     }
    1121             : 
    1122      103042 :     return tuple_width;
    1123             : }
    1124             : 
    1125             : /*
    1126             :  * get_relation_data_width
    1127             :  *
    1128             :  * External API for get_rel_data_width: same behavior except we have to
    1129             :  * open the relcache entry.
    1130             :  */
    1131             : int32
    1132        1382 : get_relation_data_width(Oid relid, int32 *attr_widths)
    1133             : {
    1134             :     int32       result;
    1135             :     Relation    relation;
    1136             : 
    1137             :     /* As above, assume relation is already locked */
    1138        1382 :     relation = table_open(relid, NoLock);
    1139             : 
    1140        1382 :     result = get_rel_data_width(relation, attr_widths);
    1141             : 
    1142        1382 :     table_close(relation, NoLock);
    1143             : 
    1144        1382 :     return result;
    1145             : }
    1146             : 
    1147             : 
    1148             : /*
    1149             :  * get_relation_constraints
    1150             :  *
    1151             :  * Retrieve the applicable constraint expressions of the given relation.
    1152             :  *
    1153             :  * Returns a List (possibly empty) of constraint expressions.  Each one
    1154             :  * has been canonicalized, and its Vars are changed to have the varno
    1155             :  * indicated by rel->relid.  This allows the expressions to be easily
    1156             :  * compared to expressions taken from WHERE.
    1157             :  *
    1158             :  * If include_noinherit is true, it's okay to include constraints that
    1159             :  * are marked NO INHERIT.
    1160             :  *
    1161             :  * If include_notnull is true, "col IS NOT NULL" expressions are generated
    1162             :  * and added to the result for each column that's marked attnotnull.
    1163             :  *
    1164             :  * If include_partition is true, and the relation is a partition,
    1165             :  * also include the partitioning constraints.
    1166             :  *
    1167             :  * Note: at present this is invoked at most once per relation per planner
    1168             :  * run, and in many cases it won't be invoked at all, so there seems no
    1169             :  * point in caching the data in RelOptInfo.
    1170             :  */
    1171             : static List *
    1172       14876 : get_relation_constraints(PlannerInfo *root,
    1173             :                          Oid relationObjectId, RelOptInfo *rel,
    1174             :                          bool include_noinherit,
    1175             :                          bool include_notnull,
    1176             :                          bool include_partition)
    1177             : {
    1178       14876 :     List       *result = NIL;
    1179       14876 :     Index       varno = rel->relid;
    1180             :     Relation    relation;
    1181             :     TupleConstr *constr;
    1182             : 
    1183             :     /*
    1184             :      * We assume the relation has already been safely locked.
    1185             :      */
    1186       14876 :     relation = table_open(relationObjectId, NoLock);
    1187             : 
    1188       14876 :     constr = relation->rd_att->constr;
    1189       14876 :     if (constr != NULL)
    1190             :     {
    1191        6340 :         int         num_check = constr->num_check;
    1192             :         int         i;
    1193             : 
    1194        6796 :         for (i = 0; i < num_check; i++)
    1195             :         {
    1196             :             Node       *cexpr;
    1197             : 
    1198             :             /*
    1199             :              * If this constraint hasn't been fully validated yet, we must
    1200             :              * ignore it here.  Also ignore if NO INHERIT and we weren't told
    1201             :              * that that's safe.
    1202             :              */
    1203         456 :             if (!constr->check[i].ccvalid)
    1204          28 :                 continue;
    1205         428 :             if (constr->check[i].ccnoinherit && !include_noinherit)
    1206           0 :                 continue;
    1207             : 
    1208         428 :             cexpr = stringToNode(constr->check[i].ccbin);
    1209             : 
    1210             :             /*
    1211             :              * Run each expression through const-simplification and
    1212             :              * canonicalization.  This is not just an optimization, but is
    1213             :              * necessary, because we will be comparing it to
    1214             :              * similarly-processed qual clauses, and may fail to detect valid
    1215             :              * matches without this.  This must match the processing done to
    1216             :              * qual clauses in preprocess_expression()!  (We can skip the
    1217             :              * stuff involving subqueries, however, since we don't allow any
    1218             :              * in check constraints.)
    1219             :              */
    1220         428 :             cexpr = eval_const_expressions(root, cexpr);
    1221             : 
    1222         428 :             cexpr = (Node *) canonicalize_qual((Expr *) cexpr, true);
    1223             : 
    1224             :             /* Fix Vars to have the desired varno */
    1225         428 :             if (varno != 1)
    1226         416 :                 ChangeVarNodes(cexpr, 1, varno, 0);
    1227             : 
    1228             :             /*
    1229             :              * Finally, convert to implicit-AND format (that is, a List) and
    1230             :              * append the resulting item(s) to our output list.
    1231             :              */
    1232         428 :             result = list_concat(result,
    1233         428 :                                  make_ands_implicit((Expr *) cexpr));
    1234             :         }
    1235             : 
    1236             :         /* Add NOT NULL constraints in expression form, if requested */
    1237        6340 :         if (include_notnull && constr->has_not_null)
    1238             :         {
    1239        6024 :             int         natts = relation->rd_att->natts;
    1240             : 
    1241       25734 :             for (i = 1; i <= natts; i++)
    1242             :             {
    1243       19710 :                 Form_pg_attribute att = TupleDescAttr(relation->rd_att, i - 1);
    1244             : 
    1245       19710 :                 if (att->attnotnull && !att->attisdropped)
    1246             :                 {
    1247        7020 :                     NullTest   *ntest = makeNode(NullTest);
    1248             : 
    1249        7020 :                     ntest->arg = (Expr *) makeVar(varno,
    1250             :                                                   i,
    1251             :                                                   att->atttypid,
    1252             :                                                   att->atttypmod,
    1253             :                                                   att->attcollation,
    1254             :                                                   0);
    1255        7020 :                     ntest->nulltesttype = IS_NOT_NULL;
    1256             : 
    1257             :                     /*
    1258             :                      * argisrow=false is correct even for a composite column,
    1259             :                      * because attnotnull does not represent a SQL-spec IS NOT
    1260             :                      * NULL test in such a case, just IS DISTINCT FROM NULL.
    1261             :                      */
    1262        7020 :                     ntest->argisrow = false;
    1263        7020 :                     ntest->location = -1;
    1264        7020 :                     result = lappend(result, ntest);
    1265             :                 }
    1266             :             }
    1267             :         }
    1268             :     }
    1269             : 
    1270             :     /*
    1271             :      * Add partitioning constraints, if requested.
    1272             :      */
    1273       14876 :     if (include_partition && relation->rd_rel->relispartition)
    1274             :     {
    1275             :         /* make sure rel->partition_qual is set */
    1276           8 :         set_baserel_partition_constraint(relation, rel);
    1277           8 :         result = list_concat(result, rel->partition_qual);
    1278             :     }
    1279             : 
    1280       14876 :     table_close(relation, NoLock);
    1281             : 
    1282       14876 :     return result;
    1283             : }
    1284             : 
    1285             : /*
    1286             :  * get_relation_statistics
    1287             :  *      Retrieve extended statistics defined on the table.
    1288             :  *
    1289             :  * Returns a List (possibly empty) of StatisticExtInfo objects describing
    1290             :  * the statistics.  Note that this doesn't load the actual statistics data,
    1291             :  * just the identifying metadata.  Only stats actually built are considered.
    1292             :  */
    1293             : static List *
    1294      282604 : get_relation_statistics(RelOptInfo *rel, Relation relation)
    1295             : {
    1296             :     List       *statoidlist;
    1297      282604 :     List       *stainfos = NIL;
    1298             :     ListCell   *l;
    1299             : 
    1300      282604 :     statoidlist = RelationGetStatExtList(relation);
    1301             : 
    1302      283040 :     foreach(l, statoidlist)
    1303             :     {
    1304         436 :         Oid         statOid = lfirst_oid(l);
    1305             :         Form_pg_statistic_ext staForm;
    1306             :         HeapTuple   htup;
    1307             :         HeapTuple   dtup;
    1308         436 :         Bitmapset  *keys = NULL;
    1309             :         int         i;
    1310             : 
    1311         436 :         htup = SearchSysCache1(STATEXTOID, ObjectIdGetDatum(statOid));
    1312         436 :         if (!HeapTupleIsValid(htup))
    1313           0 :             elog(ERROR, "cache lookup failed for statistics object %u", statOid);
    1314         436 :         staForm = (Form_pg_statistic_ext) GETSTRUCT(htup);
    1315             : 
    1316         436 :         dtup = SearchSysCache1(STATEXTDATASTXOID, ObjectIdGetDatum(statOid));
    1317         436 :         if (!HeapTupleIsValid(dtup))
    1318           0 :             elog(ERROR, "cache lookup failed for statistics object %u", statOid);
    1319             : 
    1320             :         /*
    1321             :          * First, build the array of columns covered.  This is ultimately
    1322             :          * wasted if no stats within the object have actually been built, but
    1323             :          * it doesn't seem worth troubling over that case.
    1324             :          */
    1325        1648 :         for (i = 0; i < staForm->stxkeys.dim1; i++)
    1326        1212 :             keys = bms_add_member(keys, staForm->stxkeys.values[i]);
    1327             : 
    1328             :         /* add one StatisticExtInfo for each kind built */
    1329         436 :         if (statext_is_kind_built(dtup, STATS_EXT_NDISTINCT))
    1330             :         {
    1331          44 :             StatisticExtInfo *info = makeNode(StatisticExtInfo);
    1332             : 
    1333          44 :             info->statOid = statOid;
    1334          44 :             info->rel = rel;
    1335          44 :             info->kind = STATS_EXT_NDISTINCT;
    1336          44 :             info->keys = bms_copy(keys);
    1337             : 
    1338          44 :             stainfos = lappend(stainfos, info);
    1339             :         }
    1340             : 
    1341         436 :         if (statext_is_kind_built(dtup, STATS_EXT_DEPENDENCIES))
    1342             :         {
    1343         200 :             StatisticExtInfo *info = makeNode(StatisticExtInfo);
    1344             : 
    1345         200 :             info->statOid = statOid;
    1346         200 :             info->rel = rel;
    1347         200 :             info->kind = STATS_EXT_DEPENDENCIES;
    1348         200 :             info->keys = bms_copy(keys);
    1349             : 
    1350         200 :             stainfos = lappend(stainfos, info);
    1351             :         }
    1352             : 
    1353         436 :         if (statext_is_kind_built(dtup, STATS_EXT_MCV))
    1354             :         {
    1355         256 :             StatisticExtInfo *info = makeNode(StatisticExtInfo);
    1356             : 
    1357         256 :             info->statOid = statOid;
    1358         256 :             info->rel = rel;
    1359         256 :             info->kind = STATS_EXT_MCV;
    1360         256 :             info->keys = bms_copy(keys);
    1361             : 
    1362         256 :             stainfos = lappend(stainfos, info);
    1363             :         }
    1364             : 
    1365         436 :         ReleaseSysCache(htup);
    1366         436 :         ReleaseSysCache(dtup);
    1367         436 :         bms_free(keys);
    1368             :     }
    1369             : 
    1370      282604 :     list_free(statoidlist);
    1371             : 
    1372      282604 :     return stainfos;
    1373             : }
    1374             : 
    1375             : /*
    1376             :  * relation_excluded_by_constraints
    1377             :  *
    1378             :  * Detect whether the relation need not be scanned because it has either
    1379             :  * self-inconsistent restrictions, or restrictions inconsistent with the
    1380             :  * relation's applicable constraints.
    1381             :  *
    1382             :  * Note: this examines only rel->relid, rel->reloptkind, and
    1383             :  * rel->baserestrictinfo; therefore it can be called before filling in
    1384             :  * other fields of the RelOptInfo.
    1385             :  */
    1386             : bool
    1387      292522 : relation_excluded_by_constraints(PlannerInfo *root,
    1388             :                                  RelOptInfo *rel, RangeTblEntry *rte)
    1389             : {
    1390             :     bool        include_noinherit;
    1391             :     bool        include_notnull;
    1392      292522 :     bool        include_partition = false;
    1393             :     List       *safe_restrictions;
    1394             :     List       *constraint_pred;
    1395             :     List       *safe_constraints;
    1396             :     ListCell   *lc;
    1397             : 
    1398             :     /* As of now, constraint exclusion works only with simple relations. */
    1399             :     Assert(IS_SIMPLE_REL(rel));
    1400             : 
    1401             :     /*
    1402             :      * If there are no base restriction clauses, we have no hope of proving
    1403             :      * anything below, so fall out quickly.
    1404             :      */
    1405      292522 :     if (rel->baserestrictinfo == NIL)
    1406      125922 :         return false;
    1407             : 
    1408             :     /*
    1409             :      * Regardless of the setting of constraint_exclusion, detect
    1410             :      * constant-FALSE-or-NULL restriction clauses.  Because const-folding will
    1411             :      * reduce "anything AND FALSE" to just "FALSE", any such case should
    1412             :      * result in exactly one baserestrictinfo entry.  This doesn't fire very
    1413             :      * often, but it seems cheap enough to be worth doing anyway.  (Without
    1414             :      * this, we'd miss some optimizations that 9.5 and earlier found via much
    1415             :      * more roundabout methods.)
    1416             :      */
    1417      166600 :     if (list_length(rel->baserestrictinfo) == 1)
    1418             :     {
    1419      120412 :         RestrictInfo *rinfo = (RestrictInfo *) linitial(rel->baserestrictinfo);
    1420      120412 :         Expr       *clause = rinfo->clause;
    1421             : 
    1422      120412 :         if (clause && IsA(clause, Const) &&
    1423         198 :             (((Const *) clause)->constisnull ||
    1424         198 :              !DatumGetBool(((Const *) clause)->constvalue)))
    1425         198 :             return true;
    1426             :     }
    1427             : 
    1428             :     /*
    1429             :      * Skip further tests, depending on constraint_exclusion.
    1430             :      */
    1431      166402 :     switch (constraint_exclusion)
    1432             :     {
    1433          52 :         case CONSTRAINT_EXCLUSION_OFF:
    1434             :             /* In 'off' mode, never make any further tests */
    1435          52 :             return false;
    1436             : 
    1437      166254 :         case CONSTRAINT_EXCLUSION_PARTITION:
    1438             : 
    1439             :             /*
    1440             :              * When constraint_exclusion is set to 'partition' we only handle
    1441             :              * appendrel members.  Normally, they are RELOPT_OTHER_MEMBER_REL
    1442             :              * relations, but we also consider inherited target relations as
    1443             :              * appendrel members for the purposes of constraint exclusion
    1444             :              * (since, indeed, they were appendrel members earlier in
    1445             :              * inheritance_planner).
    1446             :              *
    1447             :              * In both cases, partition pruning was already applied, so there
    1448             :              * is no need to consider the rel's partition constraints here.
    1449             :              */
    1450      166254 :             if (rel->reloptkind == RELOPT_OTHER_MEMBER_REL ||
    1451      153072 :                 (rel->relid == root->parse->resultRelation &&
    1452       10236 :                  root->inhTargetKind != INHKIND_NONE))
    1453             :                 break;          /* appendrel member, so process it */
    1454      151242 :             return false;
    1455             : 
    1456          96 :         case CONSTRAINT_EXCLUSION_ON:
    1457             : 
    1458             :             /*
    1459             :              * In 'on' mode, always apply constraint exclusion.  If we are
    1460             :              * considering a baserel that is a partition (i.e., it was
    1461             :              * directly named rather than expanded from a parent table), then
    1462             :              * its partition constraints haven't been considered yet, so
    1463             :              * include them in the processing here.
    1464             :              */
    1465          96 :             if (rel->reloptkind == RELOPT_BASEREL &&
    1466          76 :                 !(rel->relid == root->parse->resultRelation &&
    1467          24 :                   root->inhTargetKind != INHKIND_NONE))
    1468          64 :                 include_partition = true;
    1469          96 :             break;              /* always try to exclude */
    1470             :     }
    1471             : 
    1472             :     /*
    1473             :      * Check for self-contradictory restriction clauses.  We dare not make
    1474             :      * deductions with non-immutable functions, but any immutable clauses that
    1475             :      * are self-contradictory allow us to conclude the scan is unnecessary.
    1476             :      *
    1477             :      * Note: strip off RestrictInfo because predicate_refuted_by() isn't
    1478             :      * expecting to see any in its predicate argument.
    1479             :      */
    1480       15012 :     safe_restrictions = NIL;
    1481       35112 :     foreach(lc, rel->baserestrictinfo)
    1482             :     {
    1483       20004 :         RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
    1484             : 
    1485       20004 :         if (!contain_mutable_functions((Node *) rinfo->clause))
    1486       19068 :             safe_restrictions = lappend(safe_restrictions, rinfo->clause);
    1487             :     }
    1488             : 
    1489             :     /*
    1490             :      * We can use weak refutation here, since we're comparing restriction
    1491             :      * clauses with restriction clauses.
    1492             :      */
    1493       15108 :     if (predicate_refuted_by(safe_restrictions, safe_restrictions, true))
    1494          60 :         return true;
    1495             : 
    1496             :     /*
    1497             :      * Only plain relations have constraints, so stop here for other rtekinds.
    1498             :      */
    1499       15048 :     if (rte->rtekind != RTE_RELATION)
    1500         172 :         return false;
    1501             : 
    1502             :     /*
    1503             :      * If we are scanning just this table, we can use NO INHERIT constraints,
    1504             :      * but not if we're scanning its children too.  (Note that partitioned
    1505             :      * tables should never have NO INHERIT constraints; but it's not necessary
    1506             :      * for us to assume that here.)
    1507             :      */
    1508       14876 :     include_noinherit = !rte->inh;
    1509             : 
    1510             :     /*
    1511             :      * Currently, attnotnull constraints must be treated as NO INHERIT unless
    1512             :      * this is a partitioned table.  In future we might track their
    1513             :      * inheritance status more accurately, allowing this to be refined.
    1514             :      */
    1515       14876 :     include_notnull = (!rte->inh || rte->relkind == RELKIND_PARTITIONED_TABLE);
    1516             : 
    1517             :     /*
    1518             :      * Fetch the appropriate set of constraint expressions.
    1519             :      */
    1520       14876 :     constraint_pred = get_relation_constraints(root, rte->relid, rel,
    1521             :                                                include_noinherit,
    1522             :                                                include_notnull,
    1523             :                                                include_partition);
    1524             : 
    1525             :     /*
    1526             :      * We do not currently enforce that CHECK constraints contain only
    1527             :      * immutable functions, so it's necessary to check here. We daren't draw
    1528             :      * conclusions from plan-time evaluation of non-immutable functions. Since
    1529             :      * they're ANDed, we can just ignore any mutable constraints in the list,
    1530             :      * and reason about the rest.
    1531             :      */
    1532       14876 :     safe_constraints = NIL;
    1533       22400 :     foreach(lc, constraint_pred)
    1534             :     {
    1535        7524 :         Node       *pred = (Node *) lfirst(lc);
    1536             : 
    1537        7524 :         if (!contain_mutable_functions(pred))
    1538        7524 :             safe_constraints = lappend(safe_constraints, pred);
    1539             :     }
    1540             : 
    1541             :     /*
    1542             :      * The constraints are effectively ANDed together, so we can just try to
    1543             :      * refute the entire collection at once.  This may allow us to make proofs
    1544             :      * that would fail if we took them individually.
    1545             :      *
    1546             :      * Note: we use rel->baserestrictinfo, not safe_restrictions as might seem
    1547             :      * an obvious optimization.  Some of the clauses might be OR clauses that
    1548             :      * have volatile and nonvolatile subclauses, and it's OK to make
    1549             :      * deductions with the nonvolatile parts.
    1550             :      *
    1551             :      * We need strong refutation because we have to prove that the constraints
    1552             :      * would yield false, not just NULL.
    1553             :      */
    1554       14876 :     if (predicate_refuted_by(safe_constraints, rel->baserestrictinfo, false))
    1555          56 :         return true;
    1556             : 
    1557       14820 :     return false;
    1558             : }
    1559             : 
    1560             : 
    1561             : /*
    1562             :  * build_physical_tlist
    1563             :  *
    1564             :  * Build a targetlist consisting of exactly the relation's user attributes,
    1565             :  * in order.  The executor can special-case such tlists to avoid a projection
    1566             :  * step at runtime, so we use such tlists preferentially for scan nodes.
    1567             :  *
    1568             :  * Exception: if there are any dropped or missing columns, we punt and return
    1569             :  * NIL.  Ideally we would like to handle these cases too.  However this
    1570             :  * creates problems for ExecTypeFromTL, which may be asked to build a tupdesc
    1571             :  * for a tlist that includes vars of no-longer-existent types.  In theory we
    1572             :  * could dig out the required info from the pg_attribute entries of the
    1573             :  * relation, but that data is not readily available to ExecTypeFromTL.
    1574             :  * For now, we don't apply the physical-tlist optimization when there are
    1575             :  * dropped cols.
    1576             :  *
    1577             :  * We also support building a "physical" tlist for subqueries, functions,
    1578             :  * values lists, table expressions, and CTEs, since the same optimization can
    1579             :  * occur in SubqueryScan, FunctionScan, ValuesScan, CteScan, TableFunc,
    1580             :  * NamedTuplestoreScan, and WorkTableScan nodes.
    1581             :  */
    1582             : List *
    1583       78244 : build_physical_tlist(PlannerInfo *root, RelOptInfo *rel)
    1584             : {
    1585       78244 :     List       *tlist = NIL;
    1586       78244 :     Index       varno = rel->relid;
    1587       78244 :     RangeTblEntry *rte = planner_rt_fetch(varno, root);
    1588             :     Relation    relation;
    1589             :     Query      *subquery;
    1590             :     Var        *var;
    1591             :     ListCell   *l;
    1592             :     int         attrno,
    1593             :                 numattrs;
    1594             :     List       *colvars;
    1595             : 
    1596       78244 :     switch (rte->rtekind)
    1597             :     {
    1598       68356 :         case RTE_RELATION:
    1599             :             /* Assume we already have adequate lock */
    1600       68356 :             relation = table_open(rte->relid, NoLock);
    1601             : 
    1602       68356 :             numattrs = RelationGetNumberOfAttributes(relation);
    1603     1265492 :             for (attrno = 1; attrno <= numattrs; attrno++)
    1604             :             {
    1605     1197272 :                 Form_pg_attribute att_tup = TupleDescAttr(relation->rd_att,
    1606             :                                                           attrno - 1);
    1607             : 
    1608     1197272 :                 if (att_tup->attisdropped || att_tup->atthasmissing)
    1609             :                 {
    1610             :                     /* found a dropped or missing col, so punt */
    1611         136 :                     tlist = NIL;
    1612         136 :                     break;
    1613             :                 }
    1614             : 
    1615     1197136 :                 var = makeVar(varno,
    1616             :                               attrno,
    1617             :                               att_tup->atttypid,
    1618             :                               att_tup->atttypmod,
    1619             :                               att_tup->attcollation,
    1620             :                               0);
    1621             : 
    1622     1197136 :                 tlist = lappend(tlist,
    1623     1197136 :                                 makeTargetEntry((Expr *) var,
    1624             :                                                 attrno,
    1625             :                                                 NULL,
    1626             :                                                 false));
    1627             :             }
    1628             : 
    1629       68356 :             table_close(relation, NoLock);
    1630       68356 :             break;
    1631             : 
    1632        1004 :         case RTE_SUBQUERY:
    1633        1004 :             subquery = rte->subquery;
    1634        3764 :             foreach(l, subquery->targetList)
    1635             :             {
    1636        2760 :                 TargetEntry *tle = (TargetEntry *) lfirst(l);
    1637             : 
    1638             :                 /*
    1639             :                  * A resjunk column of the subquery can be reflected as
    1640             :                  * resjunk in the physical tlist; we need not punt.
    1641             :                  */
    1642        2760 :                 var = makeVarFromTargetEntry(varno, tle);
    1643             : 
    1644        2760 :                 tlist = lappend(tlist,
    1645        5520 :                                 makeTargetEntry((Expr *) var,
    1646        2760 :                                                 tle->resno,
    1647             :                                                 NULL,
    1648        2760 :                                                 tle->resjunk));
    1649             :             }
    1650        1004 :             break;
    1651             : 
    1652        8884 :         case RTE_FUNCTION:
    1653             :         case RTE_TABLEFUNC:
    1654             :         case RTE_VALUES:
    1655             :         case RTE_CTE:
    1656             :         case RTE_NAMEDTUPLESTORE:
    1657             :         case RTE_RESULT:
    1658             :             /* Not all of these can have dropped cols, but share code anyway */
    1659        8884 :             expandRTE(rte, varno, 0, -1, true /* include dropped */ ,
    1660             :                       NULL, &colvars);
    1661       32526 :             foreach(l, colvars)
    1662             :             {
    1663       23642 :                 var = (Var *) lfirst(l);
    1664             : 
    1665             :                 /*
    1666             :                  * A non-Var in expandRTE's output means a dropped column;
    1667             :                  * must punt.
    1668             :                  */
    1669       23642 :                 if (!IsA(var, Var))
    1670             :                 {
    1671           0 :                     tlist = NIL;
    1672           0 :                     break;
    1673             :                 }
    1674             : 
    1675       23642 :                 tlist = lappend(tlist,
    1676       23642 :                                 makeTargetEntry((Expr *) var,
    1677       23642 :                                                 var->varattno,
    1678             :                                                 NULL,
    1679             :                                                 false));
    1680             :             }
    1681        8884 :             break;
    1682             : 
    1683           0 :         default:
    1684             :             /* caller error */
    1685           0 :             elog(ERROR, "unsupported RTE kind %d in build_physical_tlist",
    1686             :                  (int) rte->rtekind);
    1687             :             break;
    1688             :     }
    1689             : 
    1690       78244 :     return tlist;
    1691             : }
    1692             : 
    1693             : /*
    1694             :  * build_index_tlist
    1695             :  *
    1696             :  * Build a targetlist representing the columns of the specified index.
    1697             :  * Each column is represented by a Var for the corresponding base-relation
    1698             :  * column, or an expression in base-relation Vars, as appropriate.
    1699             :  *
    1700             :  * There are never any dropped columns in indexes, so unlike
    1701             :  * build_physical_tlist, we need no failure case.
    1702             :  */
    1703             : static List *
    1704      417172 : build_index_tlist(PlannerInfo *root, IndexOptInfo *index,
    1705             :                   Relation heapRelation)
    1706             : {
    1707      417172 :     List       *tlist = NIL;
    1708      417172 :     Index       varno = index->rel->relid;
    1709             :     ListCell   *indexpr_item;
    1710             :     int         i;
    1711             : 
    1712      417172 :     indexpr_item = list_head(index->indexprs);
    1713     1167690 :     for (i = 0; i < index->ncolumns; i++)
    1714             :     {
    1715      750518 :         int         indexkey = index->indexkeys[i];
    1716             :         Expr       *indexvar;
    1717             : 
    1718      750518 :         if (indexkey != 0)
    1719             :         {
    1720             :             /* simple column */
    1721             :             const FormData_pg_attribute *att_tup;
    1722             : 
    1723      749018 :             if (indexkey < 0)
    1724           0 :                 att_tup = SystemAttributeDefinition(indexkey);
    1725             :             else
    1726      749018 :                 att_tup = TupleDescAttr(heapRelation->rd_att, indexkey - 1);
    1727             : 
    1728      749018 :             indexvar = (Expr *) makeVar(varno,
    1729             :                                         indexkey,
    1730             :                                         att_tup->atttypid,
    1731             :                                         att_tup->atttypmod,
    1732             :                                         att_tup->attcollation,
    1733             :                                         0);
    1734             :         }
    1735             :         else
    1736             :         {
    1737             :             /* expression column */
    1738        1500 :             if (indexpr_item == NULL)
    1739           0 :                 elog(ERROR, "wrong number of index expressions");
    1740        1500 :             indexvar = (Expr *) lfirst(indexpr_item);
    1741        1500 :             indexpr_item = lnext(index->indexprs, indexpr_item);
    1742             :         }
    1743             : 
    1744      750518 :         tlist = lappend(tlist,
    1745      750518 :                         makeTargetEntry(indexvar,
    1746      750518 :                                         i + 1,
    1747             :                                         NULL,
    1748             :                                         false));
    1749             :     }
    1750      417172 :     if (indexpr_item != NULL)
    1751           0 :         elog(ERROR, "wrong number of index expressions");
    1752             : 
    1753      417172 :     return tlist;
    1754             : }
    1755             : 
    1756             : /*
    1757             :  * restriction_selectivity
    1758             :  *
    1759             :  * Returns the selectivity of a specified restriction operator clause.
    1760             :  * This code executes registered procedures stored in the
    1761             :  * operator relation, by calling the function manager.
    1762             :  *
    1763             :  * See clause_selectivity() for the meaning of the additional parameters.
    1764             :  */
    1765             : Selectivity
    1766      340982 : restriction_selectivity(PlannerInfo *root,
    1767             :                         Oid operatorid,
    1768             :                         List *args,
    1769             :                         Oid inputcollid,
    1770             :                         int varRelid)
    1771             : {
    1772      340982 :     RegProcedure oprrest = get_oprrest(operatorid);
    1773             :     float8      result;
    1774             : 
    1775             :     /*
    1776             :      * if the oprrest procedure is missing for whatever reason, use a
    1777             :      * selectivity of 0.5
    1778             :      */
    1779      340982 :     if (!oprrest)
    1780          56 :         return (Selectivity) 0.5;
    1781             : 
    1782      340926 :     result = DatumGetFloat8(OidFunctionCall4Coll(oprrest,
    1783             :                                                  inputcollid,
    1784             :                                                  PointerGetDatum(root),
    1785             :                                                  ObjectIdGetDatum(operatorid),
    1786             :                                                  PointerGetDatum(args),
    1787             :                                                  Int32GetDatum(varRelid)));
    1788             : 
    1789      340926 :     if (result < 0.0 || result > 1.0)
    1790           0 :         elog(ERROR, "invalid restriction selectivity: %f", result);
    1791             : 
    1792      340926 :     return (Selectivity) result;
    1793             : }
    1794             : 
    1795             : /*
    1796             :  * join_selectivity
    1797             :  *
    1798             :  * Returns the selectivity of a specified join operator clause.
    1799             :  * This code executes registered procedures stored in the
    1800             :  * operator relation, by calling the function manager.
    1801             :  *
    1802             :  * See clause_selectivity() for the meaning of the additional parameters.
    1803             :  */
    1804             : Selectivity
    1805      124500 : join_selectivity(PlannerInfo *root,
    1806             :                  Oid operatorid,
    1807             :                  List *args,
    1808             :                  Oid inputcollid,
    1809             :                  JoinType jointype,
    1810             :                  SpecialJoinInfo *sjinfo)
    1811             : {
    1812      124500 :     RegProcedure oprjoin = get_oprjoin(operatorid);
    1813             :     float8      result;
    1814             : 
    1815             :     /*
    1816             :      * if the oprjoin procedure is missing for whatever reason, use a
    1817             :      * selectivity of 0.5
    1818             :      */
    1819      124500 :     if (!oprjoin)
    1820         100 :         return (Selectivity) 0.5;
    1821             : 
    1822      124400 :     result = DatumGetFloat8(OidFunctionCall5Coll(oprjoin,
    1823             :                                                  inputcollid,
    1824             :                                                  PointerGetDatum(root),
    1825             :                                                  ObjectIdGetDatum(operatorid),
    1826             :                                                  PointerGetDatum(args),
    1827             :                                                  Int16GetDatum(jointype),
    1828             :                                                  PointerGetDatum(sjinfo)));
    1829             : 
    1830      124400 :     if (result < 0.0 || result > 1.0)
    1831           0 :         elog(ERROR, "invalid join selectivity: %f", result);
    1832             : 
    1833      124400 :     return (Selectivity) result;
    1834             : }
    1835             : 
    1836             : /*
    1837             :  * function_selectivity
    1838             :  *
    1839             :  * Returns the selectivity of a specified boolean function clause.
    1840             :  * This code executes registered procedures stored in the
    1841             :  * pg_proc relation, by calling the function manager.
    1842             :  *
    1843             :  * See clause_selectivity() for the meaning of the additional parameters.
    1844             :  */
    1845             : Selectivity
    1846        5804 : function_selectivity(PlannerInfo *root,
    1847             :                      Oid funcid,
    1848             :                      List *args,
    1849             :                      Oid inputcollid,
    1850             :                      bool is_join,
    1851             :                      int varRelid,
    1852             :                      JoinType jointype,
    1853             :                      SpecialJoinInfo *sjinfo)
    1854             : {
    1855        5804 :     RegProcedure prosupport = get_func_support(funcid);
    1856             :     SupportRequestSelectivity req;
    1857             :     SupportRequestSelectivity *sresult;
    1858             : 
    1859             :     /*
    1860             :      * If no support function is provided, use our historical default
    1861             :      * estimate, 0.3333333.  This seems a pretty unprincipled choice, but
    1862             :      * Postgres has been using that estimate for function calls since 1992.
    1863             :      * The hoariness of this behavior suggests that we should not be in too
    1864             :      * much hurry to use another value.
    1865             :      */
    1866        5804 :     if (!prosupport)
    1867        5800 :         return (Selectivity) 0.3333333;
    1868             : 
    1869           4 :     req.type = T_SupportRequestSelectivity;
    1870           4 :     req.root = root;
    1871           4 :     req.funcid = funcid;
    1872           4 :     req.args = args;
    1873           4 :     req.inputcollid = inputcollid;
    1874           4 :     req.is_join = is_join;
    1875           4 :     req.varRelid = varRelid;
    1876           4 :     req.jointype = jointype;
    1877           4 :     req.sjinfo = sjinfo;
    1878           4 :     req.selectivity = -1;       /* to catch failure to set the value */
    1879             : 
    1880           4 :     sresult = (SupportRequestSelectivity *)
    1881           4 :         DatumGetPointer(OidFunctionCall1(prosupport,
    1882             :                                          PointerGetDatum(&req)));
    1883             : 
    1884             :     /* If support function fails, use default */
    1885           4 :     if (sresult != &req)
    1886           0 :         return (Selectivity) 0.3333333;
    1887             : 
    1888           4 :     if (req.selectivity < 0.0 || req.selectivity > 1.0)
    1889           0 :         elog(ERROR, "invalid function selectivity: %f", req.selectivity);
    1890             : 
    1891           4 :     return (Selectivity) req.selectivity;
    1892             : }
    1893             : 
    1894             : /*
    1895             :  * add_function_cost
    1896             :  *
    1897             :  * Get an estimate of the execution cost of a function, and *add* it to
    1898             :  * the contents of *cost.  The estimate may include both one-time and
    1899             :  * per-tuple components, since QualCost does.
    1900             :  *
    1901             :  * The funcid must always be supplied.  If it is being called as the
    1902             :  * implementation of a specific parsetree node (FuncExpr, OpExpr,
    1903             :  * WindowFunc, etc), pass that as "node", else pass NULL.
    1904             :  *
    1905             :  * In some usages root might be NULL, too.
    1906             :  */
    1907             : void
    1908      718342 : add_function_cost(PlannerInfo *root, Oid funcid, Node *node,
    1909             :                   QualCost *cost)
    1910             : {
    1911             :     HeapTuple   proctup;
    1912             :     Form_pg_proc procform;
    1913             : 
    1914      718342 :     proctup = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
    1915      718342 :     if (!HeapTupleIsValid(proctup))
    1916           0 :         elog(ERROR, "cache lookup failed for function %u", funcid);
    1917      718342 :     procform = (Form_pg_proc) GETSTRUCT(proctup);
    1918             : 
    1919      718342 :     if (OidIsValid(procform->prosupport))
    1920             :     {
    1921             :         SupportRequestCost req;
    1922             :         SupportRequestCost *sresult;
    1923             : 
    1924       26004 :         req.type = T_SupportRequestCost;
    1925       26004 :         req.root = root;
    1926       26004 :         req.funcid = funcid;
    1927       26004 :         req.node = node;
    1928             : 
    1929             :         /* Initialize cost fields so that support function doesn't have to */
    1930       26004 :         req.startup = 0;
    1931       26004 :         req.per_tuple = 0;
    1932             : 
    1933       26004 :         sresult = (SupportRequestCost *)
    1934       26004 :             DatumGetPointer(OidFunctionCall1(procform->prosupport,
    1935             :                                              PointerGetDatum(&req)));
    1936             : 
    1937       26004 :         if (sresult == &req)
    1938             :         {
    1939             :             /* Success, so accumulate support function's estimate into *cost */
    1940          12 :             cost->startup += req.startup;
    1941          12 :             cost->per_tuple += req.per_tuple;
    1942          12 :             ReleaseSysCache(proctup);
    1943          12 :             return;
    1944             :         }
    1945             :     }
    1946             : 
    1947             :     /* No support function, or it failed, so rely on procost */
    1948      718330 :     cost->per_tuple += procform->procost * cpu_operator_cost;
    1949             : 
    1950      718330 :     ReleaseSysCache(proctup);
    1951             : }
    1952             : 
    1953             : /*
    1954             :  * get_function_rows
    1955             :  *
    1956             :  * Get an estimate of the number of rows returned by a set-returning function.
    1957             :  *
    1958             :  * The funcid must always be supplied.  In current usage, the calling node
    1959             :  * will always be supplied, and will be either a FuncExpr or OpExpr.
    1960             :  * But it's a good idea to not fail if it's NULL.
    1961             :  *
    1962             :  * In some usages root might be NULL, too.
    1963             :  *
    1964             :  * Note: this returns the unfiltered result of the support function, if any.
    1965             :  * It's usually a good idea to apply clamp_row_est() to the result, but we
    1966             :  * leave it to the caller to do so.
    1967             :  */
    1968             : double
    1969       32612 : get_function_rows(PlannerInfo *root, Oid funcid, Node *node)
    1970             : {
    1971             :     HeapTuple   proctup;
    1972             :     Form_pg_proc procform;
    1973             :     double      result;
    1974             : 
    1975       32612 :     proctup = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
    1976       32612 :     if (!HeapTupleIsValid(proctup))
    1977           0 :         elog(ERROR, "cache lookup failed for function %u", funcid);
    1978       32612 :     procform = (Form_pg_proc) GETSTRUCT(proctup);
    1979             : 
    1980             :     Assert(procform->proretset); /* else caller error */
    1981             : 
    1982       32612 :     if (OidIsValid(procform->prosupport))
    1983             :     {
    1984             :         SupportRequestRows req;
    1985             :         SupportRequestRows *sresult;
    1986             : 
    1987       19570 :         req.type = T_SupportRequestRows;
    1988       19570 :         req.root = root;
    1989       19570 :         req.funcid = funcid;
    1990       19570 :         req.node = node;
    1991             : 
    1992       19570 :         req.rows = 0;           /* just for sanity */
    1993             : 
    1994       19570 :         sresult = (SupportRequestRows *)
    1995       19570 :             DatumGetPointer(OidFunctionCall1(procform->prosupport,
    1996             :                                              PointerGetDatum(&req)));
    1997             : 
    1998       19570 :         if (sresult == &req)
    1999             :         {
    2000             :             /* Success */
    2001       19382 :             ReleaseSysCache(proctup);
    2002       19382 :             return req.rows;
    2003             :         }
    2004             :     }
    2005             : 
    2006             :     /* No support function, or it failed, so rely on prorows */
    2007       13230 :     result = procform->prorows;
    2008             : 
    2009       13230 :     ReleaseSysCache(proctup);
    2010             : 
    2011       13230 :     return result;
    2012             : }
    2013             : 
    2014             : /*
    2015             :  * has_unique_index
    2016             :  *
    2017             :  * Detect whether there is a unique index on the specified attribute
    2018             :  * of the specified relation, thus allowing us to conclude that all
    2019             :  * the (non-null) values of the attribute are distinct.
    2020             :  *
    2021             :  * This function does not check the index's indimmediate property, which
    2022             :  * means that uniqueness may transiently fail to hold intra-transaction.
    2023             :  * That's appropriate when we are making statistical estimates, but beware
    2024             :  * of using this for any correctness proofs.
    2025             :  */
    2026             : bool
    2027      844310 : has_unique_index(RelOptInfo *rel, AttrNumber attno)
    2028             : {
    2029             :     ListCell   *ilist;
    2030             : 
    2031     2034110 :     foreach(ilist, rel->indexlist)
    2032             :     {
    2033     1471092 :         IndexOptInfo *index = (IndexOptInfo *) lfirst(ilist);
    2034             : 
    2035             :         /*
    2036             :          * Note: ignore partial indexes, since they don't allow us to conclude
    2037             :          * that all attr values are distinct, *unless* they are marked predOK
    2038             :          * which means we know the index's predicate is satisfied by the
    2039             :          * query. We don't take any interest in expressional indexes either.
    2040             :          * Also, a multicolumn unique index doesn't allow us to conclude that
    2041             :          * just the specified attr is unique.
    2042             :          */
    2043     1471092 :         if (index->unique &&
    2044     1067874 :             index->nkeycolumns == 1 &&
    2045      559402 :             index->indexkeys[0] == attno &&
    2046      281320 :             (index->indpred == NIL || index->predOK))
    2047      281292 :             return true;
    2048             :     }
    2049      563018 :     return false;
    2050             : }
    2051             : 
    2052             : 
    2053             : /*
    2054             :  * has_row_triggers
    2055             :  *
    2056             :  * Detect whether the specified relation has any row-level triggers for event.
    2057             :  */
    2058             : bool
    2059         344 : has_row_triggers(PlannerInfo *root, Index rti, CmdType event)
    2060             : {
    2061         344 :     RangeTblEntry *rte = planner_rt_fetch(rti, root);
    2062             :     Relation    relation;
    2063             :     TriggerDesc *trigDesc;
    2064         344 :     bool        result = false;
    2065             : 
    2066             :     /* Assume we already have adequate lock */
    2067         344 :     relation = table_open(rte->relid, NoLock);
    2068             : 
    2069         344 :     trigDesc = relation->trigdesc;
    2070         344 :     switch (event)
    2071             :     {
    2072          88 :         case CMD_INSERT:
    2073          88 :             if (trigDesc &&
    2074          22 :                 (trigDesc->trig_insert_after_row ||
    2075          10 :                  trigDesc->trig_insert_before_row))
    2076          22 :                 result = true;
    2077          88 :             break;
    2078         152 :         case CMD_UPDATE:
    2079         152 :             if (trigDesc &&
    2080          48 :                 (trigDesc->trig_update_after_row ||
    2081          28 :                  trigDesc->trig_update_before_row))
    2082          36 :                 result = true;
    2083         152 :             break;
    2084         104 :         case CMD_DELETE:
    2085         104 :             if (trigDesc &&
    2086          30 :                 (trigDesc->trig_delete_after_row ||
    2087          18 :                  trigDesc->trig_delete_before_row))
    2088          16 :                 result = true;
    2089         104 :             break;
    2090           0 :         default:
    2091           0 :             elog(ERROR, "unrecognized CmdType: %d", (int) event);
    2092             :             break;
    2093             :     }
    2094             : 
    2095         344 :     table_close(relation, NoLock);
    2096         344 :     return result;
    2097             : }
    2098             : 
    2099             : bool
    2100         270 : has_stored_generated_columns(PlannerInfo *root, Index rti)
    2101             : {
    2102         270 :     RangeTblEntry *rte = planner_rt_fetch(rti, root);
    2103             :     Relation    relation;
    2104             :     TupleDesc   tupdesc;
    2105         270 :     bool        result = false;
    2106             : 
    2107             :     /* Assume we already have adequate lock */
    2108         270 :     relation = table_open(rte->relid, NoLock);
    2109             : 
    2110         270 :     tupdesc = RelationGetDescr(relation);
    2111         270 :     result = tupdesc->constr && tupdesc->constr->has_generated_stored;
    2112             : 
    2113         270 :     table_close(relation, NoLock);
    2114             : 
    2115         270 :     return result;
    2116             : }
    2117             : 
    2118             : /*
    2119             :  * set_relation_partition_info
    2120             :  *
    2121             :  * Set partitioning scheme and related information for a partitioned table.
    2122             :  */
    2123             : static void
    2124        9760 : set_relation_partition_info(PlannerInfo *root, RelOptInfo *rel,
    2125             :                             Relation relation)
    2126             : {
    2127             :     PartitionDesc partdesc;
    2128             : 
    2129             :     /* Create the PartitionDirectory infrastructure if we didn't already */
    2130        9760 :     if (root->glob->partition_directory == NULL)
    2131        6662 :         root->glob->partition_directory =
    2132        6662 :             CreatePartitionDirectory(CurrentMemoryContext);
    2133             : 
    2134        9760 :     partdesc = PartitionDirectoryLookup(root->glob->partition_directory,
    2135             :                                         relation);
    2136        9760 :     rel->part_scheme = find_partition_scheme(root, relation);
    2137             :     Assert(partdesc != NULL && rel->part_scheme != NULL);
    2138        9760 :     rel->boundinfo = partdesc->boundinfo;
    2139        9760 :     rel->nparts = partdesc->nparts;
    2140        9760 :     set_baserel_partition_key_exprs(relation, rel);
    2141        9760 :     set_baserel_partition_constraint(relation, rel);
    2142        9760 : }
    2143             : 
    2144             : /*
    2145             :  * find_partition_scheme
    2146             :  *
    2147             :  * Find or create a PartitionScheme for this Relation.
    2148             :  */
    2149             : static PartitionScheme
    2150        9760 : find_partition_scheme(PlannerInfo *root, Relation relation)
    2151             : {
    2152        9760 :     PartitionKey partkey = RelationGetPartitionKey(relation);
    2153             :     ListCell   *lc;
    2154             :     int         partnatts,
    2155             :                 i;
    2156             :     PartitionScheme part_scheme;
    2157             : 
    2158             :     /* A partitioned table should have a partition key. */
    2159             :     Assert(partkey != NULL);
    2160             : 
    2161        9760 :     partnatts = partkey->partnatts;
    2162             : 
    2163             :     /* Search for a matching partition scheme and return if found one. */
    2164       10916 :     foreach(lc, root->part_schemes)
    2165             :     {
    2166        3454 :         part_scheme = lfirst(lc);
    2167             : 
    2168             :         /* Match partitioning strategy and number of keys. */
    2169        3454 :         if (partkey->strategy != part_scheme->strategy ||
    2170        2870 :             partnatts != part_scheme->partnatts)
    2171         856 :             continue;
    2172             : 
    2173             :         /* Match partition key type properties. */
    2174        2598 :         if (memcmp(partkey->partopfamily, part_scheme->partopfamily,
    2175        2298 :                    sizeof(Oid) * partnatts) != 0 ||
    2176        2298 :             memcmp(partkey->partopcintype, part_scheme->partopcintype,
    2177        2298 :                    sizeof(Oid) * partnatts) != 0 ||
    2178        2298 :             memcmp(partkey->partcollation, part_scheme->partcollation,
    2179             :                    sizeof(Oid) * partnatts) != 0)
    2180         300 :             continue;
    2181             : 
    2182             :         /*
    2183             :          * Length and byval information should match when partopcintype
    2184             :          * matches.
    2185             :          */
    2186             :         Assert(memcmp(partkey->parttyplen, part_scheme->parttyplen,
    2187             :                       sizeof(int16) * partnatts) == 0);
    2188             :         Assert(memcmp(partkey->parttypbyval, part_scheme->parttypbyval,
    2189             :                       sizeof(bool) * partnatts) == 0);
    2190             : 
    2191             :         /*
    2192             :          * If partopfamily and partopcintype matched, must have the same
    2193             :          * partition comparison functions.  Note that we cannot reliably
    2194             :          * Assert the equality of function structs themselves for they might
    2195             :          * be different across PartitionKey's, so just Assert for the function
    2196             :          * OIDs.
    2197             :          */
    2198             : #ifdef USE_ASSERT_CHECKING
    2199             :         for (i = 0; i < partkey->partnatts; i++)
    2200             :             Assert(partkey->partsupfunc[i].fn_oid ==
    2201             :                    part_scheme->partsupfunc[i].fn_oid);
    2202             : #endif
    2203             : 
    2204             :         /* Found matching partition scheme. */
    2205        2298 :         return part_scheme;
    2206             :     }
    2207             : 
    2208             :     /*
    2209             :      * Did not find matching partition scheme. Create one copying relevant
    2210             :      * information from the relcache. We need to copy the contents of the
    2211             :      * array since the relcache entry may not survive after we have closed the
    2212             :      * relation.
    2213             :      */
    2214        7462 :     part_scheme = (PartitionScheme) palloc0(sizeof(PartitionSchemeData));
    2215        7462 :     part_scheme->strategy = partkey->strategy;
    2216        7462 :     part_scheme->partnatts = partkey->partnatts;
    2217             : 
    2218        7462 :     part_scheme->partopfamily = (Oid *) palloc(sizeof(Oid) * partnatts);
    2219        7462 :     memcpy(part_scheme->partopfamily, partkey->partopfamily,
    2220             :            sizeof(Oid) * partnatts);
    2221             : 
    2222        7462 :     part_scheme->partopcintype = (Oid *) palloc(sizeof(Oid) * partnatts);
    2223        7462 :     memcpy(part_scheme->partopcintype, partkey->partopcintype,
    2224             :            sizeof(Oid) * partnatts);
    2225             : 
    2226        7462 :     part_scheme->partcollation = (Oid *) palloc(sizeof(Oid) * partnatts);
    2227        7462 :     memcpy(part_scheme->partcollation, partkey->partcollation,
    2228             :            sizeof(Oid) * partnatts);
    2229             : 
    2230        7462 :     part_scheme->parttyplen = (int16 *) palloc(sizeof(int16) * partnatts);
    2231        7462 :     memcpy(part_scheme->parttyplen, partkey->parttyplen,
    2232             :            sizeof(int16) * partnatts);
    2233             : 
    2234        7462 :     part_scheme->parttypbyval = (bool *) palloc(sizeof(bool) * partnatts);
    2235        7462 :     memcpy(part_scheme->parttypbyval, partkey->parttypbyval,
    2236             :            sizeof(bool) * partnatts);
    2237             : 
    2238        7462 :     part_scheme->partsupfunc = (FmgrInfo *)
    2239        7462 :         palloc(sizeof(FmgrInfo) * partnatts);
    2240       15704 :     for (i = 0; i < partnatts; i++)
    2241        8242 :         fmgr_info_copy(&part_scheme->partsupfunc[i], &partkey->partsupfunc[i],
    2242             :                        CurrentMemoryContext);
    2243             : 
    2244             :     /* Add the partitioning scheme to PlannerInfo. */
    2245        7462 :     root->part_schemes = lappend(root->part_schemes, part_scheme);
    2246             : 
    2247        7462 :     return part_scheme;
    2248             : }
    2249             : 
    2250             : /*
    2251             :  * set_baserel_partition_key_exprs
    2252             :  *
    2253             :  * Builds partition key expressions for the given base relation and fills
    2254             :  * rel->partexprs.
    2255             :  */
    2256             : static void
    2257        9760 : set_baserel_partition_key_exprs(Relation relation,
    2258             :                                 RelOptInfo *rel)
    2259             : {
    2260        9760 :     PartitionKey partkey = RelationGetPartitionKey(relation);
    2261             :     int         partnatts;
    2262             :     int         cnt;
    2263             :     List      **partexprs;
    2264             :     ListCell   *lc;
    2265        9760 :     Index       varno = rel->relid;
    2266             : 
    2267             :     Assert(IS_SIMPLE_REL(rel) && rel->relid > 0);
    2268             : 
    2269             :     /* A partitioned table should have a partition key. */
    2270             :     Assert(partkey != NULL);
    2271             : 
    2272        9760 :     partnatts = partkey->partnatts;
    2273        9760 :     partexprs = (List **) palloc(sizeof(List *) * partnatts);
    2274        9760 :     lc = list_head(partkey->partexprs);
    2275             : 
    2276       20320 :     for (cnt = 0; cnt < partnatts; cnt++)
    2277             :     {
    2278             :         Expr       *partexpr;
    2279       10560 :         AttrNumber  attno = partkey->partattrs[cnt];
    2280             : 
    2281       10560 :         if (attno != InvalidAttrNumber)
    2282             :         {
    2283             :             /* Single column partition key is stored as a Var node. */
    2284             :             Assert(attno > 0);
    2285             : 
    2286       30072 :             partexpr = (Expr *) makeVar(varno, attno,
    2287       10024 :                                         partkey->parttypid[cnt],
    2288       10024 :                                         partkey->parttypmod[cnt],
    2289       10024 :                                         partkey->parttypcoll[cnt], 0);
    2290             :         }
    2291             :         else
    2292             :         {
    2293         536 :             if (lc == NULL)
    2294           0 :                 elog(ERROR, "wrong number of partition key expressions");
    2295             : 
    2296             :             /* Re-stamp the expression with given varno. */
    2297         536 :             partexpr = (Expr *) copyObject(lfirst(lc));
    2298         536 :             ChangeVarNodes((Node *) partexpr, 1, varno, 0);
    2299         536 :             lc = lnext(partkey->partexprs, lc);
    2300             :         }
    2301             : 
    2302             :         /* Base relations have a single expression per key. */
    2303       10560 :         partexprs[cnt] = list_make1(partexpr);
    2304             :     }
    2305             : 
    2306        9760 :     rel->partexprs = partexprs;
    2307             : 
    2308             :     /*
    2309             :      * A base relation does not have nullable partition key expressions, since
    2310             :      * no outer join is involved.  We still allocate an array of empty
    2311             :      * expression lists to keep partition key expression handling code simple.
    2312             :      * See build_joinrel_partition_info() and match_expr_to_partition_keys().
    2313             :      */
    2314        9760 :     rel->nullable_partexprs = (List **) palloc0(sizeof(List *) * partnatts);
    2315        9760 : }
    2316             : 
    2317             : /*
    2318             :  * set_baserel_partition_constraint
    2319             :  *
    2320             :  * Builds the partition constraint for the given base relation and sets it
    2321             :  * in the given RelOptInfo.  All Var nodes are restamped with the relid of the
    2322             :  * given relation.
    2323             :  */
    2324             : static void
    2325        9768 : set_baserel_partition_constraint(Relation relation, RelOptInfo *rel)
    2326             : {
    2327             :     List       *partconstr;
    2328             : 
    2329        9768 :     if (rel->partition_qual) /* already done */
    2330           0 :         return;
    2331             : 
    2332             :     /*
    2333             :      * Run the partition quals through const-simplification similar to check
    2334             :      * constraints.  We skip canonicalize_qual, though, because partition
    2335             :      * quals should be in canonical form already; also, since the qual is in
    2336             :      * implicit-AND format, we'd have to explicitly convert it to explicit-AND
    2337             :      * format and back again.
    2338             :      */
    2339        9768 :     partconstr = RelationGetPartitionQual(relation);
    2340        9768 :     if (partconstr)
    2341             :     {
    2342        1942 :         partconstr = (List *) expression_planner((Expr *) partconstr);
    2343        1942 :         if (rel->relid != 1)
    2344        1898 :             ChangeVarNodes((Node *) partconstr, 1, rel->relid, 0);
    2345        1942 :         rel->partition_qual = partconstr;
    2346             :     }
    2347             : }

Generated by: LCOV version 1.13