PostgreSQL Source Code git master
preptlist.c File Reference
#include "postgres.h"
#include "access/table.h"
#include "nodes/makefuncs.h"
#include "optimizer/appendinfo.h"
#include "optimizer/optimizer.h"
#include "optimizer/prep.h"
#include "optimizer/tlist.h"
#include "parser/parse_coerce.h"
#include "parser/parsetree.h"
#include "utils/lsyscache.h"
#include "utils/rel.h"
Include dependency graph for preptlist.c:

Go to the source code of this file.

Functions

static Listexpand_insert_targetlist (PlannerInfo *root, List *tlist, Relation rel)
 
void preprocess_targetlist (PlannerInfo *root)
 
Listextract_update_targetlist_colnos (List *tlist)
 
PlanRowMarkget_plan_rowmark (List *rowmarks, Index rtindex)
 

Function Documentation

◆ expand_insert_targetlist()

static List * expand_insert_targetlist ( PlannerInfo root,
List tlist,
Relation  rel 
)
static

Definition at line 382 of file preptlist.c.

383{
384 List *new_tlist = NIL;
385 ListCell *tlist_item;
386 int attrno,
387 numattrs;
388
389 tlist_item = list_head(tlist);
390
391 /*
392 * The rewriter should have already ensured that the TLEs are in correct
393 * order; but we have to insert TLEs for any missing attributes.
394 *
395 * Scan the tuple description in the relation's relcache entry to make
396 * sure we have all the user attributes in the right order.
397 */
398 numattrs = RelationGetNumberOfAttributes(rel);
399
400 for (attrno = 1; attrno <= numattrs; attrno++)
401 {
402 Form_pg_attribute att_tup = TupleDescAttr(rel->rd_att, attrno - 1);
403 TargetEntry *new_tle = NULL;
404
405 if (tlist_item != NULL)
406 {
407 TargetEntry *old_tle = (TargetEntry *) lfirst(tlist_item);
408
409 if (!old_tle->resjunk && old_tle->resno == attrno)
410 {
411 new_tle = old_tle;
412 tlist_item = lnext(tlist, tlist_item);
413 }
414 }
415
416 if (new_tle == NULL)
417 {
418 /*
419 * Didn't find a matching tlist entry, so make one.
420 *
421 * INSERTs should insert NULL in this case. (We assume the
422 * rewriter would have inserted any available non-NULL default
423 * value.) Also, normally we must apply any domain constraints
424 * that might exist --- this is to catch domain NOT 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 * Another exception is that if the column is generated, the value
436 * we produce here will be ignored, and we don't want to risk
437 * throwing an error. So in that case we *don't* want to apply
438 * domain constraints, so we must produce a NULL of the base type.
439 * Again, code comparing the finished plan to the target relation
440 * must account for this.
441 */
442 Node *new_expr;
443
444 if (att_tup->attisdropped)
445 {
446 /* Insert NULL for dropped column */
447 new_expr = (Node *) makeConst(INT4OID,
448 -1,
450 sizeof(int32),
451 (Datum) 0,
452 true, /* isnull */
453 true /* byval */ );
454 }
455 else if (att_tup->attgenerated)
456 {
457 /* Generated column, insert a NULL of the base type */
458 Oid baseTypeId = att_tup->atttypid;
459 int32 baseTypeMod = att_tup->atttypmod;
460
461 baseTypeId = getBaseTypeAndTypmod(baseTypeId, &baseTypeMod);
462 new_expr = (Node *) makeConst(baseTypeId,
463 baseTypeMod,
464 att_tup->attcollation,
465 att_tup->attlen,
466 (Datum) 0,
467 true, /* isnull */
468 att_tup->attbyval);
469 }
470 else
471 {
472 /* Normal column, insert a NULL of the column datatype */
473 new_expr = coerce_null_to_domain(att_tup->atttypid,
474 att_tup->atttypmod,
475 att_tup->attcollation,
476 att_tup->attlen,
477 att_tup->attbyval);
478 /* Must run expression preprocessing on any non-const nodes */
479 if (!IsA(new_expr, Const))
480 new_expr = eval_const_expressions(root, new_expr);
481 }
482
483 new_tle = makeTargetEntry((Expr *) new_expr,
484 attrno,
485 pstrdup(NameStr(att_tup->attname)),
486 false);
487 }
488
489 new_tlist = lappend(new_tlist, new_tle);
490 }
491
492 /*
493 * The remaining tlist entries should be resjunk; append them all to the
494 * end of the new tlist, making sure they have resnos higher than the last
495 * real attribute. (Note: although the rewriter already did such
496 * renumbering, we have to do it again here in case we added NULL entries
497 * above.)
498 */
499 while (tlist_item)
500 {
501 TargetEntry *old_tle = (TargetEntry *) lfirst(tlist_item);
502
503 if (!old_tle->resjunk)
504 elog(ERROR, "targetlist is not sorted correctly");
505 /* Get the resno right, but don't copy unnecessarily */
506 if (old_tle->resno != attrno)
507 {
508 old_tle = flatCopyTargetEntry(old_tle);
509 old_tle->resno = attrno;
510 }
511 new_tlist = lappend(new_tlist, old_tle);
512 attrno++;
513 tlist_item = lnext(tlist, tlist_item);
514 }
515
516 return new_tlist;
517}
#define NameStr(name)
Definition: c.h:717
int32_t int32
Definition: c.h:498
Node * eval_const_expressions(PlannerInfo *root, Node *node)
Definition: clauses.c:2256
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:225
List * lappend(List *list, void *datum)
Definition: list.c:339
Oid getBaseTypeAndTypmod(Oid typid, int32 *typmod)
Definition: lsyscache.c:2678
TargetEntry * makeTargetEntry(Expr *expr, AttrNumber resno, char *resname, bool resjunk)
Definition: makefuncs.c:289
Const * makeConst(Oid consttype, int32 consttypmod, Oid constcollid, int constlen, Datum constvalue, bool constisnull, bool constbyval)
Definition: makefuncs.c:350
TargetEntry * flatCopyTargetEntry(TargetEntry *src_tle)
Definition: makefuncs.c:322
char * pstrdup(const char *in)
Definition: mcxt.c:2327
#define IsA(nodeptr, _type_)
Definition: nodes.h:164
Node * coerce_null_to_domain(Oid typid, int32 typmod, Oid collation, int typlen, bool typbyval)
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:202
#define lfirst(lc)
Definition: pg_list.h:172
#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
uintptr_t Datum
Definition: postgres.h:69
#define InvalidOid
Definition: postgres_ext.h:35
unsigned int Oid
Definition: postgres_ext.h:30
tree ctl root
Definition: radixtree.h:1857
#define RelationGetNumberOfAttributes(relation)
Definition: rel.h:522
Definition: pg_list.h:54
Definition: nodes.h:135
TupleDesc rd_att
Definition: rel.h:112
AttrNumber resno
Definition: primnodes.h:2221
static FormData_pg_attribute * TupleDescAttr(TupleDesc tupdesc, int i)
Definition: tupdesc.h:160

References coerce_null_to_domain(), elog, ERROR, eval_const_expressions(), flatCopyTargetEntry(), getBaseTypeAndTypmod(), InvalidOid, IsA, lappend(), lfirst, list_head(), lnext(), makeConst(), makeTargetEntry(), NameStr, NIL, pstrdup(), RelationData::rd_att, RelationGetNumberOfAttributes, TargetEntry::resno, root, and TupleDescAttr().

Referenced by preprocess_targetlist().

◆ extract_update_targetlist_colnos()

List * extract_update_targetlist_colnos ( List tlist)

Definition at line 348 of file preptlist.c.

349{
350 List *update_colnos = NIL;
351 AttrNumber nextresno = 1;
352 ListCell *lc;
353
354 foreach(lc, tlist)
355 {
356 TargetEntry *tle = (TargetEntry *) lfirst(lc);
357
358 if (!tle->resjunk)
359 update_colnos = lappend_int(update_colnos, tle->resno);
360 tle->resno = nextresno++;
361 }
362 return update_colnos;
363}
int16 AttrNumber
Definition: attnum.h:21
List * lappend_int(List *list, int datum)
Definition: list.c:357

References lappend_int(), lfirst, NIL, and TargetEntry::resno.

Referenced by make_modifytable(), and preprocess_targetlist().

◆ get_plan_rowmark()

PlanRowMark * get_plan_rowmark ( List rowmarks,
Index  rtindex 
)

Definition at line 526 of file preptlist.c.

527{
528 ListCell *l;
529
530 foreach(l, rowmarks)
531 {
532 PlanRowMark *rc = (PlanRowMark *) lfirst(l);
533
534 if (rc->rti == rtindex)
535 return rc;
536 }
537 return NULL;
538}

References lfirst, and PlanRowMark::rti.

Referenced by check_index_predicates(), deparseLockingClause(), and expand_inherited_rtentry().

◆ preprocess_targetlist()

void preprocess_targetlist ( PlannerInfo root)

Definition at line 64 of file preptlist.c.

65{
66 Query *parse = root->parse;
67 int result_relation = parse->resultRelation;
68 List *range_table = parse->rtable;
69 CmdType command_type = parse->commandType;
70 RangeTblEntry *target_rte = NULL;
71 Relation target_relation = NULL;
72 List *tlist;
73 ListCell *lc;
74
75 /*
76 * If there is a result relation, open it so we can look for missing
77 * columns and so on. We assume that previous code already acquired at
78 * least AccessShareLock on the relation, so we need no lock here.
79 */
80 if (result_relation)
81 {
82 target_rte = rt_fetch(result_relation, range_table);
83
84 /*
85 * Sanity check: it'd better be a real relation not, say, a subquery.
86 * Else parser or rewriter messed up.
87 */
88 if (target_rte->rtekind != RTE_RELATION)
89 elog(ERROR, "result relation must be a regular relation");
90
91 target_relation = table_open(target_rte->relid, NoLock);
92 }
93 else
94 Assert(command_type == CMD_SELECT);
95
96 /*
97 * In an INSERT, the executor expects the targetlist to match the exact
98 * order of the target table's attributes, including entries for
99 * attributes not mentioned in the source query.
100 *
101 * In an UPDATE, we don't rearrange the tlist order, but we need to make a
102 * separate list of the target attribute numbers, in tlist order, and then
103 * renumber the processed_tlist entries to be consecutive.
104 */
105 tlist = parse->targetList;
106 if (command_type == CMD_INSERT)
107 tlist = expand_insert_targetlist(root, tlist, target_relation);
108 else if (command_type == CMD_UPDATE)
109 root->update_colnos = extract_update_targetlist_colnos(tlist);
110
111 /*
112 * For non-inherited UPDATE/DELETE/MERGE, register any junk column(s)
113 * needed to allow the executor to identify the rows to be updated or
114 * deleted. In the inheritance case, we do nothing now, leaving this to
115 * be dealt with when expand_inherited_rtentry() makes the leaf target
116 * relations. (But there might not be any leaf target relations, in which
117 * case we must do this in distribute_row_identity_vars().)
118 */
119 if ((command_type == CMD_UPDATE || command_type == CMD_DELETE ||
120 command_type == CMD_MERGE) &&
121 !target_rte->inh)
122 {
123 /* row-identity logic expects to add stuff to processed_tlist */
124 root->processed_tlist = tlist;
125 add_row_identity_columns(root, result_relation,
126 target_rte, target_relation);
127 tlist = root->processed_tlist;
128 }
129
130 /*
131 * For MERGE we also need to handle the target list for each INSERT and
132 * UPDATE action separately. In addition, we examine the qual of each
133 * action and add any Vars there (other than those of the target rel) to
134 * the subplan targetlist.
135 */
136 if (command_type == CMD_MERGE)
137 {
138 ListCell *l;
139 List *vars;
140
141 /*
142 * For MERGE, handle targetlist of each MergeAction separately. Give
143 * the same treatment to MergeAction->targetList as we would have
144 * given to a regular INSERT. For UPDATE, collect the column numbers
145 * being modified.
146 */
147 foreach(l, parse->mergeActionList)
148 {
150 ListCell *l2;
151
152 if (action->commandType == CMD_INSERT)
154 action->targetList,
155 target_relation);
156 else if (action->commandType == CMD_UPDATE)
157 action->updateColnos =
159
160 /*
161 * Add resjunk entries for any Vars and PlaceHolderVars used in
162 * each action's targetlist and WHEN condition that belong to
163 * relations other than the target. We don't expect to see any
164 * aggregates or window functions here.
165 */
167 list_concat_copy((List *) action->qual,
168 action->targetList),
170 foreach(l2, vars)
171 {
172 Var *var = (Var *) lfirst(l2);
173 TargetEntry *tle;
174
175 if (IsA(var, Var) && var->varno == result_relation)
176 continue; /* don't need it */
177
178 if (tlist_member((Expr *) var, tlist))
179 continue; /* already got it */
180
181 tle = makeTargetEntry((Expr *) var,
182 list_length(tlist) + 1,
183 NULL, true);
184 tlist = lappend(tlist, tle);
185 }
187 }
188
189 /*
190 * Add resjunk entries for any Vars and PlaceHolderVars used in the
191 * join condition that belong to relations other than the target. We
192 * don't expect to see any aggregates or window functions here.
193 */
194 vars = pull_var_clause(parse->mergeJoinCondition,
196 foreach(l, vars)
197 {
198 Var *var = (Var *) lfirst(l);
199 TargetEntry *tle;
200
201 if (IsA(var, Var) && var->varno == result_relation)
202 continue; /* don't need it */
203
204 if (tlist_member((Expr *) var, tlist))
205 continue; /* already got it */
206
207 tle = makeTargetEntry((Expr *) var,
208 list_length(tlist) + 1,
209 NULL, true);
210 tlist = lappend(tlist, tle);
211 }
212 }
213
214 /*
215 * Add necessary junk columns for rowmarked rels. These values are needed
216 * for locking of rels selected FOR UPDATE/SHARE, and to do EvalPlanQual
217 * rechecking. See comments for PlanRowMark in plannodes.h. If you
218 * change this stanza, see also expand_inherited_rtentry(), which has to
219 * be able to add on junk columns equivalent to these.
220 *
221 * (Someday it might be useful to fold these resjunk columns into the
222 * row-identity-column management used for UPDATE/DELETE. Today is not
223 * that day, however. One notable issue is that it seems important that
224 * the whole-row Vars made here use the real table rowtype, not RECORD, so
225 * that conversion to/from child relations' rowtypes will happen. Also,
226 * since these entries don't potentially bloat with more and more child
227 * relations, there's not really much need for column sharing.)
228 */
229 foreach(lc, root->rowMarks)
230 {
231 PlanRowMark *rc = (PlanRowMark *) lfirst(lc);
232 Var *var;
233 char resname[32];
234 TargetEntry *tle;
235
236 /* child rels use the same junk attrs as their parents */
237 if (rc->rti != rc->prti)
238 continue;
239
240 if (rc->allMarkTypes & ~(1 << ROW_MARK_COPY))
241 {
242 /* Need to fetch TID */
243 var = makeVar(rc->rti,
245 TIDOID,
246 -1,
248 0);
249 snprintf(resname, sizeof(resname), "ctid%u", rc->rowmarkId);
250 tle = makeTargetEntry((Expr *) var,
251 list_length(tlist) + 1,
252 pstrdup(resname),
253 true);
254 tlist = lappend(tlist, tle);
255 }
256 if (rc->allMarkTypes & (1 << ROW_MARK_COPY))
257 {
258 /* Need the whole row as a junk var */
259 var = makeWholeRowVar(rt_fetch(rc->rti, range_table),
260 rc->rti,
261 0,
262 false);
263 snprintf(resname, sizeof(resname), "wholerow%u", rc->rowmarkId);
264 tle = makeTargetEntry((Expr *) var,
265 list_length(tlist) + 1,
266 pstrdup(resname),
267 true);
268 tlist = lappend(tlist, tle);
269 }
270
271 /* If parent of inheritance tree, always fetch the tableoid too. */
272 if (rc->isParent)
273 {
274 var = makeVar(rc->rti,
276 OIDOID,
277 -1,
279 0);
280 snprintf(resname, sizeof(resname), "tableoid%u", rc->rowmarkId);
281 tle = makeTargetEntry((Expr *) var,
282 list_length(tlist) + 1,
283 pstrdup(resname),
284 true);
285 tlist = lappend(tlist, tle);
286 }
287 }
288
289 /*
290 * If the query has a RETURNING list, add resjunk entries for any Vars
291 * used in RETURNING that belong to other relations. We need to do this
292 * to make these Vars available for the RETURNING calculation. Vars that
293 * belong to the result rel don't need to be added, because they will be
294 * made to refer to the actual heap tuple.
295 */
296 if (parse->returningList && list_length(parse->rtable) > 1)
297 {
298 List *vars;
299 ListCell *l;
300
301 vars = pull_var_clause((Node *) parse->returningList,
305 foreach(l, vars)
306 {
307 Var *var = (Var *) lfirst(l);
308 TargetEntry *tle;
309
310 if (IsA(var, Var) &&
311 var->varno == result_relation)
312 continue; /* don't need it */
313
314 if (tlist_member((Expr *) var, tlist))
315 continue; /* already got it */
316
317 tle = makeTargetEntry((Expr *) var,
318 list_length(tlist) + 1,
319 NULL,
320 true);
321
322 tlist = lappend(tlist, tle);
323 }
325 }
326
327 root->processed_tlist = tlist;
328
329 if (target_relation)
330 table_close(target_relation, NoLock);
331}
void add_row_identity_columns(PlannerInfo *root, Index rtindex, RangeTblEntry *target_rte, Relation target_relation)
Definition: appendinfo.c:904
Assert(PointerIsAligned(start, uint64))
List * list_concat_copy(const List *list1, const List *list2)
Definition: list.c:598
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
CmdType
Definition: nodes.h:269
@ CMD_MERGE
Definition: nodes.h:275
@ CMD_INSERT
Definition: nodes.h:273
@ CMD_DELETE
Definition: nodes.h:274
@ CMD_UPDATE
Definition: nodes.h:272
@ CMD_SELECT
Definition: nodes.h:271
#define PVC_RECURSE_AGGREGATES
Definition: optimizer.h:193
#define PVC_RECURSE_WINDOWFUNCS
Definition: optimizer.h:195
#define PVC_INCLUDE_PLACEHOLDERS
Definition: optimizer.h:196
@ RTE_RELATION
Definition: parsenodes.h:1026
#define rt_fetch(rangetable_index, rangetable)
Definition: parsetree.h:31
static int list_length(const List *l)
Definition: pg_list.h:152
@ ROW_MARK_COPY
Definition: plannodes.h:1491
#define snprintf
Definition: port.h:239
List * extract_update_targetlist_colnos(List *tlist)
Definition: preptlist.c:348
static List * expand_insert_targetlist(PlannerInfo *root, List *tlist, Relation rel)
Definition: preptlist.c:382
static struct subre * parse(struct vars *v, int stopper, int type, struct state *init, struct state *final)
Definition: regcomp.c:717
Index prti
Definition: plannodes.h:1542
bool isParent
Definition: plannodes.h:1554
Index rowmarkId
Definition: plannodes.h:1544
int allMarkTypes
Definition: plannodes.h:1548
RTEKind rtekind
Definition: parsenodes.h:1061
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
List * pull_var_clause(Node *node, int flags)
Definition: var.c:653

References generate_unaccent_rules::action, add_row_identity_columns(), PlanRowMark::allMarkTypes, Assert(), CMD_DELETE, CMD_INSERT, CMD_MERGE, CMD_SELECT, CMD_UPDATE, elog, ERROR, expand_insert_targetlist(), extract_update_targetlist_colnos(), RangeTblEntry::inh, InvalidOid, IsA, PlanRowMark::isParent, lappend(), lfirst, list_concat_copy(), list_free(), list_length(), makeTargetEntry(), makeVar(), makeWholeRowVar(), NoLock, parse(), PlanRowMark::prti, pstrdup(), pull_var_clause(), PVC_INCLUDE_PLACEHOLDERS, PVC_RECURSE_AGGREGATES, PVC_RECURSE_WINDOWFUNCS, root, ROW_MARK_COPY, PlanRowMark::rowmarkId, rt_fetch, RTE_RELATION, RangeTblEntry::rtekind, PlanRowMark::rti, SelfItemPointerAttributeNumber, snprintf, table_close(), table_open(), TableOidAttributeNumber, tlist_member(), and Var::varno.

Referenced by grouping_planner().