From: Amit Langote <amitlan@postgresql.org>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: SQL/JSON: Fix NULL result leakage in JSON_VALUE() RETURNING json
Date: Thu, 05 Nov 2026 07:31:41 +0000
Message-ID: <E1xOrwj-00000000HEe-375i@gemulon.postgresql.org> (raw)
SQL/JSON: Fix NULL result leakage in JSON_VALUE() RETURNING json/jsonb
Once JSON_VALUE(... RETURNING json/jsonb) returned NULL, every later
evaluation of the same expression in the same statement also returned
NULL, even for inputs that had a valid value.
In ExecEvalJsonExprPath(), the JSON_VALUE_OP branch for RETURNING json
or jsonb converts the matched item to its string form but, unlike the
sibling branches, never sets *op->resnull to false. So a NULL result
from a previous evaluation, whether due to a JSON null item, an empty
match, or an error handled by NULL ON ERROR, remained in effect. That
caused the subsequent coercion via the type's input function, which is
guarded by that flag, to be skipped, and the valid item to be returned
as SQL NULL. The result thus depended on what earlier rows, or earlier
expressions sharing the same ExprState, had returned.
Fix by setting *op->resnull to false in that branch. Also add a
comment noting that ExecGetJsonValueItemString() takes care of that
in the final branch.
Oversight in commit 6185c9737cf, so back-patch to 17.
Bug: #19695
Reported-by: Chaitanya Choudhary <chaitanyyachoudhary@gmail.com>
Author: Srinath Reddy Sadipiralla <srinath2133@gmail.com>
Reviewed-by: Manu <manuelreyesbravo@gmail.com>
Discussion: https://postgr.es/m/19695-2f2c573ef1660737@postgresql.org
Backpatch-through: 17
Branch
------
REL_18_STABLE
Details
-------
https://git.postgresql.org/pg/commitdiff/7150d0fd494166107e6efcc7e786690fd76eff08
Modified Files
--------------
src/backend/executor/execExprInterp.c | 2 +
src/test/regress/expected/sqljson_queryfuncs.out | 49 ++++++++++++++++++++++++
src/test/regress/sql/sqljson_queryfuncs.sql | 10 +++++
3 files changed, 61 insertions(+)
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: amitlan@postgresql.org
Subject: Re: pgsql: SQL/JSON: Fix NULL result leakage in JSON_VALUE() RETURNING json
In-Reply-To: <E1xOrwj-00000000HEe-375i@gemulon.postgresql.org>
References: <E1xOrwj-00000000HEe-375i@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