PostgreSQL Source Code  git master
rewriteManip.c File Reference
#include "postgres.h"
#include "catalog/pg_type.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "nodes/pathnodes.h"
#include "nodes/plannodes.h"
#include "parser/parse_coerce.h"
#include "parser/parse_relation.h"
#include "parser/parsetree.h"
#include "rewrite/rewriteManip.h"
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 AttrMap *attno_map, Oid to_rowtype, 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

◆ AddInvertedQual()

void AddInvertedQual ( Query parsetree,
Node qual 
)

Definition at line 1047 of file rewriteManip.c.

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

Referenced by CopyAndAddInvertedQual().

1048 {
1049  BooleanTest *invqual;
1050 
1051  if (qual == NULL)
1052  return;
1053 
1054  /* Need not copy input qual, because AddQual will... */
1055  invqual = makeNode(BooleanTest);
1056  invqual->arg = (Expr *) qual;
1057  invqual->booltesttype = IS_NOT_TRUE;
1058  invqual->location = -1;
1059 
1060  AddQual(parsetree, (Node *) invqual);
1061 }
Definition: nodes.h:529
Expr * arg
Definition: primnodes.h:1242
BoolTestType booltesttype
Definition: primnodes.h:1243
#define makeNode(_type_)
Definition: nodes.h:577
void AddQual(Query *parsetree, Node *qual)
Definition: rewriteManip.c:979

◆ AddQual()

void AddQual ( Query parsetree,
Node qual 
)

Definition at line 979 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(), FromExpr::quals, Query::setOperations, and Query::utilityStmt.

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

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

◆ adjust_relid_set()

static Relids adjust_relid_set ( Relids  relids,
int  oldrelid,
int  newrelid 
)
static

Definition at line 664 of file rewriteManip.c.

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

Referenced by ChangeVarNodes_walker().

665 {
666  if (bms_is_member(oldrelid, relids))
667  {
668  /* Ensure we have a modifiable copy */
669  relids = bms_copy(relids);
670  /* Remove old, add new */
671  relids = bms_del_member(relids, oldrelid);
672  relids = bms_add_member(relids, newrelid);
673  }
674  return relids;
675 }
Bitmapset * bms_copy(const Bitmapset *a)
Definition: bitmapset.c:74
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:736
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

◆ ChangeVarNodes()

void ChangeVarNodes ( Node node,
int  rt_index,
int  new_index,
int  sublevels_up 
)

Definition at line 610 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(), set_baserel_partition_constraint(), set_baserel_partition_key_exprs(), and TriggerEnabled().

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

◆ ChangeVarNodes_walker()

static bool ChangeVarNodes_walker ( Node node,
ChangeVarNodes_context context 
)
static

Definition at line 505 of file rewriteManip.c.

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

Referenced by ChangeVarNodes().

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

◆ checkExprHasSubLink()

bool checkExprHasSubLink ( Node node)

Definition at line 277 of file rewriteManip.c.

References checkExprHasSubLink_walker(), 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:3363

◆ checkExprHasSubLink_walker()

static bool checkExprHasSubLink_walker ( Node node,
void *  context 
)
static

Definition at line 290 of file rewriteManip.c.

References expression_tree_walker(), and IsA.

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:580
static bool checkExprHasSubLink_walker(Node *node, void *context)
Definition: rewriteManip.c:290
bool expression_tree_walker(Node *node, bool(*walker)(), void *context)
Definition: nodeFuncs.c:1839

◆ contain_aggs_of_level()

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:3363
static bool contain_aggs_of_level_walker(Node *node, contain_aggs_of_level_context *context)
Definition: rewriteManip.c:84

◆ contain_aggs_of_level_walker()

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, query_tree_walker(), 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:2273
#define IsA(nodeptr, _type_)
Definition: nodes.h:580
bool expression_tree_walker(Node *node, bool(*walker)(), void *context)
Definition: nodeFuncs.c:1839
static bool contain_aggs_of_level_walker(Node *node, contain_aggs_of_level_context *context)
Definition: rewriteManip.c:84

◆ contain_windowfuncs()

bool contain_windowfuncs ( Node node)

Definition at line 197 of file rewriteManip.c.

References contain_windowfuncs_walker(), 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:3363

◆ contain_windowfuncs_walker()

static bool contain_windowfuncs_walker ( Node node,
void *  context 
)
static

Definition at line 210 of file rewriteManip.c.

References expression_tree_walker(), and IsA.

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:580
static bool contain_windowfuncs_walker(Node *node, void *context)
Definition: rewriteManip.c:210
bool expression_tree_walker(Node *node, bool(*walker)(), void *context)
Definition: nodeFuncs.c:1839

◆ contains_multiexpr_param()

static bool contains_multiexpr_param ( Node node,
void *  context 
)
static

Definition at line 306 of file rewriteManip.c.

References expression_tree_walker(), IsA, 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:580
static bool contains_multiexpr_param(Node *node, void *context)
Definition: rewriteManip.c:306
bool expression_tree_walker(Node *node, bool(*walker)(), void *context)
Definition: nodeFuncs.c:1839

◆ getInsertSelectQuery()

Query* getInsertSelectQuery ( Query parsetree,
Query ***  subquery_ptr 
)

Definition at line 924 of file rewriteManip.c.

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

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

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

◆ IncrementVarSublevelsUp()

void IncrementVarSublevelsUp ( Node node,
int  delta_sublevels_up,
int  min_sublevels_up 
)

Definition at line 776 of file rewriteManip.c.

References IncrementVarSublevelsUp_context::delta_sublevels_up, IncrementVarSublevelsUp_walker(), IncrementVarSublevelsUp_context::min_sublevels_up, QTW_EXAMINE_RTES_BEFORE, 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(), inline_cte_walker(), pull_up_simple_subquery(), pullup_replace_vars_callback(), replace_outer_agg(), replace_outer_grouping(), ReplaceVarsFromTargetList_callback(), and substitute_actual_srf_parameters_mutator().

778 {
780 
781  context.delta_sublevels_up = delta_sublevels_up;
782  context.min_sublevels_up = min_sublevels_up;
783 
784  /*
785  * Must be prepared to start with a Query or a bare expression tree; if
786  * it's a Query, we don't want to increment sublevels_up.
787  */
790  (void *) &context,
792 }
#define QTW_EXAMINE_RTES_BEFORE
Definition: nodeFuncs.h:25
static bool IncrementVarSublevelsUp_walker(Node *node, IncrementVarSublevelsUp_context *context)
Definition: rewriteManip.c:703
bool query_or_expression_tree_walker(Node *node, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:3363

◆ IncrementVarSublevelsUp_rtable()

void IncrementVarSublevelsUp_rtable ( List rtable,
int  delta_sublevels_up,
int  min_sublevels_up 
)

Definition at line 799 of file rewriteManip.c.

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

Referenced by pull_up_simple_union_all().

801 {
803 
804  context.delta_sublevels_up = delta_sublevels_up;
805  context.min_sublevels_up = min_sublevels_up;
806 
807  range_table_walker(rtable,
809  (void *) &context,
811 }
#define QTW_EXAMINE_RTES_BEFORE
Definition: nodeFuncs.h:25
static bool IncrementVarSublevelsUp_walker(Node *node, IncrementVarSublevelsUp_context *context)
Definition: rewriteManip.c:703
bool range_table_walker(List *rtable, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:2372

◆ IncrementVarSublevelsUp_walker()

static bool IncrementVarSublevelsUp_walker ( Node node,
IncrementVarSublevelsUp_context context 
)
static

Definition at line 703 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, PlaceHolderVar::phlevelsup, QTW_EXAMINE_RTES_BEFORE, query_tree_walker(), RTE_CTE, RangeTblEntry::rtekind, and Var::varlevelsup.

Referenced by IncrementVarSublevelsUp(), and IncrementVarSublevelsUp_rtable().

705 {
706  if (node == NULL)
707  return false;
708  if (IsA(node, Var))
709  {
710  Var *var = (Var *) node;
711 
712  if (var->varlevelsup >= context->min_sublevels_up)
713  var->varlevelsup += context->delta_sublevels_up;
714  return false; /* done here */
715  }
716  if (IsA(node, CurrentOfExpr))
717  {
718  /* this should not happen */
719  if (context->min_sublevels_up == 0)
720  elog(ERROR, "cannot push down CurrentOfExpr");
721  return false;
722  }
723  if (IsA(node, Aggref))
724  {
725  Aggref *agg = (Aggref *) node;
726 
727  if (agg->agglevelsup >= context->min_sublevels_up)
728  agg->agglevelsup += context->delta_sublevels_up;
729  /* fall through to recurse into argument */
730  }
731  if (IsA(node, GroupingFunc))
732  {
733  GroupingFunc *grp = (GroupingFunc *) node;
734 
735  if (grp->agglevelsup >= context->min_sublevels_up)
736  grp->agglevelsup += context->delta_sublevels_up;
737  /* fall through to recurse into argument */
738  }
739  if (IsA(node, PlaceHolderVar))
740  {
741  PlaceHolderVar *phv = (PlaceHolderVar *) node;
742 
743  if (phv->phlevelsup >= context->min_sublevels_up)
744  phv->phlevelsup += context->delta_sublevels_up;
745  /* fall through to recurse into argument */
746  }
747  if (IsA(node, RangeTblEntry))
748  {
749  RangeTblEntry *rte = (RangeTblEntry *) node;
750 
751  if (rte->rtekind == RTE_CTE)
752  {
753  if (rte->ctelevelsup >= context->min_sublevels_up)
754  rte->ctelevelsup += context->delta_sublevels_up;
755  }
756  return false; /* allow range_table_walker to continue */
757  }
758  if (IsA(node, Query))
759  {
760  /* Recurse into subselects */
761  bool result;
762 
763  context->min_sublevels_up++;
764  result = query_tree_walker((Query *) node,
766  (void *) context,
768  context->min_sublevels_up--;
769  return result;
770  }
772  (void *) context);
773 }
bool query_tree_walker(Query *query, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:2273
#define IsA(nodeptr, _type_)
Definition: nodes.h:580
Index varlevelsup
Definition: primnodes.h:191
#define QTW_EXAMINE_RTES_BEFORE
Definition: nodeFuncs.h:25
static bool IncrementVarSublevelsUp_walker(Node *node, IncrementVarSublevelsUp_context *context)
Definition: rewriteManip.c:703
Definition: primnodes.h:181
#define ERROR
Definition: elog.h:43
Index agglevelsup
Definition: primnodes.h:327
Index agglevelsup
Definition: primnodes.h:363
bool expression_tree_walker(Node *node, bool(*walker)(), void *context)
Definition: nodeFuncs.c:1839
Index ctelevelsup
Definition: parsenodes.h:1080
RTEKind rtekind
Definition: parsenodes.h:976
Index phlevelsup
Definition: pathnodes.h:2107
#define elog(elevel,...)
Definition: elog.h:214

◆ locate_agg_of_level()

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:3363
static bool locate_agg_of_level_walker(Node *node, locate_agg_of_level_context *context)
Definition: rewriteManip.c:151

◆ locate_agg_of_level_walker()

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, query_tree_walker(), 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:2273
#define IsA(nodeptr, _type_)
Definition: nodes.h:580
bool expression_tree_walker(Node *node, bool(*walker)(), void *context)
Definition: nodeFuncs.c:1839
static bool locate_agg_of_level_walker(Node *node, locate_agg_of_level_context *context)
Definition: rewriteManip.c:151

◆ locate_windowfunc()

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:3363
static bool locate_windowfunc_walker(Node *node, locate_windowfunc_context *context)
Definition: rewriteManip.c:254

◆ locate_windowfunc_walker()

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, 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:580
bool expression_tree_walker(Node *node, bool(*walker)(), void *context)
Definition: nodeFuncs.c:1839
static bool locate_windowfunc_walker(Node *node, locate_windowfunc_context *context)
Definition: rewriteManip.c:254

◆ map_variable_attnos()

Node* map_variable_attnos ( Node node,
int  target_varno,
int  sublevels_up,
const AttrMap attno_map,
Oid  to_rowtype,
bool found_whole_row 
)

Definition at line 1356 of file rewriteManip.c.

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

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

1360 {
1362 
1363  context.target_varno = target_varno;
1364  context.sublevels_up = sublevels_up;
1365  context.attno_map = attno_map;
1366  context.to_rowtype = to_rowtype;
1367  context.found_whole_row = found_whole_row;
1368 
1369  *found_whole_row = false;
1370 
1371  /*
1372  * Must be prepared to start with a Query or a bare expression tree; if
1373  * it's a Query, we don't want to increment sublevels_up.
1374  */
1377  (void *) &context,
1378  0);
1379 }
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:3386

◆ map_variable_attnos_mutator()

static Node* map_variable_attnos_mutator ( Node node,
map_variable_attnos_context context 
)
static

Definition at line 1233 of file rewriteManip.c.

References ConvertRowtypeExpr::arg, Assert, map_variable_attnos_context::attno_map, AttrMap::attnums, COERCE_IMPLICIT_CAST, ConvertRowtypeExpr::convertformat, elog, ERROR, expression_tree_mutator(), map_variable_attnos_context::found_whole_row, IsA, ConvertRowtypeExpr::location, makeNode, AttrMap::maplen, OidIsValid, palloc(), query_tree_mutator(), ConvertRowtypeExpr::resulttype, map_variable_attnos_context::sublevels_up, map_variable_attnos_context::target_varno, map_variable_attnos_context::to_rowtype, Var::varattno, Var::varattnosyn, Var::varlevelsup, Var::varno, Var::varnosyn, and Var::vartype.

Referenced by map_variable_attnos().

1235 {
1236  if (node == NULL)
1237  return NULL;
1238  if (IsA(node, Var))
1239  {
1240  Var *var = (Var *) node;
1241 
1242  if (var->varno == context->target_varno &&
1243  var->varlevelsup == context->sublevels_up)
1244  {
1245  /* Found a matching variable, make the substitution */
1246  Var *newvar = (Var *) palloc(sizeof(Var));
1247  int attno = var->varattno;
1248 
1249  *newvar = *var; /* initially copy all fields of the Var */
1250 
1251  if (attno > 0)
1252  {
1253  /* user-defined column, replace attno */
1254  if (attno > context->attno_map->maplen ||
1255  context->attno_map->attnums[attno - 1] == 0)
1256  elog(ERROR, "unexpected varattno %d in expression to be mapped",
1257  attno);
1258  newvar->varattno = context->attno_map->attnums[attno - 1];
1259  /* If the syntactic referent is same RTE, fix it too */
1260  if (newvar->varnosyn == context->target_varno)
1261  newvar->varattnosyn = newvar->varattno;
1262  }
1263  else if (attno == 0)
1264  {
1265  /* whole-row variable, warn caller */
1266  *(context->found_whole_row) = true;
1267 
1268  /* If the caller expects us to convert the Var, do so. */
1269  if (OidIsValid(context->to_rowtype) &&
1270  context->to_rowtype != var->vartype)
1271  {
1272  ConvertRowtypeExpr *r;
1273 
1274  /* This certainly won't work for a RECORD variable. */
1275  Assert(var->vartype != RECORDOID);
1276 
1277  /* Var itself is changed to the requested type. */
1278  newvar->vartype = context->to_rowtype;
1279 
1280  /*
1281  * Add a conversion node on top to convert back to the
1282  * original type expected by the expression.
1283  */
1285  r->arg = (Expr *) newvar;
1286  r->resulttype = var->vartype;
1288  r->location = -1;
1289 
1290  return (Node *) r;
1291  }
1292  }
1293  return (Node *) newvar;
1294  }
1295  /* otherwise fall through to copy the var normally */
1296  }
1297  else if (IsA(node, ConvertRowtypeExpr))
1298  {
1299  ConvertRowtypeExpr *r = (ConvertRowtypeExpr *) node;
1300  Var *var = (Var *) r->arg;
1301 
1302  /*
1303  * If this is coercing a whole-row Var that we need to convert, then
1304  * just convert the Var without adding an extra ConvertRowtypeExpr.
1305  * Effectively we're simplifying var::parenttype::grandparenttype into
1306  * just var::grandparenttype. This avoids building stacks of CREs if
1307  * this function is applied repeatedly.
1308  */
1309  if (IsA(var, Var) &&
1310  var->varno == context->target_varno &&
1311  var->varlevelsup == context->sublevels_up &&
1312  var->varattno == 0 &&
1313  OidIsValid(context->to_rowtype) &&
1314  context->to_rowtype != var->vartype)
1315  {
1316  ConvertRowtypeExpr *newnode;
1317  Var *newvar = (Var *) palloc(sizeof(Var));
1318 
1319  /* whole-row variable, warn caller */
1320  *(context->found_whole_row) = true;
1321 
1322  *newvar = *var; /* initially copy all fields of the Var */
1323 
1324  /* This certainly won't work for a RECORD variable. */
1325  Assert(var->vartype != RECORDOID);
1326 
1327  /* Var itself is changed to the requested type. */
1328  newvar->vartype = context->to_rowtype;
1329 
1330  newnode = (ConvertRowtypeExpr *) palloc(sizeof(ConvertRowtypeExpr));
1331  *newnode = *r; /* initially copy all fields of the CRE */
1332  newnode->arg = (Expr *) newvar;
1333 
1334  return (Node *) newnode;
1335  }
1336  /* otherwise fall through to process the expression normally */
1337  }
1338  else if (IsA(node, Query))
1339  {
1340  /* Recurse into RTE subquery or not-yet-planned sublink subquery */
1341  Query *newnode;
1342 
1343  context->sublevels_up++;
1344  newnode = query_tree_mutator((Query *) node,
1346  (void *) context,
1347  0);
1348  context->sublevels_up--;
1349  return (Node *) newnode;
1350  }
1352  (void *) context);
1353 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:580
Index varlevelsup
Definition: primnodes.h:191
Node * expression_tree_mutator(Node *node, Node *(*mutator)(), void *context)
Definition: nodeFuncs.c:2516
int maplen
Definition: attmap.h:37
Definition: nodes.h:529
AttrNumber varattno
Definition: primnodes.h:186
Definition: primnodes.h:181
#define OidIsValid(objectId)
Definition: c.h:644
#define ERROR
Definition: elog.h:43
Oid vartype
Definition: primnodes.h:188
Index varnosyn
Definition: primnodes.h:194
Index varno
Definition: primnodes.h:184
AttrNumber varattnosyn
Definition: primnodes.h:195
CoercionForm convertformat
Definition: primnodes.h:886
static Node * map_variable_attnos_mutator(Node *node, map_variable_attnos_context *context)
#define makeNode(_type_)
Definition: nodes.h:577
#define Assert(condition)
Definition: c.h:738
AttrNumber * attnums
Definition: attmap.h:36
void * palloc(Size size)
Definition: mcxt.c:949
#define elog(elevel,...)
Definition: elog.h:214
Query * query_tree_mutator(Query *query, Node *(*mutator)(), void *context, int flags)
Definition: nodeFuncs.c:3202

◆ offset_relid_set()

static Relids offset_relid_set ( Relids  relids,
int  offset 
)
static

Definition at line 473 of file rewriteManip.c.

References bms_add_member(), and bms_next_member().

Referenced by OffsetVarNodes_walker().

474 {
475  Relids result = NULL;
476  int rtindex;
477 
478  rtindex = -1;
479  while ((rtindex = bms_next_member(relids, rtindex)) >= 0)
480  result = bms_add_member(result, rtindex + offset);
481  return result;
482 }
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1043
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:736

◆ OffsetVarNodes()

void OffsetVarNodes ( Node node,
int  offset,
int  sublevels_up 
)

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

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

◆ OffsetVarNodes_walker()

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, OffsetVarNodes_context::offset, offset_relid_set(), AppendRelInfo::parent_relid, PlaceHolderVar::phlevelsup, PlaceHolderVar::phrels, query_tree_walker(), RangeTblRef::rtindex, JoinExpr::rtindex, OffsetVarNodes_context::sublevels_up, Var::varlevelsup, Var::varno, and Var::varnosyn.

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  if (var->varnosyn > 0)
352  var->varnosyn += context->offset;
353  }
354  return false;
355  }
356  if (IsA(node, CurrentOfExpr))
357  {
358  CurrentOfExpr *cexpr = (CurrentOfExpr *) node;
359 
360  if (context->sublevels_up == 0)
361  cexpr->cvarno += context->offset;
362  return false;
363  }
364  if (IsA(node, RangeTblRef))
365  {
366  RangeTblRef *rtr = (RangeTblRef *) node;
367 
368  if (context->sublevels_up == 0)
369  rtr->rtindex += context->offset;
370  /* the subquery itself is visited separately */
371  return false;
372  }
373  if (IsA(node, JoinExpr))
374  {
375  JoinExpr *j = (JoinExpr *) node;
376 
377  if (j->rtindex && context->sublevels_up == 0)
378  j->rtindex += context->offset;
379  /* fall through to examine children */
380  }
381  if (IsA(node, PlaceHolderVar))
382  {
383  PlaceHolderVar *phv = (PlaceHolderVar *) node;
384 
385  if (phv->phlevelsup == context->sublevels_up)
386  {
387  phv->phrels = offset_relid_set(phv->phrels,
388  context->offset);
389  }
390  /* fall through to examine children */
391  }
392  if (IsA(node, AppendRelInfo))
393  {
394  AppendRelInfo *appinfo = (AppendRelInfo *) node;
395 
396  if (context->sublevels_up == 0)
397  {
398  appinfo->parent_relid += context->offset;
399  appinfo->child_relid += context->offset;
400  }
401  /* fall through to examine children */
402  }
403  /* Shouldn't need to handle other planner auxiliary nodes here */
404  Assert(!IsA(node, PlanRowMark));
405  Assert(!IsA(node, SpecialJoinInfo));
406  Assert(!IsA(node, PlaceHolderInfo));
407  Assert(!IsA(node, MinMaxAggInfo));
408 
409  if (IsA(node, Query))
410  {
411  /* Recurse into subselects */
412  bool result;
413 
414  context->sublevels_up++;
415  result = query_tree_walker((Query *) node, OffsetVarNodes_walker,
416  (void *) context, 0);
417  context->sublevels_up--;
418  return result;
419  }
421  (void *) context);
422 }
bool query_tree_walker(Query *query, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:2273
#define IsA(nodeptr, _type_)
Definition: nodes.h:580
Index varlevelsup
Definition: primnodes.h:191
static bool OffsetVarNodes_walker(Node *node, OffsetVarNodes_context *context)
Definition: rewriteManip.c:340
Definition: primnodes.h:181
static Relids offset_relid_set(Relids relids, int offset)
Definition: rewriteManip.c:473
Index varnosyn
Definition: primnodes.h:194
Index varno
Definition: primnodes.h:184
#define Assert(condition)
Definition: c.h:738
bool expression_tree_walker(Node *node, bool(*walker)(), void *context)
Definition: nodeFuncs.c:1839
Index phlevelsup
Definition: pathnodes.h:2107
Index child_relid
Definition: pathnodes.h:2230
Index parent_relid
Definition: pathnodes.h:2229
int rtindex
Definition: primnodes.h:1495

◆ rangeTableEntry_used()

bool rangeTableEntry_used ( Node node,
int  rt_index,
int  sublevels_up 
)

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

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

◆ rangeTableEntry_used_walker()

static bool rangeTableEntry_used_walker ( Node node,
rangeTableEntry_used_context context 
)
static

Definition at line 826 of file rewriteManip.c.

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

Referenced by rangeTableEntry_used().

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

◆ replace_rte_variables()

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 1087 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(), replace_rte_variables_context::sublevels_up, and replace_rte_variables_context::target_varno.

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

1091 {
1092  Node *result;
1094 
1095  context.callback = callback;
1096  context.callback_arg = callback_arg;
1097  context.target_varno = target_varno;
1098  context.sublevels_up = sublevels_up;
1099 
1100  /*
1101  * We try to initialize inserted_sublink to true if there is no need to
1102  * detect new sublinks because the query already has some.
1103  */
1104  if (node && IsA(node, Query))
1105  context.inserted_sublink = ((Query *) node)->hasSubLinks;
1106  else if (outer_hasSubLinks)
1107  context.inserted_sublink = *outer_hasSubLinks;
1108  else
1109  context.inserted_sublink = false;
1110 
1111  /*
1112  * Must be prepared to start with a Query or a bare expression tree; if
1113  * it's a Query, we don't want to increment sublevels_up.
1114  */
1115  result = query_or_expression_tree_mutator(node,
1117  (void *) &context,
1118  0);
1119 
1120  if (context.inserted_sublink)
1121  {
1122  if (result && IsA(result, Query))
1123  ((Query *) result)->hasSubLinks = true;
1124  else if (outer_hasSubLinks)
1125  *outer_hasSubLinks = true;
1126  else
1127  elog(ERROR, "replace_rte_variables inserted a SubLink, but has noplace to record it");
1128  }
1129 
1130  return result;
1131 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:580
Definition: nodes.h:529
replace_rte_variables_callback callback
Definition: rewriteManip.h:29
#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:3386
#define elog(elevel,...)
Definition: elog.h:214

◆ replace_rte_variables_mutator()

Node* replace_rte_variables_mutator ( Node node,
replace_rte_variables_context context 
)

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

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

◆ ReplaceVarsFromTargetList()

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

1521 {
1523 
1524  context.target_rte = target_rte;
1525  context.targetlist = targetlist;
1526  context.nomatch_option = nomatch_option;
1527  context.nomatch_varno = nomatch_varno;
1528 
1529  return replace_rte_variables(node, target_varno, sublevels_up,
1531  (void *) &context,
1532  outer_hasSubLinks);
1533 }
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

◆ ReplaceVarsFromTargetList_callback()

static Node* ReplaceVarsFromTargetList_callback ( Var var,
replace_rte_variables_context context 
)
static

Definition at line 1410 of file rewriteManip.c.

References RowExpr::args, replace_rte_variables_context::callback_arg, COERCE_IMPLICIT_CAST, coerce_to_domain(), COERCION_IMPLICIT, 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, 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::vartype, and Var::vartypmod.

Referenced by ReplaceVarsFromTargetList().

1412 {
1414  TargetEntry *tle;
1415 
1416  if (var->varattno == InvalidAttrNumber)
1417  {
1418  /* Must expand whole-tuple reference into RowExpr */
1419  RowExpr *rowexpr;
1420  List *colnames;
1421  List *fields;
1422 
1423  /*
1424  * If generating an expansion for a var of a named rowtype (ie, this
1425  * is a plain relation RTE), then we must include dummy items for
1426  * dropped columns. If the var is RECORD (ie, this is a JOIN), then
1427  * omit dropped columns. Either way, attach column names to the
1428  * RowExpr for use of ruleutils.c.
1429  */
1430  expandRTE(rcon->target_rte,
1431  var->varno, var->varlevelsup, var->location,
1432  (var->vartype != RECORDOID),
1433  &colnames, &fields);
1434  /* Adjust the generated per-field Vars... */
1435  fields = (List *) replace_rte_variables_mutator((Node *) fields,
1436  context);
1437  rowexpr = makeNode(RowExpr);
1438  rowexpr->args = fields;
1439  rowexpr->row_typeid = var->vartype;
1440  rowexpr->row_format = COERCE_IMPLICIT_CAST;
1441  rowexpr->colnames = colnames;
1442  rowexpr->location = var->location;
1443 
1444  return (Node *) rowexpr;
1445  }
1446 
1447  /* Normal case referencing one targetlist element */
1448  tle = get_tle_by_resno(rcon->targetlist, var->varattno);
1449 
1450  if (tle == NULL || tle->resjunk)
1451  {
1452  /* Failed to find column in targetlist */
1453  switch (rcon->nomatch_option)
1454  {
1456  /* fall through, throw error below */
1457  break;
1458 
1460  var = (Var *) copyObject(var);
1461  var->varno = rcon->nomatch_varno;
1462  /* we leave the syntactic referent alone */
1463  return (Node *) var;
1464 
1466 
1467  /*
1468  * If Var is of domain type, we should add a CoerceToDomain
1469  * node, in case there is a NOT NULL domain constraint.
1470  */
1471  return coerce_to_domain((Node *) makeNullConst(var->vartype,
1472  var->vartypmod,
1473  var->varcollid),
1474  InvalidOid, -1,
1475  var->vartype,
1478  -1,
1479  false);
1480  }
1481  elog(ERROR, "could not find replacement targetlist entry for attno %d",
1482  var->varattno);
1483  return NULL; /* keep compiler quiet */
1484  }
1485  else
1486  {
1487  /* Make a copy of the tlist item to return */
1488  Expr *newnode = copyObject(tle->expr);
1489 
1490  /* Must adjust varlevelsup if tlist item is from higher query */
1491  if (var->varlevelsup > 0)
1492  IncrementVarSublevelsUp((Node *) newnode, var->varlevelsup, 0);
1493 
1494  /*
1495  * Check to see if the tlist item contains a PARAM_MULTIEXPR Param,
1496  * and throw error if so. This case could only happen when expanding
1497  * an ON UPDATE rule's NEW variable and the referenced tlist item in
1498  * the original UPDATE command is part of a multiple assignment. There
1499  * seems no practical way to handle such cases without multiple
1500  * evaluation of the multiple assignment's sub-select, which would
1501  * create semantic oddities that users of rules would probably prefer
1502  * not to cope with. So treat it as an unimplemented feature.
1503  */
1504  if (contains_multiexpr_param((Node *) newnode, NULL))
1505  ereport(ERROR,
1506  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1507  errmsg("NEW variables in ON UPDATE rules cannot reference columns that are part of a multiple assignment in the subject UPDATE command")));
1508 
1509  return (Node *) newnode;
1510  }
1511 }
List * args
Definition: primnodes.h:1022
Index varlevelsup
Definition: primnodes.h:191
void IncrementVarSublevelsUp(Node *node, int delta_sublevels_up, int min_sublevels_up)
Definition: rewriteManip.c:776
Definition: nodes.h:529
int errcode(int sqlerrcode)
Definition: elog.c:610
AttrNumber varattno
Definition: primnodes.h:186
Definition: primnodes.h:181
Const * makeNullConst(Oid consttype, int32 consttypmod, Oid constcollid)
Definition: makefuncs.c:335
static bool contains_multiexpr_param(Node *node, void *context)
Definition: rewriteManip.c:306
#define ERROR
Definition: elog.h:43
List * colnames
Definition: primnodes.h:1038
Node * replace_rte_variables_mutator(Node *node, replace_rte_variables_context *context)
Oid vartype
Definition: primnodes.h:188
Node * coerce_to_domain(Node *arg, Oid baseTypeId, int32 baseTypeMod, Oid typeId, CoercionContext ccontext, CoercionForm cformat, int location, bool hideInputCoercion)
Definition: parse_coerce.c:666
int location
Definition: primnodes.h:196
int location
Definition: primnodes.h:1039
Index varno
Definition: primnodes.h:184
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 ereport(elevel,...)
Definition: elog.h:144
#define makeNode(_type_)
Definition: nodes.h:577
Oid row_typeid
Definition: primnodes.h:1023
#define InvalidAttrNumber
Definition: attnum.h:23
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define elog(elevel,...)
Definition: elog.h:214
TargetEntry * get_tle_by_resno(List *tlist, AttrNumber resno)
Oid varcollid
Definition: primnodes.h:190
#define copyObject(obj)
Definition: nodes.h:645
ReplaceVarsNoMatchOption nomatch_option
CoercionForm row_format
Definition: primnodes.h:1037
Definition: pg_list.h:50
int32 vartypmod
Definition: primnodes.h:189