PostgreSQL Source Code  git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
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  add_nulling_relids_context
 
struct  remove_nulling_relids_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)
 
static Nodeadd_nulling_relids_mutator (Node *node, add_nulling_relids_context *context)
 
static Noderemove_nulling_relids_mutator (Node *node, remove_nulling_relids_context *context)
 
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)
 
void CombineRangeTables (List **dst_rtable, List **dst_perminfos, List *src_rtable, List *src_perminfos)
 
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)
 
Nodeadd_nulling_relids (Node *node, const Bitmapset *target_relids, const Bitmapset *added_relids)
 
Noderemove_nulling_relids (Node *node, const Bitmapset *removable_relids, const Bitmapset *except_relids)
 
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

◆ add_nulling_relids()

Node* add_nulling_relids ( Node node,
const Bitmapset target_relids,
const Bitmapset added_relids 
)

Definition at line 1139 of file rewriteManip.c.

1142 {
1144 
1145  context.target_relids = target_relids;
1146  context.added_relids = added_relids;
1147  context.sublevels_up = 0;
1150  &context,
1151  0);
1152 }
#define query_or_expression_tree_mutator(n, m, c, f)
Definition: nodeFuncs.h:173
tree context
Definition: radixtree.h:1835
static Node * add_nulling_relids_mutator(Node *node, add_nulling_relids_context *context)

References add_nulling_relids_mutator(), context, and query_or_expression_tree_mutator.

Referenced by deconstruct_distribute_oj_quals(), mark_nullable_by_grouping(), pullup_replace_vars_callback(), and transform_MERGE_to_join().

◆ add_nulling_relids_mutator()

static Node * add_nulling_relids_mutator ( Node node,
add_nulling_relids_context context 
)
static

Definition at line 1155 of file rewriteManip.c.

1157 {
1158  if (node == NULL)
1159  return NULL;
1160  if (IsA(node, Var))
1161  {
1162  Var *var = (Var *) node;
1163 
1164  if (var->varlevelsup == context->sublevels_up &&
1165  (context->target_relids == NULL ||
1166  bms_is_member(var->varno, context->target_relids)))
1167  {
1168  Relids newnullingrels = bms_union(var->varnullingrels,
1169  context->added_relids);
1170 
1171  /* Copy the Var ... */
1172  var = copyObject(var);
1173  /* ... and replace the copy's varnullingrels field */
1174  var->varnullingrels = newnullingrels;
1175  return (Node *) var;
1176  }
1177  /* Otherwise fall through to copy the Var normally */
1178  }
1179  else if (IsA(node, PlaceHolderVar))
1180  {
1181  PlaceHolderVar *phv = (PlaceHolderVar *) node;
1182 
1183  if (phv->phlevelsup == context->sublevels_up &&
1184  (context->target_relids == NULL ||
1185  bms_overlap(phv->phrels, context->target_relids)))
1186  {
1187  Relids newnullingrels = bms_union(phv->phnullingrels,
1188  context->added_relids);
1189 
1190  /*
1191  * We don't modify the contents of the PHV's expression, only add
1192  * to phnullingrels. This corresponds to assuming that the PHV
1193  * will be evaluated at the same level as before, then perhaps be
1194  * nulled as it bubbles up. Hence, just flat-copy the node ...
1195  */
1196  phv = makeNode(PlaceHolderVar);
1197  memcpy(phv, node, sizeof(PlaceHolderVar));
1198  /* ... and replace the copy's phnullingrels field */
1199  phv->phnullingrels = newnullingrels;
1200  return (Node *) phv;
1201  }
1202  /* Otherwise fall through to copy the PlaceHolderVar normally */
1203  }
1204  else if (IsA(node, Query))
1205  {
1206  /* Recurse into RTE or sublink subquery */
1207  Query *newnode;
1208 
1209  context->sublevels_up++;
1210  newnode = query_tree_mutator((Query *) node,
1212  context,
1213  0);
1214  context->sublevels_up--;
1215  return (Node *) newnode;
1216  }
1218 }
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:510
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:251
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:582
#define expression_tree_mutator(n, m, c)
Definition: nodeFuncs.h:155
#define query_tree_mutator(q, m, c, f)
Definition: nodeFuncs.h:160
#define IsA(nodeptr, _type_)
Definition: nodes.h:158
#define copyObject(obj)
Definition: nodes.h:224
#define makeNode(_type_)
Definition: nodes.h:155
Definition: nodes.h:129
Relids phnullingrels
Definition: pathnodes.h:2801
Index phlevelsup
Definition: pathnodes.h:2807
Definition: primnodes.h:248
int varno
Definition: primnodes.h:255
Index varlevelsup
Definition: primnodes.h:280

References bms_is_member(), bms_overlap(), bms_union(), context, copyObject, expression_tree_mutator, IsA, makeNode, PlaceHolderVar::phlevelsup, PlaceHolderVar::phnullingrels, query_tree_mutator, Var::varlevelsup, and Var::varno.

Referenced by add_nulling_relids().

◆ AddInvertedQual()

void AddInvertedQual ( Query parsetree,
Node qual 
)

Definition at line 1115 of file rewriteManip.c.

1116 {
1117  BooleanTest *invqual;
1118 
1119  if (qual == NULL)
1120  return;
1121 
1122  /* Need not copy input qual, because AddQual will... */
1123  invqual = makeNode(BooleanTest);
1124  invqual->arg = (Expr *) qual;
1125  invqual->booltesttype = IS_NOT_TRUE;
1126  invqual->location = -1;
1127 
1128  AddQual(parsetree, (Node *) invqual);
1129 }
@ IS_NOT_TRUE
Definition: primnodes.h:1976
void AddQual(Query *parsetree, Node *qual)
ParseLoc location
Definition: primnodes.h:1984
BoolTestType booltesttype
Definition: primnodes.h:1983
Expr * arg
Definition: primnodes.h:1982

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

Referenced by CopyAndAddInvertedQual().

◆ AddQual()

void AddQual ( Query parsetree,
Node qual 
)

Definition at line 1047 of file rewriteManip.c.

1048 {
1049  Node *copy;
1050 
1051  if (qual == NULL)
1052  return;
1053 
1054  if (parsetree->commandType == CMD_UTILITY)
1055  {
1056  /*
1057  * There's noplace to put the qual on a utility statement.
1058  *
1059  * If it's a NOTIFY, silently ignore the qual; this means that the
1060  * NOTIFY will execute, whether or not there are any qualifying rows.
1061  * While clearly wrong, this is much more useful than refusing to
1062  * execute the rule at all, and extra NOTIFY events are harmless for
1063  * typical uses of NOTIFY.
1064  *
1065  * If it isn't a NOTIFY, error out, since unconditional execution of
1066  * other utility stmts is unlikely to be wanted. (This case is not
1067  * currently allowed anyway, but keep the test for safety.)
1068  */
1069  if (parsetree->utilityStmt && IsA(parsetree->utilityStmt, NotifyStmt))
1070  return;
1071  else
1072  ereport(ERROR,
1073  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1074  errmsg("conditional utility statements are not implemented")));
1075  }
1076 
1077  if (parsetree->setOperations != NULL)
1078  {
1079  /*
1080  * There's noplace to put the qual on a setop statement, either. (This
1081  * could be fixed, but right now the planner simply ignores any qual
1082  * condition on a setop query.)
1083  */
1084  ereport(ERROR,
1085  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1086  errmsg("conditional UNION/INTERSECT/EXCEPT statements are not implemented")));
1087  }
1088 
1089  /* INTERSECT wants the original, but we need to copy - Jan */
1090  copy = copyObject(qual);
1091 
1092  parsetree->jointree->quals = make_and_qual(parsetree->jointree->quals,
1093  copy);
1094 
1095  /*
1096  * We had better not have stuck an aggregate into the WHERE clause.
1097  */
1098  Assert(!contain_aggs_of_level(copy, 0));
1099 
1100  /*
1101  * Make sure query is marked correctly if added qual has sublinks. Need
1102  * not search qual when query is already marked.
1103  */
1104  if (!parsetree->hasSubLinks)
1105  parsetree->hasSubLinks = checkExprHasSubLink(copy);
1106 }
#define Assert(condition)
Definition: c.h:837
int errcode(int sqlerrcode)
Definition: elog.c:853
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
Node * make_and_qual(Node *qual1, Node *qual2)
Definition: makefuncs.c:730
@ CMD_UTILITY
Definition: nodes.h:270
bool checkExprHasSubLink(Node *node)
Definition: rewriteManip.c:292
bool contain_aggs_of_level(Node *node, int levelsup)
Definition: rewriteManip.c:85
Node * quals
Definition: primnodes.h:2309
FromExpr * jointree
Definition: parsenodes.h:177
Node * setOperations
Definition: parsenodes.h:221
CmdType commandType
Definition: parsenodes.h:121
Node * utilityStmt
Definition: parsenodes.h:136

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

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

◆ adjust_relid_set()

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

Definition at line 730 of file rewriteManip.c.

731 {
732  if (!IS_SPECIAL_VARNO(oldrelid) && bms_is_member(oldrelid, relids))
733  {
734  /* Ensure we have a modifiable copy */
735  relids = bms_copy(relids);
736  /* Remove old, add new */
737  relids = bms_del_member(relids, oldrelid);
738  relids = bms_add_member(relids, newrelid);
739  }
740  return relids;
741 }
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:815
Bitmapset * bms_del_member(Bitmapset *a, int x)
Definition: bitmapset.c:868
Bitmapset * bms_copy(const Bitmapset *a)
Definition: bitmapset.c:122
#define IS_SPECIAL_VARNO(varno)
Definition: primnodes.h:241

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

Referenced by ChangeVarNodes_walker().

◆ ChangeVarNodes()

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

Definition at line 668 of file rewriteManip.c.

669 {
671 
672  context.rt_index = rt_index;
673  context.new_index = new_index;
674  context.sublevels_up = sublevels_up;
675 
676  /*
677  * Must be prepared to start with a Query or a bare expression tree; if
678  * it's a Query, go straight to query_tree_walker to make sure that
679  * sublevels_up doesn't get incremented prematurely.
680  */
681  if (node && IsA(node, Query))
682  {
683  Query *qry = (Query *) node;
684 
685  /*
686  * If we are starting at a Query, and sublevels_up is zero, then we
687  * must also fix rangetable indexes in the Query itself --- namely
688  * resultRelation, mergeTargetRelation, exclRelIndex and rowMarks
689  * entries. sublevels_up cannot be zero when recursing into a
690  * subquery, so there's no need to have the same logic inside
691  * ChangeVarNodes_walker.
692  */
693  if (sublevels_up == 0)
694  {
695  ListCell *l;
696 
697  if (qry->resultRelation == rt_index)
698  qry->resultRelation = new_index;
699 
700  if (qry->mergeTargetRelation == rt_index)
701  qry->mergeTargetRelation = new_index;
702 
703  /* this is unlikely to ever be used, but ... */
704  if (qry->onConflict && qry->onConflict->exclRelIndex == rt_index)
705  qry->onConflict->exclRelIndex = new_index;
706 
707  foreach(l, qry->rowMarks)
708  {
709  RowMarkClause *rc = (RowMarkClause *) lfirst(l);
710 
711  if (rc->rti == rt_index)
712  rc->rti = new_index;
713  }
714  }
716  }
717  else
719 }
#define query_tree_walker(q, w, c, f)
Definition: nodeFuncs.h:158
#define lfirst(lc)
Definition: pg_list.h:172
static bool ChangeVarNodes_walker(Node *node, ChangeVarNodes_context *context)
Definition: rewriteManip.c:559
List * rowMarks
Definition: parsenodes.h:219
OnConflictExpr * onConflict
Definition: parsenodes.h:198

References ChangeVarNodes_walker(), context, OnConflictExpr::exclRelIndex, IsA, lfirst, Query::onConflict, query_tree_walker, Query::rowMarks, and RowMarkClause::rti.

Referenced by add_security_quals(), add_with_check_options(), ApplyRetrieveRule(), CopyAndAddInvertedQual(), get_relation_constraints(), get_relation_info(), get_relation_statistics(), infer_arbiter_indexes(), rewriteRuleAction(), rewriteTargetView(), set_baserel_partition_constraint(), set_baserel_partition_key_exprs(), and TriggerEnabled().

◆ ChangeVarNodes_walker()

static bool ChangeVarNodes_walker ( Node node,
ChangeVarNodes_context context 
)
static

Definition at line 559 of file rewriteManip.c.

560 {
561  if (node == NULL)
562  return false;
563  if (IsA(node, Var))
564  {
565  Var *var = (Var *) node;
566 
567  if (var->varlevelsup == context->sublevels_up)
568  {
569  if (var->varno == context->rt_index)
570  var->varno = context->new_index;
571  var->varnullingrels = adjust_relid_set(var->varnullingrels,
572  context->rt_index,
573  context->new_index);
574  if (var->varnosyn == context->rt_index)
575  var->varnosyn = context->new_index;
576  }
577  return false;
578  }
579  if (IsA(node, CurrentOfExpr))
580  {
581  CurrentOfExpr *cexpr = (CurrentOfExpr *) node;
582 
583  if (context->sublevels_up == 0 &&
584  cexpr->cvarno == context->rt_index)
585  cexpr->cvarno = context->new_index;
586  return false;
587  }
588  if (IsA(node, RangeTblRef))
589  {
590  RangeTblRef *rtr = (RangeTblRef *) node;
591 
592  if (context->sublevels_up == 0 &&
593  rtr->rtindex == context->rt_index)
594  rtr->rtindex = context->new_index;
595  /* the subquery itself is visited separately */
596  return false;
597  }
598  if (IsA(node, JoinExpr))
599  {
600  JoinExpr *j = (JoinExpr *) node;
601 
602  if (context->sublevels_up == 0 &&
603  j->rtindex == context->rt_index)
604  j->rtindex = context->new_index;
605  /* fall through to examine children */
606  }
607  if (IsA(node, PlaceHolderVar))
608  {
609  PlaceHolderVar *phv = (PlaceHolderVar *) node;
610 
611  if (phv->phlevelsup == context->sublevels_up)
612  {
613  phv->phrels = adjust_relid_set(phv->phrels,
614  context->rt_index,
615  context->new_index);
617  context->rt_index,
618  context->new_index);
619  }
620  /* fall through to examine children */
621  }
622  if (IsA(node, PlanRowMark))
623  {
624  PlanRowMark *rowmark = (PlanRowMark *) node;
625 
626  if (context->sublevels_up == 0)
627  {
628  if (rowmark->rti == context->rt_index)
629  rowmark->rti = context->new_index;
630  if (rowmark->prti == context->rt_index)
631  rowmark->prti = context->new_index;
632  }
633  return false;
634  }
635  if (IsA(node, AppendRelInfo))
636  {
637  AppendRelInfo *appinfo = (AppendRelInfo *) node;
638 
639  if (context->sublevels_up == 0)
640  {
641  if (appinfo->parent_relid == context->rt_index)
642  appinfo->parent_relid = context->new_index;
643  if (appinfo->child_relid == context->rt_index)
644  appinfo->child_relid = context->new_index;
645  }
646  /* fall through to examine children */
647  }
648  /* Shouldn't need to handle other planner auxiliary nodes here */
649  Assert(!IsA(node, SpecialJoinInfo));
650  Assert(!IsA(node, PlaceHolderInfo));
651  Assert(!IsA(node, MinMaxAggInfo));
652 
653  if (IsA(node, Query))
654  {
655  /* Recurse into subselects */
656  bool result;
657 
658  context->sublevels_up++;
659  result = query_tree_walker((Query *) node, ChangeVarNodes_walker,
660  context, 0);
661  context->sublevels_up--;
662  return result;
663  }
665 }
int j
Definition: isn.c:73
#define expression_tree_walker(n, w, c)
Definition: nodeFuncs.h:153
static Relids adjust_relid_set(Relids relids, int oldrelid, int newrelid)
Definition: rewriteManip.c:730
Index child_relid
Definition: pathnodes.h:2980
Index parent_relid
Definition: pathnodes.h:2979
Index prti
Definition: plannodes.h:1384

References adjust_relid_set(), Assert, AppendRelInfo::child_relid, context, CurrentOfExpr::cvarno, expression_tree_walker, IsA, j, AppendRelInfo::parent_relid, PlaceHolderVar::phlevelsup, PlaceHolderVar::phnullingrels, PlanRowMark::prti, query_tree_walker, PlanRowMark::rti, RangeTblRef::rtindex, Var::varlevelsup, and Var::varno.

Referenced by ChangeVarNodes().

◆ checkExprHasSubLink()

bool checkExprHasSubLink ( Node node)

Definition at line 292 of file rewriteManip.c.

293 {
294  /*
295  * If a Query is passed, examine it --- but we should not recurse into
296  * sub-Queries that are in its rangetable or CTE list.
297  */
300  NULL,
302 }
#define query_or_expression_tree_walker(n, w, c, f)
Definition: nodeFuncs.h:171
#define QTW_IGNORE_RC_SUBQUERIES
Definition: nodeFuncs.h:24
static bool checkExprHasSubLink_walker(Node *node, void *context)
Definition: rewriteManip.c:305

References checkExprHasSubLink_walker(), QTW_IGNORE_RC_SUBQUERIES, and query_or_expression_tree_walker.

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

◆ checkExprHasSubLink_walker()

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

Definition at line 305 of file rewriteManip.c.

306 {
307  if (node == NULL)
308  return false;
309  if (IsA(node, SubLink))
310  return true; /* abort the tree traversal and return true */
312 }

References context, expression_tree_walker, and IsA.

Referenced by checkExprHasSubLink().

◆ CombineRangeTables()

void CombineRangeTables ( List **  dst_rtable,
List **  dst_perminfos,
List src_rtable,
List src_perminfos 
)

Definition at line 347 of file rewriteManip.c.

349 {
350  ListCell *l;
351  int offset = list_length(*dst_perminfos);
352 
353  if (offset > 0)
354  {
355  foreach(l, src_rtable)
356  {
358 
359  if (rte->perminfoindex > 0)
360  rte->perminfoindex += offset;
361  }
362  }
363 
364  *dst_perminfos = list_concat(*dst_perminfos, src_perminfos);
365  *dst_rtable = list_concat(*dst_rtable, src_rtable);
366 }
List * list_concat(List *list1, const List *list2)
Definition: list.c:561
#define lfirst_node(type, lc)
Definition: pg_list.h:176
static int list_length(const List *l)
Definition: pg_list.h:152

References lfirst_node, list_concat(), and list_length().

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

◆ contain_aggs_of_level()

bool contain_aggs_of_level ( Node node,
int  levelsup 
)

Definition at line 85 of file rewriteManip.c.

86 {
88 
89  context.sublevels_up = levelsup;
90 
91  /*
92  * Must be prepared to start with a Query or a bare expression tree; if
93  * it's a Query, we don't want to increment sublevels_up.
94  */
97  &context,
98  0);
99 }
static bool contain_aggs_of_level_walker(Node *node, contain_aggs_of_level_context *context)
Definition: rewriteManip.c:102

References contain_aggs_of_level_walker(), context, and query_or_expression_tree_walker.

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

◆ contain_aggs_of_level_walker()

static bool contain_aggs_of_level_walker ( Node node,
contain_aggs_of_level_context context 
)
static

Definition at line 102 of file rewriteManip.c.

104 {
105  if (node == NULL)
106  return false;
107  if (IsA(node, Aggref))
108  {
109  if (((Aggref *) node)->agglevelsup == context->sublevels_up)
110  return true; /* abort the tree traversal and return true */
111  /* else fall through to examine argument */
112  }
113  if (IsA(node, GroupingFunc))
114  {
115  if (((GroupingFunc *) node)->agglevelsup == context->sublevels_up)
116  return true;
117  /* else fall through to examine argument */
118  }
119  if (IsA(node, Query))
120  {
121  /* Recurse into subselects */
122  bool result;
123 
124  context->sublevels_up++;
125  result = query_tree_walker((Query *) node,
127  context, 0);
128  context->sublevels_up--;
129  return result;
130  }
132  context);
133 }

References context, expression_tree_walker, IsA, and query_tree_walker.

Referenced by contain_aggs_of_level().

◆ contain_windowfuncs()

bool contain_windowfuncs ( Node node)

Definition at line 214 of file rewriteManip.c.

215 {
216  /*
217  * Must be prepared to start with a Query or a bare expression tree; if
218  * it's a Query, we don't want to increment sublevels_up.
219  */
222  NULL,
223  0);
224 }
static bool contain_windowfuncs_walker(Node *node, void *context)
Definition: rewriteManip.c:227

References contain_windowfuncs_walker(), and query_or_expression_tree_walker.

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

◆ contain_windowfuncs_walker()

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

Definition at line 227 of file rewriteManip.c.

228 {
229  if (node == NULL)
230  return false;
231  if (IsA(node, WindowFunc))
232  return true; /* abort the tree traversal and return true */
233  /* Mustn't recurse into subselects */
235 }

References context, expression_tree_walker, and IsA.

Referenced by contain_windowfuncs().

◆ contains_multiexpr_param()

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

Definition at line 321 of file rewriteManip.c.

322 {
323  if (node == NULL)
324  return false;
325  if (IsA(node, Param))
326  {
327  if (((Param *) node)->paramkind == PARAM_MULTIEXPR)
328  return true; /* abort the tree traversal and return true */
329  return false;
330  }
332 }
@ PARAM_MULTIEXPR
Definition: primnodes.h:370
static bool contains_multiexpr_param(Node *node, void *context)
Definition: rewriteManip.c:321

References context, expression_tree_walker, IsA, and PARAM_MULTIEXPR.

Referenced by ReplaceVarsFromTargetList_callback().

◆ getInsertSelectQuery()

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

Definition at line 989 of file rewriteManip.c.

990 {
991  Query *selectquery;
992  RangeTblEntry *selectrte;
993  RangeTblRef *rtr;
994 
995  if (subquery_ptr)
996  *subquery_ptr = NULL;
997 
998  if (parsetree == NULL)
999  return parsetree;
1000  if (parsetree->commandType != CMD_INSERT)
1001  return parsetree;
1002 
1003  /*
1004  * Currently, this is ONLY applied to rule-action queries, and so we
1005  * expect to find the OLD and NEW placeholder entries in the given query.
1006  * If they're not there, it must be an INSERT/SELECT in which they've been
1007  * pushed down to the SELECT.
1008  */
1009  if (list_length(parsetree->rtable) >= 2 &&
1010  strcmp(rt_fetch(PRS2_OLD_VARNO, parsetree->rtable)->eref->aliasname,
1011  "old") == 0 &&
1012  strcmp(rt_fetch(PRS2_NEW_VARNO, parsetree->rtable)->eref->aliasname,
1013  "new") == 0)
1014  return parsetree;
1015  Assert(parsetree->jointree && IsA(parsetree->jointree, FromExpr));
1016  if (list_length(parsetree->jointree->fromlist) != 1)
1017  elog(ERROR, "expected to find SELECT subquery");
1018  rtr = (RangeTblRef *) linitial(parsetree->jointree->fromlist);
1019  if (!IsA(rtr, RangeTblRef))
1020  elog(ERROR, "expected to find SELECT subquery");
1021  selectrte = rt_fetch(rtr->rtindex, parsetree->rtable);
1022  if (!(selectrte->rtekind == RTE_SUBQUERY &&
1023  selectrte->subquery &&
1024  IsA(selectrte->subquery, Query) &&
1025  selectrte->subquery->commandType == CMD_SELECT))
1026  elog(ERROR, "expected to find SELECT subquery");
1027  selectquery = selectrte->subquery;
1028  if (list_length(selectquery->rtable) >= 2 &&
1029  strcmp(rt_fetch(PRS2_OLD_VARNO, selectquery->rtable)->eref->aliasname,
1030  "old") == 0 &&
1031  strcmp(rt_fetch(PRS2_NEW_VARNO, selectquery->rtable)->eref->aliasname,
1032  "new") == 0)
1033  {
1034  if (subquery_ptr)
1035  *subquery_ptr = &(selectrte->subquery);
1036  return selectquery;
1037  }
1038  elog(ERROR, "could not find rule placeholders");
1039  return NULL; /* not reached */
1040 }
#define elog(elevel,...)
Definition: elog.h:225
@ CMD_INSERT
Definition: nodes.h:267
@ CMD_SELECT
Definition: nodes.h:265
@ RTE_SUBQUERY
Definition: parsenodes.h:1018
#define rt_fetch(rangetable_index, rangetable)
Definition: parsetree.h:31
#define linitial(l)
Definition: pg_list.h:178
#define PRS2_OLD_VARNO
Definition: primnodes.h:244
#define PRS2_NEW_VARNO
Definition: primnodes.h:245
List * fromlist
Definition: primnodes.h:2308
List * rtable
Definition: parsenodes.h:170
Query * subquery
Definition: parsenodes.h:1104
RTEKind rtekind
Definition: parsenodes.h:1047

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, RTE_SUBQUERY, RangeTblEntry::rtekind, RangeTblRef::rtindex, and RangeTblEntry::subquery.

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

◆ IncrementVarSublevelsUp()

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

Definition at line 841 of file rewriteManip.c.

843 {
845 
846  context.delta_sublevels_up = delta_sublevels_up;
847  context.min_sublevels_up = min_sublevels_up;
848 
849  /*
850  * Must be prepared to start with a Query or a bare expression tree; if
851  * it's a Query, we don't want to increment sublevels_up.
852  */
855  &context,
857 }
#define QTW_EXAMINE_RTES_BEFORE
Definition: nodeFuncs.h:27
static bool IncrementVarSublevelsUp_walker(Node *node, IncrementVarSublevelsUp_context *context)
Definition: rewriteManip.c:769

References context, IncrementVarSublevelsUp_walker(), 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_group_exprs_mutator(), flatten_join_alias_vars_mutator(), inline_cte_walker(), pull_up_simple_subquery(), pullup_replace_vars_callback(), replace_outer_agg(), replace_outer_grouping(), ReplaceVarsFromTargetList_callback(), rewriteSearchAndCycle(), and substitute_actual_srf_parameters_mutator().

◆ IncrementVarSublevelsUp_rtable()

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

Definition at line 864 of file rewriteManip.c.

866 {
868 
869  context.delta_sublevels_up = delta_sublevels_up;
870  context.min_sublevels_up = min_sublevels_up;
871 
872  range_table_walker(rtable,
874  &context,
876 }
#define range_table_walker(rt, w, c, f)
Definition: nodeFuncs.h:163

References context, IncrementVarSublevelsUp_walker(), QTW_EXAMINE_RTES_BEFORE, and range_table_walker.

Referenced by pull_up_simple_union_all().

◆ IncrementVarSublevelsUp_walker()

static bool IncrementVarSublevelsUp_walker ( Node node,
IncrementVarSublevelsUp_context context 
)
static

Definition at line 769 of file rewriteManip.c.

771 {
772  if (node == NULL)
773  return false;
774  if (IsA(node, Var))
775  {
776  Var *var = (Var *) node;
777 
778  if (var->varlevelsup >= context->min_sublevels_up)
779  var->varlevelsup += context->delta_sublevels_up;
780  return false; /* done here */
781  }
782  if (IsA(node, CurrentOfExpr))
783  {
784  /* this should not happen */
785  if (context->min_sublevels_up == 0)
786  elog(ERROR, "cannot push down CurrentOfExpr");
787  return false;
788  }
789  if (IsA(node, Aggref))
790  {
791  Aggref *agg = (Aggref *) node;
792 
793  if (agg->agglevelsup >= context->min_sublevels_up)
794  agg->agglevelsup += context->delta_sublevels_up;
795  /* fall through to recurse into argument */
796  }
797  if (IsA(node, GroupingFunc))
798  {
799  GroupingFunc *grp = (GroupingFunc *) node;
800 
801  if (grp->agglevelsup >= context->min_sublevels_up)
802  grp->agglevelsup += context->delta_sublevels_up;
803  /* fall through to recurse into argument */
804  }
805  if (IsA(node, PlaceHolderVar))
806  {
807  PlaceHolderVar *phv = (PlaceHolderVar *) node;
808 
809  if (phv->phlevelsup >= context->min_sublevels_up)
810  phv->phlevelsup += context->delta_sublevels_up;
811  /* fall through to recurse into argument */
812  }
813  if (IsA(node, RangeTblEntry))
814  {
815  RangeTblEntry *rte = (RangeTblEntry *) node;
816 
817  if (rte->rtekind == RTE_CTE)
818  {
819  if (rte->ctelevelsup >= context->min_sublevels_up)
820  rte->ctelevelsup += context->delta_sublevels_up;
821  }
822  return false; /* allow range_table_walker to continue */
823  }
824  if (IsA(node, Query))
825  {
826  /* Recurse into subselects */
827  bool result;
828 
829  context->min_sublevels_up++;
830  result = query_tree_walker((Query *) node,
832  context,
834  context->min_sublevels_up--;
835  return result;
836  }
838 }
@ RTE_CTE
Definition: parsenodes.h:1023
Index agglevelsup
Definition: primnodes.h:551
Index ctelevelsup
Definition: parsenodes.h:1198

References GroupingFunc::agglevelsup, context, RangeTblEntry::ctelevelsup, elog, ERROR, expression_tree_walker, IsA, PlaceHolderVar::phlevelsup, QTW_EXAMINE_RTES_BEFORE, query_tree_walker, RTE_CTE, RangeTblEntry::rtekind, and Var::varlevelsup.

Referenced by IncrementVarSublevelsUp(), and IncrementVarSublevelsUp_rtable().

◆ locate_agg_of_level()

int locate_agg_of_level ( Node node,
int  levelsup 
)

Definition at line 149 of file rewriteManip.c.

150 {
152 
153  context.agg_location = -1; /* in case we find nothing */
154  context.sublevels_up = levelsup;
155 
156  /*
157  * Must be prepared to start with a Query or a bare expression tree; if
158  * it's a Query, we don't want to increment sublevels_up.
159  */
162  &context,
163  0);
164 
165  return context.agg_location;
166 }
static bool locate_agg_of_level_walker(Node *node, locate_agg_of_level_context *context)
Definition: rewriteManip.c:169

References context, locate_agg_of_level_walker(), and query_or_expression_tree_walker.

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

◆ locate_agg_of_level_walker()

static bool locate_agg_of_level_walker ( Node node,
locate_agg_of_level_context context 
)
static

Definition at line 169 of file rewriteManip.c.

171 {
172  if (node == NULL)
173  return false;
174  if (IsA(node, Aggref))
175  {
176  if (((Aggref *) node)->agglevelsup == context->sublevels_up &&
177  ((Aggref *) node)->location >= 0)
178  {
179  context->agg_location = ((Aggref *) node)->location;
180  return true; /* abort the tree traversal and return true */
181  }
182  /* else fall through to examine argument */
183  }
184  if (IsA(node, GroupingFunc))
185  {
186  if (((GroupingFunc *) node)->agglevelsup == context->sublevels_up &&
187  ((GroupingFunc *) node)->location >= 0)
188  {
189  context->agg_location = ((GroupingFunc *) node)->location;
190  return true; /* abort the tree traversal and return true */
191  }
192  }
193  if (IsA(node, Query))
194  {
195  /* Recurse into subselects */
196  bool result;
197 
198  context->sublevels_up++;
199  result = query_tree_walker((Query *) node,
201  context, 0);
202  context->sublevels_up--;
203  return result;
204  }
206 }

References context, expression_tree_walker, IsA, and query_tree_walker.

Referenced by locate_agg_of_level().

◆ locate_windowfunc()

int locate_windowfunc ( Node node)

Definition at line 251 of file rewriteManip.c.

252 {
254 
255  context.win_location = -1; /* in case we find nothing */
256 
257  /*
258  * Must be prepared to start with a Query or a bare expression tree; if
259  * it's a Query, we don't want to increment sublevels_up.
260  */
263  &context,
264  0);
265 
266  return context.win_location;
267 }
static bool locate_windowfunc_walker(Node *node, locate_windowfunc_context *context)
Definition: rewriteManip.c:270

References context, locate_windowfunc_walker(), and query_or_expression_tree_walker.

Referenced by checkTargetlistEntrySQL92(), and transformWindowFuncCall().

◆ locate_windowfunc_walker()

static bool locate_windowfunc_walker ( Node node,
locate_windowfunc_context context 
)
static

Definition at line 270 of file rewriteManip.c.

271 {
272  if (node == NULL)
273  return false;
274  if (IsA(node, WindowFunc))
275  {
276  if (((WindowFunc *) node)->location >= 0)
277  {
278  context->win_location = ((WindowFunc *) node)->location;
279  return true; /* abort the tree traversal and return true */
280  }
281  /* else fall through to examine argument */
282  }
283  /* Mustn't recurse into subselects */
285 }

References context, expression_tree_walker, and IsA.

Referenced by locate_windowfunc().

◆ 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 1601 of file rewriteManip.c.

1605 {
1607 
1608  context.target_varno = target_varno;
1609  context.sublevels_up = sublevels_up;
1610  context.attno_map = attno_map;
1611  context.to_rowtype = to_rowtype;
1612  context.found_whole_row = found_whole_row;
1613 
1614  *found_whole_row = false;
1615 
1616  /*
1617  * Must be prepared to start with a Query or a bare expression tree; if
1618  * it's a Query, we don't want to increment sublevels_up.
1619  */
1622  &context,
1623  0);
1624 }
static Node * map_variable_attnos_mutator(Node *node, map_variable_attnos_context *context)

References context, map_variable_attnos_mutator(), and query_or_expression_tree_mutator.

Referenced by ATPrepAlterColumnType(), CompareIndexInfo(), ExecInitPartitionInfo(), expandTableLikeClause(), generateClonedExtStatsStmt(), generateClonedIndexStmt(), map_partition_varattnos(), and MergeAttributes().

◆ map_variable_attnos_mutator()

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

Definition at line 1479 of file rewriteManip.c.

1481 {
1482  if (node == NULL)
1483  return NULL;
1484  if (IsA(node, Var))
1485  {
1486  Var *var = (Var *) node;
1487 
1488  if (var->varno == context->target_varno &&
1489  var->varlevelsup == context->sublevels_up)
1490  {
1491  /* Found a matching variable, make the substitution */
1492  Var *newvar = (Var *) palloc(sizeof(Var));
1493  int attno = var->varattno;
1494 
1495  *newvar = *var; /* initially copy all fields of the Var */
1496 
1497  if (attno > 0)
1498  {
1499  /* user-defined column, replace attno */
1500  if (attno > context->attno_map->maplen ||
1501  context->attno_map->attnums[attno - 1] == 0)
1502  elog(ERROR, "unexpected varattno %d in expression to be mapped",
1503  attno);
1504  newvar->varattno = context->attno_map->attnums[attno - 1];
1505  /* If the syntactic referent is same RTE, fix it too */
1506  if (newvar->varnosyn == context->target_varno)
1507  newvar->varattnosyn = newvar->varattno;
1508  }
1509  else if (attno == 0)
1510  {
1511  /* whole-row variable, warn caller */
1512  *(context->found_whole_row) = true;
1513 
1514  /* If the caller expects us to convert the Var, do so. */
1515  if (OidIsValid(context->to_rowtype) &&
1516  context->to_rowtype != var->vartype)
1517  {
1518  ConvertRowtypeExpr *r;
1519 
1520  /* This certainly won't work for a RECORD variable. */
1521  Assert(var->vartype != RECORDOID);
1522 
1523  /* Var itself is changed to the requested type. */
1524  newvar->vartype = context->to_rowtype;
1525 
1526  /*
1527  * Add a conversion node on top to convert back to the
1528  * original type expected by the expression.
1529  */
1531  r->arg = (Expr *) newvar;
1532  r->resulttype = var->vartype;
1533  r->convertformat = COERCE_IMPLICIT_CAST;
1534  r->location = -1;
1535 
1536  return (Node *) r;
1537  }
1538  }
1539  return (Node *) newvar;
1540  }
1541  /* otherwise fall through to copy the var normally */
1542  }
1543  else if (IsA(node, ConvertRowtypeExpr))
1544  {
1545  ConvertRowtypeExpr *r = (ConvertRowtypeExpr *) node;
1546  Var *var = (Var *) r->arg;
1547 
1548  /*
1549  * If this is coercing a whole-row Var that we need to convert, then
1550  * just convert the Var without adding an extra ConvertRowtypeExpr.
1551  * Effectively we're simplifying var::parenttype::grandparenttype into
1552  * just var::grandparenttype. This avoids building stacks of CREs if
1553  * this function is applied repeatedly.
1554  */
1555  if (IsA(var, Var) &&
1556  var->varno == context->target_varno &&
1557  var->varlevelsup == context->sublevels_up &&
1558  var->varattno == 0 &&
1559  OidIsValid(context->to_rowtype) &&
1560  context->to_rowtype != var->vartype)
1561  {
1562  ConvertRowtypeExpr *newnode;
1563  Var *newvar = (Var *) palloc(sizeof(Var));
1564 
1565  /* whole-row variable, warn caller */
1566  *(context->found_whole_row) = true;
1567 
1568  *newvar = *var; /* initially copy all fields of the Var */
1569 
1570  /* This certainly won't work for a RECORD variable. */
1571  Assert(var->vartype != RECORDOID);
1572 
1573  /* Var itself is changed to the requested type. */
1574  newvar->vartype = context->to_rowtype;
1575 
1576  newnode = (ConvertRowtypeExpr *) palloc(sizeof(ConvertRowtypeExpr));
1577  *newnode = *r; /* initially copy all fields of the CRE */
1578  newnode->arg = (Expr *) newvar;
1579 
1580  return (Node *) newnode;
1581  }
1582  /* otherwise fall through to process the expression normally */
1583  }
1584  else if (IsA(node, Query))
1585  {
1586  /* Recurse into RTE subquery or not-yet-planned sublink subquery */
1587  Query *newnode;
1588 
1589  context->sublevels_up++;
1590  newnode = query_tree_mutator((Query *) node,
1592  context,
1593  0);
1594  context->sublevels_up--;
1595  return (Node *) newnode;
1596  }
1598 }
#define OidIsValid(objectId)
Definition: c.h:754
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:76
void * palloc(Size size)
Definition: mcxt.c:1317
@ COERCE_IMPLICIT_CAST
Definition: primnodes.h:736
AttrNumber varattno
Definition: primnodes.h:260

References ConvertRowtypeExpr::arg, Assert, COERCE_IMPLICIT_CAST, context, elog, ERROR, expression_tree_mutator, if(), IsA, ConvertRowtypeExpr::location, makeNode, OidIsValid, palloc(), query_tree_mutator, ConvertRowtypeExpr::resulttype, Var::varattno, Var::varlevelsup, and Var::varno.

Referenced by map_variable_attnos().

◆ offset_relid_set()

static Relids offset_relid_set ( Relids  relids,
int  offset 
)
static

Definition at line 527 of file rewriteManip.c.

528 {
529  Relids result = NULL;
530  int rtindex;
531 
532  rtindex = -1;
533  while ((rtindex = bms_next_member(relids, rtindex)) >= 0)
534  result = bms_add_member(result, rtindex + offset);
535  return result;
536 }
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1306

References bms_add_member(), and bms_next_member().

Referenced by OffsetVarNodes_walker().

◆ OffsetVarNodes()

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

Definition at line 476 of file rewriteManip.c.

477 {
479 
480  context.offset = offset;
481  context.sublevels_up = sublevels_up;
482 
483  /*
484  * Must be prepared to start with a Query or a bare expression tree; if
485  * it's a Query, go straight to query_tree_walker to make sure that
486  * sublevels_up doesn't get incremented prematurely.
487  */
488  if (node && IsA(node, Query))
489  {
490  Query *qry = (Query *) node;
491 
492  /*
493  * If we are starting at a Query, and sublevels_up is zero, then we
494  * must also fix rangetable indexes in the Query itself --- namely
495  * resultRelation, mergeTargetRelation, exclRelIndex and rowMarks
496  * entries. sublevels_up cannot be zero when recursing into a
497  * subquery, so there's no need to have the same logic inside
498  * OffsetVarNodes_walker.
499  */
500  if (sublevels_up == 0)
501  {
502  ListCell *l;
503 
504  if (qry->resultRelation)
505  qry->resultRelation += offset;
506 
507  if (qry->mergeTargetRelation)
508  qry->mergeTargetRelation += offset;
509 
510  if (qry->onConflict && qry->onConflict->exclRelIndex)
511  qry->onConflict->exclRelIndex += offset;
512 
513  foreach(l, qry->rowMarks)
514  {
515  RowMarkClause *rc = (RowMarkClause *) lfirst(l);
516 
517  rc->rti += offset;
518  }
519  }
521  }
522  else
524 }
static bool OffsetVarNodes_walker(Node *node, OffsetVarNodes_context *context)
Definition: rewriteManip.c:388

References context, OnConflictExpr::exclRelIndex, IsA, lfirst, OffsetVarNodes_walker(), Query::onConflict, query_tree_walker, Query::rowMarks, and RowMarkClause::rti.

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

◆ OffsetVarNodes_walker()

static bool OffsetVarNodes_walker ( Node node,
OffsetVarNodes_context context 
)
static

Definition at line 388 of file rewriteManip.c.

389 {
390  if (node == NULL)
391  return false;
392  if (IsA(node, Var))
393  {
394  Var *var = (Var *) node;
395 
396  if (var->varlevelsup == context->sublevels_up)
397  {
398  var->varno += context->offset;
399  var->varnullingrels = offset_relid_set(var->varnullingrels,
400  context->offset);
401  if (var->varnosyn > 0)
402  var->varnosyn += context->offset;
403  }
404  return false;
405  }
406  if (IsA(node, CurrentOfExpr))
407  {
408  CurrentOfExpr *cexpr = (CurrentOfExpr *) node;
409 
410  if (context->sublevels_up == 0)
411  cexpr->cvarno += context->offset;
412  return false;
413  }
414  if (IsA(node, RangeTblRef))
415  {
416  RangeTblRef *rtr = (RangeTblRef *) node;
417 
418  if (context->sublevels_up == 0)
419  rtr->rtindex += context->offset;
420  /* the subquery itself is visited separately */
421  return false;
422  }
423  if (IsA(node, JoinExpr))
424  {
425  JoinExpr *j = (JoinExpr *) node;
426 
427  if (j->rtindex && context->sublevels_up == 0)
428  j->rtindex += context->offset;
429  /* fall through to examine children */
430  }
431  if (IsA(node, PlaceHolderVar))
432  {
433  PlaceHolderVar *phv = (PlaceHolderVar *) node;
434 
435  if (phv->phlevelsup == context->sublevels_up)
436  {
437  phv->phrels = offset_relid_set(phv->phrels,
438  context->offset);
440  context->offset);
441  }
442  /* fall through to examine children */
443  }
444  if (IsA(node, AppendRelInfo))
445  {
446  AppendRelInfo *appinfo = (AppendRelInfo *) node;
447 
448  if (context->sublevels_up == 0)
449  {
450  appinfo->parent_relid += context->offset;
451  appinfo->child_relid += context->offset;
452  }
453  /* fall through to examine children */
454  }
455  /* Shouldn't need to handle other planner auxiliary nodes here */
456  Assert(!IsA(node, PlanRowMark));
457  Assert(!IsA(node, SpecialJoinInfo));
458  Assert(!IsA(node, PlaceHolderInfo));
459  Assert(!IsA(node, MinMaxAggInfo));
460 
461  if (IsA(node, Query))
462  {
463  /* Recurse into subselects */
464  bool result;
465 
466  context->sublevels_up++;
467  result = query_tree_walker((Query *) node, OffsetVarNodes_walker,
468  context, 0);
469  context->sublevels_up--;
470  return result;
471  }
473 }
static Relids offset_relid_set(Relids relids, int offset)
Definition: rewriteManip.c:527

References Assert, AppendRelInfo::child_relid, context, CurrentOfExpr::cvarno, expression_tree_walker, IsA, j, offset_relid_set(), AppendRelInfo::parent_relid, PlaceHolderVar::phlevelsup, PlaceHolderVar::phnullingrels, query_tree_walker, RangeTblRef::rtindex, Var::varlevelsup, and Var::varno.

Referenced by OffsetVarNodes().

◆ rangeTableEntry_used()

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

Definition at line 957 of file rewriteManip.c.

958 {
960 
961  context.rt_index = rt_index;
962  context.sublevels_up = sublevels_up;
963 
964  /*
965  * Must be prepared to start with a Query or a bare expression tree; if
966  * it's a Query, we don't want to increment sublevels_up.
967  */
970  &context,
971  0);
972 }
static bool rangeTableEntry_used_walker(Node *node, rangeTableEntry_used_context *context)
Definition: rewriteManip.c:891

References context, query_or_expression_tree_walker, and rangeTableEntry_used_walker().

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

◆ rangeTableEntry_used_walker()

static bool rangeTableEntry_used_walker ( Node node,
rangeTableEntry_used_context context 
)
static

Definition at line 891 of file rewriteManip.c.

893 {
894  if (node == NULL)
895  return false;
896  if (IsA(node, Var))
897  {
898  Var *var = (Var *) node;
899 
900  if (var->varlevelsup == context->sublevels_up &&
901  (var->varno == context->rt_index ||
902  bms_is_member(context->rt_index, var->varnullingrels)))
903  return true;
904  return false;
905  }
906  if (IsA(node, CurrentOfExpr))
907  {
908  CurrentOfExpr *cexpr = (CurrentOfExpr *) node;
909 
910  if (context->sublevels_up == 0 &&
911  cexpr->cvarno == context->rt_index)
912  return true;
913  return false;
914  }
915  if (IsA(node, RangeTblRef))
916  {
917  RangeTblRef *rtr = (RangeTblRef *) node;
918 
919  if (rtr->rtindex == context->rt_index &&
920  context->sublevels_up == 0)
921  return true;
922  /* the subquery itself is visited separately */
923  return false;
924  }
925  if (IsA(node, JoinExpr))
926  {
927  JoinExpr *j = (JoinExpr *) node;
928 
929  if (j->rtindex == context->rt_index &&
930  context->sublevels_up == 0)
931  return true;
932  /* fall through to examine children */
933  }
934  /* Shouldn't need to handle planner auxiliary nodes here */
935  Assert(!IsA(node, PlaceHolderVar));
936  Assert(!IsA(node, PlanRowMark));
937  Assert(!IsA(node, SpecialJoinInfo));
938  Assert(!IsA(node, AppendRelInfo));
939  Assert(!IsA(node, PlaceHolderInfo));
940  Assert(!IsA(node, MinMaxAggInfo));
941 
942  if (IsA(node, Query))
943  {
944  /* Recurse into subselects */
945  bool result;
946 
947  context->sublevels_up++;
949  context, 0);
950  context->sublevels_up--;
951  return result;
952  }
954 }

References Assert, bms_is_member(), context, CurrentOfExpr::cvarno, expression_tree_walker, IsA, j, query_tree_walker, RangeTblRef::rtindex, Var::varlevelsup, and Var::varno.

Referenced by rangeTableEntry_used().

◆ remove_nulling_relids()

Node* remove_nulling_relids ( Node node,
const Bitmapset removable_relids,
const Bitmapset except_relids 
)

Definition at line 1227 of file rewriteManip.c.

1230 {
1232 
1233  context.removable_relids = removable_relids;
1234  context.except_relids = except_relids;
1235  context.sublevels_up = 0;
1238  &context,
1239  0);
1240 }
static Node * remove_nulling_relids_mutator(Node *node, remove_nulling_relids_context *context)

References context, query_or_expression_tree_mutator, and remove_nulling_relids_mutator().

Referenced by deconstruct_distribute_oj_quals(), have_partkey_equi_join(), make_group_input_target(), make_pathkeys_for_sortclauses_extended(), reconsider_full_join_clause(), reduce_outer_joins(), remove_useless_result_rtes(), and set_upper_references().

◆ remove_nulling_relids_mutator()

static Node * remove_nulling_relids_mutator ( Node node,
remove_nulling_relids_context context 
)
static

Definition at line 1243 of file rewriteManip.c.

1245 {
1246  if (node == NULL)
1247  return NULL;
1248  if (IsA(node, Var))
1249  {
1250  Var *var = (Var *) node;
1251 
1252  if (var->varlevelsup == context->sublevels_up &&
1253  !bms_is_member(var->varno, context->except_relids) &&
1254  bms_overlap(var->varnullingrels, context->removable_relids))
1255  {
1256  /* Copy the Var ... */
1257  var = copyObject(var);
1258  /* ... and replace the copy's varnullingrels field */
1259  var->varnullingrels = bms_difference(var->varnullingrels,
1260  context->removable_relids);
1261  return (Node *) var;
1262  }
1263  /* Otherwise fall through to copy the Var normally */
1264  }
1265  else if (IsA(node, PlaceHolderVar))
1266  {
1267  PlaceHolderVar *phv = (PlaceHolderVar *) node;
1268 
1269  if (phv->phlevelsup == context->sublevels_up &&
1270  !bms_overlap(phv->phrels, context->except_relids))
1271  {
1272  /*
1273  * Note: it might seem desirable to remove the PHV altogether if
1274  * phnullingrels goes to empty. Currently we dare not do that
1275  * because we use PHVs in some cases to enforce separate identity
1276  * of subexpressions; see wrap_non_vars usages in prepjointree.c.
1277  */
1278  /* Copy the PlaceHolderVar and mutate what's below ... */
1279  phv = (PlaceHolderVar *)
1282  context);
1283  /* ... and replace the copy's phnullingrels field */
1285  context->removable_relids);
1286  /* We must also update phrels, if it contains a removable RTI */
1287  phv->phrels = bms_difference(phv->phrels,
1288  context->removable_relids);
1289  Assert(!bms_is_empty(phv->phrels));
1290  return (Node *) phv;
1291  }
1292  /* Otherwise fall through to copy the PlaceHolderVar normally */
1293  }
1294  else if (IsA(node, Query))
1295  {
1296  /* Recurse into RTE or sublink subquery */
1297  Query *newnode;
1298 
1299  context->sublevels_up++;
1300  newnode = query_tree_mutator((Query *) node,
1302  context,
1303  0);
1304  context->sublevels_up--;
1305  return (Node *) newnode;
1306  }
1308 }
Bitmapset * bms_difference(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:346
#define bms_is_empty(a)
Definition: bitmapset.h:118

References Assert, bms_difference(), bms_is_empty, bms_is_member(), bms_overlap(), context, copyObject, expression_tree_mutator, IsA, PlaceHolderVar::phlevelsup, PlaceHolderVar::phnullingrels, query_tree_mutator, Var::varlevelsup, and Var::varno.

Referenced by remove_nulling_relids().

◆ 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 1334 of file rewriteManip.c.

1338 {
1339  Node *result;
1341 
1342  context.callback = callback;
1343  context.callback_arg = callback_arg;
1344  context.target_varno = target_varno;
1345  context.sublevels_up = sublevels_up;
1346 
1347  /*
1348  * We try to initialize inserted_sublink to true if there is no need to
1349  * detect new sublinks because the query already has some.
1350  */
1351  if (node && IsA(node, Query))
1352  context.inserted_sublink = ((Query *) node)->hasSubLinks;
1353  else if (outer_hasSubLinks)
1354  context.inserted_sublink = *outer_hasSubLinks;
1355  else
1356  context.inserted_sublink = false;
1357 
1358  /*
1359  * Must be prepared to start with a Query or a bare expression tree; if
1360  * it's a Query, we don't want to increment sublevels_up.
1361  */
1362  result = query_or_expression_tree_mutator(node,
1364  &context,
1365  0);
1366 
1367  if (context.inserted_sublink)
1368  {
1369  if (result && IsA(result, Query))
1370  ((Query *) result)->hasSubLinks = true;
1371  else if (outer_hasSubLinks)
1372  *outer_hasSubLinks = true;
1373  else
1374  elog(ERROR, "replace_rte_variables inserted a SubLink, but has noplace to record it");
1375  }
1376 
1377  return result;
1378 }
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:46

References callback(), context, elog, ERROR, IsA, query_or_expression_tree_mutator, and replace_rte_variables_mutator().

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

◆ replace_rte_variables_mutator()

Node* replace_rte_variables_mutator ( Node node,
replace_rte_variables_context context 
)

Definition at line 1381 of file rewriteManip.c.

1383 {
1384  if (node == NULL)
1385  return NULL;
1386  if (IsA(node, Var))
1387  {
1388  Var *var = (Var *) node;
1389 
1390  if (var->varno == context->target_varno &&
1391  var->varlevelsup == context->sublevels_up)
1392  {
1393  /* Found a matching variable, make the substitution */
1394  Node *newnode;
1395 
1396  newnode = context->callback(var, context);
1397  /* Detect if we are adding a sublink to query */
1398  if (!context->inserted_sublink)
1399  context->inserted_sublink = checkExprHasSubLink(newnode);
1400  return newnode;
1401  }
1402  /* otherwise fall through to copy the var normally */
1403  }
1404  else if (IsA(node, CurrentOfExpr))
1405  {
1406  CurrentOfExpr *cexpr = (CurrentOfExpr *) node;
1407 
1408  if (cexpr->cvarno == context->target_varno &&
1409  context->sublevels_up == 0)
1410  {
1411  /*
1412  * We get here if a WHERE CURRENT OF expression turns out to apply
1413  * to a view. Someday we might be able to translate the
1414  * expression to apply to an underlying table of the view, but
1415  * right now it's not implemented.
1416  */
1417  ereport(ERROR,
1418  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1419  errmsg("WHERE CURRENT OF on a view is not implemented")));
1420  }
1421  /* otherwise fall through to copy the expr normally */
1422  }
1423  else if (IsA(node, Query))
1424  {
1425  /* Recurse into RTE subquery or not-yet-planned sublink subquery */
1426  Query *newnode;
1427  bool save_inserted_sublink;
1428 
1429  context->sublevels_up++;
1430  save_inserted_sublink = context->inserted_sublink;
1431  context->inserted_sublink = ((Query *) node)->hasSubLinks;
1432  newnode = query_tree_mutator((Query *) node,
1434  context,
1435  0);
1436  newnode->hasSubLinks |= context->inserted_sublink;
1437  context->inserted_sublink = save_inserted_sublink;
1438  context->sublevels_up--;
1439  return (Node *) newnode;
1440  }
1442 }

References checkExprHasSubLink(), context, CurrentOfExpr::cvarno, ereport, errcode(), errmsg(), ERROR, expression_tree_mutator, IsA, query_tree_mutator, Var::varlevelsup, and Var::varno.

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

◆ 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 1759 of file rewriteManip.c.

1766 {
1768 
1769  context.target_rte = target_rte;
1770  context.targetlist = targetlist;
1771  context.nomatch_option = nomatch_option;
1772  context.nomatch_varno = nomatch_varno;
1773 
1774  return replace_rte_variables(node, target_varno, sublevels_up,
1776  &context,
1777  outer_hasSubLinks);
1778 }
Node * replace_rte_variables(Node *node, int target_varno, int sublevels_up, replace_rte_variables_callback callback, void *callback_arg, bool *outer_hasSubLinks)
static Node * ReplaceVarsFromTargetList_callback(Var *var, replace_rte_variables_context *context)

References context, replace_rte_variables(), and ReplaceVarsFromTargetList_callback().

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

◆ ReplaceVarsFromTargetList_callback()

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

Definition at line 1655 of file rewriteManip.c.

1657 {
1659  TargetEntry *tle;
1660 
1661  if (var->varattno == InvalidAttrNumber)
1662  {
1663  /* Must expand whole-tuple reference into RowExpr */
1664  RowExpr *rowexpr;
1665  List *colnames;
1666  List *fields;
1667 
1668  /*
1669  * If generating an expansion for a var of a named rowtype (ie, this
1670  * is a plain relation RTE), then we must include dummy items for
1671  * dropped columns. If the var is RECORD (ie, this is a JOIN), then
1672  * omit dropped columns. In the latter case, attach column names to
1673  * the RowExpr for use of the executor and ruleutils.c.
1674  */
1675  expandRTE(rcon->target_rte,
1676  var->varno, var->varlevelsup, var->location,
1677  (var->vartype != RECORDOID),
1678  &colnames, &fields);
1679  /* Adjust the generated per-field Vars... */
1680  fields = (List *) replace_rte_variables_mutator((Node *) fields,
1681  context);
1682  rowexpr = makeNode(RowExpr);
1683  rowexpr->args = fields;
1684  rowexpr->row_typeid = var->vartype;
1685  rowexpr->row_format = COERCE_IMPLICIT_CAST;
1686  rowexpr->colnames = (var->vartype == RECORDOID) ? colnames : NIL;
1687  rowexpr->location = var->location;
1688 
1689  return (Node *) rowexpr;
1690  }
1691 
1692  /* Normal case referencing one targetlist element */
1693  tle = get_tle_by_resno(rcon->targetlist, var->varattno);
1694 
1695  if (tle == NULL || tle->resjunk)
1696  {
1697  /* Failed to find column in targetlist */
1698  switch (rcon->nomatch_option)
1699  {
1701  /* fall through, throw error below */
1702  break;
1703 
1705  var = copyObject(var);
1706  var->varno = rcon->nomatch_varno;
1707  /* we leave the syntactic referent alone */
1708  return (Node *) var;
1709 
1711 
1712  /*
1713  * If Var is of domain type, we should add a CoerceToDomain
1714  * node, in case there is a NOT NULL domain constraint.
1715  */
1716  return coerce_to_domain((Node *) makeNullConst(var->vartype,
1717  var->vartypmod,
1718  var->varcollid),
1719  InvalidOid, -1,
1720  var->vartype,
1723  -1,
1724  false);
1725  }
1726  elog(ERROR, "could not find replacement targetlist entry for attno %d",
1727  var->varattno);
1728  return NULL; /* keep compiler quiet */
1729  }
1730  else
1731  {
1732  /* Make a copy of the tlist item to return */
1733  Expr *newnode = copyObject(tle->expr);
1734 
1735  /* Must adjust varlevelsup if tlist item is from higher query */
1736  if (var->varlevelsup > 0)
1737  IncrementVarSublevelsUp((Node *) newnode, var->varlevelsup, 0);
1738 
1739  /*
1740  * Check to see if the tlist item contains a PARAM_MULTIEXPR Param,
1741  * and throw error if so. This case could only happen when expanding
1742  * an ON UPDATE rule's NEW variable and the referenced tlist item in
1743  * the original UPDATE command is part of a multiple assignment. There
1744  * seems no practical way to handle such cases without multiple
1745  * evaluation of the multiple assignment's sub-select, which would
1746  * create semantic oddities that users of rules would probably prefer
1747  * not to cope with. So treat it as an unimplemented feature.
1748  */
1749  if (contains_multiexpr_param((Node *) newnode, NULL))
1750  ereport(ERROR,
1751  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1752  errmsg("NEW variables in ON UPDATE rules cannot reference columns that are part of a multiple assignment in the subject UPDATE command")));
1753 
1754  return (Node *) newnode;
1755  }
1756 }
#define InvalidAttrNumber
Definition: attnum.h:23
Const * makeNullConst(Oid consttype, int32 consttypmod, Oid constcollid)
Definition: makefuncs.c:339
Node * coerce_to_domain(Node *arg, Oid baseTypeId, int32 baseTypeMod, Oid typeId, CoercionContext ccontext, CoercionForm cformat, int location, bool hideInputCoercion)
Definition: parse_coerce.c:676
TargetEntry * get_tle_by_resno(List *tlist, AttrNumber resno)
void expandRTE(RangeTblEntry *rte, int rtindex, int sublevels_up, int location, bool include_dropped, List **colnames, List **colvars)
#define NIL
Definition: pg_list.h:68
#define InvalidOid
Definition: postgres_ext.h:36
@ COERCION_IMPLICIT
Definition: primnodes.h:714
void IncrementVarSublevelsUp(Node *node, int delta_sublevels_up, int min_sublevels_up)
Definition: rewriteManip.c:841
@ REPLACEVARS_SUBSTITUTE_NULL
Definition: rewriteManip.h:40
@ REPLACEVARS_CHANGE_VARNO
Definition: rewriteManip.h:39
@ REPLACEVARS_REPORT_ERROR
Definition: rewriteManip.h:38
Definition: pg_list.h:54
ReplaceVarsNoMatchOption nomatch_option
List * args
Definition: primnodes.h:1411
ParseLoc location
Definition: primnodes.h:1435
ParseLoc location
Definition: primnodes.h:293

References RowExpr::args, COERCE_IMPLICIT_CAST, coerce_to_domain(), COERCION_IMPLICIT, contains_multiexpr_param(), context, copyObject, elog, ereport, errcode(), errmsg(), ERROR, expandRTE(), get_tle_by_resno(), if(), IncrementVarSublevelsUp(), InvalidAttrNumber, InvalidOid, Var::location, RowExpr::location, makeNode, makeNullConst(), NIL, ReplaceVarsFromTargetList_context::nomatch_option, ReplaceVarsFromTargetList_context::nomatch_varno, replace_rte_variables_mutator(), REPLACEVARS_CHANGE_VARNO, REPLACEVARS_REPORT_ERROR, REPLACEVARS_SUBSTITUTE_NULL, ReplaceVarsFromTargetList_context::target_rte, ReplaceVarsFromTargetList_context::targetlist, Var::varattno, Var::varlevelsup, and Var::varno.

Referenced by ReplaceVarsFromTargetList().