1 /*-------------------------------------------------------------------------
4 * Routines to preprocess the parse tree target list
6 * For INSERT and UPDATE queries, the targetlist must contain an entry for
7 * each attribute of the target relation in the correct order. For UPDATE and
8 * DELETE queries, it must also contain junk tlist entries needed to allow the
9 * executor to identify the rows to be updated or deleted. For all query
10 * types, we may need to add junk tlist entries for Vars used in the RETURNING
11 * list and row ID information needed for SELECT FOR UPDATE locking and/or
12 * EvalPlanQual checking.
14 * The query rewrite phase also does preprocessing of the targetlist (see
15 * rewriteTargetListIU). The division of labor between here and there is
16 * partially historical, but it's not entirely arbitrary. In particular,
17 * consider an UPDATE across an inheritance tree. What rewriteTargetListIU
18 * does need be done only once (because it depends only on the properties of
19 * the parent relation). What's done here has to be done over again for each
20 * child relation, because it depends on the properties of the child, which
21 * might be of a different relation type, or have more columns and/or a
22 * different column order than the parent.
24 * The fact that rewriteTargetListIU sorts non-resjunk tlist entries by column
25 * position, which expand_targetlist depends on, violates the above comment
26 * because the sorting is only valid for the parent relation. In inherited
27 * UPDATE cases, adjust_inherited_tlist runs in between to take care of fixing
28 * the tlists for child tables to keep expand_targetlist happy. We do it like
29 * that because it's faster in typical non-inherited cases.
32 * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
33 * Portions Copyright (c) 1994, Regents of the University of California
36 * src/backend/optimizer/prep/preptlist.c
38 *-------------------------------------------------------------------------
43 #include "access/sysattr.h"
44 #include "access/table.h"
45 #include "catalog/pg_type.h"
46 #include "nodes/makefuncs.h"
47 #include "optimizer/prep.h"
48 #include "optimizer/tlist.h"
49 #include "optimizer/var.h"
50 #include "parser/parsetree.h"
51 #include "parser/parse_coerce.h"
52 #include "rewrite/rewriteHandler.h"
53 #include "utils/rel.h"
56 static List *expand_targetlist(List *tlist, int command_type,
57 Index result_relation, Relation rel);
61 * preprocess_targetlist
62 * Driver for preprocessing the parse tree targetlist.
64 * Returns the new targetlist.
66 * As a side effect, if there's an ON CONFLICT UPDATE clause, its targetlist
67 * is also preprocessed (and updated in-place).
70 preprocess_targetlist(PlannerInfo *root)
72 Query *parse = root->parse;
73 int result_relation = parse->resultRelation;
74 List *range_table = parse->rtable;
75 CmdType command_type = parse->commandType;
76 RangeTblEntry *target_rte = NULL;
77 Relation target_relation = NULL;
82 * If there is a result relation, open it so we can look for missing
83 * columns and so on. We assume that previous code already acquired at
84 * least AccessShareLock on the relation, so we need no lock here.
88 target_rte = rt_fetch(result_relation, range_table);
91 * Sanity check: it'd better be a real relation not, say, a subquery.
92 * Else parser or rewriter messed up.
94 if (target_rte->rtekind != RTE_RELATION)
95 elog(ERROR, "result relation must be a regular relation");
97 target_relation = heap_open(target_rte->relid, NoLock);
100 Assert(command_type == CMD_SELECT);
103 * For UPDATE/DELETE, add any junk column(s) needed to allow the executor
104 * to identify the rows to be updated or deleted. Note that this step
105 * scribbles on parse->targetList, which is not very desirable, but we
106 * keep it that way to avoid changing APIs used by FDWs.
108 if (command_type == CMD_UPDATE || command_type == CMD_DELETE)
109 rewriteTargetListUD(parse, target_rte, target_relation);
112 * for heap_form_tuple to work, the targetlist must match the exact order
113 * of the attributes. We also need to fill in any missing attributes. -ay
116 tlist = parse->targetList;
117 if (command_type == CMD_INSERT || command_type == CMD_UPDATE)
118 tlist = expand_targetlist(tlist, command_type,
119 result_relation, target_relation);
122 * Add necessary junk columns for rowmarked rels. These values are needed
123 * for locking of rels selected FOR UPDATE/SHARE, and to do EvalPlanQual
124 * rechecking. See comments for PlanRowMark in plannodes.h.
126 foreach(lc, root->rowMarks)
128 PlanRowMark *rc = (PlanRowMark *) lfirst(lc);
133 /* child rels use the same junk attrs as their parents */
134 if (rc->rti != rc->prti)
137 if (rc->allMarkTypes & ~(1 << ROW_MARK_COPY))
139 /* Need to fetch TID */
140 var = makeVar(rc->rti,
141 SelfItemPointerAttributeNumber,
146 snprintf(resname, sizeof(resname), "ctid%u", rc->rowmarkId);
147 tle = makeTargetEntry((Expr *) var,
148 list_length(tlist) + 1,
151 tlist = lappend(tlist, tle);
153 if (rc->allMarkTypes & (1 << ROW_MARK_COPY))
155 /* Need the whole row as a junk var */
156 var = makeWholeRowVar(rt_fetch(rc->rti, range_table),
160 snprintf(resname, sizeof(resname), "wholerow%u", rc->rowmarkId);
161 tle = makeTargetEntry((Expr *) var,
162 list_length(tlist) + 1,
165 tlist = lappend(tlist, tle);
168 /* If parent of inheritance tree, always fetch the tableoid too. */
171 var = makeVar(rc->rti,
172 TableOidAttributeNumber,
177 snprintf(resname, sizeof(resname), "tableoid%u", rc->rowmarkId);
178 tle = makeTargetEntry((Expr *) var,
179 list_length(tlist) + 1,
182 tlist = lappend(tlist, tle);
187 * If the query has a RETURNING list, add resjunk entries for any Vars
188 * used in RETURNING that belong to other relations. We need to do this
189 * to make these Vars available for the RETURNING calculation. Vars that
190 * belong to the result rel don't need to be added, because they will be
191 * made to refer to the actual heap tuple.
193 if (parse->returningList && list_length(parse->rtable) > 1)
198 vars = pull_var_clause((Node *) parse->returningList,
199 PVC_RECURSE_AGGREGATES |
200 PVC_RECURSE_WINDOWFUNCS |
201 PVC_INCLUDE_PLACEHOLDERS);
204 Var *var = (Var *) lfirst(l);
208 var->varno == result_relation)
209 continue; /* don't need it */
211 if (tlist_member((Expr *) var, tlist))
212 continue; /* already got it */
214 tle = makeTargetEntry((Expr *) var,
215 list_length(tlist) + 1,
219 tlist = lappend(tlist, tle);
225 * If there's an ON CONFLICT UPDATE clause, preprocess its targetlist too
226 * while we have the relation open.
228 if (parse->onConflict)
229 parse->onConflict->onConflictSet =
230 expand_targetlist(parse->onConflict->onConflictSet,
236 heap_close(target_relation, NoLock);
242 /*****************************************************************************
244 * TARGETLIST EXPANSION
246 *****************************************************************************/
250 * Given a target list as generated by the parser and a result relation,
251 * add targetlist entries for any missing attributes, and ensure the
252 * non-junk attributes appear in proper field order.
255 expand_targetlist(List *tlist, int command_type,
256 Index result_relation, Relation rel)
258 List *new_tlist = NIL;
259 ListCell *tlist_item;
263 tlist_item = list_head(tlist);
266 * The rewriter should have already ensured that the TLEs are in correct
267 * order; but we have to insert TLEs for any missing attributes.
269 * Scan the tuple description in the relation's relcache entry to make
270 * sure we have all the user attributes in the right order.
272 numattrs = RelationGetNumberOfAttributes(rel);
274 for (attrno = 1; attrno <= numattrs; attrno++)
276 Form_pg_attribute att_tup = TupleDescAttr(rel->rd_att, attrno - 1);
277 TargetEntry *new_tle = NULL;
279 if (tlist_item != NULL)
281 TargetEntry *old_tle = (TargetEntry *) lfirst(tlist_item);
283 if (!old_tle->resjunk && old_tle->resno == attrno)
286 tlist_item = lnext(tlist_item);
293 * Didn't find a matching tlist entry, so make one.
295 * For INSERT, generate a NULL constant. (We assume the rewriter
296 * would have inserted any available default value.) Also, if the
297 * column isn't dropped, apply any domain constraints that might
298 * exist --- this is to catch domain NOT NULL.
300 * For UPDATE, generate a Var reference to the existing value of
301 * the attribute, so that it gets copied to the new tuple. But
302 * generate a NULL for dropped columns (we want to drop any old
305 * When generating a NULL constant for a dropped column, we label
306 * it INT4 (any other guaranteed-to-exist datatype would do as
307 * well). We can't label it with the dropped column's datatype
308 * since that might not exist anymore. It does not really matter
309 * what we claim the type is, since NULL is NULL --- its
310 * representation is datatype-independent. This could perhaps
311 * confuse code comparing the finished plan to the target
314 Oid atttype = att_tup->atttypid;
315 int32 atttypmod = att_tup->atttypmod;
316 Oid attcollation = att_tup->attcollation;
319 switch (command_type)
322 if (!att_tup->attisdropped)
324 new_expr = (Node *) makeConst(atttype,
331 new_expr = coerce_to_domain(new_expr,
335 COERCE_IMPLICIT_CAST,
341 /* Insert NULL for dropped column */
342 new_expr = (Node *) makeConst(INT4OID,
352 if (!att_tup->attisdropped)
354 new_expr = (Node *) makeVar(result_relation,
363 /* Insert NULL for dropped column */
364 new_expr = (Node *) makeConst(INT4OID,
374 elog(ERROR, "unrecognized command_type: %d",
376 new_expr = NULL; /* keep compiler quiet */
380 new_tle = makeTargetEntry((Expr *) new_expr,
382 pstrdup(NameStr(att_tup->attname)),
386 new_tlist = lappend(new_tlist, new_tle);
390 * The remaining tlist entries should be resjunk; append them all to the
391 * end of the new tlist, making sure they have resnos higher than the last
392 * real attribute. (Note: although the rewriter already did such
393 * renumbering, we have to do it again here in case we are doing an UPDATE
394 * in a table with dropped columns, or an inheritance child table with
399 TargetEntry *old_tle = (TargetEntry *) lfirst(tlist_item);
401 if (!old_tle->resjunk)
402 elog(ERROR, "targetlist is not sorted correctly");
403 /* Get the resno right, but don't copy unnecessarily */
404 if (old_tle->resno != attrno)
406 old_tle = flatCopyTargetEntry(old_tle);
407 old_tle->resno = attrno;
409 new_tlist = lappend(new_tlist, old_tle);
411 tlist_item = lnext(tlist_item);
419 * Locate PlanRowMark for given RT index, or return NULL if none
421 * This probably ought to be elsewhere, but there's no very good place
424 get_plan_rowmark(List *rowmarks, Index rtindex)
430 PlanRowMark *rc = (PlanRowMark *) lfirst(l);
432 if (rc->rti == rtindex)