[PATCH] COPY TO FORMAT json: respect column list order

From: Baji Shaik <baji(dot)pgdev(at)gmail(dot)com>
To: PostgreSQL-development <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: [PATCH] COPY TO FORMAT json: respect column list order
Date: 2026-06-12 22:50:18
Message-ID: CA+fm-RPOGQEJhoJP4xRtjfDXPBT8P-ttG2-Ey2Ak30QiESdU_Q@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

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.

Thanks,
Baji Shaik.

Attachment Content-Type Size
0001-COPY-TO-FORMAT-JSON-respect-column-list-order.patch application/octet-stream 3.7 KB

Browse pgsql-hackers by date

  From Date Subject
Previous Message Andrew Dunstan 2026-06-12 22:39:38 Re: BackgroundPsql swallowing errors on windows