Skip to content

Commit 50b797d

Browse files
committed
Fix DROP ROLE when specifying duplicated roles
This commit fixes failures with "tuple already updated by self" when listing twice the same role and in a DROP ROLE query. This is an oversight in 6566133, that has introduced a two-phase logic in DropRole() where dependencies of all the roles to drop are removed in a first phase, with the roles themselves removed from pg_authid in a second phase. The code is simplified to not rely on a List of ObjectAddress built in the first phase used to remove the pg_authid entries in the second phase, switching to a list of OIDs. Duplicated OIDs can be simply avoided in the first phase thanks to that. Using ObjectAddress was not necessary for the roles as they are not used for anything specific to dependency.c, building all the ObjectAddress in the List with AuthIdRelationId as class ID. In 15 and older versions, where a single phase is used, DROP ROLE with duplicated role names would fail on "role \"blah\" does not exist" for the second entry after the CCI() done by the first deletion. This is not really incorrect, but it does not seem worth changing based on a lack of complaints. Reported-by: Alexander Lakhin Reviewed-by: Tender Wang Discussion: https://postgr.es/m/18310-1eb233c5908189c8@postgresql.org Backpatch-through: 16
1 parent a3a836f commit 50b797d

File tree

3 files changed

+9
-13
lines changed

3 files changed

+9
-13
lines changed

src/backend/commands/user.c

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,7 +1093,7 @@ DropRole(DropRoleStmt *stmt)
10931093
Relation pg_authid_rel,
10941094
pg_auth_members_rel;
10951095
ListCell *item;
1096-
List *role_addresses = NIL;
1096+
List *role_oids = NIL;
10971097

10981098
if (!have_createrole_privilege())
10991099
ereport(ERROR,
@@ -1119,7 +1119,6 @@ DropRole(DropRoleStmt *stmt)
11191119
ScanKeyData scankey;
11201120
SysScanDesc sscan;
11211121
Oid roleid;
1122-
ObjectAddress *role_address;
11231122

11241123
if (rolspec->roletype != ROLESPEC_CSTRING)
11251124
ereport(ERROR,
@@ -1260,21 +1259,16 @@ DropRole(DropRoleStmt *stmt)
12601259
*/
12611260
CommandCounterIncrement();
12621261

1263-
/* Looks tentatively OK, add it to the list. */
1264-
role_address = palloc(sizeof(ObjectAddress));
1265-
role_address->classId = AuthIdRelationId;
1266-
role_address->objectId = roleid;
1267-
role_address->objectSubId = 0;
1268-
role_addresses = lappend(role_addresses, role_address);
1262+
/* Looks tentatively OK, add it to the list if not there yet. */
1263+
role_oids = list_append_unique_oid(role_oids, roleid);
12691264
}
12701265

12711266
/*
12721267
* Second pass over the roles to be removed.
12731268
*/
1274-
foreach(item, role_addresses)
1269+
foreach(item, role_oids)
12751270
{
1276-
ObjectAddress *role_address = lfirst(item);
1277-
Oid roleid = role_address->objectId;
1271+
Oid roleid = lfirst_oid(item);
12781272
HeapTuple tuple;
12791273
Form_pg_authid roleform;
12801274
char *detail;

src/test/regress/expected/create_role.out

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,8 @@ DROP INDEX tenant_idx;
251251
DROP TABLE tenant_table;
252252
DROP VIEW tenant_view;
253253
DROP SCHEMA regress_tenant2_schema;
254-
DROP ROLE regress_tenant;
254+
-- check for duplicated drop
255+
DROP ROLE regress_tenant, regress_tenant;
255256
DROP ROLE regress_tenant2;
256257
DROP ROLE regress_rolecreator;
257258
DROP ROLE regress_role_admin;

src/test/regress/sql/create_role.sql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,8 @@ DROP INDEX tenant_idx;
206206
DROP TABLE tenant_table;
207207
DROP VIEW tenant_view;
208208
DROP SCHEMA regress_tenant2_schema;
209-
DROP ROLE regress_tenant;
209+
-- check for duplicated drop
210+
DROP ROLE regress_tenant, regress_tenant;
210211
DROP ROLE regress_tenant2;
211212
DROP ROLE regress_rolecreator;
212213
DROP ROLE regress_role_admin;

0 commit comments

Comments
 (0)