66 * Portions Copyright (c) 1994, Regents of the University of California
77 *
88 *
9- * $PostgreSQL: pgsql/src/bin/pg_dump/pg_dumpall.c,v 1.67 2005/08/28 16:31:37 tgl Exp $
9+ * $PostgreSQL: pgsql/src/bin/pg_dump/pg_dumpall.c,v 1.68 2005/10/10 22:29:48 tgl Exp $
1010 *
1111 *-------------------------------------------------------------------------
1212 */
@@ -42,7 +42,7 @@ static const char *progname;
4242
4343static void help (void );
4444
45- static void dumpRoles (PGconn * conn , bool initdbonly );
45+ static void dumpRoles (PGconn * conn );
4646static void dumpRoleMembership (PGconn * conn );
4747static void dumpGroups (PGconn * conn );
4848static void dumpTablespaces (PGconn * conn );
@@ -316,23 +316,22 @@ main(int argc, char *argv[])
316316
317317if (!data_only )
318318{
319- /* Dumpall users excluding the initdb user */
320- dumpRoles (conn , false );
319+ /* Dumproles ( users) */
320+ dumpRoles (conn );
321321
322322/* Dump role memberships --- need different method for pre-8.1 */
323323if (server_version >=80100 )
324324dumpRoleMembership (conn );
325325else
326326dumpGroups (conn );
327327
328+ /* Dump tablespaces */
328329if (server_version >=80000 )
329330dumpTablespaces (conn );
330331
332+ /* Dump CREATE DATABASE commands */
331333if (!globals_only )
332334dumpCreateDB (conn );
333-
334- /* Dump alter command for initdb user */
335- dumpRoles (conn , true);
336335}
337336
338337if (!globals_only )
@@ -394,11 +393,9 @@ help(void)
394393
395394/*
396395 * Dump roles
397- *
398- * Is able to dump all non initdb users or just the initdb user.
399396 */
400397static void
401- dumpRoles (PGconn * conn , bool initdbonly )
398+ dumpRoles (PGconn * conn )
402399{
403400PQExpBuffer buf = createPQExpBuffer ();
404401PGresult * res ;
@@ -411,8 +408,7 @@ dumpRoles(PGconn *conn, bool initdbonly)
411408i_rolcanlogin ,
412409i_rolconnlimit ,
413410i_rolpassword ,
414- i_rolvaliduntil ,
415- i_clusterowner ;
411+ i_rolvaliduntil ;
416412int i ;
417413
418414/* note: rolconfig is dumped later */
@@ -421,8 +417,7 @@ dumpRoles(PGconn *conn, bool initdbonly)
421417"SELECT rolname, rolsuper, rolinherit, "
422418"rolcreaterole, rolcreatedb, rolcatupdate, "
423419"rolcanlogin, rolconnlimit, rolpassword, "
424- "rolvaliduntil, "
425- "(oid = (SELECT datdba FROM pg_database WHERE datname = 'template0')) AS clusterowner "
420+ "rolvaliduntil "
426421"FROM pg_authid" );
427422else
428423printfPQExpBuffer (buf ,
@@ -435,8 +430,7 @@ dumpRoles(PGconn *conn, bool initdbonly)
435430"true as rolcanlogin, "
436431"-1 as rolconnlimit, "
437432"passwd as rolpassword, "
438- "valuntil as rolvaliduntil, "
439- "(usesysid = (SELECT datdba FROM pg_database WHERE datname = '%s')) AS clusterowner "
433+ "valuntil as rolvaliduntil "
440434"FROM pg_shadow "
441435"UNION ALL "
442436"SELECT groname as rolname, "
@@ -448,10 +442,8 @@ dumpRoles(PGconn *conn, bool initdbonly)
448442"false as rolcanlogin, "
449443"-1 as rolconnlimit, "
450444"null::text as rolpassword, "
451- "null::abstime as rolvaliduntil, "
452- "false AS clusterowner "
453- "FROM pg_group" ,
454- (server_version >=70100 ) ?"template0" :"template1" );
445+ "null::abstime as rolvaliduntil "
446+ "FROM pg_group" );
455447
456448res = executeQuery (conn ,buf -> data );
457449
@@ -465,33 +457,29 @@ dumpRoles(PGconn *conn, bool initdbonly)
465457i_rolconnlimit = PQfnumber (res ,"rolconnlimit" );
466458i_rolpassword = PQfnumber (res ,"rolpassword" );
467459i_rolvaliduntil = PQfnumber (res ,"rolvaliduntil" );
468- i_clusterowner = PQfnumber (res ,"clusterowner" );
469460
470- if (PQntuples (res )> 0 || (! initdbonly && output_clean ) )
461+ if (PQntuples (res )> 0 )
471462printf ("--\n-- Roles\n--\n\n" );
472- if (!initdbonly && output_clean )
473- printf ("DELETE FROM pg_shadow WHERE usesysid <> (SELECT datdba FROM pg_database WHERE datname = 'template0');\n\n" );
474463
475464for (i = 0 ;i < PQntuples (res );i ++ )
476465{
477466const char * rolename ;
478- bool clusterowner ;
479467
480468rolename = PQgetvalue (res ,i ,i_rolname );
481- clusterowner = (strcmp (PQgetvalue (res ,i ,i_clusterowner ),"t" )== 0 );
482469
483- /* Check which pass we're on */
484- if ((initdbonly && !clusterowner )|| (!initdbonly && clusterowner ))
485- continue ;
470+ resetPQExpBuffer (buf );
471+
472+ if (output_clean )
473+ appendPQExpBuffer (buf ,"DROP ROLE %s;\n" ,fmtId (rolename ));
486474
487475/*
488- * Dump ALTER ROLE for the cluster owner and CREATE ROLE for all
489- * other roles
476+ * We dump CREATE ROLE followed by ALTER ROLE to ensure that the
477+ * role will acquire the right properties even if it already exists.
478+ * (The above DROP may therefore seem redundant, but it isn't really,
479+ * because this technique doesn't get rid of role memberships.)
490480 */
491- if (!clusterowner )
492- printfPQExpBuffer (buf ,"CREATE ROLE %s WITH" ,fmtId (rolename ));
493- else
494- printfPQExpBuffer (buf ,"ALTER ROLE %s WITH" ,fmtId (rolename ));
481+ appendPQExpBuffer (buf ,"CREATE ROLE %s;\n" ,fmtId (rolename ));
482+ appendPQExpBuffer (buf ,"ALTER ROLE %s WITH" ,fmtId (rolename ));
495483
496484if (strcmp (PQgetvalue (res ,i ,i_rolsuper ),"t" )== 0 )
497485appendPQExpBuffer (buf ," SUPERUSER" );
@@ -569,10 +557,8 @@ dumpRoleMembership(PGconn *conn)
569557"LEFT JOIN pg_authid um on um.oid = a.member "
570558"LEFT JOIN pg_authid ug on ug.oid = a.grantor" );
571559
572- if (PQntuples (res )> 0 || output_clean )
560+ if (PQntuples (res )> 0 )
573561printf ("--\n-- Role memberships\n--\n\n" );
574- if (output_clean )
575- printf ("DELETE FROM pg_auth_members;\n\n" );
576562
577563for (i = 0 ;i < PQntuples (res );i ++ )
578564{
@@ -610,10 +596,8 @@ dumpGroups(PGconn *conn)
610596
611597res = executeQuery (conn ,"SELECT groname, grolist FROM pg_group" );
612598
613- if (PQntuples (res )> 0 || output_clean )
599+ if (PQntuples (res )> 0 )
614600printf ("--\n-- Role memberships\n--\n\n" );
615- if (output_clean )
616- printf ("DELETE FROM pg_auth_members;\n\n" );
617601
618602for (i = 0 ;i < PQntuples (res );i ++ )
619603{