Re: Copy from JSON FORMAT.

From: jian he <jian(dot)universality(at)gmail(dot)com>
To: Junwang Zhao <zhjwpku(at)gmail(dot)com>
Cc: PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: Copy from JSON FORMAT.
Date: 2026-03-22 12:58:55
Message-ID: CACJufxEH-x2DU=rzaBf=3pi0SLW8ZONwtxsp7-vWPALB1T_kDg@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Sun, Mar 22, 2026 at 4:59 PM Junwang Zhao <zhjwpku(at)gmail(dot)com> wrote:
>
> Hi hackers,
>
> 7dadd38cda introduced support for COPY TO in JSON format, but did
> not include the corresponding COPY FROM functionality. The original
> discussion [1] mentioned the possibility of supporting COPY FROM as
> well, but that part of the patch never happened (If I don't miss).
>
> For a data format like JSON, basic round-trip capability seems essential:
> if data can be exported via COPY TO, it should also be possible to import
> it back using COPY FROM. So I try to close that gap.
>
Hi.

repalloc can be replaced by repalloc_array.
maybe palloc, palloc0 can aslo be replaced by palloc_object/palloc0_object.

``foreach(cur, cstate->attnumlist)``
can be replaced by
``foreach_int(cur, cstate->attnumlist)``

+ ereport(ERROR,
+ (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
+ errmsg("invalid input format for COPY JSON"),
+ errdetail("Document must begin with \"[\" or \"{\".")));
+ pg_unreachable();
The parentheses in `` (errcode`` is not necessary, (and other palces)
See https://www.postgresql.org/message-id/202510100916.s2e6n3xiwvyc%40alvherre.pgsql

Should the JSON object match the table column?
create table copytest_from_json(style int, test text);
copy copytest_from_json (style, test) from stdin (format json);

The below 3 valid json should all fail?
{"style": "1", "test": 1, "x": 1}
{"style": "1"}
{"x": "1"}

I am not sure the handling of empty strings is correct.
src9=# copy copytest_from_json (style, test) from stdin (format json);
Enter data to be copied followed by a newline.
End with a backslash and a period on a line by itself, or an EOF signal.
>>
>> \.
COPY 0

src9=# copy copytest_from_json (style, test) from stdin (format text);
Enter data to be copied followed by a newline.
End with a backslash and a period on a line by itself, or an EOF signal.
>>
>> \.
ERROR: invalid input syntax for type integer: ""
CONTEXT: COPY copytest_from_json, line 1, column style: ""

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

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message 段坤仁 (刻韧) 2026-03-22 13:09:05 回复:Bug in MultiXact replay compat logic for older minor version after crash-recovery
Previous Message Heikki Linnakangas 2026-03-22 12:39:58 Re: Avoid use of TopMemoryContext for resource owner cleanup in portals