public inbox for [email protected]  
help / color / mirror / Atom feed
From: Ayush Tiwari <[email protected]>
To: Chao Li <[email protected]>
Cc: PostgreSQL Hackers <[email protected]>
Subject: Re: COPY JSON: use trailing commas in FORCE_ARRAY output
Date: Wed, 6 May 2026 13:12:38 +0530
Message-ID: <CAJTYsWUdfOCo=m6dWpX=uxXFKiTUO751ju3mVJidd9p5VqApFg@mail.gmail.com> (raw)
In-Reply-To: <[email protected]>
References: <[email protected]>

Hi,


On Wed, 6 May 2026 at 12:11, Chao Li <[email protected]> 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.
>

The patch looks good to me.  I applied and the focused 'copy'
regression test too passed.

I agree that since FORMAT json is new for PG19, it is reasonable to adjust
the formatting before release.  The implementation still streams the output
and only changes where the separator/newline are emitted.

Regards,
Ayush


view thread (6+ messages)  latest in thread

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: [email protected]
  Cc: [email protected], [email protected], [email protected]
  Subject: Re: COPY JSON: use trailing commas in FORCE_ARRAY output
  In-Reply-To: <CAJTYsWUdfOCo=m6dWpX=uxXFKiTUO751ju3mVJidd9p5VqApFg@mail.gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox