Re: COPY JSON: use trailing commas in FORCE_ARRAY output

From: Chao Li <li(dot)evan(dot)chao(at)gmail(dot)com>
To: Alex Guo <guo(dot)alex(dot)hengchen(at)gmail(dot)com>
Cc: PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: COPY JSON: use trailing commas in FORCE_ARRAY output
Date: 2026-05-06 09:25:38
Message-ID: 3E68D1C4-8DA9-437F-B05A-DEB74782676F@gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

> On May 6, 2026, at 16:46, Alex Guo <guo(dot)alex(dot)hengchen(at)gmail(dot)com> wrote:
>
>
> On 5/6/26 2:40 PM, Chao Li wrote:
>> Hi,
>>
>> Another issue I found with COPY TO (FORMAT json, FORCE_ARRAY) is that it places the delimiter comma at the beginning of the next line, like this:
>> ```
>> evantest=# copy test_json_copy to stdout with (format json, force_array);
>> [
>> {"id":1,"name":"Alice","is_active":true,"tags":["dev","db"],"created_at":"2026-05-01"}
>> ,{"id":2,"name":"Bob","is_active":false,"tags":["manager"],"created_at":"2026-05-02"}
>> ,{"id":3,"name":"Charlie","is_active":null,"tags":null,"created_at":null}
>> ,{"id":4,"name":"Special Case: \"Quotes\"","is_active":true,"tags":["a","b"],"created_at":"2026-05-04"}
>> ]
>> ```
>>
>> I was surprised by the comma placement. It is valid JSON, but it looks quite uncommon.
>>
>> For comparison, the existing json_agg() places commas at the end of the line:
>> ```
>> evantest=# select json_agg(t) from (select id, name from test_json_copy) t;
>> json_agg
>> ----------------------------------------------
>> [{"id":1,"name":"Alice"}, +
>> {"id":2,"name":"Bob"}, +
>> {"id":3,"name":"Charlie"}, +
>> {"id":4,"name":"Special Case: \"Quotes\""}]
>> (1 row)
>> ```
>>
>> If this feature had already been released, I would not think it worth changing just for formatting. But since "FORMAT json" is a new PG19 feature and has not been released yet, I think it is better to make the output to follow the more common style.
>>
>> This patch changes the output to place the comma at the end of the previous line instead. The fix only adjusts how commas and newlines are emitted. It does not buffer the whole result, so it should not have any performance impact.
>>
>> See the attached patch for details.
>>
>> Best regards,
>> --
>> Chao Li (Evan)
>> HighGo Software Co., Ltd.
>> https://www.highgo.com/
>>
>>
>>
> Thanks for the patch, I like it as I feel better with placing commas at the end of lines.
>
> I have a small suggestion. The function name CopySendTextLikeEOL reads very similar to the existing CopySendTextLikeEndOfRow. Would it better to rename it to CopySendTextLikeLineTerminator?
>
> Other than that, the patch looks good to me.
>
> Regards,
> Alex Guo

Thanks for the suggestion, I take it.

PFA v2 - Renamed CopySendTextLikeEOL to CopySendTextLikeLineTerminator.

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/

Attachment Content-Type Size
v2-0001-COPY-JSON-use-trailing-commas-in-FORCE_ARRAY-outp.patch application/octet-stream 5.4 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message shveta malik 2026-05-06 09:31:30 Re: Proposal: Conflict log history table for Logical Replication
Previous Message Kirill Reshke 2026-05-06 09:20:15 Re: support create index on virtual generated column.