1
/*-------------------------------------------------------------------------
4
* Primary module of query rewriter.
6
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
7
* Portions Copyright (c) 1994, Regents of the University of California
10
* $PostgreSQL: pgsql/src/backend/rewrite/rewriteHandler.c,v 1.147 2004-12-31 22:00:45 pgsql Exp $
12
*-------------------------------------------------------------------------
16
#include "access/heapam.h"
17
#include "catalog/pg_operator.h"
18
#include "catalog/pg_type.h"
19
#include "miscadmin.h"
20
#include "nodes/makefuncs.h"
21
#include "optimizer/clauses.h"
22
#include "optimizer/prep.h"
23
#include "optimizer/var.h"
24
#include "parser/analyze.h"
25
#include "parser/parse_coerce.h"
26
#include "parser/parse_expr.h"
27
#include "parser/parse_oper.h"
28
#include "parser/parse_type.h"
29
#include "parser/parsetree.h"
30
#include "rewrite/rewriteHandler.h"
31
#include "rewrite/rewriteManip.h"
32
#include "utils/builtins.h"
33
#include "utils/lsyscache.h"
36
/* We use a list of these to detect recursion in RewriteQuery */
37
typedef struct rewrite_event
39
Oid relation; /* OID of relation having rules */
40
CmdType event; /* type of rule being fired */
43
static Query *rewriteRuleAction(Query *parsetree,
48
static List *adjustJoinTreeList(Query *parsetree, bool removert, int rt_index);
49
static void rewriteTargetList(Query *parsetree, Relation target_relation);
50
static TargetEntry *process_matched_tle(TargetEntry *src_tle,
51
TargetEntry *prior_tle,
52
const char *attrName);
53
static Node *get_assignment_input(Node *node);
54
static void markQueryForUpdate(Query *qry, bool skipOldNew);
55
static List *matchLocks(CmdType event, RuleLock *rulelocks,
56
int varno, Query *parsetree);
57
static Query *fireRIRrules(Query *parsetree, List *activeRIRs);
62
* Rewrite the rule action with appropriate qualifiers (taken from
63
* the triggering query).
66
rewriteRuleAction(Query *parsetree,
76
Query **sub_action_ptr;
79
* Make modifiable copies of rule action and qual (what we're passed
80
* are the stored versions in the relcache; don't touch 'em!).
82
rule_action = (Query *) copyObject(rule_action);
83
rule_qual = (Node *) copyObject(rule_qual);
85
current_varno = rt_index;
86
rt_length = list_length(parsetree->rtable);
87
new_varno = PRS2_NEW_VARNO + rt_length;
90
* Adjust rule action and qual to offset its varnos, so that we can
91
* merge its rtable with the main parsetree's rtable.
93
* If the rule action is an INSERT...SELECT, the OLD/NEW rtable entries
94
* will be in the SELECT part, and we have to modify that rather than
95
* the top-level INSERT (kluge!).
97
sub_action = getInsertSelectQuery(rule_action, &sub_action_ptr);
99
OffsetVarNodes((Node *) sub_action, rt_length, 0);
100
OffsetVarNodes(rule_qual, rt_length, 0);
101
/* but references to *OLD* should point at original rt_index */
102
ChangeVarNodes((Node *) sub_action,
103
PRS2_OLD_VARNO + rt_length, rt_index, 0);
104
ChangeVarNodes(rule_qual,
105
PRS2_OLD_VARNO + rt_length, rt_index, 0);
108
* Generate expanded rtable consisting of main parsetree's rtable plus
109
* rule action's rtable; this becomes the complete rtable for the rule
110
* action. Some of the entries may be unused after we finish
111
* rewriting, but we leave them all in place for two reasons:
113
* We'd have a much harder job to adjust the query's varnos if we
114
* selectively removed RT entries.
116
* If the rule is INSTEAD, then the original query won't be executed at
117
* all, and so its rtable must be preserved so that the executor will
118
* do the correct permissions checks on it.
120
* RT entries that are not referenced in the completed jointree will be
121
* ignored by the planner, so they do not affect query semantics. But
122
* any permissions checks specified in them will be applied during
123
* executor startup (see ExecCheckRTEPerms()). This allows us to
124
* check that the caller has, say, insert-permission on a view, when
125
* the view is not semantically referenced at all in the resulting
128
* When a rule is not INSTEAD, the permissions checks done on its copied
129
* RT entries will be redundant with those done during execution of
130
* the original query, but we don't bother to treat that case
133
* NOTE: because planner will destructively alter rtable, we must ensure
134
* that rule action's rtable is separate and shares no substructure
135
* with the main rtable. Hence do a deep copy here.
137
sub_action->rtable = list_concat((List *) copyObject(parsetree->rtable),
141
* Each rule action's jointree should be the main parsetree's jointree
142
* plus that rule's jointree, but usually *without* the original
143
* rtindex that we're replacing (if present, which it won't be for
144
* INSERT). Note that if the rule action refers to OLD, its jointree
145
* will add a reference to rt_index. If the rule action doesn't refer
146
* to OLD, but either the rule_qual or the user query quals do, then
147
* we need to keep the original rtindex in the jointree to provide
148
* data for the quals. We don't want the original rtindex to be
149
* joined twice, however, so avoid keeping it if the rule action
152
* As above, the action's jointree must not share substructure with the
155
if (sub_action->commandType != CMD_UTILITY)
160
Assert(sub_action->jointree != NULL);
161
keeporig = (!rangeTableEntry_used((Node *) sub_action->jointree,
163
(rangeTableEntry_used(rule_qual, rt_index, 0) ||
164
rangeTableEntry_used(parsetree->jointree->quals, rt_index, 0));
165
newjointree = adjustJoinTreeList(parsetree, !keeporig, rt_index);
166
if (newjointree != NIL)
169
* If sub_action is a setop, manipulating its jointree will do
170
* no good at all, because the jointree is dummy. (Perhaps
171
* someday we could push the joining and quals down to the
172
* member statements of the setop?)
174
if (sub_action->setOperations != NULL)
176
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
177
errmsg("conditional UNION/INTERSECT/EXCEPT statements are not implemented")));
179
sub_action->jointree->fromlist =
180
list_concat(newjointree, sub_action->jointree->fromlist);
185
* We copy the qualifications of the parsetree to the action and vice
186
* versa. So force hasSubLinks if one of them has it. If this is not
187
* right, the flag will get cleared later, but we mustn't risk having
188
* it not set when it needs to be. (XXX this should probably be
189
* handled by AddQual and friends, not here...)
191
if (parsetree->hasSubLinks)
192
sub_action->hasSubLinks = TRUE;
193
else if (sub_action->hasSubLinks)
194
parsetree->hasSubLinks = TRUE;
197
* Event Qualification forces copying of parsetree and splitting into
198
* two queries one w/rule_qual, one w/NOT rule_qual. Also add user
199
* query qual onto rule action
201
AddQual(sub_action, rule_qual);
203
AddQual(sub_action, parsetree->jointree->quals);
206
* Rewrite new.attribute w/ right hand side of target-list entry for
207
* appropriate field name in insert/update.
209
* KLUGE ALERT: since ResolveNew returns a mutated copy, we can't just
210
* apply it to sub_action; we have to remember to update the sublink
211
* inside rule_action, too.
213
if ((event == CMD_INSERT || event == CMD_UPDATE) &&
214
sub_action->commandType != CMD_UTILITY)
216
sub_action = (Query *) ResolveNew((Node *) sub_action,
220
parsetree->targetList,
224
*sub_action_ptr = sub_action;
226
rule_action = sub_action;
233
* Copy the query's jointree list, and optionally attempt to remove any
234
* occurrence of the given rt_index as a top-level join item (we do not look
235
* for it within join items; this is OK because we are only expecting to find
236
* it as an UPDATE or DELETE target relation, which will be at the top level
237
* of the join). Returns modified jointree list --- this is a separate copy
238
* sharing no nodes with the original.
241
adjustJoinTreeList(Query *parsetree, bool removert, int rt_index)
243
List *newjointree = copyObject(parsetree->jointree->fromlist);
248
foreach(l, newjointree)
250
RangeTblRef *rtr = lfirst(l);
252
if (IsA(rtr, RangeTblRef) &&
253
rtr->rtindex == rt_index)
255
newjointree = list_delete_ptr(newjointree, rtr);
258
* foreach is safe because we exit loop after
270
* rewriteTargetList - rewrite INSERT/UPDATE targetlist into standard form
272
* This has the following responsibilities:
274
* 1. For an INSERT, add tlist entries to compute default values for any
275
* attributes that have defaults and are not assigned to in the given tlist.
276
* (We do not insert anything for default-less attributes, however. The
277
* planner will later insert NULLs for them, but there's no reason to slow
278
* down rewriter processing with extra tlist nodes.) Also, for both INSERT
279
* and UPDATE, replace explicit DEFAULT specifications with column default
282
* 2. Merge multiple entries for the same target attribute, or declare error
283
* if we can't. Multiple entries are only allowed for INSERT/UPDATE of
284
* portions of an array or record field, for example
285
* UPDATE table SET foo[2] = 42, foo[4] = 43;
286
* We can merge such operations into a single assignment op. Essentially,
287
* the expression we want to produce in this case is like
288
* foo = array_set(array_set(foo, 2, 42), 4, 43)
290
* 3. Sort the tlist into standard order: non-junk fields in order by resno,
291
* then junk fields (these in no particular order).
293
* We must do items 1 and 2 before firing rewrite rules, else rewritten
294
* references to NEW.foo will produce wrong or incomplete results. Item 3
295
* is not needed for rewriting, but will be needed by the planner, and we
296
* can do it essentially for free while handling items 1 and 2.
299
rewriteTargetList(Query *parsetree, Relation target_relation)
301
CmdType commandType = parsetree->commandType;
302
List *tlist = parsetree->targetList;
303
List *new_tlist = NIL;
309
* Scan the tuple description in the relation's relcache entry to make
310
* sure we have all the user attributes in the right order.
312
numattrs = RelationGetNumberOfAttributes(target_relation);
314
for (attrno = 1; attrno <= numattrs; attrno++)
316
Form_pg_attribute att_tup = target_relation->rd_att->attrs[attrno - 1];
317
TargetEntry *new_tle = NULL;
319
/* We can ignore deleted attributes */
320
if (att_tup->attisdropped)
324
* Look for targetlist entries matching this attr.
326
* Junk attributes are not candidates to be matched.
330
TargetEntry *old_tle = (TargetEntry *) lfirst(temp);
331
Resdom *resdom = old_tle->resdom;
333
if (!resdom->resjunk && resdom->resno == attrno)
335
new_tle = process_matched_tle(old_tle, new_tle,
336
NameStr(att_tup->attname));
337
/* keep scanning to detect multiple assignments to attr */
342
* Handle the two cases where we need to insert a default
343
* expression: it's an INSERT and there's no tlist entry for the
344
* column, or the tlist entry is a DEFAULT placeholder node.
346
if ((new_tle == NULL && commandType == CMD_INSERT) ||
347
(new_tle && new_tle->expr && IsA(new_tle->expr, SetToDefault)))
351
new_expr = build_column_default(target_relation, attrno);
354
* If there is no default (ie, default is effectively NULL),
355
* we can omit the tlist entry in the INSERT case, since the
356
* planner can insert a NULL for itself, and there's no point
357
* in spending any more rewriter cycles on the entry. But in
358
* the UPDATE case we've got to explicitly set the column to
363
if (commandType == CMD_INSERT)
367
new_expr = (Node *) makeConst(att_tup->atttypid,
372
/* this is to catch a NOT NULL domain constraint */
373
new_expr = coerce_to_domain(new_expr,
376
COERCE_IMPLICIT_CAST,
383
new_tle = makeTargetEntry(makeResdom(attrno,
386
pstrdup(NameStr(att_tup->attname)),
392
new_tlist = lappend(new_tlist, new_tle);
396
* Copy all resjunk tlist entries to the end of the new tlist, and
397
* assign them resnos above the last real resno.
399
* Typical junk entries include ORDER BY or GROUP BY expressions (are
400
* these actually possible in an INSERT or UPDATE?), system attribute
405
TargetEntry *old_tle = (TargetEntry *) lfirst(temp);
406
Resdom *resdom = old_tle->resdom;
410
/* Get the resno right, but don't copy unnecessarily */
411
if (resdom->resno != attrno)
413
resdom = (Resdom *) copyObject((Node *) resdom);
414
resdom->resno = attrno;
415
old_tle = makeTargetEntry(resdom, old_tle->expr);
417
new_tlist = lappend(new_tlist, old_tle);
422
/* Let's just make sure we processed all the non-junk items */
423
if (resdom->resno < 1 || resdom->resno > numattrs)
424
elog(ERROR, "bogus resno %d in targetlist", resdom->resno);
428
parsetree->targetList = new_tlist;
433
* Convert a matched TLE from the original tlist into a correct new TLE.
435
* This routine detects and handles multiple assignments to the same target
436
* attribute. (The attribute name is needed only for error messages.)
439
process_matched_tle(TargetEntry *src_tle,
440
TargetEntry *prior_tle,
441
const char *attrName)
443
Resdom *resdom = src_tle->resdom;
451
if (prior_tle == NULL)
454
* Normal case where this is the first assignment to the
461
* Multiple assignments to same attribute. Allow only if all are
462
* FieldStore or ArrayRef assignment operations. This is a bit
463
* tricky because what we may actually be looking at is a nest of
464
* such nodes; consider
465
* UPDATE tab SET col.fld1.subfld1 = x, col.fld2.subfld2 = y
466
* The two expressions produced by the parser will look like
467
* FieldStore(col, fld1, FieldStore(placeholder, subfld1, x))
468
* FieldStore(col, fld2, FieldStore(placeholder, subfld2, x))
469
* However, we can ignore the substructure and just consider the top
470
* FieldStore or ArrayRef from each assignment, because it works to
472
* FieldStore(FieldStore(col, fld1,
473
* FieldStore(placeholder, subfld1, x)),
474
* fld2, FieldStore(placeholder, subfld2, x))
475
* Note the leftmost expression goes on the inside so that the
476
* assignments appear to occur left-to-right.
478
* For FieldStore, instead of nesting we can generate a single
479
* FieldStore with multiple target fields. We must nest when
480
* ArrayRefs are involved though.
483
src_expr = (Node *) src_tle->expr;
484
prior_expr = (Node *) prior_tle->expr;
485
src_input = get_assignment_input(src_expr);
486
prior_input = get_assignment_input(prior_expr);
487
if (src_input == NULL ||
488
prior_input == NULL ||
489
exprType(src_expr) != exprType(prior_expr))
491
(errcode(ERRCODE_SYNTAX_ERROR),
492
errmsg("multiple assignments to same column \"%s\"",
496
* Prior TLE could be a nest of assignments if we do this more than
499
priorbottom = prior_input;
502
Node *newbottom = get_assignment_input(priorbottom);
504
if (newbottom == NULL)
505
break; /* found the original Var reference */
506
priorbottom = newbottom;
508
if (!equal(priorbottom, src_input))
510
(errcode(ERRCODE_SYNTAX_ERROR),
511
errmsg("multiple assignments to same column \"%s\"",
515
* Looks OK to nest 'em.
517
if (IsA(src_expr, FieldStore))
519
FieldStore *fstore = makeNode(FieldStore);
521
if (IsA(prior_expr, FieldStore))
523
/* combine the two */
524
memcpy(fstore, prior_expr, sizeof(FieldStore));
526
list_concat(list_copy(((FieldStore *) prior_expr)->newvals),
527
list_copy(((FieldStore *) src_expr)->newvals));
529
list_concat(list_copy(((FieldStore *) prior_expr)->fieldnums),
530
list_copy(((FieldStore *) src_expr)->fieldnums));
534
/* general case, just nest 'em */
535
memcpy(fstore, src_expr, sizeof(FieldStore));
536
fstore->arg = (Expr *) prior_expr;
538
newexpr = (Node *) fstore;
540
else if (IsA(src_expr, ArrayRef))
542
ArrayRef *aref = makeNode(ArrayRef);
544
memcpy(aref, src_expr, sizeof(ArrayRef));
545
aref->refexpr = (Expr *) prior_expr;
546
newexpr = (Node *) aref;
550
elog(ERROR, "can't happen");
554
return makeTargetEntry(resdom, (Expr *) newexpr);
558
* If node is an assignment node, return its input; else return NULL
561
get_assignment_input(Node *node)
565
if (IsA(node, FieldStore))
567
FieldStore *fstore = (FieldStore *) node;
569
return (Node *) fstore->arg;
571
else if (IsA(node, ArrayRef))
573
ArrayRef *aref = (ArrayRef *) node;
575
if (aref->refassgnexpr == NULL)
577
return (Node *) aref->refexpr;
583
* Make an expression tree for the default value for a column.
585
* If there is no default, return a NULL instead.
588
build_column_default(Relation rel, int attrno)
590
TupleDesc rd_att = rel->rd_att;
591
Form_pg_attribute att_tup = rd_att->attrs[attrno - 1];
592
Oid atttype = att_tup->atttypid;
593
int32 atttypmod = att_tup->atttypmod;
598
* Scan to see if relation has a default for this column.
600
if (rd_att->constr && rd_att->constr->num_defval > 0)
602
AttrDefault *defval = rd_att->constr->defval;
603
int ndef = rd_att->constr->num_defval;
607
if (attrno == defval[ndef].adnum)
610
* Found it, convert string representation to node tree.
612
expr = stringToNode(defval[ndef].adbin);
621
* No per-column default, so look for a default for the type
624
expr = get_typdefault(atttype);
628
return NULL; /* No default anywhere */
631
* Make sure the value is coerced to the target column type; this will
632
* generally be true already, but there seem to be some corner cases
633
* involving domain defaults where it might not be true. This should
634
* match the parser's processing of non-defaulted expressions --- see
635
* updateTargetListEntry().
637
exprtype = exprType(expr);
639
expr = coerce_to_target_type(NULL, /* no UNKNOWN params here */
643
COERCE_IMPLICIT_CAST);
646
(errcode(ERRCODE_DATATYPE_MISMATCH),
647
errmsg("column \"%s\" is of type %s"
648
" but default expression is of type %s",
649
NameStr(att_tup->attname),
650
format_type_be(atttype),
651
format_type_be(exprtype)),
652
errhint("You will need to rewrite or cast the expression.")));
660
* match the list of locks and returns the matching rules
663
matchLocks(CmdType event,
668
List *matching_locks = NIL;
672
if (rulelocks == NULL)
675
if (parsetree->commandType != CMD_SELECT)
677
if (parsetree->resultRelation != varno)
681
nlocks = rulelocks->numLocks;
683
for (i = 0; i < nlocks; i++)
685
RewriteRule *oneLock = rulelocks->rules[i];
687
if (oneLock->event == event)
689
if (parsetree->commandType != CMD_SELECT ||
690
(oneLock->attrno == -1 ?
691
rangeTableEntry_used((Node *) parsetree, varno, 0) :
692
attribute_used((Node *) parsetree,
693
varno, oneLock->attrno, 0)))
694
matching_locks = lappend(matching_locks, oneLock);
698
return matching_locks;
703
ApplyRetrieveRule(Query *parsetree,
715
if (list_length(rule->actions) != 1)
716
elog(ERROR, "expected just one rule action");
717
if (rule->qual != NULL)
718
elog(ERROR, "cannot handle qualified ON SELECT rule");
720
elog(ERROR, "cannot handle per-attribute ON SELECT rule");
723
* Make a modifiable copy of the view query, and recursively expand
724
* any view references inside it.
726
rule_action = copyObject(linitial(rule->actions));
728
rule_action = fireRIRrules(rule_action, activeRIRs);
731
* VIEWs are really easy --- just plug the view query in as a
732
* subselect, replacing the relation's original RTE.
734
rte = rt_fetch(rt_index, parsetree->rtable);
736
rte->rtekind = RTE_SUBQUERY;
737
rte->relid = InvalidOid;
738
rte->subquery = rule_action;
739
rte->inh = false; /* must not be set for a subquery */
742
* We move the view's permission check data down to its rangetable.
743
* The checks will actually be done against the *OLD* entry therein.
745
subrte = rt_fetch(PRS2_OLD_VARNO, rule_action->rtable);
746
Assert(subrte->relid == relation->rd_id);
747
subrte->requiredPerms = rte->requiredPerms;
748
subrte->checkAsUser = rte->checkAsUser;
750
rte->requiredPerms = 0; /* no permission check on subquery itself */
751
rte->checkAsUser = 0;
754
* FOR UPDATE of view?
756
if (list_member_int(parsetree->rowMarks, rt_index))
759
* Remove the view from the list of rels that will actually be
760
* marked FOR UPDATE by the executor. It will still be access-
761
* checked for write access, though.
763
parsetree->rowMarks = list_delete_int(parsetree->rowMarks, rt_index);
766
* Set up the view's referenced tables as if FOR UPDATE.
768
markQueryForUpdate(rule_action, true);
775
* Recursively mark all relations used by a view as FOR UPDATE.
777
* This may generate an invalid query, eg if some sub-query uses an
778
* aggregate. We leave it to the planner to detect that.
780
* NB: this must agree with the parser's transformForUpdate() routine.
783
markQueryForUpdate(Query *qry, bool skipOldNew)
788
foreach(l, qry->rtable)
790
RangeTblEntry *rte = (RangeTblEntry *) lfirst(l);
794
/* Ignore OLD and NEW entries if we are at top level of view */
796
(rti == PRS2_OLD_VARNO || rti == PRS2_NEW_VARNO))
799
if (rte->rtekind == RTE_RELATION)
801
if (!list_member_int(qry->rowMarks, rti))
802
qry->rowMarks = lappend_int(qry->rowMarks, rti);
803
rte->requiredPerms |= ACL_SELECT_FOR_UPDATE;
805
else if (rte->rtekind == RTE_SUBQUERY)
807
/* FOR UPDATE of subquery is propagated to subquery's rels */
808
markQueryForUpdate(rte->subquery, false);
816
* Apply fireRIRrules() to each SubLink (subselect in expression) found
819
* NOTE: although this has the form of a walker, we cheat and modify the
820
* SubLink nodes in-place. It is caller's responsibility to ensure that
821
* no unwanted side-effects occur!
823
* This is unlike most of the other routines that recurse into subselects,
824
* because we must take control at the SubLink node in order to replace
825
* the SubLink's subselect link with the possibly-rewritten subquery.
828
fireRIRonSubLink(Node *node, List *activeRIRs)
832
if (IsA(node, SubLink))
834
SubLink *sub = (SubLink *) node;
836
/* Do what we came for */
837
sub->subselect = (Node *) fireRIRrules((Query *) sub->subselect,
839
/* Fall through to process lefthand args of SubLink */
843
* Do NOT recurse into Query nodes, because fireRIRrules already
844
* processed subselects of subselects for us.
846
return expression_tree_walker(node, fireRIRonSubLink,
847
(void *) activeRIRs);
853
* Apply all RIR rules on each rangetable entry in a query
856
fireRIRrules(Query *parsetree, List *activeRIRs)
861
* don't try to convert this into a foreach loop, because rtable list
862
* can get changed each time through...
865
while (rt_index < list_length(parsetree->rtable))
878
rte = rt_fetch(rt_index, parsetree->rtable);
881
* A subquery RTE can't have associated rules, so there's nothing
882
* to do to this level of the query, but we must recurse into the
883
* subquery to expand any rule references in it.
885
if (rte->rtekind == RTE_SUBQUERY)
887
rte->subquery = fireRIRrules(rte->subquery, activeRIRs);
892
* Joins and other non-relation RTEs can be ignored completely.
894
if (rte->rtekind != RTE_RELATION)
898
* If the table is not referenced in the query, then we ignore it.
899
* This prevents infinite expansion loop due to new rtable entries
900
* inserted by expansion of a rule. A table is referenced if it is
901
* part of the join set (a source table), or is referenced by any
902
* Var nodes, or is the result table.
904
relIsUsed = rangeTableEntry_used((Node *) parsetree, rt_index, 0);
906
if (!relIsUsed && rt_index != parsetree->resultRelation)
910
* This may well be the first access to the relation during the
911
* current statement (it will be, if this Query was extracted from
912
* a rule or somehow got here other than via the parser).
913
* Therefore, grab the appropriate lock type for the relation, and
914
* do not release it until end of transaction. This protects the
915
* rewriter and planner against schema changes mid-query.
917
* If the relation is the query's result relation, then
918
* RewriteQuery() already got the right lock on it, so we need no
919
* additional lock. Otherwise, check to see if the relation is
920
* accessed FOR UPDATE or not.
922
if (rt_index == parsetree->resultRelation)
924
else if (list_member_int(parsetree->rowMarks, rt_index))
925
lockmode = RowShareLock;
927
lockmode = AccessShareLock;
929
rel = heap_open(rte->relid, lockmode);
932
* Collect the RIR rules that we must apply
934
rules = rel->rd_rules;
937
heap_close(rel, NoLock);
941
for (i = 0; i < rules->numLocks; i++)
943
rule = rules->rules[i];
944
if (rule->event != CMD_SELECT)
947
if (rule->attrno > 0)
949
/* per-attr rule; do we need it? */
950
if (!attribute_used((Node *) parsetree, rt_index,
955
locks = lappend(locks, rule);
959
* If we found any, apply them --- but first check for recursion!
965
if (list_member_oid(activeRIRs, RelationGetRelid(rel)))
967
(errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
968
errmsg("infinite recursion detected in rules for relation \"%s\"",
969
RelationGetRelationName(rel))));
970
activeRIRs = lcons_oid(RelationGetRelid(rel), activeRIRs);
976
parsetree = ApplyRetrieveRule(parsetree,
985
activeRIRs = list_delete_first(activeRIRs);
988
heap_close(rel, NoLock);
992
* Recurse into sublink subqueries, too. But we already did the ones
995
if (parsetree->hasSubLinks)
996
query_tree_walker(parsetree, fireRIRonSubLink, (void *) activeRIRs,
997
QTW_IGNORE_RT_SUBQUERIES);
1000
* If the query was marked having aggregates, check if this is still
1001
* true after rewriting. Ditto for sublinks. Note there should be no
1002
* aggs in the qual at this point. (Does this code still do anything
1003
* useful? The view-becomes-subselect-in-FROM approach doesn't look
1004
* like it could remove aggs or sublinks...)
1006
if (parsetree->hasAggs)
1008
parsetree->hasAggs = checkExprHasAggs((Node *) parsetree);
1009
if (parsetree->hasAggs)
1010
if (checkExprHasAggs((Node *) parsetree->jointree))
1011
elog(ERROR, "failed to remove aggregates from qual");
1013
if (parsetree->hasSubLinks)
1014
parsetree->hasSubLinks = checkExprHasSubLink((Node *) parsetree);
1021
* Modify the given query by adding 'AND rule_qual IS NOT TRUE' to its
1022
* qualification. This is used to generate suitable "else clauses" for
1023
* conditional INSTEAD rules. (Unfortunately we must use "x IS NOT TRUE",
1024
* not just "NOT x" which the planner is much smarter about, else we will
1025
* do the wrong thing when the qual evaluates to NULL.)
1027
* The rule_qual may contain references to OLD or NEW. OLD references are
1028
* replaced by references to the specified rt_index (the relation that the
1029
* rule applies to). NEW references are only possible for INSERT and UPDATE
1030
* queries on the relation itself, and so they should be replaced by copies
1031
* of the related entries in the query's own targetlist.
1034
CopyAndAddInvertedQual(Query *parsetree,
1039
Query *new_tree = (Query *) copyObject(parsetree);
1040
Node *new_qual = (Node *) copyObject(rule_qual);
1042
/* Fix references to OLD */
1043
ChangeVarNodes(new_qual, PRS2_OLD_VARNO, rt_index, 0);
1044
/* Fix references to NEW */
1045
if (event == CMD_INSERT || event == CMD_UPDATE)
1046
new_qual = ResolveNew(new_qual,
1050
parsetree->targetList,
1053
/* And attach the fixed qual */
1054
AddInvertedQual(new_tree, new_qual);
1062
* Iterate through rule locks applying rules.
1065
* parsetree - original query
1066
* rt_index - RT index of result relation in original query
1067
* event - type of rule event
1068
* locks - list of rules to fire
1070
* *instead_flag - set TRUE if any unqualified INSTEAD rule is found
1071
* (must be initialized to FALSE)
1072
* *qual_product - filled with modified original query if any qualified
1073
* INSTEAD rule is found (must be initialized to NULL)
1075
* list of rule actions adjusted for use with this query
1077
* Qualified INSTEAD rules generate their action with the qualification
1078
* condition added. They also generate a modified version of the original
1079
* query with the negated qualification added, so that it will run only for
1080
* rows that the qualified action doesn't act on. (If there are multiple
1081
* qualified INSTEAD rules, we AND all the negated quals onto a single
1082
* modified original query.) We won't execute the original, unmodified
1083
* query if we find either qualified or unqualified INSTEAD rules. If
1084
* we find both, the modified original query is discarded too.
1087
fireRules(Query *parsetree,
1092
Query **qual_product)
1094
List *results = NIL;
1099
RewriteRule *rule_lock = (RewriteRule *) lfirst(l);
1100
Node *event_qual = rule_lock->qual;
1101
List *actions = rule_lock->actions;
1105
/* Determine correct QuerySource value for actions */
1106
if (rule_lock->isInstead)
1108
if (event_qual != NULL)
1109
qsrc = QSRC_QUAL_INSTEAD_RULE;
1112
qsrc = QSRC_INSTEAD_RULE;
1113
*instead_flag = true; /* report unqualified INSTEAD */
1117
qsrc = QSRC_NON_INSTEAD_RULE;
1119
if (qsrc == QSRC_QUAL_INSTEAD_RULE)
1122
* If there are INSTEAD rules with qualifications, the
1123
* original query is still performed. But all the negated rule
1124
* qualifications of the INSTEAD rules are added so it does
1125
* its actions only in cases where the rule quals of all
1126
* INSTEAD rules are false. Think of it as the default action
1127
* in a case. We save this in *qual_product so RewriteQuery()
1128
* can add it to the query list after we mangled it up enough.
1130
* If we have already found an unqualified INSTEAD rule, then
1131
* *qual_product won't be used, so don't bother building it.
1135
if (*qual_product == NULL)
1136
*qual_product = parsetree;
1137
*qual_product = CopyAndAddInvertedQual(*qual_product,
1144
/* Now process the rule's actions and add them to the result list */
1147
Query *rule_action = lfirst(r);
1149
if (rule_action->commandType == CMD_NOTHING)
1152
rule_action = rewriteRuleAction(parsetree, rule_action,
1153
event_qual, rt_index, event);
1155
rule_action->querySource = qsrc;
1156
rule_action->canSetTag = false; /* might change later */
1158
results = lappend(results, rule_action);
1168
* rewrites the query and apply the rules again on the queries rewritten
1170
* rewrite_events is a list of open query-rewrite actions, so we can detect
1171
* infinite recursion.
1174
RewriteQuery(Query *parsetree, List *rewrite_events)
1176
CmdType event = parsetree->commandType;
1177
bool instead = false;
1178
Query *qual_product = NULL;
1179
List *rewritten = NIL;
1182
* If the statement is an update, insert or delete - fire rules on it.
1184
* SELECT rules are handled later when we have all the queries that
1185
* should get executed. Also, utilities aren't rewritten at all (do
1186
* we still need that check?)
1188
if (event != CMD_SELECT && event != CMD_UTILITY)
1190
int result_relation;
1191
RangeTblEntry *rt_entry;
1192
Relation rt_entry_relation;
1195
result_relation = parsetree->resultRelation;
1196
Assert(result_relation != 0);
1197
rt_entry = rt_fetch(result_relation, parsetree->rtable);
1198
Assert(rt_entry->rtekind == RTE_RELATION);
1201
* This may well be the first access to the result relation during
1202
* the current statement (it will be, if this Query was extracted
1203
* from a rule or somehow got here other than via the parser).
1204
* Therefore, grab the appropriate lock type for a result
1205
* relation, and do not release it until end of transaction. This
1206
* protects the rewriter and planner against schema changes
1209
rt_entry_relation = heap_open(rt_entry->relid, RowExclusiveLock);
1212
* If it's an INSERT or UPDATE, rewrite the targetlist into
1213
* standard form. This will be needed by the planner anyway, and
1214
* doing it now ensures that any references to NEW.field will
1217
if (event == CMD_INSERT || event == CMD_UPDATE)
1218
rewriteTargetList(parsetree, rt_entry_relation);
1221
* Collect and apply the appropriate rules.
1223
locks = matchLocks(event, rt_entry_relation->rd_rules,
1224
result_relation, parsetree);
1228
List *product_queries;
1230
product_queries = fireRules(parsetree,
1238
* If we got any product queries, recursively rewrite them ---
1239
* but first check for recursion!
1241
if (product_queries != NIL)
1246
foreach(n, rewrite_events)
1248
rev = (rewrite_event *) lfirst(n);
1249
if (rev->relation == RelationGetRelid(rt_entry_relation) &&
1250
rev->event == event)
1252
(errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
1253
errmsg("infinite recursion detected in rules for relation \"%s\"",
1254
RelationGetRelationName(rt_entry_relation))));
1257
rev = (rewrite_event *) palloc(sizeof(rewrite_event));
1258
rev->relation = RelationGetRelid(rt_entry_relation);
1260
rewrite_events = lcons(rev, rewrite_events);
1262
foreach(n, product_queries)
1264
Query *pt = (Query *) lfirst(n);
1267
newstuff = RewriteQuery(pt, rewrite_events);
1268
rewritten = list_concat(rewritten, newstuff);
1271
rewrite_events = list_delete_first(rewrite_events);
1275
heap_close(rt_entry_relation, NoLock); /* keep lock! */
1279
* For INSERTs, the original query is done first; for UPDATE/DELETE,
1280
* it is done last. This is needed because update and delete rule
1281
* actions might not do anything if they are invoked after the update
1282
* or delete is performed. The command counter increment between the
1283
* query executions makes the deleted (and maybe the updated) tuples
1284
* disappear so the scans for them in the rule actions cannot find
1287
* If we found any unqualified INSTEAD, the original query is not done at
1288
* all, in any form. Otherwise, we add the modified form if qualified
1289
* INSTEADs were found, else the unmodified form.
1293
if (parsetree->commandType == CMD_INSERT)
1295
if (qual_product != NULL)
1296
rewritten = lcons(qual_product, rewritten);
1298
rewritten = lcons(parsetree, rewritten);
1302
if (qual_product != NULL)
1303
rewritten = lappend(rewritten, qual_product);
1305
rewritten = lappend(rewritten, parsetree);
1315
* Primary entry point to the query rewriter.
1316
* Rewrite one query via query rewrite system, possibly returning 0
1319
* NOTE: The code in QueryRewrite was formerly in pg_parse_and_plan(), and was
1320
* moved here so that it would be invoked during EXPLAIN.
1323
QueryRewrite(Query *parsetree)
1326
List *results = NIL;
1328
CmdType origCmdType;
1329
bool foundOriginalQuery;
1335
* Apply all non-SELECT rules possibly getting 0 or many queries
1337
querylist = RewriteQuery(parsetree, NIL);
1342
* Apply all the RIR rules on each query
1344
foreach(l, querylist)
1346
Query *query = (Query *) lfirst(l);
1348
query = fireRIRrules(query, NIL);
1351
* If the query target was rewritten as a view, complain.
1353
if (query->resultRelation)
1355
RangeTblEntry *rte = rt_fetch(query->resultRelation,
1358
if (rte->rtekind == RTE_SUBQUERY)
1360
switch (query->commandType)
1364
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1365
errmsg("cannot insert into a view"),
1366
errhint("You need an unconditional ON INSERT DO INSTEAD rule.")));
1370
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1371
errmsg("cannot update a view"),
1372
errhint("You need an unconditional ON UPDATE DO INSTEAD rule.")));
1376
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1377
errmsg("cannot delete from a view"),
1378
errhint("You need an unconditional ON DELETE DO INSTEAD rule.")));
1381
elog(ERROR, "unrecognized commandType: %d",
1382
(int) query->commandType);
1388
results = lappend(results, query);
1394
* Determine which, if any, of the resulting queries is supposed to set
1395
* the command-result tag; and update the canSetTag fields
1398
* If the original query is still in the list, it sets the command tag.
1399
* Otherwise, the last INSTEAD query of the same kind as the original
1400
* is allowed to set the tag. (Note these rules can leave us with no
1401
* query setting the tag. The tcop code has to cope with this by
1402
* setting up a default tag based on the original un-rewritten query.)
1404
* The Asserts verify that at most one query in the result list is marked
1405
* canSetTag. If we aren't checking asserts, we can fall out of the
1406
* loop as soon as we find the original query.
1408
origCmdType = parsetree->commandType;
1409
foundOriginalQuery = false;
1414
Query *query = (Query *) lfirst(l);
1416
if (query->querySource == QSRC_ORIGINAL)
1418
Assert(query->canSetTag);
1419
Assert(!foundOriginalQuery);
1420
foundOriginalQuery = true;
1421
#ifndef USE_ASSERT_CHECKING
1427
Assert(!query->canSetTag);
1428
if (query->commandType == origCmdType &&
1429
(query->querySource == QSRC_INSTEAD_RULE ||
1430
query->querySource == QSRC_QUAL_INSTEAD_RULE))
1431
lastInstead = query;
1435
if (!foundOriginalQuery && lastInstead != NULL)
1436
lastInstead->canSetTag = true;