OSDN Git Service

Update copyrights in source tree to 2008.
[pg-rex/syncrep.git] / src / backend / commands / view.c
1 /*-------------------------------------------------------------------------
2  *
3  * view.c
4  *        use rewrite rules to construct views
5  *
6  * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  *        $PostgreSQL: pgsql/src/backend/commands/view.c,v 1.104 2008/01/01 19:45:49 momjian Exp $
12  *
13  *-------------------------------------------------------------------------
14  */
15 #include "postgres.h"
16
17 #include "access/heapam.h"
18 #include "access/xact.h"
19 #include "catalog/dependency.h"
20 #include "catalog/namespace.h"
21 #include "commands/defrem.h"
22 #include "commands/tablecmds.h"
23 #include "commands/view.h"
24 #include "miscadmin.h"
25 #include "nodes/makefuncs.h"
26 #include "optimizer/clauses.h"
27 #include "parser/analyze.h"
28 #include "parser/parse_expr.h"
29 #include "parser/parse_relation.h"
30 #include "rewrite/rewriteDefine.h"
31 #include "rewrite/rewriteManip.h"
32 #include "rewrite/rewriteSupport.h"
33 #include "utils/acl.h"
34 #include "utils/lsyscache.h"
35
36
37 static void checkViewTupleDesc(TupleDesc newdesc, TupleDesc olddesc);
38 static bool isViewOnTempTable_walker(Node *node, void *context);
39
40 /*---------------------------------------------------------------------
41  * isViewOnTempTable
42  *
43  * Returns true iff any of the relations underlying this view are
44  * temporary tables.
45  *---------------------------------------------------------------------
46  */
47 static bool
48 isViewOnTempTable(Query *viewParse)
49 {
50         return isViewOnTempTable_walker((Node *) viewParse, NULL);
51 }
52
53 static bool
54 isViewOnTempTable_walker(Node *node, void *context)
55 {
56         if (node == NULL)
57                 return false;
58
59         if (IsA(node, Query))
60         {
61                 Query      *query = (Query *) node;
62                 ListCell   *rtable;
63
64                 foreach(rtable, query->rtable)
65                 {
66                         RangeTblEntry *rte = lfirst(rtable);
67
68                         if (rte->rtekind == RTE_RELATION)
69                         {
70                                 Relation        rel = heap_open(rte->relid, AccessShareLock);
71                                 bool            istemp = rel->rd_istemp;
72
73                                 heap_close(rel, AccessShareLock);
74                                 if (istemp)
75                                         return true;
76                         }
77                 }
78
79                 return query_tree_walker(query,
80                                                                  isViewOnTempTable_walker,
81                                                                  context,
82                                                                  QTW_IGNORE_JOINALIASES);
83         }
84
85         return expression_tree_walker(node,
86                                                                   isViewOnTempTable_walker,
87                                                                   context);
88 }
89
90 /*---------------------------------------------------------------------
91  * DefineVirtualRelation
92  *
93  * Create the "view" relation. `DefineRelation' does all the work,
94  * we just provide the correct arguments ... at least when we're
95  * creating a view.  If we're updating an existing view, we have to
96  * work harder.
97  *---------------------------------------------------------------------
98  */
99 static Oid
100 DefineVirtualRelation(const RangeVar *relation, List *tlist, bool replace)
101 {
102         Oid                     viewOid,
103                                 namespaceId;
104         CreateStmt *createStmt = makeNode(CreateStmt);
105         List       *attrList;
106         ListCell   *t;
107
108         /*
109          * create a list of ColumnDef nodes based on the names and types of the
110          * (non-junk) targetlist items from the view's SELECT list.
111          */
112         attrList = NIL;
113         foreach(t, tlist)
114         {
115                 TargetEntry *tle = lfirst(t);
116
117                 if (!tle->resjunk)
118                 {
119                         ColumnDef  *def = makeNode(ColumnDef);
120
121                         def->colname = pstrdup(tle->resname);
122                         def->typename = makeTypeNameFromOid(exprType((Node *) tle->expr),
123                                                                                          exprTypmod((Node *) tle->expr));
124                         def->inhcount = 0;
125                         def->is_local = true;
126                         def->is_not_null = false;
127                         def->raw_default = NULL;
128                         def->cooked_default = NULL;
129                         def->constraints = NIL;
130
131                         attrList = lappend(attrList, def);
132                 }
133         }
134
135         if (attrList == NIL)
136                 ereport(ERROR,
137                                 (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
138                                  errmsg("view must have at least one column")));
139
140         /*
141          * Check to see if we want to replace an existing view.
142          */
143         namespaceId = RangeVarGetCreationNamespace(relation);
144         viewOid = get_relname_relid(relation->relname, namespaceId);
145
146         if (OidIsValid(viewOid) && replace)
147         {
148                 Relation        rel;
149                 TupleDesc       descriptor;
150
151                 /*
152                  * Yes.  Get exclusive lock on the existing view ...
153                  */
154                 rel = relation_open(viewOid, AccessExclusiveLock);
155
156                 /*
157                  * Make sure it *is* a view, and do permissions checks.
158                  */
159                 if (rel->rd_rel->relkind != RELKIND_VIEW)
160                         ereport(ERROR,
161                                         (errcode(ERRCODE_WRONG_OBJECT_TYPE),
162                                          errmsg("\"%s\" is not a view",
163                                                         RelationGetRelationName(rel))));
164
165                 if (!pg_class_ownercheck(viewOid, GetUserId()))
166                         aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_CLASS,
167                                                    RelationGetRelationName(rel));
168
169                 /*
170                  * Due to the namespace visibility rules for temporary objects, we
171                  * should only end up replacing a temporary view with another
172                  * temporary view, and vice versa.
173                  */
174                 Assert(relation->istemp == rel->rd_istemp);
175
176                 /*
177                  * Create a tuple descriptor to compare against the existing view, and
178                  * verify it matches.
179                  */
180                 descriptor = BuildDescForRelation(attrList);
181                 checkViewTupleDesc(descriptor, rel->rd_att);
182
183                 /*
184                  * Seems okay, so return the OID of the pre-existing view.
185                  */
186                 relation_close(rel, NoLock);    /* keep the lock! */
187
188                 return viewOid;
189         }
190         else
191         {
192                 /*
193                  * now set the parameters for keys/inheritance etc. All of these are
194                  * uninteresting for views...
195                  */
196                 createStmt->relation = (RangeVar *) relation;
197                 createStmt->tableElts = attrList;
198                 createStmt->inhRelations = NIL;
199                 createStmt->constraints = NIL;
200                 createStmt->options = list_make1(defWithOids(false));
201                 createStmt->oncommit = ONCOMMIT_NOOP;
202                 createStmt->tablespacename = NULL;
203
204                 /*
205                  * finally create the relation (this will error out if there's an
206                  * existing view, so we don't need more code to complain if "replace"
207                  * is false).
208                  */
209                 return DefineRelation(createStmt, RELKIND_VIEW);
210         }
211 }
212
213 /*
214  * Verify that tupledesc associated with proposed new view definition
215  * matches tupledesc of old view.  This is basically a cut-down version
216  * of equalTupleDescs(), with code added to generate specific complaints.
217  */
218 static void
219 checkViewTupleDesc(TupleDesc newdesc, TupleDesc olddesc)
220 {
221         int                     i;
222
223         if (newdesc->natts != olddesc->natts)
224                 ereport(ERROR,
225                                 (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
226                                  errmsg("cannot change number of columns in view")));
227         /* we can ignore tdhasoid */
228
229         for (i = 0; i < newdesc->natts; i++)
230         {
231                 Form_pg_attribute newattr = newdesc->attrs[i];
232                 Form_pg_attribute oldattr = olddesc->attrs[i];
233
234                 /* XXX not right, but we don't support DROP COL on view anyway */
235                 if (newattr->attisdropped != oldattr->attisdropped)
236                         ereport(ERROR,
237                                         (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
238                                          errmsg("cannot change number of columns in view")));
239
240                 if (strcmp(NameStr(newattr->attname), NameStr(oldattr->attname)) != 0)
241                         ereport(ERROR,
242                                         (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
243                                          errmsg("cannot change name of view column \"%s\"",
244                                                         NameStr(oldattr->attname))));
245                 /* XXX would it be safe to allow atttypmod to change?  Not sure */
246                 if (newattr->atttypid != oldattr->atttypid ||
247                         newattr->atttypmod != oldattr->atttypmod)
248                         ereport(ERROR,
249                                         (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
250                                          errmsg("cannot change data type of view column \"%s\"",
251                                                         NameStr(oldattr->attname))));
252                 /* We can ignore the remaining attributes of an attribute... */
253         }
254
255         /*
256          * We ignore the constraint fields.  The new view desc can't have any
257          * constraints, and the only ones that could be on the old view are
258          * defaults, which we are happy to leave in place.
259          */
260 }
261
262 static void
263 DefineViewRules(Oid viewOid, Query *viewParse, bool replace)
264 {
265         /*
266          * Set up the ON SELECT rule.  Since the query has already been through
267          * parse analysis, we use DefineQueryRewrite() directly.
268          */
269         DefineQueryRewrite(pstrdup(ViewSelectRuleName),
270                                            viewOid,
271                                            NULL,
272                                            CMD_SELECT,
273                                            true,
274                                            replace,
275                                            list_make1(viewParse));
276
277         /*
278          * Someday: automatic ON INSERT, etc
279          */
280 }
281
282 /*---------------------------------------------------------------
283  * UpdateRangeTableOfViewParse
284  *
285  * Update the range table of the given parsetree.
286  * This update consists of adding two new entries IN THE BEGINNING
287  * of the range table (otherwise the rule system will die a slow,
288  * horrible and painful death, and we do not want that now, do we?)
289  * one for the OLD relation and one for the NEW one (both of
290  * them refer in fact to the "view" relation).
291  *
292  * Of course we must also increase the 'varnos' of all the Var nodes
293  * by 2...
294  *
295  * These extra RT entries are not actually used in the query,
296  * except for run-time permission checking.
297  *---------------------------------------------------------------
298  */
299 static Query *
300 UpdateRangeTableOfViewParse(Oid viewOid, Query *viewParse)
301 {
302         Relation        viewRel;
303         List       *new_rt;
304         RangeTblEntry *rt_entry1,
305                            *rt_entry2;
306
307         /*
308          * Make a copy of the given parsetree.  It's not so much that we don't
309          * want to scribble on our input, it's that the parser has a bad habit of
310          * outputting multiple links to the same subtree for constructs like
311          * BETWEEN, and we mustn't have OffsetVarNodes increment the varno of a
312          * Var node twice.      copyObject will expand any multiply-referenced subtree
313          * into multiple copies.
314          */
315         viewParse = (Query *) copyObject(viewParse);
316
317         /* need to open the rel for addRangeTableEntryForRelation */
318         viewRel = relation_open(viewOid, AccessShareLock);
319
320         /*
321          * Create the 2 new range table entries and form the new range table...
322          * OLD first, then NEW....
323          */
324         rt_entry1 = addRangeTableEntryForRelation(NULL, viewRel,
325                                                                                           makeAlias("*OLD*", NIL),
326                                                                                           false, false);
327         rt_entry2 = addRangeTableEntryForRelation(NULL, viewRel,
328                                                                                           makeAlias("*NEW*", NIL),
329                                                                                           false, false);
330         /* Must override addRangeTableEntry's default access-check flags */
331         rt_entry1->requiredPerms = 0;
332         rt_entry2->requiredPerms = 0;
333
334         new_rt = lcons(rt_entry1, lcons(rt_entry2, viewParse->rtable));
335
336         viewParse->rtable = new_rt;
337
338         /*
339          * Now offset all var nodes by 2, and jointree RT indexes too.
340          */
341         OffsetVarNodes((Node *) viewParse, 2, 0);
342
343         relation_close(viewRel, AccessShareLock);
344
345         return viewParse;
346 }
347
348 /*
349  * DefineView
350  *              Execute a CREATE VIEW command.
351  */
352 void
353 DefineView(ViewStmt *stmt, const char *queryString)
354 {
355         Query      *viewParse;
356         Oid                     viewOid;
357         RangeVar   *view;
358
359         /*
360          * Run parse analysis to convert the raw parse tree to a Query.  Note this
361          * also acquires sufficient locks on the source table(s).
362          *
363          * Since parse analysis scribbles on its input, copy the raw parse tree;
364          * this ensures we don't corrupt a prepared statement, for example.
365          */
366         viewParse = parse_analyze((Node *) copyObject(stmt->query),
367                                                           queryString, NULL, 0);
368
369         /*
370          * The grammar should ensure that the result is a single SELECT Query.
371          */
372         if (!IsA(viewParse, Query) ||
373                 viewParse->commandType != CMD_SELECT)
374                 elog(ERROR, "unexpected parse analysis result");
375
376         /*
377          * If a list of column names was given, run through and insert these into
378          * the actual query tree. - thomas 2000-03-08
379          */
380         if (stmt->aliases != NIL)
381         {
382                 ListCell   *alist_item = list_head(stmt->aliases);
383                 ListCell   *targetList;
384
385                 foreach(targetList, viewParse->targetList)
386                 {
387                         TargetEntry *te = (TargetEntry *) lfirst(targetList);
388
389                         Assert(IsA(te, TargetEntry));
390                         /* junk columns don't get aliases */
391                         if (te->resjunk)
392                                 continue;
393                         te->resname = pstrdup(strVal(lfirst(alist_item)));
394                         alist_item = lnext(alist_item);
395                         if (alist_item == NULL)
396                                 break;                  /* done assigning aliases */
397                 }
398
399                 if (alist_item != NULL)
400                         ereport(ERROR,
401                                         (errcode(ERRCODE_SYNTAX_ERROR),
402                                          errmsg("CREATE VIEW specifies more column "
403                                                         "names than columns")));
404         }
405
406         /*
407          * If the user didn't explicitly ask for a temporary view, check whether
408          * we need one implicitly.      We allow TEMP to be inserted automatically as
409          * long as the CREATE command is consistent with that --- no explicit
410          * schema name.
411          */
412         view = stmt->view;
413         if (!view->istemp && isViewOnTempTable(viewParse))
414         {
415                 view = copyObject(view);        /* don't corrupt original command */
416                 view->istemp = true;
417                 ereport(NOTICE,
418                                 (errmsg("view \"%s\" will be a temporary view",
419                                                 view->relname)));
420         }
421
422         /*
423          * Create the view relation
424          *
425          * NOTE: if it already exists and replace is false, the xact will be
426          * aborted.
427          */
428         viewOid = DefineVirtualRelation(view, viewParse->targetList,
429                                                                         stmt->replace);
430
431         /*
432          * The relation we have just created is not visible to any other commands
433          * running with the same transaction & command id. So, increment the
434          * command id counter (but do NOT pfree any memory!!!!)
435          */
436         CommandCounterIncrement();
437
438         /*
439          * The range table of 'viewParse' does not contain entries for the "OLD"
440          * and "NEW" relations. So... add them!
441          */
442         viewParse = UpdateRangeTableOfViewParse(viewOid, viewParse);
443
444         /*
445          * Now create the rules associated with the view.
446          */
447         DefineViewRules(viewOid, viewParse, stmt->replace);
448 }
449
450 /*
451  * RemoveView
452  *
453  * Remove a view given its name
454  *
455  * We just have to drop the relation; the associated rules will be
456  * cleaned up automatically.
457  */
458 void
459 RemoveView(const RangeVar *view, DropBehavior behavior)
460 {
461         Oid                     viewOid;
462         ObjectAddress object;
463
464         viewOid = RangeVarGetRelid(view, false);
465
466         object.classId = RelationRelationId;
467         object.objectId = viewOid;
468         object.objectSubId = 0;
469
470         performDeletion(&object, behavior);
471 }