Skip to content

Commit ac50404

Browse files
committed
Have REASSIGN OWNED work on extensions, too
Per bug #6593, REASSIGN OWNED fails when the affected role has created an extension. Even though the user related to the extension is not nominally the owner, its OID appears on pg_shdepend and thus causes problems when the user is to be dropped. This commit adds code to change the "ownership" of the extension itself, not of the contained objects. This is fine because it's currently only called from REASSIGN OWNED, which would also modify the ownership of the contained objects. However, this is not sufficient for a working ALTER OWNER implementation extension. Back-patch to 9.1, where extensions were introduced. Bug #6593 reported by Emiliano Leporati.
1 parent af4f029 commit ac50404

File tree

3 files changed

+99
-0
lines changed

3 files changed

+99
-0
lines changed

src/backend/catalog/pg_shdepend.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "catalog/pg_conversion.h"
2626
#include "catalog/pg_database.h"
2727
#include "catalog/pg_default_acl.h"
28+
#include "catalog/pg_extension.h"
2829
#include "catalog/pg_foreign_data_wrapper.h"
2930
#include "catalog/pg_foreign_server.h"
3031
#include "catalog/pg_language.h"
@@ -1392,6 +1393,10 @@ shdepReassignOwned(List *roleids, Oid newrole)
13921393
AlterForeignDataWrapperOwner_oid(sdepForm->objid, newrole);
13931394
break;
13941395

1396+
case ExtensionRelationId:
1397+
AlterExtensionOwner_oid(sdepForm->objid, newrole);
1398+
break;
1399+
13951400
default:
13961401
elog(ERROR, "unexpected classid %u", sdepForm->classid);
13971402
break;

src/backend/commands/extension.c

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2789,3 +2789,95 @@ ExecAlterExtensionContentsStmt(AlterExtensionContentsStmt *stmt)
27892789
if (relation != NULL)
27902790
relation_close(relation, NoLock);
27912791
}
2792+
2793+
/*
2794+
* AlterExtensionOwner_internal
2795+
*
2796+
* Internal routine for changing the owner of an extension. rel must be
2797+
* pg_extension, already open and suitably locked; it will not be closed.
2798+
*
2799+
* Note that this only changes ownership of the extension itself; it doesn't
2800+
* change the ownership of objects it contains. Since this function is
2801+
* currently only called from REASSIGN OWNED, this restriction is okay because
2802+
* said objects would also be affected by our caller. But it's not enough for
2803+
* a full-fledged ALTER OWNER implementation, so beware.
2804+
*/
2805+
static void
2806+
AlterExtensionOwner_internal(Relation rel, Oid extensionOid, Oid newOwnerId)
2807+
{
2808+
Form_pg_extension extForm;
2809+
HeapTuple tup;
2810+
SysScanDesc scandesc;
2811+
ScanKeyData entry[1];
2812+
2813+
Assert(RelationGetRelid(rel) == ExtensionRelationId);
2814+
2815+
ScanKeyInit(&entry[0],
2816+
ObjectIdAttributeNumber,
2817+
BTEqualStrategyNumber, F_OIDEQ,
2818+
ObjectIdGetDatum(extensionOid));
2819+
2820+
scandesc = systable_beginscan(rel, ExtensionOidIndexId, true,
2821+
SnapshotNow, 1, entry);
2822+
2823+
/* We assume that there can be at most one matching tuple */
2824+
tup = systable_getnext(scandesc);
2825+
if (!HeapTupleIsValid(tup)) /* should not happen */
2826+
elog(ERROR, "cache lookup failed for extension %u", extensionOid);
2827+
2828+
tup = heap_copytuple(tup);
2829+
systable_endscan(scandesc);
2830+
2831+
extForm = (Form_pg_extension) GETSTRUCT(tup);
2832+
2833+
/*
2834+
* If the new owner is the same as the existing owner, consider the
2835+
* command to have succeeded. This is for dump restoration purposes.
2836+
*/
2837+
if (extForm->extowner != newOwnerId)
2838+
{
2839+
/* Superusers can always do it */
2840+
if (!superuser())
2841+
{
2842+
/* Otherwise, must be owner of the existing object */
2843+
if (!pg_extension_ownercheck(HeapTupleGetOid(tup), GetUserId()))
2844+
aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_EXTENSION,
2845+
NameStr(extForm->extname));
2846+
2847+
/* Must be able to become new owner */
2848+
check_is_member_of_role(GetUserId(), newOwnerId);
2849+
2850+
/* no privilege checks on namespace are required */
2851+
}
2852+
2853+
/*
2854+
* Modify the owner --- okay to scribble on tup because it's a copy
2855+
*/
2856+
extForm->extowner = newOwnerId;
2857+
2858+
simple_heap_update(rel, &tup->t_self, tup);
2859+
2860+
CatalogUpdateIndexes(rel, tup);
2861+
2862+
/* Update owner dependency reference */
2863+
changeDependencyOnOwner(ExtensionRelationId, extensionOid,
2864+
newOwnerId);
2865+
}
2866+
2867+
heap_freetuple(tup);
2868+
}
2869+
2870+
/*
2871+
* Change extension owner, by OID
2872+
*/
2873+
void
2874+
AlterExtensionOwner_oid(Oid extensionOid, Oid newOwnerId)
2875+
{
2876+
Relation rel;
2877+
2878+
rel = heap_open(ExtensionRelationId, RowExclusiveLock);
2879+
2880+
AlterExtensionOwner_internal(rel, extensionOid, newOwnerId);
2881+
2882+
heap_close(rel, NoLock);
2883+
}

src/include/commands/extension.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,6 @@ extern char *get_extension_name(Oid ext_oid);
4646

4747
extern void AlterExtensionNamespace(List *names, const char *newschema);
4848

49+
extern void AlterExtensionOwner_oid(Oid extensionOid, Oid newOwnerId);
50+
4951
#endif /* EXTENSION_H */

0 commit comments

Comments
 (0)