Commit 74bdf965 authored by Tom Lane's avatar Tom Lane

Fix pg_dumpall to do something sane when a pre-8.1 installation has

identically named user and group: we merge these into a single entity
with LOGIN permission.  Also, add ORDER BY commands to ensure consistent
dump ordering, for ease of comparing outputs from different installations.
parent 39ec43a2
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* *
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_dumpall.c,v 1.73 2006/03/30 01:08:15 adunstan Exp $ * $PostgreSQL: pgsql/src/bin/pg_dump/pg_dumpall.c,v 1.74 2006/04/07 21:26:29 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -425,14 +425,16 @@ dumpRoles(PGconn *conn) ...@@ -425,14 +425,16 @@ dumpRoles(PGconn *conn)
"rolcanlogin, rolconnlimit, rolpassword, " "rolcanlogin, rolconnlimit, rolpassword, "
"rolvaliduntil, " "rolvaliduntil, "
"pg_catalog.shobj_description(oid, 'pg_authid') as rolcomment " "pg_catalog.shobj_description(oid, 'pg_authid') as rolcomment "
"FROM pg_authid"); "FROM pg_authid "
"ORDER BY 1");
else if (server_version >= 80100) else if (server_version >= 80100)
printfPQExpBuffer(buf, printfPQExpBuffer(buf,
"SELECT rolname, rolsuper, rolinherit, " "SELECT rolname, rolsuper, rolinherit, "
"rolcreaterole, rolcreatedb, rolcatupdate, " "rolcreaterole, rolcreatedb, rolcatupdate, "
"rolcanlogin, rolconnlimit, rolpassword, " "rolcanlogin, rolconnlimit, rolpassword, "
"rolvaliduntil, null as rolcomment " "rolvaliduntil, null as rolcomment "
"FROM pg_authid"); "FROM pg_authid "
"ORDER BY 1");
else else
printfPQExpBuffer(buf, printfPQExpBuffer(buf,
"SELECT usename as rolname, " "SELECT usename as rolname, "
...@@ -459,7 +461,10 @@ dumpRoles(PGconn *conn) ...@@ -459,7 +461,10 @@ dumpRoles(PGconn *conn)
"null::text as rolpassword, " "null::text as rolpassword, "
"null::abstime as rolvaliduntil, " "null::abstime as rolvaliduntil, "
"null as rolcomment " "null as rolcomment "
"FROM pg_group"); "FROM pg_group "
"WHERE NOT EXISTS (SELECT 1 FROM pg_shadow "
" WHERE usename = groname) "
"ORDER BY 1");
res = executeQuery(conn, buf->data); res = executeQuery(conn, buf->data);
...@@ -578,7 +583,8 @@ dumpRoleMembership(PGconn *conn) ...@@ -578,7 +583,8 @@ dumpRoleMembership(PGconn *conn)
"FROM pg_auth_members a " "FROM pg_auth_members a "
"LEFT JOIN pg_authid ur on ur.oid = a.roleid " "LEFT JOIN pg_authid ur on ur.oid = a.roleid "
"LEFT JOIN pg_authid um on um.oid = a.member " "LEFT JOIN pg_authid um on um.oid = a.member "
"LEFT JOIN pg_authid ug on ug.oid = a.grantor"); "LEFT JOIN pg_authid ug on ug.oid = a.grantor "
"ORDER BY 1,2,3");
if (PQntuples(res) > 0) if (PQntuples(res) > 0)
printf("--\n-- Role memberships\n--\n\n"); printf("--\n-- Role memberships\n--\n\n");
...@@ -617,7 +623,8 @@ dumpGroups(PGconn *conn) ...@@ -617,7 +623,8 @@ dumpGroups(PGconn *conn)
PGresult *res; PGresult *res;
int i; int i;
res = executeQuery(conn, "SELECT groname, grolist FROM pg_group"); res = executeQuery(conn,
"SELECT groname, grolist FROM pg_group ORDER BY 1");
if (PQntuples(res) > 0) if (PQntuples(res) > 0)
printf("--\n-- Role memberships\n--\n\n"); printf("--\n-- Role memberships\n--\n\n");
...@@ -625,34 +632,43 @@ dumpGroups(PGconn *conn) ...@@ -625,34 +632,43 @@ dumpGroups(PGconn *conn)
for (i = 0; i < PQntuples(res); i++) for (i = 0; i < PQntuples(res); i++)
{ {
char *groname = PQgetvalue(res, i, 0); char *groname = PQgetvalue(res, i, 0);
char *val; char *grolist = PQgetvalue(res, i, 1);
char *tok;
val = strdup(PQgetvalue(res, i, 1));
tok = strtok(val, ",{}");
while (tok)
{
PGresult *res2; PGresult *res2;
int j; int j;
/*
* Array representation is {1,2,3} ... convert to (1,2,3)
*/
if (strlen(grolist) < 3)
continue;
grolist = strdup(grolist);
grolist[0] = '(';
grolist[strlen(grolist) - 1] = ')';
printfPQExpBuffer(buf, printfPQExpBuffer(buf,
"SELECT usename FROM pg_shadow WHERE usesysid = %s", "SELECT usename FROM pg_shadow "
tok); "WHERE usesysid IN %s ORDER BY 1",
grolist);
free(grolist);
res2 = executeQuery(conn, buf->data); res2 = executeQuery(conn, buf->data);
for (j = 0; j < PQntuples(res2); j++) for (j = 0; j < PQntuples(res2); j++)
{ {
char *usename = PQgetvalue(res2, j, 0);
/*
* Don't try to grant a role to itself; can happen if old
* installation has identically named user and group.
*/
if (strcmp(groname, usename) == 0)
continue;
printf("GRANT %s", fmtId(groname)); printf("GRANT %s", fmtId(groname));
printf(" TO %s;\n", fmtId(PQgetvalue(res2, j, 0))); printf(" TO %s;\n", fmtId(usename));
} }
PQclear(res2); PQclear(res2);
tok = strtok(NULL, ",{}");
}
free(val);
} }
PQclear(res); PQclear(res);
...@@ -680,14 +696,16 @@ dumpTablespaces(PGconn *conn) ...@@ -680,14 +696,16 @@ dumpTablespaces(PGconn *conn)
"spclocation, spcacl, " "spclocation, spcacl, "
"pg_catalog.shobj_description(oid, 'pg_tablespace') " "pg_catalog.shobj_description(oid, 'pg_tablespace') "
"FROM pg_catalog.pg_tablespace " "FROM pg_catalog.pg_tablespace "
"WHERE spcname NOT LIKE 'pg!_%' ESCAPE '!'"); "WHERE spcname !~ '^pg_' "
"ORDER BY 1");
else else
res = executeQuery(conn, "SELECT spcname, " res = executeQuery(conn, "SELECT spcname, "
"pg_catalog.pg_get_userbyid(spcowner) AS spcowner, " "pg_catalog.pg_get_userbyid(spcowner) AS spcowner, "
"spclocation, spcacl, " "spclocation, spcacl, "
"null " "null "
"FROM pg_catalog.pg_tablespace " "FROM pg_catalog.pg_tablespace "
"WHERE spcname NOT LIKE 'pg!_%' ESCAPE '!'"); "WHERE spcname !~ '^pg_' "
"ORDER BY 1");
if (PQntuples(res) > 0) if (PQntuples(res) > 0)
printf("--\n-- Tablespaces\n--\n\n"); printf("--\n-- Tablespaces\n--\n\n");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment