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