postgres.git / summary / log / commit / refs

commit    7150d0fd494166107e6efcc7e786690fd76eff08
Author:   Amit Langote <amitlan@postgresql.org>
Date:     Thu Nov 05 03:24:43 2026 +0000
First released in: 17.12, 18.7, 19.0

    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


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(+) diff --git a/src/backend/executor/execExprInterp.c b/src/backend/executor/execExprInterp.c index cdb4df8f366..5d10f439ea2 100644 --- a/src/backend/executor/execExprInterp.c +++ b/src/backend/executor/execExprInterp.c @@ -5000,6 +5000,7 @@ ExecEvalJsonExprPath(ExprState *state, ExprEvalStep *op, { val_string = DatumGetCString(DirectFunctionCall1(jsonb_out, JsonbPGetDatum(JsonbValueToJsonb(jbv)))); + *op->resnull = false; } else if (jsexpr->use_json_coercion) { @@ -5008,6 +5009,7 @@ ExecEvalJsonExprPath(ExprState *state, ExprEvalStep *op, } else { + /* ExecGetJsonValueItemString() sets *op->resnull. */ val_string = ExecGetJsonValueItemString(jbv, op->resnull); /* diff --git a/src/test/regress/expected/sqljson_queryfuncs.out b/src/test/regress/expected/sqljson_queryfuncs.out index 4f2056847dc..3ef4dc48286 100644 --- a/src/test/regress/expected/sqljson_queryfuncs.out +++ b/src/test/regress/expected/sqljson_queryfuncs.out @@ -1663,3 +1663,52 @@ SELECT JSON_VALUE(jsonb '1234', '$' RETURNING bit(3) DEFAULT 1::bit(3) ON ERROR SELECT JSON_VALUE(jsonb '"111"', '$.a' RETURNING bit(3) DEFAULT '1111' ON EMPTY); ERROR: bit string length 4 does not match type bit(3) DROP DOMAIN queryfuncs_d_varbit3; +-- Check that a NULL result from a previous evaluation of JSON_VALUE() +-- RETURNING json or jsonb doesn't leak into the next one (bug #19695). +SELECT JSON_VALUE('null', '$' RETURNING jsonb), JSON_VALUE('123', '$' RETURNING jsonb); + json_value | json_value +------------+------------ + | 123 +(1 row) + +SELECT JSON_VALUE(x, '$' RETURNING jsonb) FROM (VALUES ('1'::jsonb), ('null'), ('2')) v(x); + json_value +------------ + 1 + + 2 +(3 rows) + +SELECT JSON_VALUE(x, '$' RETURNING json) FROM (VALUES ('1'::json), ('null'), ('2')) v(x); + json_value +------------ + 1 + + 2 +(3 rows) + +-- same, with the NULL coming from ON EMPTY, ON ERROR, and a NULL input +SELECT JSON_VALUE(x, '$.a' RETURNING jsonb) FROM (VALUES ('{"a": 1}'::jsonb), ('{}'), ('{"a": 2}')) v(x); + json_value +------------ + 1 + + 2 +(3 rows) + +SELECT JSON_VALUE(x, 'strict $.a' RETURNING jsonb) FROM (VALUES ('{"a": 1}'::jsonb), ('[]'), ('{"a": 2}')) v(x); + json_value +------------ + 1 + + 2 +(3 rows) + +SELECT JSON_VALUE(x, '$' RETURNING jsonb) FROM (VALUES ('1'::jsonb), (NULL), ('2')) v(x); + json_value +------------ + 1 + + 2 +(3 rows) + diff --git a/src/test/regress/sql/sqljson_queryfuncs.sql b/src/test/regress/sql/sqljson_queryfuncs.sql index b43308a144f..819ff3da218 100644 --- a/src/test/regress/sql/sqljson_queryfuncs.sql +++ b/src/test/regress/sql/sqljson_queryfuncs.sql @@ -581,3 +581,13 @@ SELECT JSON_VALUE(jsonb '1234', '$' RETURNING bit(3) DEFAULT 1 ON ERROR); SELECT JSON_VALUE(jsonb '1234', '$' RETURNING bit(3) DEFAULT 1::bit(3) ON ERROR); SELECT JSON_VALUE(jsonb '"111"', '$.a' RETURNING bit(3) DEFAULT '1111' ON EMPTY); DROP DOMAIN queryfuncs_d_varbit3; + +-- Check that a NULL result from a previous evaluation of JSON_VALUE() +-- RETURNING json or jsonb doesn't leak into the next one (bug #19695). +SELECT JSON_VALUE('null', '$' RETURNING jsonb), JSON_VALUE('123', '$' RETURNING jsonb); +SELECT JSON_VALUE(x, '$' RETURNING jsonb) FROM (VALUES ('1'::jsonb), ('null'), ('2')) v(x); +SELECT JSON_VALUE(x, '$' RETURNING json) FROM (VALUES ('1'::json), ('null'), ('2')) v(x); +-- same, with the NULL coming from ON EMPTY, ON ERROR, and a NULL input +SELECT JSON_VALUE(x, '$.a' RETURNING jsonb) FROM (VALUES ('{"a": 1}'::jsonb), ('{}'), ('{"a": 2}')) v(x); +SELECT JSON_VALUE(x, 'strict $.a' RETURNING jsonb) FROM (VALUES ('{"a": 1}'::jsonb), ('[]'), ('{"a": 2}')) v(x); +SELECT JSON_VALUE(x, '$' RETURNING jsonb) FROM (VALUES ('1'::jsonb), (NULL), ('2')) v(x); [parent: fbaebf450d44]