LCOV - code coverage report
Current view: top level - src/backend/optimizer/util - plancat.c (source / functions) Coverage Total Hit
Test: Code coverage Lines: 86.1 % 1172 1009
Test Date: 2026-01-26 10:56:24 Functions: 86.2 % 29 25
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 68.5 % 787 539

             Branch data     Line data    Source code
       1                 :             : /*-------------------------------------------------------------------------
       2                 :             :  *
       3                 :             :  * plancat.c
       4                 :             :  *         routines for accessing the system catalogs
       5                 :             :  *
       6                 :             :  *
       7                 :             :  * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
       8                 :             :  * Portions Copyright (c) 1994, Regents of the University of California
       9                 :             :  *
      10                 :             :  *
      11                 :             :  * IDENTIFICATION
      12                 :             :  *        src/backend/optimizer/util/plancat.c
      13                 :             :  *
      14                 :             :  *-------------------------------------------------------------------------
      15                 :             :  */
      16                 :             : #include "postgres.h"
      17                 :             : 
      18                 :             : #include <math.h>
      19                 :             : 
      20                 :             : #include "access/genam.h"
      21                 :             : #include "access/htup_details.h"
      22                 :             : #include "access/nbtree.h"
      23                 :             : #include "access/sysattr.h"
      24                 :             : #include "access/table.h"
      25                 :             : #include "access/tableam.h"
      26                 :             : #include "access/transam.h"
      27                 :             : #include "access/xlog.h"
      28                 :             : #include "catalog/catalog.h"
      29                 :             : #include "catalog/heap.h"
      30                 :             : #include "catalog/pg_am.h"
      31                 :             : #include "catalog/pg_proc.h"
      32                 :             : #include "catalog/pg_statistic_ext.h"
      33                 :             : #include "catalog/pg_statistic_ext_data.h"
      34                 :             : #include "foreign/fdwapi.h"
      35                 :             : #include "miscadmin.h"
      36                 :             : #include "nodes/makefuncs.h"
      37                 :             : #include "nodes/nodeFuncs.h"
      38                 :             : #include "nodes/supportnodes.h"
      39                 :             : #include "optimizer/cost.h"
      40                 :             : #include "optimizer/optimizer.h"
      41                 :             : #include "optimizer/plancat.h"
      42                 :             : #include "parser/parse_relation.h"
      43                 :             : #include "parser/parsetree.h"
      44                 :             : #include "partitioning/partdesc.h"
      45                 :             : #include "rewrite/rewriteHandler.h"
      46                 :             : #include "rewrite/rewriteManip.h"
      47                 :             : #include "statistics/statistics.h"
      48                 :             : #include "storage/bufmgr.h"
      49                 :             : #include "tcop/tcopprot.h"
      50                 :             : #include "utils/builtins.h"
      51                 :             : #include "utils/lsyscache.h"
      52                 :             : #include "utils/partcache.h"
      53                 :             : #include "utils/rel.h"
      54                 :             : #include "utils/snapmgr.h"
      55                 :             : #include "utils/syscache.h"
      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                 :             : typedef struct NotnullHashEntry
      64                 :             : {
      65                 :             :         Oid                     relid;                  /* OID of the relation */
      66                 :             :         Bitmapset  *notnullattnums; /* attnums of NOT NULL columns */
      67                 :             : } NotnullHashEntry;
      68                 :             : 
      69                 :             : 
      70                 :             : static void get_relation_foreign_keys(PlannerInfo *root, RelOptInfo *rel,
      71                 :             :                                                                           Relation relation, bool inhparent);
      72                 :             : static bool infer_collation_opclass_match(InferenceElem *elem, Relation idxRel,
      73                 :             :                                                                                   List *idxExprs);
      74                 :             : static List *get_relation_constraints(PlannerInfo *root,
      75                 :             :                                                                           Oid relationObjectId, RelOptInfo *rel,
      76                 :             :                                                                           bool include_noinherit,
      77                 :             :                                                                           bool include_notnull,
      78                 :             :                                                                           bool include_partition);
      79                 :             : static List *build_index_tlist(PlannerInfo *root, IndexOptInfo *index,
      80                 :             :                                                            Relation heapRelation);
      81                 :             : static List *get_relation_statistics(PlannerInfo *root, RelOptInfo *rel,
      82                 :             :                                                                          Relation relation);
      83                 :             : static void set_relation_partition_info(PlannerInfo *root, RelOptInfo *rel,
      84                 :             :                                                                                 Relation relation);
      85                 :             : static PartitionScheme find_partition_scheme(PlannerInfo *root,
      86                 :             :                                                                                          Relation relation);
      87                 :             : static void set_baserel_partition_key_exprs(Relation relation,
      88                 :             :                                                                                         RelOptInfo *rel);
      89                 :             : static void set_baserel_partition_constraint(Relation relation,
      90                 :             :                                                                                          RelOptInfo *rel);
      91                 :             : 
      92                 :             : 
      93                 :             : /*
      94                 :             :  * get_relation_info -
      95                 :             :  *        Retrieves catalog information for a given relation.
      96                 :             :  *
      97                 :             :  * Given the Oid of the relation, return the following info into fields
      98                 :             :  * of the RelOptInfo struct:
      99                 :             :  *
     100                 :             :  *      min_attr        lowest valid AttrNumber
     101                 :             :  *      max_attr        highest valid AttrNumber
     102                 :             :  *      indexlist       list of IndexOptInfos for relation's indexes
     103                 :             :  *      statlist        list of StatisticExtInfo for relation's statistic objects
     104                 :             :  *      serverid        if it's a foreign table, the server OID
     105                 :             :  *      fdwroutine      if it's a foreign table, the FDW function pointers
     106                 :             :  *      pages           number of pages
     107                 :             :  *      tuples          number of tuples
     108                 :             :  *      rel_parallel_workers user-defined number of parallel workers
     109                 :             :  *
     110                 :             :  * Also, add information about the relation's foreign keys to root->fkey_list.
     111                 :             :  *
     112                 :             :  * Also, initialize the attr_needed[] and attr_widths[] arrays.  In most
     113                 :             :  * cases these are left as zeroes, but sometimes we need to compute attr
     114                 :             :  * widths here, and we may as well cache the results for costsize.c.
     115                 :             :  *
     116                 :             :  * If inhparent is true, all we need to do is set up the attr arrays:
     117                 :             :  * the RelOptInfo actually represents the appendrel formed by an inheritance
     118                 :             :  * tree, and so the parent rel's physical size and index information isn't
     119                 :             :  * important for it, however, for partitioned tables, we do populate the
     120                 :             :  * indexlist as the planner uses unique indexes as unique proofs for certain
     121                 :             :  * optimizations.
     122                 :             :  */
     123                 :             : void
     124                 :       50915 : get_relation_info(PlannerInfo *root, Oid relationObjectId, bool inhparent,
     125                 :             :                                   RelOptInfo *rel)
     126                 :             : {
     127                 :       50915 :         Index           varno = rel->relid;
     128                 :       50915 :         Relation        relation;
     129                 :       50915 :         bool            hasindex;
     130                 :       50915 :         List       *indexinfos = NIL;
     131                 :             : 
     132                 :             :         /*
     133                 :             :          * We need not lock the relation since it was already locked, either by
     134                 :             :          * the rewriter or when expand_inherited_rtentry() added it to the query's
     135                 :             :          * rangetable.
     136                 :             :          */
     137                 :       50915 :         relation = table_open(relationObjectId, NoLock);
     138                 :             : 
     139                 :             :         /*
     140                 :             :          * Relations without a table AM can be used in a query only if they are of
     141                 :             :          * special-cased relkinds.  This check prevents us from crashing later if,
     142                 :             :          * for example, a view's ON SELECT rule has gone missing.  Note that
     143                 :             :          * table_open() already rejected indexes and composite types; spell the
     144                 :             :          * error the same way it does.
     145                 :             :          */
     146         [ +  + ]:       50915 :         if (!relation->rd_tableam)
     147                 :             :         {
     148   [ +  +  +  - ]:        2361 :                 if (!(relation->rd_rel->relkind == RELKIND_FOREIGN_TABLE ||
     149                 :        2359 :                           relation->rd_rel->relkind == RELKIND_PARTITIONED_TABLE))
     150   [ #  #  #  # ]:           0 :                         ereport(ERROR,
     151                 :             :                                         (errcode(ERRCODE_WRONG_OBJECT_TYPE),
     152                 :             :                                          errmsg("cannot open relation \"%s\"",
     153                 :             :                                                         RelationGetRelationName(relation)),
     154                 :             :                                          errdetail_relkind_not_supported(relation->rd_rel->relkind)));
     155                 :        2361 :         }
     156                 :             : 
     157                 :             :         /* Temporary and unlogged relations are inaccessible during recovery. */
     158   [ +  +  +  - ]:       50915 :         if (!RelationIsPermanent(relation) && RecoveryInProgress())
     159   [ #  #  #  # ]:           0 :                 ereport(ERROR,
     160                 :             :                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
     161                 :             :                                  errmsg("cannot access temporary or unlogged relations during recovery")));
     162                 :             : 
     163                 :       50915 :         rel->min_attr = FirstLowInvalidHeapAttributeNumber + 1;
     164                 :       50915 :         rel->max_attr = RelationGetNumberOfAttributes(relation);
     165                 :       50915 :         rel->reltablespace = RelationGetForm(relation)->reltablespace;
     166                 :             : 
     167         [ +  - ]:       50915 :         Assert(rel->max_attr >= rel->min_attr);
     168                 :       50915 :         rel->attr_needed = (Relids *)
     169                 :       50915 :                 palloc0((rel->max_attr - rel->min_attr + 1) * sizeof(Relids));
     170                 :       50915 :         rel->attr_widths = (int32 *)
     171                 :       50915 :                 palloc0((rel->max_attr - rel->min_attr + 1) * sizeof(int32));
     172                 :             : 
     173                 :             :         /*
     174                 :             :          * Record which columns are defined as NOT NULL.  We leave this
     175                 :             :          * unpopulated for non-partitioned inheritance parent relations as it's
     176                 :             :          * ambiguous as to what it means.  Some child tables may have a NOT NULL
     177                 :             :          * constraint for a column while others may not.  We could work harder and
     178                 :             :          * build a unioned set of all child relations notnullattnums, but there's
     179                 :             :          * currently no need.  The RelOptInfo corresponding to the !inh
     180                 :             :          * RangeTblEntry does get populated.
     181                 :             :          */
     182   [ +  +  +  + ]:       50915 :         if (!inhparent || relation->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
     183                 :       46936 :                 rel->notnullattnums = find_relation_notnullatts(root, relationObjectId);
     184                 :             : 
     185                 :             :         /*
     186                 :             :          * Estimate relation size --- unless it's an inheritance parent, in which
     187                 :             :          * case the size we want is not the rel's own size but the size of its
     188                 :             :          * inheritance tree.  That will be computed in set_append_rel_size().
     189                 :             :          */
     190         [ +  + ]:       50915 :         if (!inhparent)
     191                 :       89160 :                 estimate_rel_size(relation, rel->attr_widths - rel->min_attr,
     192                 :       44580 :                                                   &rel->pages, &rel->tuples, &rel->allvisfrac);
     193                 :             : 
     194                 :             :         /* Retrieve the parallel_workers reloption, or -1 if not set. */
     195         [ +  + ]:       50915 :         rel->rel_parallel_workers = RelationGetParallelWorkers(relation, -1);
     196                 :             : 
     197                 :             :         /*
     198                 :             :          * Make list of indexes.  Ignore indexes on system catalogs if told to.
     199                 :             :          * Don't bother with indexes from traditional inheritance parents.  For
     200                 :             :          * partitioned tables, we need a list of at least unique indexes as these
     201                 :             :          * serve as unique proofs for certain planner optimizations.  However,
     202                 :             :          * let's not discriminate here and just record all partitioned indexes
     203                 :             :          * whether they're unique indexes or not.
     204                 :             :          */
     205         [ +  + ]:       50915 :         if ((inhparent && relation->rd_rel->relkind != RELKIND_PARTITIONED_TABLE)
     206   [ +  +  -  + ]:       50915 :                 || (IgnoreSystemIndexes && IsSystemRelation(relation)))
     207                 :        3979 :                 hasindex = false;
     208                 :             :         else
     209                 :       46936 :                 hasindex = relation->rd_rel->relhasindex;
     210                 :             : 
     211         [ +  + ]:       50915 :         if (hasindex)
     212                 :             :         {
     213                 :       34596 :                 List       *indexoidlist;
     214                 :       34596 :                 LOCKMODE        lmode;
     215                 :       34596 :                 ListCell   *l;
     216                 :             : 
     217                 :       34596 :                 indexoidlist = RelationGetIndexList(relation);
     218                 :             : 
     219                 :             :                 /*
     220                 :             :                  * For each index, we get the same type of lock that the executor will
     221                 :             :                  * need, and do not release it.  This saves a couple of trips to the
     222                 :             :                  * shared lock manager while not creating any real loss of
     223                 :             :                  * concurrency, because no schema changes could be happening on the
     224                 :             :                  * index while we hold lock on the parent rel, and no lock type used
     225                 :             :                  * for queries blocks any other kind of index operation.
     226                 :             :                  */
     227                 :       34596 :                 lmode = root->simple_rte_array[varno]->rellockmode;
     228                 :             : 
     229   [ +  +  +  +  :      107572 :                 foreach(l, indexoidlist)
                   +  + ]
     230                 :             :                 {
     231                 :       72976 :                         Oid                     indexoid = lfirst_oid(l);
     232                 :       72976 :                         Relation        indexRelation;
     233                 :       72976 :                         Form_pg_index index;
     234                 :       72976 :                         const IndexAmRoutine *amroutine = NULL;
     235                 :       72976 :                         IndexOptInfo *info;
     236                 :       72976 :                         int                     ncolumns,
     237                 :             :                                                 nkeycolumns;
     238                 :       72976 :                         int                     i;
     239                 :             : 
     240                 :             :                         /*
     241                 :             :                          * Extract info from the relation descriptor for the index.
     242                 :             :                          */
     243                 :       72976 :                         indexRelation = index_open(indexoid, lmode);
     244                 :       72976 :                         index = indexRelation->rd_index;
     245                 :             : 
     246                 :             :                         /*
     247                 :             :                          * Ignore invalid indexes, since they can't safely be used for
     248                 :             :                          * queries.  Note that this is OK because the data structure we
     249                 :             :                          * are constructing is only used by the planner --- the executor
     250                 :             :                          * still needs to insert into "invalid" indexes, if they're marked
     251                 :             :                          * indisready.
     252                 :             :                          */
     253         [ +  + ]:       72976 :                         if (!index->indisvalid)
     254                 :             :                         {
     255                 :           2 :                                 index_close(indexRelation, NoLock);
     256                 :           2 :                                 continue;
     257                 :             :                         }
     258                 :             : 
     259                 :             :                         /*
     260                 :             :                          * If the index is valid, but cannot yet be used, ignore it; but
     261                 :             :                          * mark the plan we are generating as transient. See
     262                 :             :                          * src/backend/access/heap/README.HOT for discussion.
     263                 :             :                          */
     264   [ +  +  -  + ]:       72974 :                         if (index->indcheckxmin &&
     265                 :          98 :                                 !TransactionIdPrecedes(HeapTupleHeaderGetXmin(indexRelation->rd_indextuple->t_data),
     266                 :          49 :                                                                            TransactionXmin))
     267                 :             :                         {
     268                 :          49 :                                 root->glob->transientPlan = true;
     269                 :          49 :                                 index_close(indexRelation, NoLock);
     270                 :          49 :                                 continue;
     271                 :             :                         }
     272                 :             : 
     273                 :       72925 :                         info = makeNode(IndexOptInfo);
     274                 :             : 
     275                 :       72925 :                         info->indexoid = index->indexrelid;
     276                 :       72925 :                         info->reltablespace =
     277                 :       72925 :                                 RelationGetForm(indexRelation)->reltablespace;
     278                 :       72925 :                         info->rel = rel;
     279                 :       72925 :                         info->ncolumns = ncolumns = index->indnatts;
     280                 :       72925 :                         info->nkeycolumns = nkeycolumns = index->indnkeyatts;
     281                 :             : 
     282                 :       72925 :                         info->indexkeys = palloc_array(int, ncolumns);
     283                 :       72925 :                         info->indexcollations = palloc_array(Oid, nkeycolumns);
     284                 :       72925 :                         info->opfamily = palloc_array(Oid, nkeycolumns);
     285                 :       72925 :                         info->opcintype = palloc_array(Oid, nkeycolumns);
     286                 :       72925 :                         info->canreturn = palloc_array(bool, ncolumns);
     287                 :             : 
     288         [ +  + ]:      220972 :                         for (i = 0; i < ncolumns; i++)
     289                 :             :                         {
     290                 :      148047 :                                 info->indexkeys[i] = index->indkey.values[i];
     291                 :      148047 :                                 info->canreturn[i] = index_can_return(indexRelation, i + 1);
     292                 :      148047 :                         }
     293                 :             : 
     294         [ +  + ]:      220929 :                         for (i = 0; i < nkeycolumns; i++)
     295                 :             :                         {
     296                 :      148004 :                                 info->opfamily[i] = indexRelation->rd_opfamily[i];
     297                 :      148004 :                                 info->opcintype[i] = indexRelation->rd_opcintype[i];
     298                 :      148004 :                                 info->indexcollations[i] = indexRelation->rd_indcollation[i];
     299                 :      148004 :                         }
     300                 :             : 
     301                 :       72925 :                         info->relam = indexRelation->rd_rel->relam;
     302                 :             : 
     303                 :             :                         /*
     304                 :             :                          * We don't have an AM for partitioned indexes, so we'll just
     305                 :             :                          * NULLify the AM related fields for those.
     306                 :             :                          */
     307         [ +  + ]:       72925 :                         if (indexRelation->rd_rel->relkind != RELKIND_PARTITIONED_INDEX)
     308                 :             :                         {
     309                 :             :                                 /* We copy just the fields we need, not all of rd_indam */
     310                 :       72308 :                                 amroutine = indexRelation->rd_indam;
     311                 :       72308 :                                 info->amcanorderbyop = amroutine->amcanorderbyop;
     312                 :       72308 :                                 info->amoptionalkey = amroutine->amoptionalkey;
     313                 :       72308 :                                 info->amsearcharray = amroutine->amsearcharray;
     314                 :       72308 :                                 info->amsearchnulls = amroutine->amsearchnulls;
     315                 :       72308 :                                 info->amcanparallel = amroutine->amcanparallel;
     316                 :       72308 :                                 info->amhasgettuple = (amroutine->amgettuple != NULL);
     317         [ -  + ]:      144616 :                                 info->amhasgetbitmap = amroutine->amgetbitmap != NULL &&
     318                 :       72308 :                                         relation->rd_tableam->scan_bitmap_next_tuple != NULL;
     319         [ +  + ]:      141436 :                                 info->amcanmarkpos = (amroutine->ammarkpos != NULL &&
     320                 :       69128 :                                                                           amroutine->amrestrpos != NULL);
     321                 :       72308 :                                 info->amcostestimate = amroutine->amcostestimate;
     322         [ -  + ]:       72308 :                                 Assert(info->amcostestimate != NULL);
     323                 :             : 
     324                 :             :                                 /* Fetch index opclass options */
     325                 :       72308 :                                 info->opclassoptions = RelationGetIndexAttOptions(indexRelation, true);
     326                 :             : 
     327                 :             :                                 /*
     328                 :             :                                  * Fetch the ordering information for the index, if any.
     329                 :             :                                  */
     330         [ +  + ]:       72308 :                                 if (info->relam == BTREE_AM_OID)
     331                 :             :                                 {
     332                 :             :                                         /*
     333                 :             :                                          * If it's a btree index, we can use its opfamily OIDs
     334                 :             :                                          * directly as the sort ordering opfamily OIDs.
     335                 :             :                                          */
     336         [ -  + ]:       69128 :                                         Assert(amroutine->amcanorder);
     337                 :             : 
     338                 :       69128 :                                         info->sortopfamily = info->opfamily;
     339                 :       69128 :                                         info->reverse_sort = palloc_array(bool, nkeycolumns);
     340                 :       69128 :                                         info->nulls_first = palloc_array(bool, nkeycolumns);
     341                 :             : 
     342         [ +  + ]:      170041 :                                         for (i = 0; i < nkeycolumns; i++)
     343                 :             :                                         {
     344                 :      100913 :                                                 int16           opt = indexRelation->rd_indoption[i];
     345                 :             : 
     346                 :      100913 :                                                 info->reverse_sort[i] = (opt & INDOPTION_DESC) != 0;
     347                 :      100913 :                                                 info->nulls_first[i] = (opt & INDOPTION_NULLS_FIRST) != 0;
     348                 :      100913 :                                         }
     349                 :       69128 :                                 }
     350         [ -  + ]:        3180 :                                 else if (amroutine->amcanorder)
     351                 :             :                                 {
     352                 :             :                                         /*
     353                 :             :                                          * Otherwise, identify the corresponding btree opfamilies
     354                 :             :                                          * by trying to map this index's "<" operators into btree.
     355                 :             :                                          * Since "<" uniquely defines the behavior of a sort
     356                 :             :                                          * order, this is a sufficient test.
     357                 :             :                                          *
     358                 :             :                                          * XXX This method is rather slow and complicated.  It'd
     359                 :             :                                          * be better to have a way to explicitly declare the
     360                 :             :                                          * corresponding btree opfamily for each opfamily of the
     361                 :             :                                          * other index type.
     362                 :             :                                          */
     363                 :           0 :                                         info->sortopfamily = palloc_array(Oid, nkeycolumns);
     364                 :           0 :                                         info->reverse_sort = palloc_array(bool, nkeycolumns);
     365                 :           0 :                                         info->nulls_first = palloc_array(bool, nkeycolumns);
     366                 :             : 
     367         [ #  # ]:           0 :                                         for (i = 0; i < nkeycolumns; i++)
     368                 :             :                                         {
     369                 :           0 :                                                 int16           opt = indexRelation->rd_indoption[i];
     370                 :           0 :                                                 Oid                     ltopr;
     371                 :           0 :                                                 Oid                     opfamily;
     372                 :           0 :                                                 Oid                     opcintype;
     373                 :           0 :                                                 CompareType cmptype;
     374                 :             : 
     375                 :           0 :                                                 info->reverse_sort[i] = (opt & INDOPTION_DESC) != 0;
     376                 :           0 :                                                 info->nulls_first[i] = (opt & INDOPTION_NULLS_FIRST) != 0;
     377                 :             : 
     378                 :           0 :                                                 ltopr = get_opfamily_member_for_cmptype(info->opfamily[i],
     379                 :           0 :                                                                                                                                 info->opcintype[i],
     380                 :           0 :                                                                                                                                 info->opcintype[i],
     381                 :             :                                                                                                                                 COMPARE_LT);
     382         [ #  # ]:           0 :                                                 if (OidIsValid(ltopr) &&
     383                 :           0 :                                                         get_ordering_op_properties(ltopr,
     384                 :             :                                                                                                            &opfamily,
     385                 :             :                                                                                                            &opcintype,
     386         [ #  # ]:           0 :                                                                                                            &cmptype) &&
     387   [ #  #  #  # ]:           0 :                                                         opcintype == info->opcintype[i] &&
     388                 :           0 :                                                         cmptype == COMPARE_LT)
     389                 :             :                                                 {
     390                 :             :                                                         /* Successful mapping */
     391                 :           0 :                                                         info->sortopfamily[i] = opfamily;
     392                 :           0 :                                                 }
     393                 :             :                                                 else
     394                 :             :                                                 {
     395                 :             :                                                         /* Fail ... quietly treat index as unordered */
     396                 :           0 :                                                         info->sortopfamily = NULL;
     397                 :           0 :                                                         info->reverse_sort = NULL;
     398                 :           0 :                                                         info->nulls_first = NULL;
     399                 :           0 :                                                         break;
     400                 :             :                                                 }
     401         [ #  # ]:           0 :                                         }
     402                 :           0 :                                 }
     403                 :             :                                 else
     404                 :             :                                 {
     405                 :        3180 :                                         info->sortopfamily = NULL;
     406                 :        3180 :                                         info->reverse_sort = NULL;
     407                 :        3180 :                                         info->nulls_first = NULL;
     408                 :             :                                 }
     409                 :       72308 :                         }
     410                 :             :                         else
     411                 :             :                         {
     412                 :         617 :                                 info->amcanorderbyop = false;
     413                 :         617 :                                 info->amoptionalkey = false;
     414                 :         617 :                                 info->amsearcharray = false;
     415                 :         617 :                                 info->amsearchnulls = false;
     416                 :         617 :                                 info->amcanparallel = false;
     417                 :         617 :                                 info->amhasgettuple = false;
     418                 :         617 :                                 info->amhasgetbitmap = false;
     419                 :         617 :                                 info->amcanmarkpos = false;
     420                 :         617 :                                 info->amcostestimate = NULL;
     421                 :             : 
     422                 :         617 :                                 info->sortopfamily = NULL;
     423                 :         617 :                                 info->reverse_sort = NULL;
     424                 :         617 :                                 info->nulls_first = NULL;
     425                 :             :                         }
     426                 :             : 
     427                 :             :                         /*
     428                 :             :                          * Fetch the index expressions and predicate, if any.  We must
     429                 :             :                          * modify the copies we obtain from the relcache to have the
     430                 :             :                          * correct varno for the parent relation, so that they match up
     431                 :             :                          * correctly against qual clauses.
     432                 :             :                          *
     433                 :             :                          * After fixing the varnos, we need to run the index expressions
     434                 :             :                          * and predicate through const-simplification again, using a valid
     435                 :             :                          * "root".  This ensures that NullTest quals for Vars can be
     436                 :             :                          * properly reduced.
     437                 :             :                          */
     438                 :       72925 :                         info->indexprs = RelationGetIndexExpressions(indexRelation);
     439                 :       72925 :                         info->indpred = RelationGetIndexPredicate(indexRelation);
     440         [ +  + ]:       72925 :                         if (info->indexprs)
     441                 :             :                         {
     442         [ +  + ]:         490 :                                 if (varno != 1)
     443                 :         323 :                                         ChangeVarNodes((Node *) info->indexprs, 1, varno, 0);
     444                 :             : 
     445                 :         490 :                                 info->indexprs = (List *)
     446                 :         490 :                                         eval_const_expressions(root, (Node *) info->indexprs);
     447                 :         490 :                         }
     448         [ +  + ]:       72925 :                         if (info->indpred)
     449                 :             :                         {
     450         [ +  + ]:         147 :                                 if (varno != 1)
     451                 :          21 :                                         ChangeVarNodes((Node *) info->indpred, 1, varno, 0);
     452                 :             : 
     453                 :         147 :                                 info->indpred = (List *)
     454                 :         294 :                                         eval_const_expressions(root,
     455                 :         147 :                                                                                    (Node *) make_ands_explicit(info->indpred));
     456                 :         147 :                                 info->indpred = make_ands_implicit((Expr *) info->indpred);
     457                 :         147 :                         }
     458                 :             : 
     459                 :             :                         /* Build targetlist using the completed indexprs data */
     460                 :       72925 :                         info->indextlist = build_index_tlist(root, info, relation);
     461                 :             : 
     462                 :       72925 :                         info->indrestrictinfo = NIL; /* set later, in indxpath.c */
     463                 :       72925 :                         info->predOK = false;        /* set later, in indxpath.c */
     464                 :       72925 :                         info->unique = index->indisunique;
     465                 :       72925 :                         info->nullsnotdistinct = index->indnullsnotdistinct;
     466                 :       72925 :                         info->immediate = index->indimmediate;
     467                 :       72925 :                         info->hypothetical = false;
     468                 :             : 
     469                 :             :                         /*
     470                 :             :                          * Estimate the index size.  If it's not a partial index, we lock
     471                 :             :                          * the number-of-tuples estimate to equal the parent table; if it
     472                 :             :                          * is partial then we have to use the same methods as we would for
     473                 :             :                          * a table, except we can be sure that the index is not larger
     474                 :             :                          * than the table.  We must ignore partitioned indexes here as
     475                 :             :                          * there are not physical indexes.
     476                 :             :                          */
     477         [ +  + ]:       72925 :                         if (indexRelation->rd_rel->relkind != RELKIND_PARTITIONED_INDEX)
     478                 :             :                         {
     479         [ +  + ]:       72308 :                                 if (info->indpred == NIL)
     480                 :             :                                 {
     481                 :       72165 :                                         info->pages = RelationGetNumberOfBlocks(indexRelation);
     482                 :       72165 :                                         info->tuples = rel->tuples;
     483                 :       72165 :                                 }
     484                 :             :                                 else
     485                 :             :                                 {
     486                 :         143 :                                         double          allvisfrac; /* dummy */
     487                 :             : 
     488                 :         286 :                                         estimate_rel_size(indexRelation, NULL,
     489                 :         143 :                                                                           &info->pages, &info->tuples, &allvisfrac);
     490         [ +  + ]:         143 :                                         if (info->tuples > rel->tuples)
     491                 :           3 :                                                 info->tuples = rel->tuples;
     492                 :         143 :                                 }
     493                 :             : 
     494                 :             :                                 /*
     495                 :             :                                  * Get tree height while we have the index open
     496                 :             :                                  */
     497         [ +  + ]:       72308 :                                 if (amroutine->amgettreeheight)
     498                 :             :                                 {
     499                 :       69128 :                                         info->tree_height = amroutine->amgettreeheight(indexRelation);
     500                 :       69128 :                                 }
     501                 :             :                                 else
     502                 :             :                                 {
     503                 :             :                                         /* For other index types, just set it to "unknown" for now */
     504                 :        3180 :                                         info->tree_height = -1;
     505                 :             :                                 }
     506                 :       72308 :                         }
     507                 :             :                         else
     508                 :             :                         {
     509                 :             :                                 /* Zero these out for partitioned indexes */
     510                 :         617 :                                 info->pages = 0;
     511                 :         617 :                                 info->tuples = 0.0;
     512                 :         617 :                                 info->tree_height = -1;
     513                 :             :                         }
     514                 :             : 
     515                 :       72925 :                         index_close(indexRelation, NoLock);
     516                 :             : 
     517                 :             :                         /*
     518                 :             :                          * We've historically used lcons() here.  It'd make more sense to
     519                 :             :                          * use lappend(), but that causes the planner to change behavior
     520                 :             :                          * in cases where two indexes seem equally attractive.  For now,
     521                 :             :                          * stick with lcons() --- few tables should have so many indexes
     522                 :             :                          * that the O(N^2) behavior of lcons() is really a problem.
     523                 :             :                          */
     524                 :       72925 :                         indexinfos = lcons(info, indexinfos);
     525         [ +  + ]:       72976 :                 }
     526                 :             : 
     527                 :       34596 :                 list_free(indexoidlist);
     528                 :       34596 :         }
     529                 :             : 
     530                 :       50915 :         rel->indexlist = indexinfos;
     531                 :             : 
     532                 :       50915 :         rel->statlist = get_relation_statistics(root, rel, relation);
     533                 :             : 
     534                 :             :         /* Grab foreign-table info using the relcache, while we have it */
     535         [ +  + ]:       50915 :         if (relation->rd_rel->relkind == RELKIND_FOREIGN_TABLE)
     536                 :             :         {
     537                 :             :                 /* Check if the access to foreign tables is restricted */
     538         [ +  - ]:           2 :                 if (unlikely((restrict_nonsystem_relation_kind & RESTRICT_RELKIND_FOREIGN_TABLE) != 0))
     539                 :             :                 {
     540                 :             :                         /* there must not be built-in foreign tables */
     541         [ #  # ]:           0 :                         Assert(RelationGetRelid(relation) >= FirstNormalObjectId);
     542                 :             : 
     543   [ #  #  #  # ]:           0 :                         ereport(ERROR,
     544                 :             :                                         (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
     545                 :             :                                          errmsg("access to non-system foreign table is restricted")));
     546                 :           0 :                 }
     547                 :             : 
     548                 :           2 :                 rel->serverid = GetForeignServerIdByRelId(RelationGetRelid(relation));
     549                 :           2 :                 rel->fdwroutine = GetFdwRoutineForRelation(relation, true);
     550                 :           2 :         }
     551                 :             :         else
     552                 :             :         {
     553                 :       50913 :                 rel->serverid = InvalidOid;
     554                 :       50913 :                 rel->fdwroutine = NULL;
     555                 :             :         }
     556                 :             : 
     557                 :             :         /* Collect info about relation's foreign keys, if relevant */
     558                 :       50915 :         get_relation_foreign_keys(root, rel, relation, inhparent);
     559                 :             : 
     560                 :             :         /* Collect info about functions implemented by the rel's table AM. */
     561         [ +  + ]:       50915 :         if (relation->rd_tableam &&
     562   [ +  -  +  - ]:       48554 :                 relation->rd_tableam->scan_set_tidrange != NULL &&
     563                 :       48554 :                 relation->rd_tableam->scan_getnextslot_tidrange != NULL)
     564                 :       48554 :                 rel->amflags |= AMFLAG_HAS_TID_RANGE;
     565                 :             : 
     566                 :             :         /*
     567                 :             :          * Collect info about relation's partitioning scheme, if any. Only
     568                 :             :          * inheritance parents may be partitioned.
     569                 :             :          */
     570   [ +  +  +  + ]:       50915 :         if (inhparent && relation->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
     571                 :        2356 :                 set_relation_partition_info(root, rel, relation);
     572                 :             : 
     573                 :       50915 :         table_close(relation, NoLock);
     574                 :             : 
     575                 :             :         /*
     576                 :             :          * Allow a plugin to editorialize on the info we obtained from the
     577                 :             :          * catalogs.  Actions might include altering the assumed relation size,
     578                 :             :          * removing an index, or adding a hypothetical index to the indexlist.
     579                 :             :          *
     580                 :             :          * An extension can also modify rel->pgs_mask here to control path
     581                 :             :          * generation.
     582                 :             :          */
     583         [ +  + ]:       50915 :         if (get_relation_info_hook)
     584                 :       50880 :                 (*get_relation_info_hook) (root, relationObjectId, inhparent, rel);
     585                 :       50915 : }
     586                 :             : 
     587                 :             : /*
     588                 :             :  * get_relation_foreign_keys -
     589                 :             :  *        Retrieves foreign key information for a given relation.
     590                 :             :  *
     591                 :             :  * ForeignKeyOptInfos for relevant foreign keys are created and added to
     592                 :             :  * root->fkey_list.  We do this now while we have the relcache entry open.
     593                 :             :  * We could sometimes avoid making useless ForeignKeyOptInfos if we waited
     594                 :             :  * until all RelOptInfos have been built, but the cost of re-opening the
     595                 :             :  * relcache entries would probably exceed any savings.
     596                 :             :  */
     597                 :             : static void
     598                 :       50913 : get_relation_foreign_keys(PlannerInfo *root, RelOptInfo *rel,
     599                 :             :                                                   Relation relation, bool inhparent)
     600                 :             : {
     601                 :       50913 :         List       *rtable = root->parse->rtable;
     602                 :       50913 :         List       *cachedfkeys;
     603                 :       50913 :         ListCell   *lc;
     604                 :             : 
     605                 :             :         /*
     606                 :             :          * If it's not a baserel, we don't care about its FKs.  Also, if the query
     607                 :             :          * references only a single relation, we can skip the lookup since no FKs
     608                 :             :          * could satisfy the requirements below.
     609                 :             :          */
     610   [ +  +  +  + ]:       50913 :         if (rel->reloptkind != RELOPT_BASEREL ||
     611                 :       44356 :                 list_length(rtable) < 2)
     612                 :       28466 :                 return;
     613                 :             : 
     614                 :             :         /*
     615                 :             :          * If it's the parent of an inheritance tree, ignore its FKs.  We could
     616                 :             :          * make useful FK-based deductions if we found that all members of the
     617                 :             :          * inheritance tree have equivalent FK constraints, but detecting that
     618                 :             :          * would require code that hasn't been written.
     619                 :             :          */
     620         [ +  + ]:       22447 :         if (inhparent)
     621                 :         961 :                 return;
     622                 :             : 
     623                 :             :         /*
     624                 :             :          * Extract data about relation's FKs from the relcache.  Note that this
     625                 :             :          * list belongs to the relcache and might disappear in a cache flush, so
     626                 :             :          * we must not do any further catalog access within this function.
     627                 :             :          */
     628                 :       21486 :         cachedfkeys = RelationGetFKeyList(relation);
     629                 :             : 
     630                 :             :         /*
     631                 :             :          * Figure out which FKs are of interest for this query, and create
     632                 :             :          * ForeignKeyOptInfos for them.  We want only FKs that reference some
     633                 :             :          * other RTE of the current query.  In queries containing self-joins,
     634                 :             :          * there might be more than one other RTE for a referenced table, and we
     635                 :             :          * should make a ForeignKeyOptInfo for each occurrence.
     636                 :             :          *
     637                 :             :          * Ideally, we would ignore RTEs that correspond to non-baserels, but it's
     638                 :             :          * too hard to identify those here, so we might end up making some useless
     639                 :             :          * ForeignKeyOptInfos.  If so, match_foreign_keys_to_quals() will remove
     640                 :             :          * them again.
     641                 :             :          */
     642   [ +  +  +  +  :       21883 :         foreach(lc, cachedfkeys)
                   +  + ]
     643                 :             :         {
     644                 :         397 :                 ForeignKeyCacheInfo *cachedfk = (ForeignKeyCacheInfo *) lfirst(lc);
     645                 :         397 :                 Index           rti;
     646                 :         397 :                 ListCell   *lc2;
     647                 :             : 
     648                 :             :                 /* conrelid should always be that of the table we're considering */
     649         [ +  - ]:         397 :                 Assert(cachedfk->conrelid == RelationGetRelid(relation));
     650                 :             : 
     651                 :             :                 /* skip constraints currently not enforced */
     652         [ +  + ]:         397 :                 if (!cachedfk->conenforced)
     653                 :           3 :                         continue;
     654                 :             : 
     655                 :             :                 /* Scan to find other RTEs matching confrelid */
     656                 :         394 :                 rti = 0;
     657   [ +  -  +  +  :        1822 :                 foreach(lc2, rtable)
                   +  + ]
     658                 :             :                 {
     659                 :        1428 :                         RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc2);
     660                 :        1428 :                         ForeignKeyOptInfo *info;
     661                 :             : 
     662                 :        1428 :                         rti++;
     663                 :             :                         /* Ignore if not the correct table */
     664   [ +  +  +  + ]:        1428 :                         if (rte->rtekind != RTE_RELATION ||
     665                 :         877 :                                 rte->relid != cachedfk->confrelid)
     666                 :        1081 :                                 continue;
     667                 :             :                         /* Ignore if it's an inheritance parent; doesn't really match */
     668         [ +  + ]:         347 :                         if (rte->inh)
     669                 :          36 :                                 continue;
     670                 :             :                         /* Ignore self-referential FKs; we only care about joins */
     671         [ +  + ]:         311 :                         if (rti == rel->relid)
     672                 :          22 :                                 continue;
     673                 :             : 
     674                 :             :                         /* OK, let's make an entry */
     675                 :         289 :                         info = makeNode(ForeignKeyOptInfo);
     676                 :         289 :                         info->con_relid = rel->relid;
     677                 :         289 :                         info->ref_relid = rti;
     678                 :         289 :                         info->nkeys = cachedfk->nkeys;
     679                 :         289 :                         memcpy(info->conkey, cachedfk->conkey, sizeof(info->conkey));
     680                 :         289 :                         memcpy(info->confkey, cachedfk->confkey, sizeof(info->confkey));
     681                 :         289 :                         memcpy(info->conpfeqop, cachedfk->conpfeqop, sizeof(info->conpfeqop));
     682                 :             :                         /* zero out fields to be filled by match_foreign_keys_to_quals */
     683                 :         289 :                         info->nmatched_ec = 0;
     684                 :         289 :                         info->nconst_ec = 0;
     685                 :         289 :                         info->nmatched_rcols = 0;
     686                 :         289 :                         info->nmatched_ri = 0;
     687                 :         289 :                         memset(info->eclass, 0, sizeof(info->eclass));
     688                 :         289 :                         memset(info->fk_eclass_member, 0, sizeof(info->fk_eclass_member));
     689                 :         289 :                         memset(info->rinfos, 0, sizeof(info->rinfos));
     690                 :             : 
     691                 :         289 :                         root->fkey_list = lappend(root->fkey_list, info);
     692         [ +  + ]:        1428 :                 }
     693         [ +  + ]:         397 :         }
     694                 :       50913 : }
     695                 :             : 
     696                 :             : /*
     697                 :             :  * get_relation_notnullatts -
     698                 :             :  *        Retrieves column not-null constraint information for a given relation.
     699                 :             :  *
     700                 :             :  * We do this while we have the relcache entry open, and store the column
     701                 :             :  * not-null constraint information in a hash table based on the relation OID.
     702                 :             :  */
     703                 :             : void
     704                 :       53612 : get_relation_notnullatts(PlannerInfo *root, Relation relation)
     705                 :             : {
     706                 :       53612 :         Oid                     relid = RelationGetRelid(relation);
     707                 :       53612 :         NotnullHashEntry *hentry;
     708                 :       53612 :         bool            found;
     709                 :       53612 :         Bitmapset  *notnullattnums = NULL;
     710                 :             : 
     711                 :             :         /* bail out if the relation has no not-null constraints */
     712   [ +  +  +  + ]:       53612 :         if (relation->rd_att->constr == NULL ||
     713                 :       31374 :                 !relation->rd_att->constr->has_not_null)
     714                 :       23052 :                 return;
     715                 :             : 
     716                 :             :         /* create the hash table if it hasn't been created yet */
     717         [ +  + ]:       30560 :         if (root->glob->rel_notnullatts_hash == NULL)
     718                 :             :         {
     719                 :       15070 :                 HTAB       *hashtab;
     720                 :       15070 :                 HASHCTL         hash_ctl;
     721                 :             : 
     722                 :       15070 :                 hash_ctl.keysize = sizeof(Oid);
     723                 :       15070 :                 hash_ctl.entrysize = sizeof(NotnullHashEntry);
     724                 :       15070 :                 hash_ctl.hcxt = CurrentMemoryContext;
     725                 :             : 
     726                 :       15070 :                 hashtab = hash_create("Relation NOT NULL attnums",
     727                 :             :                                                           64L,  /* arbitrary initial size */
     728                 :             :                                                           &hash_ctl,
     729                 :             :                                                           HASH_ELEM | HASH_BLOBS | HASH_CONTEXT);
     730                 :             : 
     731                 :       15070 :                 root->glob->rel_notnullatts_hash = hashtab;
     732                 :       15070 :         }
     733                 :             : 
     734                 :             :         /*
     735                 :             :          * Create a hash entry for this relation OID, if we don't have one
     736                 :             :          * already.
     737                 :             :          */
     738                 :       30560 :         hentry = (NotnullHashEntry *) hash_search(root->glob->rel_notnullatts_hash,
     739                 :             :                                                                                           &relid,
     740                 :             :                                                                                           HASH_ENTER,
     741                 :             :                                                                                           &found);
     742                 :             : 
     743                 :             :         /* bail out if a hash entry already exists for this relation OID */
     744         [ +  + ]:       30560 :         if (found)
     745                 :        3692 :                 return;
     746                 :             : 
     747                 :             :         /* collect the column not-null constraint information for this relation */
     748         [ +  + ]:      384006 :         for (int i = 0; i < relation->rd_att->natts; i++)
     749                 :             :         {
     750                 :      357138 :                 CompactAttribute *attr = TupleDescCompactAttr(relation->rd_att, i);
     751                 :             : 
     752         [ -  + ]:      357138 :                 Assert(attr->attnullability != ATTNULLABLE_UNKNOWN);
     753                 :             : 
     754         [ +  + ]:      357138 :                 if (attr->attnullability == ATTNULLABLE_VALID)
     755                 :             :                 {
     756                 :      297520 :                         notnullattnums = bms_add_member(notnullattnums, i + 1);
     757                 :             : 
     758                 :             :                         /*
     759                 :             :                          * Per RemoveAttributeById(), dropped columns will have their
     760                 :             :                          * attnotnull unset, so we needn't check for dropped columns in
     761                 :             :                          * the above condition.
     762                 :             :                          */
     763         [ -  + ]:      297520 :                         Assert(!attr->attisdropped);
     764                 :      297520 :                 }
     765                 :      357138 :         }
     766                 :             : 
     767                 :             :         /* ... and initialize the new hash entry */
     768                 :       26868 :         hentry->notnullattnums = notnullattnums;
     769         [ -  + ]:       53612 : }
     770                 :             : 
     771                 :             : /*
     772                 :             :  * find_relation_notnullatts -
     773                 :             :  *        Searches the hash table and returns the column not-null constraint
     774                 :             :  *        information for a given relation.
     775                 :             :  */
     776                 :             : Bitmapset *
     777                 :       48856 : find_relation_notnullatts(PlannerInfo *root, Oid relid)
     778                 :             : {
     779                 :       48856 :         NotnullHashEntry *hentry;
     780                 :       48856 :         bool            found;
     781                 :             : 
     782         [ +  + ]:       48856 :         if (root->glob->rel_notnullatts_hash == NULL)
     783                 :       18159 :                 return NULL;
     784                 :             : 
     785                 :       30697 :         hentry = (NotnullHashEntry *) hash_search(root->glob->rel_notnullatts_hash,
     786                 :             :                                                                                           &relid,
     787                 :             :                                                                                           HASH_FIND,
     788                 :             :                                                                                           &found);
     789         [ +  + ]:       30697 :         if (!found)
     790                 :         561 :                 return NULL;
     791                 :             : 
     792                 :       30136 :         return hentry->notnullattnums;
     793                 :       48856 : }
     794                 :             : 
     795                 :             : /*
     796                 :             :  * infer_arbiter_indexes -
     797                 :             :  *        Determine the unique indexes used to arbitrate speculative insertion.
     798                 :             :  *
     799                 :             :  * Uses user-supplied inference clause expressions and predicate to match a
     800                 :             :  * unique index from those defined and ready on the heap relation (target).
     801                 :             :  * An exact match is required on columns/expressions (although they can appear
     802                 :             :  * in any order).  However, the predicate given by the user need only restrict
     803                 :             :  * insertion to a subset of some part of the table covered by some particular
     804                 :             :  * unique index (in particular, a partial unique index) in order to be
     805                 :             :  * inferred.
     806                 :             :  *
     807                 :             :  * The implementation does not consider which B-Tree operator class any
     808                 :             :  * particular available unique index attribute uses, unless one was specified
     809                 :             :  * in the inference specification. The same is true of collations.  In
     810                 :             :  * particular, there is no system dependency on the default operator class for
     811                 :             :  * the purposes of inference.  If no opclass (or collation) is specified, then
     812                 :             :  * all matching indexes (that may or may not match the default in terms of
     813                 :             :  * each attribute opclass/collation) are used for inference.
     814                 :             :  */
     815                 :             : List *
     816                 :         284 : infer_arbiter_indexes(PlannerInfo *root)
     817                 :             : {
     818                 :         284 :         OnConflictExpr *onconflict = root->parse->onConflict;
     819                 :             : 
     820                 :             :         /* Iteration state */
     821                 :         284 :         Index           varno;
     822                 :         284 :         RangeTblEntry *rte;
     823                 :         284 :         Relation        relation;
     824                 :         284 :         Oid                     indexOidFromConstraint = InvalidOid;
     825                 :         284 :         List       *indexList;
     826                 :         284 :         List       *indexRelList = NIL;
     827                 :             : 
     828                 :             :         /*
     829                 :             :          * Required attributes and expressions used to match indexes to the clause
     830                 :             :          * given by the user.  In the ON CONFLICT ON CONSTRAINT case, we compute
     831                 :             :          * these from that constraint's index to match all other indexes, to
     832                 :             :          * account for the case where that index is being concurrently reindexed.
     833                 :             :          */
     834                 :         284 :         List       *inferIndexExprs = (List *) onconflict->arbiterWhere;
     835                 :         284 :         Bitmapset  *inferAttrs = NULL;
     836                 :         284 :         List       *inferElems = NIL;
     837                 :             : 
     838                 :             :         /* Results */
     839                 :         284 :         List       *results = NIL;
     840                 :         284 :         bool            foundValid = false;
     841                 :             : 
     842                 :             :         /*
     843                 :             :          * Quickly return NIL for ON CONFLICT DO NOTHING without an inference
     844                 :             :          * specification or named constraint.  ON CONFLICT DO UPDATE statements
     845                 :             :          * must always provide one or the other (but parser ought to have caught
     846                 :             :          * that already).
     847                 :             :          */
     848   [ +  +  +  + ]:         284 :         if (onconflict->arbiterElems == NIL &&
     849                 :          55 :                 onconflict->constraint == InvalidOid)
     850                 :          23 :                 return NIL;
     851                 :             : 
     852                 :             :         /*
     853                 :             :          * We need not lock the relation since it was already locked, either by
     854                 :             :          * the rewriter or when expand_inherited_rtentry() added it to the query's
     855                 :             :          * rangetable.
     856                 :             :          */
     857                 :         261 :         varno = root->parse->resultRelation;
     858                 :         261 :         rte = rt_fetch(varno, root->parse->rtable);
     859                 :             : 
     860                 :         261 :         relation = table_open(rte->relid, NoLock);
     861                 :             : 
     862                 :             :         /*
     863                 :             :          * Build normalized/BMS representation of plain indexed attributes, as
     864                 :             :          * well as a separate list of expression items.  This simplifies matching
     865                 :             :          * the cataloged definition of indexes.
     866                 :             :          */
     867   [ +  +  +  +  :         826 :         foreach_ptr(InferenceElem, elem, onconflict->arbiterElems)
             +  +  +  + ]
     868                 :             :         {
     869                 :         304 :                 Var                *var;
     870                 :         304 :                 int                     attno;
     871                 :             : 
     872                 :             :                 /* we cannot also have a constraint name, per grammar */
     873         [ -  + ]:         304 :                 Assert(!OidIsValid(onconflict->constraint));
     874                 :             : 
     875         [ +  + ]:         304 :                 if (!IsA(elem->expr, Var))
     876                 :             :                 {
     877                 :             :                         /* If not a plain Var, just shove it in inferElems for now */
     878                 :          25 :                         inferElems = lappend(inferElems, elem->expr);
     879                 :          25 :                         continue;
     880                 :             :                 }
     881                 :             : 
     882                 :         279 :                 var = (Var *) elem->expr;
     883                 :         279 :                 attno = var->varattno;
     884                 :             : 
     885         [ +  - ]:         279 :                 if (attno == 0)
     886   [ #  #  #  # ]:           0 :                         ereport(ERROR,
     887                 :             :                                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
     888                 :             :                                          errmsg("whole row unique index inference specifications are not supported")));
     889                 :             : 
     890                 :         558 :                 inferAttrs = bms_add_member(inferAttrs,
     891                 :         279 :                                                                         attno - FirstLowInvalidHeapAttributeNumber);
     892         [ +  + ]:         565 :         }
     893                 :             : 
     894                 :             :         /*
     895                 :             :          * Next, open all the indexes.  We need this list for two things: first,
     896                 :             :          * if an ON CONSTRAINT clause was given, and that constraint's index is
     897                 :             :          * undergoing REINDEX CONCURRENTLY, then we need to consider all matches
     898                 :             :          * for that index.  Second, if an attribute list was specified in the ON
     899                 :             :          * CONFLICT clause, we use the list to find the indexes whose attributes
     900                 :             :          * match that list.
     901                 :             :          */
     902                 :         261 :         indexList = RelationGetIndexList(relation);
     903   [ +  +  +  -  :         857 :         foreach_oid(indexoid, indexList)
             +  +  +  + ]
     904                 :             :         {
     905                 :         335 :                 Relation        idxRel;
     906                 :             : 
     907                 :             :                 /* obtain the same lock type that the executor will ultimately use */
     908                 :         335 :                 idxRel = index_open(indexoid, rte->rellockmode);
     909                 :         335 :                 indexRelList = lappend(indexRelList, idxRel);
     910                 :         596 :         }
     911                 :             : 
     912                 :             :         /*
     913                 :             :          * If a constraint was named in the command, look up its index.  We don't
     914                 :             :          * return it immediately because we need some additional sanity checks,
     915                 :             :          * and also because we need to include other indexes as arbiters to
     916                 :             :          * account for REINDEX CONCURRENTLY processing it.
     917                 :             :          */
     918         [ +  + ]:         261 :         if (onconflict->constraint != InvalidOid)
     919                 :             :         {
     920                 :             :                 /* we cannot also have an explicit list of elements, per grammar */
     921         [ +  - ]:          32 :                 Assert(onconflict->arbiterElems == NIL);
     922                 :             : 
     923                 :          32 :                 indexOidFromConstraint = get_constraint_index(onconflict->constraint);
     924         [ +  - ]:          32 :                 if (indexOidFromConstraint == InvalidOid)
     925   [ #  #  #  # ]:           0 :                         ereport(ERROR,
     926                 :             :                                         (errcode(ERRCODE_WRONG_OBJECT_TYPE),
     927                 :             :                                          errmsg("constraint in ON CONFLICT clause has no associated index")));
     928                 :             : 
     929                 :             :                 /*
     930                 :             :                  * Find the named constraint index to extract its attributes and
     931                 :             :                  * predicates.
     932                 :             :                  */
     933   [ +  +  +  -  :          99 :                 foreach_ptr(RelationData, idxRel, indexRelList)
             -  +  +  - ]
     934                 :             :                 {
     935                 :          35 :                         Form_pg_index idxForm = idxRel->rd_index;
     936                 :             : 
     937         [ +  + ]:          35 :                         if (indexOidFromConstraint == idxForm->indexrelid)
     938                 :             :                         {
     939                 :             :                                 /* Found it. */
     940         [ -  + ]:          32 :                                 Assert(idxForm->indisready);
     941                 :             : 
     942                 :             :                                 /*
     943                 :             :                                  * Set up inferElems and inferIndexExprs to match the
     944                 :             :                                  * constraint index, so that we can match them in the loop
     945                 :             :                                  * below.
     946                 :             :                                  */
     947         [ +  + ]:          90 :                                 for (int natt = 0; natt < idxForm->indnkeyatts; natt++)
     948                 :             :                                 {
     949                 :          58 :                                         int                     attno;
     950                 :             : 
     951                 :          58 :                                         attno = idxRel->rd_index->indkey.values[natt];
     952         [ +  + ]:          58 :                                         if (attno != InvalidAttrNumber)
     953                 :          56 :                                                 inferAttrs =
     954                 :         112 :                                                         bms_add_member(inferAttrs,
     955                 :          56 :                                                                                    attno - FirstLowInvalidHeapAttributeNumber);
     956                 :          58 :                                 }
     957                 :             : 
     958                 :          32 :                                 inferElems = RelationGetIndexExpressions(idxRel);
     959                 :          32 :                                 inferIndexExprs = RelationGetIndexPredicate(idxRel);
     960                 :          32 :                                 break;
     961                 :             :                         }
     962         [ +  + ]:          67 :                 }
     963                 :          32 :         }
     964                 :             : 
     965                 :             :         /*
     966                 :             :          * Using that representation, iterate through the list of indexes on the
     967                 :             :          * target relation to find matches.
     968                 :             :          */
     969   [ +  +  +  -  :         831 :         foreach_ptr(RelationData, idxRel, indexRelList)
             +  +  +  + ]
     970                 :             :         {
     971                 :         335 :                 Form_pg_index idxForm;
     972                 :         335 :                 Bitmapset  *indexedAttrs;
     973                 :         335 :                 List       *idxExprs;
     974                 :         335 :                 List       *predExprs;
     975                 :         335 :                 AttrNumber      natt;
     976                 :         335 :                 bool            match;
     977                 :             : 
     978                 :             :                 /*
     979                 :             :                  * Extract info from the relation descriptor for the index.
     980                 :             :                  *
     981                 :             :                  * Let executor complain about !indimmediate case directly, because
     982                 :             :                  * enforcement needs to occur there anyway when an inference clause is
     983                 :             :                  * omitted.
     984                 :             :                  */
     985                 :         335 :                 idxForm = idxRel->rd_index;
     986                 :             : 
     987                 :             :                 /*
     988                 :             :                  * Ignore indexes that aren't indisready, because we cannot trust
     989                 :             :                  * their catalog structure yet.  However, if any indexes are marked
     990                 :             :                  * indisready but not yet indisvalid, we still consider them, because
     991                 :             :                  * they might turn valid while we're running.  Doing it this way
     992                 :             :                  * allows a concurrent transaction with a slightly later catalog
     993                 :             :                  * snapshot infer the same set of indexes, which is critical to
     994                 :             :                  * prevent spurious 'duplicate key' errors.
     995                 :             :                  *
     996                 :             :                  * However, another critical aspect is that a unique index that isn't
     997                 :             :                  * yet marked indisvalid=true might not be complete yet, meaning it
     998                 :             :                  * wouldn't detect possible duplicate rows.  In order to prevent false
     999                 :             :                  * negatives, we require that we include in the set of inferred
    1000                 :             :                  * indexes at least one index that is marked valid.
    1001                 :             :                  */
    1002         [ +  - ]:         335 :                 if (!idxForm->indisready)
    1003                 :           0 :                         continue;
    1004                 :             : 
    1005                 :             :                 /*
    1006                 :             :                  * Ignore invalid indexes for partitioned tables.  It's possible that
    1007                 :             :                  * some partitions don't have the index (yet), and then we would not
    1008                 :             :                  * find a match during ExecInitPartitionInfo.
    1009                 :             :                  */
    1010   [ +  +  +  + ]:         335 :                 if (relation->rd_rel->relkind == RELKIND_PARTITIONED_TABLE &&
    1011                 :          24 :                         !idxForm->indisvalid)
    1012                 :           2 :                         continue;
    1013                 :             : 
    1014                 :             :                 /*
    1015                 :             :                  * Note that we do not perform a check against indcheckxmin (like e.g.
    1016                 :             :                  * get_relation_info()) here to eliminate candidates, because
    1017                 :             :                  * uniqueness checking only cares about the most recently committed
    1018                 :             :                  * tuple versions.
    1019                 :             :                  */
    1020                 :             : 
    1021                 :             :                 /*
    1022                 :             :                  * Look for match for "ON constraint_name" variant, which may not be a
    1023                 :             :                  * unique constraint.  This can only be a constraint name.
    1024                 :             :                  */
    1025         [ +  + ]:         333 :                 if (indexOidFromConstraint == idxForm->indexrelid)
    1026                 :             :                 {
    1027   [ +  +  +  + ]:          32 :                         if (idxForm->indisexclusion && onconflict->action == ONCONFLICT_UPDATE)
    1028   [ +  -  +  - ]:          13 :                                 ereport(ERROR,
    1029                 :             :                                                 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
    1030                 :             :                                                  errmsg("ON CONFLICT DO UPDATE not supported with exclusion constraints")));
    1031                 :             : 
    1032                 :             :                         /* Consider this one a match already */
    1033                 :          19 :                         results = lappend_oid(results, idxForm->indexrelid);
    1034                 :          19 :                         foundValid |= idxForm->indisvalid;
    1035                 :          19 :                         continue;
    1036                 :             :                 }
    1037         [ +  + ]:         301 :                 else if (indexOidFromConstraint != InvalidOid)
    1038                 :             :                 {
    1039                 :             :                         /*
    1040                 :             :                          * In the case of "ON constraint_name DO UPDATE" we need to skip
    1041                 :             :                          * non-unique candidates.
    1042                 :             :                          */
    1043   [ +  +  +  - ]:           4 :                         if (!idxForm->indisunique && onconflict->action == ONCONFLICT_UPDATE)
    1044                 :           0 :                                 continue;
    1045                 :           4 :                 }
    1046                 :             :                 else
    1047                 :             :                 {
    1048                 :             :                         /*
    1049                 :             :                          * Only considering conventional inference at this point (not
    1050                 :             :                          * named constraints), so index under consideration can be
    1051                 :             :                          * immediately skipped if it's not unique.
    1052                 :             :                          */
    1053         [ +  - ]:         297 :                         if (!idxForm->indisunique)
    1054                 :           0 :                                 continue;
    1055                 :             :                 }
    1056                 :             : 
    1057                 :             :                 /*
    1058                 :             :                  * So-called unique constraints with WITHOUT OVERLAPS are really
    1059                 :             :                  * exclusion constraints, so skip those too.
    1060                 :             :                  */
    1061         [ +  + ]:         301 :                 if (idxForm->indisexclusion)
    1062                 :          25 :                         continue;
    1063                 :             : 
    1064                 :             :                 /* Build BMS representation of plain (non expression) index attrs */
    1065                 :         276 :                 indexedAttrs = NULL;
    1066         [ +  + ]:         646 :                 for (natt = 0; natt < idxForm->indnkeyatts; natt++)
    1067                 :             :                 {
    1068                 :         370 :                         int                     attno = idxRel->rd_index->indkey.values[natt];
    1069                 :             : 
    1070         [ +  + ]:         370 :                         if (attno != 0)
    1071                 :         644 :                                 indexedAttrs = bms_add_member(indexedAttrs,
    1072                 :         322 :                                                                                           attno - FirstLowInvalidHeapAttributeNumber);
    1073                 :         370 :                 }
    1074                 :             : 
    1075                 :             :                 /* Non-expression attributes (if any) must match */
    1076         [ +  + ]:         276 :                 if (!bms_equal(indexedAttrs, inferAttrs))
    1077                 :          66 :                         continue;
    1078                 :             : 
    1079                 :             :                 /* Expression attributes (if any) must match */
    1080                 :         210 :                 idxExprs = RelationGetIndexExpressions(idxRel);
    1081         [ +  + ]:         210 :                 if (idxExprs)
    1082                 :             :                 {
    1083         [ +  + ]:          25 :                         if (varno != 1)
    1084                 :           1 :                                 ChangeVarNodes((Node *) idxExprs, 1, varno, 0);
    1085                 :             : 
    1086                 :          25 :                         idxExprs = (List *) eval_const_expressions(root, (Node *) idxExprs);
    1087                 :          25 :                 }
    1088                 :             : 
    1089                 :             :                 /*
    1090                 :             :                  * If arbiterElems are present, check them.  (Note that if a
    1091                 :             :                  * constraint name was given in the command line, this list is NIL.)
    1092                 :             :                  */
    1093                 :         210 :                 match = true;
    1094   [ +  +  +  -  :         698 :                 foreach_ptr(InferenceElem, elem, onconflict->arbiterElems)
             +  +  +  + ]
    1095                 :             :                 {
    1096                 :             :                         /*
    1097                 :             :                          * Ensure that collation/opclass aspects of inference expression
    1098                 :             :                          * element match.  Even though this loop is primarily concerned
    1099                 :             :                          * with matching expressions, it is a convenient point to check
    1100                 :             :                          * this for both expressions and ordinary (non-expression)
    1101                 :             :                          * attributes appearing as inference elements.
    1102                 :             :                          */
    1103         [ +  + ]:         278 :                         if (!infer_collation_opclass_match(elem, idxRel, idxExprs))
    1104                 :             :                         {
    1105                 :           6 :                                 match = false;
    1106                 :           6 :                                 break;
    1107                 :             :                         }
    1108                 :             : 
    1109                 :             :                         /*
    1110                 :             :                          * Plain Vars don't factor into count of expression elements, and
    1111                 :             :                          * the question of whether or not they satisfy the index
    1112                 :             :                          * definition has already been considered (they must).
    1113                 :             :                          */
    1114         [ +  + ]:         272 :                         if (IsA(elem->expr, Var))
    1115                 :         247 :                                 continue;
    1116                 :             : 
    1117                 :             :                         /*
    1118                 :             :                          * Might as well avoid redundant check in the rare cases where
    1119                 :             :                          * infer_collation_opclass_match() is required to do real work.
    1120                 :             :                          * Otherwise, check that element expression appears in cataloged
    1121                 :             :                          * index definition.
    1122                 :             :                          */
    1123         [ +  + ]:          25 :                         if (elem->infercollid != InvalidOid ||
    1124   [ +  +  +  + ]:          22 :                                 elem->inferopclass != InvalidOid ||
    1125                 :          21 :                                 list_member(idxExprs, elem->expr))
    1126                 :          23 :                                 continue;
    1127                 :             : 
    1128                 :           2 :                         match = false;
    1129                 :           2 :                         break;
    1130                 :         210 :                 }
    1131         [ +  + ]:         210 :                 if (!match)
    1132                 :           8 :                         continue;
    1133                 :             : 
    1134                 :             :                 /*
    1135                 :             :                  * In case of inference from an attribute list, ensure that the
    1136                 :             :                  * expression elements from inference clause are not missing any
    1137                 :             :                  * cataloged expressions.  This does the right thing when unique
    1138                 :             :                  * indexes redundantly repeat the same attribute, or if attributes
    1139                 :             :                  * redundantly appear multiple times within an inference clause.
    1140                 :             :                  *
    1141                 :             :                  * In case a constraint was named, ensure the candidate has an equal
    1142                 :             :                  * set of expressions as the named constraint's index.
    1143                 :             :                  */
    1144         [ +  + ]:         202 :                 if (list_difference(idxExprs, inferElems) != NIL)
    1145                 :           9 :                         continue;
    1146                 :             : 
    1147                 :         193 :                 predExprs = RelationGetIndexPredicate(idxRel);
    1148         [ +  + ]:         193 :                 if (predExprs)
    1149                 :             :                 {
    1150         [ +  + ]:          11 :                         if (varno != 1)
    1151                 :           1 :                                 ChangeVarNodes((Node *) predExprs, 1, varno, 0);
    1152                 :             : 
    1153                 :          11 :                         predExprs = (List *)
    1154                 :          22 :                                 eval_const_expressions(root,
    1155                 :          11 :                                                                            (Node *) make_ands_explicit(predExprs));
    1156                 :          11 :                         predExprs = make_ands_implicit((Expr *) predExprs);
    1157                 :          11 :                 }
    1158                 :             : 
    1159                 :             :                 /*
    1160                 :             :                  * Partial indexes affect each form of ON CONFLICT differently: if a
    1161                 :             :                  * constraint was named, then the predicates must be identical.  In
    1162                 :             :                  * conventional inference, the index's predicate must be implied by
    1163                 :             :                  * the WHERE clause.
    1164                 :             :                  */
    1165         [ -  + ]:         193 :                 if (OidIsValid(indexOidFromConstraint))
    1166                 :             :                 {
    1167         [ #  # ]:           0 :                         if (list_difference(predExprs, inferIndexExprs) != NIL)
    1168                 :           0 :                                 continue;
    1169                 :           0 :                 }
    1170                 :             :                 else
    1171                 :             :                 {
    1172         [ +  + ]:         193 :                         if (!predicate_implied_by(predExprs, inferIndexExprs, false))
    1173                 :           6 :                                 continue;
    1174                 :             :                 }
    1175                 :             : 
    1176                 :             :                 /* All good -- consider this index a match */
    1177                 :         187 :                 results = lappend_oid(results, idxForm->indexrelid);
    1178                 :         187 :                 foundValid |= idxForm->indisvalid;
    1179         [ +  + ]:         570 :         }
    1180                 :             : 
    1181                 :             :         /* Close all indexes */
    1182   [ +  +  +  -  :         818 :         foreach_ptr(RelationData, idxRel, indexRelList)
             +  +  +  + ]
    1183                 :             :         {
    1184                 :         322 :                 index_close(idxRel, NoLock);
    1185                 :         570 :         }
    1186                 :             : 
    1187                 :         248 :         list_free(indexList);
    1188                 :         248 :         list_free(indexRelList);
    1189                 :         248 :         table_close(relation, NoLock);
    1190                 :             : 
    1191                 :             :         /* We require at least one indisvalid index */
    1192         [ +  + ]:         248 :         if (results == NIL || !foundValid)
    1193   [ +  -  +  - ]:          51 :                 ereport(ERROR,
    1194                 :             :                                 (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
    1195                 :             :                                  errmsg("there is no unique or exclusion constraint matching the ON CONFLICT specification")));
    1196                 :             : 
    1197                 :         197 :         return results;
    1198                 :         220 : }
    1199                 :             : 
    1200                 :             : /*
    1201                 :             :  * infer_collation_opclass_match - ensure infer element opclass/collation match
    1202                 :             :  *
    1203                 :             :  * Given unique index inference element from inference specification, if
    1204                 :             :  * collation was specified, or if opclass was specified, verify that there is
    1205                 :             :  * at least one matching indexed attribute (occasionally, there may be more).
    1206                 :             :  * Skip this in the common case where inference specification does not include
    1207                 :             :  * collation or opclass (instead matching everything, regardless of cataloged
    1208                 :             :  * collation/opclass of indexed attribute).
    1209                 :             :  *
    1210                 :             :  * At least historically, Postgres has not offered collations or opclasses
    1211                 :             :  * with alternative-to-default notions of equality, so these additional
    1212                 :             :  * criteria should only be required infrequently.
    1213                 :             :  *
    1214                 :             :  * Don't give up immediately when an inference element matches some attribute
    1215                 :             :  * cataloged as indexed but not matching additional opclass/collation
    1216                 :             :  * criteria.  This is done so that the implementation is as forgiving as
    1217                 :             :  * possible of redundancy within cataloged index attributes (or, less
    1218                 :             :  * usefully, within inference specification elements).  If collations actually
    1219                 :             :  * differ between apparently redundantly indexed attributes (redundant within
    1220                 :             :  * or across indexes), then there really is no redundancy as such.
    1221                 :             :  *
    1222                 :             :  * Note that if an inference element specifies an opclass and a collation at
    1223                 :             :  * once, both must match in at least one particular attribute within index
    1224                 :             :  * catalog definition in order for that inference element to be considered
    1225                 :             :  * inferred/satisfied.
    1226                 :             :  */
    1227                 :             : static bool
    1228                 :         278 : infer_collation_opclass_match(InferenceElem *elem, Relation idxRel,
    1229                 :             :                                                           List *idxExprs)
    1230                 :             : {
    1231                 :         278 :         AttrNumber      natt;
    1232                 :         278 :         Oid                     inferopfamily = InvalidOid; /* OID of opclass opfamily */
    1233                 :         278 :         Oid                     inferopcinputtype = InvalidOid; /* OID of opclass input type */
    1234                 :         278 :         int                     nplain = 0;             /* # plain attrs observed */
    1235                 :             : 
    1236                 :             :         /*
    1237                 :             :          * If inference specification element lacks collation/opclass, then no
    1238                 :             :          * need to check for exact match.
    1239                 :             :          */
    1240   [ +  +  +  + ]:         278 :         if (elem->infercollid == InvalidOid && elem->inferopclass == InvalidOid)
    1241                 :         259 :                 return true;
    1242                 :             : 
    1243                 :             :         /*
    1244                 :             :          * Lookup opfamily and input type, for matching indexes
    1245                 :             :          */
    1246         [ +  + ]:          19 :         if (elem->inferopclass)
    1247                 :             :         {
    1248                 :          14 :                 inferopfamily = get_opclass_family(elem->inferopclass);
    1249                 :          14 :                 inferopcinputtype = get_opclass_input_type(elem->inferopclass);
    1250                 :          14 :         }
    1251                 :             : 
    1252         [ +  + ]:          41 :         for (natt = 1; natt <= idxRel->rd_att->natts; natt++)
    1253                 :             :         {
    1254                 :          35 :                 Oid                     opfamily = idxRel->rd_opfamily[natt - 1];
    1255                 :          35 :                 Oid                     opcinputtype = idxRel->rd_opcintype[natt - 1];
    1256                 :          35 :                 Oid                     collation = idxRel->rd_indcollation[natt - 1];
    1257                 :          35 :                 int                     attno = idxRel->rd_index->indkey.values[natt - 1];
    1258                 :             : 
    1259         [ +  + ]:          35 :                 if (attno != 0)
    1260                 :          28 :                         nplain++;
    1261                 :             : 
    1262   [ +  +  -  + ]:          46 :                 if (elem->inferopclass != InvalidOid &&
    1263         [ +  + ]:          26 :                         (inferopfamily != opfamily || inferopcinputtype != opcinputtype))
    1264                 :             :                 {
    1265                 :             :                         /* Attribute needed to match opclass, but didn't */
    1266                 :          15 :                         continue;
    1267                 :             :                 }
    1268                 :             : 
    1269   [ +  +  +  + ]:          20 :                 if (elem->infercollid != InvalidOid &&
    1270                 :          14 :                         elem->infercollid != collation)
    1271                 :             :                 {
    1272                 :             :                         /* Attribute needed to match collation, but didn't */
    1273                 :           6 :                         continue;
    1274                 :             :                 }
    1275                 :             : 
    1276                 :             :                 /* If one matching index att found, good enough -- return true */
    1277         [ +  + ]:          14 :                 if (IsA(elem->expr, Var))
    1278                 :             :                 {
    1279         [ +  - ]:           9 :                         if (((Var *) elem->expr)->varattno == attno)
    1280                 :           9 :                                 return true;
    1281                 :           0 :                 }
    1282         [ -  + ]:           5 :                 else if (attno == 0)
    1283                 :             :                 {
    1284                 :           5 :                         Node       *nattExpr = list_nth(idxExprs, (natt - 1) - nplain);
    1285                 :             : 
    1286                 :             :                         /*
    1287                 :             :                          * Note that unlike routines like match_index_to_operand() we
    1288                 :             :                          * don't need to care about RelabelType.  Neither the index
    1289                 :             :                          * definition nor the inference clause should contain them.
    1290                 :             :                          */
    1291         [ +  + ]:           5 :                         if (equal(elem->expr, nattExpr))
    1292                 :           4 :                                 return true;
    1293         [ +  + ]:           5 :                 }
    1294      [ +  +  + ]:          35 :         }
    1295                 :             : 
    1296                 :           6 :         return false;
    1297                 :         278 : }
    1298                 :             : 
    1299                 :             : /*
    1300                 :             :  * estimate_rel_size - estimate # pages and # tuples in a table or index
    1301                 :             :  *
    1302                 :             :  * We also estimate the fraction of the pages that are marked all-visible in
    1303                 :             :  * the visibility map, for use in estimation of index-only scans.
    1304                 :             :  *
    1305                 :             :  * If attr_widths isn't NULL, it points to the zero-index entry of the
    1306                 :             :  * relation's attr_widths[] cache; we fill this in if we have need to compute
    1307                 :             :  * the attribute widths for estimation purposes.
    1308                 :             :  */
    1309                 :             : void
    1310                 :       47951 : estimate_rel_size(Relation rel, int32 *attr_widths,
    1311                 :             :                                   BlockNumber *pages, double *tuples, double *allvisfrac)
    1312                 :             : {
    1313                 :       47951 :         BlockNumber curpages;
    1314                 :       47951 :         BlockNumber relpages;
    1315                 :       47951 :         double          reltuples;
    1316                 :       47951 :         BlockNumber relallvisible;
    1317                 :       47951 :         double          density;
    1318                 :             : 
    1319   [ +  +  +  +  :       47951 :         if (RELKIND_HAS_TABLE_AM(rel->rd_rel->relkind))
                   +  + ]
    1320                 :             :         {
    1321                 :       95602 :                 table_relation_estimate_size(rel, attr_widths, pages, tuples,
    1322                 :       47801 :                                                                          allvisfrac);
    1323                 :       47801 :         }
    1324         [ +  + ]:         150 :         else if (rel->rd_rel->relkind == RELKIND_INDEX)
    1325                 :             :         {
    1326                 :             :                 /*
    1327                 :             :                  * XXX: It'd probably be good to move this into a callback, individual
    1328                 :             :                  * index types e.g. know if they have a metapage.
    1329                 :             :                  */
    1330                 :             : 
    1331                 :             :                 /* it has storage, ok to call the smgr */
    1332                 :         143 :                 curpages = RelationGetNumberOfBlocks(rel);
    1333                 :             : 
    1334                 :             :                 /* report estimated # pages */
    1335                 :         143 :                 *pages = curpages;
    1336                 :             :                 /* quick exit if rel is clearly empty */
    1337         [ +  - ]:         143 :                 if (curpages == 0)
    1338                 :             :                 {
    1339                 :           0 :                         *tuples = 0;
    1340                 :           0 :                         *allvisfrac = 0;
    1341                 :           0 :                         return;
    1342                 :             :                 }
    1343                 :             : 
    1344                 :             :                 /* coerce values in pg_class to more desirable types */
    1345                 :         143 :                 relpages = (BlockNumber) rel->rd_rel->relpages;
    1346                 :         143 :                 reltuples = (double) rel->rd_rel->reltuples;
    1347                 :         143 :                 relallvisible = (BlockNumber) rel->rd_rel->relallvisible;
    1348                 :             : 
    1349                 :             :                 /*
    1350                 :             :                  * Discount the metapage while estimating the number of tuples. This
    1351                 :             :                  * is a kluge because it assumes more than it ought to about index
    1352                 :             :                  * structure.  Currently it's OK for btree, hash, and GIN indexes but
    1353                 :             :                  * suspect for GiST indexes.
    1354                 :             :                  */
    1355         [ +  + ]:         143 :                 if (relpages > 0)
    1356                 :             :                 {
    1357                 :         140 :                         curpages--;
    1358                 :         140 :                         relpages--;
    1359                 :         140 :                 }
    1360                 :             : 
    1361                 :             :                 /* estimate number of tuples from previous tuple density */
    1362   [ +  +  +  + ]:         143 :                 if (reltuples >= 0 && relpages > 0)
    1363                 :         119 :                         density = reltuples / (double) relpages;
    1364                 :             :                 else
    1365                 :             :                 {
    1366                 :             :                         /*
    1367                 :             :                          * If we have no data because the relation was never vacuumed,
    1368                 :             :                          * estimate tuple width from attribute datatypes.  We assume here
    1369                 :             :                          * that the pages are completely full, which is OK for tables
    1370                 :             :                          * (since they've presumably not been VACUUMed yet) but is
    1371                 :             :                          * probably an overestimate for indexes.  Fortunately
    1372                 :             :                          * get_relation_info() can clamp the overestimate to the parent
    1373                 :             :                          * table's size.
    1374                 :             :                          *
    1375                 :             :                          * Note: this code intentionally disregards alignment
    1376                 :             :                          * considerations, because (a) that would be gilding the lily
    1377                 :             :                          * considering how crude the estimate is, and (b) it creates
    1378                 :             :                          * platform dependencies in the default plans which are kind of a
    1379                 :             :                          * headache for regression testing.
    1380                 :             :                          *
    1381                 :             :                          * XXX: Should this logic be more index specific?
    1382                 :             :                          */
    1383                 :          24 :                         int32           tuple_width;
    1384                 :             : 
    1385                 :          24 :                         tuple_width = get_rel_data_width(rel, attr_widths);
    1386                 :          24 :                         tuple_width += MAXALIGN(SizeofHeapTupleHeader);
    1387                 :          24 :                         tuple_width += sizeof(ItemIdData);
    1388                 :             :                         /* note: integer division is intentional here */
    1389                 :          24 :                         density = (BLCKSZ - SizeOfPageHeaderData) / tuple_width;
    1390                 :          24 :                 }
    1391                 :         143 :                 *tuples = rint(density * (double) curpages);
    1392                 :             : 
    1393                 :             :                 /*
    1394                 :             :                  * We use relallvisible as-is, rather than scaling it up like we do
    1395                 :             :                  * for the pages and tuples counts, on the theory that any pages added
    1396                 :             :                  * since the last VACUUM are most likely not marked all-visible.  But
    1397                 :             :                  * costsize.c wants it converted to a fraction.
    1398                 :             :                  */
    1399   [ -  +  #  # ]:         143 :                 if (relallvisible == 0 || curpages <= 0)
    1400                 :         143 :                         *allvisfrac = 0;
    1401         [ #  # ]:           0 :                 else if ((double) relallvisible >= curpages)
    1402                 :           0 :                         *allvisfrac = 1;
    1403                 :             :                 else
    1404                 :           0 :                         *allvisfrac = (double) relallvisible / curpages;
    1405                 :         143 :         }
    1406                 :             :         else
    1407                 :             :         {
    1408                 :             :                 /*
    1409                 :             :                  * Just use whatever's in pg_class.  This covers foreign tables,
    1410                 :             :                  * sequences, and also relkinds without storage (shouldn't get here?);
    1411                 :             :                  * see initializations in AddNewRelationTuple().  Note that FDW must
    1412                 :             :                  * cope if reltuples is -1!
    1413                 :             :                  */
    1414                 :           7 :                 *pages = rel->rd_rel->relpages;
    1415                 :           7 :                 *tuples = rel->rd_rel->reltuples;
    1416                 :           7 :                 *allvisfrac = 0;
    1417                 :             :         }
    1418         [ -  + ]:       47951 : }
    1419                 :             : 
    1420                 :             : 
    1421                 :             : /*
    1422                 :             :  * get_rel_data_width
    1423                 :             :  *
    1424                 :             :  * Estimate the average width of (the data part of) the relation's tuples.
    1425                 :             :  *
    1426                 :             :  * If attr_widths isn't NULL, it points to the zero-index entry of the
    1427                 :             :  * relation's attr_widths[] cache; use and update that cache as appropriate.
    1428                 :             :  *
    1429                 :             :  * Currently we ignore dropped columns.  Ideally those should be included
    1430                 :             :  * in the result, but we haven't got any way to get info about them; and
    1431                 :             :  * since they might be mostly NULLs, treating them as zero-width is not
    1432                 :             :  * necessarily the wrong thing anyway.
    1433                 :             :  */
    1434                 :             : int32
    1435                 :       21230 : get_rel_data_width(Relation rel, int32 *attr_widths)
    1436                 :             : {
    1437                 :       21230 :         int64           tuple_width = 0;
    1438                 :       21230 :         int                     i;
    1439                 :             : 
    1440         [ +  + ]:      119813 :         for (i = 1; i <= RelationGetNumberOfAttributes(rel); i++)
    1441                 :             :         {
    1442                 :       98583 :                 Form_pg_attribute att = TupleDescAttr(rel->rd_att, i - 1);
    1443                 :       98583 :                 int32           item_width;
    1444                 :             : 
    1445         [ +  + ]:       98583 :                 if (att->attisdropped)
    1446                 :         388 :                         continue;
    1447                 :             : 
    1448                 :             :                 /* use previously cached data, if any */
    1449   [ +  +  +  + ]:       98195 :                 if (attr_widths != NULL && attr_widths[i] > 0)
    1450                 :             :                 {
    1451                 :         263 :                         tuple_width += attr_widths[i];
    1452                 :         263 :                         continue;
    1453                 :             :                 }
    1454                 :             : 
    1455                 :             :                 /* This should match set_rel_width() in costsize.c */
    1456                 :       97932 :                 item_width = get_attavgwidth(RelationGetRelid(rel), i);
    1457         [ +  + ]:       97932 :                 if (item_width <= 0)
    1458                 :             :                 {
    1459                 :       97822 :                         item_width = get_typavgwidth(att->atttypid, att->atttypmod);
    1460         [ -  + ]:       97822 :                         Assert(item_width > 0);
    1461                 :       97822 :                 }
    1462         [ +  + ]:       97932 :                 if (attr_widths != NULL)
    1463                 :       89914 :                         attr_widths[i] = item_width;
    1464                 :       97932 :                 tuple_width += item_width;
    1465      [ -  +  + ]:       98583 :         }
    1466                 :             : 
    1467                 :       42460 :         return clamp_width_est(tuple_width);
    1468                 :       21230 : }
    1469                 :             : 
    1470                 :             : /*
    1471                 :             :  * get_relation_data_width
    1472                 :             :  *
    1473                 :             :  * External API for get_rel_data_width: same behavior except we have to
    1474                 :             :  * open the relcache entry.
    1475                 :             :  */
    1476                 :             : int32
    1477                 :         209 : get_relation_data_width(Oid relid, int32 *attr_widths)
    1478                 :             : {
    1479                 :         209 :         int32           result;
    1480                 :         209 :         Relation        relation;
    1481                 :             : 
    1482                 :             :         /* As above, assume relation is already locked */
    1483                 :         209 :         relation = table_open(relid, NoLock);
    1484                 :             : 
    1485                 :         209 :         result = get_rel_data_width(relation, attr_widths);
    1486                 :             : 
    1487                 :         209 :         table_close(relation, NoLock);
    1488                 :             : 
    1489                 :         418 :         return result;
    1490                 :         209 : }
    1491                 :             : 
    1492                 :             : 
    1493                 :             : /*
    1494                 :             :  * get_relation_constraints
    1495                 :             :  *
    1496                 :             :  * Retrieve the applicable constraint expressions of the given relation.
    1497                 :             :  * Only constraints that have been validated are considered.
    1498                 :             :  *
    1499                 :             :  * Returns a List (possibly empty) of constraint expressions.  Each one
    1500                 :             :  * has been canonicalized, and its Vars are changed to have the varno
    1501                 :             :  * indicated by rel->relid.  This allows the expressions to be easily
    1502                 :             :  * compared to expressions taken from WHERE.
    1503                 :             :  *
    1504                 :             :  * If include_noinherit is true, it's okay to include constraints that
    1505                 :             :  * are marked NO INHERIT.
    1506                 :             :  *
    1507                 :             :  * If include_notnull is true, "col IS NOT NULL" expressions are generated
    1508                 :             :  * and added to the result for each column that's marked attnotnull.
    1509                 :             :  *
    1510                 :             :  * If include_partition is true, and the relation is a partition,
    1511                 :             :  * also include the partitioning constraints.
    1512                 :             :  *
    1513                 :             :  * Note: at present this is invoked at most once per relation per planner
    1514                 :             :  * run, and in many cases it won't be invoked at all, so there seems no
    1515                 :             :  * point in caching the data in RelOptInfo.
    1516                 :             :  */
    1517                 :             : static List *
    1518                 :        3043 : get_relation_constraints(PlannerInfo *root,
    1519                 :             :                                                  Oid relationObjectId, RelOptInfo *rel,
    1520                 :             :                                                  bool include_noinherit,
    1521                 :             :                                                  bool include_notnull,
    1522                 :             :                                                  bool include_partition)
    1523                 :             : {
    1524                 :        3043 :         List       *result = NIL;
    1525                 :        3043 :         Index           varno = rel->relid;
    1526                 :        3043 :         Relation        relation;
    1527                 :        3043 :         TupleConstr *constr;
    1528                 :             : 
    1529                 :             :         /*
    1530                 :             :          * We assume the relation has already been safely locked.
    1531                 :             :          */
    1532                 :        3043 :         relation = table_open(relationObjectId, NoLock);
    1533                 :             : 
    1534                 :        3043 :         constr = relation->rd_att->constr;
    1535         [ +  + ]:        3043 :         if (constr != NULL)
    1536                 :             :         {
    1537                 :         940 :                 int                     num_check = constr->num_check;
    1538                 :         940 :                 int                     i;
    1539                 :             : 
    1540         [ +  + ]:        1040 :                 for (i = 0; i < num_check; i++)
    1541                 :             :                 {
    1542                 :         100 :                         Node       *cexpr;
    1543                 :             : 
    1544                 :             :                         /*
    1545                 :             :                          * If this constraint hasn't been fully validated yet, we must
    1546                 :             :                          * ignore it here.
    1547                 :             :                          */
    1548         [ +  + ]:         100 :                         if (!constr->check[i].ccvalid)
    1549                 :           9 :                                 continue;
    1550                 :             : 
    1551                 :             :                         /*
    1552                 :             :                          * NOT ENFORCED constraints are always marked as invalid, which
    1553                 :             :                          * should have been ignored.
    1554                 :             :                          */
    1555         [ -  + ]:          91 :                         Assert(constr->check[i].ccenforced);
    1556                 :             : 
    1557                 :             :                         /*
    1558                 :             :                          * Also ignore if NO INHERIT and we weren't told that that's safe.
    1559                 :             :                          */
    1560   [ +  +  +  - ]:          91 :                         if (constr->check[i].ccnoinherit && !include_noinherit)
    1561                 :           0 :                                 continue;
    1562                 :             : 
    1563                 :          91 :                         cexpr = stringToNode(constr->check[i].ccbin);
    1564                 :             : 
    1565                 :             :                         /*
    1566                 :             :                          * Fix Vars to have the desired varno.  This must be done before
    1567                 :             :                          * const-simplification because eval_const_expressions reduces
    1568                 :             :                          * NullTest for Vars based on varno.
    1569                 :             :                          */
    1570         [ +  + ]:          91 :                         if (varno != 1)
    1571                 :          89 :                                 ChangeVarNodes(cexpr, 1, varno, 0);
    1572                 :             : 
    1573                 :             :                         /*
    1574                 :             :                          * Run each expression through const-simplification and
    1575                 :             :                          * canonicalization.  This is not just an optimization, but is
    1576                 :             :                          * necessary, because we will be comparing it to
    1577                 :             :                          * similarly-processed qual clauses, and may fail to detect valid
    1578                 :             :                          * matches without this.  This must match the processing done to
    1579                 :             :                          * qual clauses in preprocess_expression()!  (We can skip the
    1580                 :             :                          * stuff involving subqueries, however, since we don't allow any
    1581                 :             :                          * in check constraints.)
    1582                 :             :                          */
    1583                 :          91 :                         cexpr = eval_const_expressions(root, cexpr);
    1584                 :             : 
    1585                 :          91 :                         cexpr = (Node *) canonicalize_qual((Expr *) cexpr, true);
    1586                 :             : 
    1587                 :             :                         /*
    1588                 :             :                          * Finally, convert to implicit-AND format (that is, a List) and
    1589                 :             :                          * append the resulting item(s) to our output list.
    1590                 :             :                          */
    1591                 :         182 :                         result = list_concat(result,
    1592                 :          91 :                                                                  make_ands_implicit((Expr *) cexpr));
    1593      [ -  +  + ]:         100 :                 }
    1594                 :             : 
    1595                 :             :                 /* Add NOT NULL constraints in expression form, if requested */
    1596   [ +  +  +  + ]:         940 :                 if (include_notnull && constr->has_not_null)
    1597                 :             :                 {
    1598                 :         868 :                         int                     natts = relation->rd_att->natts;
    1599                 :             : 
    1600         [ +  + ]:        3139 :                         for (i = 1; i <= natts; i++)
    1601                 :             :                         {
    1602                 :        2271 :                                 CompactAttribute *att = TupleDescCompactAttr(relation->rd_att, i - 1);
    1603                 :             : 
    1604   [ +  +  -  + ]:        2271 :                                 if (att->attnullability == ATTNULLABLE_VALID && !att->attisdropped)
    1605                 :             :                                 {
    1606                 :        1179 :                                         Form_pg_attribute wholeatt = TupleDescAttr(relation->rd_att, i - 1);
    1607                 :        1179 :                                         NullTest   *ntest = makeNode(NullTest);
    1608                 :             : 
    1609                 :        2358 :                                         ntest->arg = (Expr *) makeVar(varno,
    1610                 :        1179 :                                                                                                   i,
    1611                 :        1179 :                                                                                                   wholeatt->atttypid,
    1612                 :        1179 :                                                                                                   wholeatt->atttypmod,
    1613                 :        1179 :                                                                                                   wholeatt->attcollation,
    1614                 :             :                                                                                                   0);
    1615                 :        1179 :                                         ntest->nulltesttype = IS_NOT_NULL;
    1616                 :             : 
    1617                 :             :                                         /*
    1618                 :             :                                          * argisrow=false is correct even for a composite column,
    1619                 :             :                                          * because attnotnull does not represent a SQL-spec IS NOT
    1620                 :             :                                          * NULL test in such a case, just IS DISTINCT FROM NULL.
    1621                 :             :                                          */
    1622                 :        1179 :                                         ntest->argisrow = false;
    1623                 :        1179 :                                         ntest->location = -1;
    1624                 :        1179 :                                         result = lappend(result, ntest);
    1625                 :        1179 :                                 }
    1626                 :        2271 :                         }
    1627                 :         868 :                 }
    1628                 :         940 :         }
    1629                 :             : 
    1630                 :             :         /*
    1631                 :             :          * Add partitioning constraints, if requested.
    1632                 :             :          */
    1633   [ +  +  +  + ]:        3043 :         if (include_partition && relation->rd_rel->relispartition)
    1634                 :             :         {
    1635                 :             :                 /* make sure rel->partition_qual is set */
    1636                 :           2 :                 set_baserel_partition_constraint(relation, rel);
    1637                 :           2 :                 result = list_concat(result, rel->partition_qual);
    1638                 :           2 :         }
    1639                 :             : 
    1640                 :             :         /*
    1641                 :             :          * Expand virtual generated columns in the constraint expressions.
    1642                 :             :          */
    1643         [ +  + ]:        3043 :         if (result)
    1644                 :        1802 :                 result = (List *) expand_generated_columns_in_expr((Node *) result,
    1645                 :         901 :                                                                                                                    relation,
    1646                 :         901 :                                                                                                                    varno);
    1647                 :             : 
    1648                 :        3043 :         table_close(relation, NoLock);
    1649                 :             : 
    1650                 :        6086 :         return result;
    1651                 :        3043 : }
    1652                 :             : 
    1653                 :             : /*
    1654                 :             :  * Try loading data for the statistics object.
    1655                 :             :  *
    1656                 :             :  * We don't know if the data (specified by statOid and inh value) exist.
    1657                 :             :  * The result is stored in stainfos list.
    1658                 :             :  */
    1659                 :             : static void
    1660                 :         670 : get_relation_statistics_worker(List **stainfos, RelOptInfo *rel,
    1661                 :             :                                                            Oid statOid, bool inh,
    1662                 :             :                                                            Bitmapset *keys, List *exprs)
    1663                 :             : {
    1664                 :         670 :         Form_pg_statistic_ext_data dataForm;
    1665                 :         670 :         HeapTuple       dtup;
    1666                 :             : 
    1667                 :         670 :         dtup = SearchSysCache2(STATEXTDATASTXOID,
    1668                 :         670 :                                                    ObjectIdGetDatum(statOid), BoolGetDatum(inh));
    1669         [ +  + ]:         670 :         if (!HeapTupleIsValid(dtup))
    1670                 :         336 :                 return;
    1671                 :             : 
    1672                 :         334 :         dataForm = (Form_pg_statistic_ext_data) GETSTRUCT(dtup);
    1673                 :             : 
    1674                 :             :         /* add one StatisticExtInfo for each kind built */
    1675         [ +  + ]:         334 :         if (statext_is_kind_built(dtup, STATS_EXT_NDISTINCT))
    1676                 :             :         {
    1677                 :         119 :                 StatisticExtInfo *info = makeNode(StatisticExtInfo);
    1678                 :             : 
    1679                 :         119 :                 info->statOid = statOid;
    1680                 :         119 :                 info->inherit = dataForm->stxdinherit;
    1681                 :         119 :                 info->rel = rel;
    1682                 :         119 :                 info->kind = STATS_EXT_NDISTINCT;
    1683                 :         119 :                 info->keys = bms_copy(keys);
    1684                 :         119 :                 info->exprs = exprs;
    1685                 :             : 
    1686                 :         119 :                 *stainfos = lappend(*stainfos, info);
    1687                 :         119 :         }
    1688                 :             : 
    1689         [ +  + ]:         334 :         if (statext_is_kind_built(dtup, STATS_EXT_DEPENDENCIES))
    1690                 :             :         {
    1691                 :          88 :                 StatisticExtInfo *info = makeNode(StatisticExtInfo);
    1692                 :             : 
    1693                 :          88 :                 info->statOid = statOid;
    1694                 :          88 :                 info->inherit = dataForm->stxdinherit;
    1695                 :          88 :                 info->rel = rel;
    1696                 :          88 :                 info->kind = STATS_EXT_DEPENDENCIES;
    1697                 :          88 :                 info->keys = bms_copy(keys);
    1698                 :          88 :                 info->exprs = exprs;
    1699                 :             : 
    1700                 :          88 :                 *stainfos = lappend(*stainfos, info);
    1701                 :          88 :         }
    1702                 :             : 
    1703         [ +  + ]:         334 :         if (statext_is_kind_built(dtup, STATS_EXT_MCV))
    1704                 :             :         {
    1705                 :         147 :                 StatisticExtInfo *info = makeNode(StatisticExtInfo);
    1706                 :             : 
    1707                 :         147 :                 info->statOid = statOid;
    1708                 :         147 :                 info->inherit = dataForm->stxdinherit;
    1709                 :         147 :                 info->rel = rel;
    1710                 :         147 :                 info->kind = STATS_EXT_MCV;
    1711                 :         147 :                 info->keys = bms_copy(keys);
    1712                 :         147 :                 info->exprs = exprs;
    1713                 :             : 
    1714                 :         147 :                 *stainfos = lappend(*stainfos, info);
    1715                 :         147 :         }
    1716                 :             : 
    1717         [ +  + ]:         334 :         if (statext_is_kind_built(dtup, STATS_EXT_EXPRESSIONS))
    1718                 :             :         {
    1719                 :         135 :                 StatisticExtInfo *info = makeNode(StatisticExtInfo);
    1720                 :             : 
    1721                 :         135 :                 info->statOid = statOid;
    1722                 :         135 :                 info->inherit = dataForm->stxdinherit;
    1723                 :         135 :                 info->rel = rel;
    1724                 :         135 :                 info->kind = STATS_EXT_EXPRESSIONS;
    1725                 :         135 :                 info->keys = bms_copy(keys);
    1726                 :         135 :                 info->exprs = exprs;
    1727                 :             : 
    1728                 :         135 :                 *stainfos = lappend(*stainfos, info);
    1729                 :         135 :         }
    1730                 :             : 
    1731                 :         334 :         ReleaseSysCache(dtup);
    1732         [ -  + ]:         670 : }
    1733                 :             : 
    1734                 :             : /*
    1735                 :             :  * get_relation_statistics
    1736                 :             :  *              Retrieve extended statistics defined on the table.
    1737                 :             :  *
    1738                 :             :  * Returns a List (possibly empty) of StatisticExtInfo objects describing
    1739                 :             :  * the statistics.  Note that this doesn't load the actual statistics data,
    1740                 :             :  * just the identifying metadata.  Only stats actually built are considered.
    1741                 :             :  */
    1742                 :             : static List *
    1743                 :       50915 : get_relation_statistics(PlannerInfo *root, RelOptInfo *rel,
    1744                 :             :                                                 Relation relation)
    1745                 :             : {
    1746                 :       50915 :         Index           varno = rel->relid;
    1747                 :       50915 :         List       *statoidlist;
    1748                 :       50915 :         List       *stainfos = NIL;
    1749                 :       50915 :         ListCell   *l;
    1750                 :             : 
    1751                 :       50915 :         statoidlist = RelationGetStatExtList(relation);
    1752                 :             : 
    1753   [ +  +  +  +  :       51250 :         foreach(l, statoidlist)
                   +  + ]
    1754                 :             :         {
    1755                 :         335 :                 Oid                     statOid = lfirst_oid(l);
    1756                 :         335 :                 Form_pg_statistic_ext staForm;
    1757                 :         335 :                 HeapTuple       htup;
    1758                 :         335 :                 Bitmapset  *keys = NULL;
    1759                 :         335 :                 List       *exprs = NIL;
    1760                 :         335 :                 int                     i;
    1761                 :             : 
    1762                 :         335 :                 htup = SearchSysCache1(STATEXTOID, ObjectIdGetDatum(statOid));
    1763         [ +  - ]:         335 :                 if (!HeapTupleIsValid(htup))
    1764   [ #  #  #  # ]:           0 :                         elog(ERROR, "cache lookup failed for statistics object %u", statOid);
    1765                 :         335 :                 staForm = (Form_pg_statistic_ext) GETSTRUCT(htup);
    1766                 :             : 
    1767                 :             :                 /*
    1768                 :             :                  * First, build the array of columns covered.  This is ultimately
    1769                 :             :                  * wasted if no stats within the object have actually been built, but
    1770                 :             :                  * it doesn't seem worth troubling over that case.
    1771                 :             :                  */
    1772         [ +  + ]:         952 :                 for (i = 0; i < staForm->stxkeys.dim1; i++)
    1773                 :         617 :                         keys = bms_add_member(keys, staForm->stxkeys.values[i]);
    1774                 :             : 
    1775                 :             :                 /*
    1776                 :             :                  * Preprocess expressions (if any). We read the expressions, fix the
    1777                 :             :                  * varnos, and run them through eval_const_expressions.
    1778                 :             :                  *
    1779                 :             :                  * XXX We don't know yet if there are any data for this stats object,
    1780                 :             :                  * with either stxdinherit value. But it's reasonable to assume there
    1781                 :             :                  * is at least one of those, possibly both. So it's better to process
    1782                 :             :                  * keys and expressions here.
    1783                 :             :                  */
    1784                 :             :                 {
    1785                 :         335 :                         bool            isnull;
    1786                 :         335 :                         Datum           datum;
    1787                 :             : 
    1788                 :             :                         /* decode expression (if any) */
    1789                 :         335 :                         datum = SysCacheGetAttr(STATEXTOID, htup,
    1790                 :             :                                                                         Anum_pg_statistic_ext_stxexprs, &isnull);
    1791                 :             : 
    1792         [ +  + ]:         335 :                         if (!isnull)
    1793                 :             :                         {
    1794                 :         136 :                                 char       *exprsString;
    1795                 :             : 
    1796                 :         136 :                                 exprsString = TextDatumGetCString(datum);
    1797                 :         136 :                                 exprs = (List *) stringToNode(exprsString);
    1798                 :         136 :                                 pfree(exprsString);
    1799                 :             : 
    1800                 :             :                                 /*
    1801                 :             :                                  * Modify the copies we obtain from the relcache to have the
    1802                 :             :                                  * correct varno for the parent relation, so that they match
    1803                 :             :                                  * up correctly against qual clauses.
    1804                 :             :                                  *
    1805                 :             :                                  * This must be done before const-simplification because
    1806                 :             :                                  * eval_const_expressions reduces NullTest for Vars based on
    1807                 :             :                                  * varno.
    1808                 :             :                                  */
    1809         [ +  - ]:         136 :                                 if (varno != 1)
    1810                 :           0 :                                         ChangeVarNodes((Node *) exprs, 1, varno, 0);
    1811                 :             : 
    1812                 :             :                                 /*
    1813                 :             :                                  * Run the expressions through eval_const_expressions. This is
    1814                 :             :                                  * not just an optimization, but is necessary, because the
    1815                 :             :                                  * planner will be comparing them to similarly-processed qual
    1816                 :             :                                  * clauses, and may fail to detect valid matches without this.
    1817                 :             :                                  * We must not use canonicalize_qual, however, since these
    1818                 :             :                                  * aren't qual expressions.
    1819                 :             :                                  */
    1820                 :         136 :                                 exprs = (List *) eval_const_expressions(root, (Node *) exprs);
    1821                 :             : 
    1822                 :             :                                 /* May as well fix opfuncids too */
    1823                 :         136 :                                 fix_opfuncids((Node *) exprs);
    1824                 :         136 :                         }
    1825                 :         335 :                 }
    1826                 :             : 
    1827                 :             :                 /* extract statistics for possible values of stxdinherit flag */
    1828                 :             : 
    1829                 :         335 :                 get_relation_statistics_worker(&stainfos, rel, statOid, true, keys, exprs);
    1830                 :             : 
    1831                 :         335 :                 get_relation_statistics_worker(&stainfos, rel, statOid, false, keys, exprs);
    1832                 :             : 
    1833                 :         335 :                 ReleaseSysCache(htup);
    1834                 :         335 :                 bms_free(keys);
    1835                 :         335 :         }
    1836                 :             : 
    1837                 :       50915 :         list_free(statoidlist);
    1838                 :             : 
    1839                 :      101830 :         return stainfos;
    1840                 :       50915 : }
    1841                 :             : 
    1842                 :             : /*
    1843                 :             :  * relation_excluded_by_constraints
    1844                 :             :  *
    1845                 :             :  * Detect whether the relation need not be scanned because it has either
    1846                 :             :  * self-inconsistent restrictions, or restrictions inconsistent with the
    1847                 :             :  * relation's applicable constraints.
    1848                 :             :  *
    1849                 :             :  * Note: this examines only rel->relid, rel->reloptkind, and
    1850                 :             :  * rel->baserestrictinfo; therefore it can be called before filling in
    1851                 :             :  * other fields of the RelOptInfo.
    1852                 :             :  */
    1853                 :             : bool
    1854                 :       54213 : relation_excluded_by_constraints(PlannerInfo *root,
    1855                 :             :                                                                  RelOptInfo *rel, RangeTblEntry *rte)
    1856                 :             : {
    1857                 :       54213 :         bool            include_noinherit;
    1858                 :       54213 :         bool            include_notnull;
    1859                 :       54213 :         bool            include_partition = false;
    1860                 :       54213 :         List       *safe_restrictions;
    1861                 :       54213 :         List       *constraint_pred;
    1862                 :       54213 :         List       *safe_constraints;
    1863                 :       54213 :         ListCell   *lc;
    1864                 :             : 
    1865                 :             :         /* As of now, constraint exclusion works only with simple relations. */
    1866   [ +  +  +  - ]:       54213 :         Assert(IS_SIMPLE_REL(rel));
    1867                 :             : 
    1868                 :             :         /*
    1869                 :             :          * If there are no base restriction clauses, we have no hope of proving
    1870                 :             :          * anything below, so fall out quickly.
    1871                 :             :          */
    1872         [ +  + ]:       54213 :         if (rel->baserestrictinfo == NIL)
    1873                 :       26247 :                 return false;
    1874                 :             : 
    1875                 :             :         /*
    1876                 :             :          * Regardless of the setting of constraint_exclusion, detect
    1877                 :             :          * constant-FALSE-or-NULL restriction clauses.  Although const-folding
    1878                 :             :          * will reduce "anything AND FALSE" to just "FALSE", the baserestrictinfo
    1879                 :             :          * list can still have other members besides the FALSE constant, due to
    1880                 :             :          * qual pushdown and other mechanisms; so check them all.  This doesn't
    1881                 :             :          * fire very often, but it seems cheap enough to be worth doing anyway.
    1882                 :             :          * (Without this, we'd miss some optimizations that 9.5 and earlier found
    1883                 :             :          * via much more roundabout methods.)
    1884                 :             :          */
    1885   [ +  -  +  +  :       68443 :         foreach(lc, rel->baserestrictinfo)
             +  +  +  + ]
    1886                 :             :         {
    1887                 :       40477 :                 RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
    1888                 :       40477 :                 Expr       *clause = rinfo->clause;
    1889                 :             : 
    1890   [ +  -  +  +  :       40576 :                 if (clause && IsA(clause, Const) &&
                   -  + ]
    1891         [ +  + ]:         100 :                         (((Const *) clause)->constisnull ||
    1892                 :          99 :                          !DatumGetBool(((Const *) clause)->constvalue)))
    1893                 :         100 :                         return true;
    1894         [ +  + ]:       40477 :         }
    1895                 :             : 
    1896                 :             :         /*
    1897                 :             :          * Skip further tests, depending on constraint_exclusion.
    1898                 :             :          */
    1899   [ -  +  +  + ]:       27866 :         switch (constraint_exclusion)
    1900                 :             :         {
    1901                 :             :                 case CONSTRAINT_EXCLUSION_OFF:
    1902                 :             :                         /* In 'off' mode, never make any further tests */
    1903                 :           9 :                         return false;
    1904                 :             : 
    1905                 :             :                 case CONSTRAINT_EXCLUSION_PARTITION:
    1906                 :             : 
    1907                 :             :                         /*
    1908                 :             :                          * When constraint_exclusion is set to 'partition' we only handle
    1909                 :             :                          * appendrel members.  Partition pruning has already been applied,
    1910                 :             :                          * so there is no need to consider the rel's partition constraints
    1911                 :             :                          * here.
    1912                 :             :                          */
    1913         [ +  + ]:       27835 :                         if (rel->reloptkind == RELOPT_OTHER_MEMBER_REL)
    1914                 :        3080 :                                 break;                  /* appendrel member, so process it */
    1915                 :       24755 :                         return false;
    1916                 :             : 
    1917                 :             :                 case CONSTRAINT_EXCLUSION_ON:
    1918                 :             : 
    1919                 :             :                         /*
    1920                 :             :                          * In 'on' mode, always apply constraint exclusion.  If we are
    1921                 :             :                          * considering a baserel that is a partition (i.e., it was
    1922                 :             :                          * directly named rather than expanded from a parent table), then
    1923                 :             :                          * its partition constraints haven't been considered yet, so
    1924                 :             :                          * include them in the processing here.
    1925                 :             :                          */
    1926         [ +  + ]:          22 :                         if (rel->reloptkind == RELOPT_BASEREL)
    1927                 :          17 :                                 include_partition = true;
    1928                 :          22 :                         break;                          /* always try to exclude */
    1929                 :             :         }
    1930                 :             : 
    1931                 :             :         /*
    1932                 :             :          * Check for self-contradictory restriction clauses.  We dare not make
    1933                 :             :          * deductions with non-immutable functions, but any immutable clauses that
    1934                 :             :          * are self-contradictory allow us to conclude the scan is unnecessary.
    1935                 :             :          *
    1936                 :             :          * Note: strip off RestrictInfo because predicate_refuted_by() isn't
    1937                 :             :          * expecting to see any in its predicate argument.
    1938                 :             :          */
    1939                 :        3102 :         safe_restrictions = NIL;
    1940   [ +  -  +  +  :        7514 :         foreach(lc, rel->baserestrictinfo)
                   +  + ]
    1941                 :             :         {
    1942                 :        4412 :                 RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
    1943                 :             : 
    1944         [ +  + ]:        4412 :                 if (!contain_mutable_functions((Node *) rinfo->clause))
    1945                 :        4157 :                         safe_restrictions = lappend(safe_restrictions, rinfo->clause);
    1946                 :        4412 :         }
    1947                 :             : 
    1948                 :             :         /*
    1949                 :             :          * We can use weak refutation here, since we're comparing restriction
    1950                 :             :          * clauses with restriction clauses.
    1951                 :             :          */
    1952         [ +  + ]:        3102 :         if (predicate_refuted_by(safe_restrictions, safe_restrictions, true))
    1953                 :          12 :                 return true;
    1954                 :             : 
    1955                 :             :         /*
    1956                 :             :          * Only plain relations have constraints, so stop here for other rtekinds.
    1957                 :             :          */
    1958         [ +  + ]:        3090 :         if (rte->rtekind != RTE_RELATION)
    1959                 :          47 :                 return false;
    1960                 :             : 
    1961                 :             :         /*
    1962                 :             :          * If we are scanning just this table, we can use NO INHERIT constraints,
    1963                 :             :          * but not if we're scanning its children too.  (Note that partitioned
    1964                 :             :          * tables should never have NO INHERIT constraints; but it's not necessary
    1965                 :             :          * for us to assume that here.)
    1966                 :             :          */
    1967                 :        3043 :         include_noinherit = !rte->inh;
    1968                 :             : 
    1969                 :             :         /*
    1970                 :             :          * Currently, attnotnull constraints must be treated as NO INHERIT unless
    1971                 :             :          * this is a partitioned table.  In future we might track their
    1972                 :             :          * inheritance status more accurately, allowing this to be refined.
    1973                 :             :          *
    1974                 :             :          * XXX do we need/want to change this?
    1975                 :             :          */
    1976         [ +  + ]:        3043 :         include_notnull = (!rte->inh || rte->relkind == RELKIND_PARTITIONED_TABLE);
    1977                 :             : 
    1978                 :             :         /*
    1979                 :             :          * Fetch the appropriate set of constraint expressions.
    1980                 :             :          */
    1981                 :        6086 :         constraint_pred = get_relation_constraints(root, rte->relid, rel,
    1982                 :        3043 :                                                                                            include_noinherit,
    1983                 :        3043 :                                                                                            include_notnull,
    1984                 :        3043 :                                                                                            include_partition);
    1985                 :             : 
    1986                 :             :         /*
    1987                 :             :          * We do not currently enforce that CHECK constraints contain only
    1988                 :             :          * immutable functions, so it's necessary to check here. We daren't draw
    1989                 :             :          * conclusions from plan-time evaluation of non-immutable functions. Since
    1990                 :             :          * they're ANDed, we can just ignore any mutable constraints in the list,
    1991                 :             :          * and reason about the rest.
    1992                 :             :          */
    1993                 :        3043 :         safe_constraints = NIL;
    1994   [ +  +  +  +  :        4343 :         foreach(lc, constraint_pred)
                   +  + ]
    1995                 :             :         {
    1996                 :        1300 :                 Node       *pred = (Node *) lfirst(lc);
    1997                 :             : 
    1998         [ -  + ]:        1300 :                 if (!contain_mutable_functions(pred))
    1999                 :        1300 :                         safe_constraints = lappend(safe_constraints, pred);
    2000                 :        1300 :         }
    2001                 :             : 
    2002                 :             :         /*
    2003                 :             :          * The constraints are effectively ANDed together, so we can just try to
    2004                 :             :          * refute the entire collection at once.  This may allow us to make proofs
    2005                 :             :          * that would fail if we took them individually.
    2006                 :             :          *
    2007                 :             :          * Note: we use rel->baserestrictinfo, not safe_restrictions as might seem
    2008                 :             :          * an obvious optimization.  Some of the clauses might be OR clauses that
    2009                 :             :          * have volatile and nonvolatile subclauses, and it's OK to make
    2010                 :             :          * deductions with the nonvolatile parts.
    2011                 :             :          *
    2012                 :             :          * We need strong refutation because we have to prove that the constraints
    2013                 :             :          * would yield false, not just NULL.
    2014                 :             :          */
    2015         [ +  + ]:        3043 :         if (predicate_refuted_by(safe_constraints, rel->baserestrictinfo, false))
    2016                 :          30 :                 return true;
    2017                 :             : 
    2018                 :        3013 :         return false;
    2019                 :       54213 : }
    2020                 :             : 
    2021                 :             : 
    2022                 :             : /*
    2023                 :             :  * build_physical_tlist
    2024                 :             :  *
    2025                 :             :  * Build a targetlist consisting of exactly the relation's user attributes,
    2026                 :             :  * in order.  The executor can special-case such tlists to avoid a projection
    2027                 :             :  * step at runtime, so we use such tlists preferentially for scan nodes.
    2028                 :             :  *
    2029                 :             :  * Exception: if there are any dropped or missing columns, we punt and return
    2030                 :             :  * NIL.  Ideally we would like to handle these cases too.  However this
    2031                 :             :  * creates problems for ExecTypeFromTL, which may be asked to build a tupdesc
    2032                 :             :  * for a tlist that includes vars of no-longer-existent types.  In theory we
    2033                 :             :  * could dig out the required info from the pg_attribute entries of the
    2034                 :             :  * relation, but that data is not readily available to ExecTypeFromTL.
    2035                 :             :  * For now, we don't apply the physical-tlist optimization when there are
    2036                 :             :  * dropped cols.
    2037                 :             :  *
    2038                 :             :  * We also support building a "physical" tlist for subqueries, functions,
    2039                 :             :  * values lists, table expressions, and CTEs, since the same optimization can
    2040                 :             :  * occur in SubqueryScan, FunctionScan, ValuesScan, CteScan, TableFunc,
    2041                 :             :  * NamedTuplestoreScan, and WorkTableScan nodes.
    2042                 :             :  */
    2043                 :             : List *
    2044                 :       17272 : build_physical_tlist(PlannerInfo *root, RelOptInfo *rel)
    2045                 :             : {
    2046                 :       17272 :         List       *tlist = NIL;
    2047                 :       17272 :         Index           varno = rel->relid;
    2048         [ +  - ]:       17272 :         RangeTblEntry *rte = planner_rt_fetch(varno, root);
    2049                 :       17272 :         Relation        relation;
    2050                 :       17272 :         Query      *subquery;
    2051                 :       17272 :         Var                *var;
    2052                 :       17272 :         ListCell   *l;
    2053                 :       17272 :         int                     attrno,
    2054                 :             :                                 numattrs;
    2055                 :       17272 :         List       *colvars;
    2056                 :             : 
    2057   [ +  +  +  - ]:       17272 :         switch (rte->rtekind)
    2058                 :             :         {
    2059                 :             :                 case RTE_RELATION:
    2060                 :             :                         /* Assume we already have adequate lock */
    2061                 :       15505 :                         relation = table_open(rte->relid, NoLock);
    2062                 :             : 
    2063                 :       15505 :                         numattrs = RelationGetNumberOfAttributes(relation);
    2064         [ +  + ]:      269520 :                         for (attrno = 1; attrno <= numattrs; attrno++)
    2065                 :             :                         {
    2066                 :      508044 :                                 Form_pg_attribute att_tup = TupleDescAttr(relation->rd_att,
    2067                 :      254022 :                                                                                                                   attrno - 1);
    2068                 :             : 
    2069   [ +  +  +  + ]:      254022 :                                 if (att_tup->attisdropped || att_tup->atthasmissing)
    2070                 :             :                                 {
    2071                 :             :                                         /* found a dropped or missing col, so punt */
    2072                 :           7 :                                         tlist = NIL;
    2073                 :           7 :                                         break;
    2074                 :             :                                 }
    2075                 :             : 
    2076                 :      508030 :                                 var = makeVar(varno,
    2077                 :      254015 :                                                           attrno,
    2078                 :      254015 :                                                           att_tup->atttypid,
    2079                 :      254015 :                                                           att_tup->atttypmod,
    2080                 :      254015 :                                                           att_tup->attcollation,
    2081                 :             :                                                           0);
    2082                 :             : 
    2083                 :      508030 :                                 tlist = lappend(tlist,
    2084                 :      508030 :                                                                 makeTargetEntry((Expr *) var,
    2085                 :      254015 :                                                                                                 attrno,
    2086                 :             :                                                                                                 NULL,
    2087                 :             :                                                                                                 false));
    2088      [ -  +  + ]:      254022 :                         }
    2089                 :             : 
    2090                 :       15505 :                         table_close(relation, NoLock);
    2091                 :       15505 :                         break;
    2092                 :             : 
    2093                 :             :                 case RTE_SUBQUERY:
    2094                 :         294 :                         subquery = rte->subquery;
    2095   [ +  -  +  +  :        1042 :                         foreach(l, subquery->targetList)
                   +  + ]
    2096                 :             :                         {
    2097                 :         748 :                                 TargetEntry *tle = (TargetEntry *) lfirst(l);
    2098                 :             : 
    2099                 :             :                                 /*
    2100                 :             :                                  * A resjunk column of the subquery can be reflected as
    2101                 :             :                                  * resjunk in the physical tlist; we need not punt.
    2102                 :             :                                  */
    2103                 :         748 :                                 var = makeVarFromTargetEntry(varno, tle);
    2104                 :             : 
    2105                 :        1496 :                                 tlist = lappend(tlist,
    2106                 :        1496 :                                                                 makeTargetEntry((Expr *) var,
    2107                 :         748 :                                                                                                 tle->resno,
    2108                 :             :                                                                                                 NULL,
    2109                 :         748 :                                                                                                 tle->resjunk));
    2110                 :         748 :                         }
    2111                 :         294 :                         break;
    2112                 :             : 
    2113                 :             :                 case RTE_FUNCTION:
    2114                 :             :                 case RTE_TABLEFUNC:
    2115                 :             :                 case RTE_VALUES:
    2116                 :             :                 case RTE_CTE:
    2117                 :             :                 case RTE_NAMEDTUPLESTORE:
    2118                 :             :                 case RTE_RESULT:
    2119                 :             :                         /* Not all of these can have dropped cols, but share code anyway */
    2120                 :        1473 :                         expandRTE(rte, varno, 0, VAR_RETURNING_DEFAULT, -1,
    2121                 :             :                                           true /* include dropped */ , NULL, &colvars);
    2122   [ +  -  +  +  :        4826 :                         foreach(l, colvars)
                   +  + ]
    2123                 :             :                         {
    2124                 :        3353 :                                 var = (Var *) lfirst(l);
    2125                 :             : 
    2126                 :             :                                 /*
    2127                 :             :                                  * A non-Var in expandRTE's output means a dropped column;
    2128                 :             :                                  * must punt.
    2129                 :             :                                  */
    2130         [ -  + ]:        3353 :                                 if (!IsA(var, Var))
    2131                 :             :                                 {
    2132                 :           0 :                                         tlist = NIL;
    2133                 :           0 :                                         break;
    2134                 :             :                                 }
    2135                 :             : 
    2136                 :        6706 :                                 tlist = lappend(tlist,
    2137                 :        6706 :                                                                 makeTargetEntry((Expr *) var,
    2138                 :        3353 :                                                                                                 var->varattno,
    2139                 :             :                                                                                                 NULL,
    2140                 :             :                                                                                                 false));
    2141                 :        3353 :                         }
    2142                 :        1473 :                         break;
    2143                 :             : 
    2144                 :             :                 default:
    2145                 :             :                         /* caller error */
    2146   [ #  #  #  # ]:           0 :                         elog(ERROR, "unsupported RTE kind %d in build_physical_tlist",
    2147                 :             :                                  (int) rte->rtekind);
    2148                 :           0 :                         break;
    2149                 :             :         }
    2150                 :             : 
    2151                 :       34544 :         return tlist;
    2152                 :       17272 : }
    2153                 :             : 
    2154                 :             : /*
    2155                 :             :  * build_index_tlist
    2156                 :             :  *
    2157                 :             :  * Build a targetlist representing the columns of the specified index.
    2158                 :             :  * Each column is represented by a Var for the corresponding base-relation
    2159                 :             :  * column, or an expression in base-relation Vars, as appropriate.
    2160                 :             :  *
    2161                 :             :  * There are never any dropped columns in indexes, so unlike
    2162                 :             :  * build_physical_tlist, we need no failure case.
    2163                 :             :  */
    2164                 :             : static List *
    2165                 :       72925 : build_index_tlist(PlannerInfo *root, IndexOptInfo *index,
    2166                 :             :                                   Relation heapRelation)
    2167                 :             : {
    2168                 :       72925 :         List       *tlist = NIL;
    2169                 :       72925 :         Index           varno = index->rel->relid;
    2170                 :       72925 :         ListCell   *indexpr_item;
    2171                 :       72925 :         int                     i;
    2172                 :             : 
    2173                 :       72925 :         indexpr_item = list_head(index->indexprs);
    2174         [ +  + ]:      220972 :         for (i = 0; i < index->ncolumns; i++)
    2175                 :             :         {
    2176                 :      148047 :                 int                     indexkey = index->indexkeys[i];
    2177                 :      148047 :                 Expr       *indexvar;
    2178                 :             : 
    2179         [ +  + ]:      148047 :                 if (indexkey != 0)
    2180                 :             :                 {
    2181                 :             :                         /* simple column */
    2182                 :      147557 :                         const FormData_pg_attribute *att_tup;
    2183                 :             : 
    2184         [ +  - ]:      147557 :                         if (indexkey < 0)
    2185                 :           0 :                                 att_tup = SystemAttributeDefinition(indexkey);
    2186                 :             :                         else
    2187                 :      147557 :                                 att_tup = TupleDescAttr(heapRelation->rd_att, indexkey - 1);
    2188                 :             : 
    2189                 :      295114 :                         indexvar = (Expr *) makeVar(varno,
    2190                 :      147557 :                                                                                 indexkey,
    2191                 :      147557 :                                                                                 att_tup->atttypid,
    2192                 :      147557 :                                                                                 att_tup->atttypmod,
    2193                 :      147557 :                                                                                 att_tup->attcollation,
    2194                 :             :                                                                                 0);
    2195                 :      147557 :                 }
    2196                 :             :                 else
    2197                 :             :                 {
    2198                 :             :                         /* expression column */
    2199         [ +  - ]:         490 :                         if (indexpr_item == NULL)
    2200   [ #  #  #  # ]:           0 :                                 elog(ERROR, "wrong number of index expressions");
    2201                 :         490 :                         indexvar = (Expr *) lfirst(indexpr_item);
    2202                 :         490 :                         indexpr_item = lnext(index->indexprs, indexpr_item);
    2203                 :             :                 }
    2204                 :             : 
    2205                 :      296094 :                 tlist = lappend(tlist,
    2206                 :      296094 :                                                 makeTargetEntry(indexvar,
    2207                 :      148047 :                                                                                 i + 1,
    2208                 :             :                                                                                 NULL,
    2209                 :             :                                                                                 false));
    2210                 :      148047 :         }
    2211         [ +  - ]:       72925 :         if (indexpr_item != NULL)
    2212   [ #  #  #  # ]:           0 :                 elog(ERROR, "wrong number of index expressions");
    2213                 :             : 
    2214                 :      145850 :         return tlist;
    2215                 :       72925 : }
    2216                 :             : 
    2217                 :             : /*
    2218                 :             :  * restriction_selectivity
    2219                 :             :  *
    2220                 :             :  * Returns the selectivity of a specified restriction operator clause.
    2221                 :             :  * This code executes registered procedures stored in the
    2222                 :             :  * operator relation, by calling the function manager.
    2223                 :             :  *
    2224                 :             :  * See clause_selectivity() for the meaning of the additional parameters.
    2225                 :             :  */
    2226                 :             : Selectivity
    2227                 :       68727 : restriction_selectivity(PlannerInfo *root,
    2228                 :             :                                                 Oid operatorid,
    2229                 :             :                                                 List *args,
    2230                 :             :                                                 Oid inputcollid,
    2231                 :             :                                                 int varRelid)
    2232                 :             : {
    2233                 :       68727 :         RegProcedure oprrest = get_oprrest(operatorid);
    2234                 :       68727 :         float8          result;
    2235                 :             : 
    2236                 :             :         /*
    2237                 :             :          * if the oprrest procedure is missing for whatever reason, use a
    2238                 :             :          * selectivity of 0.5
    2239                 :             :          */
    2240         [ +  + ]:       68727 :         if (!oprrest)
    2241                 :           8 :                 return (Selectivity) 0.5;
    2242                 :             : 
    2243                 :      137438 :         result = DatumGetFloat8(OidFunctionCall4Coll(oprrest,
    2244                 :       68719 :                                                                                                  inputcollid,
    2245                 :       68719 :                                                                                                  PointerGetDatum(root),
    2246                 :       68719 :                                                                                                  ObjectIdGetDatum(operatorid),
    2247                 :       68719 :                                                                                                  PointerGetDatum(args),
    2248                 :       68719 :                                                                                                  Int32GetDatum(varRelid)));
    2249                 :             : 
    2250         [ +  - ]:       68719 :         if (result < 0.0 || result > 1.0)
    2251   [ #  #  #  # ]:           0 :                 elog(ERROR, "invalid restriction selectivity: %f", result);
    2252                 :             : 
    2253                 :       68719 :         return (Selectivity) result;
    2254                 :       68727 : }
    2255                 :             : 
    2256                 :             : /*
    2257                 :             :  * join_selectivity
    2258                 :             :  *
    2259                 :             :  * Returns the selectivity of a specified join operator clause.
    2260                 :             :  * This code executes registered procedures stored in the
    2261                 :             :  * operator relation, by calling the function manager.
    2262                 :             :  *
    2263                 :             :  * See clause_selectivity() for the meaning of the additional parameters.
    2264                 :             :  */
    2265                 :             : Selectivity
    2266                 :       25352 : join_selectivity(PlannerInfo *root,
    2267                 :             :                                  Oid operatorid,
    2268                 :             :                                  List *args,
    2269                 :             :                                  Oid inputcollid,
    2270                 :             :                                  JoinType jointype,
    2271                 :             :                                  SpecialJoinInfo *sjinfo)
    2272                 :             : {
    2273                 :       25352 :         RegProcedure oprjoin = get_oprjoin(operatorid);
    2274                 :       25352 :         float8          result;
    2275                 :             : 
    2276                 :             :         /*
    2277                 :             :          * if the oprjoin procedure is missing for whatever reason, use a
    2278                 :             :          * selectivity of 0.5
    2279                 :             :          */
    2280         [ +  + ]:       25352 :         if (!oprjoin)
    2281                 :          23 :                 return (Selectivity) 0.5;
    2282                 :             : 
    2283                 :       50658 :         result = DatumGetFloat8(OidFunctionCall5Coll(oprjoin,
    2284                 :       25329 :                                                                                                  inputcollid,
    2285                 :       25329 :                                                                                                  PointerGetDatum(root),
    2286                 :       25329 :                                                                                                  ObjectIdGetDatum(operatorid),
    2287                 :       25329 :                                                                                                  PointerGetDatum(args),
    2288                 :       25329 :                                                                                                  Int16GetDatum(jointype),
    2289                 :       25329 :                                                                                                  PointerGetDatum(sjinfo)));
    2290                 :             : 
    2291         [ +  - ]:       25329 :         if (result < 0.0 || result > 1.0)
    2292   [ #  #  #  # ]:           0 :                 elog(ERROR, "invalid join selectivity: %f", result);
    2293                 :             : 
    2294                 :       25329 :         return (Selectivity) result;
    2295                 :       25352 : }
    2296                 :             : 
    2297                 :             : /*
    2298                 :             :  * function_selectivity
    2299                 :             :  *
    2300                 :             :  * Attempt to estimate the selectivity of a specified boolean function clause
    2301                 :             :  * by asking its support function.  If the function lacks support, return -1.
    2302                 :             :  *
    2303                 :             :  * See clause_selectivity() for the meaning of the additional parameters.
    2304                 :             :  */
    2305                 :             : Selectivity
    2306                 :        1853 : function_selectivity(PlannerInfo *root,
    2307                 :             :                                          Oid funcid,
    2308                 :             :                                          List *args,
    2309                 :             :                                          Oid inputcollid,
    2310                 :             :                                          bool is_join,
    2311                 :             :                                          int varRelid,
    2312                 :             :                                          JoinType jointype,
    2313                 :             :                                          SpecialJoinInfo *sjinfo)
    2314                 :             : {
    2315                 :        1853 :         RegProcedure prosupport = get_func_support(funcid);
    2316                 :        1853 :         SupportRequestSelectivity req;
    2317                 :        1853 :         SupportRequestSelectivity *sresult;
    2318                 :             : 
    2319         [ +  + ]:        1853 :         if (!prosupport)
    2320                 :        1848 :                 return (Selectivity) -1;        /* no support function */
    2321                 :             : 
    2322                 :           5 :         req.type = T_SupportRequestSelectivity;
    2323                 :           5 :         req.root = root;
    2324                 :           5 :         req.funcid = funcid;
    2325                 :           5 :         req.args = args;
    2326                 :           5 :         req.inputcollid = inputcollid;
    2327                 :           5 :         req.is_join = is_join;
    2328                 :           5 :         req.varRelid = varRelid;
    2329                 :           5 :         req.jointype = jointype;
    2330                 :           5 :         req.sjinfo = sjinfo;
    2331                 :           5 :         req.selectivity = -1;           /* to catch failure to set the value */
    2332                 :             : 
    2333                 :           5 :         sresult = (SupportRequestSelectivity *)
    2334                 :           5 :                 DatumGetPointer(OidFunctionCall1(prosupport,
    2335                 :             :                                                                                  PointerGetDatum(&req)));
    2336                 :             : 
    2337         [ -  + ]:           5 :         if (sresult != &req)
    2338                 :           0 :                 return (Selectivity) -1;        /* function did not honor request */
    2339                 :             : 
    2340         [ +  - ]:           5 :         if (req.selectivity < 0.0 || req.selectivity > 1.0)
    2341   [ #  #  #  # ]:           0 :                 elog(ERROR, "invalid function selectivity: %f", req.selectivity);
    2342                 :             : 
    2343                 :           5 :         return (Selectivity) req.selectivity;
    2344                 :        1853 : }
    2345                 :             : 
    2346                 :             : /*
    2347                 :             :  * add_function_cost
    2348                 :             :  *
    2349                 :             :  * Get an estimate of the execution cost of a function, and *add* it to
    2350                 :             :  * the contents of *cost.  The estimate may include both one-time and
    2351                 :             :  * per-tuple components, since QualCost does.
    2352                 :             :  *
    2353                 :             :  * The funcid must always be supplied.  If it is being called as the
    2354                 :             :  * implementation of a specific parsetree node (FuncExpr, OpExpr,
    2355                 :             :  * WindowFunc, etc), pass that as "node", else pass NULL.
    2356                 :             :  *
    2357                 :             :  * In some usages root might be NULL, too.
    2358                 :             :  */
    2359                 :             : void
    2360                 :      123659 : add_function_cost(PlannerInfo *root, Oid funcid, Node *node,
    2361                 :             :                                   QualCost *cost)
    2362                 :             : {
    2363                 :      123659 :         HeapTuple       proctup;
    2364                 :      123659 :         Form_pg_proc procform;
    2365                 :             : 
    2366                 :      123659 :         proctup = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
    2367         [ +  - ]:      123659 :         if (!HeapTupleIsValid(proctup))
    2368   [ #  #  #  # ]:           0 :                 elog(ERROR, "cache lookup failed for function %u", funcid);
    2369                 :      123659 :         procform = (Form_pg_proc) GETSTRUCT(proctup);
    2370                 :             : 
    2371         [ +  + ]:      123659 :         if (OidIsValid(procform->prosupport))
    2372                 :             :         {
    2373                 :        4206 :                 SupportRequestCost req;
    2374                 :        4206 :                 SupportRequestCost *sresult;
    2375                 :             : 
    2376                 :        4206 :                 req.type = T_SupportRequestCost;
    2377                 :        4206 :                 req.root = root;
    2378                 :        4206 :                 req.funcid = funcid;
    2379                 :        4206 :                 req.node = node;
    2380                 :             : 
    2381                 :             :                 /* Initialize cost fields so that support function doesn't have to */
    2382                 :        4206 :                 req.startup = 0;
    2383                 :        4206 :                 req.per_tuple = 0;
    2384                 :             : 
    2385                 :        4206 :                 sresult = (SupportRequestCost *)
    2386                 :        4206 :                         DatumGetPointer(OidFunctionCall1(procform->prosupport,
    2387                 :             :                                                                                          PointerGetDatum(&req)));
    2388                 :             : 
    2389         [ +  + ]:        4206 :                 if (sresult == &req)
    2390                 :             :                 {
    2391                 :             :                         /* Success, so accumulate support function's estimate into *cost */
    2392                 :           3 :                         cost->startup += req.startup;
    2393                 :           3 :                         cost->per_tuple += req.per_tuple;
    2394                 :           3 :                         ReleaseSysCache(proctup);
    2395                 :           3 :                         return;
    2396                 :             :                 }
    2397         [ +  + ]:        4206 :         }
    2398                 :             : 
    2399                 :             :         /* No support function, or it failed, so rely on procost */
    2400                 :      123656 :         cost->per_tuple += procform->procost * cpu_operator_cost;
    2401                 :             : 
    2402                 :      123656 :         ReleaseSysCache(proctup);
    2403         [ -  + ]:      123659 : }
    2404                 :             : 
    2405                 :             : /*
    2406                 :             :  * get_function_rows
    2407                 :             :  *
    2408                 :             :  * Get an estimate of the number of rows returned by a set-returning function.
    2409                 :             :  *
    2410                 :             :  * The funcid must always be supplied.  In current usage, the calling node
    2411                 :             :  * will always be supplied, and will be either a FuncExpr or OpExpr.
    2412                 :             :  * But it's a good idea to not fail if it's NULL.
    2413                 :             :  *
    2414                 :             :  * In some usages root might be NULL, too.
    2415                 :             :  *
    2416                 :             :  * Note: this returns the unfiltered result of the support function, if any.
    2417                 :             :  * It's usually a good idea to apply clamp_row_est() to the result, but we
    2418                 :             :  * leave it to the caller to do so.
    2419                 :             :  */
    2420                 :             : double
    2421                 :        4833 : get_function_rows(PlannerInfo *root, Oid funcid, Node *node)
    2422                 :             : {
    2423                 :        4833 :         HeapTuple       proctup;
    2424                 :        4833 :         Form_pg_proc procform;
    2425                 :        4833 :         double          result;
    2426                 :             : 
    2427                 :        4833 :         proctup = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
    2428         [ +  - ]:        4833 :         if (!HeapTupleIsValid(proctup))
    2429   [ #  #  #  # ]:           0 :                 elog(ERROR, "cache lookup failed for function %u", funcid);
    2430                 :        4833 :         procform = (Form_pg_proc) GETSTRUCT(proctup);
    2431                 :             : 
    2432         [ +  - ]:        4833 :         Assert(procform->proretset); /* else caller error */
    2433                 :             : 
    2434         [ +  + ]:        4833 :         if (OidIsValid(procform->prosupport))
    2435                 :             :         {
    2436                 :        2072 :                 SupportRequestRows req;
    2437                 :        2072 :                 SupportRequestRows *sresult;
    2438                 :             : 
    2439                 :        2072 :                 req.type = T_SupportRequestRows;
    2440                 :        2072 :                 req.root = root;
    2441                 :        2072 :                 req.funcid = funcid;
    2442                 :        2072 :                 req.node = node;
    2443                 :             : 
    2444                 :        2072 :                 req.rows = 0;                   /* just for sanity */
    2445                 :             : 
    2446                 :        2072 :                 sresult = (SupportRequestRows *)
    2447                 :        2072 :                         DatumGetPointer(OidFunctionCall1(procform->prosupport,
    2448                 :             :                                                                                          PointerGetDatum(&req)));
    2449                 :             : 
    2450         [ +  + ]:        2072 :                 if (sresult == &req)
    2451                 :             :                 {
    2452                 :             :                         /* Success */
    2453                 :        1450 :                         ReleaseSysCache(proctup);
    2454                 :        1450 :                         return req.rows;
    2455                 :             :                 }
    2456         [ +  + ]:        2072 :         }
    2457                 :             : 
    2458                 :             :         /* No support function, or it failed, so rely on prorows */
    2459                 :        3383 :         result = procform->prorows;
    2460                 :             : 
    2461                 :        3383 :         ReleaseSysCache(proctup);
    2462                 :             : 
    2463                 :        3383 :         return result;
    2464                 :        4833 : }
    2465                 :             : 
    2466                 :             : /*
    2467                 :             :  * has_unique_index
    2468                 :             :  *
    2469                 :             :  * Detect whether there is a unique index on the specified attribute
    2470                 :             :  * of the specified relation, thus allowing us to conclude that all
    2471                 :             :  * the (non-null) values of the attribute are distinct.
    2472                 :             :  *
    2473                 :             :  * This function does not check the index's indimmediate property, which
    2474                 :             :  * means that uniqueness may transiently fail to hold intra-transaction.
    2475                 :             :  * That's appropriate when we are making statistical estimates, but beware
    2476                 :             :  * of using this for any correctness proofs.
    2477                 :             :  */
    2478                 :             : bool
    2479                 :      212579 : has_unique_index(RelOptInfo *rel, AttrNumber attno)
    2480                 :             : {
    2481                 :      212579 :         ListCell   *ilist;
    2482                 :             : 
    2483   [ +  +  +  +  :      579576 :         foreach(ilist, rel->indexlist)
             +  +  +  + ]
    2484                 :             :         {
    2485                 :      366997 :                 IndexOptInfo *index = (IndexOptInfo *) lfirst(ilist);
    2486                 :             : 
    2487                 :             :                 /*
    2488                 :             :                  * Note: ignore partial indexes, since they don't allow us to conclude
    2489                 :             :                  * that all attr values are distinct, *unless* they are marked predOK
    2490                 :             :                  * which means we know the index's predicate is satisfied by the
    2491                 :             :                  * query. We don't take any interest in expressional indexes either.
    2492                 :             :                  * Also, a multicolumn unique index doesn't allow us to conclude that
    2493                 :             :                  * just the specified attr is unique.
    2494                 :             :                  */
    2495         [ +  + ]:      366997 :                 if (index->unique &&
    2496         [ +  + ]:      249302 :                         index->nkeycolumns == 1 &&
    2497   [ +  +  -  + ]:      130493 :                         index->indexkeys[0] == attno &&
    2498         [ +  + ]:       54291 :                         (index->indpred == NIL || index->predOK))
    2499                 :       54280 :                         return true;
    2500         [ +  + ]:      366997 :         }
    2501                 :      158299 :         return false;
    2502                 :      212579 : }
    2503                 :             : 
    2504                 :             : 
    2505                 :             : /*
    2506                 :             :  * has_row_triggers
    2507                 :             :  *
    2508                 :             :  * Detect whether the specified relation has any row-level triggers for event.
    2509                 :             :  */
    2510                 :             : bool
    2511                 :           0 : has_row_triggers(PlannerInfo *root, Index rti, CmdType event)
    2512                 :             : {
    2513         [ #  # ]:           0 :         RangeTblEntry *rte = planner_rt_fetch(rti, root);
    2514                 :           0 :         Relation        relation;
    2515                 :           0 :         TriggerDesc *trigDesc;
    2516                 :           0 :         bool            result = false;
    2517                 :             : 
    2518                 :             :         /* Assume we already have adequate lock */
    2519                 :           0 :         relation = table_open(rte->relid, NoLock);
    2520                 :             : 
    2521                 :           0 :         trigDesc = relation->trigdesc;
    2522   [ #  #  #  #  :           0 :         switch (event)
                      # ]
    2523                 :             :         {
    2524                 :             :                 case CMD_INSERT:
    2525   [ #  #  #  # ]:           0 :                         if (trigDesc &&
    2526         [ #  # ]:           0 :                                 (trigDesc->trig_insert_after_row ||
    2527                 :           0 :                                  trigDesc->trig_insert_before_row))
    2528                 :           0 :                                 result = true;
    2529                 :           0 :                         break;
    2530                 :             :                 case CMD_UPDATE:
    2531   [ #  #  #  # ]:           0 :                         if (trigDesc &&
    2532         [ #  # ]:           0 :                                 (trigDesc->trig_update_after_row ||
    2533                 :           0 :                                  trigDesc->trig_update_before_row))
    2534                 :           0 :                                 result = true;
    2535                 :           0 :                         break;
    2536                 :             :                 case CMD_DELETE:
    2537   [ #  #  #  # ]:           0 :                         if (trigDesc &&
    2538         [ #  # ]:           0 :                                 (trigDesc->trig_delete_after_row ||
    2539                 :           0 :                                  trigDesc->trig_delete_before_row))
    2540                 :           0 :                                 result = true;
    2541                 :           0 :                         break;
    2542                 :             :                         /* There is no separate event for MERGE, only INSERT/UPDATE/DELETE */
    2543                 :             :                 case CMD_MERGE:
    2544                 :           0 :                         result = false;
    2545                 :           0 :                         break;
    2546                 :             :                 default:
    2547   [ #  #  #  # ]:           0 :                         elog(ERROR, "unrecognized CmdType: %d", (int) event);
    2548                 :           0 :                         break;
    2549                 :             :         }
    2550                 :             : 
    2551                 :           0 :         table_close(relation, NoLock);
    2552                 :           0 :         return result;
    2553                 :           0 : }
    2554                 :             : 
    2555                 :             : /*
    2556                 :             :  * has_transition_tables
    2557                 :             :  *
    2558                 :             :  * Detect whether the specified relation has any transition tables for event.
    2559                 :             :  */
    2560                 :             : bool
    2561                 :           0 : has_transition_tables(PlannerInfo *root, Index rti, CmdType event)
    2562                 :             : {
    2563         [ #  # ]:           0 :         RangeTblEntry *rte = planner_rt_fetch(rti, root);
    2564                 :           0 :         Relation        relation;
    2565                 :           0 :         TriggerDesc *trigDesc;
    2566                 :           0 :         bool            result = false;
    2567                 :             : 
    2568         [ #  # ]:           0 :         Assert(rte->rtekind == RTE_RELATION);
    2569                 :             : 
    2570                 :             :         /* Currently foreign tables cannot have transition tables */
    2571         [ #  # ]:           0 :         if (rte->relkind == RELKIND_FOREIGN_TABLE)
    2572                 :           0 :                 return result;
    2573                 :             : 
    2574                 :             :         /* Assume we already have adequate lock */
    2575                 :           0 :         relation = table_open(rte->relid, NoLock);
    2576                 :             : 
    2577                 :           0 :         trigDesc = relation->trigdesc;
    2578   [ #  #  #  #  :           0 :         switch (event)
                      # ]
    2579                 :             :         {
    2580                 :             :                 case CMD_INSERT:
    2581   [ #  #  #  # ]:           0 :                         if (trigDesc &&
    2582                 :           0 :                                 trigDesc->trig_insert_new_table)
    2583                 :           0 :                                 result = true;
    2584                 :           0 :                         break;
    2585                 :             :                 case CMD_UPDATE:
    2586   [ #  #  #  # ]:           0 :                         if (trigDesc &&
    2587         [ #  # ]:           0 :                                 (trigDesc->trig_update_old_table ||
    2588                 :           0 :                                  trigDesc->trig_update_new_table))
    2589                 :           0 :                                 result = true;
    2590                 :           0 :                         break;
    2591                 :             :                 case CMD_DELETE:
    2592   [ #  #  #  # ]:           0 :                         if (trigDesc &&
    2593                 :           0 :                                 trigDesc->trig_delete_old_table)
    2594                 :           0 :                                 result = true;
    2595                 :           0 :                         break;
    2596                 :             :                         /* There is no separate event for MERGE, only INSERT/UPDATE/DELETE */
    2597                 :             :                 case CMD_MERGE:
    2598                 :           0 :                         result = false;
    2599                 :           0 :                         break;
    2600                 :             :                 default:
    2601   [ #  #  #  # ]:           0 :                         elog(ERROR, "unrecognized CmdType: %d", (int) event);
    2602                 :           0 :                         break;
    2603                 :             :         }
    2604                 :             : 
    2605                 :           0 :         table_close(relation, NoLock);
    2606                 :           0 :         return result;
    2607                 :           0 : }
    2608                 :             : 
    2609                 :             : /*
    2610                 :             :  * has_stored_generated_columns
    2611                 :             :  *
    2612                 :             :  * Does table identified by RTI have any STORED GENERATED columns?
    2613                 :             :  */
    2614                 :             : bool
    2615                 :           0 : has_stored_generated_columns(PlannerInfo *root, Index rti)
    2616                 :             : {
    2617         [ #  # ]:           0 :         RangeTblEntry *rte = planner_rt_fetch(rti, root);
    2618                 :           0 :         Relation        relation;
    2619                 :           0 :         TupleDesc       tupdesc;
    2620                 :           0 :         bool            result = false;
    2621                 :             : 
    2622                 :             :         /* Assume we already have adequate lock */
    2623                 :           0 :         relation = table_open(rte->relid, NoLock);
    2624                 :             : 
    2625                 :           0 :         tupdesc = RelationGetDescr(relation);
    2626         [ #  # ]:           0 :         result = tupdesc->constr && tupdesc->constr->has_generated_stored;
    2627                 :             : 
    2628                 :           0 :         table_close(relation, NoLock);
    2629                 :             : 
    2630                 :           0 :         return result;
    2631                 :           0 : }
    2632                 :             : 
    2633                 :             : /*
    2634                 :             :  * get_dependent_generated_columns
    2635                 :             :  *
    2636                 :             :  * Get the column numbers of any STORED GENERATED columns of the relation
    2637                 :             :  * that depend on any column listed in target_cols.  Both the input and
    2638                 :             :  * result bitmapsets contain column numbers offset by
    2639                 :             :  * FirstLowInvalidHeapAttributeNumber.
    2640                 :             :  */
    2641                 :             : Bitmapset *
    2642                 :           0 : get_dependent_generated_columns(PlannerInfo *root, Index rti,
    2643                 :             :                                                                 Bitmapset *target_cols)
    2644                 :             : {
    2645                 :           0 :         Bitmapset  *dependentCols = NULL;
    2646         [ #  # ]:           0 :         RangeTblEntry *rte = planner_rt_fetch(rti, root);
    2647                 :           0 :         Relation        relation;
    2648                 :           0 :         TupleDesc       tupdesc;
    2649                 :           0 :         TupleConstr *constr;
    2650                 :             : 
    2651                 :             :         /* Assume we already have adequate lock */
    2652                 :           0 :         relation = table_open(rte->relid, NoLock);
    2653                 :             : 
    2654                 :           0 :         tupdesc = RelationGetDescr(relation);
    2655                 :           0 :         constr = tupdesc->constr;
    2656                 :             : 
    2657   [ #  #  #  # ]:           0 :         if (constr && constr->has_generated_stored)
    2658                 :             :         {
    2659         [ #  # ]:           0 :                 for (int i = 0; i < constr->num_defval; i++)
    2660                 :             :                 {
    2661                 :           0 :                         AttrDefault *defval = &constr->defval[i];
    2662                 :           0 :                         Node       *expr;
    2663                 :           0 :                         Bitmapset  *attrs_used = NULL;
    2664                 :             : 
    2665                 :             :                         /* skip if not generated column */
    2666         [ #  # ]:           0 :                         if (!TupleDescCompactAttr(tupdesc, defval->adnum - 1)->attgenerated)
    2667                 :           0 :                                 continue;
    2668                 :             : 
    2669                 :             :                         /* identify columns this generated column depends on */
    2670                 :           0 :                         expr = stringToNode(defval->adbin);
    2671                 :           0 :                         pull_varattnos(expr, 1, &attrs_used);
    2672                 :             : 
    2673         [ #  # ]:           0 :                         if (bms_overlap(target_cols, attrs_used))
    2674                 :           0 :                                 dependentCols = bms_add_member(dependentCols,
    2675                 :           0 :                                                                                            defval->adnum - FirstLowInvalidHeapAttributeNumber);
    2676      [ #  #  # ]:           0 :                 }
    2677                 :           0 :         }
    2678                 :             : 
    2679                 :           0 :         table_close(relation, NoLock);
    2680                 :             : 
    2681                 :           0 :         return dependentCols;
    2682                 :           0 : }
    2683                 :             : 
    2684                 :             : /*
    2685                 :             :  * set_relation_partition_info
    2686                 :             :  *
    2687                 :             :  * Set partitioning scheme and related information for a partitioned table.
    2688                 :             :  */
    2689                 :             : static void
    2690                 :        2356 : set_relation_partition_info(PlannerInfo *root, RelOptInfo *rel,
    2691                 :             :                                                         Relation relation)
    2692                 :             : {
    2693                 :        2356 :         PartitionDesc partdesc;
    2694                 :             : 
    2695                 :             :         /*
    2696                 :             :          * Create the PartitionDirectory infrastructure if we didn't already.
    2697                 :             :          */
    2698         [ +  + ]:        2356 :         if (root->glob->partition_directory == NULL)
    2699                 :             :         {
    2700                 :        1376 :                 root->glob->partition_directory =
    2701                 :        1376 :                         CreatePartitionDirectory(CurrentMemoryContext, true);
    2702                 :        1376 :         }
    2703                 :             : 
    2704                 :        4712 :         partdesc = PartitionDirectoryLookup(root->glob->partition_directory,
    2705                 :        2356 :                                                                                 relation);
    2706                 :        2356 :         rel->part_scheme = find_partition_scheme(root, relation);
    2707         [ +  - ]:        2356 :         Assert(partdesc != NULL && rel->part_scheme != NULL);
    2708                 :        2356 :         rel->boundinfo = partdesc->boundinfo;
    2709                 :        2356 :         rel->nparts = partdesc->nparts;
    2710                 :        2356 :         set_baserel_partition_key_exprs(relation, rel);
    2711                 :        2356 :         set_baserel_partition_constraint(relation, rel);
    2712                 :        2356 : }
    2713                 :             : 
    2714                 :             : /*
    2715                 :             :  * find_partition_scheme
    2716                 :             :  *
    2717                 :             :  * Find or create a PartitionScheme for this Relation.
    2718                 :             :  */
    2719                 :             : static PartitionScheme
    2720                 :        2356 : find_partition_scheme(PlannerInfo *root, Relation relation)
    2721                 :             : {
    2722                 :        2356 :         PartitionKey partkey = RelationGetPartitionKey(relation);
    2723                 :        2356 :         ListCell   *lc;
    2724                 :        2356 :         int                     partnatts,
    2725                 :             :                                 i;
    2726                 :        2356 :         PartitionScheme part_scheme;
    2727                 :             : 
    2728                 :             :         /* A partitioned table should have a partition key. */
    2729         [ +  - ]:        2356 :         Assert(partkey != NULL);
    2730                 :             : 
    2731                 :        2356 :         partnatts = partkey->partnatts;
    2732                 :             : 
    2733                 :             :         /* Search for a matching partition scheme and return if found one. */
    2734   [ +  +  +  +  :        3437 :         foreach(lc, root->part_schemes)
             +  +  +  + ]
    2735                 :             :         {
    2736                 :        1081 :                 part_scheme = lfirst(lc);
    2737                 :             : 
    2738                 :             :                 /* Match partitioning strategy and number of keys. */
    2739   [ +  +  +  + ]:        1081 :                 if (partkey->strategy != part_scheme->strategy ||
    2740                 :         917 :                         partnatts != part_scheme->partnatts)
    2741                 :         239 :                         continue;
    2742                 :             : 
    2743                 :             :                 /* Match partition key type properties. */
    2744                 :        1684 :                 if (memcmp(partkey->partopfamily, part_scheme->partopfamily,
    2745   [ +  +  +  + ]:        1684 :                                    sizeof(Oid) * partnatts) != 0 ||
    2746                 :        1534 :                         memcmp(partkey->partopcintype, part_scheme->partopcintype,
    2747   [ +  -  +  -  :        1534 :                                    sizeof(Oid) * partnatts) != 0 ||
                   -  + ]
    2748                 :        1534 :                         memcmp(partkey->partcollation, part_scheme->partcollation,
    2749                 :        1534 :                                    sizeof(Oid) * partnatts) != 0)
    2750                 :          75 :                         continue;
    2751                 :             : 
    2752                 :             :                 /*
    2753                 :             :                  * Length and byval information should match when partopcintype
    2754                 :             :                  * matches.
    2755                 :             :                  */
    2756         [ +  - ]:         767 :                 Assert(memcmp(partkey->parttyplen, part_scheme->parttyplen,
    2757                 :             :                                           sizeof(int16) * partnatts) == 0);
    2758         [ +  - ]:         767 :                 Assert(memcmp(partkey->parttypbyval, part_scheme->parttypbyval,
    2759                 :             :                                           sizeof(bool) * partnatts) == 0);
    2760                 :             : 
    2761                 :             :                 /*
    2762                 :             :                  * If partopfamily and partopcintype matched, must have the same
    2763                 :             :                  * partition comparison functions.  Note that we cannot reliably
    2764                 :             :                  * Assert the equality of function structs themselves for they might
    2765                 :             :                  * be different across PartitionKey's, so just Assert for the function
    2766                 :             :                  * OIDs.
    2767                 :             :                  */
    2768                 :             : #ifdef USE_ASSERT_CHECKING
    2769         [ +  + ]:        1539 :                 for (i = 0; i < partkey->partnatts; i++)
    2770         [ +  - ]:         772 :                         Assert(partkey->partsupfunc[i].fn_oid ==
    2771                 :             :                                    part_scheme->partsupfunc[i].fn_oid);
    2772                 :             : #endif
    2773                 :             : 
    2774                 :             :                 /* Found matching partition scheme. */
    2775                 :         767 :                 return part_scheme;
    2776                 :             :         }
    2777                 :             : 
    2778                 :             :         /*
    2779                 :             :          * Did not find matching partition scheme. Create one copying relevant
    2780                 :             :          * information from the relcache. We need to copy the contents of the
    2781                 :             :          * array since the relcache entry may not survive after we have closed the
    2782                 :             :          * relation.
    2783                 :             :          */
    2784                 :        1589 :         part_scheme = palloc0_object(PartitionSchemeData);
    2785                 :        1589 :         part_scheme->strategy = partkey->strategy;
    2786                 :        1589 :         part_scheme->partnatts = partkey->partnatts;
    2787                 :             : 
    2788                 :        1589 :         part_scheme->partopfamily = palloc_array(Oid, partnatts);
    2789                 :        1589 :         memcpy(part_scheme->partopfamily, partkey->partopfamily,
    2790                 :             :                    sizeof(Oid) * partnatts);
    2791                 :             : 
    2792                 :        1589 :         part_scheme->partopcintype = palloc_array(Oid, partnatts);
    2793                 :        1589 :         memcpy(part_scheme->partopcintype, partkey->partopcintype,
    2794                 :             :                    sizeof(Oid) * partnatts);
    2795                 :             : 
    2796                 :        1589 :         part_scheme->partcollation = palloc_array(Oid, partnatts);
    2797                 :        1589 :         memcpy(part_scheme->partcollation, partkey->partcollation,
    2798                 :             :                    sizeof(Oid) * partnatts);
    2799                 :             : 
    2800                 :        1589 :         part_scheme->parttyplen = palloc_array(int16, partnatts);
    2801                 :        1589 :         memcpy(part_scheme->parttyplen, partkey->parttyplen,
    2802                 :             :                    sizeof(int16) * partnatts);
    2803                 :             : 
    2804                 :        1589 :         part_scheme->parttypbyval = palloc_array(bool, partnatts);
    2805                 :        1589 :         memcpy(part_scheme->parttypbyval, partkey->parttypbyval,
    2806                 :             :                    sizeof(bool) * partnatts);
    2807                 :             : 
    2808                 :        1589 :         part_scheme->partsupfunc = palloc_array(FmgrInfo, partnatts);
    2809         [ +  + ]:        3494 :         for (i = 0; i < partnatts; i++)
    2810                 :        3810 :                 fmgr_info_copy(&part_scheme->partsupfunc[i], &partkey->partsupfunc[i],
    2811                 :        1905 :                                            CurrentMemoryContext);
    2812                 :             : 
    2813                 :             :         /* Add the partitioning scheme to PlannerInfo. */
    2814                 :        1589 :         root->part_schemes = lappend(root->part_schemes, part_scheme);
    2815                 :             : 
    2816                 :        1589 :         return part_scheme;
    2817                 :        2356 : }
    2818                 :             : 
    2819                 :             : /*
    2820                 :             :  * set_baserel_partition_key_exprs
    2821                 :             :  *
    2822                 :             :  * Builds partition key expressions for the given base relation and fills
    2823                 :             :  * rel->partexprs.
    2824                 :             :  */
    2825                 :             : static void
    2826                 :        2356 : set_baserel_partition_key_exprs(Relation relation,
    2827                 :             :                                                                 RelOptInfo *rel)
    2828                 :             : {
    2829                 :        2356 :         PartitionKey partkey = RelationGetPartitionKey(relation);
    2830                 :        2356 :         int                     partnatts;
    2831                 :        2356 :         int                     cnt;
    2832                 :        2356 :         List      **partexprs;
    2833                 :        2356 :         ListCell   *lc;
    2834                 :        2356 :         Index           varno = rel->relid;
    2835                 :             : 
    2836   [ +  +  +  - ]:        2356 :         Assert(IS_SIMPLE_REL(rel) && rel->relid > 0);
    2837                 :             : 
    2838                 :             :         /* A partitioned table should have a partition key. */
    2839         [ +  - ]:        2356 :         Assert(partkey != NULL);
    2840                 :             : 
    2841                 :        2356 :         partnatts = partkey->partnatts;
    2842                 :        2356 :         partexprs = palloc_array(List *, partnatts);
    2843                 :        2356 :         lc = list_head(partkey->partexprs);
    2844                 :             : 
    2845         [ +  + ]:        5033 :         for (cnt = 0; cnt < partnatts; cnt++)
    2846                 :             :         {
    2847                 :        2677 :                 Expr       *partexpr;
    2848                 :        2677 :                 AttrNumber      attno = partkey->partattrs[cnt];
    2849                 :             : 
    2850         [ +  + ]:        2677 :                 if (attno != InvalidAttrNumber)
    2851                 :             :                 {
    2852                 :             :                         /* Single column partition key is stored as a Var node. */
    2853         [ +  - ]:        2520 :                         Assert(attno > 0);
    2854                 :             : 
    2855                 :        5040 :                         partexpr = (Expr *) makeVar(varno, attno,
    2856                 :        2520 :                                                                                 partkey->parttypid[cnt],
    2857                 :        2520 :                                                                                 partkey->parttypmod[cnt],
    2858                 :        2520 :                                                                                 partkey->parttypcoll[cnt], 0);
    2859                 :        2520 :                 }
    2860                 :             :                 else
    2861                 :             :                 {
    2862         [ +  - ]:         157 :                         if (lc == NULL)
    2863   [ #  #  #  # ]:           0 :                                 elog(ERROR, "wrong number of partition key expressions");
    2864                 :             : 
    2865                 :             :                         /* Re-stamp the expression with given varno. */
    2866                 :         157 :                         partexpr = (Expr *) copyObject(lfirst(lc));
    2867                 :         157 :                         ChangeVarNodes((Node *) partexpr, 1, varno, 0);
    2868                 :         157 :                         lc = lnext(partkey->partexprs, lc);
    2869                 :             :                 }
    2870                 :             : 
    2871                 :             :                 /* Base relations have a single expression per key. */
    2872                 :        2677 :                 partexprs[cnt] = list_make1(partexpr);
    2873                 :        2677 :         }
    2874                 :             : 
    2875                 :        2356 :         rel->partexprs = partexprs;
    2876                 :             : 
    2877                 :             :         /*
    2878                 :             :          * A base relation does not have nullable partition key expressions, since
    2879                 :             :          * no outer join is involved.  We still allocate an array of empty
    2880                 :             :          * expression lists to keep partition key expression handling code simple.
    2881                 :             :          * See build_joinrel_partition_info() and match_expr_to_partition_keys().
    2882                 :             :          */
    2883                 :        2356 :         rel->nullable_partexprs = palloc0_array(List *, partnatts);
    2884                 :        2356 : }
    2885                 :             : 
    2886                 :             : /*
    2887                 :             :  * set_baserel_partition_constraint
    2888                 :             :  *
    2889                 :             :  * Builds the partition constraint for the given base relation and sets it
    2890                 :             :  * in the given RelOptInfo.  All Var nodes are restamped with the relid of the
    2891                 :             :  * given relation.
    2892                 :             :  */
    2893                 :             : static void
    2894                 :        2358 : set_baserel_partition_constraint(Relation relation, RelOptInfo *rel)
    2895                 :             : {
    2896                 :        2358 :         List       *partconstr;
    2897                 :             : 
    2898         [ -  + ]:        2358 :         if (rel->partition_qual)     /* already done */
    2899                 :           0 :                 return;
    2900                 :             : 
    2901                 :             :         /*
    2902                 :             :          * Run the partition quals through const-simplification similar to check
    2903                 :             :          * constraints.  We skip canonicalize_qual, though, because partition
    2904                 :             :          * quals should be in canonical form already; also, since the qual is in
    2905                 :             :          * implicit-AND format, we'd have to explicitly convert it to explicit-AND
    2906                 :             :          * format and back again.
    2907                 :             :          */
    2908                 :        2358 :         partconstr = RelationGetPartitionQual(relation);
    2909         [ +  + ]:        2358 :         if (partconstr)
    2910                 :             :         {
    2911                 :         616 :                 partconstr = (List *) expression_planner((Expr *) partconstr);
    2912         [ +  + ]:         616 :                 if (rel->relid != 1)
    2913                 :         601 :                         ChangeVarNodes((Node *) partconstr, 1, rel->relid, 0);
    2914                 :         616 :                 rel->partition_qual = partconstr;
    2915                 :         616 :         }
    2916         [ -  + ]:        2358 : }
        

Generated by: LCOV version 2.3.2-1