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-2026, 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/sysattr.h"
19 : : #include "access/table.h"
20 : : #include "foreign/fdwapi.h"
21 : : #include "nodes/makefuncs.h"
22 : : #include "nodes/nodeFuncs.h"
23 : : #include "optimizer/appendinfo.h"
24 : : #include "optimizer/pathnode.h"
25 : : #include "optimizer/planmain.h"
26 : : #include "parser/parsetree.h"
27 : : #include "utils/lsyscache.h"
28 : : #include "utils/rel.h"
29 : : #include "utils/syscache.h"
30 : :
31 : :
32 : : typedef struct
33 : : {
34 : : PlannerInfo *root;
35 : : int nappinfos;
36 : : AppendRelInfo **appinfos;
37 : : } adjust_appendrel_attrs_context;
38 : :
39 : : static void make_inh_translation_list(Relation oldrelation,
40 : : Relation newrelation,
41 : : Index newvarno,
42 : : AppendRelInfo *appinfo);
43 : : static Node *adjust_appendrel_attrs_mutator(Node *node,
44 : : adjust_appendrel_attrs_context *context);
45 : :
46 : :
47 : : /*
48 : : * make_append_rel_info
49 : : * Build an AppendRelInfo for the parent-child pair
50 : : */
51 : : AppendRelInfo *
2672 alvherre@alvh.no-ip. 52 :CBC 35938 : make_append_rel_info(Relation parentrel, Relation childrel,
53 : : Index parentRTindex, Index childRTindex)
54 : : {
55 : 35938 : AppendRelInfo *appinfo = makeNode(AppendRelInfo);
56 : :
57 : 35938 : appinfo->parent_relid = parentRTindex;
58 : 35938 : appinfo->child_relid = childRTindex;
59 : 35938 : appinfo->parent_reltype = parentrel->rd_rel->reltype;
60 : 35938 : appinfo->child_reltype = childrel->rd_rel->reltype;
2346 tgl@sss.pgh.pa.us 61 : 35938 : make_inh_translation_list(parentrel, childrel, childRTindex, appinfo);
2672 alvherre@alvh.no-ip. 62 : 35937 : appinfo->parent_reloid = RelationGetRelid(parentrel);
63 : :
64 : 35937 : return appinfo;
65 : : }
66 : :
67 : : /*
68 : : * make_inh_translation_list
69 : : * Build the list of translations from parent Vars to child Vars for
70 : : * an inheritance child, as well as a reverse-translation array.
71 : : *
72 : : * The reverse-translation array has an entry for each child relation
73 : : * column, which is either the 1-based index of the corresponding parent
74 : : * column, or 0 if there's no match (that happens for dropped child columns,
75 : : * as well as child columns beyond those of the parent, which are allowed in
76 : : * traditional inheritance though not partitioning).
77 : : *
78 : : * For paranoia's sake, we match type/collation as well as attribute name.
79 : : */
80 : : static void
81 : 35938 : make_inh_translation_list(Relation oldrelation, Relation newrelation,
82 : : Index newvarno,
83 : : AppendRelInfo *appinfo)
84 : : {
85 : 35938 : List *vars = NIL;
86 : : AttrNumber *pcolnos;
87 : 35938 : TupleDesc old_tupdesc = RelationGetDescr(oldrelation);
88 : 35938 : TupleDesc new_tupdesc = RelationGetDescr(newrelation);
89 : 35938 : Oid new_relid = RelationGetRelid(newrelation);
90 : 35938 : int oldnatts = old_tupdesc->natts;
91 : 35938 : int newnatts = new_tupdesc->natts;
92 : : int old_attno;
93 : 35938 : int new_attno = 0;
94 : :
95 : : /* Initialize reverse-translation array with all entries zero */
2346 tgl@sss.pgh.pa.us 96 : 35938 : appinfo->num_child_cols = newnatts;
97 : 35938 : appinfo->parent_colnos = pcolnos =
98 : 35938 : (AttrNumber *) palloc0(newnatts * sizeof(AttrNumber));
99 : :
2672 alvherre@alvh.no-ip. 100 [ + + ]: 131506 : for (old_attno = 0; old_attno < oldnatts; old_attno++)
101 : : {
102 : : Form_pg_attribute att;
103 : : char *attname;
104 : : Oid atttypid;
105 : : int32 atttypmod;
106 : : Oid attcollation;
107 : :
108 : 95569 : att = TupleDescAttr(old_tupdesc, old_attno);
109 [ + + ]: 95569 : if (att->attisdropped)
110 : : {
111 : : /* Just put NULL into this list entry */
112 : 2643 : vars = lappend(vars, NULL);
113 : 2643 : continue;
114 : : }
115 : 92926 : attname = NameStr(att->attname);
116 : 92926 : atttypid = att->atttypid;
117 : 92926 : atttypmod = att->atttypmod;
118 : 92926 : attcollation = att->attcollation;
119 : :
120 : : /*
121 : : * When we are generating the "translation list" for the parent table
122 : : * of an inheritance set, no need to search for matches.
123 : : */
124 [ + + ]: 92926 : if (oldrelation == newrelation)
125 : : {
126 : 5799 : vars = lappend(vars, makeVar(newvarno,
127 : 5799 : (AttrNumber) (old_attno + 1),
128 : : atttypid,
129 : : atttypmod,
130 : : attcollation,
131 : : 0));
2346 tgl@sss.pgh.pa.us 132 : 5799 : pcolnos[old_attno] = old_attno + 1;
2672 alvherre@alvh.no-ip. 133 : 5799 : continue;
134 : : }
135 : :
136 : : /*
137 : : * Otherwise we have to search for the matching column by name.
138 : : * There's no guarantee it'll have the same column position, because
139 : : * of cases like ALTER TABLE ADD COLUMN and multiple inheritance.
140 : : * However, in simple cases, the relative order of columns is mostly
141 : : * the same in both relations, so try the column of newrelation that
142 : : * follows immediately after the one that we just found, and if that
143 : : * fails, let syscache handle it.
144 : : */
145 [ + + ]: 87127 : if (new_attno >= newnatts ||
146 [ + + ]: 85250 : (att = TupleDescAttr(new_tupdesc, new_attno))->attisdropped ||
147 [ + + ]: 84430 : strcmp(attname, NameStr(att->attname)) != 0)
148 : : {
149 : : HeapTuple newtup;
150 : :
151 : 6992 : newtup = SearchSysCacheAttName(new_relid, attname);
2557 tgl@sss.pgh.pa.us 152 [ - + ]: 6992 : if (!HeapTupleIsValid(newtup))
2672 alvherre@alvh.no-ip. 153 [ # # ]:UBC 0 : elog(ERROR, "could not find inherited attribute \"%s\" of relation \"%s\"",
154 : : attname, RelationGetRelationName(newrelation));
2672 alvherre@alvh.no-ip. 155 :CBC 6992 : new_attno = ((Form_pg_attribute) GETSTRUCT(newtup))->attnum - 1;
2346 tgl@sss.pgh.pa.us 156 [ + - - + ]: 6992 : Assert(new_attno >= 0 && new_attno < newnatts);
2672 alvherre@alvh.no-ip. 157 : 6992 : ReleaseSysCache(newtup);
158 : :
159 : 6992 : att = TupleDescAttr(new_tupdesc, new_attno);
160 : : }
161 : :
162 : : /* Found it, check type and collation match */
163 [ + + - + ]: 87127 : if (atttypid != att->atttypid || atttypmod != att->atttypmod)
670 michael@paquier.xyz 164 [ + - ]: 1 : ereport(ERROR,
165 : : (errcode(ERRCODE_INVALID_COLUMN_DEFINITION),
166 : : errmsg("attribute \"%s\" of relation \"%s\" does not match parent's type",
167 : : attname, RelationGetRelationName(newrelation))));
2672 alvherre@alvh.no-ip. 168 [ - + ]: 87126 : if (attcollation != att->attcollation)
670 michael@paquier.xyz 169 [ # # ]:UBC 0 : ereport(ERROR,
170 : : (errcode(ERRCODE_INVALID_COLUMN_DEFINITION),
171 : : errmsg("attribute \"%s\" of relation \"%s\" does not match parent's collation",
172 : : attname, RelationGetRelationName(newrelation))));
173 : :
2672 alvherre@alvh.no-ip. 174 :CBC 87126 : vars = lappend(vars, makeVar(newvarno,
175 : 87126 : (AttrNumber) (new_attno + 1),
176 : : atttypid,
177 : : atttypmod,
178 : : attcollation,
179 : : 0));
2346 tgl@sss.pgh.pa.us 180 : 87126 : pcolnos[new_attno] = old_attno + 1;
2672 alvherre@alvh.no-ip. 181 : 87126 : new_attno++;
182 : : }
183 : :
2346 tgl@sss.pgh.pa.us 184 : 35937 : appinfo->translated_vars = vars;
2672 alvherre@alvh.no-ip. 185 : 35937 : }
186 : :
187 : : /*
188 : : * adjust_appendrel_attrs
189 : : * Copy the specified query or expression and translate Vars referring to a
190 : : * parent rel to refer to the corresponding child rel instead. We also
191 : : * update rtindexes appearing outside Vars, such as resultRelation and
192 : : * jointree relids.
193 : : *
194 : : * Note: this is only applied after conversion of sublinks to subplans,
195 : : * so we don't need to cope with recursion into sub-queries.
196 : : *
197 : : * Note: this is not hugely different from what pullup_replace_vars() does;
198 : : * maybe we should try to fold the two routines together.
199 : : */
200 : : Node *
201 : 246496 : adjust_appendrel_attrs(PlannerInfo *root, Node *node, int nappinfos,
202 : : AppendRelInfo **appinfos)
203 : : {
204 : : adjust_appendrel_attrs_context context;
205 : :
206 : 246496 : context.root = root;
207 : 246496 : context.nappinfos = nappinfos;
208 : 246496 : context.appinfos = appinfos;
209 : :
210 : : /* If there's nothing to adjust, don't call this function. */
211 [ + - - + ]: 246496 : Assert(nappinfos >= 1 && appinfos != NULL);
212 : :
213 : : /* Should never be translating a Query tree. */
1861 tgl@sss.pgh.pa.us 214 [ + + - + ]: 246496 : Assert(node == NULL || !IsA(node, Query));
215 : :
216 : 246496 : return adjust_appendrel_attrs_mutator(node, &context);
217 : : }
218 : :
219 : : static Node *
2672 alvherre@alvh.no-ip. 220 : 1279718 : adjust_appendrel_attrs_mutator(Node *node,
221 : : adjust_appendrel_attrs_context *context)
222 : : {
223 : 1279718 : AppendRelInfo **appinfos = context->appinfos;
224 : 1279718 : int nappinfos = context->nappinfos;
225 : : int cnt;
226 : :
227 [ + + ]: 1279718 : if (node == NULL)
228 : 238228 : return NULL;
229 [ + + ]: 1041490 : if (IsA(node, Var))
230 : : {
231 : 467107 : Var *var = (Var *) copyObject(node);
232 : 467107 : AppendRelInfo *appinfo = NULL;
233 : :
2308 tgl@sss.pgh.pa.us 234 [ - + ]: 467107 : if (var->varlevelsup != 0)
2308 tgl@sss.pgh.pa.us 235 :UBC 0 : return (Node *) var; /* no changes needed */
236 : :
237 : : /*
238 : : * You might think we need to adjust var->varnullingrels, but that
239 : : * shouldn't need any changes. It will contain outer-join relids,
240 : : * while the transformation we are making affects only baserels.
241 : : * Below, we just merge var->varnullingrels into the translated Var.
242 : : * (We must merge not just copy: the child Var could have some
243 : : * nullingrel bits set already, and we mustn't drop those.)
244 : : *
245 : : * If var->varnullingrels isn't empty, and the translation wouldn't be
246 : : * a Var, we have to fail. One could imagine wrapping the translated
247 : : * expression in a PlaceHolderVar, but that won't work because this is
248 : : * typically used after freezing placeholders. Fortunately, the case
249 : : * appears unreachable at the moment. We can see nonempty
250 : : * var->varnullingrels here, but only in cases involving partitionwise
251 : : * joining, and in such cases the translations will always be Vars.
252 : : * (Non-Var translations occur only for appendrels made by flattening
253 : : * UNION ALL subqueries.) Should we need to make this work in future,
254 : : * a possible fix is to mandate that prepjointree.c create PHVs for
255 : : * all non-Var outputs of such subqueries, and then we could look up
256 : : * the pre-existing PHV here. Or perhaps just wrap the translations
257 : : * that way to begin with?
258 : : *
259 : : * If var->varreturningtype is not VAR_RETURNING_DEFAULT, then that
260 : : * also needs to be copied to the translated Var. That too would fail
261 : : * if the translation wasn't a Var, but that should never happen since
262 : : * a non-default var->varreturningtype is only used for Vars referring
263 : : * to the result relation, which should never be a flattened UNION ALL
264 : : * subquery.
265 : : */
266 : :
2672 alvherre@alvh.no-ip. 267 [ + + ]:CBC 583474 : for (cnt = 0; cnt < nappinfos; cnt++)
268 : : {
269 [ + + ]: 549085 : if (var->varno == appinfos[cnt]->parent_relid)
270 : : {
271 : 432718 : appinfo = appinfos[cnt];
272 : 432718 : break;
273 : : }
274 : : }
275 : :
2308 tgl@sss.pgh.pa.us 276 [ + + ]: 467107 : if (appinfo)
277 : : {
2672 alvherre@alvh.no-ip. 278 : 432718 : var->varno = appinfo->child_relid;
279 : : /* it's now a generated Var, so drop any syntactic labeling */
2308 tgl@sss.pgh.pa.us 280 : 432718 : var->varnosyn = 0;
281 : 432718 : var->varattnosyn = 0;
2672 alvherre@alvh.no-ip. 282 [ + + ]: 432718 : if (var->varattno > 0)
283 : : {
284 : : Node *newnode;
285 : :
286 [ - + ]: 415829 : if (var->varattno > list_length(appinfo->translated_vars))
2672 alvherre@alvh.no-ip. 287 [ # # ]:UBC 0 : elog(ERROR, "attribute %d of relation \"%s\" does not exist",
288 : : var->varattno, get_rel_name(appinfo->parent_reloid));
2672 alvherre@alvh.no-ip. 289 :CBC 415829 : newnode = copyObject(list_nth(appinfo->translated_vars,
290 : : var->varattno - 1));
291 [ - + ]: 415829 : if (newnode == NULL)
2672 alvherre@alvh.no-ip. 292 [ # # ]:UBC 0 : elog(ERROR, "attribute %d of relation \"%s\" does not exist",
293 : : var->varattno, get_rel_name(appinfo->parent_reloid));
1191 tgl@sss.pgh.pa.us 294 [ + + ]:CBC 415829 : if (IsA(newnode, Var))
295 : : {
74 rguo@postgresql.org 296 : 408053 : Var *newvar = (Var *) newnode;
297 : :
298 : 408053 : newvar->varreturningtype = var->varreturningtype;
299 : 408053 : newvar->varnullingrels = bms_add_members(newvar->varnullingrels,
300 : 408053 : var->varnullingrels);
301 : : }
302 : : else
303 : : {
474 dean.a.rasheed@gmail 304 [ - + ]: 7776 : if (var->varreturningtype != VAR_RETURNING_DEFAULT)
474 dean.a.rasheed@gmail 305 [ # # ]:UBC 0 : elog(ERROR, "failed to apply returningtype to a non-Var");
474 dean.a.rasheed@gmail 306 [ - + ]:CBC 7776 : if (var->varnullingrels != NULL)
474 dean.a.rasheed@gmail 307 [ # # ]:UBC 0 : elog(ERROR, "failed to apply nullingrels to a non-Var");
308 : : }
2672 alvherre@alvh.no-ip. 309 :CBC 415829 : return newnode;
310 : : }
311 [ + + ]: 16889 : else if (var->varattno == 0)
312 : : {
313 : : /*
314 : : * Whole-row Var: if we are dealing with named rowtypes, we
315 : : * can use a whole-row Var for the child table plus a coercion
316 : : * step to convert the tuple layout to the parent's rowtype.
317 : : * Otherwise we have to generate a RowExpr.
318 : : */
319 [ + + ]: 845 : if (OidIsValid(appinfo->child_reltype))
320 : : {
321 [ - + ]: 801 : Assert(var->vartype == appinfo->parent_reltype);
322 [ + + ]: 801 : if (appinfo->parent_reltype != appinfo->child_reltype)
323 : : {
324 : 655 : ConvertRowtypeExpr *r = makeNode(ConvertRowtypeExpr);
325 : :
326 : 655 : r->arg = (Expr *) var;
327 : 655 : r->resulttype = appinfo->parent_reltype;
328 : 655 : r->convertformat = COERCE_IMPLICIT_CAST;
329 : 655 : r->location = -1;
330 : : /* Make sure the Var node has the right type ID, too */
331 : 655 : var->vartype = appinfo->child_reltype;
332 : 655 : return (Node *) r;
333 : : }
334 : : }
335 : : else
336 : : {
337 : : /*
338 : : * Build a RowExpr containing the translated variables.
339 : : *
340 : : * In practice var->vartype will always be RECORDOID here,
341 : : * so we need to come up with some suitable column names.
342 : : * We use the parent RTE's column names.
343 : : *
344 : : * Note: we can't get here for inheritance cases, so there
345 : : * is no need to worry that translated_vars might contain
346 : : * some dummy NULLs.
347 : : */
348 : : RowExpr *rowexpr;
349 : : List *fields;
350 : : RangeTblEntry *rte;
351 : :
352 : 44 : rte = rt_fetch(appinfo->parent_relid,
353 : : context->root->parse->rtable);
354 : 44 : fields = copyObject(appinfo->translated_vars);
355 : 44 : rowexpr = makeNode(RowExpr);
356 : 44 : rowexpr->args = fields;
357 : 44 : rowexpr->row_typeid = var->vartype;
358 : 44 : rowexpr->row_format = COERCE_IMPLICIT_CAST;
359 : 44 : rowexpr->colnames = copyObject(rte->eref->colnames);
360 : 44 : rowexpr->location = -1;
361 : :
474 dean.a.rasheed@gmail 362 [ - + ]: 44 : if (var->varreturningtype != VAR_RETURNING_DEFAULT)
474 dean.a.rasheed@gmail 363 [ # # ]:UBC 0 : elog(ERROR, "failed to apply returningtype to a non-Var");
1191 tgl@sss.pgh.pa.us 364 [ - + ]:CBC 44 : if (var->varnullingrels != NULL)
1191 tgl@sss.pgh.pa.us 365 [ # # ]:UBC 0 : elog(ERROR, "failed to apply nullingrels to a non-Var");
366 : :
2672 alvherre@alvh.no-ip. 367 :CBC 44 : return (Node *) rowexpr;
368 : : }
369 : : }
370 : : /* system attributes don't need any other translation */
371 : : }
1861 tgl@sss.pgh.pa.us 372 [ + + ]: 34389 : else if (var->varno == ROWID_VAR)
373 : : {
374 : : /*
375 : : * If it's a ROWID_VAR placeholder, see if we've reached a leaf
376 : : * target rel, for which we can translate the Var to a specific
377 : : * instantiation. We should never be asked to translate to a set
378 : : * of relids containing more than one leaf target rel, so the
379 : : * answer will be unique. If we're still considering non-leaf
380 : : * inheritance levels, return the ROWID_VAR Var as-is.
381 : : */
382 : 15555 : Relids leaf_result_relids = context->root->leaf_result_relids;
383 : 15555 : Index leaf_relid = 0;
384 : :
385 [ + + ]: 31110 : for (cnt = 0; cnt < nappinfos; cnt++)
386 : : {
387 [ + + ]: 15555 : if (bms_is_member(appinfos[cnt]->child_relid,
388 : : leaf_result_relids))
389 : : {
390 [ - + ]: 13689 : if (leaf_relid)
1861 tgl@sss.pgh.pa.us 391 [ # # ]:UBC 0 : elog(ERROR, "cannot translate to multiple leaf relids");
1861 tgl@sss.pgh.pa.us 392 :CBC 13689 : leaf_relid = appinfos[cnt]->child_relid;
393 : : }
394 : : }
395 : :
396 [ + + ]: 15555 : if (leaf_relid)
397 : : {
398 : : RowIdentityVarInfo *ridinfo = (RowIdentityVarInfo *)
1082 399 : 13689 : list_nth(context->root->row_identity_vars, var->varattno - 1);
400 : :
1861 401 [ + + ]: 13689 : if (bms_is_member(leaf_relid, ridinfo->rowidrels))
402 : : {
403 : : /* Substitute the Var given in the RowIdentityVarInfo */
404 : 13643 : var = copyObject(ridinfo->rowidvar);
405 : : /* ... but use the correct relid */
406 : 13643 : var->varno = leaf_relid;
407 : : /* identity vars shouldn't have nulling rels */
1191 408 [ - + ]: 13643 : Assert(var->varnullingrels == NULL);
409 : : /* varnosyn in the RowIdentityVarInfo is probably wrong */
1861 410 : 13643 : var->varnosyn = 0;
411 : 13643 : var->varattnosyn = 0;
412 : : }
413 : : else
414 : : {
415 : : /*
416 : : * This leaf rel can't return the desired value, so
417 : : * substitute a NULL of the correct type.
418 : : */
419 : 46 : return (Node *) makeNullConst(var->vartype,
420 : : var->vartypmod,
421 : : var->varcollid);
422 : : }
423 : : }
424 : : }
2672 alvherre@alvh.no-ip. 425 : 50533 : return (Node *) var;
426 : : }
427 [ + + ]: 574383 : if (IsA(node, CurrentOfExpr))
428 : : {
429 : 152 : CurrentOfExpr *cexpr = (CurrentOfExpr *) copyObject(node);
430 : :
431 [ + - ]: 152 : for (cnt = 0; cnt < nappinfos; cnt++)
432 : : {
433 : 152 : AppendRelInfo *appinfo = appinfos[cnt];
434 : :
435 [ + - ]: 152 : if (cexpr->cvarno == appinfo->parent_relid)
436 : : {
437 : 152 : cexpr->cvarno = appinfo->child_relid;
438 : 152 : break;
439 : : }
440 : : }
441 : 152 : return (Node *) cexpr;
442 : : }
443 [ + + ]: 574231 : if (IsA(node, PlaceHolderVar))
444 : : {
445 : : /* Copy the PlaceHolderVar node with correct mutation of subnodes */
446 : : PlaceHolderVar *phv;
447 : :
448 : 2602 : phv = (PlaceHolderVar *) expression_tree_mutator(node,
449 : : adjust_appendrel_attrs_mutator,
450 : : context);
451 : : /* now fix PlaceHolderVar's relid sets */
452 [ + - ]: 2602 : if (phv->phlevelsup == 0)
453 : : {
1191 tgl@sss.pgh.pa.us 454 : 2602 : phv->phrels = adjust_child_relids(phv->phrels,
455 : : nappinfos, appinfos);
456 : : /* as above, we needn't touch phnullingrels */
457 : : }
2672 alvherre@alvh.no-ip. 458 : 2602 : return (Node *) phv;
459 : : }
460 : : /* Shouldn't need to handle planner auxiliary nodes here */
461 [ - + ]: 571629 : Assert(!IsA(node, SpecialJoinInfo));
462 [ - + ]: 571629 : Assert(!IsA(node, AppendRelInfo));
463 [ - + ]: 571629 : Assert(!IsA(node, PlaceHolderInfo));
464 [ - + ]: 571629 : Assert(!IsA(node, MinMaxAggInfo));
465 : :
466 : : /*
467 : : * We have to process RestrictInfo nodes specially. (Note: although
468 : : * set_append_rel_pathlist will hide RestrictInfos in the parent's
469 : : * baserestrictinfo list from us, it doesn't hide those in joininfo.)
470 : : */
471 [ + + ]: 571629 : if (IsA(node, RestrictInfo))
472 : : {
473 : 33542 : RestrictInfo *oldinfo = (RestrictInfo *) node;
474 : 33542 : RestrictInfo *newinfo = makeNode(RestrictInfo);
475 : :
476 : : /* Copy all flat-copiable fields, notably including rinfo_serial */
477 : 33542 : memcpy(newinfo, oldinfo, sizeof(RestrictInfo));
478 : :
479 : : /* Recursively fix the clause itself */
480 : 33542 : newinfo->clause = (Expr *)
481 : 33542 : adjust_appendrel_attrs_mutator((Node *) oldinfo->clause, context);
482 : :
483 : : /* and the modified version, if an OR clause */
484 : 33542 : newinfo->orclause = (Expr *)
485 : 33542 : adjust_appendrel_attrs_mutator((Node *) oldinfo->orclause, context);
486 : :
487 : : /* adjust relid sets too */
488 : 33542 : newinfo->clause_relids = adjust_child_relids(oldinfo->clause_relids,
489 : : context->nappinfos,
490 : : context->appinfos);
491 : 33542 : newinfo->required_relids = adjust_child_relids(oldinfo->required_relids,
492 : : context->nappinfos,
493 : : context->appinfos);
494 : 33542 : newinfo->outer_relids = adjust_child_relids(oldinfo->outer_relids,
495 : : context->nappinfos,
496 : : context->appinfos);
497 : 33542 : newinfo->left_relids = adjust_child_relids(oldinfo->left_relids,
498 : : context->nappinfos,
499 : : context->appinfos);
500 : 33542 : newinfo->right_relids = adjust_child_relids(oldinfo->right_relids,
501 : : context->nappinfos,
502 : : context->appinfos);
503 : :
504 : : /*
505 : : * Reset cached derivative fields, since these might need to have
506 : : * different values when considering the child relation. Note we
507 : : * don't reset left_ec/right_ec: each child variable is implicitly
508 : : * equivalent to its parent, so still a member of the same EC if any.
509 : : */
510 : 33542 : newinfo->eval_cost.startup = -1;
511 : 33542 : newinfo->norm_selec = -1;
512 : 33542 : newinfo->outer_selec = -1;
513 : 33542 : newinfo->left_em = NULL;
514 : 33542 : newinfo->right_em = NULL;
515 : 33542 : newinfo->scansel_cache = NIL;
516 : 33542 : newinfo->left_bucketsize = -1;
517 : 33542 : newinfo->right_bucketsize = -1;
518 : 33542 : newinfo->left_mcvfreq = -1;
519 : 33542 : newinfo->right_mcvfreq = -1;
520 : :
521 : 33542 : return (Node *) newinfo;
522 : : }
523 : :
524 : : /*
525 : : * We have to process RelAggInfo nodes specially.
526 : : */
209 rguo@postgresql.org 527 [ + + ]:GNC 538087 : if (IsA(node, RelAggInfo))
528 : : {
529 : 15460 : RelAggInfo *oldinfo = (RelAggInfo *) node;
530 : 15460 : RelAggInfo *newinfo = makeNode(RelAggInfo);
531 : :
532 : 15460 : newinfo->target = (PathTarget *)
533 : 15460 : adjust_appendrel_attrs_mutator((Node *) oldinfo->target,
534 : : context);
535 : :
536 : 15460 : newinfo->agg_input = (PathTarget *)
537 : 15460 : adjust_appendrel_attrs_mutator((Node *) oldinfo->agg_input,
538 : : context);
539 : :
540 : 15460 : newinfo->group_clauses = oldinfo->group_clauses;
541 : :
542 : 15460 : newinfo->group_exprs = (List *)
543 : 15460 : adjust_appendrel_attrs_mutator((Node *) oldinfo->group_exprs,
544 : : context);
545 : :
546 : 15460 : return (Node *) newinfo;
547 : : }
548 : :
549 : : /*
550 : : * We have to process PathTarget nodes specially.
551 : : */
552 [ + + ]: 522627 : if (IsA(node, PathTarget))
553 : : {
554 : 30920 : PathTarget *oldtarget = (PathTarget *) node;
555 : 30920 : PathTarget *newtarget = makeNode(PathTarget);
556 : :
557 : : /* Copy all flat-copiable fields */
558 : 30920 : memcpy(newtarget, oldtarget, sizeof(PathTarget));
559 : :
560 : 30920 : newtarget->exprs = (List *)
561 : 30920 : adjust_appendrel_attrs_mutator((Node *) oldtarget->exprs,
562 : : context);
563 : :
564 [ + - ]: 30920 : if (oldtarget->sortgrouprefs)
565 : : {
566 : 30920 : Size nbytes = list_length(oldtarget->exprs) * sizeof(Index);
567 : :
568 : 30920 : newtarget->sortgrouprefs = (Index *) palloc(nbytes);
569 : 30920 : memcpy(newtarget->sortgrouprefs, oldtarget->sortgrouprefs, nbytes);
570 : : }
571 : :
572 : 30920 : return (Node *) newtarget;
573 : : }
574 : :
575 : : /*
576 : : * NOTE: we do not need to recurse into sublinks, because they should
577 : : * already have been converted to subplans before we see them.
578 : : */
2672 alvherre@alvh.no-ip. 579 [ - + ]:CBC 491707 : Assert(!IsA(node, SubLink));
580 [ - + ]: 491707 : Assert(!IsA(node, Query));
581 : : /* We should never see these Query substructures, either. */
1861 tgl@sss.pgh.pa.us 582 [ - + ]: 491707 : Assert(!IsA(node, RangeTblRef));
583 [ - + ]: 491707 : Assert(!IsA(node, JoinExpr));
584 : :
523 peter@eisentraut.org 585 : 491707 : return expression_tree_mutator(node, adjust_appendrel_attrs_mutator, context);
586 : : }
587 : :
588 : : /*
589 : : * adjust_appendrel_attrs_multilevel
590 : : * Apply Var translations from an appendrel parent down to a child.
591 : : *
592 : : * Replace Vars in the "node" expression that reference "parentrel" with
593 : : * the appropriate Vars for "childrel". childrel can be more than one
594 : : * inheritance level removed from parentrel.
595 : : */
596 : : Node *
2666 alvherre@alvh.no-ip. 597 : 39564 : adjust_appendrel_attrs_multilevel(PlannerInfo *root, Node *node,
598 : : RelOptInfo *childrel,
599 : : RelOptInfo *parentrel)
600 : : {
601 : : AppendRelInfo **appinfos;
602 : : int nappinfos;
603 : :
604 : : /* Recurse if immediate parent is not the top parent. */
1356 tgl@sss.pgh.pa.us 605 [ + + ]: 39564 : if (childrel->parent != parentrel)
606 : : {
607 [ + - ]: 13624 : if (childrel->parent)
608 : 13624 : node = adjust_appendrel_attrs_multilevel(root, node,
609 : 13624 : childrel->parent,
610 : : parentrel);
611 : : else
1356 tgl@sss.pgh.pa.us 612 [ # # ]:UBC 0 : elog(ERROR, "childrel is not a child of parentrel");
613 : : }
614 : :
615 : : /* Now translate for this child. */
1356 tgl@sss.pgh.pa.us 616 :CBC 39564 : appinfos = find_appinfos_by_relids(root, childrel->relids, &nappinfos);
617 : :
2666 alvherre@alvh.no-ip. 618 : 39564 : node = adjust_appendrel_attrs(root, node, nappinfos, appinfos);
619 : :
620 : 39564 : pfree(appinfos);
621 : :
622 : 39564 : return node;
623 : : }
624 : :
625 : : /*
626 : : * Substitute child relids for parent relids in a Relid set. The array of
627 : : * appinfos specifies the substitutions to be performed.
628 : : */
629 : : Relids
2672 630 : 213477 : adjust_child_relids(Relids relids, int nappinfos, AppendRelInfo **appinfos)
631 : : {
632 : 213477 : Bitmapset *result = NULL;
633 : : int cnt;
634 : :
635 [ + + ]: 584529 : for (cnt = 0; cnt < nappinfos; cnt++)
636 : : {
637 : 371052 : AppendRelInfo *appinfo = appinfos[cnt];
638 : :
639 : : /* Remove parent, add child */
640 [ + + ]: 371052 : if (bms_is_member(appinfo->parent_relid, relids))
641 : : {
642 : : /* Make a copy if we are changing the set. */
643 [ + + ]: 248998 : if (!result)
644 : 170333 : result = bms_copy(relids);
645 : :
646 : 248998 : result = bms_del_member(result, appinfo->parent_relid);
647 : 248998 : result = bms_add_member(result, appinfo->child_relid);
648 : : }
649 : : }
650 : :
651 : : /* If we made any changes, return the modified copy. */
652 [ + + ]: 213477 : if (result)
653 : 170333 : return result;
654 : :
655 : : /* Otherwise, return the original set without modification. */
656 : 43144 : return relids;
657 : : }
658 : :
659 : : /*
660 : : * Substitute child's relids for parent's relids in a Relid set.
661 : : * The childrel can be multiple inheritance levels below the parent.
662 : : */
663 : : Relids
664 : 967 : adjust_child_relids_multilevel(PlannerInfo *root, Relids relids,
665 : : RelOptInfo *childrel,
666 : : RelOptInfo *parentrel)
667 : : {
668 : : AppendRelInfo **appinfos;
669 : : int nappinfos;
670 : :
671 : : /*
672 : : * If the given relids set doesn't contain any of the parent relids, it
673 : : * will remain unchanged.
674 : : */
1356 tgl@sss.pgh.pa.us 675 [ - + ]: 967 : if (!bms_overlap(relids, parentrel->relids))
2672 alvherre@alvh.no-ip. 676 :UBC 0 : return relids;
677 : :
678 : : /* Recurse if immediate parent is not the top parent. */
1356 tgl@sss.pgh.pa.us 679 [ + + ]:CBC 967 : if (childrel->parent != parentrel)
680 : : {
681 [ + - ]: 120 : if (childrel->parent)
682 : 120 : relids = adjust_child_relids_multilevel(root, relids,
683 : 120 : childrel->parent,
684 : : parentrel);
685 : : else
1356 tgl@sss.pgh.pa.us 686 [ # # ]:UBC 0 : elog(ERROR, "childrel is not a child of parentrel");
687 : : }
688 : :
689 : : /* Now translate for this child. */
1356 tgl@sss.pgh.pa.us 690 :CBC 967 : appinfos = find_appinfos_by_relids(root, childrel->relids, &nappinfos);
691 : :
692 : 967 : relids = adjust_child_relids(relids, nappinfos, appinfos);
693 : :
2672 alvherre@alvh.no-ip. 694 : 967 : pfree(appinfos);
695 : :
1356 tgl@sss.pgh.pa.us 696 : 967 : return relids;
697 : : }
698 : :
699 : : /*
700 : : * adjust_inherited_attnums
701 : : * Translate an integer list of attribute numbers from parent to child.
702 : : */
703 : : List *
1861 704 : 4120 : adjust_inherited_attnums(List *attnums, AppendRelInfo *context)
705 : : {
706 : 4120 : List *result = NIL;
707 : : ListCell *lc;
708 : :
709 : : /* This should only happen for an inheritance case, not UNION ALL */
2672 alvherre@alvh.no-ip. 710 [ - + ]: 4120 : Assert(OidIsValid(context->parent_reloid));
711 : :
712 : : /* Look up each attribute in the AppendRelInfo's translated_vars list */
1861 tgl@sss.pgh.pa.us 713 [ + - + + : 9278 : foreach(lc, attnums)
+ + ]
714 : : {
715 : 5158 : AttrNumber parentattno = lfirst_int(lc);
716 : : Var *childvar;
717 : :
718 : : /* Look up the translation of this column: it must be a Var */
719 [ + - - + ]: 10316 : if (parentattno <= 0 ||
720 : 5158 : parentattno > list_length(context->translated_vars))
2672 alvherre@alvh.no-ip. 721 [ # # ]:UBC 0 : elog(ERROR, "attribute %d of relation \"%s\" does not exist",
722 : : parentattno, get_rel_name(context->parent_reloid));
1861 tgl@sss.pgh.pa.us 723 :CBC 5158 : childvar = (Var *) list_nth(context->translated_vars, parentattno - 1);
2672 alvherre@alvh.no-ip. 724 [ + - - + ]: 5158 : if (childvar == NULL || !IsA(childvar, Var))
2672 alvherre@alvh.no-ip. 725 [ # # ]:UBC 0 : elog(ERROR, "attribute %d of relation \"%s\" does not exist",
726 : : parentattno, get_rel_name(context->parent_reloid));
727 : :
1861 tgl@sss.pgh.pa.us 728 :CBC 5158 : result = lappend_int(result, childvar->varattno);
729 : : }
730 : 4120 : return result;
731 : : }
732 : :
733 : : /*
734 : : * adjust_inherited_attnums_multilevel
735 : : * As above, but traverse multiple inheritance levels as needed.
736 : : */
737 : : List *
738 : 4120 : adjust_inherited_attnums_multilevel(PlannerInfo *root, List *attnums,
739 : : Index child_relid, Index top_parent_relid)
740 : : {
741 : 4120 : AppendRelInfo *appinfo = root->append_rel_array[child_relid];
742 : :
743 [ - + ]: 4120 : if (!appinfo)
1861 tgl@sss.pgh.pa.us 744 [ # # ]:UBC 0 : elog(ERROR, "child rel %d not found in append_rel_array", child_relid);
745 : :
746 : : /* Recurse if immediate parent is not the top parent. */
1861 tgl@sss.pgh.pa.us 747 [ + + ]:CBC 4120 : if (appinfo->parent_relid != top_parent_relid)
748 : 677 : attnums = adjust_inherited_attnums_multilevel(root, attnums,
749 : : appinfo->parent_relid,
750 : : top_parent_relid);
751 : :
752 : : /* Now translate for this child */
753 : 4120 : return adjust_inherited_attnums(attnums, appinfo);
754 : : }
755 : :
756 : : /*
757 : : * get_translated_update_targetlist
758 : : * Get the processed_tlist of an UPDATE query, translated as needed to
759 : : * match a child target relation.
760 : : *
761 : : * Optionally also return the list of target column numbers translated
762 : : * to this target relation. (The resnos in processed_tlist MUST NOT be
763 : : * relied on for this purpose.)
764 : : */
765 : : void
766 : 50 : get_translated_update_targetlist(PlannerInfo *root, Index relid,
767 : : List **processed_tlist, List **update_colnos)
768 : : {
769 : : /* This is pretty meaningless for commands other than UPDATE. */
770 [ - + ]: 50 : Assert(root->parse->commandType == CMD_UPDATE);
771 [ + + ]: 50 : if (relid == root->parse->resultRelation)
772 : : {
773 : : /*
774 : : * Non-inheritance case, so it's easy. The caller might be expecting
775 : : * a tree it can scribble on, though, so copy.
776 : : */
777 : 33 : *processed_tlist = copyObject(root->processed_tlist);
778 [ + - ]: 33 : if (update_colnos)
779 : 33 : *update_colnos = copyObject(root->update_colnos);
780 : : }
781 : : else
782 : : {
783 [ - + ]: 17 : Assert(bms_is_member(relid, root->all_result_relids));
784 : 17 : *processed_tlist = (List *)
785 : 17 : adjust_appendrel_attrs_multilevel(root,
786 : 17 : (Node *) root->processed_tlist,
787 : : find_base_rel(root, relid),
1356 788 : 17 : find_base_rel(root, root->parse->resultRelation));
1861 789 [ + - ]: 17 : if (update_colnos)
790 : 17 : *update_colnos =
791 : 17 : adjust_inherited_attnums_multilevel(root, root->update_colnos,
792 : : relid,
793 : 17 : root->parse->resultRelation);
794 : : }
2672 alvherre@alvh.no-ip. 795 : 50 : }
796 : :
797 : : /*
798 : : * find_appinfos_by_relids
799 : : * Find AppendRelInfo structures for base relations listed in relids.
800 : : *
801 : : * The relids argument is typically a join relation's relids, which can
802 : : * include outer-join RT indexes in addition to baserels. We silently
803 : : * ignore the outer joins.
804 : : *
805 : : * The AppendRelInfos are returned in an array, which can be pfree'd by the
806 : : * caller. *nappinfos is set to the number of entries in the array.
807 : : */
808 : : AppendRelInfo **
809 : 83987 : find_appinfos_by_relids(PlannerInfo *root, Relids relids, int *nappinfos)
810 : : {
811 : : AppendRelInfo **appinfos;
812 : 83987 : int cnt = 0;
813 : : int i;
814 : :
815 : : /* Allocate an array that's certainly big enough */
146 michael@paquier.xyz 816 :GNC 83987 : appinfos = palloc_array(AppendRelInfo *, bms_num_members(relids));
817 : :
2672 alvherre@alvh.no-ip. 818 :CBC 83987 : i = -1;
819 [ + + ]: 211159 : while ((i = bms_next_member(relids, i)) >= 0)
820 : : {
821 : 127172 : AppendRelInfo *appinfo = root->append_rel_array[i];
822 : :
823 [ + + ]: 127172 : if (!appinfo)
824 : : {
825 : : /* Probably i is an OJ index, but let's check */
1191 tgl@sss.pgh.pa.us 826 [ + - ]: 2976 : if (find_base_rel_ignore_join(root, i) == NULL)
827 : 2976 : continue;
828 : : /* It's a base rel, but we lack an append_rel_array entry */
2672 alvherre@alvh.no-ip. 829 [ # # ]:UBC 0 : elog(ERROR, "child rel %d not found in append_rel_array", i);
830 : : }
831 : :
2672 alvherre@alvh.no-ip. 832 :CBC 124196 : appinfos[cnt++] = appinfo;
833 : : }
1191 tgl@sss.pgh.pa.us 834 : 83987 : *nappinfos = cnt;
2672 alvherre@alvh.no-ip. 835 : 83987 : return appinfos;
836 : : }
837 : :
838 : :
839 : : /*****************************************************************************
840 : : *
841 : : * ROW-IDENTITY VARIABLE MANAGEMENT
842 : : *
843 : : * This code lacks a good home, perhaps. We choose to keep it here because
844 : : * adjust_appendrel_attrs_mutator() is its principal co-conspirator. That
845 : : * function does most of what is needed to expand ROWID_VAR Vars into the
846 : : * right things.
847 : : *
848 : : *****************************************************************************/
849 : :
850 : : /*
851 : : * add_row_identity_var
852 : : * Register a row-identity column to be used in UPDATE/DELETE/MERGE.
853 : : *
854 : : * The Var must be equal(), aside from varno, to any other row-identity
855 : : * column with the same rowid_name. Thus, for example, "wholerow"
856 : : * row identities had better use vartype == RECORDOID.
857 : : *
858 : : * rtindex is currently redundant with rowid_var->varno, but we specify
859 : : * it as a separate parameter in case this is ever generalized to support
860 : : * non-Var expressions. (We could reasonably handle expressions over
861 : : * Vars of the specified rtindex, but for now that seems unnecessary.)
862 : : */
863 : : void
1861 tgl@sss.pgh.pa.us 864 : 22036 : add_row_identity_var(PlannerInfo *root, Var *orig_var,
865 : : Index rtindex, const char *rowid_name)
866 : : {
867 : : TargetEntry *tle;
868 : : Var *rowid_var;
869 : : RowIdentityVarInfo *ridinfo;
870 : : ListCell *lc;
871 : :
872 : : /* For now, the argument must be just a Var of the given rtindex */
873 [ - + ]: 22036 : Assert(IsA(orig_var, Var));
874 [ - + ]: 22036 : Assert(orig_var->varno == rtindex);
875 [ - + ]: 22036 : Assert(orig_var->varlevelsup == 0);
1191 876 [ - + ]: 22036 : Assert(orig_var->varnullingrels == NULL);
877 : :
878 : : /*
879 : : * If we're doing non-inherited UPDATE/DELETE/MERGE, there's little need
880 : : * for ROWID_VAR shenanigans. Just shove the presented Var into the
881 : : * processed_tlist, and we're done.
882 : : */
1861 883 [ + + ]: 22036 : if (rtindex == root->parse->resultRelation)
884 : : {
885 : 13133 : tle = makeTargetEntry((Expr *) orig_var,
886 : 13133 : list_length(root->processed_tlist) + 1,
887 : : pstrdup(rowid_name),
888 : : true);
889 : 13133 : root->processed_tlist = lappend(root->processed_tlist, tle);
890 : 13133 : return;
891 : : }
892 : :
893 : : /*
894 : : * Otherwise, rtindex should reference a leaf target relation that's being
895 : : * added to the query during expand_inherited_rtentry().
896 : : */
897 [ - + ]: 8903 : Assert(bms_is_member(rtindex, root->leaf_result_relids));
898 [ - + ]: 8903 : Assert(root->append_rel_array[rtindex] != NULL);
899 : :
900 : : /*
901 : : * We have to find a matching RowIdentityVarInfo, or make one if there is
902 : : * none. To allow using equal() to match the vars, change the varno to
903 : : * ROWID_VAR, leaving all else alone.
904 : : */
905 : 8903 : rowid_var = copyObject(orig_var);
906 : : /* This could eventually become ChangeVarNodes() */
907 : 8903 : rowid_var->varno = ROWID_VAR;
908 : :
909 : : /* Look for an existing row-id column of the same name */
910 [ + + + + : 13417 : foreach(lc, root->row_identity_vars)
+ + ]
911 : : {
912 : 8800 : ridinfo = (RowIdentityVarInfo *) lfirst(lc);
913 [ + + ]: 8800 : if (strcmp(rowid_name, ridinfo->rowidname) != 0)
914 : 4514 : continue;
915 [ + - ]: 4286 : if (equal(rowid_var, ridinfo->rowidvar))
916 : : {
917 : : /* Found a match; we need only record that rtindex needs it too */
918 : 4286 : ridinfo->rowidrels = bms_add_member(ridinfo->rowidrels, rtindex);
919 : 4286 : return;
920 : : }
921 : : else
922 : : {
923 : : /* Ooops, can't handle this */
1861 tgl@sss.pgh.pa.us 924 [ # # ]:UBC 0 : elog(ERROR, "conflicting uses of row-identity name \"%s\"",
925 : : rowid_name);
926 : : }
927 : : }
928 : :
929 : : /* No request yet, so add a new RowIdentityVarInfo */
1861 tgl@sss.pgh.pa.us 930 :CBC 4617 : ridinfo = makeNode(RowIdentityVarInfo);
931 : 4617 : ridinfo->rowidvar = copyObject(rowid_var);
932 : : /* for the moment, estimate width using just the datatype info */
933 : 4617 : ridinfo->rowidwidth = get_typavgwidth(exprType((Node *) rowid_var),
934 : : exprTypmod((Node *) rowid_var));
935 : 4617 : ridinfo->rowidname = pstrdup(rowid_name);
936 : 4617 : ridinfo->rowidrels = bms_make_singleton(rtindex);
937 : :
938 : 4617 : root->row_identity_vars = lappend(root->row_identity_vars, ridinfo);
939 : :
940 : : /* Change rowid_var into a reference to this row_identity_vars entry */
941 : 4617 : rowid_var->varattno = list_length(root->row_identity_vars);
942 : :
943 : : /* Push the ROWID_VAR reference variable into processed_tlist */
944 : 4617 : tle = makeTargetEntry((Expr *) rowid_var,
945 : 4617 : list_length(root->processed_tlist) + 1,
946 : : pstrdup(rowid_name),
947 : : true);
948 : 4617 : root->processed_tlist = lappend(root->processed_tlist, tle);
949 : : }
950 : :
951 : : /*
952 : : * add_row_identity_columns
953 : : *
954 : : * This function adds the row identity columns needed by the core code.
955 : : * FDWs might call add_row_identity_var() for themselves to add nonstandard
956 : : * columns. (Duplicate requests are fine.)
957 : : */
958 : : void
959 : 17736 : add_row_identity_columns(PlannerInfo *root, Index rtindex,
960 : : RangeTblEntry *target_rte,
961 : : Relation target_relation)
962 : : {
963 : 17736 : CmdType commandType = root->parse->commandType;
964 : 17736 : char relkind = target_relation->rd_rel->relkind;
965 : : Var *var;
966 : :
1499 alvherre@alvh.no-ip. 967 [ + + + + : 17736 : Assert(commandType == CMD_UPDATE || commandType == CMD_DELETE || commandType == CMD_MERGE);
- + ]
968 : :
796 dean.a.rasheed@gmail 969 [ + + + + ]: 17736 : if (relkind == RELKIND_RELATION ||
1861 tgl@sss.pgh.pa.us 970 [ + + ]: 470 : relkind == RELKIND_MATVIEW ||
971 : : relkind == RELKIND_PARTITIONED_TABLE)
972 : : {
973 : : /*
974 : : * Emit CTID so that executor can find the row to merge, update or
975 : : * delete.
976 : : */
977 : 17299 : var = makeVar(rtindex,
978 : : SelfItemPointerAttributeNumber,
979 : : TIDOID,
980 : : -1,
981 : : InvalidOid,
982 : : 0);
983 : 17299 : add_row_identity_var(root, var, rtindex, "ctid");
984 : : }
985 [ + + ]: 437 : else if (relkind == RELKIND_FOREIGN_TABLE)
986 : : {
987 : : /*
988 : : * Let the foreign table's FDW add whatever junk TLEs it wants.
989 : : */
990 : : FdwRoutine *fdwroutine;
991 : :
992 : 196 : fdwroutine = GetFdwRoutineForRelation(target_relation, false);
993 : :
994 [ + + ]: 196 : if (fdwroutine->AddForeignUpdateTargets != NULL)
995 : 189 : fdwroutine->AddForeignUpdateTargets(root, rtindex,
996 : : target_rte, target_relation);
997 : :
998 : : /*
999 : : * For UPDATE, we need to make the FDW fetch unchanged columns by
1000 : : * asking it to fetch a whole-row Var. That's because the top-level
1001 : : * targetlist only contains entries for changed columns, but
1002 : : * ExecUpdate will need to build the complete new tuple. (Actually,
1003 : : * we only really need this in UPDATEs that are not pushed to the
1004 : : * remote side, but it's hard to tell if that will be the case at the
1005 : : * point when this function is called.)
1006 : : *
1007 : : * We will also need the whole row if there are any row triggers, so
1008 : : * that the executor will have the "old" row to pass to the trigger.
1009 : : * Alas, this misses system columns.
1010 : : */
1011 [ + + ]: 196 : if (commandType == CMD_UPDATE ||
1012 [ + + ]: 88 : (target_relation->trigdesc &&
1013 [ + + ]: 15 : (target_relation->trigdesc->trig_delete_after_row ||
1014 [ + + ]: 9 : target_relation->trigdesc->trig_delete_before_row)))
1015 : : {
1016 : 116 : var = makeVar(rtindex,
1017 : : InvalidAttrNumber,
1018 : : RECORDOID,
1019 : : -1,
1020 : : InvalidOid,
1021 : : 0);
1022 : 116 : add_row_identity_var(root, var, rtindex, "wholerow");
1023 : : }
1024 : : }
1025 : 17736 : }
1026 : :
1027 : : /*
1028 : : * distribute_row_identity_vars
1029 : : *
1030 : : * After we have finished identifying all the row identity columns
1031 : : * needed by an inherited UPDATE/DELETE/MERGE query, make sure that
1032 : : * these columns will be generated by all the target relations.
1033 : : *
1034 : : * This is more or less like what build_base_rel_tlists() does,
1035 : : * except that it would not understand what to do with ROWID_VAR Vars.
1036 : : * Since that function runs before inheritance relations are expanded,
1037 : : * it will never see any such Vars anyway.
1038 : : */
1039 : : void
1040 : 255244 : distribute_row_identity_vars(PlannerInfo *root)
1041 : : {
1042 : 255244 : Query *parse = root->parse;
1043 : 255244 : int result_relation = parse->resultRelation;
1044 : : RangeTblEntry *target_rte;
1045 : : RelOptInfo *target_rel;
1046 : : ListCell *lc;
1047 : :
1048 : : /*
1049 : : * There's nothing to do if this isn't an inherited UPDATE/DELETE/MERGE.
1050 : : */
1499 alvherre@alvh.no-ip. 1051 [ + + + + ]: 255244 : if (parse->commandType != CMD_UPDATE && parse->commandType != CMD_DELETE &&
1052 [ + + ]: 241132 : parse->commandType != CMD_MERGE)
1053 : : {
1861 tgl@sss.pgh.pa.us 1054 [ - + ]: 239657 : Assert(root->row_identity_vars == NIL);
1055 : 239657 : return;
1056 : : }
1057 : 15587 : target_rte = rt_fetch(result_relation, parse->rtable);
1058 [ + + ]: 15587 : if (!target_rte->inh)
1059 : : {
1060 [ - + ]: 13270 : Assert(root->row_identity_vars == NIL);
1061 : 13270 : return;
1062 : : }
1063 : :
1064 : : /*
1065 : : * Ordinarily, we expect that leaf result relation(s) will have added some
1066 : : * ROWID_VAR Vars to the query. However, it's possible that constraint
1067 : : * exclusion suppressed every leaf relation. The executor will get upset
1068 : : * if the plan has no row identity columns at all, even though it will
1069 : : * certainly process no rows. Handle this edge case by re-opening the top
1070 : : * result relation and adding the row identity columns it would have used,
1071 : : * as preprocess_targetlist() would have done if it weren't marked "inh".
1072 : : * Then re-run build_base_rel_tlists() to ensure that the added columns
1073 : : * get propagated to the relation's reltarget. (This is a bit ugly, but
1074 : : * it seems better to confine the ugliness and extra cycles to this
1075 : : * unusual corner case.)
1076 : : */
1077 [ + + ]: 2317 : if (root->row_identity_vars == NIL)
1078 : : {
1079 : : Relation target_relation;
1080 : :
1081 : 28 : target_relation = table_open(target_rte->relid, NoLock);
1082 : 28 : add_row_identity_columns(root, result_relation,
1083 : : target_rte, target_relation);
1084 : 28 : table_close(target_relation, NoLock);
1134 1085 : 28 : build_base_rel_tlists(root, root->processed_tlist);
1086 : : /* There are no ROWID_VAR Vars in this case, so we're done. */
1861 1087 : 28 : return;
1088 : : }
1089 : :
1090 : : /*
1091 : : * Dig through the processed_tlist to find the ROWID_VAR reference Vars,
1092 : : * and forcibly copy them into the reltarget list of the topmost target
1093 : : * relation. That's sufficient because they'll be copied to the
1094 : : * individual leaf target rels (with appropriate translation) later,
1095 : : * during appendrel expansion --- see set_append_rel_size().
1096 : : */
1097 : 2289 : target_rel = find_base_rel(root, result_relation);
1098 : :
1099 [ + - + + : 9652 : foreach(lc, root->processed_tlist)
+ + ]
1100 : : {
1101 : 7363 : TargetEntry *tle = lfirst(lc);
1102 : 7363 : Var *var = (Var *) tle->expr;
1103 : :
1104 [ + - + + : 7363 : if (var && IsA(var, Var) && var->varno == ROWID_VAR)
+ + ]
1105 : : {
1106 : 4617 : target_rel->reltarget->exprs =
1107 : 4617 : lappend(target_rel->reltarget->exprs, copyObject(var));
1108 : : /* reltarget cost and width will be computed later */
1109 : : }
1110 : : }
1111 : : }
|