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.89) (envelope-from ) id 1iGNaZ-0008AA-5E for pgsql-general@arkaria.postgresql.org; Fri, 04 Oct 2019 13:25:43 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iGNaX-0006a2-Rb for pgsql-general@arkaria.postgresql.org; Fri, 04 Oct 2019 13:25:41 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iGNaX-0006Zq-Hh for pgsql-general@lists.postgresql.org; Fri, 04 Oct 2019 13:25:41 +0000 Received: from ms-10.1blu.de ([178.254.4.101]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iGNaQ-0007Jj-KZ for pgsql-general@lists.postgresql.org; Fri, 04 Oct 2019 13:25:41 +0000 Received: from [188.174.51.111] (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 1iGNaO-00045D-Ss; Fri, 04 Oct 2019 15:25:33 +0200 Received: from localhost.my.domain (localhost [127.0.0.1]) by localhost.unixarea.de (8.15.2/8.14.9) with ESMTPS id x94DPUmK003030 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NO); Fri, 4 Oct 2019 15:25:30 +0200 (CEST) (envelope-from guru@unixarea.de) Received: (from guru@localhost) by localhost.my.domain (8.15.2/8.14.9/Submit) id x94DPT6U003029; Fri, 4 Oct 2019 15:25:29 +0200 (CEST) (envelope-from guru@unixarea.de) X-Authentication-Warning: localhost.my.domain: guru set sender to guru@unixarea.de using -f Date: Fri, 4 Oct 2019 15:25:29 +0200 From: Matthias Apitz To: pgsql-general@lists.postgresql.org Subject: Perl DBI converts UTF-8 again to UTF-8 before sending it to the server Message-ID: <20191004132529.GA2871@c720-r342378> Reply-To: Matthias Apitz MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="gBBFr7Ir9EOA20Yy" Content-Disposition: inline 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.51.111 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --gBBFr7Ir9EOA20Yy Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Hello, We're facing the problem that UTF-8 data to be INSERT'ed into a CHAR column is converted again to UTF-8, assuming it's ISO. I have here a small Perl program which can be used for testing: #!/usr/local/bin/perl use utf8; my $PGDB =3D 'dbi:Pg:dbname=3Dnewsisis;host=3D127.0.0.1'; my $PGDB_USER =3D 'sisis'; =20 my $SQL_INSERT =3D 'INSERT INTO dbctest (tstchar25, tstint) VALUES (?, ?)'; use DBI; my $dbh =3D DBI->connect($PGDB, $PGDB_USER) || die "Couldn't connect to $PGDB as user $PGDB_USER: $DBI::errstr\n"; print "DBI is version $DBI::VERSION, DBD::Pg is version $DBD::Pg::VERSION\n= "; $dbh->do("SET client_encoding TO UTF8"); $dbh->{pg_enable_utf8} =3D 1; my $sth =3D $dbh->prepare( $SQL_INSERT ) || die "Can't prepare insert statement $SQL_INSERT: $DBI::errstr"; my $text =3D "\xc3\xa4"; print "text: ".$text."\n"; $sth->execute($text, 1) or die $sth->errstr, "\n"; Running this, gives the following output: $ ./utf8.pl DBI is version 1.642, DBD::Pg is version 3.8.0 text: =C3=A4 $ ./utf8.pl | od -tx1 0000000 44 42 49 20 69 73 20 76 65 72 73 69 6f 6e 20 31 0000020 2e 36 34 32 2c 20 44 42 44 3a 3a 50 67 20 69 73 0000040 20 76 65 72 73 69 6f 6e 20 33 2e 38 2e 30 0a 74 0000060 65 78 74 3a 20 c3 a4 0a ^^^^^ (this shows that the var '$text' contains \xc3a4, an UTF-8 '=C3=A4' (a-Umlaut). If we now look into the table in hex we see: $ printf "select tstchar25::bytea from dbctest ;\n" | psql -Usisis -dnewsis= is tstchar25 ---------------------------------------------------------- \xc383c2a42020202020202020202020202020202020202020202020 (1 Zeile) i.e. the '=C3=A4' is converted again, like this cmd would do: $ printf '=C3=A4' | iconv -f iso-8859-1 -t utf-8 | od -tx1 0000000 c3 83 c2 a4 and ofc it's looking broken: $ printf "select tstchar25 from dbctest ;\n" | psql -Usisis -dnewsisis tstchar25 --------------------------- =C3=83=C2=A4 (1 Zeile) I watched the trafic between the client ./utf8.pl and the server with strace and it's sent broken already to the server: =2E.. write(1, "text: \303\244\n", 9) =3D 9 sendto(3, "P\0\0\0G\0INSERT INTO dbctest (tstchar25, tstint) VALUES ($1, $2= )\0\0\2\0\0\0\0\0\0\0\0B\0\0\0\33\0\0\0\0\0\2\0\0\0\4\303\203\302\244\0\0\0= \0011\0\1\0\0D\0\0\0\6P\0E\0\0\0\t\0\0\0\0\0S\0\0\0\4", 122, MSG_NOSIGNAL, = NULL, 0) =3D 122 =2E.. see the sequence '\303\203\302\244' in octal values. What is the problem with DBI? Thanks matthias --=20 Matthias Apitz, =E2=9C=89 guru@unixarea.de, http://www.unixarea.de/ +49-176= -38902045 Public GnuPG key: http://www.unixarea.de/key.pub 3. Oktober! Wir gratulieren! Der Berliner Fernsehturm wird 50=20 aus: https://www.jungewelt.de/2019/10-02/index.php --gBBFr7Ir9EOA20Yy Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCAAdFiEEXmn7rBYYViyzy/vBR8z35Hb+nREFAl2XSEYACgkQR8z35Hb+ nRHNLw/6AtPvTICnIb5s6ecwkuck5VOOdv9lETtIQ9W9Svg3ccNu2LZJC6pk0xk/ YHA+jvzN7yIqFRCbkndi5yJjlS2uMpidYzYhQaEXzwq4muWInychO0SOtrJGmIdX /rETyxc1xOz6E0GhKo1gqol8mv1g2h0uhGdCCp4fJsCg3FJcvkEX3RX07s8h/tdw NqDn4PIZt6aYwBBQdUvpo5OGBwAkq6f8JuevNpL2/uuLfDQYza0LZpdZ3ZQAcglS f7ih+RaM4g3N/ZebFPKPA6FXnjoc4kvdgxvYERak6mKbKKiBVOUDmY3VSDvHqfyu q2vv2vUPYifelBGUTj6q41priNIqVJhI6qOo88/JXD3TITZkg5YmLSID/+bHf0kQ RCBT4u0oBbvqOjhMfTXQ1tnMyLIGim8PosY0NzUw36P3PXMyGKntT1hjwYmwpeyo hvWrQxraktdFdn9aPI6vP4oqVNRAv+pSfWFU7WAd+TUPumbVQsa0akUIz0ulm92H rHXyzBcKXYgxbk/K2hsRxYOYecSgAaNCBCZpHjLc2KZq9JS43VQJer31nH5TKM4V 9/d0p4rZJ0SZUvyJik4smJ5ms67RTHjNeDdYgUgjandps5bPfwwEXMbMnC8S5KgQ U/KdFGgipIVc6DMhhqsA2V8zmrH75VxmMTvKUuAx291STvKIxHk= =em9V -----END PGP SIGNATURE----- --gBBFr7Ir9EOA20Yy--