1 /*-------------------------------------------------------------------------
4 * use rewrite rules to construct views
6 * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * $Header: /cvsroot/pgsql/src/backend/commands/view.c,v 1.70 2002/09/02 20:04:40 tgl Exp $
13 *-------------------------------------------------------------------------
17 #include "access/heapam.h"
18 #include "catalog/dependency.h"
19 #include "catalog/namespace.h"
20 #include "commands/tablecmds.h"
21 #include "commands/view.h"
22 #include "miscadmin.h"
23 #include "nodes/makefuncs.h"
24 #include "parser/parse_relation.h"
25 #include "rewrite/rewriteDefine.h"
26 #include "rewrite/rewriteManip.h"
27 #include "rewrite/rewriteSupport.h"
28 #include "utils/acl.h"
29 #include "utils/lsyscache.h"
32 static void checkViewTupleDesc(TupleDesc newdesc, TupleDesc olddesc);
35 /*---------------------------------------------------------------------
36 * DefineVirtualRelation
38 * Create the "view" relation. `DefineRelation' does all the work,
39 * we just provide the correct arguments ... at least when we're
40 * creating a view. If we're updating an existing view, we have to
42 *---------------------------------------------------------------------
45 DefineVirtualRelation(const RangeVar *relation, List *tlist, bool replace)
49 CreateStmt *createStmt = makeNode(CreateStmt);
54 * create a list of ColumnDef nodes based on the names and types of
55 * the (non-junk) targetlist items from the view's SELECT list.
60 TargetEntry *entry = lfirst(t);
61 Resdom *res = entry->resdom;
65 ColumnDef *def = makeNode(ColumnDef);
66 TypeName *typename = makeNode(TypeName);
68 def->colname = pstrdup(res->resname);
70 typename->typeid = res->restype;
71 typename->typmod = res->restypmod;
72 def->typename = typename;
74 def->is_inherited = false;
75 def->is_not_null = false;
76 def->raw_default = NULL;
77 def->cooked_default = NULL;
78 def->constraints = NIL;
81 attrList = lappend(attrList, def);
86 elog(ERROR, "attempted to define virtual relation with no attrs");
89 * Check to see if we want to replace an existing view.
91 namespaceId = RangeVarGetCreationNamespace(relation);
92 viewOid = get_relname_relid(relation->relname, namespaceId);
94 if (OidIsValid(viewOid) && replace)
100 * Yes. Get exclusive lock on the existing view ...
102 rel = relation_open(viewOid, AccessExclusiveLock);
105 * Make sure it *is* a view, and do permissions checks.
107 if (rel->rd_rel->relkind != RELKIND_VIEW)
108 elog(ERROR, "%s is not a view",
109 RelationGetRelationName(rel));
111 if (!pg_class_ownercheck(viewOid, GetUserId()))
112 aclcheck_error(ACLCHECK_NOT_OWNER, RelationGetRelationName(rel));
115 * Create a tuple descriptor to compare against the existing view,
116 * and verify it matches.
118 descriptor = BuildDescForRelation(attrList);
119 checkViewTupleDesc(descriptor, rel->rd_att);
122 * Seems okay, so return the OID of the pre-existing view.
124 relation_close(rel, NoLock); /* keep the lock! */
131 * now create the parameters for keys/inheritance etc. All of them are
134 createStmt->relation = (RangeVar *) relation;
135 createStmt->tableElts = attrList;
136 createStmt->inhRelations = NIL;
137 createStmt->constraints = NIL;
138 createStmt->hasoids = false;
141 * finally create the relation (this will error out if there's
142 * an existing view, so we don't need more code to complain
143 * if "replace" is false).
145 return DefineRelation(createStmt, RELKIND_VIEW);
150 * Verify that tupledesc associated with proposed new view definition
151 * matches tupledesc of old view. This is basically a cut-down version
152 * of equalTupleDescs(), with code added to generate specific complaints.
155 checkViewTupleDesc(TupleDesc newdesc, TupleDesc olddesc)
159 if (newdesc->natts != olddesc->natts)
160 elog(ERROR, "Cannot change number of columns in view");
161 /* we can ignore tdhasoid */
163 for (i = 0; i < newdesc->natts; i++)
165 Form_pg_attribute newattr = newdesc->attrs[i];
166 Form_pg_attribute oldattr = olddesc->attrs[i];
168 /* XXX not right, but we don't support DROP COL on view anyway */
169 if (newattr->attisdropped != oldattr->attisdropped)
170 elog(ERROR, "Cannot change number of columns in view");
172 if (strcmp(NameStr(newattr->attname), NameStr(oldattr->attname)) != 0)
173 elog(ERROR, "Cannot change name of view column \"%s\"",
174 NameStr(oldattr->attname));
175 /* XXX would it be safe to allow atttypmod to change? Not sure */
176 if (newattr->atttypid != oldattr->atttypid ||
177 newattr->atttypmod != oldattr->atttypmod)
178 elog(ERROR, "Cannot change datatype of view column \"%s\"",
179 NameStr(oldattr->attname));
180 /* We can ignore the remaining attributes of an attribute... */
183 * We ignore the constraint fields. The new view desc can't have any
184 * constraints, and the only ones that could be on the old view are
185 * defaults, which we are happy to leave in place.
190 FormViewRetrieveRule(const RangeVar *view, Query *viewParse, bool replace)
195 * Create a RuleStmt that corresponds to the suitable rewrite rule
196 * args for DefineQueryRewrite();
198 rule = makeNode(RuleStmt);
199 rule->relation = copyObject((RangeVar *) view);
200 rule->rulename = pstrdup(ViewSelectRuleName);
201 rule->whereClause = NULL;
202 rule->event = CMD_SELECT;
203 rule->instead = true;
204 rule->actions = makeList1(viewParse);
205 rule->replace = replace;
211 DefineViewRules(const RangeVar *view, Query *viewParse, bool replace)
213 RuleStmt *retrieve_rule;
216 RuleStmt *replace_rule;
217 RuleStmt *append_rule;
218 RuleStmt *delete_rule;
221 retrieve_rule = FormViewRetrieveRule(view, viewParse, replace);
224 replace_rule = FormViewReplaceRule(view, viewParse);
225 append_rule = FormViewAppendRule(view, viewParse);
226 delete_rule = FormViewDeleteRule(view, viewParse);
229 DefineQueryRewrite(retrieve_rule);
232 DefineQueryRewrite(replace_rule);
233 DefineQueryRewrite(append_rule);
234 DefineQueryRewrite(delete_rule);
239 /*---------------------------------------------------------------
240 * UpdateRangeTableOfViewParse
242 * Update the range table of the given parsetree.
243 * This update consists of adding two new entries IN THE BEGINNING
244 * of the range table (otherwise the rule system will die a slow,
245 * horrible and painful death, and we do not want that now, do we?)
246 * one for the OLD relation and one for the NEW one (both of
247 * them refer in fact to the "view" relation).
249 * Of course we must also increase the 'varnos' of all the Var nodes
252 * These extra RT entries are not actually used in the query,
253 * except for run-time permission checking.
254 *---------------------------------------------------------------
257 UpdateRangeTableOfViewParse(Oid viewOid, Query *viewParse)
260 RangeTblEntry *rt_entry1,
264 * Make a copy of the given parsetree. It's not so much that we don't
265 * want to scribble on our input, it's that the parser has a bad habit
266 * of outputting multiple links to the same subtree for constructs
267 * like BETWEEN, and we mustn't have OffsetVarNodes increment the
268 * varno of a Var node twice. copyObject will expand any
269 * multiply-referenced subtree into multiple copies.
271 viewParse = (Query *) copyObject(viewParse);
274 * Create the 2 new range table entries and form the new range
275 * table... OLD first, then NEW....
277 rt_entry1 = addRangeTableEntryForRelation(NULL, viewOid,
278 makeAlias("*OLD*", NIL),
280 rt_entry2 = addRangeTableEntryForRelation(NULL, viewOid,
281 makeAlias("*NEW*", NIL),
283 /* Must override addRangeTableEntry's default access-check flags */
284 rt_entry1->checkForRead = false;
285 rt_entry2->checkForRead = false;
287 new_rt = lcons(rt_entry1, lcons(rt_entry2, viewParse->rtable));
289 viewParse->rtable = new_rt;
292 * Now offset all var nodes by 2, and jointree RT indexes too.
294 OffsetVarNodes((Node *) viewParse, 2, 0);
299 /*-------------------------------------------------------------------
302 * - takes a "viewname", "parsetree" pair and then
303 * 1) construct the "virtual" relation
304 * 2) commit the command but NOT the transaction,
305 * so that the relation exists
306 * before the rules are defined.
307 * 2) define the "n" rules specified in the PRS2 paper
308 * over the "virtual" relation
309 *-------------------------------------------------------------------
312 DefineView(const RangeVar *view, Query *viewParse, bool replace)
317 * Create the view relation
319 * NOTE: if it already exists and replace is false, the xact will
323 viewOid = DefineVirtualRelation(view, viewParse->targetList, replace);
326 * The relation we have just created is not visible to any other
327 * commands running with the same transaction & command id. So,
328 * increment the command id counter (but do NOT pfree any memory!!!!)
330 CommandCounterIncrement();
333 * The range table of 'viewParse' does not contain entries for the
334 * "OLD" and "NEW" relations. So... add them!
336 viewParse = UpdateRangeTableOfViewParse(viewOid, viewParse);
339 * Now create the rules associated with the view.
341 DefineViewRules(view, viewParse, replace);
347 * Remove a view given its name
349 * We just have to drop the relation; the associated rules will be
350 * cleaned up automatically.
353 RemoveView(const RangeVar *view, DropBehavior behavior)
356 ObjectAddress object;
358 viewOid = RangeVarGetRelid(view, false);
360 object.classId = RelOid_pg_class;
361 object.objectId = viewOid;
362 object.objectSubId = 0;
364 performDeletion(&object, behavior);