LCOV - code coverage report
Current view: top level - src/backend/optimizer/prep - preptlist.c (source / functions) Coverage Total Hit
Test: PostgreSQL 19devel Lines: 89.3 % 150 134
Test Date: 2026-03-23 20:15:48 Functions: 100.0 % 4 4
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /*-------------------------------------------------------------------------
       2              :  *
       3              :  * preptlist.c
       4              :  *    Routines to preprocess the parse tree target list
       5              :  *
       6              :  * For an INSERT, the targetlist must contain an entry for each attribute of
       7              :  * the target relation in the correct order.
       8              :  *
       9              :  * For an UPDATE, the targetlist just contains the expressions for the new
      10              :  * column values.
      11              :  *
      12              :  * For UPDATE and DELETE queries, the targetlist must also contain "junk"
      13              :  * tlist entries needed to allow the executor to identify the rows to be
      14              :  * updated or deleted; for example, the ctid of a heap row.  (The planner
      15              :  * adds these; they're not in what we receive from the parser/rewriter.)
      16              :  *
      17              :  * For all query types, there can be additional junk tlist entries, such as
      18              :  * sort keys, Vars needed for a RETURNING list, and row ID information needed
      19              :  * for SELECT FOR UPDATE locking and/or EvalPlanQual checking.
      20              :  *
      21              :  * The query rewrite phase also does preprocessing of the targetlist (see
      22              :  * rewriteTargetListIU).  The division of labor between here and there is
      23              :  * partially historical, but it's not entirely arbitrary.  The stuff done
      24              :  * here is closely connected to physical access to tables, whereas the
      25              :  * rewriter's work is more concerned with SQL semantics.
      26              :  *
      27              :  *
      28              :  * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
      29              :  * Portions Copyright (c) 1994, Regents of the University of California
      30              :  *
      31              :  * IDENTIFICATION
      32              :  *    src/backend/optimizer/prep/preptlist.c
      33              :  *
      34              :  *-------------------------------------------------------------------------
      35              :  */
      36              : 
      37              : #include "postgres.h"
      38              : 
      39              : #include "access/sysattr.h"
      40              : #include "access/table.h"
      41              : #include "catalog/pg_type_d.h"
      42              : #include "nodes/makefuncs.h"
      43              : #include "optimizer/appendinfo.h"
      44              : #include "optimizer/optimizer.h"
      45              : #include "optimizer/prep.h"
      46              : #include "optimizer/tlist.h"
      47              : #include "parser/parse_coerce.h"
      48              : #include "parser/parsetree.h"
      49              : #include "utils/lsyscache.h"
      50              : #include "utils/rel.h"
      51              : 
      52              : static List *expand_insert_targetlist(PlannerInfo *root, List *tlist,
      53              :                                       Relation rel);
      54              : 
      55              : 
      56              : /*
      57              :  * preprocess_targetlist
      58              :  *    Driver for preprocessing the parse tree targetlist.
      59              :  *
      60              :  * The preprocessed targetlist is returned in root->processed_tlist.
      61              :  * Also, if this is an UPDATE, we return a list of target column numbers
      62              :  * in root->update_colnos.  (Resnos in processed_tlist will be consecutive,
      63              :  * so do not look at that to find out which columns are targets!)
      64              :  */
      65              : void
      66       388653 : preprocess_targetlist(PlannerInfo *root)
      67              : {
      68       388653 :     Query      *parse = root->parse;
      69       388653 :     int         result_relation = parse->resultRelation;
      70       388653 :     List       *range_table = parse->rtable;
      71       388653 :     CmdType     command_type = parse->commandType;
      72       388653 :     RangeTblEntry *target_rte = NULL;
      73       388653 :     Relation    target_relation = NULL;
      74              :     List       *tlist;
      75              :     ListCell   *lc;
      76              : 
      77              :     /*
      78              :      * If there is a result relation, open it so we can look for missing
      79              :      * columns and so on.  We assume that previous code already acquired at
      80              :      * least AccessShareLock on the relation, so we need no lock here.
      81              :      */
      82       388653 :     if (result_relation)
      83              :     {
      84        62838 :         target_rte = rt_fetch(result_relation, range_table);
      85              : 
      86              :         /*
      87              :          * Sanity check: it'd better be a real relation not, say, a subquery.
      88              :          * Else parser or rewriter messed up.
      89              :          */
      90        62838 :         if (target_rte->rtekind != RTE_RELATION)
      91            0 :             elog(ERROR, "result relation must be a regular relation");
      92              : 
      93        62838 :         target_relation = table_open(target_rte->relid, NoLock);
      94              :     }
      95              :     else
      96              :         Assert(command_type == CMD_SELECT);
      97              : 
      98              :     /*
      99              :      * In an INSERT, the executor expects the targetlist to match the exact
     100              :      * order of the target table's attributes, including entries for
     101              :      * attributes not mentioned in the source query.
     102              :      *
     103              :      * In an UPDATE, we don't rearrange the tlist order, but we need to make a
     104              :      * separate list of the target attribute numbers, in tlist order, and then
     105              :      * renumber the processed_tlist entries to be consecutive.
     106              :      */
     107       388653 :     tlist = parse->targetList;
     108       388653 :     if (command_type == CMD_INSERT)
     109        48401 :         tlist = expand_insert_targetlist(root, tlist, target_relation);
     110       340252 :     else if (command_type == CMD_UPDATE)
     111         9728 :         root->update_colnos = extract_update_targetlist_colnos(tlist);
     112              : 
     113              :     /*
     114              :      * For non-inherited UPDATE/DELETE/MERGE, register any junk column(s)
     115              :      * needed to allow the executor to identify the rows to be updated or
     116              :      * deleted.  In the inheritance case, we do nothing now, leaving this to
     117              :      * be dealt with when expand_inherited_rtentry() makes the leaf target
     118              :      * relations.  (But there might not be any leaf target relations, in which
     119              :      * case we must do this in distribute_row_identity_vars().)
     120              :      */
     121       388653 :     if ((command_type == CMD_UPDATE || command_type == CMD_DELETE ||
     122        14437 :          command_type == CMD_MERGE) &&
     123        14437 :         !target_rte->inh)
     124              :     {
     125              :         /* row-identity logic expects to add stuff to processed_tlist */
     126        12191 :         root->processed_tlist = tlist;
     127        12191 :         add_row_identity_columns(root, result_relation,
     128              :                                  target_rte, target_relation);
     129        12191 :         tlist = root->processed_tlist;
     130              :     }
     131              : 
     132              :     /*
     133              :      * For MERGE we also need to handle the target list for each INSERT and
     134              :      * UPDATE action separately.  In addition, we examine the qual of each
     135              :      * action and add any Vars there (other than those of the target rel) to
     136              :      * the subplan targetlist.
     137              :      */
     138       388653 :     if (command_type == CMD_MERGE)
     139              :     {
     140              :         ListCell   *l;
     141              :         List       *vars;
     142              : 
     143              :         /*
     144              :          * For MERGE, handle targetlist of each MergeAction separately. Give
     145              :          * the same treatment to MergeAction->targetList as we would have
     146              :          * given to a regular INSERT.  For UPDATE, collect the column numbers
     147              :          * being modified.
     148              :          */
     149         3731 :         foreach(l, parse->mergeActionList)
     150              :         {
     151         2256 :             MergeAction *action = (MergeAction *) lfirst(l);
     152              :             ListCell   *l2;
     153              : 
     154         2256 :             if (action->commandType == CMD_INSERT)
     155          741 :                 action->targetList = expand_insert_targetlist(root,
     156              :                                                               action->targetList,
     157              :                                                               target_relation);
     158         1515 :             else if (action->commandType == CMD_UPDATE)
     159         1067 :                 action->updateColnos =
     160         1067 :                     extract_update_targetlist_colnos(action->targetList);
     161              : 
     162              :             /*
     163              :              * Add resjunk entries for any Vars and PlaceHolderVars used in
     164              :              * each action's targetlist and WHEN condition that belong to
     165              :              * relations other than the target.  We don't expect to see any
     166              :              * aggregates or window functions here.
     167              :              */
     168         2256 :             vars = pull_var_clause((Node *)
     169         2256 :                                    list_concat_copy((List *) action->qual,
     170         2256 :                                                     action->targetList),
     171              :                                    PVC_INCLUDE_PLACEHOLDERS);
     172         5050 :             foreach(l2, vars)
     173              :             {
     174         2794 :                 Var        *var = (Var *) lfirst(l2);
     175              :                 TargetEntry *tle;
     176              : 
     177         2794 :                 if (IsA(var, Var) && var->varno == result_relation)
     178         1330 :                     continue;   /* don't need it */
     179              : 
     180         1464 :                 if (tlist_member((Expr *) var, tlist))
     181          389 :                     continue;   /* already got it */
     182              : 
     183         1075 :                 tle = makeTargetEntry((Expr *) var,
     184         1075 :                                       list_length(tlist) + 1,
     185              :                                       NULL, true);
     186         1075 :                 tlist = lappend(tlist, tle);
     187              :             }
     188         2256 :             list_free(vars);
     189              :         }
     190              : 
     191              :         /*
     192              :          * Add resjunk entries for any Vars and PlaceHolderVars used in the
     193              :          * join condition that belong to relations other than the target.  We
     194              :          * don't expect to see any aggregates or window functions here.
     195              :          */
     196         1475 :         vars = pull_var_clause(parse->mergeJoinCondition,
     197              :                                PVC_INCLUDE_PLACEHOLDERS);
     198         1800 :         foreach(l, vars)
     199              :         {
     200          325 :             Var        *var = (Var *) lfirst(l);
     201              :             TargetEntry *tle;
     202              : 
     203          325 :             if (IsA(var, Var) && var->varno == result_relation)
     204          110 :                 continue;       /* don't need it */
     205              : 
     206          215 :             if (tlist_member((Expr *) var, tlist))
     207           85 :                 continue;       /* already got it */
     208              : 
     209          130 :             tle = makeTargetEntry((Expr *) var,
     210          130 :                                   list_length(tlist) + 1,
     211              :                                   NULL, true);
     212          130 :             tlist = lappend(tlist, tle);
     213              :         }
     214              :     }
     215              : 
     216              :     /*
     217              :      * Add necessary junk columns for rowmarked rels.  These values are needed
     218              :      * for locking of rels selected FOR UPDATE/SHARE, and to do EvalPlanQual
     219              :      * rechecking.  See comments for PlanRowMark in plannodes.h.  If you
     220              :      * change this stanza, see also expand_inherited_rtentry(), which has to
     221              :      * be able to add on junk columns equivalent to these.
     222              :      *
     223              :      * (Someday it might be useful to fold these resjunk columns into the
     224              :      * row-identity-column management used for UPDATE/DELETE.  Today is not
     225              :      * that day, however.  One notable issue is that it seems important that
     226              :      * the whole-row Vars made here use the real table rowtype, not RECORD, so
     227              :      * that conversion to/from child relations' rowtypes will happen.  Also,
     228              :      * since these entries don't potentially bloat with more and more child
     229              :      * relations, there's not really much need for column sharing.)
     230              :      */
     231       399525 :     foreach(lc, root->rowMarks)
     232              :     {
     233        10872 :         PlanRowMark *rc = (PlanRowMark *) lfirst(lc);
     234              :         Var        *var;
     235              :         char        resname[32];
     236              :         TargetEntry *tle;
     237              : 
     238              :         /* child rels use the same junk attrs as their parents */
     239        10872 :         if (rc->rti != rc->prti)
     240            0 :             continue;
     241              : 
     242        10872 :         if (rc->allMarkTypes & ~(1 << ROW_MARK_COPY))
     243              :         {
     244              :             /* Need to fetch TID */
     245        10344 :             var = makeVar(rc->rti,
     246              :                           SelfItemPointerAttributeNumber,
     247              :                           TIDOID,
     248              :                           -1,
     249              :                           InvalidOid,
     250              :                           0);
     251        10344 :             snprintf(resname, sizeof(resname), "ctid%u", rc->rowmarkId);
     252        10344 :             tle = makeTargetEntry((Expr *) var,
     253        10344 :                                   list_length(tlist) + 1,
     254              :                                   pstrdup(resname),
     255              :                                   true);
     256        10344 :             tlist = lappend(tlist, tle);
     257              :         }
     258        10872 :         if (rc->allMarkTypes & (1 << ROW_MARK_COPY))
     259              :         {
     260              :             /* Need the whole row as a junk var */
     261          528 :             var = makeWholeRowVar(rt_fetch(rc->rti, range_table),
     262          528 :                                   rc->rti,
     263              :                                   0,
     264              :                                   false);
     265          528 :             snprintf(resname, sizeof(resname), "wholerow%u", rc->rowmarkId);
     266          528 :             tle = makeTargetEntry((Expr *) var,
     267          528 :                                   list_length(tlist) + 1,
     268              :                                   pstrdup(resname),
     269              :                                   true);
     270          528 :             tlist = lappend(tlist, tle);
     271              :         }
     272              : 
     273              :         /* If parent of inheritance tree, always fetch the tableoid too. */
     274        10872 :         if (rc->isParent)
     275              :         {
     276            0 :             var = makeVar(rc->rti,
     277              :                           TableOidAttributeNumber,
     278              :                           OIDOID,
     279              :                           -1,
     280              :                           InvalidOid,
     281              :                           0);
     282            0 :             snprintf(resname, sizeof(resname), "tableoid%u", rc->rowmarkId);
     283            0 :             tle = makeTargetEntry((Expr *) var,
     284            0 :                                   list_length(tlist) + 1,
     285              :                                   pstrdup(resname),
     286              :                                   true);
     287            0 :             tlist = lappend(tlist, tle);
     288              :         }
     289              :     }
     290              : 
     291              :     /*
     292              :      * If the query has a RETURNING list, add resjunk entries for any Vars
     293              :      * used in RETURNING that belong to other relations.  We need to do this
     294              :      * to make these Vars available for the RETURNING calculation.  Vars that
     295              :      * belong to the result rel don't need to be added, because they will be
     296              :      * made to refer to the actual heap tuple.
     297              :      */
     298       388653 :     if (parse->returningList && list_length(parse->rtable) > 1)
     299              :     {
     300              :         List       *vars;
     301              :         ListCell   *l;
     302              : 
     303         1716 :         vars = pull_var_clause((Node *) parse->returningList,
     304              :                                PVC_RECURSE_AGGREGATES |
     305              :                                PVC_RECURSE_WINDOWFUNCS |
     306              :                                PVC_INCLUDE_PLACEHOLDERS);
     307         7480 :         foreach(l, vars)
     308              :         {
     309         5764 :             Var        *var = (Var *) lfirst(l);
     310              :             TargetEntry *tle;
     311              : 
     312         5764 :             if (IsA(var, Var) &&
     313         5764 :                 var->varno == result_relation)
     314         5252 :                 continue;       /* don't need it */
     315              : 
     316          512 :             if (tlist_member((Expr *) var, tlist))
     317          192 :                 continue;       /* already got it */
     318              : 
     319          320 :             tle = makeTargetEntry((Expr *) var,
     320          320 :                                   list_length(tlist) + 1,
     321              :                                   NULL,
     322              :                                   true);
     323              : 
     324          320 :             tlist = lappend(tlist, tle);
     325              :         }
     326         1716 :         list_free(vars);
     327              :     }
     328              : 
     329       388653 :     root->processed_tlist = tlist;
     330              : 
     331       388653 :     if (target_relation)
     332        62838 :         table_close(target_relation, NoLock);
     333       388653 : }
     334              : 
     335              : /*
     336              :  * extract_update_targetlist_colnos
     337              :  *      Extract a list of the target-table column numbers that
     338              :  *      an UPDATE's targetlist wants to assign to, then renumber.
     339              :  *
     340              :  * The convention in the parser and rewriter is that the resnos in an
     341              :  * UPDATE's non-resjunk TLE entries are the target column numbers
     342              :  * to assign to.  Here, we extract that info into a separate list, and
     343              :  * then convert the tlist to the sequential-numbering convention that's
     344              :  * used by all other query types.
     345              :  *
     346              :  * This is also applied to the tlist associated with INSERT ... ON CONFLICT
     347              :  * ... UPDATE, although not till much later in planning.
     348              :  */
     349              : List *
     350        12559 : extract_update_targetlist_colnos(List *tlist)
     351              : {
     352        12559 :     List       *update_colnos = NIL;
     353        12559 :     AttrNumber  nextresno = 1;
     354              :     ListCell   *lc;
     355              : 
     356        27581 :     foreach(lc, tlist)
     357              :     {
     358        15022 :         TargetEntry *tle = (TargetEntry *) lfirst(lc);
     359              : 
     360        15022 :         if (!tle->resjunk)
     361        14786 :             update_colnos = lappend_int(update_colnos, tle->resno);
     362        15022 :         tle->resno = nextresno++;
     363              :     }
     364        12559 :     return update_colnos;
     365              : }
     366              : 
     367              : 
     368              : /*****************************************************************************
     369              :  *
     370              :  *      TARGETLIST EXPANSION
     371              :  *
     372              :  *****************************************************************************/
     373              : 
     374              : /*
     375              :  * expand_insert_targetlist
     376              :  *    Given a target list as generated by the parser and a result relation,
     377              :  *    add targetlist entries for any missing attributes, and ensure the
     378              :  *    non-junk attributes appear in proper field order.
     379              :  *
     380              :  * Once upon a time we also did more or less this with UPDATE targetlists,
     381              :  * but now this code is only applied to INSERT targetlists.
     382              :  */
     383              : static List *
     384        49142 : expand_insert_targetlist(PlannerInfo *root, List *tlist, Relation rel)
     385              : {
     386        49142 :     List       *new_tlist = NIL;
     387              :     ListCell   *tlist_item;
     388              :     int         attrno,
     389              :                 numattrs;
     390              : 
     391        49142 :     tlist_item = list_head(tlist);
     392              : 
     393              :     /*
     394              :      * The rewriter should have already ensured that the TLEs are in correct
     395              :      * order; but we have to insert TLEs for any missing attributes.
     396              :      *
     397              :      * Scan the tuple description in the relation's relcache entry to make
     398              :      * sure we have all the user attributes in the right order.
     399              :      */
     400        49142 :     numattrs = RelationGetNumberOfAttributes(rel);
     401              : 
     402       163694 :     for (attrno = 1; attrno <= numattrs; attrno++)
     403              :     {
     404       114552 :         Form_pg_attribute att_tup = TupleDescAttr(rel->rd_att, attrno - 1);
     405       114552 :         TargetEntry *new_tle = NULL;
     406              : 
     407       114552 :         if (tlist_item != NULL)
     408              :         {
     409       107926 :             TargetEntry *old_tle = (TargetEntry *) lfirst(tlist_item);
     410              : 
     411       107926 :             if (!old_tle->resjunk && old_tle->resno == attrno)
     412              :             {
     413       100488 :                 new_tle = old_tle;
     414       100488 :                 tlist_item = lnext(tlist, tlist_item);
     415              :             }
     416              :         }
     417              : 
     418       114552 :         if (new_tle == NULL)
     419              :         {
     420              :             /*
     421              :              * Didn't find a matching tlist entry, so make one.
     422              :              *
     423              :              * INSERTs should insert NULL in this case.  (We assume the
     424              :              * rewriter would have inserted any available non-NULL default
     425              :              * value.)  Also, normally we must apply any domain constraints
     426              :              * that might exist --- this is to catch domain NOT NULL.
     427              :              *
     428              :              * When generating a NULL constant for a dropped column, we label
     429              :              * it INT4 (any other guaranteed-to-exist datatype would do as
     430              :              * well). We can't label it with the dropped column's datatype
     431              :              * since that might not exist anymore.  It does not really matter
     432              :              * what we claim the type is, since NULL is NULL --- its
     433              :              * representation is datatype-independent.  This could perhaps
     434              :              * confuse code comparing the finished plan to the target
     435              :              * relation, however.
     436              :              *
     437              :              * Another exception is that if the column is generated, the value
     438              :              * we produce here will be ignored, and we don't want to risk
     439              :              * throwing an error.  So in that case we *don't* want to apply
     440              :              * domain constraints, so we must produce a NULL of the base type.
     441              :              * Again, code comparing the finished plan to the target relation
     442              :              * must account for this.
     443              :              */
     444              :             Node       *new_expr;
     445              : 
     446        14064 :             if (att_tup->attisdropped)
     447              :             {
     448              :                 /* Insert NULL for dropped column */
     449          545 :                 new_expr = (Node *) makeConst(INT4OID,
     450              :                                               -1,
     451              :                                               InvalidOid,
     452              :                                               sizeof(int32),
     453              :                                               (Datum) 0,
     454              :                                               true, /* isnull */
     455              :                                               true /* byval */ );
     456              :             }
     457        13519 :             else if (att_tup->attgenerated)
     458              :             {
     459              :                 /* Generated column, insert a NULL of the base type */
     460          921 :                 Oid         baseTypeId = att_tup->atttypid;
     461          921 :                 int32       baseTypeMod = att_tup->atttypmod;
     462              : 
     463          921 :                 baseTypeId = getBaseTypeAndTypmod(baseTypeId, &baseTypeMod);
     464          921 :                 new_expr = (Node *) makeConst(baseTypeId,
     465              :                                               baseTypeMod,
     466              :                                               att_tup->attcollation,
     467          921 :                                               att_tup->attlen,
     468              :                                               (Datum) 0,
     469              :                                               true, /* isnull */
     470          921 :                                               att_tup->attbyval);
     471              :             }
     472              :             else
     473              :             {
     474              :                 /* Normal column, insert a NULL of the column datatype */
     475        12598 :                 new_expr = coerce_null_to_domain(att_tup->atttypid,
     476              :                                                  att_tup->atttypmod,
     477              :                                                  att_tup->attcollation,
     478        12598 :                                                  att_tup->attlen,
     479        12598 :                                                  att_tup->attbyval);
     480              :                 /* Must run expression preprocessing on any non-const nodes */
     481        12598 :                 if (!IsA(new_expr, Const))
     482           55 :                     new_expr = eval_const_expressions(root, new_expr);
     483              :             }
     484              : 
     485        14064 :             new_tle = makeTargetEntry((Expr *) new_expr,
     486              :                                       attrno,
     487        14064 :                                       pstrdup(NameStr(att_tup->attname)),
     488              :                                       false);
     489              :         }
     490              : 
     491       114552 :         new_tlist = lappend(new_tlist, new_tle);
     492              :     }
     493              : 
     494              :     /*
     495              :      * The remaining tlist entries should be resjunk; append them all to the
     496              :      * end of the new tlist, making sure they have resnos higher than the last
     497              :      * real attribute.  (Note: although the rewriter already did such
     498              :      * renumbering, we have to do it again here in case we added NULL entries
     499              :      * above.)
     500              :      */
     501        49142 :     while (tlist_item)
     502              :     {
     503            0 :         TargetEntry *old_tle = (TargetEntry *) lfirst(tlist_item);
     504              : 
     505            0 :         if (!old_tle->resjunk)
     506            0 :             elog(ERROR, "targetlist is not sorted correctly");
     507              :         /* Get the resno right, but don't copy unnecessarily */
     508            0 :         if (old_tle->resno != attrno)
     509              :         {
     510            0 :             old_tle = flatCopyTargetEntry(old_tle);
     511            0 :             old_tle->resno = attrno;
     512              :         }
     513            0 :         new_tlist = lappend(new_tlist, old_tle);
     514            0 :         attrno++;
     515            0 :         tlist_item = lnext(tlist, tlist_item);
     516              :     }
     517              : 
     518        49142 :     return new_tlist;
     519              : }
     520              : 
     521              : 
     522              : /*
     523              :  * Locate PlanRowMark for given RT index, or return NULL if none
     524              :  *
     525              :  * This probably ought to be elsewhere, but there's no very good place
     526              :  */
     527              : PlanRowMark *
     528        16885 : get_plan_rowmark(List *rowmarks, Index rtindex)
     529              : {
     530              :     ListCell   *l;
     531              : 
     532        17903 :     foreach(l, rowmarks)
     533              :     {
     534         2731 :         PlanRowMark *rc = (PlanRowMark *) lfirst(l);
     535              : 
     536         2731 :         if (rc->rti == rtindex)
     537         1713 :             return rc;
     538              :     }
     539        15172 :     return NULL;
     540              : }
        

Generated by: LCOV version 2.0-1