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