1
0
mirror of https://github.com/postgres/postgres.git synced 2025-11-12 05:01:15 +03:00

Implement DROP SCHEMA. It lacks support for dropping conversions and

operator classes, both of which are schema-local and so should really
be droppable.
This commit is contained in:
Tom Lane
2002-07-18 16:47:26 +00:00
parent 8bed350c4a
commit 11333426f1
16 changed files with 438 additions and 107 deletions

View File

@@ -8,7 +8,7 @@
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/catalog/dependency.c,v 1.3 2002/07/16 05:53:33 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/catalog/dependency.c,v 1.4 2002/07/18 16:47:22 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -25,12 +25,14 @@
#include "catalog/pg_constraint.h"
#include "catalog/pg_depend.h"
#include "catalog/pg_language.h"
#include "catalog/pg_namespace.h"
#include "catalog/pg_rewrite.h"
#include "catalog/pg_trigger.h"
#include "catalog/pg_type.h"
#include "commands/comment.h"
#include "commands/defrem.h"
#include "commands/proclang.h"
#include "commands/schemacmds.h"
#include "commands/trigger.h"
#include "lib/stringinfo.h"
#include "miscadmin.h"
@@ -54,6 +56,7 @@ typedef enum ObjectClasses
OCLASS_OPERATOR, /* pg_operator */
OCLASS_REWRITE, /* pg_rewrite */
OCLASS_TRIGGER, /* pg_trigger */
OCLASS_SCHEMA, /* pg_namespace */
MAX_OCLASS /* MUST BE LAST */
} ObjectClasses;
@@ -597,6 +600,10 @@ doDeletion(const ObjectAddress *object)
RemoveTriggerById(object->objectId);
break;
case OCLASS_SCHEMA:
RemoveSchemaById(object->objectId);
break;
default:
elog(ERROR, "doDeletion: Unsupported object class %u",
object->classId);
@@ -981,6 +988,7 @@ init_object_classes(void)
object_classes[OCLASS_OPERATOR] = get_system_catalog_relid(OperatorRelationName);
object_classes[OCLASS_REWRITE] = get_system_catalog_relid(RewriteRelationName);
object_classes[OCLASS_TRIGGER] = get_system_catalog_relid(TriggerRelationName);
object_classes[OCLASS_SCHEMA] = get_system_catalog_relid(NamespaceRelationName);
object_classes_initialized = true;
}
@@ -1045,6 +1053,11 @@ getObjectClass(const ObjectAddress *object)
Assert(object->objectSubId == 0);
return OCLASS_TRIGGER;
}
if (object->classId == object_classes[OCLASS_SCHEMA])
{
Assert(object->objectSubId == 0);
return OCLASS_SCHEMA;
}
elog(ERROR, "getObjectClass: Unknown object class %u",
object->classId);
@@ -1265,6 +1278,22 @@ getObjectDescription(const ObjectAddress *object)
break;
}
case OCLASS_SCHEMA:
{
HeapTuple schemaTup;
schemaTup = SearchSysCache(NAMESPACEOID,
ObjectIdGetDatum(object->objectId),
0, 0, 0);
if (!HeapTupleIsValid(schemaTup))
elog(ERROR, "getObjectDescription: Schema %u does not exist",
object->objectId);
appendStringInfo(&buffer, "schema %s",
NameStr(((Form_pg_namespace) GETSTRUCT(schemaTup))->nspname));
ReleaseSysCache(schemaTup);
break;
}
default:
appendStringInfo(&buffer, "unknown object %u %u %d",
object->classId,

View File

@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/catalog/heap.c,v 1.209 2002/07/16 22:12:18 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/catalog/heap.c,v 1.210 2002/07/18 16:47:22 tgl Exp $
*
*
* INTERFACE ROUTINES
@@ -742,6 +742,25 @@ heap_create_with_catalog(const char *relname,
AddNewAttributeTuples(new_rel_oid, new_rel_desc->rd_att,
relhasoids, relkind);
/*
* make a dependency link to force the relation to be deleted if
* its namespace is. Skip this in bootstrap mode, since we don't
* make dependencies while bootstrapping.
*/
if (!IsBootstrapProcessingMode())
{
ObjectAddress myself,
referenced;
myself.classId = RelOid_pg_class;
myself.objectId = new_rel_oid;
myself.objectSubId = 0;
referenced.classId = get_system_catalog_relid(NamespaceRelationName);
referenced.objectId = relnamespace;
referenced.objectSubId = 0;
recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
}
/*
* store constraints and defaults passed in the tupdesc, if any.
*

View File

@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/catalog/index.c,v 1.184 2002/07/16 05:53:33 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/catalog/index.c,v 1.185 2002/07/18 16:47:23 tgl Exp $
*
*
* INTERFACE ROUTINES
@@ -670,6 +670,9 @@ index_create(Oid heapRelationId,
* linked to the table. If it's not a CONSTRAINT, make the dependency
* directly on the table.
*
* We don't need a dependency on the namespace, because there'll be
* an indirect dependency via our parent table.
*
* During bootstrap we can't register any dependencies, and we don't
* try to make a constraint either.
*/

View File

@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/catalog/pg_operator.c,v 1.71 2002/07/16 22:12:18 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/catalog/pg_operator.c,v 1.72 2002/07/18 16:47:23 tgl Exp $
*
* NOTES
* these routines moved here from commands/define.c and somewhat cleaned up.
@@ -907,7 +907,7 @@ OperatorUpd(Oid baseId, Oid commId, Oid negId)
* Create dependencies for a new operator (either a freshly inserted
* complete operator, a new shell operator, or a just-updated shell).
*
* NB: the OidIsValid tests in this routine are *all* necessary, in case
* NB: the OidIsValid tests in this routine are necessary, in case
* the given operator is a shell.
*/
static void
@@ -924,6 +924,15 @@ makeOperatorDependencies(HeapTuple tuple, Oid pg_operator_relid)
/* In case we are updating a shell, delete any existing entries */
deleteDependencyRecordsFor(myself.classId, myself.objectId);
/* Dependency on namespace */
if (OidIsValid(oper->oprnamespace))
{
referenced.classId = get_system_catalog_relid(NamespaceRelationName);
referenced.objectId = oper->oprnamespace;
referenced.objectSubId = 0;
recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
}
/* Dependency on left type */
if (OidIsValid(oper->oprleft))
{

View File

@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/catalog/pg_proc.c,v 1.77 2002/07/16 22:12:19 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/catalog/pg_proc.c,v 1.78 2002/07/18 16:47:23 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -268,6 +268,12 @@ ProcedureCreate(const char *procedureName,
myself.objectId = retval;
myself.objectSubId = 0;
/* dependency on namespace */
referenced.classId = get_system_catalog_relid(NamespaceRelationName);
referenced.objectId = procNamespace;
referenced.objectSubId = 0;
recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
/* dependency on implementation language */
referenced.classId = get_system_catalog_relid(LanguageRelationName);
referenced.objectId = languageObjectId;

View File

@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/catalog/pg_type.c,v 1.73 2002/07/12 18:43:15 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/catalog/pg_type.c,v 1.74 2002/07/18 16:47:23 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -21,6 +21,7 @@
#include "catalog/pg_type.h"
#include "miscadmin.h"
#include "utils/builtins.h"
#include "utils/lsyscache.h"
#include "utils/syscache.h"
@@ -167,8 +168,6 @@ TypeCreate(const char *typeName,
NameData name;
TupleDesc tupDesc;
int i;
ObjectAddress myself,
referenced;
/*
* validate size specifications: either positive (fixed-length) or -1
@@ -302,74 +301,90 @@ TypeCreate(const char *typeName,
}
/*
* Create dependencies
* Create dependencies. We can/must skip this in bootstrap mode.
*/
myself.classId = RelOid_pg_type;
myself.objectId = typeObjectId;
myself.objectSubId = 0;
/* Normal dependencies on the I/O functions */
referenced.classId = RelOid_pg_proc;
referenced.objectId = inputProcedure;
referenced.objectSubId = 0;
recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
referenced.classId = RelOid_pg_proc;
referenced.objectId = outputProcedure;
referenced.objectSubId = 0;
recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
if (receiveProcedure != inputProcedure)
if (!IsBootstrapProcessingMode())
{
ObjectAddress myself,
referenced;
myself.classId = RelOid_pg_type;
myself.objectId = typeObjectId;
myself.objectSubId = 0;
/* dependency on namespace */
/* skip for relation rowtype, since we have indirect dependency */
if (!OidIsValid(relationOid))
{
referenced.classId = get_system_catalog_relid(NamespaceRelationName);
referenced.objectId = typeNamespace;
referenced.objectSubId = 0;
recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
}
/* Normal dependencies on the I/O functions */
referenced.classId = RelOid_pg_proc;
referenced.objectId = receiveProcedure;
referenced.objectId = inputProcedure;
referenced.objectSubId = 0;
recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
}
if (sendProcedure != outputProcedure)
{
referenced.classId = RelOid_pg_proc;
referenced.objectId = sendProcedure;
referenced.objectId = outputProcedure;
referenced.objectSubId = 0;
recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
}
/*
* If the type is a rowtype for a relation, mark it as internally
* dependent on the relation. This allows it to be auto-dropped
* when the relation is, and not otherwise.
*/
if (OidIsValid(relationOid))
{
referenced.classId = RelOid_pg_class;
referenced.objectId = relationOid;
referenced.objectSubId = 0;
recordDependencyOn(&myself, &referenced, DEPENDENCY_INTERNAL);
}
if (receiveProcedure != inputProcedure)
{
referenced.classId = RelOid_pg_proc;
referenced.objectId = receiveProcedure;
referenced.objectSubId = 0;
recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
}
/*
* If the type is an array type, mark it auto-dependent on the
* base type. (This is a compromise between the typical case where the
* array type is automatically generated and the case where it is manually
* created: we'd prefer INTERNAL for the former case and NORMAL for the
* latter.)
*/
if (OidIsValid(elementType))
{
referenced.classId = RelOid_pg_type;
referenced.objectId = elementType;
referenced.objectSubId = 0;
recordDependencyOn(&myself, &referenced, DEPENDENCY_AUTO);
}
if (sendProcedure != outputProcedure)
{
referenced.classId = RelOid_pg_proc;
referenced.objectId = sendProcedure;
referenced.objectSubId = 0;
recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
}
/* Normal dependency from a domain to its base type. */
if (OidIsValid(baseType))
{
referenced.classId = RelOid_pg_type;
referenced.objectId = baseType;
referenced.objectSubId = 0;
recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
/*
* If the type is a rowtype for a relation, mark it as internally
* dependent on the relation. This allows it to be auto-dropped
* when the relation is, and not otherwise.
*/
if (OidIsValid(relationOid))
{
referenced.classId = RelOid_pg_class;
referenced.objectId = relationOid;
referenced.objectSubId = 0;
recordDependencyOn(&myself, &referenced, DEPENDENCY_INTERNAL);
}
/*
* If the type is an array type, mark it auto-dependent on the base
* type. (This is a compromise between the typical case where the
* array type is automatically generated and the case where it is
* manually created: we'd prefer INTERNAL for the former case and
* NORMAL for the latter.)
*/
if (OidIsValid(elementType))
{
referenced.classId = RelOid_pg_type;
referenced.objectId = elementType;
referenced.objectSubId = 0;
recordDependencyOn(&myself, &referenced, DEPENDENCY_AUTO);
}
/* Normal dependency from a domain to its base type. */
if (OidIsValid(baseType))
{
referenced.classId = RelOid_pg_type;
referenced.objectId = baseType;
referenced.objectSubId = 0;
recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
}
}
/*

View File

@@ -1,20 +1,23 @@
/*-------------------------------------------------------------------------
*
* schemacmds.c
* schema creation command support code
* schema creation/manipulation commands
*
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/commands/schemacmds.c,v 1.4 2002/06/11 13:40:50 wieck Exp $
* $Header: /cvsroot/pgsql/src/backend/commands/schemacmds.c,v 1.5 2002/07/18 16:47:24 tgl Exp $
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "access/heapam.h"
#include "catalog/catalog.h"
#include "catalog/catname.h"
#include "catalog/dependency.h"
#include "catalog/namespace.h"
#include "catalog/pg_namespace.h"
#include "commands/schemacmds.h"
@@ -23,6 +26,7 @@
#include "tcop/utility.h"
#include "utils/acl.h"
#include "utils/lsyscache.h"
#include "utils/syscache.h"
/*
@@ -139,3 +143,70 @@ CreateSchemaCommand(CreateSchemaStmt *stmt)
/* Reset current user */
SetUserId(saved_userid);
}
/*
* RemoveSchema
* Removes a schema.
*/
void
RemoveSchema(List *names, DropBehavior behavior)
{
char *namespaceName;
Oid namespaceId;
ObjectAddress object;
if (length(names) != 1)
elog(ERROR, "Schema name may not be qualified");
namespaceName = strVal(lfirst(names));
namespaceId = GetSysCacheOid(NAMESPACENAME,
CStringGetDatum(namespaceName),
0, 0, 0);
if (!OidIsValid(namespaceId))
elog(ERROR, "Schema \"%s\" does not exist", namespaceName);
/* Permission check */
if (!pg_namespace_ownercheck(namespaceId, GetUserId()))
aclcheck_error(ACLCHECK_NOT_OWNER, namespaceName);
/*
* Do the deletion. Objects contained in the schema are removed
* by means of their dependency links to the schema.
*
* XXX currently, index opclasses don't have creation/deletion
* commands, so they will not get removed when the containing
* schema is removed. This is annoying but not fatal.
*/
object.classId = get_system_catalog_relid(NamespaceRelationName);
object.objectId = namespaceId;
object.objectSubId = 0;
performDeletion(&object, behavior);
}
/*
* Guts of schema deletion.
*/
void
RemoveSchemaById(Oid schemaOid)
{
Relation relation;
HeapTuple tup;
relation = heap_openr(NamespaceRelationName, RowExclusiveLock);
tup = SearchSysCache(NAMESPACEOID,
ObjectIdGetDatum(schemaOid),
0, 0, 0);
if (!HeapTupleIsValid(tup))
elog(ERROR, "RemoveSchemaById: schema %u not found",
schemaOid);
simple_heap_delete(relation, &tup->t_self);
ReleaseSysCache(tup);
heap_close(relation, RowExclusiveLock);
}

View File

@@ -11,7 +11,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/parser/gram.y,v 2.344 2002/07/18 04:43:50 momjian Exp $
* $Header: /cvsroot/pgsql/src/backend/parser/gram.y,v 2.345 2002/07/18 16:47:24 tgl Exp $
*
* HISTORY
* AUTHOR DATE MAJOR EVENT
@@ -132,7 +132,7 @@ static void doNegateFloat(Value *v);
}
%type <node> stmt, schema_stmt,
AlterDatabaseSetStmt, AlterGroupStmt, AlterSchemaStmt,
AlterDatabaseSetStmt, AlterGroupStmt,
AlterTableStmt, AlterUserStmt, AlterUserSetStmt,
AnalyzeStmt, ClosePortalStmt, ClusterStmt, CommentStmt,
ConstraintsSetStmt, CopyStmt, CreateAsStmt,
@@ -140,7 +140,7 @@ static void doNegateFloat(Value *v);
CreateSchemaStmt, CreateSeqStmt, CreateStmt,
CreateAssertStmt, CreateTrigStmt, CreateUserStmt,
CreatedbStmt, CursorStmt, DefineStmt, DeleteStmt,
DropGroupStmt, DropPLangStmt, DropSchemaStmt, DropStmt,
DropGroupStmt, DropPLangStmt, DropStmt,
DropAssertStmt, DropTrigStmt, DropRuleStmt,
DropUserStmt, DropdbStmt, ExplainStmt, FetchStmt,
GrantStmt, IndexStmt, InsertStmt, ListenStmt, LoadStmt,
@@ -468,7 +468,6 @@ stmtmulti: stmtmulti ';' stmt
stmt :
AlterDatabaseSetStmt
| AlterGroupStmt
| AlterSchemaStmt
| AlterTableStmt
| AlterUserStmt
| AlterUserSetStmt
@@ -488,7 +487,6 @@ stmt :
| ClusterStmt
| DefineStmt
| DropStmt
| DropSchemaStmt
| TruncateStmt
| CommentStmt
| DropGroupStmt
@@ -746,7 +744,6 @@ DropGroupStmt:
*
* Manipulate a schema
*
*
*****************************************************************************/
CreateSchemaStmt:
@@ -773,20 +770,6 @@ CreateSchemaStmt:
}
;
AlterSchemaStmt:
ALTER SCHEMA ColId
{
elog(ERROR, "ALTER SCHEMA not yet supported");
}
;
DropSchemaStmt:
DROP SCHEMA ColId opt_drop_behavior
{
elog(ERROR, "DROP SCHEMA not yet supported");
}
;
OptSchemaName:
ColId { $$ = $1; }
| /* EMPTY */ { $$ = NULL; }
@@ -2306,6 +2289,7 @@ drop_type: TABLE { $$ = DROP_TABLE; }
| TYPE_P { $$ = DROP_TYPE; }
| DOMAIN_P { $$ = DROP_DOMAIN; }
| CONVERSION_P { $$ = DROP_CONVERSION; }
| SCHEMA { $$ = DROP_SCHEMA; }
;
any_name_list:

View File

@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/tcop/postgres.c,v 1.270 2002/07/13 01:02:14 momjian Exp $
* $Header: /cvsroot/pgsql/src/backend/tcop/postgres.c,v 1.271 2002/07/18 16:47:25 tgl Exp $
*
* NOTES
* this is the "main" module of the postgres backend and
@@ -1693,7 +1693,7 @@ PostgresMain(int argc, char *argv[], const char *username)
if (!IsUnderPostmaster)
{
puts("\nPOSTGRES backend interactive interface ");
puts("$Revision: 1.270 $ $Date: 2002/07/13 01:02:14 $\n");
puts("$Revision: 1.271 $ $Date: 2002/07/18 16:47:25 $\n");
}
/*
@@ -2220,7 +2220,10 @@ CreateCommandTag(Node *parsetree)
tag = "DROP DOMAIN";
break;
case DROP_CONVERSION:
tag = "DROP CONVERSON";
tag = "DROP CONVERSION";
break;
case DROP_SCHEMA:
tag = "DROP SCHEMA";
break;
default:
tag = "???";

View File

@@ -10,7 +10,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/tcop/utility.c,v 1.162 2002/07/12 18:43:17 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/tcop/utility.c,v 1.163 2002/07/18 16:47:25 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -252,11 +252,7 @@ ProcessUtility(Node *parsetree,
* relation and attribute manipulation
*/
case T_CreateSchemaStmt:
{
CreateSchemaStmt *stmt = (CreateSchemaStmt *) parsetree;
CreateSchemaCommand(stmt);
}
CreateSchemaCommand((CreateSchemaStmt *) parsetree);
break;
case T_CreateStmt:
@@ -322,17 +318,20 @@ ProcessUtility(Node *parsetree,
break;
case DROP_CONVERSION:
/* RemoveDomain does its own permissions checks */
/* does its own permissions checks */
DropConversionCommand(names);
break;
case DROP_SCHEMA:
/* RemoveSchema does its own permissions checks */
RemoveSchema(names, stmt->behavior);
break;
}
/*
* Make sure subsequent loop iterations will see
* results of this one; needed if removing multiple
* rules for same table, for example.
* We used to need to do CommandCounterIncrement()
* here, but now it's done inside performDeletion().
*/
CommandCounterIncrement();
}
}
break;