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

Commit 68a7c24

Browse files
committed
Fix ordering in pg_dump of GRANTs
The order in which GRANTs are output is important as GRANTs which have been GRANT'd by individuals via WITH GRANT OPTION GRANTs have to come after the GRANT which included the WITH GRANT OPTION. This happens naturally in the backend during normal operation as we only change existing ACLs in-place, only add new ACLs to the end, and when removing an ACL we remove any which depend on it also. Also, adjust the comments in acl.h to make this clear. Unfortunately, the updates to pg_dump to handle initial privileges involved pulling apart ACLs and then combining them back together and could end up putting them back together in an invalid order, leading to dumps which wouldn't restore. Fix this by adjusting the queries used by pg_dump to ensure that the ACLs are rebuilt in the same order in which they were originally. Back-patch to 9.6 where the changes for initial privileges were done.
1 parent eaf7001 commit 68a7c24

File tree

2 files changed

+47
-18
lines changed

2 files changed

+47
-18
lines changed

src/bin/pg_dump/dumputils.c

+36-15
Original file line numberDiff line numberDiff line change
@@ -722,21 +722,36 @@ buildACLQueries(PQExpBuffer acl_subquery, PQExpBuffer racl_subquery,
722722
* We always perform this delta on all ACLs and expect that by the time
723723
* these are run the initial privileges will be in place, even in a binary
724724
* upgrade situation (see below).
725+
*
726+
* Finally, the order in which privileges are in the ACL string (the order
727+
* they been GRANT'd in, which the backend maintains) must be preserved to
728+
* ensure that GRANTs WITH GRANT OPTION and subsequent GRANTs based on
729+
* those are dumped in the correct order.
725730
*/
726-
printfPQExpBuffer(acl_subquery, "(SELECT pg_catalog.array_agg(acl) FROM "
727-
"(SELECT pg_catalog.unnest(coalesce(%s,pg_catalog.acldefault(%s,%s))) AS acl "
728-
"EXCEPT "
729-
"SELECT pg_catalog.unnest(coalesce(pip.initprivs,pg_catalog.acldefault(%s,%s)))) as foo)",
731+
printfPQExpBuffer(acl_subquery,
732+
"(SELECT pg_catalog.array_agg(acl ORDER BY row_n) FROM "
733+
"(SELECT acl, row_n FROM "
734+
"pg_catalog.unnest(coalesce(%s,pg_catalog.acldefault(%s,%s))) "
735+
"WITH ORDINALITY AS perm(acl,row_n) "
736+
"WHERE NOT EXISTS ( "
737+
"SELECT 1 FROM "
738+
"pg_catalog.unnest(coalesce(pip.initprivs,pg_catalog.acldefault(%s,%s))) "
739+
"AS init(init_acl) WHERE acl = init_acl)) as foo)",
730740
acl_column,
731741
obj_kind,
732742
acl_owner,
733743
obj_kind,
734744
acl_owner);
735745

736-
printfPQExpBuffer(racl_subquery, "(SELECT pg_catalog.array_agg(acl) FROM "
737-
"(SELECT pg_catalog.unnest(coalesce(pip.initprivs,pg_catalog.acldefault(%s,%s))) AS acl "
738-
"EXCEPT "
739-
"SELECT pg_catalog.unnest(coalesce(%s,pg_catalog.acldefault(%s,%s)))) as foo)",
746+
printfPQExpBuffer(racl_subquery,
747+
"(SELECT pg_catalog.array_agg(acl ORDER BY row_n) FROM "
748+
"(SELECT acl, row_n FROM "
749+
"pg_catalog.unnest(coalesce(pip.initprivs,pg_catalog.acldefault(%s,%s))) "
750+
"WITH ORDINALITY AS initp(acl,row_n) "
751+
"WHERE NOT EXISTS ( "
752+
"SELECT 1 FROM "
753+
"pg_catalog.unnest(coalesce(%s,pg_catalog.acldefault(%s,%s))) "
754+
"AS permp(orig_acl) WHERE acl = orig_acl)) as foo)",
740755
obj_kind,
741756
acl_owner,
742757
acl_column,
@@ -761,19 +776,25 @@ buildACLQueries(PQExpBuffer acl_subquery, PQExpBuffer racl_subquery,
761776
{
762777
printfPQExpBuffer(init_acl_subquery,
763778
"CASE WHEN privtype = 'e' THEN "
764-
"(SELECT pg_catalog.array_agg(acl) FROM "
765-
"(SELECT pg_catalog.unnest(pip.initprivs) AS acl "
766-
"EXCEPT "
767-
"SELECT pg_catalog.unnest(pg_catalog.acldefault(%s,%s))) as foo) END",
779+
"(SELECT pg_catalog.array_agg(acl ORDER BY row_n) FROM "
780+
"(SELECT acl, row_n FROM pg_catalog.unnest(pip.initprivs) "
781+
"WITH ORDINALITY AS initp(acl,row_n) "
782+
"WHERE NOT EXISTS ( "
783+
"SELECT 1 FROM "
784+
"pg_catalog.unnest(pg_catalog.acldefault(%s,%s)) "
785+
"AS privm(orig_acl) WHERE acl = orig_acl)) as foo) END",
768786
obj_kind,
769787
acl_owner);
770788

771789
printfPQExpBuffer(init_racl_subquery,
772790
"CASE WHEN privtype = 'e' THEN "
773791
"(SELECT pg_catalog.array_agg(acl) FROM "
774-
"(SELECT pg_catalog.unnest(pg_catalog.acldefault(%s,%s)) AS acl "
775-
"EXCEPT "
776-
"SELECT pg_catalog.unnest(pip.initprivs)) as foo) END",
792+
"(SELECT acl, row_n FROM "
793+
"pg_catalog.unnest(pg_catalog.acldefault(%s,%s)) "
794+
"WITH ORDINALITY AS privp(acl,row_n) "
795+
"WHERE NOT EXISTS ( "
796+
"SELECT 1 FROM pg_catalog.unnest(pip.initprivs) "
797+
"AS initp(init_acl) WHERE acl = init_acl)) as foo) END",
777798
obj_kind,
778799
acl_owner);
779800
}

src/include/utils/acl.h

+11-3
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,17 @@
1212
* NOTES
1313
* An ACL array is simply an array of AclItems, representing the union
1414
* of the privileges represented by the individual items. A zero-length
15-
* array represents "no privileges". There are no assumptions about the
16-
* ordering of the items, but we do expect that there are no two entries
17-
* in the array with the same grantor and grantee.
15+
* array represents "no privileges".
16+
*
17+
* The order of items in the array is important as client utilities (in
18+
* particular, pg_dump, though possibly other clients) expect to be able
19+
* to issue GRANTs in the ordering of the items in the array. The reason
20+
* this matters is that GRANTs WITH GRANT OPTION must be before any GRANTs
21+
* which depend on it. This happens naturally in the backend during
22+
* operations as we update ACLs in-place, new items are appended, and
23+
* existing entries are only removed if there's no dependency on them (no
24+
* GRANT can been based on it, or, if there was, those GRANTs are also
25+
* removed).
1826
*
1927
* For backward-compatibility purposes we have to allow null ACL entries
2028
* in system catalogs. A null ACL will be treated as meaning "default

0 commit comments

Comments
 (0)