Apply encoding conversion in COPY TO FORMAT JSON

CopyToJsonOneRow() sent the output of composite_to_json() directly
via CopySendData() without encoding conversion.  The text and CSV
paths convert per-attribute via pg_server_to_any() when
need_transcoding is true, but the JSON path skipped this entirely.

This meant COPY ... TO ... WITH (FORMAT json, ENCODING 'LATIN1') on
a UTF-8 server silently produced UTF-8 output, and COPY TO STDOUT
with a non-UTF-8 client_encoding would send unconverted bytes to
the client.

Apply pg_server_to_any() to the whole JSON buffer after
composite_to_json() returns, converting to the requested file
encoding when it differs from the server encoding.  Tests cover
both the explicit ENCODING option and the implicit case where
file_encoding is inherited from client_encoding.

Introduced by 7dadd38cda9 (json format for COPY TO).

Author: Ayush Tiwari <[email protected]>
Reviewed-by: Andrew Dunstan <[email protected]>
Discussion: 
https://postgr.es/m/cajtyswx-jslzxgrab-dwnepgyrpbdyhwce8lctve92lidfm...@mail.gmail.com

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/7dc5bbcf220be05786a08aad2455c61b6dbd4b78

Modified Files
--------------
src/backend/commands/copyto.c              | 20 +++++++++++++++++++-
src/test/regress/expected/copyencoding.out | 27 +++++++++++++++++++++++++++
src/test/regress/sql/copyencoding.sql      | 19 +++++++++++++++++++
3 files changed, 65 insertions(+), 1 deletion(-)

Reply via email to