Age Owner Branch data TLA 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-2025, 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 *
2431 alvherre@alvh.no-ip. 51 :CBC 20362 : make_append_rel_info(Relation parentrel, Relation childrel,
52 : : Index parentRTindex, Index childRTindex)
53 : : {
54 : 20362 : AppendRelInfo *appinfo = makeNode(AppendRelInfo);
55 : :
56 : 20362 : appinfo->parent_relid = parentRTindex;
57 : 20362 : appinfo->child_relid = childRTindex;
58 : 20362 : appinfo->parent_reltype = parentrel->rd_rel->reltype;
59 : 20362 : appinfo->child_reltype = childrel->rd_rel->reltype;
2105 tgl@sss.pgh.pa.us 60 : 20362 : make_inh_translation_list(parentrel, childrel, childRTindex, appinfo);
2431 alvherre@alvh.no-ip. 61 : 20361 : appinfo->parent_reloid = RelationGetRelid(parentrel);
62 : :
63 : 20361 : return appinfo;
64 : : }
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 : 20362 : make_inh_translation_list(Relation oldrelation, Relation newrelation,
81 : : Index newvarno,
82 : : AppendRelInfo *appinfo)
83 : : {
84 : 20362 : List *vars = NIL;
85 : : AttrNumber *pcolnos;
86 : 20362 : TupleDesc old_tupdesc = RelationGetDescr(oldrelation);
87 : 20362 : TupleDesc new_tupdesc = RelationGetDescr(newrelation);
88 : 20362 : Oid new_relid = RelationGetRelid(newrelation);
89 : 20362 : int oldnatts = old_tupdesc->natts;
90 : 20362 : int newnatts = new_tupdesc->natts;
91 : : int old_attno;
92 : 20362 : int new_attno = 0;
93 : :
94 : : /* Initialize reverse-translation array with all entries zero */
2105 tgl@sss.pgh.pa.us 95 : 20362 : appinfo->num_child_cols = newnatts;
96 : 20362 : appinfo->parent_colnos = pcolnos =
97 : 20362 : (AttrNumber *) palloc0(newnatts * sizeof(AttrNumber));
98 : :
2431 alvherre@alvh.no-ip. 99 [ + + ]: 75718 : for (old_attno = 0; old_attno < oldnatts; old_attno++)
100 : : {
101 : : Form_pg_attribute att;
102 : : char *attname;
103 : : Oid atttypid;
104 : : int32 atttypmod;
105 : : Oid attcollation;
106 : :
107 : 55357 : att = TupleDescAttr(old_tupdesc, old_attno);
108 [ + + ]: 55357 : if (att->attisdropped)
109 : : {
110 : : /* Just put NULL into this list entry */
111 : 1566 : vars = lappend(vars, NULL);
112 : 1566 : continue;
113 : : }
114 : 53791 : attname = NameStr(att->attname);
115 : 53791 : atttypid = att->atttypid;
116 : 53791 : atttypmod = att->atttypmod;
117 : 53791 : 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 [ + + ]: 53791 : if (oldrelation == newrelation)
124 : : {
125 : 3430 : vars = lappend(vars, makeVar(newvarno,
126 : 3430 : (AttrNumber) (old_attno + 1),
127 : : atttypid,
128 : : atttypmod,
129 : : attcollation,
130 : : 0));
2105 tgl@sss.pgh.pa.us 131 : 3430 : pcolnos[old_attno] = old_attno + 1;
2431 alvherre@alvh.no-ip. 132 : 3430 : 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 [ + + ]: 50361 : if (new_attno >= newnatts ||
145 [ + + ]: 49232 : (att = TupleDescAttr(new_tupdesc, new_attno))->attisdropped ||
146 [ + + ]: 48762 : strcmp(attname, NameStr(att->attname)) != 0)
147 : : {
148 : : HeapTuple newtup;
149 : :
150 : 4193 : newtup = SearchSysCacheAttName(new_relid, attname);
2316 tgl@sss.pgh.pa.us 151 [ - + ]: 4193 : if (!HeapTupleIsValid(newtup))
2431 alvherre@alvh.no-ip. 152 [ # # ]:UBC 0 : elog(ERROR, "could not find inherited attribute \"%s\" of relation \"%s\"",
153 : : attname, RelationGetRelationName(newrelation));
2431 alvherre@alvh.no-ip. 154 :CBC 4193 : new_attno = ((Form_pg_attribute) GETSTRUCT(newtup))->attnum - 1;
2105 tgl@sss.pgh.pa.us 155 [ + - - + ]: 4193 : Assert(new_attno >= 0 && new_attno < newnatts);
2431 alvherre@alvh.no-ip. 156 : 4193 : ReleaseSysCache(newtup);
157 : :
158 : 4193 : att = TupleDescAttr(new_tupdesc, new_attno);
159 : : }
160 : :
161 : : /* Found it, check type and collation match */
162 [ + + - + ]: 50361 : if (atttypid != att->atttypid || atttypmod != att->atttypmod)
429 michael@paquier.xyz 163 [ + - ]: 1 : 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))));
2431 alvherre@alvh.no-ip. 167 [ - + ]: 50360 : if (attcollation != att->attcollation)
429 michael@paquier.xyz 168 [ # # ]:UBC 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 : :
2431 alvherre@alvh.no-ip. 173 :CBC 50360 : vars = lappend(vars, makeVar(newvarno,
174 : 50360 : (AttrNumber) (new_attno + 1),
175 : : atttypid,
176 : : atttypmod,
177 : : attcollation,
178 : : 0));
2105 tgl@sss.pgh.pa.us 179 : 50360 : pcolnos[new_attno] = old_attno + 1;
2431 alvherre@alvh.no-ip. 180 : 50360 : new_attno++;
181 : : }
182 : :
2105 tgl@sss.pgh.pa.us 183 : 20361 : appinfo->translated_vars = vars;
2431 alvherre@alvh.no-ip. 184 : 20361 : }
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 : 103975 : adjust_appendrel_attrs(PlannerInfo *root, Node *node, int nappinfos,
201 : : AppendRelInfo **appinfos)
202 : : {
203 : : adjust_appendrel_attrs_context context;
204 : :
205 : 103975 : context.root = root;
206 : 103975 : context.nappinfos = nappinfos;
207 : 103975 : context.appinfos = appinfos;
208 : :
209 : : /* If there's nothing to adjust, don't call this function. */
210 [ + - - + ]: 103975 : Assert(nappinfos >= 1 && appinfos != NULL);
211 : :
212 : : /* Should never be translating a Query tree. */
1620 tgl@sss.pgh.pa.us 213 [ + + - + ]: 103975 : Assert(node == NULL || !IsA(node, Query));
214 : :
215 : 103975 : return adjust_appendrel_attrs_mutator(node, &context);
216 : : }
217 : :
218 : : static Node *
2431 alvherre@alvh.no-ip. 219 : 419994 : adjust_appendrel_attrs_mutator(Node *node,
220 : : adjust_appendrel_attrs_context *context)
221 : : {
222 : 419994 : AppendRelInfo **appinfos = context->appinfos;
223 : 419994 : int nappinfos = context->nappinfos;
224 : : int cnt;
225 : :
226 [ + + ]: 419994 : if (node == NULL)
227 : 39031 : return NULL;
228 [ + + ]: 380963 : if (IsA(node, Var))
229 : : {
230 : 176627 : Var *var = (Var *) copyObject(node);
231 : 176627 : AppendRelInfo *appinfo = NULL;
232 : :
2067 tgl@sss.pgh.pa.us 233 [ - + ]: 176627 : if (var->varlevelsup != 0)
2067 tgl@sss.pgh.pa.us 234 :UBC 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 : :
2431 alvherre@alvh.no-ip. 265 [ + + ]:CBC 215677 : for (cnt = 0; cnt < nappinfos; cnt++)
266 : : {
267 [ + + ]: 195222 : if (var->varno == appinfos[cnt]->parent_relid)
268 : : {
269 : 156172 : appinfo = appinfos[cnt];
270 : 156172 : break;
271 : : }
272 : : }
273 : :
2067 tgl@sss.pgh.pa.us 274 [ + + ]: 176627 : if (appinfo)
275 : : {
2431 alvherre@alvh.no-ip. 276 : 156172 : var->varno = appinfo->child_relid;
277 : : /* it's now a generated Var, so drop any syntactic labeling */
2067 tgl@sss.pgh.pa.us 278 : 156172 : var->varnosyn = 0;
279 : 156172 : var->varattnosyn = 0;
2431 alvherre@alvh.no-ip. 280 [ + + ]: 156172 : if (var->varattno > 0)
281 : : {
282 : : Node *newnode;
283 : :
284 [ - + ]: 147181 : if (var->varattno > list_length(appinfo->translated_vars))
2431 alvherre@alvh.no-ip. 285 [ # # ]:UBC 0 : elog(ERROR, "attribute %d of relation \"%s\" does not exist",
286 : : var->varattno, get_rel_name(appinfo->parent_reloid));
2431 alvherre@alvh.no-ip. 287 :CBC 147181 : newnode = copyObject(list_nth(appinfo->translated_vars,
288 : : var->varattno - 1));
289 [ - + ]: 147181 : if (newnode == NULL)
2431 alvherre@alvh.no-ip. 290 [ # # ]:UBC 0 : elog(ERROR, "attribute %d of relation \"%s\" does not exist",
291 : : var->varattno, get_rel_name(appinfo->parent_reloid));
950 tgl@sss.pgh.pa.us 292 [ + + ]:CBC 147181 : if (IsA(newnode, Var))
293 : : {
233 dean.a.rasheed@gmail 294 : 142745 : ((Var *) newnode)->varreturningtype = var->varreturningtype;
950 tgl@sss.pgh.pa.us 295 : 142745 : ((Var *) newnode)->varnullingrels = var->varnullingrels;
296 : : }
297 : : else
298 : : {
233 dean.a.rasheed@gmail 299 [ - + ]: 4436 : if (var->varreturningtype != VAR_RETURNING_DEFAULT)
233 dean.a.rasheed@gmail 300 [ # # ]:UBC 0 : elog(ERROR, "failed to apply returningtype to a non-Var");
233 dean.a.rasheed@gmail 301 [ - + ]:CBC 4436 : if (var->varnullingrels != NULL)
233 dean.a.rasheed@gmail 302 [ # # ]:UBC 0 : elog(ERROR, "failed to apply nullingrels to a non-Var");
303 : : }
2431 alvherre@alvh.no-ip. 304 :CBC 147181 : return newnode;
305 : : }
306 [ + + ]: 8991 : 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 [ + + ]: 545 : if (OidIsValid(appinfo->child_reltype))
315 : : {
316 [ - + ]: 517 : Assert(var->vartype == appinfo->parent_reltype);
317 [ + + ]: 517 : if (appinfo->parent_reltype != appinfo->child_reltype)
318 : : {
319 : 421 : ConvertRowtypeExpr *r = makeNode(ConvertRowtypeExpr);
320 : :
321 : 421 : r->arg = (Expr *) var;
322 : 421 : r->resulttype = appinfo->parent_reltype;
323 : 421 : r->convertformat = COERCE_IMPLICIT_CAST;
324 : 421 : r->location = -1;
325 : : /* Make sure the Var node has the right type ID, too */
326 : 421 : var->vartype = appinfo->child_reltype;
327 : 421 : return (Node *) r;
328 : : }
329 : : }
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 : : RowExpr *rowexpr;
344 : : List *fields;
345 : : RangeTblEntry *rte;
346 : :
347 : 28 : rte = rt_fetch(appinfo->parent_relid,
348 : : context->root->parse->rtable);
349 : 28 : fields = copyObject(appinfo->translated_vars);
350 : 28 : rowexpr = makeNode(RowExpr);
351 : 28 : rowexpr->args = fields;
352 : 28 : rowexpr->row_typeid = var->vartype;
353 : 28 : rowexpr->row_format = COERCE_IMPLICIT_CAST;
354 : 28 : rowexpr->colnames = copyObject(rte->eref->colnames);
355 : 28 : rowexpr->location = -1;
356 : :
233 dean.a.rasheed@gmail 357 [ - + ]: 28 : if (var->varreturningtype != VAR_RETURNING_DEFAULT)
233 dean.a.rasheed@gmail 358 [ # # ]:UBC 0 : elog(ERROR, "failed to apply returningtype to a non-Var");
950 tgl@sss.pgh.pa.us 359 [ - + ]:CBC 28 : if (var->varnullingrels != NULL)
950 tgl@sss.pgh.pa.us 360 [ # # ]:UBC 0 : elog(ERROR, "failed to apply nullingrels to a non-Var");
361 : :
2431 alvherre@alvh.no-ip. 362 :CBC 28 : return (Node *) rowexpr;
363 : : }
364 : : }
365 : : /* system attributes don't need any other translation */
366 : : }
1620 tgl@sss.pgh.pa.us 367 [ + + ]: 20455 : 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 : 9627 : Relids leaf_result_relids = context->root->leaf_result_relids;
378 : 9627 : Index leaf_relid = 0;
379 : :
380 [ + + ]: 19254 : for (cnt = 0; cnt < nappinfos; cnt++)
381 : : {
382 [ + + ]: 9627 : if (bms_is_member(appinfos[cnt]->child_relid,
383 : : leaf_result_relids))
384 : : {
385 [ - + ]: 8537 : if (leaf_relid)
1620 tgl@sss.pgh.pa.us 386 [ # # ]:UBC 0 : elog(ERROR, "cannot translate to multiple leaf relids");
1620 tgl@sss.pgh.pa.us 387 :CBC 8537 : leaf_relid = appinfos[cnt]->child_relid;
388 : : }
389 : : }
390 : :
391 [ + + ]: 9627 : if (leaf_relid)
392 : : {
393 : : RowIdentityVarInfo *ridinfo = (RowIdentityVarInfo *)
841 394 : 8537 : list_nth(context->root->row_identity_vars, var->varattno - 1);
395 : :
1620 396 [ + + ]: 8537 : if (bms_is_member(leaf_relid, ridinfo->rowidrels))
397 : : {
398 : : /* Substitute the Var given in the RowIdentityVarInfo */
399 : 8491 : var = copyObject(ridinfo->rowidvar);
400 : : /* ... but use the correct relid */
401 : 8491 : var->varno = leaf_relid;
402 : : /* identity vars shouldn't have nulling rels */
950 403 [ - + ]: 8491 : Assert(var->varnullingrels == NULL);
404 : : /* varnosyn in the RowIdentityVarInfo is probably wrong */
1620 405 : 8491 : var->varnosyn = 0;
406 : 8491 : var->varattnosyn = 0;
407 : : }
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 : 46 : return (Node *) makeNullConst(var->vartype,
415 : : var->vartypmod,
416 : : var->varcollid);
417 : : }
418 : : }
419 : : }
2431 alvherre@alvh.no-ip. 420 : 28951 : return (Node *) var;
421 : : }
422 [ + + ]: 204336 : if (IsA(node, CurrentOfExpr))
423 : : {
424 : 92 : CurrentOfExpr *cexpr = (CurrentOfExpr *) copyObject(node);
425 : :
426 [ + - ]: 92 : for (cnt = 0; cnt < nappinfos; cnt++)
427 : : {
428 : 92 : AppendRelInfo *appinfo = appinfos[cnt];
429 : :
430 [ + - ]: 92 : if (cexpr->cvarno == appinfo->parent_relid)
431 : : {
432 : 92 : cexpr->cvarno = appinfo->child_relid;
433 : 92 : break;
434 : : }
435 : : }
436 : 92 : return (Node *) cexpr;
437 : : }
438 [ + + ]: 204244 : if (IsA(node, PlaceHolderVar))
439 : : {
440 : : /* Copy the PlaceHolderVar node with correct mutation of subnodes */
441 : : PlaceHolderVar *phv;
442 : :
443 : 1404 : phv = (PlaceHolderVar *) expression_tree_mutator(node,
444 : : adjust_appendrel_attrs_mutator,
445 : : context);
446 : : /* now fix PlaceHolderVar's relid sets */
447 [ + - ]: 1404 : if (phv->phlevelsup == 0)
448 : : {
950 tgl@sss.pgh.pa.us 449 : 1404 : phv->phrels = adjust_child_relids(phv->phrels,
450 : : nappinfos, appinfos);
451 : : /* as above, we needn't touch phnullingrels */
452 : : }
2431 alvherre@alvh.no-ip. 453 : 1404 : return (Node *) phv;
454 : : }
455 : : /* Shouldn't need to handle planner auxiliary nodes here */
456 [ - + ]: 202840 : Assert(!IsA(node, SpecialJoinInfo));
457 [ - + ]: 202840 : Assert(!IsA(node, AppendRelInfo));
458 [ - + ]: 202840 : Assert(!IsA(node, PlaceHolderInfo));
459 [ - + ]: 202840 : 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 [ + + ]: 202840 : if (IsA(node, RestrictInfo))
467 : : {
468 : 12799 : RestrictInfo *oldinfo = (RestrictInfo *) node;
469 : 12799 : RestrictInfo *newinfo = makeNode(RestrictInfo);
470 : :
471 : : /* Copy all flat-copiable fields, notably including rinfo_serial */
472 : 12799 : memcpy(newinfo, oldinfo, sizeof(RestrictInfo));
473 : :
474 : : /* Recursively fix the clause itself */
475 : 12799 : newinfo->clause = (Expr *)
476 : 12799 : adjust_appendrel_attrs_mutator((Node *) oldinfo->clause, context);
477 : :
478 : : /* and the modified version, if an OR clause */
479 : 12799 : newinfo->orclause = (Expr *)
480 : 12799 : adjust_appendrel_attrs_mutator((Node *) oldinfo->orclause, context);
481 : :
482 : : /* adjust relid sets too */
483 : 12799 : newinfo->clause_relids = adjust_child_relids(oldinfo->clause_relids,
484 : : context->nappinfos,
485 : : context->appinfos);
486 : 12799 : newinfo->required_relids = adjust_child_relids(oldinfo->required_relids,
487 : : context->nappinfos,
488 : : context->appinfos);
489 : 12799 : newinfo->outer_relids = adjust_child_relids(oldinfo->outer_relids,
490 : : context->nappinfos,
491 : : context->appinfos);
492 : 12799 : newinfo->left_relids = adjust_child_relids(oldinfo->left_relids,
493 : : context->nappinfos,
494 : : context->appinfos);
495 : 12799 : newinfo->right_relids = adjust_child_relids(oldinfo->right_relids,
496 : : context->nappinfos,
497 : : 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 : 12799 : newinfo->eval_cost.startup = -1;
506 : 12799 : newinfo->norm_selec = -1;
507 : 12799 : newinfo->outer_selec = -1;
508 : 12799 : newinfo->left_em = NULL;
509 : 12799 : newinfo->right_em = NULL;
510 : 12799 : newinfo->scansel_cache = NIL;
511 : 12799 : newinfo->left_bucketsize = -1;
512 : 12799 : newinfo->right_bucketsize = -1;
513 : 12799 : newinfo->left_mcvfreq = -1;
514 : 12799 : newinfo->right_mcvfreq = -1;
515 : :
516 : 12799 : return (Node *) newinfo;
517 : : }
518 : :
519 : : /*
520 : : * NOTE: we do not need to recurse into sublinks, because they should
521 : : * already have been converted to subplans before we see them.
522 : : */
523 [ - + ]: 190041 : Assert(!IsA(node, SubLink));
524 [ - + ]: 190041 : Assert(!IsA(node, Query));
525 : : /* We should never see these Query substructures, either. */
1620 tgl@sss.pgh.pa.us 526 [ - + ]: 190041 : Assert(!IsA(node, RangeTblRef));
527 [ - + ]: 190041 : Assert(!IsA(node, JoinExpr));
528 : :
282 peter@eisentraut.org 529 : 190041 : return expression_tree_mutator(node, adjust_appendrel_attrs_mutator, context);
530 : : }
531 : :
532 : : /*
533 : : * adjust_appendrel_attrs_multilevel
534 : : * Apply Var translations from an appendrel parent down to a child.
535 : : *
536 : : * Replace Vars in the "node" expression that reference "parentrel" with
537 : : * the appropriate Vars for "childrel". childrel can be more than one
538 : : * inheritance level removed from parentrel.
539 : : */
540 : : Node *
2425 alvherre@alvh.no-ip. 541 : 13857 : adjust_appendrel_attrs_multilevel(PlannerInfo *root, Node *node,
542 : : RelOptInfo *childrel,
543 : : RelOptInfo *parentrel)
544 : : {
545 : : AppendRelInfo **appinfos;
546 : : int nappinfos;
547 : :
548 : : /* Recurse if immediate parent is not the top parent. */
1115 tgl@sss.pgh.pa.us 549 [ + + ]: 13857 : if (childrel->parent != parentrel)
550 : : {
551 [ + - ]: 5192 : if (childrel->parent)
552 : 5192 : node = adjust_appendrel_attrs_multilevel(root, node,
553 : 5192 : childrel->parent,
554 : : parentrel);
555 : : else
1115 tgl@sss.pgh.pa.us 556 [ # # ]:UBC 0 : elog(ERROR, "childrel is not a child of parentrel");
557 : : }
558 : :
559 : : /* Now translate for this child. */
1115 tgl@sss.pgh.pa.us 560 :CBC 13857 : appinfos = find_appinfos_by_relids(root, childrel->relids, &nappinfos);
561 : :
2425 alvherre@alvh.no-ip. 562 : 13857 : node = adjust_appendrel_attrs(root, node, nappinfos, appinfos);
563 : :
564 : 13857 : pfree(appinfos);
565 : :
566 : 13857 : return node;
567 : : }
568 : :
569 : : /*
570 : : * Substitute child relids for parent relids in a Relid set. The array of
571 : : * appinfos specifies the substitutions to be performed.
572 : : */
573 : : Relids
2431 574 : 77642 : adjust_child_relids(Relids relids, int nappinfos, AppendRelInfo **appinfos)
575 : : {
576 : 77642 : Bitmapset *result = NULL;
577 : : int cnt;
578 : :
579 [ + + ]: 198686 : for (cnt = 0; cnt < nappinfos; cnt++)
580 : : {
581 : 121044 : AppendRelInfo *appinfo = appinfos[cnt];
582 : :
583 : : /* Remove parent, add child */
584 [ + + ]: 121044 : if (bms_is_member(appinfo->parent_relid, relids))
585 : : {
586 : : /* Make a copy if we are changing the set. */
587 [ + + ]: 78613 : if (!result)
588 : 59622 : result = bms_copy(relids);
589 : :
590 : 78613 : result = bms_del_member(result, appinfo->parent_relid);
591 : 78613 : result = bms_add_member(result, appinfo->child_relid);
592 : : }
593 : : }
594 : :
595 : : /* If we made any changes, return the modified copy. */
596 [ + + ]: 77642 : if (result)
597 : 59622 : return result;
598 : :
599 : : /* Otherwise, return the original set without modification. */
600 : 18020 : return relids;
601 : : }
602 : :
603 : : /*
604 : : * Substitute child's relids for parent's relids in a Relid set.
605 : : * The childrel can be multiple inheritance levels below the parent.
606 : : */
607 : : Relids
608 : 585 : adjust_child_relids_multilevel(PlannerInfo *root, Relids relids,
609 : : RelOptInfo *childrel,
610 : : RelOptInfo *parentrel)
611 : : {
612 : : AppendRelInfo **appinfos;
613 : : int nappinfos;
614 : :
615 : : /*
616 : : * If the given relids set doesn't contain any of the parent relids, it
617 : : * will remain unchanged.
618 : : */
1115 tgl@sss.pgh.pa.us 619 [ - + ]: 585 : if (!bms_overlap(relids, parentrel->relids))
2431 alvherre@alvh.no-ip. 620 :UBC 0 : return relids;
621 : :
622 : : /* Recurse if immediate parent is not the top parent. */
1115 tgl@sss.pgh.pa.us 623 [ + + ]:CBC 585 : if (childrel->parent != parentrel)
624 : : {
625 [ + - ]: 72 : if (childrel->parent)
626 : 72 : relids = adjust_child_relids_multilevel(root, relids,
627 : 72 : childrel->parent,
628 : : parentrel);
629 : : else
1115 tgl@sss.pgh.pa.us 630 [ # # ]:UBC 0 : elog(ERROR, "childrel is not a child of parentrel");
631 : : }
632 : :
633 : : /* Now translate for this child. */
1115 tgl@sss.pgh.pa.us 634 :CBC 585 : appinfos = find_appinfos_by_relids(root, childrel->relids, &nappinfos);
635 : :
636 : 585 : relids = adjust_child_relids(relids, nappinfos, appinfos);
637 : :
2431 alvherre@alvh.no-ip. 638 : 585 : pfree(appinfos);
639 : :
1115 tgl@sss.pgh.pa.us 640 : 585 : return relids;
641 : : }
642 : :
643 : : /*
644 : : * adjust_inherited_attnums
645 : : * Translate an integer list of attribute numbers from parent to child.
646 : : */
647 : : List *
1620 648 : 2563 : adjust_inherited_attnums(List *attnums, AppendRelInfo *context)
649 : : {
650 : 2563 : List *result = NIL;
651 : : ListCell *lc;
652 : :
653 : : /* This should only happen for an inheritance case, not UNION ALL */
2431 alvherre@alvh.no-ip. 654 [ - + ]: 2563 : Assert(OidIsValid(context->parent_reloid));
655 : :
656 : : /* Look up each attribute in the AppendRelInfo's translated_vars list */
1620 tgl@sss.pgh.pa.us 657 [ + - + + : 5721 : foreach(lc, attnums)
+ + ]
658 : : {
659 : 3158 : AttrNumber parentattno = lfirst_int(lc);
660 : : Var *childvar;
661 : :
662 : : /* Look up the translation of this column: it must be a Var */
663 [ + - - + ]: 6316 : if (parentattno <= 0 ||
664 : 3158 : parentattno > list_length(context->translated_vars))
2431 alvherre@alvh.no-ip. 665 [ # # ]:UBC 0 : elog(ERROR, "attribute %d of relation \"%s\" does not exist",
666 : : parentattno, get_rel_name(context->parent_reloid));
1620 tgl@sss.pgh.pa.us 667 :CBC 3158 : childvar = (Var *) list_nth(context->translated_vars, parentattno - 1);
2431 alvherre@alvh.no-ip. 668 [ + - - + ]: 3158 : if (childvar == NULL || !IsA(childvar, Var))
2431 alvherre@alvh.no-ip. 669 [ # # ]:UBC 0 : elog(ERROR, "attribute %d of relation \"%s\" does not exist",
670 : : parentattno, get_rel_name(context->parent_reloid));
671 : :
1620 tgl@sss.pgh.pa.us 672 :CBC 3158 : result = lappend_int(result, childvar->varattno);
673 : : }
674 : 2563 : return result;
675 : : }
676 : :
677 : : /*
678 : : * adjust_inherited_attnums_multilevel
679 : : * As above, but traverse multiple inheritance levels as needed.
680 : : */
681 : : List *
682 : 2563 : adjust_inherited_attnums_multilevel(PlannerInfo *root, List *attnums,
683 : : Index child_relid, Index top_parent_relid)
684 : : {
685 : 2563 : AppendRelInfo *appinfo = root->append_rel_array[child_relid];
686 : :
687 [ - + ]: 2563 : if (!appinfo)
1620 tgl@sss.pgh.pa.us 688 [ # # ]:UBC 0 : elog(ERROR, "child rel %d not found in append_rel_array", child_relid);
689 : :
690 : : /* Recurse if immediate parent is not the top parent. */
1620 tgl@sss.pgh.pa.us 691 [ + + ]:CBC 2563 : if (appinfo->parent_relid != top_parent_relid)
692 : 404 : attnums = adjust_inherited_attnums_multilevel(root, attnums,
693 : : appinfo->parent_relid,
694 : : top_parent_relid);
695 : :
696 : : /* Now translate for this child */
697 : 2563 : return adjust_inherited_attnums(attnums, appinfo);
698 : : }
699 : :
700 : : /*
701 : : * get_translated_update_targetlist
702 : : * Get the processed_tlist of an UPDATE query, translated as needed to
703 : : * match a child target relation.
704 : : *
705 : : * Optionally also return the list of target column numbers translated
706 : : * to this target relation. (The resnos in processed_tlist MUST NOT be
707 : : * relied on for this purpose.)
708 : : */
709 : : void
710 : 50 : get_translated_update_targetlist(PlannerInfo *root, Index relid,
711 : : List **processed_tlist, List **update_colnos)
712 : : {
713 : : /* This is pretty meaningless for commands other than UPDATE. */
714 [ - + ]: 50 : Assert(root->parse->commandType == CMD_UPDATE);
715 [ + + ]: 50 : if (relid == root->parse->resultRelation)
716 : : {
717 : : /*
718 : : * Non-inheritance case, so it's easy. The caller might be expecting
719 : : * a tree it can scribble on, though, so copy.
720 : : */
721 : 33 : *processed_tlist = copyObject(root->processed_tlist);
722 [ + - ]: 33 : if (update_colnos)
723 : 33 : *update_colnos = copyObject(root->update_colnos);
724 : : }
725 : : else
726 : : {
727 [ - + ]: 17 : Assert(bms_is_member(relid, root->all_result_relids));
728 : 17 : *processed_tlist = (List *)
729 : 17 : adjust_appendrel_attrs_multilevel(root,
730 : 17 : (Node *) root->processed_tlist,
731 : : find_base_rel(root, relid),
1115 732 : 17 : find_base_rel(root, root->parse->resultRelation));
1620 733 [ + - ]: 17 : if (update_colnos)
734 : 17 : *update_colnos =
735 : 17 : adjust_inherited_attnums_multilevel(root, root->update_colnos,
736 : : relid,
737 : 17 : root->parse->resultRelation);
738 : : }
2431 alvherre@alvh.no-ip. 739 : 50 : }
740 : :
741 : : /*
742 : : * find_appinfos_by_relids
743 : : * Find AppendRelInfo structures for base relations listed in relids.
744 : : *
745 : : * The relids argument is typically a join relation's relids, which can
746 : : * include outer-join RT indexes in addition to baserels. We silently
747 : : * ignore the outer joins.
748 : : *
749 : : * The AppendRelInfos are returned in an array, which can be pfree'd by the
750 : : * caller. *nappinfos is set to the number of entries in the array.
751 : : */
752 : : AppendRelInfo **
753 : 32659 : find_appinfos_by_relids(PlannerInfo *root, Relids relids, int *nappinfos)
754 : : {
755 : : AppendRelInfo **appinfos;
756 : 32659 : int cnt = 0;
757 : : int i;
758 : :
759 : : /* Allocate an array that's certainly big enough */
760 : : appinfos = (AppendRelInfo **)
950 tgl@sss.pgh.pa.us 761 : 32659 : palloc(sizeof(AppendRelInfo *) * bms_num_members(relids));
762 : :
2431 alvherre@alvh.no-ip. 763 : 32659 : i = -1;
764 [ + + ]: 73845 : while ((i = bms_next_member(relids, i)) >= 0)
765 : : {
766 : 41186 : AppendRelInfo *appinfo = root->append_rel_array[i];
767 : :
768 [ + + ]: 41186 : if (!appinfo)
769 : : {
770 : : /* Probably i is an OJ index, but let's check */
950 tgl@sss.pgh.pa.us 771 [ + - ]: 1788 : if (find_base_rel_ignore_join(root, i) == NULL)
772 : 1788 : continue;
773 : : /* It's a base rel, but we lack an append_rel_array entry */
2431 alvherre@alvh.no-ip. 774 [ # # ]:UBC 0 : elog(ERROR, "child rel %d not found in append_rel_array", i);
775 : : }
776 : :
2431 alvherre@alvh.no-ip. 777 :CBC 39398 : appinfos[cnt++] = appinfo;
778 : : }
950 tgl@sss.pgh.pa.us 779 : 32659 : *nappinfos = cnt;
2431 alvherre@alvh.no-ip. 780 : 32659 : return appinfos;
781 : : }
782 : :
783 : :
784 : : /*****************************************************************************
785 : : *
786 : : * ROW-IDENTITY VARIABLE MANAGEMENT
787 : : *
788 : : * This code lacks a good home, perhaps. We choose to keep it here because
789 : : * adjust_appendrel_attrs_mutator() is its principal co-conspirator. That
790 : : * function does most of what is needed to expand ROWID_VAR Vars into the
791 : : * right things.
792 : : *
793 : : *****************************************************************************/
794 : :
795 : : /*
796 : : * add_row_identity_var
797 : : * Register a row-identity column to be used in UPDATE/DELETE/MERGE.
798 : : *
799 : : * The Var must be equal(), aside from varno, to any other row-identity
800 : : * column with the same rowid_name. Thus, for example, "wholerow"
801 : : * row identities had better use vartype == RECORDOID.
802 : : *
803 : : * rtindex is currently redundant with rowid_var->varno, but we specify
804 : : * it as a separate parameter in case this is ever generalized to support
805 : : * non-Var expressions. (We could reasonably handle expressions over
806 : : * Vars of the specified rtindex, but for now that seems unnecessary.)
807 : : */
808 : : void
1620 tgl@sss.pgh.pa.us 809 : 14037 : add_row_identity_var(PlannerInfo *root, Var *orig_var,
810 : : Index rtindex, const char *rowid_name)
811 : : {
812 : : TargetEntry *tle;
813 : : Var *rowid_var;
814 : : RowIdentityVarInfo *ridinfo;
815 : : ListCell *lc;
816 : :
817 : : /* For now, the argument must be just a Var of the given rtindex */
818 [ - + ]: 14037 : Assert(IsA(orig_var, Var));
819 [ - + ]: 14037 : Assert(orig_var->varno == rtindex);
820 [ - + ]: 14037 : Assert(orig_var->varlevelsup == 0);
950 821 [ - + ]: 14037 : Assert(orig_var->varnullingrels == NULL);
822 : :
823 : : /*
824 : : * If we're doing non-inherited UPDATE/DELETE/MERGE, there's little need
825 : : * for ROWID_VAR shenanigans. Just shove the presented Var into the
826 : : * processed_tlist, and we're done.
827 : : */
1620 828 [ + + ]: 14037 : if (rtindex == root->parse->resultRelation)
829 : : {
830 : 8502 : tle = makeTargetEntry((Expr *) orig_var,
831 : 8502 : list_length(root->processed_tlist) + 1,
832 : : pstrdup(rowid_name),
833 : : true);
834 : 8502 : root->processed_tlist = lappend(root->processed_tlist, tle);
835 : 8502 : return;
836 : : }
837 : :
838 : : /*
839 : : * Otherwise, rtindex should reference a leaf target relation that's being
840 : : * added to the query during expand_inherited_rtentry().
841 : : */
842 [ - + ]: 5535 : Assert(bms_is_member(rtindex, root->leaf_result_relids));
843 [ - + ]: 5535 : Assert(root->append_rel_array[rtindex] != NULL);
844 : :
845 : : /*
846 : : * We have to find a matching RowIdentityVarInfo, or make one if there is
847 : : * none. To allow using equal() to match the vars, change the varno to
848 : : * ROWID_VAR, leaving all else alone.
849 : : */
850 : 5535 : rowid_var = copyObject(orig_var);
851 : : /* This could eventually become ChangeVarNodes() */
852 : 5535 : rowid_var->varno = ROWID_VAR;
853 : :
854 : : /* Look for an existing row-id column of the same name */
855 [ + + + + : 8366 : foreach(lc, root->row_identity_vars)
+ + ]
856 : : {
857 : 5465 : ridinfo = (RowIdentityVarInfo *) lfirst(lc);
858 [ + + ]: 5465 : if (strcmp(rowid_name, ridinfo->rowidname) != 0)
859 : 2831 : continue;
860 [ + - ]: 2634 : if (equal(rowid_var, ridinfo->rowidvar))
861 : : {
862 : : /* Found a match; we need only record that rtindex needs it too */
863 : 2634 : ridinfo->rowidrels = bms_add_member(ridinfo->rowidrels, rtindex);
864 : 2634 : return;
865 : : }
866 : : else
867 : : {
868 : : /* Ooops, can't handle this */
1620 tgl@sss.pgh.pa.us 869 [ # # ]:UBC 0 : elog(ERROR, "conflicting uses of row-identity name \"%s\"",
870 : : rowid_name);
871 : : }
872 : : }
873 : :
874 : : /* No request yet, so add a new RowIdentityVarInfo */
1620 tgl@sss.pgh.pa.us 875 :CBC 2901 : ridinfo = makeNode(RowIdentityVarInfo);
876 : 2901 : ridinfo->rowidvar = copyObject(rowid_var);
877 : : /* for the moment, estimate width using just the datatype info */
878 : 2901 : ridinfo->rowidwidth = get_typavgwidth(exprType((Node *) rowid_var),
879 : : exprTypmod((Node *) rowid_var));
880 : 2901 : ridinfo->rowidname = pstrdup(rowid_name);
881 : 2901 : ridinfo->rowidrels = bms_make_singleton(rtindex);
882 : :
883 : 2901 : root->row_identity_vars = lappend(root->row_identity_vars, ridinfo);
884 : :
885 : : /* Change rowid_var into a reference to this row_identity_vars entry */
886 : 2901 : rowid_var->varattno = list_length(root->row_identity_vars);
887 : :
888 : : /* Push the ROWID_VAR reference variable into processed_tlist */
889 : 2901 : tle = makeTargetEntry((Expr *) rowid_var,
890 : 2901 : list_length(root->processed_tlist) + 1,
891 : : pstrdup(rowid_name),
892 : : true);
893 : 2901 : root->processed_tlist = lappend(root->processed_tlist, tle);
894 : : }
895 : :
896 : : /*
897 : : * add_row_identity_columns
898 : : *
899 : : * This function adds the row identity columns needed by the core code.
900 : : * FDWs might call add_row_identity_var() for themselves to add nonstandard
901 : : * columns. (Duplicate requests are fine.)
902 : : */
903 : : void
904 : 11326 : add_row_identity_columns(PlannerInfo *root, Index rtindex,
905 : : RangeTblEntry *target_rte,
906 : : Relation target_relation)
907 : : {
908 : 11326 : CmdType commandType = root->parse->commandType;
909 : 11326 : char relkind = target_relation->rd_rel->relkind;
910 : : Var *var;
911 : :
1258 alvherre@alvh.no-ip. 912 [ + + + + : 11326 : Assert(commandType == CMD_UPDATE || commandType == CMD_DELETE || commandType == CMD_MERGE);
- + ]
913 : :
555 dean.a.rasheed@gmail 914 [ + + + + ]: 11326 : if (relkind == RELKIND_RELATION ||
1620 tgl@sss.pgh.pa.us 915 [ + + ]: 358 : relkind == RELKIND_MATVIEW ||
916 : : relkind == RELKIND_PARTITIONED_TABLE)
917 : : {
918 : : /*
919 : : * Emit CTID so that executor can find the row to merge, update or
920 : : * delete.
921 : : */
922 : 10986 : var = makeVar(rtindex,
923 : : SelfItemPointerAttributeNumber,
924 : : TIDOID,
925 : : -1,
926 : : InvalidOid,
927 : : 0);
928 : 10986 : add_row_identity_var(root, var, rtindex, "ctid");
929 : : }
930 [ + + ]: 340 : else if (relkind == RELKIND_FOREIGN_TABLE)
931 : : {
932 : : /*
933 : : * Let the foreign table's FDW add whatever junk TLEs it wants.
934 : : */
935 : : FdwRoutine *fdwroutine;
936 : :
937 : 193 : fdwroutine = GetFdwRoutineForRelation(target_relation, false);
938 : :
939 [ + + ]: 193 : if (fdwroutine->AddForeignUpdateTargets != NULL)
940 : 189 : fdwroutine->AddForeignUpdateTargets(root, rtindex,
941 : : target_rte, target_relation);
942 : :
943 : : /*
944 : : * For UPDATE, we need to make the FDW fetch unchanged columns by
945 : : * asking it to fetch a whole-row Var. That's because the top-level
946 : : * targetlist only contains entries for changed columns, but
947 : : * ExecUpdate will need to build the complete new tuple. (Actually,
948 : : * we only really need this in UPDATEs that are not pushed to the
949 : : * remote side, but it's hard to tell if that will be the case at the
950 : : * point when this function is called.)
951 : : *
952 : : * We will also need the whole row if there are any row triggers, so
953 : : * that the executor will have the "old" row to pass to the trigger.
954 : : * Alas, this misses system columns.
955 : : */
956 [ + + ]: 193 : if (commandType == CMD_UPDATE ||
957 [ + + ]: 86 : (target_relation->trigdesc &&
958 [ + + ]: 15 : (target_relation->trigdesc->trig_delete_after_row ||
959 [ + + ]: 9 : target_relation->trigdesc->trig_delete_before_row)))
960 : : {
961 : 115 : var = makeVar(rtindex,
962 : : InvalidAttrNumber,
963 : : RECORDOID,
964 : : -1,
965 : : InvalidOid,
966 : : 0);
967 : 115 : add_row_identity_var(root, var, rtindex, "wholerow");
968 : : }
969 : : }
970 : 11326 : }
971 : :
972 : : /*
973 : : * distribute_row_identity_vars
974 : : *
975 : : * After we have finished identifying all the row identity columns
976 : : * needed by an inherited UPDATE/DELETE/MERGE query, make sure that
977 : : * these columns will be generated by all the target relations.
978 : : *
979 : : * This is more or less like what build_base_rel_tlists() does,
980 : : * except that it would not understand what to do with ROWID_VAR Vars.
981 : : * Since that function runs before inheritance relations are expanded,
982 : : * it will never see any such Vars anyway.
983 : : */
984 : : void
985 : 159352 : distribute_row_identity_vars(PlannerInfo *root)
986 : : {
987 : 159352 : Query *parse = root->parse;
988 : 159352 : int result_relation = parse->resultRelation;
989 : : RangeTblEntry *target_rte;
990 : : RelOptInfo *target_rel;
991 : : ListCell *lc;
992 : :
993 : : /*
994 : : * There's nothing to do if this isn't an inherited UPDATE/DELETE/MERGE.
995 : : */
1258 alvherre@alvh.no-ip. 996 [ + + + + ]: 159352 : if (parse->commandType != CMD_UPDATE && parse->commandType != CMD_DELETE &&
997 [ + + ]: 150238 : parse->commandType != CMD_MERGE)
998 : : {
1620 tgl@sss.pgh.pa.us 999 [ - + ]: 149347 : Assert(root->row_identity_vars == NIL);
1000 : 149347 : return;
1001 : : }
1002 : 10005 : target_rte = rt_fetch(result_relation, parse->rtable);
1003 [ + + ]: 10005 : if (!target_rte->inh)
1004 : : {
1005 [ - + ]: 8560 : Assert(root->row_identity_vars == NIL);
1006 : 8560 : return;
1007 : : }
1008 : :
1009 : : /*
1010 : : * Ordinarily, we expect that leaf result relation(s) will have added some
1011 : : * ROWID_VAR Vars to the query. However, it's possible that constraint
1012 : : * exclusion suppressed every leaf relation. The executor will get upset
1013 : : * if the plan has no row identity columns at all, even though it will
1014 : : * certainly process no rows. Handle this edge case by re-opening the top
1015 : : * result relation and adding the row identity columns it would have used,
1016 : : * as preprocess_targetlist() would have done if it weren't marked "inh".
1017 : : * Then re-run build_base_rel_tlists() to ensure that the added columns
1018 : : * get propagated to the relation's reltarget. (This is a bit ugly, but
1019 : : * it seems better to confine the ugliness and extra cycles to this
1020 : : * unusual corner case.)
1021 : : */
1022 [ + + ]: 1445 : if (root->row_identity_vars == NIL)
1023 : : {
1024 : : Relation target_relation;
1025 : :
1026 : 15 : target_relation = table_open(target_rte->relid, NoLock);
1027 : 15 : add_row_identity_columns(root, result_relation,
1028 : : target_rte, target_relation);
1029 : 15 : table_close(target_relation, NoLock);
893 1030 : 15 : build_base_rel_tlists(root, root->processed_tlist);
1031 : : /* There are no ROWID_VAR Vars in this case, so we're done. */
1620 1032 : 15 : return;
1033 : : }
1034 : :
1035 : : /*
1036 : : * Dig through the processed_tlist to find the ROWID_VAR reference Vars,
1037 : : * and forcibly copy them into the reltarget list of the topmost target
1038 : : * relation. That's sufficient because they'll be copied to the
1039 : : * individual leaf target rels (with appropriate translation) later,
1040 : : * during appendrel expansion --- see set_append_rel_size().
1041 : : */
1042 : 1430 : target_rel = find_base_rel(root, result_relation);
1043 : :
1044 [ + - + + : 6002 : foreach(lc, root->processed_tlist)
+ + ]
1045 : : {
1046 : 4572 : TargetEntry *tle = lfirst(lc);
1047 : 4572 : Var *var = (Var *) tle->expr;
1048 : :
1049 [ + - + + : 4572 : if (var && IsA(var, Var) && var->varno == ROWID_VAR)
+ + ]
1050 : : {
1051 : 2901 : target_rel->reltarget->exprs =
1052 : 2901 : lappend(target_rel->reltarget->exprs, copyObject(var));
1053 : : /* reltarget cost and width will be computed later */
1054 : : }
1055 : : }
1056 : : }
|