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