Skip to content

Commit 0c2a5a8

Browse files
committed
Fix ordering of GRANT commands in pg_dumpall for database creation
This uses a method similar to 68a7c24, which guarantees that GRANT commands using the WITH GRANT OPTION are dumped in a way so as cascading dependencies are respected. As databases do not have support for initial privileges via pg_init_privs, we need to repeat again the same ACL reordering method. ACL for databases have been moved from pg_dumpall to pg_dump in v11, so this impacts pg_dump for v11 and above, and pg_dumpall for v9.6 and v10. Discussion: https://postgr.es/m/15788-4e18847520ebcc75@postgresql.org Author: Nathan Bossart Reviewed-by: Haribabu Kommi Backpatch-through: 9.6
1 parent 7f920b8 commit 0c2a5a8

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

src/bin/pg_dump/pg_dumpall.c

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1349,8 +1349,13 @@ dumpCreateDB(PGconn *conn)
13491349
*
13501350
* See buildACLQueries() and buildACLCommands().
13511351
*
1352+
* The order in which privileges are in the ACL string (the order they
1353+
* have been GRANT'd in, which the backend maintains) must be preserved to
1354+
* ensure that GRANTs WITH GRANT OPTION and subsequent GRANTs based on
1355+
* those are dumped in the correct order.
1356+
*
13521357
* Note that we do not support initial privileges (pg_init_privs) on
1353-
* databases.
1358+
* databases, so this logic cannot make use of buildACLQueries().
13541359
*/
13551360
if (server_version >= 90600)
13561361
printfPQExpBuffer(buf,
@@ -1359,14 +1364,26 @@ dumpCreateDB(PGconn *conn)
13591364
"pg_encoding_to_char(d.encoding), "
13601365
"datcollate, datctype, datfrozenxid, datminmxid, "
13611366
"datistemplate, "
1362-
"(SELECT pg_catalog.array_agg(acl ORDER BY acl::text COLLATE \"C\") FROM ( "
1363-
" SELECT pg_catalog.unnest(coalesce(datacl,pg_catalog.acldefault('d',datdba))) AS acl "
1364-
" EXCEPT SELECT pg_catalog.unnest(pg_catalog.acldefault('d',datdba))) as datacls)"
1365-
"AS datacl, "
1366-
"(SELECT pg_catalog.array_agg(acl ORDER BY acl::text COLLATE \"C\") FROM ( "
1367-
" SELECT pg_catalog.unnest(pg_catalog.acldefault('d',datdba)) AS acl "
1368-
" EXCEPT SELECT pg_catalog.unnest(coalesce(datacl,pg_catalog.acldefault('d',datdba)))) as rdatacls)"
1369-
"AS rdatacl, "
1367+
"(SELECT array_agg(acl ORDER BY row_n) FROM "
1368+
" (SELECT acl, row_n FROM "
1369+
" unnest(coalesce(datacl,acldefault('d',datdba))) "
1370+
" WITH ORDINALITY AS perm(acl,row_n) "
1371+
" WHERE NOT EXISTS ( "
1372+
" SELECT 1 "
1373+
" FROM unnest(acldefault('d',datdba)) "
1374+
" AS init(init_acl) "
1375+
" WHERE acl = init_acl)) AS datacls) "
1376+
" AS datacl, "
1377+
"(SELECT array_agg(acl ORDER BY row_n) FROM "
1378+
" (SELECT acl, row_n FROM "
1379+
" unnest(acldefault('d',datdba)) "
1380+
" WITH ORDINALITY AS initp(acl,row_n) "
1381+
" WHERE NOT EXISTS ( "
1382+
" SELECT 1 "
1383+
" FROM unnest(coalesce(datacl,acldefault('d',datdba))) "
1384+
" AS permp(orig_acl) "
1385+
" WHERE acl = orig_acl)) AS rdatacls) "
1386+
" AS rdatacl, "
13701387
"datconnlimit, "
13711388
"(SELECT spcname FROM pg_tablespace t WHERE t.oid = d.dattablespace) AS dattablespace "
13721389
"FROM pg_database d LEFT JOIN %s u ON (datdba = u.oid) "

0 commit comments

Comments
 (0)