Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd07afa4

Browse files
committed
Rework internals of changing a type's ownership
This is necessary so that REASSIGN OWNED does the right thing withcomposite types, to wit, that it also alters ownership of the type'spg_class entry -- previously, the pg_class entry remained owned by theoriginal user, which caused later other failures such as the new owner'sinability to use ALTER TYPE to rename an attribute of the affectedcomposite. Also, if the original owner is later dropped, the pg_classentry becomes owned by a non-existant user which is bogus.To fix, create a new routine AlterTypeOwner_oid which knows whether topass the request to ATExecChangeOwner or deal with it directly, and usethat in shdepReassignOwner rather than calling AlterTypeOwnerInternaldirectly. AlterTypeOwnerInternal is now simpler in that it onlymodifies the pg_type entry and recurses to handle a possible array type;higher-level tasks are handled by either AlterTypeOwner directly orAlterTypeOwner_oid.I took the opportunity to add a few more objects to the test rig forREASSIGN OWNED, so that more cases are exercised. Additional ones couldbe added for superuser-only-ownable objects (such as FDWs and eventtriggers) but I didn't want to push my luck by adding a new superuser tothe tests on a backpatchable bug fix.Per bug #13666 reported by Chris Pacejo.This is a backpatch of commit756e7b4 to branches 9.1 -- 9.4.
1 parent2c8ae64 commitd07afa4

File tree

6 files changed

+97
-73
lines changed

6 files changed

+97
-73
lines changed

‎src/backend/catalog/pg_shdepend.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1350,7 +1350,7 @@ shdepReassignOwned(List *roleids, Oid newrole)
13501350
switch (sdepForm->classid)
13511351
{
13521352
caseTypeRelationId:
1353-
AlterTypeOwnerInternal(sdepForm->objid,newrole, true);
1353+
AlterTypeOwner_oid(sdepForm->objid,newrole, true);
13541354
break;
13551355

13561356
caseNamespaceRelationId:

‎src/backend/commands/tablecmds.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8641,8 +8641,7 @@ ATExecChangeOwner(Oid relationOid, Oid newOwnerId, bool recursing, LOCKMODE lock
86418641
* Also change the ownership of the table's row type, if it has one
86428642
*/
86438643
if (tuple_class->relkind!=RELKIND_INDEX)
8644-
AlterTypeOwnerInternal(tuple_class->reltype,newOwnerId,
8645-
tuple_class->relkind==RELKIND_COMPOSITE_TYPE);
8644+
AlterTypeOwnerInternal(tuple_class->reltype,newOwnerId);
86468645

86478646
/*
86488647
* If we are operating on a table or materialized view, also change

‎src/backend/commands/typecmds.c

Lines changed: 47 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -3315,81 +3315,68 @@ AlterTypeOwner(List *names, Oid newOwnerId, ObjectType objecttype)
33153315
get_namespace_name(typTup->typnamespace));
33163316
}
33173317

3318-
/*
3319-
* If it's a composite type, invoke ATExecChangeOwner so that we fix
3320-
* up the pg_class entry properly. That will call back to
3321-
* AlterTypeOwnerInternal to take care of the pg_type entry(s).
3322-
*/
3323-
if (typTup->typtype==TYPTYPE_COMPOSITE)
3324-
ATExecChangeOwner(typTup->typrelid,newOwnerId, true,AccessExclusiveLock);
3325-
else
3326-
{
3327-
Datumrepl_val[Natts_pg_type];
3328-
boolrepl_null[Natts_pg_type];
3329-
boolrepl_repl[Natts_pg_type];
3330-
Acl*newAcl;
3331-
DatumaclDatum;
3332-
boolisNull;
3333-
3334-
memset(repl_null, false,sizeof(repl_null));
3335-
memset(repl_repl, false,sizeof(repl_repl));
3318+
AlterTypeOwner_oid(typeOid,newOwnerId, true);
3319+
}
33363320

3337-
repl_repl[Anum_pg_type_typowner-1]= true;
3338-
repl_val[Anum_pg_type_typowner-1]=ObjectIdGetDatum(newOwnerId);
3321+
/* Clean up */
3322+
heap_close(rel,RowExclusiveLock);
33393323

3340-
aclDatum=heap_getattr(tup,
3341-
Anum_pg_type_typacl,
3342-
RelationGetDescr(rel),
3343-
&isNull);
3344-
/* Null ACLs do not require changes */
3345-
if (!isNull)
3346-
{
3347-
newAcl=aclnewowner(DatumGetAclP(aclDatum),
3348-
typTup->typowner,newOwnerId);
3349-
repl_repl[Anum_pg_type_typacl-1]= true;
3350-
repl_val[Anum_pg_type_typacl-1]=PointerGetDatum(newAcl);
3351-
}
3324+
returntypeOid;
3325+
}
33523326

3353-
tup=heap_modify_tuple(tup,RelationGetDescr(rel),repl_val,repl_null,
3354-
repl_repl);
3327+
/*
3328+
* AlterTypeOwner_oid - change type owner unconditionally
3329+
*
3330+
* This function recurses to handle a pg_class entry, if necessary. It
3331+
* invokes any necessary access object hooks. If hasDependEntry is TRUE, this
3332+
* function modifies the pg_shdepend entry appropriately (this should be
3333+
* passed as FALSE only for table rowtypes and array types).
3334+
*
3335+
* This is used by ALTER TABLE/TYPE OWNER commands, as well as by REASSIGN
3336+
* OWNED BY. It assumes the caller has done all needed check.
3337+
*/
3338+
void
3339+
AlterTypeOwner_oid(OidtypeOid,OidnewOwnerId,boolhasDependEntry)
3340+
{
3341+
Relationrel;
3342+
HeapTupletup;
3343+
Form_pg_typetypTup;
33553344

3356-
simple_heap_update(rel,&tup->t_self,tup);
3345+
rel=heap_open(TypeRelationId,RowExclusiveLock);
33573346

3358-
CatalogUpdateIndexes(rel,tup);
3347+
tup=SearchSysCache1(TYPEOID,ObjectIdGetDatum(typeOid));
3348+
if (!HeapTupleIsValid(tup))
3349+
elog(ERROR,"cache lookup failed for type %u",typeOid);
3350+
typTup= (Form_pg_type)GETSTRUCT(tup);
33593351

3360-
/* Update owner dependency reference */
3361-
changeDependencyOnOwner(TypeRelationId,typeOid,newOwnerId);
3352+
/*
3353+
* If it's a composite type, invoke ATExecChangeOwner so that we fix up the
3354+
* pg_class entry properly. That will call back to AlterTypeOwnerInternal
3355+
* to take care of the pg_type entry(s).
3356+
*/
3357+
if (typTup->typtype==TYPTYPE_COMPOSITE)
3358+
ATExecChangeOwner(typTup->typrelid,newOwnerId, true,AccessExclusiveLock);
3359+
else
3360+
AlterTypeOwnerInternal(typeOid,newOwnerId);
33623361

3363-
InvokeObjectPostAlterHook(TypeRelationId,typeOid,0);
3362+
/* Update owner dependency reference */
3363+
if (hasDependEntry)
3364+
changeDependencyOnOwner(TypeRelationId,typeOid,newOwnerId);
33643365

3365-
/* If it has an array type, update that too */
3366-
if (OidIsValid(typTup->typarray))
3367-
AlterTypeOwnerInternal(typTup->typarray,newOwnerId, false);
3368-
}
3369-
}
3366+
InvokeObjectPostAlterHook(TypeRelationId,typeOid,0);
33703367

3371-
/* Clean up */
3368+
ReleaseSysCache(tup);
33723369
heap_close(rel,RowExclusiveLock);
3373-
3374-
returntypeOid;
33753370
}
33763371

33773372
/*
3378-
* AlterTypeOwnerInternal - change type owner unconditionally
3379-
*
3380-
* This is currently only used to propagate ALTER TABLE/TYPE OWNER to a
3381-
* table's rowtype or an array type, and to implement REASSIGN OWNED BY.
3382-
* It assumes the caller has done all needed checks. The function will
3383-
* automatically recurse to an array type if the type has one.
3373+
* AlterTypeOwnerInternal - bare-bones type owner change.
33843374
*
3385-
* hasDependEntry should be TRUE if type is expected to have a pg_shdepend
3386-
* entry (ie, it's not a table rowtype nor an array type).
3387-
* is_primary_ops should be TRUE if this function is invoked with user's
3388-
* direct operation (e.g, shdepReassignOwned). Elsewhere,
3375+
* This routine simply modifies the owner of a pg_type entry, and recurses
3376+
* to handle a possible array type.
33893377
*/
33903378
void
3391-
AlterTypeOwnerInternal(OidtypeOid,OidnewOwnerId,
3392-
boolhasDependEntry)
3379+
AlterTypeOwnerInternal(OidtypeOid,OidnewOwnerId)
33933380
{
33943381
Relationrel;
33953382
HeapTupletup;
@@ -3434,15 +3421,9 @@ AlterTypeOwnerInternal(Oid typeOid, Oid newOwnerId,
34343421

34353422
CatalogUpdateIndexes(rel,tup);
34363423

3437-
/* Update owner dependency reference, if it has one */
3438-
if (hasDependEntry)
3439-
changeDependencyOnOwner(TypeRelationId,typeOid,newOwnerId);
3440-
34413424
/* If it has an array type, update that too */
34423425
if (OidIsValid(typTup->typarray))
3443-
AlterTypeOwnerInternal(typTup->typarray,newOwnerId, false);
3444-
3445-
InvokeObjectPostAlterHook(TypeRelationId,typeOid,0);
3426+
AlterTypeOwnerInternal(typTup->typarray,newOwnerId);
34463427

34473428
/* Clean up */
34483429
heap_close(rel,RowExclusiveLock);

‎src/include/commands/typecmds.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ extern List *GetDomainConstraints(Oid typeOid);
4343

4444
externOidRenameType(RenameStmt*stmt);
4545
externOidAlterTypeOwner(List*names,OidnewOwnerId,ObjectTypeobjecttype);
46-
externvoidAlterTypeOwnerInternal(OidtypeOid,OidnewOwnerId,
47-
boolhasDependEntry);
46+
externvoidAlterTypeOwner_oid(OidtypeOid,OidnewOwnerId,boolhasDependEntry);
47+
externvoidAlterTypeOwnerInternal(OidtypeOid,OidnewOwnerId);
4848
externOidAlterTypeNamespace(List*names,constchar*newschema,ObjectTypeobjecttype);
4949
externOidAlterTypeNamespace_oid(OidtypeOid,OidnspOid,ObjectAddresses*objsMoved);
5050
externOidAlterTypeNamespaceInternal(OidtypeOid,OidnspOid,

‎src/test/regress/expected/dependency.out

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,31 @@ DROP OWNED BY regression_user1;
8484
\d deptest
8585
-- Test REASSIGN OWNED
8686
GRANT ALL ON deptest1 TO regression_user1;
87+
GRANT CREATE ON DATABASE regression TO regression_user1;
8788
SET SESSION AUTHORIZATION regression_user1;
89+
CREATE SCHEMA deptest;
8890
CREATE TABLE deptest (a serial primary key, b text);
91+
ALTER DEFAULT PRIVILEGES FOR ROLE regression_user1 IN SCHEMA deptest
92+
GRANT ALL ON TABLES TO regression_user2;
93+
CREATE FUNCTION deptest_func() RETURNS void LANGUAGE plpgsql
94+
AS $$ BEGIN END; $$;
95+
CREATE TYPE deptest_enum AS ENUM ('red');
96+
CREATE TYPE deptest_range AS RANGE (SUBTYPE = int4);
8997
CREATE TABLE deptest2 (f1 int);
9098
-- make a serial column the hard way
9199
CREATE SEQUENCE ss1;
92100
ALTER TABLE deptest2 ALTER f1 SET DEFAULT nextval('ss1');
93101
ALTER SEQUENCE ss1 OWNED BY deptest2.f1;
102+
-- When reassigning ownership of a composite type, its pg_class entry
103+
-- should match
104+
CREATE TYPE deptest_t AS (a int);
105+
SELECT typowner = relowner
106+
FROM pg_type JOIN pg_class c ON typrelid = c.oid WHERE typname = 'deptest_t';
107+
?column?
108+
----------
109+
t
110+
(1 row)
111+
94112
RESET SESSION AUTHORIZATION;
95113
REASSIGN OWNED BY regression_user1 TO regression_user2;
96114
\dt deptest
@@ -100,10 +118,19 @@ REASSIGN OWNED BY regression_user1 TO regression_user2;
100118
public | deptest | table | regression_user2
101119
(1 row)
102120

121+
SELECT typowner = relowner
122+
FROM pg_type JOIN pg_class c ON typrelid = c.oid WHERE typname = 'deptest_t';
123+
?column?
124+
----------
125+
t
126+
(1 row)
127+
103128
-- doesn't work: grant still exists
104129
DROP USER regression_user1;
105130
ERROR: role "regression_user1" cannot be dropped because some objects depend on it
106-
DETAIL: privileges for table deptest1
131+
DETAIL: owner of default privileges on new relations belonging to role regression_user1 in schema deptest
132+
privileges for database regression
133+
privileges for table deptest1
107134
DROP OWNED BY regression_user1;
108135
DROP USER regression_user1;
109136
\set VERBOSITY terse

‎src/test/regress/sql/dependency.sql

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,20 +74,37 @@ DROP OWNED BY regression_user1;
7474

7575
-- Test REASSIGN OWNED
7676
GRANT ALLON deptest1 TO regression_user1;
77+
GRANT CREATEON DATABASE regression TO regression_user1;
7778

7879
SET SESSION AUTHORIZATION regression_user1;
80+
CREATESCHEMAdeptest;
7981
CREATETABLEdeptest (aserialprimary key, btext);
82+
ALTER DEFAULT PRIVILEGES FOR ROLE regression_user1IN SCHEMA deptest
83+
GRANT ALLON TABLES TO regression_user2;
84+
CREATEFUNCTIONdeptest_func() RETURNS void LANGUAGE plpgsql
85+
AS $$BEGIN END; $$;
86+
CREATETYPEdeptest_enumAS ENUM ('red');
87+
CREATETYPEdeptest_rangeAS RANGE (SUBTYPE= int4);
8088

8189
CREATETABLEdeptest2 (f1int);
8290
-- make a serial column the hard way
8391
CREATESEQUENCEss1;
8492
ALTERTABLE deptest2 ALTER f1SET DEFAULT nextval('ss1');
8593
ALTERSEQUENCE ss1 OWNED BYdeptest2.f1;
86-
RESET SESSION AUTHORIZATION;
8794

95+
-- When reassigning ownership of a composite type, its pg_class entry
96+
-- should match
97+
CREATETYPEdeptest_tAS (aint);
98+
SELECT typowner= relowner
99+
FROM pg_typeJOIN pg_class cON typrelid=c.oidWHERE typname='deptest_t';
100+
101+
RESET SESSION AUTHORIZATION;
88102
REASSIGN OWNED BY regression_user1 TO regression_user2;
89103
\dt deptest
90104

105+
SELECT typowner= relowner
106+
FROM pg_typeJOIN pg_class cON typrelid=c.oidWHERE typname='deptest_t';
107+
91108
-- doesn't work: grant still exists
92109
DROPUSER regression_user1;
93110
DROP OWNED BY regression_user1;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp