6
6
* Portions Copyright (c) 1994, Regents of the University of California
7
7
*
8
8
*
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 $
10
10
*
11
11
*-------------------------------------------------------------------------
12
12
*/
@@ -1076,11 +1076,59 @@ static void
1076
1076
dumpCreateDB (PGconn * conn )
1077
1077
{
1078
1078
PQExpBuffer buf = createPQExpBuffer ();
1079
+ char * default_encoding = NULL ;
1080
+ char * default_collate = NULL ;
1081
+ char * default_ctype = NULL ;
1079
1082
PGresult * res ;
1080
1083
int i ;
1081
1084
1082
1085
fprintf (OPF , "--\n-- Database creation\n--\n\n" );
1083
1086
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 */
1084
1132
if (server_version >= 80400 )
1085
1133
res = executeQuery (conn ,
1086
1134
"SELECT datname, "
@@ -1184,16 +1232,19 @@ dumpCreateDB(PGconn *conn)
1184
1232
if (strlen (dbowner ) != 0 )
1185
1233
appendPQExpBuffer (buf , " OWNER = %s" , fmtId (dbowner ));
1186
1234
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
+ }
1189
1240
1190
- if (strlen (dbcollate ) != 0 )
1241
+ if (default_collate && strcmp (dbcollate , default_collate ) != 0 )
1191
1242
{
1192
1243
appendPQExpBuffer (buf , " LC_COLLATE = " );
1193
1244
appendStringLiteralConn (buf , dbcollate , conn );
1194
1245
}
1195
1246
1196
- if (strlen (dbctype ) != 0 )
1247
+ if (default_ctype && strcmp (dbctype , default_ctype ) != 0 )
1197
1248
{
1198
1249
appendPQExpBuffer (buf , " LC_CTYPE = " );
1199
1250
appendStringLiteralConn (buf , dbctype , conn );
@@ -1219,18 +1270,18 @@ dumpCreateDB(PGconn *conn)
1219
1270
1220
1271
if (strcmp (dbistemplate , "t" ) == 0 )
1221
1272
{
1222
- appendPQExpBuffer (buf , "UPDATE pg_database SET datistemplate = 't' WHERE datname = " );
1273
+ appendPQExpBuffer (buf , "UPDATE pg_catalog. pg_database SET datistemplate = 't' WHERE datname = " );
1223
1274
appendStringLiteralConn (buf , dbname , conn );
1224
1275
appendPQExpBuffer (buf , ";\n" );
1225
1276
}
1226
1277
1227
1278
if (binary_upgrade )
1228
1279
{
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 , "UPDATE pg_catalog. pg_database "
1282
+ "SET datfrozenxid = '%u' "
1283
+ "WHERE datname = " ,
1284
+ dbfrozenxid );
1234
1285
appendStringLiteralConn (buf , dbname , conn );
1235
1286
appendPQExpBuffer (buf , ";\n" );
1236
1287
}
0 commit comments