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