postgresql/src/backend/commands/alter.c

425 lines
11 KiB
C
Raw Normal View History

2003-06-27 16:45:32 +02:00
/*-------------------------------------------------------------------------
*
* alter.c
* Drivers for generic alter commands
*
2010-01-02 17:58:17 +01:00
* Portions Copyright (c) 1996-2010, PostgreSQL Global Development Group
2003-06-27 16:45:32 +02:00
* Portions Copyright (c) 1994, Regents of the University of California
*
*
* IDENTIFICATION
2010-09-20 22:08:53 +02:00
* src/backend/commands/alter.c
2003-06-27 16:45:32 +02:00
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "catalog/dependency.h"
#include "catalog/indexing.h"
2003-06-27 16:45:32 +02:00
#include "catalog/namespace.h"
#include "catalog/pg_largeobject.h"
#include "catalog/pg_namespace.h"
2003-06-27 16:45:32 +02:00
#include "commands/alter.h"
#include "commands/conversioncmds.h"
#include "commands/dbcommands.h"
#include "commands/defrem.h"
#include "commands/proclang.h"
#include "commands/schemacmds.h"
#include "commands/tablecmds.h"
#include "commands/tablespace.h"
2003-06-27 16:45:32 +02:00
#include "commands/trigger.h"
#include "commands/typecmds.h"
2003-06-27 16:45:32 +02:00
#include "commands/user.h"
#include "miscadmin.h"
#include "parser/parse_clause.h"
#include "tcop/utility.h"
2003-06-27 16:45:32 +02:00
#include "utils/acl.h"
#include "utils/builtins.h"
2003-06-27 16:45:32 +02:00
#include "utils/lsyscache.h"
#include "utils/syscache.h"
2003-06-27 16:45:32 +02:00
/*
2004-08-29 07:07:03 +02:00
* Executes an ALTER OBJECT / RENAME TO statement. Based on the object
* type, the function appropriate to that type is executed.
*/
2003-06-27 16:45:32 +02:00
void
ExecRenameStmt(RenameStmt *stmt)
{
switch (stmt->renameType)
{
case OBJECT_AGGREGATE:
RenameAggregate(stmt->object, stmt->objarg, stmt->newname);
2003-06-27 16:45:32 +02:00
break;
case OBJECT_CONVERSION:
RenameConversion(stmt->object, stmt->newname);
break;
case OBJECT_DATABASE:
RenameDatabase(stmt->subname, stmt->newname);
break;
case OBJECT_FUNCTION:
RenameFunction(stmt->object, stmt->objarg, stmt->newname);
break;
case OBJECT_LANGUAGE:
RenameLanguage(stmt->subname, stmt->newname);
break;
case OBJECT_OPCLASS:
RenameOpClass(stmt->object, stmt->subname, stmt->newname);
break;
case OBJECT_OPFAMILY:
RenameOpFamily(stmt->object, stmt->subname, stmt->newname);
break;
case OBJECT_ROLE:
RenameRole(stmt->subname, stmt->newname);
break;
2003-06-27 16:45:32 +02:00
case OBJECT_SCHEMA:
RenameSchema(stmt->subname, stmt->newname);
break;
case OBJECT_TABLESPACE:
RenameTableSpace(stmt->subname, stmt->newname);
break;
2003-06-27 16:45:32 +02:00
case OBJECT_TABLE:
case OBJECT_SEQUENCE:
case OBJECT_VIEW:
2004-08-22 02:08:28 +02:00
case OBJECT_INDEX:
2003-06-27 16:45:32 +02:00
case OBJECT_COLUMN:
case OBJECT_ATTRIBUTE:
2003-06-27 16:45:32 +02:00
case OBJECT_TRIGGER:
2003-08-04 02:43:34 +02:00
{
Oid relid;
2003-06-27 16:45:32 +02:00
2003-08-04 02:43:34 +02:00
CheckRelationOwnership(stmt->relation, true);
2003-06-27 16:45:32 +02:00
2003-08-04 02:43:34 +02:00
relid = RangeVarGetRelid(stmt->relation, false);
2003-06-27 16:45:32 +02:00
2003-08-04 02:43:34 +02:00
switch (stmt->renameType)
2003-06-27 16:45:32 +02:00
{
2003-08-04 02:43:34 +02:00
case OBJECT_TABLE:
case OBJECT_SEQUENCE:
case OBJECT_VIEW:
2004-08-22 02:08:28 +02:00
case OBJECT_INDEX:
2003-08-04 02:43:34 +02:00
{
/*
* RENAME TABLE requires that we (still) hold
2005-10-15 04:49:52 +02:00
* CREATE rights on the containing namespace, as
* well as ownership of the table.
2003-08-04 02:43:34 +02:00
*/
Oid namespaceId = get_rel_namespace(relid);
AclResult aclresult;
aclresult = pg_namespace_aclcheck(namespaceId,
GetUserId(),
ACL_CREATE);
2003-08-04 02:43:34 +02:00
if (aclresult != ACLCHECK_OK)
aclcheck_error(aclresult, ACL_KIND_NAMESPACE,
2005-10-15 04:49:52 +02:00
get_namespace_name(namespaceId));
2003-08-04 02:43:34 +02:00
RenameRelation(relid, stmt->newname, stmt->renameType);
2003-08-04 02:43:34 +02:00
break;
}
case OBJECT_COLUMN:
case OBJECT_ATTRIBUTE:
renameatt(relid, stmt);
2003-08-04 02:43:34 +02:00
break;
case OBJECT_TRIGGER:
renametrig(relid,
stmt->subname, /* old att name */
stmt->newname); /* new att name */
break;
default:
/* can't happen */ ;
}
break;
2003-06-27 16:45:32 +02:00
}
case OBJECT_TSPARSER:
RenameTSParser(stmt->object, stmt->newname);
break;
case OBJECT_TSDICTIONARY:
RenameTSDictionary(stmt->object, stmt->newname);
break;
case OBJECT_TSTEMPLATE:
RenameTSTemplate(stmt->object, stmt->newname);
break;
case OBJECT_TSCONFIGURATION:
RenameTSConfiguration(stmt->object, stmt->newname);
break;
case OBJECT_TYPE:
RenameType(stmt->object, stmt->newname);
break;
2003-06-27 16:45:32 +02:00
default:
elog(ERROR, "unrecognized rename stmt type: %d",
(int) stmt->renameType);
2003-06-27 16:45:32 +02:00
}
}
/*
* Executes an ALTER OBJECT / SET SCHEMA statement. Based on the object
* type, the function appropriate to that type is executed.
*/
void
ExecAlterObjectSchemaStmt(AlterObjectSchemaStmt *stmt)
{
switch (stmt->objectType)
{
case OBJECT_AGGREGATE:
AlterFunctionNamespace(stmt->object, stmt->objarg, true,
stmt->newschema);
break;
case OBJECT_CONVERSION:
AlterConversionNamespace(stmt->object, stmt->newschema);
break;
case OBJECT_FUNCTION:
AlterFunctionNamespace(stmt->object, stmt->objarg, false,
stmt->newschema);
break;
2005-10-15 04:49:52 +02:00
case OBJECT_OPERATOR:
AlterOperatorNamespace(stmt->object, stmt->objarg, stmt->newschema);
break;
case OBJECT_OPCLASS:
AlterOpClassNamespace(stmt->object, stmt->objarg, stmt->newschema);
break;
case OBJECT_OPFAMILY:
AlterOpFamilyNamespace(stmt->object, stmt->objarg, stmt->newschema);
break;
case OBJECT_SEQUENCE:
case OBJECT_TABLE:
case OBJECT_VIEW:
CheckRelationOwnership(stmt->relation, true);
AlterTableNamespace(stmt->relation, stmt->newschema,
stmt->objectType, AccessExclusiveLock);
break;
2005-10-15 04:49:52 +02:00
case OBJECT_TSPARSER:
AlterTSParserNamespace(stmt->object, stmt->newschema);
break;
case OBJECT_TSDICTIONARY:
AlterTSDictionaryNamespace(stmt->object, stmt->newschema);
break;
case OBJECT_TSTEMPLATE:
AlterTSTemplateNamespace(stmt->object, stmt->newschema);
break;
case OBJECT_TSCONFIGURATION:
AlterTSConfigurationNamespace(stmt->object, stmt->newschema);
break;
case OBJECT_TYPE:
case OBJECT_DOMAIN:
AlterTypeNamespace(stmt->object, stmt->newschema);
break;
2005-10-15 04:49:52 +02:00
default:
elog(ERROR, "unrecognized AlterObjectSchemaStmt type: %d",
(int) stmt->objectType);
}
}
/*
* Generic function to change the namespace of a given object, for simple
* cases (won't work for tables or functions, objects which have more than 2
* key-attributes to use when searching for their syscache entries --- we
* don't want nor need to get this generic here).
*
* The AlterFooNamespace() calls just above will call a function whose job
* is to lookup the arguments for the generic function here.
*
* Relation must already by open, it's the responsibility of the caller to
* close it.
*/
void
AlterObjectNamespace(Relation rel, int cacheId,
Oid classId, Oid objid, Oid nspOid,
int Anum_name, int Anum_namespace, int Anum_owner,
AclObjectKind acl_kind,
bool superuser_only)
{
Oid oldNspOid;
Datum name, namespace;
bool isnull;
HeapTuple tup, newtup = NULL;
Datum *values;
bool *nulls;
bool *replaces;
tup = SearchSysCacheCopy1(cacheId, ObjectIdGetDatum(objid));
if (!HeapTupleIsValid(tup)) /* should not happen */
elog(ERROR, "cache lookup failed for object %u: %s",
objid, getObjectDescriptionOids(classId, objid));
name = heap_getattr(tup, Anum_name, rel->rd_att, &isnull);
namespace = heap_getattr(tup, Anum_namespace, rel->rd_att, &isnull);
oldNspOid = DatumGetObjectId(namespace);
/* Check basic namespace related issues */
CheckSetNamespace(oldNspOid, nspOid, classId, objid);
/* check for duplicate name (more friendly than unique-index failure) */
if (SearchSysCacheExists2(cacheId, name, ObjectIdGetDatum(nspOid)))
ereport(ERROR,
(errcode(ERRCODE_DUPLICATE_OBJECT),
errmsg("%s already exists in schema \"%s\"",
getObjectDescriptionOids(classId, objid),
get_namespace_name(nspOid))));
/* Superusers can always do it */
if (!superuser())
{
Datum owner;
Oid ownerId;
AclResult aclresult;
if (superuser_only)
ereport(ERROR,
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
(errmsg("must be superuser to SET SCHEMA of %s",
getObjectDescriptionOids(classId, objid)))));
/* Otherwise, must be owner of the existing object */
owner = heap_getattr(tup, Anum_owner, rel->rd_att, &isnull);
ownerId = DatumGetObjectId(owner);
if (!has_privs_of_role(GetUserId(), ownerId))
aclcheck_error(ACLCHECK_NOT_OWNER, acl_kind,
NameStr(*(DatumGetName(name))));
/* owner must have CREATE privilege on namespace */
aclresult = pg_namespace_aclcheck(oldNspOid, GetUserId(), ACL_CREATE);
if (aclresult != ACLCHECK_OK)
aclcheck_error(aclresult, ACL_KIND_NAMESPACE,
get_namespace_name(oldNspOid));
}
/* Prepare to update tuple */
values = palloc0(rel->rd_att->natts * sizeof(Datum));
nulls = palloc0(rel->rd_att->natts * sizeof(bool));
replaces = palloc0(rel->rd_att->natts * sizeof(bool));
values[Anum_namespace - 1] = nspOid;
replaces[Anum_namespace - 1] = true;
newtup = heap_modify_tuple(tup, rel->rd_att, values, nulls, replaces);
/* Perform actual update */
simple_heap_update(rel, &tup->t_self, newtup);
CatalogUpdateIndexes(rel, newtup);
/* Release memory */
pfree(values);
pfree(nulls);
pfree(replaces);
/* update dependencies to point to the new schema */
changeDependencyFor(classId, objid,
NamespaceRelationId, oldNspOid, nspOid);
}
/*
* Executes an ALTER OBJECT / OWNER TO statement. Based on the object
* type, the function appropriate to that type is executed.
*/
void
ExecAlterOwnerStmt(AlterOwnerStmt *stmt)
{
Oid newowner = get_role_oid(stmt->newowner, false);
switch (stmt->objectType)
{
case OBJECT_AGGREGATE:
AlterAggregateOwner(stmt->object, stmt->objarg, newowner);
break;
case OBJECT_CONVERSION:
AlterConversionOwner(stmt->object, newowner);
break;
case OBJECT_DATABASE:
AlterDatabaseOwner(strVal(linitial(stmt->object)), newowner);
break;
case OBJECT_FUNCTION:
AlterFunctionOwner(stmt->object, stmt->objarg, newowner);
break;
case OBJECT_LANGUAGE:
AlterLanguageOwner(strVal(linitial(stmt->object)), newowner);
break;
case OBJECT_LARGEOBJECT:
LargeObjectAlterOwner(oidparse(linitial(stmt->object)), newowner);
break;
case OBJECT_OPERATOR:
Assert(list_length(stmt->objarg) == 2);
AlterOperatorOwner(stmt->object,
(TypeName *) linitial(stmt->objarg),
(TypeName *) lsecond(stmt->objarg),
newowner);
break;
case OBJECT_OPCLASS:
AlterOpClassOwner(stmt->object, stmt->addname, newowner);
break;
case OBJECT_OPFAMILY:
AlterOpFamilyOwner(stmt->object, stmt->addname, newowner);
break;
case OBJECT_SCHEMA:
AlterSchemaOwner(strVal(linitial(stmt->object)), newowner);
break;
case OBJECT_TABLESPACE:
AlterTableSpaceOwner(strVal(linitial(stmt->object)), newowner);
break;
case OBJECT_TYPE:
case OBJECT_DOMAIN: /* same as TYPE */
AlterTypeOwner(stmt->object, newowner);
break;
case OBJECT_TSDICTIONARY:
AlterTSDictionaryOwner(stmt->object, newowner);
break;
case OBJECT_TSCONFIGURATION:
AlterTSConfigurationOwner(stmt->object, newowner);
break;
case OBJECT_FDW:
AlterForeignDataWrapperOwner(strVal(linitial(stmt->object)),
newowner);
break;
case OBJECT_FOREIGN_SERVER:
AlterForeignServerOwner(strVal(linitial(stmt->object)), newowner);
break;
default:
elog(ERROR, "unrecognized AlterOwnerStmt type: %d",
(int) stmt->objectType);
}
}