1 /*-------------------------------------------------------------------------
4 * routines for defining a rewrite rule
6 * Copyright (c) 1994, Regents of the University of California
10 * $Header: /cvsroot/pgsql/src/backend/rewrite/rewriteDefine.c,v 1.27 1999/05/10 00:45:30 momjian Exp $
12 *-------------------------------------------------------------------------
19 #include "access/heapam.h" /* access methods like amopenr */
20 #include "nodes/parsenodes.h"
21 #include "nodes/pg_list.h" /* for Lisp support */
22 #include "parser/parse_relation.h"
23 #include "rewrite/locks.h"
24 #include "rewrite/rewriteDefine.h"
25 #include "rewrite/rewriteRemove.h"
26 #include "rewrite/rewriteSupport.h"
27 #include "tcop/tcopprot.h"
28 #include "utils/builtins.h"
29 #include "utils/lsyscache.h" /* for get_typlen */
30 #include "utils/rel.h" /* for Relation stuff */
33 Oid LastOidProcessed = InvalidOid;
36 * This is too small for many rule plans, but it'll have to do for now.
37 * Rule plans, etc will eventually have to be large objects.
39 * should this be smaller?
41 #define RULE_PLAN_SIZE BLCKSZ
44 strcpyq(char *dest, char *source)
46 char *current = source,
49 for (current = source; *current; current++)
64 * takes the arguments and inserts them as attributes into the system
65 * relation "pg_rewrite"
67 * MODS : changes the value of LastOidProcessed as a side
68 * effect of inserting the rule tuple
70 * ARGS : rulname - name of the rule
71 * evtype - one of RETRIEVE,REPLACE,DELETE,APPEND
72 * evobj - name of relation
73 * evslot - comma delimited list of slots
74 * if null => multi-attr rule
75 * evinstead - is an instead rule
76 * actiontree - parsetree(s) of rule action
79 InsertRule(char *rulname,
87 static char rulebuf[RULE_PLAN_SIZE];
88 static char actionbuf[RULE_PLAN_SIZE];
89 static char qualbuf[RULE_PLAN_SIZE];
90 Oid eventrel_oid = InvalidOid;
91 AttrNumber evslot_index = InvalidAttrNumber;
92 Relation eventrel = NULL;
93 char *is_instead = "f";
94 extern void eval_as_new_xact();
97 eventrel = heap_openr(evobj);
99 elog(ERROR, "rules cannot be defined on relations not in schema");
100 eventrel_oid = RelationGetRelid(eventrel);
103 * if the slotname is null, we know that this is a multi-attr rule
108 evslot_index = attnameAttNum(eventrel, (char *) evslot);
109 heap_close(eventrel);
117 if (IsDefinedRewriteRule(rulname))
118 elog(ERROR, "Attempt to insert rule '%s' failed: already exists",
120 strcpyq(actionbuf, actiontree);
121 strcpyq(qualbuf, evqual);
123 template = "INSERT INTO pg_rewrite \
124 (rulename, ev_type, ev_class, ev_attr, ev_action, ev_qual, is_instead) VALUES \
125 ('%s', %d::char, %u::oid, %d::int2, '%s'::text, '%s'::text, \
127 if (strlen(template) + strlen(rulname) + strlen(actionbuf) +
128 strlen(qualbuf) + 20 /* fudge fac */ > RULE_PLAN_SIZE)
129 elog(ERROR, "DefineQueryRewrite: rule plan string too big.");
130 sprintf(rulebuf, template,
131 rulname, evtype, eventrel_oid, evslot_index, actionbuf,
132 qualbuf, is_instead);
134 pg_exec_query_acl_override(rulebuf);
136 return LastOidProcessed;
140 * for now, event_object must be a single attribute
143 ValidateRule(int event_type,
151 if (((event_type == CMD_INSERT) || (event_type == CMD_DELETE)) &&
155 "rules not allowed for insert or delete events to an attribute");
161 * on retrieve to class.attribute do instead nothing is converted to
162 * 'on retrieve to class.attribute do instead retrieve (attribute =
163 * NULL)' --- this is also a terrible hack that works well -- glass
165 if (is_instead && !*action && eslot_string && event_type == CMD_SELECT)
167 char *temp_buffer = (char *) palloc(strlen(template) + 80);
169 sprintf(temp_buffer, template, event_attype,
170 get_typlen(event_attype), eslot_string,
173 *action = (List *) stringToNode(temp_buffer);
181 DefineQueryRewrite(RuleStmt *stmt)
183 CmdType event_type = stmt->event;
184 Attr *event_obj = stmt->object;
185 Node *event_qual = stmt->whereClause;
186 bool is_instead = stmt->instead;
187 List *action = stmt->actions;
188 Relation event_relation = NULL;
191 char *eslot_string = NULL;
193 Oid event_attype = 0;
200 * The current rewrite handler is known to work on relation level
201 * rules only. And for SELECT events, it expects one non-nothing
202 * action that is instead and returns exactly a tuple of the
203 * rewritten relation. This restricts SELECT rules to views.
208 if (event_obj->attrs)
209 elog(ERROR, "attribute level rules currently not supported");
212 * eslot_string = strVal(lfirst(event_obj->attrs));
218 * No rule actions that modify OLD or NEW
223 query = (Query *) lfirst(l);
224 if (query->resultRelation == 1)
226 elog(NOTICE, "rule actions on OLD currently not supported");
227 elog(ERROR, " use views or triggers instead");
229 if (query->resultRelation == 2)
231 elog(NOTICE, "rule actions on NEW currently not supported");
232 elog(ERROR, " use triggers instead");
237 * Rules ON SELECT are restricted to view definitions
239 if (event_type == CMD_SELECT)
243 Form_pg_attribute attr;
246 char expected_name[NAMEDATALEN + 5];
249 * So there cannot be INSTEAD NOTHING, ...
251 if (length(action) == 0)
253 elog(NOTICE, "instead nothing rules on select currently not supported");
254 elog(ERROR, " use views instead");
258 * ... there cannot be multiple actions, ...
260 if (length(action) > 1)
261 elog(ERROR, "multiple action rules on select currently not supported");
263 * ... the one action must be a SELECT, ...
265 query = (Query *) lfirst(action);
266 if (!is_instead || query->commandType != CMD_SELECT)
267 elog(ERROR, "only instead-select rules currently supported on select");
268 if (event_qual != NULL)
269 elog(ERROR, "event qualifications not supported for rules on select");
272 * ... the targetlist of the SELECT action must
273 * exactly match the event relation, ...
275 event_relation = heap_openr(event_obj->relname);
276 if (event_relation == NULL)
277 elog(ERROR, "virtual relations not supported yet");
279 if (event_relation->rd_att->natts != length(query->targetList))
280 elog(ERROR, "select rules target list must match event relations structure");
282 for (i = 1; i <= event_relation->rd_att->natts; i++) {
283 tle = (TargetEntry *)nth(i - 1, query->targetList);
284 resdom = tle->resdom;
285 attr = event_relation->rd_att->attrs[i - 1];
286 attname = nameout(&(attr->attname));
288 if (strcmp(resdom->resname, attname) != 0)
289 elog(ERROR, "select rules target entry %d has different column name from %s", i, attname);
291 if (attr->atttypid != resdom->restype)
292 elog(ERROR, "select rules target entry %d has different type from attribute %s", i, attname);
294 if (attr->atttypmod != resdom->restypmod)
295 elog(ERROR, "select rules target entry %d has different size from attribute %s", i, attname);
299 * ... there must not be another ON SELECT
302 if (event_relation->rd_rules != NULL) {
303 for (i = 0; i < event_relation->rd_rules->numLocks; i++) {
306 rule = event_relation->rd_rules->rules[i];
307 if (rule->event == CMD_SELECT)
308 elog(ERROR, "%s is already a view", nameout(&(event_relation->rd_rel->relname)));
312 heap_close(event_relation);
315 * LIMIT in view is not supported
317 if (query->limitOffset != NULL || query->limitCount != NULL)
318 elog(ERROR, "LIMIT clause not supported in views");
321 * ... and finally the rule must be named _RETviewname.
323 sprintf(expected_name, "_RET%s", event_obj->relname);
324 if (strcmp(expected_name, stmt->rulename) != 0) {
325 elog(ERROR, "view rule for %s must be named %s",
326 event_obj->relname, expected_name);
331 * This rule is allowed - install it.
334 event_relation = heap_openr(event_obj->relname);
335 if (event_relation == NULL)
336 elog(ERROR, "virtual relations not supported yet");
337 ev_relid = RelationGetRelid(event_relation);
339 if (eslot_string == NULL)
342 event_attype = -1; /* XXX - don't care */
346 event_attno = attnameAttNum(event_relation, eslot_string);
347 event_attype = attnumTypeId(event_relation, event_attno);
349 heap_close(event_relation);
351 /* fix bug about instead nothing */
352 ValidateRule(event_type, event_obj->relname,
353 eslot_string, event_qual, &action,
354 is_instead, event_attype);
359 return; /* doesn't do anything */
361 event_qualP = nodeToString(event_qual);
363 ruleId = InsertRule(stmt->rulename,
370 prs2_addToRelation(ev_relid, ruleId, event_type, event_attno, TRUE,
376 event_qualP = nodeToString(event_qual);
377 actionP = nodeToString(action);
379 ruleId = InsertRule(stmt->rulename,
387 /* what is the max size of type text? XXX -- glass */
388 if (length(action) > 15)
389 elog(ERROR, "max # of actions exceeded");
390 prs2_addToRelation(ev_relid, ruleId, event_type, event_attno,
391 is_instead, event_qual, action);