pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Andrew Dunstan <andrew@dunslane.net>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: COPY TO FORMAT JSON: use the query's column names as keys
Date: Thu, 08 Oct 2026 17:41:25 +0000
Message-ID: <E1xEs7R-00000000qEN-1j9f@gemulon.postgresql.org> (raw)

COPY TO FORMAT JSON: use the query's column names as keys

CopyToJsonOneRow() stamped the row datum with the query's descriptor
only when the slot's descriptor was of type RECORD.  A scan that does
not project returns the table's row type, so the keys came from the
table, not the query, losing column aliases and mislabelling rows
from UNION ALL branches with different column orders.

Fix by always using the query's descriptor.  For non-virtual slots,
heap_copy_tuple_as_datum() can just be passed that descriptor, so
there is no extra cost.  This is safe because a scan skips projection
only when tlist_matches_tupdesc() holds, so the layouts match.

Bug introduced in 7dadd38cda9.

Reported-by: Shihao Zhong <zhong950419@gmail.com>
Reviewed-by: Shihao Zhong <zhong950419@gmail.com>
Reviewed-by: João Marcelo Detomini <joao.detomini@enterprisedb.com>
Discussion: https://postgr.es/m/CAGRkXqQHjxgufHPz86+dxGaQZt37hikduUArYtfbn2_3mryZAw@mail.gmail.com
Backpatch-through: 19

Branch
------
REL_19_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/05b522024838e19f7b9f0fe54d72b29bc588b230

Modified Files
--------------
src/backend/commands/copyto.c      | 40 ++++++++++++++++++++++++++------------
src/test/regress/expected/copy.out | 13 +++++++++++++
src/test/regress/sql/copy.sql      | 10 ++++++++++
3 files changed, 51 insertions(+), 12 deletions(-)



view thread (2+ messages)

Message-ID: <E1xEs7R-00000000qEN-1j9f@gemulon.postgresql.org>
Permalink:  ../E1xEs7R-00000000qEN-1j9f@gemulon.postgresql.org/
Also on:    postgresql.org/message-id/E1xEs7R-00000000qEN-1j9f@gemulon.postgresql.org

 · 

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 from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-committers@postgresql.org
  Cc: andrew@dunslane.net
  Subject: Re: pgsql: COPY TO FORMAT JSON: use the query's column names as keys
  In-Reply-To: <E1xEs7R-00000000qEN-1j9f@gemulon.postgresql.org>
  References: <E1xEs7R-00000000qEN-1j9f@gemulon.postgresql.org>

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

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