PostgreSQL Source Code  git master
rewriteManip.h File Reference
#include "nodes/parsenodes.h"
Include dependency graph for rewriteManip.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  replace_rte_variables_context
 

Typedefs

typedef struct replace_rte_variables_context replace_rte_variables_context
 
typedef Node *(* replace_rte_variables_callback) (Var *var, replace_rte_variables_context *context)
 
typedef enum ReplaceVarsNoMatchOption ReplaceVarsNoMatchOption
 

Enumerations

enum  ReplaceVarsNoMatchOption { REPLACEVARS_REPORT_ERROR , REPLACEVARS_CHANGE_VARNO , REPLACEVARS_SUBSTITUTE_NULL }
 

Functions

void CombineRangeTables (List **dst_rtable, List **dst_perminfos, List *src_rtable, List *src_perminfos)
 
void OffsetVarNodes (Node *node, int offset, int sublevels_up)
 
void ChangeVarNodes (Node *node, int rt_index, int new_index, int sublevels_up)
 
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)
 
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)
 
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)
 
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)
 
Nodemap_variable_attnos (Node *node, int target_varno, int sublevels_up, const struct AttrMap *attno_map, Oid to_rowtype, bool *found_whole_row)
 
NodeReplaceVarsFromTargetList (Node *node, int target_varno, int sublevels_up, RangeTblEntry *target_rte, List *targetlist, ReplaceVarsNoMatchOption nomatch_option, int nomatch_varno, bool *outer_hasSubLinks)
 

Typedef Documentation

◆ replace_rte_variables_callback

typedef Node*(* replace_rte_variables_callback) (Var *var, replace_rte_variables_context *context)

Definition at line 24 of file rewriteManip.h.

◆ replace_rte_variables_context

◆ ReplaceVarsNoMatchOption

Enumeration Type Documentation

◆ ReplaceVarsNoMatchOption

Enumerator
REPLACEVARS_REPORT_ERROR 
REPLACEVARS_CHANGE_VARNO 
REPLACEVARS_SUBSTITUTE_NULL 

Definition at line 36 of file rewriteManip.h.

37 {
38  REPLACEVARS_REPORT_ERROR, /* throw error if no match */
39  REPLACEVARS_CHANGE_VARNO, /* change the Var's varno, nothing else */
40  REPLACEVARS_SUBSTITUTE_NULL, /* replace with a NULL Const */
ReplaceVarsNoMatchOption
Definition: rewriteManip.h:37
@ REPLACEVARS_SUBSTITUTE_NULL
Definition: rewriteManip.h:40
@ REPLACEVARS_CHANGE_VARNO
Definition: rewriteManip.h:39
@ REPLACEVARS_REPORT_ERROR
Definition: rewriteManip.h:38

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

◆ 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 }
#define makeNode(_type_)
Definition: nodes.h:155
@ 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
Definition: nodes.h:129

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
#define IsA(nodeptr, _type_)
Definition: nodes.h:158
#define copyObject(obj)
Definition: nodes.h:223
@ 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().

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

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

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

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

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

◆ map_variable_attnos()

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

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

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

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

◆ 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 }
#define expression_tree_mutator(n, m, c)
Definition: nodeFuncs.h:153
#define query_tree_mutator(q, m, c, f)
Definition: nodeFuncs.h:158
Definition: primnodes.h:234
int varno
Definition: primnodes.h:241
Index varlevelsup
Definition: primnodes.h:266

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