Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1XW5LK-00054K-T8 for pgsql-bugs@arkaria.postgresql.org; Mon, 22 Sep 2014 15:15:59 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1XW5LJ-0003F9-Mx for pgsql-bugs@arkaria.postgresql.org; Mon, 22 Sep 2014 15:15:57 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1XW5LI-0003El-2Y for pgsql-bugs@postgresql.org; Mon, 22 Sep 2014 15:15:56 +0000 Received: from smtp-outbound-1.vmware.com ([208.91.2.12]) by magus.postgresql.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.80) (envelope-from ) id 1XW5LA-0003RI-1a for pgsql-bugs@postgresql.org; Mon, 22 Sep 2014 15:15:53 +0000 Received: from sc9-mailhost3.vmware.com (sc9-mailhost3.vmware.com [10.113.161.73]) by smtp-outbound-1.vmware.com (Postfix) with ESMTP id DC5D62AB1F; Mon, 22 Sep 2014 08:15:42 -0700 (PDT) Received: from EX13-CAS-003.vmware.com (EX13-CAS-003.vmware.com [10.113.191.53]) by sc9-mailhost3.vmware.com (Postfix) with ESMTP id D5F3D40F5A; Mon, 22 Sep 2014 08:15:42 -0700 (PDT) Received: from EX13-MBX-024.vmware.com (10.113.191.44) by EX13-MBX-012.vmware.com (10.113.191.32) with Microsoft SMTP Server (TLS) id 15.0.775.38; Mon, 22 Sep 2014 08:15:42 -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; Mon, 22 Sep 2014 08:15:41 -0700 Message-ID: <54203D19.5020202@vmware.com> Date: Mon, 22 Sep 2014 18:15:37 +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_BUG_=2311431=3A_?= =?windows-1252?Q?Failing_to_backup_and_restore_a_Windows_p?= =?windows-1252?Q?ostgres_database=2C_with_Norwegian_Bokm=E5l_l?= =?windows-1252?Q?ocale=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> In-Reply-To: <20140921180413.GA1585212@tornado.leadboat.com> Content-Type: multipart/mixed; boundary="------------040809000807010303040207" 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.9 (-------) 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 --------------040809000807010303040207 Content-Type: text/plain; charset="windows-1252"; format=flowed Content-Transfer-Encoding: 8bit On 09/21/2014 09:04 PM, Noah Misch wrote: > On Sun, Sep 21, 2014 at 12:13:25PM -0400, Tom Lane wrote: >> Noah Misch writes: >>> On Fri, Sep 19, 2014 at 03:15:53PM -0700, Alon wrote: >>>> The pg_dump file contains this command: >>>> CREATE DATABASE workgroup WITH TEMPLATE = template0 ENCODING = 'UTF8' >>>> LC_COLLATE = 'Norwegian (Bokmål)_Norway.1252' LC_CTYPE = 'Norwegian >>>> (Bokmål)_Norway.1252'; >> >>> In WIN1252, "e5 6c 29" is "ål)". We're likely failing to set client_encoding >>> at some essential point in the process. >> >> The level of stupidity needed to use non-ASCII characters in a locale name >> is breathtaking. What were Microsoft thinking? > > Windows Vista did deprecate that locale name style in favor of "nb-NO". > setlocale(LC_x, "") still returns the old style, though. You need PostgreSQL > built with VS2012 or later to use "nb-NO" style; see IsoLocaleName(). Older versions support "norwegian-bokmal" as an alias, so we can use that. Unfortunately, the user can't use the alias as a work-around, as we always use the canonical string returned by setlocale(), regardless of what alias the user used. But we could use it as a work-around in our code. We have a similar mapping for a few country names that have dots in the name, "Hong Kong S.A.R." , "U.A.E.", and "Macau S.A.R.". This is slightly different, though. With the dots, the problem is that setlocale() doesn't accept the country name as argument. With Bokmål, the problem is that we don't store the locale name in the catalogs correctly, and hence don't pass correctly to setlocale(). (even if we stored it correctly when a single encoding is used throughout the cluster, things will go wrong as soon as you create a database that uses a non-default encoding). I think we should map "Norwegian (Bokmål)" to "norwegian-bokmal", so that the mapping is applied to the return value of setlocale(). So when initdb does setlocale(NULL, "") to get the locale, we'll return "norwegian-bokmal", and that gets stored in the catalogs and postgresql.conf. 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. - Heikki --------------040809000807010303040207 Content-Type: text/x-diff; name="map-bokmal-1.patch" Content-Transfer-Encoding: 8bit Content-Disposition: attachment; filename="map-bokmal-1.patch" commit b96ea7e2ba0ff510a5b681310c1f6d0363275f2b Author: Heikki Linnakangas Date: Mon Sep 22 14:59:23 2014 +0300 Map Windows "Norwegian (BokmÃ¥l)" locale name to "norwegian-bokmal". diff --git a/src/port/win32setlocale.c b/src/port/win32setlocale.c index b1172ec..29326c1 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 a separate start and end string. 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,106 @@ 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 */ - - break; + memcpy(&aliasbuf[0], &locale[0], matchpos); + memcpy(&aliasbuf[matchpos], replacement, replacementlen); + /* includes null terminator */ + memcpy(&aliasbuf[matchpos + replacementlen], rest, restlen + 1); + 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; } --------------040809000807010303040207 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 --------------040809000807010303040207--