BUG #19825: JSON_VALUE(... RETURNING <domain over json/jsonb>) loses the re-quoting of scalar strings and silent
Thread notes
1 note#bug
hackorum-bot#bug
@global #bug
The following bug has been logged on the website:
Bug reference: 19825
Logged by: chunling qin
Email address: 303677365@qq.com
PostgreSQL version: 18.6
Operating system: Linux (TencentOS 4.4, x86_64)
Description:
Hello,
For JSON_VALUE with RETURNING json / jsonb, a scalar *string* result is
re-quoted on output (locked in by the regression suite:
SELECT JSON_VALUE(jsonb '"aaa"', '$' RETURNING jsonb) -> "aaa").
When the RETURNING type is a domain over json / jsonb -- semantically
equivalent, no constraint -- that re-quoting is lost and the value
silently becomes NULL:
CREATE DOMAIN djb AS jsonb;
CREATE DOMAIN djs AS json;
SELECT JSON_VALUE(jsonb '"x"', '$' RETURNING jsonb); -- "x" (correct)
SELECT JSON_VALUE(jsonb '"x"', '$' RETURNING djb); -- NULL (wrong,
want "x")
SELECT JSON_VALUE(jsonb '"x"', '$' RETURNING json); -- "x" (correct)
SELECT JSON_VALUE(jsonb '"x"', '$' RETURNING djs); -- NULL (wrong,
want "x")
SELECT JSON_VALUE(jsonb '"x"', '$' RETURNING djb ERROR ON ERROR);
-- ERROR: invalid input syntax for type json
-- DETAIL: Token "x" is invalid.
Both coercion routes fail: adding a trivially-true CHECK switches the
domain to the use_json_coercion path (json_populate_type()), which
still returns NULL / the same error, because JSON_VALUE always sets
omit_quotes = true (src/backend/parser/parse_expr.c:4694), so
json_populate_type() unquotes first and then tries to parse "x" as
jsonb.
Controls that isolate the defect to string values over domains:
JSON_VALUE(jsonb '1', '$' RETURNING jsonb) = 1 and RETURNING djb = 1
JSON_QUERY(... RETURNING jsonb) and RETURNING djb both -> "x"
non-scalar inputs fail identically on both sides
Expected: a domain over json / jsonb should behave exactly like its
base type here (the domain carries no constraint); i.e. RETURNING djb
should return "x", as the base-type path and the JSON_QUERY path do.
Actual: the same query with the domain as RETURNING type silently
drops the value (NULL under the default NULL ON ERROR), or fails with
an error that points at JSON parsing. Anyone using domains over
json/jsonb to tighten a schema loses the extracted string values.
Root cause pointer: the special-casing in
ExecEvalJsonExprPath() (src/backend/executor/execExpr.c) tests
"typid == JSONOID || typid == JSONBOID" and does not include domains
over those types (compare getBaseType()).
Reproduced on:
- PostgreSQL 18.6 (REL_18_STABLE, self-built, snapshot 2026-10-26)
- PostgreSQL 20devel (snapshot of 2026-10-29)
Regards,
Chunling Qin
Hi Chunling,
Thanks for the report and for tracking down the cause. I can reproduce
it on master, 17, 18 and 19.
The attached patch does what you suggested. ExecInitJsonExpr() now
looks at the RETURNING type's base type, so a domain over json or jsonb
gets the quoted string, same as the base type. For a domain with a
CHECK, json_populate_type() is no longer told to strip the quotes.
The same flag also breaks a jsonb DEFAULT, even with no domain:
SELECT JSON_VALUE(jsonb '{}', '$.a' RETURNING json
DEFAULT jsonb '"123"' ON EMPTY);
gives the number 123 instead of the string "123", and DEFAULT jsonb
'"y"' fails to coerce. The patch fixes that too.
Since the check happens at executor startup, existing views pick up the
fix as well. I checked that on master with a view created by the
unpatched server.
The new tests in sqljson_queryfuncs fail without the fix. make check
passes on master, 17, 18 and 19, and the patch applies to all of them
as is.
Regards,
Ishjaap Singh