PostgreSQL Source Code  git master
appendinfo.h File Reference
#include "nodes/pathnodes.h"
#include "utils/relcache.h"
Include dependency graph for appendinfo.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

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, Relids child_relids, Relids top_parent_relids)
 
Relids adjust_child_relids (Relids relids, int nappinfos, AppendRelInfo **appinfos)
 
Relids adjust_child_relids_multilevel (PlannerInfo *root, Relids relids, Relids child_relids, Relids top_parent_relids)
 
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 *rowid_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 857 of file appendinfo.c.

References add_row_identity_var(), FdwRoutine::AddForeignUpdateTargets, Assert, CMD_DELETE, CMD_UPDATE, Query::commandType, GetFdwRoutineForRelation(), InvalidAttrNumber, InvalidOid, makeVar(), PlannerInfo::parse, RelationData::rd_rel, SelfItemPointerAttributeNumber, TriggerDesc::trig_delete_after_row, TriggerDesc::trig_delete_before_row, and RelationData::trigdesc.

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

860 {
861  CmdType commandType = root->parse->commandType;
862  char relkind = target_relation->rd_rel->relkind;
863  Var *var;
864 
865  Assert(commandType == CMD_UPDATE || commandType == CMD_DELETE);
866 
867  if (relkind == RELKIND_RELATION ||
868  relkind == RELKIND_MATVIEW ||
869  relkind == RELKIND_PARTITIONED_TABLE)
870  {
871  /*
872  * Emit CTID so that executor can find the row to update or delete.
873  */
874  var = makeVar(rtindex,
876  TIDOID,
877  -1,
878  InvalidOid,
879  0);
880  add_row_identity_var(root, var, rtindex, "ctid");
881  }
882  else if (relkind == RELKIND_FOREIGN_TABLE)
883  {
884  /*
885  * Let the foreign table's FDW add whatever junk TLEs it wants.
886  */
887  FdwRoutine *fdwroutine;
888 
889  fdwroutine = GetFdwRoutineForRelation(target_relation, false);
890 
891  if (fdwroutine->AddForeignUpdateTargets != NULL)
892  fdwroutine->AddForeignUpdateTargets(root, rtindex,
893  target_rte, target_relation);
894 
895  /*
896  * For UPDATE, we need to make the FDW fetch unchanged columns by
897  * asking it to fetch a whole-row Var. That's because the top-level
898  * targetlist only contains entries for changed columns, but
899  * ExecUpdate will need to build the complete new tuple. (Actually,
900  * we only really need this in UPDATEs that are not pushed to the
901  * remote side, but it's hard to tell if that will be the case at the
902  * point when this function is called.)
903  *
904  * We will also need the whole row if there are any row triggers, so
905  * that the executor will have the "old" row to pass to the trigger.
906  * Alas, this misses system columns.
907  */
908  if (commandType == CMD_UPDATE ||
909  (target_relation->trigdesc &&
910  (target_relation->trigdesc->trig_delete_after_row ||
911  target_relation->trigdesc->trig_delete_before_row)))
912  {
913  var = makeVar(rtindex,
915  RECORDOID,
916  -1,
917  InvalidOid,
918  0);
919  add_row_identity_var(root, var, rtindex, "wholerow");
920  }
921  }
922 }
Query * parse
Definition: pathnodes.h:161
Form_pg_class rd_rel
Definition: rel.h:110
Definition: primnodes.h:186
AddForeignUpdateTargets_function AddForeignUpdateTargets
Definition: fdwapi.h:230
TriggerDesc * trigdesc
Definition: rel.h:116
bool trig_delete_after_row
Definition: reltrigger.h:67
Var * makeVar(Index varno, AttrNumber varattno, Oid vartype, int32 vartypmod, Oid varcollid, Index varlevelsup)
Definition: makefuncs.c:66
#define InvalidOid
Definition: postgres_ext.h:36
CmdType commandType
Definition: parsenodes.h:120
#define Assert(condition)
Definition: c.h:804
void add_row_identity_var(PlannerInfo *root, Var *orig_var, Index rtindex, const char *rowid_name)
Definition: appendinfo.c:763
#define InvalidAttrNumber
Definition: attnum.h:23
FdwRoutine * GetFdwRoutineForRelation(Relation relation, bool makecopy)
Definition: foreign.c:427
#define SelfItemPointerAttributeNumber
Definition: sysattr.h:21
CmdType
Definition: nodes.h:682
bool trig_delete_before_row
Definition: reltrigger.h:66

◆ add_row_identity_var()

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

Definition at line 763 of file appendinfo.c.

References PlannerInfo::append_rel_array, Assert, bms_add_member(), bms_is_member(), bms_make_singleton(), copyObject, elog, equal(), ERROR, exprType(), exprTypmod(), get_typavgwidth(), IsA, lappend(), PlannerInfo::leaf_result_relids, lfirst, list_length(), makeNode, makeTargetEntry(), PlannerInfo::parse, PlannerInfo::processed_tlist, pstrdup(), Query::resultRelation, PlannerInfo::row_identity_vars, ROWID_VAR, RowIdentityVarInfo::rowidname, RowIdentityVarInfo::rowidrels, RowIdentityVarInfo::rowidvar, RowIdentityVarInfo::rowidwidth, Var::varlevelsup, and Var::varno.

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

765 {
766  TargetEntry *tle;
767  Var *rowid_var;
768  RowIdentityVarInfo *ridinfo;
769  ListCell *lc;
770 
771  /* For now, the argument must be just a Var of the given rtindex */
772  Assert(IsA(orig_var, Var));
773  Assert(orig_var->varno == rtindex);
774  Assert(orig_var->varlevelsup == 0);
775 
776  /*
777  * If we're doing non-inherited UPDATE/DELETE, there's little need for
778  * ROWID_VAR shenanigans. Just shove the presented Var into the
779  * processed_tlist, and we're done.
780  */
781  if (rtindex == root->parse->resultRelation)
782  {
783  tle = makeTargetEntry((Expr *) orig_var,
784  list_length(root->processed_tlist) + 1,
785  pstrdup(rowid_name),
786  true);
787  root->processed_tlist = lappend(root->processed_tlist, tle);
788  return;
789  }
790 
791  /*
792  * Otherwise, rtindex should reference a leaf target relation that's being
793  * added to the query during expand_inherited_rtentry().
794  */
795  Assert(bms_is_member(rtindex, root->leaf_result_relids));
796  Assert(root->append_rel_array[rtindex] != NULL);
797 
798  /*
799  * We have to find a matching RowIdentityVarInfo, or make one if there is
800  * none. To allow using equal() to match the vars, change the varno to
801  * ROWID_VAR, leaving all else alone.
802  */
803  rowid_var = copyObject(orig_var);
804  /* This could eventually become ChangeVarNodes() */
805  rowid_var->varno = ROWID_VAR;
806 
807  /* Look for an existing row-id column of the same name */
808  foreach(lc, root->row_identity_vars)
809  {
810  ridinfo = (RowIdentityVarInfo *) lfirst(lc);
811  if (strcmp(rowid_name, ridinfo->rowidname) != 0)
812  continue;
813  if (equal(rowid_var, ridinfo->rowidvar))
814  {
815  /* Found a match; we need only record that rtindex needs it too */
816  ridinfo->rowidrels = bms_add_member(ridinfo->rowidrels, rtindex);
817  return;
818  }
819  else
820  {
821  /* Ooops, can't handle this */
822  elog(ERROR, "conflicting uses of row-identity name \"%s\"",
823  rowid_name);
824  }
825  }
826 
827  /* No request yet, so add a new RowIdentityVarInfo */
828  ridinfo = makeNode(RowIdentityVarInfo);
829  ridinfo->rowidvar = copyObject(rowid_var);
830  /* for the moment, estimate width using just the datatype info */
831  ridinfo->rowidwidth = get_typavgwidth(exprType((Node *) rowid_var),
832  exprTypmod((Node *) rowid_var));
833  ridinfo->rowidname = pstrdup(rowid_name);
834  ridinfo->rowidrels = bms_make_singleton(rtindex);
835 
836  root->row_identity_vars = lappend(root->row_identity_vars, ridinfo);
837 
838  /* Change rowid_var into a reference to this row_identity_vars entry */
839  rowid_var->varattno = list_length(root->row_identity_vars);
840 
841  /* Push the ROWID_VAR reference variable into processed_tlist */
842  tle = makeTargetEntry((Expr *) rowid_var,
843  list_length(root->processed_tlist) + 1,
844  pstrdup(rowid_name),
845  true);
846  root->processed_tlist = lappend(root->processed_tlist, tle);
847 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:590
Query * parse
Definition: pathnodes.h:161
#define ROWID_VAR
Definition: primnodes.h:178
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:3105
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:267
char * pstrdup(const char *in)
Definition: mcxt.c:1299
int resultRelation
Definition: parsenodes.h:130
Definition: nodes.h:539
Definition: primnodes.h:186
#define ERROR
Definition: elog.h:46
List * row_identity_vars
Definition: pathnodes.h:285
Bitmapset * bms_make_singleton(int x)
Definition: bitmapset.c:186
TargetEntry * makeTargetEntry(Expr *expr, AttrNumber resno, char *resname, bool resjunk)
Definition: makefuncs.c:238
List * lappend(List *list, void *datum)
Definition: list.c:336
Index varno
Definition: primnodes.h:189
struct AppendRelInfo ** append_rel_array
Definition: pathnodes.h:201
int32 get_typavgwidth(Oid typid, int32 typmod)
Definition: lsyscache.c:2525
#define makeNode(_type_)
Definition: nodes.h:587
#define Assert(condition)
Definition: c.h:804
#define lfirst(lc)
Definition: pg_list.h:169
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:41
static int list_length(const List *l)
Definition: pg_list.h:149
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:736
#define elog(elevel,...)
Definition: elog.h:232
Relids leaf_result_relids
Definition: pathnodes.h:276
#define copyObject(obj)
Definition: nodes.h:655
List * processed_tlist
Definition: pathnodes.h:320
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:427

◆ adjust_appendrel_attrs()

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

Definition at line 195 of file appendinfo.c.

References adjust_appendrel_attrs_mutator(), adjust_appendrel_attrs_context::appinfos, Assert, IsA, adjust_appendrel_attrs_context::nappinfos, and adjust_appendrel_attrs_context::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().

197 {
199 
200  context.root = root;
201  context.nappinfos = nappinfos;
202  context.appinfos = appinfos;
203 
204  /* If there's nothing to adjust, don't call this function. */
205  Assert(nappinfos >= 1 && appinfos != NULL);
206 
207  /* Should never be translating a Query tree. */
208  Assert(node == NULL || !IsA(node, Query));
209 
210  return adjust_appendrel_attrs_mutator(node, &context);
211 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:590
static Node * adjust_appendrel_attrs_mutator(Node *node, adjust_appendrel_attrs_context *context)
Definition: appendinfo.c:214
#define Assert(condition)
Definition: c.h:804
AppendRelInfo ** appinfos
Definition: appendinfo.c:34

◆ adjust_appendrel_attrs_multilevel()

Node* adjust_appendrel_attrs_multilevel ( PlannerInfo root,
Node node,
Relids  child_relids,
Relids  top_parent_relids 
)

Definition at line 488 of file appendinfo.c.

References adjust_appendrel_attrs(), adjust_appendrel_attrs_multilevel(), Assert, bms_add_member(), bms_equal(), bms_num_members(), find_appinfos_by_relids(), AppendRelInfo::parent_relid, and pfree().

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

491 {
492  AppendRelInfo **appinfos;
493  Bitmapset *parent_relids = NULL;
494  int nappinfos;
495  int cnt;
496 
497  Assert(bms_num_members(child_relids) == bms_num_members(top_parent_relids));
498 
499  appinfos = find_appinfos_by_relids(root, child_relids, &nappinfos);
500 
501  /* Construct relids set for the immediate parent of given child. */
502  for (cnt = 0; cnt < nappinfos; cnt++)
503  {
504  AppendRelInfo *appinfo = appinfos[cnt];
505 
506  parent_relids = bms_add_member(parent_relids, appinfo->parent_relid);
507  }
508 
509  /* Recurse if immediate parent is not the top parent. */
510  if (!bms_equal(parent_relids, top_parent_relids))
511  node = adjust_appendrel_attrs_multilevel(root, node, parent_relids,
512  top_parent_relids);
513 
514  /* Now translate for this child */
515  node = adjust_appendrel_attrs(root, node, nappinfos, appinfos);
516 
517  pfree(appinfos);
518 
519  return node;
520 }
Node * adjust_appendrel_attrs_multilevel(PlannerInfo *root, Node *node, Relids child_relids, Relids top_parent_relids)
Definition: appendinfo.c:488
void pfree(void *pointer)
Definition: mcxt.c:1169
int bms_num_members(const Bitmapset *a)
Definition: bitmapset.c:646
AppendRelInfo ** find_appinfos_by_relids(PlannerInfo *root, Relids relids, int *nappinfos)
Definition: appendinfo.c:715
#define Assert(condition)
Definition: c.h:804
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:736
Index parent_relid
Definition: pathnodes.h:2295
bool bms_equal(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:94
Node * adjust_appendrel_attrs(PlannerInfo *root, Node *node, int nappinfos, AppendRelInfo **appinfos)
Definition: appendinfo.c:195

◆ adjust_child_relids()

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

Definition at line 527 of file appendinfo.c.

References bms_add_member(), bms_copy(), bms_del_member(), bms_is_member(), AppendRelInfo::child_relid, and AppendRelInfo::parent_relid.

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

528 {
529  Bitmapset *result = NULL;
530  int cnt;
531 
532  for (cnt = 0; cnt < nappinfos; cnt++)
533  {
534  AppendRelInfo *appinfo = appinfos[cnt];
535 
536  /* Remove parent, add child */
537  if (bms_is_member(appinfo->parent_relid, relids))
538  {
539  /* Make a copy if we are changing the set. */
540  if (!result)
541  result = bms_copy(relids);
542 
543  result = bms_del_member(result, appinfo->parent_relid);
544  result = bms_add_member(result, appinfo->child_relid);
545  }
546  }
547 
548  /* If we made any changes, return the modified copy. */
549  if (result)
550  return result;
551 
552  /* Otherwise, return the original set without modification. */
553  return relids;
554 }
Bitmapset * bms_copy(const Bitmapset *a)
Definition: bitmapset.c:74
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:736
Index child_relid
Definition: pathnodes.h:2296
Index parent_relid
Definition: pathnodes.h:2295
Bitmapset * bms_del_member(Bitmapset *a, int x)
Definition: bitmapset.c:773
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:427

◆ adjust_child_relids_multilevel()

Relids adjust_child_relids_multilevel ( PlannerInfo root,
Relids  relids,
Relids  child_relids,
Relids  top_parent_relids 
)

Definition at line 562 of file appendinfo.c.

References adjust_child_relids(), adjust_child_relids_multilevel(), bms_add_member(), bms_equal(), bms_free(), bms_overlap(), find_appinfos_by_relids(), AppendRelInfo::parent_relid, and pfree().

Referenced by add_child_join_rel_equivalences(), adjust_child_relids_multilevel(), and reparameterize_path_by_child().

564 {
565  AppendRelInfo **appinfos;
566  int nappinfos;
567  Relids parent_relids = NULL;
568  Relids result;
569  Relids tmp_result = NULL;
570  int cnt;
571 
572  /*
573  * If the given relids set doesn't contain any of the top parent relids,
574  * it will remain unchanged.
575  */
576  if (!bms_overlap(relids, top_parent_relids))
577  return relids;
578 
579  appinfos = find_appinfos_by_relids(root, child_relids, &nappinfos);
580 
581  /* Construct relids set for the immediate parent of the given child. */
582  for (cnt = 0; cnt < nappinfos; cnt++)
583  {
584  AppendRelInfo *appinfo = appinfos[cnt];
585 
586  parent_relids = bms_add_member(parent_relids, appinfo->parent_relid);
587  }
588 
589  /* Recurse if immediate parent is not the top parent. */
590  if (!bms_equal(parent_relids, top_parent_relids))
591  {
592  tmp_result = adjust_child_relids_multilevel(root, relids,
593  parent_relids,
594  top_parent_relids);
595  relids = tmp_result;
596  }
597 
598  result = adjust_child_relids(relids, nappinfos, appinfos);
599 
600  /* Free memory consumed by any intermediate result. */
601  if (tmp_result)
602  bms_free(tmp_result);
603  bms_free(parent_relids);
604  pfree(appinfos);
605 
606  return result;
607 }
Relids adjust_child_relids_multilevel(PlannerInfo *root, Relids relids, Relids child_relids, Relids top_parent_relids)
Definition: appendinfo.c:562
void pfree(void *pointer)
Definition: mcxt.c:1169
AppendRelInfo ** find_appinfos_by_relids(PlannerInfo *root, Relids relids, int *nappinfos)
Definition: appendinfo.c:715
void bms_free(Bitmapset *a)
Definition: bitmapset.c:208
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:736
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:494
Relids adjust_child_relids(Relids relids, int nappinfos, AppendRelInfo **appinfos)
Definition: appendinfo.c:527
Index parent_relid
Definition: pathnodes.h:2295
bool bms_equal(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:94

◆ adjust_inherited_attnums()

List* adjust_inherited_attnums ( List attnums,
AppendRelInfo context 
)

Definition at line 614 of file appendinfo.c.

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

Referenced by adjust_inherited_attnums_multilevel().

615 {
616  List *result = NIL;
617  ListCell *lc;
618 
619  /* This should only happen for an inheritance case, not UNION ALL */
620  Assert(OidIsValid(context->parent_reloid));
621 
622  /* Look up each attribute in the AppendRelInfo's translated_vars list */
623  foreach(lc, attnums)
624  {
625  AttrNumber parentattno = lfirst_int(lc);
626  Var *childvar;
627 
628  /* Look up the translation of this column: it must be a Var */
629  if (parentattno <= 0 ||
630  parentattno > list_length(context->translated_vars))
631  elog(ERROR, "attribute %d of relation \"%s\" does not exist",
632  parentattno, get_rel_name(context->parent_reloid));
633  childvar = (Var *) list_nth(context->translated_vars, parentattno - 1);
634  if (childvar == NULL || !IsA(childvar, Var))
635  elog(ERROR, "attribute %d of relation \"%s\" does not exist",
636  parentattno, get_rel_name(context->parent_reloid));
637 
638  result = lappend_int(result, childvar->varattno);
639  }
640  return result;
641 }
#define NIL
Definition: pg_list.h:65
#define IsA(nodeptr, _type_)
Definition: nodes.h:590
AttrNumber varattno
Definition: primnodes.h:191
Definition: primnodes.h:186
#define OidIsValid(objectId)
Definition: c.h:710
List * translated_vars
Definition: pathnodes.h:2323
#define ERROR
Definition: elog.h:46
#define lfirst_int(lc)
Definition: pg_list.h:170
static void * list_nth(const List *list, int n)
Definition: pg_list.h:278
List * lappend_int(List *list, int datum)
Definition: list.c:354
#define Assert(condition)
Definition: c.h:804
static int list_length(const List *l)
Definition: pg_list.h:149
#define elog(elevel,...)
Definition: elog.h:232
Definition: pg_list.h:50
char * get_rel_name(Oid relid)
Definition: lsyscache.c:1899
int16 AttrNumber
Definition: attnum.h:21

◆ adjust_inherited_attnums_multilevel()

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

Definition at line 648 of file appendinfo.c.

References adjust_inherited_attnums(), adjust_inherited_attnums_multilevel(), PlannerInfo::append_rel_array, elog, ERROR, and AppendRelInfo::parent_relid.

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

650 {
651  AppendRelInfo *appinfo = root->append_rel_array[child_relid];
652 
653  if (!appinfo)
654  elog(ERROR, "child rel %d not found in append_rel_array", child_relid);
655 
656  /* Recurse if immediate parent is not the top parent. */
657  if (appinfo->parent_relid != top_parent_relid)
658  attnums = adjust_inherited_attnums_multilevel(root, attnums,
659  appinfo->parent_relid,
660  top_parent_relid);
661 
662  /* Now translate for this child */
663  return adjust_inherited_attnums(attnums, appinfo);
664 }
List * adjust_inherited_attnums_multilevel(PlannerInfo *root, List *attnums, Index child_relid, Index top_parent_relid)
Definition: appendinfo.c:648
#define ERROR
Definition: elog.h:46
struct AppendRelInfo ** append_rel_array
Definition: pathnodes.h:201
#define elog(elevel,...)
Definition: elog.h:232
List * adjust_inherited_attnums(List *attnums, AppendRelInfo *context)
Definition: appendinfo.c:614
Index parent_relid
Definition: pathnodes.h:2295

◆ distribute_row_identity_vars()

void distribute_row_identity_vars ( PlannerInfo root)

Definition at line 937 of file appendinfo.c.

References add_row_identity_columns(), Assert, CMD_DELETE, CMD_UPDATE, Query::commandType, copyObject, TargetEntry::expr, PathTarget::exprs, find_base_rel(), RangeTblEntry::inh, IsA, lappend(), lfirst, NIL, NoLock, parse(), PlannerInfo::parse, PlannerInfo::processed_tlist, RangeTblEntry::relid, RelOptInfo::reltarget, Query::resultRelation, PlannerInfo::row_identity_vars, ROWID_VAR, rt_fetch, Query::rtable, table_close(), table_open(), and Var::varno.

Referenced by query_planner().

938 {
939  Query *parse = root->parse;
940  int result_relation = parse->resultRelation;
941  RangeTblEntry *target_rte;
942  RelOptInfo *target_rel;
943  ListCell *lc;
944 
945  /* There's nothing to do if this isn't an inherited UPDATE/DELETE. */
946  if (parse->commandType != CMD_UPDATE && parse->commandType != CMD_DELETE)
947  {
948  Assert(root->row_identity_vars == NIL);
949  return;
950  }
951  target_rte = rt_fetch(result_relation, parse->rtable);
952  if (!target_rte->inh)
953  {
954  Assert(root->row_identity_vars == NIL);
955  return;
956  }
957 
958  /*
959  * Ordinarily, we expect that leaf result relation(s) will have added some
960  * ROWID_VAR Vars to the query. However, it's possible that constraint
961  * exclusion suppressed every leaf relation. The executor will get upset
962  * if the plan has no row identity columns at all, even though it will
963  * certainly process no rows. Handle this edge case by re-opening the top
964  * result relation and adding the row identity columns it would have used,
965  * as preprocess_targetlist() would have done if it weren't marked "inh".
966  * (This is a bit ugly, but it seems better to confine the ugliness and
967  * extra cycles to this unusual corner case.) We needn't worry about
968  * fixing the rel's reltarget, as that won't affect the finished plan.
969  */
970  if (root->row_identity_vars == NIL)
971  {
972  Relation target_relation;
973 
974  target_relation = table_open(target_rte->relid, NoLock);
975  add_row_identity_columns(root, result_relation,
976  target_rte, target_relation);
977  table_close(target_relation, NoLock);
978  return;
979  }
980 
981  /*
982  * Dig through the processed_tlist to find the ROWID_VAR reference Vars,
983  * and forcibly copy them into the reltarget list of the topmost target
984  * relation. That's sufficient because they'll be copied to the
985  * individual leaf target rels (with appropriate translation) later,
986  * during appendrel expansion --- see set_append_rel_size().
987  */
988  target_rel = find_base_rel(root, result_relation);
989 
990  foreach(lc, root->processed_tlist)
991  {
992  TargetEntry *tle = lfirst(lc);
993  Var *var = (Var *) tle->expr;
994 
995  if (var && IsA(var, Var) && var->varno == ROWID_VAR)
996  {
997  target_rel->reltarget->exprs =
998  lappend(target_rel->reltarget->exprs, copyObject(var));
999  /* reltarget cost and width will be computed later */
1000  }
1001  }
1002 }
#define NIL
Definition: pg_list.h:65
#define IsA(nodeptr, _type_)
Definition: nodes.h:590
Query * parse
Definition: pathnodes.h:161
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
#define ROWID_VAR
Definition: primnodes.h:178
int resultRelation
Definition: parsenodes.h:130
Definition: primnodes.h:186
List * rtable
Definition: parsenodes.h:147
List * row_identity_vars
Definition: pathnodes.h:285
#define NoLock
Definition: lockdefs.h:34
#define rt_fetch(rangetable_index, rangetable)
Definition: parsetree.h:31
List * lappend(List *list, void *datum)
Definition: list.c:336
Index varno
Definition: primnodes.h:189
List * exprs
Definition: pathnodes.h:1102
CmdType commandType
Definition: parsenodes.h:120
#define Assert(condition)
Definition: c.h:804
#define lfirst(lc)
Definition: pg_list.h:169
Expr * expr
Definition: primnodes.h:1444
RelOptInfo * find_base_rel(PlannerInfo *root, int relid)
Definition: relnode.c:374
#define copyObject(obj)
Definition: nodes.h:655
List * processed_tlist
Definition: pathnodes.h:320
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
struct PathTarget * reltarget
Definition: pathnodes.h:687
void add_row_identity_columns(PlannerInfo *root, Index rtindex, RangeTblEntry *target_rte, Relation target_relation)
Definition: appendinfo.c:857
static struct subre * parse(struct vars *, int, int, struct state *, struct state *)
Definition: regcomp.c:666

◆ find_appinfos_by_relids()

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

Definition at line 715 of file appendinfo.c.

References PlannerInfo::append_rel_array, bms_next_member(), bms_num_members(), elog, ERROR, i, and palloc().

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

716 {
717  AppendRelInfo **appinfos;
718  int cnt = 0;
719  int i;
720 
721  *nappinfos = bms_num_members(relids);
722  appinfos = (AppendRelInfo **) palloc(sizeof(AppendRelInfo *) * *nappinfos);
723 
724  i = -1;
725  while ((i = bms_next_member(relids, i)) >= 0)
726  {
727  AppendRelInfo *appinfo = root->append_rel_array[i];
728 
729  if (!appinfo)
730  elog(ERROR, "child rel %d not found in append_rel_array", i);
731 
732  appinfos[cnt++] = appinfo;
733  }
734  return appinfos;
735 }
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1043
#define ERROR
Definition: elog.h:46
int bms_num_members(const Bitmapset *a)
Definition: bitmapset.c:646
struct AppendRelInfo ** append_rel_array
Definition: pathnodes.h:201
void * palloc(Size size)
Definition: mcxt.c:1062
#define elog(elevel,...)
Definition: elog.h:232
int i

◆ get_translated_update_targetlist()

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

Definition at line 676 of file appendinfo.c.

References adjust_appendrel_attrs_multilevel(), adjust_inherited_attnums_multilevel(), PlannerInfo::all_result_relids, Assert, bms_is_member(), bms_make_singleton(), CMD_UPDATE, Query::commandType, copyObject, PlannerInfo::parse, PlannerInfo::processed_tlist, Query::resultRelation, and PlannerInfo::update_colnos.

Referenced by postgresPlanDirectModify().

678 {
679  /* This is pretty meaningless for commands other than UPDATE. */
680  Assert(root->parse->commandType == CMD_UPDATE);
681  if (relid == root->parse->resultRelation)
682  {
683  /*
684  * Non-inheritance case, so it's easy. The caller might be expecting
685  * a tree it can scribble on, though, so copy.
686  */
687  *processed_tlist = copyObject(root->processed_tlist);
688  if (update_colnos)
689  *update_colnos = copyObject(root->update_colnos);
690  }
691  else
692  {
693  Assert(bms_is_member(relid, root->all_result_relids));
694  *processed_tlist = (List *)
696  (Node *) root->processed_tlist,
697  bms_make_singleton(relid),
699  if (update_colnos)
700  *update_colnos =
702  relid,
703  root->parse->resultRelation);
704  }
705 }
Relids all_result_relids
Definition: pathnodes.h:275
Query * parse
Definition: pathnodes.h:161
List * adjust_inherited_attnums_multilevel(PlannerInfo *root, List *attnums, Index child_relid, Index top_parent_relid)
Definition: appendinfo.c:648
int resultRelation
Definition: parsenodes.h:130
Definition: nodes.h:539
Node * adjust_appendrel_attrs_multilevel(PlannerInfo *root, Node *node, Relids child_relids, Relids top_parent_relids)
Definition: appendinfo.c:488
Bitmapset * bms_make_singleton(int x)
Definition: bitmapset.c:186
CmdType commandType
Definition: parsenodes.h:120
#define Assert(condition)
Definition: c.h:804
#define copyObject(obj)
Definition: nodes.h:655
List * processed_tlist
Definition: pathnodes.h:320
Definition: pg_list.h:50
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:427
List * update_colnos
Definition: pathnodes.h:328

◆ make_append_rel_info()

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

Definition at line 50 of file appendinfo.c.

References AppendRelInfo::child_relid, AppendRelInfo::child_reltype, make_inh_translation_list(), makeNode, AppendRelInfo::parent_relid, AppendRelInfo::parent_reloid, AppendRelInfo::parent_reltype, RelationData::rd_rel, and RelationGetRelid.

Referenced by expand_single_inheritance_child().

52 {
54 
55  appinfo->parent_relid = parentRTindex;
56  appinfo->child_relid = childRTindex;
57  appinfo->parent_reltype = parentrel->rd_rel->reltype;
58  appinfo->child_reltype = childrel->rd_rel->reltype;
59  make_inh_translation_list(parentrel, childrel, childRTindex, appinfo);
60  appinfo->parent_reloid = RelationGetRelid(parentrel);
61 
62  return appinfo;
63 }
static void make_inh_translation_list(Relation oldrelation, Relation newrelation, Index newvarno, AppendRelInfo *appinfo)
Definition: appendinfo.c:79
Form_pg_class rd_rel
Definition: rel.h:110
Oid parent_reltype
Definition: pathnodes.h:2304
#define makeNode(_type_)
Definition: nodes.h:587
Index child_relid
Definition: pathnodes.h:2296
Index parent_relid
Definition: pathnodes.h:2295
#define RelationGetRelid(relation)
Definition: rel.h:457