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

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

References add_nulling_relids_mutator(), context, and query_or_expression_tree_mutator.

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

◆ AddInvertedQual()

void AddInvertedQual ( Query parsetree,
Node qual 
)

Definition at line 1115 of file rewriteManip.c.

1116 {
1117  BooleanTest *invqual;
1118 
1119  if (qual == NULL)
1120  return;
1121 
1122  /* Need not copy input qual, because AddQual will... */
1123  invqual = makeNode(BooleanTest);
1124  invqual->arg = (Expr *) qual;
1125  invqual->booltesttype = IS_NOT_TRUE;
1126  invqual->location = -1;
1127 
1128  AddQual(parsetree, (Node *) invqual);
1129 }
#define makeNode(_type_)
Definition: nodes.h:155
@ IS_NOT_TRUE
Definition: primnodes.h:1976
void AddQual(Query *parsetree, Node *qual)
ParseLoc location
Definition: primnodes.h:1984
BoolTestType booltesttype
Definition: primnodes.h:1983
Expr * arg
Definition: primnodes.h:1982
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 1047 of file rewriteManip.c.

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

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

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

◆ ChangeVarNodes()

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

Definition at line 668 of file rewriteManip.c.

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

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

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

◆ checkExprHasSubLink()

bool checkExprHasSubLink ( Node node)

Definition at line 292 of file rewriteManip.c.

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

References checkExprHasSubLink_walker(), QTW_IGNORE_RC_SUBQUERIES, and query_or_expression_tree_walker.

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

◆ CombineRangeTables()

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

Definition at line 347 of file rewriteManip.c.

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

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

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

◆ contain_aggs_of_level()

bool contain_aggs_of_level ( Node node,
int  levelsup 
)

Definition at line 85 of file rewriteManip.c.

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

References contain_aggs_of_level_walker(), context, and query_or_expression_tree_walker.

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

◆ contain_windowfuncs()

bool contain_windowfuncs ( Node node)

Definition at line 214 of file rewriteManip.c.

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

References contain_windowfuncs_walker(), and query_or_expression_tree_walker.

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

◆ getInsertSelectQuery()

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

Definition at line 989 of file rewriteManip.c.

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

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

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

◆ IncrementVarSublevelsUp()

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

Definition at line 841 of file rewriteManip.c.

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

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

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

◆ IncrementVarSublevelsUp_rtable()

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

Definition at line 864 of file rewriteManip.c.

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

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

Referenced by pull_up_simple_union_all().

◆ locate_agg_of_level()

int locate_agg_of_level ( Node node,
int  levelsup 
)

Definition at line 149 of file rewriteManip.c.

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

References context, locate_agg_of_level_walker(), and query_or_expression_tree_walker.

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

◆ locate_windowfunc()

int locate_windowfunc ( Node node)

Definition at line 251 of file rewriteManip.c.

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

References context, locate_windowfunc_walker(), and query_or_expression_tree_walker.

Referenced by checkTargetlistEntrySQL92(), and transformWindowFuncCall().

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

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

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

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

◆ rangeTableEntry_used()

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

Definition at line 957 of file rewriteManip.c.

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

References context, query_or_expression_tree_walker, and rangeTableEntry_used_walker().

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

◆ remove_nulling_relids()

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

Definition at line 1227 of file rewriteManip.c.

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

References context, query_or_expression_tree_mutator, and remove_nulling_relids_mutator().

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

◆ replace_rte_variables()

Node* replace_rte_variables ( Node node,
int  target_varno,
int  sublevels_up,
replace_rte_variables_callback  callback,
void *  callback_arg,
bool *  outer_hasSubLinks 
)

Definition at line 1334 of file rewriteManip.c.

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

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

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

◆ replace_rte_variables_mutator()

Node* replace_rte_variables_mutator ( Node node,
replace_rte_variables_context context 
)

Definition at line 1381 of file rewriteManip.c.

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

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

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

◆ ReplaceVarsFromTargetList()

Node* ReplaceVarsFromTargetList ( Node node,
int  target_varno,
int  sublevels_up,
RangeTblEntry target_rte,
List targetlist,
ReplaceVarsNoMatchOption  nomatch_option,
int  nomatch_varno,
bool *  outer_hasSubLinks 
)

Definition at line 1759 of file rewriteManip.c.

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

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

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