PostgreSQL Source Code git master
Loading...
Searching...
No Matches
rewriteManip.c
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * rewriteManip.c
4 *
5 * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
6 * Portions Copyright (c) 1994, Regents of the University of California
7 *
8 *
9 * IDENTIFICATION
10 * src/backend/rewrite/rewriteManip.c
11 *
12 *-------------------------------------------------------------------------
13 */
14#include "postgres.h"
15
16#include "access/attmap.h"
17#include "catalog/pg_type.h"
18#include "nodes/makefuncs.h"
19#include "nodes/nodeFuncs.h"
20#include "nodes/pathnodes.h"
21#include "nodes/plannodes.h"
22#include "parser/parse_coerce.h"
24#include "parser/parsetree.h"
26#include "utils/lsyscache.h"
27
28
33
39
40typedef struct
41{
44
51
58
59static bool contain_aggs_of_level_walker(Node *node,
61static bool locate_agg_of_level_walker(Node *node,
63static bool contain_windowfuncs_walker(Node *node, void *context);
64static bool locate_windowfunc_walker(Node *node,
66static bool checkExprHasSubLink_walker(Node *node, void *context);
67static Relids offset_relid_set(Relids relids, int offset);
72
73
74/*
75 * contain_aggs_of_level -
76 * Check if an expression contains an aggregate function call of a
77 * specified query level.
78 *
79 * The objective of this routine is to detect whether there are aggregates
80 * belonging to the given query level. Aggregates belonging to subqueries
81 * or outer queries do NOT cause a true result. We must recurse into
82 * subqueries to detect outer-reference aggregates that logically belong to
83 * the specified query level.
84 */
85bool
86contain_aggs_of_level(Node *node, int levelsup)
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}
101
102static bool
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}
135
136/*
137 * locate_agg_of_level -
138 * Find the parse location of any aggregate of the specified query level.
139 *
140 * Returns -1 if no such agg is in the querytree, or if they all have
141 * unknown parse location. (The former case is probably caller error,
142 * but we don't bother to distinguish it from the latter case.)
143 *
144 * Note: it might seem appropriate to merge this functionality into
145 * contain_aggs_of_level, but that would complicate that function's API.
146 * Currently, the only uses of this function are for error reporting,
147 * and so shaving cycles probably isn't very important.
148 */
149int
150locate_agg_of_level(Node *node, int levelsup)
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}
168
169static bool
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}
208
209/*
210 * contain_windowfuncs -
211 * Check if an expression contains a window function call of the
212 * current query level.
213 */
214bool
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}
226
227static bool
228contain_windowfuncs_walker(Node *node, void *context)
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}
237
238/*
239 * locate_windowfunc -
240 * Find the parse location of any windowfunc of the current query level.
241 *
242 * Returns -1 if no such windowfunc is in the querytree, or if they all have
243 * unknown parse location. (The former case is probably caller error,
244 * but we don't bother to distinguish it from the latter case.)
245 *
246 * Note: it might seem appropriate to merge this functionality into
247 * contain_windowfuncs, but that would complicate that function's API.
248 * Currently, the only uses of this function are for error reporting,
249 * and so shaving cycles probably isn't very important.
250 */
251int
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}
269
270static bool
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}
287
288/*
289 * checkExprHasSubLink -
290 * Check if an expression contains a SubLink.
291 */
292bool
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}
304
305static bool
306checkExprHasSubLink_walker(Node *node, void *context)
307{
308 if (node == NULL)
309 return false;
310 if (IsA(node, SubLink))
311 return true; /* abort the tree traversal and return true */
313}
314
315/*
316 * Check for MULTIEXPR Param within expression tree
317 *
318 * We intentionally don't descend into SubLinks: only Params at the current
319 * query level are of interest.
320 */
321static bool
322contains_multiexpr_param(Node *node, void *context)
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}
334
335/*
336 * CombineRangeTables
337 * Adds the RTEs of 'src_rtable' into 'dst_rtable'
338 *
339 * This also adds the RTEPermissionInfos of 'src_perminfos' (belonging to the
340 * RTEs in 'src_rtable') into *dst_perminfos and also updates perminfoindex of
341 * the RTEs in 'src_rtable' to now point to the perminfos' indexes in
342 * *dst_perminfos.
343 *
344 * Note that this changes both 'dst_rtable' and 'dst_perminfos' destructively,
345 * so the caller should have better passed safe-to-modify copies.
346 */
347void
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}
368
369/*
370 * OffsetVarNodes - adjust Vars when appending one query's RT to another
371 *
372 * Find all Var nodes in the given tree with varlevelsup == sublevels_up,
373 * and increment their varno fields (rangetable indexes) by 'offset'.
374 * The varnosyn fields are adjusted similarly. Also, adjust other nodes
375 * that contain rangetable indexes, such as RangeTblRef and JoinExpr.
376 *
377 * NOTE: although this has the form of a walker, we cheat and modify the
378 * nodes in-place. The given expression tree should have been copied
379 * earlier to ensure that no unwanted side-effects occur!
380 */
381
382typedef struct
383{
387
388static bool
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}
475
476void
477OffsetVarNodes(Node *node, int offset, int sublevels_up)
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}
526
527static Relids
528offset_relid_set(Relids relids, int offset)
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}
538
539/*
540 * ChangeVarNodes - adjust Var nodes for a specific change of RT index
541 *
542 * Find all Var nodes in the given tree belonging to a specific relation
543 * (identified by sublevels_up and rt_index), and change their varno fields
544 * to 'new_index'. The varnosyn fields are changed too. Also, adjust other
545 * nodes that contain rangetable indexes, such as RangeTblRef and JoinExpr.
546 *
547 * NOTE: although this has the form of a walker, we cheat and modify the
548 * nodes in-place. The given expression tree should have been copied
549 * earlier to ensure that no unwanted side-effects occur!
550 */
551
552static bool
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}
664
665/*
666 * ChangeVarNodesExtended - similar to ChangeVarNodes, but with an additional
667 * 'callback' param
668 *
669 * ChangeVarNodes changes a given node and all of its underlying nodes. This
670 * version of function additionally takes a callback, which has a chance to
671 * process a node before ChangeVarNodes_walker. A callback returns a boolean
672 * value indicating if the given node should be skipped from further processing
673 * by ChangeVarNodes_walker. The callback is called only for expressions and
674 * other children nodes of a Query processed by a walker. Initial processing
675 * of the root Query doesn't involve the callback.
676 */
677void
678ChangeVarNodesExtended(Node *node, int rt_index, int new_index,
679 int sublevels_up, ChangeVarNodes_callback callback)
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}
732
733void
734ChangeVarNodes(Node *node, int rt_index, int new_index, int sublevels_up)
735{
736 ChangeVarNodesExtended(node, rt_index, new_index, sublevels_up, NULL);
737}
738
739/*
740 * ChangeVarNodesWalkExpression - process expression within the custom
741 * callback provided to the
742 * ChangeVarNodesExtended.
743 */
744bool
746{
747 return ChangeVarNodes_walker(node, context);
748}
749
750/*
751 * adjust_relid_set - substitute newrelid for oldrelid in a Relid set
752 *
753 * Attempt to remove oldrelid from a Relid set (as long as it's not a special
754 * varno). If oldrelid was found and removed, insert newrelid into a Relid
755 * set (as long as it's not a special varno). Therefore, when oldrelid is
756 * a special varno, this function does nothing. When newrelid is a special
757 * varno, this function behaves as delete.
758 */
759Relids
761{
763 {
764 /* Ensure we have a modifiable copy */
765 relids = bms_copy(relids);
766 /* Remove old, add new */
767 relids = bms_del_member(relids, oldrelid);
769 relids = bms_add_member(relids, newrelid);
770 }
771 return relids;
772}
773
774/*
775 * IncrementVarSublevelsUp - adjust Var nodes when pushing them down in tree
776 *
777 * Find all Var nodes in the given tree having varlevelsup >= min_sublevels_up,
778 * and add delta_sublevels_up to their varlevelsup value. This is needed when
779 * an expression that's correct for some nesting level is inserted into a
780 * subquery. Ordinarily the initial call has min_sublevels_up == 0 so that
781 * all Vars are affected. The point of min_sublevels_up is that we can
782 * increment it when we recurse into a sublink, so that local variables in
783 * that sublink are not affected, only outer references to vars that belong
784 * to the expression's original query level or parents thereof.
785 *
786 * Likewise for other nodes containing levelsup fields, such as Aggref.
787 *
788 * NOTE: although this has the form of a walker, we cheat and modify the
789 * Var nodes in-place. The given expression tree should have been copied
790 * earlier to ensure that no unwanted side-effects occur!
791 */
792
798
799static bool
802{
803 if (node == NULL)
804 return false;
805 if (IsA(node, Var))
806 {
807 Var *var = (Var *) node;
808
809 if (var->varlevelsup >= context->min_sublevels_up)
810 var->varlevelsup += context->delta_sublevels_up;
811 return false; /* done here */
812 }
813 if (IsA(node, CurrentOfExpr))
814 {
815 /* this should not happen */
816 if (context->min_sublevels_up == 0)
817 elog(ERROR, "cannot push down CurrentOfExpr");
818 return false;
819 }
820 if (IsA(node, Aggref))
821 {
822 Aggref *agg = (Aggref *) node;
823
824 if (agg->agglevelsup >= context->min_sublevels_up)
825 agg->agglevelsup += context->delta_sublevels_up;
826 /* fall through to recurse into argument */
827 }
828 if (IsA(node, GroupingFunc))
829 {
830 GroupingFunc *grp = (GroupingFunc *) node;
831
832 if (grp->agglevelsup >= context->min_sublevels_up)
833 grp->agglevelsup += context->delta_sublevels_up;
834 /* fall through to recurse into argument */
835 }
836 if (IsA(node, PlaceHolderVar))
837 {
839
840 if (phv->phlevelsup >= context->min_sublevels_up)
841 phv->phlevelsup += context->delta_sublevels_up;
842 /* fall through to recurse into argument */
843 }
844 if (IsA(node, ReturningExpr))
845 {
846 ReturningExpr *rexpr = (ReturningExpr *) node;
847
848 if (rexpr->retlevelsup >= context->min_sublevels_up)
849 rexpr->retlevelsup += context->delta_sublevels_up;
850 /* fall through to recurse into argument */
851 }
852 if (IsA(node, RangeTblEntry))
853 {
854 RangeTblEntry *rte = (RangeTblEntry *) node;
855
856 if (rte->rtekind == RTE_CTE)
857 {
858 if (rte->ctelevelsup >= context->min_sublevels_up)
859 rte->ctelevelsup += context->delta_sublevels_up;
860 }
861 return false; /* allow range_table_walker to continue */
862 }
863 if (IsA(node, Query))
864 {
865 /* Recurse into subselects */
866 bool result;
867
868 context->min_sublevels_up++;
869 result = query_tree_walker((Query *) node,
871 context,
873 context->min_sublevels_up--;
874 return result;
875 }
877}
878
879void
880IncrementVarSublevelsUp(Node *node, int delta_sublevels_up,
881 int min_sublevels_up)
882{
884
885 context.delta_sublevels_up = delta_sublevels_up;
886 context.min_sublevels_up = min_sublevels_up;
887
888 /*
889 * Must be prepared to start with a Query or a bare expression tree; if
890 * it's a Query, we don't want to increment sublevels_up.
891 */
894 &context,
896}
897
898/*
899 * IncrementVarSublevelsUp_rtable -
900 * Same as IncrementVarSublevelsUp, but to be invoked on a range table.
901 */
902void
903IncrementVarSublevelsUp_rtable(List *rtable, int delta_sublevels_up,
904 int min_sublevels_up)
905{
907
908 context.delta_sublevels_up = delta_sublevels_up;
909 context.min_sublevels_up = min_sublevels_up;
910
911 range_table_walker(rtable,
913 &context,
915}
916
917/*
918 * SetVarReturningType - adjust Var nodes for a specified varreturningtype.
919 *
920 * Find all Var nodes referring to the specified result relation in the given
921 * expression and set their varreturningtype to the specified value.
922 *
923 * NOTE: although this has the form of a walker, we cheat and modify the
924 * Var nodes in-place. The given expression tree should have been copied
925 * earlier to ensure that no unwanted side-effects occur!
926 */
927
934
935static bool
937{
938 if (node == NULL)
939 return false;
940 if (IsA(node, Var))
941 {
942 Var *var = (Var *) node;
943
944 if (var->varno == context->result_relation &&
945 var->varlevelsup == context->sublevels_up)
946 var->varreturningtype = context->returning_type;
947
948 return false;
949 }
950
951 if (IsA(node, Query))
952 {
953 /* Recurse into subselects */
954 bool result;
955
956 context->sublevels_up++;
958 context, 0);
959 context->sublevels_up--;
960 return result;
961 }
963}
964
965static void
966SetVarReturningType(Node *node, int result_relation, int sublevels_up,
967 VarReturningType returning_type)
968{
970
971 context.result_relation = result_relation;
972 context.sublevels_up = sublevels_up;
973 context.returning_type = returning_type;
974
975 /* Expect to start with an expression */
976 SetVarReturningType_walker(node, &context);
977}
978
979/*
980 * rangeTableEntry_used - detect whether an RTE is referenced somewhere
981 * in var nodes or join or setOp trees of a query or expression.
982 */
983
989
990static bool
993{
994 if (node == NULL)
995 return false;
996 if (IsA(node, Var))
997 {
998 Var *var = (Var *) node;
999
1000 if (var->varlevelsup == context->sublevels_up &&
1001 (var->varno == context->rt_index ||
1002 bms_is_member(context->rt_index, var->varnullingrels)))
1003 return true;
1004 return false;
1005 }
1006 if (IsA(node, CurrentOfExpr))
1007 {
1008 CurrentOfExpr *cexpr = (CurrentOfExpr *) node;
1009
1010 if (context->sublevels_up == 0 &&
1011 cexpr->cvarno == context->rt_index)
1012 return true;
1013 return false;
1014 }
1015 if (IsA(node, RangeTblRef))
1016 {
1017 RangeTblRef *rtr = (RangeTblRef *) node;
1018
1019 if (rtr->rtindex == context->rt_index &&
1020 context->sublevels_up == 0)
1021 return true;
1022 /* the subquery itself is visited separately */
1023 return false;
1024 }
1025 if (IsA(node, JoinExpr))
1026 {
1027 JoinExpr *j = (JoinExpr *) node;
1028
1029 if (j->rtindex == context->rt_index &&
1030 context->sublevels_up == 0)
1031 return true;
1032 /* fall through to examine children */
1033 }
1034 /* Shouldn't need to handle planner auxiliary nodes here */
1035 Assert(!IsA(node, PlaceHolderVar));
1036 Assert(!IsA(node, PlanRowMark));
1037 Assert(!IsA(node, SpecialJoinInfo));
1038 Assert(!IsA(node, AppendRelInfo));
1039 Assert(!IsA(node, PlaceHolderInfo));
1040 Assert(!IsA(node, MinMaxAggInfo));
1041
1042 if (IsA(node, Query))
1043 {
1044 /* Recurse into subselects */
1045 bool result;
1046
1047 context->sublevels_up++;
1049 context, 0);
1050 context->sublevels_up--;
1051 return result;
1052 }
1054}
1055
1056bool
1057rangeTableEntry_used(Node *node, int rt_index, int sublevels_up)
1058{
1060
1061 context.rt_index = rt_index;
1062 context.sublevels_up = sublevels_up;
1063
1064 /*
1065 * Must be prepared to start with a Query or a bare expression tree; if
1066 * it's a Query, we don't want to increment sublevels_up.
1067 */
1070 &context,
1071 0);
1072}
1073
1074
1075/*
1076 * If the given Query is an INSERT ... SELECT construct, extract and
1077 * return the sub-Query node that represents the SELECT part. Otherwise
1078 * return the given Query.
1079 *
1080 * If subquery_ptr is not NULL, then *subquery_ptr is set to the location
1081 * of the link to the SELECT subquery inside parsetree, or NULL if not an
1082 * INSERT ... SELECT.
1083 *
1084 * This is a hack needed because transformations on INSERT ... SELECTs that
1085 * appear in rule actions should be applied to the source SELECT, not to the
1086 * INSERT part. Perhaps this can be cleaned up with redesigned querytrees.
1087 */
1088Query *
1090{
1094
1095 if (subquery_ptr)
1096 *subquery_ptr = NULL;
1097
1098 if (parsetree == NULL)
1099 return parsetree;
1100 if (parsetree->commandType != CMD_INSERT)
1101 return parsetree;
1102
1103 /*
1104 * Currently, this is ONLY applied to rule-action queries, and so we
1105 * expect to find the OLD and NEW placeholder entries in the given query.
1106 * If they're not there, it must be an INSERT/SELECT in which they've been
1107 * pushed down to the SELECT.
1108 */
1109 if (list_length(parsetree->rtable) >= 2 &&
1110 strcmp(rt_fetch(PRS2_OLD_VARNO, parsetree->rtable)->eref->aliasname,
1111 "old") == 0 &&
1112 strcmp(rt_fetch(PRS2_NEW_VARNO, parsetree->rtable)->eref->aliasname,
1113 "new") == 0)
1114 return parsetree;
1115 Assert(parsetree->jointree && IsA(parsetree->jointree, FromExpr));
1116 if (list_length(parsetree->jointree->fromlist) != 1)
1117 elog(ERROR, "expected to find SELECT subquery");
1118 rtr = (RangeTblRef *) linitial(parsetree->jointree->fromlist);
1119 if (!IsA(rtr, RangeTblRef))
1120 elog(ERROR, "expected to find SELECT subquery");
1121 selectrte = rt_fetch(rtr->rtindex, parsetree->rtable);
1122 if (!(selectrte->rtekind == RTE_SUBQUERY &&
1123 selectrte->subquery &&
1124 IsA(selectrte->subquery, Query) &&
1125 selectrte->subquery->commandType == CMD_SELECT))
1126 elog(ERROR, "expected to find SELECT subquery");
1127 selectquery = selectrte->subquery;
1128 if (list_length(selectquery->rtable) >= 2 &&
1129 strcmp(rt_fetch(PRS2_OLD_VARNO, selectquery->rtable)->eref->aliasname,
1130 "old") == 0 &&
1131 strcmp(rt_fetch(PRS2_NEW_VARNO, selectquery->rtable)->eref->aliasname,
1132 "new") == 0)
1133 {
1134 if (subquery_ptr)
1135 *subquery_ptr = &(selectrte->subquery);
1136 return selectquery;
1137 }
1138 elog(ERROR, "could not find rule placeholders");
1139 return NULL; /* not reached */
1140}
1141
1142
1143/*
1144 * Add the given qualifier condition to the query's WHERE clause
1145 */
1146void
1147AddQual(Query *parsetree, Node *qual)
1148{
1149 Node *copy;
1150
1151 if (qual == NULL)
1152 return;
1153
1154 if (parsetree->commandType == CMD_UTILITY)
1155 {
1156 /*
1157 * There's noplace to put the qual on a utility statement.
1158 *
1159 * If it's a NOTIFY, silently ignore the qual; this means that the
1160 * NOTIFY will execute, whether or not there are any qualifying rows.
1161 * While clearly wrong, this is much more useful than refusing to
1162 * execute the rule at all, and extra NOTIFY events are harmless for
1163 * typical uses of NOTIFY.
1164 *
1165 * If it isn't a NOTIFY, error out, since unconditional execution of
1166 * other utility stmts is unlikely to be wanted. (This case is not
1167 * currently allowed anyway, but keep the test for safety.)
1168 */
1169 if (parsetree->utilityStmt && IsA(parsetree->utilityStmt, NotifyStmt))
1170 return;
1171 else
1172 ereport(ERROR,
1174 errmsg("conditional utility statements are not implemented")));
1175 }
1176
1177 if (parsetree->setOperations != NULL)
1178 {
1179 /*
1180 * There's noplace to put the qual on a setop statement, either. (This
1181 * could be fixed, but right now the planner simply ignores any qual
1182 * condition on a setop query.)
1183 */
1184 ereport(ERROR,
1186 errmsg("conditional UNION/INTERSECT/EXCEPT statements are not implemented")));
1187 }
1188
1189 /* INTERSECT wants the original, but we need to copy - Jan */
1190 copy = copyObject(qual);
1191
1192 parsetree->jointree->quals = make_and_qual(parsetree->jointree->quals,
1193 copy);
1194
1195 /*
1196 * We had better not have stuck an aggregate into the WHERE clause.
1197 */
1199
1200 /*
1201 * Make sure query is marked correctly if added qual has sublinks. Need
1202 * not search qual when query is already marked.
1203 */
1204 if (!parsetree->hasSubLinks)
1205 parsetree->hasSubLinks = checkExprHasSubLink(copy);
1206}
1207
1208
1209/*
1210 * Invert the given clause and add it to the WHERE qualifications of the
1211 * given querytree. Inversion means "x IS NOT TRUE", not just "NOT x",
1212 * else we will do the wrong thing when x evaluates to NULL.
1213 */
1214void
1215AddInvertedQual(Query *parsetree, Node *qual)
1216{
1218
1219 if (qual == NULL)
1220 return;
1221
1222 /* Need not copy input qual, because AddQual will... */
1224 invqual->arg = (Expr *) qual;
1225 invqual->booltesttype = IS_NOT_TRUE;
1226 invqual->location = -1;
1227
1228 AddQual(parsetree, (Node *) invqual);
1229}
1230
1231
1232/*
1233 * add_nulling_relids() finds Vars and PlaceHolderVars that belong to any
1234 * of the target_relids, and adds added_relids to their varnullingrels
1235 * and phnullingrels fields. If target_relids is NULL, all level-zero
1236 * Vars and PHVs are modified.
1237 */
1238Node *
1240 const Bitmapset *target_relids,
1241 const Bitmapset *added_relids)
1242{
1244
1245 context.target_relids = target_relids;
1246 context.added_relids = added_relids;
1247 context.sublevels_up = 0;
1250 &context,
1251 0);
1252}
1253
1254static Node *
1257{
1258 if (node == NULL)
1259 return NULL;
1260 if (IsA(node, Var))
1261 {
1262 Var *var = (Var *) node;
1263
1264 if (var->varlevelsup == context->sublevels_up &&
1265 (context->target_relids == NULL ||
1266 bms_is_member(var->varno, context->target_relids)))
1267 {
1268 Relids newnullingrels = bms_union(var->varnullingrels,
1269 context->added_relids);
1270
1271 /* Copy the Var ... */
1272 var = copyObject(var);
1273 /* ... and replace the copy's varnullingrels field */
1274 var->varnullingrels = newnullingrels;
1275 return (Node *) var;
1276 }
1277 /* Otherwise fall through to copy the Var normally */
1278 }
1279 else if (IsA(node, PlaceHolderVar))
1280 {
1281 PlaceHolderVar *phv = (PlaceHolderVar *) node;
1282
1283 if (phv->phlevelsup == context->sublevels_up &&
1284 (context->target_relids == NULL ||
1285 bms_overlap(phv->phrels, context->target_relids)))
1286 {
1287 Relids newnullingrels = bms_union(phv->phnullingrels,
1288 context->added_relids);
1289
1290 /*
1291 * We don't modify the contents of the PHV's expression, only add
1292 * to phnullingrels. This corresponds to assuming that the PHV
1293 * will be evaluated at the same level as before, then perhaps be
1294 * nulled as it bubbles up. Hence, just flat-copy the node ...
1295 */
1297 memcpy(phv, node, sizeof(PlaceHolderVar));
1298 /* ... and replace the copy's phnullingrels field */
1299 phv->phnullingrels = newnullingrels;
1300 return (Node *) phv;
1301 }
1302 /* Otherwise fall through to copy the PlaceHolderVar normally */
1303 }
1304 else if (IsA(node, Query))
1305 {
1306 /* Recurse into RTE or sublink subquery */
1307 Query *newnode;
1308
1309 context->sublevels_up++;
1310 newnode = query_tree_mutator((Query *) node,
1312 context,
1313 0);
1314 context->sublevels_up--;
1315 return (Node *) newnode;
1316 }
1318}
1319
1320/*
1321 * remove_nulling_relids() removes mentions of the specified RT index(es)
1322 * in Var.varnullingrels and PlaceHolderVar.phnullingrels fields within
1323 * the given expression, except in nodes belonging to rels listed in
1324 * except_relids.
1325 */
1326Node *
1328 const Bitmapset *removable_relids,
1329 const Bitmapset *except_relids)
1330{
1332
1333 context.removable_relids = removable_relids;
1334 context.except_relids = except_relids;
1335 context.sublevels_up = 0;
1338 &context,
1339 0);
1340}
1341
1342static Node *
1345{
1346 if (node == NULL)
1347 return NULL;
1348 if (IsA(node, Var))
1349 {
1350 Var *var = (Var *) node;
1351
1352 if (var->varlevelsup == context->sublevels_up &&
1353 !bms_is_member(var->varno, context->except_relids) &&
1354 bms_overlap(var->varnullingrels, context->removable_relids))
1355 {
1356 /* Copy the Var ... */
1357 var = copyObject(var);
1358 /* ... and replace the copy's varnullingrels field */
1359 var->varnullingrels = bms_difference(var->varnullingrels,
1360 context->removable_relids);
1361 return (Node *) var;
1362 }
1363 /* Otherwise fall through to copy the Var normally */
1364 }
1365 else if (IsA(node, PlaceHolderVar))
1366 {
1367 PlaceHolderVar *phv = (PlaceHolderVar *) node;
1368
1369 if (phv->phlevelsup == context->sublevels_up &&
1370 !bms_overlap(phv->phrels, context->except_relids))
1371 {
1372 /*
1373 * Note: it might seem desirable to remove the PHV altogether if
1374 * phnullingrels goes to empty. Currently we dare not do that
1375 * because we use PHVs in some cases to enforce separate identity
1376 * of subexpressions; see wrap_option usages in prepjointree.c.
1377 */
1378 /* Copy the PlaceHolderVar and mutate what's below ... */
1379 phv = (PlaceHolderVar *)
1382 context);
1383 /* ... and replace the copy's phnullingrels field */
1384 phv->phnullingrels = bms_difference(phv->phnullingrels,
1385 context->removable_relids);
1386 /* We must also update phrels, if it contains a removable RTI */
1387 phv->phrels = bms_difference(phv->phrels,
1388 context->removable_relids);
1389 Assert(!bms_is_empty(phv->phrels));
1390 return (Node *) phv;
1391 }
1392 /* Otherwise fall through to copy the PlaceHolderVar normally */
1393 }
1394 else if (IsA(node, Query))
1395 {
1396 /* Recurse into RTE or sublink subquery */
1397 Query *newnode;
1398
1399 context->sublevels_up++;
1400 newnode = query_tree_mutator((Query *) node,
1402 context,
1403 0);
1404 context->sublevels_up--;
1405 return (Node *) newnode;
1406 }
1408}
1409
1410
1411/*
1412 * replace_rte_variables() finds all Vars in an expression tree
1413 * that reference a particular RTE, and replaces them with substitute
1414 * expressions obtained from a caller-supplied callback function.
1415 *
1416 * When invoking replace_rte_variables on a portion of a Query, pass the
1417 * address of the containing Query's hasSubLinks field as outer_hasSubLinks.
1418 * Otherwise, pass NULL, but inserting a SubLink into a non-Query expression
1419 * will then cause an error.
1420 *
1421 * Note: the business with inserted_sublink is needed to update hasSubLinks
1422 * in subqueries when the replacement adds a subquery inside a subquery.
1423 * Messy, isn't it? We do not need to do similar pushups for hasAggs,
1424 * because it isn't possible for this transformation to insert a level-zero
1425 * aggregate reference into a subquery --- it could only insert outer aggs.
1426 * Likewise for hasWindowFuncs.
1427 *
1428 * Note: usually, we'd not expose the mutator function or context struct
1429 * for a function like this. We do so because callbacks often find it
1430 * convenient to recurse directly to the mutator on sub-expressions of
1431 * what they will return.
1432 */
1433Node *
1434replace_rte_variables(Node *node, int target_varno, int sublevels_up,
1436 void *callback_arg,
1437 bool *outer_hasSubLinks)
1438{
1439 Node *result;
1441
1442 context.callback = callback;
1443 context.callback_arg = callback_arg;
1444 context.target_varno = target_varno;
1445 context.sublevels_up = sublevels_up;
1446
1447 /*
1448 * We try to initialize inserted_sublink to true if there is no need to
1449 * detect new sublinks because the query already has some.
1450 */
1451 if (node && IsA(node, Query))
1452 context.inserted_sublink = ((Query *) node)->hasSubLinks;
1453 else if (outer_hasSubLinks)
1454 context.inserted_sublink = *outer_hasSubLinks;
1455 else
1456 context.inserted_sublink = false;
1457
1458 /*
1459 * Must be prepared to start with a Query or a bare expression tree; if
1460 * it's a Query, we don't want to increment sublevels_up.
1461 */
1464 &context,
1465 0);
1466
1467 if (context.inserted_sublink)
1468 {
1469 if (result && IsA(result, Query))
1470 ((Query *) result)->hasSubLinks = true;
1471 else if (outer_hasSubLinks)
1472 *outer_hasSubLinks = true;
1473 else
1474 elog(ERROR, "replace_rte_variables inserted a SubLink, but has noplace to record it");
1475 }
1476
1477 return result;
1478}
1479
1480Node *
1483{
1484 if (node == NULL)
1485 return NULL;
1486 if (IsA(node, Var))
1487 {
1488 Var *var = (Var *) node;
1489
1490 if (var->varno == context->target_varno &&
1491 var->varlevelsup == context->sublevels_up)
1492 {
1493 /* Found a matching variable, make the substitution */
1494 Node *newnode;
1495
1496 newnode = context->callback(var, context);
1497 /* Detect if we are adding a sublink to query */
1498 if (!context->inserted_sublink)
1500 return newnode;
1501 }
1502 /* otherwise fall through to copy the var normally */
1503 }
1504 else if (IsA(node, CurrentOfExpr))
1505 {
1506 CurrentOfExpr *cexpr = (CurrentOfExpr *) node;
1507
1508 if (cexpr->cvarno == context->target_varno &&
1509 context->sublevels_up == 0)
1510 {
1511 /*
1512 * We get here if a WHERE CURRENT OF expression turns out to apply
1513 * to a view. Someday we might be able to translate the
1514 * expression to apply to an underlying table of the view, but
1515 * right now it's not implemented.
1516 */
1517 ereport(ERROR,
1519 errmsg("WHERE CURRENT OF on a view is not implemented")));
1520 }
1521 /* otherwise fall through to copy the expr normally */
1522 }
1523 else if (IsA(node, Query))
1524 {
1525 /* Recurse into RTE subquery or not-yet-planned sublink subquery */
1526 Query *newnode;
1528
1529 context->sublevels_up++;
1531 context->inserted_sublink = ((Query *) node)->hasSubLinks;
1532 newnode = query_tree_mutator((Query *) node,
1534 context,
1535 0);
1536 newnode->hasSubLinks |= context->inserted_sublink;
1538 context->sublevels_up--;
1539 return (Node *) newnode;
1540 }
1542}
1543
1544
1545/*
1546 * map_variable_attnos() finds all user-column Vars in an expression tree
1547 * that reference a particular RTE, and adjusts their varattnos according
1548 * to the given mapping array (varattno n is replaced by attno_map[n-1]).
1549 * Vars for system columns are not modified.
1550 *
1551 * A zero in the mapping array represents a dropped column, which should not
1552 * appear in the expression.
1553 *
1554 * If the expression tree contains a whole-row Var for the target RTE,
1555 * *found_whole_row is set to true. In addition, if to_rowtype is
1556 * not InvalidOid, we replace the Var with a Var of that vartype, inserting
1557 * a ConvertRowtypeExpr to map back to the rowtype expected by the expression.
1558 * (Therefore, to_rowtype had better be a child rowtype of the rowtype of the
1559 * RTE we're changing references to.) Callers that don't provide to_rowtype
1560 * should report an error if *found_whole_row is true; we don't do that here
1561 * because we don't know exactly what wording for the error message would
1562 * be most appropriate. The caller will be aware of the context.
1563 *
1564 * This could be built using replace_rte_variables and a callback function,
1565 * but since we don't ever need to insert sublinks, replace_rte_variables is
1566 * overly complicated.
1567 */
1568
1569typedef struct
1570{
1571 int target_varno; /* RTE index to search for */
1572 int sublevels_up; /* (current) nesting depth */
1573 const AttrMap *attno_map; /* map array for user attnos */
1574 Oid to_rowtype; /* change whole-row Vars to this type */
1575 bool *found_whole_row; /* output flag */
1577
1578static Node *
1581{
1582 if (node == NULL)
1583 return NULL;
1584 if (IsA(node, Var))
1585 {
1586 Var *var = (Var *) node;
1587
1588 if (var->varno == context->target_varno &&
1589 var->varlevelsup == context->sublevels_up)
1590 {
1591 /* Found a matching variable, make the substitution */
1593 int attno = var->varattno;
1594
1595 *newvar = *var; /* initially copy all fields of the Var */
1596
1597 if (attno > 0)
1598 {
1599 /* user-defined column, replace attno */
1600 if (attno > context->attno_map->maplen ||
1601 context->attno_map->attnums[attno - 1] == 0)
1602 elog(ERROR, "unexpected varattno %d in expression to be mapped",
1603 attno);
1604 newvar->varattno = context->attno_map->attnums[attno - 1];
1605 /* If the syntactic referent is same RTE, fix it too */
1606 if (newvar->varnosyn == context->target_varno)
1607 newvar->varattnosyn = newvar->varattno;
1608 }
1609 else if (attno == 0)
1610 {
1611 /* whole-row variable, warn caller */
1612 *(context->found_whole_row) = true;
1613
1614 /* If the caller expects us to convert the Var, do so. */
1615 if (OidIsValid(context->to_rowtype) &&
1616 context->to_rowtype != var->vartype)
1617 {
1619
1620 /* This certainly won't work for a RECORD variable. */
1621 Assert(var->vartype != RECORDOID);
1622
1623 /* Var itself is changed to the requested type. */
1624 newvar->vartype = context->to_rowtype;
1625
1626 /*
1627 * Add a conversion node on top to convert back to the
1628 * original type expected by the expression.
1629 */
1631 r->arg = (Expr *) newvar;
1632 r->resulttype = var->vartype;
1633 r->convertformat = COERCE_IMPLICIT_CAST;
1634 r->location = -1;
1635
1636 return (Node *) r;
1637 }
1638 }
1639 return (Node *) newvar;
1640 }
1641 /* otherwise fall through to copy the var normally */
1642 }
1643 else if (IsA(node, ConvertRowtypeExpr))
1644 {
1646 Var *var = (Var *) r->arg;
1647
1648 /*
1649 * If this is coercing a whole-row Var that we need to convert, then
1650 * just convert the Var without adding an extra ConvertRowtypeExpr.
1651 * Effectively we're simplifying var::parenttype::grandparenttype into
1652 * just var::grandparenttype. This avoids building stacks of CREs if
1653 * this function is applied repeatedly.
1654 */
1655 if (IsA(var, Var) &&
1656 var->varno == context->target_varno &&
1657 var->varlevelsup == context->sublevels_up &&
1658 var->varattno == 0 &&
1659 OidIsValid(context->to_rowtype) &&
1660 context->to_rowtype != var->vartype)
1661 {
1664
1665 /* whole-row variable, warn caller */
1666 *(context->found_whole_row) = true;
1667
1668 *newvar = *var; /* initially copy all fields of the Var */
1669
1670 /* This certainly won't work for a RECORD variable. */
1671 Assert(var->vartype != RECORDOID);
1672
1673 /* Var itself is changed to the requested type. */
1674 newvar->vartype = context->to_rowtype;
1675
1677 *newnode = *r; /* initially copy all fields of the CRE */
1678 newnode->arg = (Expr *) newvar;
1679
1680 return (Node *) newnode;
1681 }
1682 /* otherwise fall through to process the expression normally */
1683 }
1684 else if (IsA(node, Query))
1685 {
1686 /* Recurse into RTE subquery or not-yet-planned sublink subquery */
1687 Query *newnode;
1688
1689 context->sublevels_up++;
1690 newnode = query_tree_mutator((Query *) node,
1692 context,
1693 0);
1694 context->sublevels_up--;
1695 return (Node *) newnode;
1696 }
1698}
1699
1700Node *
1702 int target_varno, int sublevels_up,
1703 const AttrMap *attno_map,
1704 Oid to_rowtype, bool *found_whole_row)
1705{
1707
1708 context.target_varno = target_varno;
1709 context.sublevels_up = sublevels_up;
1710 context.attno_map = attno_map;
1711 context.to_rowtype = to_rowtype;
1712 context.found_whole_row = found_whole_row;
1713
1714 *found_whole_row = false;
1715
1716 /*
1717 * Must be prepared to start with a Query or a bare expression tree; if
1718 * it's a Query, we don't want to increment sublevels_up.
1719 */
1722 &context,
1723 0);
1724}
1725
1726
1727/*
1728 * ReplaceVarsFromTargetList - replace Vars with items from a targetlist
1729 *
1730 * Vars matching target_varno and sublevels_up are replaced by the
1731 * entry with matching resno from targetlist, if there is one.
1732 *
1733 * If there is no matching resno for such a Var, the action depends on the
1734 * nomatch_option:
1735 * REPLACEVARS_REPORT_ERROR: throw an error
1736 * REPLACEVARS_CHANGE_VARNO: change Var's varno to nomatch_varno
1737 * REPLACEVARS_SUBSTITUTE_NULL: replace Var with a NULL Const of same type
1738 *
1739 * The caller must also provide target_rte, the RTE describing the target
1740 * relation. This is needed to handle whole-row Vars referencing the target.
1741 * We expand such Vars into RowExpr constructs.
1742 *
1743 * In addition, for INSERT/UPDATE/DELETE/MERGE queries, the caller must
1744 * provide result_relation, the index of the result relation in the rewritten
1745 * query. This is needed to handle OLD/NEW RETURNING list Vars referencing
1746 * target_varno. When such Vars are expanded, their varreturningtype is
1747 * copied onto any replacement Vars referencing result_relation. In addition,
1748 * if the replacement expression from the targetlist is not simply a Var
1749 * referencing result_relation, it is wrapped in a ReturningExpr node (causing
1750 * the executor to return NULL if the OLD/NEW row doesn't exist).
1751 *
1752 * Note that ReplaceVarFromTargetList always generates the replacement
1753 * expression with varlevelsup = 0. The caller is responsible for adjusting
1754 * the varlevelsup if needed. This simplifies the caller's life if it wants to
1755 * cache the replacement expressions.
1756 *
1757 * outer_hasSubLinks works the same as for replace_rte_variables().
1758 */
1759
1768
1769static Node *
1772{
1774 Node *newnode;
1775
1777 rcon->target_rte,
1778 rcon->targetlist,
1779 rcon->result_relation,
1780 rcon->nomatch_option,
1781 rcon->nomatch_varno);
1782
1783 /* Must adjust varlevelsup if replaced Var is within a subquery */
1784 if (var->varlevelsup > 0)
1786
1787 return newnode;
1788}
1789
1790Node *
1792 RangeTblEntry *target_rte,
1793 List *targetlist,
1794 int result_relation,
1795 ReplaceVarsNoMatchOption nomatch_option,
1796 int nomatch_varno)
1797{
1799
1800 if (var->varattno == InvalidAttrNumber)
1801 {
1802 /* Must expand whole-tuple reference into RowExpr */
1803 RowExpr *rowexpr;
1804 List *colnames;
1805 List *fields;
1806 ListCell *lc;
1807
1808 /*
1809 * If generating an expansion for a var of a named rowtype (ie, this
1810 * is a plain relation RTE), then we must include dummy items for
1811 * dropped columns. If the var is RECORD (ie, this is a JOIN), then
1812 * omit dropped columns. In the latter case, attach column names to
1813 * the RowExpr for use of the executor and ruleutils.c.
1814 *
1815 * In order to be able to cache the results, we always generate the
1816 * expansion with varlevelsup = 0. The caller is responsible for
1817 * adjusting it if needed.
1818 *
1819 * The varreturningtype is copied onto each individual field Var, so
1820 * that it is handled correctly when we recurse.
1821 */
1822 expandRTE(target_rte,
1823 var->varno, 0 /* not varlevelsup */ ,
1824 var->varreturningtype, var->location,
1825 (var->vartype != RECORDOID),
1826 &colnames, &fields);
1827 rowexpr = makeNode(RowExpr);
1828 /* the fields will be set below */
1829 rowexpr->args = NIL;
1830 rowexpr->row_typeid = var->vartype;
1831 rowexpr->row_format = COERCE_IMPLICIT_CAST;
1832 rowexpr->colnames = (var->vartype == RECORDOID) ? colnames : NIL;
1833 rowexpr->location = var->location;
1834 /* Adjust the generated per-field Vars... */
1835 foreach(lc, fields)
1836 {
1837 Node *field = lfirst(lc);
1838
1839 if (field && IsA(field, Var))
1840 field = ReplaceVarFromTargetList((Var *) field,
1841 target_rte,
1842 targetlist,
1843 result_relation,
1844 nomatch_option,
1845 nomatch_varno);
1846 rowexpr->args = lappend(rowexpr->args, field);
1847 }
1848
1849 /* Wrap it in a ReturningExpr, if needed, per comments above */
1851 {
1853
1854 rexpr->retlevelsup = 0;
1855 rexpr->retold = (var->varreturningtype == VAR_RETURNING_OLD);
1856 rexpr->retexpr = (Expr *) rowexpr;
1857
1858 return (Node *) rexpr;
1859 }
1860
1861 return (Node *) rowexpr;
1862 }
1863
1864 /* Normal case referencing one targetlist element */
1865 tle = get_tle_by_resno(targetlist, var->varattno);
1866
1867 if (tle == NULL || tle->resjunk)
1868 {
1869 /* Failed to find column in targetlist */
1870 switch (nomatch_option)
1871 {
1873 /* fall through, throw error below */
1874 break;
1875
1877 {
1878 Var *newvar = copyObject(var);
1879
1880 newvar->varno = nomatch_varno;
1881 newvar->varlevelsup = 0;
1882 /* we leave the syntactic referent alone */
1883 return (Node *) newvar;
1884 }
1885
1887 {
1888 /*
1889 * If Var is of domain type, we must add a CoerceToDomain
1890 * node, in case there is a NOT NULL domain constraint.
1891 */
1893 bool vartypbyval;
1894
1895 get_typlenbyval(var->vartype, &vartyplen, &vartypbyval);
1896 return coerce_null_to_domain(var->vartype,
1897 var->vartypmod,
1898 var->varcollid,
1899 vartyplen,
1900 vartypbyval);
1901 }
1902 }
1903 elog(ERROR, "could not find replacement targetlist entry for attno %d",
1904 var->varattno);
1905 return NULL; /* keep compiler quiet */
1906 }
1907 else
1908 {
1909 /* Make a copy of the tlist item to return */
1910 Expr *newnode = copyObject(tle->expr);
1911
1912 /*
1913 * Check to see if the tlist item contains a PARAM_MULTIEXPR Param,
1914 * and throw error if so. This case could only happen when expanding
1915 * an ON UPDATE rule's NEW variable and the referenced tlist item in
1916 * the original UPDATE command is part of a multiple assignment. There
1917 * seems no practical way to handle such cases without multiple
1918 * evaluation of the multiple assignment's sub-select, which would
1919 * create semantic oddities that users of rules would probably prefer
1920 * not to cope with. So treat it as an unimplemented feature.
1921 */
1923 ereport(ERROR,
1925 errmsg("NEW variables in ON UPDATE rules cannot reference columns that are part of a multiple assignment in the subject UPDATE command")));
1926
1927 /* Handle any OLD/NEW RETURNING list Vars */
1929 {
1930 /*
1931 * Copy varreturningtype onto any Vars in the tlist item that
1932 * refer to result_relation (which had better be non-zero).
1933 */
1934 if (result_relation == 0)
1935 elog(ERROR, "variable returning old/new found outside RETURNING list");
1936
1937 SetVarReturningType((Node *) newnode, result_relation,
1938 0, var->varreturningtype);
1939
1940 /* Wrap it in a ReturningExpr, if needed, per comments above */
1941 if (!IsA(newnode, Var) ||
1942 ((Var *) newnode)->varno != result_relation ||
1943 ((Var *) newnode)->varlevelsup != 0)
1944 {
1946
1947 rexpr->retlevelsup = 0;
1948 rexpr->retold = (var->varreturningtype == VAR_RETURNING_OLD);
1949 rexpr->retexpr = newnode;
1950
1951 newnode = (Expr *) rexpr;
1952 }
1953 }
1954
1955 return (Node *) newnode;
1956 }
1957}
1958
1959Node *
1961 int target_varno, int sublevels_up,
1962 RangeTblEntry *target_rte,
1963 List *targetlist,
1964 int result_relation,
1965 ReplaceVarsNoMatchOption nomatch_option,
1966 int nomatch_varno,
1967 bool *outer_hasSubLinks)
1968{
1970
1971 context.target_rte = target_rte;
1972 context.targetlist = targetlist;
1973 context.result_relation = result_relation;
1974 context.nomatch_option = nomatch_option;
1975 context.nomatch_varno = nomatch_varno;
1976
1977 return replace_rte_variables(node, target_varno, sublevels_up,
1979 &context,
1980 outer_hasSubLinks);
1981}
#define InvalidAttrNumber
Definition attnum.h:23
Bitmapset * bms_difference(const Bitmapset *a, const Bitmapset *b)
Definition bitmapset.c:346
int bms_next_member(const Bitmapset *a, int prevbit)
Definition bitmapset.c:1290
Bitmapset * bms_del_member(Bitmapset *a, int x)
Definition bitmapset.c:852
bool bms_is_member(int x, const Bitmapset *a)
Definition bitmapset.c:510
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition bitmapset.c:799
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition bitmapset.c:251
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition bitmapset.c:575
Bitmapset * bms_copy(const Bitmapset *a)
Definition bitmapset.c:122
#define bms_is_empty(a)
Definition bitmapset.h:118
#define Assert(condition)
Definition c.h:945
int16_t int16
Definition c.h:613
#define OidIsValid(objectId)
Definition c.h:860
int errcode(int sqlerrcode)
Definition elog.c:874
#define ERROR
Definition elog.h:39
#define elog(elevel,...)
Definition elog.h:226
#define ereport(elevel,...)
Definition elog.h:150
#define palloc_object(type)
Definition fe_memutils.h:74
int j
Definition isn.c:78
List * lappend(List *list, void *datum)
Definition list.c:339
List * list_concat(List *list1, const List *list2)
Definition list.c:561
void get_typlenbyval(Oid typid, int16 *typlen, bool *typbyval)
Definition lsyscache.c:2471
Node * make_and_qual(Node *qual1, Node *qual2)
Definition makefuncs.c:780
#define expression_tree_mutator(n, m, c)
Definition nodeFuncs.h:155
#define query_or_expression_tree_mutator(n, m, c, f)
Definition nodeFuncs.h:173
#define range_table_walker(rt, w, c, f)
Definition nodeFuncs.h:163
#define query_tree_walker(q, w, c, f)
Definition nodeFuncs.h:158
#define query_or_expression_tree_walker(n, w, c, f)
Definition nodeFuncs.h:171
#define expression_tree_walker(n, w, c)
Definition nodeFuncs.h:153
#define query_tree_mutator(q, m, c, f)
Definition nodeFuncs.h:160
#define QTW_IGNORE_RC_SUBQUERIES
Definition nodeFuncs.h:24
#define QTW_EXAMINE_RTES_BEFORE
Definition nodeFuncs.h:27
#define IsA(nodeptr, _type_)
Definition nodes.h:164
#define copyObject(obj)
Definition nodes.h:232
@ CMD_UTILITY
Definition nodes.h:280
@ CMD_INSERT
Definition nodes.h:277
@ CMD_SELECT
Definition nodes.h:275
#define makeNode(_type_)
Definition nodes.h:161
static char * errmsg
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)
@ RTE_CTE
@ RTE_SUBQUERY
#define rt_fetch(rangetable_index, rangetable)
Definition parsetree.h:31
#define lfirst(lc)
Definition pg_list.h:172
#define lfirst_node(type, lc)
Definition pg_list.h:176
static int list_length(const List *l)
Definition pg_list.h:152
#define NIL
Definition pg_list.h:68
#define linitial(l)
Definition pg_list.h:178
unsigned int Oid
static int fb(int x)
@ IS_NOT_TRUE
Definition primnodes.h:2003
#define PRS2_OLD_VARNO
Definition primnodes.h:251
@ PARAM_MULTIEXPR
Definition primnodes.h:388
#define IS_SPECIAL_VARNO(varno)
Definition primnodes.h:248
#define PRS2_NEW_VARNO
Definition primnodes.h:252
VarReturningType
Definition primnodes.h:256
@ VAR_RETURNING_OLD
Definition primnodes.h:258
@ VAR_RETURNING_DEFAULT
Definition primnodes.h:257
@ COERCE_IMPLICIT_CAST
Definition primnodes.h:769
bool contain_windowfuncs(Node *node)
static bool SetVarReturningType_walker(Node *node, SetVarReturningType_context *context)
static Node * remove_nulling_relids_mutator(Node *node, remove_nulling_relids_context *context)
bool ChangeVarNodesWalkExpression(Node *node, ChangeVarNodes_context *context)
void IncrementVarSublevelsUp_rtable(List *rtable, int delta_sublevels_up, int min_sublevels_up)
void ChangeVarNodes(Node *node, int rt_index, int new_index, int sublevels_up)
Node * replace_rte_variables_mutator(Node *node, replace_rte_variables_context *context)
static bool contain_windowfuncs_walker(Node *node, void *context)
Relids adjust_relid_set(Relids relids, int oldrelid, int newrelid)
static bool contains_multiexpr_param(Node *node, void *context)
void OffsetVarNodes(Node *node, int offset, int sublevels_up)
bool checkExprHasSubLink(Node *node)
static bool locate_windowfunc_walker(Node *node, locate_windowfunc_context *context)
void CombineRangeTables(List **dst_rtable, List **dst_perminfos, List *src_rtable, List *src_perminfos)
static bool rangeTableEntry_used_walker(Node *node, rangeTableEntry_used_context *context)
void AddQual(Query *parsetree, Node *qual)
static Node * map_variable_attnos_mutator(Node *node, map_variable_attnos_context *context)
int locate_agg_of_level(Node *node, int levelsup)
static bool checkExprHasSubLink_walker(Node *node, void *context)
static bool IncrementVarSublevelsUp_walker(Node *node, IncrementVarSublevelsUp_context *context)
static bool ChangeVarNodes_walker(Node *node, ChangeVarNodes_context *context)
static bool locate_agg_of_level_walker(Node *node, locate_agg_of_level_context *context)
Node * add_nulling_relids(Node *node, const Bitmapset *target_relids, const Bitmapset *added_relids)
bool rangeTableEntry_used(Node *node, int rt_index, int sublevels_up)
static bool contain_aggs_of_level_walker(Node *node, contain_aggs_of_level_context *context)
bool contain_aggs_of_level(Node *node, int levelsup)
Query * getInsertSelectQuery(Query *parsetree, Query ***subquery_ptr)
int locate_windowfunc(Node *node)
Node * map_variable_attnos(Node *node, int target_varno, int sublevels_up, const AttrMap *attno_map, Oid to_rowtype, bool *found_whole_row)
Node * remove_nulling_relids(Node *node, const Bitmapset *removable_relids, const Bitmapset *except_relids)
void AddInvertedQual(Query *parsetree, Node *qual)
static void SetVarReturningType(Node *node, int result_relation, int sublevels_up, VarReturningType returning_type)
static bool OffsetVarNodes_walker(Node *node, OffsetVarNodes_context *context)
void ChangeVarNodesExtended(Node *node, int rt_index, int new_index, int sublevels_up, ChangeVarNodes_callback callback)
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)
static Node * add_nulling_relids_mutator(Node *node, add_nulling_relids_context *context)
void IncrementVarSublevelsUp(Node *node, int delta_sublevels_up, int min_sublevels_up)
static Relids offset_relid_set(Relids relids, int offset)
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)
Node * ReplaceVarFromTargetList(const Var *var, RangeTblEntry *target_rte, List *targetlist, int result_relation, ReplaceVarsNoMatchOption nomatch_option, int nomatch_varno)
bool(* ChangeVarNodes_callback)(Node *node, ChangeVarNodes_context *arg)
Node *(* replace_rte_variables_callback)(const Var *var, replace_rte_variables_context *context)
ReplaceVarsNoMatchOption
@ REPLACEVARS_SUBSTITUTE_NULL
@ REPLACEVARS_CHANGE_VARNO
@ REPLACEVARS_REPORT_ERROR
int maplen
Definition attmap.h:37
AttrNumber * attnums
Definition attmap.h:36
ChangeVarNodes_callback callback
Node * quals
Definition primnodes.h:2384
List * fromlist
Definition primnodes.h:2383
Definition pg_list.h:54
Definition nodes.h:135
List * rowMarks
Definition parsenodes.h:234
FromExpr * jointree
Definition parsenodes.h:182
Node * setOperations
Definition parsenodes.h:236
OnConflictExpr * onConflict
Definition parsenodes.h:203
List * rtable
Definition parsenodes.h:175
CmdType commandType
Definition parsenodes.h:121
Node * utilityStmt
Definition parsenodes.h:141
ReplaceVarsNoMatchOption nomatch_option
List * args
Definition primnodes.h:1449
ParseLoc location
Definition primnodes.h:1473
VarReturningType returning_type
ParseLoc location
Definition primnodes.h:311
AttrNumber varattno
Definition primnodes.h:275
int varno
Definition primnodes.h:270
VarReturningType varreturningtype
Definition primnodes.h:298
Index varlevelsup
Definition primnodes.h:295
const Bitmapset * target_relids
const Bitmapset * added_relids
const Bitmapset * removable_relids
const Bitmapset * except_relids
replace_rte_variables_callback callback
static void callback(struct sockaddr *addr, struct sockaddr *mask, void *unused)