1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-08 11:42:09 +03:00

refactor ALTER some-obj SET OWNER implementation

Remove duplicate implementation of catalog munging and miscellaneous
privilege and consistency checks.  Instead rely on already existing data
in objectaddress.c to do the work.

Author: KaiGai Kohei
Tweaked by me
Reviewed by Robert Haas
This commit is contained in:
Alvaro Herrera
2012-10-03 18:02:38 -03:00
parent 1f91c8ca1d
commit 994c36e01d
24 changed files with 270 additions and 1338 deletions

View File

@ -31,9 +31,6 @@
#include "utils/rel.h"
#include "utils/syscache.h"
static void AlterConversionOwner_internal(Relation rel, Oid conversionOid,
Oid newOwnerId);
/*
* CREATE CONVERSION
*/
@ -168,101 +165,3 @@ RenameConversion(List *name, const char *newname)
heap_close(rel, NoLock);
heap_freetuple(tup);
}
/*
* Change conversion owner, by name
*/
void
AlterConversionOwner(List *name, Oid newOwnerId)
{
Oid conversionOid;
Relation rel;
rel = heap_open(ConversionRelationId, RowExclusiveLock);
conversionOid = get_conversion_oid(name, false);
AlterConversionOwner_internal(rel, conversionOid, newOwnerId);
heap_close(rel, NoLock);
}
/*
* Change conversion owner, by oid
*/
void
AlterConversionOwner_oid(Oid conversionOid, Oid newOwnerId)
{
Relation rel;
rel = heap_open(ConversionRelationId, RowExclusiveLock);
AlterConversionOwner_internal(rel, conversionOid, newOwnerId);
heap_close(rel, NoLock);
}
/*
* AlterConversionOwner_internal
*
* Internal routine for changing the owner. rel must be pg_conversion, already
* open and suitably locked; it will not be closed.
*/
static void
AlterConversionOwner_internal(Relation rel, Oid conversionOid, Oid newOwnerId)
{
Form_pg_conversion convForm;
HeapTuple tup;
Assert(RelationGetRelid(rel) == ConversionRelationId);
tup = SearchSysCacheCopy1(CONVOID, ObjectIdGetDatum(conversionOid));
if (!HeapTupleIsValid(tup)) /* should not happen */
elog(ERROR, "cache lookup failed for conversion %u", conversionOid);
convForm = (Form_pg_conversion) GETSTRUCT(tup);
/*
* If the new owner is the same as the existing owner, consider the
* command to have succeeded. This is for dump restoration purposes.
*/
if (convForm->conowner != newOwnerId)
{
AclResult aclresult;
/* Superusers can always do it */
if (!superuser())
{
/* Otherwise, must be owner of the existing object */
if (!pg_conversion_ownercheck(HeapTupleGetOid(tup), GetUserId()))
aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_CONVERSION,
NameStr(convForm->conname));
/* Must be able to become new owner */
check_is_member_of_role(GetUserId(), newOwnerId);
/* New owner must have CREATE privilege on namespace */
aclresult = pg_namespace_aclcheck(convForm->connamespace,
newOwnerId,
ACL_CREATE);
if (aclresult != ACLCHECK_OK)
aclcheck_error(aclresult, ACL_KIND_NAMESPACE,
get_namespace_name(convForm->connamespace));
}
/*
* Modify the owner --- okay to scribble on tup because it's a copy
*/
convForm->conowner = newOwnerId;
simple_heap_update(rel, &tup->t_self, tup);
CatalogUpdateIndexes(rel, tup);
/* Update owner dependency reference */
changeDependencyOnOwner(ConversionRelationId, conversionOid,
newOwnerId);
}
heap_freetuple(tup);
}