Re: Emitting JSON to file using COPY TO

From: jian he <jian(dot)universality(at)gmail(dot)com>
To: Florents Tselai <florents(dot)tselai(at)gmail(dot)com>
Cc: Junwang Zhao <zhjwpku(at)gmail(dot)com>, Joe Conway <mail(at)joeconway(dot)com>, "Andrey M(dot) Borodin" <x4mmm(at)yandex-team(dot)ru>, Dean Rasheed <dean(dot)a(dot)rasheed(at)gmail(dot)com>, Daniel Verite <daniel(at)manitou-mail(dot)org>, Andrew Dunstan <andrew(at)dunslane(dot)net>, Davin Shearer <davin(at)apache(dot)org>, PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Emitting JSON to file using COPY TO
Date: 2026-02-06 03:26:04
Message-ID: CACJufxEjZwrocCpt29xtmJTwhWZUu1Nt0GfHFarNGXUS5AAecw@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-hackers

On Wed, Feb 4, 2026 at 12:41 AM Florents Tselai
<florents(dot)tselai(at)gmail(dot)com> wrote:
>
> I (and others I assume) would really like to see this in 19;
> glancing at the thread above and in the test cases I see this is in good shape for comitter review.
> No?
>
> If I were to add something it would be an example in copy.sgml
> <para>
> When the <literal>FORCE_ARRAY</literal> option is enabled,
> the entire output is wrapped in a JSON array and individual rows are separated by commas:
> <programlisting>
> COPY (SELECT id, name FROM users) TO STDOUT (FORMAT JSON, FORCE_ARRAY);
> </programlisting>
> <programlisting>
> [
> {"id": 1, "name": "Alice"}
> ,{"id": 2, "name": "Bob"}
> ,{"id": 3, "name": "Charlie"}
> ]
> </programlisting>
> </para>
>

v23-0003-Add-option-force_array-for-COPY-JSON-FORMAT.patch
I've added:

+<para>
+ When the <literal>FORCE_ARRAY</literal> option is enabled,
+ the entire output is wrapped in a single JSON array with rows
separated by commas:
+<programlisting>
+COPY (SELECT * FROM (VALUES(1),(2)) val(id)) TO STDOUT (FORMAT JSON,
FORCE_ARRAY);
+</programlisting>
+The output is as follows:
+<screen>
+[
+ {"id":1}
+,{"id":2}
+]
+</screen>
+</para>
+
+

> Also, apologies if that has been discussed already,
> is there a good reason why didn't we just go with a simple "WRAP_ARRAY" ?
>

I don’t have a particular preference.
If the consensus is that WRAP_ARRAY is better than FORCE_ARRAY, we can
change it accordingly.

--
jian
https://www.enterprisedb.com/

Attachment Content-Type Size
v23-0002-json-format-for-COPY-TO.patch text/x-patch 21.9 KB
v23-0003-Add-option-force_array-for-COPY-JSON-FORMAT.patch text/x-patch 10.3 KB
v23-0001-introduce-CopyFormat-refactor-CopyFormatOptions.patch text/x-patch 13.1 KB

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Junwang Zhao 2026-02-06 06:19:11 Re: Emitting JSON to file using COPY TO
Previous Message Sophie Alpert 2026-02-05 19:16:29 Re: Is this expected concurrency behaviour for EvalPlanQual and ctid?

Browse pgsql-hackers by date

  From Date Subject
Next Message jian he 2026-02-06 03:40:02 Re: Change COPY ... ON_ERROR ignore to ON_ERROR ignore_row
Previous Message Ashutosh Bapat 2026-02-06 03:01:16 Re: relkind as an enum