PostgreSQL Source Code  git master
appendinfo.c File Reference
#include "postgres.h"
#include "access/htup_details.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/appendinfo.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)
 
static Listadjust_inherited_tlist (List *tlist, AppendRelInfo *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, 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)
 
AppendRelInfo ** find_appinfos_by_relids (PlannerInfo *root, Relids relids, int *nappinfos)
 

Function Documentation

◆ adjust_appendrel_attrs()

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

Definition at line 194 of file appendinfo.c.

References adjust_appendrel_attrs_mutator(), adjust_inherited_tlist(), adjust_appendrel_attrs_context::appinfos, Assert, AppendRelInfo::child_relid, CMD_UPDATE, Query::commandType, IsA, adjust_appendrel_attrs_context::nappinfos, AppendRelInfo::parent_relid, QTW_IGNORE_RC_SUBQUERIES, query_tree_mutator(), Query::resultRelation, adjust_appendrel_attrs_context::root, and Query::targetList.

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(), inheritance_planner(), make_partitionedrel_pruneinfo(), set_append_rel_size(), and try_partitionwise_join().

196 {
197  Node *result;
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  /*
208  * Must be prepared to start with a Query or a bare expression tree.
209  */
210  if (node && IsA(node, Query))
211  {
212  Query *newnode;
213  int cnt;
214 
215  newnode = query_tree_mutator((Query *) node,
217  (void *) &context,
219  for (cnt = 0; cnt < nappinfos; cnt++)
220  {
221  AppendRelInfo *appinfo = appinfos[cnt];
222 
223  if (newnode->resultRelation == appinfo->parent_relid)
224  {
225  newnode->resultRelation = appinfo->child_relid;
226  /* Fix tlist resnos too, if it's inherited UPDATE */
227  if (newnode->commandType == CMD_UPDATE)
228  newnode->targetList =
230  appinfo);
231  break;
232  }
233  }
234 
235  result = (Node *) newnode;
236  }
237  else
238  result = adjust_appendrel_attrs_mutator(node, &context);
239 
240  return result;
241 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:576
static List * adjust_inherited_tlist(List *tlist, AppendRelInfo *context)
Definition: appendinfo.c:635
int resultRelation
Definition: parsenodes.h:122
Definition: nodes.h:525
List * targetList
Definition: parsenodes.h:140
#define QTW_IGNORE_RC_SUBQUERIES
Definition: nodeFuncs.h:22
static Node * adjust_appendrel_attrs_mutator(Node *node, adjust_appendrel_attrs_context *context)
Definition: appendinfo.c:244
CmdType commandType
Definition: parsenodes.h:112
#define Assert(condition)
Definition: c.h:739
AppendRelInfo ** appinfos
Definition: appendinfo.c:31
Index child_relid
Definition: pathnodes.h:2188
Index parent_relid
Definition: pathnodes.h:2187
Query * query_tree_mutator(Query *query, Node *(*mutator)(), void *context, int flags)
Definition: nodeFuncs.c:3187

◆ adjust_appendrel_attrs_multilevel()

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

Definition at line 497 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(), and make_partitionedrel_pruneinfo().

500 {
501  AppendRelInfo **appinfos;
502  Bitmapset *parent_relids = NULL;
503  int nappinfos;
504  int cnt;
505 
506  Assert(bms_num_members(child_relids) == bms_num_members(top_parent_relids));
507 
508  appinfos = find_appinfos_by_relids(root, child_relids, &nappinfos);
509 
510  /* Construct relids set for the immediate parent of given child. */
511  for (cnt = 0; cnt < nappinfos; cnt++)
512  {
513  AppendRelInfo *appinfo = appinfos[cnt];
514 
515  parent_relids = bms_add_member(parent_relids, appinfo->parent_relid);
516  }
517 
518  /* Recurse if immediate parent is not the top parent. */
519  if (!bms_equal(parent_relids, top_parent_relids))
520  node = adjust_appendrel_attrs_multilevel(root, node, parent_relids,
521  top_parent_relids);
522 
523  /* Now translate for this child */
524  node = adjust_appendrel_attrs(root, node, nappinfos, appinfos);
525 
526  pfree(appinfos);
527 
528  return node;
529 }
Node * adjust_appendrel_attrs_multilevel(PlannerInfo *root, Node *node, Relids child_relids, Relids top_parent_relids)
Definition: appendinfo.c:497
void pfree(void *pointer)
Definition: mcxt.c:1056
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:723
#define Assert(condition)
Definition: c.h:739
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:736
Index parent_relid
Definition: pathnodes.h:2187
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:194

◆ adjust_appendrel_attrs_mutator()

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

Definition at line 244 of file appendinfo.c.

References adjust_child_relids(), adjust_appendrel_attrs_context::appinfos, ConvertRowtypeExpr::arg, RowExpr::args, Assert, AppendRelInfo::child_relid, AppendRelInfo::child_reltype, RestrictInfo::clause, RestrictInfo::clause_relids, COERCE_IMPLICIT_CAST, Alias::colnames, RowExpr::colnames, ConvertRowtypeExpr::convertformat, copyObject, CurrentOfExpr::cvarno, elog, RangeTblEntry::eref, ERROR, RestrictInfo::eval_cost, expression_tree_mutator(), get_rel_name(), IsA, RestrictInfo::left_bucketsize, RestrictInfo::left_em, RestrictInfo::left_mcvfreq, RestrictInfo::left_relids, list_length(), list_nth(), ConvertRowtypeExpr::location, RowExpr::location, makeNode, adjust_appendrel_attrs_context::nappinfos, NIL, RestrictInfo::norm_selec, RestrictInfo::nullable_relids, OidIsValid, RestrictInfo::orclause, RestrictInfo::outer_relids, RestrictInfo::outer_selec, AppendRelInfo::parent_relid, AppendRelInfo::parent_reloid, AppendRelInfo::parent_reltype, PlannerInfo::parse, PlaceHolderVar::phlevelsup, PlaceHolderVar::phrels, RestrictInfo::required_relids, ConvertRowtypeExpr::resulttype, RestrictInfo::right_bucketsize, RestrictInfo::right_em, RestrictInfo::right_mcvfreq, RestrictInfo::right_relids, adjust_appendrel_attrs_context::root, RowExpr::row_format, RowExpr::row_typeid, rt_fetch, Query::rtable, RangeTblRef::rtindex, JoinExpr::rtindex, RestrictInfo::scansel_cache, QualCost::startup, AppendRelInfo::translated_vars, Var::varattno, Var::varlevelsup, Var::varno, Var::varnoold, and Var::vartype.

Referenced by adjust_appendrel_attrs().

246 {
247  AppendRelInfo **appinfos = context->appinfos;
248  int nappinfos = context->nappinfos;
249  int cnt;
250 
251  if (node == NULL)
252  return NULL;
253  if (IsA(node, Var))
254  {
255  Var *var = (Var *) copyObject(node);
256  AppendRelInfo *appinfo = NULL;
257 
258  for (cnt = 0; cnt < nappinfos; cnt++)
259  {
260  if (var->varno == appinfos[cnt]->parent_relid)
261  {
262  appinfo = appinfos[cnt];
263  break;
264  }
265  }
266 
267  if (var->varlevelsup == 0 && appinfo)
268  {
269  var->varno = appinfo->child_relid;
270  var->varnoold = appinfo->child_relid;
271  if (var->varattno > 0)
272  {
273  Node *newnode;
274 
275  if (var->varattno > list_length(appinfo->translated_vars))
276  elog(ERROR, "attribute %d of relation \"%s\" does not exist",
277  var->varattno, get_rel_name(appinfo->parent_reloid));
278  newnode = copyObject(list_nth(appinfo->translated_vars,
279  var->varattno - 1));
280  if (newnode == NULL)
281  elog(ERROR, "attribute %d of relation \"%s\" does not exist",
282  var->varattno, get_rel_name(appinfo->parent_reloid));
283  return newnode;
284  }
285  else if (var->varattno == 0)
286  {
287  /*
288  * Whole-row Var: if we are dealing with named rowtypes, we
289  * can use a whole-row Var for the child table plus a coercion
290  * step to convert the tuple layout to the parent's rowtype.
291  * Otherwise we have to generate a RowExpr.
292  */
293  if (OidIsValid(appinfo->child_reltype))
294  {
295  Assert(var->vartype == appinfo->parent_reltype);
296  if (appinfo->parent_reltype != appinfo->child_reltype)
297  {
299 
300  r->arg = (Expr *) var;
301  r->resulttype = appinfo->parent_reltype;
303  r->location = -1;
304  /* Make sure the Var node has the right type ID, too */
305  var->vartype = appinfo->child_reltype;
306  return (Node *) r;
307  }
308  }
309  else
310  {
311  /*
312  * Build a RowExpr containing the translated variables.
313  *
314  * In practice var->vartype will always be RECORDOID here,
315  * so we need to come up with some suitable column names.
316  * We use the parent RTE's column names.
317  *
318  * Note: we can't get here for inheritance cases, so there
319  * is no need to worry that translated_vars might contain
320  * some dummy NULLs.
321  */
322  RowExpr *rowexpr;
323  List *fields;
324  RangeTblEntry *rte;
325 
326  rte = rt_fetch(appinfo->parent_relid,
327  context->root->parse->rtable);
328  fields = copyObject(appinfo->translated_vars);
329  rowexpr = makeNode(RowExpr);
330  rowexpr->args = fields;
331  rowexpr->row_typeid = var->vartype;
332  rowexpr->row_format = COERCE_IMPLICIT_CAST;
333  rowexpr->colnames = copyObject(rte->eref->colnames);
334  rowexpr->location = -1;
335 
336  return (Node *) rowexpr;
337  }
338  }
339  /* system attributes don't need any other translation */
340  }
341  return (Node *) var;
342  }
343  if (IsA(node, CurrentOfExpr))
344  {
345  CurrentOfExpr *cexpr = (CurrentOfExpr *) copyObject(node);
346 
347  for (cnt = 0; cnt < nappinfos; cnt++)
348  {
349  AppendRelInfo *appinfo = appinfos[cnt];
350 
351  if (cexpr->cvarno == appinfo->parent_relid)
352  {
353  cexpr->cvarno = appinfo->child_relid;
354  break;
355  }
356  }
357  return (Node *) cexpr;
358  }
359  if (IsA(node, RangeTblRef))
360  {
361  RangeTblRef *rtr = (RangeTblRef *) copyObject(node);
362 
363  for (cnt = 0; cnt < nappinfos; cnt++)
364  {
365  AppendRelInfo *appinfo = appinfos[cnt];
366 
367  if (rtr->rtindex == appinfo->parent_relid)
368  {
369  rtr->rtindex = appinfo->child_relid;
370  break;
371  }
372  }
373  return (Node *) rtr;
374  }
375  if (IsA(node, JoinExpr))
376  {
377  /* Copy the JoinExpr node with correct mutation of subnodes */
378  JoinExpr *j;
379  AppendRelInfo *appinfo;
380 
381  j = (JoinExpr *) expression_tree_mutator(node,
383  (void *) context);
384  /* now fix JoinExpr's rtindex (probably never happens) */
385  for (cnt = 0; cnt < nappinfos; cnt++)
386  {
387  appinfo = appinfos[cnt];
388 
389  if (j->rtindex == appinfo->parent_relid)
390  {
391  j->rtindex = appinfo->child_relid;
392  break;
393  }
394  }
395  return (Node *) j;
396  }
397  if (IsA(node, PlaceHolderVar))
398  {
399  /* Copy the PlaceHolderVar node with correct mutation of subnodes */
400  PlaceHolderVar *phv;
401 
404  (void *) context);
405  /* now fix PlaceHolderVar's relid sets */
406  if (phv->phlevelsup == 0)
407  phv->phrels = adjust_child_relids(phv->phrels, context->nappinfos,
408  context->appinfos);
409  return (Node *) phv;
410  }
411  /* Shouldn't need to handle planner auxiliary nodes here */
412  Assert(!IsA(node, SpecialJoinInfo));
413  Assert(!IsA(node, AppendRelInfo));
414  Assert(!IsA(node, PlaceHolderInfo));
415  Assert(!IsA(node, MinMaxAggInfo));
416 
417  /*
418  * We have to process RestrictInfo nodes specially. (Note: although
419  * set_append_rel_pathlist will hide RestrictInfos in the parent's
420  * baserestrictinfo list from us, it doesn't hide those in joininfo.)
421  */
422  if (IsA(node, RestrictInfo))
423  {
424  RestrictInfo *oldinfo = (RestrictInfo *) node;
425  RestrictInfo *newinfo = makeNode(RestrictInfo);
426 
427  /* Copy all flat-copiable fields */
428  memcpy(newinfo, oldinfo, sizeof(RestrictInfo));
429 
430  /* Recursively fix the clause itself */
431  newinfo->clause = (Expr *)
432  adjust_appendrel_attrs_mutator((Node *) oldinfo->clause, context);
433 
434  /* and the modified version, if an OR clause */
435  newinfo->orclause = (Expr *)
436  adjust_appendrel_attrs_mutator((Node *) oldinfo->orclause, context);
437 
438  /* adjust relid sets too */
439  newinfo->clause_relids = adjust_child_relids(oldinfo->clause_relids,
440  context->nappinfos,
441  context->appinfos);
443  context->nappinfos,
444  context->appinfos);
445  newinfo->outer_relids = adjust_child_relids(oldinfo->outer_relids,
446  context->nappinfos,
447  context->appinfos);
449  context->nappinfos,
450  context->appinfos);
451  newinfo->left_relids = adjust_child_relids(oldinfo->left_relids,
452  context->nappinfos,
453  context->appinfos);
454  newinfo->right_relids = adjust_child_relids(oldinfo->right_relids,
455  context->nappinfos,
456  context->appinfos);
457 
458  /*
459  * Reset cached derivative fields, since these might need to have
460  * different values when considering the child relation. Note we
461  * don't reset left_ec/right_ec: each child variable is implicitly
462  * equivalent to its parent, so still a member of the same EC if any.
463  */
464  newinfo->eval_cost.startup = -1;
465  newinfo->norm_selec = -1;
466  newinfo->outer_selec = -1;
467  newinfo->left_em = NULL;
468  newinfo->right_em = NULL;
469  newinfo->scansel_cache = NIL;
470  newinfo->left_bucketsize = -1;
471  newinfo->right_bucketsize = -1;
472  newinfo->left_mcvfreq = -1;
473  newinfo->right_mcvfreq = -1;
474 
475  return (Node *) newinfo;
476  }
477 
478  /*
479  * NOTE: we do not need to recurse into sublinks, because they should
480  * already have been converted to subplans before we see them.
481  */
482  Assert(!IsA(node, SubLink));
483  Assert(!IsA(node, Query));
484 
486  (void *) context);
487 }
QualCost eval_cost
Definition: pathnodes.h:1980
#define NIL
Definition: pg_list.h:65
List * args
Definition: primnodes.h:1008
#define IsA(nodeptr, _type_)
Definition: nodes.h:576
Query * parse
Definition: pathnodes.h:177
Index varlevelsup
Definition: primnodes.h:177
Node * expression_tree_mutator(Node *node, Node *(*mutator)(), void *context)
Definition: nodeFuncs.c:2501
Relids required_relids
Definition: pathnodes.h:1961
List * colnames
Definition: primnodes.h:43
Selectivity right_mcvfreq
Definition: pathnodes.h:2007
Expr * orclause
Definition: pathnodes.h:1974
Relids clause_relids
Definition: pathnodes.h:1958
Definition: nodes.h:525
Relids left_relids
Definition: pathnodes.h:1970
AttrNumber varattno
Definition: primnodes.h:172
Definition: primnodes.h:167
#define OidIsValid(objectId)
Definition: c.h:645
List * translated_vars
Definition: pathnodes.h:2215
Oid parent_reltype
Definition: pathnodes.h:2196
Cost startup
Definition: pathnodes.h:45
Relids outer_relids
Definition: pathnodes.h:1964
Selectivity norm_selec
Definition: pathnodes.h:1981
Index varnoold
Definition: primnodes.h:180
List * rtable
Definition: parsenodes.h:137
#define ERROR
Definition: elog.h:43
List * colnames
Definition: primnodes.h:1024
static void * list_nth(const List *list, int n)
Definition: pg_list.h:277
Oid vartype
Definition: primnodes.h:174
EquivalenceMember * left_em
Definition: pathnodes.h:1993
int location
Definition: primnodes.h:1025
Selectivity outer_selec
Definition: pathnodes.h:1984
#define rt_fetch(rangetable_index, rangetable)
Definition: parsetree.h:31
EquivalenceMember * right_em
Definition: pathnodes.h:1994
Expr * clause
Definition: pathnodes.h:1943
static Node * adjust_appendrel_attrs_mutator(Node *node, adjust_appendrel_attrs_context *context)
Definition: appendinfo.c:244
Index varno
Definition: primnodes.h:170
Relids nullable_relids
Definition: pathnodes.h:1967
CoercionForm convertformat
Definition: primnodes.h:872
Selectivity left_bucketsize
Definition: pathnodes.h:2004
#define makeNode(_type_)
Definition: nodes.h:573
Relids right_relids
Definition: pathnodes.h:1971
#define Assert(condition)
Definition: c.h:739
Selectivity left_mcvfreq
Definition: pathnodes.h:2006
Oid row_typeid
Definition: primnodes.h:1009
static int list_length(const List *l)
Definition: pg_list.h:169
AppendRelInfo ** appinfos
Definition: appendinfo.c:31
Index phlevelsup
Definition: pathnodes.h:2065
Selectivity right_bucketsize
Definition: pathnodes.h:2005
Relids adjust_child_relids(Relids relids, int nappinfos, AppendRelInfo **appinfos)
Definition: appendinfo.c:536
#define elog(elevel,...)
Definition: elog.h:228
Index child_relid
Definition: pathnodes.h:2188
Alias * eref
Definition: parsenodes.h:1092
#define copyObject(obj)
Definition: nodes.h:641
Index parent_relid
Definition: pathnodes.h:2187
CoercionForm row_format
Definition: primnodes.h:1023
int rtindex
Definition: primnodes.h:1481
Definition: pg_list.h:50
char * get_rel_name(Oid relid)
Definition: lsyscache.c:1730
List * scansel_cache
Definition: pathnodes.h:1995

◆ adjust_child_relids()

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

Definition at line 536 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().

537 {
538  Bitmapset *result = NULL;
539  int cnt;
540 
541  for (cnt = 0; cnt < nappinfos; cnt++)
542  {
543  AppendRelInfo *appinfo = appinfos[cnt];
544 
545  /* Remove parent, add child */
546  if (bms_is_member(appinfo->parent_relid, relids))
547  {
548  /* Make a copy if we are changing the set. */
549  if (!result)
550  result = bms_copy(relids);
551 
552  result = bms_del_member(result, appinfo->parent_relid);
553  result = bms_add_member(result, appinfo->child_relid);
554  }
555  }
556 
557  /* If we made any changes, return the modified copy. */
558  if (result)
559  return result;
560 
561  /* Otherwise, return the original set without modification. */
562  return relids;
563 }
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:2188
Index parent_relid
Definition: pathnodes.h:2187
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 571 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().

573 {
574  AppendRelInfo **appinfos;
575  int nappinfos;
576  Relids parent_relids = NULL;
577  Relids result;
578  Relids tmp_result = NULL;
579  int cnt;
580 
581  /*
582  * If the given relids set doesn't contain any of the top parent relids,
583  * it will remain unchanged.
584  */
585  if (!bms_overlap(relids, top_parent_relids))
586  return relids;
587 
588  appinfos = find_appinfos_by_relids(root, child_relids, &nappinfos);
589 
590  /* Construct relids set for the immediate parent of the given child. */
591  for (cnt = 0; cnt < nappinfos; cnt++)
592  {
593  AppendRelInfo *appinfo = appinfos[cnt];
594 
595  parent_relids = bms_add_member(parent_relids, appinfo->parent_relid);
596  }
597 
598  /* Recurse if immediate parent is not the top parent. */
599  if (!bms_equal(parent_relids, top_parent_relids))
600  {
601  tmp_result = adjust_child_relids_multilevel(root, relids,
602  parent_relids,
603  top_parent_relids);
604  relids = tmp_result;
605  }
606 
607  result = adjust_child_relids(relids, nappinfos, appinfos);
608 
609  /* Free memory consumed by any intermediate result. */
610  if (tmp_result)
611  bms_free(tmp_result);
612  bms_free(parent_relids);
613  pfree(appinfos);
614 
615  return result;
616 }
Relids adjust_child_relids_multilevel(PlannerInfo *root, Relids relids, Relids child_relids, Relids top_parent_relids)
Definition: appendinfo.c:571
void pfree(void *pointer)
Definition: mcxt.c:1056
AppendRelInfo ** find_appinfos_by_relids(PlannerInfo *root, Relids relids, int *nappinfos)
Definition: appendinfo.c:723
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:536
Index parent_relid
Definition: pathnodes.h:2187
bool bms_equal(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:94

◆ adjust_inherited_tlist()

static List * adjust_inherited_tlist ( List tlist,
AppendRelInfo context 
)
static

Definition at line 635 of file appendinfo.c.

References Assert, elog, ERROR, get_rel_name(), IsA, lappend(), lfirst, list_length(), list_nth(), NIL, OidIsValid, AppendRelInfo::parent_reloid, TargetEntry::resjunk, TargetEntry::resno, AppendRelInfo::translated_vars, and Var::varattno.

Referenced by adjust_appendrel_attrs().

636 {
637  bool changed_it = false;
638  ListCell *tl;
639  List *new_tlist;
640  bool more;
641  int attrno;
642 
643  /* This should only happen for an inheritance case, not UNION ALL */
644  Assert(OidIsValid(context->parent_reloid));
645 
646  /* Scan tlist and update resnos to match attnums of child rel */
647  foreach(tl, tlist)
648  {
649  TargetEntry *tle = (TargetEntry *) lfirst(tl);
650  Var *childvar;
651 
652  if (tle->resjunk)
653  continue; /* ignore junk items */
654 
655  /* Look up the translation of this column: it must be a Var */
656  if (tle->resno <= 0 ||
657  tle->resno > list_length(context->translated_vars))
658  elog(ERROR, "attribute %d of relation \"%s\" does not exist",
659  tle->resno, get_rel_name(context->parent_reloid));
660  childvar = (Var *) list_nth(context->translated_vars, tle->resno - 1);
661  if (childvar == NULL || !IsA(childvar, Var))
662  elog(ERROR, "attribute %d of relation \"%s\" does not exist",
663  tle->resno, get_rel_name(context->parent_reloid));
664 
665  if (tle->resno != childvar->varattno)
666  {
667  tle->resno = childvar->varattno;
668  changed_it = true;
669  }
670  }
671 
672  /*
673  * If we changed anything, re-sort the tlist by resno, and make sure
674  * resjunk entries have resnos above the last real resno. The sort
675  * algorithm is a bit stupid, but for such a seldom-taken path, small is
676  * probably better than fast.
677  */
678  if (!changed_it)
679  return tlist;
680 
681  new_tlist = NIL;
682  more = true;
683  for (attrno = 1; more; attrno++)
684  {
685  more = false;
686  foreach(tl, tlist)
687  {
688  TargetEntry *tle = (TargetEntry *) lfirst(tl);
689 
690  if (tle->resjunk)
691  continue; /* ignore junk items */
692 
693  if (tle->resno == attrno)
694  new_tlist = lappend(new_tlist, tle);
695  else if (tle->resno > attrno)
696  more = true;
697  }
698  }
699 
700  foreach(tl, tlist)
701  {
702  TargetEntry *tle = (TargetEntry *) lfirst(tl);
703 
704  if (!tle->resjunk)
705  continue; /* here, ignore non-junk items */
706 
707  tle->resno = attrno;
708  new_tlist = lappend(new_tlist, tle);
709  attrno++;
710  }
711 
712  return new_tlist;
713 }
#define NIL
Definition: pg_list.h:65
#define IsA(nodeptr, _type_)
Definition: nodes.h:576
AttrNumber varattno
Definition: primnodes.h:172
Definition: primnodes.h:167
#define OidIsValid(objectId)
Definition: c.h:645
List * translated_vars
Definition: pathnodes.h:2215
bool resjunk
Definition: primnodes.h:1400
#define ERROR
Definition: elog.h:43
static void * list_nth(const List *list, int n)
Definition: pg_list.h:277
AttrNumber resno
Definition: primnodes.h:1394
List * lappend(List *list, void *datum)
Definition: list.c:322
#define Assert(condition)
Definition: c.h:739
#define lfirst(lc)
Definition: pg_list.h:190
static int list_length(const List *l)
Definition: pg_list.h:169
#define elog(elevel,...)
Definition: elog.h:228
Definition: pg_list.h:50
char * get_rel_name(Oid relid)
Definition: lsyscache.c:1730

◆ find_appinfos_by_relids()

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

Definition at line 723 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().

724 {
725  AppendRelInfo **appinfos;
726  int cnt = 0;
727  int i;
728 
729  *nappinfos = bms_num_members(relids);
730  appinfos = (AppendRelInfo **) palloc(sizeof(AppendRelInfo *) * *nappinfos);
731 
732  i = -1;
733  while ((i = bms_next_member(relids, i)) >= 0)
734  {
735  AppendRelInfo *appinfo = root->append_rel_array[i];
736 
737  if (!appinfo)
738  elog(ERROR, "child rel %d not found in append_rel_array", i);
739 
740  appinfos[cnt++] = appinfo;
741  }
742  return appinfos;
743 }
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1043
#define ERROR
Definition: elog.h:43
int bms_num_members(const Bitmapset *a)
Definition: bitmapset.c:646
struct AppendRelInfo ** append_rel_array
Definition: pathnodes.h:217
void * palloc(Size size)
Definition: mcxt.c:949
#define elog(elevel,...)
Definition: elog.h:228
int i

◆ make_append_rel_info()

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

Definition at line 49 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().

51 {
53 
54  appinfo->parent_relid = parentRTindex;
55  appinfo->child_relid = childRTindex;
56  appinfo->parent_reltype = parentrel->rd_rel->reltype;
57  appinfo->child_reltype = childrel->rd_rel->reltype;
58  make_inh_translation_list(parentrel, childrel, childRTindex, appinfo);
59  appinfo->parent_reloid = RelationGetRelid(parentrel);
60 
61  return appinfo;
62 }
static void make_inh_translation_list(Relation oldrelation, Relation newrelation, Index newvarno, AppendRelInfo *appinfo)
Definition: appendinfo.c:78
Form_pg_class rd_rel
Definition: rel.h:83
Oid parent_reltype
Definition: pathnodes.h:2196
#define makeNode(_type_)
Definition: nodes.h:573
Index child_relid
Definition: pathnodes.h:2188
Index parent_relid
Definition: pathnodes.h:2187
#define RelationGetRelid(relation)
Definition: rel.h:422

◆ make_inh_translation_list()

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

Definition at line 78 of file appendinfo.c.

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

Referenced by make_append_rel_info().

81 {
82  List *vars = NIL;
83  AttrNumber *pcolnos;
84  TupleDesc old_tupdesc = RelationGetDescr(oldrelation);
85  TupleDesc new_tupdesc = RelationGetDescr(newrelation);
86  Oid new_relid = RelationGetRelid(newrelation);
87  int oldnatts = old_tupdesc->natts;
88  int newnatts = new_tupdesc->natts;
89  int old_attno;
90  int new_attno = 0;
91 
92  /* Initialize reverse-translation array with all entries zero */
93  appinfo->num_child_cols = newnatts;
94  appinfo->parent_colnos = pcolnos =
95  (AttrNumber *) palloc0(newnatts * sizeof(AttrNumber));
96 
97  for (old_attno = 0; old_attno < oldnatts; old_attno++)
98  {
100  char *attname;
101  Oid atttypid;
102  int32 atttypmod;
104 
105  att = TupleDescAttr(old_tupdesc, old_attno);
106  if (att->attisdropped)
107  {
108  /* Just put NULL into this list entry */
109  vars = lappend(vars, NULL);
110  continue;
111  }
112  attname = NameStr(att->attname);
113  atttypid = att->atttypid;
114  atttypmod = att->atttypmod;
115  attcollation = att->attcollation;
116 
117  /*
118  * When we are generating the "translation list" for the parent table
119  * of an inheritance set, no need to search for matches.
120  */
121  if (oldrelation == newrelation)
122  {
123  vars = lappend(vars, makeVar(newvarno,
124  (AttrNumber) (old_attno + 1),
125  atttypid,
126  atttypmod,
127  attcollation,
128  0));
129  pcolnos[old_attno] = old_attno + 1;
130  continue;
131  }
132 
133  /*
134  * Otherwise we have to search for the matching column by name.
135  * There's no guarantee it'll have the same column position, because
136  * of cases like ALTER TABLE ADD COLUMN and multiple inheritance.
137  * However, in simple cases, the relative order of columns is mostly
138  * the same in both relations, so try the column of newrelation that
139  * follows immediately after the one that we just found, and if that
140  * fails, let syscache handle it.
141  */
142  if (new_attno >= newnatts ||
143  (att = TupleDescAttr(new_tupdesc, new_attno))->attisdropped ||
144  strcmp(attname, NameStr(att->attname)) != 0)
145  {
146  HeapTuple newtup;
147 
148  newtup = SearchSysCacheAttName(new_relid, attname);
149  if (!HeapTupleIsValid(newtup))
150  elog(ERROR, "could not find inherited attribute \"%s\" of relation \"%s\"",
151  attname, RelationGetRelationName(newrelation));
152  new_attno = ((Form_pg_attribute) GETSTRUCT(newtup))->attnum - 1;
153  Assert(new_attno >= 0 && new_attno < newnatts);
154  ReleaseSysCache(newtup);
155 
156  att = TupleDescAttr(new_tupdesc, new_attno);
157  }
158 
159  /* Found it, check type and collation match */
160  if (atttypid != att->atttypid || atttypmod != att->atttypmod)
161  elog(ERROR, "attribute \"%s\" of relation \"%s\" does not match parent's type",
162  attname, RelationGetRelationName(newrelation));
163  if (attcollation != att->attcollation)
164  elog(ERROR, "attribute \"%s\" of relation \"%s\" does not match parent's collation",
165  attname, RelationGetRelationName(newrelation));
166 
167  vars = lappend(vars, makeVar(newvarno,
168  (AttrNumber) (new_attno + 1),
169  atttypid,
170  atttypmod,
171  attcollation,
172  0));
173  pcolnos[new_attno] = old_attno + 1;
174  new_attno++;
175  }
176 
177  appinfo->translated_vars = vars;
178 }
#define NIL
Definition: pg_list.h:65
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
int num_child_cols
Definition: pathnodes.h:2223
#define RelationGetDescr(relation)
Definition: rel.h:448
AttrNumber * parent_colnos
Definition: pathnodes.h:2224
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
unsigned int Oid
Definition: postgres_ext.h:31
List * translated_vars
Definition: pathnodes.h:2215
signed int int32
Definition: c.h:347
#define ERROR
Definition: elog.h:43
NameData attname
Definition: pg_attribute.h:40
Oid attcollation
Definition: pg_attribute.h:164
#define RelationGetRelationName(relation)
Definition: rel.h:456
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:200
Oid atttypid
Definition: pg_attribute.h:49
Var * makeVar(Index varno, AttrNumber varattno, Oid vartype, int32 vartypmod, Oid varcollid, Index varlevelsup)
Definition: makefuncs.c:66
List * lappend(List *list, void *datum)
Definition: list.c:322
void * palloc0(Size size)
Definition: mcxt.c:980
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1164
int16 attnum
Definition: pg_attribute.h:79
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define Assert(condition)
Definition: c.h:739
HeapTuple SearchSysCacheAttName(Oid relid, const char *attname)
Definition: syscache.c:1257
#define elog(elevel,...)
Definition: elog.h:228
#define NameStr(name)
Definition: c.h:616
Definition: regcomp.c:224
Definition: pg_list.h:50
int16 AttrNumber
Definition: attnum.h:21
#define RelationGetRelid(relation)
Definition: rel.h:422