PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
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 OffsetVarNodes (Node *node, int offset, int sublevels_up)
 
void ChangeVarNodes (Node *node, int old_varno, int new_varno, 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)
 
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 AttrNumber *attno_map, int map_length, 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

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

Definition at line 22 of file rewriteManip.h.

Enumeration Type Documentation

Enumerator
REPLACEVARS_REPORT_ERROR 
REPLACEVARS_CHANGE_VARNO 
REPLACEVARS_SUBSTITUTE_NULL 

Definition at line 34 of file rewriteManip.h.

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

Function Documentation

void AddInvertedQual ( Query parsetree,
Node qual 
)

Definition at line 1044 of file rewriteManip.c.

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

Referenced by CopyAndAddInvertedQual().

1045 {
1046  BooleanTest *invqual;
1047 
1048  if (qual == NULL)
1049  return;
1050 
1051  /* Need not copy input qual, because AddQual will... */
1052  invqual = makeNode(BooleanTest);
1053  invqual->arg = (Expr *) qual;
1054  invqual->booltesttype = IS_NOT_TRUE;
1055  invqual->location = -1;
1056 
1057  AddQual(parsetree, (Node *) invqual);
1058 }
Definition: nodes.h:509
Expr * arg
Definition: primnodes.h:1203
BoolTestType booltesttype
Definition: primnodes.h:1204
#define makeNode(_type_)
Definition: nodes.h:557
void AddQual(Query *parsetree, Node *qual)
Definition: rewriteManip.c:976
void AddQual ( Query parsetree,
Node qual 
)

Definition at line 976 of file rewriteManip.c.

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

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

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

Definition at line 607 of file rewriteManip.c.

References ChangeVarNodes_walker(), OnConflictExpr::exclRelIndex, IsA, lfirst, ChangeVarNodes_context::new_index, Query::onConflict, query_tree_walker(), Query::resultRelation, Query::rowMarks, ChangeVarNodes_context::rt_index, RowMarkClause::rti, and ChangeVarNodes_context::sublevels_up.

Referenced by add_security_quals(), add_with_check_options(), ApplyRetrieveRule(), CopyAndAddInvertedQual(), get_relation_constraints(), get_relation_info(), inheritance_planner(), rewriteRuleAction(), rewriteTargetView(), and TriggerEnabled().

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

Definition at line 277 of file rewriteManip.c.

References checkExprHasSubLink_walker(), QTW_IGNORE_RC_SUBQUERIES, and query_or_expression_tree_walker().

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

278 {
279  /*
280  * If a Query is passed, examine it --- but we should not recurse into
281  * sub-Queries that are in its rangetable or CTE list.
282  */
285  NULL,
287 }
static bool checkExprHasSubLink_walker(Node *node, void *context)
Definition: rewriteManip.c:290
#define QTW_IGNORE_RC_SUBQUERIES
Definition: nodeFuncs.h:22
bool query_or_expression_tree_walker(Node *node, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:3178
bool contain_aggs_of_level ( Node node,
int  levelsup 
)

Definition at line 67 of file rewriteManip.c.

References contain_aggs_of_level_walker(), query_or_expression_tree_walker(), and contain_aggs_of_level_context::sublevels_up.

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

68 {
70 
71  context.sublevels_up = levelsup;
72 
73  /*
74  * Must be prepared to start with a Query or a bare expression tree; if
75  * it's a Query, we don't want to increment sublevels_up.
76  */
79  (void *) &context,
80  0);
81 }
bool query_or_expression_tree_walker(Node *node, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:3178
static bool contain_aggs_of_level_walker(Node *node, contain_aggs_of_level_context *context)
Definition: rewriteManip.c:84
bool contain_windowfuncs ( Node node)

Definition at line 197 of file rewriteManip.c.

References contain_windowfuncs_walker(), and query_or_expression_tree_walker().

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

198 {
199  /*
200  * Must be prepared to start with a Query or a bare expression tree; if
201  * it's a Query, we don't want to increment sublevels_up.
202  */
205  NULL,
206  0);
207 }
static bool contain_windowfuncs_walker(Node *node, void *context)
Definition: rewriteManip.c:210
bool query_or_expression_tree_walker(Node *node, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:3178
Query* getInsertSelectQuery ( Query parsetree,
Query ***  subquery_ptr 
)

Definition at line 921 of file rewriteManip.c.

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

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

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

Definition at line 773 of file rewriteManip.c.

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

775 {
777 
778  context.delta_sublevels_up = delta_sublevels_up;
779  context.min_sublevels_up = min_sublevels_up;
780 
781  /*
782  * Must be prepared to start with a Query or a bare expression tree; if
783  * it's a Query, we don't want to increment sublevels_up.
784  */
787  (void *) &context,
789 }
static bool IncrementVarSublevelsUp_walker(Node *node, IncrementVarSublevelsUp_context *context)
Definition: rewriteManip.c:700
#define QTW_EXAMINE_RTES
Definition: nodeFuncs.h:25
bool query_or_expression_tree_walker(Node *node, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:3178
void IncrementVarSublevelsUp_rtable ( List rtable,
int  delta_sublevels_up,
int  min_sublevels_up 
)

Definition at line 796 of file rewriteManip.c.

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

Referenced by pull_up_simple_union_all().

798 {
800 
801  context.delta_sublevels_up = delta_sublevels_up;
802  context.min_sublevels_up = min_sublevels_up;
803 
804  range_table_walker(rtable,
806  (void *) &context,
808 }
static bool IncrementVarSublevelsUp_walker(Node *node, IncrementVarSublevelsUp_context *context)
Definition: rewriteManip.c:700
#define QTW_EXAMINE_RTES
Definition: nodeFuncs.h:25
bool range_table_walker(List *rtable, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:2290
int locate_agg_of_level ( Node node,
int  levelsup 
)

Definition at line 131 of file rewriteManip.c.

References locate_agg_of_level_context::agg_location, locate_agg_of_level_walker(), query_or_expression_tree_walker(), and locate_agg_of_level_context::sublevels_up.

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

132 {
134 
135  context.agg_location = -1; /* in case we find nothing */
136  context.sublevels_up = levelsup;
137 
138  /*
139  * Must be prepared to start with a Query or a bare expression tree; if
140  * it's a Query, we don't want to increment sublevels_up.
141  */
144  (void *) &context,
145  0);
146 
147  return context.agg_location;
148 }
bool query_or_expression_tree_walker(Node *node, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:3178
static bool locate_agg_of_level_walker(Node *node, locate_agg_of_level_context *context)
Definition: rewriteManip.c:151
int locate_windowfunc ( Node node)

Definition at line 235 of file rewriteManip.c.

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

Referenced by checkTargetlistEntrySQL92(), and transformWindowFuncCall().

236 {
238 
239  context.win_location = -1; /* in case we find nothing */
240 
241  /*
242  * Must be prepared to start with a Query or a bare expression tree; if
243  * it's a Query, we don't want to increment sublevels_up.
244  */
247  (void *) &context,
248  0);
249 
250  return context.win_location;
251 }
bool query_or_expression_tree_walker(Node *node, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:3178
static bool locate_windowfunc_walker(Node *node, locate_windowfunc_context *context)
Definition: rewriteManip.c:254
Node* map_variable_attnos ( Node node,
int  target_varno,
int  sublevels_up,
const AttrNumber attno_map,
int  map_length,
Oid  to_rowtype,
bool found_whole_row 
)

Definition at line 1311 of file rewriteManip.c.

References map_variable_attnos_context::attno_map, map_variable_attnos_context::found_whole_row, map_variable_attnos_context::map_length, 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(), generateClonedIndexStmt(), map_partition_varattnos(), MergeAttributes(), and transformTableLikeClause().

1315 {
1317 
1318  context.target_varno = target_varno;
1319  context.sublevels_up = sublevels_up;
1320  context.attno_map = attno_map;
1321  context.map_length = map_length;
1322  context.to_rowtype = to_rowtype;
1323  context.found_whole_row = found_whole_row;
1324 
1325  *found_whole_row = false;
1326 
1327  /*
1328  * Must be prepared to start with a Query or a bare expression tree; if
1329  * it's a Query, we don't want to increment sublevels_up.
1330  */
1333  (void *) &context,
1334  0);
1335 }
static Node * map_variable_attnos_mutator(Node *node, map_variable_attnos_context *context)
Node * query_or_expression_tree_mutator(Node *node, Node *(*mutator)(), void *context, int flags)
Definition: nodeFuncs.c:3201
const AttrNumber * attno_map
void OffsetVarNodes ( Node node,
int  offset,
int  sublevels_up 
)

Definition at line 424 of file rewriteManip.c.

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

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

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

Definition at line 889 of file rewriteManip.c.

References query_or_expression_tree_walker(), rangeTableEntry_used_walker(), rangeTableEntry_used_context::rt_index, and rangeTableEntry_used_context::sublevels_up.

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

890 {
892 
893  context.rt_index = rt_index;
894  context.sublevels_up = sublevels_up;
895 
896  /*
897  * Must be prepared to start with a Query or a bare expression tree; if
898  * it's a Query, we don't want to increment sublevels_up.
899  */
902  (void *) &context,
903  0);
904 }
bool query_or_expression_tree_walker(Node *node, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:3178
static bool rangeTableEntry_used_walker(Node *node, rangeTableEntry_used_context *context)
Definition: rewriteManip.c:823
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 1084 of file rewriteManip.c.

References replace_rte_variables_context::callback, callback(), replace_rte_variables_context::callback_arg, elog, ERROR, replace_rte_variables_context::inserted_sublink, IsA, query_or_expression_tree_mutator(), replace_rte_variables_mutator(), result, replace_rte_variables_context::sublevels_up, and replace_rte_variables_context::target_varno.

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

1088 {
1089  Node *result;
1091 
1092  context.callback = callback;
1093  context.callback_arg = callback_arg;
1094  context.target_varno = target_varno;
1095  context.sublevels_up = sublevels_up;
1096 
1097  /*
1098  * We try to initialize inserted_sublink to true if there is no need to
1099  * detect new sublinks because the query already has some.
1100  */
1101  if (node && IsA(node, Query))
1102  context.inserted_sublink = ((Query *) node)->hasSubLinks;
1103  else if (outer_hasSubLinks)
1104  context.inserted_sublink = *outer_hasSubLinks;
1105  else
1106  context.inserted_sublink = false;
1107 
1108  /*
1109  * Must be prepared to start with a Query or a bare expression tree; if
1110  * it's a Query, we don't want to increment sublevels_up.
1111  */
1112  result = query_or_expression_tree_mutator(node,
1114  (void *) &context,
1115  0);
1116 
1117  if (context.inserted_sublink)
1118  {
1119  if (result && IsA(result, Query))
1120  ((Query *) result)->hasSubLinks = true;
1121  else if (outer_hasSubLinks)
1122  *outer_hasSubLinks = true;
1123  else
1124  elog(ERROR, "replace_rte_variables inserted a SubLink, but has noplace to record it");
1125  }
1126 
1127  return result;
1128 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
Definition: nodes.h:509
return result
Definition: formatting.c:1633
replace_rte_variables_callback callback
Definition: rewriteManip.h:27
#define ERROR
Definition: elog.h:43
Node * replace_rte_variables_mutator(Node *node, replace_rte_variables_context *context)
static void callback(struct sockaddr *addr, struct sockaddr *mask, void *unused)
Definition: test_ifaddrs.c:48
Node * query_or_expression_tree_mutator(Node *node, Node *(*mutator)(), void *context, int flags)
Definition: nodeFuncs.c:3201
#define elog
Definition: elog.h:219
Node* replace_rte_variables_mutator ( Node node,
replace_rte_variables_context context 
)

Definition at line 1131 of file rewriteManip.c.

References replace_rte_variables_context::callback, checkExprHasSubLink(), CurrentOfExpr::cvarno, ereport, errcode(), errmsg(), ERROR, expression_tree_mutator(), Query::hasSubLinks, replace_rte_variables_context::inserted_sublink, IsA, query_tree_mutator(), replace_rte_variables_mutator(), replace_rte_variables_context::sublevels_up, replace_rte_variables_context::target_varno, Var::varlevelsup, and Var::varno.

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

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

References ReplaceVarsFromTargetList_context::nomatch_option, ReplaceVarsFromTargetList_context::nomatch_varno, replace_rte_variables(), ReplaceVarsFromTargetList_callback(), ReplaceVarsFromTargetList_context::target_rte, and ReplaceVarsFromTargetList_context::targetlist.

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

1477 {
1479 
1480  context.target_rte = target_rte;
1481  context.targetlist = targetlist;
1482  context.nomatch_option = nomatch_option;
1483  context.nomatch_varno = nomatch_varno;
1484 
1485  return replace_rte_variables(node, target_varno, sublevels_up,
1487  (void *) &context,
1488  outer_hasSubLinks);
1489 }
static Node * ReplaceVarsFromTargetList_callback(Var *var, replace_rte_variables_context *context)
Node * replace_rte_variables(Node *node, int target_varno, int sublevels_up, replace_rte_variables_callback callback, void *callback_arg, bool *outer_hasSubLinks)
ReplaceVarsNoMatchOption nomatch_option