mirror of
https://github.com/postgres/postgres.git
synced 2025-07-28 23:42:10 +03:00
Rule names are now unique per-relation, rather than unique globally.
DROP RULE and COMMENT ON RULE syntax adds an 'ON tablename' clause, similar to TRIGGER syntaxes. To allow loading of existing pg_dump files containing COMMENT ON RULE, the COMMENT code will still accept the old syntax --- but only if the target rulename is unique across the whole database.
This commit is contained in:
@ -8,7 +8,7 @@
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $Header: /cvsroot/pgsql/src/backend/rewrite/rewriteDefine.c,v 1.66 2002/03/26 19:16:02 tgl Exp $
|
||||
* $Header: /cvsroot/pgsql/src/backend/rewrite/rewriteDefine.c,v 1.67 2002/04/18 20:01:09 tgl Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@ -37,7 +37,7 @@ static bool setRuleCheckAsUser_walker(Node *node, Oid *context);
|
||||
|
||||
/*
|
||||
* InsertRule -
|
||||
* takes the arguments and inserts them as attributes into the system
|
||||
* takes the arguments and inserts them as a row into the system
|
||||
* relation "pg_rewrite"
|
||||
*/
|
||||
static Oid
|
||||
@ -58,7 +58,7 @@ InsertRule(char *rulname,
|
||||
HeapTuple tup;
|
||||
Oid rewriteObjectId;
|
||||
|
||||
if (IsDefinedRewriteRule(rulname))
|
||||
if (IsDefinedRewriteRule(eventrel_oid, rulname))
|
||||
elog(ERROR, "Attempt to insert rule \"%s\" failed: already exists",
|
||||
rulname);
|
||||
|
||||
@ -69,13 +69,13 @@ InsertRule(char *rulname,
|
||||
|
||||
i = 0;
|
||||
namestrcpy(&rname, rulname);
|
||||
values[i++] = NameGetDatum(&rname);
|
||||
values[i++] = CharGetDatum(evtype + '0');
|
||||
values[i++] = ObjectIdGetDatum(eventrel_oid);
|
||||
values[i++] = Int16GetDatum(evslot_index);
|
||||
values[i++] = BoolGetDatum(evinstead);
|
||||
values[i++] = DirectFunctionCall1(textin, CStringGetDatum(evqual));
|
||||
values[i++] = DirectFunctionCall1(textin, CStringGetDatum(actiontree));
|
||||
values[i++] = NameGetDatum(&rname); /* rulename */
|
||||
values[i++] = ObjectIdGetDatum(eventrel_oid); /* ev_class */
|
||||
values[i++] = Int16GetDatum(evslot_index); /* ev_attr */
|
||||
values[i++] = CharGetDatum(evtype + '0'); /* ev_type */
|
||||
values[i++] = BoolGetDatum(evinstead); /* is_instead */
|
||||
values[i++] = DirectFunctionCall1(textin, CStringGetDatum(evqual)); /* ev_qual */
|
||||
values[i++] = DirectFunctionCall1(textin, CStringGetDatum(actiontree)); /* ev_action */
|
||||
|
||||
/*
|
||||
* create a new pg_rewrite tuple
|
||||
@ -423,26 +423,27 @@ setRuleCheckAsUser_walker(Node *node, Oid *context)
|
||||
* ON SELECT rule associated with a view, when the view is renamed.
|
||||
*/
|
||||
void
|
||||
RenameRewriteRule(char *oldname, char *newname)
|
||||
RenameRewriteRule(Oid owningRel, const char *oldName,
|
||||
const char *newName)
|
||||
{
|
||||
Relation pg_rewrite_desc;
|
||||
HeapTuple ruletup;
|
||||
|
||||
pg_rewrite_desc = heap_openr(RewriteRelationName, RowExclusiveLock);
|
||||
|
||||
ruletup = SearchSysCacheCopy(RULENAME,
|
||||
PointerGetDatum(oldname),
|
||||
0, 0, 0);
|
||||
ruletup = SearchSysCacheCopy(RULERELNAME,
|
||||
ObjectIdGetDatum(owningRel),
|
||||
PointerGetDatum(oldName),
|
||||
0, 0);
|
||||
if (!HeapTupleIsValid(ruletup))
|
||||
elog(ERROR, "RenameRewriteRule: rule \"%s\" does not exist", oldname);
|
||||
elog(ERROR, "RenameRewriteRule: rule \"%s\" does not exist", oldName);
|
||||
|
||||
/* should not already exist */
|
||||
if (IsDefinedRewriteRule(newname))
|
||||
if (IsDefinedRewriteRule(owningRel, newName))
|
||||
elog(ERROR, "Attempt to rename rule \"%s\" failed: \"%s\" already exists",
|
||||
oldname, newname);
|
||||
oldName, newName);
|
||||
|
||||
StrNCpy(NameStr(((Form_pg_rewrite) GETSTRUCT(ruletup))->rulename),
|
||||
newname, NAMEDATALEN);
|
||||
namestrcpy(&(((Form_pg_rewrite) GETSTRUCT(ruletup))->rulename), newName);
|
||||
|
||||
simple_heap_update(pg_rewrite_desc, &ruletup->t_self, ruletup);
|
||||
|
||||
|
@ -8,15 +8,16 @@
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $Header: /cvsroot/pgsql/src/backend/rewrite/rewriteRemove.c,v 1.47 2002/03/29 19:06:13 tgl Exp $
|
||||
* $Header: /cvsroot/pgsql/src/backend/rewrite/rewriteRemove.c,v 1.48 2002/04/18 20:01:09 tgl Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
#include "postgres.h"
|
||||
|
||||
#include "utils/builtins.h"
|
||||
#include "access/genam.h"
|
||||
#include "access/heapam.h"
|
||||
#include "catalog/catname.h"
|
||||
#include "catalog/indexing.h"
|
||||
#include "catalog/pg_rewrite.h"
|
||||
#include "commands/comment.h"
|
||||
#include "miscadmin.h"
|
||||
@ -30,12 +31,11 @@
|
||||
/*
|
||||
* RemoveRewriteRule
|
||||
*
|
||||
* Delete a rule given its (possibly qualified) rulename.
|
||||
* Delete a rule given its name.
|
||||
*/
|
||||
void
|
||||
RemoveRewriteRule(List *names)
|
||||
RemoveRewriteRule(Oid owningRel, const char *ruleName)
|
||||
{
|
||||
char *ruleName;
|
||||
Relation RewriteRelation;
|
||||
Relation event_relation;
|
||||
HeapTuple tuple;
|
||||
@ -44,13 +44,6 @@ RemoveRewriteRule(List *names)
|
||||
bool hasMoreRules;
|
||||
int32 aclcheck_result;
|
||||
|
||||
/*
|
||||
* XXX temporary until rules become schema-tized
|
||||
*/
|
||||
if (length(names) != 1)
|
||||
elog(ERROR, "Qualified rule names not supported yet");
|
||||
ruleName = strVal(lfirst(names));
|
||||
|
||||
/*
|
||||
* Open the pg_rewrite relation.
|
||||
*/
|
||||
@ -59,9 +52,10 @@ RemoveRewriteRule(List *names)
|
||||
/*
|
||||
* Find the tuple for the target rule.
|
||||
*/
|
||||
tuple = SearchSysCacheCopy(RULENAME,
|
||||
tuple = SearchSysCacheCopy(RULERELNAME,
|
||||
ObjectIdGetDatum(owningRel),
|
||||
PointerGetDatum(ruleName),
|
||||
0, 0, 0);
|
||||
0, 0);
|
||||
|
||||
/*
|
||||
* complain if no rule with such name existed
|
||||
@ -75,6 +69,7 @@ RemoveRewriteRule(List *names)
|
||||
*/
|
||||
ruleId = tuple->t_data->t_oid;
|
||||
eventRelationOid = ((Form_pg_rewrite) GETSTRUCT(tuple))->ev_class;
|
||||
Assert(eventRelationOid == owningRel);
|
||||
|
||||
/*
|
||||
* We had better grab AccessExclusiveLock so that we know no other
|
||||
@ -137,10 +132,10 @@ RemoveRewriteRule(List *names)
|
||||
void
|
||||
RelationRemoveRules(Oid relid)
|
||||
{
|
||||
Relation RewriteRelation = NULL;
|
||||
HeapScanDesc scanDesc = NULL;
|
||||
Relation RewriteRelation;
|
||||
SysScanDesc scanDesc;
|
||||
ScanKeyData scanKeyData;
|
||||
HeapTuple tuple = NULL;
|
||||
HeapTuple tuple;
|
||||
|
||||
/*
|
||||
* Open the pg_rewrite relation.
|
||||
@ -148,18 +143,21 @@ RelationRemoveRules(Oid relid)
|
||||
RewriteRelation = heap_openr(RewriteRelationName, RowExclusiveLock);
|
||||
|
||||
/*
|
||||
* Scan the RuleRelation ('pg_rewrite') for all the tuples that has
|
||||
* the same ev_class as relid (the relation to be removed).
|
||||
* Scan pg_rewrite for all the tuples that have the same ev_class
|
||||
* as relid (the relation to be removed).
|
||||
*/
|
||||
ScanKeyEntryInitialize(&scanKeyData,
|
||||
0,
|
||||
Anum_pg_rewrite_ev_class,
|
||||
F_OIDEQ,
|
||||
ObjectIdGetDatum(relid));
|
||||
scanDesc = heap_beginscan(RewriteRelation,
|
||||
0, SnapshotNow, 1, &scanKeyData);
|
||||
|
||||
while (HeapTupleIsValid(tuple = heap_getnext(scanDesc, 0)))
|
||||
scanDesc = systable_beginscan(RewriteRelation,
|
||||
RewriteRelRulenameIndex,
|
||||
true, SnapshotNow,
|
||||
1, &scanKeyData);
|
||||
|
||||
while (HeapTupleIsValid(tuple = systable_getnext(scanDesc)))
|
||||
{
|
||||
/* Delete any comments associated with this rule */
|
||||
DeleteComments(tuple->t_data->t_oid, RelationGetRelid(RewriteRelation));
|
||||
@ -167,6 +165,7 @@ RelationRemoveRules(Oid relid)
|
||||
simple_heap_delete(RewriteRelation, &tuple->t_self);
|
||||
}
|
||||
|
||||
heap_endscan(scanDesc);
|
||||
systable_endscan(scanDesc);
|
||||
|
||||
heap_close(RewriteRelation, RowExclusiveLock);
|
||||
}
|
||||
|
@ -8,7 +8,7 @@
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $Header: /cvsroot/pgsql/src/backend/rewrite/rewriteSupport.c,v 1.49 2001/08/12 21:35:19 tgl Exp $
|
||||
* $Header: /cvsroot/pgsql/src/backend/rewrite/rewriteSupport.c,v 1.50 2002/04/18 20:01:09 tgl Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@ -29,11 +29,12 @@
|
||||
* Is there a rule by the given name?
|
||||
*/
|
||||
bool
|
||||
IsDefinedRewriteRule(const char *ruleName)
|
||||
IsDefinedRewriteRule(Oid owningRel, const char *ruleName)
|
||||
{
|
||||
return SearchSysCacheExists(RULENAME,
|
||||
return SearchSysCacheExists(RULERELNAME,
|
||||
ObjectIdGetDatum(owningRel),
|
||||
PointerGetDatum(ruleName),
|
||||
0, 0, 0);
|
||||
0, 0);
|
||||
}
|
||||
|
||||
/*
|
||||
|
Reference in New Issue
Block a user