Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 0c7b9dc

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 b33385b commit 0c7b9dc

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
@@ -2724,3 +2724,95 @@ ExecAlterExtensionContentsStmt(AlterExtensionContentsStmt *stmt)
27242724
if (relation != NULL)
27252725
relation_close(relation, NoLock);
27262726
}
2727+
2728+
/*
2729+
* AlterExtensionOwner_internal
2730+
*
2731+
* Internal routine for changing the owner of an extension. rel must be
2732+
* pg_extension, already open and suitably locked; it will not be closed.
2733+
*
2734+
* Note that this only changes ownership of the extension itself; it doesn't
2735+
* change the ownership of objects it contains. Since this function is
2736+
* currently only called from REASSIGN OWNED, this restriction is okay because
2737+
* said objects would also be affected by our caller. But it's not enough for
2738+
* a full-fledged ALTER OWNER implementation, so beware.
2739+
*/
2740+
static void
2741+
AlterExtensionOwner_internal(Relation rel, Oid extensionOid, Oid newOwnerId)
2742+
{
2743+
Form_pg_extension extForm;
2744+
HeapTuple tup;
2745+
SysScanDesc scandesc;
2746+
ScanKeyData entry[1];
2747+
2748+
Assert(RelationGetRelid(rel) == ExtensionRelationId);
2749+
2750+
ScanKeyInit(&entry[0],
2751+
ObjectIdAttributeNumber,
2752+
BTEqualStrategyNumber, F_OIDEQ,
2753+
ObjectIdGetDatum(extensionOid));
2754+
2755+
scandesc = systable_beginscan(rel, ExtensionOidIndexId, true,
2756+
SnapshotNow, 1, entry);
2757+
2758+
/* We assume that there can be at most one matching tuple */
2759+
tup = systable_getnext(scandesc);
2760+
if (!HeapTupleIsValid(tup)) /* should not happen */
2761+
elog(ERROR, "cache lookup failed for extension %u", extensionOid);
2762+
2763+
tup = heap_copytuple(tup);
2764+
systable_endscan(scandesc);
2765+
2766+
extForm = (Form_pg_extension) GETSTRUCT(tup);
2767+
2768+
/*
2769+
* If the new owner is the same as the existing owner, consider the
2770+
* command to have succeeded. This is for dump restoration purposes.
2771+
*/
2772+
if (extForm->extowner != newOwnerId)
2773+
{
2774+
/* Superusers can always do it */
2775+
if (!superuser())
2776+
{
2777+
/* Otherwise, must be owner of the existing object */
2778+
if (!pg_extension_ownercheck(HeapTupleGetOid(tup), GetUserId()))
2779+
aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_EXTENSION,
2780+
NameStr(extForm->extname));
2781+
2782+
/* Must be able to become new owner */
2783+
check_is_member_of_role(GetUserId(), newOwnerId);
2784+
2785+
/* no privilege checks on namespace are required */
2786+
}
2787+
2788+
/*
2789+
* Modify the owner --- okay to scribble on tup because it's a copy
2790+
*/
2791+
extForm->extowner = newOwnerId;
2792+
2793+
simple_heap_update(rel, &tup->t_self, tup);
2794+
2795+
CatalogUpdateIndexes(rel, tup);
2796+
2797+
/* Update owner dependency reference */
2798+
changeDependencyOnOwner(ExtensionRelationId, extensionOid,
2799+
newOwnerId);
2800+
}
2801+
2802+
heap_freetuple(tup);
2803+
}
2804+
2805+
/*
2806+
* Change extension owner, by OID
2807+
*/
2808+
void
2809+
AlterExtensionOwner_oid(Oid extensionOid, Oid newOwnerId)
2810+
{
2811+
Relation rel;
2812+
2813+
rel = heap_open(ExtensionRelationId, RowExclusiveLock);
2814+
2815+
AlterExtensionOwner_internal(rel, extensionOid, newOwnerId);
2816+
2817+
heap_close(rel, NoLock);
2818+
}

src/include/commands/extension.h

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

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

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

0 commit comments

Comments
 (0)