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

1150 {
1152 
1153  context.target_relids = target_relids;
1154  context.added_relids = added_relids;
1155  context.sublevels_up = 0;
1158  &context,
1159  0);
1160 }
#define query_or_expression_tree_mutator(n, m, c, f)
Definition: nodeFuncs.h:171
static Node * add_nulling_relids_mutator(Node *node, add_nulling_relids_context *context)
const Bitmapset * target_relids
Definition: rewriteManip.c:45
const Bitmapset * added_relids
Definition: rewriteManip.c:46

References add_nulling_relids_mutator(), add_nulling_relids_context::added_relids, query_or_expression_tree_mutator, add_nulling_relids_context::sublevels_up, and add_nulling_relids_context::target_relids.

Referenced by deconstruct_distribute_oj_quals(), 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 1163 of file rewriteManip.c.

1165 {
1166  if (node == NULL)
1167  return NULL;
1168  if (IsA(node, Var))
1169  {
1170  Var *var = (Var *) node;
1171 
1172  if (var->varlevelsup == context->sublevels_up &&
1173  bms_is_member(var->varno, context->target_relids))
1174  {
1175  Relids newnullingrels = bms_union(var->varnullingrels,
1176  context->added_relids);
1177 
1178  /* Copy the Var ... */
1179  var = copyObject(var);
1180  /* ... and replace the copy's varnullingrels field */
1181  var->varnullingrels = newnullingrels;
1182  return (Node *) var;
1183  }
1184  /* Otherwise fall through to copy the Var normally */
1185  }
1186  else if (IsA(node, PlaceHolderVar))
1187  {
1188  PlaceHolderVar *phv = (PlaceHolderVar *) node;
1189 
1190  if (phv->phlevelsup == context->sublevels_up &&
1191  bms_overlap(phv->phrels, context->target_relids))
1192  {
1193  Relids newnullingrels = bms_union(phv->phnullingrels,
1194  context->added_relids);
1195 
1196  /*
1197  * We don't modify the contents of the PHV's expression, only add
1198  * to phnullingrels. This corresponds to assuming that the PHV
1199  * will be evaluated at the same level as before, then perhaps be
1200  * nulled as it bubbles up. Hence, just flat-copy the node ...
1201  */
1202  phv = makeNode(PlaceHolderVar);
1203  memcpy(phv, node, sizeof(PlaceHolderVar));
1204  /* ... and replace the copy's phnullingrels field */
1205  phv->phnullingrels = newnullingrels;
1206  return (Node *) phv;
1207  }
1208  /* Otherwise fall through to copy the PlaceHolderVar normally */
1209  }
1210  else if (IsA(node, Query))
1211  {
1212  /* Recurse into RTE or sublink subquery */
1213  Query *newnode;
1214 
1215  context->sublevels_up++;
1216  newnode = query_tree_mutator((Query *) node,
1218  (void *) context,
1219  0);
1220  context->sublevels_up--;
1221  return (Node *) newnode;
1222  }
1224  (void *) context);
1225 }
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:153
#define query_tree_mutator(q, m, c, f)
Definition: nodeFuncs.h:158
#define IsA(nodeptr, _type_)
Definition: nodes.h:158
#define copyObject(obj)
Definition: nodes.h:223
#define makeNode(_type_)
Definition: nodes.h:155
Definition: nodes.h:129
Relids phnullingrels
Definition: pathnodes.h:2768
Index phlevelsup
Definition: pathnodes.h:2774
Definition: primnodes.h:234
int varno
Definition: primnodes.h:241
Index varlevelsup
Definition: primnodes.h:266

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

Referenced by add_nulling_relids().

◆ AddInvertedQual()

void AddInvertedQual ( Query parsetree,
Node qual 
)

Definition at line 1124 of file rewriteManip.c.

1125 {
1126  BooleanTest *invqual;
1127 
1128  if (qual == NULL)
1129  return;
1130 
1131  /* Need not copy input qual, because AddQual will... */
1132  invqual = makeNode(BooleanTest);
1133  invqual->arg = (Expr *) qual;
1134  invqual->booltesttype = IS_NOT_TRUE;
1135  invqual->location = -1;
1136 
1137  AddQual(parsetree, (Node *) invqual);
1138 }
@ IS_NOT_TRUE
Definition: primnodes.h:1739
void AddQual(Query *parsetree, Node *qual)
BoolTestType booltesttype
Definition: primnodes.h:1746
Expr * arg
Definition: primnodes.h:1745

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

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

738 {
739  if (!IS_SPECIAL_VARNO(oldrelid) && bms_is_member(oldrelid, relids))
740  {
741  /* Ensure we have a modifiable copy */
742  relids = bms_copy(relids);
743  /* Remove old, add new */
744  relids = bms_del_member(relids, oldrelid);
745  relids = bms_add_member(relids, newrelid);
746  }
747  return relids;
748 }
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:227

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

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

References ChangeVarNodes_walker(), OnConflictExpr::exclRelIndex, IsA, lfirst, ChangeVarNodes_context::new_index, Query::onConflict, query_tree_walker, 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(), get_relation_statistics(), 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 564 of file rewriteManip.c.

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

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

Referenced by ChangeVarNodes().

◆ checkExprHasSubLink()

bool checkExprHasSubLink ( Node node)

Definition at line 295 of file rewriteManip.c.

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

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

◆ checkExprHasSubLink_walker()

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

Definition at line 308 of file rewriteManip.c.

309 {
310  if (node == NULL)
311  return false;
312  if (IsA(node, SubLink))
313  return true; /* abort the tree traversal and return true */
314  return expression_tree_walker(node, checkExprHasSubLink_walker, context);
315 }

References 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 350 of file rewriteManip.c.

352 {
353  ListCell *l;
354  int offset = list_length(*dst_perminfos);
355 
356  if (offset > 0)
357  {
358  foreach(l, src_rtable)
359  {
361 
362  if (rte->perminfoindex > 0)
363  rte->perminfoindex += offset;
364  }
365  }
366 
367  *dst_perminfos = list_concat(*dst_perminfos, src_perminfos);
368  *dst_rtable = list_concat(*dst_rtable, src_rtable);
369 }
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
Index perminfoindex
Definition: parsenodes.h:1080

References lfirst_node, list_concat(), list_length(), and RangeTblEntry::perminfoindex.

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  (void *) &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(), query_or_expression_tree_walker, and contain_aggs_of_level_context::sublevels_up.

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  (void *) context, 0);
128  context->sublevels_up--;
129  return result;
130  }
132  (void *) context);
133 }

References expression_tree_walker, IsA, query_tree_walker, and contain_aggs_of_level_context::sublevels_up.

Referenced by contain_aggs_of_level().

◆ contain_windowfuncs()

bool contain_windowfuncs ( Node node)

Definition at line 215 of file rewriteManip.c.

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

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

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

References 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 324 of file rewriteManip.c.

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

References expression_tree_walker, IsA, and PARAM_MULTIEXPR.

Referenced by ReplaceVarsFromTargetList_callback().

◆ getInsertSelectQuery()

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

Definition at line 998 of file rewriteManip.c.

999 {
1000  Query *selectquery;
1001  RangeTblEntry *selectrte;
1002  RangeTblRef *rtr;
1003 
1004  if (subquery_ptr)
1005  *subquery_ptr = NULL;
1006 
1007  if (parsetree == NULL)
1008  return parsetree;
1009  if (parsetree->commandType != CMD_INSERT)
1010  return parsetree;
1011 
1012  /*
1013  * Currently, this is ONLY applied to rule-action queries, and so we
1014  * expect to find the OLD and NEW placeholder entries in the given query.
1015  * If they're not there, it must be an INSERT/SELECT in which they've been
1016  * pushed down to the SELECT.
1017  */
1018  if (list_length(parsetree->rtable) >= 2 &&
1019  strcmp(rt_fetch(PRS2_OLD_VARNO, parsetree->rtable)->eref->aliasname,
1020  "old") == 0 &&
1021  strcmp(rt_fetch(PRS2_NEW_VARNO, parsetree->rtable)->eref->aliasname,
1022  "new") == 0)
1023  return parsetree;
1024  Assert(parsetree->jointree && IsA(parsetree->jointree, FromExpr));
1025  if (list_length(parsetree->jointree->fromlist) != 1)
1026  elog(ERROR, "expected to find SELECT subquery");
1027  rtr = (RangeTblRef *) linitial(parsetree->jointree->fromlist);
1028  if (!IsA(rtr, RangeTblRef))
1029  elog(ERROR, "expected to find SELECT subquery");
1030  selectrte = rt_fetch(rtr->rtindex, parsetree->rtable);
1031  if (!(selectrte->rtekind == RTE_SUBQUERY &&
1032  selectrte->subquery &&
1033  IsA(selectrte->subquery, Query) &&
1034  selectrte->subquery->commandType == CMD_SELECT))
1035  elog(ERROR, "expected to find SELECT subquery");
1036  selectquery = selectrte->subquery;
1037  if (list_length(selectquery->rtable) >= 2 &&
1038  strcmp(rt_fetch(PRS2_OLD_VARNO, selectquery->rtable)->eref->aliasname,
1039  "old") == 0 &&
1040  strcmp(rt_fetch(PRS2_NEW_VARNO, selectquery->rtable)->eref->aliasname,
1041  "new") == 0)
1042  {
1043  if (subquery_ptr)
1044  *subquery_ptr = &(selectrte->subquery);
1045  return selectquery;
1046  }
1047  elog(ERROR, "could not find rule placeholders");
1048  return NULL; /* not reached */
1049 }
#define elog(elevel,...)
Definition: elog.h:224
@ CMD_INSERT
Definition: nodes.h:257
@ CMD_SELECT
Definition: nodes.h:255
@ RTE_SUBQUERY
Definition: parsenodes.h:1012
#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:230
#define PRS2_NEW_VARNO
Definition: primnodes.h:231
List * fromlist
Definition: primnodes.h:2061
List * rtable
Definition: parsenodes.h:168
Query * subquery
Definition: parsenodes.h:1086
RTEKind rtekind
Definition: parsenodes.h:1030

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

851 {
853 
854  context.delta_sublevels_up = delta_sublevels_up;
855  context.min_sublevels_up = min_sublevels_up;
856 
857  /*
858  * Must be prepared to start with a Query or a bare expression tree; if
859  * it's a Query, we don't want to increment sublevels_up.
860  */
863  (void *) &context,
865 }
#define QTW_EXAMINE_RTES_BEFORE
Definition: nodeFuncs.h:27
static bool IncrementVarSublevelsUp_walker(Node *node, IncrementVarSublevelsUp_context *context)
Definition: rewriteManip.c:776

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(), 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 872 of file rewriteManip.c.

874 {
876 
877  context.delta_sublevels_up = delta_sublevels_up;
878  context.min_sublevels_up = min_sublevels_up;
879 
880  range_table_walker(rtable,
882  (void *) &context,
884 }
#define range_table_walker(rt, w, c, f)
Definition: nodeFuncs.h:161

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

◆ IncrementVarSublevelsUp_walker()

static bool IncrementVarSublevelsUp_walker ( Node node,
IncrementVarSublevelsUp_context context 
)
static

Definition at line 776 of file rewriteManip.c.

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

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

◆ 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  (void *) &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 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().

◆ 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  (void *) context, 0);
202  context->sublevels_up--;
203  return result;
204  }
206  (void *) context);
207 }

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

◆ locate_windowfunc()

int locate_windowfunc ( Node node)

Definition at line 253 of file rewriteManip.c.

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

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

Referenced by checkTargetlistEntrySQL92(), and transformWindowFuncCall().

◆ locate_windowfunc_walker()

static bool locate_windowfunc_walker ( Node node,
locate_windowfunc_context context 
)
static

Definition at line 272 of file rewriteManip.c.

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

References expression_tree_walker, IsA, and locate_windowfunc_context::win_location.

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

1615 {
1617 
1618  context.target_varno = target_varno;
1619  context.sublevels_up = sublevels_up;
1620  context.attno_map = attno_map;
1621  context.to_rowtype = to_rowtype;
1622  context.found_whole_row = found_whole_row;
1623 
1624  *found_whole_row = false;
1625 
1626  /*
1627  * Must be prepared to start with a Query or a bare expression tree; if
1628  * it's a Query, we don't want to increment sublevels_up.
1629  */
1632  (void *) &context,
1633  0);
1634 }
static Node * map_variable_attnos_mutator(Node *node, map_variable_attnos_context *context)

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(), expandTableLikeClause(), 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 1488 of file rewriteManip.c.

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

References ConvertRowtypeExpr::arg, Assert(), map_variable_attnos_context::attno_map, AttrMap::attnums, COERCE_IMPLICIT_CAST, elog, ERROR, expression_tree_mutator, map_variable_attnos_context::found_whole_row, if(), 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::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 532 of file rewriteManip.c.

533 {
534  Relids result = NULL;
535  int rtindex;
536 
537  rtindex = -1;
538  while ((rtindex = bms_next_member(relids, rtindex)) >= 0)
539  result = bms_add_member(result, rtindex + offset);
540  return result;
541 }
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 480 of file rewriteManip.c.

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

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

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

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

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

Referenced by OffsetVarNodes().

◆ rangeTableEntry_used()

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

Definition at line 966 of file rewriteManip.c.

967 {
969 
970  context.rt_index = rt_index;
971  context.sublevels_up = sublevels_up;
972 
973  /*
974  * Must be prepared to start with a Query or a bare expression tree; if
975  * it's a Query, we don't want to increment sublevels_up.
976  */
979  (void *) &context,
980  0);
981 }
static bool rangeTableEntry_used_walker(Node *node, rangeTableEntry_used_context *context)
Definition: rewriteManip.c:899

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

◆ rangeTableEntry_used_walker()

static bool rangeTableEntry_used_walker ( Node node,
rangeTableEntry_used_context context 
)
static

Definition at line 899 of file rewriteManip.c.

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

References Assert(), bms_is_member(), CurrentOfExpr::cvarno, expression_tree_walker, IsA, j, query_tree_walker, rangeTableEntry_used_context::rt_index, RangeTblRef::rtindex, rangeTableEntry_used_context::sublevels_up, 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 1234 of file rewriteManip.c.

1237 {
1239 
1240  context.removable_relids = removable_relids;
1241  context.except_relids = except_relids;
1242  context.sublevels_up = 0;
1245  &context,
1246  0);
1247 }
static Node * remove_nulling_relids_mutator(Node *node, remove_nulling_relids_context *context)
const Bitmapset * removable_relids
Definition: rewriteManip.c:52
const Bitmapset * except_relids
Definition: rewriteManip.c:53

References remove_nulling_relids_context::except_relids, query_or_expression_tree_mutator, remove_nulling_relids_context::removable_relids, remove_nulling_relids_mutator(), and remove_nulling_relids_context::sublevels_up.

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

◆ remove_nulling_relids_mutator()

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

Definition at line 1250 of file rewriteManip.c.

1252 {
1253  if (node == NULL)
1254  return NULL;
1255  if (IsA(node, Var))
1256  {
1257  Var *var = (Var *) node;
1258 
1259  if (var->varlevelsup == context->sublevels_up &&
1260  !bms_is_member(var->varno, context->except_relids) &&
1261  bms_overlap(var->varnullingrels, context->removable_relids))
1262  {
1263  /* Copy the Var ... */
1264  var = copyObject(var);
1265  /* ... and replace the copy's varnullingrels field */
1266  var->varnullingrels = bms_difference(var->varnullingrels,
1267  context->removable_relids);
1268  return (Node *) var;
1269  }
1270  /* Otherwise fall through to copy the Var normally */
1271  }
1272  else if (IsA(node, PlaceHolderVar))
1273  {
1274  PlaceHolderVar *phv = (PlaceHolderVar *) node;
1275 
1276  if (phv->phlevelsup == context->sublevels_up &&
1277  !bms_overlap(phv->phrels, context->except_relids))
1278  {
1279  /*
1280  * Note: it might seem desirable to remove the PHV altogether if
1281  * phnullingrels goes to empty. Currently we dare not do that
1282  * because we use PHVs in some cases to enforce separate identity
1283  * of subexpressions; see wrap_non_vars usages in prepjointree.c.
1284  */
1285  /* Copy the PlaceHolderVar and mutate what's below ... */
1286  phv = (PlaceHolderVar *)
1289  (void *) context);
1290  /* ... and replace the copy's phnullingrels field */
1292  context->removable_relids);
1293  /* We must also update phrels, if it contains a removable RTI */
1294  phv->phrels = bms_difference(phv->phrels,
1295  context->removable_relids);
1296  Assert(!bms_is_empty(phv->phrels));
1297  return (Node *) phv;
1298  }
1299  /* Otherwise fall through to copy the PlaceHolderVar normally */
1300  }
1301  else if (IsA(node, Query))
1302  {
1303  /* Recurse into RTE or sublink subquery */
1304  Query *newnode;
1305 
1306  context->sublevels_up++;
1307  newnode = query_tree_mutator((Query *) node,
1309  (void *) context,
1310  0);
1311  context->sublevels_up--;
1312  return (Node *) newnode;
1313  }
1315  (void *) context);
1316 }
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(), copyObject, remove_nulling_relids_context::except_relids, expression_tree_mutator, IsA, PlaceHolderVar::phlevelsup, PlaceHolderVar::phnullingrels, query_tree_mutator, remove_nulling_relids_context::removable_relids, remove_nulling_relids_context::sublevels_up, 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 1342 of file rewriteManip.c.

1346 {
1347  Node *result;
1349 
1350  context.callback = callback;
1351  context.callback_arg = callback_arg;
1352  context.target_varno = target_varno;
1353  context.sublevels_up = sublevels_up;
1354 
1355  /*
1356  * We try to initialize inserted_sublink to true if there is no need to
1357  * detect new sublinks because the query already has some.
1358  */
1359  if (node && IsA(node, Query))
1360  context.inserted_sublink = ((Query *) node)->hasSubLinks;
1361  else if (outer_hasSubLinks)
1362  context.inserted_sublink = *outer_hasSubLinks;
1363  else
1364  context.inserted_sublink = false;
1365 
1366  /*
1367  * Must be prepared to start with a Query or a bare expression tree; if
1368  * it's a Query, we don't want to increment sublevels_up.
1369  */
1370  result = query_or_expression_tree_mutator(node,
1372  (void *) &context,
1373  0);
1374 
1375  if (context.inserted_sublink)
1376  {
1377  if (result && IsA(result, Query))
1378  ((Query *) result)->hasSubLinks = true;
1379  else if (outer_hasSubLinks)
1380  *outer_hasSubLinks = true;
1381  else
1382  elog(ERROR, "replace_rte_variables inserted a SubLink, but has noplace to record it");
1383  }
1384 
1385  return result;
1386 }
Node * replace_rte_variables_mutator(Node *node, replace_rte_variables_context *context)
replace_rte_variables_callback callback
Definition: rewriteManip.h:29
static void callback(struct sockaddr *addr, struct sockaddr *mask, void *unused)
Definition: test_ifaddrs.c:46

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

◆ replace_rte_variables_mutator()

Node* replace_rte_variables_mutator ( Node node,
replace_rte_variables_context context 
)

Definition at line 1389 of file rewriteManip.c.

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

References replace_rte_variables_context::callback, checkExprHasSubLink(), CurrentOfExpr::cvarno, ereport, errcode(), errmsg(), ERROR, expression_tree_mutator, replace_rte_variables_context::inserted_sublink, IsA, query_tree_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(), 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 1769 of file rewriteManip.c.

1776 {
1778 
1779  context.target_rte = target_rte;
1780  context.targetlist = targetlist;
1781  context.nomatch_option = nomatch_option;
1782  context.nomatch_varno = nomatch_varno;
1783 
1784  return replace_rte_variables(node, target_varno, sublevels_up,
1786  (void *) &context,
1787  outer_hasSubLinks);
1788 }
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)
ReplaceVarsNoMatchOption nomatch_option

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

◆ ReplaceVarsFromTargetList_callback()

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

Definition at line 1665 of file rewriteManip.c.

1667 {
1669  TargetEntry *tle;
1670 
1671  if (var->varattno == InvalidAttrNumber)
1672  {
1673  /* Must expand whole-tuple reference into RowExpr */
1674  RowExpr *rowexpr;
1675  List *colnames;
1676  List *fields;
1677 
1678  /*
1679  * If generating an expansion for a var of a named rowtype (ie, this
1680  * is a plain relation RTE), then we must include dummy items for
1681  * dropped columns. If the var is RECORD (ie, this is a JOIN), then
1682  * omit dropped columns. In the latter case, attach column names to
1683  * the RowExpr for use of the executor and ruleutils.c.
1684  */
1685  expandRTE(rcon->target_rte,
1686  var->varno, var->varlevelsup, var->location,
1687  (var->vartype != RECORDOID),
1688  &colnames, &fields);
1689  /* Adjust the generated per-field Vars... */
1690  fields = (List *) replace_rte_variables_mutator((Node *) fields,
1691  context);
1692  rowexpr = makeNode(RowExpr);
1693  rowexpr->args = fields;
1694  rowexpr->row_typeid = var->vartype;
1695  rowexpr->row_format = COERCE_IMPLICIT_CAST;
1696  rowexpr->colnames = (var->vartype == RECORDOID) ? colnames : NIL;
1697  rowexpr->location = var->location;
1698 
1699  return (Node *) rowexpr;
1700  }
1701 
1702  /* Normal case referencing one targetlist element */
1703  tle = get_tle_by_resno(rcon->targetlist, var->varattno);
1704 
1705  if (tle == NULL || tle->resjunk)
1706  {
1707  /* Failed to find column in targetlist */
1708  switch (rcon->nomatch_option)
1709  {
1711  /* fall through, throw error below */
1712  break;
1713 
1715  var = (Var *) copyObject(var);
1716  var->varno = rcon->nomatch_varno;
1717  /* we leave the syntactic referent alone */
1718  return (Node *) var;
1719 
1721 
1722  /*
1723  * If Var is of domain type, we should add a CoerceToDomain
1724  * node, in case there is a NOT NULL domain constraint.
1725  */
1726  return coerce_to_domain((Node *) makeNullConst(var->vartype,
1727  var->vartypmod,
1728  var->varcollid),
1729  InvalidOid, -1,
1730  var->vartype,
1733  -1,
1734  false);
1735  }
1736  elog(ERROR, "could not find replacement targetlist entry for attno %d",
1737  var->varattno);
1738  return NULL; /* keep compiler quiet */
1739  }
1740  else
1741  {
1742  /* Make a copy of the tlist item to return */
1743  Expr *newnode = copyObject(tle->expr);
1744 
1745  /* Must adjust varlevelsup if tlist item is from higher query */
1746  if (var->varlevelsup > 0)
1747  IncrementVarSublevelsUp((Node *) newnode, var->varlevelsup, 0);
1748 
1749  /*
1750  * Check to see if the tlist item contains a PARAM_MULTIEXPR Param,
1751  * and throw error if so. This case could only happen when expanding
1752  * an ON UPDATE rule's NEW variable and the referenced tlist item in
1753  * the original UPDATE command is part of a multiple assignment. There
1754  * seems no practical way to handle such cases without multiple
1755  * evaluation of the multiple assignment's sub-select, which would
1756  * create semantic oddities that users of rules would probably prefer
1757  * not to cope with. So treat it as an unimplemented feature.
1758  */
1759  if (contains_multiexpr_param((Node *) newnode, NULL))
1760  ereport(ERROR,
1761  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1762  errmsg("NEW variables in ON UPDATE rules cannot reference columns that are part of a multiple assignment in the subject UPDATE command")));
1763 
1764  return (Node *) newnode;
1765  }
1766 }
#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:670
void IncrementVarSublevelsUp(Node *node, int delta_sublevels_up, int min_sublevels_up)
Definition: rewriteManip.c:849
@ 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
int location
Definition: primnodes.h:1391
List * args
Definition: primnodes.h:1367
int location
Definition: primnodes.h:279

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