PostgreSQL Source Code git master
preptlist.c
Go to the documentation of this file.
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-2025, 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/table.h"
40#include "nodes/makefuncs.h"
42#include "optimizer/optimizer.h"
43#include "optimizer/prep.h"
44#include "optimizer/tlist.h"
45#include "parser/parse_coerce.h"
46#include "parser/parsetree.h"
47#include "utils/rel.h"
48
50 Relation rel);
51
52
53/*
54 * preprocess_targetlist
55 * Driver for preprocessing the parse tree targetlist.
56 *
57 * The preprocessed targetlist is returned in root->processed_tlist.
58 * Also, if this is an UPDATE, we return a list of target column numbers
59 * in root->update_colnos. (Resnos in processed_tlist will be consecutive,
60 * so do not look at that to find out which columns are targets!)
61 */
62void
64{
65 Query *parse = root->parse;
66 int result_relation = parse->resultRelation;
67 List *range_table = parse->rtable;
68 CmdType command_type = parse->commandType;
69 RangeTblEntry *target_rte = NULL;
70 Relation target_relation = NULL;
71 List *tlist;
72 ListCell *lc;
73
74 /*
75 * If there is a result relation, open it so we can look for missing
76 * columns and so on. We assume that previous code already acquired at
77 * least AccessShareLock on the relation, so we need no lock here.
78 */
79 if (result_relation)
80 {
81 target_rte = rt_fetch(result_relation, range_table);
82
83 /*
84 * Sanity check: it'd better be a real relation not, say, a subquery.
85 * Else parser or rewriter messed up.
86 */
87 if (target_rte->rtekind != RTE_RELATION)
88 elog(ERROR, "result relation must be a regular relation");
89
90 target_relation = table_open(target_rte->relid, NoLock);
91 }
92 else
93 Assert(command_type == CMD_SELECT);
94
95 /*
96 * In an INSERT, the executor expects the targetlist to match the exact
97 * order of the target table's attributes, including entries for
98 * attributes not mentioned in the source query.
99 *
100 * In an UPDATE, we don't rearrange the tlist order, but we need to make a
101 * separate list of the target attribute numbers, in tlist order, and then
102 * renumber the processed_tlist entries to be consecutive.
103 */
104 tlist = parse->targetList;
105 if (command_type == CMD_INSERT)
106 tlist = expand_insert_targetlist(root, tlist, target_relation);
107 else if (command_type == CMD_UPDATE)
108 root->update_colnos = extract_update_targetlist_colnos(tlist);
109
110 /*
111 * For non-inherited UPDATE/DELETE/MERGE, register any junk column(s)
112 * needed to allow the executor to identify the rows to be updated or
113 * deleted. In the inheritance case, we do nothing now, leaving this to
114 * be dealt with when expand_inherited_rtentry() makes the leaf target
115 * relations. (But there might not be any leaf target relations, in which
116 * case we must do this in distribute_row_identity_vars().)
117 */
118 if ((command_type == CMD_UPDATE || command_type == CMD_DELETE ||
119 command_type == CMD_MERGE) &&
120 !target_rte->inh)
121 {
122 /* row-identity logic expects to add stuff to processed_tlist */
123 root->processed_tlist = tlist;
124 add_row_identity_columns(root, result_relation,
125 target_rte, target_relation);
126 tlist = root->processed_tlist;
127 }
128
129 /*
130 * For MERGE we also need to handle the target list for each INSERT and
131 * UPDATE action separately. In addition, we examine the qual of each
132 * action and add any Vars there (other than those of the target rel) to
133 * the subplan targetlist.
134 */
135 if (command_type == CMD_MERGE)
136 {
137 ListCell *l;
138 List *vars;
139
140 /*
141 * For MERGE, handle targetlist of each MergeAction separately. Give
142 * the same treatment to MergeAction->targetList as we would have
143 * given to a regular INSERT. For UPDATE, collect the column numbers
144 * being modified.
145 */
146 foreach(l, parse->mergeActionList)
147 {
149 ListCell *l2;
150
151 if (action->commandType == CMD_INSERT)
153 action->targetList,
154 target_relation);
155 else if (action->commandType == CMD_UPDATE)
156 action->updateColnos =
158
159 /*
160 * Add resjunk entries for any Vars and PlaceHolderVars used in
161 * each action's targetlist and WHEN condition that belong to
162 * relations other than the target. We don't expect to see any
163 * aggregates or window functions here.
164 */
166 list_concat_copy((List *) action->qual,
167 action->targetList),
169 foreach(l2, vars)
170 {
171 Var *var = (Var *) lfirst(l2);
172 TargetEntry *tle;
173
174 if (IsA(var, Var) && var->varno == result_relation)
175 continue; /* don't need it */
176
177 if (tlist_member((Expr *) var, tlist))
178 continue; /* already got it */
179
180 tle = makeTargetEntry((Expr *) var,
181 list_length(tlist) + 1,
182 NULL, true);
183 tlist = lappend(tlist, tle);
184 }
186 }
187
188 /*
189 * Add resjunk entries for any Vars and PlaceHolderVars used in the
190 * join condition that belong to relations other than the target. We
191 * don't expect to see any aggregates or window functions here.
192 */
193 vars = pull_var_clause(parse->mergeJoinCondition,
195 foreach(l, vars)
196 {
197 Var *var = (Var *) lfirst(l);
198 TargetEntry *tle;
199
200 if (IsA(var, Var) && var->varno == result_relation)
201 continue; /* don't need it */
202
203 if (tlist_member((Expr *) var, tlist))
204 continue; /* already got it */
205
206 tle = makeTargetEntry((Expr *) var,
207 list_length(tlist) + 1,
208 NULL, true);
209 tlist = lappend(tlist, tle);
210 }
211 }
212
213 /*
214 * Add necessary junk columns for rowmarked rels. These values are needed
215 * for locking of rels selected FOR UPDATE/SHARE, and to do EvalPlanQual
216 * rechecking. See comments for PlanRowMark in plannodes.h. If you
217 * change this stanza, see also expand_inherited_rtentry(), which has to
218 * be able to add on junk columns equivalent to these.
219 *
220 * (Someday it might be useful to fold these resjunk columns into the
221 * row-identity-column management used for UPDATE/DELETE. Today is not
222 * that day, however. One notable issue is that it seems important that
223 * the whole-row Vars made here use the real table rowtype, not RECORD, so
224 * that conversion to/from child relations' rowtypes will happen. Also,
225 * since these entries don't potentially bloat with more and more child
226 * relations, there's not really much need for column sharing.)
227 */
228 foreach(lc, root->rowMarks)
229 {
230 PlanRowMark *rc = (PlanRowMark *) lfirst(lc);
231 Var *var;
232 char resname[32];
233 TargetEntry *tle;
234
235 /* child rels use the same junk attrs as their parents */
236 if (rc->rti != rc->prti)
237 continue;
238
239 if (rc->allMarkTypes & ~(1 << ROW_MARK_COPY))
240 {
241 /* Need to fetch TID */
242 var = makeVar(rc->rti,
244 TIDOID,
245 -1,
247 0);
248 snprintf(resname, sizeof(resname), "ctid%u", rc->rowmarkId);
249 tle = makeTargetEntry((Expr *) var,
250 list_length(tlist) + 1,
251 pstrdup(resname),
252 true);
253 tlist = lappend(tlist, tle);
254 }
255 if (rc->allMarkTypes & (1 << ROW_MARK_COPY))
256 {
257 /* Need the whole row as a junk var */
258 var = makeWholeRowVar(rt_fetch(rc->rti, range_table),
259 rc->rti,
260 0,
261 false);
262 snprintf(resname, sizeof(resname), "wholerow%u", rc->rowmarkId);
263 tle = makeTargetEntry((Expr *) var,
264 list_length(tlist) + 1,
265 pstrdup(resname),
266 true);
267 tlist = lappend(tlist, tle);
268 }
269
270 /* If parent of inheritance tree, always fetch the tableoid too. */
271 if (rc->isParent)
272 {
273 var = makeVar(rc->rti,
275 OIDOID,
276 -1,
278 0);
279 snprintf(resname, sizeof(resname), "tableoid%u", rc->rowmarkId);
280 tle = makeTargetEntry((Expr *) var,
281 list_length(tlist) + 1,
282 pstrdup(resname),
283 true);
284 tlist = lappend(tlist, tle);
285 }
286 }
287
288 /*
289 * If the query has a RETURNING list, add resjunk entries for any Vars
290 * used in RETURNING that belong to other relations. We need to do this
291 * to make these Vars available for the RETURNING calculation. Vars that
292 * belong to the result rel don't need to be added, because they will be
293 * made to refer to the actual heap tuple.
294 */
295 if (parse->returningList && list_length(parse->rtable) > 1)
296 {
297 List *vars;
298 ListCell *l;
299
300 vars = pull_var_clause((Node *) parse->returningList,
304 foreach(l, vars)
305 {
306 Var *var = (Var *) lfirst(l);
307 TargetEntry *tle;
308
309 if (IsA(var, Var) &&
310 var->varno == result_relation)
311 continue; /* don't need it */
312
313 if (tlist_member((Expr *) var, tlist))
314 continue; /* already got it */
315
316 tle = makeTargetEntry((Expr *) var,
317 list_length(tlist) + 1,
318 NULL,
319 true);
320
321 tlist = lappend(tlist, tle);
322 }
324 }
325
326 root->processed_tlist = tlist;
327
328 if (target_relation)
329 table_close(target_relation, NoLock);
330}
331
332/*
333 * extract_update_targetlist_colnos
334 * Extract a list of the target-table column numbers that
335 * an UPDATE's targetlist wants to assign to, then renumber.
336 *
337 * The convention in the parser and rewriter is that the resnos in an
338 * UPDATE's non-resjunk TLE entries are the target column numbers
339 * to assign to. Here, we extract that info into a separate list, and
340 * then convert the tlist to the sequential-numbering convention that's
341 * used by all other query types.
342 *
343 * This is also applied to the tlist associated with INSERT ... ON CONFLICT
344 * ... UPDATE, although not till much later in planning.
345 */
346List *
348{
349 List *update_colnos = NIL;
350 AttrNumber nextresno = 1;
351 ListCell *lc;
352
353 foreach(lc, tlist)
354 {
355 TargetEntry *tle = (TargetEntry *) lfirst(lc);
356
357 if (!tle->resjunk)
358 update_colnos = lappend_int(update_colnos, tle->resno);
359 tle->resno = nextresno++;
360 }
361 return update_colnos;
362}
363
364
365/*****************************************************************************
366 *
367 * TARGETLIST EXPANSION
368 *
369 *****************************************************************************/
370
371/*
372 * expand_insert_targetlist
373 * Given a target list as generated by the parser and a result relation,
374 * add targetlist entries for any missing attributes, and ensure the
375 * non-junk attributes appear in proper field order.
376 *
377 * Once upon a time we also did more or less this with UPDATE targetlists,
378 * but now this code is only applied to INSERT targetlists.
379 */
380static List *
382{
383 List *new_tlist = NIL;
384 ListCell *tlist_item;
385 int attrno,
386 numattrs;
387
388 tlist_item = list_head(tlist);
389
390 /*
391 * The rewriter should have already ensured that the TLEs are in correct
392 * order; but we have to insert TLEs for any missing attributes.
393 *
394 * Scan the tuple description in the relation's relcache entry to make
395 * sure we have all the user attributes in the right order.
396 */
397 numattrs = RelationGetNumberOfAttributes(rel);
398
399 for (attrno = 1; attrno <= numattrs; attrno++)
400 {
401 Form_pg_attribute att_tup = TupleDescAttr(rel->rd_att, attrno - 1);
402 TargetEntry *new_tle = NULL;
403
404 if (tlist_item != NULL)
405 {
406 TargetEntry *old_tle = (TargetEntry *) lfirst(tlist_item);
407
408 if (!old_tle->resjunk && old_tle->resno == attrno)
409 {
410 new_tle = old_tle;
411 tlist_item = lnext(tlist, tlist_item);
412 }
413 }
414
415 if (new_tle == NULL)
416 {
417 /*
418 * Didn't find a matching tlist entry, so make one.
419 *
420 * INSERTs should insert NULL in this case. (We assume the
421 * rewriter would have inserted any available non-NULL default
422 * value.) Also, if the column isn't dropped, apply any domain
423 * constraints that might exist --- this is to catch domain NOT
424 * NULL.
425 *
426 * When generating a NULL constant for a dropped column, we label
427 * it INT4 (any other guaranteed-to-exist datatype would do as
428 * well). We can't label it with the dropped column's datatype
429 * since that might not exist anymore. It does not really matter
430 * what we claim the type is, since NULL is NULL --- its
431 * representation is datatype-independent. This could perhaps
432 * confuse code comparing the finished plan to the target
433 * relation, however.
434 */
435 Node *new_expr;
436
437 if (!att_tup->attisdropped)
438 {
439 new_expr = coerce_null_to_domain(att_tup->atttypid,
440 att_tup->atttypmod,
441 att_tup->attcollation,
442 att_tup->attlen,
443 att_tup->attbyval);
444 /* Must run expression preprocessing on any non-const nodes */
445 if (!IsA(new_expr, Const))
446 new_expr = eval_const_expressions(root, new_expr);
447 }
448 else
449 {
450 /* Insert NULL for dropped column */
451 new_expr = (Node *) makeConst(INT4OID,
452 -1,
454 sizeof(int32),
455 (Datum) 0,
456 true, /* isnull */
457 true /* byval */ );
458 }
459
460 new_tle = makeTargetEntry((Expr *) new_expr,
461 attrno,
462 pstrdup(NameStr(att_tup->attname)),
463 false);
464 }
465
466 new_tlist = lappend(new_tlist, new_tle);
467 }
468
469 /*
470 * The remaining tlist entries should be resjunk; append them all to the
471 * end of the new tlist, making sure they have resnos higher than the last
472 * real attribute. (Note: although the rewriter already did such
473 * renumbering, we have to do it again here in case we added NULL entries
474 * above.)
475 */
476 while (tlist_item)
477 {
478 TargetEntry *old_tle = (TargetEntry *) lfirst(tlist_item);
479
480 if (!old_tle->resjunk)
481 elog(ERROR, "targetlist is not sorted correctly");
482 /* Get the resno right, but don't copy unnecessarily */
483 if (old_tle->resno != attrno)
484 {
485 old_tle = flatCopyTargetEntry(old_tle);
486 old_tle->resno = attrno;
487 }
488 new_tlist = lappend(new_tlist, old_tle);
489 attrno++;
490 tlist_item = lnext(tlist, tlist_item);
491 }
492
493 return new_tlist;
494}
495
496
497/*
498 * Locate PlanRowMark for given RT index, or return NULL if none
499 *
500 * This probably ought to be elsewhere, but there's no very good place
501 */
503get_plan_rowmark(List *rowmarks, Index rtindex)
504{
505 ListCell *l;
506
507 foreach(l, rowmarks)
508 {
509 PlanRowMark *rc = (PlanRowMark *) lfirst(l);
510
511 if (rc->rti == rtindex)
512 return rc;
513 }
514 return NULL;
515}
void add_row_identity_columns(PlannerInfo *root, Index rtindex, RangeTblEntry *target_rte, Relation target_relation)
Definition: appendinfo.c:904
int16 AttrNumber
Definition: attnum.h:21
#define NameStr(name)
Definition: c.h:703
#define Assert(condition)
Definition: c.h:815
int32_t int32
Definition: c.h:484
unsigned int Index
Definition: c.h:571
Node * eval_const_expressions(PlannerInfo *root, Node *node)
Definition: clauses.c:2254
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:225
List * lappend(List *list, void *datum)
Definition: list.c:339
List * list_concat_copy(const List *list1, const List *list2)
Definition: list.c:598
List * lappend_int(List *list, int datum)
Definition: list.c:357
void list_free(List *list)
Definition: list.c:1546
#define NoLock
Definition: lockdefs.h:34
Var * makeVar(int varno, AttrNumber varattno, Oid vartype, int32 vartypmod, Oid varcollid, Index varlevelsup)
Definition: makefuncs.c:66
Var * makeWholeRowVar(RangeTblEntry *rte, int varno, Index varlevelsup, bool allowScalar)
Definition: makefuncs.c:137
TargetEntry * makeTargetEntry(Expr *expr, AttrNumber resno, char *resname, bool resjunk)
Definition: makefuncs.c:242
Const * makeConst(Oid consttype, int32 consttypmod, Oid constcollid, int constlen, Datum constvalue, bool constisnull, bool constbyval)
Definition: makefuncs.c:303
TargetEntry * flatCopyTargetEntry(TargetEntry *src_tle)
Definition: makefuncs.c:275
char * pstrdup(const char *in)
Definition: mcxt.c:1696
#define IsA(nodeptr, _type_)
Definition: nodes.h:158
CmdType
Definition: nodes.h:263
@ CMD_MERGE
Definition: nodes.h:269
@ CMD_INSERT
Definition: nodes.h:267
@ CMD_DELETE
Definition: nodes.h:268
@ CMD_UPDATE
Definition: nodes.h:266
@ CMD_SELECT
Definition: nodes.h:265
#define PVC_RECURSE_AGGREGATES
Definition: optimizer.h:188
#define PVC_RECURSE_WINDOWFUNCS
Definition: optimizer.h:190
#define PVC_INCLUDE_PLACEHOLDERS
Definition: optimizer.h:191
Node * coerce_null_to_domain(Oid typid, int32 typmod, Oid collation, int typlen, bool typbyval)
@ RTE_RELATION
Definition: parsenodes.h:1026
#define rt_fetch(rangetable_index, rangetable)
Definition: parsetree.h:31
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:200
#define lfirst(lc)
Definition: pg_list.h:172
static int list_length(const List *l)
Definition: pg_list.h:152
#define NIL
Definition: pg_list.h:68
static ListCell * list_head(const List *l)
Definition: pg_list.h:128
static ListCell * lnext(const List *l, const ListCell *c)
Definition: pg_list.h:343
@ ROW_MARK_COPY
Definition: plannodes.h:1478
#define snprintf
Definition: port.h:239
uintptr_t Datum
Definition: postgres.h:69
#define InvalidOid
Definition: postgres_ext.h:37
List * extract_update_targetlist_colnos(List *tlist)
Definition: preptlist.c:347
void preprocess_targetlist(PlannerInfo *root)
Definition: preptlist.c:63
PlanRowMark * get_plan_rowmark(List *rowmarks, Index rtindex)
Definition: preptlist.c:503
static List * expand_insert_targetlist(PlannerInfo *root, List *tlist, Relation rel)
Definition: preptlist.c:381
tree ctl root
Definition: radixtree.h:1857
static struct subre * parse(struct vars *v, int stopper, int type, struct state *init, struct state *final)
Definition: regcomp.c:717
#define RelationGetNumberOfAttributes(relation)
Definition: rel.h:518
Definition: pg_list.h:54
Definition: nodes.h:129
Index prti
Definition: plannodes.h:1529
bool isParent
Definition: plannodes.h:1541
Index rowmarkId
Definition: plannodes.h:1531
int allMarkTypes
Definition: plannodes.h:1535
RTEKind rtekind
Definition: parsenodes.h:1056
TupleDesc rd_att
Definition: rel.h:112
AttrNumber resno
Definition: primnodes.h:2221
Definition: primnodes.h:262
int varno
Definition: primnodes.h:269
Definition: regcomp.c:282
#define TableOidAttributeNumber
Definition: sysattr.h:26
#define SelfItemPointerAttributeNumber
Definition: sysattr.h:21
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:126
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:40
TargetEntry * tlist_member(Expr *node, List *targetlist)
Definition: tlist.c:79
static FormData_pg_attribute * TupleDescAttr(TupleDesc tupdesc, int i)
Definition: tupdesc.h:154
List * pull_var_clause(Node *node, int flags)
Definition: var.c:653