Re: [PATCH] Handle SQL NULL results from casts in JSON generation

From: Neil Chen <carpenter(dot)nail(dot)cz(at)gmail(dot)com>
To: Yuefei Shi <shiyuefei1004(at)gmail(dot)com>
Cc: Postgres hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: [PATCH] Handle SQL NULL results from casts in JSON generation
Date: 2026-12-02 06:47:29
Message-ID: CAA3qoJ=_-KrpxAfymQ=kCT=AgkmdJ74+18WOLMAHiaQpT4_M2A@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi Yuefei,

Thanks for taking a look.

On Tue, Dec 1, 2026 at 5:22 PM Yuefei Shi <shiyuefei1004(at)gmail(dot)com> wrote:

>
> Please add check_stack_depth() in the new branch or at the function entry.
>

Good catch. The attached v3 puts the check back before the cast in
both to_json() and to_jsonb().

> 2. The v1 commit message says COPY TO (FORMAT json) and FORCE_ARRAY are
> fixed, but there is no COPY test. Please add one.
>

I tried COPY with FORCE_ARRAY on two rows. Without the patch, it errors
on the second row and leaves the array open. With v3, the second row is
{"flag":null} and the array closes. I'd keep this as a manual check:
the array constructor tests already hit the same cast code, and
copy.sql tests FORCE_ARRAY separately. I left the object and aggregate
cases out for the same reason.

> Minor comments:
> - In to_json(), you PG_RETURN_DATUM(val) without DatumGetTextPP. It is
> probably fine because the output function detoasts, but it is a small
> difference from the old path. If you want to be safe, detoast before return.
> - Since this changes behavior (error -> NULL/JSON null), please add a
> sentence in the functions-json docs and in the commit message.
>
>
I left detoasting to json_out(), as you noted. I've added the COPY case
to the commit message but left the manual unchanged for now.

>
Thanks,
--
Ze Chen (Neil)
HighGo Software Co., Ltd.
https://www.highgo.com/

Attachment Content-Type Size
v3-0001-Handle-SQL-NULL-results-from-casts-in-JSON-generation.patch text/x-patch 10.5 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Chao Li 2026-12-02 06:48:00 Re: [PATCH] pg_upgrade: add --initdb option to create the new cluster automatically
Previous Message Tom Lane 2026-12-02 06:47:23 Re: Missing CFI() in SplitIdentifierString