1 /*-------------------------------------------------------------------------
4 * use rewrite rules to construct views
6 * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * $PostgreSQL: pgsql/src/backend/commands/view.c,v 1.91 2005/10/15 02:49:16 momjian 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 "optimizer/clauses.h"
25 #include "parser/parse_expr.h"
26 #include "parser/parse_relation.h"
27 #include "rewrite/rewriteDefine.h"
28 #include "rewrite/rewriteManip.h"
29 #include "rewrite/rewriteSupport.h"
30 #include "utils/acl.h"
31 #include "utils/lsyscache.h"
34 static void checkViewTupleDesc(TupleDesc newdesc, TupleDesc olddesc);
35 static bool isViewOnTempTable_walker(Node *node, void *context);
37 /*---------------------------------------------------------------------
40 * Returns true iff any of the relations underlying this view are
42 *---------------------------------------------------------------------
45 isViewOnTempTable(Query *viewParse)
47 return isViewOnTempTable_walker((Node *) viewParse, NULL);
51 isViewOnTempTable_walker(Node *node, void *context)
58 Query *query = (Query *) node;
61 foreach(rtable, query->rtable)
63 RangeTblEntry *rte = lfirst(rtable);
65 if (rte->rtekind == RTE_RELATION)
67 Relation rel = heap_open(rte->relid, AccessShareLock);
68 bool istemp = rel->rd_istemp;
70 heap_close(rel, AccessShareLock);
76 return query_tree_walker(query,
77 isViewOnTempTable_walker,
79 QTW_IGNORE_JOINALIASES);
82 return expression_tree_walker(node,
83 isViewOnTempTable_walker,
87 /*---------------------------------------------------------------------
88 * DefineVirtualRelation
90 * Create the "view" relation. `DefineRelation' does all the work,
91 * we just provide the correct arguments ... at least when we're
92 * creating a view. If we're updating an existing view, we have to
94 *---------------------------------------------------------------------
97 DefineVirtualRelation(const RangeVar *relation, List *tlist, bool replace)
101 CreateStmt *createStmt = makeNode(CreateStmt);
106 * create a list of ColumnDef nodes based on the names and types of the
107 * (non-junk) targetlist items from the view's SELECT list.
112 TargetEntry *tle = lfirst(t);
116 ColumnDef *def = makeNode(ColumnDef);
117 TypeName *typename = makeNode(TypeName);
119 def->colname = pstrdup(tle->resname);
121 typename->typeid = exprType((Node *) tle->expr);
122 typename->typmod = exprTypmod((Node *) tle->expr);
123 def->typename = typename;
126 def->is_local = true;
127 def->is_not_null = false;
128 def->raw_default = NULL;
129 def->cooked_default = NULL;
130 def->constraints = NIL;
133 attrList = lappend(attrList, def);
139 (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
140 errmsg("view must have at least one column")));
143 * Check to see if we want to replace an existing view.
145 namespaceId = RangeVarGetCreationNamespace(relation);
146 viewOid = get_relname_relid(relation->relname, namespaceId);
148 if (OidIsValid(viewOid) && replace)
151 TupleDesc descriptor;
154 * Yes. Get exclusive lock on the existing view ...
156 rel = relation_open(viewOid, AccessExclusiveLock);
159 * Make sure it *is* a view, and do permissions checks.
161 if (rel->rd_rel->relkind != RELKIND_VIEW)
163 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
164 errmsg("\"%s\" is not a view",
165 RelationGetRelationName(rel))));
167 if (!pg_class_ownercheck(viewOid, GetUserId()))
168 aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_CLASS,
169 RelationGetRelationName(rel));
172 * Due to the namespace visibility rules for temporary objects, we
173 * should only end up replacing a temporary view with another
174 * temporary view, and vice versa.
176 Assert(relation->istemp == rel->rd_istemp);
179 * Create a tuple descriptor to compare against the existing view, and
182 descriptor = BuildDescForRelation(attrList);
183 checkViewTupleDesc(descriptor, rel->rd_att);
186 * Seems okay, so return the OID of the pre-existing view.
188 relation_close(rel, NoLock); /* keep the lock! */
195 * now set the parameters for keys/inheritance etc. All of these are
196 * uninteresting for views...
198 createStmt->relation = (RangeVar *) relation;
199 createStmt->tableElts = attrList;
200 createStmt->inhRelations = NIL;
201 createStmt->constraints = NIL;
202 createStmt->hasoids = MUST_NOT_HAVE_OIDS;
203 createStmt->oncommit = ONCOMMIT_NOOP;
204 createStmt->tablespacename = NULL;
207 * finally create the relation (this will error out if there's an
208 * existing view, so we don't need more code to complain if "replace"
211 return DefineRelation(createStmt, RELKIND_VIEW);
216 * Verify that tupledesc associated with proposed new view definition
217 * matches tupledesc of old view. This is basically a cut-down version
218 * of equalTupleDescs(), with code added to generate specific complaints.
221 checkViewTupleDesc(TupleDesc newdesc, TupleDesc olddesc)
225 if (newdesc->natts != olddesc->natts)
227 (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
228 errmsg("cannot change number of columns in view")));
229 /* we can ignore tdhasoid */
231 for (i = 0; i < newdesc->natts; i++)
233 Form_pg_attribute newattr = newdesc->attrs[i];
234 Form_pg_attribute oldattr = olddesc->attrs[i];
236 /* XXX not right, but we don't support DROP COL on view anyway */
237 if (newattr->attisdropped != oldattr->attisdropped)
239 (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
240 errmsg("cannot change number of columns in view")));
242 if (strcmp(NameStr(newattr->attname), NameStr(oldattr->attname)) != 0)
244 (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
245 errmsg("cannot change name of view column \"%s\"",
246 NameStr(oldattr->attname))));
247 /* XXX would it be safe to allow atttypmod to change? Not sure */
248 if (newattr->atttypid != oldattr->atttypid ||
249 newattr->atttypmod != oldattr->atttypmod)
251 (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
252 errmsg("cannot change data type of view column \"%s\"",
253 NameStr(oldattr->attname))));
254 /* We can ignore the remaining attributes of an attribute... */
258 * We ignore the constraint fields. The new view desc can't have any
259 * constraints, and the only ones that could be on the old view are
260 * defaults, which we are happy to leave in place.
265 FormViewRetrieveRule(const RangeVar *view, Query *viewParse, bool replace)
270 * Create a RuleStmt that corresponds to the suitable rewrite rule args
271 * for DefineQueryRewrite();
273 rule = makeNode(RuleStmt);
274 rule->relation = copyObject((RangeVar *) view);
275 rule->rulename = pstrdup(ViewSelectRuleName);
276 rule->whereClause = NULL;
277 rule->event = CMD_SELECT;
278 rule->instead = true;
279 rule->actions = list_make1(viewParse);
280 rule->replace = replace;
286 DefineViewRules(const RangeVar *view, Query *viewParse, bool replace)
288 RuleStmt *retrieve_rule;
291 RuleStmt *replace_rule;
292 RuleStmt *append_rule;
293 RuleStmt *delete_rule;
296 retrieve_rule = FormViewRetrieveRule(view, viewParse, replace);
299 replace_rule = FormViewReplaceRule(view, viewParse);
300 append_rule = FormViewAppendRule(view, viewParse);
301 delete_rule = FormViewDeleteRule(view, viewParse);
304 DefineQueryRewrite(retrieve_rule);
307 DefineQueryRewrite(replace_rule);
308 DefineQueryRewrite(append_rule);
309 DefineQueryRewrite(delete_rule);
314 /*---------------------------------------------------------------
315 * UpdateRangeTableOfViewParse
317 * Update the range table of the given parsetree.
318 * This update consists of adding two new entries IN THE BEGINNING
319 * of the range table (otherwise the rule system will die a slow,
320 * horrible and painful death, and we do not want that now, do we?)
321 * one for the OLD relation and one for the NEW one (both of
322 * them refer in fact to the "view" relation).
324 * Of course we must also increase the 'varnos' of all the Var nodes
327 * These extra RT entries are not actually used in the query,
328 * except for run-time permission checking.
329 *---------------------------------------------------------------
332 UpdateRangeTableOfViewParse(Oid viewOid, Query *viewParse)
336 RangeTblEntry *rt_entry1,
340 * Make a copy of the given parsetree. It's not so much that we don't
341 * want to scribble on our input, it's that the parser has a bad habit of
342 * outputting multiple links to the same subtree for constructs like
343 * BETWEEN, and we mustn't have OffsetVarNodes increment the varno of a
344 * Var node twice. copyObject will expand any multiply-referenced subtree
345 * into multiple copies.
347 viewParse = (Query *) copyObject(viewParse);
349 /* need to open the rel for addRangeTableEntryForRelation */
350 viewRel = relation_open(viewOid, AccessShareLock);
353 * Create the 2 new range table entries and form the new range table...
354 * OLD first, then NEW....
356 rt_entry1 = addRangeTableEntryForRelation(NULL, viewRel,
357 makeAlias("*OLD*", NIL),
359 rt_entry2 = addRangeTableEntryForRelation(NULL, viewRel,
360 makeAlias("*NEW*", NIL),
362 /* Must override addRangeTableEntry's default access-check flags */
363 rt_entry1->requiredPerms = 0;
364 rt_entry2->requiredPerms = 0;
366 new_rt = lcons(rt_entry1, lcons(rt_entry2, viewParse->rtable));
368 viewParse->rtable = new_rt;
371 * Now offset all var nodes by 2, and jointree RT indexes too.
373 OffsetVarNodes((Node *) viewParse, 2, 0);
375 relation_close(viewRel, AccessShareLock);
380 /*-------------------------------------------------------------------
383 * - takes a "viewname", "parsetree" pair and then
384 * 1) construct the "virtual" relation
385 * 2) commit the command but NOT the transaction,
386 * so that the relation exists
387 * before the rules are defined.
388 * 2) define the "n" rules specified in the PRS2 paper
389 * over the "virtual" relation
390 *-------------------------------------------------------------------
393 DefineView(RangeVar *view, Query *viewParse, bool replace)
398 * If the user didn't explicitly ask for a temporary view, check whether
399 * we need one implicitly.
403 view->istemp = isViewOnTempTable(viewParse);
406 (errmsg("view \"%s\" will be a temporary view",
411 * Create the view relation
413 * NOTE: if it already exists and replace is false, the xact will be aborted.
415 viewOid = DefineVirtualRelation(view, viewParse->targetList, replace);
418 * The relation we have just created is not visible to any other commands
419 * running with the same transaction & command id. So, increment the
420 * command id counter (but do NOT pfree any memory!!!!)
422 CommandCounterIncrement();
425 * The range table of 'viewParse' does not contain entries for the "OLD"
426 * and "NEW" relations. So... add them!
428 viewParse = UpdateRangeTableOfViewParse(viewOid, viewParse);
431 * Now create the rules associated with the view.
433 DefineViewRules(view, viewParse, replace);
439 * Remove a view given its name
441 * We just have to drop the relation; the associated rules will be
442 * cleaned up automatically.
445 RemoveView(const RangeVar *view, DropBehavior behavior)
448 ObjectAddress object;
450 viewOid = RangeVarGetRelid(view, false);
452 object.classId = RelationRelationId;
453 object.objectId = viewOid;
454 object.objectSubId = 0;
456 performDeletion(&object, behavior);