From dd73227a7722aaa672117077ac8d9e64e19d8a2a Mon Sep 17 00:00:00 2001
From: Nathan Bossart <bossartn@amazon.com>
Date: Wed, 18 Aug 2021 16:30:35 +0000
Subject: [PATCH v1 1/1] fix pg_dump handling of ALTER DEFAULT PRIVILEGES IN
 SCHEMA

---
 src/bin/pg_dump/dumputils.c | 26 +++++++++++++++++++++-----
 src/bin/pg_dump/dumputils.h |  2 +-
 src/bin/pg_dump/pg_dump.c   | 24 ++++++++++++------------
 3 files changed, 34 insertions(+), 18 deletions(-)

diff --git a/src/bin/pg_dump/dumputils.c b/src/bin/pg_dump/dumputils.c
index ea67e52a3f..2f2cc272c0 100644
--- a/src/bin/pg_dump/dumputils.c
+++ b/src/bin/pg_dump/dumputils.c
@@ -726,7 +726,7 @@ buildACLQueries(PQExpBuffer acl_subquery, PQExpBuffer racl_subquery,
 				PQExpBuffer init_acl_subquery, PQExpBuffer init_racl_subquery,
 				const char *acl_column, const char *acl_owner,
 				const char *initprivs_expr,
-				const char *obj_kind, bool binary_upgrade)
+				const char *obj_kind, bool binary_upgrade, const char *case_stmt)
 {
 	/*
 	 * To get the delta from what the permissions were at creation time
@@ -762,32 +762,48 @@ buildACLQueries(PQExpBuffer acl_subquery, PQExpBuffer racl_subquery,
 	printfPQExpBuffer(acl_subquery,
 					  "(SELECT pg_catalog.array_agg(acl ORDER BY row_n) FROM "
 					  "(SELECT acl, row_n FROM "
-					  "pg_catalog.unnest(coalesce(%s,pg_catalog.acldefault(%s,%s))) "
+					  "pg_catalog.unnest(coalesce(%s,"
+					  "CASE WHEN %s THEN "
+					  "pg_catalog.acldefault(%s,%s) "
+					  "ELSE NULL END)) "
 					  "WITH ORDINALITY AS perm(acl,row_n) "
 					  "WHERE NOT EXISTS ( "
 					  "SELECT 1 FROM "
-					  "pg_catalog.unnest(coalesce(%s,pg_catalog.acldefault(%s,%s))) "
+					  "pg_catalog.unnest(coalesce(%s,"
+					  "CASE WHEN %s THEN "
+					  "pg_catalog.acldefault(%s,%s) "
+					  "ELSE NULL END)) "
 					  "AS init(init_acl) WHERE acl = init_acl)) as foo)",
 					  acl_column,
+					  case_stmt ? case_stmt : "TRUE",
 					  obj_kind,
 					  acl_owner,
 					  initprivs_expr,
+					  case_stmt ? case_stmt : "TRUE",
 					  obj_kind,
 					  acl_owner);
 
 	printfPQExpBuffer(racl_subquery,
 					  "(SELECT pg_catalog.array_agg(acl ORDER BY row_n) FROM "
 					  "(SELECT acl, row_n FROM "
-					  "pg_catalog.unnest(coalesce(%s,pg_catalog.acldefault(%s,%s))) "
+					  "pg_catalog.unnest(coalesce(%s,"
+					  "CASE WHEN %s THEN "
+					  "pg_catalog.acldefault(%s,%s) "
+					  "ELSE NULL END)) "
 					  "WITH ORDINALITY AS initp(acl,row_n) "
 					  "WHERE NOT EXISTS ( "
 					  "SELECT 1 FROM "
-					  "pg_catalog.unnest(coalesce(%s,pg_catalog.acldefault(%s,%s))) "
+					  "pg_catalog.unnest(coalesce(%s,"
+					  "CASE WHEN %s THEN "
+					  "pg_catalog.acldefault(%s,%s) "
+					  "ELSE NULL END)) "
 					  "AS permp(orig_acl) WHERE acl = orig_acl)) as foo)",
 					  initprivs_expr,
+					  case_stmt ? case_stmt : "TRUE",
 					  obj_kind,
 					  acl_owner,
 					  acl_column,
+					  case_stmt ? case_stmt : "TRUE",
 					  obj_kind,
 					  acl_owner);
 
diff --git a/src/bin/pg_dump/dumputils.h b/src/bin/pg_dump/dumputils.h
index f5465f19ae..b5b1792fb3 100644
--- a/src/bin/pg_dump/dumputils.h
+++ b/src/bin/pg_dump/dumputils.h
@@ -55,7 +55,7 @@ extern void buildACLQueries(PQExpBuffer acl_subquery, PQExpBuffer racl_subquery,
 							PQExpBuffer init_acl_subquery, PQExpBuffer init_racl_subquery,
 							const char *acl_column, const char *acl_owner,
 							const char *initprivs_expr,
-							const char *obj_kind, bool binary_upgrade);
+							const char *obj_kind, bool binary_upgrade, const char *case_stmt);
 
 extern bool variable_is_guc_list_quote(const char *name);
 
diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c
index 90ac445bcd..301868e4e6 100644
--- a/src/bin/pg_dump/pg_dump.c
+++ b/src/bin/pg_dump/pg_dump.c
@@ -3421,7 +3421,7 @@ getBlobs(Archive *fout)
 
 		buildACLQueries(acl_subquery, racl_subquery, init_acl_subquery,
 						init_racl_subquery, "l.lomacl", "l.lomowner",
-						"pip.initprivs", "'L'", dopt->binary_upgrade);
+						"pip.initprivs", "'L'", dopt->binary_upgrade, NULL);
 
 		appendPQExpBuffer(blobQry,
 						  "SELECT l.oid, (%s l.lomowner) AS rolname, "
@@ -4874,7 +4874,7 @@ getNamespaces(Archive *fout, int *numNamespaces)
 						"         n.nspowner::regrole, n.nspowner::regrole),"
 						"  format('=UC/%s', n.nspowner::regrole)]::aclitem[] "
 						"ELSE pip.initprivs END",
-						"'n'", dopt->binary_upgrade);
+						"'n'", dopt->binary_upgrade, NULL);
 
 		appendPQExpBuffer(query, "SELECT n.tableoid, n.oid, n.nspname, "
 						  "n.nspowner, "
@@ -5125,7 +5125,7 @@ getTypes(Archive *fout, int *numTypes)
 
 		buildACLQueries(acl_subquery, racl_subquery, initacl_subquery,
 						initracl_subquery, "t.typacl", "t.typowner",
-						"pip.initprivs", "'T'", dopt->binary_upgrade);
+						"pip.initprivs", "'T'", dopt->binary_upgrade, NULL);
 
 		appendPQExpBuffer(query, "SELECT t.tableoid, t.oid, t.typname, "
 						  "t.typnamespace, "
@@ -5827,7 +5827,7 @@ getAggregates(Archive *fout, int *numAggs)
 
 		buildACLQueries(acl_subquery, racl_subquery, initacl_subquery,
 						initracl_subquery, "p.proacl", "p.proowner",
-						"pip.initprivs", "'f'", dopt->binary_upgrade);
+						"pip.initprivs", "'f'", dopt->binary_upgrade, NULL);
 
 		agg_check = (fout->remoteVersion >= 110000 ? "p.prokind = 'a'"
 					 : "p.proisagg");
@@ -6040,7 +6040,7 @@ getFuncs(Archive *fout, int *numFuncs)
 
 		buildACLQueries(acl_subquery, racl_subquery, initacl_subquery,
 						initracl_subquery, "p.proacl", "p.proowner",
-						"pip.initprivs", "'f'", dopt->binary_upgrade);
+						"pip.initprivs", "'f'", dopt->binary_upgrade, NULL);
 
 		not_agg_check = (fout->remoteVersion >= 110000 ? "p.prokind <> 'a'"
 						 : "NOT p.proisagg");
@@ -6339,11 +6339,11 @@ getTables(Archive *fout, int *numTables)
 						"pip.initprivs",
 						"CASE WHEN c.relkind = " CppAsString2(RELKIND_SEQUENCE)
 						" THEN 's' ELSE 'r' END::\"char\"",
-						dopt->binary_upgrade);
+						dopt->binary_upgrade, NULL);
 
 		buildACLQueries(attacl_subquery, attracl_subquery, attinitacl_subquery,
 						attinitracl_subquery, "at.attacl", "c.relowner",
-						"pip.initprivs", "'c'", dopt->binary_upgrade);
+						"pip.initprivs", "'c'", dopt->binary_upgrade, NULL);
 
 		appendPQExpBuffer(query,
 						  "SELECT c.tableoid, c.oid, c.relname, "
@@ -8318,7 +8318,7 @@ getProcLangs(Archive *fout, int *numProcLangs)
 
 		buildACLQueries(acl_subquery, racl_subquery, initacl_subquery,
 						initracl_subquery, "l.lanacl", "l.lanowner",
-						"pip.initprivs", "'l'", dopt->binary_upgrade);
+						"pip.initprivs", "'l'", dopt->binary_upgrade, NULL);
 
 		/* pg_language has a laninline column */
 		appendPQExpBuffer(query, "SELECT l.tableoid, l.oid, "
@@ -9509,7 +9509,7 @@ getForeignDataWrappers(Archive *fout, int *numForeignDataWrappers)
 
 		buildACLQueries(acl_subquery, racl_subquery, initacl_subquery,
 						initracl_subquery, "f.fdwacl", "f.fdwowner",
-						"pip.initprivs", "'F'", dopt->binary_upgrade);
+						"pip.initprivs", "'F'", dopt->binary_upgrade, NULL);
 
 		appendPQExpBuffer(query, "SELECT f.tableoid, f.oid, f.fdwname, "
 						  "(%s f.fdwowner) AS rolname, "
@@ -9676,7 +9676,7 @@ getForeignServers(Archive *fout, int *numForeignServers)
 
 		buildACLQueries(acl_subquery, racl_subquery, initacl_subquery,
 						initracl_subquery, "f.srvacl", "f.srvowner",
-						"pip.initprivs", "'S'", dopt->binary_upgrade);
+						"pip.initprivs", "'S'", dopt->binary_upgrade, NULL);
 
 		appendPQExpBuffer(query, "SELECT f.tableoid, f.oid, f.srvname, "
 						  "(%s f.srvowner) AS rolname, "
@@ -9824,7 +9824,7 @@ getDefaultACLs(Archive *fout, int *numDefaultACLs)
 						initracl_subquery, "defaclacl", "defaclrole",
 						"pip.initprivs",
 						"CASE WHEN defaclobjtype = 'S' THEN 's' ELSE defaclobjtype END::\"char\"",
-						dopt->binary_upgrade);
+						dopt->binary_upgrade, "defaclnamespace = 0");
 
 		appendPQExpBuffer(query, "SELECT d.oid, d.tableoid, "
 						  "(%s d.defaclrole) AS defaclrole, "
@@ -15689,7 +15689,7 @@ dumpTable(Archive *fout, const TableInfo *tbinfo)
 
 			buildACLQueries(acl_subquery, racl_subquery, initacl_subquery,
 							initracl_subquery, "at.attacl", "c.relowner",
-							"pip.initprivs", "'c'", dopt->binary_upgrade);
+							"pip.initprivs", "'c'", dopt->binary_upgrade, NULL);
 
 			appendPQExpBuffer(query,
 							  "SELECT at.attname, "
-- 
2.16.6

