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.124 2009/04/11 20:23:05 tgl Exp $
9+ * $PostgreSQL: pgsql/src/bin/pg_dump/pg_dumpall.c,v 1.125 2009/05/10 02:51:44 tgl Exp $
1010 *
1111 *-------------------------------------------------------------------------
1212 */
@@ -1076,11 +1076,59 @@ static void
10761076dumpCreateDB (PGconn * conn )
10771077{
10781078PQExpBuffer buf = createPQExpBuffer ();
1079+ char * default_encoding = NULL ;
1080+ char * default_collate = NULL ;
1081+ char * default_ctype = NULL ;
10791082PGresult * res ;
10801083int i ;
10811084
10821085fprintf (OPF ,"--\n-- Database creation\n--\n\n" );
10831086
1087+ /*
1088+ * First, get the installation's default encoding and locale information.
1089+ * We will dump encoding and locale specifications in the CREATE DATABASE
1090+ * commands for just those databases with values different from defaults.
1091+ *
1092+ * We consider template0's encoding and locale (or, pre-7.1, template1's)
1093+ * to define the installation default. Pre-8.4 installations do not
1094+ * have per-database locale settings; for them, every database must
1095+ * necessarily be using the installation default, so there's no need to
1096+ * do anything (which is good, since in very old versions there is no
1097+ * good way to find out what the installation locale is anyway...)
1098+ */
1099+ if (server_version >=80400 )
1100+ res = executeQuery (conn ,
1101+ "SELECT pg_encoding_to_char(encoding), "
1102+ "datcollate, datctype "
1103+ "FROM pg_database "
1104+ "WHERE datname = 'template0'" );
1105+ else if (server_version >=70100 )
1106+ res = executeQuery (conn ,
1107+ "SELECT pg_encoding_to_char(encoding), "
1108+ "null::text AS datcollate, null::text AS datctype "
1109+ "FROM pg_database "
1110+ "WHERE datname = 'template0'" );
1111+ else
1112+ res = executeQuery (conn ,
1113+ "SELECT pg_encoding_to_char(encoding), "
1114+ "null::text AS datcollate, null::text AS datctype "
1115+ "FROM pg_database "
1116+ "WHERE datname = 'template1'" );
1117+
1118+ /* If for some reason the template DB isn't there, treat as unknown */
1119+ if (PQntuples (res )> 0 )
1120+ {
1121+ if (!PQgetisnull (res ,0 ,0 ))
1122+ default_encoding = strdup (PQgetvalue (res ,0 ,0 ));
1123+ if (!PQgetisnull (res ,0 ,1 ))
1124+ default_collate = strdup (PQgetvalue (res ,0 ,1 ));
1125+ if (!PQgetisnull (res ,0 ,2 ))
1126+ default_ctype = strdup (PQgetvalue (res ,0 ,2 ));
1127+ }
1128+
1129+ PQclear (res );
1130+
1131+ /* Now collect all the information about databases to dump */
10841132if (server_version >=80400 )
10851133res = executeQuery (conn ,
10861134"SELECT datname, "
@@ -1184,16 +1232,19 @@ dumpCreateDB(PGconn *conn)
11841232if (strlen (dbowner )!= 0 )
11851233appendPQExpBuffer (buf ," OWNER = %s" ,fmtId (dbowner ));
11861234
1187- appendPQExpBuffer (buf ," ENCODING = " );
1188- appendStringLiteralConn (buf ,dbencoding ,conn );
1235+ if (default_encoding && strcmp (dbencoding ,default_encoding )!= 0 )
1236+ {
1237+ appendPQExpBuffer (buf ," ENCODING = " );
1238+ appendStringLiteralConn (buf ,dbencoding ,conn );
1239+ }
11891240
1190- if (strlen (dbcollate )!= 0 )
1241+ if (default_collate && strcmp (dbcollate , default_collate )!= 0 )
11911242{
11921243appendPQExpBuffer (buf ," LC_COLLATE = " );
11931244appendStringLiteralConn (buf ,dbcollate ,conn );
11941245}
11951246
1196- if (strlen (dbctype )!= 0 )
1247+ if (default_ctype && strcmp (dbctype , default_ctype )!= 0 )
11971248{
11981249appendPQExpBuffer (buf ," LC_CTYPE = " );
11991250appendStringLiteralConn (buf ,dbctype ,conn );
@@ -1219,18 +1270,18 @@ dumpCreateDB(PGconn *conn)
12191270
12201271if (strcmp (dbistemplate ,"t" )== 0 )
12211272{
1222- appendPQExpBuffer (buf ,"UPDATE pg_database SET datistemplate = 't' WHERE datname = " );
1273+ appendPQExpBuffer (buf ,"UPDATEpg_catalog. pg_database SET datistemplate = 't' WHERE datname = " );
12231274appendStringLiteralConn (buf ,dbname ,conn );
12241275appendPQExpBuffer (buf ,";\n" );
12251276}
12261277
12271278if (binary_upgrade )
12281279{
1229- appendPQExpBuffer (buf ,"\n -- For binary upgrade, set datfrozenxid.\n" );
1230- appendPQExpBuffer (buf ,"UPDATE pg_database\n "
1231- "SET datfrozenxid = '%u'\n "
1232- "WHERE datname = " ,
1233- dbfrozenxid );
1280+ appendPQExpBuffer (buf ,"-- For binary upgrade, set datfrozenxid.\n" );
1281+ appendPQExpBuffer (buf ,"UPDATEpg_catalog. pg_database "
1282+ "SET datfrozenxid = '%u' "
1283+ "WHERE datname = " ,
1284+ dbfrozenxid );
12341285appendStringLiteralConn (buf ,dbname ,conn );
12351286appendPQExpBuffer (buf ,";\n" );
12361287}