Re: [PATCH] COPY TO FORMAT json: respect column list order - Mailing list pgsql-hackers

From Andrew Dunstan
Subject Re: [PATCH] COPY TO FORMAT json: respect column list order
Date
Msg-id 2e5d0d7a-c707-4183-9da6-2658d039819b@dunslane.net
Whole thread
In response to [PATCH] COPY TO FORMAT json: respect column list order  (Baji Shaik <baji.pgdev@gmail.com>)
Responses Re: [PATCH] COPY TO FORMAT json: respect column list order
List pgsql-hackers
On 2026-06-12 Fr 6:50 PM, Baji Shaik wrote:
> Hi,
>
> While testing the new COPY JSON format, I noticed the column list
> ordering is ignored when the list mentions every column of the
> relation.  CSV and TEXT formats respect the order; JSON does not.
>
>   CREATE TABLE t (a int, b int);
>   INSERT INTO t VALUES (1, 2);
>
>   COPY t (b, a) TO STDOUT WITH (FORMAT csv);   -- "2,1"
>   COPY t (b, a) TO STDOUT WITH (FORMAT json);  -- {"a":1,"b":2}
>                                                -- expected: {"b":2,"a":1}
>
> In BeginCopyTo() (commit 7dadd38cda9), the JSON path builds a custom
> TupleDesc only when the column list is shorter than the relation:
>
>   if (rel && list_length(cstate->attnumlist) < tupDesc->natts)
>
> When the user provides a full column list reordered, list_length
> equals natts, so the natural TupleDesc is reused.
>
> Fix is to extend the condition to also fire when an explicit column list
> was supplied (attnamelist != NIL).
>
> All 245 regression tests pass.
>
>


This seems to be a harmless enough change, and if we're going to do it 
we should do it now.

OTOH, any application that relies on a particular field ordering in JSON 
data is broken by design, IMNSHO.


cheers


andrew

--
Andrew Dunstan
EDB: https://www.enterprisedb.com




pgsql-hackers by date:

Previous
From: Ed Behn
Date:
Subject: Re: access numeric data in module
Next
From: Sami Imseih
Date:
Subject: Re: [PATCH] COPY TO FORMAT json: respect column list order