/* *make_inh_translation_list *BuildthelistoftranslationsfromparentVarstochildVarsfor *aninheritancechild,aswellasareverse-translationarray. * *Thereverse-translationarrayhasanentryforeachchildrelation *column,whichiseitherthe1-basedindexofthecorrespondingparent *column,or0ifthere'snomatch(thathappensfordroppedchildcolumns, *aswellaschildcolumnsbeyondthoseoftheparent,whichareallowedin *traditionalinheritancethoughnotpartitioning). * *Forparanoia'ssake,wematchtype/collationaswellasattributename.
*/ staticvoid
make_inh_translation_list(Relation oldrelation, Relation newrelation,
Index newvarno,
AppendRelInfo *appinfo)
{
List *vars = NIL;
AttrNumber *pcolnos;
TupleDesc old_tupdesc = RelationGetDescr(oldrelation);
TupleDesc new_tupdesc = RelationGetDescr(newrelation);
Oid new_relid = RelationGetRelid(newrelation); int oldnatts = old_tupdesc->natts; int newnatts = new_tupdesc->natts; int old_attno; int new_attno = 0;
/* Initialize reverse-translation array with all entries zero */
appinfo->num_child_cols = newnatts;
appinfo->parent_colnos = pcolnos =
(AttrNumber *) palloc0(newnatts * sizeof(AttrNumber));
for (old_attno = 0; old_attno < oldnatts; old_attno++)
{
Form_pg_attribute att; char *attname;
Oid atttypid;
int32 atttypmod;
Oid attcollation;
att = TupleDescAttr(old_tupdesc, old_attno); if (att->attisdropped)
{ /* Just put NULL into this list entry */
vars = lappend(vars, NULL); continue;
}
attname = NameStr(att->attname);
atttypid = att->atttypid;
atttypmod = att->atttypmod;
attcollation = att->attcollation;
/* Found it, check type and collation match */ if (atttypid != att->atttypid || atttypmod != att->atttypmod)
ereport(ERROR,
(errcode(ERRCODE_INVALID_COLUMN_DEFINITION),
errmsg("attribute \"%s\" of relation \"%s\" does not match parent's type",
attname, RelationGetRelationName(newrelation)))); if (attcollation != att->attcollation)
ereport(ERROR,
(errcode(ERRCODE_INVALID_COLUMN_DEFINITION),
errmsg("attribute \"%s\" of relation \"%s\" does not match parent's collation",
attname, RelationGetRelationName(newrelation))));
for (cnt = 0; cnt < nappinfos; cnt++)
{ if (var->varno == appinfos[cnt]->parent_relid)
{
appinfo = appinfos[cnt]; break;
}
}
if (appinfo)
{
var->varno = appinfo->child_relid; /* it's now a generated Var, so drop any syntactic labeling */
var->varnosyn = 0;
var->varattnosyn = 0; if (var->varattno > 0)
{
Node *newnode;
if (var->varattno > list_length(appinfo->translated_vars))
elog(ERROR, "attribute %d of relation \"%s\" does not exist",
var->varattno, get_rel_name(appinfo->parent_reloid));
newnode = copyObject(list_nth(appinfo->translated_vars,
var->varattno - 1)); if (newnode == NULL)
elog(ERROR, "attribute %d of relation \"%s\" does not exist",
var->varattno, get_rel_name(appinfo->parent_reloid)); if (IsA(newnode, Var))
{
Var *newvar = (Var *) newnode;
newvar->varreturningtype = var->varreturningtype;
newvar->varnullingrels = bms_add_members(newvar->varnullingrels,
var->varnullingrels);
} else
{ if (var->varreturningtype != VAR_RETURNING_DEFAULT)
elog(ERROR, "failed to apply returningtype to a non-Var"); if (var->varnullingrels != NULL)
elog(ERROR, "failed to apply nullingrels to a non-Var");
} return newnode;
} elseif (var->varattno == 0)
{ /* *Whole-rowVar:ifwearedealingwithnamedrowtypes,we *canuseawhole-rowVarforthechildtableplusacoercion *steptoconvertthetuplelayouttotheparent'srowtype. *OtherwisewehavetogenerateaRowExpr.
*/ if (OidIsValid(appinfo->child_reltype))
{
Assert(var->vartype == appinfo->parent_reltype); if (appinfo->parent_reltype != appinfo->child_reltype)
{
ConvertRowtypeExpr *r = makeNode(ConvertRowtypeExpr);
r->arg = (Expr *) var;
r->resulttype = appinfo->parent_reltype;
r->convertformat = COERCE_IMPLICIT_CAST;
r->location = -1; /* Make sure the Var node has the right type ID, too */
var->vartype = appinfo->child_reltype; return (Node *) r;
}
} else
{ /* *BuildaRowExprcontainingthetranslatedvariables. * *Inpracticevar->vartypewillalwaysbeRECORDOIDhere, *soweneedtocomeupwithsomesuitablecolumnnames. *WeusetheparentRTE'scolumnnames. * *Note:wecan'tgethereforinheritancecases,sothere *isnoneedtoworrythattranslated_varsmightcontain *somedummyNULLs.
*/
RowExpr *rowexpr;
List *fields;
RangeTblEntry *rte;
if (var->varreturningtype != VAR_RETURNING_DEFAULT)
elog(ERROR, "failed to apply returningtype to a non-Var"); if (var->varnullingrels != NULL)
elog(ERROR, "failed to apply nullingrels to a non-Var");
return (Node *) rowexpr;
}
} /* system attributes don't need any other translation */
} elseif (var->varno == ROWID_VAR)
{ /* *Ifit'saROWID_VARplaceholder,seeifwe'vereachedaleaf *targetrel,forwhichwecantranslatetheVartoaspecific *instantiation.Weshouldneverbeaskedtotranslatetoaset *ofrelidscontainingmorethanoneleaftargetrel,sothe *answerwillbeunique.Ifwe'restillconsideringnon-leaf *inheritancelevels,returntheROWID_VARVaras-is.
*/
Relids leaf_result_relids = context->root->leaf_result_relids;
Index leaf_relid = 0;
for (cnt = 0; cnt < nappinfos; cnt++)
{ if (bms_is_member(appinfos[cnt]->child_relid,
leaf_result_relids))
{ if (leaf_relid)
elog(ERROR, "cannot translate to multiple leaf relids");
leaf_relid = appinfos[cnt]->child_relid;
}
}
/* *NOTE:wedonotneedtorecurseintosublinks,becausetheyshould *alreadyhavebeenconvertedtosubplansbeforeweseethem.
*/
Assert(!IsA(node, SubLink));
Assert(!IsA(node, Query)); /* We should never see these Query substructures, either. */
Assert(!IsA(node, RangeTblRef));
Assert(!IsA(node, JoinExpr));
/* Recurse if immediate parent is not the top parent. */ if (childrel->parent != parentrel)
{ if (childrel->parent)
node = adjust_appendrel_attrs_multilevel(root, node,
childrel->parent,
parentrel); else
elog(ERROR, "childrel is not a child of parentrel");
}
/* Now translate for this child. */
appinfos = find_appinfos_by_relids(root, childrel->relids, &nappinfos);
/* Remove parent, add child */ if (bms_is_member(appinfo->parent_relid, relids))
{ /* Make a copy if we are changing the set. */ if (!result)
result = bms_copy(relids);
result = bms_del_member(result, appinfo->parent_relid);
result = bms_add_member(result, appinfo->child_relid);
}
}
/* If we made any changes, return the modified copy. */ if (result) return result;
/* Otherwise, return the original set without modification. */ return relids;
}
/* *Ifthegivenrelidssetdoesn'tcontainanyoftheparentrelids,it *willremainunchanged.
*/ if (!bms_overlap(relids, parentrel->relids)) return relids;
/* Recurse if immediate parent is not the top parent. */ if (childrel->parent != parentrel)
{ if (childrel->parent)
relids = adjust_child_relids_multilevel(root, relids,
childrel->parent,
parentrel); else
elog(ERROR, "childrel is not a child of parentrel");
}
/* Now translate for this child. */
appinfos = find_appinfos_by_relids(root, childrel->relids, &nappinfos);
/* *adjust_inherited_attnums *Translateanintegerlistofattributenumbersfromparenttochild.
*/
List *
adjust_inherited_attnums(List *attnums, AppendRelInfo *context)
{
List *result = NIL;
ListCell *lc;
/* This should only happen for an inheritance case, not UNION ALL */
Assert(OidIsValid(context->parent_reloid));
/* Look up each attribute in the AppendRelInfo's translated_vars list */
foreach(lc, attnums)
{
AttrNumber parentattno = lfirst_int(lc);
Var *childvar;
/* Look up the translation of this column: it must be a Var */ if (parentattno <= 0 ||
parentattno > list_length(context->translated_vars))
elog(ERROR, "attribute %d of relation \"%s\" does not exist",
parentattno, get_rel_name(context->parent_reloid));
childvar = (Var *) list_nth(context->translated_vars, parentattno - 1); if (childvar == NULL || !IsA(childvar, Var))
elog(ERROR, "attribute %d of relation \"%s\" does not exist",
parentattno, get_rel_name(context->parent_reloid));
result = lappend_int(result, childvar->varattno);
} return result;
}
/* *adjust_inherited_attnums_multilevel *Asabove,buttraversemultipleinheritancelevelsasneeded.
*/
List *
adjust_inherited_attnums_multilevel(PlannerInfo *root, List *attnums,
Index child_relid, Index top_parent_relid)
{
AppendRelInfo *appinfo = root->append_rel_array[child_relid];
if (!appinfo)
elog(ERROR, "child rel %d not found in append_rel_array", child_relid);
/* Recurse if immediate parent is not the top parent. */ if (appinfo->parent_relid != top_parent_relid)
attnums = adjust_inherited_attnums_multilevel(root, attnums,
appinfo->parent_relid,
top_parent_relid);
/* Now translate for this child */ return adjust_inherited_attnums(attnums, appinfo);
}
/* *get_translated_update_targetlist *Gettheprocessed_tlistofanUPDATEquery,translatedasneededto *matchachildtargetrelation. * *Optionallyalsoreturnthelistoftargetcolumnnumberstranslated *tothistargetrelation.(Theresnosinprocessed_tlistMUSTNOTbe *reliedonforthispurpose.)
*/ void
get_translated_update_targetlist(PlannerInfo *root, Index relid,
List **processed_tlist, List **update_colnos)
{ /* This is pretty meaningless for commands other than UPDATE. */
Assert(root->parse->commandType == CMD_UPDATE); if (relid == root->parse->resultRelation)
{ /* *Non-inheritancecase,soit'seasy.Thecallermightbeexpecting *atreeitcanscribbleon,though,socopy.
*/
*processed_tlist = copyObject(root->processed_tlist); if (update_colnos)
*update_colnos = copyObject(root->update_colnos);
} else
{
Assert(bms_is_member(relid, root->all_result_relids));
*processed_tlist = (List *)
adjust_appendrel_attrs_multilevel(root,
(Node *) root->processed_tlist,
find_base_rel(root, relid),
find_base_rel(root, root->parse->resultRelation)); if (update_colnos)
*update_colnos =
adjust_inherited_attnums_multilevel(root, root->update_colnos,
relid,
root->parse->resultRelation);
}
}
/* *find_appinfos_by_relids *FindAppendRelInfostructuresforbaserelationslistedinrelids. * *Therelidsargumentistypicallyajoinrelation'srelids,whichcan *includeouter-joinRTindexesinadditiontobaserels.Wesilently *ignoretheouterjoins. * *TheAppendRelInfosarereturnedinanarray,whichcanbepfree'dbythe *caller.*nappinfosissettothenumberofentriesinthearray.
*/
AppendRelInfo **
find_appinfos_by_relids(PlannerInfo *root, Relids relids, int *nappinfos)
{
AppendRelInfo **appinfos; int cnt = 0; int i;
/* Allocate an array that's certainly big enough */
appinfos = (AppendRelInfo **)
palloc(sizeof(AppendRelInfo *) * bms_num_members(relids));
i = -1; while ((i = bms_next_member(relids, i)) >= 0)
{
AppendRelInfo *appinfo = root->append_rel_array[i];
if (!appinfo)
{ /* Probably i is an OJ index, but let's check */ if (find_base_rel_ignore_join(root, i) == NULL) continue; /* It's a base rel, but we lack an append_rel_array entry */
elog(ERROR, "child rel %d not found in append_rel_array", i);
}
/* For now, the argument must be just a Var of the given rtindex */
Assert(IsA(orig_var, Var));
Assert(orig_var->varno == rtindex);
Assert(orig_var->varlevelsup == 0);
Assert(orig_var->varnullingrels == NULL);
/* *WehavetofindamatchingRowIdentityVarInfo,ormakeoneifthereis *none.Toallowusingequal()tomatchthevars,changethevarnoto *ROWID_VAR,leavingallelsealone.
*/
rowid_var = copyObject(orig_var); /* This could eventually become ChangeVarNodes() */
rowid_var->varno = ROWID_VAR;
/* Look for an existing row-id column of the same name */
foreach(lc, root->row_identity_vars)
{
ridinfo = (RowIdentityVarInfo *) lfirst(lc); if (strcmp(rowid_name, ridinfo->rowidname) != 0) continue; if (equal(rowid_var, ridinfo->rowidvar))
{ /* Found a match; we need only record that rtindex needs it too */
ridinfo->rowidrels = bms_add_member(ridinfo->rowidrels, rtindex); return;
} else
{ /* Ooops, can't handle this */
elog(ERROR, "conflicting uses of row-identity name \"%s\"",
rowid_name);
}
}
/* No request yet, so add a new RowIdentityVarInfo */
ridinfo = makeNode(RowIdentityVarInfo);
ridinfo->rowidvar = copyObject(rowid_var); /* for the moment, estimate width using just the datatype info */
ridinfo->rowidwidth = get_typavgwidth(exprType((Node *) rowid_var),
exprTypmod((Node *) rowid_var));
ridinfo->rowidname = pstrdup(rowid_name);
ridinfo->rowidrels = bms_make_singleton(rtindex);
target_relation = table_open(target_rte->relid, NoLock);
add_row_identity_columns(root, result_relation,
target_rte, target_relation);
table_close(target_relation, NoLock);
build_base_rel_tlists(root, root->processed_tlist); /* There are no ROWID_VAR Vars in this case, so we're done. */ return;
}
if (var && IsA(var, Var) && var->varno == ROWID_VAR)
{
target_rel->reltarget->exprs =
lappend(target_rel->reltarget->exprs, copyObject(var)); /* reltarget cost and width will be computed later */
}
}
}
Messung V0.5 in Prozent
¤ Diese beiden folgenden Angebotsgruppen bietet das Unternehmen0.31Angebot
(Wie Sie bei der Firma Beratungs- und Dienstleistungen beauftragen können 2026-08-08)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.