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

From: Yuefei Shi <shiyuefei1004(at)gmail(dot)com>
To: Neil Chen <carpenter(dot)nail(dot)cz(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-01 09:21:47
Message-ID: CAD43U4U=JnqTSZhL=-S4iWpdBtxMNN_L2uqTif9XH85mO-7Bfg@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi Neil,
I read v1 and v2. I agree with the v2 direction. Returning SQL NULL from
to_json()/to_jsonb() for the top-level case is more consistent: it matches
the explicit x::json cast, and also matches how to_json(NULL) already
behaves. Nested constructors still give JSON null, which is what we want
(same as json_build_array(x::json) when the cast gives NULL).
A few things I think should be fixed before this goes further:
1. The top-level cast branch in to_json()/to_jsonb() now skips
check_stack_depth(). Before, both functions went through the
datum_to_*_internal() functions, which call check_stack_depth() at the top.
With v2, when tcategory == JSONTYPE_CAST, the new branch calls the cast
directly and returns, so that check is never reached.
Before:
to_json()
-> datum_to_json()
-> datum_to_json_internal() /* check_stack_depth() here */
-> OidFunctionCall1() /* cast */

to_jsonb()
-> datum_to_jsonb()
-> datum_to_jsonb_internal() /* check_stack_depth() here */
-> OidFunctionCall1() /* cast */
After v2 (only the JSONTYPE_CAST top-level case):
to_json()
-> json_invoke_cast()
-> FunctionCallInvoke() /* cast, no check_stack_depth() */

to_jsonb()
-> json_invoke_cast()
-> FunctionCallInvoke() /* cast, no check_stack_depth() */
So a recursive C cast function that calls to_json()/to_jsonb() again no
longer gets the stack check on this path. The nested paths
(array/composite/agg) still go through datum_to_*_internal, so they are
fine. Please add check_stack_depth() in the new branch or at the function
entry.
2. The v1 commit message says COPY TO (FORMAT json) and FORCE_ARRAY are
fixed, but there is no COPY test. Please add one. It would be good to also
cover json_build_object value position and json_agg/jsonb_agg, since those
go through the same internal functions.
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.

Thanks for working on this. It is a real bug and the fix is not big.

>

--
Yuefei Shi
HighGo Software Co., Ltd.
https://www.highgo.com/

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message ZizhuanLiu X-MAN 2026-12-01 09:27:11 Re: examine_variable ignored CollateExpr
Previous Message shveta malik 2026-12-01 09:19:38 ALTER DEFAULT PRIVILEGES IN SCHEMA pg_conflict silently has no effect