Skip to content

Commit 3352c23

Browse files
committed
Fix privilege dumping from servers too old to have that type of privilege.
pg_dump produced fairly silly GRANT/REVOKE commands when dumping types from pre-9.2 servers, and when dumping functions or procedural languages from pre-7.3 servers. Those server versions lack the typacl, proacl, and/or lanacl columns respectively, and pg_dump substituted default values that were in fact incorrect. We ended up revoking all the owner's own privileges for the object while granting all privileges to PUBLIC. Of course the owner would then have those privileges again via PUBLIC, so long as she did not try to revoke PUBLIC's privileges; which may explain the lack of field reports. Nonetheless this is pretty silly behavior. The stakes were raised by my recent patch to make pg_dump dump shell types, because 9.2 and up pg_dump would proceed to emit bogus GRANT/REVOKE commands for a shell type if dumping from a pre-9.2 server; and the server will not accept GRANT/REVOKE commands for a shell type. (Perhaps it should, but that's a topic for another day.) So the resulting dump script wouldn't load without errors. The right thing to do is to act as though these objects have default privileges (null ACL entries), which causes pg_dump to print no GRANT/REVOKE commands at all for them. That fixes the silly results and also dodges the problem with shell types. In passing, modify getProcLangs() to be less creatively different about how to handle missing columns when dumping from older server versions. Every other data-acquisition function in pg_dump does that by substituting appropriate default values in the version-specific SQL commands, and I see no reason why this one should march to its own drummer. Its use of "SELECT *" was likewise not conformant with anyplace else, not to mention it's not considered good SQL style for production queries. Back-patch to all supported versions. Although 9.0 and 9.1 pg_dump don't have the issue with typacl, they are more likely than newer versions to be used to dump from ancient servers, so we ought to fix the proacl/lanacl issues all the way back.
1 parent 7e0add3 commit 3352c23

File tree

1 file changed

+47
-38
lines changed

1 file changed

+47
-38
lines changed

src/bin/pg_dump/pg_dump.c

+47-38
Original file line numberDiff line numberDiff line change
@@ -3268,7 +3268,7 @@ getTypes(Archive *fout, int *numTypes)
32683268
else if (fout->remoteVersion >= 80300)
32693269
{
32703270
appendPQExpBuffer(query, "SELECT tableoid, oid, typname, "
3271-
"typnamespace, '{=U}' AS typacl, "
3271+
"typnamespace, NULL AS typacl, "
32723272
"(%s typowner) AS rolname, "
32733273
"typinput::oid AS typinput, "
32743274
"typoutput::oid AS typoutput, typelem, typrelid, "
@@ -3283,7 +3283,7 @@ getTypes(Archive *fout, int *numTypes)
32833283
else if (fout->remoteVersion >= 70300)
32843284
{
32853285
appendPQExpBuffer(query, "SELECT tableoid, oid, typname, "
3286-
"typnamespace, '{=U}' AS typacl, "
3286+
"typnamespace, NULL AS typacl, "
32873287
"(%s typowner) AS rolname, "
32883288
"typinput::oid AS typinput, "
32893289
"typoutput::oid AS typoutput, typelem, typrelid, "
@@ -3297,7 +3297,7 @@ getTypes(Archive *fout, int *numTypes)
32973297
else if (fout->remoteVersion >= 70100)
32983298
{
32993299
appendPQExpBuffer(query, "SELECT tableoid, oid, typname, "
3300-
"0::oid AS typnamespace, '{=U}' AS typacl, "
3300+
"0::oid AS typnamespace, NULL AS typacl, "
33013301
"(%s typowner) AS rolname, "
33023302
"typinput::oid AS typinput, "
33033303
"typoutput::oid AS typoutput, typelem, typrelid, "
@@ -3313,7 +3313,7 @@ getTypes(Archive *fout, int *numTypes)
33133313
appendPQExpBuffer(query, "SELECT "
33143314
"(SELECT oid FROM pg_class WHERE relname = 'pg_type') AS tableoid, "
33153315
"oid, typname, "
3316-
"0::oid AS typnamespace, '{=U}' AS typacl, "
3316+
"0::oid AS typnamespace, NULL AS typacl, "
33173317
"(%s typowner) AS rolname, "
33183318
"typinput::oid AS typinput, "
33193319
"typoutput::oid AS typoutput, typelem, typrelid, "
@@ -4004,7 +4004,7 @@ getAggregates(Archive *fout, int *numAggs)
40044004
"CASE WHEN aggbasetype = 0 THEN 0 ELSE 1 END AS pronargs, "
40054005
"aggbasetype AS proargtypes, "
40064006
"(%s aggowner) AS rolname, "
4007-
"'{=X}' AS aggacl "
4007+
"NULL AS aggacl "
40084008
"FROM pg_aggregate "
40094009
"where oid > '%u'::oid",
40104010
username_subquery,
@@ -4019,7 +4019,7 @@ getAggregates(Archive *fout, int *numAggs)
40194019
"CASE WHEN aggbasetype = 0 THEN 0 ELSE 1 END AS pronargs, "
40204020
"aggbasetype AS proargtypes, "
40214021
"(%s aggowner) AS rolname, "
4022-
"'{=X}' AS aggacl "
4022+
"NULL AS aggacl "
40234023
"FROM pg_aggregate "
40244024
"where oid > '%u'::oid",
40254025
username_subquery,
@@ -4163,7 +4163,7 @@ getFuncs(Archive *fout, int *numFuncs)
41634163
appendPQExpBuffer(query,
41644164
"SELECT tableoid, oid, proname, prolang, "
41654165
"pronargs, proargtypes, prorettype, "
4166-
"'{=X}' AS proacl, "
4166+
"NULL AS proacl, "
41674167
"0::oid AS pronamespace, "
41684168
"(%s proowner) AS rolname "
41694169
"FROM pg_proc "
@@ -4179,7 +4179,7 @@ getFuncs(Archive *fout, int *numFuncs)
41794179
" WHERE relname = 'pg_proc') AS tableoid, "
41804180
"oid, proname, prolang, "
41814181
"pronargs, proargtypes, prorettype, "
4182-
"'{=X}' AS proacl, "
4182+
"NULL AS proacl, "
41834183
"0::oid AS pronamespace, "
41844184
"(%s proowner) AS rolname "
41854185
"FROM pg_proc "
@@ -6014,7 +6014,7 @@ getProcLangs(Archive *fout, int *numProcLangs)
60146014
/* pg_language has a laninline column */
60156015
appendPQExpBuffer(query, "SELECT tableoid, oid, "
60166016
"lanname, lanpltrusted, lanplcallfoid, "
6017-
"laninline, lanvalidator, lanacl, "
6017+
"laninline, lanvalidator, lanacl, "
60186018
"(%s lanowner) AS lanowner "
60196019
"FROM pg_language "
60206020
"WHERE lanispl "
@@ -6026,7 +6026,7 @@ getProcLangs(Archive *fout, int *numProcLangs)
60266026
/* pg_language has a lanowner column */
60276027
appendPQExpBuffer(query, "SELECT tableoid, oid, "
60286028
"lanname, lanpltrusted, lanplcallfoid, "
6029-
"lanvalidator, lanacl, "
6029+
"0 AS laninline, lanvalidator, lanacl, "
60306030
"(%s lanowner) AS lanowner "
60316031
"FROM pg_language "
60326032
"WHERE lanispl "
@@ -6036,7 +6036,9 @@ getProcLangs(Archive *fout, int *numProcLangs)
60366036
else if (fout->remoteVersion >= 80100)
60376037
{
60386038
/* Languages are owned by the bootstrap superuser, OID 10 */
6039-
appendPQExpBuffer(query, "SELECT tableoid, oid, *, "
6039+
appendPQExpBuffer(query, "SELECT tableoid, oid, "
6040+
"lanname, lanpltrusted, lanplcallfoid, "
6041+
"0 AS laninline, lanvalidator, lanacl, "
60406042
"(%s '10') AS lanowner "
60416043
"FROM pg_language "
60426044
"WHERE lanispl "
@@ -6046,27 +6048,47 @@ getProcLangs(Archive *fout, int *numProcLangs)
60466048
else if (fout->remoteVersion >= 70400)
60476049
{
60486050
/* Languages are owned by the bootstrap superuser, sysid 1 */
6049-
appendPQExpBuffer(query, "SELECT tableoid, oid, *, "
6051+
appendPQExpBuffer(query, "SELECT tableoid, oid, "
6052+
"lanname, lanpltrusted, lanplcallfoid, "
6053+
"0 AS laninline, lanvalidator, lanacl, "
60506054
"(%s '1') AS lanowner "
60516055
"FROM pg_language "
60526056
"WHERE lanispl "
60536057
"ORDER BY oid",
60546058
username_subquery);
60556059
}
6056-
else if (fout->remoteVersion >= 70100)
6060+
else if (fout->remoteVersion >= 70300)
60576061
{
60586062
/* No clear notion of an owner at all before 7.4 ... */
6059-
appendPQExpBufferStr(query, "SELECT tableoid, oid, * FROM pg_language "
6060-
"WHERE lanispl "
6061-
"ORDER BY oid");
6063+
appendPQExpBuffer(query, "SELECT tableoid, oid, "
6064+
"lanname, lanpltrusted, lanplcallfoid, "
6065+
"0 AS laninline, lanvalidator, lanacl, "
6066+
"NULL AS lanowner "
6067+
"FROM pg_language "
6068+
"WHERE lanispl "
6069+
"ORDER BY oid");
6070+
}
6071+
else if (fout->remoteVersion >= 70100)
6072+
{
6073+
appendPQExpBuffer(query, "SELECT tableoid, oid, "
6074+
"lanname, lanpltrusted, lanplcallfoid, "
6075+
"0 AS laninline, 0 AS lanvalidator, NULL AS lanacl, "
6076+
"NULL AS lanowner "
6077+
"FROM pg_language "
6078+
"WHERE lanispl "
6079+
"ORDER BY oid");
60626080
}
60636081
else
60646082
{
6065-
appendPQExpBufferStr(query, "SELECT "
6066-
"(SELECT oid FROM pg_class WHERE relname = 'pg_language') AS tableoid, "
6067-
"oid, * FROM pg_language "
6068-
"WHERE lanispl "
6069-
"ORDER BY oid");
6083+
appendPQExpBuffer(query, "SELECT "
6084+
"(SELECT oid FROM pg_class WHERE relname = 'pg_language') AS tableoid, "
6085+
"oid, "
6086+
"lanname, lanpltrusted, lanplcallfoid, "
6087+
"0 AS laninline, 0 AS lanvalidator, NULL AS lanacl, "
6088+
"NULL AS lanowner "
6089+
"FROM pg_language "
6090+
"WHERE lanispl "
6091+
"ORDER BY oid");
60706092
}
60716093

60726094
res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
@@ -6082,7 +6104,6 @@ getProcLangs(Archive *fout, int *numProcLangs)
60826104
i_lanname = PQfnumber(res, "lanname");
60836105
i_lanpltrusted = PQfnumber(res, "lanpltrusted");
60846106
i_lanplcallfoid = PQfnumber(res, "lanplcallfoid");
6085-
/* these may fail and return -1: */
60866107
i_laninline = PQfnumber(res, "laninline");
60876108
i_lanvalidator = PQfnumber(res, "lanvalidator");
60886109
i_lanacl = PQfnumber(res, "lanacl");
@@ -6098,22 +6119,10 @@ getProcLangs(Archive *fout, int *numProcLangs)
60986119
planginfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_lanname));
60996120
planginfo[i].lanpltrusted = *(PQgetvalue(res, i, i_lanpltrusted)) == 't';
61006121
planginfo[i].lanplcallfoid = atooid(PQgetvalue(res, i, i_lanplcallfoid));
6101-
if (i_laninline >= 0)
6102-
planginfo[i].laninline = atooid(PQgetvalue(res, i, i_laninline));
6103-
else
6104-
planginfo[i].laninline = InvalidOid;
6105-
if (i_lanvalidator >= 0)
6106-
planginfo[i].lanvalidator = atooid(PQgetvalue(res, i, i_lanvalidator));
6107-
else
6108-
planginfo[i].lanvalidator = InvalidOid;
6109-
if (i_lanacl >= 0)
6110-
planginfo[i].lanacl = pg_strdup(PQgetvalue(res, i, i_lanacl));
6111-
else
6112-
planginfo[i].lanacl = pg_strdup("{=U}");
6113-
if (i_lanowner >= 0)
6114-
planginfo[i].lanowner = pg_strdup(PQgetvalue(res, i, i_lanowner));
6115-
else
6116-
planginfo[i].lanowner = pg_strdup("");
6122+
planginfo[i].laninline = atooid(PQgetvalue(res, i, i_laninline));
6123+
planginfo[i].lanvalidator = atooid(PQgetvalue(res, i, i_lanvalidator));
6124+
planginfo[i].lanacl = pg_strdup(PQgetvalue(res, i, i_lanacl));
6125+
planginfo[i].lanowner = pg_strdup(PQgetvalue(res, i, i_lanowner));
61176126

61186127
if (fout->remoteVersion < 70300)
61196128
{

0 commit comments

Comments
 (0)