PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
appendinfo.c File Reference
#include "postgres.h"
#include "access/htup_details.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 955 of file appendinfo.c.

958{
959 CmdType commandType = root->parse->commandType;
960 char relkind = target_relation->rd_rel->relkind;
961 Var *var;
962
963 Assert(commandType == CMD_UPDATE || commandType == CMD_DELETE || commandType == CMD_MERGE);
964
965 if (relkind == RELKIND_RELATION ||
966 relkind == RELKIND_MATVIEW ||
967 relkind == RELKIND_PARTITIONED_TABLE)
968 {
969 /*
970 * Emit CTID so that executor can find the row to merge, update or
971 * delete.
972 */
973 var = makeVar(rtindex,
975 TIDOID,
976 -1,
978 0);
979 add_row_identity_var(root, var, rtindex, "ctid");
980 }
981 else if (relkind == RELKIND_FOREIGN_TABLE)
982 {
983 /*
984 * Let the foreign table's FDW add whatever junk TLEs it wants.
985 */
986 FdwRoutine *fdwroutine;
987
988 fdwroutine = GetFdwRoutineForRelation(target_relation, false);
989
990 if (fdwroutine->AddForeignUpdateTargets != NULL)
991 fdwroutine->AddForeignUpdateTargets(root, rtindex,
992 target_rte, target_relation);
993
994 /*
995 * For UPDATE, we need to make the FDW fetch unchanged columns by
996 * asking it to fetch a whole-row Var. That's because the top-level
997 * targetlist only contains entries for changed columns, but
998 * ExecUpdate will need to build the complete new tuple. (Actually,
999 * we only really need this in UPDATEs that are not pushed to the
1000 * remote side, but it's hard to tell if that will be the case at the
1001 * point when this function is called.)
1002 *
1003 * We will also need the whole row if there are any row triggers, so
1004 * that the executor will have the "old" row to pass to the trigger.
1005 * Alas, this misses system columns.
1006 */
1007 if (commandType == CMD_UPDATE ||
1008 (target_relation->trigdesc &&
1009 (target_relation->trigdesc->trig_delete_after_row ||
1010 target_relation->trigdesc->trig_delete_before_row)))
1011 {
1012 var = makeVar(rtindex,
1014 RECORDOID,
1015 -1,
1016 InvalidOid,
1017 0);
1018 add_row_identity_var(root, var, rtindex, "wholerow");
1019 }
1020 }
1021}
void add_row_identity_var(PlannerInfo *root, Var *orig_var, Index rtindex, const char *rowid_name)
Definition: appendinfo.c:860
#define InvalidAttrNumber
Definition: attnum.h:23
FdwRoutine * GetFdwRoutineForRelation(Relation relation, bool makecopy)
Definition: foreign.c:443
Assert(PointerIsAligned(start, uint64))
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
Definition: postgres_ext.h:37
tree ctl root
Definition: radixtree.h:1857
AddForeignUpdateTargets_function AddForeignUpdateTargets
Definition: fdwapi.h:229
TriggerDesc * trigdesc
Definition: rel.h:117
Form_pg_class rd_rel
Definition: rel.h:111
bool trig_delete_before_row
Definition: reltrigger.h:66
bool trig_delete_after_row
Definition: reltrigger.h:67
Definition: primnodes.h:262
#define SelfItemPointerAttributeNumber
Definition: sysattr.h:21

References add_row_identity_var(), FdwRoutine::AddForeignUpdateTargets, Assert(), CMD_DELETE, CMD_MERGE, CMD_UPDATE, GetFdwRoutineForRelation(), InvalidAttrNumber, InvalidOid, makeVar(), RelationData::rd_rel, root, 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().

◆ add_row_identity_var()

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

Definition at line 860 of file appendinfo.c.

862{
863 TargetEntry *tle;
864 Var *rowid_var;
865 RowIdentityVarInfo *ridinfo;
866 ListCell *lc;
867
868 /* For now, the argument must be just a Var of the given rtindex */
869 Assert(IsA(orig_var, Var));
870 Assert(orig_var->varno == rtindex);
871 Assert(orig_var->varlevelsup == 0);
872 Assert(orig_var->varnullingrels == NULL);
873
874 /*
875 * If we're doing non-inherited UPDATE/DELETE/MERGE, there's little need
876 * for ROWID_VAR shenanigans. Just shove the presented Var into the
877 * processed_tlist, and we're done.
878 */
879 if (rtindex == root->parse->resultRelation)
880 {
881 tle = makeTargetEntry((Expr *) orig_var,
882 list_length(root->processed_tlist) + 1,
883 pstrdup(rowid_name),
884 true);
885 root->processed_tlist = lappend(root->processed_tlist, tle);
886 return;
887 }
888
889 /*
890 * Otherwise, rtindex should reference a leaf target relation that's being
891 * added to the query during expand_inherited_rtentry().
892 */
893 Assert(bms_is_member(rtindex, root->leaf_result_relids));
894 Assert(root->append_rel_array[rtindex] != NULL);
895
896 /*
897 * We have to find a matching RowIdentityVarInfo, or make one if there is
898 * none. To allow using equal() to match the vars, change the varno to
899 * ROWID_VAR, leaving all else alone.
900 */
901 rowid_var = copyObject(orig_var);
902 /* This could eventually become ChangeVarNodes() */
903 rowid_var->varno = ROWID_VAR;
904
905 /* Look for an existing row-id column of the same name */
906 foreach(lc, root->row_identity_vars)
907 {
908 ridinfo = (RowIdentityVarInfo *) lfirst(lc);
909 if (strcmp(rowid_name, ridinfo->rowidname) != 0)
910 continue;
911 if (equal(rowid_var, ridinfo->rowidvar))
912 {
913 /* Found a match; we need only record that rtindex needs it too */
914 ridinfo->rowidrels = bms_add_member(ridinfo->rowidrels, rtindex);
915 return;
916 }
917 else
918 {
919 /* Ooops, can't handle this */
920 elog(ERROR, "conflicting uses of row-identity name \"%s\"",
921 rowid_name);
922 }
923 }
924
925 /* No request yet, so add a new RowIdentityVarInfo */
926 ridinfo = makeNode(RowIdentityVarInfo);
927 ridinfo->rowidvar = copyObject(rowid_var);
928 /* for the moment, estimate width using just the datatype info */
929 ridinfo->rowidwidth = get_typavgwidth(exprType((Node *) rowid_var),
930 exprTypmod((Node *) rowid_var));
931 ridinfo->rowidname = pstrdup(rowid_name);
932 ridinfo->rowidrels = bms_make_singleton(rtindex);
933
934 root->row_identity_vars = lappend(root->row_identity_vars, ridinfo);
935
936 /* Change rowid_var into a reference to this row_identity_vars entry */
937 rowid_var->varattno = list_length(root->row_identity_vars);
938
939 /* Push the ROWID_VAR reference variable into processed_tlist */
940 tle = makeTargetEntry((Expr *) rowid_var,
941 list_length(root->processed_tlist) + 1,
942 pstrdup(rowid_name),
943 true);
944 root->processed_tlist = lappend(root->processed_tlist, tle);
945}
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:815
#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:2745
TargetEntry * makeTargetEntry(Expr *expr, AttrNumber resno, char *resname, bool resjunk)
Definition: makefuncs.c:289
char * pstrdup(const char *in)
Definition: mcxt.c:1759
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:301
#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:245
Definition: nodes.h:135
int varno
Definition: primnodes.h:269
Index varlevelsup
Definition: primnodes.h:294

References Assert(), bms_add_member(), bms_is_member(), bms_make_singleton(), copyObject, elog, equal(), ERROR, exprType(), exprTypmod(), get_typavgwidth(), IsA, lappend(), lfirst, list_length(), makeNode, makeTargetEntry(), pstrdup(), root, ROWID_VAR, RowIdentityVarInfo::rowidname, RowIdentityVarInfo::rowidrels, RowIdentityVarInfo::rowidvar, RowIdentityVarInfo::rowidwidth, Var::varattno, Var::varlevelsup, and Var::varno.

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 200 of file appendinfo.c.

202{
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}
static Node * adjust_appendrel_attrs_mutator(Node *node, adjust_appendrel_attrs_context *context)
Definition: appendinfo.c:219
AppendRelInfo ** appinfos
Definition: appendinfo.c:35

References adjust_appendrel_attrs_mutator(), adjust_appendrel_attrs_context::appinfos, Assert(), 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 592 of file appendinfo.c.

595{
596 AppendRelInfo **appinfos;
597 int nappinfos;
598
599 /* Recurse if immediate parent is not the top parent. */
600 if (childrel->parent != parentrel)
601 {
602 if (childrel->parent)
604 childrel->parent,
605 parentrel);
606 else
607 elog(ERROR, "childrel is not a child of parentrel");
608 }
609
610 /* Now translate for this child. */
611 appinfos = find_appinfos_by_relids(root, childrel->relids, &nappinfos);
612
613 node = adjust_appendrel_attrs(root, node, nappinfos, appinfos);
614
615 pfree(appinfos);
616
617 return node;
618}
AppendRelInfo ** find_appinfos_by_relids(PlannerInfo *root, Relids relids, int *nappinfos)
Definition: appendinfo.c:804
Node * adjust_appendrel_attrs(PlannerInfo *root, Node *node, int nappinfos, AppendRelInfo **appinfos)
Definition: appendinfo.c:200
Node * adjust_appendrel_attrs_multilevel(PlannerInfo *root, Node *node, RelOptInfo *childrel, RelOptInfo *parentrel)
Definition: appendinfo.c:592
void pfree(void *pointer)
Definition: mcxt.c:1594
Relids relids
Definition: pathnodes.h:927

References adjust_appendrel_attrs(), adjust_appendrel_attrs_multilevel(), elog, ERROR, find_appinfos_by_relids(), pfree(), RelOptInfo::relids, 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 219 of file appendinfo.c.

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 propagate var->varnullingrels into the translated
241 * Var.
242 *
243 * If var->varnullingrels isn't empty, and the translation wouldn't be
244 * a Var, we have to fail. One could imagine wrapping the translated
245 * expression in a PlaceHolderVar, but that won't work because this is
246 * typically used after freezing placeholders. Fortunately, the case
247 * appears unreachable at the moment. We can see nonempty
248 * var->varnullingrels here, but only in cases involving partitionwise
249 * joining, and in such cases the translations will always be Vars.
250 * (Non-Var translations occur only for appendrels made by flattening
251 * UNION ALL subqueries.) Should we need to make this work in future,
252 * a possible fix is to mandate that prepjointree.c create PHVs for
253 * all non-Var outputs of such subqueries, and then we could look up
254 * the pre-existing PHV here. Or perhaps just wrap the translations
255 * that way to begin with?
256 *
257 * If var->varreturningtype is not VAR_RETURNING_DEFAULT, then that
258 * also needs to be copied to the translated Var. That too would fail
259 * if the translation wasn't a Var, but that should never happen since
260 * a non-default var->varreturningtype is only used for Vars referring
261 * to the result relation, which should never be a flattened UNION ALL
262 * subquery.
263 */
264
265 for (cnt = 0; cnt < nappinfos; cnt++)
266 {
267 if (var->varno == appinfos[cnt]->parent_relid)
268 {
269 appinfo = appinfos[cnt];
270 break;
271 }
272 }
273
274 if (appinfo)
275 {
276 var->varno = appinfo->child_relid;
277 /* it's now a generated Var, so drop any syntactic labeling */
278 var->varnosyn = 0;
279 var->varattnosyn = 0;
280 if (var->varattno > 0)
281 {
282 Node *newnode;
283
284 if (var->varattno > list_length(appinfo->translated_vars))
285 elog(ERROR, "attribute %d of relation \"%s\" does not exist",
286 var->varattno, get_rel_name(appinfo->parent_reloid));
287 newnode = copyObject(list_nth(appinfo->translated_vars,
288 var->varattno - 1));
289 if (newnode == NULL)
290 elog(ERROR, "attribute %d of relation \"%s\" does not exist",
291 var->varattno, get_rel_name(appinfo->parent_reloid));
292 if (IsA(newnode, Var))
293 {
294 ((Var *) newnode)->varreturningtype = var->varreturningtype;
295 ((Var *) newnode)->varnullingrels = var->varnullingrels;
296 }
297 else
298 {
300 elog(ERROR, "failed to apply returningtype to a non-Var");
301 if (var->varnullingrels != NULL)
302 elog(ERROR, "failed to apply nullingrels to a non-Var");
303 }
304 return newnode;
305 }
306 else if (var->varattno == 0)
307 {
308 /*
309 * Whole-row Var: if we are dealing with named rowtypes, we
310 * can use a whole-row Var for the child table plus a coercion
311 * step to convert the tuple layout to the parent's rowtype.
312 * Otherwise we have to generate a RowExpr.
313 */
314 if (OidIsValid(appinfo->child_reltype))
315 {
316 Assert(var->vartype == appinfo->parent_reltype);
317 if (appinfo->parent_reltype != appinfo->child_reltype)
318 {
320
321 r->arg = (Expr *) var;
322 r->resulttype = appinfo->parent_reltype;
323 r->convertformat = COERCE_IMPLICIT_CAST;
324 r->location = -1;
325 /* Make sure the Var node has the right type ID, too */
326 var->vartype = appinfo->child_reltype;
327 return (Node *) r;
328 }
329 }
330 else
331 {
332 /*
333 * Build a RowExpr containing the translated variables.
334 *
335 * In practice var->vartype will always be RECORDOID here,
336 * so we need to come up with some suitable column names.
337 * We use the parent RTE's column names.
338 *
339 * Note: we can't get here for inheritance cases, so there
340 * is no need to worry that translated_vars might contain
341 * some dummy NULLs.
342 */
343 RowExpr *rowexpr;
344 List *fields;
345 RangeTblEntry *rte;
346
347 rte = rt_fetch(appinfo->parent_relid,
348 context->root->parse->rtable);
349 fields = copyObject(appinfo->translated_vars);
350 rowexpr = makeNode(RowExpr);
351 rowexpr->args = fields;
352 rowexpr->row_typeid = var->vartype;
353 rowexpr->row_format = COERCE_IMPLICIT_CAST;
354 rowexpr->colnames = copyObject(rte->eref->colnames);
355 rowexpr->location = -1;
356
358 elog(ERROR, "failed to apply returningtype to a non-Var");
359 if (var->varnullingrels != NULL)
360 elog(ERROR, "failed to apply nullingrels to a non-Var");
361
362 return (Node *) rowexpr;
363 }
364 }
365 /* system attributes don't need any other translation */
366 }
367 else if (var->varno == ROWID_VAR)
368 {
369 /*
370 * If it's a ROWID_VAR placeholder, see if we've reached a leaf
371 * target rel, for which we can translate the Var to a specific
372 * instantiation. We should never be asked to translate to a set
373 * of relids containing more than one leaf target rel, so the
374 * answer will be unique. If we're still considering non-leaf
375 * inheritance levels, return the ROWID_VAR Var as-is.
376 */
377 Relids leaf_result_relids = context->root->leaf_result_relids;
378 Index leaf_relid = 0;
379
380 for (cnt = 0; cnt < nappinfos; cnt++)
381 {
382 if (bms_is_member(appinfos[cnt]->child_relid,
383 leaf_result_relids))
384 {
385 if (leaf_relid)
386 elog(ERROR, "cannot translate to multiple leaf relids");
387 leaf_relid = appinfos[cnt]->child_relid;
388 }
389 }
390
391 if (leaf_relid)
392 {
394 list_nth(context->root->row_identity_vars, var->varattno - 1);
395
396 if (bms_is_member(leaf_relid, ridinfo->rowidrels))
397 {
398 /* Substitute the Var given in the RowIdentityVarInfo */
399 var = copyObject(ridinfo->rowidvar);
400 /* ... but use the correct relid */
401 var->varno = leaf_relid;
402 /* identity vars shouldn't have nulling rels */
403 Assert(var->varnullingrels == NULL);
404 /* varnosyn in the RowIdentityVarInfo is probably wrong */
405 var->varnosyn = 0;
406 var->varattnosyn = 0;
407 }
408 else
409 {
410 /*
411 * This leaf rel can't return the desired value, so
412 * substitute a NULL of the correct type.
413 */
414 return (Node *) makeNullConst(var->vartype,
415 var->vartypmod,
416 var->varcollid);
417 }
418 }
419 }
420 return (Node *) var;
421 }
422 if (IsA(node, CurrentOfExpr))
423 {
424 CurrentOfExpr *cexpr = (CurrentOfExpr *) copyObject(node);
425
426 for (cnt = 0; cnt < nappinfos; cnt++)
427 {
428 AppendRelInfo *appinfo = appinfos[cnt];
429
430 if (cexpr->cvarno == appinfo->parent_relid)
431 {
432 cexpr->cvarno = appinfo->child_relid;
433 break;
434 }
435 }
436 return (Node *) cexpr;
437 }
438 if (IsA(node, PlaceHolderVar))
439 {
440 /* Copy the PlaceHolderVar node with correct mutation of subnodes */
441 PlaceHolderVar *phv;
442
445 context);
446 /* now fix PlaceHolderVar's relid sets */
447 if (phv->phlevelsup == 0)
448 {
449 phv->phrels = adjust_child_relids(phv->phrels,
450 nappinfos, appinfos);
451 /* as above, we needn't touch phnullingrels */
452 }
453 return (Node *) phv;
454 }
455 /* Shouldn't need to handle planner auxiliary nodes here */
456 Assert(!IsA(node, SpecialJoinInfo));
457 Assert(!IsA(node, AppendRelInfo));
458 Assert(!IsA(node, PlaceHolderInfo));
459 Assert(!IsA(node, MinMaxAggInfo));
460
461 /*
462 * We have to process RestrictInfo nodes specially. (Note: although
463 * set_append_rel_pathlist will hide RestrictInfos in the parent's
464 * baserestrictinfo list from us, it doesn't hide those in joininfo.)
465 */
466 if (IsA(node, RestrictInfo))
467 {
468 RestrictInfo *oldinfo = (RestrictInfo *) node;
470
471 /* Copy all flat-copiable fields, notably including rinfo_serial */
472 memcpy(newinfo, oldinfo, sizeof(RestrictInfo));
473
474 /* Recursively fix the clause itself */
475 newinfo->clause = (Expr *)
476 adjust_appendrel_attrs_mutator((Node *) oldinfo->clause, context);
477
478 /* and the modified version, if an OR clause */
479 newinfo->orclause = (Expr *)
480 adjust_appendrel_attrs_mutator((Node *) oldinfo->orclause, context);
481
482 /* adjust relid sets too */
483 newinfo->clause_relids = adjust_child_relids(oldinfo->clause_relids,
484 context->nappinfos,
485 context->appinfos);
487 context->nappinfos,
488 context->appinfos);
490 context->nappinfos,
491 context->appinfos);
492 newinfo->left_relids = adjust_child_relids(oldinfo->left_relids,
493 context->nappinfos,
494 context->appinfos);
495 newinfo->right_relids = adjust_child_relids(oldinfo->right_relids,
496 context->nappinfos,
497 context->appinfos);
498
499 /*
500 * Reset cached derivative fields, since these might need to have
501 * different values when considering the child relation. Note we
502 * don't reset left_ec/right_ec: each child variable is implicitly
503 * equivalent to its parent, so still a member of the same EC if any.
504 */
505 newinfo->eval_cost.startup = -1;
506 newinfo->norm_selec = -1;
507 newinfo->outer_selec = -1;
508 newinfo->left_em = NULL;
509 newinfo->right_em = NULL;
510 newinfo->scansel_cache = NIL;
511 newinfo->left_bucketsize = -1;
512 newinfo->right_bucketsize = -1;
513 newinfo->left_mcvfreq = -1;
514 newinfo->right_mcvfreq = -1;
515
516 return (Node *) newinfo;
517 }
518
519 /*
520 * We have to process RelAggInfo nodes specially.
521 */
522 if (IsA(node, RelAggInfo))
523 {
524 RelAggInfo *oldinfo = (RelAggInfo *) node;
525 RelAggInfo *newinfo = makeNode(RelAggInfo);
526
527 newinfo->target = (PathTarget *)
529 context);
530
531 newinfo->agg_input = (PathTarget *)
533 context);
534
535 newinfo->group_clauses = oldinfo->group_clauses;
536
537 newinfo->group_exprs = (List *)
539 context);
540
541 return (Node *) newinfo;
542 }
543
544 /*
545 * We have to process PathTarget nodes specially.
546 */
547 if (IsA(node, PathTarget))
548 {
549 PathTarget *oldtarget = (PathTarget *) node;
550 PathTarget *newtarget = makeNode(PathTarget);
551
552 /* Copy all flat-copiable fields */
553 memcpy(newtarget, oldtarget, sizeof(PathTarget));
554
555 newtarget->exprs = (List *)
557 context);
558
559 if (oldtarget->sortgrouprefs)
560 {
561 Size nbytes = list_length(oldtarget->exprs) * sizeof(Index);
562
563 newtarget->sortgrouprefs = (Index *) palloc(nbytes);
564 memcpy(newtarget->sortgrouprefs, oldtarget->sortgrouprefs, nbytes);
565 }
566
567 return (Node *) newtarget;
568 }
569
570 /*
571 * NOTE: we do not need to recurse into sublinks, because they should
572 * already have been converted to subplans before we see them.
573 */
574 Assert(!IsA(node, SubLink));
575 Assert(!IsA(node, Query));
576 /* We should never see these Query substructures, either. */
577 Assert(!IsA(node, RangeTblRef));
578 Assert(!IsA(node, JoinExpr));
579
581}
Relids adjust_child_relids(Relids relids, int nappinfos, AppendRelInfo **appinfos)
Definition: appendinfo.c:625
unsigned int Index
Definition: c.h:623
#define OidIsValid(objectId)
Definition: c.h:778
size_t Size
Definition: c.h:614
char * get_rel_name(Oid relid)
Definition: lsyscache.c:2095
Const * makeNullConst(Oid consttype, int32 consttypmod, Oid constcollid)
Definition: makefuncs.c:388
void * palloc(Size size)
Definition: mcxt.c:1365
#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:256
@ COERCE_IMPLICIT_CAST
Definition: primnodes.h:768
Index child_relid
Definition: pathnodes.h:3192
List * translated_vars
Definition: pathnodes.h:3219
Index parent_relid
Definition: pathnodes.h:3191
Oid parent_reltype
Definition: pathnodes.h:3200
Definition: pg_list.h:54
List * exprs
Definition: pathnodes.h:1779
Index phlevelsup
Definition: pathnodes.h:3024
List * row_identity_vars
Definition: pathnodes.h:396
Query * parse
Definition: pathnodes.h:227
Relids leaf_result_relids
Definition: pathnodes.h:384
List * rtable
Definition: parsenodes.h:175
List * group_exprs
Definition: pathnodes.h:1203
List * group_clauses
Definition: pathnodes.h:1201
struct PathTarget * agg_input
Definition: pathnodes.h:1198
struct PathTarget * target
Definition: pathnodes.h:1195
Relids required_relids
Definition: pathnodes.h:2822
Relids outer_relids
Definition: pathnodes.h:2828
Expr * clause
Definition: pathnodes.h:2791
List * args
Definition: primnodes.h:1448
ParseLoc location
Definition: primnodes.h:1472
AttrNumber varattno
Definition: primnodes.h:274
VarReturningType varreturningtype
Definition: primnodes.h:297

References adjust_appendrel_attrs_mutator(), adjust_child_relids(), RelAggInfo::agg_input, adjust_appendrel_attrs_context::appinfos, ConvertRowtypeExpr::arg, RowExpr::args, Assert(), bms_is_member(), AppendRelInfo::child_relid, AppendRelInfo::child_reltype, RestrictInfo::clause, COERCE_IMPLICIT_CAST, copyObject, CurrentOfExpr::cvarno, elog, ERROR, expression_tree_mutator, PathTarget::exprs, get_rel_name(), RelAggInfo::group_clauses, RelAggInfo::group_exprs, IsA, PlannerInfo::leaf_result_relids, list_length(), list_nth(), ConvertRowtypeExpr::location, RowExpr::location, makeNode, makeNullConst(), adjust_appendrel_attrs_context::nappinfos, NIL, OidIsValid, RestrictInfo::outer_relids, palloc(), AppendRelInfo::parent_relid, AppendRelInfo::parent_reloid, AppendRelInfo::parent_reltype, PlannerInfo::parse, PlaceHolderVar::phlevelsup, RestrictInfo::required_relids, ConvertRowtypeExpr::resulttype, adjust_appendrel_attrs_context::root, PlannerInfo::row_identity_vars, ROWID_VAR, RowIdentityVarInfo::rowidrels, RowIdentityVarInfo::rowidvar, rt_fetch, Query::rtable, RelAggInfo::target, AppendRelInfo::translated_vars, 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 625 of file appendinfo.c.

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

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(), 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 659 of file appendinfo.c.

662{
663 AppendRelInfo **appinfos;
664 int nappinfos;
665
666 /*
667 * If the given relids set doesn't contain any of the parent relids, it
668 * will remain unchanged.
669 */
670 if (!bms_overlap(relids, parentrel->relids))
671 return relids;
672
673 /* Recurse if immediate parent is not the top parent. */
674 if (childrel->parent != parentrel)
675 {
676 if (childrel->parent)
677 relids = adjust_child_relids_multilevel(root, relids,
678 childrel->parent,
679 parentrel);
680 else
681 elog(ERROR, "childrel is not a child of parentrel");
682 }
683
684 /* Now translate for this child. */
685 appinfos = find_appinfos_by_relids(root, childrel->relids, &nappinfos);
686
687 relids = adjust_child_relids(relids, nappinfos, appinfos);
688
689 pfree(appinfos);
690
691 return relids;
692}
Relids adjust_child_relids_multilevel(PlannerInfo *root, Relids relids, RelOptInfo *childrel, RelOptInfo *parentrel)
Definition: appendinfo.c:659
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:582

References adjust_child_relids(), adjust_child_relids_multilevel(), bms_overlap(), elog, ERROR, find_appinfos_by_relids(), pfree(), RelOptInfo::relids, 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 699 of file appendinfo.c.

700{
701 List *result = NIL;
702 ListCell *lc;
703
704 /* This should only happen for an inheritance case, not UNION ALL */
706
707 /* Look up each attribute in the AppendRelInfo's translated_vars list */
708 foreach(lc, attnums)
709 {
710 AttrNumber parentattno = lfirst_int(lc);
711 Var *childvar;
712
713 /* Look up the translation of this column: it must be a Var */
714 if (parentattno <= 0 ||
715 parentattno > list_length(context->translated_vars))
716 elog(ERROR, "attribute %d of relation \"%s\" does not exist",
717 parentattno, get_rel_name(context->parent_reloid));
718 childvar = (Var *) list_nth(context->translated_vars, parentattno - 1);
719 if (childvar == NULL || !IsA(childvar, Var))
720 elog(ERROR, "attribute %d of relation \"%s\" does not exist",
721 parentattno, get_rel_name(context->parent_reloid));
722
723 result = lappend_int(result, childvar->varattno);
724 }
725 return result;
726}
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

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().

◆ adjust_inherited_attnums_multilevel()

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

Definition at line 733 of file appendinfo.c.

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

References adjust_inherited_attnums(), adjust_inherited_attnums_multilevel(), elog, ERROR, AppendRelInfo::parent_relid, 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 1036 of file appendinfo.c.

1037{
1038 Query *parse = root->parse;
1039 int result_relation = parse->resultRelation;
1040 RangeTblEntry *target_rte;
1041 RelOptInfo *target_rel;
1042 ListCell *lc;
1043
1044 /*
1045 * There's nothing to do if this isn't an inherited UPDATE/DELETE/MERGE.
1046 */
1047 if (parse->commandType != CMD_UPDATE && parse->commandType != CMD_DELETE &&
1048 parse->commandType != CMD_MERGE)
1049 {
1050 Assert(root->row_identity_vars == NIL);
1051 return;
1052 }
1053 target_rte = rt_fetch(result_relation, parse->rtable);
1054 if (!target_rte->inh)
1055 {
1056 Assert(root->row_identity_vars == NIL);
1057 return;
1058 }
1059
1060 /*
1061 * Ordinarily, we expect that leaf result relation(s) will have added some
1062 * ROWID_VAR Vars to the query. However, it's possible that constraint
1063 * exclusion suppressed every leaf relation. The executor will get upset
1064 * if the plan has no row identity columns at all, even though it will
1065 * certainly process no rows. Handle this edge case by re-opening the top
1066 * result relation and adding the row identity columns it would have used,
1067 * as preprocess_targetlist() would have done if it weren't marked "inh".
1068 * Then re-run build_base_rel_tlists() to ensure that the added columns
1069 * get propagated to the relation's reltarget. (This is a bit ugly, but
1070 * it seems better to confine the ugliness and extra cycles to this
1071 * unusual corner case.)
1072 */
1073 if (root->row_identity_vars == NIL)
1074 {
1075 Relation target_relation;
1076
1077 target_relation = table_open(target_rte->relid, NoLock);
1078 add_row_identity_columns(root, result_relation,
1079 target_rte, target_relation);
1080 table_close(target_relation, NoLock);
1081 build_base_rel_tlists(root, root->processed_tlist);
1082 /* There are no ROWID_VAR Vars in this case, so we're done. */
1083 return;
1084 }
1085
1086 /*
1087 * Dig through the processed_tlist to find the ROWID_VAR reference Vars,
1088 * and forcibly copy them into the reltarget list of the topmost target
1089 * relation. That's sufficient because they'll be copied to the
1090 * individual leaf target rels (with appropriate translation) later,
1091 * during appendrel expansion --- see set_append_rel_size().
1092 */
1093 target_rel = find_base_rel(root, result_relation);
1094
1095 foreach(lc, root->processed_tlist)
1096 {
1097 TargetEntry *tle = lfirst(lc);
1098 Var *var = (Var *) tle->expr;
1099
1100 if (var && IsA(var, Var) && var->varno == ROWID_VAR)
1101 {
1102 target_rel->reltarget->exprs =
1103 lappend(target_rel->reltarget->exprs, copyObject(var));
1104 /* reltarget cost and width will be computed later */
1105 }
1106 }
1107}
void add_row_identity_columns(PlannerInfo *root, Index rtindex, RangeTblEntry *target_rte, Relation target_relation)
Definition: appendinfo.c:955
void build_base_rel_tlists(PlannerInfo *root, List *final_tlist)
Definition: initsplan.c:242
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:81
#define NoLock
Definition: lockdefs.h:34
static struct subre * parse(struct vars *v, int stopper, int type, struct state *init, struct state *final)
Definition: regcomp.c:717
RelOptInfo * find_base_rel(PlannerInfo *root, int relid)
Definition: relnode.c:529
struct PathTarget * reltarget
Definition: pathnodes.h:949
Expr * expr
Definition: primnodes.h:2239
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, TargetEntry::expr, PathTarget::exprs, find_base_rel(), if(), RangeTblEntry::inh, IsA, lappend(), lfirst, NIL, NoLock, parse(), RelOptInfo::reltarget, 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 804 of file appendinfo.c.

805{
806 AppendRelInfo **appinfos;
807 int cnt = 0;
808 int i;
809
810 /* Allocate an array that's certainly big enough */
811 appinfos = (AppendRelInfo **)
812 palloc(sizeof(AppendRelInfo *) * bms_num_members(relids));
813
814 i = -1;
815 while ((i = bms_next_member(relids, i)) >= 0)
816 {
817 AppendRelInfo *appinfo = root->append_rel_array[i];
818
819 if (!appinfo)
820 {
821 /* Probably i is an OJ index, but let's check */
822 if (find_base_rel_ignore_join(root, i) == NULL)
823 continue;
824 /* It's a base rel, but we lack an append_rel_array entry */
825 elog(ERROR, "child rel %d not found in append_rel_array", i);
826 }
827
828 appinfos[cnt++] = appinfo;
829 }
830 *nappinfos = cnt;
831 return appinfos;
832}
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1306
int bms_num_members(const Bitmapset *a)
Definition: bitmapset.c:751
int i
Definition: isn.c:77
RelOptInfo * find_base_rel_ignore_join(PlannerInfo *root, int relid)
Definition: relnode.c:569

References bms_next_member(), bms_num_members(), elog, ERROR, find_base_rel_ignore_join(), i, palloc(), 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 761 of file appendinfo.c.

763{
764 /* This is pretty meaningless for commands other than UPDATE. */
765 Assert(root->parse->commandType == CMD_UPDATE);
766 if (relid == root->parse->resultRelation)
767 {
768 /*
769 * Non-inheritance case, so it's easy. The caller might be expecting
770 * a tree it can scribble on, though, so copy.
771 */
772 *processed_tlist = copyObject(root->processed_tlist);
773 if (update_colnos)
774 *update_colnos = copyObject(root->update_colnos);
775 }
776 else
777 {
778 Assert(bms_is_member(relid, root->all_result_relids));
779 *processed_tlist = (List *)
781 (Node *) root->processed_tlist,
782 find_base_rel(root, relid),
783 find_base_rel(root, root->parse->resultRelation));
784 if (update_colnos)
785 *update_colnos =
787 relid,
788 root->parse->resultRelation);
789 }
790}

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 51 of file appendinfo.c.

53{
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}
static void make_inh_translation_list(Relation oldrelation, Relation newrelation, Index newvarno, AppendRelInfo *appinfo)
Definition: appendinfo.c:80
#define RelationGetRelid(relation)
Definition: rel.h:515

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().

◆ make_inh_translation_list()

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

Definition at line 80 of file appendinfo.c.

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 {
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)
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)
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}
#define NameStr(name)
Definition: c.h:755
int32_t int32
Definition: c.h:538
int errcode(int sqlerrcode)
Definition: elog.c:863
int errmsg(const char *fmt,...)
Definition: elog.c:1080
#define ereport(elevel,...)
Definition: elog.h:150
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
static void * GETSTRUCT(const HeapTupleData *tuple)
Definition: htup_details.h:728
void * palloc0(Size size)
Definition: mcxt.c:1395
NameData attname
Definition: pg_attribute.h:41
int16 attnum
Definition: pg_attribute.h:74
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:202
unsigned int Oid
Definition: postgres_ext.h:32
#define RelationGetDescr(relation)
Definition: rel.h:541
#define RelationGetRelationName(relation)
Definition: rel.h:549
int num_child_cols
Definition: pathnodes.h:3227
Definition: regcomp.c:282
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:160

References Assert(), attname, attnum, elog, ereport, errcode(), errmsg(), ERROR, GETSTRUCT(), HeapTupleIsValid, lappend(), makeVar(), NameStr, TupleDescData::natts, NIL, AppendRelInfo::num_child_cols, palloc0(), RelationGetDescr, RelationGetRelationName, RelationGetRelid, ReleaseSysCache(), SearchSysCacheAttName(), AppendRelInfo::translated_vars, and TupleDescAttr().

Referenced by make_append_rel_info().