PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
rewriteManip.c File Reference
Include dependency graph for rewriteManip.c:

Go to the source code of this file.

Data Structures

struct  contain_aggs_of_level_context
 
struct  locate_agg_of_level_context
 
struct  locate_windowfunc_context
 
struct  OffsetVarNodes_context
 
struct  ChangeVarNodes_context
 
struct  IncrementVarSublevelsUp_context
 
struct  rangeTableEntry_used_context
 
struct  map_variable_attnos_context
 
struct  ReplaceVarsFromTargetList_context
 

Functions

static bool contain_aggs_of_level_walker (Node *node, contain_aggs_of_level_context *context)
 
static bool locate_agg_of_level_walker (Node *node, locate_agg_of_level_context *context)
 
static bool contain_windowfuncs_walker (Node *node, void *context)
 
static bool locate_windowfunc_walker (Node *node, locate_windowfunc_context *context)
 
static bool checkExprHasSubLink_walker (Node *node, void *context)
 
static Relids offset_relid_set (Relids relids, int offset)
 
static Relids adjust_relid_set (Relids relids, int oldrelid, int newrelid)
 
bool contain_aggs_of_level (Node *node, int levelsup)
 
int locate_agg_of_level (Node *node, int levelsup)
 
bool contain_windowfuncs (Node *node)
 
int locate_windowfunc (Node *node)
 
bool checkExprHasSubLink (Node *node)
 
static bool contains_multiexpr_param (Node *node, void *context)
 
static bool OffsetVarNodes_walker (Node *node, OffsetVarNodes_context *context)
 
void OffsetVarNodes (Node *node, int offset, int sublevels_up)
 
static bool ChangeVarNodes_walker (Node *node, ChangeVarNodes_context *context)
 
void ChangeVarNodes (Node *node, int rt_index, int new_index, int sublevels_up)
 
static bool IncrementVarSublevelsUp_walker (Node *node, IncrementVarSublevelsUp_context *context)
 
void IncrementVarSublevelsUp (Node *node, int delta_sublevels_up, int min_sublevels_up)
 
void IncrementVarSublevelsUp_rtable (List *rtable, int delta_sublevels_up, int min_sublevels_up)
 
static bool rangeTableEntry_used_walker (Node *node, rangeTableEntry_used_context *context)
 
bool rangeTableEntry_used (Node *node, int rt_index, int sublevels_up)
 
QuerygetInsertSelectQuery (Query *parsetree, Query ***subquery_ptr)
 
void AddQual (Query *parsetree, Node *qual)
 
void AddInvertedQual (Query *parsetree, Node *qual)
 
Nodereplace_rte_variables (Node *node, int target_varno, int sublevels_up, replace_rte_variables_callback callback, void *callback_arg, bool *outer_hasSubLinks)
 
Nodereplace_rte_variables_mutator (Node *node, replace_rte_variables_context *context)
 
static Nodemap_variable_attnos_mutator (Node *node, map_variable_attnos_context *context)
 
Nodemap_variable_attnos (Node *node, int target_varno, int sublevels_up, const AttrNumber *attno_map, int map_length, bool *found_whole_row)
 
static NodeReplaceVarsFromTargetList_callback (Var *var, replace_rte_variables_context *context)
 
NodeReplaceVarsFromTargetList (Node *node, int target_varno, int sublevels_up, RangeTblEntry *target_rte, List *targetlist, ReplaceVarsNoMatchOption nomatch_option, int nomatch_varno, bool *outer_hasSubLinks)
 

Function Documentation

void AddInvertedQual ( Query parsetree,
Node qual 
)

Definition at line 1044 of file rewriteManip.c.

References AddQual(), BooleanTest::arg, BooleanTest::booltesttype, IS_NOT_TRUE, BooleanTest::location, makeNode, and NULL.

Referenced by CopyAndAddInvertedQual().

1045 {
1046  BooleanTest *invqual;
1047 
1048  if (qual == NULL)
1049  return;
1050 
1051  /* Need not copy input qual, because AddQual will... */
1052  invqual = makeNode(BooleanTest);
1053  invqual->arg = (Expr *) qual;
1054  invqual->booltesttype = IS_NOT_TRUE;
1055  invqual->location = -1;
1056 
1057  AddQual(parsetree, (Node *) invqual);
1058 }
Definition: nodes.h:509
Expr * arg
Definition: primnodes.h:1202
BoolTestType booltesttype
Definition: primnodes.h:1203
#define makeNode(_type_)
Definition: nodes.h:557
#define NULL
Definition: c.h:229
void AddQual(Query *parsetree, Node *qual)
Definition: rewriteManip.c:976
void AddQual ( Query parsetree,
Node qual 
)

Definition at line 976 of file rewriteManip.c.

References Assert, checkExprHasSubLink(), CMD_UTILITY, Query::commandType, contain_aggs_of_level(), copyObject, ereport, errcode(), errmsg(), ERROR, Query::hasSubLinks, IsA, Query::jointree, make_and_qual(), NULL, FromExpr::quals, Query::setOperations, and Query::utilityStmt.

Referenced by AddInvertedQual(), rewriteRuleAction(), and rewriteTargetView().

977 {
978  Node *copy;
979 
980  if (qual == NULL)
981  return;
982 
983  if (parsetree->commandType == CMD_UTILITY)
984  {
985  /*
986  * There's noplace to put the qual on a utility statement.
987  *
988  * If it's a NOTIFY, silently ignore the qual; this means that the
989  * NOTIFY will execute, whether or not there are any qualifying rows.
990  * While clearly wrong, this is much more useful than refusing to
991  * execute the rule at all, and extra NOTIFY events are harmless for
992  * typical uses of NOTIFY.
993  *
994  * If it isn't a NOTIFY, error out, since unconditional execution of
995  * other utility stmts is unlikely to be wanted. (This case is not
996  * currently allowed anyway, but keep the test for safety.)
997  */
998  if (parsetree->utilityStmt && IsA(parsetree->utilityStmt, NotifyStmt))
999  return;
1000  else
1001  ereport(ERROR,
1002  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1003  errmsg("conditional utility statements are not implemented")));
1004  }
1005 
1006  if (parsetree->setOperations != NULL)
1007  {
1008  /*
1009  * There's noplace to put the qual on a setop statement, either. (This
1010  * could be fixed, but right now the planner simply ignores any qual
1011  * condition on a setop query.)
1012  */
1013  ereport(ERROR,
1014  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1015  errmsg("conditional UNION/INTERSECT/EXCEPT statements are not implemented")));
1016  }
1017 
1018  /* INTERSECT want's the original, but we need to copy - Jan */
1019  copy = copyObject(qual);
1020 
1021  parsetree->jointree->quals = make_and_qual(parsetree->jointree->quals,
1022  copy);
1023 
1024  /*
1025  * We had better not have stuck an aggregate into the WHERE clause.
1026  */
1027  Assert(!contain_aggs_of_level(copy, 0));
1028 
1029  /*
1030  * Make sure query is marked correctly if added qual has sublinks. Need
1031  * not search qual when query is already marked.
1032  */
1033  if (!parsetree->hasSubLinks)
1034  parsetree->hasSubLinks = checkExprHasSubLink(copy);
1035 }
Node * make_and_qual(Node *qual1, Node *qual2)
Definition: clauses.c:348
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
FromExpr * jointree
Definition: parsenodes.h:136
bool checkExprHasSubLink(Node *node)
Definition: rewriteManip.c:277
Definition: nodes.h:509
int errcode(int sqlerrcode)
Definition: elog.c:575
Node * utilityStmt
Definition: parsenodes.h:118
Node * quals
Definition: primnodes.h:1471
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
CmdType commandType
Definition: parsenodes.h:110
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
bool contain_aggs_of_level(Node *node, int levelsup)
Definition: rewriteManip.c:67
Node * setOperations
Definition: parsenodes.h:163
int errmsg(const char *fmt,...)
Definition: elog.c:797
bool hasSubLinks
Definition: parsenodes.h:126
#define copyObject(obj)
Definition: nodes.h:621
static Relids adjust_relid_set ( Relids  relids,
int  oldrelid,
int  newrelid 
)
static

Definition at line 661 of file rewriteManip.c.

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

Referenced by ChangeVarNodes_walker().

662 {
663  if (bms_is_member(oldrelid, relids))
664  {
665  /* Ensure we have a modifiable copy */
666  relids = bms_copy(relids);
667  /* Remove old, add new */
668  relids = bms_del_member(relids, oldrelid);
669  relids = bms_add_member(relids, newrelid);
670  }
671  return relids;
672 }
Bitmapset * bms_copy(const Bitmapset *a)
Definition: bitmapset.c:111
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:698
Bitmapset * bms_del_member(Bitmapset *a, int x)
Definition: bitmapset.c:735
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:420
void ChangeVarNodes ( Node node,
int  rt_index,
int  new_index,
int  sublevels_up 
)

Definition at line 607 of file rewriteManip.c.

References ChangeVarNodes_walker(), OnConflictExpr::exclRelIndex, IsA, lfirst, ChangeVarNodes_context::new_index, Query::onConflict, query_tree_walker(), Query::resultRelation, Query::rowMarks, ChangeVarNodes_context::rt_index, RowMarkClause::rti, and ChangeVarNodes_context::sublevels_up.

Referenced by add_security_quals(), add_with_check_options(), ApplyRetrieveRule(), CopyAndAddInvertedQual(), get_relation_constraints(), get_relation_info(), inheritance_planner(), rewriteRuleAction(), rewriteTargetView(), and TriggerEnabled().

608 {
609  ChangeVarNodes_context context;
610 
611  context.rt_index = rt_index;
612  context.new_index = new_index;
613  context.sublevels_up = sublevels_up;
614 
615  /*
616  * Must be prepared to start with a Query or a bare expression tree; if
617  * it's a Query, go straight to query_tree_walker to make sure that
618  * sublevels_up doesn't get incremented prematurely.
619  */
620  if (node && IsA(node, Query))
621  {
622  Query *qry = (Query *) node;
623 
624  /*
625  * If we are starting at a Query, and sublevels_up is zero, then we
626  * must also fix rangetable indexes in the Query itself --- namely
627  * resultRelation and rowMarks entries. sublevels_up cannot be zero
628  * when recursing into a subquery, so there's no need to have the same
629  * logic inside ChangeVarNodes_walker.
630  */
631  if (sublevels_up == 0)
632  {
633  ListCell *l;
634 
635  if (qry->resultRelation == rt_index)
636  qry->resultRelation = new_index;
637 
638  /* this is unlikely to ever be used, but ... */
639  if (qry->onConflict && qry->onConflict->exclRelIndex == rt_index)
640  qry->onConflict->exclRelIndex = new_index;
641 
642  foreach(l, qry->rowMarks)
643  {
644  RowMarkClause *rc = (RowMarkClause *) lfirst(l);
645 
646  if (rc->rti == rt_index)
647  rc->rti = new_index;
648  }
649  }
651  (void *) &context, 0);
652  }
653  else
654  ChangeVarNodes_walker(node, &context);
655 }
bool query_tree_walker(Query *query, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:2267
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
OnConflictExpr * onConflict
Definition: parsenodes.h:142
int resultRelation
Definition: parsenodes.h:120
List * rowMarks
Definition: parsenodes.h:161
#define lfirst(lc)
Definition: pg_list.h:106
static bool ChangeVarNodes_walker(Node *node, ChangeVarNodes_context *context)
Definition: rewriteManip.c:504
static bool ChangeVarNodes_walker ( Node node,
ChangeVarNodes_context context 
)
static

Definition at line 504 of file rewriteManip.c.

References adjust_relid_set(), Assert, AppendRelInfo::child_relid, CurrentOfExpr::cvarno, expression_tree_walker(), IsA, ChangeVarNodes_context::new_index, NULL, AppendRelInfo::parent_relid, PlaceHolderVar::phlevelsup, PlaceHolderVar::phrels, PlanRowMark::prti, query_tree_walker(), result, ChangeVarNodes_context::rt_index, PlanRowMark::rti, RangeTblRef::rtindex, JoinExpr::rtindex, ChangeVarNodes_context::sublevels_up, Var::varlevelsup, Var::varno, and Var::varnoold.

Referenced by ChangeVarNodes().

505 {
506  if (node == NULL)
507  return false;
508  if (IsA(node, Var))
509  {
510  Var *var = (Var *) node;
511 
512  if (var->varlevelsup == context->sublevels_up &&
513  var->varno == context->rt_index)
514  {
515  var->varno = context->new_index;
516  var->varnoold = context->new_index;
517  }
518  return false;
519  }
520  if (IsA(node, CurrentOfExpr))
521  {
522  CurrentOfExpr *cexpr = (CurrentOfExpr *) node;
523 
524  if (context->sublevels_up == 0 &&
525  cexpr->cvarno == context->rt_index)
526  cexpr->cvarno = context->new_index;
527  return false;
528  }
529  if (IsA(node, RangeTblRef))
530  {
531  RangeTblRef *rtr = (RangeTblRef *) node;
532 
533  if (context->sublevels_up == 0 &&
534  rtr->rtindex == context->rt_index)
535  rtr->rtindex = context->new_index;
536  /* the subquery itself is visited separately */
537  return false;
538  }
539  if (IsA(node, JoinExpr))
540  {
541  JoinExpr *j = (JoinExpr *) node;
542 
543  if (context->sublevels_up == 0 &&
544  j->rtindex == context->rt_index)
545  j->rtindex = context->new_index;
546  /* fall through to examine children */
547  }
548  if (IsA(node, PlaceHolderVar))
549  {
550  PlaceHolderVar *phv = (PlaceHolderVar *) node;
551 
552  if (phv->phlevelsup == context->sublevels_up)
553  {
554  phv->phrels = adjust_relid_set(phv->phrels,
555  context->rt_index,
556  context->new_index);
557  }
558  /* fall through to examine children */
559  }
560  if (IsA(node, PlanRowMark))
561  {
562  PlanRowMark *rowmark = (PlanRowMark *) node;
563 
564  if (context->sublevels_up == 0)
565  {
566  if (rowmark->rti == context->rt_index)
567  rowmark->rti = context->new_index;
568  if (rowmark->prti == context->rt_index)
569  rowmark->prti = context->new_index;
570  }
571  return false;
572  }
573  if (IsA(node, AppendRelInfo))
574  {
575  AppendRelInfo *appinfo = (AppendRelInfo *) node;
576 
577  if (context->sublevels_up == 0)
578  {
579  if (appinfo->parent_relid == context->rt_index)
580  appinfo->parent_relid = context->new_index;
581  if (appinfo->child_relid == context->rt_index)
582  appinfo->child_relid = context->new_index;
583  }
584  /* fall through to examine children */
585  }
586  /* Shouldn't need to handle other planner auxiliary nodes here */
587  Assert(!IsA(node, SpecialJoinInfo));
588  Assert(!IsA(node, PlaceHolderInfo));
589  Assert(!IsA(node, MinMaxAggInfo));
590 
591  if (IsA(node, Query))
592  {
593  /* Recurse into subselects */
594  bool result;
595 
596  context->sublevels_up++;
597  result = query_tree_walker((Query *) node, ChangeVarNodes_walker,
598  (void *) context, 0);
599  context->sublevels_up--;
600  return result;
601  }
603  (void *) context);
604 }
bool query_tree_walker(Query *query, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:2267
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
Index varlevelsup
Definition: primnodes.h:173
Index prti
Definition: plannodes.h:999
return result
Definition: formatting.c:1618
Definition: primnodes.h:163
Index varnoold
Definition: primnodes.h:176
Relids phrels
Definition: relation.h:1850
static Relids adjust_relid_set(Relids relids, int oldrelid, int newrelid)
Definition: rewriteManip.c:661
Index varno
Definition: primnodes.h:166
Index rti
Definition: plannodes.h:998
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
bool expression_tree_walker(Node *node, bool(*walker)(), void *context)
Definition: nodeFuncs.c:1864
static bool ChangeVarNodes_walker(Node *node, ChangeVarNodes_context *context)
Definition: rewriteManip.c:504
Index phlevelsup
Definition: relation.h:1852
Index child_relid
Definition: relation.h:1975
Index parent_relid
Definition: relation.h:1974
int rtindex
Definition: primnodes.h:1455
bool checkExprHasSubLink ( Node node)

Definition at line 277 of file rewriteManip.c.

References checkExprHasSubLink_walker(), NULL, QTW_IGNORE_RC_SUBQUERIES, and query_or_expression_tree_walker().

Referenced by AddQual(), flatten_join_alias_vars_mutator(), RelationBuildRowSecurity(), replace_rte_variables_mutator(), rewriteRuleAction(), and rewriteTargetView().

278 {
279  /*
280  * If a Query is passed, examine it --- but we should not recurse into
281  * sub-Queries that are in its rangetable or CTE list.
282  */
285  NULL,
287 }
static bool checkExprHasSubLink_walker(Node *node, void *context)
Definition: rewriteManip.c:290
#define QTW_IGNORE_RC_SUBQUERIES
Definition: nodeFuncs.h:22
bool query_or_expression_tree_walker(Node *node, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:3199
#define NULL
Definition: c.h:229
static bool checkExprHasSubLink_walker ( Node node,
void *  context 
)
static

Definition at line 290 of file rewriteManip.c.

References expression_tree_walker(), IsA, and NULL.

Referenced by checkExprHasSubLink().

291 {
292  if (node == NULL)
293  return false;
294  if (IsA(node, SubLink))
295  return true; /* abort the tree traversal and return true */
296  return expression_tree_walker(node, checkExprHasSubLink_walker, context);
297 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
static bool checkExprHasSubLink_walker(Node *node, void *context)
Definition: rewriteManip.c:290
#define NULL
Definition: c.h:229
bool expression_tree_walker(Node *node, bool(*walker)(), void *context)
Definition: nodeFuncs.c:1864
bool contain_aggs_of_level ( Node node,
int  levelsup 
)

Definition at line 67 of file rewriteManip.c.

References contain_aggs_of_level_walker(), query_or_expression_tree_walker(), and contain_aggs_of_level_context::sublevels_up.

Referenced by AddQual(), checkTargetlistEntrySQL92(), and convert_EXISTS_to_ANY().

68 {
70 
71  context.sublevels_up = levelsup;
72 
73  /*
74  * Must be prepared to start with a Query or a bare expression tree; if
75  * it's a Query, we don't want to increment sublevels_up.
76  */
79  (void *) &context,
80  0);
81 }
bool query_or_expression_tree_walker(Node *node, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:3199
static bool contain_aggs_of_level_walker(Node *node, contain_aggs_of_level_context *context)
Definition: rewriteManip.c:84
static bool contain_aggs_of_level_walker ( Node node,
contain_aggs_of_level_context context 
)
static

Definition at line 84 of file rewriteManip.c.

References expression_tree_walker(), IsA, NULL, query_tree_walker(), result, and contain_aggs_of_level_context::sublevels_up.

Referenced by contain_aggs_of_level().

86 {
87  if (node == NULL)
88  return false;
89  if (IsA(node, Aggref))
90  {
91  if (((Aggref *) node)->agglevelsup == context->sublevels_up)
92  return true; /* abort the tree traversal and return true */
93  /* else fall through to examine argument */
94  }
95  if (IsA(node, GroupingFunc))
96  {
97  if (((GroupingFunc *) node)->agglevelsup == context->sublevels_up)
98  return true;
99  /* else fall through to examine argument */
100  }
101  if (IsA(node, Query))
102  {
103  /* Recurse into subselects */
104  bool result;
105 
106  context->sublevels_up++;
107  result = query_tree_walker((Query *) node,
109  (void *) context, 0);
110  context->sublevels_up--;
111  return result;
112  }
114  (void *) context);
115 }
bool query_tree_walker(Query *query, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:2267
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
return result
Definition: formatting.c:1618
#define NULL
Definition: c.h:229
bool expression_tree_walker(Node *node, bool(*walker)(), void *context)
Definition: nodeFuncs.c:1864
static bool contain_aggs_of_level_walker(Node *node, contain_aggs_of_level_context *context)
Definition: rewriteManip.c:84
bool contain_windowfuncs ( Node node)

Definition at line 197 of file rewriteManip.c.

References contain_windowfuncs_walker(), NULL, and query_or_expression_tree_walker().

Referenced by checkTargetlistEntrySQL92(), contain_window_function(), and transformWindowFuncCall().

198 {
199  /*
200  * Must be prepared to start with a Query or a bare expression tree; if
201  * it's a Query, we don't want to increment sublevels_up.
202  */
205  NULL,
206  0);
207 }
static bool contain_windowfuncs_walker(Node *node, void *context)
Definition: rewriteManip.c:210
bool query_or_expression_tree_walker(Node *node, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:3199
#define NULL
Definition: c.h:229
static bool contain_windowfuncs_walker ( Node node,
void *  context 
)
static

Definition at line 210 of file rewriteManip.c.

References expression_tree_walker(), IsA, and NULL.

Referenced by contain_windowfuncs().

211 {
212  if (node == NULL)
213  return false;
214  if (IsA(node, WindowFunc))
215  return true; /* abort the tree traversal and return true */
216  /* Mustn't recurse into subselects */
218  (void *) context);
219 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
static bool contain_windowfuncs_walker(Node *node, void *context)
Definition: rewriteManip.c:210
#define NULL
Definition: c.h:229
bool expression_tree_walker(Node *node, bool(*walker)(), void *context)
Definition: nodeFuncs.c:1864
static bool contains_multiexpr_param ( Node node,
void *  context 
)
static

Definition at line 306 of file rewriteManip.c.

References expression_tree_walker(), IsA, NULL, and PARAM_MULTIEXPR.

Referenced by ReplaceVarsFromTargetList_callback().

307 {
308  if (node == NULL)
309  return false;
310  if (IsA(node, Param))
311  {
312  if (((Param *) node)->paramkind == PARAM_MULTIEXPR)
313  return true; /* abort the tree traversal and return true */
314  return false;
315  }
316  return expression_tree_walker(node, contains_multiexpr_param, context);
317 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
static bool contains_multiexpr_param(Node *node, void *context)
Definition: rewriteManip.c:306
#define NULL
Definition: c.h:229
bool expression_tree_walker(Node *node, bool(*walker)(), void *context)
Definition: nodeFuncs.c:1864
Query* getInsertSelectQuery ( Query parsetree,
Query ***  subquery_ptr 
)

Definition at line 921 of file rewriteManip.c.

References Assert, CMD_INSERT, CMD_SELECT, Query::commandType, elog, ERROR, FromExpr::fromlist, IsA, Query::jointree, linitial, list_length(), NULL, PRS2_NEW_VARNO, PRS2_OLD_VARNO, rt_fetch, Query::rtable, RangeTblRef::rtindex, and RangeTblEntry::subquery.

Referenced by DefineQueryRewrite(), InsertRule(), make_ruledef(), rewriteRuleAction(), and transformRuleStmt().

922 {
923  Query *selectquery;
924  RangeTblEntry *selectrte;
925  RangeTblRef *rtr;
926 
927  if (subquery_ptr)
928  *subquery_ptr = NULL;
929 
930  if (parsetree == NULL)
931  return parsetree;
932  if (parsetree->commandType != CMD_INSERT)
933  return parsetree;
934 
935  /*
936  * Currently, this is ONLY applied to rule-action queries, and so we
937  * expect to find the OLD and NEW placeholder entries in the given query.
938  * If they're not there, it must be an INSERT/SELECT in which they've been
939  * pushed down to the SELECT.
940  */
941  if (list_length(parsetree->rtable) >= 2 &&
942  strcmp(rt_fetch(PRS2_OLD_VARNO, parsetree->rtable)->eref->aliasname,
943  "old") == 0 &&
944  strcmp(rt_fetch(PRS2_NEW_VARNO, parsetree->rtable)->eref->aliasname,
945  "new") == 0)
946  return parsetree;
947  Assert(parsetree->jointree && IsA(parsetree->jointree, FromExpr));
948  if (list_length(parsetree->jointree->fromlist) != 1)
949  elog(ERROR, "expected to find SELECT subquery");
950  rtr = (RangeTblRef *) linitial(parsetree->jointree->fromlist);
951  Assert(IsA(rtr, RangeTblRef));
952  selectrte = rt_fetch(rtr->rtindex, parsetree->rtable);
953  selectquery = selectrte->subquery;
954  if (!(selectquery && IsA(selectquery, Query) &&
955  selectquery->commandType == CMD_SELECT))
956  elog(ERROR, "expected to find SELECT subquery");
957  if (list_length(selectquery->rtable) >= 2 &&
958  strcmp(rt_fetch(PRS2_OLD_VARNO, selectquery->rtable)->eref->aliasname,
959  "old") == 0 &&
960  strcmp(rt_fetch(PRS2_NEW_VARNO, selectquery->rtable)->eref->aliasname,
961  "new") == 0)
962  {
963  if (subquery_ptr)
964  *subquery_ptr = &(selectrte->subquery);
965  return selectquery;
966  }
967  elog(ERROR, "could not find rule placeholders");
968  return NULL; /* not reached */
969 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
FromExpr * jointree
Definition: parsenodes.h:136
List * fromlist
Definition: primnodes.h:1470
#define linitial(l)
Definition: pg_list.h:111
List * rtable
Definition: parsenodes.h:135
#define ERROR
Definition: elog.h:43
#define rt_fetch(rangetable_index, rangetable)
Definition: parsetree.h:31
#define PRS2_OLD_VARNO
Definition: primnodes.h:160
CmdType commandType
Definition: parsenodes.h:110
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
static int list_length(const List *l)
Definition: pg_list.h:89
Query * subquery
Definition: parsenodes.h:946
#define elog
Definition: elog.h:219
#define PRS2_NEW_VARNO
Definition: primnodes.h:161
void IncrementVarSublevelsUp ( Node node,
int  delta_sublevels_up,
int  min_sublevels_up 
)

Definition at line 773 of file rewriteManip.c.

References IncrementVarSublevelsUp_context::delta_sublevels_up, IncrementVarSublevelsUp_walker(), IncrementVarSublevelsUp_context::min_sublevels_up, QTW_EXAMINE_RTES, and query_or_expression_tree_walker().

Referenced by assign_param_for_placeholdervar(), build_minmax_path(), convert_EXISTS_sublink_to_join(), convert_EXISTS_to_ANY(), extract_lateral_references(), flatten_join_alias_vars_mutator(), pull_up_simple_subquery(), pullup_replace_vars_callback(), replace_outer_agg(), replace_outer_grouping(), ReplaceVarsFromTargetList_callback(), and substitute_actual_srf_parameters_mutator().

775 {
777 
778  context.delta_sublevels_up = delta_sublevels_up;
779  context.min_sublevels_up = min_sublevels_up;
780 
781  /*
782  * Must be prepared to start with a Query or a bare expression tree; if
783  * it's a Query, we don't want to increment sublevels_up.
784  */
787  (void *) &context,
789 }
static bool IncrementVarSublevelsUp_walker(Node *node, IncrementVarSublevelsUp_context *context)
Definition: rewriteManip.c:700
#define QTW_EXAMINE_RTES
Definition: nodeFuncs.h:25
bool query_or_expression_tree_walker(Node *node, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:3199
void IncrementVarSublevelsUp_rtable ( List rtable,
int  delta_sublevels_up,
int  min_sublevels_up 
)

Definition at line 796 of file rewriteManip.c.

References IncrementVarSublevelsUp_context::delta_sublevels_up, IncrementVarSublevelsUp_walker(), IncrementVarSublevelsUp_context::min_sublevels_up, QTW_EXAMINE_RTES, and range_table_walker().

Referenced by pull_up_simple_union_all().

798 {
800 
801  context.delta_sublevels_up = delta_sublevels_up;
802  context.min_sublevels_up = min_sublevels_up;
803 
804  range_table_walker(rtable,
806  (void *) &context,
808 }
static bool IncrementVarSublevelsUp_walker(Node *node, IncrementVarSublevelsUp_context *context)
Definition: rewriteManip.c:700
#define QTW_EXAMINE_RTES
Definition: nodeFuncs.h:25
bool range_table_walker(List *rtable, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:2311
static bool IncrementVarSublevelsUp_walker ( Node node,
IncrementVarSublevelsUp_context context 
)
static

Definition at line 700 of file rewriteManip.c.

References Aggref::agglevelsup, GroupingFunc::agglevelsup, RangeTblEntry::ctelevelsup, IncrementVarSublevelsUp_context::delta_sublevels_up, elog, ERROR, expression_tree_walker(), IsA, IncrementVarSublevelsUp_context::min_sublevels_up, NULL, PlaceHolderVar::phlevelsup, QTW_EXAMINE_RTES, query_tree_walker(), result, RTE_CTE, RangeTblEntry::rtekind, and Var::varlevelsup.

Referenced by IncrementVarSublevelsUp(), and IncrementVarSublevelsUp_rtable().

702 {
703  if (node == NULL)
704  return false;
705  if (IsA(node, Var))
706  {
707  Var *var = (Var *) node;
708 
709  if (var->varlevelsup >= context->min_sublevels_up)
710  var->varlevelsup += context->delta_sublevels_up;
711  return false; /* done here */
712  }
713  if (IsA(node, CurrentOfExpr))
714  {
715  /* this should not happen */
716  if (context->min_sublevels_up == 0)
717  elog(ERROR, "cannot push down CurrentOfExpr");
718  return false;
719  }
720  if (IsA(node, Aggref))
721  {
722  Aggref *agg = (Aggref *) node;
723 
724  if (agg->agglevelsup >= context->min_sublevels_up)
725  agg->agglevelsup += context->delta_sublevels_up;
726  /* fall through to recurse into argument */
727  }
728  if (IsA(node, GroupingFunc))
729  {
730  GroupingFunc *grp = (GroupingFunc *) node;
731 
732  if (grp->agglevelsup >= context->min_sublevels_up)
733  grp->agglevelsup += context->delta_sublevels_up;
734  /* fall through to recurse into argument */
735  }
736  if (IsA(node, PlaceHolderVar))
737  {
738  PlaceHolderVar *phv = (PlaceHolderVar *) node;
739 
740  if (phv->phlevelsup >= context->min_sublevels_up)
741  phv->phlevelsup += context->delta_sublevels_up;
742  /* fall through to recurse into argument */
743  }
744  if (IsA(node, RangeTblEntry))
745  {
746  RangeTblEntry *rte = (RangeTblEntry *) node;
747 
748  if (rte->rtekind == RTE_CTE)
749  {
750  if (rte->ctelevelsup >= context->min_sublevels_up)
751  rte->ctelevelsup += context->delta_sublevels_up;
752  }
753  return false; /* allow range_table_walker to continue */
754  }
755  if (IsA(node, Query))
756  {
757  /* Recurse into subselects */
758  bool result;
759 
760  context->min_sublevels_up++;
761  result = query_tree_walker((Query *) node,
763  (void *) context,
765  context->min_sublevels_up--;
766  return result;
767  }
769  (void *) context);
770 }
bool query_tree_walker(Query *query, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:2267
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
Index varlevelsup
Definition: primnodes.h:173
return result
Definition: formatting.c:1618
static bool IncrementVarSublevelsUp_walker(Node *node, IncrementVarSublevelsUp_context *context)
Definition: rewriteManip.c:700
Definition: primnodes.h:163
#define QTW_EXAMINE_RTES
Definition: nodeFuncs.h:25
#define ERROR
Definition: elog.h:43
Index agglevelsup
Definition: primnodes.h:309
Index agglevelsup
Definition: primnodes.h:345
#define NULL
Definition: c.h:229
bool expression_tree_walker(Node *node, bool(*walker)(), void *context)
Definition: nodeFuncs.c:1864
Index ctelevelsup
Definition: parsenodes.h:994
RTEKind rtekind
Definition: parsenodes.h:928
Index phlevelsup
Definition: relation.h:1852
#define elog
Definition: elog.h:219
int locate_agg_of_level ( Node node,
int  levelsup 
)

Definition at line 131 of file rewriteManip.c.

References locate_agg_of_level_context::agg_location, locate_agg_of_level_walker(), query_or_expression_tree_walker(), and locate_agg_of_level_context::sublevels_up.

Referenced by check_agg_arguments(), checkTargetlistEntrySQL92(), and parseCheckAggregates().

132 {
134 
135  context.agg_location = -1; /* in case we find nothing */
136  context.sublevels_up = levelsup;
137 
138  /*
139  * Must be prepared to start with a Query or a bare expression tree; if
140  * it's a Query, we don't want to increment sublevels_up.
141  */
144  (void *) &context,
145  0);
146 
147  return context.agg_location;
148 }
bool query_or_expression_tree_walker(Node *node, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:3199
static bool locate_agg_of_level_walker(Node *node, locate_agg_of_level_context *context)
Definition: rewriteManip.c:151
static bool locate_agg_of_level_walker ( Node node,
locate_agg_of_level_context context 
)
static

Definition at line 151 of file rewriteManip.c.

References locate_agg_of_level_context::agg_location, expression_tree_walker(), IsA, NULL, query_tree_walker(), result, and locate_agg_of_level_context::sublevels_up.

Referenced by locate_agg_of_level().

153 {
154  if (node == NULL)
155  return false;
156  if (IsA(node, Aggref))
157  {
158  if (((Aggref *) node)->agglevelsup == context->sublevels_up &&
159  ((Aggref *) node)->location >= 0)
160  {
161  context->agg_location = ((Aggref *) node)->location;
162  return true; /* abort the tree traversal and return true */
163  }
164  /* else fall through to examine argument */
165  }
166  if (IsA(node, GroupingFunc))
167  {
168  if (((GroupingFunc *) node)->agglevelsup == context->sublevels_up &&
169  ((GroupingFunc *) node)->location >= 0)
170  {
171  context->agg_location = ((GroupingFunc *) node)->location;
172  return true; /* abort the tree traversal and return true */
173  }
174  }
175  if (IsA(node, Query))
176  {
177  /* Recurse into subselects */
178  bool result;
179 
180  context->sublevels_up++;
181  result = query_tree_walker((Query *) node,
183  (void *) context, 0);
184  context->sublevels_up--;
185  return result;
186  }
188  (void *) context);
189 }
bool query_tree_walker(Query *query, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:2267
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
return result
Definition: formatting.c:1618
#define NULL
Definition: c.h:229
bool expression_tree_walker(Node *node, bool(*walker)(), void *context)
Definition: nodeFuncs.c:1864
static bool locate_agg_of_level_walker(Node *node, locate_agg_of_level_context *context)
Definition: rewriteManip.c:151
int locate_windowfunc ( Node node)

Definition at line 235 of file rewriteManip.c.

References locate_windowfunc_walker(), query_or_expression_tree_walker(), and locate_windowfunc_context::win_location.

Referenced by checkTargetlistEntrySQL92(), and transformWindowFuncCall().

236 {
238 
239  context.win_location = -1; /* in case we find nothing */
240 
241  /*
242  * Must be prepared to start with a Query or a bare expression tree; if
243  * it's a Query, we don't want to increment sublevels_up.
244  */
247  (void *) &context,
248  0);
249 
250  return context.win_location;
251 }
bool query_or_expression_tree_walker(Node *node, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:3199
static bool locate_windowfunc_walker(Node *node, locate_windowfunc_context *context)
Definition: rewriteManip.c:254
static bool locate_windowfunc_walker ( Node node,
locate_windowfunc_context context 
)
static

Definition at line 254 of file rewriteManip.c.

References expression_tree_walker(), IsA, NULL, and locate_windowfunc_context::win_location.

Referenced by locate_windowfunc().

255 {
256  if (node == NULL)
257  return false;
258  if (IsA(node, WindowFunc))
259  {
260  if (((WindowFunc *) node)->location >= 0)
261  {
262  context->win_location = ((WindowFunc *) node)->location;
263  return true; /* abort the tree traversal and return true */
264  }
265  /* else fall through to examine argument */
266  }
267  /* Mustn't recurse into subselects */
269  (void *) context);
270 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
#define NULL
Definition: c.h:229
bool expression_tree_walker(Node *node, bool(*walker)(), void *context)
Definition: nodeFuncs.c:1864
static bool locate_windowfunc_walker(Node *node, locate_windowfunc_context *context)
Definition: rewriteManip.c:254
Node* map_variable_attnos ( Node node,
int  target_varno,
int  sublevels_up,
const AttrNumber attno_map,
int  map_length,
bool found_whole_row 
)

Definition at line 1283 of file rewriteManip.c.

References map_variable_attnos_context::attno_map, map_variable_attnos_context::found_whole_row, map_variable_attnos_context::map_length, map_variable_attnos_mutator(), query_or_expression_tree_mutator(), map_variable_attnos_context::sublevels_up, and map_variable_attnos_context::target_varno.

Referenced by ATPrepAlterColumnType(), generateClonedIndexStmt(), map_partition_varattnos(), MergeAttributes(), and transformTableLikeClause().

1287 {
1289 
1290  context.target_varno = target_varno;
1291  context.sublevels_up = sublevels_up;
1292  context.attno_map = attno_map;
1293  context.map_length = map_length;
1294  context.found_whole_row = found_whole_row;
1295 
1296  *found_whole_row = false;
1297 
1298  /*
1299  * Must be prepared to start with a Query or a bare expression tree; if
1300  * it's a Query, we don't want to increment sublevels_up.
1301  */
1304  (void *) &context,
1305  0);
1306 }
static Node * map_variable_attnos_mutator(Node *node, map_variable_attnos_context *context)
Node * query_or_expression_tree_mutator(Node *node, Node *(*mutator)(), void *context, int flags)
Definition: nodeFuncs.c:3222
const AttrNumber * attno_map
static Node* map_variable_attnos_mutator ( Node node,
map_variable_attnos_context context 
)
static

Definition at line 1230 of file rewriteManip.c.

References map_variable_attnos_context::attno_map, elog, ERROR, expression_tree_mutator(), map_variable_attnos_context::found_whole_row, IsA, map_variable_attnos_context::map_length, NULL, palloc(), query_tree_mutator(), map_variable_attnos_context::sublevels_up, map_variable_attnos_context::target_varno, Var::varattno, Var::varlevelsup, Var::varno, and Var::varoattno.

Referenced by map_variable_attnos().

1232 {
1233  if (node == NULL)
1234  return NULL;
1235  if (IsA(node, Var))
1236  {
1237  Var *var = (Var *) node;
1238 
1239  if (var->varno == context->target_varno &&
1240  var->varlevelsup == context->sublevels_up)
1241  {
1242  /* Found a matching variable, make the substitution */
1243  Var *newvar = (Var *) palloc(sizeof(Var));
1244  int attno = var->varattno;
1245 
1246  *newvar = *var;
1247  if (attno > 0)
1248  {
1249  /* user-defined column, replace attno */
1250  if (attno > context->map_length ||
1251  context->attno_map[attno - 1] == 0)
1252  elog(ERROR, "unexpected varattno %d in expression to be mapped",
1253  attno);
1254  newvar->varattno = newvar->varoattno = context->attno_map[attno - 1];
1255  }
1256  else if (attno == 0)
1257  {
1258  /* whole-row variable, warn caller */
1259  *(context->found_whole_row) = true;
1260  }
1261  return (Node *) newvar;
1262  }
1263  /* otherwise fall through to copy the var normally */
1264  }
1265  else if (IsA(node, Query))
1266  {
1267  /* Recurse into RTE subquery or not-yet-planned sublink subquery */
1268  Query *newnode;
1269 
1270  context->sublevels_up++;
1271  newnode = query_tree_mutator((Query *) node,
1273  (void *) context,
1274  0);
1275  context->sublevels_up--;
1276  return (Node *) newnode;
1277  }
1279  (void *) context);
1280 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
Index varlevelsup
Definition: primnodes.h:173
Node * expression_tree_mutator(Node *node, Node *(*mutator)(), void *context)
Definition: nodeFuncs.c:2431
Definition: nodes.h:509
AttrNumber varattno
Definition: primnodes.h:168
Definition: primnodes.h:163
#define ERROR
Definition: elog.h:43
Index varno
Definition: primnodes.h:166
static Node * map_variable_attnos_mutator(Node *node, map_variable_attnos_context *context)
#define NULL
Definition: c.h:229
const AttrNumber * attno_map
AttrNumber varoattno
Definition: primnodes.h:177
void * palloc(Size size)
Definition: mcxt.c:849
#define elog
Definition: elog.h:219
Query * query_tree_mutator(Query *query, Node *(*mutator)(), void *context, int flags)
Definition: nodeFuncs.c:3089
static Relids offset_relid_set ( Relids  relids,
int  offset 
)
static

Definition at line 472 of file rewriteManip.c.

References bms_add_member(), bms_next_member(), NULL, and result.

Referenced by OffsetVarNodes_walker().

473 {
474  Relids result = NULL;
475  int rtindex;
476 
477  rtindex = -1;
478  while ((rtindex = bms_next_member(relids, rtindex)) >= 0)
479  result = bms_add_member(result, rtindex + offset);
480  return result;
481 }
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:937
return result
Definition: formatting.c:1618
#define NULL
Definition: c.h:229
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:698
void OffsetVarNodes ( Node node,
int  offset,
int  sublevels_up 
)

Definition at line 424 of file rewriteManip.c.

References OnConflictExpr::exclRelIndex, IsA, lfirst, OffsetVarNodes_context::offset, OffsetVarNodes_walker(), Query::onConflict, query_tree_walker(), Query::resultRelation, Query::rowMarks, RowMarkClause::rti, and OffsetVarNodes_context::sublevels_up.

Referenced by convert_EXISTS_sublink_to_join(), pull_up_simple_subquery(), rewriteRuleAction(), and UpdateRangeTableOfViewParse().

425 {
426  OffsetVarNodes_context context;
427 
428  context.offset = offset;
429  context.sublevels_up = sublevels_up;
430 
431  /*
432  * Must be prepared to start with a Query or a bare expression tree; if
433  * it's a Query, go straight to query_tree_walker to make sure that
434  * sublevels_up doesn't get incremented prematurely.
435  */
436  if (node && IsA(node, Query))
437  {
438  Query *qry = (Query *) node;
439 
440  /*
441  * If we are starting at a Query, and sublevels_up is zero, then we
442  * must also fix rangetable indexes in the Query itself --- namely
443  * resultRelation, exclRelIndex and rowMarks entries. sublevels_up
444  * cannot be zero when recursing into a subquery, so there's no need
445  * to have the same logic inside OffsetVarNodes_walker.
446  */
447  if (sublevels_up == 0)
448  {
449  ListCell *l;
450 
451  if (qry->resultRelation)
452  qry->resultRelation += offset;
453 
454  if (qry->onConflict && qry->onConflict->exclRelIndex)
455  qry->onConflict->exclRelIndex += offset;
456 
457  foreach(l, qry->rowMarks)
458  {
459  RowMarkClause *rc = (RowMarkClause *) lfirst(l);
460 
461  rc->rti += offset;
462  }
463  }
465  (void *) &context, 0);
466  }
467  else
468  OffsetVarNodes_walker(node, &context);
469 }
bool query_tree_walker(Query *query, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:2267
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
OnConflictExpr * onConflict
Definition: parsenodes.h:142
int resultRelation
Definition: parsenodes.h:120
static bool OffsetVarNodes_walker(Node *node, OffsetVarNodes_context *context)
Definition: rewriteManip.c:340
List * rowMarks
Definition: parsenodes.h:161
#define lfirst(lc)
Definition: pg_list.h:106
static bool OffsetVarNodes_walker ( Node node,
OffsetVarNodes_context context 
)
static

Definition at line 340 of file rewriteManip.c.

References Assert, AppendRelInfo::child_relid, CurrentOfExpr::cvarno, expression_tree_walker(), IsA, NULL, OffsetVarNodes_context::offset, offset_relid_set(), AppendRelInfo::parent_relid, PlaceHolderVar::phlevelsup, PlaceHolderVar::phrels, query_tree_walker(), result, RangeTblRef::rtindex, JoinExpr::rtindex, OffsetVarNodes_context::sublevels_up, Var::varlevelsup, Var::varno, and Var::varnoold.

Referenced by OffsetVarNodes().

341 {
342  if (node == NULL)
343  return false;
344  if (IsA(node, Var))
345  {
346  Var *var = (Var *) node;
347 
348  if (var->varlevelsup == context->sublevels_up)
349  {
350  var->varno += context->offset;
351  var->varnoold += context->offset;
352  }
353  return false;
354  }
355  if (IsA(node, CurrentOfExpr))
356  {
357  CurrentOfExpr *cexpr = (CurrentOfExpr *) node;
358 
359  if (context->sublevels_up == 0)
360  cexpr->cvarno += context->offset;
361  return false;
362  }
363  if (IsA(node, RangeTblRef))
364  {
365  RangeTblRef *rtr = (RangeTblRef *) node;
366 
367  if (context->sublevels_up == 0)
368  rtr->rtindex += context->offset;
369  /* the subquery itself is visited separately */
370  return false;
371  }
372  if (IsA(node, JoinExpr))
373  {
374  JoinExpr *j = (JoinExpr *) node;
375 
376  if (j->rtindex && context->sublevels_up == 0)
377  j->rtindex += context->offset;
378  /* fall through to examine children */
379  }
380  if (IsA(node, PlaceHolderVar))
381  {
382  PlaceHolderVar *phv = (PlaceHolderVar *) node;
383 
384  if (phv->phlevelsup == context->sublevels_up)
385  {
386  phv->phrels = offset_relid_set(phv->phrels,
387  context->offset);
388  }
389  /* fall through to examine children */
390  }
391  if (IsA(node, AppendRelInfo))
392  {
393  AppendRelInfo *appinfo = (AppendRelInfo *) node;
394 
395  if (context->sublevels_up == 0)
396  {
397  appinfo->parent_relid += context->offset;
398  appinfo->child_relid += context->offset;
399  }
400  /* fall through to examine children */
401  }
402  /* Shouldn't need to handle other planner auxiliary nodes here */
403  Assert(!IsA(node, PlanRowMark));
404  Assert(!IsA(node, SpecialJoinInfo));
405  Assert(!IsA(node, PlaceHolderInfo));
406  Assert(!IsA(node, MinMaxAggInfo));
407 
408  if (IsA(node, Query))
409  {
410  /* Recurse into subselects */
411  bool result;
412 
413  context->sublevels_up++;
414  result = query_tree_walker((Query *) node, OffsetVarNodes_walker,
415  (void *) context, 0);
416  context->sublevels_up--;
417  return result;
418  }
420  (void *) context);
421 }
bool query_tree_walker(Query *query, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:2267
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
Index varlevelsup
Definition: primnodes.h:173
static bool OffsetVarNodes_walker(Node *node, OffsetVarNodes_context *context)
Definition: rewriteManip.c:340
return result
Definition: formatting.c:1618
Definition: primnodes.h:163
static Relids offset_relid_set(Relids relids, int offset)
Definition: rewriteManip.c:472
Index varnoold
Definition: primnodes.h:176
Relids phrels
Definition: relation.h:1850
Index varno
Definition: primnodes.h:166
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
bool expression_tree_walker(Node *node, bool(*walker)(), void *context)
Definition: nodeFuncs.c:1864
Index phlevelsup
Definition: relation.h:1852
Index child_relid
Definition: relation.h:1975
Index parent_relid
Definition: relation.h:1974
int rtindex
Definition: primnodes.h:1455
bool rangeTableEntry_used ( Node node,
int  rt_index,
int  sublevels_up 
)

Definition at line 889 of file rewriteManip.c.

References query_or_expression_tree_walker(), rangeTableEntry_used_walker(), rangeTableEntry_used_context::rt_index, and rangeTableEntry_used_context::sublevels_up.

Referenced by fireRIRrules(), matchLocks(), rewriteRuleAction(), and transformRuleStmt().

890 {
892 
893  context.rt_index = rt_index;
894  context.sublevels_up = sublevels_up;
895 
896  /*
897  * Must be prepared to start with a Query or a bare expression tree; if
898  * it's a Query, we don't want to increment sublevels_up.
899  */
902  (void *) &context,
903  0);
904 }
bool query_or_expression_tree_walker(Node *node, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:3199
static bool rangeTableEntry_used_walker(Node *node, rangeTableEntry_used_context *context)
Definition: rewriteManip.c:823
static bool rangeTableEntry_used_walker ( Node node,
rangeTableEntry_used_context context 
)
static

Definition at line 823 of file rewriteManip.c.

References Assert, CurrentOfExpr::cvarno, expression_tree_walker(), IsA, NULL, query_tree_walker(), result, rangeTableEntry_used_context::rt_index, RangeTblRef::rtindex, JoinExpr::rtindex, rangeTableEntry_used_context::sublevels_up, Var::varlevelsup, and Var::varno.

Referenced by rangeTableEntry_used().

825 {
826  if (node == NULL)
827  return false;
828  if (IsA(node, Var))
829  {
830  Var *var = (Var *) node;
831 
832  if (var->varlevelsup == context->sublevels_up &&
833  var->varno == context->rt_index)
834  return true;
835  return false;
836  }
837  if (IsA(node, CurrentOfExpr))
838  {
839  CurrentOfExpr *cexpr = (CurrentOfExpr *) node;
840 
841  if (context->sublevels_up == 0 &&
842  cexpr->cvarno == context->rt_index)
843  return true;
844  return false;
845  }
846  if (IsA(node, RangeTblRef))
847  {
848  RangeTblRef *rtr = (RangeTblRef *) node;
849 
850  if (rtr->rtindex == context->rt_index &&
851  context->sublevels_up == 0)
852  return true;
853  /* the subquery itself is visited separately */
854  return false;
855  }
856  if (IsA(node, JoinExpr))
857  {
858  JoinExpr *j = (JoinExpr *) node;
859 
860  if (j->rtindex == context->rt_index &&
861  context->sublevels_up == 0)
862  return true;
863  /* fall through to examine children */
864  }
865  /* Shouldn't need to handle planner auxiliary nodes here */
866  Assert(!IsA(node, PlaceHolderVar));
867  Assert(!IsA(node, PlanRowMark));
868  Assert(!IsA(node, SpecialJoinInfo));
869  Assert(!IsA(node, AppendRelInfo));
870  Assert(!IsA(node, PlaceHolderInfo));
871  Assert(!IsA(node, MinMaxAggInfo));
872 
873  if (IsA(node, Query))
874  {
875  /* Recurse into subselects */
876  bool result;
877 
878  context->sublevels_up++;
880  (void *) context, 0);
881  context->sublevels_up--;
882  return result;
883  }
885  (void *) context);
886 }
bool query_tree_walker(Query *query, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:2267
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
Index varlevelsup
Definition: primnodes.h:173
return result
Definition: formatting.c:1618
Definition: primnodes.h:163
Index varno
Definition: primnodes.h:166
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
bool expression_tree_walker(Node *node, bool(*walker)(), void *context)
Definition: nodeFuncs.c:1864
static bool rangeTableEntry_used_walker(Node *node, rangeTableEntry_used_context *context)
Definition: rewriteManip.c:823
int rtindex
Definition: primnodes.h:1455
Node* replace_rte_variables ( Node node,
int  target_varno,
int  sublevels_up,
replace_rte_variables_callback  callback,
void *  callback_arg,
bool outer_hasSubLinks 
)

Definition at line 1084 of file rewriteManip.c.

References replace_rte_variables_context::callback, callback(), replace_rte_variables_context::callback_arg, elog, ERROR, replace_rte_variables_context::inserted_sublink, IsA, query_or_expression_tree_mutator(), replace_rte_variables_mutator(), result, replace_rte_variables_context::sublevels_up, and replace_rte_variables_context::target_varno.

Referenced by pullup_replace_vars(), pullup_replace_vars_subquery(), and ReplaceVarsFromTargetList().

1088 {
1089  Node *result;
1091 
1092  context.callback = callback;
1093  context.callback_arg = callback_arg;
1094  context.target_varno = target_varno;
1095  context.sublevels_up = sublevels_up;
1096 
1097  /*
1098  * We try to initialize inserted_sublink to true if there is no need to
1099  * detect new sublinks because the query already has some.
1100  */
1101  if (node && IsA(node, Query))
1102  context.inserted_sublink = ((Query *) node)->hasSubLinks;
1103  else if (outer_hasSubLinks)
1104  context.inserted_sublink = *outer_hasSubLinks;
1105  else
1106  context.inserted_sublink = false;
1107 
1108  /*
1109  * Must be prepared to start with a Query or a bare expression tree; if
1110  * it's a Query, we don't want to increment sublevels_up.
1111  */
1112  result = query_or_expression_tree_mutator(node,
1114  (void *) &context,
1115  0);
1116 
1117  if (context.inserted_sublink)
1118  {
1119  if (result && IsA(result, Query))
1120  ((Query *) result)->hasSubLinks = true;
1121  else if (outer_hasSubLinks)
1122  *outer_hasSubLinks = true;
1123  else
1124  elog(ERROR, "replace_rte_variables inserted a SubLink, but has noplace to record it");
1125  }
1126 
1127  return result;
1128 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
Definition: nodes.h:509
return result
Definition: formatting.c:1618
replace_rte_variables_callback callback
Definition: rewriteManip.h:27
#define ERROR
Definition: elog.h:43
Node * replace_rte_variables_mutator(Node *node, replace_rte_variables_context *context)
static void callback(struct sockaddr *addr, struct sockaddr *mask, void *unused)
Definition: test_ifaddrs.c:48
Node * query_or_expression_tree_mutator(Node *node, Node *(*mutator)(), void *context, int flags)
Definition: nodeFuncs.c:3222
#define elog
Definition: elog.h:219
Node* replace_rte_variables_mutator ( Node node,
replace_rte_variables_context context 
)

Definition at line 1131 of file rewriteManip.c.

References replace_rte_variables_context::callback, checkExprHasSubLink(), CurrentOfExpr::cvarno, ereport, errcode(), errmsg(), ERROR, expression_tree_mutator(), Query::hasSubLinks, replace_rte_variables_context::inserted_sublink, IsA, NULL, query_tree_mutator(), replace_rte_variables_mutator(), replace_rte_variables_context::sublevels_up, replace_rte_variables_context::target_varno, Var::varlevelsup, and Var::varno.

Referenced by pullup_replace_vars_callback(), replace_rte_variables(), replace_rte_variables_mutator(), and ReplaceVarsFromTargetList_callback().

1133 {
1134  if (node == NULL)
1135  return NULL;
1136  if (IsA(node, Var))
1137  {
1138  Var *var = (Var *) node;
1139 
1140  if (var->varno == context->target_varno &&
1141  var->varlevelsup == context->sublevels_up)
1142  {
1143  /* Found a matching variable, make the substitution */
1144  Node *newnode;
1145 
1146  newnode = (*context->callback) (var, context);
1147  /* Detect if we are adding a sublink to query */
1148  if (!context->inserted_sublink)
1149  context->inserted_sublink = checkExprHasSubLink(newnode);
1150  return newnode;
1151  }
1152  /* otherwise fall through to copy the var normally */
1153  }
1154  else if (IsA(node, CurrentOfExpr))
1155  {
1156  CurrentOfExpr *cexpr = (CurrentOfExpr *) node;
1157 
1158  if (cexpr->cvarno == context->target_varno &&
1159  context->sublevels_up == 0)
1160  {
1161  /*
1162  * We get here if a WHERE CURRENT OF expression turns out to apply
1163  * to a view. Someday we might be able to translate the
1164  * expression to apply to an underlying table of the view, but
1165  * right now it's not implemented.
1166  */
1167  ereport(ERROR,
1168  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1169  errmsg("WHERE CURRENT OF on a view is not implemented")));
1170  }
1171  /* otherwise fall through to copy the expr normally */
1172  }
1173  else if (IsA(node, Query))
1174  {
1175  /* Recurse into RTE subquery or not-yet-planned sublink subquery */
1176  Query *newnode;
1177  bool save_inserted_sublink;
1178 
1179  context->sublevels_up++;
1180  save_inserted_sublink = context->inserted_sublink;
1181  context->inserted_sublink = ((Query *) node)->hasSubLinks;
1182  newnode = query_tree_mutator((Query *) node,
1184  (void *) context,
1185  0);
1186  newnode->hasSubLinks |= context->inserted_sublink;
1187  context->inserted_sublink = save_inserted_sublink;
1188  context->sublevels_up--;
1189  return (Node *) newnode;
1190  }
1192  (void *) context);
1193 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
Index varlevelsup
Definition: primnodes.h:173
Node * expression_tree_mutator(Node *node, Node *(*mutator)(), void *context)
Definition: nodeFuncs.c:2431
bool checkExprHasSubLink(Node *node)
Definition: rewriteManip.c:277
Definition: nodes.h:509
int errcode(int sqlerrcode)
Definition: elog.c:575
replace_rte_variables_callback callback
Definition: rewriteManip.h:27
Definition: primnodes.h:163
#define ERROR
Definition: elog.h:43
Node * replace_rte_variables_mutator(Node *node, replace_rte_variables_context *context)
#define ereport(elevel, rest)
Definition: elog.h:122
Index varno
Definition: primnodes.h:166
#define NULL
Definition: c.h:229
int errmsg(const char *fmt,...)
Definition: elog.c:797
bool hasSubLinks
Definition: parsenodes.h:126
Query * query_tree_mutator(Query *query, Node *(*mutator)(), void *context, int flags)
Definition: nodeFuncs.c:3089
Node* ReplaceVarsFromTargetList ( Node node,
int  target_varno,
int  sublevels_up,
RangeTblEntry target_rte,
List targetlist,
ReplaceVarsNoMatchOption  nomatch_option,
int  nomatch_varno,
bool outer_hasSubLinks 
)

Definition at line 1441 of file rewriteManip.c.

References ReplaceVarsFromTargetList_context::nomatch_option, ReplaceVarsFromTargetList_context::nomatch_varno, replace_rte_variables(), ReplaceVarsFromTargetList_callback(), ReplaceVarsFromTargetList_context::target_rte, and ReplaceVarsFromTargetList_context::targetlist.

Referenced by CopyAndAddInvertedQual(), rewriteRuleAction(), rewriteTargetView(), and subquery_push_qual().

1448 {
1450 
1451  context.target_rte = target_rte;
1452  context.targetlist = targetlist;
1453  context.nomatch_option = nomatch_option;
1454  context.nomatch_varno = nomatch_varno;
1455 
1456  return replace_rte_variables(node, target_varno, sublevels_up,
1458  (void *) &context,
1459  outer_hasSubLinks);
1460 }
static Node * ReplaceVarsFromTargetList_callback(Var *var, replace_rte_variables_context *context)
Node * replace_rte_variables(Node *node, int target_varno, int sublevels_up, replace_rte_variables_callback callback, void *callback_arg, bool *outer_hasSubLinks)
ReplaceVarsNoMatchOption nomatch_option
static Node* ReplaceVarsFromTargetList_callback ( Var var,
replace_rte_variables_context context 
)
static

Definition at line 1337 of file rewriteManip.c.

References RowExpr::args, replace_rte_variables_context::callback_arg, COERCE_IMPLICIT_CAST, coerce_to_domain(), RowExpr::colnames, contains_multiexpr_param(), copyObject, elog, ereport, errcode(), errmsg(), ERROR, expandRTE(), get_tle_by_resno(), IncrementVarSublevelsUp(), InvalidAttrNumber, InvalidOid, Var::location, RowExpr::location, makeNode, makeNullConst(), ReplaceVarsFromTargetList_context::nomatch_option, ReplaceVarsFromTargetList_context::nomatch_varno, NULL, RECORDOID, replace_rte_variables_mutator(), REPLACEVARS_CHANGE_VARNO, REPLACEVARS_REPORT_ERROR, REPLACEVARS_SUBSTITUTE_NULL, RowExpr::row_format, RowExpr::row_typeid, ReplaceVarsFromTargetList_context::target_rte, ReplaceVarsFromTargetList_context::targetlist, Var::varattno, Var::varcollid, Var::varlevelsup, Var::varno, Var::varnoold, Var::vartype, and Var::vartypmod.

Referenced by ReplaceVarsFromTargetList().

1339 {
1341  TargetEntry *tle;
1342 
1343  if (var->varattno == InvalidAttrNumber)
1344  {
1345  /* Must expand whole-tuple reference into RowExpr */
1346  RowExpr *rowexpr;
1347  List *colnames;
1348  List *fields;
1349 
1350  /*
1351  * If generating an expansion for a var of a named rowtype (ie, this
1352  * is a plain relation RTE), then we must include dummy items for
1353  * dropped columns. If the var is RECORD (ie, this is a JOIN), then
1354  * omit dropped columns. Either way, attach column names to the
1355  * RowExpr for use of ruleutils.c.
1356  */
1357  expandRTE(rcon->target_rte,
1358  var->varno, var->varlevelsup, var->location,
1359  (var->vartype != RECORDOID),
1360  &colnames, &fields);
1361  /* Adjust the generated per-field Vars... */
1362  fields = (List *) replace_rte_variables_mutator((Node *) fields,
1363  context);
1364  rowexpr = makeNode(RowExpr);
1365  rowexpr->args = fields;
1366  rowexpr->row_typeid = var->vartype;
1367  rowexpr->row_format = COERCE_IMPLICIT_CAST;
1368  rowexpr->colnames = colnames;
1369  rowexpr->location = var->location;
1370 
1371  return (Node *) rowexpr;
1372  }
1373 
1374  /* Normal case referencing one targetlist element */
1375  tle = get_tle_by_resno(rcon->targetlist, var->varattno);
1376 
1377  if (tle == NULL || tle->resjunk)
1378  {
1379  /* Failed to find column in targetlist */
1380  switch (rcon->nomatch_option)
1381  {
1383  /* fall through, throw error below */
1384  break;
1385 
1387  var = (Var *) copyObject(var);
1388  var->varno = rcon->nomatch_varno;
1389  var->varnoold = rcon->nomatch_varno;
1390  return (Node *) var;
1391 
1393 
1394  /*
1395  * If Var is of domain type, we should add a CoerceToDomain
1396  * node, in case there is a NOT NULL domain constraint.
1397  */
1398  return coerce_to_domain((Node *) makeNullConst(var->vartype,
1399  var->vartypmod,
1400  var->varcollid),
1401  InvalidOid, -1,
1402  var->vartype,
1404  -1,
1405  false,
1406  false);
1407  }
1408  elog(ERROR, "could not find replacement targetlist entry for attno %d",
1409  var->varattno);
1410  return NULL; /* keep compiler quiet */
1411  }
1412  else
1413  {
1414  /* Make a copy of the tlist item to return */
1415  Expr *newnode = copyObject(tle->expr);
1416 
1417  /* Must adjust varlevelsup if tlist item is from higher query */
1418  if (var->varlevelsup > 0)
1419  IncrementVarSublevelsUp((Node *) newnode, var->varlevelsup, 0);
1420 
1421  /*
1422  * Check to see if the tlist item contains a PARAM_MULTIEXPR Param,
1423  * and throw error if so. This case could only happen when expanding
1424  * an ON UPDATE rule's NEW variable and the referenced tlist item in
1425  * the original UPDATE command is part of a multiple assignment. There
1426  * seems no practical way to handle such cases without multiple
1427  * evaluation of the multiple assignment's sub-select, which would
1428  * create semantic oddities that users of rules would probably prefer
1429  * not to cope with. So treat it as an unimplemented feature.
1430  */
1431  if (contains_multiexpr_param((Node *) newnode, NULL))
1432  ereport(ERROR,
1433  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1434  errmsg("NEW variables in ON UPDATE rules cannot reference columns that are part of a multiple assignment in the subject UPDATE command")));
1435 
1436  return (Node *) newnode;
1437  }
1438 }
List * args
Definition: primnodes.h:985
Index varlevelsup
Definition: primnodes.h:173
void IncrementVarSublevelsUp(Node *node, int delta_sublevels_up, int min_sublevels_up)
Definition: rewriteManip.c:773
Definition: nodes.h:509
int errcode(int sqlerrcode)
Definition: elog.c:575
AttrNumber varattno
Definition: primnodes.h:168
Definition: primnodes.h:163
Const * makeNullConst(Oid consttype, int32 consttypmod, Oid constcollid)
Definition: makefuncs.c:334
Index varnoold
Definition: primnodes.h:176
static bool contains_multiexpr_param(Node *node, void *context)
Definition: rewriteManip.c:306
#define ERROR
Definition: elog.h:43
List * colnames
Definition: primnodes.h:998
Node * replace_rte_variables_mutator(Node *node, replace_rte_variables_context *context)
Oid vartype
Definition: primnodes.h:170
Node * coerce_to_domain(Node *arg, Oid baseTypeId, int32 baseTypeMod, Oid typeId, CoercionForm cformat, int location, bool hideInputCoercion, bool lengthCoercionDone)
Definition: parse_coerce.c:648
int location
Definition: primnodes.h:178
int location
Definition: primnodes.h:999
#define RECORDOID
Definition: pg_type.h:680
#define ereport(elevel, rest)
Definition: elog.h:122
Index varno
Definition: primnodes.h:166
void expandRTE(RangeTblEntry *rte, int rtindex, int sublevels_up, int location, bool include_dropped, List **colnames, List **colvars)
#define InvalidOid
Definition: postgres_ext.h:36
#define makeNode(_type_)
Definition: nodes.h:557
#define NULL
Definition: c.h:229
Oid row_typeid
Definition: primnodes.h:986
#define InvalidAttrNumber
Definition: attnum.h:23
int errmsg(const char *fmt,...)
Definition: elog.c:797
TargetEntry * get_tle_by_resno(List *tlist, AttrNumber resno)
Oid varcollid
Definition: primnodes.h:172
#define elog
Definition: elog.h:219
#define copyObject(obj)
Definition: nodes.h:621
ReplaceVarsNoMatchOption nomatch_option
CoercionForm row_format
Definition: primnodes.h:997
Definition: pg_list.h:45
int32 vartypmod
Definition: primnodes.h:171