LCOV - code coverage report
Current view: top level - src/backend/optimizer/util - appendinfo.c (source / functions) Coverage Total Hit
Test: Code coverage Lines: 88.4 % 481 425
Test Date: 2026-01-26 10:56:24 Functions: 92.9 % 14 13
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 54.5 % 323 176

             Branch data     Line data    Source code
       1                 :             : /*-------------------------------------------------------------------------
       2                 :             :  *
       3                 :             :  * appendinfo.c
       4                 :             :  *        Routines for mapping between append parent(s) and children
       5                 :             :  *
       6                 :             :  * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
       7                 :             :  * Portions Copyright (c) 1994, Regents of the University of California
       8                 :             :  *
       9                 :             :  *
      10                 :             :  * IDENTIFICATION
      11                 :             :  *        src/backend/optimizer/util/appendinfo.c
      12                 :             :  *
      13                 :             :  *-------------------------------------------------------------------------
      14                 :             :  */
      15                 :             : #include "postgres.h"
      16                 :             : 
      17                 :             : #include "access/htup_details.h"
      18                 :             : #include "access/table.h"
      19                 :             : #include "foreign/fdwapi.h"
      20                 :             : #include "nodes/makefuncs.h"
      21                 :             : #include "nodes/nodeFuncs.h"
      22                 :             : #include "optimizer/appendinfo.h"
      23                 :             : #include "optimizer/pathnode.h"
      24                 :             : #include "optimizer/planmain.h"
      25                 :             : #include "parser/parsetree.h"
      26                 :             : #include "utils/lsyscache.h"
      27                 :             : #include "utils/rel.h"
      28                 :             : #include "utils/syscache.h"
      29                 :             : 
      30                 :             : 
      31                 :             : typedef struct
      32                 :             : {
      33                 :             :         PlannerInfo *root;
      34                 :             :         int                     nappinfos;
      35                 :             :         AppendRelInfo **appinfos;
      36                 :             : } adjust_appendrel_attrs_context;
      37                 :             : 
      38                 :             : static void make_inh_translation_list(Relation oldrelation,
      39                 :             :                                                                           Relation newrelation,
      40                 :             :                                                                           Index newvarno,
      41                 :             :                                                                           AppendRelInfo *appinfo);
      42                 :             : static Node *adjust_appendrel_attrs_mutator(Node *node,
      43                 :             :                                                                                         adjust_appendrel_attrs_context *context);
      44                 :             : 
      45                 :             : 
      46                 :             : /*
      47                 :             :  * make_append_rel_info
      48                 :             :  *        Build an AppendRelInfo for the parent-child pair
      49                 :             :  */
      50                 :             : AppendRelInfo *
      51                 :        6380 : make_append_rel_info(Relation parentrel, Relation childrel,
      52                 :             :                                          Index parentRTindex, Index childRTindex)
      53                 :             : {
      54                 :        6380 :         AppendRelInfo *appinfo = makeNode(AppendRelInfo);
      55                 :             : 
      56                 :        6380 :         appinfo->parent_relid = parentRTindex;
      57                 :        6380 :         appinfo->child_relid = childRTindex;
      58                 :        6380 :         appinfo->parent_reltype = parentrel->rd_rel->reltype;
      59                 :        6380 :         appinfo->child_reltype = childrel->rd_rel->reltype;
      60                 :        6380 :         make_inh_translation_list(parentrel, childrel, childRTindex, appinfo);
      61                 :        6380 :         appinfo->parent_reloid = RelationGetRelid(parentrel);
      62                 :             : 
      63                 :       12760 :         return appinfo;
      64                 :        6380 : }
      65                 :             : 
      66                 :             : /*
      67                 :             :  * make_inh_translation_list
      68                 :             :  *        Build the list of translations from parent Vars to child Vars for
      69                 :             :  *        an inheritance child, as well as a reverse-translation array.
      70                 :             :  *
      71                 :             :  * The reverse-translation array has an entry for each child relation
      72                 :             :  * column, which is either the 1-based index of the corresponding parent
      73                 :             :  * column, or 0 if there's no match (that happens for dropped child columns,
      74                 :             :  * as well as child columns beyond those of the parent, which are allowed in
      75                 :             :  * traditional inheritance though not partitioning).
      76                 :             :  *
      77                 :             :  * For paranoia's sake, we match type/collation as well as attribute name.
      78                 :             :  */
      79                 :             : static void
      80                 :        6380 : make_inh_translation_list(Relation oldrelation, Relation newrelation,
      81                 :             :                                                   Index newvarno,
      82                 :             :                                                   AppendRelInfo *appinfo)
      83                 :             : {
      84                 :        6380 :         List       *vars = NIL;
      85                 :        6380 :         AttrNumber *pcolnos;
      86                 :        6380 :         TupleDesc       old_tupdesc = RelationGetDescr(oldrelation);
      87                 :        6380 :         TupleDesc       new_tupdesc = RelationGetDescr(newrelation);
      88                 :        6380 :         Oid                     new_relid = RelationGetRelid(newrelation);
      89                 :        6380 :         int                     oldnatts = old_tupdesc->natts;
      90                 :        6380 :         int                     newnatts = new_tupdesc->natts;
      91                 :        6380 :         int                     old_attno;
      92                 :        6380 :         int                     new_attno = 0;
      93                 :             : 
      94                 :             :         /* Initialize reverse-translation array with all entries zero */
      95                 :        6380 :         appinfo->num_child_cols = newnatts;
      96                 :        6380 :         appinfo->parent_colnos = pcolnos =
      97                 :        6380 :                 (AttrNumber *) palloc0(newnatts * sizeof(AttrNumber));
      98                 :             : 
      99         [ +  + ]:       23340 :         for (old_attno = 0; old_attno < oldnatts; old_attno++)
     100                 :             :         {
     101                 :       16960 :                 Form_pg_attribute att;
     102                 :       16960 :                 char       *attname;
     103                 :       16960 :                 Oid                     atttypid;
     104                 :       16960 :                 int32           atttypmod;
     105                 :       16960 :                 Oid                     attcollation;
     106                 :             : 
     107                 :       16960 :                 att = TupleDescAttr(old_tupdesc, old_attno);
     108         [ +  + ]:       16960 :                 if (att->attisdropped)
     109                 :             :                 {
     110                 :             :                         /* Just put NULL into this list entry */
     111                 :         521 :                         vars = lappend(vars, NULL);
     112                 :         521 :                         continue;
     113                 :             :                 }
     114                 :       16439 :                 attname = NameStr(att->attname);
     115                 :       16439 :                 atttypid = att->atttypid;
     116                 :       16439 :                 atttypmod = att->atttypmod;
     117                 :       16439 :                 attcollation = att->attcollation;
     118                 :             : 
     119                 :             :                 /*
     120                 :             :                  * When we are generating the "translation list" for the parent table
     121                 :             :                  * of an inheritance set, no need to search for matches.
     122                 :             :                  */
     123         [ +  + ]:       16439 :                 if (oldrelation == newrelation)
     124                 :             :                 {
     125                 :        2154 :                         vars = lappend(vars, makeVar(newvarno,
     126                 :        1077 :                                                                                  (AttrNumber) (old_attno + 1),
     127                 :        1077 :                                                                                  atttypid,
     128                 :        1077 :                                                                                  atttypmod,
     129                 :        1077 :                                                                                  attcollation,
     130                 :             :                                                                                  0));
     131                 :        1077 :                         pcolnos[old_attno] = old_attno + 1;
     132                 :        1077 :                         continue;
     133                 :             :                 }
     134                 :             : 
     135                 :             :                 /*
     136                 :             :                  * Otherwise we have to search for the matching column by name.
     137                 :             :                  * There's no guarantee it'll have the same column position, because
     138                 :             :                  * of cases like ALTER TABLE ADD COLUMN and multiple inheritance.
     139                 :             :                  * However, in simple cases, the relative order of columns is mostly
     140                 :             :                  * the same in both relations, so try the column of newrelation that
     141                 :             :                  * follows immediately after the one that we just found, and if that
     142                 :             :                  * fails, let syscache handle it.
     143                 :             :                  */
     144         [ +  + ]:       15362 :                 if (new_attno >= newnatts ||
     145   [ +  +  +  + ]:       15013 :                         (att = TupleDescAttr(new_tupdesc, new_attno))->attisdropped ||
     146                 :       14863 :                         strcmp(attname, NameStr(att->attname)) != 0)
     147                 :             :                 {
     148                 :        1333 :                         HeapTuple       newtup;
     149                 :             : 
     150                 :        1333 :                         newtup = SearchSysCacheAttName(new_relid, attname);
     151         [ +  - ]:        1333 :                         if (!HeapTupleIsValid(newtup))
     152   [ #  #  #  # ]:           0 :                                 elog(ERROR, "could not find inherited attribute \"%s\" of relation \"%s\"",
     153                 :             :                                          attname, RelationGetRelationName(newrelation));
     154                 :        1333 :                         new_attno = ((Form_pg_attribute) GETSTRUCT(newtup))->attnum - 1;
     155         [ +  - ]:        1333 :                         Assert(new_attno >= 0 && new_attno < newnatts);
     156                 :        1333 :                         ReleaseSysCache(newtup);
     157                 :             : 
     158                 :        1333 :                         att = TupleDescAttr(new_tupdesc, new_attno);
     159                 :        1333 :                 }
     160                 :             : 
     161                 :             :                 /* Found it, check type and collation match */
     162         [ +  - ]:       15362 :                 if (atttypid != att->atttypid || atttypmod != att->atttypmod)
     163   [ #  #  #  # ]:           0 :                         ereport(ERROR,
     164                 :             :                                         (errcode(ERRCODE_INVALID_COLUMN_DEFINITION),
     165                 :             :                                          errmsg("attribute \"%s\" of relation \"%s\" does not match parent's type",
     166                 :             :                                                         attname, RelationGetRelationName(newrelation))));
     167         [ +  - ]:       15362 :                 if (attcollation != att->attcollation)
     168   [ #  #  #  # ]:           0 :                         ereport(ERROR,
     169                 :             :                                         (errcode(ERRCODE_INVALID_COLUMN_DEFINITION),
     170                 :             :                                          errmsg("attribute \"%s\" of relation \"%s\" does not match parent's collation",
     171                 :             :                                                         attname, RelationGetRelationName(newrelation))));
     172                 :             : 
     173                 :       30724 :                 vars = lappend(vars, makeVar(newvarno,
     174                 :       15362 :                                                                          (AttrNumber) (new_attno + 1),
     175                 :       15362 :                                                                          atttypid,
     176                 :       15362 :                                                                          atttypmod,
     177                 :       15362 :                                                                          attcollation,
     178                 :             :                                                                          0));
     179                 :       15362 :                 pcolnos[new_attno] = old_attno + 1;
     180                 :       15362 :                 new_attno++;
     181      [ -  +  + ]:       16960 :         }
     182                 :             : 
     183                 :        6380 :         appinfo->translated_vars = vars;
     184                 :        6380 : }
     185                 :             : 
     186                 :             : /*
     187                 :             :  * adjust_appendrel_attrs
     188                 :             :  *        Copy the specified query or expression and translate Vars referring to a
     189                 :             :  *        parent rel to refer to the corresponding child rel instead.  We also
     190                 :             :  *        update rtindexes appearing outside Vars, such as resultRelation and
     191                 :             :  *        jointree relids.
     192                 :             :  *
     193                 :             :  * Note: this is only applied after conversion of sublinks to subplans,
     194                 :             :  * so we don't need to cope with recursion into sub-queries.
     195                 :             :  *
     196                 :             :  * Note: this is not hugely different from what pullup_replace_vars() does;
     197                 :             :  * maybe we should try to fold the two routines together.
     198                 :             :  */
     199                 :             : Node *
     200                 :       43607 : adjust_appendrel_attrs(PlannerInfo *root, Node *node, int nappinfos,
     201                 :             :                                            AppendRelInfo **appinfos)
     202                 :             : {
     203                 :       43607 :         adjust_appendrel_attrs_context context;
     204                 :             : 
     205                 :       43607 :         context.root = root;
     206                 :       43607 :         context.nappinfos = nappinfos;
     207                 :       43607 :         context.appinfos = appinfos;
     208                 :             : 
     209                 :             :         /* If there's nothing to adjust, don't call this function. */
     210         [ +  - ]:       43607 :         Assert(nappinfos >= 1 && appinfos != NULL);
     211                 :             : 
     212                 :             :         /* Should never be translating a Query tree. */
     213   [ +  +  +  - ]:       43607 :         Assert(node == NULL || !IsA(node, Query));
     214                 :             : 
     215                 :       87214 :         return adjust_appendrel_attrs_mutator(node, &context);
     216                 :       43607 : }
     217                 :             : 
     218                 :             : static Node *
     219                 :      241406 : adjust_appendrel_attrs_mutator(Node *node,
     220                 :             :                                                            adjust_appendrel_attrs_context *context)
     221                 :             : {
     222                 :      241406 :         AppendRelInfo **appinfos = context->appinfos;
     223                 :      241406 :         int                     nappinfos = context->nappinfos;
     224                 :      241406 :         int                     cnt;
     225                 :             : 
     226         [ +  + ]:      241406 :         if (node == NULL)
     227                 :       47326 :                 return NULL;
     228         [ +  + ]:      194080 :         if (IsA(node, Var))
     229                 :             :         {
     230                 :       84017 :                 Var                *var = (Var *) copyObject(node);
     231                 :       84017 :                 AppendRelInfo *appinfo = NULL;
     232                 :             : 
     233         [ -  + ]:       84017 :                 if (var->varlevelsup != 0)
     234                 :           0 :                         return (Node *) var;    /* no changes needed */
     235                 :             : 
     236                 :             :                 /*
     237                 :             :                  * You might think we need to adjust var->varnullingrels, but that
     238                 :             :                  * shouldn't need any changes.  It will contain outer-join relids,
     239                 :             :                  * while the transformation we are making affects only baserels.
     240                 :             :                  * Below, we just propagate var->varnullingrels into the translated
     241                 :             :                  * Var.
     242                 :             :                  *
     243                 :             :                  * If var->varnullingrels isn't empty, and the translation wouldn't be
     244                 :             :                  * a Var, we have to fail.  One could imagine wrapping the translated
     245                 :             :                  * expression in a PlaceHolderVar, but that won't work because this is
     246                 :             :                  * typically used after freezing placeholders.  Fortunately, the case
     247                 :             :                  * appears unreachable at the moment.  We can see nonempty
     248                 :             :                  * var->varnullingrels here, but only in cases involving partitionwise
     249                 :             :                  * joining, and in such cases the translations will always be Vars.
     250                 :             :                  * (Non-Var translations occur only for appendrels made by flattening
     251                 :             :                  * UNION ALL subqueries.)  Should we need to make this work in future,
     252                 :             :                  * a possible fix is to mandate that prepjointree.c create PHVs for
     253                 :             :                  * all non-Var outputs of such subqueries, and then we could look up
     254                 :             :                  * the pre-existing PHV here.  Or perhaps just wrap the translations
     255                 :             :                  * that way to begin with?
     256                 :             :                  *
     257                 :             :                  * If var->varreturningtype is not VAR_RETURNING_DEFAULT, then that
     258                 :             :                  * also needs to be copied to the translated Var.  That too would fail
     259                 :             :                  * if the translation wasn't a Var, but that should never happen since
     260                 :             :                  * a non-default var->varreturningtype is only used for Vars referring
     261                 :             :                  * to the result relation, which should never be a flattened UNION ALL
     262                 :             :                  * subquery.
     263                 :             :                  */
     264                 :             : 
     265         [ +  + ]:      107170 :                 for (cnt = 0; cnt < nappinfos; cnt++)
     266                 :             :                 {
     267         [ +  + ]:      100899 :                         if (var->varno == appinfos[cnt]->parent_relid)
     268                 :             :                         {
     269                 :       77746 :                                 appinfo = appinfos[cnt];
     270                 :       77746 :                                 break;
     271                 :             :                         }
     272                 :       23153 :                 }
     273                 :             : 
     274         [ +  + ]:       84017 :                 if (appinfo)
     275                 :             :                 {
     276                 :       77746 :                         var->varno = appinfo->child_relid;
     277                 :             :                         /* it's now a generated Var, so drop any syntactic labeling */
     278                 :       77746 :                         var->varnosyn = 0;
     279                 :       77746 :                         var->varattnosyn = 0;
     280         [ +  + ]:       77746 :                         if (var->varattno > 0)
     281                 :             :                         {
     282                 :       74682 :                                 Node       *newnode;
     283                 :             : 
     284         [ +  - ]:       74682 :                                 if (var->varattno > list_length(appinfo->translated_vars))
     285   [ #  #  #  # ]:           0 :                                         elog(ERROR, "attribute %d of relation \"%s\" does not exist",
     286                 :             :                                                  var->varattno, get_rel_name(appinfo->parent_reloid));
     287                 :       74682 :                                 newnode = copyObject(list_nth(appinfo->translated_vars,
     288                 :             :                                                                                           var->varattno - 1));
     289         [ +  - ]:       74682 :                                 if (newnode == NULL)
     290   [ #  #  #  # ]:           0 :                                         elog(ERROR, "attribute %d of relation \"%s\" does not exist",
     291                 :             :                                                  var->varattno, get_rel_name(appinfo->parent_reloid));
     292         [ +  + ]:       74682 :                                 if (IsA(newnode, Var))
     293                 :             :                                 {
     294                 :       73210 :                                         ((Var *) newnode)->varreturningtype = var->varreturningtype;
     295                 :       73210 :                                         ((Var *) newnode)->varnullingrels = var->varnullingrels;
     296                 :       73210 :                                 }
     297                 :             :                                 else
     298                 :             :                                 {
     299         [ +  - ]:        1472 :                                         if (var->varreturningtype != VAR_RETURNING_DEFAULT)
     300   [ #  #  #  # ]:           0 :                                                 elog(ERROR, "failed to apply returningtype to a non-Var");
     301         [ +  - ]:        1472 :                                         if (var->varnullingrels != NULL)
     302   [ #  #  #  # ]:           0 :                                                 elog(ERROR, "failed to apply nullingrels to a non-Var");
     303                 :             :                                 }
     304                 :       74682 :                                 return newnode;
     305                 :       74682 :                         }
     306         [ +  + ]:        3064 :                         else if (var->varattno == 0)
     307                 :             :                         {
     308                 :             :                                 /*
     309                 :             :                                  * Whole-row Var: if we are dealing with named rowtypes, we
     310                 :             :                                  * can use a whole-row Var for the child table plus a coercion
     311                 :             :                                  * step to convert the tuple layout to the parent's rowtype.
     312                 :             :                                  * Otherwise we have to generate a RowExpr.
     313                 :             :                                  */
     314         [ +  + ]:         147 :                                 if (OidIsValid(appinfo->child_reltype))
     315                 :             :                                 {
     316         [ +  - ]:         139 :                                         Assert(var->vartype == appinfo->parent_reltype);
     317         [ +  + ]:         139 :                                         if (appinfo->parent_reltype != appinfo->child_reltype)
     318                 :             :                                         {
     319                 :         114 :                                                 ConvertRowtypeExpr *r = makeNode(ConvertRowtypeExpr);
     320                 :             : 
     321                 :         114 :                                                 r->arg = (Expr *) var;
     322                 :         114 :                                                 r->resulttype = appinfo->parent_reltype;
     323                 :         114 :                                                 r->convertformat = COERCE_IMPLICIT_CAST;
     324                 :         114 :                                                 r->location = -1;
     325                 :             :                                                 /* Make sure the Var node has the right type ID, too */
     326                 :         114 :                                                 var->vartype = appinfo->child_reltype;
     327                 :         114 :                                                 return (Node *) r;
     328                 :         114 :                                         }
     329                 :          25 :                                 }
     330                 :             :                                 else
     331                 :             :                                 {
     332                 :             :                                         /*
     333                 :             :                                          * Build a RowExpr containing the translated variables.
     334                 :             :                                          *
     335                 :             :                                          * In practice var->vartype will always be RECORDOID here,
     336                 :             :                                          * so we need to come up with some suitable column names.
     337                 :             :                                          * We use the parent RTE's column names.
     338                 :             :                                          *
     339                 :             :                                          * Note: we can't get here for inheritance cases, so there
     340                 :             :                                          * is no need to worry that translated_vars might contain
     341                 :             :                                          * some dummy NULLs.
     342                 :             :                                          */
     343                 :           8 :                                         RowExpr    *rowexpr;
     344                 :           8 :                                         List       *fields;
     345                 :           8 :                                         RangeTblEntry *rte;
     346                 :             : 
     347                 :           8 :                                         rte = rt_fetch(appinfo->parent_relid,
     348                 :             :                                                                    context->root->parse->rtable);
     349                 :           8 :                                         fields = copyObject(appinfo->translated_vars);
     350                 :           8 :                                         rowexpr = makeNode(RowExpr);
     351                 :           8 :                                         rowexpr->args = fields;
     352                 :           8 :                                         rowexpr->row_typeid = var->vartype;
     353                 :           8 :                                         rowexpr->row_format = COERCE_IMPLICIT_CAST;
     354                 :           8 :                                         rowexpr->colnames = copyObject(rte->eref->colnames);
     355                 :           8 :                                         rowexpr->location = -1;
     356                 :             : 
     357         [ +  - ]:           8 :                                         if (var->varreturningtype != VAR_RETURNING_DEFAULT)
     358   [ #  #  #  # ]:           0 :                                                 elog(ERROR, "failed to apply returningtype to a non-Var");
     359         [ +  - ]:           8 :                                         if (var->varnullingrels != NULL)
     360   [ #  #  #  # ]:           0 :                                                 elog(ERROR, "failed to apply nullingrels to a non-Var");
     361                 :             : 
     362                 :           8 :                                         return (Node *) rowexpr;
     363                 :           8 :                                 }
     364                 :          25 :                         }
     365                 :             :                         /* system attributes don't need any other translation */
     366                 :        2942 :                 }
     367         [ +  + ]:        6271 :                 else if (var->varno == ROWID_VAR)
     368                 :             :                 {
     369                 :             :                         /*
     370                 :             :                          * If it's a ROWID_VAR placeholder, see if we've reached a leaf
     371                 :             :                          * target rel, for which we can translate the Var to a specific
     372                 :             :                          * instantiation.  We should never be asked to translate to a set
     373                 :             :                          * of relids containing more than one leaf target rel, so the
     374                 :             :                          * answer will be unique.  If we're still considering non-leaf
     375                 :             :                          * inheritance levels, return the ROWID_VAR Var as-is.
     376                 :             :                          */
     377                 :        2636 :                         Relids          leaf_result_relids = context->root->leaf_result_relids;
     378                 :        2636 :                         Index           leaf_relid = 0;
     379                 :             : 
     380         [ +  + ]:        5272 :                         for (cnt = 0; cnt < nappinfos; cnt++)
     381                 :             :                         {
     382   [ +  +  +  + ]:        5272 :                                 if (bms_is_member(appinfos[cnt]->child_relid,
     383                 :        2636 :                                                                   leaf_result_relids))
     384                 :             :                                 {
     385         [ +  - ]:        2278 :                                         if (leaf_relid)
     386   [ #  #  #  # ]:           0 :                                                 elog(ERROR, "cannot translate to multiple leaf relids");
     387                 :        2278 :                                         leaf_relid = appinfos[cnt]->child_relid;
     388                 :        2278 :                                 }
     389                 :        2636 :                         }
     390                 :             : 
     391         [ +  + ]:        2636 :                         if (leaf_relid)
     392                 :             :                         {
     393                 :        4556 :                                 RowIdentityVarInfo *ridinfo = (RowIdentityVarInfo *)
     394                 :        2278 :                                         list_nth(context->root->row_identity_vars, var->varattno - 1);
     395                 :             : 
     396         [ +  - ]:        2278 :                                 if (bms_is_member(leaf_relid, ridinfo->rowidrels))
     397                 :             :                                 {
     398                 :             :                                         /* Substitute the Var given in the RowIdentityVarInfo */
     399                 :        2278 :                                         var = copyObject(ridinfo->rowidvar);
     400                 :             :                                         /* ... but use the correct relid */
     401                 :        2278 :                                         var->varno = leaf_relid;
     402                 :             :                                         /* identity vars shouldn't have nulling rels */
     403         [ +  - ]:        2278 :                                         Assert(var->varnullingrels == NULL);
     404                 :             :                                         /* varnosyn in the RowIdentityVarInfo is probably wrong */
     405                 :        2278 :                                         var->varnosyn = 0;
     406                 :        2278 :                                         var->varattnosyn = 0;
     407                 :        2278 :                                 }
     408                 :             :                                 else
     409                 :             :                                 {
     410                 :             :                                         /*
     411                 :             :                                          * This leaf rel can't return the desired value, so
     412                 :             :                                          * substitute a NULL of the correct type.
     413                 :             :                                          */
     414                 :           0 :                                         return (Node *) makeNullConst(var->vartype,
     415                 :           0 :                                                                                                   var->vartypmod,
     416                 :           0 :                                                                                                   var->varcollid);
     417                 :             :                                 }
     418         [ -  + ]:        2278 :                         }
     419         [ -  + ]:        2636 :                 }
     420                 :        9213 :                 return (Node *) var;
     421                 :       84017 :         }
     422         [ +  + ]:      110063 :         if (IsA(node, CurrentOfExpr))
     423                 :             :         {
     424                 :          30 :                 CurrentOfExpr *cexpr = (CurrentOfExpr *) copyObject(node);
     425                 :             : 
     426         [ -  + ]:          30 :                 for (cnt = 0; cnt < nappinfos; cnt++)
     427                 :             :                 {
     428                 :          30 :                         AppendRelInfo *appinfo = appinfos[cnt];
     429                 :             : 
     430         [ +  - ]:          30 :                         if (cexpr->cvarno == appinfo->parent_relid)
     431                 :             :                         {
     432                 :          30 :                                 cexpr->cvarno = appinfo->child_relid;
     433                 :          30 :                                 break;
     434                 :             :                         }
     435      [ -  +  - ]:          30 :                 }
     436                 :          30 :                 return (Node *) cexpr;
     437                 :          30 :         }
     438         [ +  + ]:      110033 :         if (IsA(node, PlaceHolderVar))
     439                 :             :         {
     440                 :             :                 /* Copy the PlaceHolderVar node with correct mutation of subnodes */
     441                 :         440 :                 PlaceHolderVar *phv;
     442                 :             : 
     443                 :         440 :                 phv = (PlaceHolderVar *) expression_tree_mutator(node,
     444                 :             :                                                                                                                  adjust_appendrel_attrs_mutator,
     445                 :             :                                                                                                                  context);
     446                 :             :                 /* now fix PlaceHolderVar's relid sets */
     447         [ -  + ]:         440 :                 if (phv->phlevelsup == 0)
     448                 :             :                 {
     449                 :         880 :                         phv->phrels = adjust_child_relids(phv->phrels,
     450                 :         440 :                                                                                           nappinfos, appinfos);
     451                 :             :                         /* as above, we needn't touch phnullingrels */
     452                 :         440 :                 }
     453                 :         440 :                 return (Node *) phv;
     454                 :         440 :         }
     455                 :             :         /* Shouldn't need to handle planner auxiliary nodes here */
     456         [ +  - ]:      109593 :         Assert(!IsA(node, SpecialJoinInfo));
     457         [ +  - ]:      109593 :         Assert(!IsA(node, AppendRelInfo));
     458         [ +  - ]:      109593 :         Assert(!IsA(node, PlaceHolderInfo));
     459         [ +  - ]:      109593 :         Assert(!IsA(node, MinMaxAggInfo));
     460                 :             : 
     461                 :             :         /*
     462                 :             :          * We have to process RestrictInfo nodes specially.  (Note: although
     463                 :             :          * set_append_rel_pathlist will hide RestrictInfos in the parent's
     464                 :             :          * baserestrictinfo list from us, it doesn't hide those in joininfo.)
     465                 :             :          */
     466         [ +  + ]:      109593 :         if (IsA(node, RestrictInfo))
     467                 :             :         {
     468                 :        6691 :                 RestrictInfo *oldinfo = (RestrictInfo *) node;
     469                 :        6691 :                 RestrictInfo *newinfo = makeNode(RestrictInfo);
     470                 :             : 
     471                 :             :                 /* Copy all flat-copiable fields, notably including rinfo_serial */
     472                 :        6691 :                 memcpy(newinfo, oldinfo, sizeof(RestrictInfo));
     473                 :             : 
     474                 :             :                 /* Recursively fix the clause itself */
     475                 :        6691 :                 newinfo->clause = (Expr *)
     476                 :        6691 :                         adjust_appendrel_attrs_mutator((Node *) oldinfo->clause, context);
     477                 :             : 
     478                 :             :                 /* and the modified version, if an OR clause */
     479                 :        6691 :                 newinfo->orclause = (Expr *)
     480                 :        6691 :                         adjust_appendrel_attrs_mutator((Node *) oldinfo->orclause, context);
     481                 :             : 
     482                 :             :                 /* adjust relid sets too */
     483                 :       13382 :                 newinfo->clause_relids = adjust_child_relids(oldinfo->clause_relids,
     484                 :        6691 :                                                                                                          context->nappinfos,
     485                 :        6691 :                                                                                                          context->appinfos);
     486                 :       13382 :                 newinfo->required_relids = adjust_child_relids(oldinfo->required_relids,
     487                 :        6691 :                                                                                                            context->nappinfos,
     488                 :        6691 :                                                                                                            context->appinfos);
     489                 :       13382 :                 newinfo->outer_relids = adjust_child_relids(oldinfo->outer_relids,
     490                 :        6691 :                                                                                                         context->nappinfos,
     491                 :        6691 :                                                                                                         context->appinfos);
     492                 :       13382 :                 newinfo->left_relids = adjust_child_relids(oldinfo->left_relids,
     493                 :        6691 :                                                                                                    context->nappinfos,
     494                 :        6691 :                                                                                                    context->appinfos);
     495                 :       13382 :                 newinfo->right_relids = adjust_child_relids(oldinfo->right_relids,
     496                 :        6691 :                                                                                                         context->nappinfos,
     497                 :        6691 :                                                                                                         context->appinfos);
     498                 :             : 
     499                 :             :                 /*
     500                 :             :                  * Reset cached derivative fields, since these might need to have
     501                 :             :                  * different values when considering the child relation.  Note we
     502                 :             :                  * don't reset left_ec/right_ec: each child variable is implicitly
     503                 :             :                  * equivalent to its parent, so still a member of the same EC if any.
     504                 :             :                  */
     505                 :        6691 :                 newinfo->eval_cost.startup = -1;
     506                 :        6691 :                 newinfo->norm_selec = -1;
     507                 :        6691 :                 newinfo->outer_selec = -1;
     508                 :        6691 :                 newinfo->left_em = NULL;
     509                 :        6691 :                 newinfo->right_em = NULL;
     510                 :        6691 :                 newinfo->scansel_cache = NIL;
     511                 :        6691 :                 newinfo->left_bucketsize = -1;
     512                 :        6691 :                 newinfo->right_bucketsize = -1;
     513                 :        6691 :                 newinfo->left_mcvfreq = -1;
     514                 :        6691 :                 newinfo->right_mcvfreq = -1;
     515                 :             : 
     516                 :        6691 :                 return (Node *) newinfo;
     517                 :        6691 :         }
     518                 :             : 
     519                 :             :         /*
     520                 :             :          * We have to process RelAggInfo nodes specially.
     521                 :             :          */
     522         [ +  + ]:      102902 :         if (IsA(node, RelAggInfo))
     523                 :             :         {
     524                 :        3100 :                 RelAggInfo *oldinfo = (RelAggInfo *) node;
     525                 :        3100 :                 RelAggInfo *newinfo = makeNode(RelAggInfo);
     526                 :             : 
     527                 :        3100 :                 newinfo->target = (PathTarget *)
     528                 :        6200 :                         adjust_appendrel_attrs_mutator((Node *) oldinfo->target,
     529                 :        3100 :                                                                                    context);
     530                 :             : 
     531                 :        3100 :                 newinfo->agg_input = (PathTarget *)
     532                 :        6200 :                         adjust_appendrel_attrs_mutator((Node *) oldinfo->agg_input,
     533                 :        3100 :                                                                                    context);
     534                 :             : 
     535                 :        3100 :                 newinfo->group_clauses = oldinfo->group_clauses;
     536                 :             : 
     537                 :        3100 :                 newinfo->group_exprs = (List *)
     538                 :        6200 :                         adjust_appendrel_attrs_mutator((Node *) oldinfo->group_exprs,
     539                 :        3100 :                                                                                    context);
     540                 :             : 
     541                 :        3100 :                 return (Node *) newinfo;
     542                 :        3100 :         }
     543                 :             : 
     544                 :             :         /*
     545                 :             :          * We have to process PathTarget nodes specially.
     546                 :             :          */
     547         [ +  + ]:       99802 :         if (IsA(node, PathTarget))
     548                 :             :         {
     549                 :        6200 :                 PathTarget *oldtarget = (PathTarget *) node;
     550                 :        6200 :                 PathTarget *newtarget = makeNode(PathTarget);
     551                 :             : 
     552                 :             :                 /* Copy all flat-copiable fields */
     553                 :        6200 :                 memcpy(newtarget, oldtarget, sizeof(PathTarget));
     554                 :             : 
     555                 :        6200 :                 newtarget->exprs = (List *)
     556                 :       12400 :                         adjust_appendrel_attrs_mutator((Node *) oldtarget->exprs,
     557                 :        6200 :                                                                                    context);
     558                 :             : 
     559         [ -  + ]:        6200 :                 if (oldtarget->sortgrouprefs)
     560                 :             :                 {
     561                 :        6200 :                         Size            nbytes = list_length(oldtarget->exprs) * sizeof(Index);
     562                 :             : 
     563                 :        6200 :                         newtarget->sortgrouprefs = (Index *) palloc(nbytes);
     564                 :        6200 :                         memcpy(newtarget->sortgrouprefs, oldtarget->sortgrouprefs, nbytes);
     565                 :        6200 :                 }
     566                 :             : 
     567                 :        6200 :                 return (Node *) newtarget;
     568                 :        6200 :         }
     569                 :             : 
     570                 :             :         /*
     571                 :             :          * NOTE: we do not need to recurse into sublinks, because they should
     572                 :             :          * already have been converted to subplans before we see them.
     573                 :             :          */
     574         [ +  - ]:       93602 :         Assert(!IsA(node, SubLink));
     575         [ +  - ]:       93602 :         Assert(!IsA(node, Query));
     576                 :             :         /* We should never see these Query substructures, either. */
     577         [ +  - ]:       93602 :         Assert(!IsA(node, RangeTblRef));
     578         [ +  - ]:       93602 :         Assert(!IsA(node, JoinExpr));
     579                 :             : 
     580                 :       93602 :         return expression_tree_mutator(node, adjust_appendrel_attrs_mutator, context);
     581                 :      241406 : }
     582                 :             : 
     583                 :             : /*
     584                 :             :  * adjust_appendrel_attrs_multilevel
     585                 :             :  *        Apply Var translations from an appendrel parent down to a child.
     586                 :             :  *
     587                 :             :  * Replace Vars in the "node" expression that reference "parentrel" with
     588                 :             :  * the appropriate Vars for "childrel".  childrel can be more than one
     589                 :             :  * inheritance level removed from parentrel.
     590                 :             :  */
     591                 :             : Node *
     592                 :        7852 : adjust_appendrel_attrs_multilevel(PlannerInfo *root, Node *node,
     593                 :             :                                                                   RelOptInfo *childrel,
     594                 :             :                                                                   RelOptInfo *parentrel)
     595                 :             : {
     596                 :        7852 :         AppendRelInfo **appinfos;
     597                 :        7852 :         int                     nappinfos;
     598                 :             : 
     599                 :             :         /* Recurse if immediate parent is not the top parent. */
     600         [ +  + ]:        7852 :         if (childrel->parent != parentrel)
     601                 :             :         {
     602         [ +  - ]:        2711 :                 if (childrel->parent)
     603                 :        5422 :                         node = adjust_appendrel_attrs_multilevel(root, node,
     604                 :        2711 :                                                                                                          childrel->parent,
     605                 :        2711 :                                                                                                          parentrel);
     606                 :             :                 else
     607   [ #  #  #  # ]:           0 :                         elog(ERROR, "childrel is not a child of parentrel");
     608                 :        2711 :         }
     609                 :             : 
     610                 :             :         /* Now translate for this child. */
     611                 :        7852 :         appinfos = find_appinfos_by_relids(root, childrel->relids, &nappinfos);
     612                 :             : 
     613                 :        7852 :         node = adjust_appendrel_attrs(root, node, nappinfos, appinfos);
     614                 :             : 
     615                 :        7852 :         pfree(appinfos);
     616                 :             : 
     617                 :       15704 :         return node;
     618                 :        7852 : }
     619                 :             : 
     620                 :             : /*
     621                 :             :  * Substitute child relids for parent relids in a Relid set.  The array of
     622                 :             :  * appinfos specifies the substitutions to be performed.
     623                 :             :  */
     624                 :             : Relids
     625                 :       42660 : adjust_child_relids(Relids relids, int nappinfos, AppendRelInfo **appinfos)
     626                 :             : {
     627                 :       42660 :         Bitmapset  *result = NULL;
     628                 :       42660 :         int                     cnt;
     629                 :             : 
     630         [ +  + ]:      117787 :         for (cnt = 0; cnt < nappinfos; cnt++)
     631                 :             :         {
     632                 :       75127 :                 AppendRelInfo *appinfo = appinfos[cnt];
     633                 :             : 
     634                 :             :                 /* Remove parent, add child */
     635         [ +  + ]:       75127 :                 if (bms_is_member(appinfo->parent_relid, relids))
     636                 :             :                 {
     637                 :             :                         /* Make a copy if we are changing the set. */
     638         [ +  + ]:       50256 :                         if (!result)
     639                 :       34195 :                                 result = bms_copy(relids);
     640                 :             : 
     641                 :       50256 :                         result = bms_del_member(result, appinfo->parent_relid);
     642                 :       50256 :                         result = bms_add_member(result, appinfo->child_relid);
     643                 :       50256 :                 }
     644                 :       75127 :         }
     645                 :             : 
     646                 :             :         /* If we made any changes, return the modified copy. */
     647         [ +  + ]:       42660 :         if (result)
     648                 :       34195 :                 return result;
     649                 :             : 
     650                 :             :         /* Otherwise, return the original set without modification. */
     651                 :        8465 :         return relids;
     652                 :       42660 : }
     653                 :             : 
     654                 :             : /*
     655                 :             :  * Substitute child's relids for parent's relids in a Relid set.
     656                 :             :  * The childrel can be multiple inheritance levels below the parent.
     657                 :             :  */
     658                 :             : Relids
     659                 :         203 : adjust_child_relids_multilevel(PlannerInfo *root, Relids relids,
     660                 :             :                                                            RelOptInfo *childrel,
     661                 :             :                                                            RelOptInfo *parentrel)
     662                 :             : {
     663                 :         203 :         AppendRelInfo **appinfos;
     664                 :         203 :         int                     nappinfos;
     665                 :             : 
     666                 :             :         /*
     667                 :             :          * If the given relids set doesn't contain any of the parent relids, it
     668                 :             :          * will remain unchanged.
     669                 :             :          */
     670         [ -  + ]:         203 :         if (!bms_overlap(relids, parentrel->relids))
     671                 :           0 :                 return relids;
     672                 :             : 
     673                 :             :         /* Recurse if immediate parent is not the top parent. */
     674         [ +  + ]:         203 :         if (childrel->parent != parentrel)
     675                 :             :         {
     676         [ +  - ]:          24 :                 if (childrel->parent)
     677                 :          48 :                         relids = adjust_child_relids_multilevel(root, relids,
     678                 :          24 :                                                                                                         childrel->parent,
     679                 :          24 :                                                                                                         parentrel);
     680                 :             :                 else
     681   [ #  #  #  # ]:           0 :                         elog(ERROR, "childrel is not a child of parentrel");
     682                 :          24 :         }
     683                 :             : 
     684                 :             :         /* Now translate for this child. */
     685                 :         203 :         appinfos = find_appinfos_by_relids(root, childrel->relids, &nappinfos);
     686                 :             : 
     687                 :         203 :         relids = adjust_child_relids(relids, nappinfos, appinfos);
     688                 :             : 
     689                 :         203 :         pfree(appinfos);
     690                 :             : 
     691                 :         203 :         return relids;
     692                 :         203 : }
     693                 :             : 
     694                 :             : /*
     695                 :             :  * adjust_inherited_attnums
     696                 :             :  *        Translate an integer list of attribute numbers from parent to child.
     697                 :             :  */
     698                 :             : List *
     699                 :         719 : adjust_inherited_attnums(List *attnums, AppendRelInfo *context)
     700                 :             : {
     701                 :         719 :         List       *result = NIL;
     702                 :         719 :         ListCell   *lc;
     703                 :             : 
     704                 :             :         /* This should only happen for an inheritance case, not UNION ALL */
     705         [ +  - ]:         719 :         Assert(OidIsValid(context->parent_reloid));
     706                 :             : 
     707                 :             :         /* Look up each attribute in the AppendRelInfo's translated_vars list */
     708   [ +  -  +  +  :        1609 :         foreach(lc, attnums)
                   +  + ]
     709                 :             :         {
     710                 :         890 :                 AttrNumber      parentattno = lfirst_int(lc);
     711                 :         890 :                 Var                *childvar;
     712                 :             : 
     713                 :             :                 /* Look up the translation of this column: it must be a Var */
     714         [ +  - ]:         890 :                 if (parentattno <= 0 ||
     715                 :         890 :                         parentattno > list_length(context->translated_vars))
     716   [ #  #  #  # ]:           0 :                         elog(ERROR, "attribute %d of relation \"%s\" does not exist",
     717                 :             :                                  parentattno, get_rel_name(context->parent_reloid));
     718                 :         890 :                 childvar = (Var *) list_nth(context->translated_vars, parentattno - 1);
     719         [ +  - ]:         890 :                 if (childvar == NULL || !IsA(childvar, Var))
     720   [ #  #  #  # ]:           0 :                         elog(ERROR, "attribute %d of relation \"%s\" does not exist",
     721                 :             :                                  parentattno, get_rel_name(context->parent_reloid));
     722                 :             : 
     723                 :         890 :                 result = lappend_int(result, childvar->varattno);
     724                 :         890 :         }
     725                 :        1438 :         return result;
     726                 :         719 : }
     727                 :             : 
     728                 :             : /*
     729                 :             :  * adjust_inherited_attnums_multilevel
     730                 :             :  *        As above, but traverse multiple inheritance levels as needed.
     731                 :             :  */
     732                 :             : List *
     733                 :         719 : adjust_inherited_attnums_multilevel(PlannerInfo *root, List *attnums,
     734                 :             :                                                                         Index child_relid, Index top_parent_relid)
     735                 :             : {
     736                 :         719 :         AppendRelInfo *appinfo = root->append_rel_array[child_relid];
     737                 :             : 
     738         [ +  - ]:         719 :         if (!appinfo)
     739   [ #  #  #  # ]:           0 :                 elog(ERROR, "child rel %d not found in append_rel_array", child_relid);
     740                 :             : 
     741                 :             :         /* Recurse if immediate parent is not the top parent. */
     742         [ +  + ]:         719 :         if (appinfo->parent_relid != top_parent_relid)
     743                 :         268 :                 attnums = adjust_inherited_attnums_multilevel(root, attnums,
     744                 :         134 :                                                                                                           appinfo->parent_relid,
     745                 :         134 :                                                                                                           top_parent_relid);
     746                 :             : 
     747                 :             :         /* Now translate for this child */
     748                 :        1438 :         return adjust_inherited_attnums(attnums, appinfo);
     749                 :         719 : }
     750                 :             : 
     751                 :             : /*
     752                 :             :  * get_translated_update_targetlist
     753                 :             :  *        Get the processed_tlist of an UPDATE query, translated as needed to
     754                 :             :  *        match a child target relation.
     755                 :             :  *
     756                 :             :  * Optionally also return the list of target column numbers translated
     757                 :             :  * to this target relation.  (The resnos in processed_tlist MUST NOT be
     758                 :             :  * relied on for this purpose.)
     759                 :             :  */
     760                 :             : void
     761                 :           0 : get_translated_update_targetlist(PlannerInfo *root, Index relid,
     762                 :             :                                                                  List **processed_tlist, List **update_colnos)
     763                 :             : {
     764                 :             :         /* This is pretty meaningless for commands other than UPDATE. */
     765         [ #  # ]:           0 :         Assert(root->parse->commandType == CMD_UPDATE);
     766         [ #  # ]:           0 :         if (relid == root->parse->resultRelation)
     767                 :             :         {
     768                 :             :                 /*
     769                 :             :                  * Non-inheritance case, so it's easy.  The caller might be expecting
     770                 :             :                  * a tree it can scribble on, though, so copy.
     771                 :             :                  */
     772                 :           0 :                 *processed_tlist = copyObject(root->processed_tlist);
     773         [ #  # ]:           0 :                 if (update_colnos)
     774                 :           0 :                         *update_colnos = copyObject(root->update_colnos);
     775                 :           0 :         }
     776                 :             :         else
     777                 :             :         {
     778         [ #  # ]:           0 :                 Assert(bms_is_member(relid, root->all_result_relids));
     779                 :           0 :                 *processed_tlist = (List *)
     780                 :           0 :                         adjust_appendrel_attrs_multilevel(root,
     781                 :           0 :                                                                                           (Node *) root->processed_tlist,
     782                 :           0 :                                                                                           find_base_rel(root, relid),
     783                 :           0 :                                                                                           find_base_rel(root, root->parse->resultRelation));
     784         [ #  # ]:           0 :                 if (update_colnos)
     785                 :           0 :                         *update_colnos =
     786                 :           0 :                                 adjust_inherited_attnums_multilevel(root, root->update_colnos,
     787                 :           0 :                                                                                                         relid,
     788                 :           0 :                                                                                                         root->parse->resultRelation);
     789                 :             :         }
     790                 :           0 : }
     791                 :             : 
     792                 :             : /*
     793                 :             :  * find_appinfos_by_relids
     794                 :             :  *              Find AppendRelInfo structures for base relations listed in relids.
     795                 :             :  *
     796                 :             :  * The relids argument is typically a join relation's relids, which can
     797                 :             :  * include outer-join RT indexes in addition to baserels.  We silently
     798                 :             :  * ignore the outer joins.
     799                 :             :  *
     800                 :             :  * The AppendRelInfos are returned in an array, which can be pfree'd by the
     801                 :             :  * caller. *nappinfos is set to the number of entries in the array.
     802                 :             :  */
     803                 :             : AppendRelInfo **
     804                 :       16177 : find_appinfos_by_relids(PlannerInfo *root, Relids relids, int *nappinfos)
     805                 :             : {
     806                 :       16177 :         AppendRelInfo **appinfos;
     807                 :       16177 :         int                     cnt = 0;
     808                 :       16177 :         int                     i;
     809                 :             : 
     810                 :             :         /* Allocate an array that's certainly big enough */
     811                 :       16177 :         appinfos = palloc_array(AppendRelInfo *, bms_num_members(relids));
     812                 :             : 
     813                 :       16177 :         i = -1;
     814         [ +  + ]:       41169 :         while ((i = bms_next_member(relids, i)) >= 0)
     815                 :             :         {
     816                 :       24992 :                 AppendRelInfo *appinfo = root->append_rel_array[i];
     817                 :             : 
     818         [ +  + ]:       24992 :                 if (!appinfo)
     819                 :             :                 {
     820                 :             :                         /* Probably i is an OJ index, but let's check */
     821         [ +  - ]:         594 :                         if (find_base_rel_ignore_join(root, i) == NULL)
     822                 :         594 :                                 continue;
     823                 :             :                         /* It's a base rel, but we lack an append_rel_array entry */
     824   [ #  #  #  # ]:           0 :                         elog(ERROR, "child rel %d not found in append_rel_array", i);
     825                 :           0 :                 }
     826                 :             : 
     827                 :       24398 :                 appinfos[cnt++] = appinfo;
     828      [ -  +  + ]:       24992 :         }
     829                 :       16177 :         *nappinfos = cnt;
     830                 :       32354 :         return appinfos;
     831                 :       16177 : }
     832                 :             : 
     833                 :             : 
     834                 :             : /*****************************************************************************
     835                 :             :  *
     836                 :             :  *              ROW-IDENTITY VARIABLE MANAGEMENT
     837                 :             :  *
     838                 :             :  * This code lacks a good home, perhaps.  We choose to keep it here because
     839                 :             :  * adjust_appendrel_attrs_mutator() is its principal co-conspirator.  That
     840                 :             :  * function does most of what is needed to expand ROWID_VAR Vars into the
     841                 :             :  * right things.
     842                 :             :  *
     843                 :             :  *****************************************************************************/
     844                 :             : 
     845                 :             : /*
     846                 :             :  * add_row_identity_var
     847                 :             :  *        Register a row-identity column to be used in UPDATE/DELETE/MERGE.
     848                 :             :  *
     849                 :             :  * The Var must be equal(), aside from varno, to any other row-identity
     850                 :             :  * column with the same rowid_name.  Thus, for example, "wholerow"
     851                 :             :  * row identities had better use vartype == RECORDOID.
     852                 :             :  *
     853                 :             :  * rtindex is currently redundant with rowid_var->varno, but we specify
     854                 :             :  * it as a separate parameter in case this is ever generalized to support
     855                 :             :  * non-Var expressions.  (We could reasonably handle expressions over
     856                 :             :  * Vars of the specified rtindex, but for now that seems unnecessary.)
     857                 :             :  */
     858                 :             : void
     859                 :        3016 : add_row_identity_var(PlannerInfo *root, Var *orig_var,
     860                 :             :                                          Index rtindex, const char *rowid_name)
     861                 :             : {
     862                 :        3016 :         TargetEntry *tle;
     863                 :        3016 :         Var                *rowid_var;
     864                 :        3016 :         RowIdentityVarInfo *ridinfo;
     865                 :        3016 :         ListCell   *lc;
     866                 :             : 
     867                 :             :         /* For now, the argument must be just a Var of the given rtindex */
     868         [ +  - ]:        3016 :         Assert(IsA(orig_var, Var));
     869         [ +  - ]:        3016 :         Assert(orig_var->varno == rtindex);
     870         [ +  - ]:        3016 :         Assert(orig_var->varlevelsup == 0);
     871         [ +  - ]:        3016 :         Assert(orig_var->varnullingrels == NULL);
     872                 :             : 
     873                 :             :         /*
     874                 :             :          * If we're doing non-inherited UPDATE/DELETE/MERGE, there's little need
     875                 :             :          * for ROWID_VAR shenanigans.  Just shove the presented Var into the
     876                 :             :          * processed_tlist, and we're done.
     877                 :             :          */
     878         [ +  + ]:        3016 :         if (rtindex == root->parse->resultRelation)
     879                 :             :         {
     880                 :        2980 :                 tle = makeTargetEntry((Expr *) orig_var,
     881                 :        1490 :                                                           list_length(root->processed_tlist) + 1,
     882                 :        1490 :                                                           pstrdup(rowid_name),
     883                 :             :                                                           true);
     884                 :        1490 :                 root->processed_tlist = lappend(root->processed_tlist, tle);
     885                 :        1490 :                 return;
     886                 :             :         }
     887                 :             : 
     888                 :             :         /*
     889                 :             :          * Otherwise, rtindex should reference a leaf target relation that's being
     890                 :             :          * added to the query during expand_inherited_rtentry().
     891                 :             :          */
     892         [ +  - ]:        1526 :         Assert(bms_is_member(rtindex, root->leaf_result_relids));
     893         [ +  - ]:        1526 :         Assert(root->append_rel_array[rtindex] != NULL);
     894                 :             : 
     895                 :             :         /*
     896                 :             :          * We have to find a matching RowIdentityVarInfo, or make one if there is
     897                 :             :          * none.  To allow using equal() to match the vars, change the varno to
     898                 :             :          * ROWID_VAR, leaving all else alone.
     899                 :             :          */
     900                 :        1526 :         rowid_var = copyObject(orig_var);
     901                 :             :         /* This could eventually become ChangeVarNodes() */
     902                 :        1526 :         rowid_var->varno = ROWID_VAR;
     903                 :             : 
     904                 :             :         /* Look for an existing row-id column of the same name */
     905   [ +  +  +  +  :        3075 :         foreach(lc, root->row_identity_vars)
             +  +  +  + ]
     906                 :             :         {
     907                 :        1549 :                 ridinfo = (RowIdentityVarInfo *) lfirst(lc);
     908         [ +  + ]:        1549 :                 if (strcmp(rowid_name, ridinfo->rowidname) != 0)
     909                 :         763 :                         continue;
     910         [ +  - ]:         786 :                 if (equal(rowid_var, ridinfo->rowidvar))
     911                 :             :                 {
     912                 :             :                         /* Found a match; we need only record that rtindex needs it too */
     913                 :         786 :                         ridinfo->rowidrels = bms_add_member(ridinfo->rowidrels, rtindex);
     914                 :         786 :                         return;
     915                 :             :                 }
     916                 :             :                 else
     917                 :             :                 {
     918                 :             :                         /* Ooops, can't handle this */
     919   [ #  #  #  # ]:           0 :                         elog(ERROR, "conflicting uses of row-identity name \"%s\"",
     920                 :             :                                  rowid_name);
     921                 :             :                 }
     922                 :           0 :         }
     923                 :             : 
     924                 :             :         /* No request yet, so add a new RowIdentityVarInfo */
     925                 :         740 :         ridinfo = makeNode(RowIdentityVarInfo);
     926                 :         740 :         ridinfo->rowidvar = copyObject(rowid_var);
     927                 :             :         /* for the moment, estimate width using just the datatype info */
     928                 :        1480 :         ridinfo->rowidwidth = get_typavgwidth(exprType((Node *) rowid_var),
     929                 :         740 :                                                                                   exprTypmod((Node *) rowid_var));
     930                 :         740 :         ridinfo->rowidname = pstrdup(rowid_name);
     931                 :         740 :         ridinfo->rowidrels = bms_make_singleton(rtindex);
     932                 :             : 
     933                 :         740 :         root->row_identity_vars = lappend(root->row_identity_vars, ridinfo);
     934                 :             : 
     935                 :             :         /* Change rowid_var into a reference to this row_identity_vars entry */
     936                 :         740 :         rowid_var->varattno = list_length(root->row_identity_vars);
     937                 :             : 
     938                 :             :         /* Push the ROWID_VAR reference variable into processed_tlist */
     939                 :        1480 :         tle = makeTargetEntry((Expr *) rowid_var,
     940                 :         740 :                                                   list_length(root->processed_tlist) + 1,
     941                 :         740 :                                                   pstrdup(rowid_name),
     942                 :             :                                                   true);
     943                 :         740 :         root->processed_tlist = lappend(root->processed_tlist, tle);
     944         [ -  + ]:        3016 : }
     945                 :             : 
     946                 :             : /*
     947                 :             :  * add_row_identity_columns
     948                 :             :  *
     949                 :             :  * This function adds the row identity columns needed by the core code.
     950                 :             :  * FDWs might call add_row_identity_var() for themselves to add nonstandard
     951                 :             :  * columns.  (Duplicate requests are fine.)
     952                 :             :  */
     953                 :             : void
     954                 :        2300 : add_row_identity_columns(PlannerInfo *root, Index rtindex,
     955                 :             :                                                  RangeTblEntry *target_rte,
     956                 :             :                                                  Relation target_relation)
     957                 :             : {
     958                 :        2300 :         CmdType         commandType = root->parse->commandType;
     959                 :        2300 :         char            relkind = target_relation->rd_rel->relkind;
     960                 :        2300 :         Var                *var;
     961                 :             : 
     962   [ +  +  +  +  :        2300 :         Assert(commandType == CMD_UPDATE || commandType == CMD_DELETE || commandType == CMD_MERGE);
                   +  - ]
     963                 :             : 
     964         [ +  + ]:        2300 :         if (relkind == RELKIND_RELATION ||
     965   [ +  +  +  + ]:          60 :                 relkind == RELKIND_MATVIEW ||
     966                 :          53 :                 relkind == RELKIND_PARTITIONED_TABLE)
     967                 :             :         {
     968                 :             :                 /*
     969                 :             :                  * Emit CTID so that executor can find the row to merge, update or
     970                 :             :                  * delete.
     971                 :             :                  */
     972                 :        2253 :                 var = makeVar(rtindex,
     973                 :             :                                           SelfItemPointerAttributeNumber,
     974                 :             :                                           TIDOID,
     975                 :             :                                           -1,
     976                 :             :                                           InvalidOid,
     977                 :             :                                           0);
     978                 :        2253 :                 add_row_identity_var(root, var, rtindex, "ctid");
     979                 :        2253 :         }
     980         [ +  - ]:          47 :         else if (relkind == RELKIND_FOREIGN_TABLE)
     981                 :             :         {
     982                 :             :                 /*
     983                 :             :                  * Let the foreign table's FDW add whatever junk TLEs it wants.
     984                 :             :                  */
     985                 :           0 :                 FdwRoutine *fdwroutine;
     986                 :             : 
     987                 :           0 :                 fdwroutine = GetFdwRoutineForRelation(target_relation, false);
     988                 :             : 
     989         [ #  # ]:           0 :                 if (fdwroutine->AddForeignUpdateTargets != NULL)
     990                 :           0 :                         fdwroutine->AddForeignUpdateTargets(root, rtindex,
     991                 :           0 :                                                                                                 target_rte, target_relation);
     992                 :             : 
     993                 :             :                 /*
     994                 :             :                  * For UPDATE, we need to make the FDW fetch unchanged columns by
     995                 :             :                  * asking it to fetch a whole-row Var.  That's because the top-level
     996                 :             :                  * targetlist only contains entries for changed columns, but
     997                 :             :                  * ExecUpdate will need to build the complete new tuple.  (Actually,
     998                 :             :                  * we only really need this in UPDATEs that are not pushed to the
     999                 :             :                  * remote side, but it's hard to tell if that will be the case at the
    1000                 :             :                  * point when this function is called.)
    1001                 :             :                  *
    1002                 :             :                  * We will also need the whole row if there are any row triggers, so
    1003                 :             :                  * that the executor will have the "old" row to pass to the trigger.
    1004                 :             :                  * Alas, this misses system columns.
    1005                 :             :                  */
    1006   [ #  #  #  # ]:           0 :                 if (commandType == CMD_UPDATE ||
    1007         [ #  # ]:           0 :                         (target_relation->trigdesc &&
    1008         [ #  # ]:           0 :                          (target_relation->trigdesc->trig_delete_after_row ||
    1009                 :           0 :                           target_relation->trigdesc->trig_delete_before_row)))
    1010                 :             :                 {
    1011                 :           0 :                         var = makeVar(rtindex,
    1012                 :             :                                                   InvalidAttrNumber,
    1013                 :             :                                                   RECORDOID,
    1014                 :             :                                                   -1,
    1015                 :             :                                                   InvalidOid,
    1016                 :             :                                                   0);
    1017                 :           0 :                         add_row_identity_var(root, var, rtindex, "wholerow");
    1018                 :           0 :                 }
    1019                 :           0 :         }
    1020                 :        2300 : }
    1021                 :             : 
    1022                 :             : /*
    1023                 :             :  * distribute_row_identity_vars
    1024                 :             :  *
    1025                 :             :  * After we have finished identifying all the row identity columns
    1026                 :             :  * needed by an inherited UPDATE/DELETE/MERGE query, make sure that
    1027                 :             :  * these columns will be generated by all the target relations.
    1028                 :             :  *
    1029                 :             :  * This is more or less like what build_base_rel_tlists() does,
    1030                 :             :  * except that it would not understand what to do with ROWID_VAR Vars.
    1031                 :             :  * Since that function runs before inheritance relations are expanded,
    1032                 :             :  * it will never see any such Vars anyway.
    1033                 :             :  */
    1034                 :             : void
    1035                 :       33901 : distribute_row_identity_vars(PlannerInfo *root)
    1036                 :             : {
    1037                 :       33901 :         Query      *parse = root->parse;
    1038                 :       33901 :         int                     result_relation = parse->resultRelation;
    1039                 :       33901 :         RangeTblEntry *target_rte;
    1040                 :       33901 :         RelOptInfo *target_rel;
    1041                 :       33901 :         ListCell   *lc;
    1042                 :             : 
    1043                 :             :         /*
    1044                 :             :          * There's nothing to do if this isn't an inherited UPDATE/DELETE/MERGE.
    1045                 :             :          */
    1046   [ +  +  +  +  :       33901 :         if (parse->commandType != CMD_UPDATE && parse->commandType != CMD_DELETE &&
                   +  + ]
    1047                 :       32267 :                 parse->commandType != CMD_MERGE)
    1048                 :             :         {
    1049         [ +  - ]:       31995 :                 Assert(root->row_identity_vars == NIL);
    1050                 :       31995 :                 return;
    1051                 :             :         }
    1052                 :        1906 :         target_rte = rt_fetch(result_relation, parse->rtable);
    1053         [ +  + ]:        1906 :         if (!target_rte->inh)
    1054                 :             :         {
    1055         [ +  - ]:        1531 :                 Assert(root->row_identity_vars == NIL);
    1056                 :        1531 :                 return;
    1057                 :             :         }
    1058                 :             : 
    1059                 :             :         /*
    1060                 :             :          * Ordinarily, we expect that leaf result relation(s) will have added some
    1061                 :             :          * ROWID_VAR Vars to the query.  However, it's possible that constraint
    1062                 :             :          * exclusion suppressed every leaf relation.  The executor will get upset
    1063                 :             :          * if the plan has no row identity columns at all, even though it will
    1064                 :             :          * certainly process no rows.  Handle this edge case by re-opening the top
    1065                 :             :          * result relation and adding the row identity columns it would have used,
    1066                 :             :          * as preprocess_targetlist() would have done if it weren't marked "inh".
    1067                 :             :          * Then re-run build_base_rel_tlists() to ensure that the added columns
    1068                 :             :          * get propagated to the relation's reltarget.  (This is a bit ugly, but
    1069                 :             :          * it seems better to confine the ugliness and extra cycles to this
    1070                 :             :          * unusual corner case.)
    1071                 :             :          */
    1072         [ +  + ]:         375 :         if (root->row_identity_vars == NIL)
    1073                 :             :         {
    1074                 :           5 :                 Relation        target_relation;
    1075                 :             : 
    1076                 :           5 :                 target_relation = table_open(target_rte->relid, NoLock);
    1077                 :          10 :                 add_row_identity_columns(root, result_relation,
    1078                 :           5 :                                                                  target_rte, target_relation);
    1079                 :           5 :                 table_close(target_relation, NoLock);
    1080                 :           5 :                 build_base_rel_tlists(root, root->processed_tlist);
    1081                 :             :                 /* There are no ROWID_VAR Vars in this case, so we're done. */
    1082                 :             :                 return;
    1083                 :           5 :         }
    1084                 :             : 
    1085                 :             :         /*
    1086                 :             :          * Dig through the processed_tlist to find the ROWID_VAR reference Vars,
    1087                 :             :          * and forcibly copy them into the reltarget list of the topmost target
    1088                 :             :          * relation.  That's sufficient because they'll be copied to the
    1089                 :             :          * individual leaf target rels (with appropriate translation) later,
    1090                 :             :          * during appendrel expansion --- see set_append_rel_size().
    1091                 :             :          */
    1092                 :         370 :         target_rel = find_base_rel(root, result_relation);
    1093                 :             : 
    1094   [ +  -  +  +  :        1565 :         foreach(lc, root->processed_tlist)
                   +  + ]
    1095                 :             :         {
    1096                 :        1195 :                 TargetEntry *tle = lfirst(lc);
    1097                 :        1195 :                 Var                *var = (Var *) tle->expr;
    1098                 :             : 
    1099   [ +  -  +  +  :        1195 :                 if (var && IsA(var, Var) && var->varno == ROWID_VAR)
                   +  + ]
    1100                 :             :                 {
    1101                 :         740 :                         target_rel->reltarget->exprs =
    1102                 :         740 :                                 lappend(target_rel->reltarget->exprs, copyObject(var));
    1103                 :             :                         /* reltarget cost and width will be computed later */
    1104                 :         740 :                 }
    1105                 :        1195 :         }
    1106         [ -  + ]:       33901 : }
        

Generated by: LCOV version 2.3.2-1