Thanks to visit codestin.com
Credit goes to doxygen.postgresql.org

PostgreSQL Source Code git master
rewriteHandler.c
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * rewriteHandler.c
4 * Primary module of query rewriter.
5 *
6 * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
8 *
9 * IDENTIFICATION
10 * src/backend/rewrite/rewriteHandler.c
11 *
12 * NOTES
13 * Some of the terms used in this file are of historic nature: "retrieve"
14 * was the PostQUEL keyword for what today is SELECT. "RIR" stands for
15 * "Retrieve-Instead-Retrieve", that is an ON SELECT DO INSTEAD SELECT rule
16 * (which has to be unconditional and where only one rule can exist on each
17 * relation).
18 *
19 *-------------------------------------------------------------------------
20 */
21#include "postgres.h"
22
23#include "access/relation.h"
24#include "access/sysattr.h"
25#include "access/table.h"
26#include "catalog/dependency.h"
27#include "commands/trigger.h"
28#include "executor/executor.h"
29#include "foreign/fdwapi.h"
30#include "miscadmin.h"
31#include "nodes/makefuncs.h"
32#include "nodes/nodeFuncs.h"
33#include "optimizer/optimizer.h"
34#include "parser/analyze.h"
35#include "parser/parse_coerce.h"
37#include "parser/parsetree.h"
42#include "rewrite/rowsecurity.h"
43#include "tcop/tcopprot.h"
44#include "utils/builtins.h"
45#include "utils/lsyscache.h"
46#include "utils/rel.h"
47
48
49/* We use a list of these to detect recursion in RewriteQuery */
50typedef struct rewrite_event
51{
52 Oid relation; /* OID of relation having rules */
53 CmdType event; /* type of rule being fired */
55
57{
58 bool for_execute; /* AcquireRewriteLocks' forExecute param */
60
62{
66
67static bool acquireLocksOnSubLinks(Node *node,
69static Query *rewriteRuleAction(Query *parsetree,
70 Query *rule_action,
71 Node *rule_qual,
72 int rt_index,
73 CmdType event,
74 bool *returning_flag);
75static List *adjustJoinTreeList(Query *parsetree, bool removert, int rt_index);
76static List *rewriteTargetListIU(List *targetList,
77 CmdType commandType,
78 OverridingKind override,
79 Relation target_relation,
80 RangeTblEntry *values_rte,
81 int values_rte_index,
82 Bitmapset **unused_values_attrnos);
84 TargetEntry *prior_tle,
85 const char *attrName);
86static Node *get_assignment_input(Node *node);
88static bool rewriteValuesRTE(Query *parsetree, RangeTblEntry *rte, int rti,
89 Relation target_relation,
90 Bitmapset *unused_cols);
91static void rewriteValuesRTEToNulls(Query *parsetree, RangeTblEntry *rte);
92static void markQueryForLocking(Query *qry, Node *jtnode,
93 LockClauseStrength strength, LockWaitPolicy waitPolicy,
94 bool pushedDown);
95static List *matchLocks(CmdType event, Relation relation,
96 int varno, Query *parsetree, bool *hasUpdate);
97static Query *fireRIRrules(Query *parsetree, List *activeRIRs);
98static Bitmapset *adjust_view_column_set(Bitmapset *cols, List *targetlist);
99static Node *expand_generated_columns_internal(Node *node, Relation rel, int rt_index,
100 RangeTblEntry *rte, int result_relation);
101
102
103/*
104 * AcquireRewriteLocks -
105 * Acquire suitable locks on all the relations mentioned in the Query.
106 * These locks will ensure that the relation schemas don't change under us
107 * while we are rewriting, planning, and executing the query.
108 *
109 * Caution: this may modify the querytree, therefore caller should usually
110 * have done a copyObject() to make a writable copy of the querytree in the
111 * current memory context.
112 *
113 * forExecute indicates that the query is about to be executed. If so,
114 * we'll acquire the lock modes specified in the RTE rellockmode fields.
115 * If forExecute is false, AccessShareLock is acquired on all relations.
116 * This case is suitable for ruleutils.c, for example, where we only need
117 * schema stability and we don't intend to actually modify any relations.
118 *
119 * forUpdatePushedDown indicates that a pushed-down FOR [KEY] UPDATE/SHARE
120 * applies to the current subquery, requiring all rels to be opened with at
121 * least RowShareLock. This should always be false at the top of the
122 * recursion. When it is true, we adjust RTE rellockmode fields to reflect
123 * the higher lock level. This flag is ignored if forExecute is false.
124 *
125 * A secondary purpose of this routine is to fix up JOIN RTE references to
126 * dropped columns (see details below). Such RTEs are modified in-place.
127 *
128 * This processing can, and for efficiency's sake should, be skipped when the
129 * querytree has just been built by the parser: parse analysis already got
130 * all the same locks we'd get here, and the parser will have omitted dropped
131 * columns from JOINs to begin with. But we must do this whenever we are
132 * dealing with a querytree produced earlier than the current command.
133 *
134 * About JOINs and dropped columns: although the parser never includes an
135 * already-dropped column in a JOIN RTE's alias var list, it is possible for
136 * such a list in a stored rule to include references to dropped columns.
137 * (If the column is not explicitly referenced anywhere else in the query,
138 * the dependency mechanism won't consider it used by the rule and so won't
139 * prevent the column drop.) To support get_rte_attribute_is_dropped(), we
140 * replace join alias vars that reference dropped columns with null pointers.
141 *
142 * (In PostgreSQL 8.0, we did not do this processing but instead had
143 * get_rte_attribute_is_dropped() recurse to detect dropped columns in joins.
144 * That approach had horrible performance unfortunately; in particular
145 * construction of a nested join was O(N^2) in the nesting depth.)
146 */
147void
149 bool forExecute,
150 bool forUpdatePushedDown)
151{
152 ListCell *l;
153 int rt_index;
155
156 context.for_execute = forExecute;
157
158 /*
159 * First, process RTEs of the current query level.
160 */
161 rt_index = 0;
162 foreach(l, parsetree->rtable)
163 {
164 RangeTblEntry *rte = (RangeTblEntry *) lfirst(l);
165 Relation rel;
166 LOCKMODE lockmode;
167 List *newaliasvars;
168 Index curinputvarno;
169 RangeTblEntry *curinputrte;
170 ListCell *ll;
171
172 ++rt_index;
173 switch (rte->rtekind)
174 {
175 case RTE_RELATION:
176
177 /*
178 * Grab the appropriate lock type for the relation, and do not
179 * release it until end of transaction. This protects the
180 * rewriter, planner, and executor against schema changes
181 * mid-query.
182 *
183 * If forExecute is false, ignore rellockmode and just use
184 * AccessShareLock.
185 */
186 if (!forExecute)
187 lockmode = AccessShareLock;
188 else if (forUpdatePushedDown)
189 {
190 /* Upgrade RTE's lock mode to reflect pushed-down lock */
191 if (rte->rellockmode == AccessShareLock)
192 rte->rellockmode = RowShareLock;
193 lockmode = rte->rellockmode;
194 }
195 else
196 lockmode = rte->rellockmode;
197
198 rel = table_open(rte->relid, lockmode);
199
200 /*
201 * While we have the relation open, update the RTE's relkind,
202 * just in case it changed since this rule was made.
203 */
204 rte->relkind = rel->rd_rel->relkind;
205
206 table_close(rel, NoLock);
207 break;
208
209 case RTE_JOIN:
210
211 /*
212 * Scan the join's alias var list to see if any columns have
213 * been dropped, and if so replace those Vars with null
214 * pointers.
215 *
216 * Since a join has only two inputs, we can expect to see
217 * multiple references to the same input RTE; optimize away
218 * multiple fetches.
219 */
220 newaliasvars = NIL;
221 curinputvarno = 0;
222 curinputrte = NULL;
223 foreach(ll, rte->joinaliasvars)
224 {
225 Var *aliasitem = (Var *) lfirst(ll);
226 Var *aliasvar = aliasitem;
227
228 /* Look through any implicit coercion */
229 aliasvar = (Var *) strip_implicit_coercions((Node *) aliasvar);
230
231 /*
232 * If the list item isn't a simple Var, then it must
233 * represent a merged column, ie a USING column, and so it
234 * couldn't possibly be dropped, since it's referenced in
235 * the join clause. (Conceivably it could also be a null
236 * pointer already? But that's OK too.)
237 */
238 if (aliasvar && IsA(aliasvar, Var))
239 {
240 /*
241 * The elements of an alias list have to refer to
242 * earlier RTEs of the same rtable, because that's the
243 * order the planner builds things in. So we already
244 * processed the referenced RTE, and so it's safe to
245 * use get_rte_attribute_is_dropped on it. (This might
246 * not hold after rewriting or planning, but it's OK
247 * to assume here.)
248 */
249 Assert(aliasvar->varlevelsup == 0);
250 if (aliasvar->varno != curinputvarno)
251 {
252 curinputvarno = aliasvar->varno;
253 if (curinputvarno >= rt_index)
254 elog(ERROR, "unexpected varno %d in JOIN RTE %d",
255 curinputvarno, rt_index);
256 curinputrte = rt_fetch(curinputvarno,
257 parsetree->rtable);
258 }
259 if (get_rte_attribute_is_dropped(curinputrte,
260 aliasvar->varattno))
261 {
262 /* Replace the join alias item with a NULL */
263 aliasitem = NULL;
264 }
265 }
266 newaliasvars = lappend(newaliasvars, aliasitem);
267 }
268 rte->joinaliasvars = newaliasvars;
269 break;
270
271 case RTE_SUBQUERY:
272
273 /*
274 * The subquery RTE itself is all right, but we have to
275 * recurse to process the represented subquery.
276 */
278 forExecute,
279 (forUpdatePushedDown ||
280 get_parse_rowmark(parsetree, rt_index) != NULL));
281 break;
282
283 default:
284 /* ignore other types of RTEs */
285 break;
286 }
287 }
288
289 /* Recurse into subqueries in WITH */
290 foreach(l, parsetree->cteList)
291 {
293
294 AcquireRewriteLocks((Query *) cte->ctequery, forExecute, false);
295 }
296
297 /*
298 * Recurse into sublink subqueries, too. But we already did the ones in
299 * the rtable and cteList.
300 */
301 if (parsetree->hasSubLinks)
302 query_tree_walker(parsetree, acquireLocksOnSubLinks, &context,
304}
305
306/*
307 * Walker to find sublink subqueries for AcquireRewriteLocks
308 */
309static bool
311{
312 if (node == NULL)
313 return false;
314 if (IsA(node, SubLink))
315 {
316 SubLink *sub = (SubLink *) node;
317
318 /* Do what we came for */
320 context->for_execute,
321 false);
322 /* Fall through to process lefthand args of SubLink */
323 }
324
325 /*
326 * Do NOT recurse into Query nodes, because AcquireRewriteLocks already
327 * processed subselects of subselects for us.
328 */
329 return expression_tree_walker(node, acquireLocksOnSubLinks, context);
330}
331
332
333/*
334 * rewriteRuleAction -
335 * Rewrite the rule action with appropriate qualifiers (taken from
336 * the triggering query).
337 *
338 * Input arguments:
339 * parsetree - original query
340 * rule_action - one action (query) of a rule
341 * rule_qual - WHERE condition of rule, or NULL if unconditional
342 * rt_index - RT index of result relation in original query
343 * event - type of rule event
344 * Output arguments:
345 * *returning_flag - set true if we rewrite RETURNING clause in rule_action
346 * (must be initialized to false)
347 * Return value:
348 * rewritten form of rule_action
349 */
350static Query *
352 Query *rule_action,
353 Node *rule_qual,
354 int rt_index,
355 CmdType event,
356 bool *returning_flag)
357{
358 int current_varno,
359 new_varno;
360 int rt_length;
361 Query *sub_action;
362 Query **sub_action_ptr;
364 ListCell *lc;
365
366 context.for_execute = true;
367
368 /*
369 * Make modifiable copies of rule action and qual (what we're passed are
370 * the stored versions in the relcache; don't touch 'em!).
371 */
372 rule_action = copyObject(rule_action);
373 rule_qual = copyObject(rule_qual);
374
375 /*
376 * Acquire necessary locks and fix any deleted JOIN RTE entries.
377 */
378 AcquireRewriteLocks(rule_action, true, false);
379 (void) acquireLocksOnSubLinks(rule_qual, &context);
380
381 current_varno = rt_index;
382 rt_length = list_length(parsetree->rtable);
383 new_varno = PRS2_NEW_VARNO + rt_length;
384
385 /*
386 * Adjust rule action and qual to offset its varnos, so that we can merge
387 * its rtable with the main parsetree's rtable.
388 *
389 * If the rule action is an INSERT...SELECT, the OLD/NEW rtable entries
390 * will be in the SELECT part, and we have to modify that rather than the
391 * top-level INSERT (kluge!).
392 */
393 sub_action = getInsertSelectQuery(rule_action, &sub_action_ptr);
394
395 OffsetVarNodes((Node *) sub_action, rt_length, 0);
396 OffsetVarNodes(rule_qual, rt_length, 0);
397 /* but references to OLD should point at original rt_index */
398 ChangeVarNodes((Node *) sub_action,
399 PRS2_OLD_VARNO + rt_length, rt_index, 0);
400 ChangeVarNodes(rule_qual,
401 PRS2_OLD_VARNO + rt_length, rt_index, 0);
402
403 /*
404 * Mark any subquery RTEs in the rule action as LATERAL if they contain
405 * Vars referring to the current query level (references to NEW/OLD).
406 * Those really are lateral references, but we've historically not
407 * required users to mark such subqueries with LATERAL explicitly. But
408 * the planner will complain if such Vars exist in a non-LATERAL subquery,
409 * so we have to fix things up here.
410 */
411 foreach(lc, sub_action->rtable)
412 {
413 RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc);
414
415 if (rte->rtekind == RTE_SUBQUERY && !rte->lateral &&
417 rte->lateral = true;
418 }
419
420 /*
421 * Generate expanded rtable consisting of main parsetree's rtable plus
422 * rule action's rtable; this becomes the complete rtable for the rule
423 * action. Some of the entries may be unused after we finish rewriting,
424 * but we leave them all in place to avoid having to adjust the query's
425 * varnos. RT entries that are not referenced in the completed jointree
426 * will be ignored by the planner, so they do not affect query semantics.
427 *
428 * Also merge RTEPermissionInfo lists to ensure that all permissions are
429 * checked correctly.
430 *
431 * If the rule is INSTEAD, then the original query won't be executed at
432 * all, and so its rteperminfos must be preserved so that the executor
433 * will do the correct permissions checks on the relations referenced in
434 * it. This allows us to check that the caller has, say, insert-permission
435 * on a view, when the view is not semantically referenced at all in the
436 * resulting query.
437 *
438 * When a rule is not INSTEAD, the permissions checks done using the
439 * copied entries will be redundant with those done during execution of
440 * the original query, but we don't bother to treat that case differently.
441 *
442 * NOTE: because planner will destructively alter rtable and rteperminfos,
443 * we must ensure that rule action's lists are separate and shares no
444 * substructure with the main query's lists. Hence do a deep copy here
445 * for both.
446 */
447 {
448 List *rtable_tail = sub_action->rtable;
449 List *perminfos_tail = sub_action->rteperminfos;
450
451 /*
452 * RewriteQuery relies on the fact that RT entries from the original
453 * query appear at the start of the expanded rtable, so we put the
454 * action's original table at the end of the list.
455 */
456 sub_action->rtable = copyObject(parsetree->rtable);
457 sub_action->rteperminfos = copyObject(parsetree->rteperminfos);
458 CombineRangeTables(&sub_action->rtable, &sub_action->rteperminfos,
459 rtable_tail, perminfos_tail);
460 }
461
462 /*
463 * There could have been some SubLinks in parsetree's rtable, in which
464 * case we'd better mark the sub_action correctly.
465 */
466 if (parsetree->hasSubLinks && !sub_action->hasSubLinks)
467 {
468 foreach(lc, parsetree->rtable)
469 {
470 RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc);
471
472 switch (rte->rtekind)
473 {
474 case RTE_RELATION:
475 sub_action->hasSubLinks =
477 break;
478 case RTE_FUNCTION:
479 sub_action->hasSubLinks =
481 break;
482 case RTE_TABLEFUNC:
483 sub_action->hasSubLinks =
485 break;
486 case RTE_VALUES:
487 sub_action->hasSubLinks =
489 break;
490 default:
491 /* other RTE types don't contain bare expressions */
492 break;
493 }
494 sub_action->hasSubLinks |=
495 checkExprHasSubLink((Node *) rte->securityQuals);
496 if (sub_action->hasSubLinks)
497 break; /* no need to keep scanning rtable */
498 }
499 }
500
501 /*
502 * Also, we might have absorbed some RTEs with RLS conditions into the
503 * sub_action. If so, mark it as hasRowSecurity, whether or not those
504 * RTEs will be referenced after we finish rewriting. (Note: currently
505 * this is a no-op because RLS conditions aren't added till later, but it
506 * seems like good future-proofing to do this anyway.)
507 */
508 sub_action->hasRowSecurity |= parsetree->hasRowSecurity;
509
510 /*
511 * Each rule action's jointree should be the main parsetree's jointree
512 * plus that rule's jointree, but usually *without* the original rtindex
513 * that we're replacing (if present, which it won't be for INSERT). Note
514 * that if the rule action refers to OLD, its jointree will add a
515 * reference to rt_index. If the rule action doesn't refer to OLD, but
516 * either the rule_qual or the user query quals do, then we need to keep
517 * the original rtindex in the jointree to provide data for the quals. We
518 * don't want the original rtindex to be joined twice, however, so avoid
519 * keeping it if the rule action mentions it.
520 *
521 * As above, the action's jointree must not share substructure with the
522 * main parsetree's.
523 */
524 if (sub_action->commandType != CMD_UTILITY)
525 {
526 bool keeporig;
527 List *newjointree;
528
529 Assert(sub_action->jointree != NULL);
530 keeporig = (!rangeTableEntry_used((Node *) sub_action->jointree,
531 rt_index, 0)) &&
532 (rangeTableEntry_used(rule_qual, rt_index, 0) ||
533 rangeTableEntry_used(parsetree->jointree->quals, rt_index, 0));
534 newjointree = adjustJoinTreeList(parsetree, !keeporig, rt_index);
535 if (newjointree != NIL)
536 {
537 /*
538 * If sub_action is a setop, manipulating its jointree will do no
539 * good at all, because the jointree is dummy. (Perhaps someday
540 * we could push the joining and quals down to the member
541 * statements of the setop?)
542 */
543 if (sub_action->setOperations != NULL)
545 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
546 errmsg("conditional UNION/INTERSECT/EXCEPT statements are not implemented")));
547
548 sub_action->jointree->fromlist =
549 list_concat(newjointree, sub_action->jointree->fromlist);
550
551 /*
552 * There could have been some SubLinks in newjointree, in which
553 * case we'd better mark the sub_action correctly.
554 */
555 if (parsetree->hasSubLinks && !sub_action->hasSubLinks)
556 sub_action->hasSubLinks =
557 checkExprHasSubLink((Node *) newjointree);
558 }
559 }
560
561 /*
562 * If the original query has any CTEs, copy them into the rule action. But
563 * we don't need them for a utility action.
564 */
565 if (parsetree->cteList != NIL && sub_action->commandType != CMD_UTILITY)
566 {
567 /*
568 * Annoying implementation restriction: because CTEs are identified by
569 * name within a cteList, we can't merge a CTE from the original query
570 * if it has the same name as any CTE in the rule action.
571 *
572 * This could possibly be fixed by using some sort of internally
573 * generated ID, instead of names, to link CTE RTEs to their CTEs.
574 * However, decompiling the results would be quite confusing; note the
575 * merge of hasRecursive flags below, which could change the apparent
576 * semantics of such redundantly-named CTEs.
577 */
578 foreach(lc, parsetree->cteList)
579 {
581 ListCell *lc2;
582
583 foreach(lc2, sub_action->cteList)
584 {
585 CommonTableExpr *cte2 = (CommonTableExpr *) lfirst(lc2);
586
587 if (strcmp(cte->ctename, cte2->ctename) == 0)
589 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
590 errmsg("WITH query name \"%s\" appears in both a rule action and the query being rewritten",
591 cte->ctename)));
592 }
593 }
594
595 /* OK, it's safe to combine the CTE lists */
596 sub_action->cteList = list_concat(sub_action->cteList,
597 copyObject(parsetree->cteList));
598 /* ... and don't forget about the associated flags */
599 sub_action->hasRecursive |= parsetree->hasRecursive;
600 sub_action->hasModifyingCTE |= parsetree->hasModifyingCTE;
601
602 /*
603 * If rule_action is different from sub_action (i.e., the rule action
604 * is an INSERT...SELECT), then we might have just added some
605 * data-modifying CTEs that are not at the top query level. This is
606 * disallowed by the parser and we mustn't generate such trees here
607 * either, so throw an error.
608 *
609 * Conceivably such cases could be supported by attaching the original
610 * query's CTEs to rule_action not sub_action. But to do that, we'd
611 * have to increment ctelevelsup in RTEs and SubLinks copied from the
612 * original query. For now, it doesn't seem worth the trouble.
613 */
614 if (sub_action->hasModifyingCTE && rule_action != sub_action)
616 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
617 errmsg("INSERT ... SELECT rule actions are not supported for queries having data-modifying statements in WITH")));
618 }
619
620 /*
621 * Event Qualification forces copying of parsetree and splitting into two
622 * queries one w/rule_qual, one w/NOT rule_qual. Also add user query qual
623 * onto rule action
624 */
625 AddQual(sub_action, rule_qual);
626
627 AddQual(sub_action, parsetree->jointree->quals);
628
629 /*
630 * Rewrite new.attribute with right hand side of target-list entry for
631 * appropriate field name in insert/update.
632 *
633 * KLUGE ALERT: since ReplaceVarsFromTargetList returns a mutated copy, we
634 * can't just apply it to sub_action; we have to remember to update the
635 * sublink inside rule_action, too.
636 */
637 if ((event == CMD_INSERT || event == CMD_UPDATE) &&
638 sub_action->commandType != CMD_UTILITY)
639 {
640 sub_action = (Query *)
641 ReplaceVarsFromTargetList((Node *) sub_action,
642 new_varno,
643 0,
644 rt_fetch(new_varno, sub_action->rtable),
645 parsetree->targetList,
646 sub_action->resultRelation,
647 (event == CMD_UPDATE) ?
650 current_varno,
651 NULL);
652 if (sub_action_ptr)
653 *sub_action_ptr = sub_action;
654 else
655 rule_action = sub_action;
656 }
657
658 /*
659 * If rule_action has a RETURNING clause, then either throw it away if the
660 * triggering query has no RETURNING clause, or rewrite it to emit what
661 * the triggering query's RETURNING clause asks for. Throw an error if
662 * more than one rule has a RETURNING clause.
663 */
664 if (!parsetree->returningList)
665 rule_action->returningList = NIL;
666 else if (rule_action->returningList)
667 {
668 if (*returning_flag)
670 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
671 errmsg("cannot have RETURNING lists in multiple rules")));
672 *returning_flag = true;
673 rule_action->returningList = (List *)
675 parsetree->resultRelation,
676 0,
677 rt_fetch(parsetree->resultRelation,
678 parsetree->rtable),
679 rule_action->returningList,
680 rule_action->resultRelation,
682 0,
683 &rule_action->hasSubLinks);
684
685 /* use triggering query's aliases for OLD and NEW in RETURNING list */
686 rule_action->returningOldAlias = parsetree->returningOldAlias;
687 rule_action->returningNewAlias = parsetree->returningNewAlias;
688
689 /*
690 * There could have been some SubLinks in parsetree's returningList,
691 * in which case we'd better mark the rule_action correctly.
692 */
693 if (parsetree->hasSubLinks && !rule_action->hasSubLinks)
694 rule_action->hasSubLinks =
695 checkExprHasSubLink((Node *) rule_action->returningList);
696 }
697
698 return rule_action;
699}
700
701/*
702 * Copy the query's jointree list, and optionally attempt to remove any
703 * occurrence of the given rt_index as a top-level join item (we do not look
704 * for it within join items; this is OK because we are only expecting to find
705 * it as an UPDATE or DELETE target relation, which will be at the top level
706 * of the join). Returns modified jointree list --- this is a separate copy
707 * sharing no nodes with the original.
708 */
709static List *
710adjustJoinTreeList(Query *parsetree, bool removert, int rt_index)
711{
712 List *newjointree = copyObject(parsetree->jointree->fromlist);
713 ListCell *l;
714
715 if (removert)
716 {
717 foreach(l, newjointree)
718 {
719 RangeTblRef *rtr = lfirst(l);
720
721 if (IsA(rtr, RangeTblRef) &&
722 rtr->rtindex == rt_index)
723 {
724 newjointree = foreach_delete_current(newjointree, l);
725 break;
726 }
727 }
728 }
729 return newjointree;
730}
731
732
733/*
734 * rewriteTargetListIU - rewrite INSERT/UPDATE targetlist into standard form
735 *
736 * This has the following responsibilities:
737 *
738 * 1. For an INSERT, add tlist entries to compute default values for any
739 * attributes that have defaults and are not assigned to in the given tlist.
740 * (We do not insert anything for default-less attributes, however. The
741 * planner will later insert NULLs for them, but there's no reason to slow
742 * down rewriter processing with extra tlist nodes.) Also, for both INSERT
743 * and UPDATE, replace explicit DEFAULT specifications with column default
744 * expressions.
745 *
746 * 2. Merge multiple entries for the same target attribute, or declare error
747 * if we can't. Multiple entries are only allowed for INSERT/UPDATE of
748 * portions of an array or record field, for example
749 * UPDATE table SET foo[2] = 42, foo[4] = 43;
750 * We can merge such operations into a single assignment op. Essentially,
751 * the expression we want to produce in this case is like
752 * foo = array_set_element(array_set_element(foo, 2, 42), 4, 43)
753 *
754 * 3. Sort the tlist into standard order: non-junk fields in order by resno,
755 * then junk fields (these in no particular order).
756 *
757 * We must do items 1 and 2 before firing rewrite rules, else rewritten
758 * references to NEW.foo will produce wrong or incomplete results. Item 3
759 * is not needed for rewriting, but it is helpful for the planner, and we
760 * can do it essentially for free while handling the other items.
761 *
762 * If values_rte is non-NULL (i.e., we are doing a multi-row INSERT using
763 * values from a VALUES RTE), we populate *unused_values_attrnos with the
764 * attribute numbers of any unused columns from the VALUES RTE. This can
765 * happen for identity and generated columns whose targetlist entries are
766 * replaced with generated expressions (if INSERT ... OVERRIDING USER VALUE is
767 * used, or all the values to be inserted are DEFAULT). This information is
768 * required by rewriteValuesRTE() to handle any DEFAULT items in the unused
769 * columns. The caller must have initialized *unused_values_attrnos to NULL.
770 */
771static List *
773 CmdType commandType,
774 OverridingKind override,
775 Relation target_relation,
776 RangeTblEntry *values_rte,
777 int values_rte_index,
778 Bitmapset **unused_values_attrnos)
779{
780 TargetEntry **new_tles;
781 List *new_tlist = NIL;
782 List *junk_tlist = NIL;
783 Form_pg_attribute att_tup;
784 int attrno,
785 next_junk_attrno,
786 numattrs;
787 ListCell *temp;
788 Bitmapset *default_only_cols = NULL;
789
790 /*
791 * We process the normal (non-junk) attributes by scanning the input tlist
792 * once and transferring TLEs into an array, then scanning the array to
793 * build an output tlist. This avoids O(N^2) behavior for large numbers
794 * of attributes.
795 *
796 * Junk attributes are tossed into a separate list during the same tlist
797 * scan, then appended to the reconstructed tlist.
798 */
799 numattrs = RelationGetNumberOfAttributes(target_relation);
800 new_tles = (TargetEntry **) palloc0(numattrs * sizeof(TargetEntry *));
801 next_junk_attrno = numattrs + 1;
802
803 foreach(temp, targetList)
804 {
805 TargetEntry *old_tle = (TargetEntry *) lfirst(temp);
806
807 if (!old_tle->resjunk)
808 {
809 /* Normal attr: stash it into new_tles[] */
810 attrno = old_tle->resno;
811 if (attrno < 1 || attrno > numattrs)
812 elog(ERROR, "bogus resno %d in targetlist", attrno);
813 att_tup = TupleDescAttr(target_relation->rd_att, attrno - 1);
814
815 /* We can (and must) ignore deleted attributes */
816 if (att_tup->attisdropped)
817 continue;
818
819 /* Merge with any prior assignment to same attribute */
820 new_tles[attrno - 1] =
821 process_matched_tle(old_tle,
822 new_tles[attrno - 1],
823 NameStr(att_tup->attname));
824 }
825 else
826 {
827 /*
828 * Copy all resjunk tlist entries to junk_tlist, and assign them
829 * resnos above the last real resno.
830 *
831 * Typical junk entries include ORDER BY or GROUP BY expressions
832 * (are these actually possible in an INSERT or UPDATE?), system
833 * attribute references, etc.
834 */
835
836 /* Get the resno right, but don't copy unnecessarily */
837 if (old_tle->resno != next_junk_attrno)
838 {
839 old_tle = flatCopyTargetEntry(old_tle);
840 old_tle->resno = next_junk_attrno;
841 }
842 junk_tlist = lappend(junk_tlist, old_tle);
843 next_junk_attrno++;
844 }
845 }
846
847 for (attrno = 1; attrno <= numattrs; attrno++)
848 {
849 TargetEntry *new_tle = new_tles[attrno - 1];
850 bool apply_default;
851
852 att_tup = TupleDescAttr(target_relation->rd_att, attrno - 1);
853
854 /* We can (and must) ignore deleted attributes */
855 if (att_tup->attisdropped)
856 continue;
857
858 /*
859 * Handle the two cases where we need to insert a default expression:
860 * it's an INSERT and there's no tlist entry for the column, or the
861 * tlist entry is a DEFAULT placeholder node.
862 */
863 apply_default = ((new_tle == NULL && commandType == CMD_INSERT) ||
864 (new_tle && new_tle->expr && IsA(new_tle->expr, SetToDefault)));
865
866 if (commandType == CMD_INSERT)
867 {
868 int values_attrno = 0;
869
870 /* Source attribute number for values that come from a VALUES RTE */
871 if (values_rte && new_tle && IsA(new_tle->expr, Var))
872 {
873 Var *var = (Var *) new_tle->expr;
874
875 if (var->varno == values_rte_index)
876 values_attrno = var->varattno;
877 }
878
879 /*
880 * Can only insert DEFAULT into GENERATED ALWAYS identity columns,
881 * unless either OVERRIDING USER VALUE or OVERRIDING SYSTEM VALUE
882 * is specified.
883 */
884 if (att_tup->attidentity == ATTRIBUTE_IDENTITY_ALWAYS && !apply_default)
885 {
886 if (override == OVERRIDING_USER_VALUE)
887 apply_default = true;
888 else if (override != OVERRIDING_SYSTEM_VALUE)
889 {
890 /*
891 * If this column's values come from a VALUES RTE, test
892 * whether it contains only SetToDefault items. Since the
893 * VALUES list might be quite large, we arrange to only
894 * scan it once.
895 */
896 if (values_attrno != 0)
897 {
898 if (default_only_cols == NULL)
899 default_only_cols = findDefaultOnlyColumns(values_rte);
900
901 if (bms_is_member(values_attrno, default_only_cols))
902 apply_default = true;
903 }
904
905 if (!apply_default)
907 (errcode(ERRCODE_GENERATED_ALWAYS),
908 errmsg("cannot insert a non-DEFAULT value into column \"%s\"",
909 NameStr(att_tup->attname)),
910 errdetail("Column \"%s\" is an identity column defined as GENERATED ALWAYS.",
911 NameStr(att_tup->attname)),
912 errhint("Use OVERRIDING SYSTEM VALUE to override.")));
913 }
914 }
915
916 /*
917 * Although inserting into a GENERATED BY DEFAULT identity column
918 * is allowed, apply the default if OVERRIDING USER VALUE is
919 * specified.
920 */
921 if (att_tup->attidentity == ATTRIBUTE_IDENTITY_BY_DEFAULT &&
922 override == OVERRIDING_USER_VALUE)
923 apply_default = true;
924
925 /*
926 * Can only insert DEFAULT into generated columns. (The
927 * OVERRIDING clause does not apply to generated columns, so we
928 * don't consider it here.)
929 */
930 if (att_tup->attgenerated && !apply_default)
931 {
932 /*
933 * If this column's values come from a VALUES RTE, test
934 * whether it contains only SetToDefault items, as above.
935 */
936 if (values_attrno != 0)
937 {
938 if (default_only_cols == NULL)
939 default_only_cols = findDefaultOnlyColumns(values_rte);
940
941 if (bms_is_member(values_attrno, default_only_cols))
942 apply_default = true;
943 }
944
945 if (!apply_default)
947 (errcode(ERRCODE_GENERATED_ALWAYS),
948 errmsg("cannot insert a non-DEFAULT value into column \"%s\"",
949 NameStr(att_tup->attname)),
950 errdetail("Column \"%s\" is a generated column.",
951 NameStr(att_tup->attname))));
952 }
953
954 /*
955 * For an INSERT from a VALUES RTE, return the attribute numbers
956 * of any VALUES columns that will no longer be used (due to the
957 * targetlist entry being replaced by a default expression).
958 */
959 if (values_attrno != 0 && apply_default && unused_values_attrnos)
960 *unused_values_attrnos = bms_add_member(*unused_values_attrnos,
961 values_attrno);
962 }
963
964 /*
965 * Updates to identity and generated columns follow the same rules as
966 * above, except that UPDATE doesn't admit OVERRIDING clauses. Also,
967 * the source can't be a VALUES RTE, so we needn't consider that.
968 */
969 if (commandType == CMD_UPDATE)
970 {
971 if (att_tup->attidentity == ATTRIBUTE_IDENTITY_ALWAYS &&
972 new_tle && !apply_default)
974 (errcode(ERRCODE_GENERATED_ALWAYS),
975 errmsg("column \"%s\" can only be updated to DEFAULT",
976 NameStr(att_tup->attname)),
977 errdetail("Column \"%s\" is an identity column defined as GENERATED ALWAYS.",
978 NameStr(att_tup->attname))));
979
980 if (att_tup->attgenerated && new_tle && !apply_default)
982 (errcode(ERRCODE_GENERATED_ALWAYS),
983 errmsg("column \"%s\" can only be updated to DEFAULT",
984 NameStr(att_tup->attname)),
985 errdetail("Column \"%s\" is a generated column.",
986 NameStr(att_tup->attname))));
987 }
988
989 if (att_tup->attgenerated)
990 {
991 /*
992 * virtual generated column stores a null value; stored generated
993 * column will be fixed in executor
994 */
995 new_tle = NULL;
996 }
997 else if (apply_default)
998 {
999 Node *new_expr;
1000
1001 new_expr = build_column_default(target_relation, attrno);
1002
1003 /*
1004 * If there is no default (ie, default is effectively NULL), we
1005 * can omit the tlist entry in the INSERT case, since the planner
1006 * can insert a NULL for itself, and there's no point in spending
1007 * any more rewriter cycles on the entry. But in the UPDATE case
1008 * we've got to explicitly set the column to NULL.
1009 */
1010 if (!new_expr)
1011 {
1012 if (commandType == CMD_INSERT)
1013 new_tle = NULL;
1014 else
1015 new_expr = coerce_null_to_domain(att_tup->atttypid,
1016 att_tup->atttypmod,
1017 att_tup->attcollation,
1018 att_tup->attlen,
1019 att_tup->attbyval);
1020 }
1021
1022 if (new_expr)
1023 new_tle = makeTargetEntry((Expr *) new_expr,
1024 attrno,
1025 pstrdup(NameStr(att_tup->attname)),
1026 false);
1027 }
1028
1029 if (new_tle)
1030 new_tlist = lappend(new_tlist, new_tle);
1031 }
1032
1033 pfree(new_tles);
1034
1035 return list_concat(new_tlist, junk_tlist);
1036}
1037
1038
1039/*
1040 * Convert a matched TLE from the original tlist into a correct new TLE.
1041 *
1042 * This routine detects and handles multiple assignments to the same target
1043 * attribute. (The attribute name is needed only for error messages.)
1044 */
1045static TargetEntry *
1047 TargetEntry *prior_tle,
1048 const char *attrName)
1049{
1050 TargetEntry *result;
1051 CoerceToDomain *coerce_expr = NULL;
1052 Node *src_expr;
1053 Node *prior_expr;
1054 Node *src_input;
1055 Node *prior_input;
1056 Node *priorbottom;
1057 Node *newexpr;
1058
1059 if (prior_tle == NULL)
1060 {
1061 /*
1062 * Normal case where this is the first assignment to the attribute.
1063 */
1064 return src_tle;
1065 }
1066
1067 /*----------
1068 * Multiple assignments to same attribute. Allow only if all are
1069 * FieldStore or SubscriptingRef assignment operations. This is a bit
1070 * tricky because what we may actually be looking at is a nest of
1071 * such nodes; consider
1072 * UPDATE tab SET col.fld1.subfld1 = x, col.fld2.subfld2 = y
1073 * The two expressions produced by the parser will look like
1074 * FieldStore(col, fld1, FieldStore(placeholder, subfld1, x))
1075 * FieldStore(col, fld2, FieldStore(placeholder, subfld2, y))
1076 * However, we can ignore the substructure and just consider the top
1077 * FieldStore or SubscriptingRef from each assignment, because it works to
1078 * combine these as
1079 * FieldStore(FieldStore(col, fld1,
1080 * FieldStore(placeholder, subfld1, x)),
1081 * fld2, FieldStore(placeholder, subfld2, y))
1082 * Note the leftmost expression goes on the inside so that the
1083 * assignments appear to occur left-to-right.
1084 *
1085 * For FieldStore, instead of nesting we can generate a single
1086 * FieldStore with multiple target fields. We must nest when
1087 * SubscriptingRefs are involved though.
1088 *
1089 * As a further complication, the destination column might be a domain,
1090 * resulting in each assignment containing a CoerceToDomain node over a
1091 * FieldStore or SubscriptingRef. These should have matching target
1092 * domains, so we strip them and reconstitute a single CoerceToDomain over
1093 * the combined FieldStore/SubscriptingRef nodes. (Notice that this has
1094 * the result that the domain's checks are applied only after we do all
1095 * the field or element updates, not after each one. This is desirable.)
1096 *----------
1097 */
1098 src_expr = (Node *) src_tle->expr;
1099 prior_expr = (Node *) prior_tle->expr;
1100
1101 if (src_expr && IsA(src_expr, CoerceToDomain) &&
1102 prior_expr && IsA(prior_expr, CoerceToDomain) &&
1103 ((CoerceToDomain *) src_expr)->resulttype ==
1104 ((CoerceToDomain *) prior_expr)->resulttype)
1105 {
1106 /* we assume without checking that resulttypmod/resultcollid match */
1107 coerce_expr = (CoerceToDomain *) src_expr;
1108 src_expr = (Node *) ((CoerceToDomain *) src_expr)->arg;
1109 prior_expr = (Node *) ((CoerceToDomain *) prior_expr)->arg;
1110 }
1111
1112 src_input = get_assignment_input(src_expr);
1113 prior_input = get_assignment_input(prior_expr);
1114 if (src_input == NULL ||
1115 prior_input == NULL ||
1116 exprType(src_expr) != exprType(prior_expr))
1117 ereport(ERROR,
1118 (errcode(ERRCODE_SYNTAX_ERROR),
1119 errmsg("multiple assignments to same column \"%s\"",
1120 attrName)));
1121
1122 /*
1123 * Prior TLE could be a nest of assignments if we do this more than once.
1124 */
1125 priorbottom = prior_input;
1126 for (;;)
1127 {
1128 Node *newbottom = get_assignment_input(priorbottom);
1129
1130 if (newbottom == NULL)
1131 break; /* found the original Var reference */
1132 priorbottom = newbottom;
1133 }
1134 if (!equal(priorbottom, src_input))
1135 ereport(ERROR,
1136 (errcode(ERRCODE_SYNTAX_ERROR),
1137 errmsg("multiple assignments to same column \"%s\"",
1138 attrName)));
1139
1140 /*
1141 * Looks OK to nest 'em.
1142 */
1143 if (IsA(src_expr, FieldStore))
1144 {
1145 FieldStore *fstore = makeNode(FieldStore);
1146
1147 if (IsA(prior_expr, FieldStore))
1148 {
1149 /* combine the two */
1150 memcpy(fstore, prior_expr, sizeof(FieldStore));
1151 fstore->newvals =
1152 list_concat_copy(((FieldStore *) prior_expr)->newvals,
1153 ((FieldStore *) src_expr)->newvals);
1154 fstore->fieldnums =
1155 list_concat_copy(((FieldStore *) prior_expr)->fieldnums,
1156 ((FieldStore *) src_expr)->fieldnums);
1157 }
1158 else
1159 {
1160 /* general case, just nest 'em */
1161 memcpy(fstore, src_expr, sizeof(FieldStore));
1162 fstore->arg = (Expr *) prior_expr;
1163 }
1164 newexpr = (Node *) fstore;
1165 }
1166 else if (IsA(src_expr, SubscriptingRef))
1167 {
1169
1170 memcpy(sbsref, src_expr, sizeof(SubscriptingRef));
1171 sbsref->refexpr = (Expr *) prior_expr;
1172 newexpr = (Node *) sbsref;
1173 }
1174 else
1175 {
1176 elog(ERROR, "cannot happen");
1177 newexpr = NULL;
1178 }
1179
1180 if (coerce_expr)
1181 {
1182 /* put back the CoerceToDomain */
1184
1185 memcpy(newcoerce, coerce_expr, sizeof(CoerceToDomain));
1186 newcoerce->arg = (Expr *) newexpr;
1187 newexpr = (Node *) newcoerce;
1188 }
1189
1190 result = flatCopyTargetEntry(src_tle);
1191 result->expr = (Expr *) newexpr;
1192 return result;
1193}
1194
1195/*
1196 * If node is an assignment node, return its input; else return NULL
1197 */
1198static Node *
1200{
1201 if (node == NULL)
1202 return NULL;
1203 if (IsA(node, FieldStore))
1204 {
1205 FieldStore *fstore = (FieldStore *) node;
1206
1207 return (Node *) fstore->arg;
1208 }
1209 else if (IsA(node, SubscriptingRef))
1210 {
1211 SubscriptingRef *sbsref = (SubscriptingRef *) node;
1212
1213 if (sbsref->refassgnexpr == NULL)
1214 return NULL;
1215
1216 return (Node *) sbsref->refexpr;
1217 }
1218
1219 return NULL;
1220}
1221
1222/*
1223 * Make an expression tree for the default value for a column.
1224 *
1225 * If there is no default, return a NULL instead.
1226 */
1227Node *
1229{
1230 TupleDesc rd_att = rel->rd_att;
1231 Form_pg_attribute att_tup = TupleDescAttr(rd_att, attrno - 1);
1232 Oid atttype = att_tup->atttypid;
1233 int32 atttypmod = att_tup->atttypmod;
1234 Node *expr = NULL;
1235 Oid exprtype;
1236
1237 if (att_tup->attidentity)
1238 {
1240
1241 nve->seqid = getIdentitySequence(rel, attrno, false);
1242 nve->typeId = att_tup->atttypid;
1243
1244 return (Node *) nve;
1245 }
1246
1247 /*
1248 * If relation has a default for this column, fetch that expression.
1249 */
1250 if (att_tup->atthasdef)
1251 {
1252 expr = TupleDescGetDefault(rd_att, attrno);
1253 if (expr == NULL)
1254 elog(ERROR, "default expression not found for attribute %d of relation \"%s\"",
1255 attrno, RelationGetRelationName(rel));
1256 }
1257
1258 /*
1259 * No per-column default, so look for a default for the type itself. But
1260 * not for generated columns.
1261 */
1262 if (expr == NULL && !att_tup->attgenerated)
1263 expr = get_typdefault(atttype);
1264
1265 if (expr == NULL)
1266 return NULL; /* No default anywhere */
1267
1268 /*
1269 * Make sure the value is coerced to the target column type; this will
1270 * generally be true already, but there seem to be some corner cases
1271 * involving domain defaults where it might not be true. This should match
1272 * the parser's processing of non-defaulted expressions --- see
1273 * transformAssignedExpr().
1274 */
1275 exprtype = exprType(expr);
1276
1277 expr = coerce_to_target_type(NULL, /* no UNKNOWN params here */
1278 expr, exprtype,
1279 atttype, atttypmod,
1282 -1);
1283 if (expr == NULL)
1284 ereport(ERROR,
1285 (errcode(ERRCODE_DATATYPE_MISMATCH),
1286 errmsg("column \"%s\" is of type %s"
1287 " but default expression is of type %s",
1288 NameStr(att_tup->attname),
1289 format_type_be(atttype),
1290 format_type_be(exprtype)),
1291 errhint("You will need to rewrite or cast the expression.")));
1292
1293 return expr;
1294}
1295
1296
1297/* Does VALUES RTE contain any SetToDefault items? */
1298static bool
1300{
1301 ListCell *lc;
1302
1303 foreach(lc, rte->values_lists)
1304 {
1305 List *sublist = (List *) lfirst(lc);
1306 ListCell *lc2;
1307
1308 foreach(lc2, sublist)
1309 {
1310 Node *col = (Node *) lfirst(lc2);
1311
1312 if (IsA(col, SetToDefault))
1313 return true;
1314 }
1315 }
1316 return false;
1317}
1318
1319
1320/*
1321 * Search a VALUES RTE for columns that contain only SetToDefault items,
1322 * returning a Bitmapset containing the attribute numbers of any such columns.
1323 */
1324static Bitmapset *
1326{
1327 Bitmapset *default_only_cols = NULL;
1328 ListCell *lc;
1329
1330 foreach(lc, rte->values_lists)
1331 {
1332 List *sublist = (List *) lfirst(lc);
1333 ListCell *lc2;
1334 int i;
1335
1336 if (default_only_cols == NULL)
1337 {
1338 /* Populate the initial result bitmap from the first row */
1339 i = 0;
1340 foreach(lc2, sublist)
1341 {
1342 Node *col = (Node *) lfirst(lc2);
1343
1344 i++;
1345 if (IsA(col, SetToDefault))
1346 default_only_cols = bms_add_member(default_only_cols, i);
1347 }
1348 }
1349 else
1350 {
1351 /* Update the result bitmap from this next row */
1352 i = 0;
1353 foreach(lc2, sublist)
1354 {
1355 Node *col = (Node *) lfirst(lc2);
1356
1357 i++;
1358 if (!IsA(col, SetToDefault))
1359 default_only_cols = bms_del_member(default_only_cols, i);
1360 }
1361 }
1362
1363 /*
1364 * If no column in the rows read so far contains only DEFAULT items,
1365 * we are done.
1366 */
1367 if (bms_is_empty(default_only_cols))
1368 break;
1369 }
1370
1371 return default_only_cols;
1372}
1373
1374
1375/*
1376 * When processing INSERT ... VALUES with a VALUES RTE (ie, multiple VALUES
1377 * lists), we have to replace any DEFAULT items in the VALUES lists with
1378 * the appropriate default expressions. The other aspects of targetlist
1379 * rewriting need be applied only to the query's targetlist proper.
1380 *
1381 * For an auto-updatable view, each DEFAULT item in the VALUES list is
1382 * replaced with the default from the view, if it has one. Otherwise it is
1383 * left untouched so that the underlying base relation's default can be
1384 * applied instead (when we later recurse to here after rewriting the query
1385 * to refer to the base relation instead of the view).
1386 *
1387 * For other types of relation, including rule- and trigger-updatable views,
1388 * all DEFAULT items are replaced, and if the target relation doesn't have a
1389 * default, the value is explicitly set to NULL.
1390 *
1391 * Also, if a DEFAULT item is found in a column mentioned in unused_cols,
1392 * it is explicitly set to NULL. This happens for columns in the VALUES RTE
1393 * whose corresponding targetlist entries have already been replaced with the
1394 * relation's default expressions, so that any values in those columns of the
1395 * VALUES RTE are no longer used. This can happen for identity and generated
1396 * columns (if INSERT ... OVERRIDING USER VALUE is used, or all the values to
1397 * be inserted are DEFAULT). In principle we could replace all entries in
1398 * such a column with NULL, whether DEFAULT or not; but it doesn't seem worth
1399 * the trouble.
1400 *
1401 * Note that we may have subscripted or field assignment targetlist entries,
1402 * as well as more complex expressions from already-replaced DEFAULT items if
1403 * we have recursed to here for an auto-updatable view. However, it ought to
1404 * be impossible for such entries to have DEFAULTs assigned to them, except
1405 * for unused columns, as described above --- we should only have to replace
1406 * DEFAULT items for targetlist entries that contain simple Vars referencing
1407 * the VALUES RTE, or which are no longer referred to by the targetlist.
1408 *
1409 * Returns true if all DEFAULT items were replaced, and false if some were
1410 * left untouched.
1411 */
1412static bool
1413rewriteValuesRTE(Query *parsetree, RangeTblEntry *rte, int rti,
1414 Relation target_relation,
1415 Bitmapset *unused_cols)
1416{
1417 List *newValues;
1418 ListCell *lc;
1419 bool isAutoUpdatableView;
1420 bool allReplaced;
1421 int numattrs;
1422 int *attrnos;
1423
1424 /* Steps below are not sensible for non-INSERT queries */
1425 Assert(parsetree->commandType == CMD_INSERT);
1426 Assert(rte->rtekind == RTE_VALUES);
1427
1428 /*
1429 * Rebuilding all the lists is a pretty expensive proposition in a big
1430 * VALUES list, and it's a waste of time if there aren't any DEFAULT
1431 * placeholders. So first scan to see if there are any.
1432 */
1433 if (!searchForDefault(rte))
1434 return true; /* nothing to do */
1435
1436 /*
1437 * Scan the targetlist for entries referring to the VALUES RTE, and note
1438 * the target attributes. As noted above, we should only need to do this
1439 * for targetlist entries containing simple Vars --- nothing else in the
1440 * VALUES RTE should contain DEFAULT items (except possibly for unused
1441 * columns), and we complain if such a thing does occur.
1442 */
1443 numattrs = list_length(linitial(rte->values_lists));
1444 attrnos = (int *) palloc0(numattrs * sizeof(int));
1445
1446 foreach(lc, parsetree->targetList)
1447 {
1448 TargetEntry *tle = (TargetEntry *) lfirst(lc);
1449
1450 if (IsA(tle->expr, Var))
1451 {
1452 Var *var = (Var *) tle->expr;
1453
1454 if (var->varno == rti)
1455 {
1456 int attrno = var->varattno;
1457
1458 Assert(attrno >= 1 && attrno <= numattrs);
1459 attrnos[attrno - 1] = tle->resno;
1460 }
1461 }
1462 }
1463
1464 /*
1465 * Check if the target relation is an auto-updatable view, in which case
1466 * unresolved defaults will be left untouched rather than being set to
1467 * NULL.
1468 */
1469 isAutoUpdatableView = false;
1470 if (target_relation->rd_rel->relkind == RELKIND_VIEW &&
1471 !view_has_instead_trigger(target_relation, CMD_INSERT, NIL))
1472 {
1473 List *locks;
1474 bool hasUpdate;
1475 bool found;
1476 ListCell *l;
1477
1478 /* Look for an unconditional DO INSTEAD rule */
1479 locks = matchLocks(CMD_INSERT, target_relation,
1480 parsetree->resultRelation, parsetree, &hasUpdate);
1481
1482 found = false;
1483 foreach(l, locks)
1484 {
1485 RewriteRule *rule_lock = (RewriteRule *) lfirst(l);
1486
1487 if (rule_lock->isInstead &&
1488 rule_lock->qual == NULL)
1489 {
1490 found = true;
1491 break;
1492 }
1493 }
1494
1495 /*
1496 * If we didn't find an unconditional DO INSTEAD rule, assume that the
1497 * view is auto-updatable. If it isn't, rewriteTargetView() will
1498 * throw an error.
1499 */
1500 if (!found)
1501 isAutoUpdatableView = true;
1502 }
1503
1504 newValues = NIL;
1505 allReplaced = true;
1506 foreach(lc, rte->values_lists)
1507 {
1508 List *sublist = (List *) lfirst(lc);
1509 List *newList = NIL;
1510 ListCell *lc2;
1511 int i;
1512
1513 Assert(list_length(sublist) == numattrs);
1514
1515 i = 0;
1516 foreach(lc2, sublist)
1517 {
1518 Node *col = (Node *) lfirst(lc2);
1519 int attrno = attrnos[i++];
1520
1521 if (IsA(col, SetToDefault))
1522 {
1523 Form_pg_attribute att_tup;
1524 Node *new_expr;
1525
1526 /*
1527 * If this column isn't used, just replace the DEFAULT with
1528 * NULL (attrno will be 0 in this case because the targetlist
1529 * entry will have been replaced by the default expression).
1530 */
1531 if (bms_is_member(i, unused_cols))
1532 {
1533 SetToDefault *def = (SetToDefault *) col;
1534
1535 newList = lappend(newList,
1536 makeNullConst(def->typeId,
1537 def->typeMod,
1538 def->collation));
1539 continue;
1540 }
1541
1542 if (attrno == 0)
1543 elog(ERROR, "cannot set value in column %d to DEFAULT", i);
1544 Assert(attrno > 0 && attrno <= target_relation->rd_att->natts);
1545 att_tup = TupleDescAttr(target_relation->rd_att, attrno - 1);
1546
1547 if (!att_tup->attisdropped)
1548 new_expr = build_column_default(target_relation, attrno);
1549 else
1550 new_expr = NULL; /* force a NULL if dropped */
1551
1552 /*
1553 * If there is no default (ie, default is effectively NULL),
1554 * we've got to explicitly set the column to NULL, unless the
1555 * target relation is an auto-updatable view.
1556 */
1557 if (!new_expr)
1558 {
1559 if (isAutoUpdatableView)
1560 {
1561 /* Leave the value untouched */
1562 newList = lappend(newList, col);
1563 allReplaced = false;
1564 continue;
1565 }
1566
1567 new_expr = coerce_null_to_domain(att_tup->atttypid,
1568 att_tup->atttypmod,
1569 att_tup->attcollation,
1570 att_tup->attlen,
1571 att_tup->attbyval);
1572 }
1573 newList = lappend(newList, new_expr);
1574 }
1575 else
1576 newList = lappend(newList, col);
1577 }
1578 newValues = lappend(newValues, newList);
1579 }
1580 rte->values_lists = newValues;
1581
1582 pfree(attrnos);
1583
1584 return allReplaced;
1585}
1586
1587/*
1588 * Mop up any remaining DEFAULT items in the given VALUES RTE by
1589 * replacing them with NULL constants.
1590 *
1591 * This is used for the product queries generated by DO ALSO rules attached to
1592 * an auto-updatable view. The action can't depend on the "target relation"
1593 * since the product query might not have one (it needn't be an INSERT).
1594 * Essentially, such queries are treated as being attached to a rule-updatable
1595 * view.
1596 */
1597static void
1599{
1600 List *newValues;
1601 ListCell *lc;
1602
1603 newValues = NIL;
1604 foreach(lc, rte->values_lists)
1605 {
1606 List *sublist = (List *) lfirst(lc);
1607 List *newList = NIL;
1608 ListCell *lc2;
1609
1610 foreach(lc2, sublist)
1611 {
1612 Node *col = (Node *) lfirst(lc2);
1613
1614 if (IsA(col, SetToDefault))
1615 {
1616 SetToDefault *def = (SetToDefault *) col;
1617
1618 newList = lappend(newList, makeNullConst(def->typeId,
1619 def->typeMod,
1620 def->collation));
1621 }
1622 else
1623 newList = lappend(newList, col);
1624 }
1625 newValues = lappend(newValues, newList);
1626 }
1627 rte->values_lists = newValues;
1628}
1629
1630
1631/*
1632 * matchLocks -
1633 * match a relation's list of locks and returns the matching rules
1634 */
1635static List *
1637 Relation relation,
1638 int varno,
1639 Query *parsetree,
1640 bool *hasUpdate)
1641{
1642 RuleLock *rulelocks = relation->rd_rules;
1643 List *matching_locks = NIL;
1644 int nlocks;
1645 int i;
1646
1647 if (rulelocks == NULL)
1648 return NIL;
1649
1650 if (parsetree->commandType != CMD_SELECT)
1651 {
1652 if (parsetree->resultRelation != varno)
1653 return NIL;
1654 }
1655
1656 nlocks = rulelocks->numLocks;
1657
1658 for (i = 0; i < nlocks; i++)
1659 {
1660 RewriteRule *oneLock = rulelocks->rules[i];
1661
1662 if (oneLock->event == CMD_UPDATE)
1663 *hasUpdate = true;
1664
1665 /*
1666 * Suppress ON INSERT/UPDATE/DELETE rules that are disabled or
1667 * configured to not fire during the current session's replication
1668 * role. ON SELECT rules will always be applied in order to keep views
1669 * working even in LOCAL or REPLICA role.
1670 */
1671 if (oneLock->event != CMD_SELECT)
1672 {
1674 {
1675 if (oneLock->enabled == RULE_FIRES_ON_ORIGIN ||
1676 oneLock->enabled == RULE_DISABLED)
1677 continue;
1678 }
1679 else /* ORIGIN or LOCAL ROLE */
1680 {
1681 if (oneLock->enabled == RULE_FIRES_ON_REPLICA ||
1682 oneLock->enabled == RULE_DISABLED)
1683 continue;
1684 }
1685
1686 /* Non-SELECT rules are not supported for MERGE */
1687 if (parsetree->commandType == CMD_MERGE)
1688 ereport(ERROR,
1689 errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1690 errmsg("cannot execute MERGE on relation \"%s\"",
1691 RelationGetRelationName(relation)),
1692 errdetail("MERGE is not supported for relations with rules."));
1693 }
1694
1695 if (oneLock->event == event)
1696 {
1697 if (parsetree->commandType != CMD_SELECT ||
1698 rangeTableEntry_used((Node *) parsetree, varno, 0))
1699 matching_locks = lappend(matching_locks, oneLock);
1700 }
1701 }
1702
1703 return matching_locks;
1704}
1705
1706
1707/*
1708 * ApplyRetrieveRule - expand an ON SELECT rule
1709 */
1710static Query *
1713 int rt_index,
1714 Relation relation,
1715 List *activeRIRs)
1716{
1717 Query *rule_action;
1718 RangeTblEntry *rte;
1719 RowMarkClause *rc;
1720 int numCols;
1721
1722 if (list_length(rule->actions) != 1)
1723 elog(ERROR, "expected just one rule action");
1724 if (rule->qual != NULL)
1725 elog(ERROR, "cannot handle qualified ON SELECT rule");
1726
1727 /* Check if the expansion of non-system views are restricted */
1730 ereport(ERROR,
1731 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1732 errmsg("access to non-system view \"%s\" is restricted",
1733 RelationGetRelationName(relation))));
1734
1735 if (rt_index == parsetree->resultRelation)
1736 {
1737 /*
1738 * We have a view as the result relation of the query, and it wasn't
1739 * rewritten by any rule. This case is supported if there is an
1740 * INSTEAD OF trigger that will trap attempts to insert/update/delete
1741 * view rows. The executor will check that; for the moment just plow
1742 * ahead. We have two cases:
1743 *
1744 * For INSERT, we needn't do anything. The unmodified RTE will serve
1745 * fine as the result relation.
1746 *
1747 * For UPDATE/DELETE/MERGE, we need to expand the view so as to have
1748 * source data for the operation. But we also need an unmodified RTE
1749 * to serve as the target. So, copy the RTE and add the copy to the
1750 * rangetable. Note that the copy does not get added to the jointree.
1751 * Also note that there's a hack in fireRIRrules to avoid calling this
1752 * function again when it arrives at the copied RTE.
1753 */
1754 if (parsetree->commandType == CMD_INSERT)
1755 return parsetree;
1756 else if (parsetree->commandType == CMD_UPDATE ||
1757 parsetree->commandType == CMD_DELETE ||
1758 parsetree->commandType == CMD_MERGE)
1759 {
1760 RangeTblEntry *newrte;
1761 Var *var;
1762 TargetEntry *tle;
1763
1764 rte = rt_fetch(rt_index, parsetree->rtable);
1765 newrte = copyObject(rte);
1766 parsetree->rtable = lappend(parsetree->rtable, newrte);
1767 parsetree->resultRelation = list_length(parsetree->rtable);
1768 /* parsetree->mergeTargetRelation unchanged (use expanded view) */
1769
1770 /*
1771 * For the most part, Vars referencing the view should remain as
1772 * they are, meaning that they implicitly represent OLD values.
1773 * But in the RETURNING list if any, we want such Vars to
1774 * represent NEW values, so change them to reference the new RTE.
1775 *
1776 * Since ChangeVarNodes scribbles on the tree in-place, copy the
1777 * RETURNING list first for safety.
1778 */
1779 parsetree->returningList = copyObject(parsetree->returningList);
1780 ChangeVarNodes((Node *) parsetree->returningList, rt_index,
1781 parsetree->resultRelation, 0);
1782
1783 /*
1784 * To allow the executor to compute the original view row to pass
1785 * to the INSTEAD OF trigger, we add a resjunk whole-row Var
1786 * referencing the original RTE. This will later get expanded
1787 * into a RowExpr computing all the OLD values of the view row.
1788 */
1789 var = makeWholeRowVar(rte, rt_index, 0, false);
1790 tle = makeTargetEntry((Expr *) var,
1791 list_length(parsetree->targetList) + 1,
1792 pstrdup("wholerow"),
1793 true);
1794
1795 parsetree->targetList = lappend(parsetree->targetList, tle);
1796
1797 /* Now, continue with expanding the original view RTE */
1798 }
1799 else
1800 elog(ERROR, "unrecognized commandType: %d",
1801 (int) parsetree->commandType);
1802 }
1803
1804 /*
1805 * Check if there's a FOR [KEY] UPDATE/SHARE clause applying to this view.
1806 *
1807 * Note: we needn't explicitly consider any such clauses appearing in
1808 * ancestor query levels; their effects have already been pushed down to
1809 * here by markQueryForLocking, and will be reflected in "rc".
1810 */
1811 rc = get_parse_rowmark(parsetree, rt_index);
1812
1813 /*
1814 * Make a modifiable copy of the view query, and acquire needed locks on
1815 * the relations it mentions. Force at least RowShareLock for all such
1816 * rels if there's a FOR [KEY] UPDATE/SHARE clause affecting this view.
1817 */
1818 rule_action = copyObject(linitial(rule->actions));
1819
1820 AcquireRewriteLocks(rule_action, true, (rc != NULL));
1821
1822 /*
1823 * If FOR [KEY] UPDATE/SHARE of view, mark all the contained tables as
1824 * implicit FOR [KEY] UPDATE/SHARE, the same as the parser would have done
1825 * if the view's subquery had been written out explicitly.
1826 */
1827 if (rc != NULL)
1828 markQueryForLocking(rule_action, (Node *) rule_action->jointree,
1829 rc->strength, rc->waitPolicy, true);
1830
1831 /*
1832 * Recursively expand any view references inside the view.
1833 */
1834 rule_action = fireRIRrules(rule_action, activeRIRs);
1835
1836 /*
1837 * Make sure the query is marked as having row security if the view query
1838 * does.
1839 */
1840 parsetree->hasRowSecurity |= rule_action->hasRowSecurity;
1841
1842 /*
1843 * Now, plug the view query in as a subselect, converting the relation's
1844 * original RTE to a subquery RTE.
1845 */
1846 rte = rt_fetch(rt_index, parsetree->rtable);
1847
1848 rte->rtekind = RTE_SUBQUERY;
1849 rte->subquery = rule_action;
1850 rte->security_barrier = RelationIsSecurityView(relation);
1851
1852 /*
1853 * Clear fields that should not be set in a subquery RTE. Note that we
1854 * leave the relid, relkind, rellockmode, and perminfoindex fields set, so
1855 * that the view relation can be appropriately locked before execution and
1856 * its permissions checked.
1857 */
1858 rte->tablesample = NULL;
1859 rte->inh = false; /* must not be set for a subquery */
1860
1861 /*
1862 * Since we allow CREATE OR REPLACE VIEW to add columns to a view, the
1863 * rule_action might emit more columns than we expected when the current
1864 * query was parsed. Various places expect rte->eref->colnames to be
1865 * consistent with the non-junk output columns of the subquery, so patch
1866 * things up if necessary by adding some dummy column names.
1867 */
1868 numCols = ExecCleanTargetListLength(rule_action->targetList);
1869 while (list_length(rte->eref->colnames) < numCols)
1870 {
1871 rte->eref->colnames = lappend(rte->eref->colnames,
1872 makeString(pstrdup("?column?")));
1873 }
1874
1875 return parsetree;
1876}
1877
1878/*
1879 * Recursively mark all relations used by a view as FOR [KEY] UPDATE/SHARE.
1880 *
1881 * This may generate an invalid query, eg if some sub-query uses an
1882 * aggregate. We leave it to the planner to detect that.
1883 *
1884 * NB: this must agree with the parser's transformLockingClause() routine.
1885 * However, we used to have to avoid marking a view's OLD and NEW rels for
1886 * updating, which motivated scanning the jointree to determine which rels
1887 * are used. Possibly that could now be simplified into just scanning the
1888 * rangetable as the parser does.
1889 */
1890static void
1892 LockClauseStrength strength, LockWaitPolicy waitPolicy,
1893 bool pushedDown)
1894{
1895 if (jtnode == NULL)
1896 return;
1897 if (IsA(jtnode, RangeTblRef))
1898 {
1899 int rti = ((RangeTblRef *) jtnode)->rtindex;
1900 RangeTblEntry *rte = rt_fetch(rti, qry->rtable);
1901
1902 if (rte->rtekind == RTE_RELATION)
1903 {
1904 RTEPermissionInfo *perminfo;
1905
1906 applyLockingClause(qry, rti, strength, waitPolicy, pushedDown);
1907
1908 perminfo = getRTEPermissionInfo(qry->rteperminfos, rte);
1910 }
1911 else if (rte->rtekind == RTE_SUBQUERY)
1912 {
1913 applyLockingClause(qry, rti, strength, waitPolicy, pushedDown);
1914 /* FOR UPDATE/SHARE of subquery is propagated to subquery's rels */
1916 strength, waitPolicy, true);
1917 }
1918 /* other RTE types are unaffected by FOR UPDATE */
1919 }
1920 else if (IsA(jtnode, FromExpr))
1921 {
1922 FromExpr *f = (FromExpr *) jtnode;
1923 ListCell *l;
1924
1925 foreach(l, f->fromlist)
1926 markQueryForLocking(qry, lfirst(l), strength, waitPolicy, pushedDown);
1927 }
1928 else if (IsA(jtnode, JoinExpr))
1929 {
1930 JoinExpr *j = (JoinExpr *) jtnode;
1931
1932 markQueryForLocking(qry, j->larg, strength, waitPolicy, pushedDown);
1933 markQueryForLocking(qry, j->rarg, strength, waitPolicy, pushedDown);
1934 }
1935 else
1936 elog(ERROR, "unrecognized node type: %d",
1937 (int) nodeTag(jtnode));
1938}
1939
1940
1941/*
1942 * fireRIRonSubLink -
1943 * Apply fireRIRrules() to each SubLink (subselect in expression) found
1944 * in the given tree.
1945 *
1946 * NOTE: although this has the form of a walker, we cheat and modify the
1947 * SubLink nodes in-place. It is caller's responsibility to ensure that
1948 * no unwanted side-effects occur!
1949 *
1950 * This is unlike most of the other routines that recurse into subselects,
1951 * because we must take control at the SubLink node in order to replace
1952 * the SubLink's subselect link with the possibly-rewritten subquery.
1953 */
1954static bool
1956{
1957 if (node == NULL)
1958 return false;
1959 if (IsA(node, SubLink))
1960 {
1961 SubLink *sub = (SubLink *) node;
1962
1963 /* Do what we came for */
1964 sub->subselect = (Node *) fireRIRrules((Query *) sub->subselect,
1965 context->activeRIRs);
1966
1967 /*
1968 * Remember if any of the sublinks have row security.
1969 */
1970 context->hasRowSecurity |= ((Query *) sub->subselect)->hasRowSecurity;
1971
1972 /* Fall through to process lefthand args of SubLink */
1973 }
1974
1975 /*
1976 * Do NOT recurse into Query nodes, because fireRIRrules already processed
1977 * subselects of subselects for us.
1978 */
1979 return expression_tree_walker(node, fireRIRonSubLink, context);
1980}
1981
1982
1983/*
1984 * fireRIRrules -
1985 * Apply all RIR rules on each rangetable entry in the given query
1986 *
1987 * activeRIRs is a list of the OIDs of views we're already processing RIR
1988 * rules for, used to detect/reject recursion.
1989 */
1990static Query *
1991fireRIRrules(Query *parsetree, List *activeRIRs)
1992{
1993 int origResultRelation = parsetree->resultRelation;
1994 int rt_index;
1995 ListCell *lc;
1996
1997 /*
1998 * Expand SEARCH and CYCLE clauses in CTEs.
1999 *
2000 * This is just a convenient place to do this, since we are already
2001 * looking at each Query.
2002 */
2003 foreach(lc, parsetree->cteList)
2004 {
2006
2007 if (cte->search_clause || cte->cycle_clause)
2008 {
2009 cte = rewriteSearchAndCycle(cte);
2010 lfirst(lc) = cte;
2011 }
2012 }
2013
2014 /*
2015 * don't try to convert this into a foreach loop, because rtable list can
2016 * get changed each time through...
2017 */
2018 rt_index = 0;
2019 while (rt_index < list_length(parsetree->rtable))
2020 {
2021 RangeTblEntry *rte;
2022 Relation rel;
2023 List *locks;
2024 RuleLock *rules;
2026 int i;
2027
2028 ++rt_index;
2029
2030 rte = rt_fetch(rt_index, parsetree->rtable);
2031
2032 /*
2033 * A subquery RTE can't have associated rules, so there's nothing to
2034 * do to this level of the query, but we must recurse into the
2035 * subquery to expand any rule references in it.
2036 */
2037 if (rte->rtekind == RTE_SUBQUERY)
2038 {
2039 rte->subquery = fireRIRrules(rte->subquery, activeRIRs);
2040
2041 /*
2042 * While we are here, make sure the query is marked as having row
2043 * security if any of its subqueries do.
2044 */
2045 parsetree->hasRowSecurity |= rte->subquery->hasRowSecurity;
2046
2047 continue;
2048 }
2049
2050 /*
2051 * Joins and other non-relation RTEs can be ignored completely.
2052 */
2053 if (rte->rtekind != RTE_RELATION)
2054 continue;
2055
2056 /*
2057 * Always ignore RIR rules for materialized views referenced in
2058 * queries. (This does not prevent refreshing MVs, since they aren't
2059 * referenced in their own query definitions.)
2060 *
2061 * Note: in the future we might want to allow MVs to be conditionally
2062 * expanded as if they were regular views, if they are not scannable.
2063 * In that case this test would need to be postponed till after we've
2064 * opened the rel, so that we could check its state.
2065 */
2066 if (rte->relkind == RELKIND_MATVIEW)
2067 continue;
2068
2069 /*
2070 * In INSERT ... ON CONFLICT, ignore the EXCLUDED pseudo-relation;
2071 * even if it points to a view, we needn't expand it, and should not
2072 * because we want the RTE to remain of RTE_RELATION type. Otherwise,
2073 * it would get changed to RTE_SUBQUERY type, which is an
2074 * untested/unsupported situation.
2075 */
2076 if (parsetree->onConflict &&
2077 rt_index == parsetree->onConflict->exclRelIndex)
2078 continue;
2079
2080 /*
2081 * If the table is not referenced in the query, then we ignore it.
2082 * This prevents infinite expansion loop due to new rtable entries
2083 * inserted by expansion of a rule. A table is referenced if it is
2084 * part of the join set (a source table), or is referenced by any Var
2085 * nodes, or is the result table.
2086 */
2087 if (rt_index != parsetree->resultRelation &&
2088 !rangeTableEntry_used((Node *) parsetree, rt_index, 0))
2089 continue;
2090
2091 /*
2092 * Also, if this is a new result relation introduced by
2093 * ApplyRetrieveRule, we don't want to do anything more with it.
2094 */
2095 if (rt_index == parsetree->resultRelation &&
2096 rt_index != origResultRelation)
2097 continue;
2098
2099 /*
2100 * We can use NoLock here since either the parser or
2101 * AcquireRewriteLocks should have locked the rel already.
2102 */
2103 rel = table_open(rte->relid, NoLock);
2104
2105 /*
2106 * Collect the RIR rules that we must apply
2107 */
2108 rules = rel->rd_rules;
2109 if (rules != NULL)
2110 {
2111 locks = NIL;
2112 for (i = 0; i < rules->numLocks; i++)
2113 {
2114 rule = rules->rules[i];
2115 if (rule->event != CMD_SELECT)
2116 continue;
2117
2118 locks = lappend(locks, rule);
2119 }
2120
2121 /*
2122 * If we found any, apply them --- but first check for recursion!
2123 */
2124 if (locks != NIL)
2125 {
2126 ListCell *l;
2127
2128 if (list_member_oid(activeRIRs, RelationGetRelid(rel)))
2129 ereport(ERROR,
2130 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
2131 errmsg("infinite recursion detected in rules for relation \"%s\"",
2133 activeRIRs = lappend_oid(activeRIRs, RelationGetRelid(rel));
2134
2135 foreach(l, locks)
2136 {
2137 rule = lfirst(l);
2138
2139 parsetree = ApplyRetrieveRule(parsetree,
2140 rule,
2141 rt_index,
2142 rel,
2143 activeRIRs);
2144 }
2145
2146 activeRIRs = list_delete_last(activeRIRs);
2147 }
2148 }
2149
2150 table_close(rel, NoLock);
2151 }
2152
2153 /* Recurse into subqueries in WITH */
2154 foreach(lc, parsetree->cteList)
2155 {
2156 CommonTableExpr *cte = (CommonTableExpr *) lfirst(lc);
2157
2158 cte->ctequery = (Node *)
2159 fireRIRrules((Query *) cte->ctequery, activeRIRs);
2160
2161 /*
2162 * While we are here, make sure the query is marked as having row
2163 * security if any of its CTEs do.
2164 */
2165 parsetree->hasRowSecurity |= ((Query *) cte->ctequery)->hasRowSecurity;
2166 }
2167
2168 /*
2169 * Recurse into sublink subqueries, too. But we already did the ones in
2170 * the rtable and cteList.
2171 */
2172 if (parsetree->hasSubLinks)
2173 {
2175
2176 context.activeRIRs = activeRIRs;
2177 context.hasRowSecurity = false;
2178
2179 query_tree_walker(parsetree, fireRIRonSubLink, &context,
2181
2182 /*
2183 * Make sure the query is marked as having row security if any of its
2184 * sublinks do.
2185 */
2186 parsetree->hasRowSecurity |= context.hasRowSecurity;
2187 }
2188
2189 /*
2190 * Apply any row-level security policies. We do this last because it
2191 * requires special recursion detection if the new quals have sublink
2192 * subqueries, and if we did it in the loop above query_tree_walker would
2193 * then recurse into those quals a second time.
2194 */
2195 rt_index = 0;
2196 foreach(lc, parsetree->rtable)
2197 {
2198 RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc);
2199 Relation rel;
2200 List *securityQuals;
2201 List *withCheckOptions;
2202 bool hasRowSecurity;
2203 bool hasSubLinks;
2204
2205 ++rt_index;
2206
2207 /* Only normal relations can have RLS policies */
2208 if (rte->rtekind != RTE_RELATION ||
2209 (rte->relkind != RELKIND_RELATION &&
2210 rte->relkind != RELKIND_PARTITIONED_TABLE))
2211 continue;
2212
2213 rel = table_open(rte->relid, NoLock);
2214
2215 /*
2216 * Fetch any new security quals that must be applied to this RTE.
2217 */
2218 get_row_security_policies(parsetree, rte, rt_index,
2219 &securityQuals, &withCheckOptions,
2220 &hasRowSecurity, &hasSubLinks);
2221
2222 if (securityQuals != NIL || withCheckOptions != NIL)
2223 {
2224 if (hasSubLinks)
2225 {
2227 fireRIRonSubLink_context fire_context;
2228
2229 /*
2230 * Recursively process the new quals, checking for infinite
2231 * recursion.
2232 */
2233 if (list_member_oid(activeRIRs, RelationGetRelid(rel)))
2234 ereport(ERROR,
2235 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
2236 errmsg("infinite recursion detected in policy for relation \"%s\"",
2238
2239 activeRIRs = lappend_oid(activeRIRs, RelationGetRelid(rel));
2240
2241 /*
2242 * get_row_security_policies just passed back securityQuals
2243 * and/or withCheckOptions, and there were SubLinks, make sure
2244 * we lock any relations which are referenced.
2245 *
2246 * These locks would normally be acquired by the parser, but
2247 * securityQuals and withCheckOptions are added post-parsing.
2248 */
2249 context.for_execute = true;
2250 (void) acquireLocksOnSubLinks((Node *) securityQuals, &context);
2251 (void) acquireLocksOnSubLinks((Node *) withCheckOptions,
2252 &context);
2253
2254 /*
2255 * Now that we have the locks on anything added by
2256 * get_row_security_policies, fire any RIR rules for them.
2257 */
2258 fire_context.activeRIRs = activeRIRs;
2259 fire_context.hasRowSecurity = false;
2260
2261 expression_tree_walker((Node *) securityQuals,
2262 fireRIRonSubLink, &fire_context);
2263
2264 expression_tree_walker((Node *) withCheckOptions,
2265 fireRIRonSubLink, &fire_context);
2266
2267 /*
2268 * We can ignore the value of fire_context.hasRowSecurity
2269 * since we only reach this code in cases where hasRowSecurity
2270 * is already true.
2271 */
2272 Assert(hasRowSecurity);
2273
2274 activeRIRs = list_delete_last(activeRIRs);
2275 }
2276
2277 /*
2278 * Add the new security barrier quals to the start of the RTE's
2279 * list so that they get applied before any existing barrier quals
2280 * (which would have come from a security-barrier view, and should
2281 * get lower priority than RLS conditions on the table itself).
2282 */
2283 rte->securityQuals = list_concat(securityQuals,
2284 rte->securityQuals);
2285
2286 parsetree->withCheckOptions = list_concat(withCheckOptions,
2287 parsetree->withCheckOptions);
2288 }
2289
2290 /*
2291 * Make sure the query is marked correctly if row-level security
2292 * applies, or if the new quals had sublinks.
2293 */
2294 if (hasRowSecurity)
2295 parsetree->hasRowSecurity = true;
2296 if (hasSubLinks)
2297 parsetree->hasSubLinks = true;
2298
2299 table_close(rel, NoLock);
2300 }
2301
2302 return parsetree;
2303}
2304
2305
2306/*
2307 * Modify the given query by adding 'AND rule_qual IS NOT TRUE' to its
2308 * qualification. This is used to generate suitable "else clauses" for
2309 * conditional INSTEAD rules. (Unfortunately we must use "x IS NOT TRUE",
2310 * not just "NOT x" which the planner is much smarter about, else we will
2311 * do the wrong thing when the qual evaluates to NULL.)
2312 *
2313 * The rule_qual may contain references to OLD or NEW. OLD references are
2314 * replaced by references to the specified rt_index (the relation that the
2315 * rule applies to). NEW references are only possible for INSERT and UPDATE
2316 * queries on the relation itself, and so they should be replaced by copies
2317 * of the related entries in the query's own targetlist.
2318 */
2319static Query *
2321 Node *rule_qual,
2322 int rt_index,
2323 CmdType event)
2324{
2325 /* Don't scribble on the passed qual (it's in the relcache!) */
2326 Node *new_qual = copyObject(rule_qual);
2328
2329 context.for_execute = true;
2330
2331 /*
2332 * In case there are subqueries in the qual, acquire necessary locks and
2333 * fix any deleted JOIN RTE entries. (This is somewhat redundant with
2334 * rewriteRuleAction, but not entirely ... consider restructuring so that
2335 * we only need to process the qual this way once.)
2336 */
2337 (void) acquireLocksOnSubLinks(new_qual, &context);
2338
2339 /* Fix references to OLD */
2340 ChangeVarNodes(new_qual, PRS2_OLD_VARNO, rt_index, 0);
2341 /* Fix references to NEW */
2342 if (event == CMD_INSERT || event == CMD_UPDATE)
2343 new_qual = ReplaceVarsFromTargetList(new_qual,
2345 0,
2346 rt_fetch(rt_index,
2347 parsetree->rtable),
2348 parsetree->targetList,
2349 parsetree->resultRelation,
2350 (event == CMD_UPDATE) ?
2353 rt_index,
2354 &parsetree->hasSubLinks);
2355 /* And attach the fixed qual */
2356 AddInvertedQual(parsetree, new_qual);
2357
2358 return parsetree;
2359}
2360
2361
2362/*
2363 * fireRules -
2364 * Iterate through rule locks applying rules.
2365 *
2366 * Input arguments:
2367 * parsetree - original query
2368 * rt_index - RT index of result relation in original query
2369 * event - type of rule event
2370 * locks - list of rules to fire
2371 * Output arguments:
2372 * *instead_flag - set true if any unqualified INSTEAD rule is found
2373 * (must be initialized to false)
2374 * *returning_flag - set true if we rewrite RETURNING clause in any rule
2375 * (must be initialized to false)
2376 * *qual_product - filled with modified original query if any qualified
2377 * INSTEAD rule is found (must be initialized to NULL)
2378 * Return value:
2379 * list of rule actions adjusted for use with this query
2380 *
2381 * Qualified INSTEAD rules generate their action with the qualification
2382 * condition added. They also generate a modified version of the original
2383 * query with the negated qualification added, so that it will run only for
2384 * rows that the qualified action doesn't act on. (If there are multiple
2385 * qualified INSTEAD rules, we AND all the negated quals onto a single
2386 * modified original query.) We won't execute the original, unmodified
2387 * query if we find either qualified or unqualified INSTEAD rules. If
2388 * we find both, the modified original query is discarded too.
2389 */
2390static List *
2391fireRules(Query *parsetree,
2392 int rt_index,
2393 CmdType event,
2394 List *locks,
2395 bool *instead_flag,
2396 bool *returning_flag,
2397 Query **qual_product)
2398{
2399 List *results = NIL;
2400 ListCell *l;
2401
2402 foreach(l, locks)
2403 {
2404 RewriteRule *rule_lock = (RewriteRule *) lfirst(l);
2405 Node *event_qual = rule_lock->qual;
2406 List *actions = rule_lock->actions;
2407 QuerySource qsrc;
2408 ListCell *r;
2409
2410 /* Determine correct QuerySource value for actions */
2411 if (rule_lock->isInstead)
2412 {
2413 if (event_qual != NULL)
2415 else
2416 {
2417 qsrc = QSRC_INSTEAD_RULE;
2418 *instead_flag = true; /* report unqualified INSTEAD */
2419 }
2420 }
2421 else
2422 qsrc = QSRC_NON_INSTEAD_RULE;
2423
2424 if (qsrc == QSRC_QUAL_INSTEAD_RULE)
2425 {
2426 /*
2427 * If there are INSTEAD rules with qualifications, the original
2428 * query is still performed. But all the negated rule
2429 * qualifications of the INSTEAD rules are added so it does its
2430 * actions only in cases where the rule quals of all INSTEAD rules
2431 * are false. Think of it as the default action in a case. We save
2432 * this in *qual_product so RewriteQuery() can add it to the query
2433 * list after we mangled it up enough.
2434 *
2435 * If we have already found an unqualified INSTEAD rule, then
2436 * *qual_product won't be used, so don't bother building it.
2437 */
2438 if (!*instead_flag)
2439 {
2440 if (*qual_product == NULL)
2441 *qual_product = copyObject(parsetree);
2442 *qual_product = CopyAndAddInvertedQual(*qual_product,
2443 event_qual,
2444 rt_index,
2445 event);
2446 }
2447 }
2448
2449 /* Now process the rule's actions and add them to the result list */
2450 foreach(r, actions)
2451 {
2452 Query *rule_action = lfirst(r);
2453
2454 if (rule_action->commandType == CMD_NOTHING)
2455 continue;
2456
2457 rule_action = rewriteRuleAction(parsetree, rule_action,
2458 event_qual, rt_index, event,
2459 returning_flag);
2460
2461 rule_action->querySource = qsrc;
2462 rule_action->canSetTag = false; /* might change later */
2463
2464 results = lappend(results, rule_action);
2465 }
2466 }
2467
2468 return results;
2469}
2470
2471
2472/*
2473 * get_view_query - get the Query from a view's _RETURN rule.
2474 *
2475 * Caller should have verified that the relation is a view, and therefore
2476 * we should find an ON SELECT action.
2477 *
2478 * Note that the pointer returned is into the relcache and therefore must
2479 * be treated as read-only to the caller and not modified or scribbled on.
2480 */
2481Query *
2483{
2484 int i;
2485
2486 Assert(view->rd_rel->relkind == RELKIND_VIEW);
2487
2488 for (i = 0; i < view->rd_rules->numLocks; i++)
2489 {
2490 RewriteRule *rule = view->rd_rules->rules[i];
2491
2492 if (rule->event == CMD_SELECT)
2493 {
2494 /* A _RETURN rule should have only one action */
2495 if (list_length(rule->actions) != 1)
2496 elog(ERROR, "invalid _RETURN rule action specification");
2497
2498 return (Query *) linitial(rule->actions);
2499 }
2500 }
2501
2502 elog(ERROR, "failed to find _RETURN rule for view");
2503 return NULL; /* keep compiler quiet */
2504}
2505
2506
2507/*
2508 * view_has_instead_trigger - does view have an INSTEAD OF trigger for event?
2509 *
2510 * If it does, we don't want to treat it as auto-updatable. This test can't
2511 * be folded into view_query_is_auto_updatable because it's not an error
2512 * condition.
2513 *
2514 * For MERGE, this will return true if there is an INSTEAD OF trigger for
2515 * every action in mergeActionList, and false if there are any actions that
2516 * lack an INSTEAD OF trigger. If there are no data-modifying MERGE actions
2517 * (only DO NOTHING actions), true is returned so that the view is treated
2518 * as trigger-updatable, rather than erroring out if it's not auto-updatable.
2519 */
2520bool
2521view_has_instead_trigger(Relation view, CmdType event, List *mergeActionList)
2522{
2523 TriggerDesc *trigDesc = view->trigdesc;
2524
2525 switch (event)
2526 {
2527 case CMD_INSERT:
2528 if (trigDesc && trigDesc->trig_insert_instead_row)
2529 return true;
2530 break;
2531 case CMD_UPDATE:
2532 if (trigDesc && trigDesc->trig_update_instead_row)
2533 return true;
2534 break;
2535 case CMD_DELETE:
2536 if (trigDesc && trigDesc->trig_delete_instead_row)
2537 return true;
2538 break;
2539 case CMD_MERGE:
2540 foreach_node(MergeAction, action, mergeActionList)
2541 {
2542 switch (action->commandType)
2543 {
2544 case CMD_INSERT:
2545 if (!trigDesc || !trigDesc->trig_insert_instead_row)
2546 return false;
2547 break;
2548 case CMD_UPDATE:
2549 if (!trigDesc || !trigDesc->trig_update_instead_row)
2550 return false;
2551 break;
2552 case CMD_DELETE:
2553 if (!trigDesc || !trigDesc->trig_delete_instead_row)
2554 return false;
2555 break;
2556 case CMD_NOTHING:
2557 /* No trigger required */
2558 break;
2559 default:
2560 elog(ERROR, "unrecognized commandType: %d", action->commandType);
2561 break;
2562 }
2563 }
2564 return true; /* no actions without an INSTEAD OF trigger */
2565 default:
2566 elog(ERROR, "unrecognized CmdType: %d", (int) event);
2567 break;
2568 }
2569 return false;
2570}
2571
2572
2573/*
2574 * view_col_is_auto_updatable - test whether the specified column of a view
2575 * is auto-updatable. Returns NULL (if the column can be updated) or a message
2576 * string giving the reason that it cannot be.
2577 *
2578 * The returned string has not been translated; if it is shown as an error
2579 * message, the caller should apply _() to translate it.
2580 *
2581 * Note that the checks performed here are local to this view. We do not check
2582 * whether the referenced column of the underlying base relation is updatable.
2583 */
2584static const char *
2586{
2587 Var *var = (Var *) tle->expr;
2588
2589 /*
2590 * For now, the only updatable columns we support are those that are Vars
2591 * referring to user columns of the underlying base relation.
2592 *
2593 * The view targetlist may contain resjunk columns (e.g., a view defined
2594 * like "SELECT * FROM t ORDER BY a+b" is auto-updatable) but such columns
2595 * are not auto-updatable, and in fact should never appear in the outer
2596 * query's targetlist.
2597 */
2598 if (tle->resjunk)
2599 return gettext_noop("Junk view columns are not updatable.");
2600
2601 if (!IsA(var, Var) ||
2602 var->varno != rtr->rtindex ||
2603 var->varlevelsup != 0)
2604 return gettext_noop("View columns that are not columns of their base relation are not updatable.");
2605
2606 if (var->varattno < 0)
2607 return gettext_noop("View columns that refer to system columns are not updatable.");
2608
2609 if (var->varattno == 0)
2610 return gettext_noop("View columns that return whole-row references are not updatable.");
2611
2612 return NULL; /* the view column is updatable */
2613}
2614
2615
2616/*
2617 * view_query_is_auto_updatable - test whether the specified view definition
2618 * represents an auto-updatable view. Returns NULL (if the view can be updated)
2619 * or a message string giving the reason that it cannot be.
2620
2621 * The returned string has not been translated; if it is shown as an error
2622 * message, the caller should apply _() to translate it.
2623 *
2624 * If check_cols is true, the view is required to have at least one updatable
2625 * column (necessary for INSERT/UPDATE). Otherwise the view's columns are not
2626 * checked for updatability. See also view_cols_are_auto_updatable.
2627 *
2628 * Note that the checks performed here are only based on the view definition.
2629 * We do not check whether any base relations referred to by the view are
2630 * updatable.
2631 */
2632const char *
2633view_query_is_auto_updatable(Query *viewquery, bool check_cols)
2634{
2635 RangeTblRef *rtr;
2636 RangeTblEntry *base_rte;
2637
2638 /*----------
2639 * Check if the view is simply updatable. According to SQL-92 this means:
2640 * - No DISTINCT clause.
2641 * - Each TLE is a column reference, and each column appears at most once.
2642 * - FROM contains exactly one base relation.
2643 * - No GROUP BY or HAVING clauses.
2644 * - No set operations (UNION, INTERSECT or EXCEPT).
2645 * - No sub-queries in the WHERE clause that reference the target table.
2646 *
2647 * We ignore that last restriction since it would be complex to enforce
2648 * and there isn't any actual benefit to disallowing sub-queries. (The
2649 * semantic issues that the standard is presumably concerned about don't
2650 * arise in Postgres, since any such sub-query will not see any updates
2651 * executed by the outer query anyway, thanks to MVCC snapshotting.)
2652 *
2653 * We also relax the second restriction by supporting part of SQL:1999
2654 * feature T111, which allows for a mix of updatable and non-updatable
2655 * columns, provided that an INSERT or UPDATE doesn't attempt to assign to
2656 * a non-updatable column.
2657 *
2658 * In addition we impose these constraints, involving features that are
2659 * not part of SQL-92:
2660 * - No CTEs (WITH clauses).
2661 * - No OFFSET or LIMIT clauses (this matches a SQL:2008 restriction).
2662 * - No system columns (including whole-row references) in the tlist.
2663 * - No window functions in the tlist.
2664 * - No set-returning functions in the tlist.
2665 *
2666 * Note that we do these checks without recursively expanding the view.
2667 * If the base relation is a view, we'll recursively deal with it later.
2668 *----------
2669 */
2670 if (viewquery->distinctClause != NIL)
2671 return gettext_noop("Views containing DISTINCT are not automatically updatable.");
2672
2673 if (viewquery->groupClause != NIL || viewquery->groupingSets)
2674 return gettext_noop("Views containing GROUP BY are not automatically updatable.");
2675
2676 if (viewquery->havingQual != NULL)
2677 return gettext_noop("Views containing HAVING are not automatically updatable.");
2678
2679 if (viewquery->setOperations != NULL)
2680 return gettext_noop("Views containing UNION, INTERSECT, or EXCEPT are not automatically updatable.");
2681
2682 if (viewquery->cteList != NIL)
2683 return gettext_noop("Views containing WITH are not automatically updatable.");
2684
2685 if (viewquery->limitOffset != NULL || viewquery->limitCount != NULL)
2686 return gettext_noop("Views containing LIMIT or OFFSET are not automatically updatable.");
2687
2688 /*
2689 * We must not allow window functions or set returning functions in the
2690 * targetlist. Otherwise we might end up inserting them into the quals of
2691 * the main query. We must also check for aggregates in the targetlist in
2692 * case they appear without a GROUP BY.
2693 *
2694 * These restrictions ensure that each row of the view corresponds to a
2695 * unique row in the underlying base relation.
2696 */
2697 if (viewquery->hasAggs)
2698 return gettext_noop("Views that return aggregate functions are not automatically updatable.");
2699
2700 if (viewquery->hasWindowFuncs)
2701 return gettext_noop("Views that return window functions are not automatically updatable.");
2702
2703 if (viewquery->hasTargetSRFs)
2704 return gettext_noop("Views that return set-returning functions are not automatically updatable.");
2705
2706 /*
2707 * The view query should select from a single base relation, which must be
2708 * a table or another view.
2709 */
2710 if (list_length(viewquery->jointree->fromlist) != 1)
2711 return gettext_noop("Views that do not select from a single table or view are not automatically updatable.");
2712
2713 rtr = (RangeTblRef *) linitial(viewquery->jointree->fromlist);
2714 if (!IsA(rtr, RangeTblRef))
2715 return gettext_noop("Views that do not select from a single table or view are not automatically updatable.");
2716
2717 base_rte = rt_fetch(rtr->rtindex, viewquery->rtable);
2718 if (base_rte->rtekind != RTE_RELATION ||
2719 (base_rte->relkind != RELKIND_RELATION &&
2720 base_rte->relkind != RELKIND_FOREIGN_TABLE &&
2721 base_rte->relkind != RELKIND_VIEW &&
2722 base_rte->relkind != RELKIND_PARTITIONED_TABLE))
2723 return gettext_noop("Views that do not select from a single table or view are not automatically updatable.");
2724
2725 if (base_rte->tablesample)
2726 return gettext_noop("Views containing TABLESAMPLE are not automatically updatable.");
2727
2728 /*
2729 * Check that the view has at least one updatable column. This is required
2730 * for INSERT/UPDATE but not for DELETE.
2731 */
2732 if (check_cols)
2733 {
2734 ListCell *cell;
2735 bool found;
2736
2737 found = false;
2738 foreach(cell, viewquery->targetList)
2739 {
2740 TargetEntry *tle = (TargetEntry *) lfirst(cell);
2741
2742 if (view_col_is_auto_updatable(rtr, tle) == NULL)
2743 {
2744 found = true;
2745 break;
2746 }
2747 }
2748
2749 if (!found)
2750 return gettext_noop("Views that have no updatable columns are not automatically updatable.");
2751 }
2752
2753 return NULL; /* the view is updatable */
2754}
2755
2756
2757/*
2758 * view_cols_are_auto_updatable - test whether all of the required columns of
2759 * an auto-updatable view are actually updatable. Returns NULL (if all the
2760 * required columns can be updated) or a message string giving the reason that
2761 * they cannot be.
2762 *
2763 * The returned string has not been translated; if it is shown as an error
2764 * message, the caller should apply _() to translate it.
2765 *
2766 * This should be used for INSERT/UPDATE to ensure that we don't attempt to
2767 * assign to any non-updatable columns.
2768 *
2769 * Additionally it may be used to retrieve the set of updatable columns in the
2770 * view, or if one or more of the required columns is not updatable, the name
2771 * of the first offending non-updatable column.
2772 *
2773 * The caller must have already verified that this is an auto-updatable view
2774 * using view_query_is_auto_updatable.
2775 *
2776 * Note that the checks performed here are only based on the view definition.
2777 * We do not check whether the referenced columns of the base relation are
2778 * updatable.
2779 */
2780static const char *
2782 Bitmapset *required_cols,
2783 Bitmapset **updatable_cols,
2784 char **non_updatable_col)
2785{
2786 RangeTblRef *rtr;
2787 AttrNumber col;
2788 ListCell *cell;
2789
2790 /*
2791 * The caller should have verified that this view is auto-updatable and so
2792 * there should be a single base relation.
2793 */
2794 Assert(list_length(viewquery->jointree->fromlist) == 1);
2795 rtr = linitial_node(RangeTblRef, viewquery->jointree->fromlist);
2796
2797 /* Initialize the optional return values */
2798 if (updatable_cols != NULL)
2799 *updatable_cols = NULL;
2800 if (non_updatable_col != NULL)
2801 *non_updatable_col = NULL;
2802
2803 /* Test each view column for updatability */
2805 foreach(cell, viewquery->targetList)
2806 {
2807 TargetEntry *tle = (TargetEntry *) lfirst(cell);
2808 const char *col_update_detail;
2809
2810 col++;
2811 col_update_detail = view_col_is_auto_updatable(rtr, tle);
2812
2813 if (col_update_detail == NULL)
2814 {
2815 /* The column is updatable */
2816 if (updatable_cols != NULL)
2817 *updatable_cols = bms_add_member(*updatable_cols, col);
2818 }
2819 else if (bms_is_member(col, required_cols))
2820 {
2821 /* The required column is not updatable */
2822 if (non_updatable_col != NULL)
2823 *non_updatable_col = tle->resname;
2824 return col_update_detail;
2825 }
2826 }
2827
2828 return NULL; /* all the required view columns are updatable */
2829}
2830
2831
2832/*
2833 * relation_is_updatable - determine which update events the specified
2834 * relation supports.
2835 *
2836 * Note that views may contain a mix of updatable and non-updatable columns.
2837 * For a view to support INSERT/UPDATE it must have at least one updatable
2838 * column, but there is no such restriction for DELETE. If include_cols is
2839 * non-NULL, then only the specified columns are considered when testing for
2840 * updatability.
2841 *
2842 * Unlike the preceding functions, this does recurse to look at a view's
2843 * base relations, so it needs to detect recursion. To do that, we pass
2844 * a list of currently-considered outer relations. External callers need
2845 * only pass NIL.
2846 *
2847 * This is used for the information_schema views, which have separate concepts
2848 * of "updatable" and "trigger updatable". A relation is "updatable" if it
2849 * can be updated without the need for triggers (either because it has a
2850 * suitable RULE, or because it is simple enough to be automatically updated).
2851 * A relation is "trigger updatable" if it has a suitable INSTEAD OF trigger.
2852 * The SQL standard regards this as not necessarily updatable, presumably
2853 * because there is no way of knowing what the trigger will actually do.
2854 * The information_schema views therefore call this function with
2855 * include_triggers = false. However, other callers might only care whether
2856 * data-modifying SQL will work, so they can pass include_triggers = true
2857 * to have trigger updatability included in the result.
2858 *
2859 * The return value is a bitmask of rule event numbers indicating which of
2860 * the INSERT, UPDATE and DELETE operations are supported. (We do it this way
2861 * so that we can test for UPDATE plus DELETE support in a single call.)
2862 */
2863int
2865 List *outer_reloids,
2866 bool include_triggers,
2867 Bitmapset *include_cols)
2868{
2869 int events = 0;
2870 Relation rel;
2871 RuleLock *rulelocks;
2872
2873#define ALL_EVENTS ((1 << CMD_INSERT) | (1 << CMD_UPDATE) | (1 << CMD_DELETE))
2874
2875 /* Since this function recurses, it could be driven to stack overflow */
2877
2878 rel = try_relation_open(reloid, AccessShareLock);
2879
2880 /*
2881 * If the relation doesn't exist, return zero rather than throwing an
2882 * error. This is helpful since scanning an information_schema view under
2883 * MVCC rules can result in referencing rels that have actually been
2884 * deleted already.
2885 */
2886 if (rel == NULL)
2887 return 0;
2888
2889 /* If we detect a recursive view, report that it is not updatable */
2890 if (list_member_oid(outer_reloids, RelationGetRelid(rel)))
2891 {
2893 return 0;
2894 }
2895
2896 /* If the relation is a table, it is always updatable */
2897 if (rel->rd_rel->relkind == RELKIND_RELATION ||
2898 rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
2899 {
2901 return ALL_EVENTS;
2902 }
2903
2904 /* Look for unconditional DO INSTEAD rules, and note supported events */
2905 rulelocks = rel->rd_rules;
2906 if (rulelocks != NULL)
2907 {
2908 int i;
2909
2910 for (i = 0; i < rulelocks->numLocks; i++)
2911 {
2912 if (rulelocks->rules[i]->isInstead &&
2913 rulelocks->rules[i]->qual == NULL)
2914 {
2915 events |= ((1 << rulelocks->rules[i]->event) & ALL_EVENTS);
2916 }
2917 }
2918
2919 /* If we have rules for all events, we're done */
2920 if (events == ALL_EVENTS)
2921 {
2923 return events;
2924 }
2925 }
2926
2927 /* Similarly look for INSTEAD OF triggers, if they are to be included */
2928 if (include_triggers)
2929 {
2930 TriggerDesc *trigDesc = rel->trigdesc;
2931
2932 if (trigDesc)
2933 {
2934 if (trigDesc->trig_insert_instead_row)
2935 events |= (1 << CMD_INSERT);
2936 if (trigDesc->trig_update_instead_row)
2937 events |= (1 << CMD_UPDATE);
2938 if (trigDesc->trig_delete_instead_row)
2939 events |= (1 << CMD_DELETE);
2940
2941 /* If we have triggers for all events, we're done */
2942 if (events == ALL_EVENTS)
2943 {
2945 return events;
2946 }
2947 }
2948 }
2949
2950 /* If this is a foreign table, check which update events it supports */
2951 if (rel->rd_rel->relkind == RELKIND_FOREIGN_TABLE)
2952 {
2953 FdwRoutine *fdwroutine = GetFdwRoutineForRelation(rel, false);
2954
2955 if (fdwroutine->IsForeignRelUpdatable != NULL)
2956 events |= fdwroutine->IsForeignRelUpdatable(rel);
2957 else
2958 {
2959 /* Assume presence of executor functions is sufficient */
2960 if (fdwroutine->ExecForeignInsert != NULL)
2961 events |= (1 << CMD_INSERT);
2962 if (fdwroutine->ExecForeignUpdate != NULL)
2963 events |= (1 << CMD_UPDATE);
2964 if (fdwroutine->ExecForeignDelete != NULL)
2965 events |= (1 << CMD_DELETE);
2966 }
2967
2969 return events;
2970 }
2971
2972 /* Check if this is an automatically updatable view */
2973 if (rel->rd_rel->relkind == RELKIND_VIEW)
2974 {
2975 Query *viewquery = get_view_query(rel);
2976
2977 if (view_query_is_auto_updatable(viewquery, false) == NULL)
2978 {
2979 Bitmapset *updatable_cols;
2980 int auto_events;
2981 RangeTblRef *rtr;
2982 RangeTblEntry *base_rte;
2983 Oid baseoid;
2984
2985 /*
2986 * Determine which of the view's columns are updatable. If there
2987 * are none within the set of columns we are looking at, then the
2988 * view doesn't support INSERT/UPDATE, but it may still support
2989 * DELETE.
2990 */
2991 view_cols_are_auto_updatable(viewquery, NULL,
2992 &updatable_cols, NULL);
2993
2994 if (include_cols != NULL)
2995 updatable_cols = bms_int_members(updatable_cols, include_cols);
2996
2997 if (bms_is_empty(updatable_cols))
2998 auto_events = (1 << CMD_DELETE); /* May support DELETE */
2999 else
3000 auto_events = ALL_EVENTS; /* May support all events */
3001
3002 /*
3003 * The base relation must also support these update commands.
3004 * Tables are always updatable, but for any other kind of base
3005 * relation we must do a recursive check limited to the columns
3006 * referenced by the locally updatable columns in this view.
3007 */
3008 rtr = (RangeTblRef *) linitial(viewquery->jointree->fromlist);
3009 base_rte = rt_fetch(rtr->rtindex, viewquery->rtable);
3010 Assert(base_rte->rtekind == RTE_RELATION);
3011
3012 if (base_rte->relkind != RELKIND_RELATION &&
3013 base_rte->relkind != RELKIND_PARTITIONED_TABLE)
3014 {
3015 baseoid = base_rte->relid;
3016 outer_reloids = lappend_oid(outer_reloids,
3017 RelationGetRelid(rel));
3018 include_cols = adjust_view_column_set(updatable_cols,
3019 viewquery->targetList);
3020 auto_events &= relation_is_updatable(baseoid,
3021 outer_reloids,
3022 include_triggers,
3023 include_cols);
3024 outer_reloids = list_delete_last(outer_reloids);
3025 }
3026 events |= auto_events;
3027 }
3028 }
3029
3030 /* If we reach here, the relation may support some update commands */
3032 return events;
3033}
3034
3035
3036/*
3037 * adjust_view_column_set - map a set of column numbers according to targetlist
3038 *
3039 * This is used with simply-updatable views to map column-permissions sets for
3040 * the view columns onto the matching columns in the underlying base relation.
3041 * Relevant entries in the targetlist must be plain Vars of the underlying
3042 * relation (as per the checks above in view_query_is_auto_updatable).
3043 */
3044static Bitmapset *
3046{
3047 Bitmapset *result = NULL;
3048 int col;
3049
3050 col = -1;
3051 while ((col = bms_next_member(cols, col)) >= 0)
3052 {
3053 /* bit numbers are offset by FirstLowInvalidHeapAttributeNumber */
3055
3056 if (attno == InvalidAttrNumber)
3057 {
3058 /*
3059 * There's a whole-row reference to the view. For permissions
3060 * purposes, treat it as a reference to each column available from
3061 * the view. (We should *not* convert this to a whole-row
3062 * reference to the base relation, since the view may not touch
3063 * all columns of the base relation.)
3064 */
3065 ListCell *lc;
3066
3067 foreach(lc, targetlist)
3068 {
3070 Var *var;
3071
3072 if (tle->resjunk)
3073 continue;
3074 var = castNode(Var, tle->expr);
3075 result = bms_add_member(result,
3077 }
3078 }
3079 else
3080 {
3081 /*
3082 * Views do not have system columns, so we do not expect to see
3083 * any other system attnos here. If we do find one, the error
3084 * case will apply.
3085 */
3086 TargetEntry *tle = get_tle_by_resno(targetlist, attno);
3087
3088 if (tle != NULL && !tle->resjunk && IsA(tle->expr, Var))
3089 {
3090 Var *var = (Var *) tle->expr;
3091
3092 result = bms_add_member(result,
3094 }
3095 else
3096 elog(ERROR, "attribute number %d not found in view targetlist",
3097 attno);
3098 }
3099 }
3100
3101 return result;
3102}
3103
3104
3105/*
3106 * error_view_not_updatable -
3107 * Report an error due to an attempt to update a non-updatable view.
3108 *
3109 * Generally this is expected to be called from the rewriter, with suitable
3110 * error detail explaining why the view is not updatable. Note, however, that
3111 * the executor also performs a just-in-case check that the target view is
3112 * updatable. That check is expected to never fail, but if it does, it will
3113 * call this function with NULL error detail --- see CheckValidResultRel().
3114 *
3115 * Note: for MERGE, at least one of the actions in mergeActionList is expected
3116 * to lack a suitable INSTEAD OF trigger --- see view_has_instead_trigger().
3117 */
3118void
3120 CmdType command,
3121 List *mergeActionList,
3122 const char *detail)
3123{
3124 TriggerDesc *trigDesc = view->trigdesc;
3125
3126 switch (command)
3127 {
3128 case CMD_INSERT:
3129 ereport(ERROR,
3130 errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
3131 errmsg("cannot insert into view \"%s\"",
3133 detail ? errdetail_internal("%s", _(detail)) : 0,
3134 errhint("To enable inserting into the view, provide an INSTEAD OF INSERT trigger or an unconditional ON INSERT DO INSTEAD rule."));
3135 break;
3136 case CMD_UPDATE:
3137 ereport(ERROR,
3138 errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
3139 errmsg("cannot update view \"%s\"",
3141 detail ? errdetail_internal("%s", _(detail)) : 0,
3142 errhint("To enable updating the view, provide an INSTEAD OF UPDATE trigger or an unconditional ON UPDATE DO INSTEAD rule."));
3143 break;
3144 case CMD_DELETE:
3145 ereport(ERROR,
3146 errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
3147 errmsg("cannot delete from view \"%s\"",
3149 detail ? errdetail_internal("%s", _(detail)) : 0,
3150 errhint("To enable deleting from the view, provide an INSTEAD OF DELETE trigger or an unconditional ON DELETE DO INSTEAD rule."));
3151 break;
3152 case CMD_MERGE:
3153
3154 /*
3155 * Note that the error hints here differ from above, since MERGE
3156 * doesn't support rules.
3157 */
3158 foreach_node(MergeAction, action, mergeActionList)
3159 {
3160 switch (action->commandType)
3161 {
3162 case CMD_INSERT:
3163 if (!trigDesc || !trigDesc->trig_insert_instead_row)
3164 ereport(ERROR,
3165 errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
3166 errmsg("cannot insert into view \"%s\"",
3168 detail ? errdetail_internal("%s", _(detail)) : 0,
3169 errhint("To enable inserting into the view using MERGE, provide an INSTEAD OF INSERT trigger."));
3170 break;
3171 case CMD_UPDATE:
3172 if (!trigDesc || !trigDesc->trig_update_instead_row)
3173 ereport(ERROR,
3174 errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
3175 errmsg("cannot update view \"%s\"",
3177 detail ? errdetail_internal("%s", _(detail)) : 0,
3178 errhint("To enable updating the view using MERGE, provide an INSTEAD OF UPDATE trigger."));
3179 break;
3180 case CMD_DELETE:
3181 if (!trigDesc || !trigDesc->trig_delete_instead_row)
3182 ereport(ERROR,
3183 errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
3184 errmsg("cannot delete from view \"%s\"",
3186 detail ? errdetail_internal("%s", _(detail)) : 0,
3187 errhint("To enable deleting from the view using MERGE, provide an INSTEAD OF DELETE trigger."));
3188 break;
3189 case CMD_NOTHING:
3190 break;
3191 default:
3192 elog(ERROR, "unrecognized commandType: %d", action->commandType);
3193 break;
3194 }
3195 }
3196 break;
3197 default:
3198 elog(ERROR, "unrecognized CmdType: %d", (int) command);
3199 break;
3200 }
3201}
3202
3203
3204/*
3205 * rewriteTargetView -
3206 * Attempt to rewrite a query where the target relation is a view, so that
3207 * the view's base relation becomes the target relation.
3208 *
3209 * Note that the base relation here may itself be a view, which may or may not
3210 * have INSTEAD OF triggers or rules to handle the update. That is handled by
3211 * the recursion in RewriteQuery.
3212 */
3213static Query *
3215{
3216 Query *viewquery;
3217 bool insert_or_update;
3218 const char *auto_update_detail;
3219 RangeTblRef *rtr;
3220 int base_rt_index;
3221 int new_rt_index;
3222 RangeTblEntry *base_rte;
3223 RangeTblEntry *view_rte;
3224 RangeTblEntry *new_rte;
3225 RTEPermissionInfo *base_perminfo;
3226 RTEPermissionInfo *view_perminfo;
3227 RTEPermissionInfo *new_perminfo;
3228 Relation base_rel;
3229 List *view_targetlist;
3230 ListCell *lc;
3231
3232 /*
3233 * Get the Query from the view's ON SELECT rule. We're going to munge the
3234 * Query to change the view's base relation into the target relation,
3235 * along with various other changes along the way, so we need to make a
3236 * copy of it (get_view_query() returns a pointer into the relcache, so we
3237 * have to treat it as read-only).
3238 */
3239 viewquery = copyObject(get_view_query(view));
3240
3241 /* Locate RTE and perminfo describing the view in the outer query */
3242 view_rte = rt_fetch(parsetree->resultRelation, parsetree->rtable);
3243 view_perminfo = getRTEPermissionInfo(parsetree->rteperminfos, view_rte);
3244
3245 /*
3246 * Are we doing INSERT/UPDATE, or MERGE containing INSERT/UPDATE? If so,
3247 * various additional checks on the view columns need to be applied, and
3248 * any view CHECK OPTIONs need to be enforced.
3249 */
3250 insert_or_update =
3251 (parsetree->commandType == CMD_INSERT ||
3252 parsetree->commandType == CMD_UPDATE);
3253
3254 if (parsetree->commandType == CMD_MERGE)
3255 {
3257 {
3258 if (action->commandType == CMD_INSERT ||
3259 action->commandType == CMD_UPDATE)
3260 {
3261 insert_or_update = true;
3262 break;
3263 }
3264 }
3265 }
3266
3267 /* Check if the expansion of non-system views are restricted */
3270 ereport(ERROR,
3271 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
3272 errmsg("access to non-system view \"%s\" is restricted",
3273 RelationGetRelationName(view))));
3274
3275 /*
3276 * The view must be updatable, else fail.
3277 *
3278 * If we are doing INSERT/UPDATE (or MERGE containing INSERT/UPDATE), we
3279 * also check that there is at least one updatable column.
3280 */
3281 auto_update_detail =
3282 view_query_is_auto_updatable(viewquery, insert_or_update);
3283
3284 if (auto_update_detail)
3286 parsetree->commandType,
3287 parsetree->mergeActionList,
3288 auto_update_detail);
3289
3290 /*
3291 * For INSERT/UPDATE (or MERGE containing INSERT/UPDATE) the modified
3292 * columns must all be updatable.
3293 */
3294 if (insert_or_update)
3295 {
3296 Bitmapset *modified_cols;
3297 char *non_updatable_col;
3298
3299 /*
3300 * Compute the set of modified columns as those listed in the result
3301 * RTE's insertedCols and/or updatedCols sets plus those that are
3302 * targets of the query's targetlist(s). We must consider the query's
3303 * targetlist because rewriteTargetListIU may have added additional
3304 * targetlist entries for view defaults, and these must also be
3305 * updatable. But rewriteTargetListIU can also remove entries if they
3306 * are DEFAULT markers and the column's default is NULL, so
3307 * considering only the targetlist would also be wrong.
3308 */
3309 modified_cols = bms_union(view_perminfo->insertedCols,
3310 view_perminfo->updatedCols);
3311
3312 foreach(lc, parsetree->targetList)
3313 {
3314 TargetEntry *tle = (TargetEntry *) lfirst(lc);
3315
3316 if (!tle->resjunk)
3317 modified_cols = bms_add_member(modified_cols,
3319 }
3320
3321 if (parsetree->onConflict)
3322 {
3323 foreach(lc, parsetree->onConflict->onConflictSet)
3324 {
3325 TargetEntry *tle = (TargetEntry *) lfirst(lc);
3326
3327 if (!tle->resjunk)
3328 modified_cols = bms_add_member(modified_cols,
3330 }
3331 }
3332
3334 {
3335 if (action->commandType == CMD_INSERT ||
3336 action->commandType == CMD_UPDATE)
3337 {
3338 foreach_node(TargetEntry, tle, action->targetList)
3339 {
3340 if (!tle->resjunk)
3341 modified_cols = bms_add_member(modified_cols,
3343 }
3344 }
3345 }
3346
3347 auto_update_detail = view_cols_are_auto_updatable(viewquery,
3348 modified_cols,
3349 NULL,
3350 &non_updatable_col);
3351 if (auto_update_detail)
3352 {
3353 /*
3354 * This is a different error, caused by an attempt to update a
3355 * non-updatable column in an otherwise updatable view.
3356 */
3357 switch (parsetree->commandType)
3358 {
3359 case CMD_INSERT:
3360 ereport(ERROR,
3361 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3362 errmsg("cannot insert into column \"%s\" of view \"%s\"",
3363 non_updatable_col,
3365 errdetail_internal("%s", _(auto_update_detail))));
3366 break;
3367 case CMD_UPDATE:
3368 ereport(ERROR,
3369 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3370 errmsg("cannot update column \"%s\" of view \"%s\"",
3371 non_updatable_col,
3373 errdetail_internal("%s", _(auto_update_detail))));
3374 break;
3375 case CMD_MERGE:
3376 ereport(ERROR,
3377 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3378 errmsg("cannot merge into column \"%s\" of view \"%s\"",
3379 non_updatable_col,
3381 errdetail_internal("%s", _(auto_update_detail))));
3382 break;
3383 default:
3384 elog(ERROR, "unrecognized CmdType: %d",
3385 (int) parsetree->commandType);
3386 break;
3387 }
3388 }
3389 }
3390
3391 /*
3392 * For MERGE, there must not be any INSTEAD OF triggers on an otherwise
3393 * updatable view. The caller already checked that there isn't a full set
3394 * of INSTEAD OF triggers, so this is to guard against having a partial
3395 * set (mixing auto-update and trigger-update actions in a single command
3396 * isn't supported).
3397 */
3398 if (parsetree->commandType == CMD_MERGE)
3399 {
3401 {
3402 if (action->commandType != CMD_NOTHING &&
3403 view_has_instead_trigger(view, action->commandType, NIL))
3404 ereport(ERROR,
3405 errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3406 errmsg("cannot merge into view \"%s\"",
3408 errdetail("MERGE is not supported for views with INSTEAD OF triggers for some actions but not all."),
3409 errhint("To enable merging into the view, either provide a full set of INSTEAD OF triggers or drop the existing INSTEAD OF triggers."));
3410 }
3411 }
3412
3413 /*
3414 * If we get here, view_query_is_auto_updatable() has verified that the
3415 * view contains a single base relation.
3416 */
3417 Assert(list_length(viewquery->jointree->fromlist) == 1);
3418 rtr = linitial_node(RangeTblRef, viewquery->jointree->fromlist);
3419
3420 base_rt_index = rtr->rtindex;
3421 base_rte = rt_fetch(base_rt_index, viewquery->rtable);
3422 Assert(base_rte->rtekind == RTE_RELATION);
3423 base_perminfo = getRTEPermissionInfo(viewquery->rteperminfos, base_rte);
3424
3425 /*
3426 * Up to now, the base relation hasn't been touched at all in our query.
3427 * We need to acquire lock on it before we try to do anything with it.
3428 * (The subsequent recursive call of RewriteQuery will suppose that we
3429 * already have the right lock!) Since it will become the query target
3430 * relation, RowExclusiveLock is always the right thing.
3431 */
3432 base_rel = table_open(base_rte->relid, RowExclusiveLock);
3433
3434 /*
3435 * While we have the relation open, update the RTE's relkind, just in case
3436 * it changed since this view was made (cf. AcquireRewriteLocks).
3437 */
3438 base_rte->relkind = base_rel->rd_rel->relkind;
3439
3440 /*
3441 * If the view query contains any sublink subqueries then we need to also
3442 * acquire locks on any relations they refer to. We know that there won't
3443 * be any subqueries in the range table or CTEs, so we can skip those, as
3444 * in AcquireRewriteLocks.
3445 */
3446 if (viewquery->hasSubLinks)
3447 {
3449
3450 context.for_execute = true;
3451 query_tree_walker(viewquery, acquireLocksOnSubLinks, &context,
3453 }
3454
3455 /*
3456 * Create a new target RTE describing the base relation, and add it to the
3457 * outer query's rangetable. (What's happening in the next few steps is
3458 * very much like what the planner would do to "pull up" the view into the
3459 * outer query. Perhaps someday we should refactor things enough so that
3460 * we can share code with the planner.)
3461 *
3462 * Be sure to set rellockmode to the correct thing for the target table.
3463 * Since we copied the whole viewquery above, we can just scribble on
3464 * base_rte instead of copying it.
3465 */
3466 new_rte = base_rte;
3467 new_rte->rellockmode = RowExclusiveLock;
3468
3469 parsetree->rtable = lappend(parsetree->rtable, new_rte);
3470 new_rt_index = list_length(parsetree->rtable);
3471
3472 /*
3473 * INSERTs never inherit. For UPDATE/DELETE/MERGE, we use the view
3474 * query's inheritance flag for the base relation.
3475 */
3476 if (parsetree->commandType == CMD_INSERT)
3477 new_rte->inh = false;
3478
3479 /*
3480 * Adjust the view's targetlist Vars to reference the new target RTE, ie
3481 * make their varnos be new_rt_index instead of base_rt_index. There can
3482 * be no Vars for other rels in the tlist, so this is sufficient to pull
3483 * up the tlist expressions for use in the outer query. The tlist will
3484 * provide the replacement expressions used by ReplaceVarsFromTargetList
3485 * below.
3486 */
3487 view_targetlist = viewquery->targetList;
3488
3489 ChangeVarNodes((Node *) view_targetlist,
3490 base_rt_index,
3491 new_rt_index,
3492 0);
3493
3494 /*
3495 * If the view has "security_invoker" set, mark the new target relation
3496 * for the permissions checks that we want to enforce against the query
3497 * caller. Otherwise we want to enforce them against the view owner.
3498 *
3499 * At the relation level, require the same INSERT/UPDATE/DELETE
3500 * permissions that the query caller needs against the view. We drop the
3501 * ACL_SELECT bit that is presumably in new_perminfo->requiredPerms
3502 * initially.
3503 *
3504 * Note: the original view's RTEPermissionInfo remains in the query's
3505 * rteperminfos so that the executor still performs appropriate
3506 * permissions checks for the query caller's use of the view.
3507 *
3508 * Disregard the perminfo in viewquery->rteperminfos that the base_rte
3509 * would currently be pointing at, because we'd like it to point now to a
3510 * new one that will be filled below. Must set perminfoindex to 0 to not
3511 * trip over the Assert in addRTEPermissionInfo().
3512 */
3513 new_rte->perminfoindex = 0;
3514 new_perminfo = addRTEPermissionInfo(&parsetree->rteperminfos, new_rte);
3516 new_perminfo->checkAsUser = InvalidOid;
3517 else
3518 new_perminfo->checkAsUser = view->rd_rel->relowner;
3519 new_perminfo->requiredPerms = view_perminfo->requiredPerms;
3520
3521 /*
3522 * Now for the per-column permissions bits.
3523 *
3524 * Initially, new_perminfo (base_perminfo) contains selectedCols
3525 * permission check bits for all base-rel columns referenced by the view,
3526 * but since the view is a SELECT query its insertedCols/updatedCols is
3527 * empty. We set insertedCols and updatedCols to include all the columns
3528 * the outer query is trying to modify, adjusting the column numbers as
3529 * needed. But we leave selectedCols as-is, so the view owner must have
3530 * read permission for all columns used in the view definition, even if
3531 * some of them are not read by the outer query. We could try to limit
3532 * selectedCols to only columns used in the transformed query, but that
3533 * does not correspond to what happens in ordinary SELECT usage of a view:
3534 * all referenced columns must have read permission, even if optimization
3535 * finds that some of them can be discarded during query transformation.
3536 * The flattening we're doing here is an optional optimization, too. (If
3537 * you are unpersuaded and want to change this, note that applying
3538 * adjust_view_column_set to view_perminfo->selectedCols is clearly *not*
3539 * the right answer, since that neglects base-rel columns used in the
3540 * view's WHERE quals.)
3541 *
3542 * This step needs the modified view targetlist, so we have to do things
3543 * in this order.
3544 */
3545 Assert(bms_is_empty(new_perminfo->insertedCols) &&
3546 bms_is_empty(new_perminfo->updatedCols));
3547
3548 new_perminfo->selectedCols = base_perminfo->selectedCols;
3549
3550 new_perminfo->insertedCols =
3551 adjust_view_column_set(view_perminfo->insertedCols, view_targetlist);
3552
3553 new_perminfo->updatedCols =
3554 adjust_view_column_set(view_perminfo->updatedCols, view_targetlist);
3555
3556 /*
3557 * Move any security barrier quals from the view RTE onto the new target
3558 * RTE. Any such quals should now apply to the new target RTE and will
3559 * not reference the original view RTE in the rewritten query.
3560 */
3561 new_rte->securityQuals = view_rte->securityQuals;
3562 view_rte->securityQuals = NIL;
3563
3564 /*
3565 * Now update all Vars in the outer query that reference the view to
3566 * reference the appropriate column of the base relation instead.
3567 */
3568 parsetree = (Query *)
3569 ReplaceVarsFromTargetList((Node *) parsetree,
3570 parsetree->resultRelation,
3571 0,
3572 view_rte,
3573 view_targetlist,
3574 new_rt_index,
3576 0,
3577 NULL);
3578
3579 /*
3580 * Update all other RTI references in the query that point to the view
3581 * (for example, parsetree->resultRelation itself) to point to the new
3582 * base relation instead. Vars will not be affected since none of them
3583 * reference parsetree->resultRelation any longer.
3584 */
3585 ChangeVarNodes((Node *) parsetree,
3586 parsetree->resultRelation,
3587 new_rt_index,
3588 0);
3589 Assert(parsetree->resultRelation == new_rt_index);
3590
3591 /*
3592 * For INSERT/UPDATE we must also update resnos in the targetlist to refer
3593 * to columns of the base relation, since those indicate the target
3594 * columns to be affected. Similarly, for MERGE we must update the resnos
3595 * in the merge action targetlists of any INSERT/UPDATE actions.
3596 *
3597 * Note that this destroys the resno ordering of the targetlists, but that
3598 * will be fixed when we recurse through RewriteQuery, which will invoke
3599 * rewriteTargetListIU again on the updated targetlists.
3600 */
3601 if (parsetree->commandType != CMD_DELETE)
3602 {
3603 foreach(lc, parsetree->targetList)
3604 {
3605 TargetEntry *tle = (TargetEntry *) lfirst(lc);
3606 TargetEntry *view_tle;
3607
3608 if (tle->resjunk)
3609 continue;
3610
3611 view_tle = get_tle_by_resno(view_targetlist, tle->resno);
3612 if (view_tle != NULL && !view_tle->resjunk && IsA(view_tle->expr, Var))
3613 tle->resno = ((Var *) view_tle->expr)->varattno;
3614 else
3615 elog(ERROR, "attribute number %d not found in view targetlist",
3616 tle->resno);
3617 }
3618
3620 {
3621 if (action->commandType == CMD_INSERT ||
3622 action->commandType == CMD_UPDATE)
3623 {
3624 foreach_node(TargetEntry, tle, action->targetList)
3625 {
3626 TargetEntry *view_tle;
3627
3628 if (tle->resjunk)
3629 continue;
3630
3631 view_tle = get_tle_by_resno(view_targetlist, tle->resno);
3632 if (view_tle != NULL && !view_tle->resjunk && IsA(view_tle->expr, Var))
3633 tle->resno = ((Var *) view_tle->expr)->varattno;
3634 else
3635 elog(ERROR, "attribute number %d not found in view targetlist",
3636 tle->resno);
3637 }
3638 }
3639 }
3640 }
3641
3642 /*
3643 * For INSERT .. ON CONFLICT .. DO UPDATE, we must also update assorted
3644 * stuff in the onConflict data structure.
3645 */
3646 if (parsetree->onConflict &&
3647 parsetree->onConflict->action == ONCONFLICT_UPDATE)
3648 {
3649 Index old_exclRelIndex,
3650 new_exclRelIndex;
3651 ParseNamespaceItem *new_exclNSItem;
3652 RangeTblEntry *new_exclRte;
3653 List *tmp_tlist;
3654
3655 /*
3656 * Like the INSERT/UPDATE code above, update the resnos in the
3657 * auxiliary UPDATE targetlist to refer to columns of the base
3658 * relation.
3659 */
3660 foreach(lc, parsetree->onConflict->onConflictSet)
3661 {
3662 TargetEntry *tle = (TargetEntry *) lfirst(lc);
3663 TargetEntry *view_tle;
3664
3665 if (tle->resjunk)
3666 continue;
3667
3668 view_tle = get_tle_by_resno(view_targetlist, tle->resno);
3669 if (view_tle != NULL && !view_tle->resjunk && IsA(view_tle->expr, Var))
3670 tle->resno = ((Var *) view_tle->expr)->varattno;
3671 else
3672 elog(ERROR, "attribute number %d not found in view targetlist",
3673 tle->resno);
3674 }
3675
3676 /*
3677 * Also, create a new RTE for the EXCLUDED pseudo-relation, using the
3678 * query's new base rel (which may well have a different column list
3679 * from the view, hence we need a new column alias list). This should
3680 * match transformOnConflictClause. In particular, note that the
3681 * relkind is set to composite to signal that we're not dealing with
3682 * an actual relation.
3683 */
3684 old_exclRelIndex = parsetree->onConflict->exclRelIndex;
3685
3686 new_exclNSItem = addRangeTableEntryForRelation(make_parsestate(NULL),
3687 base_rel,
3689 makeAlias("excluded", NIL),
3690 false, false);
3691 new_exclRte = new_exclNSItem->p_rte;
3692 new_exclRte->relkind = RELKIND_COMPOSITE_TYPE;
3693 /* Ignore the RTEPermissionInfo that would've been added. */
3694 new_exclRte->perminfoindex = 0;
3695
3696 parsetree->rtable = lappend(parsetree->rtable, new_exclRte);
3697 new_exclRelIndex = parsetree->onConflict->exclRelIndex =
3698 list_length(parsetree->rtable);
3699
3700 /*
3701 * Replace the targetlist for the EXCLUDED pseudo-relation with a new
3702 * one, representing the columns from the new base relation.
3703 */
3704 parsetree->onConflict->exclRelTlist =
3705 BuildOnConflictExcludedTargetlist(base_rel, new_exclRelIndex);
3706
3707 /*
3708 * Update all Vars in the ON CONFLICT clause that refer to the old
3709 * EXCLUDED pseudo-relation. We want to use the column mappings
3710 * defined in the view targetlist, but we need the outputs to refer to
3711 * the new EXCLUDED pseudo-relation rather than the new target RTE.
3712 * Also notice that "EXCLUDED.*" will be expanded using the view's
3713 * rowtype, which seems correct.
3714 */
3715 tmp_tlist = copyObject(view_targetlist);
3716
3717 ChangeVarNodes((Node *) tmp_tlist, new_rt_index,
3718 new_exclRelIndex, 0);
3719
3720 parsetree->onConflict = (OnConflictExpr *)
3722 old_exclRelIndex,
3723 0,
3724 view_rte,
3725 tmp_tlist,
3726 new_rt_index,
3728 0,
3729 &parsetree->hasSubLinks);
3730 }
3731
3732 /*
3733 * For UPDATE/DELETE/MERGE, pull up any WHERE quals from the view. We
3734 * know that any Vars in the quals must reference the one base relation,
3735 * so we need only adjust their varnos to reference the new target (just
3736 * the same as we did with the view targetlist).
3737 *
3738 * If it's a security-barrier view, its WHERE quals must be applied before
3739 * quals from the outer query, so we attach them to the RTE as security
3740 * barrier quals rather than adding them to the main WHERE clause.
3741 *
3742 * For INSERT, the view's quals can be ignored in the main query.
3743 */
3744 if (parsetree->commandType != CMD_INSERT &&
3745 viewquery->jointree->quals != NULL)
3746 {
3747 Node *viewqual = (Node *) viewquery->jointree->quals;
3748
3749 /*
3750 * Even though we copied viewquery already at the top of this
3751 * function, we must duplicate the viewqual again here, because we may
3752 * need to use the quals again below for a WithCheckOption clause.
3753 */
3754 viewqual = copyObject(viewqual);
3755
3756 ChangeVarNodes(viewqual, base_rt_index, new_rt_index, 0);
3757
3758 if (RelationIsSecurityView(view))
3759 {
3760 /*
3761 * The view's quals go in front of existing barrier quals: those
3762 * would have come from an outer level of security-barrier view,
3763 * and so must get evaluated later.
3764 *
3765 * Note: the parsetree has been mutated, so the new_rte pointer is
3766 * stale and needs to be re-computed.
3767 */
3768 new_rte = rt_fetch(new_rt_index, parsetree->rtable);
3769 new_rte->securityQuals = lcons(viewqual, new_rte->securityQuals);
3770
3771 /*
3772 * Do not set parsetree->hasRowSecurity, because these aren't RLS
3773 * conditions (they aren't affected by enabling/disabling RLS).
3774 */
3775
3776 /*
3777 * Make sure that the query is marked correctly if the added qual
3778 * has sublinks.
3779 */
3780 if (!parsetree->hasSubLinks)
3781 parsetree->hasSubLinks = checkExprHasSubLink(viewqual);
3782 }
3783 else
3784 AddQual(parsetree, (Node *) viewqual);
3785 }
3786
3787 /*
3788 * For INSERT/UPDATE (or MERGE containing INSERT/UPDATE), if the view has
3789 * the WITH CHECK OPTION, or any parent view specified WITH CASCADED CHECK
3790 * OPTION, add the quals from the view to the query's withCheckOptions
3791 * list.
3792 */
3793 if (insert_or_update)
3794 {
3795 bool has_wco = RelationHasCheckOption(view);
3796 bool cascaded = RelationHasCascadedCheckOption(view);
3797
3798 /*
3799 * If the parent view has a cascaded check option, treat this view as
3800 * if it also had a cascaded check option.
3801 *
3802 * New WithCheckOptions are added to the start of the list, so if
3803 * there is a cascaded check option, it will be the first item in the
3804 * list.
3805 */
3806 if (parsetree->withCheckOptions != NIL)
3807 {
3808 WithCheckOption *parent_wco =
3809 (WithCheckOption *) linitial(parsetree->withCheckOptions);
3810
3811 if (parent_wco->cascaded)
3812 {
3813 has_wco = true;
3814 cascaded = true;
3815 }
3816 }
3817
3818 /*
3819 * Add the new WithCheckOption to the start of the list, so that
3820 * checks on inner views are run before checks on outer views, as
3821 * required by the SQL standard.
3822 *
3823 * If the new check is CASCADED, we need to add it even if this view
3824 * has no quals, since there may be quals on child views. A LOCAL
3825 * check can be omitted if this view has no quals.
3826 */
3827 if (has_wco && (cascaded || viewquery->jointree->quals != NULL))
3828 {
3829 WithCheckOption *wco;
3830
3832 wco->kind = WCO_VIEW_CHECK;
3834 wco->polname = NULL;
3835 wco->qual = NULL;
3836 wco->cascaded = cascaded;
3837
3838 parsetree->withCheckOptions = lcons(wco,
3839 parsetree->withCheckOptions);
3840
3841 if (viewquery->jointree->quals != NULL)
3842 {
3843 wco->qual = (Node *) viewquery->jointree->quals;
3844 ChangeVarNodes(wco->qual, base_rt_index, new_rt_index, 0);
3845
3846 /*
3847 * For INSERT, make sure that the query is marked correctly if
3848 * the added qual has sublinks. This can be skipped for
3849 * UPDATE/MERGE, since the same qual will have already been
3850 * added above, and the check will already have been done.
3851 */
3852 if (!parsetree->hasSubLinks &&
3853 parsetree->commandType == CMD_INSERT)
3854 parsetree->hasSubLinks = checkExprHasSubLink(wco->qual);
3855 }
3856 }
3857 }
3858
3859 table_close(base_rel, NoLock);
3860
3861 return parsetree;
3862}
3863
3864
3865/*
3866 * RewriteQuery -
3867 * rewrites the query and apply the rules again on the queries rewritten
3868 *
3869 * rewrite_events is a list of open query-rewrite actions, so we can detect
3870 * infinite recursion.
3871 *
3872 * orig_rt_length is the length of the originating query's rtable, for product
3873 * queries created by fireRules(), and 0 otherwise. This is used to skip any
3874 * already-processed VALUES RTEs from the original query.
3875 */
3876static List *
3877RewriteQuery(Query *parsetree, List *rewrite_events, int orig_rt_length)
3878{
3879 CmdType event = parsetree->commandType;
3880 bool instead = false;
3881 bool returning = false;
3882 bool updatableview = false;
3883 Query *qual_product = NULL;
3884 List *rewritten = NIL;
3885 ListCell *lc1;
3886
3887 /*
3888 * First, recursively process any insert/update/delete/merge statements in
3889 * WITH clauses. (We have to do this first because the WITH clauses may
3890 * get copied into rule actions below.)
3891 */
3892 foreach(lc1, parsetree->cteList)
3893 {
3895 Query *ctequery = castNode(Query, cte->ctequery);
3896 List *newstuff;
3897
3898 if (ctequery->commandType == CMD_SELECT)
3899 continue;
3900
3901 newstuff = RewriteQuery(ctequery, rewrite_events, 0);
3902
3903 /*
3904 * Currently we can only handle unconditional, single-statement DO
3905 * INSTEAD rules correctly; we have to get exactly one non-utility
3906 * Query out of the rewrite operation to stuff back into the CTE node.
3907 */
3908 if (list_length(newstuff) == 1)
3909 {
3910 /* Must check it's not a utility command */
3911 ctequery = linitial_node(Query, newstuff);
3912 if (!(ctequery->commandType == CMD_SELECT ||
3913 ctequery->commandType == CMD_UPDATE ||
3914 ctequery->commandType == CMD_INSERT ||
3915 ctequery->commandType == CMD_DELETE ||
3916 ctequery->commandType == CMD_MERGE))
3917 {
3918 /*
3919 * Currently it could only be NOTIFY; this error message will
3920 * need work if we ever allow other utility commands in rules.
3921 */
3922 ereport(ERROR,
3923 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3924 errmsg("DO INSTEAD NOTIFY rules are not supported for data-modifying statements in WITH")));
3925 }
3926 /* WITH queries should never be canSetTag */
3927 Assert(!ctequery->canSetTag);
3928 /* Push the single Query back into the CTE node */
3929 cte->ctequery = (Node *) ctequery;
3930 }
3931 else if (newstuff == NIL)
3932 {
3933 ereport(ERROR,
3934 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3935 errmsg("DO INSTEAD NOTHING rules are not supported for data-modifying statements in WITH")));
3936 }
3937 else
3938 {
3939 ListCell *lc2;
3940
3941 /* examine queries to determine which error message to issue */
3942 foreach(lc2, newstuff)
3943 {
3944 Query *q = (Query *) lfirst(lc2);
3945
3946 if (q->querySource == QSRC_QUAL_INSTEAD_RULE)
3947 ereport(ERROR,
3948 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3949 errmsg("conditional DO INSTEAD rules are not supported for data-modifying statements in WITH")));
3950 if (q->querySource == QSRC_NON_INSTEAD_RULE)
3951 ereport(ERROR,
3952 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3953 errmsg("DO ALSO rules are not supported for data-modifying statements in WITH")));
3954 }
3955
3956 ereport(ERROR,
3957 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3958 errmsg("multi-statement DO INSTEAD rules are not supported for data-modifying statements in WITH")));
3959 }
3960 }
3961
3962 /*
3963 * If the statement is an insert, update, delete, or merge, adjust its
3964 * targetlist as needed, and then fire INSERT/UPDATE/DELETE rules on it.
3965 *
3966 * SELECT rules are handled later when we have all the queries that should
3967 * get executed. Also, utilities aren't rewritten at all (do we still
3968 * need that check?)
3969 */
3970 if (event != CMD_SELECT && event != CMD_UTILITY)
3971 {
3972 int result_relation;
3973 RangeTblEntry *rt_entry;
3974 Relation rt_entry_relation;
3975 List *locks;
3976 int product_orig_rt_length;
3977 List *product_queries;
3978 bool hasUpdate = false;
3979 int values_rte_index = 0;
3980 bool defaults_remaining = false;
3981
3982 result_relation = parsetree->resultRelation;
3983 Assert(result_relation != 0);
3984 rt_entry = rt_fetch(result_relation, parsetree->rtable);
3985 Assert(rt_entry->rtekind == RTE_RELATION);
3986
3987 /*
3988 * We can use NoLock here since either the parser or
3989 * AcquireRewriteLocks should have locked the rel already.
3990 */
3991 rt_entry_relation = table_open(rt_entry->relid, NoLock);
3992
3993 /*
3994 * Rewrite the targetlist as needed for the command type.
3995 */
3996 if (event == CMD_INSERT)
3997 {
3998 ListCell *lc2;
3999 RangeTblEntry *values_rte = NULL;
4000
4001 /*
4002 * Test if it's a multi-row INSERT ... VALUES (...), (...), ... by
4003 * looking for a VALUES RTE in the fromlist. For product queries,
4004 * we must ignore any already-processed VALUES RTEs from the
4005 * original query. These appear at the start of the rangetable.
4006 */
4007 foreach(lc2, parsetree->jointree->fromlist)
4008 {
4009 RangeTblRef *rtr = (RangeTblRef *) lfirst(lc2);
4010
4011 if (IsA(rtr, RangeTblRef) && rtr->rtindex > orig_rt_length)
4012 {
4013 RangeTblEntry *rte = rt_fetch(rtr->rtindex,
4014 parsetree->rtable);
4015
4016 if (rte->rtekind == RTE_VALUES)
4017 {
4018 /* should not find more than one VALUES RTE */
4019 if (values_rte != NULL)
4020 elog(ERROR, "more than one VALUES RTE found");
4021
4022 values_rte = rte;
4023 values_rte_index = rtr->rtindex;
4024 }
4025 }
4026 }
4027
4028 if (values_rte)
4029 {
4030 Bitmapset *unused_values_attrnos = NULL;
4031
4032 /* Process the main targetlist ... */
4033 parsetree->targetList = rewriteTargetListIU(parsetree->targetList,
4034 parsetree->commandType,
4035 parsetree->override,
4036 rt_entry_relation,
4037 values_rte,
4038 values_rte_index,
4039 &unused_values_attrnos);
4040 /* ... and the VALUES expression lists */
4041 if (!rewriteValuesRTE(parsetree, values_rte, values_rte_index,
4042 rt_entry_relation,
4043 unused_values_attrnos))
4044 defaults_remaining = true;
4045 }
4046 else
4047 {
4048 /* Process just the main targetlist */
4049 parsetree->targetList =
4051 parsetree->commandType,
4052 parsetree->override,
4053 rt_entry_relation,
4054 NULL, 0, NULL);
4055 }
4056
4057 if (parsetree->onConflict &&
4058 parsetree->onConflict->action == ONCONFLICT_UPDATE)
4059 {
4060 parsetree->onConflict->onConflictSet =
4062 CMD_UPDATE,
4063 parsetree->override,
4064 rt_entry_relation,
4065 NULL, 0, NULL);
4066 }
4067 }
4068 else if (event == CMD_UPDATE)
4069 {
4070 Assert(parsetree->override == OVERRIDING_NOT_SET);
4071 parsetree->targetList =
4073 parsetree->commandType,
4074 parsetree->override,
4075 rt_entry_relation,
4076 NULL, 0, NULL);
4077 }
4078 else if (event == CMD_MERGE)
4079 {
4080 Assert(parsetree->override == OVERRIDING_NOT_SET);
4081
4082 /*
4083 * Rewrite each action targetlist separately
4084 */
4085 foreach(lc1, parsetree->mergeActionList)
4086 {
4088
4089 switch (action->commandType)
4090 {
4091 case CMD_NOTHING:
4092 case CMD_DELETE: /* Nothing to do here */
4093 break;
4094 case CMD_UPDATE:
4095 case CMD_INSERT:
4096
4097 /*
4098 * MERGE actions do not permit multi-row INSERTs, so
4099 * there is no VALUES RTE to deal with here.
4100 */
4101 action->targetList =
4102 rewriteTargetListIU(action->targetList,
4103 action->commandType,
4104 action->override,
4105 rt_entry_relation,
4106 NULL, 0, NULL);
4107 break;
4108 default:
4109 elog(ERROR, "unrecognized commandType: %d", action->commandType);
4110 break;
4111 }
4112 }
4113 }
4114 else if (event == CMD_DELETE)
4115 {
4116 /* Nothing to do here */
4117 }
4118 else
4119 elog(ERROR, "unrecognized commandType: %d", (int) event);
4120
4121 /*
4122 * Collect and apply the appropriate rules.
4123 */
4124 locks = matchLocks(event, rt_entry_relation,
4125 result_relation, parsetree, &hasUpdate);
4126
4127 product_orig_rt_length = list_length(parsetree->rtable);
4128 product_queries = fireRules(parsetree,
4129 result_relation,
4130 event,
4131 locks,
4132 &instead,
4133 &returning,
4134 &qual_product);
4135
4136 /*
4137 * If we have a VALUES RTE with any remaining untouched DEFAULT items,
4138 * and we got any product queries, finalize the VALUES RTE for each
4139 * product query (replacing the remaining DEFAULT items with NULLs).
4140 * We don't do this for the original query, because we know that it
4141 * must be an auto-insert on a view, and so should use the base
4142 * relation's defaults for any remaining DEFAULT items.
4143 */
4144 if (defaults_remaining && product_queries != NIL)
4145 {
4146 ListCell *n;
4147
4148 /*
4149 * Each product query has its own copy of the VALUES RTE at the
4150 * same index in the rangetable, so we must finalize each one.
4151 *
4152 * Note that if the product query is an INSERT ... SELECT, then
4153 * the VALUES RTE will be at the same index in the SELECT part of
4154 * the product query rather than the top-level product query
4155 * itself.
4156 */
4157 foreach(n, product_queries)
4158 {
4159 Query *pt = (Query *) lfirst(n);
4160 RangeTblEntry *values_rte;
4161
4162 if (pt->commandType == CMD_INSERT &&
4163 pt->jointree && IsA(pt->jointree, FromExpr) &&
4164 list_length(pt->jointree->fromlist) == 1)
4165 {
4166 Node *jtnode = (Node *) linitial(pt->jointree->fromlist);
4167
4168 if (IsA(jtnode, RangeTblRef))
4169 {
4170 int rtindex = ((RangeTblRef *) jtnode)->rtindex;
4171 RangeTblEntry *src_rte = rt_fetch(rtindex, pt->rtable);
4172
4173 if (src_rte->rtekind == RTE_SUBQUERY &&
4174 src_rte->subquery &&
4175 IsA(src_rte->subquery, Query) &&
4176 src_rte->subquery->commandType == CMD_SELECT)
4177 pt = src_rte->subquery;
4178 }
4179 }
4180
4181 values_rte = rt_fetch(values_rte_index, pt->rtable);
4182 if (values_rte->rtekind != RTE_VALUES)
4183 elog(ERROR, "failed to find VALUES RTE in product query");
4184
4185 rewriteValuesRTEToNulls(pt, values_rte);
4186 }
4187 }
4188
4189 /*
4190 * If there was no unqualified INSTEAD rule, and the target relation
4191 * is a view without any INSTEAD OF triggers, see if the view can be
4192 * automatically updated. If so, we perform the necessary query
4193 * transformation here and add the resulting query to the
4194 * product_queries list, so that it gets recursively rewritten if
4195 * necessary. For MERGE, the view must be automatically updatable if
4196 * any of the merge actions lack a corresponding INSTEAD OF trigger.
4197 *
4198 * If the view cannot be automatically updated, we throw an error here
4199 * which is OK since the query would fail at runtime anyway. Throwing
4200 * the error here is preferable to the executor check since we have
4201 * more detailed information available about why the view isn't
4202 * updatable.
4203 */
4204 if (!instead &&
4205 rt_entry_relation->rd_rel->relkind == RELKIND_VIEW &&
4206 !view_has_instead_trigger(rt_entry_relation, event,
4207 parsetree->mergeActionList))
4208 {
4209 /*
4210 * If there were any qualified INSTEAD rules, don't allow the view
4211 * to be automatically updated (an unqualified INSTEAD rule or
4212 * INSTEAD OF trigger is required).
4213 */
4214 if (qual_product != NULL)
4215 error_view_not_updatable(rt_entry_relation,
4216 parsetree->commandType,
4217 parsetree->mergeActionList,
4218 gettext_noop("Views with conditional DO INSTEAD rules are not automatically updatable."));
4219
4220 /*
4221 * Attempt to rewrite the query to automatically update the view.
4222 * This throws an error if the view can't be automatically
4223 * updated.
4224 */
4225 parsetree = rewriteTargetView(parsetree, rt_entry_relation);
4226
4227 /*
4228 * At this point product_queries contains any DO ALSO rule
4229 * actions. Add the rewritten query before or after those. This
4230 * must match the handling the original query would have gotten
4231 * below, if we allowed it to be included again.
4232 */
4233 if (parsetree->commandType == CMD_INSERT)
4234 product_queries = lcons(parsetree, product_queries);
4235 else
4236 product_queries = lappend(product_queries, parsetree);
4237
4238 /*
4239 * Set the "instead" flag, as if there had been an unqualified
4240 * INSTEAD, to prevent the original query from being included a
4241 * second time below. The transformation will have rewritten any
4242 * RETURNING list, so we can also set "returning" to forestall
4243 * throwing an error below.
4244 */
4245 instead = true;
4246 returning = true;
4247 updatableview = true;
4248 }
4249
4250 /*
4251 * If we got any product queries, recursively rewrite them --- but
4252 * first check for recursion!
4253 */
4254 if (product_queries != NIL)
4255 {
4256 ListCell *n;
4257 rewrite_event *rev;
4258
4259 foreach(n, rewrite_events)
4260 {
4261 rev = (rewrite_event *) lfirst(n);
4262 if (rev->relation == RelationGetRelid(rt_entry_relation) &&
4263 rev->event == event)
4264 ereport(ERROR,
4265 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
4266 errmsg("infinite recursion detected in rules for relation \"%s\"",
4267 RelationGetRelationName(rt_entry_relation))));
4268 }
4269
4270 rev = (rewrite_event *) palloc(sizeof(rewrite_event));
4271 rev->relation = RelationGetRelid(rt_entry_relation);
4272 rev->event = event;
4273 rewrite_events = lappend(rewrite_events, rev);
4274
4275 foreach(n, product_queries)
4276 {
4277 Query *pt = (Query *) lfirst(n);
4278 List *newstuff;
4279
4280 /*
4281 * For an updatable view, pt might be the rewritten version of
4282 * the original query, in which case we pass on orig_rt_length
4283 * to finish processing any VALUES RTE it contained.
4284 *
4285 * Otherwise, we have a product query created by fireRules().
4286 * Any VALUES RTEs from the original query have been fully
4287 * processed, and must be skipped when we recurse.
4288 */
4289 newstuff = RewriteQuery(pt, rewrite_events,
4290 pt == parsetree ?
4291 orig_rt_length :
4292 product_orig_rt_length);
4293 rewritten = list_concat(rewritten, newstuff);
4294 }
4295
4296 rewrite_events = list_delete_last(rewrite_events);
4297 }
4298
4299 /*
4300 * If there is an INSTEAD, and the original query has a RETURNING, we
4301 * have to have found a RETURNING in the rule(s), else fail. (Because
4302 * DefineQueryRewrite only allows RETURNING in unconditional INSTEAD
4303 * rules, there's no need to worry whether the substituted RETURNING
4304 * will actually be executed --- it must be.)
4305 */
4306 if ((instead || qual_product != NULL) &&
4307 parsetree->returningList &&
4308 !returning)
4309 {
4310 switch (event)
4311 {
4312 case CMD_INSERT:
4313 ereport(ERROR,
4314 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4315 errmsg("cannot perform INSERT RETURNING on relation \"%s\"",
4316 RelationGetRelationName(rt_entry_relation)),
4317 errhint("You need an unconditional ON INSERT DO INSTEAD rule with a RETURNING clause.")));
4318 break;
4319 case CMD_UPDATE:
4320 ereport(ERROR,
4321 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4322 errmsg("cannot perform UPDATE RETURNING on relation \"%s\"",
4323 RelationGetRelationName(rt_entry_relation)),
4324 errhint("You need an unconditional ON UPDATE DO INSTEAD rule with a RETURNING clause.")));
4325 break;
4326 case CMD_DELETE:
4327 ereport(ERROR,
4328 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4329 errmsg("cannot perform DELETE RETURNING on relation \"%s\"",
4330 RelationGetRelationName(rt_entry_relation)),
4331 errhint("You need an unconditional ON DELETE DO INSTEAD rule with a RETURNING clause.")));
4332 break;
4333 default:
4334 elog(ERROR, "unrecognized commandType: %d",
4335 (int) event);
4336 break;
4337 }
4338 }
4339
4340 /*
4341 * Updatable views are supported by ON CONFLICT, so don't prevent that
4342 * case from proceeding
4343 */
4344 if (parsetree->onConflict &&
4345 (product_queries != NIL || hasUpdate) &&
4346 !updatableview)
4347 ereport(ERROR,
4348 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4349 errmsg("INSERT with ON CONFLICT clause cannot be used with table that has INSERT or UPDATE rules")));
4350
4351 table_close(rt_entry_relation, NoLock);
4352 }
4353
4354 /*
4355 * For INSERTs, the original query is done first; for UPDATE/DELETE, it is
4356 * done last. This is needed because update and delete rule actions might
4357 * not do anything if they are invoked after the update or delete is
4358 * performed. The command counter increment between the query executions
4359 * makes the deleted (and maybe the updated) tuples disappear so the scans
4360 * for them in the rule actions cannot find them.
4361 *
4362 * If we found any unqualified INSTEAD, the original query is not done at
4363 * all, in any form. Otherwise, we add the modified form if qualified
4364 * INSTEADs were found, else the unmodified form.
4365 */
4366 if (!instead)
4367 {
4368 if (parsetree->commandType == CMD_INSERT)
4369 {
4370 if (qual_product != NULL)
4371 rewritten = lcons(qual_product, rewritten);
4372 else
4373 rewritten = lcons(parsetree, rewritten);
4374 }
4375 else
4376 {
4377 if (qual_product != NULL)
4378 rewritten = lappend(rewritten, qual_product);
4379 else
4380 rewritten = lappend(rewritten, parsetree);
4381 }
4382 }
4383
4384 /*
4385 * If the original query has a CTE list, and we generated more than one
4386 * non-utility result query, we have to fail because we'll have copied the
4387 * CTE list into each result query. That would break the expectation of
4388 * single evaluation of CTEs. This could possibly be fixed by
4389 * restructuring so that a CTE list can be shared across multiple Query
4390 * and PlannableStatement nodes.
4391 */
4392 if (parsetree->cteList != NIL)
4393 {
4394 int qcount = 0;
4395
4396 foreach(lc1, rewritten)
4397 {
4398 Query *q = (Query *) lfirst(lc1);
4399
4400 if (q->commandType != CMD_UTILITY)
4401 qcount++;
4402 }
4403 if (qcount > 1)
4404 ereport(ERROR,
4405 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4406 errmsg("WITH cannot be used in a query that is rewritten by rules into multiple queries")));
4407 }
4408
4409 return rewritten;
4410}
4411
4412
4413/*
4414 * Expand virtual generated columns
4415 *
4416 * If the table contains virtual generated columns, build a target list
4417 * containing the expanded expressions and use ReplaceVarsFromTargetList() to
4418 * do the replacements.
4419 *
4420 * Vars matching rt_index at the current query level are replaced by the
4421 * virtual generated column expressions from rel, if there are any.
4422 *
4423 * The caller must also provide rte, the RTE describing the target relation,
4424 * in order to handle any whole-row Vars referencing the target, and
4425 * result_relation, the index of the result relation, if this is part of an
4426 * INSERT/UPDATE/DELETE/MERGE query.
4427 */
4428static Node *
4430 RangeTblEntry *rte, int result_relation)
4431{
4432 TupleDesc tupdesc;
4433
4434 tupdesc = RelationGetDescr(rel);
4435 if (tupdesc->constr && tupdesc->constr->has_generated_virtual)
4436 {
4437 List *tlist = NIL;
4438
4439 for (int i = 0; i < tupdesc->natts; i++)
4440 {
4441 Form_pg_attribute attr = TupleDescAttr(tupdesc, i);
4442
4443 if (attr->attgenerated == ATTRIBUTE_GENERATED_VIRTUAL)
4444 {
4445 Node *defexpr;
4446 TargetEntry *te;
4447
4448 defexpr = build_generation_expression(rel, i + 1);
4449 ChangeVarNodes(defexpr, 1, rt_index, 0);
4450
4451 te = makeTargetEntry((Expr *) defexpr, i + 1, 0, false);
4452 tlist = lappend(tlist, te);
4453 }
4454 }
4455
4456 Assert(list_length(tlist) > 0);
4457
4458 node = ReplaceVarsFromTargetList(node, rt_index, 0, rte, tlist,
4459 result_relation,
4460 REPLACEVARS_CHANGE_VARNO, rt_index,
4461 NULL);
4462 }
4463
4464 return node;
4465}
4466
4467/*
4468 * Expand virtual generated columns in an expression
4469 *
4470 * This is for expressions that are not part of a query, such as default
4471 * expressions or index predicates. The rt_index is usually 1.
4472 */
4473Node *
4475{
4476 TupleDesc tupdesc = RelationGetDescr(rel);
4477
4478 if (tupdesc->constr && tupdesc->constr->has_generated_virtual)
4479 {
4480 RangeTblEntry *rte;
4481
4482 rte = makeNode(RangeTblEntry);
4483 /* eref needs to be set, but the actual name doesn't matter */
4484 rte->eref = makeAlias(RelationGetRelationName(rel), NIL);
4485 rte->rtekind = RTE_RELATION;
4486 rte->relid = RelationGetRelid(rel);
4487
4488 node = expand_generated_columns_internal(node, rel, rt_index, rte, 0);
4489 }
4490
4491 return node;
4492}
4493
4494/*
4495 * Build the generation expression for the virtual generated column.
4496 *
4497 * Error out if there is no generation expression found for the given column.
4498 */
4499Node *
4501{
4502 TupleDesc rd_att = RelationGetDescr(rel);
4503 Form_pg_attribute att_tup = TupleDescAttr(rd_att, attrno - 1);
4504 Node *defexpr;
4505 Oid attcollid;
4506
4507 Assert(rd_att->constr && rd_att->constr->has_generated_virtual);
4508 Assert(att_tup->attgenerated == ATTRIBUTE_GENERATED_VIRTUAL);
4509
4510 defexpr = build_column_default(rel, attrno);
4511 if (defexpr == NULL)
4512 elog(ERROR, "no generation expression found for column number %d of table \"%s\"",
4513 attrno, RelationGetRelationName(rel));
4514
4515 /*
4516 * If the column definition has a collation and it is different from the
4517 * collation of the generation expression, put a COLLATE clause around the
4518 * expression.
4519 */
4520 attcollid = att_tup->attcollation;
4521 if (attcollid && attcollid != exprCollation(defexpr))
4522 {
4524
4525 ce->arg = (Expr *) defexpr;
4526 ce->collOid = attcollid;
4527 ce->location = -1;
4528
4529 defexpr = (Node *) ce;
4530 }
4531
4532 return defexpr;
4533}
4534
4535
4536/*
4537 * QueryRewrite -
4538 * Primary entry point to the query rewriter.
4539 * Rewrite one query via query rewrite system, possibly returning 0
4540 * or many queries.
4541 *
4542 * NOTE: the parsetree must either have come straight from the parser,
4543 * or have been scanned by AcquireRewriteLocks to acquire suitable locks.
4544 */
4545List *
4547{
4548 int64 input_query_id = parsetree->queryId;
4549 List *querylist;
4550 List *results;
4551 ListCell *l;
4552 CmdType origCmdType;
4553 bool foundOriginalQuery;
4554 Query *lastInstead;
4555
4556 /*
4557 * This function is only applied to top-level original queries
4558 */
4559 Assert(parsetree->querySource == QSRC_ORIGINAL);
4560 Assert(parsetree->canSetTag);
4561
4562 /*
4563 * Step 1
4564 *
4565 * Apply all non-SELECT rules possibly getting 0 or many queries
4566 */
4567 querylist = RewriteQuery(parsetree, NIL, 0);
4568
4569 /*
4570 * Step 2
4571 *
4572 * Apply all the RIR rules on each query
4573 *
4574 * This is also a handy place to mark each query with the original queryId
4575 */
4576 results = NIL;
4577 foreach(l, querylist)
4578 {
4579 Query *query = (Query *) lfirst(l);
4580
4581 query = fireRIRrules(query, NIL);
4582
4583 query->queryId = input_query_id;
4584
4585 results = lappend(results, query);
4586 }
4587
4588 /*
4589 * Step 3
4590 *
4591 * Determine which, if any, of the resulting queries is supposed to set
4592 * the command-result tag; and update the canSetTag fields accordingly.
4593 *
4594 * If the original query is still in the list, it sets the command tag.
4595 * Otherwise, the last INSTEAD query of the same kind as the original is
4596 * allowed to set the tag. (Note these rules can leave us with no query
4597 * setting the tag. The tcop code has to cope with this by setting up a
4598 * default tag based on the original un-rewritten query.)
4599 *
4600 * The Asserts verify that at most one query in the result list is marked
4601 * canSetTag. If we aren't checking asserts, we can fall out of the loop
4602 * as soon as we find the original query.
4603 */
4604 origCmdType = parsetree->commandType;
4605 foundOriginalQuery = false;
4606 lastInstead = NULL;
4607
4608 foreach(l, results)
4609 {
4610 Query *query = (Query *) lfirst(l);
4611
4612 if (query->querySource == QSRC_ORIGINAL)
4613 {
4614 Assert(query->canSetTag);
4615 Assert(!foundOriginalQuery);
4616 foundOriginalQuery = true;
4617#ifndef USE_ASSERT_CHECKING
4618 break;
4619#endif
4620 }
4621 else
4622 {
4623 Assert(!query->canSetTag);
4624 if (query->commandType == origCmdType &&
4625 (query->querySource == QSRC_INSTEAD_RULE ||
4626 query->querySource == QSRC_QUAL_INSTEAD_RULE))
4627 lastInstead = query;
4628 }
4629 }
4630
4631 if (!foundOriginalQuery && lastInstead != NULL)
4632 lastInstead->canSetTag = true;
4633
4634 return results;
4635}
int16 AttrNumber
Definition: attnum.h:21
#define InvalidAttrNumber
Definition: attnum.h:23
Bitmapset * bms_int_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:1109
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1306
Bitmapset * bms_del_member(Bitmapset *a, int x)
Definition: bitmapset.c:868
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:510
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:815
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:251
#define bms_is_empty(a)
Definition: bitmapset.h:118
#define NameStr(name)
Definition: c.h:752
#define gettext_noop(x)
Definition: c.h:1196
int64_t int64
Definition: c.h:536
int32_t int32
Definition: c.h:535
#define unlikely(x)
Definition: c.h:403
unsigned int Index
Definition: c.h:620
int errdetail_internal(const char *fmt,...)
Definition: elog.c:1234
int errdetail(const char *fmt,...)
Definition: elog.c:1207
int errhint(const char *fmt,...)
Definition: elog.c:1321
int errcode(int sqlerrcode)
Definition: elog.c:854
int errmsg(const char *fmt,...)
Definition: elog.c:1071
#define _(x)
Definition: elog.c:91
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:226
#define ereport(elevel,...)
Definition: elog.h:150
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:223
int ExecCleanTargetListLength(List *targetlist)
Definition: execUtils.c:1185
FdwRoutine * GetFdwRoutineForRelation(Relation relation, bool makecopy)
Definition: foreign.c:443
char * format_type_be(Oid type_oid)
Definition: format_type.c:343
Assert(PointerIsAligned(start, uint64))
int j
Definition: isn.c:78
int i
Definition: isn.c:77
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:81
List * lappend(List *list, void *datum)
Definition: list.c:339
List * list_concat(List *list1, const List *list2)
Definition: list.c:561
List * list_concat_copy(const List *list1, const List *list2)
Definition: list.c:598
List * lappend_oid(List *list, Oid datum)
Definition: list.c:375
List * lcons(void *datum, List *list)
Definition: list.c:495
List * list_delete_last(List *list)
Definition: list.c:957
bool list_member_oid(const List *list, Oid datum)
Definition: list.c:722
int LOCKMODE
Definition: lockdefs.h:26
#define NoLock
Definition: lockdefs.h:34
#define AccessShareLock
Definition: lockdefs.h:36
#define RowShareLock
Definition: lockdefs.h:37
#define RowExclusiveLock
Definition: lockdefs.h:38
LockWaitPolicy
Definition: lockoptions.h:37
LockClauseStrength
Definition: lockoptions.h:22
Node * get_typdefault(Oid typid)
Definition: lsyscache.c:2615
Alias * makeAlias(const char *aliasname, List *colnames)
Definition: makefuncs.c:438
Const * makeNullConst(Oid consttype, int32 consttypmod, Oid constcollid)
Definition: makefuncs.c:388
Var * makeWholeRowVar(RangeTblEntry *rte, int varno, Index varlevelsup, bool allowScalar)
Definition: makefuncs.c:137
TargetEntry * makeTargetEntry(Expr *expr, AttrNumber resno, char *resname, bool resjunk)
Definition: makefuncs.c:289
TargetEntry * flatCopyTargetEntry(TargetEntry *src_tle)
Definition: makefuncs.c:322
char * pstrdup(const char *in)
Definition: mcxt.c:1759
void pfree(void *pointer)
Definition: mcxt.c:1594
void * palloc0(Size size)
Definition: mcxt.c:1395
void * palloc(Size size)
Definition: mcxt.c:1365
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:821
Node * strip_implicit_coercions(Node *node)
Definition: nodeFuncs.c:705
#define query_tree_walker(q, w, c, f)
Definition: nodeFuncs.h:158
#define expression_tree_walker(n, w, c)
Definition: nodeFuncs.h:153
#define QTW_IGNORE_RC_SUBQUERIES
Definition: nodeFuncs.h:24
#define IsA(nodeptr, _type_)
Definition: nodes.h:164
#define copyObject(obj)
Definition: nodes.h:232
#define nodeTag(nodeptr)
Definition: nodes.h:139
@ ONCONFLICT_UPDATE
Definition: nodes.h:430
CmdType
Definition: nodes.h:273
@ CMD_MERGE
Definition: nodes.h:279
@ CMD_UTILITY
Definition: nodes.h:280
@ CMD_INSERT
Definition: nodes.h:277
@ CMD_DELETE
Definition: nodes.h:278
@ CMD_UPDATE
Definition: nodes.h:276
@ CMD_SELECT
Definition: nodes.h:275
@ CMD_NOTHING
Definition: nodes.h:282
#define makeNode(_type_)
Definition: nodes.h:161
#define castNode(_type_, nodeptr)
Definition: nodes.h:182
Node * coerce_null_to_domain(Oid typid, int32 typmod, Oid collation, int typlen, bool typbyval)
Node * coerce_to_target_type(ParseState *pstate, Node *expr, Oid exprtype, Oid targettype, int32 targettypmod, CoercionContext ccontext, CoercionForm cformat, int location)
Definition: parse_coerce.c:78
ParseState * make_parsestate(ParseState *parentParseState)
Definition: parse_node.c:39
ParseNamespaceItem * addRangeTableEntryForRelation(ParseState *pstate, Relation rel, int lockmode, Alias *alias, bool inh, bool inFromCl)
RTEPermissionInfo * getRTEPermissionInfo(List *rteperminfos, RangeTblEntry *rte)
RowMarkClause * get_parse_rowmark(Query *qry, Index rtindex)
TargetEntry * get_tle_by_resno(List *tlist, AttrNumber resno)
bool get_rte_attribute_is_dropped(RangeTblEntry *rte, AttrNumber attnum)
RTEPermissionInfo * addRTEPermissionInfo(List **rteperminfos, RangeTblEntry *rte)
@ WCO_VIEW_CHECK
Definition: parsenodes.h:1388
QuerySource
Definition: parsenodes.h:35
@ QSRC_NON_INSTEAD_RULE
Definition: parsenodes.h:40
@ QSRC_QUAL_INSTEAD_RULE
Definition: parsenodes.h:39
@ QSRC_ORIGINAL
Definition: parsenodes.h:36
@ QSRC_INSTEAD_RULE
Definition: parsenodes.h:38
@ RTE_JOIN
Definition: parsenodes.h:1043
@ RTE_VALUES
Definition: parsenodes.h:1046
@ RTE_SUBQUERY
Definition: parsenodes.h:1042
@ RTE_FUNCTION
Definition: parsenodes.h:1044
@ RTE_TABLEFUNC
Definition: parsenodes.h:1045
@ RTE_RELATION
Definition: parsenodes.h:1041
#define ACL_SELECT_FOR_UPDATE
Definition: parsenodes.h:94
void applyLockingClause(Query *qry, Index rtindex, LockClauseStrength strength, LockWaitPolicy waitPolicy, bool pushedDown)
Definition: analyze.c:3634
List * BuildOnConflictExcludedTargetlist(Relation targetrel, Index exclRelIndex)
Definition: analyze.c:1280
#define rt_fetch(rangetable_index, rangetable)
Definition: parsetree.h:31
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:202
void * arg
Oid getIdentitySequence(Relation rel, AttrNumber attnum, bool missing_ok)
Definition: pg_depend.c:945
#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 linitial_node(type, l)
Definition: pg_list.h:181
#define NIL
Definition: pg_list.h:68
#define foreach_delete_current(lst, var_or_cell)
Definition: pg_list.h:391
#define linitial(l)
Definition: pg_list.h:178
#define foreach_node(type, var, lst)
Definition: pg_list.h:496
int restrict_nonsystem_relation_kind
Definition: postgres.c:105
#define InvalidOid
Definition: postgres_ext.h:37
unsigned int Oid
Definition: postgres_ext.h:32
#define PRS2_OLD_VARNO
Definition: primnodes.h:250
#define PRS2_NEW_VARNO
Definition: primnodes.h:251
@ COERCE_IMPLICIT_CAST
Definition: primnodes.h:755
OverridingKind
Definition: primnodes.h:27
@ OVERRIDING_NOT_SET
Definition: primnodes.h:28
@ OVERRIDING_SYSTEM_VALUE
Definition: primnodes.h:30
@ OVERRIDING_USER_VALUE
Definition: primnodes.h:29
@ COERCION_ASSIGNMENT
Definition: primnodes.h:734
#define RelationGetRelid(relation)
Definition: rel.h:514
#define RelationHasCheckOption(relation)
Definition: rel.h:457
#define RelationHasSecurityInvoker(relation)
Definition: rel.h:447
#define RelationGetDescr(relation)
Definition: rel.h:540
#define RelationGetNumberOfAttributes(relation)
Definition: rel.h:520
#define RelationGetRelationName(relation)
Definition: rel.h:548
#define RelationHasCascadedCheckOption(relation)
Definition: rel.h:479
#define RelationIsSecurityView(relation)
Definition: rel.h:437
#define RULE_FIRES_ON_ORIGIN
Definition: rewriteDefine.h:21
#define RULE_FIRES_ON_REPLICA
Definition: rewriteDefine.h:23
#define RULE_DISABLED
Definition: rewriteDefine.h:24
static void markQueryForLocking(Query *qry, Node *jtnode, LockClauseStrength strength, LockWaitPolicy waitPolicy, bool pushedDown)
static Query * ApplyRetrieveRule(Query *parsetree, RewriteRule *rule, int rt_index, Relation relation, List *activeRIRs)
static Query * rewriteRuleAction(Query *parsetree, Query *rule_action, Node *rule_qual, int rt_index, CmdType event, bool *returning_flag)
static TargetEntry * process_matched_tle(TargetEntry *src_tle, TargetEntry *prior_tle, const char *attrName)
static bool fireRIRonSubLink(Node *node, fireRIRonSubLink_context *context)
static List * adjustJoinTreeList(Query *parsetree, bool removert, int rt_index)
void AcquireRewriteLocks(Query *parsetree, bool forExecute, bool forUpdatePushedDown)
bool view_has_instead_trigger(Relation view, CmdType event, List *mergeActionList)
static const char * view_cols_are_auto_updatable(Query *viewquery, Bitmapset *required_cols, Bitmapset **updatable_cols, char **non_updatable_col)
struct fireRIRonSubLink_context fireRIRonSubLink_context
static List * fireRules(Query *parsetree, int rt_index, CmdType event, List *locks, bool *instead_flag, bool *returning_flag, Query **qual_product)
static Query * CopyAndAddInvertedQual(Query *parsetree, Node *rule_qual, int rt_index, CmdType event)
int relation_is_updatable(Oid reloid, List *outer_reloids, bool include_triggers, Bitmapset *include_cols)
Query * get_view_query(Relation view)
static bool acquireLocksOnSubLinks(Node *node, acquireLocksOnSubLinks_context *context)
static bool rewriteValuesRTE(Query *parsetree, RangeTblEntry *rte, int rti, Relation target_relation, Bitmapset *unused_cols)
static Node * get_assignment_input(Node *node)
static List * RewriteQuery(Query *parsetree, List *rewrite_events, int orig_rt_length)
static Bitmapset * adjust_view_column_set(Bitmapset *cols, List *targetlist)
const char * view_query_is_auto_updatable(Query *viewquery, bool check_cols)
struct acquireLocksOnSubLinks_context acquireLocksOnSubLinks_context
static const char * view_col_is_auto_updatable(RangeTblRef *rtr, TargetEntry *tle)
static bool searchForDefault(RangeTblEntry *rte)
struct rewrite_event rewrite_event
static Query * fireRIRrules(Query *parsetree, List *activeRIRs)
static Query * rewriteTargetView(Query *parsetree, Relation view)
List * QueryRewrite(Query *parsetree)
static Node * expand_generated_columns_internal(Node *node, Relation rel, int rt_index, RangeTblEntry *rte, int result_relation)
static Bitmapset * findDefaultOnlyColumns(RangeTblEntry *rte)
static void rewriteValuesRTEToNulls(Query *parsetree, RangeTblEntry *rte)
Node * build_generation_expression(Relation rel, int attrno)
static List * matchLocks(CmdType event, Relation relation, int varno, Query *parsetree, bool *hasUpdate)
void error_view_not_updatable(Relation view, CmdType command, List *mergeActionList, const char *detail)
Node * build_column_default(Relation rel, int attrno)
static List * rewriteTargetListIU(List *targetList, CmdType commandType, OverridingKind override, Relation target_relation, RangeTblEntry *values_rte, int values_rte_index, Bitmapset **unused_values_attrnos)
#define ALL_EVENTS
Node * expand_generated_columns_in_expr(Node *node, Relation rel, int rt_index)
void ChangeVarNodes(Node *node, int rt_index, int new_index, int sublevels_up)
Definition: rewriteManip.c:736
void OffsetVarNodes(Node *node, int offset, int sublevels_up)
Definition: rewriteManip.c:476
bool checkExprHasSubLink(Node *node)
Definition: rewriteManip.c:292
void CombineRangeTables(List **dst_rtable, List **dst_perminfos, List *src_rtable, List *src_perminfos)
Definition: rewriteManip.c:347
void AddQual(Query *parsetree, Node *qual)
bool rangeTableEntry_used(Node *node, int rt_index, int sublevels_up)
Query * getInsertSelectQuery(Query *parsetree, Query ***subquery_ptr)
void AddInvertedQual(Query *parsetree, Node *qual)
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)
@ REPLACEVARS_SUBSTITUTE_NULL
Definition: rewriteManip.h:41
@ REPLACEVARS_CHANGE_VARNO
Definition: rewriteManip.h:40
@ REPLACEVARS_REPORT_ERROR
Definition: rewriteManip.h:39
CommonTableExpr * rewriteSearchAndCycle(CommonTableExpr *cte)
void get_row_security_policies(Query *root, RangeTblEntry *rte, int rt_index, List **securityQuals, List **withCheckOptions, bool *hasRowSecurity, bool *hasSubLinks)
Definition: rowsecurity.c:98
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: relation.c:205
Relation try_relation_open(Oid relationId, LOCKMODE lockmode)
Definition: relation.c:88
void check_stack_depth(void)
Definition: stack_depth.c:95
Expr * arg
Definition: primnodes.h:1298
ParseLoc location
Definition: primnodes.h:1300
ExecForeignInsert_function ExecForeignInsert
Definition: fdwapi.h:232
ExecForeignUpdate_function ExecForeignUpdate
Definition: fdwapi.h:235
ExecForeignDelete_function ExecForeignDelete
Definition: fdwapi.h:236
IsForeignRelUpdatable_function IsForeignRelUpdatable
Definition: fdwapi.h:240
List * newvals
Definition: primnodes.h:1179
Expr * arg
Definition: primnodes.h:1178
Node * quals
Definition: primnodes.h:2344
List * fromlist
Definition: primnodes.h:2343
Definition: pg_list.h:54
Definition: nodes.h:135
OnConflictAction action
Definition: primnodes.h:2359
List * onConflictSet
Definition: primnodes.h:2368
List * exclRelTlist
Definition: primnodes.h:2371
RangeTblEntry * p_rte
Definition: parse_node.h:295
Node * limitCount
Definition: parsenodes.h:230
FromExpr * jointree
Definition: parsenodes.h:182
List * returningList
Definition: parsenodes.h:214
Node * setOperations
Definition: parsenodes.h:235
List * cteList
Definition: parsenodes.h:173
OnConflictExpr * onConflict
Definition: parsenodes.h:203
List * groupClause
Definition: parsenodes.h:216
Node * havingQual
Definition: parsenodes.h:221
List * rtable
Definition: parsenodes.h:175
Node * limitOffset
Definition: parsenodes.h:229
CmdType commandType
Definition: parsenodes.h:121
List * mergeActionList
Definition: parsenodes.h:185
List * targetList
Definition: parsenodes.h:198
List * groupingSets
Definition: parsenodes.h:219
List * distinctClause
Definition: parsenodes.h:225
Bitmapset * selectedCols
Definition: parsenodes.h:1322
AclMode requiredPerms
Definition: parsenodes.h:1320
Bitmapset * insertedCols
Definition: parsenodes.h:1323
Bitmapset * updatedCols
Definition: parsenodes.h:1324
TableFunc * tablefunc
Definition: parsenodes.h:1213
struct TableSampleClause * tablesample
Definition: parsenodes.h:1127
Query * subquery
Definition: parsenodes.h:1133
List * values_lists
Definition: parsenodes.h:1219
List * functions
Definition: parsenodes.h:1206
RTEKind rtekind
Definition: parsenodes.h:1076
TriggerDesc * trigdesc
Definition: rel.h:117
TupleDesc rd_att
Definition: rel.h:112
RuleLock * rd_rules
Definition: rel.h:115
Form_pg_class rd_rel
Definition: rel.h:111
CmdType event
Definition: prs2lock.h:27
List * actions
Definition: prs2lock.h:29
bool isInstead
Definition: prs2lock.h:31
Node * qual
Definition: prs2lock.h:28
char enabled
Definition: prs2lock.h:30
LockClauseStrength strength
Definition: parsenodes.h:1609
LockWaitPolicy waitPolicy
Definition: parsenodes.h:1610
RewriteRule ** rules
Definition: prs2lock.h:43
int numLocks
Definition: prs2lock.h:42
Expr * refassgnexpr
Definition: primnodes.h:722
Expr * refexpr
Definition: primnodes.h:720
Expr * expr
Definition: primnodes.h:2225
AttrNumber resno
Definition: primnodes.h:2227
bool trig_update_instead_row
Definition: reltrigger.h:63
bool trig_delete_instead_row
Definition: reltrigger.h:68
bool trig_insert_instead_row
Definition: reltrigger.h:58
bool has_generated_virtual
Definition: tupdesc.h:47
TupleConstr * constr
Definition: tupdesc.h:141
Definition: primnodes.h:262
AttrNumber varattno
Definition: primnodes.h:274
int varno
Definition: primnodes.h:269
Index varlevelsup
Definition: primnodes.h:294
Definition: localtime.c:73
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:27
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:126
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:40
#define RESTRICT_RELKIND_VIEW
Definition: tcopprot.h:43
#define FirstNormalObjectId
Definition: transam.h:197
int SessionReplicationRole
Definition: trigger.c:63
#define SESSION_REPLICATION_ROLE_REPLICA
Definition: trigger.h:141
Node * TupleDescGetDefault(TupleDesc tupdesc, AttrNumber attnum)
Definition: tupdesc.c:1092
static FormData_pg_attribute * TupleDescAttr(TupleDesc tupdesc, int i)
Definition: tupdesc.h:160
String * makeString(char *str)
Definition: value.c:63
bool contain_vars_of_level(Node *node, int levelsup)
Definition: var.c:444
static struct rule * rules
Definition: zic.c:283