Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hSddg-00008B-FJ for pgsql-bugs@arkaria.postgresql.org; Mon, 20 May 2019 08:27:21 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hSdde-0000Ta-C8 for pgsql-bugs@arkaria.postgresql.org; Mon, 20 May 2019 08:27:18 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hSddc-0000TE-IZ for pgsql-bugs@lists.postgresql.org; Mon, 20 May 2019 08:27:18 +0000 Received: from out2-smtp.messagingengine.com ([66.111.4.26]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hSddZ-0004BE-7j for pgsql-bugs@lists.postgresql.org; Mon, 20 May 2019 08:27:15 +0000 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.nyi.internal (Postfix) with ESMTP id 5C9DF23E47; Mon, 20 May 2019 04:27:11 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute4.internal (MEProxy); Mon, 20 May 2019 04:27:11 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=paquier.xyz; h= date:from:to:cc:subject:message-id:references:mime-version :content-type:in-reply-to; s=fm1; bh=tldKgUKxG7kKJIpCnIcTPwkdvW4 aSS8kO3l44BoAIfM=; b=m7JbIPJrkW4wLFM/VhIzgsD8spsZyIFYaoljV9dYaL/ Uh8UoxK7IVz9c7CfMPEJPyF+Ii2E9EY/nd17gvoEoNOxdtnzHHgozSrPSRC64N2X LsbymyOoxEigKj8178oRNsshXT0CYSlMsSDVUeXI7gUmRxyHmfuINMwUrl9pQbq2 VnwRaUlbDYi+cfqn0OFv3KSzojxk8ua4CIgwnbXYdnt0DGdwWSjibPdDhxK633SY tmCfxAwtB6C8X54PiKDZ1618Gtcc85DkDMmaFuGZ+nck6lpcbFnE5GkMr0MtaL25 UzoqSL6/l7O5/C2Udrb/eUmPsrvfZmPOSzvTMYeYRrQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-proxy :x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm2; bh=tldKgU KxG7kKJIpCnIcTPwkdvW4aSS8kO3l44BoAIfM=; b=cm1R9XQhYTJzmKqkFKiFon QcZ4rfdhgMa7pzWjdk/FBR2N5WTYv0J64EPmsCACI21ras5kGLht4CHIdIQYd/fa lpWK5UTwDCNfAGQZB2aHvDVhsRk72IMR3kK46LaAUXaYOzYeAjjKV1Rl+7hZWjeo l0ckG1xTnu6XMRAcetEHgSrwx7lcURof2dQpmiBhNqTOac2uREUpDpSc+6rhdPBR E4DJA96yb4mW51VJb6/SNg1Cf2wj0PhUrjvuqAPdst8D2NFpIyb272rEKuSkL1yT Vnwc2h2CUBprGvIp3UuoOBvxQqBcckNfMdRVjZsio0/BuYf4qNOvbKp3NrOIiqqQ == X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduuddruddtkedgtdegucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne gfrhhlucfvnfffucdlfeehmdenucfjughrpeffhffvuffkfhggtggujgesghdtreertddt vdenucfhrhhomhepofhitghhrggvlhcurfgrqhhuihgvrhcuoehmihgthhgrvghlsehprg hquhhivghrrdighiiiqeenucffohhmrghinhepphhoshhtghhrvghsqhhlrdhorhhgnecu kfhppeduuddurddutddvrddukedtrddukeehnecurfgrrhgrmhepmhgrihhlfhhrohhmpe hmihgthhgrvghlsehprghquhhivghrrdighiiinecuvehluhhsthgvrhfuihiivgeptd X-ME-Proxy: Received: from paquier.xyz (ee0822lan1.rev.em-net.ne.jp [111.102.180.185]) by mail.messagingengine.com (Postfix) with ESMTPA id CEEE880060; Mon, 20 May 2019 04:27:09 -0400 (EDT) Date: Mon, 20 May 2019 17:27:05 +0900 From: Michael Paquier To: "Bossart, Nathan" Cc: Haribabu Kommi , "pgsql-bugs@lists.postgresql.org" Subject: Re: BUG #15788: 'pg_dump --create' orders database GRANTs incorrectly Message-ID: <20190520082705.GA1921@paquier.xyz> References: <15788-4e18847520ebcc75@postgresql.org> <9C9271CF-9A7A-45AE-B034-1246A93788C9@amazon.com> <4D066CB2-B177-40B4-8510-9318B9A184CE@amazon.com> <20190504123221.GA2127@paquier.xyz> <0AF6E4BC-EE9A-4158-85BE-3EAE4308D228@amazon.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="0eh6TmSyL6TZE2Uz" Content-Disposition: inline In-Reply-To: <0AF6E4BC-EE9A-4158-85BE-3EAE4308D228@amazon.com> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --0eh6TmSyL6TZE2Uz Content-Type: multipart/mixed; boundary="fdj2RfSjLxBAspz7" Content-Disposition: inline --fdj2RfSjLxBAspz7 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Mon, May 06, 2019 at 04:08:47PM +0000, Bossart, Nathan wrote: > Added here: https://commitfest.postgresql.org/23/2111/ Thanks for adding the patch to the CF. With your patch attached the difference in the dump is plain: REVOKE CONNECT,TEMPORARY ON DATABASE mydb FROM PUBLIC; +GRANT TEMPORARY ON DATABASE mydb TO c_user WITH GRANT OPTION; SET SESSION AUTHORIZATION c_user; GRANT TEMPORARY ON DATABASE mydb TO a_user; RESET SESSION AUTHORIZATION; -GRANT TEMPORARY ON DATABASE mydb TO c_user WITH GRANT OPTION; So what happens is that the GRANT command to a_user fails when switching to the session context to c_user as this user does not have yet the authorization to perform this command. If the GRANT permissions assigned to c_user are moved prior its actual actions then the restore is able to work. I have been looking at it, and wondered first if we could have just used buildACLQueries(), until I noticed that we don't support initial privileges on databases, so the patch you have sent looks fine to me. I had first a hard time parsing the subqueries added, so I have tweaked your patch with more indentation, and a comment block with more details about why we need to preserve the ACL ordering (you will note that I don't have a lot of imagination here). v12 beta1 is going to ship soon, so let's wait for the version to be tagged before committing it. -- Michael --fdj2RfSjLxBAspz7 Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="database-acls-v3.patch" Content-Transfer-Encoding: quoted-printable diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c index a8f1ad7333..8931fc4014 100644 --- a/src/bin/pg_dump/pg_dump.c +++ b/src/bin/pg_dump/pg_dump.c @@ -2664,20 +2664,41 @@ dumpDatabase(Archive *fout) =20 pg_log_info("saving database definition"); =20 - /* Fetch the database-level properties for this database */ + /* + * Fetch the database-level properties for this database. + * + * The order in which privileges are in the ACL string (the order they + * have been GRANT'd in, which the backend maintains) must be preserved to + * ensure that GRANTs WITH GRANT OPTION and subsequent GRANTs based on + * those are dumped in the correct order. Note that initial privileges + * (pg_init_privs) are not supported on databases, so this logic cannot + * make use of buildACLQueries(). + */ if (fout->remoteVersion >=3D 90600) { appendPQExpBuffer(dbQry, "SELECT tableoid, oid, datname, " "(%s datdba) AS dba, " "pg_encoding_to_char(encoding) AS encoding, " "datcollate, datctype, datfrozenxid, datminmxid, " - "(SELECT array_agg(acl ORDER BY acl::text COLLATE \"C\") FROM ( " - " SELECT unnest(coalesce(datacl,acldefault('d',datdba))) AS acl " - " EXCEPT SELECT unnest(acldefault('d',datdba))) as datacls)" + "(SELECT array_agg(acl ORDER BY row_n) FROM " + " (SELECT acl, row_n FROM " + " unnest(coalesce(datacl,acldefault('d',datdba))) " + " WITH ORDINALITY AS perm(acl,row_n) " + " WHERE NOT EXISTS ( " + " SELECT 1 " + " FROM unnest(acldefault('d',datdba)) " + " AS init(init_acl) " + " WHERE acl =3D init_acl)) as datacls) " " AS datacl, " - "(SELECT array_agg(acl ORDER BY acl::text COLLATE \"C\") FROM ( " - " SELECT unnest(acldefault('d',datdba)) AS acl " - " EXCEPT SELECT unnest(coalesce(datacl,acldefault('d',datdba)))) = as rdatacls)" + "(SELECT array_agg(acl ORDER BY row_n) FROM " + " (SELECT acl, row_n FROM " + " unnest(acldefault('d',datdba)) " + " WITH ORDINALITY AS initp(acl,row_n) " + " WHERE NOT EXISTS ( " + " SELECT 1 " + " FROM unnest(coalesce(datacl,acldefault('d',datdba))) " + " AS permp(orig_acl) " + " WHERE acl =3D orig_acl)) AS rdatacls)" " AS rdatacl, " "datistemplate, datconnlimit, " "(SELECT spcname FROM pg_tablespace t WHERE t.oid =3D dattablespac= e) AS tablespace, " --fdj2RfSjLxBAspz7-- --0eh6TmSyL6TZE2Uz Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEEG72nH6vTowiyblFKnvQgOdbyQH0FAlziZNgACgkQnvQgOdby QH08bBAAleehAYyxvBACl28hVznRKdlk0NdoliYRtQ2ZVjsrAu5iJBEfkB9Ljb4v CuXhvaJjeTWwZnGo4ZoFo7z+GvH0LRaOKRHvb3htzTL6VwQERqxZe1qhWh19dopS om4JEqXIFw68FLvMlApODg/oB+YqLPSC3r8pxZmS4CYS+kKzPPcQzVQtuyUrMaz8 t2sAzCwuGlFfA4ZcxzE4EWF/N5m4NDwdkHfrg5GKRtcd4aJlWogC8vQ3E1K/wKrC VI0y3uWCyAvrYA5WUDENdYLoSS8CyfAWVogZfwPqrg0Ha5ah6Azc2o005iteMOhl snZsajg3WQpdAQkQnAbrABlzX8a4p27PT9zpuwxGVvqCFG3mzUE4GABHnXn62yHs JN7o2dSfolx6KvFBAo0FXspp5maj7fBKgOHmttDq/Xr4wtK+xy27e7OldAZ2t9fP xHr6STMCi0Dctcz3MX3RJvFNB2UHREoxZIbbt8l4AM0e6UKzOFPEBW6WqhuBtZEv UP9+moGFFQ0pAG0/nx0+4aJ0W+0QuZ/OFzlZhMx4Bbh8r0OrUkZOFvCHqaRwrpbB WbiKHtXsrq6d72kH4q+EpGnr559wArDrecS/Tzu1Ow6WBr972nI5bYmIp1NrrGYT cRCFPs/sp+t1ep1EyuPRGdKfCnPzVfmkM5QdAY0D4vd9eMG8noQ= =haGR -----END PGP SIGNATURE----- --0eh6TmSyL6TZE2Uz--