Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1XX84T-00049J-00 for pgsql-bugs@arkaria.postgresql.org; Thu, 25 Sep 2014 12:22:53 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1XX84S-0006P7-G0 for pgsql-bugs@arkaria.postgresql.org; Thu, 25 Sep 2014 12:22:52 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1XX84R-0006P0-60 for pgsql-bugs@postgresql.org; Thu, 25 Sep 2014 12:22:51 +0000 Received: from smtp-outbound-1.vmware.com ([208.91.2.12]) by makus.postgresql.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.80) (envelope-from ) id 1XX84L-0003Et-TO for pgsql-bugs@postgresql.org; Thu, 25 Sep 2014 12:22:49 +0000 Received: from sc9-mailhost1.vmware.com (sc9-mailhost1.vmware.com [10.113.161.71]) by smtp-outbound-1.vmware.com (Postfix) with ESMTP id BEE7A289B8; Thu, 25 Sep 2014 05:22:43 -0700 (PDT) Received: from EX13-CAS-003.vmware.com (EX13-CAS-003.vmware.com [10.113.191.53]) by sc9-mailhost1.vmware.com (Postfix) with ESMTP id B5C5B191AA; Thu, 25 Sep 2014 05:22:43 -0700 (PDT) Received: from EX13-MBX-024.vmware.com (10.113.191.44) by EX13-MBX-007.vmware.com (10.113.191.27) with Microsoft SMTP Server (TLS) id 15.0.775.38; Thu, 25 Sep 2014 05:22:43 -0700 Received: from [192.168.1.90] (10.113.160.246) by EX13-MBX-024.vmware.com (10.113.191.44) with Microsoft SMTP Server (TLS) id 15.0.775.38; Thu, 25 Sep 2014 05:22:42 -0700 Message-ID: <5424090E.9060700@vmware.com> Date: Thu, 25 Sep 2014 15:22:38 +0300 From: Heikki Linnakangas Organization: VMware User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Icedove/31.0 MIME-Version: 1.0 To: Noah Misch , Tom Lane CC: Alon , Subject: =?windows-1252?Q?Re=3A_=5BBUGS=5D_Re=3A_=5BBUGS=5D_Re=3A?= =?windows-1252?Q?_BUG_=2311431=3A_Failing_to_backup_and_re?= =?windows-1252?Q?store_a_Windows_postgres_database=2C_with_N?= =?windows-1252?Q?orwegian_Bokm=E5l_locale=2E?= References: <20140915230427.2486.29437@wrigleys.postgresql.org> <1411164953375-5819745.post@n5.nabble.com> <20140921051846.GA1565935@tornado.leadboat.com> <7107.1411316005@sss.pgh.pa.us> <20140921180413.GA1585212@tornado.leadboat.com> <54203D19.5020202@vmware.com> In-Reply-To: <54203D19.5020202@vmware.com> Content-Type: multipart/mixed; boundary="------------030804030208050408090309" X-Originating-IP: [10.113.160.246] X-ClientProxiedBy: EX13-CAS-013.vmware.com (10.113.191.65) To EX13-MBX-024.vmware.com (10.113.191.44) X-Pg-Spam-Score: -7.7 (-------) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org --------------030804030208050408090309 Content-Type: text/plain; charset="windows-1252"; format=flowed Content-Transfer-Encoding: 8bit On 09/22/2014 06:15 PM, Heikki Linnakangas wrote: > Patch for that attached. pg_upgrade canonicalizes locale names by > passing them through setlocale(), before comparing them, so it should > still work. I'm a bit wary of back-patching, though. I think this would > work with existing clusters (as far as they work currently, with the > non-ASCII characters stored in pg_database), but would need some more > testing to be confident. This seems the best way to fix this in master, but there's a problem if we backpatch this. If existing databases in the cluster already have "Norwegian (Bokmål)" as the locale, and you update the binaries and try to create a new database: postgres=# create database foodb; ERROR: new collation (norwegian-bokmal_Norway.1252) is incompatible with the co llation of the template database (Norwegian (Bokmål)_Norway.1252) HINT: Use the same collation as in the template database, or use template0 as t emplate. That's straightforward to fix; instead of doing a straight strcmp() to check if the locales are the same, canonicalize them by calling check_locale first. Attached patch does that. After this, it's a bit strange that newly created databases use "norwegian-bokmal" as the locale, while old ones use "Norwegian (Bokmål)": foodb=# select datname, encoding, datcollate from pg_database; datname | encoding | datcollate -----------+----------+-------------------------------- template1 | 24 | Norwegian (Bokmål)_Norway.1252 template0 | 24 | Norwegian (Bokmål)_Norway.1252 postgres | 24 | Norwegian (Bokmål)_Norway.1252 foodb | 24 | norwegian-bokmal_Norway.1252 utf8db | 6 | norwegian-bokmal_Norway.1252 (5 rows) But we know those non-ASCII characters are problematic, so I think this is an improvement even in old clusters. At least you won't get any more of them. You could also UPDATE pg_database manually to fix that in an existing cluster. One more problem: pg_upgrade doesn't canonicalize locale names either, so you get: > lc_collate cluster values do not match: old "Norwegian (BokmÕl)_Norway.1252", n > ew "norwegian-bokmal_Norway.1252" > > Failure, exiting Bruce: do you think it would be OK to canonicalize the locale names before comparing? pg_upgrade already has a function to canonicalize, but it's only used when upgrading from a pre-9.2 server; locale names on newer versions are assumed to be already in canonical form. Alternatively, we could not bother with changing pg_upgrade or CREATE DATABASE, and instead instruct Bokmål users to do the manual UPDATE of pg_database in the release notes. That might be the most robust solution, if there are more cases where we compare locales that I've missed. - Heikki --------------030804030208050408090309 Content-Type: text/x-diff; name="map-bokmal-2.patch" Content-Transfer-Encoding: 8bit Content-Disposition: attachment; filename="map-bokmal-2.patch" commit 1925e6d66a13e5e35bb88f9d7aac2e44202b075c Author: Heikki Linnakangas Date: Mon Sep 22 14:59:23 2014 +0300 Work around Windows locale name with non-ASCII character, Norwegian (BokmÃ¥l) Windows has one a locale whose name contains a non-ASCII character: "Norwegian (BokmÃ¥l)" (that's an 'a' with a ring on top). That causes trouble: when passing it setlocale(), it's not clear what encoding the argument should be in. Another problem is that the locale name is stored in pg_database catalog table, and the encoding used there depends on what server encoding happens to be in use when the database is created. For example, if you issue the CREATE DATABASE when connected to a UTF-8 database, the locale name is stored in pg_database in UTF-8. As long as all locale names are pure ASCII, that's not a problem either. To work around that, map the troublesome locale name to a pure-ASCII alias of the same locale, "norwegian-bokmal". Since the canonical form of the locale changes as a result of this, make CREATE DATABASE to canonicalize the source locale name before comparing it with the new database's locale. This fixes bug #11431 reported by Alon Siman-Tov. diff --git a/src/backend/commands/dbcommands.c b/src/backend/commands/dbcommands.c index 7831e90..95421d0 100644 --- a/src/backend/commands/dbcommands.c +++ b/src/backend/commands/dbcommands.c @@ -372,6 +372,9 @@ createdb(const CreatedbStmt *stmt) */ if (strcmp(dbtemplate, "template0") != 0) { + char *src_canon_collate; + char *src_canon_ctype; + if (encoding != src_encoding) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), @@ -380,18 +383,44 @@ createdb(const CreatedbStmt *stmt) pg_encoding_to_char(src_encoding)), errhint("Use the same encoding as in the template database, or use template0 as template."))); - if (strcmp(dbcollate, src_collate) != 0) + /* + * Canonicalize the source locale names before comparison. Normally, + * the locale names in pg_database are already in canonical form, but + * let's cope if e.g. the cluster is moved from one system to another + * with compatible locale names, but different canonical forms. + */ + if (check_locale(LC_COLLATE, src_collate, &canonname)) + src_canon_collate = canonname; + else + { + ereport(WARNING, + (errcode(ERRCODE_WRONG_OBJECT_TYPE), + errmsg("could not canonicalize template database's locale name: \"%s\"", src_collate))); + src_canon_collate = src_collate; + } + + if (check_locale(LC_CTYPE, src_ctype, &canonname)) + src_canon_ctype = canonname; + else + { + ereport(WARNING, + (errcode(ERRCODE_WRONG_OBJECT_TYPE), + errmsg("could not canonicalize template database's locale name: \"%s\"", src_ctype))); + src_canon_ctype = src_ctype; + } + + if (strcmp(dbcollate, src_canon_collate) != 0) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("new collation (%s) is incompatible with the collation of the template database (%s)", - dbcollate, src_collate), + dbcollate, src_canon_collate), errhint("Use the same collation as in the template database, or use template0 as template."))); - if (strcmp(dbctype, src_ctype) != 0) + if (strcmp(dbctype, src_canon_ctype) != 0) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("new LC_CTYPE (%s) is incompatible with the LC_CTYPE of the template database (%s)", - dbctype, src_ctype), + dbctype, src_canon_ctype), errhint("Use the same LC_CTYPE as in the template database, or use template0 as template."))); } diff --git a/src/port/win32setlocale.c b/src/port/win32setlocale.c index b1172ec..379049b 100644 --- a/src/port/win32setlocale.c +++ b/src/port/win32setlocale.c @@ -9,15 +9,26 @@ * src/port/win32setlocale.c * * - * Windows has a problem with locale names that have a dot in the country - * name. For example: + * The setlocale() function in Windows is broken in two ways. First, it + * has a problem with locale names that have a dot in the country name. For + * example: * * "Chinese (Traditional)_Hong Kong S.A.R..950" * - * For some reason, setlocale() doesn't accept that. Fortunately, Windows' - * setlocale() accepts various alternative names for such countries, so we - * provide a wrapper setlocale() function that maps the troublemaking locale - * names to accepted aliases. + * For some reason, setlocale() doesn't accept that as argument, even though + * setlocale(LC_ALL, NULL) returns exactly that. Fortunately, it accepts + * various alternative names for such countries, so to work around the broken + * setlocale() function, we map the troublemaking locale names to accepted + * aliases, before calling setlocale(). + * + * The second problem is that the locale name for "Norwegian (Bokmål)" + * contains a non-ASCII character. That's problematic, because it's not clear + * what encoding the locale name itself is supposed to be in, when you + * haven't yet set a locale. Also, it causes problems when the cluster + * contains databases with different encodings, as the locale name is stored + * in the pg_database system catalog. To work around that, when setlocale() + * returns that locale name, map it to a pure-ASCII alias for the same + * locale. *------------------------------------------------------------------------- */ @@ -27,11 +38,23 @@ struct locale_map { - const char *locale_name_part; /* string in locale name to replace */ - const char *replacement; /* string to replace it with */ + /* + * String in locale name to replace. Can be a single string (end is NULL), + * or separate start and end strings. If two strings are given, the + * locale name must contain both of them, and everything between them + * is replaced. This is used for a poor-man's regexp search, allowing + * replacement of "start.*end". + */ + const char *locale_name_start; + const char *locale_name_end; + + const char *replacement; /* string to replace the match with */ }; -static const struct locale_map locale_map_list[] = { +/* + * Mappings applied before calling setlocale(), to the argument. + */ +static const struct locale_map locale_map_argument[] = { /* * "HKG" is listed here: * http://msdn.microsoft.com/en-us/library/cdax410z%28v=vs.71%29.aspx @@ -40,8 +63,8 @@ static const struct locale_map locale_map_list[] = { * "ARE" is the ISO-3166 three-letter code for U.A.E. It is not on the * above list, but seems to work anyway. */ - {"Hong Kong S.A.R.", "HKG"}, - {"U.A.E.", "ARE"}, + {"Hong Kong S.A.R.", NULL, "HKG"}, + {"U.A.E.", NULL, "ARE"}, /* * The ISO-3166 country code for Macau S.A.R. is MAC, but Windows doesn't @@ -56,60 +79,107 @@ static const struct locale_map locale_map_list[] = { * * Some versions of Windows spell it "Macau", others "Macao". */ - {"Chinese (Traditional)_Macau S.A.R..950", "ZHM"}, - {"Chinese_Macau S.A.R..950", "ZHM"}, - {"Chinese (Traditional)_Macao S.A.R..950", "ZHM"}, - {"Chinese_Macao S.A.R..950", "ZHM"} + {"Chinese (Traditional)_Macau S.A.R..950", NULL, "ZHM"}, + {"Chinese_Macau S.A.R..950", NULL, "ZHM"}, + {"Chinese (Traditional)_Macao S.A.R..950", NULL, "ZHM"}, + {"Chinese_Macao S.A.R..950", NULL, "ZHM"}, + {NULL, NULL, NULL} }; -char * -pgwin32_setlocale(int category, const char *locale) +/* + * Mappings applied after calling setlocale(), to its return value. + */ +static const struct locale_map locale_map_result[] = { + /* + * "Norwegian (Bokmål)" locale name contains the a-ring character. + * Map it to a pure-ASCII alias. + * + * It's not clear what encoding setlocale() uses when it returns the + * locale name, so to play it safe, we search for "Norwegian (Bok*l)". + */ + {"Norwegian (Bokm", "l)", "norwegian-bokmal"}, + {NULL, NULL, NULL} +}; + +#define MAX_LOCALE_NAME_LEN 100 + +static char * +map_locale(struct locale_map *map, char *locale) { - char *result; - char *alias; + static char aliasbuf[MAX_LOCALE_NAME_LEN]; int i; - if (locale == NULL) - return setlocale(category, locale); - /* Check if the locale name matches any of the problematic ones. */ - alias = NULL; - for (i = 0; i < lengthof(locale_map_list); i++) + for (i = 0; map[i].locale_name_start != NULL; i++) { - const char *needle = locale_map_list[i].locale_name_part; - const char *replacement = locale_map_list[i].replacement; + const char *needle_start = map[i].locale_name_start; + const char *needle_end = map[i].locale_name_end; + const char *replacement = map[i].replacement; char *match; + char *match_start = NULL; + char *match_end = NULL; - match = strstr(locale, needle); - if (match != NULL) + match = strstr(locale, needle_start); + if (match) + { + /* + * Found a match for the first part. If this was a two-part + * replacement, find the second part. + */ + match_start = match; + if (needle_end) + { + match = strstr(match_start + strlen(needle_start), needle_end); + if (match) + match_end = match + strlen(needle_end); + else + match_start = NULL; + } + else + match_end = match_start + strlen(needle_start); + } + + if (match_start) { /* Found a match. Replace the matched string. */ - int matchpos = match - locale; + int matchpos = match_start - locale; int replacementlen = strlen(replacement); - char *rest = match + strlen(needle); + char *rest = match_end; int restlen = strlen(rest); - alias = malloc(matchpos + replacementlen + restlen + 1); - if (!alias) + /* check that the result fits in the static buffer */ + if (matchpos + replacementlen + restlen + 1 > MAX_LOCALE_NAME_LEN) return NULL; - memcpy(&alias[0], &locale[0], matchpos); - memcpy(&alias[matchpos], replacement, replacementlen); - memcpy(&alias[matchpos + replacementlen], rest, restlen + 1); /* includes null - * terminator */ + memcpy(&aliasbuf[0], &locale[0], matchpos); + memcpy(&aliasbuf[matchpos], replacement, replacementlen); + /* includes null terminator */ + memcpy(&aliasbuf[matchpos + replacementlen], rest, restlen + 1); - break; + return aliasbuf; } } - /* Call the real setlocale() function */ - if (alias) - { - result = setlocale(category, alias); - free(alias); - } + /* no match, just return the original string */ + return locale; +} + +char * +pgwin32_setlocale(int category, const char *locale) +{ + char *argument; + char *result; + + if (locale == NULL) + argument = NULL; else - result = setlocale(category, locale); + argument = map_locale(locale_map_argument, locale); + + /* Call the real setlocale() function */ + result = setlocale(category, argument); + + if (result) + result = map_locale(locale_map_result, result); return result; } --------------030804030208050408090309 Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs --------------030804030208050408090309--