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