Skip to content

Commit 1cd4685

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 f8e4e0e commit 1cd4685

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
@@ -3513,7 +3513,7 @@ getTypes(Archive *fout, int *numTypes)
35133513
else if (fout->remoteVersion >= 80300)
35143514
{
35153515
appendPQExpBuffer(query, "SELECT tableoid, oid, typname, "
3516-
"typnamespace, '{=U}' AS typacl, "
3516+
"typnamespace, NULL AS typacl, "
35173517
"(%s typowner) AS rolname, "
35183518
"typinput::oid AS typinput, "
35193519
"typoutput::oid AS typoutput, typelem, typrelid, "
@@ -3528,7 +3528,7 @@ getTypes(Archive *fout, int *numTypes)
35283528
else if (fout->remoteVersion >= 70300)
35293529
{
35303530
appendPQExpBuffer(query, "SELECT tableoid, oid, typname, "
3531-
"typnamespace, '{=U}' AS typacl, "
3531+
"typnamespace, NULL AS typacl, "
35323532
"(%s typowner) AS rolname, "
35333533
"typinput::oid AS typinput, "
35343534
"typoutput::oid AS typoutput, typelem, typrelid, "
@@ -3542,7 +3542,7 @@ getTypes(Archive *fout, int *numTypes)
35423542
else if (fout->remoteVersion >= 70100)
35433543
{
35443544
appendPQExpBuffer(query, "SELECT tableoid, oid, typname, "
3545-
"0::oid AS typnamespace, '{=U}' AS typacl, "
3545+
"0::oid AS typnamespace, NULL AS typacl, "
35463546
"(%s typowner) AS rolname, "
35473547
"typinput::oid AS typinput, "
35483548
"typoutput::oid AS typoutput, typelem, typrelid, "
@@ -3558,7 +3558,7 @@ getTypes(Archive *fout, int *numTypes)
35583558
appendPQExpBuffer(query, "SELECT "
35593559
"(SELECT oid FROM pg_class WHERE relname = 'pg_type') AS tableoid, "
35603560
"oid, typname, "
3561-
"0::oid AS typnamespace, '{=U}' AS typacl, "
3561+
"0::oid AS typnamespace, NULL AS typacl, "
35623562
"(%s typowner) AS rolname, "
35633563
"typinput::oid AS typinput, "
35643564
"typoutput::oid AS typoutput, typelem, typrelid, "
@@ -4249,7 +4249,7 @@ getAggregates(Archive *fout, DumpOptions *dopt, int *numAggs)
42494249
"CASE WHEN aggbasetype = 0 THEN 0 ELSE 1 END AS pronargs, "
42504250
"aggbasetype AS proargtypes, "
42514251
"(%s aggowner) AS rolname, "
4252-
"'{=X}' AS aggacl "
4252+
"NULL AS aggacl "
42534253
"FROM pg_aggregate "
42544254
"where oid > '%u'::oid",
42554255
username_subquery,
@@ -4264,7 +4264,7 @@ getAggregates(Archive *fout, DumpOptions *dopt, int *numAggs)
42644264
"CASE WHEN aggbasetype = 0 THEN 0 ELSE 1 END AS pronargs, "
42654265
"aggbasetype AS proargtypes, "
42664266
"(%s aggowner) AS rolname, "
4267-
"'{=X}' AS aggacl "
4267+
"NULL AS aggacl "
42684268
"FROM pg_aggregate "
42694269
"where oid > '%u'::oid",
42704270
username_subquery,
@@ -4408,7 +4408,7 @@ getFuncs(Archive *fout, DumpOptions *dopt, int *numFuncs)
44084408
appendPQExpBuffer(query,
44094409
"SELECT tableoid, oid, proname, prolang, "
44104410
"pronargs, proargtypes, prorettype, "
4411-
"'{=X}' AS proacl, "
4411+
"NULL AS proacl, "
44124412
"0::oid AS pronamespace, "
44134413
"(%s proowner) AS rolname "
44144414
"FROM pg_proc "
@@ -4424,7 +4424,7 @@ getFuncs(Archive *fout, DumpOptions *dopt, int *numFuncs)
44244424
" WHERE relname = 'pg_proc') AS tableoid, "
44254425
"oid, proname, prolang, "
44264426
"pronargs, proargtypes, prorettype, "
4427-
"'{=X}' AS proacl, "
4427+
"NULL AS proacl, "
44284428
"0::oid AS pronamespace, "
44294429
"(%s proowner) AS rolname "
44304430
"FROM pg_proc "
@@ -6317,7 +6317,7 @@ getProcLangs(Archive *fout, int *numProcLangs)
63176317
/* pg_language has a laninline column */
63186318
appendPQExpBuffer(query, "SELECT tableoid, oid, "
63196319
"lanname, lanpltrusted, lanplcallfoid, "
6320-
"laninline, lanvalidator, lanacl, "
6320+
"laninline, lanvalidator, lanacl, "
63216321
"(%s lanowner) AS lanowner "
63226322
"FROM pg_language "
63236323
"WHERE lanispl "
@@ -6329,7 +6329,7 @@ getProcLangs(Archive *fout, int *numProcLangs)
63296329
/* pg_language has a lanowner column */
63306330
appendPQExpBuffer(query, "SELECT tableoid, oid, "
63316331
"lanname, lanpltrusted, lanplcallfoid, "
6332-
"lanvalidator, lanacl, "
6332+
"0 AS laninline, lanvalidator, lanacl, "
63336333
"(%s lanowner) AS lanowner "
63346334
"FROM pg_language "
63356335
"WHERE lanispl "
@@ -6339,7 +6339,9 @@ getProcLangs(Archive *fout, int *numProcLangs)
63396339
else if (fout->remoteVersion >= 80100)
63406340
{
63416341
/* Languages are owned by the bootstrap superuser, OID 10 */
6342-
appendPQExpBuffer(query, "SELECT tableoid, oid, *, "
6342+
appendPQExpBuffer(query, "SELECT tableoid, oid, "
6343+
"lanname, lanpltrusted, lanplcallfoid, "
6344+
"0 AS laninline, lanvalidator, lanacl, "
63436345
"(%s '10') AS lanowner "
63446346
"FROM pg_language "
63456347
"WHERE lanispl "
@@ -6349,27 +6351,47 @@ getProcLangs(Archive *fout, int *numProcLangs)
63496351
else if (fout->remoteVersion >= 70400)
63506352
{
63516353
/* Languages are owned by the bootstrap superuser, sysid 1 */
6352-
appendPQExpBuffer(query, "SELECT tableoid, oid, *, "
6354+
appendPQExpBuffer(query, "SELECT tableoid, oid, "
6355+
"lanname, lanpltrusted, lanplcallfoid, "
6356+
"0 AS laninline, lanvalidator, lanacl, "
63536357
"(%s '1') AS lanowner "
63546358
"FROM pg_language "
63556359
"WHERE lanispl "
63566360
"ORDER BY oid",
63576361
username_subquery);
63586362
}
6359-
else if (fout->remoteVersion >= 70100)
6363+
else if (fout->remoteVersion >= 70300)
63606364
{
63616365
/* No clear notion of an owner at all before 7.4 ... */
6362-
appendPQExpBufferStr(query, "SELECT tableoid, oid, * FROM pg_language "
6363-
"WHERE lanispl "
6364-
"ORDER BY oid");
6366+
appendPQExpBuffer(query, "SELECT tableoid, oid, "
6367+
"lanname, lanpltrusted, lanplcallfoid, "
6368+
"0 AS laninline, lanvalidator, lanacl, "
6369+
"NULL AS lanowner "
6370+
"FROM pg_language "
6371+
"WHERE lanispl "
6372+
"ORDER BY oid");
6373+
}
6374+
else if (fout->remoteVersion >= 70100)
6375+
{
6376+
appendPQExpBuffer(query, "SELECT tableoid, oid, "
6377+
"lanname, lanpltrusted, lanplcallfoid, "
6378+
"0 AS laninline, 0 AS lanvalidator, NULL AS lanacl, "
6379+
"NULL AS lanowner "
6380+
"FROM pg_language "
6381+
"WHERE lanispl "
6382+
"ORDER BY oid");
63656383
}
63666384
else
63676385
{
6368-
appendPQExpBufferStr(query, "SELECT "
6369-
"(SELECT oid FROM pg_class WHERE relname = 'pg_language') AS tableoid, "
6370-
"oid, * FROM pg_language "
6371-
"WHERE lanispl "
6372-
"ORDER BY oid");
6386+
appendPQExpBuffer(query, "SELECT "
6387+
"(SELECT oid FROM pg_class WHERE relname = 'pg_language') AS tableoid, "
6388+
"oid, "
6389+
"lanname, lanpltrusted, lanplcallfoid, "
6390+
"0 AS laninline, 0 AS lanvalidator, NULL AS lanacl, "
6391+
"NULL AS lanowner "
6392+
"FROM pg_language "
6393+
"WHERE lanispl "
6394+
"ORDER BY oid");
63736395
}
63746396

63756397
res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
@@ -6385,7 +6407,6 @@ getProcLangs(Archive *fout, int *numProcLangs)
63856407
i_lanname = PQfnumber(res, "lanname");
63866408
i_lanpltrusted = PQfnumber(res, "lanpltrusted");
63876409
i_lanplcallfoid = PQfnumber(res, "lanplcallfoid");
6388-
/* these may fail and return -1: */
63896410
i_laninline = PQfnumber(res, "laninline");
63906411
i_lanvalidator = PQfnumber(res, "lanvalidator");
63916412
i_lanacl = PQfnumber(res, "lanacl");
@@ -6401,22 +6422,10 @@ getProcLangs(Archive *fout, int *numProcLangs)
64016422
planginfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_lanname));
64026423
planginfo[i].lanpltrusted = *(PQgetvalue(res, i, i_lanpltrusted)) == 't';
64036424
planginfo[i].lanplcallfoid = atooid(PQgetvalue(res, i, i_lanplcallfoid));
6404-
if (i_laninline >= 0)
6405-
planginfo[i].laninline = atooid(PQgetvalue(res, i, i_laninline));
6406-
else
6407-
planginfo[i].laninline = InvalidOid;
6408-
if (i_lanvalidator >= 0)
6409-
planginfo[i].lanvalidator = atooid(PQgetvalue(res, i, i_lanvalidator));
6410-
else
6411-
planginfo[i].lanvalidator = InvalidOid;
6412-
if (i_lanacl >= 0)
6413-
planginfo[i].lanacl = pg_strdup(PQgetvalue(res, i, i_lanacl));
6414-
else
6415-
planginfo[i].lanacl = pg_strdup("{=U}");
6416-
if (i_lanowner >= 0)
6417-
planginfo[i].lanowner = pg_strdup(PQgetvalue(res, i, i_lanowner));
6418-
else
6419-
planginfo[i].lanowner = pg_strdup("");
6425+
planginfo[i].laninline = atooid(PQgetvalue(res, i, i_laninline));
6426+
planginfo[i].lanvalidator = atooid(PQgetvalue(res, i, i_lanvalidator));
6427+
planginfo[i].lanacl = pg_strdup(PQgetvalue(res, i, i_lanacl));
6428+
planginfo[i].lanowner = pg_strdup(PQgetvalue(res, i, i_lanowner));
64206429

64216430
if (fout->remoteVersion < 70300)
64226431
{

0 commit comments

Comments
 (0)