Apply encoding conversion in COPY TO FORMAT JSON
Andrew Dunstan <andrew@dunslane.net>
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
Files
| Path | Change | +/− |
|---|---|---|
| src/backend/commands/copyto.c | modified | +19 −1 |
| src/test/regress/expected/copyencoding.out | modified | +27 −0 |
| src/test/regress/sql/copyencoding.sql | modified | +19 −0 |
Discussion
- [PATCH] Reject ENCODING option for COPY TO FORMAT JSON 10 messages · 2026-04-20 → 2026-05-30