2003-06-27 16:45:32 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* alter.c
|
|
|
|
* Drivers for generic alter commands
|
|
|
|
*
|
2013-01-01 23:15:01 +01:00
|
|
|
* Portions Copyright (c) 1996-2013, 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"
|
|
|
|
|
2012-08-30 22:15:44 +02:00
|
|
|
#include "access/htup_details.h"
|
2012-10-03 23:02:38 +02:00
|
|
|
#include "access/sysattr.h"
|
2010-11-26 23:27:23 +01:00
|
|
|
#include "catalog/dependency.h"
|
|
|
|
#include "catalog/indexing.h"
|
2003-06-27 16:45:32 +02:00
|
|
|
#include "catalog/namespace.h"
|
2009-12-11 04:34:57 +01:00
|
|
|
#include "catalog/pg_largeobject.h"
|
2012-10-03 23:02:38 +02:00
|
|
|
#include "catalog/pg_largeobject_metadata.h"
|
2010-11-26 23:27:23 +01:00
|
|
|
#include "catalog/pg_namespace.h"
|
2003-06-27 16:45:32 +02:00
|
|
|
#include "commands/alter.h"
|
2011-02-12 14:54:13 +01:00
|
|
|
#include "commands/collationcmds.h"
|
2003-06-27 16:45:32 +02:00
|
|
|
#include "commands/conversioncmds.h"
|
|
|
|
#include "commands/dbcommands.h"
|
|
|
|
#include "commands/defrem.h"
|
2012-07-18 16:16:16 +02:00
|
|
|
#include "commands/event_trigger.h"
|
2011-02-08 22:08:41 +01:00
|
|
|
#include "commands/extension.h"
|
2003-06-27 16:45:32 +02:00
|
|
|
#include "commands/proclang.h"
|
|
|
|
#include "commands/schemacmds.h"
|
|
|
|
#include "commands/tablecmds.h"
|
2004-06-25 23:55:59 +02:00
|
|
|
#include "commands/tablespace.h"
|
2003-06-27 16:45:32 +02:00
|
|
|
#include "commands/trigger.h"
|
2004-06-25 23:55:59 +02:00
|
|
|
#include "commands/typecmds.h"
|
2003-06-27 16:45:32 +02:00
|
|
|
#include "commands/user.h"
|
|
|
|
#include "miscadmin.h"
|
2003-07-22 21:00:12 +02:00
|
|
|
#include "tcop/utility.h"
|
2010-06-13 19:43:13 +02:00
|
|
|
#include "utils/builtins.h"
|
2012-10-03 23:02:38 +02:00
|
|
|
#include "utils/fmgroids.h"
|
2003-06-27 16:45:32 +02:00
|
|
|
#include "utils/lsyscache.h"
|
2011-02-23 18:18:09 +01:00
|
|
|
#include "utils/rel.h"
|
2010-11-26 23:27:23 +01:00
|
|
|
#include "utils/syscache.h"
|
2012-10-03 23:02:38 +02:00
|
|
|
#include "utils/tqual.h"
|
2003-06-27 16:45:32 +02:00
|
|
|
|
|
|
|
|
2004-06-25 23:55:59 +02:00
|
|
|
/*
|
2004-08-29 07:07:03 +02:00
|
|
|
* Executes an ALTER OBJECT / RENAME TO statement. Based on the object
|
2004-06-25 23:55:59 +02:00
|
|
|
* type, the function appropriate to that type is executed.
|
|
|
|
*/
|
2012-12-24 00:25:03 +01:00
|
|
|
Oid
|
2003-06-27 16:45:32 +02:00
|
|
|
ExecRenameStmt(RenameStmt *stmt)
|
|
|
|
{
|
|
|
|
switch (stmt->renameType)
|
|
|
|
{
|
|
|
|
case OBJECT_AGGREGATE:
|
2012-12-24 00:25:03 +01:00
|
|
|
return RenameAggregate(stmt->object, stmt->objarg, stmt->newname);
|
2003-06-27 16:45:32 +02:00
|
|
|
|
2011-02-12 14:54:13 +01:00
|
|
|
case OBJECT_COLLATION:
|
2012-12-24 00:25:03 +01:00
|
|
|
return RenameCollation(stmt->object, stmt->newname);
|
2011-02-12 14:54:13 +01:00
|
|
|
|
2012-03-10 19:19:13 +01:00
|
|
|
case OBJECT_CONSTRAINT:
|
2012-12-24 00:25:03 +01:00
|
|
|
return RenameConstraint(stmt);
|
2012-03-10 19:19:13 +01:00
|
|
|
|
2003-06-27 16:45:32 +02:00
|
|
|
case OBJECT_CONVERSION:
|
2012-12-24 00:25:03 +01:00
|
|
|
return RenameConversion(stmt->object, stmt->newname);
|
2003-06-27 16:45:32 +02:00
|
|
|
|
|
|
|
case OBJECT_DATABASE:
|
2012-12-24 00:25:03 +01:00
|
|
|
return RenameDatabase(stmt->subname, stmt->newname);
|
2003-06-27 16:45:32 +02:00
|
|
|
|
2011-12-09 19:42:30 +01:00
|
|
|
case OBJECT_FDW:
|
2012-12-24 00:25:03 +01:00
|
|
|
return RenameForeignDataWrapper(stmt->subname, stmt->newname);
|
2011-12-09 19:42:30 +01:00
|
|
|
|
|
|
|
case OBJECT_FOREIGN_SERVER:
|
2012-12-24 00:25:03 +01:00
|
|
|
return RenameForeignServer(stmt->subname, stmt->newname);
|
2011-12-09 19:42:30 +01:00
|
|
|
|
2012-07-18 16:16:16 +02:00
|
|
|
case OBJECT_EVENT_TRIGGER:
|
2012-12-24 00:25:03 +01:00
|
|
|
return RenameEventTrigger(stmt->subname, stmt->newname);
|
2012-07-18 16:16:16 +02:00
|
|
|
|
2003-06-27 16:45:32 +02:00
|
|
|
case OBJECT_FUNCTION:
|
2012-12-24 00:25:03 +01:00
|
|
|
return RenameFunction(stmt->object, stmt->objarg, stmt->newname);
|
2003-06-27 16:45:32 +02:00
|
|
|
|
|
|
|
case OBJECT_LANGUAGE:
|
2012-12-24 00:25:03 +01:00
|
|
|
return RenameLanguage(stmt->subname, stmt->newname);
|
2003-06-27 16:45:32 +02:00
|
|
|
|
|
|
|
case OBJECT_OPCLASS:
|
2012-12-24 00:25:03 +01:00
|
|
|
return RenameOpClass(stmt->object, stmt->subname, stmt->newname);
|
2003-06-27 16:45:32 +02:00
|
|
|
|
2007-01-23 06:07:18 +01:00
|
|
|
case OBJECT_OPFAMILY:
|
2012-12-24 00:25:03 +01:00
|
|
|
return RenameOpFamily(stmt->object, stmt->subname, stmt->newname);
|
2007-01-23 06:07:18 +01:00
|
|
|
|
2005-06-28 07:09:14 +02:00
|
|
|
case OBJECT_ROLE:
|
2012-12-24 00:25:03 +01:00
|
|
|
return RenameRole(stmt->subname, stmt->newname);
|
2005-06-28 07:09:14 +02:00
|
|
|
|
2003-06-27 16:45:32 +02:00
|
|
|
case OBJECT_SCHEMA:
|
2012-12-24 00:25:03 +01:00
|
|
|
return RenameSchema(stmt->subname, stmt->newname);
|
2003-06-27 16:45:32 +02:00
|
|
|
|
2004-06-25 23:55:59 +02:00
|
|
|
case OBJECT_TABLESPACE:
|
2012-12-24 00:25:03 +01:00
|
|
|
return RenameTableSpace(stmt->subname, stmt->newname);
|
2004-06-25 23:55:59 +02:00
|
|
|
|
2003-06-27 16:45:32 +02:00
|
|
|
case OBJECT_TABLE:
|
2007-07-03 03:30:37 +02:00
|
|
|
case OBJECT_SEQUENCE:
|
|
|
|
case OBJECT_VIEW:
|
2004-08-22 02:08:28 +02:00
|
|
|
case OBJECT_INDEX:
|
Improve behavior of concurrent rename statements.
Previously, renaming a table, sequence, view, index, foreign table,
column, or trigger checked permissions before locking the object, which
meant that if permissions were revoked during the lock wait, we would
still allow the operation. Similarly, if the original object is dropped
and a new one with the same name is created, the operation will be allowed
if we had permissions on the old object; the permissions on the new
object don't matter. All this is now fixed.
Along the way, attempting to rename a trigger on a foreign table now gives
the same error message as trying to create one there in the first place
(i.e. that it's not a table or view) rather than simply stating that no
trigger by that name exists.
Patch by me; review by Noah Misch.
2011-12-16 00:51:46 +01:00
|
|
|
case OBJECT_FOREIGN_TABLE:
|
2012-12-24 00:25:03 +01:00
|
|
|
return RenameRelation(stmt);
|
Improve behavior of concurrent rename statements.
Previously, renaming a table, sequence, view, index, foreign table,
column, or trigger checked permissions before locking the object, which
meant that if permissions were revoked during the lock wait, we would
still allow the operation. Similarly, if the original object is dropped
and a new one with the same name is created, the operation will be allowed
if we had permissions on the old object; the permissions on the new
object don't matter. All this is now fixed.
Along the way, attempting to rename a trigger on a foreign table now gives
the same error message as trying to create one there in the first place
(i.e. that it's not a table or view) rather than simply stating that no
trigger by that name exists.
Patch by me; review by Noah Misch.
2011-12-16 00:51:46 +01:00
|
|
|
|
2003-06-27 16:45:32 +02:00
|
|
|
case OBJECT_COLUMN:
|
2010-09-26 13:41:03 +02:00
|
|
|
case OBJECT_ATTRIBUTE:
|
2012-12-24 00:25:03 +01:00
|
|
|
return renameatt(stmt);
|
Improve behavior of concurrent rename statements.
Previously, renaming a table, sequence, view, index, foreign table,
column, or trigger checked permissions before locking the object, which
meant that if permissions were revoked during the lock wait, we would
still allow the operation. Similarly, if the original object is dropped
and a new one with the same name is created, the operation will be allowed
if we had permissions on the old object; the permissions on the new
object don't matter. All this is now fixed.
Along the way, attempting to rename a trigger on a foreign table now gives
the same error message as trying to create one there in the first place
(i.e. that it's not a table or view) rather than simply stating that no
trigger by that name exists.
Patch by me; review by Noah Misch.
2011-12-16 00:51:46 +01:00
|
|
|
|
2003-06-27 16:45:32 +02:00
|
|
|
case OBJECT_TRIGGER:
|
2012-12-24 00:25:03 +01:00
|
|
|
return renametrig(stmt);
|
2003-06-27 16:45:32 +02:00
|
|
|
|
2007-08-21 03:11:32 +02:00
|
|
|
case OBJECT_TSPARSER:
|
2012-12-24 00:25:03 +01:00
|
|
|
return RenameTSParser(stmt->object, stmt->newname);
|
2007-08-21 03:11:32 +02:00
|
|
|
|
|
|
|
case OBJECT_TSDICTIONARY:
|
2012-12-24 00:25:03 +01:00
|
|
|
return RenameTSDictionary(stmt->object, stmt->newname);
|
2007-08-21 03:11:32 +02:00
|
|
|
|
|
|
|
case OBJECT_TSTEMPLATE:
|
2012-12-24 00:25:03 +01:00
|
|
|
return RenameTSTemplate(stmt->object, stmt->newname);
|
2007-08-21 03:11:32 +02:00
|
|
|
|
|
|
|
case OBJECT_TSCONFIGURATION:
|
2012-12-24 00:25:03 +01:00
|
|
|
return RenameTSConfiguration(stmt->object, stmt->newname);
|
2007-08-21 03:11:32 +02:00
|
|
|
|
2011-12-22 21:43:56 +01:00
|
|
|
case OBJECT_DOMAIN:
|
2008-03-19 19:38:30 +01:00
|
|
|
case OBJECT_TYPE:
|
2012-12-24 00:25:03 +01:00
|
|
|
return RenameType(stmt);
|
2008-03-19 19:38:30 +01:00
|
|
|
|
2003-06-27 16:45:32 +02:00
|
|
|
default:
|
2003-07-20 23:56:35 +02:00
|
|
|
elog(ERROR, "unrecognized rename stmt type: %d",
|
|
|
|
(int) stmt->renameType);
|
2012-12-24 00:25:03 +01:00
|
|
|
return InvalidOid; /* keep compiler happy */
|
2003-06-27 16:45:32 +02:00
|
|
|
}
|
|
|
|
}
|
2004-06-25 23:55:59 +02:00
|
|
|
|
2005-08-01 06:03:59 +02:00
|
|
|
/*
|
|
|
|
* Executes an ALTER OBJECT / SET SCHEMA statement. Based on the object
|
|
|
|
* type, the function appropriate to that type is executed.
|
|
|
|
*/
|
2012-12-24 00:25:03 +01:00
|
|
|
Oid
|
2005-08-01 06:03:59 +02:00
|
|
|
ExecAlterObjectSchemaStmt(AlterObjectSchemaStmt *stmt)
|
|
|
|
{
|
|
|
|
switch (stmt->objectType)
|
|
|
|
{
|
|
|
|
case OBJECT_AGGREGATE:
|
2012-12-24 00:25:03 +01:00
|
|
|
return AlterFunctionNamespace(stmt->object, stmt->objarg, true,
|
|
|
|
stmt->newschema);
|
2006-04-15 19:45:46 +02:00
|
|
|
|
2011-02-12 14:54:13 +01:00
|
|
|
case OBJECT_COLLATION:
|
2012-12-24 00:25:03 +01:00
|
|
|
return AlterCollationNamespace(stmt->object, stmt->newschema);
|
2011-02-12 14:54:13 +01:00
|
|
|
|
2011-02-08 22:08:41 +01:00
|
|
|
case OBJECT_EXTENSION:
|
2012-12-24 00:25:03 +01:00
|
|
|
return AlterExtensionNamespace(stmt->object, stmt->newschema);
|
2011-02-08 22:08:41 +01:00
|
|
|
|
2005-08-01 06:03:59 +02:00
|
|
|
case OBJECT_FUNCTION:
|
2012-12-24 00:25:03 +01:00
|
|
|
return AlterFunctionNamespace(stmt->object, stmt->objarg, false,
|
|
|
|
stmt->newschema);
|
2005-10-15 04:49:52 +02:00
|
|
|
|
2005-08-01 06:03:59 +02:00
|
|
|
case OBJECT_SEQUENCE:
|
|
|
|
case OBJECT_TABLE:
|
2008-06-15 03:25:54 +02:00
|
|
|
case OBJECT_VIEW:
|
2011-01-02 05:48:11 +01:00
|
|
|
case OBJECT_FOREIGN_TABLE:
|
2012-12-24 00:25:03 +01:00
|
|
|
return AlterTableNamespace(stmt);
|
2005-10-15 04:49:52 +02:00
|
|
|
|
2012-09-27 23:13:09 +02:00
|
|
|
case OBJECT_TYPE:
|
|
|
|
case OBJECT_DOMAIN:
|
2012-12-24 00:25:03 +01:00
|
|
|
return AlterTypeNamespace(stmt->object, stmt->newschema,
|
|
|
|
stmt->objectType);
|
2010-11-26 23:27:23 +01:00
|
|
|
|
2012-09-27 23:13:09 +02:00
|
|
|
/* generic code path */
|
|
|
|
case OBJECT_CONVERSION:
|
|
|
|
case OBJECT_OPERATOR:
|
|
|
|
case OBJECT_OPCLASS:
|
|
|
|
case OBJECT_OPFAMILY:
|
|
|
|
case OBJECT_TSPARSER:
|
2010-11-26 23:27:23 +01:00
|
|
|
case OBJECT_TSDICTIONARY:
|
|
|
|
case OBJECT_TSTEMPLATE:
|
|
|
|
case OBJECT_TSCONFIGURATION:
|
2012-09-27 23:13:09 +02:00
|
|
|
{
|
|
|
|
Relation catalog;
|
|
|
|
Relation relation;
|
|
|
|
Oid classId;
|
|
|
|
Oid nspOid;
|
|
|
|
ObjectAddress address;
|
|
|
|
|
|
|
|
address = get_object_address(stmt->objectType,
|
|
|
|
stmt->object,
|
|
|
|
stmt->objarg,
|
|
|
|
&relation,
|
|
|
|
AccessExclusiveLock,
|
|
|
|
false);
|
|
|
|
Assert(relation == NULL);
|
|
|
|
classId = address.classId;
|
|
|
|
catalog = heap_open(classId, RowExclusiveLock);
|
|
|
|
nspOid = LookupCreationNamespace(stmt->newschema);
|
|
|
|
|
|
|
|
AlterObjectNamespace_internal(catalog, address.objectId,
|
|
|
|
nspOid);
|
|
|
|
heap_close(catalog, RowExclusiveLock);
|
2012-12-24 00:25:03 +01:00
|
|
|
|
|
|
|
return address.objectId;
|
2012-09-27 23:13:09 +02:00
|
|
|
}
|
2005-08-01 06:03:59 +02:00
|
|
|
break;
|
2005-10-15 04:49:52 +02:00
|
|
|
|
2005-08-01 06:03:59 +02:00
|
|
|
default:
|
|
|
|
elog(ERROR, "unrecognized AlterObjectSchemaStmt type: %d",
|
|
|
|
(int) stmt->objectType);
|
2012-12-24 00:25:03 +01:00
|
|
|
return InvalidOid; /* keep compiler happy */
|
2005-08-01 06:03:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-08 22:08:41 +01:00
|
|
|
/*
|
|
|
|
* Change an object's namespace given its classOid and object Oid.
|
|
|
|
*
|
|
|
|
* Objects that don't have a namespace should be ignored.
|
|
|
|
*
|
|
|
|
* This function is currently used only by ALTER EXTENSION SET SCHEMA,
|
|
|
|
* so it only needs to cover object types that can be members of an
|
|
|
|
* extension, and it doesn't have to deal with certain special cases
|
|
|
|
* such as not wanting to process array types --- those should never
|
|
|
|
* be direct members of an extension anyway.
|
|
|
|
*
|
|
|
|
* Returns the OID of the object's previous namespace, or InvalidOid if
|
|
|
|
* object doesn't have a schema.
|
|
|
|
*/
|
|
|
|
Oid
|
2012-10-31 14:52:55 +01:00
|
|
|
AlterObjectNamespace_oid(Oid classId, Oid objid, Oid nspOid,
|
|
|
|
ObjectAddresses *objsMoved)
|
2011-02-08 22:08:41 +01:00
|
|
|
{
|
|
|
|
Oid oldNspOid = InvalidOid;
|
|
|
|
ObjectAddress dep;
|
|
|
|
|
|
|
|
dep.classId = classId;
|
|
|
|
dep.objectId = objid;
|
|
|
|
dep.objectSubId = 0;
|
|
|
|
|
|
|
|
switch (getObjectClass(&dep))
|
|
|
|
{
|
|
|
|
case OCLASS_CLASS:
|
2011-04-10 17:42:00 +02:00
|
|
|
{
|
|
|
|
Relation rel;
|
2011-02-08 22:08:41 +01:00
|
|
|
|
2011-04-10 17:42:00 +02:00
|
|
|
rel = relation_open(objid, AccessExclusiveLock);
|
|
|
|
oldNspOid = RelationGetNamespace(rel);
|
2011-02-08 22:08:41 +01:00
|
|
|
|
2012-10-31 14:52:55 +01:00
|
|
|
AlterTableNamespaceInternal(rel, oldNspOid, nspOid, objsMoved);
|
2011-02-08 22:08:41 +01:00
|
|
|
|
2011-04-10 17:42:00 +02:00
|
|
|
relation_close(rel, NoLock);
|
|
|
|
break;
|
|
|
|
}
|
2011-02-08 22:08:41 +01:00
|
|
|
|
|
|
|
case OCLASS_PROC:
|
|
|
|
oldNspOid = AlterFunctionNamespace_oid(objid, nspOid);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OCLASS_TYPE:
|
2012-10-31 14:52:55 +01:00
|
|
|
oldNspOid = AlterTypeNamespace_oid(objid, nspOid, objsMoved);
|
2011-02-08 22:08:41 +01:00
|
|
|
break;
|
|
|
|
|
2011-02-12 14:54:13 +01:00
|
|
|
case OCLASS_COLLATION:
|
|
|
|
oldNspOid = AlterCollationNamespace_oid(objid, nspOid);
|
|
|
|
break;
|
|
|
|
|
2011-02-08 22:08:41 +01:00
|
|
|
case OCLASS_CONVERSION:
|
|
|
|
case OCLASS_OPERATOR:
|
|
|
|
case OCLASS_OPCLASS:
|
|
|
|
case OCLASS_OPFAMILY:
|
|
|
|
case OCLASS_TSPARSER:
|
|
|
|
case OCLASS_TSDICT:
|
|
|
|
case OCLASS_TSTEMPLATE:
|
|
|
|
case OCLASS_TSCONFIG:
|
2012-09-27 23:13:09 +02:00
|
|
|
{
|
|
|
|
Relation catalog;
|
|
|
|
|
|
|
|
catalog = heap_open(classId, RowExclusiveLock);
|
|
|
|
|
|
|
|
oldNspOid = AlterObjectNamespace_internal(catalog, objid,
|
|
|
|
nspOid);
|
|
|
|
|
|
|
|
heap_close(catalog, RowExclusiveLock);
|
|
|
|
}
|
2011-02-08 22:08:41 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return oldNspOid;
|
|
|
|
}
|
|
|
|
|
2010-11-26 23:27:23 +01:00
|
|
|
/*
|
|
|
|
* Generic function to change the namespace of a given object, for simple
|
2011-02-08 22:08:41 +01:00
|
|
|
* cases (won't work for tables, nor other cases where we need to do more
|
|
|
|
* than change the namespace column of a single catalog entry).
|
2010-11-26 23:27:23 +01:00
|
|
|
*
|
2011-02-08 22:08:41 +01:00
|
|
|
* rel: catalog relation containing object (RowExclusiveLock'd by caller)
|
|
|
|
* objid: OID of object to change the namespace of
|
|
|
|
* nspOid: OID of new namespace
|
|
|
|
*
|
|
|
|
* Returns the OID of the object's previous namespace.
|
2010-11-26 23:27:23 +01:00
|
|
|
*/
|
2011-02-08 22:08:41 +01:00
|
|
|
Oid
|
2012-09-27 23:13:09 +02:00
|
|
|
AlterObjectNamespace_internal(Relation rel, Oid objid, Oid nspOid)
|
2010-11-26 23:27:23 +01:00
|
|
|
{
|
2011-02-08 22:08:41 +01:00
|
|
|
Oid classId = RelationGetRelid(rel);
|
2012-09-27 23:13:09 +02:00
|
|
|
int oidCacheId = get_object_catcache_oid(classId);
|
|
|
|
int nameCacheId = get_object_catcache_name(classId);
|
|
|
|
AttrNumber Anum_name = get_object_attnum_name(classId);
|
|
|
|
AttrNumber Anum_namespace = get_object_attnum_namespace(classId);
|
|
|
|
AttrNumber Anum_owner = get_object_attnum_owner(classId);
|
|
|
|
AclObjectKind acl_kind = get_object_aclkind(classId);
|
2010-11-26 23:27:23 +01:00
|
|
|
Oid oldNspOid;
|
2011-04-10 17:42:00 +02:00
|
|
|
Datum name,
|
|
|
|
namespace;
|
|
|
|
bool isnull;
|
|
|
|
HeapTuple tup,
|
|
|
|
newtup;
|
2010-11-26 23:27:23 +01:00
|
|
|
Datum *values;
|
|
|
|
bool *nulls;
|
|
|
|
bool *replaces;
|
|
|
|
|
2011-02-08 22:08:41 +01:00
|
|
|
tup = SearchSysCacheCopy1(oidCacheId, ObjectIdGetDatum(objid));
|
2010-11-26 23:27:23 +01:00
|
|
|
if (!HeapTupleIsValid(tup)) /* should not happen */
|
2011-02-08 22:08:41 +01:00
|
|
|
elog(ERROR, "cache lookup failed for object %u of catalog \"%s\"",
|
|
|
|
objid, RelationGetRelationName(rel));
|
2010-11-26 23:27:23 +01:00
|
|
|
|
2011-02-08 22:08:41 +01:00
|
|
|
name = heap_getattr(tup, Anum_name, RelationGetDescr(rel), &isnull);
|
|
|
|
Assert(!isnull);
|
2012-09-27 23:13:09 +02:00
|
|
|
namespace = heap_getattr(tup, Anum_namespace, RelationGetDescr(rel),
|
|
|
|
&isnull);
|
2011-02-08 22:08:41 +01:00
|
|
|
Assert(!isnull);
|
2010-11-26 23:27:23 +01:00
|
|
|
oldNspOid = DatumGetObjectId(namespace);
|
|
|
|
|
|
|
|
/* Check basic namespace related issues */
|
|
|
|
CheckSetNamespace(oldNspOid, nspOid, classId, objid);
|
|
|
|
|
2011-02-08 22:08:41 +01:00
|
|
|
/* Permission checks ... superusers can always do it */
|
2010-11-26 23:27:23 +01:00
|
|
|
if (!superuser())
|
|
|
|
{
|
2011-04-10 17:42:00 +02:00
|
|
|
Datum owner;
|
2010-11-26 23:27:23 +01:00
|
|
|
Oid ownerId;
|
|
|
|
AclResult aclresult;
|
|
|
|
|
2011-02-08 22:08:41 +01:00
|
|
|
/* Fail if object does not have an explicit owner */
|
|
|
|
if (Anum_owner <= 0)
|
2010-11-26 23:27:23 +01:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
|
2011-07-08 06:37:04 +02:00
|
|
|
(errmsg("must be superuser to set schema of %s",
|
2010-11-26 23:27:23 +01:00
|
|
|
getObjectDescriptionOids(classId, objid)))));
|
|
|
|
|
|
|
|
/* Otherwise, must be owner of the existing object */
|
2011-02-08 22:08:41 +01:00
|
|
|
owner = heap_getattr(tup, Anum_owner, RelationGetDescr(rel), &isnull);
|
|
|
|
Assert(!isnull);
|
2010-11-26 23:27:23 +01:00
|
|
|
ownerId = DatumGetObjectId(owner);
|
|
|
|
|
|
|
|
if (!has_privs_of_role(GetUserId(), ownerId))
|
|
|
|
aclcheck_error(ACLCHECK_NOT_OWNER, acl_kind,
|
|
|
|
NameStr(*(DatumGetName(name))));
|
|
|
|
|
2011-02-08 22:08:41 +01:00
|
|
|
/* User must have CREATE privilege on new namespace */
|
|
|
|
aclresult = pg_namespace_aclcheck(nspOid, GetUserId(), ACL_CREATE);
|
2010-11-26 23:27:23 +01:00
|
|
|
if (aclresult != ACLCHECK_OK)
|
|
|
|
aclcheck_error(aclresult, ACL_KIND_NAMESPACE,
|
2011-02-08 22:08:41 +01:00
|
|
|
get_namespace_name(nspOid));
|
2010-11-26 23:27:23 +01:00
|
|
|
}
|
|
|
|
|
2011-02-08 22:08:41 +01:00
|
|
|
/*
|
|
|
|
* Check for duplicate name (more friendly than unique-index failure).
|
|
|
|
* Since this is just a friendliness check, we can just skip it in cases
|
|
|
|
* where there isn't a suitable syscache available.
|
|
|
|
*/
|
|
|
|
if (nameCacheId >= 0 &&
|
|
|
|
SearchSysCacheExists2(nameCacheId, name, ObjectIdGetDatum(nspOid)))
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_DUPLICATE_OBJECT),
|
|
|
|
errmsg("%s already exists in schema \"%s\"",
|
|
|
|
getObjectDescriptionOids(classId, objid),
|
|
|
|
get_namespace_name(nspOid))));
|
|
|
|
|
|
|
|
/* Build modified tuple */
|
|
|
|
values = palloc0(RelationGetNumberOfAttributes(rel) * sizeof(Datum));
|
|
|
|
nulls = palloc0(RelationGetNumberOfAttributes(rel) * sizeof(bool));
|
|
|
|
replaces = palloc0(RelationGetNumberOfAttributes(rel) * sizeof(bool));
|
|
|
|
values[Anum_namespace - 1] = ObjectIdGetDatum(nspOid);
|
2010-11-26 23:27:23 +01:00
|
|
|
replaces[Anum_namespace - 1] = true;
|
2011-02-08 22:08:41 +01:00
|
|
|
newtup = heap_modify_tuple(tup, RelationGetDescr(rel),
|
|
|
|
values, nulls, replaces);
|
2010-11-26 23:27:23 +01:00
|
|
|
|
|
|
|
/* 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);
|
2011-02-08 22:08:41 +01:00
|
|
|
|
|
|
|
return oldNspOid;
|
2010-11-26 23:27:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-06-25 23:55:59 +02:00
|
|
|
/*
|
|
|
|
* Executes an ALTER OBJECT / OWNER TO statement. Based on the object
|
|
|
|
* type, the function appropriate to that type is executed.
|
|
|
|
*/
|
2012-12-24 00:25:03 +01:00
|
|
|
Oid
|
2004-06-25 23:55:59 +02:00
|
|
|
ExecAlterOwnerStmt(AlterOwnerStmt *stmt)
|
|
|
|
{
|
2010-08-05 16:45:09 +02:00
|
|
|
Oid newowner = get_role_oid(stmt->newowner, false);
|
2004-06-25 23:55:59 +02:00
|
|
|
|
|
|
|
switch (stmt->objectType)
|
|
|
|
{
|
|
|
|
case OBJECT_DATABASE:
|
2012-12-24 00:25:03 +01:00
|
|
|
return AlterDatabaseOwner(strVal(linitial(stmt->object)), newowner);
|
2004-06-25 23:55:59 +02:00
|
|
|
|
|
|
|
case OBJECT_SCHEMA:
|
2012-12-24 00:25:03 +01:00
|
|
|
return AlterSchemaOwner(strVal(linitial(stmt->object)), newowner);
|
2004-06-25 23:55:59 +02:00
|
|
|
|
|
|
|
case OBJECT_TYPE:
|
|
|
|
case OBJECT_DOMAIN: /* same as TYPE */
|
2012-12-24 00:25:03 +01:00
|
|
|
return AlterTypeOwner(stmt->object, newowner, stmt->objectType);
|
2004-06-25 23:55:59 +02:00
|
|
|
break;
|
|
|
|
|
2008-12-19 17:25:19 +01:00
|
|
|
case OBJECT_FDW:
|
2012-12-24 00:25:03 +01:00
|
|
|
return AlterForeignDataWrapperOwner(strVal(linitial(stmt->object)),
|
|
|
|
newowner);
|
2008-12-19 17:25:19 +01:00
|
|
|
|
|
|
|
case OBJECT_FOREIGN_SERVER:
|
2012-12-24 00:25:03 +01:00
|
|
|
return AlterForeignServerOwner(strVal(linitial(stmt->object)),
|
|
|
|
newowner);
|
2008-12-19 17:25:19 +01:00
|
|
|
|
2012-07-18 16:16:16 +02:00
|
|
|
case OBJECT_EVENT_TRIGGER:
|
2012-12-24 00:25:03 +01:00
|
|
|
return AlterEventTriggerOwner(strVal(linitial(stmt->object)),
|
|
|
|
newowner);
|
2012-07-18 16:16:16 +02:00
|
|
|
|
2012-10-03 23:02:38 +02:00
|
|
|
/* Generic cases */
|
|
|
|
case OBJECT_AGGREGATE:
|
|
|
|
case OBJECT_COLLATION:
|
|
|
|
case OBJECT_CONVERSION:
|
|
|
|
case OBJECT_FUNCTION:
|
|
|
|
case OBJECT_LANGUAGE:
|
|
|
|
case OBJECT_LARGEOBJECT:
|
|
|
|
case OBJECT_OPERATOR:
|
|
|
|
case OBJECT_OPCLASS:
|
|
|
|
case OBJECT_OPFAMILY:
|
|
|
|
case OBJECT_TABLESPACE:
|
|
|
|
case OBJECT_TSDICTIONARY:
|
|
|
|
case OBJECT_TSCONFIGURATION:
|
|
|
|
{
|
|
|
|
Relation catalog;
|
|
|
|
Relation relation;
|
|
|
|
Oid classId;
|
|
|
|
ObjectAddress address;
|
|
|
|
|
|
|
|
address = get_object_address(stmt->objectType,
|
|
|
|
stmt->object,
|
|
|
|
stmt->objarg,
|
|
|
|
&relation,
|
|
|
|
AccessExclusiveLock,
|
|
|
|
false);
|
|
|
|
Assert(relation == NULL);
|
|
|
|
classId = address.classId;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* XXX - get_object_address returns Oid of pg_largeobject
|
|
|
|
* catalog for OBJECT_LARGEOBJECT because of historical
|
|
|
|
* reasons. Fix up it here.
|
|
|
|
*/
|
|
|
|
if (classId == LargeObjectRelationId)
|
|
|
|
classId = LargeObjectMetadataRelationId;
|
|
|
|
|
|
|
|
catalog = heap_open(classId, RowExclusiveLock);
|
|
|
|
|
|
|
|
AlterObjectOwner_internal(catalog, address.objectId, newowner);
|
|
|
|
heap_close(catalog, RowExclusiveLock);
|
2012-12-24 00:25:03 +01:00
|
|
|
|
|
|
|
return address.objectId;
|
2012-10-03 23:02:38 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2004-06-25 23:55:59 +02:00
|
|
|
default:
|
|
|
|
elog(ERROR, "unrecognized AlterOwnerStmt type: %d",
|
|
|
|
(int) stmt->objectType);
|
2012-12-24 00:25:03 +01:00
|
|
|
|
|
|
|
return InvalidOid; /* keep compiler happy */
|
2004-06-25 23:55:59 +02:00
|
|
|
}
|
|
|
|
}
|
2012-10-03 23:02:38 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Return a copy of the tuple for the object with the given object OID, from
|
|
|
|
* the given catalog (which must have been opened by the caller and suitably
|
|
|
|
* locked). NULL is returned if the OID is not found.
|
|
|
|
*
|
|
|
|
* We try a syscache first, if available.
|
|
|
|
*
|
|
|
|
* XXX this function seems general in possible usage. Given sufficient callers
|
|
|
|
* elsewhere, we should consider moving it to a more appropriate place.
|
|
|
|
*/
|
|
|
|
static HeapTuple
|
|
|
|
get_catalog_object_by_oid(Relation catalog, Oid objectId)
|
|
|
|
{
|
|
|
|
HeapTuple tuple;
|
|
|
|
Oid classId = RelationGetRelid(catalog);
|
|
|
|
int oidCacheId = get_object_catcache_oid(classId);
|
|
|
|
|
|
|
|
if (oidCacheId > 0)
|
|
|
|
{
|
|
|
|
tuple = SearchSysCacheCopy1(oidCacheId, ObjectIdGetDatum(objectId));
|
|
|
|
if (!HeapTupleIsValid(tuple)) /* should not happen */
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Oid oidIndexId = get_object_oid_index(classId);
|
|
|
|
SysScanDesc scan;
|
|
|
|
ScanKeyData skey;
|
|
|
|
|
|
|
|
Assert(OidIsValid(oidIndexId));
|
|
|
|
|
|
|
|
ScanKeyInit(&skey,
|
|
|
|
ObjectIdAttributeNumber,
|
|
|
|
BTEqualStrategyNumber, F_OIDEQ,
|
|
|
|
ObjectIdGetDatum(objectId));
|
|
|
|
|
|
|
|
scan = systable_beginscan(catalog, oidIndexId, true,
|
|
|
|
SnapshotNow, 1, &skey);
|
|
|
|
tuple = systable_getnext(scan);
|
|
|
|
if (!HeapTupleIsValid(tuple))
|
|
|
|
{
|
|
|
|
systable_endscan(scan);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
tuple = heap_copytuple(tuple);
|
|
|
|
|
|
|
|
systable_endscan(scan);
|
|
|
|
}
|
|
|
|
|
|
|
|
return tuple;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Generic function to change the ownership of a given object, for simple
|
|
|
|
* cases (won't work for tables, nor other cases where we need to do more than
|
|
|
|
* change the ownership column of a single catalog entry).
|
|
|
|
*
|
|
|
|
* rel: catalog relation containing object (RowExclusiveLock'd by caller)
|
|
|
|
* objectId: OID of object to change the ownership of
|
|
|
|
* new_ownerId: OID of new object owner
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
AlterObjectOwner_internal(Relation rel, Oid objectId, Oid new_ownerId)
|
|
|
|
{
|
|
|
|
Oid classId = RelationGetRelid(rel);
|
|
|
|
AttrNumber Anum_owner = get_object_attnum_owner(classId);
|
|
|
|
AttrNumber Anum_namespace = get_object_attnum_namespace(classId);
|
|
|
|
AttrNumber Anum_acl = get_object_attnum_acl(classId);
|
|
|
|
AttrNumber Anum_name = get_object_attnum_name(classId);
|
|
|
|
HeapTuple oldtup;
|
|
|
|
Datum datum;
|
|
|
|
bool isnull;
|
|
|
|
Oid old_ownerId;
|
|
|
|
Oid namespaceId = InvalidOid;
|
|
|
|
|
|
|
|
oldtup = get_catalog_object_by_oid(rel, objectId);
|
|
|
|
if (oldtup == NULL)
|
|
|
|
elog(ERROR, "cache lookup failed for object %u of catalog \"%s\"",
|
|
|
|
objectId, RelationGetRelationName(rel));
|
|
|
|
|
|
|
|
datum = heap_getattr(oldtup, Anum_owner,
|
|
|
|
RelationGetDescr(rel), &isnull);
|
|
|
|
Assert(!isnull);
|
|
|
|
old_ownerId = DatumGetObjectId(datum);
|
|
|
|
|
|
|
|
if (Anum_namespace != InvalidAttrNumber)
|
|
|
|
{
|
|
|
|
datum = heap_getattr(oldtup, Anum_namespace,
|
|
|
|
RelationGetDescr(rel), &isnull);
|
|
|
|
Assert(!isnull);
|
|
|
|
namespaceId = DatumGetObjectId(datum);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (old_ownerId != new_ownerId)
|
|
|
|
{
|
|
|
|
AttrNumber nattrs;
|
|
|
|
HeapTuple newtup;
|
|
|
|
Datum *values;
|
|
|
|
bool *nulls;
|
|
|
|
bool *replaces;
|
|
|
|
|
|
|
|
/* Superusers can bypass permission checks */
|
|
|
|
if (!superuser())
|
|
|
|
{
|
|
|
|
AclObjectKind aclkind = get_object_aclkind(classId);
|
|
|
|
|
|
|
|
/* must be owner */
|
|
|
|
if (!has_privs_of_role(GetUserId(), old_ownerId))
|
|
|
|
{
|
|
|
|
char *objname;
|
|
|
|
char namebuf[NAMEDATALEN];
|
|
|
|
|
|
|
|
if (Anum_name != InvalidAttrNumber)
|
|
|
|
{
|
|
|
|
datum = heap_getattr(oldtup, Anum_name,
|
|
|
|
RelationGetDescr(rel), &isnull);
|
|
|
|
Assert(!isnull);
|
|
|
|
objname = NameStr(*DatumGetName(datum));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
snprintf(namebuf, sizeof(namebuf), "%u",
|
|
|
|
HeapTupleGetOid(oldtup));
|
|
|
|
objname = namebuf;
|
|
|
|
}
|
|
|
|
aclcheck_error(ACLCHECK_NOT_OWNER, aclkind, objname);
|
|
|
|
}
|
|
|
|
/* Must be able to become new owner */
|
|
|
|
check_is_member_of_role(GetUserId(), new_ownerId);
|
|
|
|
|
|
|
|
/* New owner must have CREATE privilege on namespace */
|
|
|
|
if (OidIsValid(namespaceId))
|
|
|
|
{
|
|
|
|
AclResult aclresult;
|
|
|
|
|
|
|
|
aclresult = pg_namespace_aclcheck(namespaceId, new_ownerId,
|
|
|
|
ACL_CREATE);
|
|
|
|
if (aclresult != ACLCHECK_OK)
|
|
|
|
aclcheck_error(aclresult, aclkind,
|
|
|
|
get_namespace_name(namespaceId));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Build a modified tuple */
|
|
|
|
nattrs = RelationGetNumberOfAttributes(rel);
|
|
|
|
values = palloc0(nattrs * sizeof(Datum));
|
|
|
|
nulls = palloc0(nattrs * sizeof(bool));
|
|
|
|
replaces = palloc0(nattrs * sizeof(bool));
|
|
|
|
values[Anum_owner - 1] = ObjectIdGetDatum(new_ownerId);
|
|
|
|
replaces[Anum_owner - 1] = true;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Determine the modified ACL for the new owner. This is only
|
|
|
|
* necessary when the ACL is non-null.
|
|
|
|
*/
|
|
|
|
if (Anum_acl != InvalidAttrNumber)
|
|
|
|
{
|
|
|
|
datum = heap_getattr(oldtup,
|
|
|
|
Anum_acl, RelationGetDescr(rel), &isnull);
|
|
|
|
if (!isnull)
|
|
|
|
{
|
|
|
|
Acl *newAcl;
|
|
|
|
|
|
|
|
newAcl = aclnewowner(DatumGetAclP(datum),
|
|
|
|
old_ownerId, new_ownerId);
|
|
|
|
values[Anum_acl - 1] = PointerGetDatum(newAcl);
|
|
|
|
replaces[Anum_acl - 1] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
newtup = heap_modify_tuple(oldtup, RelationGetDescr(rel),
|
|
|
|
values, nulls, replaces);
|
|
|
|
|
|
|
|
/* Perform actual update */
|
|
|
|
simple_heap_update(rel, &newtup->t_self, newtup);
|
|
|
|
CatalogUpdateIndexes(rel, newtup);
|
|
|
|
|
|
|
|
/* Update owner dependency reference */
|
|
|
|
if (classId == LargeObjectMetadataRelationId)
|
|
|
|
classId = LargeObjectRelationId;
|
|
|
|
changeDependencyOnOwner(classId, HeapTupleGetOid(newtup), new_ownerId);
|
|
|
|
|
|
|
|
/* Release memory */
|
|
|
|
pfree(values);
|
|
|
|
pfree(nulls);
|
|
|
|
pfree(replaces);
|
|
|
|
}
|
|
|
|
}
|