Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1aNBCd-0004Zy-B9 for pgsql-bugs@arkaria.postgresql.org; Sun, 24 Jan 2016 03:18:59 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84) (envelope-from ) id 1aNBCc-0008As-Pi for pgsql-bugs@arkaria.postgresql.org; Sun, 24 Jan 2016 03:18:58 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1aNBBy-0007U2-LW for pgsql-bugs@postgresql.org; Sun, 24 Jan 2016 03:18:18 +0000 Received: from relay4-d.mail.gandi.net ([2001:4b98:c:538::196]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1aNBBt-0008CH-62 for pgsql-bugs@postgresql.org; Sun, 24 Jan 2016 03:18:17 +0000 Received: from [IPv6:2001:41d0:fc94:4400:ca0a:a9ff:fe90:47fd] (unknown [IPv6:2001:41d0:fc94:4400:ca0a:a9ff:fe90:47fd]) (Authenticated sender: mlpo@mlpo.fr) by relay4-d.mail.gandi.net (Postfix) with ESMTPSA id BCD6717209A; Sun, 24 Jan 2016 04:18:08 +0100 (CET) Subject: Re: BUG #13440: unaccent does not remove all diacritics To: Thomas Munro References: <20150614063707.2734.30270@wrigleys.postgresql.org> <12238.1434304758@sss.pgh.pa.us> <20150615045056.GM133018@postgresql.org> <38161.1434372933@sss.pgh.pa.us> <1790.1434492074@sss.pgh.pa.us> <4186.1434655846@sss.pgh.pa.us> Cc: Tom Lane , Alvaro Herrera , Michael Gradek , PostgreSQL Bugs From: =?UTF-8?Q?L=c3=a9onard_Benedetti?= X-Enigmail-Draft-Status: N1110 Message-ID: <56A4426F.2040108@mlpo.fr> Date: Sun, 24 Jan 2016 04:18:07 +0100 User-Agent: Mozilla/5.0 (X11; Linux i686; rv:38.0) Gecko/20100101 Thunderbird/38.5.1 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/mixed; boundary="------------010102080507080303040108" X-Pg-Spam-Score: -2.6 (--) 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 This is a multi-part message in MIME format. --------------010102080507080303040108 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Le 19/06/2015 04:00, Thomas Munro a =C3=A9crit : > On Fri, Jun 19, 2015 at 7:30 AM, Tom Lane wrote: >> I took a quick look at this list and it seems fairly sane as far as >> the automatically-generated items go, except that I see it hits a few >> LIGATURE cases (including the existing ij cases, but also fi fl and >> ffl). I'm still quite dubious that that is appropriate; at least, if >> we do it I think we should be expanding out to the equivalent >> multi-letter form, not simply taking one of the letters and dropping >> the rest. Anybody else have an opinion on how to handle ligatures?=20 > Here is a version that optionally expands ligatures if asked to with > --expand-ligatures. It uses the Unicode 'general category' data to > identify and strip diacritical marks and distinguish them from > ligatures which are expanded to all their parts. It meant I had to > load a bunch of stuff into memory up front, but this approach can > handle an awkward bunch of ligatures whose component characters have > marks: =C7=84, =C7=85, =C7=86 -> DZ, Dz, dz. (These are considered to be= single > characters to maintain a one-to-one mapping with certain Cyrillic > characters in some Balkan countries which use or used both scripts.) > > As for whether we *should* expand ligatures, I'm pretty sure that's > what I'd always want, but my only direct experience of languages with > ligatures as part of the orthography (rather than ligatures as a > typesetting artefact like =EF=AC=84 et al) is French, where =C5=93 is use= d in the > official spelling of a bunch of words like =C5=93il, s=C5=93ur, c=C5=93ur= , =C5=93uvre when > they appear in books, but substituting oe is acceptable on computers > because neither the standard French keyboard nor the historically > important Latin1 character set includes the character. I'm fairly > sure the Dutch have a similar situation with =C4=B2, it's completely > interchangeable with the sequence IJ. > > So +1 from me for ligature expansion. It might be tempting to think > that a function called 'unaccent' should only remove diacritical > marks, but if we are going to be pedantic about it, not all > diacritical marks are actually accents anyway... > >> The manually added special cases don't look any saner than they did >> before :-(. Anybody have an objection to removing those (except maybe >> dotless i) in HEAD? > +1 from me for getting rid of the bogus =C5=93->e, =C4=B2 -> I, ... trans= formations, but: > > 1. For some reason =C5=93, =C3=A6 (and uppercase equivalents) don't have > combining character data in the Unicode file, so they still need to be > treated as special cases if we're going to include ligatures. Their > expansion should of course be oe and ae rather that what we have. > 2. Likewise =C3=9F still needs special treatment (it may be historically > composed of sz but Unicode doesn't know that, it's its own character > now and expands to ss anyway). > 3. I don't see any reason to drop the Afrikaans =C5=89, though it should > surely be expanded to 'n rather than n. > 4. I have no clue about whether the single Cyrillic item in there > belongs there. > > Just by the way, there are conventional rules for diacritic removal in > some languages, like =C3=A4, =C3=B6, =C3=BC -> ae, oe, ue in German, =C3= =A5 -> aa in > Scandinavian languages and =C3=A8 -> e' in Italian. A German friend of > mine has a =C3=BC in his last name and he finishes up with any of three > possible spellings of his name on various official documents, credit > cards etc as a result! But these sorts of things are specific to > individual languages and don't belong in a general accent removal rule > file (it would be inappropriate to convert French aig=C3=BCe to aiguee or > Spanish ping=C3=BCino to pingueino). I guess speakers of those languages > could consider submitting rules files for language-specific > conventions like that. > I use "unaccent" and I am very pleased with the applied patches for the default rules and the Python script to generate them. But as you pointed out, the "extra cases" (the subset of characters which is not generated by the script, but hardcoded) are pretty disturbing. The main problem to me is that it lacks a number of "extra cases". In fact, the script manages arbitrarily few ligatures but leaves many things aside. So I looked for a way to improve the generation, to avoid having this trouble. As you said, some characters don't have Unicode decomposition. So, to handle all these cases, we can use the standard Unicode transliterator Latin-ASCII (available in CLDR), it associates Unicode characters to ASCII-range equivalent. This approach seems much more elegant, this avoids hardcoded cases and transliterations are semantically correct (at least, as much as they can). So, I modified the script: the arguments of the command line are used to pass the file path of the transliterator (available as an XML file in Unicode Common Locale Data Repository), so you find attached the new script and the generated output for convenience, I will also propose a patch for Commitfest. Note that the script now takes (at most) two input files: UnicodeData.txt and (optionally) the XML file of the transliterator. By the way, I took the opportunity to make the script more user-friendly by several surface changes. There is now a very light support for command line arguments with help messages. The text file was, before, passed to the script on standard input; this approach is not appropriate when two files must be used. So as I mentioned, the arguments of the command line are now used to pass the paths. Finally, the use of this transliterator increase inevitably the number of characters handled, I do not think it's a problem (there is 1044 characters handled), on the contrary, and after several tests on index generations, I have no significant performance difference. Nonetheless, using the transliterator remains optional and a command line option is available to disable it (so one can easily generate a small rules file, if desired). It seemed however logical to me to keep it on by default: that is, a priori, the desired behavior. L=C3=A9onard Benedetti --------------010102080507080303040108 Content-Type: text/plain; charset=UTF-8; name="unaccent.rules" Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename="unaccent.rules" wqkJKEMpCsKrCTw8CsKtCS0Kwq4JKFIpCsK7CT4+CsK8CSAxLzQKwr0JIDEv MgrCvgkgMy80CsOACUEKw4EJQQrDgglBCsODCUEKw4QJQQrDhQlBCsOGCUFF CsOHCUMKw4gJRQrDiQlFCsOKCUUKw4sJRQrDjAlJCsONCUkKw44JSQrDjwlJ CsOQCUQKw5EJTgrDkglPCsOTCU8Kw5QJTwrDlQlPCsOWCU8Kw5cJKgrDmAlP CsOZCVUKw5oJVQrDmwlVCsOcCVUKw50JWQrDnglUSArDnwlzcwrDoAlhCsOh CWEKw6IJYQrDowlhCsOkCWEKw6UJYQrDpglhZQrDpwljCsOoCWUKw6kJZQrD qgllCsOrCWUKw6wJaQrDrQlpCsOuCWkKw68JaQrDsAlkCsOxCW4Kw7IJbwrD swlvCsO0CW8Kw7UJbwrDtglvCsO3CS8Kw7gJbwrDuQl1CsO6CXUKw7sJdQrD vAl1CsO9CXkKw74JdGgKw78JeQrEgAlBCsSBCWEKxIIJQQrEgwlhCsSECUEK xIUJYQrEhglDCsSHCWMKxIgJQwrEiQljCsSKCUMKxIsJYwrEjAlDCsSNCWMK xI4JRArEjwlkCsSQCUQKxJEJZArEkglFCsSTCWUKxJQJRQrElQllCsS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JeArhuowJWArh uo0JeArhuo4JWQrhuo8JeQrhupAJWgrhupEJegrhupIJWgrhupMJegrhupQJ WgrhupUJegrhupYJaArhupcJdArhupgJdwrhupkJeQrhupoJYQrhupwJcwrh up0Jcwrhup4JU1MK4bqgCUEK4bqhCWEK4bqiCUEK4bqjCWEK4bq4CUUK4bq5 CWUK4bq6CUUK4bq7CWUK4bq8CUUK4bq9CWUK4buICUkK4buJCWkK4buKCUkK 4buLCWkK4buMCU8K4buNCW8K4buOCU8K4buPCW8K4bukCVUK4bulCXUK4bum CVUK4bunCXUK4buyCVkK4buzCXkK4bu0CVkK4bu1CXkK4bu2CVkK4bu3CXkK 4bu4CVkK4bu5CXkK4bu6CUxMCuG7uwlsbArhu7wJVgrhu70Jdgrhu74JWQrh u78JeQrigJAJLQrigJEJLQrigJIJLQrigJMJLQrigJQJLQrigJUJLQrigJYJ fHwK4oCYCScK4oCZCScK4oCaCSwK4oCbCScK4oCcCSIK4oCdCSIK4oCeCSws CuKAnwkiCuKApAkuCuKApQkuLgrigKYJLi4uCuKAsgknCuKAswkiCuKAuQk8 CuKAugk+CuKAvAkhIQrigYQJLwrigYUJWwrigYYJXQrigYcJPz8K4oGICT8h CuKBiQkhPwrigY4JKgrigqAJQ0UK4oKiCUNyCuKCowlGci4K4oKkCUwuCuKC pwlQdHMK4oK5CVJzCuKCuglUTArihIAJYS9jCuKEgQlhL3MK4oSCCUMK4oSF CWMvbwrihIYJYy91CuKEiglnCuKEiwlICuKEjAl4CuKEjQlICuKEjgloCuKE kAlJCuKEkQlJCuKEkglMCuKEkwlsCuKElQlOCuKElglObwrihJkJUArihJoJ UQrihJsJUgrihJwJUgrihJ0JUgrihJ4JUngK4oShCVRFTArihKQJWgrihKgJ WgrihKwJQgrihK0JQwrihK8JZQrihLAJRQrihLEJRgrihLMJTQrihLQJbwri hLkJaQrihLsJRkFYCuKFhQlECuKFhglkCuKFhwllCuKFiAlpCuKFiQlqCuKF kwkgMS8zCuKFlAkgMi8zCuKF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rikq4JKHMpCuKS rwkodCkK4pKwCSh1KQrikrEJKHYpCuKSsgkodykK4pKzCSh4KQrikrQJKHkp CuKStQkoeikK4qaFCSgoCuKmhgkpKQriqbQJOjo9CuKptQk9PQriqbYJPT09 CuOAgQksCuOAggkuCuOAhwkwCuOAiAk8CuOAiQk+CuOAigk8PArjgIsJPj4K 44CUCVsK44CVCV0K44CYCVsK44CZCV0K44CaCVsK44CbCV0K44CdCSIK44Ce CSIK442xCWhQYQrjjbIJZGEK442zCUFVCuONtAliYXIK4421CW9WCuONtglw YwrjjbcJZG0K4426CUlVCuOOgAlwQQrjjoEJbkEK446DCW1BCuOOhAlrQQrj joUJS0IK446GCU1CCuOOhwlHQgrjjogJY2FsCuOOiQlrY2FsCuOOiglwRgrj josJbkYK446OCW1nCuOOjwlrZwrjjpAJSHoK446RCWtIegrjjpIJTUh6CuOO kwlHSHoK446UCVRIegrjjpkJZm0K446aCW5tCuOOnAltbQrjjp0JY20K446e CWttCuOOpwltL3MK446pCVBhCuOOqglrUGEK446rCU1QYQrjjqwJR1BhCuOO rQlyYWQK446uCXJhZC9zCuOOsAlwcwrjjrEJbnMK446zCW1zCuOOtAlwVgrj jrUJblYK4463CW1WCuOOuAlrVgrjjrkJTVYK4466CXBXCuOOuwluVwrjjr0J bVcK446+CWtXCuOOvwlNVwrjj4IJYS5tLgrjj4MJQnEK44+ECWNjCuOPhQlj ZArjj4YJQy9rZwrjj4cJQ28uCuOPiAlkQgrjj4kJR3kK44+KCWhhCuOPiwlI UArjj4wJaW4K44+NCUtLCuOPjglLTQrjj48Ja3QK44+QCWxtCuOPkQlsbgrj j5IJbG9nCuOPkwlseArjj5QJbWIK44+VCW1pbArjj5YJbW9sCuOPlwlwSArj j5gJcC5tLgrjj5kJUFBNCuOPmglQUgrjj5sJc3IK44+cCVN2CuOPnQlXYgrj j54JVi9tCuOPnwlBL20K76yACWZmCu+sgQlmaQrvrIIJZmwK76yDCWZmaQrv rIQJZmZsCu+shQlzdArvrIYJc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vvZMJcwrvvZQJdArvvZUJdQrvvZYJdgrvvZcJdwrvvZgJeArvvZkJeQrv vZoJegrvvZsJewrvvZwJfArvvZ0JfQrvvZ4JfgrvvZ8JKCgK772gCSkpCu+9 oQkuCu+9pAksCg== --------------010102080507080303040108 Content-Type: text/x-python; name="contrib_unaccent_generate_unaccent_rules.py" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="contrib_unaccent_generate_unaccent_rules.py" #!/usr/bin/python2 # -*- coding: utf-8 -*- # # This script builds unaccent.rules on standard output when given the # contents of UnicodeData.txt [1] and Latin-ASCII.xml [2] given as # arguments. Optionally includes ligature expansion and Unicode CLDR # Latin-ASCII transliterator, enabled by default, this can be disabled # with =E2=80=9C--no-ligatures-expansion=E2=80=9D command line option. # # The approach is to use the Unicode decomposition data to identify # precomposed codepoints that are equivalent to a ligature of several # letters, or a base letter with any number of diacritical marks. # # This approach handles most letters with diacritical marks and some # ligatures. However, several characters (notably a majority of # ligatures) don't have decomposition. To handle all these cases, one can # use a standard Unicode transliterator available in Common Locale Data # Repository (CLDR): Latin-ASCII. This transliterator associates Unicode # characters to ASCII-range equivalent. Unless =E2=80=9C--no-ligatures-exp= ansion=E2=80=9D # option is enabled, the XML file of this transliterator [2] -- given as a # command line argument -- will be parsed and used. # # [1] http://unicode.org/Public/8.0.0/ucd/UnicodeData.txt # [2] http://unicode.org/cldr/trac/export/12304/tags/release-28/common/tran= sforms/Latin-ASCII.xml import re import argparse import sys import xml.etree.ElementTree as ET def print_record(codepoint, letter): print (unichr(codepoint) + "\t" + letter).encode("UTF-8") class Codepoint: def __init__(self, id, general_category, combining_ids): self.id =3D id self.general_category =3D general_category self.combining_ids =3D combining_ids def is_plain_letter(codepoint): """Return true if codepoint represents a plain ASCII letter.""" return (codepoint.id >=3D ord('a') and codepoint.id <=3D ord('z')) or \ (codepoint.id >=3D ord('A') and codepoint.id <=3D ord('Z')) def is_mark(codepoint): """Returns true for diacritical marks (combining codepoints).""" return codepoint.general_category in ("Mn", "Me", "Mc") def is_letter_with_marks(codepoint, table): """Returns true for plain letters combined with one or more marks.""" # See http://www.unicode.org/reports/tr44/tr44-14.html#General_Category= _Values return len(codepoint.combining_ids) > 1 and \ is_plain_letter(table[codepoint.combining_ids[0]]) and \ all(is_mark(table[i]) for i in codepoint.combining_ids[1:]) def is_letter(codepoint, table): """Return true for letter with or without diacritical marks.""" return is_plain_letter(codepoint) or is_letter_with_marks(codepoint, ta= ble) def get_plain_letter(codepoint, table): """Return the base codepoint without marks.""" if is_letter_with_marks(codepoint, table): return table[codepoint.combining_ids[0]] elif is_plain_letter(codepoint): return codepoint else: raise "mu" def is_ligature(codepoint, table): """Return true for letters combined with letters.""" return all(is_letter(table[i], table) for i in codepoint.combining_ids) def get_plain_letters(codepoint, table): """Return a list of plain letters from a ligature.""" assert(is_ligature(codepoint, table)) return [get_plain_letter(table[id], table) for id in codepoint.combinin= g_ids] =20=20=20=20 def parse_cldr_latin_ascii_transliterator(latinAsciiFilePath): """Parse the XML file and return a set of tuples (src, trg), where =E2= =80=9Csrc=E2=80=9D is the original character and =E2=80=9Ctrg=E2=80=9D the substitute.""" charactersSet =3D set() =20=20=20=20 # RegEx to parse rules (e.g. =E2=80=9C=C4=90 =E2=86=92 D ; [=E2=80=A6]= =E2=80=9D) rulePattern =3D re.compile(ur'^(?:(.)|(\\u[0-9a-fA-F]{4})) =E2=86=92 (?= :\'(.+)\'|(.+)) ;') =20=20=20=20 # construct tree from XML transliterationTree =3D ET.parse(latinAsciiFilePath) transliterationTreeRoot =3D transliterationTree.getroot() =20=20=20=20 for rule in transliterationTreeRoot.findall("./transforms/transform/tRu= le"): matches =3D rulePattern.search(rule.text) =20=20=20=20=20=20=20=20 # The regular expression capture four groups corresponding # to the characters. # # Group 1: plain =E2=80=9Csrc=E2=80=9D char (e.g. =E2=80=9C=C4=90= =E2=80=9D). Empty if group 2 is not. # Group 2: unicode-espaced =E2=80=9Csrc=E2=80=9D char (e.g. =E2=80= =9C\u0110=E2=80=9D). Empty if group 1 is not. # # Group 3: plain =E2=80=9Ctrg=E2=80=9D char (e.g. =E2=80=9CD=E2=80= =9D). Empty if group 4 is not. # Group 4: plain =E2=80=9Ctrg=E2=80=9D char between quotes (e.g. = =E2=80=9C'(C)'=E2=80=9D). Empty if group 3 is not. if matches is not None: src =3D matches.group(1) if matches.group(1) is not None else m= atches.group(2).decode('unicode-escape') trg =3D matches.group(3) if matches.group(3) is not None else m= atches.group(4) # =E2=80=9C'=E2=80=9D and =E2=80=9C"=E2=80=9D are escaped trg =3D trg.replace("\\'", "'").replace('\\"', '"') # the parser of unaccent accept only non-whitespace characters # for =E2=80=9Csrc=E2=80=9D and =E2=80=9Ctrg=E2=80=9D (see unac= cent.c) if not src.isspace() and not trg.isspace(): charactersSet.add((ord(src), trg)) =20=20=20=20 return charactersSet def main(args): # http://www.unicode.org/reports/tr44/tr44-14.html#Character_Decomposit= ion_Mappings decomposition_type_pattern =3D re.compile(" *<[^>]*> *") table =3D {} all =3D [] =20=20=20=20 # unordered set for ensure uniqueness charactersSet =3D set() =20=20=20=20 # read file UnicodeData.txt unicodeDataFile =3D open(args.unicodeDataFilePath, 'r') # read everything we need into memory for line in unicodeDataFile: fields =3D line.split(";") if len(fields) > 5: # http://www.unicode.org/reports/tr44/tr44-14.html#UnicodeData.= txt general_category =3D fields[2] decomposition =3D fields[5] decomposition =3D re.sub(decomposition_type_pattern, ' ', decom= position) id =3D int(fields[0], 16) combining_ids =3D [int(s, 16) for s in decomposition.split(" ")= if s !=3D ""] codepoint =3D Codepoint(id, general_category, combining_ids) table[id] =3D codepoint all.append(codepoint) # walk through all the codepoints looking for interesting mappings for codepoint in all: if codepoint.general_category.startswith('L') and \ len(codepoint.combining_ids) > 1: if is_letter_with_marks(codepoint, table): charactersSet.add((codepoint.id, chr(get_plain_letter(codepoint, table).id))) elif args.noLigaturesExpansion is False and is_ligature(codepoi= nt, table): charactersSet.add((codepoint.id, "".join(unichr(combining_codepoint.id) for combining_codepoint \ in get_plain_letters(codepoint, table)= ))) =20=20=20=20 # add CLDR Latin-ASCII characters if not args.noLigaturesExpansion: charactersSet |=3D parse_cldr_latin_ascii_transliterator(args.latin= AsciiFilePath) =20=20=20=20=20=20=20=20 # sort for more convenient display charactersList =3D sorted(charactersSet, key=3Dlambda characterPair: ch= aracterPair[0]) =20=20=20=20 for characterPair in charactersList: print_record(characterPair[0], characterPair[1]) if __name__ =3D=3D "__main__": parser =3D argparse.ArgumentParser(description=3D'This script builds un= accent.rules on standard output when given the contents of UnicodeData.txt = and Latin-ASCII.xml given as arguments.') parser.add_argument("--unicode-data-file", help=3D"Path to formatted te= xt file corresponding to UnicodeData.txt. See .", type=3Dstr, required=3DTrue, dest=3D'unicodeDat= aFilePath') parser.add_argument("--latin-ascii-file", help=3D"Path to XML file from= Unicode Common Locale Data Repository (CLDR) corresponding to Latin-ASCII = transliterator (Latin-ASCII.xml). See .", type=3Dstr, des= t=3D'latinAsciiFilePath') parser.add_argument("--no-ligatures-expansion", help=3D"Do not expand l= igatures and do not use Unicode CLDR Latin-ASCII transliterator. By default= , this option is not enabled and =E2=80=9C--latin-ascii-file=E2=80=9D argum= ent is required. If this option is enabled, =E2=80=9C--latin-ascii-file=E2= =80=9D argument is optional and ignored.", action=3D"store_true", dest=3D'n= oLigaturesExpansion') args =3D parser.parse_args() =20=20=20=20 if args.noLigaturesExpansion is False and args.latinAsciiFilePath is No= ne: sys.stderr.write('You must specify the path to Latin-ASCII translit= erator file with =E2=80=9C--latin-ascii-file=E2=80=9D option or use =E2=80= =9C--no-ligatures-expansion=E2=80=9D option. Use =E2=80=9C-h=E2=80=9D optio= n for help.') sys.exit(1) =20=20=20=20 main(args) --------------010102080507080303040108 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 --------------010102080507080303040108--