PostgreSQL Source Code git master
Loading...
Searching...
No Matches
rewriteManip.c File Reference
#include "postgres.h"
#include "access/attmap.h"
#include "catalog/pg_type.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "nodes/pathnodes.h"
#include "nodes/plannodes.h"
#include "parser/parse_coerce.h"
#include "parser/parse_relation.h"
#include "parser/parsetree.h"
#include "rewrite/rewriteManip.h"
#include "utils/lsyscache.h"
Include dependency graph for rewriteManip.c:

Go to the source code of this file.

Data Structures

struct  contain_aggs_of_level_context
 
struct  locate_agg_of_level_context
 
struct  locate_windowfunc_context
 
struct  add_nulling_relids_context
 
struct  remove_nulling_relids_context
 
struct  OffsetVarNodes_context
 
struct  IncrementVarSublevelsUp_context
 
struct  SetVarReturningType_context
 
struct  rangeTableEntry_used_context
 
struct  map_variable_attnos_context
 
struct  ReplaceVarsFromTargetList_context
 

Functions

static bool contain_aggs_of_level_walker (Node *node, contain_aggs_of_level_context *context)
 
static bool locate_agg_of_level_walker (Node *node, locate_agg_of_level_context *context)
 
static bool contain_windowfuncs_walker (Node *node, void *context)
 
static bool locate_windowfunc_walker (Node *node, locate_windowfunc_context *context)
 
static bool checkExprHasSubLink_walker (Node *node, void *context)
 
static Relids offset_relid_set (Relids relids, int offset)
 
static Nodeadd_nulling_relids_mutator (Node *node, add_nulling_relids_context *context)
 
static Noderemove_nulling_relids_mutator (Node *node, remove_nulling_relids_context *context)
 
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)
 
static bool contains_multiexpr_param (Node *node, void *context)
 
void CombineRangeTables (List **dst_rtable, List **dst_perminfos, List *src_rtable, List *src_perminfos)
 
static bool OffsetVarNodes_walker (Node *node, OffsetVarNodes_context *context)
 
void OffsetVarNodes (Node *node, int offset, int sublevels_up)
 
static bool ChangeVarNodes_walker (Node *node, ChangeVarNodes_context *context)
 
void ChangeVarNodesExtended (Node *node, int rt_index, int new_index, int sublevels_up, ChangeVarNodes_callback callback)
 
void ChangeVarNodes (Node *node, int rt_index, int new_index, int sublevels_up)
 
bool ChangeVarNodesWalkExpression (Node *node, ChangeVarNodes_context *context)
 
Relids adjust_relid_set (Relids relids, int oldrelid, int newrelid)
 
static bool IncrementVarSublevelsUp_walker (Node *node, IncrementVarSublevelsUp_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)
 
static bool SetVarReturningType_walker (Node *node, SetVarReturningType_context *context)
 
static void SetVarReturningType (Node *node, int result_relation, int sublevels_up, VarReturningType returning_type)
 
static bool rangeTableEntry_used_walker (Node *node, rangeTableEntry_used_context *context)
 
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)
 
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)
 
static Nodemap_variable_attnos_mutator (Node *node, map_variable_attnos_context *context)
 
Nodemap_variable_attnos (Node *node, int target_varno, int sublevels_up, const AttrMap *attno_map, Oid to_rowtype, bool *found_whole_row)
 
static NodeReplaceVarsFromTargetList_callback (const Var *var, replace_rte_variables_context *context)
 
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)
 

Function Documentation

◆ add_nulling_relids()

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

Definition at line 1241 of file rewriteManip.c.

1244{
1246
1247 context.target_relids = target_relids;
1248 context.added_relids = added_relids;
1249 context.sublevels_up = 0;
1252 &context,
1253 0);
1254}
#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().

◆ add_nulling_relids_mutator()

static Node * add_nulling_relids_mutator ( Node node,
add_nulling_relids_context context 
)
static

Definition at line 1257 of file rewriteManip.c.

1259{
1260 if (node == NULL)
1261 return NULL;
1262 if (IsA(node, Var))
1263 {
1264 Var *var = (Var *) node;
1265
1266 if (var->varlevelsup == context->sublevels_up &&
1267 (context->target_relids == NULL ||
1268 bms_is_member(var->varno, context->target_relids)))
1269 {
1270 Relids newnullingrels = bms_union(var->varnullingrels,
1271 context->added_relids);
1272
1273 /* Copy the Var ... */
1274 var = copyObject(var);
1275 /* ... and replace the copy's varnullingrels field */
1276 var->varnullingrels = newnullingrels;
1277 return (Node *) var;
1278 }
1279 /* Otherwise fall through to copy the Var normally */
1280 }
1281 else if (IsA(node, PlaceHolderVar))
1282 {
1283 PlaceHolderVar *phv = (PlaceHolderVar *) node;
1284
1285 if (phv->phlevelsup == context->sublevels_up &&
1286 (context->target_relids == NULL ||
1287 bms_overlap(phv->phrels, context->target_relids)))
1288 {
1289 Relids newnullingrels = bms_union(phv->phnullingrels,
1290 context->added_relids);
1291
1292 /*
1293 * We don't modify the contents of the PHV's expression, only add
1294 * to phnullingrels. This corresponds to assuming that the PHV
1295 * will be evaluated at the same level as before, then perhaps be
1296 * nulled as it bubbles up. Hence, just flat-copy the node ...
1297 */
1299 memcpy(phv, node, sizeof(PlaceHolderVar));
1300 /* ... and replace the copy's phnullingrels field */
1301 phv->phnullingrels = newnullingrels;
1302 return (Node *) phv;
1303 }
1304 /* Otherwise fall through to copy the PlaceHolderVar normally */
1305 }
1306 else if (IsA(node, Query))
1307 {
1308 /* Recurse into RTE or sublink subquery */
1309 Query *newnode;
1310
1311 context->sublevels_up++;
1312 newnode = query_tree_mutator((Query *) node,
1314 context,
1315 0);
1316 context->sublevels_up--;
1317 return (Node *) newnode;
1318 }
1320}
bool bms_is_member(int x, const Bitmapset *a)
Definition bitmapset.c:510
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition bitmapset.c:251
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition bitmapset.c:575
#define expression_tree_mutator(n, m, c)
Definition nodeFuncs.h:155
#define query_tree_mutator(q, m, c, f)
Definition nodeFuncs.h:160
#define IsA(nodeptr, _type_)
Definition nodes.h:164
#define copyObject(obj)
Definition nodes.h:232
#define makeNode(_type_)
Definition nodes.h:161
static int fb(int x)
Definition nodes.h:135
int varno
Definition primnodes.h:270
Index varlevelsup
Definition primnodes.h:295

References add_nulling_relids_mutator(), add_nulling_relids_context::added_relids, bms_is_member(), bms_overlap(), bms_union(), copyObject, expression_tree_mutator, fb(), IsA, makeNode, query_tree_mutator, add_nulling_relids_context::sublevels_up, add_nulling_relids_context::target_relids, Var::varlevelsup, and Var::varno.

Referenced by add_nulling_relids(), and add_nulling_relids_mutator().

◆ AddInvertedQual()

void AddInvertedQual ( Query parsetree,
Node qual 
)

Definition at line 1217 of file rewriteManip.c.

1218{
1220
1221 if (qual == NULL)
1222 return;
1223
1224 /* Need not copy input qual, because AddQual will... */
1226 invqual->arg = (Expr *) qual;
1227 invqual->booltesttype = IS_NOT_TRUE;
1228 invqual->location = -1;
1229
1230 AddQual(parsetree, (Node *) invqual);
1231}
@ IS_NOT_TRUE
Definition primnodes.h:2003
void AddQual(Query *parsetree, Node *qual)

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

Referenced by CopyAndAddInvertedQual().

◆ AddQual()

void AddQual ( Query parsetree,
Node qual 
)

Definition at line 1149 of file rewriteManip.c.

1150{
1151 Node *copy;
1152
1153 if (qual == NULL)
1154 return;
1155
1156 if (parsetree->commandType == CMD_UTILITY)
1157 {
1158 /*
1159 * There's noplace to put the qual on a utility statement.
1160 *
1161 * If it's a NOTIFY, silently ignore the qual; this means that the
1162 * NOTIFY will execute, whether or not there are any qualifying rows.
1163 * While clearly wrong, this is much more useful than refusing to
1164 * execute the rule at all, and extra NOTIFY events are harmless for
1165 * typical uses of NOTIFY.
1166 *
1167 * If it isn't a NOTIFY, error out, since unconditional execution of
1168 * other utility stmts is unlikely to be wanted. (This case is not
1169 * currently allowed anyway, but keep the test for safety.)
1170 */
1171 if (parsetree->utilityStmt && IsA(parsetree->utilityStmt, NotifyStmt))
1172 return;
1173 else
1174 ereport(ERROR,
1176 errmsg("conditional utility statements are not implemented")));
1177 }
1178
1179 if (parsetree->setOperations != NULL)
1180 {
1181 /*
1182 * There's noplace to put the qual on a setop statement, either. (This
1183 * could be fixed, but right now the planner simply ignores any qual
1184 * condition on a setop query.)
1185 */
1186 ereport(ERROR,
1188 errmsg("conditional UNION/INTERSECT/EXCEPT statements are not implemented")));
1189 }
1190
1191 /* INTERSECT wants the original, but we need to copy - Jan */
1192 copy = copyObject(qual);
1193
1194 parsetree->jointree->quals = make_and_qual(parsetree->jointree->quals,
1195 copy);
1196
1197 /*
1198 * We had better not have stuck an aggregate into the WHERE clause.
1199 */
1201
1202 /*
1203 * Make sure query is marked correctly if added qual has sublinks. Need
1204 * not search qual when query is already marked.
1205 */
1206 if (!parsetree->hasSubLinks)
1207 parsetree->hasSubLinks = checkExprHasSubLink(copy);
1208}
#define Assert(condition)
Definition c.h:945
int errcode(int sqlerrcode)
Definition elog.c:874
#define ERROR
Definition elog.h:39
#define ereport(elevel,...)
Definition elog.h:150
Node * make_and_qual(Node *qual1, Node *qual2)
Definition makefuncs.c:780
@ 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:2384
FromExpr * jointree
Definition parsenodes.h:182
Node * setOperations
Definition parsenodes.h:236
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(), rewriteRuleAction(), and rewriteTargetView().

◆ adjust_relid_set()

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

Definition at line 762 of file rewriteManip.c.

763{
765 {
766 /* Ensure we have a modifiable copy */
767 relids = bms_copy(relids);
768 /* Remove old, add new */
769 relids = bms_del_member(relids, oldrelid);
771 relids = bms_add_member(relids, newrelid);
772 }
773 return relids;
774}
Bitmapset * bms_del_member(Bitmapset *a, int x)
Definition bitmapset.c:852
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()

◆ ChangeVarNodes_walker()

static bool ChangeVarNodes_walker ( Node node,
ChangeVarNodes_context context 
)
static

Definition at line 553 of file rewriteManip.c.

554{
555 if (node == NULL)
556 return false;
557
558 if (context->callback && context->callback(node, context))
559 return false;
560
561 if (IsA(node, Var))
562 {
563 Var *var = (Var *) node;
564
565 if (var->varlevelsup == context->sublevels_up)
566 {
567 if (var->varno == context->rt_index)
568 var->varno = context->new_index;
569 var->varnullingrels = adjust_relid_set(var->varnullingrels,
570 context->rt_index,
571 context->new_index);
572 if (var->varnosyn == context->rt_index)
573 var->varnosyn = context->new_index;
574 }
575 return false;
576 }
577 if (IsA(node, CurrentOfExpr))
578 {
579 CurrentOfExpr *cexpr = (CurrentOfExpr *) node;
580
581 if (context->sublevels_up == 0 &&
582 cexpr->cvarno == context->rt_index)
583 cexpr->cvarno = context->new_index;
584 return false;
585 }
586 if (IsA(node, RangeTblRef))
587 {
588 RangeTblRef *rtr = (RangeTblRef *) node;
589
590 if (context->sublevels_up == 0 &&
591 rtr->rtindex == context->rt_index)
592 rtr->rtindex = context->new_index;
593 /* the subquery itself is visited separately */
594 return false;
595 }
596 if (IsA(node, JoinExpr))
597 {
598 JoinExpr *j = (JoinExpr *) node;
599
600 if (context->sublevels_up == 0 &&
601 j->rtindex == context->rt_index)
602 j->rtindex = context->new_index;
603 /* fall through to examine children */
604 }
605 if (IsA(node, PlaceHolderVar))
606 {
608
609 if (phv->phlevelsup == context->sublevels_up)
610 {
611 phv->phrels = adjust_relid_set(phv->phrels,
612 context->rt_index,
613 context->new_index);
614 phv->phnullingrels = adjust_relid_set(phv->phnullingrels,
615 context->rt_index,
616 context->new_index);
617 }
618 /* fall through to examine children */
619 }
620 if (IsA(node, PlanRowMark))
621 {
622 PlanRowMark *rowmark = (PlanRowMark *) node;
623
624 if (context->sublevels_up == 0)
625 {
626 if (rowmark->rti == context->rt_index)
627 rowmark->rti = context->new_index;
628 if (rowmark->prti == context->rt_index)
629 rowmark->prti = context->new_index;
630 }
631 return false;
632 }
633 if (IsA(node, AppendRelInfo))
634 {
636
637 if (context->sublevels_up == 0)
638 {
639 if (appinfo->parent_relid == context->rt_index)
640 appinfo->parent_relid = context->new_index;
641 if (appinfo->child_relid == context->rt_index)
642 appinfo->child_relid = context->new_index;
643 }
644 /* fall through to examine children */
645 }
646 /* Shouldn't need to handle other planner auxiliary nodes here */
647 Assert(!IsA(node, SpecialJoinInfo));
648 Assert(!IsA(node, PlaceHolderInfo));
649 Assert(!IsA(node, MinMaxAggInfo));
650
651 if (IsA(node, Query))
652 {
653 /* Recurse into subselects */
654 bool result;
655
656 context->sublevels_up++;
658 context, 0);
659 context->sublevels_up--;
660 return result;
661 }
662 return expression_tree_walker(node, ChangeVarNodes_walker, context);
663}
int j
Definition isn.c:78
#define query_tree_walker(q, w, c, f)
Definition nodeFuncs.h:158
#define expression_tree_walker(n, w, c)
Definition nodeFuncs.h:153
Relids adjust_relid_set(Relids relids, int oldrelid, int newrelid)
static bool ChangeVarNodes_walker(Node *node, ChangeVarNodes_context *context)
Index parent_relid
Definition pathnodes.h:3288
ChangeVarNodes_callback callback
int rtindex
Definition primnodes.h:2368

References adjust_relid_set(), Assert, ChangeVarNodes_context::callback, ChangeVarNodes_walker(), CurrentOfExpr::cvarno, expression_tree_walker, fb(), IsA, j, ChangeVarNodes_context::new_index, PlanRowMark::prti, query_tree_walker, ChangeVarNodes_context::rt_index, PlanRowMark::rti, ChangeVarNodes_context::sublevels_up, Var::varlevelsup, and Var::varno.

Referenced by ChangeVarNodes_walker(), ChangeVarNodesExtended(), and ChangeVarNodesWalkExpression().

◆ ChangeVarNodesExtended()

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

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 lfirst(lc)
Definition pg_list.h:172
List * rowMarks
Definition parsenodes.h:234
OnConflictExpr * onConflict
Definition parsenodes.h:203
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 
)

Definition at line 745 of file rewriteManip.c.

746{
747 return expression_tree_walker(node,
749 (void *) context);
750}

References ChangeVarNodes_walker(), and expression_tree_walker.

Referenced by replace_relid_callback().

◆ checkExprHasSubLink()

bool checkExprHasSubLink ( Node node)

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

◆ checkExprHasSubLink_walker()

static bool checkExprHasSubLink_walker ( Node node,
void context 
)
static

Definition at line 306 of file rewriteManip.c.

307{
308 if (node == NULL)
309 return false;
310 if (IsA(node, SubLink))
311 return true; /* abort the tree traversal and return true */
313}

References checkExprHasSubLink_walker(), expression_tree_walker, fb(), and IsA.

Referenced by checkExprHasSubLink(), and checkExprHasSubLink_walker().

◆ CombineRangeTables()

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

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 
)

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

static bool contain_aggs_of_level_walker ( Node node,
contain_aggs_of_level_context context 
)
static

Definition at line 103 of file rewriteManip.c.

105{
106 if (node == NULL)
107 return false;
108 if (IsA(node, Aggref))
109 {
110 if (((Aggref *) node)->agglevelsup == context->sublevels_up)
111 return true; /* abort the tree traversal and return true */
112 /* else fall through to examine argument */
113 }
114 if (IsA(node, GroupingFunc))
115 {
116 if (((GroupingFunc *) node)->agglevelsup == context->sublevels_up)
117 return true;
118 /* else fall through to examine argument */
119 }
120 if (IsA(node, Query))
121 {
122 /* Recurse into subselects */
123 bool result;
124
125 context->sublevels_up++;
126 result = query_tree_walker((Query *) node,
128 context, 0);
129 context->sublevels_up--;
130 return result;
131 }
133 context);
134}

References contain_aggs_of_level_walker(), expression_tree_walker, fb(), IsA, query_tree_walker, and contain_aggs_of_level_context::sublevels_up.

Referenced by contain_aggs_of_level(), and contain_aggs_of_level_walker().

◆ contain_windowfuncs()

bool contain_windowfuncs ( Node node)

Definition at line 215 of file rewriteManip.c.

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

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

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

◆ contain_windowfuncs_walker()

static bool contain_windowfuncs_walker ( Node node,
void context 
)
static

Definition at line 228 of file rewriteManip.c.

229{
230 if (node == NULL)
231 return false;
232 if (IsA(node, WindowFunc))
233 return true; /* abort the tree traversal and return true */
234 /* Mustn't recurse into subselects */
236}

References contain_windowfuncs_walker(), expression_tree_walker, fb(), and IsA.

Referenced by contain_windowfuncs(), and contain_windowfuncs_walker().

◆ contains_multiexpr_param()

static bool contains_multiexpr_param ( Node node,
void context 
)
static

Definition at line 322 of file rewriteManip.c.

323{
324 if (node == NULL)
325 return false;
326 if (IsA(node, Param))
327 {
328 if (((Param *) node)->paramkind == PARAM_MULTIEXPR)
329 return true; /* abort the tree traversal and return true */
330 return false;
331 }
333}
@ PARAM_MULTIEXPR
Definition primnodes.h:388
static bool contains_multiexpr_param(Node *node, void *context)

References contains_multiexpr_param(), expression_tree_walker, fb(), IsA, and PARAM_MULTIEXPR.

Referenced by contains_multiexpr_param(), and ReplaceVarFromTargetList().

◆ getInsertSelectQuery()

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

Definition at line 1091 of file rewriteManip.c.

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

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 
)

Definition at line 882 of file rewriteManip.c.

884{
886
887 context.delta_sublevels_up = delta_sublevels_up;
888 context.min_sublevels_up = min_sublevels_up;
889
890 /*
891 * Must be prepared to start with a Query or a bare expression tree; if
892 * it's a Query, we don't want to increment sublevels_up.
893 */
896 &context,
898}
#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 
)

Definition at line 905 of file rewriteManip.c.

907{
909
910 context.delta_sublevels_up = delta_sublevels_up;
911 context.min_sublevels_up = min_sublevels_up;
912
913 range_table_walker(rtable,
915 &context,
917}
#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().

◆ IncrementVarSublevelsUp_walker()

static bool IncrementVarSublevelsUp_walker ( Node node,
IncrementVarSublevelsUp_context context 
)
static

Definition at line 802 of file rewriteManip.c.

804{
805 if (node == NULL)
806 return false;
807 if (IsA(node, Var))
808 {
809 Var *var = (Var *) node;
810
811 if (var->varlevelsup >= context->min_sublevels_up)
812 var->varlevelsup += context->delta_sublevels_up;
813 return false; /* done here */
814 }
815 if (IsA(node, CurrentOfExpr))
816 {
817 /* this should not happen */
818 if (context->min_sublevels_up == 0)
819 elog(ERROR, "cannot push down CurrentOfExpr");
820 return false;
821 }
822 if (IsA(node, Aggref))
823 {
824 Aggref *agg = (Aggref *) node;
825
826 if (agg->agglevelsup >= context->min_sublevels_up)
827 agg->agglevelsup += context->delta_sublevels_up;
828 /* fall through to recurse into argument */
829 }
830 if (IsA(node, GroupingFunc))
831 {
832 GroupingFunc *grp = (GroupingFunc *) node;
833
834 if (grp->agglevelsup >= context->min_sublevels_up)
836 /* fall through to recurse into argument */
837 }
838 if (IsA(node, PlaceHolderVar))
839 {
841
842 if (phv->phlevelsup >= context->min_sublevels_up)
843 phv->phlevelsup += context->delta_sublevels_up;
844 /* fall through to recurse into argument */
845 }
846 if (IsA(node, ReturningExpr))
847 {
848 ReturningExpr *rexpr = (ReturningExpr *) node;
849
850 if (rexpr->retlevelsup >= context->min_sublevels_up)
851 rexpr->retlevelsup += context->delta_sublevels_up;
852 /* fall through to recurse into argument */
853 }
854 if (IsA(node, RangeTblEntry))
855 {
856 RangeTblEntry *rte = (RangeTblEntry *) node;
857
858 if (rte->rtekind == RTE_CTE)
859 {
860 if (rte->ctelevelsup >= context->min_sublevels_up)
862 }
863 return false; /* allow range_table_walker to continue */
864 }
865 if (IsA(node, Query))
866 {
867 /* Recurse into subselects */
868 bool result;
869
870 context->min_sublevels_up++;
871 result = query_tree_walker((Query *) node,
873 context,
875 context->min_sublevels_up--;
876 return result;
877 }
879}
@ RTE_CTE
Index agglevelsup
Definition primnodes.h:571

References IncrementVarSublevelsUp_context::delta_sublevels_up, elog, ERROR, expression_tree_walker, fb(), IncrementVarSublevelsUp_walker(), IsA, IncrementVarSublevelsUp_context::min_sublevels_up, QTW_EXAMINE_RTES_BEFORE, query_tree_walker, ReturningExpr::retlevelsup, RTE_CTE, and Var::varlevelsup.

Referenced by IncrementVarSublevelsUp(), IncrementVarSublevelsUp_rtable(), and IncrementVarSublevelsUp_walker().

◆ locate_agg_of_level()

int locate_agg_of_level ( Node node,
int  levelsup 
)

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

static bool locate_agg_of_level_walker ( Node node,
locate_agg_of_level_context context 
)
static

Definition at line 170 of file rewriteManip.c.

172{
173 if (node == NULL)
174 return false;
175 if (IsA(node, Aggref))
176 {
177 if (((Aggref *) node)->agglevelsup == context->sublevels_up &&
178 ((Aggref *) node)->location >= 0)
179 {
180 context->agg_location = ((Aggref *) node)->location;
181 return true; /* abort the tree traversal and return true */
182 }
183 /* else fall through to examine argument */
184 }
185 if (IsA(node, GroupingFunc))
186 {
187 if (((GroupingFunc *) node)->agglevelsup == context->sublevels_up &&
188 ((GroupingFunc *) node)->location >= 0)
189 {
190 context->agg_location = ((GroupingFunc *) node)->location;
191 return true; /* abort the tree traversal and return true */
192 }
193 }
194 if (IsA(node, Query))
195 {
196 /* Recurse into subselects */
197 bool result;
198
199 context->sublevels_up++;
200 result = query_tree_walker((Query *) node,
202 context, 0);
203 context->sublevels_up--;
204 return result;
205 }
207}

References locate_agg_of_level_context::agg_location, expression_tree_walker, fb(), IsA, locate_agg_of_level_walker(), query_tree_walker, and locate_agg_of_level_context::sublevels_up.

Referenced by locate_agg_of_level(), and locate_agg_of_level_walker().

◆ locate_windowfunc()

int locate_windowfunc ( Node node)

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

◆ locate_windowfunc_walker()

static bool locate_windowfunc_walker ( Node node,
locate_windowfunc_context context 
)
static

Definition at line 271 of file rewriteManip.c.

272{
273 if (node == NULL)
274 return false;
275 if (IsA(node, WindowFunc))
276 {
277 if (((WindowFunc *) node)->location >= 0)
278 {
279 context->win_location = ((WindowFunc *) node)->location;
280 return true; /* abort the tree traversal and return true */
281 }
282 /* else fall through to examine argument */
283 }
284 /* Mustn't recurse into subselects */
286}

References expression_tree_walker, fb(), IsA, locate_windowfunc_walker(), and locate_windowfunc_context::win_location.

Referenced by locate_windowfunc(), and locate_windowfunc_walker().

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

Definition at line 1703 of file rewriteManip.c.

1707{
1709
1710 context.target_varno = target_varno;
1711 context.sublevels_up = sublevels_up;
1712 context.attno_map = attno_map;
1713 context.to_rowtype = to_rowtype;
1714 context.found_whole_row = found_whole_row;
1715
1716 *found_whole_row = false;
1717
1718 /*
1719 * Must be prepared to start with a Query or a bare expression tree; if
1720 * it's a Query, we don't want to increment sublevels_up.
1721 */
1724 &context,
1725 0);
1726}
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().

◆ map_variable_attnos_mutator()

static Node * map_variable_attnos_mutator ( Node node,
map_variable_attnos_context context 
)
static

Definition at line 1581 of file rewriteManip.c.

1583{
1584 if (node == NULL)
1585 return NULL;
1586 if (IsA(node, Var))
1587 {
1588 Var *var = (Var *) node;
1589
1590 if (var->varno == context->target_varno &&
1591 var->varlevelsup == context->sublevels_up)
1592 {
1593 /* Found a matching variable, make the substitution */
1595 int attno = var->varattno;
1596
1597 *newvar = *var; /* initially copy all fields of the Var */
1598
1599 if (attno > 0)
1600 {
1601 /* user-defined column, replace attno */
1602 if (attno > context->attno_map->maplen ||
1603 context->attno_map->attnums[attno - 1] == 0)
1604 elog(ERROR, "unexpected varattno %d in expression to be mapped",
1605 attno);
1606 newvar->varattno = context->attno_map->attnums[attno - 1];
1607 /* If the syntactic referent is same RTE, fix it too */
1608 if (newvar->varnosyn == context->target_varno)
1609 newvar->varattnosyn = newvar->varattno;
1610 }
1611 else if (attno == 0)
1612 {
1613 /* whole-row variable, warn caller */
1614 *(context->found_whole_row) = true;
1615
1616 /* If the caller expects us to convert the Var, do so. */
1617 if (OidIsValid(context->to_rowtype) &&
1618 context->to_rowtype != var->vartype)
1619 {
1621
1622 /* This certainly won't work for a RECORD variable. */
1623 Assert(var->vartype != RECORDOID);
1624
1625 /* Var itself is changed to the requested type. */
1626 newvar->vartype = context->to_rowtype;
1627
1628 /*
1629 * Add a conversion node on top to convert back to the
1630 * original type expected by the expression.
1631 */
1633 r->arg = (Expr *) newvar;
1634 r->resulttype = var->vartype;
1635 r->convertformat = COERCE_IMPLICIT_CAST;
1636 r->location = -1;
1637
1638 return (Node *) r;
1639 }
1640 }
1641 return (Node *) newvar;
1642 }
1643 /* otherwise fall through to copy the var normally */
1644 }
1645 else if (IsA(node, ConvertRowtypeExpr))
1646 {
1648 Var *var = (Var *) r->arg;
1649
1650 /*
1651 * If this is coercing a whole-row Var that we need to convert, then
1652 * just convert the Var without adding an extra ConvertRowtypeExpr.
1653 * Effectively we're simplifying var::parenttype::grandparenttype into
1654 * just var::grandparenttype. This avoids building stacks of CREs if
1655 * this function is applied repeatedly.
1656 */
1657 if (IsA(var, Var) &&
1658 var->varno == context->target_varno &&
1659 var->varlevelsup == context->sublevels_up &&
1660 var->varattno == 0 &&
1661 OidIsValid(context->to_rowtype) &&
1662 context->to_rowtype != var->vartype)
1663 {
1666
1667 /* whole-row variable, warn caller */
1668 *(context->found_whole_row) = true;
1669
1670 *newvar = *var; /* initially copy all fields of the Var */
1671
1672 /* This certainly won't work for a RECORD variable. */
1673 Assert(var->vartype != RECORDOID);
1674
1675 /* Var itself is changed to the requested type. */
1676 newvar->vartype = context->to_rowtype;
1677
1679 *newnode = *r; /* initially copy all fields of the CRE */
1680 newnode->arg = (Expr *) newvar;
1681
1682 return (Node *) newnode;
1683 }
1684 /* otherwise fall through to process the expression normally */
1685 }
1686 else if (IsA(node, Query))
1687 {
1688 /* Recurse into RTE subquery or not-yet-planned sublink subquery */
1689 Query *newnode;
1690
1691 context->sublevels_up++;
1692 newnode = query_tree_mutator((Query *) node,
1694 context,
1695 0);
1696 context->sublevels_up--;
1697 return (Node *) newnode;
1698 }
1700}
#define OidIsValid(objectId)
Definition c.h:860
#define palloc_object(type)
Definition fe_memutils.h:74
@ COERCE_IMPLICIT_CAST
Definition primnodes.h:769
int maplen
Definition attmap.h:37
AttrNumber * attnums
Definition attmap.h:36
AttrNumber varattno
Definition primnodes.h:275

References ConvertRowtypeExpr::arg, Assert, map_variable_attnos_context::attno_map, AttrMap::attnums, COERCE_IMPLICIT_CAST, elog, ERROR, expression_tree_mutator, fb(), map_variable_attnos_context::found_whole_row, IsA, ConvertRowtypeExpr::location, makeNode, map_variable_attnos_mutator(), AttrMap::maplen, OidIsValid, palloc_object, query_tree_mutator, ConvertRowtypeExpr::resulttype, map_variable_attnos_context::sublevels_up, map_variable_attnos_context::target_varno, map_variable_attnos_context::to_rowtype, Var::varattno, Var::varlevelsup, and Var::varno.

Referenced by map_variable_attnos(), and map_variable_attnos_mutator().

◆ offset_relid_set()

static Relids offset_relid_set ( Relids  relids,
int  offset 
)
static

Definition at line 528 of file rewriteManip.c.

529{
530 Relids result = NULL;
531 int rtindex;
532
533 rtindex = -1;
534 while ((rtindex = bms_next_member(relids, rtindex)) >= 0)
535 result = bms_add_member(result, rtindex + offset);
536 return result;
537}
int bms_next_member(const Bitmapset *a, int prevbit)
Definition bitmapset.c:1290

References bms_add_member(), bms_next_member(), and fb().

Referenced by OffsetVarNodes_walker().

◆ OffsetVarNodes()

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

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

◆ OffsetVarNodes_walker()

static bool OffsetVarNodes_walker ( Node node,
OffsetVarNodes_context context 
)
static

Definition at line 389 of file rewriteManip.c.

390{
391 if (node == NULL)
392 return false;
393 if (IsA(node, Var))
394 {
395 Var *var = (Var *) node;
396
397 if (var->varlevelsup == context->sublevels_up)
398 {
399 var->varno += context->offset;
400 var->varnullingrels = offset_relid_set(var->varnullingrels,
401 context->offset);
402 if (var->varnosyn > 0)
403 var->varnosyn += context->offset;
404 }
405 return false;
406 }
407 if (IsA(node, CurrentOfExpr))
408 {
409 CurrentOfExpr *cexpr = (CurrentOfExpr *) node;
410
411 if (context->sublevels_up == 0)
412 cexpr->cvarno += context->offset;
413 return false;
414 }
415 if (IsA(node, RangeTblRef))
416 {
417 RangeTblRef *rtr = (RangeTblRef *) node;
418
419 if (context->sublevels_up == 0)
420 rtr->rtindex += context->offset;
421 /* the subquery itself is visited separately */
422 return false;
423 }
424 if (IsA(node, JoinExpr))
425 {
426 JoinExpr *j = (JoinExpr *) node;
427
428 if (j->rtindex && context->sublevels_up == 0)
429 j->rtindex += context->offset;
430 /* fall through to examine children */
431 }
432 if (IsA(node, PlaceHolderVar))
433 {
435
436 if (phv->phlevelsup == context->sublevels_up)
437 {
438 phv->phrels = offset_relid_set(phv->phrels,
439 context->offset);
440 phv->phnullingrels = offset_relid_set(phv->phnullingrels,
441 context->offset);
442 }
443 /* fall through to examine children */
444 }
445 if (IsA(node, AppendRelInfo))
446 {
448
449 if (context->sublevels_up == 0)
450 {
451 appinfo->parent_relid += context->offset;
452 appinfo->child_relid += context->offset;
453 }
454 /* fall through to examine children */
455 }
456 /* Shouldn't need to handle other planner auxiliary nodes here */
457 Assert(!IsA(node, PlanRowMark));
458 Assert(!IsA(node, SpecialJoinInfo));
459 Assert(!IsA(node, PlaceHolderInfo));
460 Assert(!IsA(node, MinMaxAggInfo));
461
462 if (IsA(node, Query))
463 {
464 /* Recurse into subselects */
465 bool result;
466
467 context->sublevels_up++;
469 context, 0);
470 context->sublevels_up--;
471 return result;
472 }
473 return expression_tree_walker(node, OffsetVarNodes_walker, context);
474}
static Relids offset_relid_set(Relids relids, int offset)

References Assert, CurrentOfExpr::cvarno, expression_tree_walker, fb(), IsA, j, OffsetVarNodes_context::offset, offset_relid_set(), OffsetVarNodes_walker(), query_tree_walker, OffsetVarNodes_context::sublevels_up, Var::varlevelsup, and Var::varno.

Referenced by OffsetVarNodes(), and OffsetVarNodes_walker().

◆ rangeTableEntry_used()

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

Definition at line 1059 of file rewriteManip.c.

1060{
1062
1063 context.rt_index = rt_index;
1064 context.sublevels_up = sublevels_up;
1065
1066 /*
1067 * Must be prepared to start with a Query or a bare expression tree; if
1068 * it's a Query, we don't want to increment sublevels_up.
1069 */
1072 &context,
1073 0);
1074}
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().

◆ rangeTableEntry_used_walker()

static bool rangeTableEntry_used_walker ( Node node,
rangeTableEntry_used_context context 
)
static

Definition at line 993 of file rewriteManip.c.

995{
996 if (node == NULL)
997 return false;
998 if (IsA(node, Var))
999 {
1000 Var *var = (Var *) node;
1001
1002 if (var->varlevelsup == context->sublevels_up &&
1003 (var->varno == context->rt_index ||
1004 bms_is_member(context->rt_index, var->varnullingrels)))
1005 return true;
1006 return false;
1007 }
1008 if (IsA(node, CurrentOfExpr))
1009 {
1010 CurrentOfExpr *cexpr = (CurrentOfExpr *) node;
1011
1012 if (context->sublevels_up == 0 &&
1013 cexpr->cvarno == context->rt_index)
1014 return true;
1015 return false;
1016 }
1017 if (IsA(node, RangeTblRef))
1018 {
1019 RangeTblRef *rtr = (RangeTblRef *) node;
1020
1021 if (rtr->rtindex == context->rt_index &&
1022 context->sublevels_up == 0)
1023 return true;
1024 /* the subquery itself is visited separately */
1025 return false;
1026 }
1027 if (IsA(node, JoinExpr))
1028 {
1029 JoinExpr *j = (JoinExpr *) node;
1030
1031 if (j->rtindex == context->rt_index &&
1032 context->sublevels_up == 0)
1033 return true;
1034 /* fall through to examine children */
1035 }
1036 /* Shouldn't need to handle planner auxiliary nodes here */
1037 Assert(!IsA(node, PlaceHolderVar));
1038 Assert(!IsA(node, PlanRowMark));
1039 Assert(!IsA(node, SpecialJoinInfo));
1040 Assert(!IsA(node, AppendRelInfo));
1041 Assert(!IsA(node, PlaceHolderInfo));
1042 Assert(!IsA(node, MinMaxAggInfo));
1043
1044 if (IsA(node, Query))
1045 {
1046 /* Recurse into subselects */
1047 bool result;
1048
1049 context->sublevels_up++;
1051 context, 0);
1052 context->sublevels_up--;
1053 return result;
1054 }
1056}

References Assert, bms_is_member(), CurrentOfExpr::cvarno, expression_tree_walker, fb(), IsA, j, query_tree_walker, rangeTableEntry_used_walker(), rangeTableEntry_used_context::rt_index, rangeTableEntry_used_context::sublevels_up, Var::varlevelsup, and Var::varno.

Referenced by rangeTableEntry_used(), and rangeTableEntry_used_walker().

◆ remove_nulling_relids()

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

◆ remove_nulling_relids_mutator()

static Node * remove_nulling_relids_mutator ( Node node,
remove_nulling_relids_context context 
)
static

Definition at line 1345 of file rewriteManip.c.

1347{
1348 if (node == NULL)
1349 return NULL;
1350 if (IsA(node, Var))
1351 {
1352 Var *var = (Var *) node;
1353
1354 if (var->varlevelsup == context->sublevels_up &&
1355 !bms_is_member(var->varno, context->except_relids) &&
1356 bms_overlap(var->varnullingrels, context->removable_relids))
1357 {
1358 /* Copy the Var ... */
1359 var = copyObject(var);
1360 /* ... and replace the copy's varnullingrels field */
1361 var->varnullingrels = bms_difference(var->varnullingrels,
1362 context->removable_relids);
1363 return (Node *) var;
1364 }
1365 /* Otherwise fall through to copy the Var normally */
1366 }
1367 else if (IsA(node, PlaceHolderVar))
1368 {
1369 PlaceHolderVar *phv = (PlaceHolderVar *) node;
1370
1371 if (phv->phlevelsup == context->sublevels_up &&
1372 !bms_overlap(phv->phrels, context->except_relids))
1373 {
1374 /*
1375 * Note: it might seem desirable to remove the PHV altogether if
1376 * phnullingrels goes to empty. Currently we dare not do that
1377 * because we use PHVs in some cases to enforce separate identity
1378 * of subexpressions; see wrap_option usages in prepjointree.c.
1379 */
1380 /* Copy the PlaceHolderVar and mutate what's below ... */
1381 phv = (PlaceHolderVar *)
1384 context);
1385 /* ... and replace the copy's phnullingrels field */
1386 phv->phnullingrels = bms_difference(phv->phnullingrels,
1387 context->removable_relids);
1388 /* We must also update phrels, if it contains a removable RTI */
1389 phv->phrels = bms_difference(phv->phrels,
1390 context->removable_relids);
1391 Assert(!bms_is_empty(phv->phrels));
1392 return (Node *) phv;
1393 }
1394 /* Otherwise fall through to copy the PlaceHolderVar normally */
1395 }
1396 else if (IsA(node, Query))
1397 {
1398 /* Recurse into RTE or sublink subquery */
1399 Query *newnode;
1400
1401 context->sublevels_up++;
1402 newnode = query_tree_mutator((Query *) node,
1404 context,
1405 0);
1406 context->sublevels_up--;
1407 return (Node *) newnode;
1408 }
1410}
Bitmapset * bms_difference(const Bitmapset *a, const Bitmapset *b)
Definition bitmapset.c:346
#define bms_is_empty(a)
Definition bitmapset.h:118
Relids phnullingrels
Definition pathnodes.h:3115

References Assert, bms_difference(), bms_is_empty, bms_is_member(), bms_overlap(), copyObject, remove_nulling_relids_context::except_relids, expression_tree_mutator, fb(), IsA, query_tree_mutator, remove_nulling_relids_context::removable_relids, remove_nulling_relids_mutator(), remove_nulling_relids_context::sublevels_up, Var::varlevelsup, and Var::varno.

Referenced by remove_nulling_relids(), and remove_nulling_relids_mutator().

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

1440{
1441 Node *result;
1443
1444 context.callback = callback;
1445 context.callback_arg = callback_arg;
1446 context.target_varno = target_varno;
1447 context.sublevels_up = sublevels_up;
1448
1449 /*
1450 * We try to initialize inserted_sublink to true if there is no need to
1451 * detect new sublinks because the query already has some.
1452 */
1453 if (node && IsA(node, Query))
1454 context.inserted_sublink = ((Query *) node)->hasSubLinks;
1455 else if (outer_hasSubLinks)
1456 context.inserted_sublink = *outer_hasSubLinks;
1457 else
1458 context.inserted_sublink = false;
1459
1460 /*
1461 * Must be prepared to start with a Query or a bare expression tree; if
1462 * it's a Query, we don't want to increment sublevels_up.
1463 */
1466 &context,
1467 0);
1468
1469 if (context.inserted_sublink)
1470 {
1471 if (result && IsA(result, Query))
1472 ((Query *) result)->hasSubLinks = true;
1473 else if (outer_hasSubLinks)
1474 *outer_hasSubLinks = true;
1475 else
1476 elog(ERROR, "replace_rte_variables inserted a SubLink, but has noplace to record it");
1477 }
1478
1479 return result;
1480}
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(), 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 1483 of file rewriteManip.c.

1485{
1486 if (node == NULL)
1487 return NULL;
1488 if (IsA(node, Var))
1489 {
1490 Var *var = (Var *) node;
1491
1492 if (var->varno == context->target_varno &&
1493 var->varlevelsup == context->sublevels_up)
1494 {
1495 /* Found a matching variable, make the substitution */
1496 Node *newnode;
1497
1498 newnode = context->callback(var, context);
1499 /* Detect if we are adding a sublink to query */
1500 if (!context->inserted_sublink)
1502 return newnode;
1503 }
1504 /* otherwise fall through to copy the var normally */
1505 }
1506 else if (IsA(node, CurrentOfExpr))
1507 {
1508 CurrentOfExpr *cexpr = (CurrentOfExpr *) node;
1509
1510 if (cexpr->cvarno == context->target_varno &&
1511 context->sublevels_up == 0)
1512 {
1513 /*
1514 * We get here if a WHERE CURRENT OF expression turns out to apply
1515 * to a view. Someday we might be able to translate the
1516 * expression to apply to an underlying table of the view, but
1517 * right now it's not implemented.
1518 */
1519 ereport(ERROR,
1521 errmsg("WHERE CURRENT OF on a view is not implemented")));
1522 }
1523 /* otherwise fall through to copy the expr normally */
1524 }
1525 else if (IsA(node, Query))
1526 {
1527 /* Recurse into RTE subquery or not-yet-planned sublink subquery */
1528 Query *newnode;
1530
1531 context->sublevels_up++;
1533 context->inserted_sublink = ((Query *) node)->hasSubLinks;
1534 newnode = query_tree_mutator((Query *) node,
1536 context,
1537 0);
1538 newnode->hasSubLinks |= context->inserted_sublink;
1540 context->sublevels_up--;
1541 return (Node *) newnode;
1542 }
1544}

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 
)

Definition at line 1793 of file rewriteManip.c.

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

Definition at line 1962 of file rewriteManip.c.

1970{
1972
1973 context.target_rte = target_rte;
1974 context.targetlist = targetlist;
1975 context.result_relation = result_relation;
1976 context.nomatch_option = nomatch_option;
1977 context.nomatch_varno = nomatch_varno;
1978
1979 return replace_rte_variables(node, target_varno, sublevels_up,
1981 &context,
1982 outer_hasSubLinks);
1983}
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().

◆ ReplaceVarsFromTargetList_callback()

static Node * ReplaceVarsFromTargetList_callback ( const Var var,
replace_rte_variables_context context 
)
static

Definition at line 1772 of file rewriteManip.c.

1774{
1776 Node *newnode;
1777
1779 rcon->target_rte,
1780 rcon->targetlist,
1781 rcon->result_relation,
1782 rcon->nomatch_option,
1783 rcon->nomatch_varno);
1784
1785 /* Must adjust varlevelsup if replaced Var is within a subquery */
1786 if (var->varlevelsup > 0)
1788
1789 return newnode;
1790}
void IncrementVarSublevelsUp(Node *node, int delta_sublevels_up, int min_sublevels_up)

References replace_rte_variables_context::callback_arg, fb(), IncrementVarSublevelsUp(), ReplaceVarFromTargetList(), and Var::varlevelsup.

Referenced by ReplaceVarsFromTargetList().

◆ SetVarReturningType()

static void SetVarReturningType ( Node node,
int  result_relation,
int  sublevels_up,
VarReturningType  returning_type 
)
static

Definition at line 968 of file rewriteManip.c.

970{
972
973 context.result_relation = result_relation;
974 context.sublevels_up = sublevels_up;
975 context.returning_type = returning_type;
976
977 /* Expect to start with an expression */
978 SetVarReturningType_walker(node, &context);
979}
static bool SetVarReturningType_walker(Node *node, SetVarReturningType_context *context)
VarReturningType returning_type

References SetVarReturningType_context::result_relation, SetVarReturningType_context::returning_type, SetVarReturningType_walker(), and SetVarReturningType_context::sublevels_up.

Referenced by ReplaceVarFromTargetList().

◆ SetVarReturningType_walker()

static bool SetVarReturningType_walker ( Node node,
SetVarReturningType_context context 
)
static

Definition at line 938 of file rewriteManip.c.

939{
940 if (node == NULL)
941 return false;
942 if (IsA(node, Var))
943 {
944 Var *var = (Var *) node;
945
946 if (var->varno == context->result_relation &&
947 var->varlevelsup == context->sublevels_up)
948 var->varreturningtype = context->returning_type;
949
950 return false;
951 }
952
953 if (IsA(node, Query))
954 {
955 /* Recurse into subselects */
956 bool result;
957
958 context->sublevels_up++;
960 context, 0);
961 context->sublevels_up--;
962 return result;
963 }
965}

References expression_tree_walker, fb(), IsA, query_tree_walker, SetVarReturningType_context::result_relation, SetVarReturningType_context::returning_type, SetVarReturningType_walker(), SetVarReturningType_context::sublevels_up, Var::varlevelsup, Var::varno, and Var::varreturningtype.

Referenced by SetVarReturningType(), and SetVarReturningType_walker().