Fix COPY TO FORMAT JSON to exclude generated columns.

COPY TO with FORMAT json was including generated columns in the
output, unlike TEXT and CSV formats.  Virtual generated columns
appeared as null, and stored ones showed their computed values.

The JSON code path only built a restricted TupleDesc when an explicit
column list was given (attnamelist != NIL), but CopyGetAttnums()
also excludes generated columns from the default list.  Fix by
checking whether the attnumlist is shorter than the full TupleDesc
instead.

Bug introduced in 7dadd38cda.

Author: Satya Narlapuram <satya.narlapuram@gmail.com>
Reviewed-by: Jian He <jian.universality@gmail.com>
Discussion: https://postgr.es/m/CAHg+QDcfpGDoPL3fvfjXRtfn=fny6DdJR6BAy6TpS1Xj2EZfXA@mail.gmail.com
This commit is contained in:
Andrew Dunstan 2026-04-15 07:47:12 -04:00
parent 3e2a1496ba
commit f30d0c720f
5 changed files with 19 additions and 1 deletions

View file

@ -1033,7 +1033,7 @@ BeginCopyTo(ParseState *pstate,
{
cstate->json_buf = makeStringInfo();
if (attnamelist != NIL && rel)
if (rel && list_length(cstate->attnumlist) < tupDesc->natts)
{
int natts = list_length(cstate->attnumlist);
TupleDesc resultDesc;

View file

@ -541,6 +541,12 @@ SELECT * FROM gtest3 ORDER BY a;
4 | 12
(4 rows)
-- COPY JSON should exclude generated columns, same as text/CSV
COPY gtest1 TO stdout WITH (FORMAT json);
{"a":1}
{"a":2}
{"a":3}
{"a":4}
-- null values
CREATE TABLE gtest2 (a int PRIMARY KEY, b int GENERATED ALWAYS AS (NULL) STORED);
INSERT INTO gtest2 VALUES (1);

View file

@ -535,6 +535,12 @@ SELECT * FROM gtest3 ORDER BY a;
4 | 12
(4 rows)
-- COPY JSON should exclude generated columns, same as text/CSV
COPY gtest1 TO stdout WITH (FORMAT json);
{"a":1}
{"a":2}
{"a":3}
{"a":4}
-- null values
CREATE TABLE gtest2 (a int PRIMARY KEY, b int GENERATED ALWAYS AS (NULL) VIRTUAL);
INSERT INTO gtest2 VALUES (1);

View file

@ -239,6 +239,9 @@ COPY gtest3 (a, b) FROM stdin;
SELECT * FROM gtest3 ORDER BY a;
-- COPY JSON should exclude generated columns, same as text/CSV
COPY gtest1 TO stdout WITH (FORMAT json);
-- null values
CREATE TABLE gtest2 (a int PRIMARY KEY, b int GENERATED ALWAYS AS (NULL) STORED);
INSERT INTO gtest2 VALUES (1);

View file

@ -239,6 +239,9 @@ COPY gtest3 (a, b) FROM stdin;
SELECT * FROM gtest3 ORDER BY a;
-- COPY JSON should exclude generated columns, same as text/CSV
COPY gtest1 TO stdout WITH (FORMAT json);
-- null values
CREATE TABLE gtest2 (a int PRIMARY KEY, b int GENERATED ALWAYS AS (NULL) VIRTUAL);
INSERT INTO gtest2 VALUES (1);