PostgreSQL Source Code git master
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
 

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

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, bool change_RangeTblRef)
 
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)
 
NodeReplaceVarFromTargetList (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

◆ replace_rte_variables_callback

typedef Node *(* replace_rte_variables_callback) (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
Definition: rewriteManip.h:38
@ REPLACEVARS_SUBSTITUTE_NULL
Definition: rewriteManip.h:41
@ REPLACEVARS_CHANGE_VARNO
Definition: rewriteManip.h:40
@ REPLACEVARS_REPORT_ERROR
Definition: rewriteManip.h:39

Function Documentation

◆ add_nulling_relids()

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

Definition at line 1321 of file rewriteManip.c.

1324{
1326
1327 context.target_relids = target_relids;
1328 context.added_relids = added_relids;
1329 context.sublevels_up = 0;
1332 &context,
1333 0);
1334}
#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
Definition: rewriteManip.c:46
const Bitmapset * added_relids
Definition: rewriteManip.c:47

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 
)

Definition at line 1297 of file rewriteManip.c.

1298{
1299 BooleanTest *invqual;
1300
1301 if (qual == NULL)
1302 return;
1303
1304 /* Need not copy input qual, because AddQual will... */
1305 invqual = makeNode(BooleanTest);
1306 invqual->arg = (Expr *) qual;
1307 invqual->booltesttype = IS_NOT_TRUE;
1308 invqual->location = -1;
1309
1310 AddQual(parsetree, (Node *) invqual);
1311}
#define makeNode(_type_)
Definition: nodes.h:161
@ IS_NOT_TRUE
Definition: primnodes.h:1981
void AddQual(Query *parsetree, Node *qual)
ParseLoc location
Definition: primnodes.h:1989
BoolTestType booltesttype
Definition: primnodes.h:1988
Expr * arg
Definition: primnodes.h:1987
Definition: nodes.h:135

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

1230{
1231 Node *copy;
1232
1233 if (qual == NULL)
1234 return;
1235
1236 if (parsetree->commandType == CMD_UTILITY)
1237 {
1238 /*
1239 * There's noplace to put the qual on a utility statement.
1240 *
1241 * If it's a NOTIFY, silently ignore the qual; this means that the
1242 * NOTIFY will execute, whether or not there are any qualifying rows.
1243 * While clearly wrong, this is much more useful than refusing to
1244 * execute the rule at all, and extra NOTIFY events are harmless for
1245 * typical uses of NOTIFY.
1246 *
1247 * If it isn't a NOTIFY, error out, since unconditional execution of
1248 * other utility stmts is unlikely to be wanted. (This case is not
1249 * currently allowed anyway, but keep the test for safety.)
1250 */
1251 if (parsetree->utilityStmt && IsA(parsetree->utilityStmt, NotifyStmt))
1252 return;
1253 else
1254 ereport(ERROR,
1255 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1256 errmsg("conditional utility statements are not implemented")));
1257 }
1258
1259 if (parsetree->setOperations != NULL)
1260 {
1261 /*
1262 * There's noplace to put the qual on a setop statement, either. (This
1263 * could be fixed, but right now the planner simply ignores any qual
1264 * condition on a setop query.)
1265 */
1266 ereport(ERROR,
1267 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1268 errmsg("conditional UNION/INTERSECT/EXCEPT statements are not implemented")));
1269 }
1270
1271 /* INTERSECT wants the original, but we need to copy - Jan */
1272 copy = copyObject(qual);
1273
1274 parsetree->jointree->quals = make_and_qual(parsetree->jointree->quals,
1275 copy);
1276
1277 /*
1278 * We had better not have stuck an aggregate into the WHERE clause.
1279 */
1280 Assert(!contain_aggs_of_level(copy, 0));
1281
1282 /*
1283 * Make sure query is marked correctly if added qual has sublinks. Need
1284 * not search qual when query is already marked.
1285 */
1286 if (!parsetree->hasSubLinks)
1287 parsetree->hasSubLinks = checkExprHasSubLink(copy);
1288}
int errcode(int sqlerrcode)
Definition: elog.c:854
int errmsg(const char *fmt,...)
Definition: elog.c:1071
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
Assert(PointerIsAligned(start, uint64))
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:230
@ CMD_UTILITY
Definition: nodes.h:276
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:2338
FromExpr * jointree
Definition: parsenodes.h:177
Node * setOperations
Definition: parsenodes.h:230
CmdType commandType
Definition: parsenodes.h:121
Node * utilityStmt
Definition: parsenodes.h:136

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

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

◆ adjust_relid_set()

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

Definition at line 842 of file rewriteManip.c.

843{
844 if (!IS_SPECIAL_VARNO(oldrelid) && bms_is_member(oldrelid, relids))
845 {
846 /* Ensure we have a modifiable copy */
847 relids = bms_copy(relids);
848 /* Remove old, add new */
849 relids = bms_del_member(relids, oldrelid);
850 if (!IS_SPECIAL_VARNO(newrelid))
851 relids = bms_add_member(relids, newrelid);
852 }
853 return relids;
854}
Bitmapset * bms_del_member(Bitmapset *a, int x)
Definition: bitmapset.c:868
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:510
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:815
Bitmapset * bms_copy(const Bitmapset *a)
Definition: bitmapset.c:122
#define IS_SPECIAL_VARNO(varno)
Definition: primnodes.h:247

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

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

◆ ChangeVarNodes()

◆ ChangeVarNodesExtended()

void ChangeVarNodesExtended ( Node node,
int  rt_index,
int  new_index,
int  sublevels_up,
bool  change_RangeTblRef 
)

Definition at line 771 of file rewriteManip.c.

773{
775
776 context.rt_index = rt_index;
777 context.new_index = new_index;
778 context.sublevels_up = sublevels_up;
779 context.change_RangeTblRef = change_RangeTblRef;
780
781 /*
782 * Must be prepared to start with a Query or a bare expression tree; if
783 * it's a Query, go straight to query_tree_walker to make sure that
784 * sublevels_up doesn't get incremented prematurely.
785 */
786 if (node && IsA(node, Query))
787 {
788 Query *qry = (Query *) node;
789
790 /*
791 * If we are starting at a Query, and sublevels_up is zero, then we
792 * must also fix rangetable indexes in the Query itself --- namely
793 * resultRelation, mergeTargetRelation, exclRelIndex and rowMarks
794 * entries. sublevels_up cannot be zero when recursing into a
795 * subquery, so there's no need to have the same logic inside
796 * ChangeVarNodes_walker.
797 */
798 if (sublevels_up == 0)
799 {
800 ListCell *l;
801
802 if (qry->resultRelation == rt_index)
803 qry->resultRelation = new_index;
804
805 if (qry->mergeTargetRelation == rt_index)
806 qry->mergeTargetRelation = new_index;
807
808 /* this is unlikely to ever be used, but ... */
809 if (qry->onConflict && qry->onConflict->exclRelIndex == rt_index)
810 qry->onConflict->exclRelIndex = new_index;
811
812 foreach(l, qry->rowMarks)
813 {
815
816 if (rc->rti == rt_index)
817 rc->rti = new_index;
818 }
819 }
820 query_tree_walker(qry, ChangeVarNodes_walker, &context, 0);
821 }
822 else
823 ChangeVarNodes_walker(node, &context);
824}
#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:562
List * rowMarks
Definition: parsenodes.h:228
OnConflictExpr * onConflict
Definition: parsenodes.h:198

References ChangeVarNodes_context::change_RangeTblRef, 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(), and remove_self_join_rel().

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

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

◆ contain_windowfuncs()

bool contain_windowfuncs ( Node node)

Definition at line 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 1171 of file rewriteManip.c.

1172{
1173 Query *selectquery;
1174 RangeTblEntry *selectrte;
1175 RangeTblRef *rtr;
1176
1177 if (subquery_ptr)
1178 *subquery_ptr = NULL;
1179
1180 if (parsetree == NULL)
1181 return parsetree;
1182 if (parsetree->commandType != CMD_INSERT)
1183 return parsetree;
1184
1185 /*
1186 * Currently, this is ONLY applied to rule-action queries, and so we
1187 * expect to find the OLD and NEW placeholder entries in the given query.
1188 * If they're not there, it must be an INSERT/SELECT in which they've been
1189 * pushed down to the SELECT.
1190 */
1191 if (list_length(parsetree->rtable) >= 2 &&
1192 strcmp(rt_fetch(PRS2_OLD_VARNO, parsetree->rtable)->eref->aliasname,
1193 "old") == 0 &&
1194 strcmp(rt_fetch(PRS2_NEW_VARNO, parsetree->rtable)->eref->aliasname,
1195 "new") == 0)
1196 return parsetree;
1197 Assert(parsetree->jointree && IsA(parsetree->jointree, FromExpr));
1198 if (list_length(parsetree->jointree->fromlist) != 1)
1199 elog(ERROR, "expected to find SELECT subquery");
1200 rtr = (RangeTblRef *) linitial(parsetree->jointree->fromlist);
1201 if (!IsA(rtr, RangeTblRef))
1202 elog(ERROR, "expected to find SELECT subquery");
1203 selectrte = rt_fetch(rtr->rtindex, parsetree->rtable);
1204 if (!(selectrte->rtekind == RTE_SUBQUERY &&
1205 selectrte->subquery &&
1206 IsA(selectrte->subquery, Query) &&
1207 selectrte->subquery->commandType == CMD_SELECT))
1208 elog(ERROR, "expected to find SELECT subquery");
1209 selectquery = selectrte->subquery;
1210 if (list_length(selectquery->rtable) >= 2 &&
1211 strcmp(rt_fetch(PRS2_OLD_VARNO, selectquery->rtable)->eref->aliasname,
1212 "old") == 0 &&
1213 strcmp(rt_fetch(PRS2_NEW_VARNO, selectquery->rtable)->eref->aliasname,
1214 "new") == 0)
1215 {
1216 if (subquery_ptr)
1217 *subquery_ptr = &(selectrte->subquery);
1218 return selectquery;
1219 }
1220 elog(ERROR, "could not find rule placeholders");
1221 return NULL; /* not reached */
1222}
#define elog(elevel,...)
Definition: elog.h:225
@ CMD_INSERT
Definition: nodes.h:273
@ CMD_SELECT
Definition: nodes.h:271
@ RTE_SUBQUERY
Definition: parsenodes.h:1027
#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:250
#define PRS2_NEW_VARNO
Definition: primnodes.h:251
List * fromlist
Definition: primnodes.h:2337
List * rtable
Definition: parsenodes.h:170
Query * subquery
Definition: parsenodes.h:1118
RTEKind rtekind
Definition: parsenodes.h:1061

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

964{
966
967 context.delta_sublevels_up = delta_sublevels_up;
968 context.min_sublevels_up = min_sublevels_up;
969
970 /*
971 * Must be prepared to start with a Query or a bare expression tree; if
972 * it's a Query, we don't want to increment sublevels_up.
973 */
976 &context,
978}
#define QTW_EXAMINE_RTES_BEFORE
Definition: nodeFuncs.h:27
static bool IncrementVarSublevelsUp_walker(Node *node, IncrementVarSublevelsUp_context *context)
Definition: rewriteManip.c:882

References IncrementVarSublevelsUp_context::delta_sublevels_up, IncrementVarSublevelsUp_walker(), IncrementVarSublevelsUp_context::min_sublevels_up, QTW_EXAMINE_RTES_BEFORE, and query_or_expression_tree_walker.

Referenced by assign_param_for_placeholdervar(), build_minmax_path(), convert_EXISTS_sublink_to_join(), convert_EXISTS_to_ANY(), extract_lateral_references(), flatten_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(), and substitute_actual_srf_parameters_mutator().

◆ IncrementVarSublevelsUp_rtable()

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

Definition at line 985 of file rewriteManip.c.

987{
989
990 context.delta_sublevels_up = delta_sublevels_up;
991 context.min_sublevels_up = min_sublevels_up;
992
993 range_table_walker(rtable,
995 &context,
997}
#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 
)

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 locate_agg_of_level_context::agg_location, locate_agg_of_level_walker(), query_or_expression_tree_walker, and locate_agg_of_level_context::sublevels_up.

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

◆ locate_windowfunc()

int locate_windowfunc ( Node node)

Definition at line 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 locate_windowfunc_walker(), query_or_expression_tree_walker, and locate_windowfunc_context::win_location.

Referenced by checkTargetlistEntrySQL92(), and transformWindowFuncCall().

◆ map_variable_attnos()

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

◆ OffsetVarNodes()

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

Definition at line 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 {
516
517 rc->rti += offset;
518 }
519 }
520 query_tree_walker(qry, OffsetVarNodes_walker, &context, 0);
521 }
522 else
523 OffsetVarNodes_walker(node, &context);
524}
static bool OffsetVarNodes_walker(Node *node, OffsetVarNodes_context *context)
Definition: rewriteManip.c:388

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

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

◆ rangeTableEntry_used()

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

Definition at line 1139 of file rewriteManip.c.

1140{
1142
1143 context.rt_index = rt_index;
1144 context.sublevels_up = sublevels_up;
1145
1146 /*
1147 * Must be prepared to start with a Query or a bare expression tree; if
1148 * it's a Query, we don't want to increment sublevels_up.
1149 */
1152 &context,
1153 0);
1154}
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 
)

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

1520{
1521 Node *result;
1523
1524 context.callback = callback;
1525 context.callback_arg = callback_arg;
1526 context.target_varno = target_varno;
1527 context.sublevels_up = sublevels_up;
1528
1529 /*
1530 * We try to initialize inserted_sublink to true if there is no need to
1531 * detect new sublinks because the query already has some.
1532 */
1533 if (node && IsA(node, Query))
1534 context.inserted_sublink = ((Query *) node)->hasSubLinks;
1535 else if (outer_hasSubLinks)
1536 context.inserted_sublink = *outer_hasSubLinks;
1537 else
1538 context.inserted_sublink = false;
1539
1540 /*
1541 * Must be prepared to start with a Query or a bare expression tree; if
1542 * it's a Query, we don't want to increment sublevels_up.
1543 */
1546 &context,
1547 0);
1548
1549 if (context.inserted_sublink)
1550 {
1551 if (result && IsA(result, Query))
1552 ((Query *) result)->hasSubLinks = true;
1553 else if (outer_hasSubLinks)
1554 *outer_hasSubLinks = true;
1555 else
1556 elog(ERROR, "replace_rte_variables inserted a SubLink, but has noplace to record it");
1557 }
1558
1559 return result;
1560}
Node * replace_rte_variables_mutator(Node *node, replace_rte_variables_context *context)
replace_rte_variables_callback callback
Definition: rewriteManip.h:30
static void callback(struct sockaddr *addr, struct sockaddr *mask, void *unused)
Definition: test_ifaddrs.c:46

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

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

◆ replace_rte_variables_mutator()

Node * replace_rte_variables_mutator ( Node node,
replace_rte_variables_context context 
)

Definition at line 1563 of file rewriteManip.c.

1565{
1566 if (node == NULL)
1567 return NULL;
1568 if (IsA(node, Var))
1569 {
1570 Var *var = (Var *) node;
1571
1572 if (var->varno == context->target_varno &&
1573 var->varlevelsup == context->sublevels_up)
1574 {
1575 /* Found a matching variable, make the substitution */
1576 Node *newnode;
1577
1578 newnode = context->callback(var, context);
1579 /* Detect if we are adding a sublink to query */
1580 if (!context->inserted_sublink)
1581 context->inserted_sublink = checkExprHasSubLink(newnode);
1582 return newnode;
1583 }
1584 /* otherwise fall through to copy the var normally */
1585 }
1586 else if (IsA(node, CurrentOfExpr))
1587 {
1588 CurrentOfExpr *cexpr = (CurrentOfExpr *) node;
1589
1590 if (cexpr->cvarno == context->target_varno &&
1591 context->sublevels_up == 0)
1592 {
1593 /*
1594 * We get here if a WHERE CURRENT OF expression turns out to apply
1595 * to a view. Someday we might be able to translate the
1596 * expression to apply to an underlying table of the view, but
1597 * right now it's not implemented.
1598 */
1599 ereport(ERROR,
1600 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1601 errmsg("WHERE CURRENT OF on a view is not implemented")));
1602 }
1603 /* otherwise fall through to copy the expr normally */
1604 }
1605 else if (IsA(node, Query))
1606 {
1607 /* Recurse into RTE subquery or not-yet-planned sublink subquery */
1608 Query *newnode;
1609 bool save_inserted_sublink;
1610
1611 context->sublevels_up++;
1612 save_inserted_sublink = context->inserted_sublink;
1613 context->inserted_sublink = ((Query *) node)->hasSubLinks;
1614 newnode = query_tree_mutator((Query *) node,
1616 context,
1617 0);
1618 newnode->hasSubLinks |= context->inserted_sublink;
1619 context->inserted_sublink = save_inserted_sublink;
1620 context->sublevels_up--;
1621 return (Node *) newnode;
1622 }
1624}
#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:262
int varno
Definition: primnodes.h:269
Index varlevelsup
Definition: primnodes.h:294

References replace_rte_variables_context::callback, checkExprHasSubLink(), CurrentOfExpr::cvarno, ereport, errcode(), errmsg(), ERROR, expression_tree_mutator, replace_rte_variables_context::inserted_sublink, IsA, query_tree_mutator, replace_rte_variables_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 ( Var var,
RangeTblEntry target_rte,
List targetlist,
int  result_relation,
ReplaceVarsNoMatchOption  nomatch_option,
int  nomatch_varno 
)

Definition at line 1873 of file rewriteManip.c.

1879{
1880 TargetEntry *tle;
1881
1882 if (var->varattno == InvalidAttrNumber)
1883 {
1884 /* Must expand whole-tuple reference into RowExpr */
1885 RowExpr *rowexpr;
1886 List *colnames;
1887 List *fields;
1888 ListCell *lc;
1889
1890 /*
1891 * If generating an expansion for a var of a named rowtype (ie, this
1892 * is a plain relation RTE), then we must include dummy items for
1893 * dropped columns. If the var is RECORD (ie, this is a JOIN), then
1894 * omit dropped columns. In the latter case, attach column names to
1895 * the RowExpr for use of the executor and ruleutils.c.
1896 *
1897 * In order to be able to cache the results, we always generate the
1898 * expansion with varlevelsup = 0. The caller is responsible for
1899 * adjusting it if needed.
1900 *
1901 * The varreturningtype is copied onto each individual field Var, so
1902 * that it is handled correctly when we recurse.
1903 */
1904 expandRTE(target_rte,
1905 var->varno, 0 /* not varlevelsup */ ,
1906 var->varreturningtype, var->location,
1907 (var->vartype != RECORDOID),
1908 &colnames, &fields);
1909 rowexpr = makeNode(RowExpr);
1910 /* the fields will be set below */
1911 rowexpr->args = NIL;
1912 rowexpr->row_typeid = var->vartype;
1913 rowexpr->row_format = COERCE_IMPLICIT_CAST;
1914 rowexpr->colnames = (var->vartype == RECORDOID) ? colnames : NIL;
1915 rowexpr->location = var->location;
1916 /* Adjust the generated per-field Vars... */
1917 foreach(lc, fields)
1918 {
1919 Node *field = lfirst(lc);
1920
1921 if (field && IsA(field, Var))
1922 field = ReplaceVarFromTargetList((Var *) field,
1923 target_rte,
1924 targetlist,
1925 result_relation,
1926 nomatch_option,
1927 nomatch_varno);
1928 rowexpr->args = lappend(rowexpr->args, field);
1929 }
1930
1931 /* Wrap it in a ReturningExpr, if needed, per comments above */
1933 {
1935
1936 rexpr->retlevelsup = 0;
1937 rexpr->retold = (var->varreturningtype == VAR_RETURNING_OLD);
1938 rexpr->retexpr = (Expr *) rowexpr;
1939
1940 return (Node *) rexpr;
1941 }
1942
1943 return (Node *) rowexpr;
1944 }
1945
1946 /* Normal case referencing one targetlist element */
1947 tle = get_tle_by_resno(targetlist, var->varattno);
1948
1949 if (tle == NULL || tle->resjunk)
1950 {
1951 /* Failed to find column in targetlist */
1952 switch (nomatch_option)
1953 {
1955 /* fall through, throw error below */
1956 break;
1957
1959 var = copyObject(var);
1960 var->varno = nomatch_varno;
1961 var->varlevelsup = 0;
1962 /* we leave the syntactic referent alone */
1963 return (Node *) var;
1964
1966 {
1967 /*
1968 * If Var is of domain type, we must add a CoerceToDomain
1969 * node, in case there is a NOT NULL domain constraint.
1970 */
1971 int16 vartyplen;
1972 bool vartypbyval;
1973
1974 get_typlenbyval(var->vartype, &vartyplen, &vartypbyval);
1975 return coerce_null_to_domain(var->vartype,
1976 var->vartypmod,
1977 var->varcollid,
1978 vartyplen,
1979 vartypbyval);
1980 }
1981 }
1982 elog(ERROR, "could not find replacement targetlist entry for attno %d",
1983 var->varattno);
1984 return NULL; /* keep compiler quiet */
1985 }
1986 else
1987 {
1988 /* Make a copy of the tlist item to return */
1989 Expr *newnode = copyObject(tle->expr);
1990
1991 /*
1992 * Check to see if the tlist item contains a PARAM_MULTIEXPR Param,
1993 * and throw error if so. This case could only happen when expanding
1994 * an ON UPDATE rule's NEW variable and the referenced tlist item in
1995 * the original UPDATE command is part of a multiple assignment. There
1996 * seems no practical way to handle such cases without multiple
1997 * evaluation of the multiple assignment's sub-select, which would
1998 * create semantic oddities that users of rules would probably prefer
1999 * not to cope with. So treat it as an unimplemented feature.
2000 */
2001 if (contains_multiexpr_param((Node *) newnode, NULL))
2002 ereport(ERROR,
2003 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2004 errmsg("NEW variables in ON UPDATE rules cannot reference columns that are part of a multiple assignment in the subject UPDATE command")));
2005
2006 /* Handle any OLD/NEW RETURNING list Vars */
2008 {
2009 /*
2010 * Copy varreturningtype onto any Vars in the tlist item that
2011 * refer to result_relation (which had better be non-zero).
2012 */
2013 if (result_relation == 0)
2014 elog(ERROR, "variable returning old/new found outside RETURNING list");
2015
2016 SetVarReturningType((Node *) newnode, result_relation,
2017 0, var->varreturningtype);
2018
2019 /* Wrap it in a ReturningExpr, if needed, per comments above */
2020 if (!IsA(newnode, Var) ||
2021 ((Var *) newnode)->varno != result_relation ||
2022 ((Var *) newnode)->varlevelsup != 0)
2023 {
2025
2026 rexpr->retlevelsup = 0;
2027 rexpr->retold = (var->varreturningtype == VAR_RETURNING_OLD);
2028 rexpr->retexpr = newnode;
2029
2030 newnode = (Expr *) rexpr;
2031 }
2032 }
2033
2034 return (Node *) newnode;
2035 }
2036}
#define InvalidAttrNumber
Definition: attnum.h:23
int16_t int16
Definition: c.h:497
List * lappend(List *list, void *datum)
Definition: list.c:339
void get_typlenbyval(Oid typid, int16 *typlen, bool *typbyval)
Definition: lsyscache.c:2391
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:257
@ VAR_RETURNING_DEFAULT
Definition: primnodes.h:256
@ COERCE_IMPLICIT_CAST
Definition: primnodes.h:753
static bool contains_multiexpr_param(Node *node, void *context)
Definition: rewriteManip.c:321
static void SetVarReturningType(Node *node, int result_relation, int sublevels_up, VarReturningType returning_type)
Node * ReplaceVarFromTargetList(Var *var, RangeTblEntry *target_rte, List *targetlist, int result_relation, ReplaceVarsNoMatchOption nomatch_option, int nomatch_varno)
Definition: pg_list.h:54
Expr * retexpr
Definition: primnodes.h:2157
List * args
Definition: primnodes.h:1428
ParseLoc location
Definition: primnodes.h:1452
Expr * expr
Definition: primnodes.h:2219
ParseLoc location
Definition: primnodes.h:310
AttrNumber varattno
Definition: primnodes.h:274
VarReturningType varreturningtype
Definition: primnodes.h:297

References RowExpr::args, COERCE_IMPLICIT_CAST, coerce_null_to_domain(), contains_multiexpr_param(), copyObject, elog, ereport, errcode(), errmsg(), ERROR, expandRTE(), TargetEntry::expr, 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::varlevelsup, 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 
)

Definition at line 2039 of file rewriteManip.c.

2047{
2049
2050 context.target_rte = target_rte;
2051 context.targetlist = targetlist;
2052 context.result_relation = result_relation;
2053 context.nomatch_option = nomatch_option;
2054 context.nomatch_varno = nomatch_varno;
2055
2056 return replace_rte_variables(node, target_varno, sublevels_up,
2058 &context,
2059 outer_hasSubLinks);
2060}
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

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