LCOV - differential code coverage report
Current view: top level - src/backend/optimizer/util - plancat.c (source / functions) Coverage Total Hit UNC LBC UIC GBC GIC GNC CBC EUB ECB DUB DCB
Current: Differential Code Coverage HEAD vs 15 Lines: 92.8 % 806 748 20 12 26 6 483 51 208 37 492 15 36
Current Date: 2023-04-08 17:13:01 Functions: 100.0 % 26 26 26 26
Baseline: 15 Line coverage date bins:
Baseline Date: 2023-04-08 15:09:40 [..60] days: 100.0 % 1 1 1
Legend: Lines: hit not hit (60,120] days: 76.4 % 89 68 20 1 12 50 6 6 9
(120,180] days: 75.0 % 4 3 1 2 1 1 3
(240..) days: 94.9 % 712 676 11 25 6 469 201 29 470
Function coverage date bins:
(60,120] days: 33.3 % 3 1 1 2
(240..) days: 52.1 % 48 25 25 23

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

Generated by: LCOV version v1.16-55-g56c0a2a