PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
rewriteManip.c File Reference
#include "postgres.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  ChangeVarNodes_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, bool change_RangeTblRef)
 
void ChangeVarNodes (Node *node, int rt_index, int new_index, int sublevels_up)
 
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 (Var *var, replace_rte_variables_context *context)
 
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)
 

Function Documentation

◆ add_nulling_relids()

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

Definition at line 1287 of file rewriteManip.c.

1290{
1292
1293 context.target_relids = target_relids;
1294 context.added_relids = added_relids;
1295 context.sublevels_up = 0;
1298 &context,
1299 0);
1300}
#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().

◆ add_nulling_relids_mutator()

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

Definition at line 1303 of file rewriteManip.c.

1305{
1306 if (node == NULL)
1307 return NULL;
1308 if (IsA(node, Var))
1309 {
1310 Var *var = (Var *) node;
1311
1312 if (var->varlevelsup == context->sublevels_up &&
1313 (context->target_relids == NULL ||
1314 bms_is_member(var->varno, context->target_relids)))
1315 {
1316 Relids newnullingrels = bms_union(var->varnullingrels,
1317 context->added_relids);
1318
1319 /* Copy the Var ... */
1320 var = copyObject(var);
1321 /* ... and replace the copy's varnullingrels field */
1322 var->varnullingrels = newnullingrels;
1323 return (Node *) var;
1324 }
1325 /* Otherwise fall through to copy the Var normally */
1326 }
1327 else if (IsA(node, PlaceHolderVar))
1328 {
1329 PlaceHolderVar *phv = (PlaceHolderVar *) node;
1330
1331 if (phv->phlevelsup == context->sublevels_up &&
1332 (context->target_relids == NULL ||
1333 bms_overlap(phv->phrels, context->target_relids)))
1334 {
1335 Relids newnullingrels = bms_union(phv->phnullingrels,
1336 context->added_relids);
1337
1338 /*
1339 * We don't modify the contents of the PHV's expression, only add
1340 * to phnullingrels. This corresponds to assuming that the PHV
1341 * will be evaluated at the same level as before, then perhaps be
1342 * nulled as it bubbles up. Hence, just flat-copy the node ...
1343 */
1344 phv = makeNode(PlaceHolderVar);
1345 memcpy(phv, node, sizeof(PlaceHolderVar));
1346 /* ... and replace the copy's phnullingrels field */
1347 phv->phnullingrels = newnullingrels;
1348 return (Node *) phv;
1349 }
1350 /* Otherwise fall through to copy the PlaceHolderVar normally */
1351 }
1352 else if (IsA(node, Query))
1353 {
1354 /* Recurse into RTE or sublink subquery */
1355 Query *newnode;
1356
1357 context->sublevels_up++;
1358 newnode = query_tree_mutator((Query *) node,
1360 context,
1361 0);
1362 context->sublevels_up--;
1363 return (Node *) newnode;
1364 }
1366}
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:582
#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:230
#define makeNode(_type_)
Definition: nodes.h:161
Definition: nodes.h:135
Relids phnullingrels
Definition: pathnodes.h:2829
Index phlevelsup
Definition: pathnodes.h:2835
Definition: primnodes.h:262
int varno
Definition: primnodes.h:269
Index varlevelsup
Definition: primnodes.h:294

References add_nulling_relids_mutator(), add_nulling_relids_context::added_relids, bms_is_member(), bms_overlap(), bms_union(), copyObject, expression_tree_mutator, IsA, makeNode, PlaceHolderVar::phlevelsup, PlaceHolderVar::phnullingrels, 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 1263 of file rewriteManip.c.

1264{
1265 BooleanTest *invqual;
1266
1267 if (qual == NULL)
1268 return;
1269
1270 /* Need not copy input qual, because AddQual will... */
1271 invqual = makeNode(BooleanTest);
1272 invqual->arg = (Expr *) qual;
1273 invqual->booltesttype = IS_NOT_TRUE;
1274 invqual->location = -1;
1275
1276 AddQual(parsetree, (Node *) invqual);
1277}
@ 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

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

1196{
1197 Node *copy;
1198
1199 if (qual == NULL)
1200 return;
1201
1202 if (parsetree->commandType == CMD_UTILITY)
1203 {
1204 /*
1205 * There's noplace to put the qual on a utility statement.
1206 *
1207 * If it's a NOTIFY, silently ignore the qual; this means that the
1208 * NOTIFY will execute, whether or not there are any qualifying rows.
1209 * While clearly wrong, this is much more useful than refusing to
1210 * execute the rule at all, and extra NOTIFY events are harmless for
1211 * typical uses of NOTIFY.
1212 *
1213 * If it isn't a NOTIFY, error out, since unconditional execution of
1214 * other utility stmts is unlikely to be wanted. (This case is not
1215 * currently allowed anyway, but keep the test for safety.)
1216 */
1217 if (parsetree->utilityStmt && IsA(parsetree->utilityStmt, NotifyStmt))
1218 return;
1219 else
1220 ereport(ERROR,
1221 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1222 errmsg("conditional utility statements are not implemented")));
1223 }
1224
1225 if (parsetree->setOperations != NULL)
1226 {
1227 /*
1228 * There's noplace to put the qual on a setop statement, either. (This
1229 * could be fixed, but right now the planner simply ignores any qual
1230 * condition on a setop query.)
1231 */
1232 ereport(ERROR,
1233 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1234 errmsg("conditional UNION/INTERSECT/EXCEPT statements are not implemented")));
1235 }
1236
1237 /* INTERSECT wants the original, but we need to copy - Jan */
1238 copy = copyObject(qual);
1239
1240 parsetree->jointree->quals = make_and_qual(parsetree->jointree->quals,
1241 copy);
1242
1243 /*
1244 * We had better not have stuck an aggregate into the WHERE clause.
1245 */
1246 Assert(!contain_aggs_of_level(copy, 0));
1247
1248 /*
1249 * Make sure query is marked correctly if added qual has sublinks. Need
1250 * not search qual when query is already marked.
1251 */
1252 if (!parsetree->hasSubLinks)
1253 parsetree->hasSubLinks = checkExprHasSubLink(copy);
1254}
int errcode(int sqlerrcode)
Definition: elog.c:853
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
Assert(PointerIsAligned(start, uint64))
Node * make_and_qual(Node *qual1, Node *qual2)
Definition: makefuncs.c:780
@ 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 808 of file rewriteManip.c.

809{
810 if (!IS_SPECIAL_VARNO(oldrelid) && bms_is_member(oldrelid, relids))
811 {
812 /* Ensure we have a modifiable copy */
813 relids = bms_copy(relids);
814 /* Remove old, add new */
815 relids = bms_del_member(relids, oldrelid);
816 if (!IS_SPECIAL_VARNO(newrelid))
817 relids = bms_add_member(relids, newrelid);
818 }
819 return relids;
820}
Bitmapset * bms_del_member(Bitmapset *a, int x)
Definition: bitmapset.c:868
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()

◆ ChangeVarNodes_walker()

static bool ChangeVarNodes_walker ( Node node,
ChangeVarNodes_context context 
)
static

Definition at line 562 of file rewriteManip.c.

563{
564 if (node == NULL)
565 return false;
566 if (IsA(node, Var))
567 {
568 Var *var = (Var *) node;
569
570 if (var->varlevelsup == context->sublevels_up)
571 {
572 if (var->varno == context->rt_index)
573 var->varno = context->new_index;
574 var->varnullingrels = adjust_relid_set(var->varnullingrels,
575 context->rt_index,
576 context->new_index);
577 if (var->varnosyn == context->rt_index)
578 var->varnosyn = context->new_index;
579 }
580 return false;
581 }
582 if (IsA(node, CurrentOfExpr))
583 {
584 CurrentOfExpr *cexpr = (CurrentOfExpr *) node;
585
586 if (context->sublevels_up == 0 &&
587 cexpr->cvarno == context->rt_index)
588 cexpr->cvarno = context->new_index;
589 return false;
590 }
591 if (IsA(node, RangeTblRef) && context->change_RangeTblRef)
592 {
593 RangeTblRef *rtr = (RangeTblRef *) node;
594
595 if (context->sublevels_up == 0 &&
596 rtr->rtindex == context->rt_index)
597 rtr->rtindex = context->new_index;
598 /* the subquery itself is visited separately */
599 return false;
600 }
601 if (IsA(node, JoinExpr))
602 {
603 JoinExpr *j = (JoinExpr *) node;
604
605 if (context->sublevels_up == 0 &&
606 j->rtindex == context->rt_index)
607 j->rtindex = context->new_index;
608 /* fall through to examine children */
609 }
610 if (IsA(node, PlaceHolderVar))
611 {
612 PlaceHolderVar *phv = (PlaceHolderVar *) node;
613
614 if (phv->phlevelsup == context->sublevels_up)
615 {
616 phv->phrels = adjust_relid_set(phv->phrels,
617 context->rt_index,
618 context->new_index);
620 context->rt_index,
621 context->new_index);
622 }
623 /* fall through to examine children */
624 }
625 if (IsA(node, PlanRowMark))
626 {
627 PlanRowMark *rowmark = (PlanRowMark *) node;
628
629 if (context->sublevels_up == 0)
630 {
631 if (rowmark->rti == context->rt_index)
632 rowmark->rti = context->new_index;
633 if (rowmark->prti == context->rt_index)
634 rowmark->prti = context->new_index;
635 }
636 return false;
637 }
638 if (IsA(node, RestrictInfo))
639 {
640 RestrictInfo *rinfo = (RestrictInfo *) node;
641 int relid = -1;
642 bool is_req_equal =
643 (rinfo->required_relids == rinfo->clause_relids);
644 bool clause_relids_is_multiple =
645 (bms_membership(rinfo->clause_relids) == BMS_MULTIPLE);
646
647 if (bms_is_member(context->rt_index, rinfo->clause_relids))
648 {
649 expression_tree_walker((Node *) rinfo->clause, ChangeVarNodes_walker, (void *) context);
650 expression_tree_walker((Node *) rinfo->orclause, ChangeVarNodes_walker, (void *) context);
651
652 rinfo->clause_relids =
653 adjust_relid_set(rinfo->clause_relids, context->rt_index, context->new_index);
654 rinfo->num_base_rels = bms_num_members(rinfo->clause_relids);
655 rinfo->left_relids =
656 adjust_relid_set(rinfo->left_relids, context->rt_index, context->new_index);
657 rinfo->right_relids =
658 adjust_relid_set(rinfo->right_relids, context->rt_index, context->new_index);
659 }
660
661 if (is_req_equal)
662 rinfo->required_relids = rinfo->clause_relids;
663 else
664 rinfo->required_relids =
665 adjust_relid_set(rinfo->required_relids, context->rt_index, context->new_index);
666
667 rinfo->outer_relids =
668 adjust_relid_set(rinfo->outer_relids, context->rt_index, context->new_index);
669 rinfo->incompatible_relids =
670 adjust_relid_set(rinfo->incompatible_relids, context->rt_index, context->new_index);
671
672 if (rinfo->mergeopfamilies &&
673 bms_get_singleton_member(rinfo->clause_relids, &relid) &&
674 clause_relids_is_multiple &&
675 relid == context->new_index && IsA(rinfo->clause, OpExpr))
676 {
677 Expr *leftOp;
678 Expr *rightOp;
679
680 leftOp = (Expr *) get_leftop(rinfo->clause);
681 rightOp = (Expr *) get_rightop(rinfo->clause);
682
683 /*
684 * For self-join elimination, changing varnos could transform
685 * "t1.a = t2.a" into "t1.a = t1.a". That is always true as long
686 * as "t1.a" is not null. We use qual() to check for such a case,
687 * and then we replace the qual for a check for not null
688 * (NullTest).
689 */
690 if (leftOp != NULL && equal(leftOp, rightOp))
691 {
692 NullTest *ntest = makeNode(NullTest);
693
694 ntest->arg = leftOp;
695 ntest->nulltesttype = IS_NOT_NULL;
696 ntest->argisrow = false;
697 ntest->location = -1;
698 rinfo->clause = (Expr *) ntest;
699 rinfo->mergeopfamilies = NIL;
700 rinfo->left_em = NULL;
701 rinfo->right_em = NULL;
702 }
703 Assert(rinfo->orclause == NULL);
704 }
705 return false;
706 }
707 if (IsA(node, AppendRelInfo))
708 {
709 AppendRelInfo *appinfo = (AppendRelInfo *) node;
710
711 if (context->sublevels_up == 0)
712 {
713 if (appinfo->parent_relid == context->rt_index)
714 appinfo->parent_relid = context->new_index;
715 if (appinfo->child_relid == context->rt_index)
716 appinfo->child_relid = context->new_index;
717 }
718 /* fall through to examine children */
719 }
720 /* Shouldn't need to handle other planner auxiliary nodes here */
721 Assert(!IsA(node, SpecialJoinInfo));
722 Assert(!IsA(node, PlaceHolderInfo));
723 Assert(!IsA(node, MinMaxAggInfo));
724
725 if (IsA(node, Query))
726 {
727 /* Recurse into subselects */
728 bool result;
729
730 context->sublevels_up++;
732 context, 0);
733 context->sublevels_up--;
734 return result;
735 }
736 return expression_tree_walker(node, ChangeVarNodes_walker, context);
737}
int bms_num_members(const Bitmapset *a)
Definition: bitmapset.c:751
BMS_Membership bms_membership(const Bitmapset *a)
Definition: bitmapset.c:781
bool bms_get_singleton_member(const Bitmapset *a, int *member)
Definition: bitmapset.c:715
@ BMS_MULTIPLE
Definition: bitmapset.h:73
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:223
int j
Definition: isn.c:75
static Node * get_rightop(const void *clause)
Definition: nodeFuncs.h:95
#define query_tree_walker(q, w, c, f)
Definition: nodeFuncs.h:158
#define expression_tree_walker(n, w, c)
Definition: nodeFuncs.h:153
static Node * get_leftop(const void *clause)
Definition: nodeFuncs.h:83
#define NIL
Definition: pg_list.h:68
@ IS_NOT_NULL
Definition: primnodes.h:1957
Relids adjust_relid_set(Relids relids, int oldrelid, int newrelid)
Definition: rewriteManip.c:808
static bool ChangeVarNodes_walker(Node *node, ChangeVarNodes_context *context)
Definition: rewriteManip.c:562
Index child_relid
Definition: pathnodes.h:3008
Index parent_relid
Definition: pathnodes.h:3007
NullTestType nulltesttype
Definition: primnodes.h:1964
ParseLoc location
Definition: primnodes.h:1967
Expr * arg
Definition: primnodes.h:1963
Index prti
Definition: plannodes.h:1542
Relids required_relids
Definition: pathnodes.h:2633
Relids outer_relids
Definition: pathnodes.h:2639
Relids incompatible_relids
Definition: pathnodes.h:2636
Expr * clause
Definition: pathnodes.h:2602

References adjust_relid_set(), NullTest::arg, Assert(), bms_get_singleton_member(), bms_is_member(), bms_membership(), BMS_MULTIPLE, bms_num_members(), ChangeVarNodes_context::change_RangeTblRef, ChangeVarNodes_walker(), AppendRelInfo::child_relid, RestrictInfo::clause, CurrentOfExpr::cvarno, equal(), expression_tree_walker, get_leftop(), get_rightop(), RestrictInfo::incompatible_relids, IS_NOT_NULL, IsA, j, NullTest::location, makeNode, ChangeVarNodes_context::new_index, NIL, NullTest::nulltesttype, RestrictInfo::outer_relids, AppendRelInfo::parent_relid, PlaceHolderVar::phlevelsup, PlaceHolderVar::phnullingrels, PlanRowMark::prti, query_tree_walker, RestrictInfo::required_relids, ChangeVarNodes_context::rt_index, PlanRowMark::rti, RangeTblRef::rtindex, ChangeVarNodes_context::sublevels_up, Var::varlevelsup, and Var::varno.

Referenced by ChangeVarNodes_walker(), and ChangeVarNodesExtended().

◆ ChangeVarNodesExtended()

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

Definition at line 751 of file rewriteManip.c.

753{
755
756 context.rt_index = rt_index;
757 context.new_index = new_index;
758 context.sublevels_up = sublevels_up;
759 context.change_RangeTblRef = change_RangeTblRef;
760
761 if (node && IsA(node, Query))
762 {
763 Query *qry = (Query *) node;
764
765 if (sublevels_up == 0)
766 {
767 ListCell *l;
768
769 if (qry->resultRelation == rt_index)
770 qry->resultRelation = new_index;
771
772 if (qry->mergeTargetRelation == rt_index)
773 qry->mergeTargetRelation = new_index;
774
775 if (qry->onConflict && qry->onConflict->exclRelIndex == rt_index)
776 qry->onConflict->exclRelIndex = new_index;
777
778 foreach(l, qry->rowMarks)
779 {
781
782 if (rc->rti == rt_index)
783 rc->rti = new_index;
784 }
785 }
786 query_tree_walker(qry, ChangeVarNodes_walker, &context, 0);
787 }
788 else
789 ChangeVarNodes_walker(node, &context);
790}
#define lfirst(lc)
Definition: pg_list.h:172
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().

◆ checkExprHasSubLink_walker()

static bool checkExprHasSubLink_walker ( Node node,
void *  context 
)
static

Definition at line 305 of file rewriteManip.c.

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

References checkExprHasSubLink_walker(), expression_tree_walker, 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 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_aggs_of_level_walker()

static bool contain_aggs_of_level_walker ( Node node,
contain_aggs_of_level_context context 
)
static

Definition at line 102 of file rewriteManip.c.

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

References contain_aggs_of_level_walker(), expression_tree_walker, 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 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().

◆ contain_windowfuncs_walker()

static bool contain_windowfuncs_walker ( Node node,
void *  context 
)
static

Definition at line 227 of file rewriteManip.c.

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

References contain_windowfuncs_walker(), expression_tree_walker, 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 321 of file rewriteManip.c.

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

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

Referenced by contains_multiexpr_param(), and ReplaceVarFromTargetList().

◆ getInsertSelectQuery()

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

Definition at line 1137 of file rewriteManip.c.

1138{
1139 Query *selectquery;
1140 RangeTblEntry *selectrte;
1141 RangeTblRef *rtr;
1142
1143 if (subquery_ptr)
1144 *subquery_ptr = NULL;
1145
1146 if (parsetree == NULL)
1147 return parsetree;
1148 if (parsetree->commandType != CMD_INSERT)
1149 return parsetree;
1150
1151 /*
1152 * Currently, this is ONLY applied to rule-action queries, and so we
1153 * expect to find the OLD and NEW placeholder entries in the given query.
1154 * If they're not there, it must be an INSERT/SELECT in which they've been
1155 * pushed down to the SELECT.
1156 */
1157 if (list_length(parsetree->rtable) >= 2 &&
1158 strcmp(rt_fetch(PRS2_OLD_VARNO, parsetree->rtable)->eref->aliasname,
1159 "old") == 0 &&
1160 strcmp(rt_fetch(PRS2_NEW_VARNO, parsetree->rtable)->eref->aliasname,
1161 "new") == 0)
1162 return parsetree;
1163 Assert(parsetree->jointree && IsA(parsetree->jointree, FromExpr));
1164 if (list_length(parsetree->jointree->fromlist) != 1)
1165 elog(ERROR, "expected to find SELECT subquery");
1166 rtr = (RangeTblRef *) linitial(parsetree->jointree->fromlist);
1167 if (!IsA(rtr, RangeTblRef))
1168 elog(ERROR, "expected to find SELECT subquery");
1169 selectrte = rt_fetch(rtr->rtindex, parsetree->rtable);
1170 if (!(selectrte->rtekind == RTE_SUBQUERY &&
1171 selectrte->subquery &&
1172 IsA(selectrte->subquery, Query) &&
1173 selectrte->subquery->commandType == CMD_SELECT))
1174 elog(ERROR, "expected to find SELECT subquery");
1175 selectquery = selectrte->subquery;
1176 if (list_length(selectquery->rtable) >= 2 &&
1177 strcmp(rt_fetch(PRS2_OLD_VARNO, selectquery->rtable)->eref->aliasname,
1178 "old") == 0 &&
1179 strcmp(rt_fetch(PRS2_NEW_VARNO, selectquery->rtable)->eref->aliasname,
1180 "new") == 0)
1181 {
1182 if (subquery_ptr)
1183 *subquery_ptr = &(selectrte->subquery);
1184 return selectquery;
1185 }
1186 elog(ERROR, "could not find rule placeholders");
1187 return NULL; /* not reached */
1188}
#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 928 of file rewriteManip.c.

930{
932
933 context.delta_sublevels_up = delta_sublevels_up;
934 context.min_sublevels_up = min_sublevels_up;
935
936 /*
937 * Must be prepared to start with a Query or a bare expression tree; if
938 * it's a Query, we don't want to increment sublevels_up.
939 */
942 &context,
944}
#define QTW_EXAMINE_RTES_BEFORE
Definition: nodeFuncs.h:27
static bool IncrementVarSublevelsUp_walker(Node *node, IncrementVarSublevelsUp_context *context)
Definition: rewriteManip.c:848

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

953{
955
956 context.delta_sublevels_up = delta_sublevels_up;
957 context.min_sublevels_up = min_sublevels_up;
958
959 range_table_walker(rtable,
961 &context,
963}
#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 848 of file rewriteManip.c.

850{
851 if (node == NULL)
852 return false;
853 if (IsA(node, Var))
854 {
855 Var *var = (Var *) node;
856
857 if (var->varlevelsup >= context->min_sublevels_up)
858 var->varlevelsup += context->delta_sublevels_up;
859 return false; /* done here */
860 }
861 if (IsA(node, CurrentOfExpr))
862 {
863 /* this should not happen */
864 if (context->min_sublevels_up == 0)
865 elog(ERROR, "cannot push down CurrentOfExpr");
866 return false;
867 }
868 if (IsA(node, Aggref))
869 {
870 Aggref *agg = (Aggref *) node;
871
872 if (agg->agglevelsup >= context->min_sublevels_up)
873 agg->agglevelsup += context->delta_sublevels_up;
874 /* fall through to recurse into argument */
875 }
876 if (IsA(node, GroupingFunc))
877 {
878 GroupingFunc *grp = (GroupingFunc *) node;
879
880 if (grp->agglevelsup >= context->min_sublevels_up)
881 grp->agglevelsup += context->delta_sublevels_up;
882 /* fall through to recurse into argument */
883 }
884 if (IsA(node, PlaceHolderVar))
885 {
886 PlaceHolderVar *phv = (PlaceHolderVar *) node;
887
888 if (phv->phlevelsup >= context->min_sublevels_up)
889 phv->phlevelsup += context->delta_sublevels_up;
890 /* fall through to recurse into argument */
891 }
892 if (IsA(node, ReturningExpr))
893 {
894 ReturningExpr *rexpr = (ReturningExpr *) node;
895
896 if (rexpr->retlevelsup >= context->min_sublevels_up)
897 rexpr->retlevelsup += context->delta_sublevels_up;
898 /* fall through to recurse into argument */
899 }
900 if (IsA(node, RangeTblEntry))
901 {
902 RangeTblEntry *rte = (RangeTblEntry *) node;
903
904 if (rte->rtekind == RTE_CTE)
905 {
906 if (rte->ctelevelsup >= context->min_sublevels_up)
907 rte->ctelevelsup += context->delta_sublevels_up;
908 }
909 return false; /* allow range_table_walker to continue */
910 }
911 if (IsA(node, Query))
912 {
913 /* Recurse into subselects */
914 bool result;
915
916 context->min_sublevels_up++;
917 result = query_tree_walker((Query *) node,
919 context,
921 context->min_sublevels_up--;
922 return result;
923 }
925}
@ RTE_CTE
Definition: parsenodes.h:1032
Index agglevelsup
Definition: primnodes.h:568
Index ctelevelsup
Definition: parsenodes.h:1212

References GroupingFunc::agglevelsup, RangeTblEntry::ctelevelsup, IncrementVarSublevelsUp_context::delta_sublevels_up, elog, ERROR, expression_tree_walker, IncrementVarSublevelsUp_walker(), IsA, IncrementVarSublevelsUp_context::min_sublevels_up, PlaceHolderVar::phlevelsup, QTW_EXAMINE_RTES_BEFORE, query_tree_walker, ReturningExpr::retlevelsup, RTE_CTE, RangeTblEntry::rtekind, 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 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_agg_of_level_walker()

static bool locate_agg_of_level_walker ( Node node,
locate_agg_of_level_context context 
)
static

Definition at line 169 of file rewriteManip.c.

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

References locate_agg_of_level_context::agg_location, expression_tree_walker, 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 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().

◆ locate_windowfunc_walker()

static bool locate_windowfunc_walker ( Node node,
locate_windowfunc_context context 
)
static

Definition at line 270 of file rewriteManip.c.

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

References expression_tree_walker, 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 1749 of file rewriteManip.c.

1753{
1755
1756 context.target_varno = target_varno;
1757 context.sublevels_up = sublevels_up;
1758 context.attno_map = attno_map;
1759 context.to_rowtype = to_rowtype;
1760 context.found_whole_row = found_whole_row;
1761
1762 *found_whole_row = false;
1763
1764 /*
1765 * Must be prepared to start with a Query or a bare expression tree; if
1766 * it's a Query, we don't want to increment sublevels_up.
1767 */
1770 &context,
1771 0);
1772}
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(), 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 1627 of file rewriteManip.c.

1629{
1630 if (node == NULL)
1631 return NULL;
1632 if (IsA(node, Var))
1633 {
1634 Var *var = (Var *) node;
1635
1636 if (var->varno == context->target_varno &&
1637 var->varlevelsup == context->sublevels_up)
1638 {
1639 /* Found a matching variable, make the substitution */
1640 Var *newvar = (Var *) palloc(sizeof(Var));
1641 int attno = var->varattno;
1642
1643 *newvar = *var; /* initially copy all fields of the Var */
1644
1645 if (attno > 0)
1646 {
1647 /* user-defined column, replace attno */
1648 if (attno > context->attno_map->maplen ||
1649 context->attno_map->attnums[attno - 1] == 0)
1650 elog(ERROR, "unexpected varattno %d in expression to be mapped",
1651 attno);
1652 newvar->varattno = context->attno_map->attnums[attno - 1];
1653 /* If the syntactic referent is same RTE, fix it too */
1654 if (newvar->varnosyn == context->target_varno)
1655 newvar->varattnosyn = newvar->varattno;
1656 }
1657 else if (attno == 0)
1658 {
1659 /* whole-row variable, warn caller */
1660 *(context->found_whole_row) = true;
1661
1662 /* If the caller expects us to convert the Var, do so. */
1663 if (OidIsValid(context->to_rowtype) &&
1664 context->to_rowtype != var->vartype)
1665 {
1667
1668 /* This certainly won't work for a RECORD variable. */
1669 Assert(var->vartype != RECORDOID);
1670
1671 /* Var itself is changed to the requested type. */
1672 newvar->vartype = context->to_rowtype;
1673
1674 /*
1675 * Add a conversion node on top to convert back to the
1676 * original type expected by the expression.
1677 */
1679 r->arg = (Expr *) newvar;
1680 r->resulttype = var->vartype;
1681 r->convertformat = COERCE_IMPLICIT_CAST;
1682 r->location = -1;
1683
1684 return (Node *) r;
1685 }
1686 }
1687 return (Node *) newvar;
1688 }
1689 /* otherwise fall through to copy the var normally */
1690 }
1691 else if (IsA(node, ConvertRowtypeExpr))
1692 {
1694 Var *var = (Var *) r->arg;
1695
1696 /*
1697 * If this is coercing a whole-row Var that we need to convert, then
1698 * just convert the Var without adding an extra ConvertRowtypeExpr.
1699 * Effectively we're simplifying var::parenttype::grandparenttype into
1700 * just var::grandparenttype. This avoids building stacks of CREs if
1701 * this function is applied repeatedly.
1702 */
1703 if (IsA(var, Var) &&
1704 var->varno == context->target_varno &&
1705 var->varlevelsup == context->sublevels_up &&
1706 var->varattno == 0 &&
1707 OidIsValid(context->to_rowtype) &&
1708 context->to_rowtype != var->vartype)
1709 {
1710 ConvertRowtypeExpr *newnode;
1711 Var *newvar = (Var *) palloc(sizeof(Var));
1712
1713 /* whole-row variable, warn caller */
1714 *(context->found_whole_row) = true;
1715
1716 *newvar = *var; /* initially copy all fields of the Var */
1717
1718 /* This certainly won't work for a RECORD variable. */
1719 Assert(var->vartype != RECORDOID);
1720
1721 /* Var itself is changed to the requested type. */
1722 newvar->vartype = context->to_rowtype;
1723
1724 newnode = (ConvertRowtypeExpr *) palloc(sizeof(ConvertRowtypeExpr));
1725 *newnode = *r; /* initially copy all fields of the CRE */
1726 newnode->arg = (Expr *) newvar;
1727
1728 return (Node *) newnode;
1729 }
1730 /* otherwise fall through to process the expression normally */
1731 }
1732 else if (IsA(node, Query))
1733 {
1734 /* Recurse into RTE subquery or not-yet-planned sublink subquery */
1735 Query *newnode;
1736
1737 context->sublevels_up++;
1738 newnode = query_tree_mutator((Query *) node,
1740 context,
1741 0);
1742 context->sublevels_up--;
1743 return (Node *) newnode;
1744 }
1746}
#define OidIsValid(objectId)
Definition: c.h:746
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:78
void * palloc(Size size)
Definition: mcxt.c:1317
@ COERCE_IMPLICIT_CAST
Definition: primnodes.h:753
int maplen
Definition: attmap.h:37
AttrNumber * attnums
Definition: attmap.h:36
AttrNumber varattno
Definition: primnodes.h:274

References ConvertRowtypeExpr::arg, Assert(), map_variable_attnos_context::attno_map, AttrMap::attnums, COERCE_IMPLICIT_CAST, elog, ERROR, expression_tree_mutator, map_variable_attnos_context::found_whole_row, if(), IsA, ConvertRowtypeExpr::location, makeNode, map_variable_attnos_mutator(), AttrMap::maplen, OidIsValid, palloc(), 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 527 of file rewriteManip.c.

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

References bms_add_member(), and bms_next_member().

Referenced by OffsetVarNodes_walker().

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

◆ OffsetVarNodes_walker()

static bool OffsetVarNodes_walker ( Node node,
OffsetVarNodes_context context 
)
static

Definition at line 388 of file rewriteManip.c.

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

References Assert(), AppendRelInfo::child_relid, CurrentOfExpr::cvarno, expression_tree_walker, IsA, j, OffsetVarNodes_context::offset, offset_relid_set(), OffsetVarNodes_walker(), AppendRelInfo::parent_relid, PlaceHolderVar::phlevelsup, PlaceHolderVar::phnullingrels, query_tree_walker, RangeTblRef::rtindex, 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 1105 of file rewriteManip.c.

1106{
1108
1109 context.rt_index = rt_index;
1110 context.sublevels_up = sublevels_up;
1111
1112 /*
1113 * Must be prepared to start with a Query or a bare expression tree; if
1114 * it's a Query, we don't want to increment sublevels_up.
1115 */
1118 &context,
1119 0);
1120}
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 1039 of file rewriteManip.c.

1041{
1042 if (node == NULL)
1043 return false;
1044 if (IsA(node, Var))
1045 {
1046 Var *var = (Var *) node;
1047
1048 if (var->varlevelsup == context->sublevels_up &&
1049 (var->varno == context->rt_index ||
1050 bms_is_member(context->rt_index, var->varnullingrels)))
1051 return true;
1052 return false;
1053 }
1054 if (IsA(node, CurrentOfExpr))
1055 {
1056 CurrentOfExpr *cexpr = (CurrentOfExpr *) node;
1057
1058 if (context->sublevels_up == 0 &&
1059 cexpr->cvarno == context->rt_index)
1060 return true;
1061 return false;
1062 }
1063 if (IsA(node, RangeTblRef))
1064 {
1065 RangeTblRef *rtr = (RangeTblRef *) node;
1066
1067 if (rtr->rtindex == context->rt_index &&
1068 context->sublevels_up == 0)
1069 return true;
1070 /* the subquery itself is visited separately */
1071 return false;
1072 }
1073 if (IsA(node, JoinExpr))
1074 {
1075 JoinExpr *j = (JoinExpr *) node;
1076
1077 if (j->rtindex == context->rt_index &&
1078 context->sublevels_up == 0)
1079 return true;
1080 /* fall through to examine children */
1081 }
1082 /* Shouldn't need to handle planner auxiliary nodes here */
1083 Assert(!IsA(node, PlaceHolderVar));
1084 Assert(!IsA(node, PlanRowMark));
1085 Assert(!IsA(node, SpecialJoinInfo));
1086 Assert(!IsA(node, AppendRelInfo));
1087 Assert(!IsA(node, PlaceHolderInfo));
1088 Assert(!IsA(node, MinMaxAggInfo));
1089
1090 if (IsA(node, Query))
1091 {
1092 /* Recurse into subselects */
1093 bool result;
1094
1095 context->sublevels_up++;
1097 context, 0);
1098 context->sublevels_up--;
1099 return result;
1100 }
1102}

References Assert(), bms_is_member(), CurrentOfExpr::cvarno, expression_tree_walker, IsA, j, query_tree_walker, rangeTableEntry_used_walker(), rangeTableEntry_used_context::rt_index, RangeTblRef::rtindex, 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 1391 of file rewriteManip.c.

1393{
1394 if (node == NULL)
1395 return NULL;
1396 if (IsA(node, Var))
1397 {
1398 Var *var = (Var *) node;
1399
1400 if (var->varlevelsup == context->sublevels_up &&
1401 !bms_is_member(var->varno, context->except_relids) &&
1402 bms_overlap(var->varnullingrels, context->removable_relids))
1403 {
1404 /* Copy the Var ... */
1405 var = copyObject(var);
1406 /* ... and replace the copy's varnullingrels field */
1407 var->varnullingrels = bms_difference(var->varnullingrels,
1408 context->removable_relids);
1409 return (Node *) var;
1410 }
1411 /* Otherwise fall through to copy the Var normally */
1412 }
1413 else if (IsA(node, PlaceHolderVar))
1414 {
1415 PlaceHolderVar *phv = (PlaceHolderVar *) node;
1416
1417 if (phv->phlevelsup == context->sublevels_up &&
1418 !bms_overlap(phv->phrels, context->except_relids))
1419 {
1420 /*
1421 * Note: it might seem desirable to remove the PHV altogether if
1422 * phnullingrels goes to empty. Currently we dare not do that
1423 * because we use PHVs in some cases to enforce separate identity
1424 * of subexpressions; see wrap_option usages in prepjointree.c.
1425 */
1426 /* Copy the PlaceHolderVar and mutate what's below ... */
1427 phv = (PlaceHolderVar *)
1430 context);
1431 /* ... and replace the copy's phnullingrels field */
1433 context->removable_relids);
1434 /* We must also update phrels, if it contains a removable RTI */
1435 phv->phrels = bms_difference(phv->phrels,
1436 context->removable_relids);
1437 Assert(!bms_is_empty(phv->phrels));
1438 return (Node *) phv;
1439 }
1440 /* Otherwise fall through to copy the PlaceHolderVar normally */
1441 }
1442 else if (IsA(node, Query))
1443 {
1444 /* Recurse into RTE or sublink subquery */
1445 Query *newnode;
1446
1447 context->sublevels_up++;
1448 newnode = query_tree_mutator((Query *) node,
1450 context,
1451 0);
1452 context->sublevels_up--;
1453 return (Node *) newnode;
1454 }
1456}
Bitmapset * bms_difference(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:346
#define bms_is_empty(a)
Definition: bitmapset.h:118

References Assert(), bms_difference(), bms_is_empty, bms_is_member(), bms_overlap(), copyObject, remove_nulling_relids_context::except_relids, expression_tree_mutator, IsA, PlaceHolderVar::phlevelsup, PlaceHolderVar::phnullingrels, 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 1482 of file rewriteManip.c.

1486{
1487 Node *result;
1489
1490 context.callback = callback;
1491 context.callback_arg = callback_arg;
1492 context.target_varno = target_varno;
1493 context.sublevels_up = sublevels_up;
1494
1495 /*
1496 * We try to initialize inserted_sublink to true if there is no need to
1497 * detect new sublinks because the query already has some.
1498 */
1499 if (node && IsA(node, Query))
1500 context.inserted_sublink = ((Query *) node)->hasSubLinks;
1501 else if (outer_hasSubLinks)
1502 context.inserted_sublink = *outer_hasSubLinks;
1503 else
1504 context.inserted_sublink = false;
1505
1506 /*
1507 * Must be prepared to start with a Query or a bare expression tree; if
1508 * it's a Query, we don't want to increment sublevels_up.
1509 */
1512 &context,
1513 0);
1514
1515 if (context.inserted_sublink)
1516 {
1517 if (result && IsA(result, Query))
1518 ((Query *) result)->hasSubLinks = true;
1519 else if (outer_hasSubLinks)
1520 *outer_hasSubLinks = true;
1521 else
1522 elog(ERROR, "replace_rte_variables inserted a SubLink, but has noplace to record it");
1523 }
1524
1525 return result;
1526}
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 1529 of file rewriteManip.c.

1531{
1532 if (node == NULL)
1533 return NULL;
1534 if (IsA(node, Var))
1535 {
1536 Var *var = (Var *) node;
1537
1538 if (var->varno == context->target_varno &&
1539 var->varlevelsup == context->sublevels_up)
1540 {
1541 /* Found a matching variable, make the substitution */
1542 Node *newnode;
1543
1544 newnode = context->callback(var, context);
1545 /* Detect if we are adding a sublink to query */
1546 if (!context->inserted_sublink)
1547 context->inserted_sublink = checkExprHasSubLink(newnode);
1548 return newnode;
1549 }
1550 /* otherwise fall through to copy the var normally */
1551 }
1552 else if (IsA(node, CurrentOfExpr))
1553 {
1554 CurrentOfExpr *cexpr = (CurrentOfExpr *) node;
1555
1556 if (cexpr->cvarno == context->target_varno &&
1557 context->sublevels_up == 0)
1558 {
1559 /*
1560 * We get here if a WHERE CURRENT OF expression turns out to apply
1561 * to a view. Someday we might be able to translate the
1562 * expression to apply to an underlying table of the view, but
1563 * right now it's not implemented.
1564 */
1565 ereport(ERROR,
1566 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1567 errmsg("WHERE CURRENT OF on a view is not implemented")));
1568 }
1569 /* otherwise fall through to copy the expr normally */
1570 }
1571 else if (IsA(node, Query))
1572 {
1573 /* Recurse into RTE subquery or not-yet-planned sublink subquery */
1574 Query *newnode;
1575 bool save_inserted_sublink;
1576
1577 context->sublevels_up++;
1578 save_inserted_sublink = context->inserted_sublink;
1579 context->inserted_sublink = ((Query *) node)->hasSubLinks;
1580 newnode = query_tree_mutator((Query *) node,
1582 context,
1583 0);
1584 newnode->hasSubLinks |= context->inserted_sublink;
1585 context->inserted_sublink = save_inserted_sublink;
1586 context->sublevels_up--;
1587 return (Node *) newnode;
1588 }
1590}

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

1845{
1846 TargetEntry *tle;
1847
1848 if (var->varattno == InvalidAttrNumber)
1849 {
1850 /* Must expand whole-tuple reference into RowExpr */
1851 RowExpr *rowexpr;
1852 List *colnames;
1853 List *fields;
1854 ListCell *lc;
1855
1856 /*
1857 * If generating an expansion for a var of a named rowtype (ie, this
1858 * is a plain relation RTE), then we must include dummy items for
1859 * dropped columns. If the var is RECORD (ie, this is a JOIN), then
1860 * omit dropped columns. In the latter case, attach column names to
1861 * the RowExpr for use of the executor and ruleutils.c.
1862 *
1863 * In order to be able to cache the results, we always generate the
1864 * expansion with varlevelsup = 0. The caller is responsible for
1865 * adjusting it if needed.
1866 *
1867 * The varreturningtype is copied onto each individual field Var, so
1868 * that it is handled correctly when we recurse.
1869 */
1870 expandRTE(target_rte,
1871 var->varno, 0 /* not varlevelsup */ ,
1872 var->varreturningtype, var->location,
1873 (var->vartype != RECORDOID),
1874 &colnames, &fields);
1875 rowexpr = makeNode(RowExpr);
1876 /* the fields will be set below */
1877 rowexpr->args = NIL;
1878 rowexpr->row_typeid = var->vartype;
1879 rowexpr->row_format = COERCE_IMPLICIT_CAST;
1880 rowexpr->colnames = (var->vartype == RECORDOID) ? colnames : NIL;
1881 rowexpr->location = var->location;
1882 /* Adjust the generated per-field Vars... */
1883 foreach(lc, fields)
1884 {
1885 Node *field = lfirst(lc);
1886
1887 if (field && IsA(field, Var))
1888 field = ReplaceVarFromTargetList((Var *) field,
1889 target_rte,
1890 targetlist,
1891 result_relation,
1892 nomatch_option,
1893 nomatch_varno);
1894 rowexpr->args = lappend(rowexpr->args, field);
1895 }
1896
1897 /* Wrap it in a ReturningExpr, if needed, per comments above */
1899 {
1901
1902 rexpr->retlevelsup = 0;
1903 rexpr->retold = (var->varreturningtype == VAR_RETURNING_OLD);
1904 rexpr->retexpr = (Expr *) rowexpr;
1905
1906 return (Node *) rexpr;
1907 }
1908
1909 return (Node *) rowexpr;
1910 }
1911
1912 /* Normal case referencing one targetlist element */
1913 tle = get_tle_by_resno(targetlist, var->varattno);
1914
1915 if (tle == NULL || tle->resjunk)
1916 {
1917 /* Failed to find column in targetlist */
1918 switch (nomatch_option)
1919 {
1921 /* fall through, throw error below */
1922 break;
1923
1925 var = copyObject(var);
1926 var->varno = nomatch_varno;
1927 var->varlevelsup = 0;
1928 /* we leave the syntactic referent alone */
1929 return (Node *) var;
1930
1932 {
1933 /*
1934 * If Var is of domain type, we must add a CoerceToDomain
1935 * node, in case there is a NOT NULL domain constraint.
1936 */
1937 int16 vartyplen;
1938 bool vartypbyval;
1939
1940 get_typlenbyval(var->vartype, &vartyplen, &vartypbyval);
1941 return coerce_null_to_domain(var->vartype,
1942 var->vartypmod,
1943 var->varcollid,
1944 vartyplen,
1945 vartypbyval);
1946 }
1947 }
1948 elog(ERROR, "could not find replacement targetlist entry for attno %d",
1949 var->varattno);
1950 return NULL; /* keep compiler quiet */
1951 }
1952 else
1953 {
1954 /* Make a copy of the tlist item to return */
1955 Expr *newnode = copyObject(tle->expr);
1956
1957 /*
1958 * Check to see if the tlist item contains a PARAM_MULTIEXPR Param,
1959 * and throw error if so. This case could only happen when expanding
1960 * an ON UPDATE rule's NEW variable and the referenced tlist item in
1961 * the original UPDATE command is part of a multiple assignment. There
1962 * seems no practical way to handle such cases without multiple
1963 * evaluation of the multiple assignment's sub-select, which would
1964 * create semantic oddities that users of rules would probably prefer
1965 * not to cope with. So treat it as an unimplemented feature.
1966 */
1967 if (contains_multiexpr_param((Node *) newnode, NULL))
1968 ereport(ERROR,
1969 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1970 errmsg("NEW variables in ON UPDATE rules cannot reference columns that are part of a multiple assignment in the subject UPDATE command")));
1971
1972 /* Handle any OLD/NEW RETURNING list Vars */
1974 {
1975 /*
1976 * Copy varreturningtype onto any Vars in the tlist item that
1977 * refer to result_relation (which had better be non-zero).
1978 */
1979 if (result_relation == 0)
1980 elog(ERROR, "variable returning old/new found outside RETURNING list");
1981
1982 SetVarReturningType((Node *) newnode, result_relation,
1983 0, var->varreturningtype);
1984
1985 /* Wrap it in a ReturningExpr, if needed, per comments above */
1986 if (!IsA(newnode, Var) ||
1987 ((Var *) newnode)->varno != result_relation ||
1988 ((Var *) newnode)->varlevelsup != 0)
1989 {
1991
1992 rexpr->retlevelsup = 0;
1993 rexpr->retold = (var->varreturningtype == VAR_RETURNING_OLD);
1994 rexpr->retexpr = newnode;
1995
1996 newnode = (Expr *) rexpr;
1997 }
1998 }
1999
2000 return (Node *) newnode;
2001 }
2002}
#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:2334
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)
@ VAR_RETURNING_OLD
Definition: primnodes.h:257
@ VAR_RETURNING_DEFAULT
Definition: primnodes.h:256
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)
@ REPLACEVARS_SUBSTITUTE_NULL
Definition: rewriteManip.h:41
@ REPLACEVARS_CHANGE_VARNO
Definition: rewriteManip.h:40
@ REPLACEVARS_REPORT_ERROR
Definition: rewriteManip.h:39
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
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 2005 of file rewriteManip.c.

2013{
2015
2016 context.target_rte = target_rte;
2017 context.targetlist = targetlist;
2018 context.result_relation = result_relation;
2019 context.nomatch_option = nomatch_option;
2020 context.nomatch_varno = nomatch_varno;
2021
2022 return replace_rte_variables(node, target_varno, sublevels_up,
2024 &context,
2025 outer_hasSubLinks);
2026}
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().

◆ ReplaceVarsFromTargetList_callback()

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

Definition at line 1818 of file rewriteManip.c.

1820{
1822 Node *newnode;
1823
1824 newnode = ReplaceVarFromTargetList(var,
1825 rcon->target_rte,
1826 rcon->targetlist,
1827 rcon->result_relation,
1828 rcon->nomatch_option,
1829 rcon->nomatch_varno);
1830
1831 /* Must adjust varlevelsup if replaced Var is within a subquery */
1832 if (var->varlevelsup > 0)
1833 IncrementVarSublevelsUp(newnode, var->varlevelsup, 0);
1834
1835 return newnode;
1836}
void IncrementVarSublevelsUp(Node *node, int delta_sublevels_up, int min_sublevels_up)
Definition: rewriteManip.c:928

References replace_rte_variables_context::callback_arg, IncrementVarSublevelsUp(), ReplaceVarsFromTargetList_context::nomatch_option, ReplaceVarsFromTargetList_context::nomatch_varno, ReplaceVarFromTargetList(), ReplaceVarsFromTargetList_context::result_relation, ReplaceVarsFromTargetList_context::target_rte, ReplaceVarsFromTargetList_context::targetlist, 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 1014 of file rewriteManip.c.

1016{
1018
1019 context.result_relation = result_relation;
1020 context.sublevels_up = sublevels_up;
1021 context.returning_type = returning_type;
1022
1023 /* Expect to start with an expression */
1024 SetVarReturningType_walker(node, &context);
1025}
static bool SetVarReturningType_walker(Node *node, SetVarReturningType_context *context)
Definition: rewriteManip.c:984
VarReturningType returning_type
Definition: rewriteManip.c:980

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

985{
986 if (node == NULL)
987 return false;
988 if (IsA(node, Var))
989 {
990 Var *var = (Var *) node;
991
992 if (var->varno == context->result_relation &&
993 var->varlevelsup == context->sublevels_up)
994 var->varreturningtype = context->returning_type;
995
996 return false;
997 }
998
999 if (IsA(node, Query))
1000 {
1001 /* Recurse into subselects */
1002 bool result;
1003
1004 context->sublevels_up++;
1006 context, 0);
1007 context->sublevels_up--;
1008 return result;
1009 }
1011}

References expression_tree_walker, 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().