Fix ALTER DEFAULT PRIVILEGES with duplicated objects
authorMichael Paquier <michael@paquier.xyz>
Wed, 20 Jan 2021 02:38:17 +0000 (11:38 +0900)
committerMichael Paquier <michael@paquier.xyz>
Wed, 20 Jan 2021 02:38:17 +0000 (11:38 +0900)
Specifying duplicated objects in this command would lead to unique
constraint violations in pg_default_acl or "tuple already updated by
self" errors.  Similarly to GRANT/REVOKE, increment the command ID after
each subcommand processing to allow this case to work transparently.

A regression test is added by tweaking one of the existing queries of
privileges.sql to stress this case.

Reported-by: Andrus
Author: Michael Paquier
Reviewed-by: Álvaro Herrera
Discussion: https://postgr.es/m/ae2a7dc1-9d71-8cba-3bb9-e4cb7eb1f44e@hot.ee
Backpatch-through: 9.5

src/backend/catalog/aclchk.c
src/test/regress/expected/privileges.out
src/test/regress/sql/privileges.sql

index 1a81e768eccd580906dc030fc33ed084274172f9..f3c1ca18ae7476ec623bb7265afd3e08d947f5f7 100644 (file)
@@ -1365,6 +1365,9 @@ SetDefaultACL(InternalDefaultACL *iacls)
                ReleaseSysCache(tuple);
 
        table_close(rel, RowExclusiveLock);
+
+       /* prevent error when processing duplicate objects */
+       CommandCounterIncrement();
 }
 
 
index 7754c20db47aa4c59410d75688fb7a9537d65fe1..5e5f98ac68a5806722c6c9e4c749393ec06255c0 100644 (file)
@@ -1649,7 +1649,8 @@ SELECT has_table_privilege('regress_priv_user1', 'testns.acltest1', 'INSERT'); -
  f
 (1 row)
 
-ALTER DEFAULT PRIVILEGES IN SCHEMA testns GRANT SELECT ON TABLES TO public;
+-- placeholder for test with duplicated schema and role names
+ALTER DEFAULT PRIVILEGES IN SCHEMA testns,testns GRANT SELECT ON TABLES TO public,public;
 SELECT has_table_privilege('regress_priv_user1', 'testns.acltest1', 'SELECT'); -- no
  has_table_privilege 
 ---------------------
index 4911ad4add8478cb35750e185291c1273d97702b..fff76e0bd08d174c4013835b7fac0e33c6e0a100 100644 (file)
@@ -985,7 +985,8 @@ CREATE TABLE testns.acltest1 (x int);
 SELECT has_table_privilege('regress_priv_user1', 'testns.acltest1', 'SELECT'); -- no
 SELECT has_table_privilege('regress_priv_user1', 'testns.acltest1', 'INSERT'); -- no
 
-ALTER DEFAULT PRIVILEGES IN SCHEMA testns GRANT SELECT ON TABLES TO public;
+-- placeholder for test with duplicated schema and role names
+ALTER DEFAULT PRIVILEGES IN SCHEMA testns,testns GRANT SELECT ON TABLES TO public,public;
 
 SELECT has_table_privilege('regress_priv_user1', 'testns.acltest1', 'SELECT'); -- no
 SELECT has_table_privilege('regress_priv_user1', 'testns.acltest1', 'INSERT'); -- no