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