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

Generated by: LCOV version 1.13