Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.92) (envelope-from ) id 1jI7x9-0000bI-DS for pgsql-general@arkaria.postgresql.org; Sat, 28 Mar 2020 09:40:31 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1jI7x7-0006FV-BI for pgsql-general@arkaria.postgresql.org; Sat, 28 Mar 2020 09:40:29 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1jI7x6-0006FO-Ud for pgsql-general@lists.postgresql.org; Sat, 28 Mar 2020 09:40:29 +0000 Received: from ms-10.1blu.de ([178.254.4.101]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1jI7wz-0007md-Np for pgsql-general@lists.postgresql.org; Sat, 28 Mar 2020 09:40:27 +0000 Received: from [188.174.61.222] (helo=localhost.unixarea.de) by ms-10.1blu.de with esmtpsa (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.86_2) (envelope-from ) id 1jI7wr-0002Fu-7j; Sat, 28 Mar 2020 10:40:13 +0100 Received: from localhost.my.domain (localhost [127.0.0.1]) by localhost.unixarea.de (8.15.2/8.14.9) with ESMTPS id 02S9eCtg003146 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NO); Sat, 28 Mar 2020 10:40:12 +0100 (CET) (envelope-from guru@unixarea.de) Received: (from guru@localhost) by localhost.my.domain (8.15.2/8.14.9/Submit) id 02S9eBCJ003145; Sat, 28 Mar 2020 10:40:11 +0100 (CET) (envelope-from guru@unixarea.de) X-Authentication-Warning: localhost.my.domain: guru set sender to guru@unixarea.de using -f Date: Sat, 28 Mar 2020 10:40:11 +0100 From: Matthias Apitz To: Thomas Munro Cc: Tom Lane , Matthias Apitz , "pgsql-generallists.postgresql.org" Subject: Re: \COPY to accept non UTF-8 chars in CHAR columns Message-ID: <20200328094011.GA2961@c720-r342378> Reply-To: Matthias Apitz References: <20200327153623.GA32266@sh4-5.1blu.de> <8922.1585323994@sss.pgh.pa.us> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: X-Operating-System: FreeBSD 13.0-CURRENT r342378 (amd64) X-message-flag: Mails containing HTML will not be read! Please send only plain text. User-Agent: Mutt/1.11.1 (2018-12-01) X-Con-Id: 51246 X-Con-U: 0-guru X-Originating-IP: 188.174.61.222 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk El día sábado, marzo 28, 2020 a las 09:40:30a. m. +1300, Thomas Munro escribió: > On Sat, Mar 28, 2020 at 4:46 AM Tom Lane wrote: > > Matthias Apitz writes: > > > In short, it there a way to let \COPY accept such broken ISO bytes, just > > > complaining about, but not stopping the insert of the row? > > > > No. We don't particularly believe in the utility of invalid data. > > > > If you don't actually care about what encoding your data is in, > > you could use SQL_ASCII as the database "encoding" and thereby > > disable all UTF8-specific behavior. Otherwise, maybe this conversion > > is a good time to clean up the mess? > > Something like this approach might be useful for fixing the CSV file: > > https://codereview.stackexchange.com/questions/185821/convert-a-mix-of-latin-1-and-utf-8-to-proper-utf-8 > > I haven't tested that program but it looks like the right sort of > approach; I remember writing similar logic to untangle the strange > mixtures of Latin 1, Windows 1252, and UTF-8 that late 90s browsers > used to send. That sort of approach can't fix every theoretical > problem (some valid Latin1 sequences are also valid UTF-8 sequences) > but it's doable with text in European languages. Thomas, This is a very good finding, thanks for this. I played around a bit with the original code, and tested some modifications to fit better in our problem... It works fine: - it translates any ISO char, for example 0xfc (German Umlaut ü in ISO), into the correct UTF-8 coding 0xc3bc: perl -e 'print pack("H*", "4040fc4040")' | ./convert2properUTF8 | hexdump -C 00000000 40 40 c3 bc 40 40 |@@..@@| 00000006 - it translates a situation where 0xc3bc (German Umlaut ü in UTF-8 coding) was broken into two columns, one terminating in 0xc3 and the 2nd row starting with 0xbc; this would give: perl -e 'print pack("H*", "c3")' | ./convert2properUTF8 | hexdump -C 00000000 c3 83 |..| 00000002 perl -e 'print pack("H*", "bc40")' | ./convert2properUTF8 | hexdump -C 00000000 c2 bc 40 |..@| 00000003 i.e. 0xc3 is translated to 0xc383 and the 2nd half, the 0xbc to 0xc2bc, both translations have nothing to do with the original split 0xc3bc, and perhaps in this case it would be better to spill out a blank 0x40 for each of the bytes which formed the 0xc3bc. But this we will discuss here and align the code to our use cases. Thanks again matthias -- Matthias Apitz, ✉ guru@unixarea.de, http://www.unixarea.de/ +49-176-38902045 Public GnuPG key: http://www.unixarea.de/key.pub