PostgreSQL Source Code git master
Loading...
Searching...
No Matches
rewriteManip.h File Reference
#include "nodes/parsenodes.h"
#include "nodes/pathnodes.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
 
struct  ChangeVarNodes_context
 

Typedefs

typedef struct AttrMap AttrMap
 
typedef struct replace_rte_variables_context replace_rte_variables_context
 
typedef Node *(* replace_rte_variables_callback) (const Var *var, replace_rte_variables_context *context)
 
typedef enum ReplaceVarsNoMatchOption ReplaceVarsNoMatchOption
 
typedef struct ChangeVarNodes_context ChangeVarNodes_context
 
typedef bool(* ChangeVarNodes_callback) (Node *node, ChangeVarNodes_context *arg)
 

Enumerations

enum  ReplaceVarsNoMatchOption { REPLACEVARS_REPORT_ERROR , REPLACEVARS_CHANGE_VARNO , REPLACEVARS_SUBSTITUTE_NULL }
 

Functions

Relids adjust_relid_set (Relids relids, int oldrelid, int newrelid)
 
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 ChangeVarNodesExtended (Node *node, int rt_index, int new_index, int sublevels_up, ChangeVarNodes_callback callback)
 
bool ChangeVarNodesWalkExpression (Node *node, ChangeVarNodes_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)
 
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 AttrMap *attno_map, Oid to_rowtype, bool *found_whole_row)
 
NodeReplaceVarFromTargetList (const Var *var, RangeTblEntry *target_rte, List *targetlist, int result_relation, ReplaceVarsNoMatchOption nomatch_option, int nomatch_varno)
 
NodeReplaceVarsFromTargetList (Node *node, int target_varno, int sublevels_up, RangeTblEntry *target_rte, List *targetlist, int result_relation, ReplaceVarsNoMatchOption nomatch_option, int nomatch_varno, bool *outer_hasSubLinks)
 

Typedef Documentation

◆ AttrMap

Definition at line 20 of file rewriteManip.h.

◆ ChangeVarNodes_callback

typedef bool(* ChangeVarNodes_callback) (Node *node, ChangeVarNodes_context *arg)

Definition at line 46 of file rewriteManip.h.

◆ ChangeVarNodes_context

◆ replace_rte_variables_callback

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

Definition at line 25 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 37 of file rewriteManip.h.

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

Function Documentation

◆ add_nulling_relids()

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

Definition at line 1252 of file rewriteManip.c.

1255{
1257
1258 context.target_relids = target_relids;
1259 context.added_relids = added_relids;
1260 context.sublevels_up = 0;
1263 &context,
1264 0);
1265}
#define query_or_expression_tree_mutator(n, m, c, f)
Definition nodeFuncs.h:173
static Node * add_nulling_relids_mutator(Node *node, add_nulling_relids_context *context)
const Bitmapset * target_relids
const Bitmapset * added_relids

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(), mark_nullable_by_grouping(), pullup_replace_vars_callback(), and transform_MERGE_to_join().

◆ AddInvertedQual()

void AddInvertedQual ( Query parsetree,
Node qual 
)
extern

Definition at line 1228 of file rewriteManip.c.

1229{
1231
1232 if (qual == NULL)
1233 return;
1234
1235 /* Need not copy input qual, because AddQual will... */
1237 invqual->arg = (Expr *) qual;
1238 invqual->booltesttype = IS_NOT_TRUE;
1239 invqual->location = -1;
1240
1241 AddQual(parsetree, (Node *) invqual);
1242}
#define makeNode(_type_)
Definition nodes.h:161
static int fb(int x)
@ IS_NOT_TRUE
Definition primnodes.h:2004
void AddQual(Query *parsetree, Node *qual)
Definition nodes.h:135

References AddQual(), fb(), IS_NOT_TRUE, and makeNode.

Referenced by CopyAndAddInvertedQual().

◆ AddQual()

void AddQual ( Query parsetree,
Node qual 
)
extern

Definition at line 1160 of file rewriteManip.c.

1161{
1162 Node *copy;
1163
1164 if (qual == NULL)
1165 return;
1166
1167 if (parsetree->commandType == CMD_UTILITY)
1168 {
1169 /*
1170 * There's noplace to put the qual on a utility statement.
1171 *
1172 * If it's a NOTIFY, silently ignore the qual; this means that the
1173 * NOTIFY will execute, whether or not there are any qualifying rows.
1174 * While clearly wrong, this is much more useful than refusing to
1175 * execute the rule at all, and extra NOTIFY events are harmless for
1176 * typical uses of NOTIFY.
1177 *
1178 * If it isn't a NOTIFY, error out, since unconditional execution of
1179 * other utility stmts is unlikely to be wanted. (This case is not
1180 * currently allowed anyway, but keep the test for safety.)
1181 */
1182 if (parsetree->utilityStmt && IsA(parsetree->utilityStmt, NotifyStmt))
1183 return;
1184 else
1185 ereport(ERROR,
1187 errmsg("conditional utility statements are not implemented")));
1188 }
1189
1190 if (parsetree->setOperations != NULL)
1191 {
1192 /*
1193 * There's noplace to put the qual on a setop statement, either. (This
1194 * could be fixed, but right now the planner simply ignores any qual
1195 * condition on a setop query.)
1196 */
1197 ereport(ERROR,
1199 errmsg("conditional UNION/INTERSECT/EXCEPT statements are not implemented")));
1200 }
1201
1202 /* INTERSECT wants the original, but we need to copy - Jan */
1203 copy = copyObject(qual);
1204
1205 parsetree->jointree->quals = make_and_qual(parsetree->jointree->quals,
1206 copy);
1207
1208 /*
1209 * We had better not have stuck an aggregate into the WHERE clause.
1210 */
1212
1213 /*
1214 * Make sure query is marked correctly if added qual has sublinks. Need
1215 * not search qual when query is already marked.
1216 */
1217 if (!parsetree->hasSubLinks)
1218 parsetree->hasSubLinks = checkExprHasSubLink(copy);
1219}
#define Assert(condition)
Definition c.h:943
int errcode(int sqlerrcode)
Definition elog.c:874
#define ERROR
Definition elog.h:39
#define ereport(elevel,...)
Definition elog.h:151
Node * make_and_qual(Node *qual1, Node *qual2)
Definition makefuncs.c:780
#define IsA(nodeptr, _type_)
Definition nodes.h:164
#define copyObject(obj)
Definition nodes.h:232
@ CMD_UTILITY
Definition nodes.h:280
static char * errmsg
bool checkExprHasSubLink(Node *node)
bool contain_aggs_of_level(Node *node, int levelsup)
Node * quals
Definition primnodes.h:2385
FromExpr * jointree
Definition parsenodes.h:185
Node * setOperations
Definition parsenodes.h:239
CmdType commandType
Definition parsenodes.h:121
Node * utilityStmt
Definition parsenodes.h:141

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

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

◆ adjust_relid_set()

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

Definition at line 773 of file rewriteManip.c.

774{
776 {
777 /* Ensure we have a modifiable copy */
778 relids = bms_copy(relids);
779 /* Remove old, add new */
780 relids = bms_del_member(relids, oldrelid);
782 relids = bms_add_member(relids, newrelid);
783 }
784 return relids;
785}
Bitmapset * bms_del_member(Bitmapset *a, int x)
Definition bitmapset.c:852
bool bms_is_member(int x, const Bitmapset *a)
Definition bitmapset.c:510
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition bitmapset.c:799
Bitmapset * bms_copy(const Bitmapset *a)
Definition bitmapset.c:122
#define IS_SPECIAL_VARNO(varno)
Definition primnodes.h:248

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

Referenced by ChangeVarNodes_walker(), remove_rel_from_eclass(), remove_rel_from_query(), remove_self_join_rel(), replace_relid_callback(), and update_eclasses().

◆ ChangeVarNodes()

◆ ChangeVarNodesExtended()

void ChangeVarNodesExtended ( Node node,
int  rt_index,
int  new_index,
int  sublevels_up,
ChangeVarNodes_callback  callback 
)
extern

Definition at line 678 of file rewriteManip.c.

680{
682
683 context.rt_index = rt_index;
684 context.new_index = new_index;
685 context.sublevels_up = sublevels_up;
686 context.callback = callback;
687
688 /*
689 * Must be prepared to start with a Query or a bare expression tree; if
690 * it's a Query, go straight to query_tree_walker to make sure that
691 * sublevels_up doesn't get incremented prematurely.
692 */
693 if (node && IsA(node, Query))
694 {
695 Query *qry = (Query *) node;
696
697 /*
698 * If we are starting at a Query, and sublevels_up is zero, then we
699 * must also fix rangetable indexes in the Query itself --- namely
700 * resultRelation, mergeTargetRelation, exclRelIndex and rowMarks
701 * entries. sublevels_up cannot be zero when recursing into a
702 * subquery, so there's no need to have the same logic inside
703 * ChangeVarNodes_walker.
704 */
705 if (sublevels_up == 0)
706 {
707 ListCell *l;
708
709 if (qry->resultRelation == rt_index)
710 qry->resultRelation = new_index;
711
712 if (qry->mergeTargetRelation == rt_index)
713 qry->mergeTargetRelation = new_index;
714
715 /* this is unlikely to ever be used, but ... */
716 if (qry->onConflict && qry->onConflict->exclRelIndex == rt_index)
717 qry->onConflict->exclRelIndex = new_index;
718
719 foreach(l, qry->rowMarks)
720 {
722
723 if (rc->rti == rt_index)
724 rc->rti = new_index;
725 }
726 }
727 query_tree_walker(qry, ChangeVarNodes_walker, &context, 0);
728 }
729 else
730 ChangeVarNodes_walker(node, &context);
731}
#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)
ChangeVarNodes_callback callback
List * rowMarks
Definition parsenodes.h:237
OnConflictExpr * onConflict
Definition parsenodes.h:206
static void callback(struct sockaddr *addr, struct sockaddr *mask, void *unused)

References ChangeVarNodes_context::callback, callback(), 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 ChangeVarNodes(), match_unique_clauses(), remove_rel_from_eclass(), remove_rel_from_query(), remove_self_join_rel(), split_selfjoin_quals(), and update_eclasses().

◆ ChangeVarNodesWalkExpression()

bool ChangeVarNodesWalkExpression ( Node node,
ChangeVarNodes_context context 
)
extern

Definition at line 758 of file rewriteManip.c.

759{
760 return ChangeVarNodes_walker(node, context);
761}

References ChangeVarNodes_walker().

Referenced by replace_relid_callback().

◆ checkExprHasSubLink()

bool checkExprHasSubLink ( Node node)
extern

Definition at line 293 of file rewriteManip.c.

294{
295 /*
296 * If a Query is passed, examine it --- but we should not recurse into
297 * sub-Queries that are in its rangetable or CTE list.
298 */
301 NULL,
303}
#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)

References checkExprHasSubLink_walker(), fb(), 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 
)
extern

Definition at line 348 of file rewriteManip.c.

350{
351 ListCell *l;
352 int offset = list_length(*dst_perminfos);
353
354 if (offset > 0)
355 {
356 foreach(l, src_rtable)
357 {
359
360 if (rte->perminfoindex > 0)
361 rte->perminfoindex += offset;
362 }
363 }
364
367}
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 fb(), 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 
)
extern

Definition at line 86 of file rewriteManip.c.

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

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

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

◆ contain_windowfuncs()

bool contain_windowfuncs ( Node node)
extern

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)

References contain_windowfuncs_walker(), fb(), and query_or_expression_tree_walker.

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

◆ getInsertSelectQuery()

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

Definition at line 1102 of file rewriteManip.c.

1103{
1107
1108 if (subquery_ptr)
1109 *subquery_ptr = NULL;
1110
1111 if (parsetree == NULL)
1112 return parsetree;
1113 if (parsetree->commandType != CMD_INSERT)
1114 return parsetree;
1115
1116 /*
1117 * Currently, this is ONLY applied to rule-action queries, and so we
1118 * expect to find the OLD and NEW placeholder entries in the given query.
1119 * If they're not there, it must be an INSERT/SELECT in which they've been
1120 * pushed down to the SELECT.
1121 */
1122 if (list_length(parsetree->rtable) >= 2 &&
1123 strcmp(rt_fetch(PRS2_OLD_VARNO, parsetree->rtable)->eref->aliasname,
1124 "old") == 0 &&
1125 strcmp(rt_fetch(PRS2_NEW_VARNO, parsetree->rtable)->eref->aliasname,
1126 "new") == 0)
1127 return parsetree;
1128 Assert(parsetree->jointree && IsA(parsetree->jointree, FromExpr));
1129 if (list_length(parsetree->jointree->fromlist) != 1)
1130 elog(ERROR, "expected to find SELECT subquery");
1131 rtr = (RangeTblRef *) linitial(parsetree->jointree->fromlist);
1132 if (!IsA(rtr, RangeTblRef))
1133 elog(ERROR, "expected to find SELECT subquery");
1134 selectrte = rt_fetch(rtr->rtindex, parsetree->rtable);
1135 if (!(selectrte->rtekind == RTE_SUBQUERY &&
1136 selectrte->subquery &&
1137 IsA(selectrte->subquery, Query) &&
1138 selectrte->subquery->commandType == CMD_SELECT))
1139 elog(ERROR, "expected to find SELECT subquery");
1140 selectquery = selectrte->subquery;
1141 if (list_length(selectquery->rtable) >= 2 &&
1142 strcmp(rt_fetch(PRS2_OLD_VARNO, selectquery->rtable)->eref->aliasname,
1143 "old") == 0 &&
1144 strcmp(rt_fetch(PRS2_NEW_VARNO, selectquery->rtable)->eref->aliasname,
1145 "new") == 0)
1146 {
1147 if (subquery_ptr)
1148 *subquery_ptr = &(selectrte->subquery);
1149 return selectquery;
1150 }
1151 elog(ERROR, "could not find rule placeholders");
1152 return NULL; /* not reached */
1153}
#define elog(elevel,...)
Definition elog.h:227
@ CMD_INSERT
Definition nodes.h:277
@ CMD_SELECT
Definition nodes.h:275
@ RTE_SUBQUERY
#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:251
#define PRS2_NEW_VARNO
Definition primnodes.h:252
List * fromlist
Definition primnodes.h:2384
List * rtable
Definition parsenodes.h:178

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

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

◆ IncrementVarSublevelsUp()

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

Definition at line 893 of file rewriteManip.c.

895{
897
898 context.delta_sublevels_up = delta_sublevels_up;
899 context.min_sublevels_up = min_sublevels_up;
900
901 /*
902 * Must be prepared to start with a Query or a bare expression tree; if
903 * it's a Query, we don't want to increment sublevels_up.
904 */
907 &context,
909}
#define QTW_EXAMINE_RTES_BEFORE
Definition nodeFuncs.h:27
static bool IncrementVarSublevelsUp_walker(Node *node, IncrementVarSublevelsUp_context *context)

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(), finalize_grouping_exprs_walker(), 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(), replace_outer_returning(), ReplaceVarsFromTargetList_callback(), rewriteSearchAndCycle(), substitute_actual_parameters_in_from_mutator(), and substitute_grouped_columns_mutator().

◆ IncrementVarSublevelsUp_rtable()

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

Definition at line 916 of file rewriteManip.c.

918{
920
921 context.delta_sublevels_up = delta_sublevels_up;
922 context.min_sublevels_up = min_sublevels_up;
923
924 range_table_walker(rtable,
926 &context,
928}
#define range_table_walker(rt, w, c, f)
Definition nodeFuncs.h:163

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 
)
extern

Definition at line 150 of file rewriteManip.c.

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

References locate_agg_of_level_context::agg_location, fb(), 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)
extern

Definition at line 252 of file rewriteManip.c.

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

References fb(), 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 AttrMap attno_map,
Oid  to_rowtype,
bool found_whole_row 
)
extern

Definition at line 1714 of file rewriteManip.c.

1718{
1720
1721 context.target_varno = target_varno;
1722 context.sublevels_up = sublevels_up;
1723 context.attno_map = attno_map;
1724 context.to_rowtype = to_rowtype;
1725 context.found_whole_row = found_whole_row;
1726
1727 *found_whole_row = false;
1728
1729 /*
1730 * Must be prepared to start with a Query or a bare expression tree; if
1731 * it's a Query, we don't want to increment sublevels_up.
1732 */
1735 &context,
1736 0);
1737}
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(), createTableConstraints(), ExecInitMerge(), ExecInitPartitionInfo(), expandTableLikeClause(), generateClonedExtStatsStmt(), generateClonedIndexStmt(), map_partition_varattnos(), and MergeAttributes().

◆ OffsetVarNodes()

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

Definition at line 477 of file rewriteManip.c.

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

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 
)
extern

Definition at line 1070 of file rewriteManip.c.

1071{
1073
1074 context.rt_index = rt_index;
1075 context.sublevels_up = sublevels_up;
1076
1077 /*
1078 * Must be prepared to start with a Query or a bare expression tree; if
1079 * it's a Query, we don't want to increment sublevels_up.
1080 */
1083 &context,
1084 0);
1085}
static bool rangeTableEntry_used_walker(Node *node, rangeTableEntry_used_context *context)

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 
)
extern

◆ 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 
)
extern

Definition at line 1447 of file rewriteManip.c.

1451{
1452 Node *result;
1454
1455 context.callback = callback;
1456 context.callback_arg = callback_arg;
1457 context.target_varno = target_varno;
1458 context.sublevels_up = sublevels_up;
1459
1460 /*
1461 * We try to initialize inserted_sublink to true if there is no need to
1462 * detect new sublinks because the query already has some.
1463 */
1464 if (node && IsA(node, Query))
1465 context.inserted_sublink = ((Query *) node)->hasSubLinks;
1466 else if (outer_hasSubLinks)
1467 context.inserted_sublink = *outer_hasSubLinks;
1468 else
1469 context.inserted_sublink = false;
1470
1471 /*
1472 * Must be prepared to start with a Query or a bare expression tree; if
1473 * it's a Query, we don't want to increment sublevels_up.
1474 */
1477 &context,
1478 0);
1479
1480 if (context.inserted_sublink)
1481 {
1482 if (result && IsA(result, Query))
1483 ((Query *) result)->hasSubLinks = true;
1484 else if (outer_hasSubLinks)
1485 *outer_hasSubLinks = true;
1486 else
1487 elog(ERROR, "replace_rte_variables inserted a SubLink, but has noplace to record it");
1488 }
1489
1490 return result;
1491}
uint32 result
Node * replace_rte_variables_mutator(Node *node, replace_rte_variables_context *context)
replace_rte_variables_callback callback

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

◆ replace_rte_variables_mutator()

Node * replace_rte_variables_mutator ( Node node,
replace_rte_variables_context context 
)
extern

Definition at line 1494 of file rewriteManip.c.

1496{
1497 if (node == NULL)
1498 return NULL;
1499 if (IsA(node, Var))
1500 {
1501 Var *var = (Var *) node;
1502
1503 if (var->varno == context->target_varno &&
1504 var->varlevelsup == context->sublevels_up)
1505 {
1506 /* Found a matching variable, make the substitution */
1507 Node *newnode;
1508
1509 newnode = context->callback(var, context);
1510 /* Detect if we are adding a sublink to query */
1511 if (!context->inserted_sublink)
1513 return newnode;
1514 }
1515 /* otherwise fall through to copy the var normally */
1516 }
1517 else if (IsA(node, CurrentOfExpr))
1518 {
1519 CurrentOfExpr *cexpr = (CurrentOfExpr *) node;
1520
1521 if (cexpr->cvarno == context->target_varno &&
1522 context->sublevels_up == 0)
1523 {
1524 /*
1525 * We get here if a WHERE CURRENT OF expression turns out to apply
1526 * to a view. Someday we might be able to translate the
1527 * expression to apply to an underlying table of the view, but
1528 * right now it's not implemented.
1529 */
1530 ereport(ERROR,
1532 errmsg("WHERE CURRENT OF on a view is not implemented")));
1533 }
1534 /* otherwise fall through to copy the expr normally */
1535 }
1536 else if (IsA(node, Query))
1537 {
1538 /* Recurse into RTE subquery or not-yet-planned sublink subquery */
1539 Query *newnode;
1541
1542 context->sublevels_up++;
1544 context->inserted_sublink = ((Query *) node)->hasSubLinks;
1545 newnode = query_tree_mutator((Query *) node,
1547 context,
1548 0);
1549 newnode->hasSubLinks |= context->inserted_sublink;
1551 context->sublevels_up--;
1552 return (Node *) newnode;
1553 }
1555}
#define expression_tree_mutator(n, m, c)
Definition nodeFuncs.h:155
#define query_tree_mutator(q, m, c, f)
Definition nodeFuncs.h:160
int varno
Definition primnodes.h:270
Index varlevelsup
Definition primnodes.h:295

References replace_rte_variables_context::callback, checkExprHasSubLink(), CurrentOfExpr::cvarno, ereport, errcode(), errmsg, ERROR, expression_tree_mutator, fb(), 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 replace_rte_variables(), and replace_rte_variables_mutator().

◆ ReplaceVarFromTargetList()

Node * ReplaceVarFromTargetList ( const Var var,
RangeTblEntry target_rte,
List targetlist,
int  result_relation,
ReplaceVarsNoMatchOption  nomatch_option,
int  nomatch_varno 
)
extern

Definition at line 1804 of file rewriteManip.c.

1810{
1812
1813 if (var->varattno == InvalidAttrNumber)
1814 {
1815 /* Must expand whole-tuple reference into RowExpr */
1816 RowExpr *rowexpr;
1817 List *colnames;
1818 List *fields;
1819 ListCell *lc;
1820
1821 /*
1822 * If generating an expansion for a var of a named rowtype (ie, this
1823 * is a plain relation RTE), then we must include dummy items for
1824 * dropped columns. If the var is RECORD (ie, this is a JOIN), then
1825 * omit dropped columns. In the latter case, attach column names to
1826 * the RowExpr for use of the executor and ruleutils.c.
1827 *
1828 * In order to be able to cache the results, we always generate the
1829 * expansion with varlevelsup = 0. The caller is responsible for
1830 * adjusting it if needed.
1831 *
1832 * The varreturningtype is copied onto each individual field Var, so
1833 * that it is handled correctly when we recurse.
1834 */
1835 expandRTE(target_rte,
1836 var->varno, 0 /* not varlevelsup */ ,
1837 var->varreturningtype, var->location,
1838 (var->vartype != RECORDOID),
1839 &colnames, &fields);
1840 rowexpr = makeNode(RowExpr);
1841 /* the fields will be set below */
1842 rowexpr->args = NIL;
1843 rowexpr->row_typeid = var->vartype;
1844 rowexpr->row_format = COERCE_IMPLICIT_CAST;
1845 rowexpr->colnames = (var->vartype == RECORDOID) ? colnames : NIL;
1846 rowexpr->location = var->location;
1847 /* Adjust the generated per-field Vars... */
1848 foreach(lc, fields)
1849 {
1850 Node *field = lfirst(lc);
1851
1852 if (field && IsA(field, Var))
1853 field = ReplaceVarFromTargetList((Var *) field,
1854 target_rte,
1855 targetlist,
1856 result_relation,
1857 nomatch_option,
1858 nomatch_varno);
1859 rowexpr->args = lappend(rowexpr->args, field);
1860 }
1861
1862 /* Wrap it in a ReturningExpr, if needed, per comments above */
1864 {
1866
1867 rexpr->retlevelsup = 0;
1868 rexpr->retold = (var->varreturningtype == VAR_RETURNING_OLD);
1869 rexpr->retexpr = (Expr *) rowexpr;
1870
1871 return (Node *) rexpr;
1872 }
1873
1874 return (Node *) rowexpr;
1875 }
1876
1877 /* Normal case referencing one targetlist element */
1878 tle = get_tle_by_resno(targetlist, var->varattno);
1879
1880 if (tle == NULL || tle->resjunk)
1881 {
1882 /* Failed to find column in targetlist */
1883 switch (nomatch_option)
1884 {
1886 /* fall through, throw error below */
1887 break;
1888
1890 {
1891 Var *newvar = copyObject(var);
1892
1893 newvar->varno = nomatch_varno;
1894 newvar->varlevelsup = 0;
1895 /* we leave the syntactic referent alone */
1896 return (Node *) newvar;
1897 }
1898
1900 {
1901 /*
1902 * If Var is of domain type, we must add a CoerceToDomain
1903 * node, in case there is a NOT NULL domain constraint.
1904 */
1906 bool vartypbyval;
1907
1908 get_typlenbyval(var->vartype, &vartyplen, &vartypbyval);
1909 return coerce_null_to_domain(var->vartype,
1910 var->vartypmod,
1911 var->varcollid,
1912 vartyplen,
1913 vartypbyval);
1914 }
1915 }
1916 elog(ERROR, "could not find replacement targetlist entry for attno %d",
1917 var->varattno);
1918 return NULL; /* keep compiler quiet */
1919 }
1920 else
1921 {
1922 /* Make a copy of the tlist item to return */
1923 Expr *newnode = copyObject(tle->expr);
1924
1925 /*
1926 * Check to see if the tlist item contains a PARAM_MULTIEXPR Param,
1927 * and throw error if so. This case could only happen when expanding
1928 * an ON UPDATE rule's NEW variable and the referenced tlist item in
1929 * the original UPDATE command is part of a multiple assignment. There
1930 * seems no practical way to handle such cases without multiple
1931 * evaluation of the multiple assignment's sub-select, which would
1932 * create semantic oddities that users of rules would probably prefer
1933 * not to cope with. So treat it as an unimplemented feature.
1934 */
1936 ereport(ERROR,
1938 errmsg("NEW variables in ON UPDATE rules cannot reference columns that are part of a multiple assignment in the subject UPDATE command")));
1939
1940 /* Handle any OLD/NEW RETURNING list Vars */
1942 {
1943 /*
1944 * Copy varreturningtype onto any Vars in the tlist item that
1945 * refer to result_relation (which had better be non-zero).
1946 */
1947 if (result_relation == 0)
1948 elog(ERROR, "variable returning old/new found outside RETURNING list");
1949
1950 SetVarReturningType((Node *) newnode, result_relation,
1951 0, var->varreturningtype);
1952
1953 /* Wrap it in a ReturningExpr, if needed, per comments above */
1954 if (!IsA(newnode, Var) ||
1955 ((Var *) newnode)->varno != result_relation ||
1956 ((Var *) newnode)->varlevelsup != 0)
1957 {
1959
1960 rexpr->retlevelsup = 0;
1961 rexpr->retold = (var->varreturningtype == VAR_RETURNING_OLD);
1962 rexpr->retexpr = newnode;
1963
1964 newnode = (Expr *) rexpr;
1965 }
1966 }
1967
1968 return (Node *) newnode;
1969 }
1970}
#define InvalidAttrNumber
Definition attnum.h:23
int16_t int16
Definition c.h:619
List * lappend(List *list, void *datum)
Definition list.c:339
void get_typlenbyval(Oid typid, int16 *typlen, bool *typbyval)
Definition lsyscache.c:2471
Node * coerce_null_to_domain(Oid typid, int32 typmod, Oid collation, int typlen, bool typbyval)
TargetEntry * get_tle_by_resno(List *tlist, AttrNumber resno)
void expandRTE(RangeTblEntry *rte, int rtindex, int sublevels_up, VarReturningType returning_type, int location, bool include_dropped, List **colnames, List **colvars)
#define NIL
Definition pg_list.h:68
@ VAR_RETURNING_OLD
Definition primnodes.h:258
@ VAR_RETURNING_DEFAULT
Definition primnodes.h:257
@ COERCE_IMPLICIT_CAST
Definition primnodes.h:769
static bool contains_multiexpr_param(Node *node, void *context)
static void SetVarReturningType(Node *node, int result_relation, int sublevels_up, VarReturningType returning_type)
Node * ReplaceVarFromTargetList(const Var *var, RangeTblEntry *target_rte, List *targetlist, int result_relation, ReplaceVarsNoMatchOption nomatch_option, int nomatch_varno)
Definition pg_list.h:54
List * args
Definition primnodes.h:1450
ParseLoc location
Definition primnodes.h:1474
ParseLoc location
Definition primnodes.h:311
AttrNumber varattno
Definition primnodes.h:275
VarReturningType varreturningtype
Definition primnodes.h:298

References RowExpr::args, COERCE_IMPLICIT_CAST, coerce_null_to_domain(), contains_multiexpr_param(), copyObject, elog, ereport, errcode(), errmsg, ERROR, expandRTE(), fb(), get_tle_by_resno(), get_typlenbyval(), InvalidAttrNumber, IsA, lappend(), lfirst, Var::location, RowExpr::location, makeNode, NIL, ReplaceVarFromTargetList(), REPLACEVARS_CHANGE_VARNO, REPLACEVARS_REPORT_ERROR, REPLACEVARS_SUBSTITUTE_NULL, ReturningExpr::retexpr, ReturningExpr::retlevelsup, ReturningExpr::retold, SetVarReturningType(), VAR_RETURNING_DEFAULT, VAR_RETURNING_OLD, Var::varattno, Var::varno, and Var::varreturningtype.

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

◆ ReplaceVarsFromTargetList()

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

Definition at line 1973 of file rewriteManip.c.

1981{
1983
1984 context.target_rte = target_rte;
1985 context.targetlist = targetlist;
1986 context.result_relation = result_relation;
1987 context.nomatch_option = nomatch_option;
1988 context.nomatch_varno = nomatch_varno;
1989
1990 return replace_rte_variables(node, target_varno, sublevels_up,
1992 &context,
1993 outer_hasSubLinks);
1994}
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(const 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::result_relation, ReplaceVarsFromTargetList_context::target_rte, and ReplaceVarsFromTargetList_context::targetlist.

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