]> git.ipfire.org Git - thirdparty/postgresql.git/commit
Apply encoding conversion in COPY TO FORMAT JSON
authorAndrew Dunstan <andrew@dunslane.net>
Mon, 20 Apr 2026 05:46:36 +0000 (11:16 +0530)
committerAndrew Dunstan <andrew@dunslane.net>
Sat, 30 May 2026 01:48:45 +0000 (21:48 -0400)
commit7dc5bbcf220be05786a08aad2455c61b6dbd4b78
treef7b7e71bfa83d41b62e0ce34b7d561c659d48562
parent45b02984e2fa9ee7bd83f7e58797b160baf0cc13
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 <ayushtiwari.slg01@gmail.com>
Reviewed-by: Andrew Dunstan <andrew@dunslane.net>
Discussion: https://postgr.es/m/CAJTYsWX-jsLzxGRAb-dWnEpGYRPbDYHwce8LctVE92LiDfM2Jw@mail.gmail.com
src/backend/commands/copyto.c
src/test/regress/expected/copyencoding.out
src/test/regress/sql/copyencoding.sql