PostgreSQL Source Code git master
Loading...
Searching...
No Matches
appendinfo.c File Reference
#include "postgres.h"
#include "access/htup_details.h"
#include "access/sysattr.h"
#include "access/table.h"
#include "foreign/fdwapi.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/appendinfo.h"
#include "optimizer/pathnode.h"
#include "optimizer/planmain.h"
#include "parser/parsetree.h"
#include "utils/lsyscache.h"
#include "utils/rel.h"
#include "utils/syscache.h"
Include dependency graph for appendinfo.c:

Go to the source code of this file.

Data Structures

struct  adjust_appendrel_attrs_context
 

Functions

static void make_inh_translation_list (Relation oldrelation, Relation newrelation, Index newvarno, AppendRelInfo *appinfo)
 
static Nodeadjust_appendrel_attrs_mutator (Node *node, adjust_appendrel_attrs_context *context)
 
AppendRelInfomake_append_rel_info (Relation parentrel, Relation childrel, Index parentRTindex, Index childRTindex)
 
Nodeadjust_appendrel_attrs (PlannerInfo *root, Node *node, int nappinfos, AppendRelInfo **appinfos)
 
Nodeadjust_appendrel_attrs_multilevel (PlannerInfo *root, Node *node, RelOptInfo *childrel, RelOptInfo *parentrel)
 
Relids adjust_child_relids (Relids relids, int nappinfos, AppendRelInfo **appinfos)
 
Relids adjust_child_relids_multilevel (PlannerInfo *root, Relids relids, RelOptInfo *childrel, RelOptInfo *parentrel)
 
Listadjust_inherited_attnums (List *attnums, AppendRelInfo *context)
 
Listadjust_inherited_attnums_multilevel (PlannerInfo *root, List *attnums, Index child_relid, Index top_parent_relid)
 
void get_translated_update_targetlist (PlannerInfo *root, Index relid, List **processed_tlist, List **update_colnos)
 
AppendRelInfo ** find_appinfos_by_relids (PlannerInfo *root, Relids relids, int *nappinfos)
 
void add_row_identity_var (PlannerInfo *root, Var *orig_var, Index rtindex, const char *rowid_name)
 
void add_row_identity_columns (PlannerInfo *root, Index rtindex, RangeTblEntry *target_rte, Relation target_relation)
 
void distribute_row_identity_vars (PlannerInfo *root)
 

Function Documentation

◆ add_row_identity_columns()

void add_row_identity_columns ( PlannerInfo root,
Index  rtindex,
RangeTblEntry target_rte,
Relation  target_relation 
)

Definition at line 959 of file appendinfo.c.

962{
963 CmdType commandType = root->parse->commandType;
964 char relkind = target_relation->rd_rel->relkind;
965 Var *var;
966
967 Assert(commandType == CMD_UPDATE || commandType == CMD_DELETE || commandType == CMD_MERGE);
968
969 if (relkind == RELKIND_RELATION ||
970 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 var = makeVar(rtindex,
979 TIDOID,
980 -1,
982 0);
983 add_row_identity_var(root, var, rtindex, "ctid");
984 }
985 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 fdwroutine = GetFdwRoutineForRelation(target_relation, false);
993
994 if (fdwroutine->AddForeignUpdateTargets != NULL)
995 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 if (commandType == CMD_UPDATE ||
1012 (target_relation->trigdesc &&
1013 (target_relation->trigdesc->trig_delete_after_row ||
1014 target_relation->trigdesc->trig_delete_before_row)))
1015 {
1016 var = makeVar(rtindex,
1018 RECORDOID,
1019 -1,
1020 InvalidOid,
1021 0);
1022 add_row_identity_var(root, var, rtindex, "wholerow");
1023 }
1024 }
1025}
void add_row_identity_var(PlannerInfo *root, Var *orig_var, Index rtindex, const char *rowid_name)
Definition appendinfo.c:864
#define InvalidAttrNumber
Definition attnum.h:23
#define Assert(condition)
Definition c.h:942
FdwRoutine * GetFdwRoutineForRelation(Relation relation, bool makecopy)
Definition foreign.c:531
Var * makeVar(int varno, AttrNumber varattno, Oid vartype, int32 vartypmod, Oid varcollid, Index varlevelsup)
Definition makefuncs.c:66
CmdType
Definition nodes.h:273
@ CMD_MERGE
Definition nodes.h:279
@ CMD_DELETE
Definition nodes.h:278
@ CMD_UPDATE
Definition nodes.h:276
#define InvalidOid
static int fb(int x)
tree ctl root
Definition radixtree.h:1857
AddForeignUpdateTargets_function AddForeignUpdateTargets
Definition fdwapi.h:229
#define SelfItemPointerAttributeNumber
Definition sysattr.h:21

References add_row_identity_var(), FdwRoutine::AddForeignUpdateTargets, Assert, CMD_DELETE, CMD_MERGE, CMD_UPDATE, fb(), GetFdwRoutineForRelation(), InvalidAttrNumber, InvalidOid, makeVar(), root, and SelfItemPointerAttributeNumber.

Referenced by distribute_row_identity_vars(), expand_single_inheritance_child(), and preprocess_targetlist().

◆ add_row_identity_var()

void add_row_identity_var ( PlannerInfo root,
Var orig_var,
Index  rtindex,
const char rowid_name 
)

Definition at line 864 of file appendinfo.c.

866{
868 Var *rowid_var;
870 ListCell *lc;
871
872 /* For now, the argument must be just a Var of the given rtindex */
874 Assert(orig_var->varno == rtindex);
875 Assert(orig_var->varlevelsup == 0);
876 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 */
883 if (rtindex == root->parse->resultRelation)
884 {
886 list_length(root->processed_tlist) + 1,
888 true);
889 root->processed_tlist = lappend(root->processed_tlist, tle);
890 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 Assert(bms_is_member(rtindex, root->leaf_result_relids));
898 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 */
906 /* This could eventually become ChangeVarNodes() */
907 rowid_var->varno = ROWID_VAR;
908
909 /* Look for an existing row-id column of the same name */
910 foreach(lc, root->row_identity_vars)
911 {
913 if (strcmp(rowid_name, ridinfo->rowidname) != 0)
914 continue;
915 if (equal(rowid_var, ridinfo->rowidvar))
916 {
917 /* Found a match; we need only record that rtindex needs it too */
918 ridinfo->rowidrels = bms_add_member(ridinfo->rowidrels, rtindex);
919 return;
920 }
921 else
922 {
923 /* Ooops, can't handle this */
924 elog(ERROR, "conflicting uses of row-identity name \"%s\"",
925 rowid_name);
926 }
927 }
928
929 /* No request yet, so add a new RowIdentityVarInfo */
931 ridinfo->rowidvar = copyObject(rowid_var);
932 /* for the moment, estimate width using just the datatype info */
933 ridinfo->rowidwidth = get_typavgwidth(exprType((Node *) rowid_var),
935 ridinfo->rowidname = pstrdup(rowid_name);
936 ridinfo->rowidrels = bms_make_singleton(rtindex);
937
938 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 rowid_var->varattno = list_length(root->row_identity_vars);
942
943 /* Push the ROWID_VAR reference variable into processed_tlist */
945 list_length(root->processed_tlist) + 1,
947 true);
948 root->processed_tlist = lappend(root->processed_tlist, tle);
949}
Bitmapset * bms_make_singleton(int x)
Definition bitmapset.c:216
bool bms_is_member(int x, const Bitmapset *a)
Definition bitmapset.c:510
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition bitmapset.c:799
#define ERROR
Definition elog.h:39
#define elog(elevel,...)
Definition elog.h:226
bool equal(const void *a, const void *b)
Definition equalfuncs.c:223
List * lappend(List *list, void *datum)
Definition list.c:339
int32 get_typavgwidth(Oid typid, int32 typmod)
Definition lsyscache.c:2800
TargetEntry * makeTargetEntry(Expr *expr, AttrNumber resno, char *resname, bool resjunk)
Definition makefuncs.c:289
char * pstrdup(const char *in)
Definition mcxt.c:1781
Oid exprType(const Node *expr)
Definition nodeFuncs.c:42
int32 exprTypmod(const Node *expr)
Definition nodeFuncs.c:304
#define IsA(nodeptr, _type_)
Definition nodes.h:164
#define copyObject(obj)
Definition nodes.h:232
#define makeNode(_type_)
Definition nodes.h:161
#define lfirst(lc)
Definition pg_list.h:172
static int list_length(const List *l)
Definition pg_list.h:152
#define ROWID_VAR
Definition primnodes.h:246
Definition nodes.h:135

References Assert, bms_add_member(), bms_is_member(), bms_make_singleton(), copyObject, elog, equal(), ERROR, exprType(), exprTypmod(), fb(), get_typavgwidth(), IsA, lappend(), lfirst, list_length(), makeNode, makeTargetEntry(), pstrdup(), root, and ROWID_VAR.

Referenced by add_row_identity_columns(), expand_single_inheritance_child(), and postgresAddForeignUpdateTargets().

◆ adjust_appendrel_attrs()

Node * adjust_appendrel_attrs ( PlannerInfo root,
Node node,
int  nappinfos,
AppendRelInfo **  appinfos 
)

Definition at line 201 of file appendinfo.c.

203{
205
206 context.root = root;
207 context.nappinfos = nappinfos;
208 context.appinfos = appinfos;
209
210 /* If there's nothing to adjust, don't call this function. */
211 Assert(nappinfos >= 1 && appinfos != NULL);
212
213 /* Should never be translating a Query tree. */
214 Assert(node == NULL || !IsA(node, Query));
215
216 return adjust_appendrel_attrs_mutator(node, &context);
217}
static Node * adjust_appendrel_attrs_mutator(Node *node, adjust_appendrel_attrs_context *context)
Definition appendinfo.c:220

References adjust_appendrel_attrs_mutator(), adjust_appendrel_attrs_context::appinfos, Assert, fb(), IsA, adjust_appendrel_attrs_context::nappinfos, adjust_appendrel_attrs_context::root, and root.

Referenced by add_child_join_rel_equivalences(), add_child_rel_equivalences(), adjust_appendrel_attrs_multilevel(), apply_child_basequals(), apply_scanjoin_target_to_paths(), build_child_join_rel(), build_child_join_reltarget(), build_child_join_sjinfo(), create_partitionwise_grouping_paths(), make_partitionedrel_pruneinfo(), set_append_rel_size(), and try_partitionwise_join().

◆ adjust_appendrel_attrs_multilevel()

Node * adjust_appendrel_attrs_multilevel ( PlannerInfo root,
Node node,
RelOptInfo childrel,
RelOptInfo parentrel 
)

Definition at line 597 of file appendinfo.c.

600{
601 AppendRelInfo **appinfos;
602 int nappinfos;
603
604 /* Recurse if immediate parent is not the top parent. */
605 if (childrel->parent != parentrel)
606 {
607 if (childrel->parent)
609 childrel->parent,
610 parentrel);
611 else
612 elog(ERROR, "childrel is not a child of parentrel");
613 }
614
615 /* Now translate for this child. */
616 appinfos = find_appinfos_by_relids(root, childrel->relids, &nappinfos);
617
618 node = adjust_appendrel_attrs(root, node, nappinfos, appinfos);
619
620 pfree(appinfos);
621
622 return node;
623}
AppendRelInfo ** find_appinfos_by_relids(PlannerInfo *root, Relids relids, int *nappinfos)
Definition appendinfo.c:809
Node * adjust_appendrel_attrs(PlannerInfo *root, Node *node, int nappinfos, AppendRelInfo **appinfos)
Definition appendinfo.c:201
Node * adjust_appendrel_attrs_multilevel(PlannerInfo *root, Node *node, RelOptInfo *childrel, RelOptInfo *parentrel)
Definition appendinfo.c:597
void pfree(void *pointer)
Definition mcxt.c:1616

References adjust_appendrel_attrs(), adjust_appendrel_attrs_multilevel(), elog, ERROR, fb(), find_appinfos_by_relids(), pfree(), and root.

Referenced by add_child_join_rel_equivalences(), add_child_rel_equivalences(), adjust_appendrel_attrs_multilevel(), create_rel_agg_info(), create_unique_paths(), generate_join_implied_equalities_broken(), get_translated_update_targetlist(), grouping_planner(), and make_partitionedrel_pruneinfo().

◆ adjust_appendrel_attrs_mutator()

static Node * adjust_appendrel_attrs_mutator ( Node node,
adjust_appendrel_attrs_context context 
)
static

Definition at line 220 of file appendinfo.c.

222{
223 AppendRelInfo **appinfos = context->appinfos;
224 int nappinfos = context->nappinfos;
225 int cnt;
226
227 if (node == NULL)
228 return NULL;
229 if (IsA(node, Var))
230 {
231 Var *var = (Var *) copyObject(node);
233
234 if (var->varlevelsup != 0)
235 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
267 for (cnt = 0; cnt < nappinfos; cnt++)
268 {
269 if (var->varno == appinfos[cnt]->parent_relid)
270 {
271 appinfo = appinfos[cnt];
272 break;
273 }
274 }
275
276 if (appinfo)
277 {
278 var->varno = appinfo->child_relid;
279 /* it's now a generated Var, so drop any syntactic labeling */
280 var->varnosyn = 0;
281 var->varattnosyn = 0;
282 if (var->varattno > 0)
283 {
284 Node *newnode;
285
286 if (var->varattno > list_length(appinfo->translated_vars))
287 elog(ERROR, "attribute %d of relation \"%s\" does not exist",
288 var->varattno, get_rel_name(appinfo->parent_reloid));
289 newnode = copyObject(list_nth(appinfo->translated_vars,
290 var->varattno - 1));
291 if (newnode == NULL)
292 elog(ERROR, "attribute %d of relation \"%s\" does not exist",
293 var->varattno, get_rel_name(appinfo->parent_reloid));
294 if (IsA(newnode, Var))
295 {
296 Var *newvar = (Var *) newnode;
297
299 newvar->varnullingrels = bms_add_members(newvar->varnullingrels,
300 var->varnullingrels);
301 }
302 else
303 {
305 elog(ERROR, "failed to apply returningtype to a non-Var");
306 if (var->varnullingrels != NULL)
307 elog(ERROR, "failed to apply nullingrels to a non-Var");
308 }
309 return newnode;
310 }
311 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 if (OidIsValid(appinfo->child_reltype))
320 {
321 Assert(var->vartype == appinfo->parent_reltype);
322 if (appinfo->parent_reltype != appinfo->child_reltype)
323 {
325
326 r->arg = (Expr *) var;
327 r->resulttype = appinfo->parent_reltype;
328 r->convertformat = COERCE_IMPLICIT_CAST;
329 r->location = -1;
330 /* Make sure the Var node has the right type ID, too */
331 var->vartype = appinfo->child_reltype;
332 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;
351
352 rte = rt_fetch(appinfo->parent_relid,
353 context->root->parse->rtable);
354 fields = copyObject(appinfo->translated_vars);
355 rowexpr = makeNode(RowExpr);
356 rowexpr->args = fields;
357 rowexpr->row_typeid = var->vartype;
358 rowexpr->row_format = COERCE_IMPLICIT_CAST;
359 rowexpr->colnames = copyObject(rte->eref->colnames);
360 rowexpr->location = -1;
361
363 elog(ERROR, "failed to apply returningtype to a non-Var");
364 if (var->varnullingrels != NULL)
365 elog(ERROR, "failed to apply nullingrels to a non-Var");
366
367 return (Node *) rowexpr;
368 }
369 }
370 /* system attributes don't need any other translation */
371 }
372 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 Relids leaf_result_relids = context->root->leaf_result_relids;
383 Index leaf_relid = 0;
384
385 for (cnt = 0; cnt < nappinfos; cnt++)
386 {
387 if (bms_is_member(appinfos[cnt]->child_relid,
388 leaf_result_relids))
389 {
390 if (leaf_relid)
391 elog(ERROR, "cannot translate to multiple leaf relids");
392 leaf_relid = appinfos[cnt]->child_relid;
393 }
394 }
395
396 if (leaf_relid)
397 {
399 list_nth(context->root->row_identity_vars, var->varattno - 1);
400
401 if (bms_is_member(leaf_relid, ridinfo->rowidrels))
402 {
403 /* Substitute the Var given in the RowIdentityVarInfo */
404 var = copyObject(ridinfo->rowidvar);
405 /* ... but use the correct relid */
406 var->varno = leaf_relid;
407 /* identity vars shouldn't have nulling rels */
408 Assert(var->varnullingrels == NULL);
409 /* varnosyn in the RowIdentityVarInfo is probably wrong */
410 var->varnosyn = 0;
411 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 return (Node *) makeNullConst(var->vartype,
420 var->vartypmod,
421 var->varcollid);
422 }
423 }
424 }
425 return (Node *) var;
426 }
427 if (IsA(node, CurrentOfExpr))
428 {
429 CurrentOfExpr *cexpr = (CurrentOfExpr *) copyObject(node);
430
431 for (cnt = 0; cnt < nappinfos; cnt++)
432 {
433 AppendRelInfo *appinfo = appinfos[cnt];
434
435 if (cexpr->cvarno == appinfo->parent_relid)
436 {
437 cexpr->cvarno = appinfo->child_relid;
438 break;
439 }
440 }
441 return (Node *) cexpr;
442 }
443 if (IsA(node, PlaceHolderVar))
444 {
445 /* Copy the PlaceHolderVar node with correct mutation of subnodes */
447
450 context);
451 /* now fix PlaceHolderVar's relid sets */
452 if (phv->phlevelsup == 0)
453 {
454 phv->phrels = adjust_child_relids(phv->phrels,
455 nappinfos, appinfos);
456 /* as above, we needn't touch phnullingrels */
457 }
458 return (Node *) phv;
459 }
460 /* Shouldn't need to handle planner auxiliary nodes here */
461 Assert(!IsA(node, SpecialJoinInfo));
462 Assert(!IsA(node, AppendRelInfo));
463 Assert(!IsA(node, PlaceHolderInfo));
464 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 if (IsA(node, RestrictInfo))
472 {
475
476 /* Copy all flat-copiable fields, notably including rinfo_serial */
478
479 /* Recursively fix the clause itself */
480 newinfo->clause = (Expr *)
481 adjust_appendrel_attrs_mutator((Node *) oldinfo->clause, context);
482
483 /* and the modified version, if an OR clause */
484 newinfo->orclause = (Expr *)
485 adjust_appendrel_attrs_mutator((Node *) oldinfo->orclause, context);
486
487 /* adjust relid sets too */
488 newinfo->clause_relids = adjust_child_relids(oldinfo->clause_relids,
489 context->nappinfos,
490 context->appinfos);
491 newinfo->required_relids = adjust_child_relids(oldinfo->required_relids,
492 context->nappinfos,
493 context->appinfos);
494 newinfo->outer_relids = adjust_child_relids(oldinfo->outer_relids,
495 context->nappinfos,
496 context->appinfos);
497 newinfo->left_relids = adjust_child_relids(oldinfo->left_relids,
498 context->nappinfos,
499 context->appinfos);
500 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 newinfo->eval_cost.startup = -1;
511 newinfo->norm_selec = -1;
512 newinfo->outer_selec = -1;
513 newinfo->left_em = NULL;
514 newinfo->right_em = NULL;
515 newinfo->scansel_cache = NIL;
516 newinfo->left_bucketsize = -1;
517 newinfo->right_bucketsize = -1;
518 newinfo->left_mcvfreq = -1;
519 newinfo->right_mcvfreq = -1;
520
521 return (Node *) newinfo;
522 }
523
524 /*
525 * We have to process RelAggInfo nodes specially.
526 */
527 if (IsA(node, RelAggInfo))
528 {
529 RelAggInfo *oldinfo = (RelAggInfo *) node;
531
532 newinfo->target = (PathTarget *)
534 context);
535
536 newinfo->agg_input = (PathTarget *)
538 context);
539
540 newinfo->group_clauses = oldinfo->group_clauses;
541
542 newinfo->group_exprs = (List *)
544 context);
545
546 return (Node *) newinfo;
547 }
548
549 /*
550 * We have to process PathTarget nodes specially.
551 */
552 if (IsA(node, PathTarget))
553 {
554 PathTarget *oldtarget = (PathTarget *) node;
556
557 /* Copy all flat-copiable fields */
559
560 newtarget->exprs = (List *)
562 context);
563
564 if (oldtarget->sortgrouprefs)
565 {
566 Size nbytes = list_length(oldtarget->exprs) * sizeof(Index);
567
568 newtarget->sortgrouprefs = (Index *) palloc(nbytes);
569 memcpy(newtarget->sortgrouprefs, oldtarget->sortgrouprefs, nbytes);
570 }
571
572 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 */
579 Assert(!IsA(node, SubLink));
580 Assert(!IsA(node, Query));
581 /* We should never see these Query substructures, either. */
582 Assert(!IsA(node, RangeTblRef));
583 Assert(!IsA(node, JoinExpr));
584
586}
Relids adjust_child_relids(Relids relids, int nappinfos, AppendRelInfo **appinfos)
Definition appendinfo.c:630
Bitmapset * bms_add_members(Bitmapset *a, const Bitmapset *b)
Definition bitmapset.c:901
unsigned int Index
Definition c.h:697
#define OidIsValid(objectId)
Definition c.h:857
size_t Size
Definition c.h:688
char * get_rel_name(Oid relid)
Definition lsyscache.c:2148
Const * makeNullConst(Oid consttype, int32 consttypmod, Oid constcollid)
Definition makefuncs.c:388
void * palloc(Size size)
Definition mcxt.c:1387
#define expression_tree_mutator(n, m, c)
Definition nodeFuncs.h:155
#define rt_fetch(rangetable_index, rangetable)
Definition parsetree.h:31
#define NIL
Definition pg_list.h:68
static void * list_nth(const List *list, int n)
Definition pg_list.h:299
@ VAR_RETURNING_DEFAULT
Definition primnodes.h:257
@ COERCE_IMPLICIT_CAST
Definition primnodes.h:769
Index child_relid
Definition pathnodes.h:3289
Definition pg_list.h:54
List * row_identity_vars
Definition pathnodes.h:478
Query * parse
Definition pathnodes.h:309
Relids leaf_result_relids
Definition pathnodes.h:466
List * rtable
Definition parsenodes.h:175
List * args
Definition primnodes.h:1449
ParseLoc location
Definition primnodes.h:1473
AttrNumber varattno
Definition primnodes.h:275
int varno
Definition primnodes.h:270
VarReturningType varreturningtype
Definition primnodes.h:298
Index varlevelsup
Definition primnodes.h:295

References adjust_appendrel_attrs_mutator(), adjust_child_relids(), adjust_appendrel_attrs_context::appinfos, ConvertRowtypeExpr::arg, RowExpr::args, Assert, bms_add_members(), bms_is_member(), AppendRelInfo::child_relid, COERCE_IMPLICIT_CAST, copyObject, CurrentOfExpr::cvarno, elog, ERROR, expression_tree_mutator, fb(), get_rel_name(), IsA, PlannerInfo::leaf_result_relids, list_length(), list_nth(), ConvertRowtypeExpr::location, RowExpr::location, makeNode, makeNullConst(), adjust_appendrel_attrs_context::nappinfos, NIL, OidIsValid, palloc(), AppendRelInfo::parent_relid, PlannerInfo::parse, ConvertRowtypeExpr::resulttype, adjust_appendrel_attrs_context::root, PlannerInfo::row_identity_vars, ROWID_VAR, rt_fetch, Query::rtable, VAR_RETURNING_DEFAULT, Var::varattno, Var::varlevelsup, Var::varno, and Var::varreturningtype.

Referenced by adjust_appendrel_attrs(), and adjust_appendrel_attrs_mutator().

◆ adjust_child_relids()

Relids adjust_child_relids ( Relids  relids,
int  nappinfos,
AppendRelInfo **  appinfos 
)

Definition at line 630 of file appendinfo.c.

631{
632 Bitmapset *result = NULL;
633 int cnt;
634
635 for (cnt = 0; cnt < nappinfos; cnt++)
636 {
637 AppendRelInfo *appinfo = appinfos[cnt];
638
639 /* Remove parent, add child */
640 if (bms_is_member(appinfo->parent_relid, relids))
641 {
642 /* Make a copy if we are changing the set. */
643 if (!result)
644 result = bms_copy(relids);
645
646 result = bms_del_member(result, appinfo->parent_relid);
647 result = bms_add_member(result, appinfo->child_relid);
648 }
649 }
650
651 /* If we made any changes, return the modified copy. */
652 if (result)
653 return result;
654
655 /* Otherwise, return the original set without modification. */
656 return relids;
657}
Bitmapset * bms_del_member(Bitmapset *a, int x)
Definition bitmapset.c:852
Bitmapset * bms_copy(const Bitmapset *a)
Definition bitmapset.c:122

References bms_add_member(), bms_copy(), bms_del_member(), bms_is_member(), and fb().

Referenced by adjust_appendrel_attrs_mutator(), adjust_child_relids_multilevel(), build_child_join_rel(), build_child_join_sjinfo(), and try_partitionwise_join().

◆ adjust_child_relids_multilevel()

Relids adjust_child_relids_multilevel ( PlannerInfo root,
Relids  relids,
RelOptInfo childrel,
RelOptInfo parentrel 
)

Definition at line 664 of file appendinfo.c.

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 */
675 if (!bms_overlap(relids, parentrel->relids))
676 return relids;
677
678 /* Recurse if immediate parent is not the top parent. */
679 if (childrel->parent != parentrel)
680 {
681 if (childrel->parent)
682 relids = adjust_child_relids_multilevel(root, relids,
683 childrel->parent,
684 parentrel);
685 else
686 elog(ERROR, "childrel is not a child of parentrel");
687 }
688
689 /* Now translate for this child. */
690 appinfos = find_appinfos_by_relids(root, childrel->relids, &nappinfos);
691
692 relids = adjust_child_relids(relids, nappinfos, appinfos);
693
694 pfree(appinfos);
695
696 return relids;
697}
Relids adjust_child_relids_multilevel(PlannerInfo *root, Relids relids, RelOptInfo *childrel, RelOptInfo *parentrel)
Definition appendinfo.c:664
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition bitmapset.c:575

References adjust_child_relids(), adjust_child_relids_multilevel(), bms_overlap(), elog, ERROR, fb(), find_appinfos_by_relids(), pfree(), and root.

Referenced by adjust_child_relids_multilevel(), and reparameterize_path_by_child().

◆ adjust_inherited_attnums()

List * adjust_inherited_attnums ( List attnums,
AppendRelInfo context 
)

Definition at line 704 of file appendinfo.c.

705{
706 List *result = NIL;
707 ListCell *lc;
708
709 /* This should only happen for an inheritance case, not UNION ALL */
711
712 /* Look up each attribute in the AppendRelInfo's translated_vars list */
713 foreach(lc, attnums)
714 {
716 Var *childvar;
717
718 /* Look up the translation of this column: it must be a Var */
719 if (parentattno <= 0 ||
721 elog(ERROR, "attribute %d of relation \"%s\" does not exist",
723 childvar = (Var *) list_nth(context->translated_vars, parentattno - 1);
724 if (childvar == NULL || !IsA(childvar, Var))
725 elog(ERROR, "attribute %d of relation \"%s\" does not exist",
727
728 result = lappend_int(result, childvar->varattno);
729 }
730 return result;
731}
int16 AttrNumber
Definition attnum.h:21
List * lappend_int(List *list, int datum)
Definition list.c:357
#define lfirst_int(lc)
Definition pg_list.h:173
List * translated_vars
Definition pathnodes.h:3316

References Assert, elog, ERROR, fb(), get_rel_name(), IsA, lappend_int(), lfirst_int, list_length(), list_nth(), NIL, OidIsValid, AppendRelInfo::parent_reloid, and AppendRelInfo::translated_vars.

Referenced by adjust_inherited_attnums_multilevel().

◆ adjust_inherited_attnums_multilevel()

List * adjust_inherited_attnums_multilevel ( PlannerInfo root,
List attnums,
Index  child_relid,
Index  top_parent_relid 
)

Definition at line 738 of file appendinfo.c.

740{
741 AppendRelInfo *appinfo = root->append_rel_array[child_relid];
742
743 if (!appinfo)
744 elog(ERROR, "child rel %d not found in append_rel_array", child_relid);
745
746 /* Recurse if immediate parent is not the top parent. */
747 if (appinfo->parent_relid != top_parent_relid)
748 attnums = adjust_inherited_attnums_multilevel(root, attnums,
749 appinfo->parent_relid,
751
752 /* Now translate for this child */
753 return adjust_inherited_attnums(attnums, appinfo);
754}
List * adjust_inherited_attnums_multilevel(PlannerInfo *root, List *attnums, Index child_relid, Index top_parent_relid)
Definition appendinfo.c:738
List * adjust_inherited_attnums(List *attnums, AppendRelInfo *context)
Definition appendinfo.c:704

References adjust_inherited_attnums(), adjust_inherited_attnums_multilevel(), elog, ERROR, fb(), and root.

Referenced by adjust_inherited_attnums_multilevel(), get_translated_update_targetlist(), and grouping_planner().

◆ distribute_row_identity_vars()

void distribute_row_identity_vars ( PlannerInfo root)

Definition at line 1040 of file appendinfo.c.

1041{
1042 Query *parse = root->parse;
1043 int result_relation = parse->resultRelation;
1044 RangeTblEntry *target_rte;
1046 ListCell *lc;
1047
1048 /*
1049 * There's nothing to do if this isn't an inherited UPDATE/DELETE/MERGE.
1050 */
1051 if (parse->commandType != CMD_UPDATE && parse->commandType != CMD_DELETE &&
1052 parse->commandType != CMD_MERGE)
1053 {
1054 Assert(root->row_identity_vars == NIL);
1055 return;
1056 }
1057 target_rte = rt_fetch(result_relation, parse->rtable);
1058 if (!target_rte->inh)
1059 {
1060 Assert(root->row_identity_vars == NIL);
1061 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 if (root->row_identity_vars == NIL)
1078 {
1080
1081 target_relation = table_open(target_rte->relid, NoLock);
1082 add_row_identity_columns(root, result_relation,
1083 target_rte, target_relation);
1085 build_base_rel_tlists(root, root->processed_tlist);
1086 /* There are no ROWID_VAR Vars in this case, so we're done. */
1087 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 target_rel = find_base_rel(root, result_relation);
1098
1099 foreach(lc, root->processed_tlist)
1100 {
1102 Var *var = (Var *) tle->expr;
1103
1104 if (var && IsA(var, Var) && var->varno == ROWID_VAR)
1105 {
1106 target_rel->reltarget->exprs =
1107 lappend(target_rel->reltarget->exprs, copyObject(var));
1108 /* reltarget cost and width will be computed later */
1109 }
1110 }
1111}
void add_row_identity_columns(PlannerInfo *root, Index rtindex, RangeTblEntry *target_rte, Relation target_relation)
Definition appendinfo.c:959
void parse(int)
Definition parse.c:49
void build_base_rel_tlists(PlannerInfo *root, List *final_tlist)
Definition initsplan.c:243
#define NoLock
Definition lockdefs.h:34
RelOptInfo * find_base_rel(PlannerInfo *root, int relid)
Definition relnode.c:544
void table_close(Relation relation, LOCKMODE lockmode)
Definition table.c:126
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition table.c:40

References add_row_identity_columns(), Assert, build_base_rel_tlists(), CMD_DELETE, CMD_MERGE, CMD_UPDATE, copyObject, fb(), find_base_rel(), RangeTblEntry::inh, IsA, lappend(), lfirst, NIL, NoLock, parse(), root, ROWID_VAR, rt_fetch, table_close(), table_open(), and Var::varno.

Referenced by query_planner().

◆ find_appinfos_by_relids()

AppendRelInfo ** find_appinfos_by_relids ( PlannerInfo root,
Relids  relids,
int nappinfos 
)

Definition at line 809 of file appendinfo.c.

810{
811 AppendRelInfo **appinfos;
812 int cnt = 0;
813 int i;
814
815 /* Allocate an array that's certainly big enough */
816 appinfos = palloc_array(AppendRelInfo *, bms_num_members(relids));
817
818 i = -1;
819 while ((i = bms_next_member(relids, i)) >= 0)
820 {
821 AppendRelInfo *appinfo = root->append_rel_array[i];
822
823 if (!appinfo)
824 {
825 /* Probably i is an OJ index, but let's check */
827 continue;
828 /* It's a base rel, but we lack an append_rel_array entry */
829 elog(ERROR, "child rel %d not found in append_rel_array", i);
830 }
831
832 appinfos[cnt++] = appinfo;
833 }
834 *nappinfos = cnt;
835 return appinfos;
836}
int bms_next_member(const Bitmapset *a, int prevbit)
Definition bitmapset.c:1290
int bms_num_members(const Bitmapset *a)
Definition bitmapset.c:744
#define palloc_array(type, count)
Definition fe_memutils.h:76
int i
Definition isn.c:77
RelOptInfo * find_base_rel_ignore_join(PlannerInfo *root, int relid)
Definition relnode.c:584

References bms_next_member(), bms_num_members(), elog, ERROR, fb(), find_base_rel_ignore_join(), i, palloc_array, and root.

Referenced by adjust_appendrel_attrs_multilevel(), adjust_child_relids_multilevel(), apply_scanjoin_target_to_paths(), build_child_join_sjinfo(), create_partitionwise_grouping_paths(), make_partitionedrel_pruneinfo(), and try_partitionwise_join().

◆ get_translated_update_targetlist()

void get_translated_update_targetlist ( PlannerInfo root,
Index  relid,
List **  processed_tlist,
List **  update_colnos 
)

Definition at line 766 of file appendinfo.c.

768{
769 /* This is pretty meaningless for commands other than UPDATE. */
770 Assert(root->parse->commandType == CMD_UPDATE);
771 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 *processed_tlist = copyObject(root->processed_tlist);
778 if (update_colnos)
779 *update_colnos = copyObject(root->update_colnos);
780 }
781 else
782 {
783 Assert(bms_is_member(relid, root->all_result_relids));
784 *processed_tlist = (List *)
786 (Node *) root->processed_tlist,
787 find_base_rel(root, relid),
788 find_base_rel(root, root->parse->resultRelation));
789 if (update_colnos)
790 *update_colnos =
792 relid,
793 root->parse->resultRelation);
794 }
795}

References adjust_appendrel_attrs_multilevel(), adjust_inherited_attnums_multilevel(), Assert, bms_is_member(), CMD_UPDATE, copyObject, find_base_rel(), and root.

Referenced by postgresPlanDirectModify().

◆ make_append_rel_info()

AppendRelInfo * make_append_rel_info ( Relation  parentrel,
Relation  childrel,
Index  parentRTindex,
Index  childRTindex 
)

Definition at line 52 of file appendinfo.c.

54{
56
57 appinfo->parent_relid = parentRTindex;
58 appinfo->child_relid = childRTindex;
59 appinfo->parent_reltype = parentrel->rd_rel->reltype;
60 appinfo->child_reltype = childrel->rd_rel->reltype;
62 appinfo->parent_reloid = RelationGetRelid(parentrel);
63
64 return appinfo;
65}
static void make_inh_translation_list(Relation oldrelation, Relation newrelation, Index newvarno, AppendRelInfo *appinfo)
Definition appendinfo.c:81
#define RelationGetRelid(relation)
Definition rel.h:514

References fb(), make_inh_translation_list(), makeNode, and RelationGetRelid.

Referenced by expand_single_inheritance_child().

◆ make_inh_translation_list()

static void make_inh_translation_list ( Relation  oldrelation,
Relation  newrelation,
Index  newvarno,
AppendRelInfo appinfo 
)
static

Definition at line 81 of file appendinfo.c.

84{
85 List *vars = NIL;
90 int oldnatts = old_tupdesc->natts;
91 int newnatts = new_tupdesc->natts;
92 int old_attno;
93 int new_attno = 0;
94
95 /* Initialize reverse-translation array with all entries zero */
96 appinfo->num_child_cols = newnatts;
97 appinfo->parent_colnos = pcolnos =
99
100 for (old_attno = 0; old_attno < oldnatts; old_attno++)
101 {
103 char *attname;
105 int32 atttypmod;
106 Oid attcollation;
107
109 if (att->attisdropped)
110 {
111 /* Just put NULL into this list entry */
112 vars = lappend(vars, NULL);
113 continue;
114 }
115 attname = NameStr(att->attname);
116 atttypid = att->atttypid;
117 atttypmod = att->atttypmod;
118 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 */
125 {
126 vars = lappend(vars, makeVar(newvarno,
127 (AttrNumber) (old_attno + 1),
128 atttypid,
129 atttypmod,
130 attcollation,
131 0));
133 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 if (new_attno >= newnatts ||
146 (att = TupleDescAttr(new_tupdesc, new_attno))->attisdropped ||
147 strcmp(attname, NameStr(att->attname)) != 0)
148 {
150
153 elog(ERROR, "could not find inherited attribute \"%s\" of relation \"%s\"",
158
160 }
161
162 /* Found it, check type and collation match */
163 if (atttypid != att->atttypid || atttypmod != att->atttypmod)
166 errmsg("attribute \"%s\" of relation \"%s\" does not match parent's type",
168 if (attcollation != att->attcollation)
171 errmsg("attribute \"%s\" of relation \"%s\" does not match parent's collation",
173
174 vars = lappend(vars, makeVar(newvarno,
175 (AttrNumber) (new_attno + 1),
176 atttypid,
177 atttypmod,
178 attcollation,
179 0));
181 new_attno++;
182 }
183
184 appinfo->translated_vars = vars;
185}
#define NameStr(name)
Definition c.h:834
int32_t int32
Definition c.h:611
int errcode(int sqlerrcode)
Definition elog.c:874
#define ereport(elevel,...)
Definition elog.h:150
#define HeapTupleIsValid(tuple)
Definition htup.h:78
static void * GETSTRUCT(const HeapTupleData *tuple)
void * palloc0(Size size)
Definition mcxt.c:1417
static char * errmsg
NameData attname
int16 attnum
FormData_pg_attribute * Form_pg_attribute
unsigned int Oid
#define RelationGetDescr(relation)
Definition rel.h:540
#define RelationGetRelationName(relation)
Definition rel.h:548
void ReleaseSysCache(HeapTuple tuple)
Definition syscache.c:264
HeapTuple SearchSysCacheAttName(Oid relid, const char *attname)
Definition syscache.c:475
static FormData_pg_attribute * TupleDescAttr(TupleDesc tupdesc, int i)
Definition tupdesc.h:178

References Assert, attname, attnum, elog, ereport, errcode(), errmsg, ERROR, fb(), GETSTRUCT(), HeapTupleIsValid, lappend(), makeVar(), NameStr, NIL, palloc0(), RelationGetDescr, RelationGetRelationName, RelationGetRelid, ReleaseSysCache(), SearchSysCacheAttName(), and TupleDescAttr().

Referenced by make_append_rel_info().