pg.ddx.io pgsql-bugs@postgresql.org mailing list archivehelp / color / mirror / Atom feed
Re: [BUGS] Re: [BUGS] Re: BUG #11431: Failing to backup and restore a Windows postgres database, with Norwegian Bokmål locale. 2+ messages / 1 participants [nested] [flat]
* Re: [BUGS] Re: [BUGS] Re: BUG #11431: Failing to backup and restore a Windows postgres database, with Norwegian Bokmål locale. @ 2014-09-25 12:22 Heikki Linnakangas <hlinnakangas@vmware.com> 2014-10-10 12:45 ` Re: [BUGS] Re: [BUGS] Re: [BUGS] Re: BUG #11431: Failing to backup and restore a Windows postgres database, with Norwegian Bokmål locale. Heikki Linnakangas <hlinnakangas@vmware.com> 0 siblings, 1 reply; 2+ messages in thread From: Heikki Linnakangas @ 2014-09-25 12:22 UTC (permalink / raw) To: Noah Misch <noah@leadboat.com>; Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Alon <asimantov@tableausoftware.com>; pgsql-bugs 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 -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs Attachments:^ permalink raw reply [nested|flat] 2+ messages in threadmap-bokmal-2.patch (text/x-diff, 10.7K) download
commit 1925e6d66a13e5e35bb88f9d7aac2e44202b075c Author: Heikki Linnakangas <heikki.linnakangas@iki.fi> 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; }
* Re: [BUGS] Re: [BUGS] Re: [BUGS] Re: BUG #11431: Failing to backup and restore a Windows postgres database, with Norwegian Bokmål locale. 2014-09-25 12:22 Re: [BUGS] Re: [BUGS] Re: BUG #11431: Failing to backup and restore a Windows postgres database, with Norwegian Bokmål locale. Heikki Linnakangas <hlinnakangas@vmware.com> @ 2014-10-10 12:45 ` Heikki Linnakangas <hlinnakangas@vmware.com> 0 siblings, 0 replies; 2+ messages in thread From: Heikki Linnakangas @ 2014-10-10 12:45 UTC (permalink / raw) To: Noah Misch <noah@leadboat.com>; Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Alon <asimantov@tableausoftware.com>; pgsql-bugs; Bruce Momjian <bruce@momjian.us> On 09/25/2014 03:22 PM, Heikki Linnakangas wrote: > 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. I just realized that pg_upgrade doesn't currently do the canonicalization when upgrading from a pre-8.4 server. So AFAICS, upgrading from 8.3 to 9.2-9.4 will fail, if the locale name in the old cluster doesn't happen to match the canonical form. I committed the pg_upgrade patch discussed on the other thread to master. For the 9.2->9.4 back-branches, I think we should do the attached (0001-Make-the-locale-comparison-in-pg_upgrade-more-lenien.patch). This fixes the problem with 8.3 -> 9.2 upgrades, and prepares pg_upgrade for the "Norwegian (Bokmål)" -> "norwegian-bokmal" mapping. Now, for the mapping. I thought about it some more, and I think we really need to provide instructions to fix existing clusters, as part of the release notes. Even if we do the change to CREATE DATABASE that I included in my earlier patch, you'll continue to have problems with different encodings. And initdb writes the values in postgresql.conf too, for lc_messages et all. Those really should be fixed too. So I don't think there's much point in the CREATE DATABASE part. Let's backpatch the attached patch (0001-Work-around-Windows-locale-name-with-non-ASCII-chara.patch) and add the attached instructions to the release notes. The UPDATE statements in the instructions are specially crafted to avoid "invalid byte sequence" errors, if they're run from a database that uses a different encoding than the one used in the existing rows. I tried using regexp_replace at first, but tries to decode all the characters, and fails. I'm not 100% this would work with every encoding, but at least it works with a mixture of LATIN1 and UTF-8, which are the most likely encodings to be used with Norwegian. These three things together, the pg_upgrade patch, the setlocale() mapping patch and the release note changes, resolves the issue. - Heikki -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs Attachments: download all (3 files, .tar.gz)0001-Make-the-locale-comparison-in-pg_upgrade-more-lenien.patch (text/x-diff, 5.8K) download
From 3159ec354251b3b1e395040677a79b611e92efb7 Mon Sep 17 00:00:00 2001 From: Heikki Linnakangas <heikki.linnakangas@iki.fi> Date: Fri, 10 Oct 2014 13:41:39 +0300 Subject: [PATCH 1/1] Make the locale comparison in pg_upgrade more lenient If the locale names are not equal, try to canonicalize both of them by passing them to setlocale(). Before, we only canonicalized the old cluster's locale if upgrading from a 8.4-9.2 server, but we also need to canonicalize when upgrading from a pre-8.4 server. That was an oversight in the code. But we should also canonicalize on newer server versions, so that we cope if the canonical form changes from one release to another. I'm about to do just that to fix bug #11431, by mapping a locale name that contains non-ASCII characters to a pure-ASCII alias of the same locale. This is partial backpatch of commit 33755e8edf149dabfc0ed9b697a84f70b0cca0de in master. Apply to 9.2, 9.3 and 9.4. The canonicalization code didn't exist before 9.2. --- contrib/pg_upgrade/check.c | 78 +++++++++++++++++++++------------------------- 1 file changed, 35 insertions(+), 43 deletions(-) diff --git a/contrib/pg_upgrade/check.c b/contrib/pg_upgrade/check.c index 73d8cd1..1743ca3 100644 --- a/contrib/pg_upgrade/check.c +++ b/contrib/pg_upgrade/check.c @@ -17,7 +17,7 @@ static void set_locale_and_encoding(ClusterInfo *cluster); static void check_new_cluster_is_empty(void); static void check_locale_and_encoding(ControlData *oldctrl, ControlData *newctrl); -static bool equivalent_locale(const char *loca, const char *locb); +static bool equivalent_locale(int category, const char *loca, const char *locb); static bool equivalent_encoding(const char *chara, const char *charb); static void check_is_super_user(ClusterInfo *cluster); static void check_for_prepared_transactions(ClusterInfo *cluster); @@ -370,23 +370,8 @@ set_locale_and_encoding(ClusterInfo *cluster) i_datcollate = PQfnumber(res, "datcollate"); i_datctype = PQfnumber(res, "datctype"); - if (GET_MAJOR_VERSION(cluster->major_version) < 902) - { - /* - * Pre-9.2 did not canonicalize the supplied locale names to match - * what the system returns, while 9.2+ does, so convert pre-9.2 to - * match. - */ - ctrl->lc_collate = get_canonical_locale_name(LC_COLLATE, - pg_strdup(PQgetvalue(res, 0, i_datcollate))); - ctrl->lc_ctype = get_canonical_locale_name(LC_CTYPE, - pg_strdup(PQgetvalue(res, 0, i_datctype))); - } - else - { - ctrl->lc_collate = pg_strdup(PQgetvalue(res, 0, i_datcollate)); - ctrl->lc_ctype = pg_strdup(PQgetvalue(res, 0, i_datctype)); - } + ctrl->lc_collate = pg_strdup(PQgetvalue(res, 0, i_datcollate)); + ctrl->lc_ctype = pg_strdup(PQgetvalue(res, 0, i_datctype)); PQclear(res); } @@ -418,10 +403,10 @@ static void check_locale_and_encoding(ControlData *oldctrl, ControlData *newctrl) { - if (!equivalent_locale(oldctrl->lc_collate, newctrl->lc_collate)) + if (!equivalent_locale(LC_COLLATE, oldctrl->lc_collate, newctrl->lc_collate)) pg_fatal("lc_collate cluster values do not match: old \"%s\", new \"%s\"\n", oldctrl->lc_collate, newctrl->lc_collate); - if (!equivalent_locale(oldctrl->lc_ctype, newctrl->lc_ctype)) + if (!equivalent_locale(LC_CTYPE, oldctrl->lc_ctype, newctrl->lc_ctype)) pg_fatal("lc_ctype cluster values do not match: old \"%s\", new \"%s\"\n", oldctrl->lc_ctype, newctrl->lc_ctype); if (!equivalent_encoding(oldctrl->encoding, newctrl->encoding)) @@ -434,39 +419,46 @@ check_locale_and_encoding(ControlData *oldctrl, * * Best effort locale-name comparison. Return false if we are not 100% sure * the locales are equivalent. + * + * Note: The encoding parts of the names are ignored. This function is + * currently used to compare locale names stored in pg_database, and + * pg_database contains a separate encoding field. That's compared directly + * in check_locale_and_encoding(). */ static bool -equivalent_locale(const char *loca, const char *locb) +equivalent_locale(int category, const char *loca, const char *locb) { - const char *chara = strrchr(loca, '.'); - const char *charb = strrchr(locb, '.'); - int lencmp; - - /* If they don't both contain an encoding part, just do strcasecmp(). */ - if (!chara || !charb) - return (pg_strcasecmp(loca, locb) == 0); + const char *chara; + const char *charb; + char *canona; + char *canonb; + int lena; + int lenb; /* - * Compare the encoding parts. Windows tends to use code page numbers for - * the encoding part, which equivalent_encoding() won't like, so accept if - * the strings are case-insensitive equal; otherwise use - * equivalent_encoding() to compare. + * If the names are equal, the locales are equivalent. Checking this + * first avoids calling setlocale() in the common case that the names + * are equal. That's a good thing, if setlocale() is buggy, for example. */ - if (pg_strcasecmp(chara + 1, charb + 1) != 0 && - !equivalent_encoding(chara + 1, charb + 1)) - return false; + if (pg_strcasecmp(loca, locb) == 0) + return true; /* - * OK, compare the locale identifiers (e.g. en_US part of en_US.utf8). - * - * It's tempting to ignore non-alphanumeric chars here, but for now it's - * not clear that that's necessary; just do case-insensitive comparison. + * Not identical. Canonicalize both names, remove the encoding parts, + * and try again. */ - lencmp = chara - loca; - if (lencmp != charb - locb) - return false; + canona = get_canonical_locale_name(category, loca); + chara = strrchr(canona, '.'); + lena = chara ? (chara - canona) : strlen(canona); + + canonb = get_canonical_locale_name(category, locb); + charb = strrchr(canonb, '.'); + lenb = charb ? (charb - canonb) : strlen(canonb); + + if (lena == lenb && pg_strncasecmp(canona, canonb, lena) == 0) + return true; - return (pg_strncasecmp(loca, locb, lencmp) == 0); + return false; } /* -- 2.1.10001-Work-around-Windows-locale-name-with-non-ASCII-chara.patch (text/x-diff, 8.6K) download
From 29483a465faa28554f024449c7a5a5ac11bf30f7 Mon Sep 17 00:00:00 2001 From: Heikki Linnakangas <heikki.linnakangas@iki.fi> Date: Mon, 22 Sep 2014 14:59:23 +0300 Subject: [PATCH 1/1] =?UTF-8?q?Work=20around=20Windows=20locale=20name=20w?= =?UTF-8?q?ith=20non-ASCII=20character,=20Norwegian=20(Bokm=C3=A5l)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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". Now, this doesn't change the existing values that are already in pg_database and in postgresql.conf. Old clusters will need to be fixed manually. Instructions for that needs to be put in the release notes. This fixes bug #11431 reported by Alon Siman-Tov. --- src/port/win32setlocale.c | 158 +++++++++++++++++++++++++++++++++------------- 1 file changed, 114 insertions(+), 44 deletions(-) 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; } -- 2.1.1^ permalink raw reply [nested|flat] 2+ messages in threadfix-bokmal-pg_database.txt (text/plain, 1.3K) download
1. Run these two commands as superuser: UPDATE pg_database SET datcollate = 'norwegian-bokmal' || substr(datcollate, position('_' in datcollate)) WHERE datcollate LIKE 'Norwegian (Bokm%'; UPDATE pg_database SET datctype = 'norwegian-bokmal' || substr(datctype, position('_' in datcollate)) WHERE datctype LIKE 'Norwegian (Bokm%'; 2. Fix up locale names in postgresql.conf. Open postgresql.conf in a text editor, and find the settings lc_messages, lc_monetary, lc_numeric, and lc_time. Replace the string "Norwegian (Bokmål)" with "norwegian-bokmal". For example, if your postgresql.conf looks like this: # These settings are initialized by initdb, but they can be changed. lc_messages = 'Norwegian (Bokmål)_Norway.1252' # locale for system error message # strings lc_monetary = 'Norwegian (Bokmål)_Norway.1252' # locale for monetary formatting lc_numeric = 'Norwegian (Bokmål)_Norway.1252' # locale for number formatting lc_time = 'Norwegian (Bokmål)_Norway.1252' # locale for time formatting Change the settings like this: lc_messages = 'norwegian-bokmal_Norway.1252' # locale for system error message # strings lc_monetary = 'norwegian-bokmal_Norway.1252' # locale for monetary formatting lc_numeric = 'norwegian-bokmal_Norway.1252' # locale for number formatting lc_time = 'norwegian-bokmal_Norway.1252' # locale for time formatting
end of thread, other threads:[~2014-10-10 12:45 UTC | newest] Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed) -- links below jump to the message on this page -- 2014-09-25 12:22 Re: [BUGS] Re: [BUGS] Re: BUG #11431: Failing to backup and restore a Windows postgres database, with Norwegian Bokmål locale. Heikki Linnakangas <hlinnakangas@vmware.com> 2014-10-10 12:45 ` Heikki Linnakangas <hlinnakangas@vmware.com>
This inbox is served by DDX for PostgreSQL; see mirroring instructions for how to clone and mirror all data and code used for this inbox