1996-07-09 08:22:35 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
1999-02-14 00:22:53 +01:00
|
|
|
* view.c
|
1997-09-07 07:04:48 +02:00
|
|
|
* use rewrite rules to construct views
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2001-01-24 20:43:33 +01:00
|
|
|
* Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
|
2000-01-26 06:58:53 +01:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2002-03-29 20:06:29 +01:00
|
|
|
* $Id: view.c,v 1.61 2002/03/29 19:06:08 tgl Exp $
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
1999-07-16 01:04:24 +02:00
|
|
|
#include "postgres.h"
|
1996-11-06 09:21:43 +01:00
|
|
|
|
1999-07-16 01:04:24 +02:00
|
|
|
#include "access/xact.h"
|
1999-07-16 07:00:38 +02:00
|
|
|
#include "catalog/heap.h"
|
2002-03-29 20:06:29 +01:00
|
|
|
#include "catalog/namespace.h"
|
1999-07-16 07:00:38 +02:00
|
|
|
#include "commands/creatinh.h"
|
|
|
|
#include "commands/view.h"
|
2000-07-04 08:11:54 +02:00
|
|
|
#include "miscadmin.h"
|
2000-02-15 04:38:29 +01:00
|
|
|
#include "nodes/makefuncs.h"
|
1999-07-16 01:04:24 +02:00
|
|
|
#include "parser/parse_relation.h"
|
|
|
|
#include "parser/parse_type.h"
|
|
|
|
#include "rewrite/rewriteDefine.h"
|
|
|
|
#include "rewrite/rewriteManip.h"
|
|
|
|
#include "rewrite/rewriteRemove.h"
|
2001-08-12 23:35:19 +02:00
|
|
|
#include "rewrite/rewriteSupport.h"
|
2002-03-29 20:06:29 +01:00
|
|
|
#include "utils/syscache.h"
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2000-10-26 19:04:12 +02:00
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
/*---------------------------------------------------------------------
|
|
|
|
* DefineVirtualRelation
|
|
|
|
*
|
|
|
|
* Create the "view" relation.
|
|
|
|
* `DefineRelation' does all the work, we just provide the correct
|
|
|
|
* arguments!
|
|
|
|
*
|
|
|
|
* If the relation already exists, then 'DefineRelation' will abort
|
|
|
|
* the xact...
|
|
|
|
*---------------------------------------------------------------------
|
|
|
|
*/
|
2002-03-22 03:56:37 +01:00
|
|
|
static Oid
|
2002-03-29 20:06:29 +01:00
|
|
|
DefineVirtualRelation(const RangeVar *relation, List *tlist)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2000-09-29 20:21:41 +02:00
|
|
|
CreateStmt *createStmt = makeNode(CreateStmt);
|
1997-09-08 04:41:22 +02:00
|
|
|
List *attrList,
|
|
|
|
*t;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/*
|
2000-09-29 20:21:41 +02:00
|
|
|
* create a list of ColumnDef nodes based on the names and types of
|
|
|
|
* the (non-junk) targetlist items from the view's SELECT list.
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
|
|
|
attrList = NIL;
|
2000-09-29 20:21:41 +02:00
|
|
|
foreach(t, tlist)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2000-09-29 20:21:41 +02:00
|
|
|
TargetEntry *entry = lfirst(t);
|
|
|
|
Resdom *res = entry->resdom;
|
|
|
|
|
2001-03-22 05:01:46 +01:00
|
|
|
if (!res->resjunk)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
ColumnDef *def = makeNode(ColumnDef);
|
2000-09-29 20:21:41 +02:00
|
|
|
TypeName *typename = makeNode(TypeName);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2002-03-29 20:06:29 +01:00
|
|
|
def->colname = pstrdup(res->resname);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2002-03-29 20:06:29 +01:00
|
|
|
typename->typeid = res->restype;
|
1998-02-10 05:02:59 +01:00
|
|
|
typename->typmod = res->restypmod;
|
1997-09-07 07:04:48 +02:00
|
|
|
def->typename = typename;
|
|
|
|
|
|
|
|
def->is_not_null = false;
|
1999-10-04 01:55:40 +02:00
|
|
|
def->raw_default = NULL;
|
|
|
|
def->cooked_default = NULL;
|
2000-09-29 20:21:41 +02:00
|
|
|
def->constraints = NIL;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
attrList = lappend(attrList, def);
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
2000-09-29 20:21:41 +02:00
|
|
|
|
|
|
|
if (attrList == NIL)
|
1998-01-05 17:40:20 +01:00
|
|
|
elog(ERROR, "attempted to define virtual relation with no attrs");
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/*
|
2000-09-29 20:21:41 +02:00
|
|
|
* now create the parameters for keys/inheritance etc. All of them are
|
1997-09-07 07:04:48 +02:00
|
|
|
* nil...
|
|
|
|
*/
|
2002-03-29 20:06:29 +01:00
|
|
|
createStmt->relation = (RangeVar *) relation;
|
2000-09-29 20:21:41 +02:00
|
|
|
createStmt->tableElts = attrList;
|
2002-03-21 17:02:16 +01:00
|
|
|
createStmt->inhRelations = NIL;
|
2000-09-29 20:21:41 +02:00
|
|
|
createStmt->constraints = NIL;
|
2001-08-10 20:57:42 +02:00
|
|
|
createStmt->hasoids = false;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* finally create the relation...
|
|
|
|
*/
|
2002-03-22 03:56:37 +01:00
|
|
|
return DefineRelation(createStmt, RELKIND_VIEW);
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
|
|
static RuleStmt *
|
2002-03-29 20:06:29 +01:00
|
|
|
FormViewRetrieveRule(const RangeVar *view, Query *viewParse)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
RuleStmt *rule;
|
|
|
|
char *rname;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Create a RuleStmt that corresponds to the suitable rewrite rule
|
|
|
|
* args for DefineQueryRewrite();
|
|
|
|
*/
|
2002-03-29 20:06:29 +01:00
|
|
|
rname = MakeRetrieveViewRuleName(view->relname);
|
2002-03-21 17:02:16 +01:00
|
|
|
|
|
|
|
rule = makeNode(RuleStmt);
|
2002-03-29 20:06:29 +01:00
|
|
|
rule->relation = copyObject((RangeVar *) view);
|
1997-09-07 07:04:48 +02:00
|
|
|
rule->rulename = pstrdup(rname);
|
|
|
|
rule->whereClause = NULL;
|
|
|
|
rule->event = CMD_SELECT;
|
|
|
|
rule->instead = true;
|
2000-09-29 20:21:41 +02:00
|
|
|
rule->actions = makeList1(viewParse);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
return rule;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2002-03-29 20:06:29 +01:00
|
|
|
DefineViewRules(const RangeVar *view, Query *viewParse)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2000-12-21 18:36:15 +01:00
|
|
|
RuleStmt *retrieve_rule;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
#ifdef NOTYET
|
2000-12-21 18:36:15 +01:00
|
|
|
RuleStmt *replace_rule;
|
|
|
|
RuleStmt *append_rule;
|
|
|
|
RuleStmt *delete_rule;
|
1996-07-09 08:22:35 +02:00
|
|
|
#endif
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2002-03-29 20:06:29 +01:00
|
|
|
retrieve_rule = FormViewRetrieveRule(view, viewParse);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
#ifdef NOTYET
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2002-03-29 20:06:29 +01:00
|
|
|
replace_rule = FormViewReplaceRule(view, viewParse);
|
|
|
|
append_rule = FormViewAppendRule(view, viewParse);
|
|
|
|
delete_rule = FormViewDeleteRule(view, viewParse);
|
1996-07-09 08:22:35 +02:00
|
|
|
#endif
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
DefineQueryRewrite(retrieve_rule);
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
|
|
#ifdef NOTYET
|
1997-09-07 07:04:48 +02:00
|
|
|
DefineQueryRewrite(replace_rule);
|
|
|
|
DefineQueryRewrite(append_rule);
|
|
|
|
DefineQueryRewrite(delete_rule);
|
1996-07-09 08:22:35 +02:00
|
|
|
#endif
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
|
|
/*---------------------------------------------------------------
|
|
|
|
* UpdateRangeTableOfViewParse
|
|
|
|
*
|
|
|
|
* Update the range table of the given parsetree.
|
|
|
|
* This update consists of adding two new entries IN THE BEGINNING
|
|
|
|
* of the range table (otherwise the rule system will die a slow,
|
|
|
|
* horrible and painful death, and we do not want that now, do we?)
|
2000-06-12 21:40:58 +02:00
|
|
|
* one for the OLD relation and one for the NEW one (both of
|
1996-07-09 08:22:35 +02:00
|
|
|
* them refer in fact to the "view" relation).
|
|
|
|
*
|
|
|
|
* Of course we must also increase the 'varnos' of all the Var nodes
|
|
|
|
* by 2...
|
|
|
|
*
|
2000-12-21 18:36:15 +01:00
|
|
|
* These extra RT entries are not actually used in the query,
|
|
|
|
* except for run-time permission checking.
|
1996-07-09 08:22:35 +02:00
|
|
|
*---------------------------------------------------------------
|
|
|
|
*/
|
2000-12-21 18:36:15 +01:00
|
|
|
static Query *
|
2002-03-22 03:56:37 +01:00
|
|
|
UpdateRangeTableOfViewParse(Oid viewOid, Query *viewParse)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
List *new_rt;
|
|
|
|
RangeTblEntry *rt_entry1,
|
|
|
|
*rt_entry2;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/*
|
2001-03-22 05:01:46 +01:00
|
|
|
* Make a copy of the given parsetree. It's not so much that we don't
|
|
|
|
* want to scribble on our input, it's that the parser has a bad habit
|
|
|
|
* of outputting multiple links to the same subtree for constructs
|
|
|
|
* like BETWEEN, and we mustn't have OffsetVarNodes increment the
|
|
|
|
* varno of a Var node twice. copyObject will expand any
|
|
|
|
* multiply-referenced subtree into multiple copies.
|
2000-12-21 18:36:15 +01:00
|
|
|
*/
|
|
|
|
viewParse = (Query *) copyObject(viewParse);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create the 2 new range table entries and form the new range
|
2000-06-12 21:40:58 +02:00
|
|
|
* table... OLD first, then NEW....
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
2002-03-22 03:56:37 +01:00
|
|
|
rt_entry1 = addRangeTableEntryForRelation(NULL, viewOid,
|
|
|
|
makeAlias("*OLD*", NIL),
|
|
|
|
false, false);
|
|
|
|
rt_entry2 = addRangeTableEntryForRelation(NULL, viewOid,
|
|
|
|
makeAlias("*NEW*", NIL),
|
|
|
|
false, false);
|
2000-09-29 20:21:41 +02:00
|
|
|
/* Must override addRangeTableEntry's default access-check flags */
|
|
|
|
rt_entry1->checkForRead = false;
|
|
|
|
rt_entry2->checkForRead = false;
|
|
|
|
|
2000-09-12 23:07:18 +02:00
|
|
|
new_rt = lcons(rt_entry1, lcons(rt_entry2, viewParse->rtable));
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
viewParse->rtable = new_rt;
|
2000-09-12 23:07:18 +02:00
|
|
|
|
|
|
|
/*
|
2000-12-21 18:36:15 +01:00
|
|
|
* Now offset all var nodes by 2, and jointree RT indexes too.
|
2000-09-12 23:07:18 +02:00
|
|
|
*/
|
|
|
|
OffsetVarNodes((Node *) viewParse, 2, 0);
|
2000-12-21 18:36:15 +01:00
|
|
|
|
|
|
|
return viewParse;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------
|
|
|
|
* DefineView
|
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* - takes a "viewname", "parsetree" pair and then
|
|
|
|
* 1) construct the "virtual" relation
|
|
|
|
* 2) commit the command but NOT the transaction,
|
|
|
|
* so that the relation exists
|
|
|
|
* before the rules are defined.
|
|
|
|
* 2) define the "n" rules specified in the PRS2 paper
|
|
|
|
* over the "virtual" relation
|
1996-07-09 08:22:35 +02:00
|
|
|
*-------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
void
|
2002-03-29 20:06:29 +01:00
|
|
|
DefineView(const RangeVar *view, Query *viewParse)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2002-03-22 03:56:37 +01:00
|
|
|
Oid viewOid;
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/*
|
2002-03-22 03:56:37 +01:00
|
|
|
* Create the view relation
|
|
|
|
*
|
|
|
|
* NOTE: if it already exists, the xact will be aborted.
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
2002-03-29 20:06:29 +01:00
|
|
|
viewOid = DefineVirtualRelation(view, viewParse->targetList);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The relation we have just created is not visible to any other
|
|
|
|
* commands running with the same transaction & command id. So,
|
|
|
|
* increment the command id counter (but do NOT pfree any memory!!!!)
|
|
|
|
*/
|
|
|
|
CommandCounterIncrement();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The range table of 'viewParse' does not contain entries for the
|
2000-12-21 18:36:15 +01:00
|
|
|
* "OLD" and "NEW" relations. So... add them!
|
|
|
|
*/
|
2002-03-22 03:56:37 +01:00
|
|
|
viewParse = UpdateRangeTableOfViewParse(viewOid, viewParse);
|
2000-12-21 18:36:15 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Now create the rules associated with the view.
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
2002-03-29 20:06:29 +01:00
|
|
|
DefineViewRules(view, viewParse);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*------------------------------------------------------------------
|
|
|
|
* RemoveView
|
|
|
|
*
|
|
|
|
* Remove a view given its name
|
|
|
|
*------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
void
|
2002-03-29 20:06:29 +01:00
|
|
|
RemoveView(const RangeVar *view)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2002-03-29 20:06:29 +01:00
|
|
|
Oid viewOid;
|
|
|
|
|
|
|
|
viewOid = RangeVarGetRelid(view, false);
|
1997-09-07 07:04:48 +02:00
|
|
|
/*
|
2001-03-22 05:01:46 +01:00
|
|
|
* We just have to drop the relation; the associated rules will be
|
|
|
|
* cleaned up automatically.
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
2002-03-29 20:06:29 +01:00
|
|
|
heap_drop_with_catalog(viewOid, allowSystemTableMods);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|