| From: | Abraham Pérez <abraham1798(at)gmail(dot)com> |
|---|---|
| To: | pgsql-hackers(at)lists(dot)postgresql(dot)org |
| Subject: | Re: JSON_SERIALIZE for JSONB returns parts of the internal JSONB representation |
| Date: | 2026-11-23 13:20:55 |
| Message-ID: | CAF6dGh2OL-8s6ra2fcLKboCJ4RoknhGkCMFRJ-EYgP4u8-L7Zg@mail.gmail.com |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-hackers |
Hi Shihao,
I took v2 for a review and tested it on current master (HEAD
19db7e5b36e, "Fix cross-partition source values in MERGE after a
concurrent update"), configured with --enable-debug --enable-cassert
--enable-tap-tests.
Summary: the fix looks correct and well targeted, the build is clean,
and the full regression suite passes. Details below.
Build and tests
---------------
- Both patches apply cleanly with git am.
- Clean build, no new compiler warnings.
- make check: all 243 tests pass, including the new sqljson cases.
I reproduced the reported problem on an unpatched master and confirmed
the fix resolves it:
-- without the patch
SELECT JSON_SERIALIZE('[1,2,4]'::jsonb);
json_serialize
----------------
\x03
-- with the patch
SELECT JSON_SERIALIZE('[1,2,4]'::jsonb);
json_serialize
----------------
[1, 2, 4]
Code
----
The root-cause analysis in your message matches what I see. For
JSON_SERIALIZE the argument is handed through unchanged
(transformJsonSerializeExpr -> makeJsonConstructorExpr with fexpr ==
NULL), and in that branch the CaseTestExpr placeholder type was taken
from the RETURNING format, which for JSON_SERIALIZE is always JS_FORMAT
_JSON. So coerceJsonFuncExpr() built a json_out()-based coercion that
then executed over raw jsonb bytes. Deriving the placeholder type from
exprType(linitial(args)) for JSCTOR_JSON_SERIALIZE makes the coercion
match the actual input type, which is the right place to fix it.
Leaving typmod/collation at -1/InvalidOid is fine here, since the
argument is always json or jsonb.
To check that 0002 actually guards the fix, I reverted only the
parse_expr.c hunk (keeping the test changes) and re-ran the regression.
The new sqljson cases fail as expected, emitting the internal header
bytes instead of the serialized text:
-- jsonb input
SELECT JSON_SERIALIZE('{"b": [1, 2], "a": "x"}'::jsonb);
- {"a": "x", "b": [1, 2]}
+ \x02
SELECT JSON_SERIALIZE('[1, 2, 4]'::jsonb RETURNING bytea);
- \x5b312c20322c20345d
+ \x03
so the added coverage is meaningful.
A few extra cases I tried, all now behaving as I would expect:
SELECT JSON_SERIALIZE('[1,2,4]'::jsonb RETURNING text); -> [1, 2, 4]
SELECT JSON_SERIALIZE('"abc"'::jsonb), JSON_SERIALIZE('123'::jsonb);
-> "abc", 123
SELECT JSON_SERIALIZE(j) FROM (VALUES ('{"z":1,"a":2}'::jsonb)) v(j);
-> {"a": 2, "z": 1}
SELECT JSON_SERIALIZE('[1,2,4]'::json); ->
[1,2,4] (json unchanged)
SELECT JSON_SERIALIZE('[1,2,4]'::jsonb RETURNING jsonb); -> still
errors as before
The non-constant (table/VALUES) case works too, which makes sense given
the fix is at parse time; and I confirmed the caveat in your commit
message, i.e. a view created before the fix keeps the old wrong output
until it is recreated.
On the backpatch range: I checked that JSON_SERIALIZE was introduced in
PG17 (commit 03734a7fed, "Add more SQL/JSON constructor functions";
it is not present in REL_16), so "Backpatch-through: 17" looks right.
One optional suggestion
-----------------------
The tests cover an object, an array (via bytea), a varchar overflow and
NULL. It might be worth adding a jsonb scalar case as well (e.g.
'"abc"'::jsonb and a numeric scalar), since scalars are a slightly
different shape; it is a one-line addition and not essential.
With or without that, the fix itself looks good to me. Thanks for the
patch!
Best regards,
Abraham
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Keyerror Smart | 2026-11-23 13:25:02 | Re: [PATCH]Fix planner's strictness check for JsonExpr |
| Previous Message | Jeevan Chalke | 2026-11-23 12:53:37 | Re: Add PRODUCT() aggregate function |