Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 4db44b4

Browse files
committed
Adjust pg_dumpall so that it emits ENCODING, LC_COLLATE, and LC_CTYPE options
in its CREATE DATABASE commands only for databases that have settings different from the installation defaults. This is a low-tech method of avoiding unnecessary platform dependencies in dump files. Eventually we ought to have a platform-independent way of specifying LC_COLLATE and LC_CTYPE, but that's not going to happen for 8.4, and this patch at least avoids the issue for people who aren't setting up per-database locales. ENCODING doesn't have the platform dependency problem, but it seems consistent to make it act the same as the locale settings.
1 parent 8dcf184 commit 4db44b4

File tree

1 file changed

+62
-11
lines changed

1 file changed

+62
-11
lines changed

src/bin/pg_dump/pg_dumpall.c

Lines changed: 62 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
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
10761076
dumpCreateDB(PGconn *conn)
10771077
{
10781078
PQExpBuffer buf = createPQExpBuffer();
1079+
char *default_encoding = NULL;
1080+
char *default_collate = NULL;
1081+
char *default_ctype = NULL;
10791082
PGresult *res;
10801083
int i;
10811084

10821085
fprintf(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 */
10841132
if (server_version >= 80400)
10851133
res = executeQuery(conn,
10861134
"SELECT datname, "
@@ -1184,16 +1232,19 @@ dumpCreateDB(PGconn *conn)
11841232
if (strlen(dbowner) != 0)
11851233
appendPQExpBuffer(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
{
11921243
appendPQExpBuffer(buf, " LC_COLLATE = ");
11931244
appendStringLiteralConn(buf, dbcollate, conn);
11941245
}
11951246

1196-
if (strlen(dbctype) != 0)
1247+
if (default_ctype && strcmp(dbctype, default_ctype) != 0)
11971248
{
11981249
appendPQExpBuffer(buf, " LC_CTYPE = ");
11991250
appendStringLiteralConn(buf, dbctype, conn);
@@ -1219,18 +1270,18 @@ dumpCreateDB(PGconn *conn)
12191270

12201271
if (strcmp(dbistemplate, "t") == 0)
12211272
{
1222-
appendPQExpBuffer(buf, "UPDATE pg_database SET datistemplate = 't' WHERE datname = ");
1273+
appendPQExpBuffer(buf, "UPDATE pg_catalog.pg_database SET datistemplate = 't' WHERE datname = ");
12231274
appendStringLiteralConn(buf, dbname, conn);
12241275
appendPQExpBuffer(buf, ";\n");
12251276
}
12261277

12271278
if (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, "UPDATE pg_catalog.pg_database "
1282+
"SET datfrozenxid = '%u' "
1283+
"WHERE datname = ",
1284+
dbfrozenxid);
12341285
appendStringLiteralConn(buf, dbname, conn);
12351286
appendPQExpBuffer(buf, ";\n");
12361287
}

0 commit comments

Comments
 (0)