Skip to content

Commit ce1475a

Browse files
committed
Fix pg_dumpall to cope with dangling OIDs in pg_auth_members.
There is a race condition between "GRANT role" and "DROP ROLE", which allows GRANT to install pg_auth_members entries that refer to dropped roles. (Commit 6566133 prevented that for the grantor field, but not for the granted or grantee roles.) We'll soon fix that, at least in HEAD, but pg_dumpall needs to cope with the situation in case of pre-existing inconsistency. As pg_dumpall stands, it will emit invalid commands like 'GRANT foo TO ""', which causes pg_upgrade to fail. Fix it to emit warnings and skip those GRANTs, instead. There was some discussion of removing the problem by changing dumpRoleMembership's query to use JOIN not LEFT JOIN, but that would result in silently ignoring such entries. It seems better to produce a warning. Pre-v16 branches already coped with dangling grantor OIDs by simply omitting the GRANTED BY clause. I left that behavior as-is, although it's somewhat inconsistent with the behavior of later branches. Reported-by: Virender Singla <virender.cse@gmail.com> Discussion: https://postgr.es/m/CAM6Zo8woa62ZFHtMKox6a4jb8qQ=w87R2L0K8347iE-juQL2EA@mail.gmail.com Backpatch-through: 13
1 parent b91326f commit ce1475a

File tree

1 file changed

+28
-6
lines changed

1 file changed

+28
-6
lines changed

src/bin/pg_dump/pg_dumpall.c

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,10 +1008,12 @@ dumpRoleMembership(PGconn *conn)
10081008
PGresult *res;
10091009
int i;
10101010

1011-
printfPQExpBuffer(buf, "SELECT ur.rolname AS roleid, "
1011+
printfPQExpBuffer(buf, "SELECT ur.rolname AS role, "
10121012
"um.rolname AS member, "
10131013
"a.admin_option, "
1014-
"ug.rolname AS grantor "
1014+
"ug.rolname AS grantor, "
1015+
"a.roleid AS roleid, "
1016+
"a.member AS memberid "
10151017
"FROM pg_auth_members a "
10161018
"LEFT JOIN %s ur on ur.oid = a.roleid "
10171019
"LEFT JOIN %s um on um.oid = a.member "
@@ -1025,13 +1027,33 @@ dumpRoleMembership(PGconn *conn)
10251027

10261028
for (i = 0; i < PQntuples(res); i++)
10271029
{
1028-
char *roleid = PQgetvalue(res, i, 0);
1030+
char *role = PQgetvalue(res, i, 0);
10291031
char *member = PQgetvalue(res, i, 1);
1030-
char *option = PQgetvalue(res, i, 2);
1032+
char *admin_option = PQgetvalue(res, i, 2);
10311033

1032-
fprintf(OPF, "GRANT %s", fmtId(roleid));
1034+
/*
1035+
* Due to race conditions, the role and/or member could have been
1036+
* dropped. If we find such cases, print a warning and skip the
1037+
* entry.
1038+
*/
1039+
if (PQgetisnull(res, i, 0))
1040+
{
1041+
/* translator: %s represents a numeric role OID */
1042+
pg_log_warning("found orphaned pg_auth_members entry for role %s",
1043+
PQgetvalue(res, i, 4));
1044+
continue;
1045+
}
1046+
if (PQgetisnull(res, i, 1))
1047+
{
1048+
/* translator: %s represents a numeric role OID */
1049+
pg_log_warning("found orphaned pg_auth_members entry for role %s",
1050+
PQgetvalue(res, i, 5));
1051+
continue;
1052+
}
1053+
1054+
fprintf(OPF, "GRANT %s", fmtId(role));
10331055
fprintf(OPF, " TO %s", fmtId(member));
1034-
if (*option == 't')
1056+
if (*admin_option == 't')
10351057
fprintf(OPF, " WITH ADMIN OPTION");
10361058

10371059
/*

0 commit comments

Comments
 (0)