From 769207ca46793923a0235d3184939a1507d16fb4 Mon Sep 17 00:00:00 2001 From: Nathan Bossart Date: Wed, 2 Dec 2026 15:39:53 -0600 Subject: [PATCH v3 1/1] Fix JSON_SERIALIZE() for jsonb input. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit JSON_SERIALIZE() converts its json or jsonb argument to a string, but the parser always sets up that conversion as if the argument is json. This means that for a jsonb argument, the function returns a few bytes of the value's internal representation. To fix, set up the conversion for the argument's actual type instead. Note that existing views and other stored expressions that call JSON_SERIALIZE() with a jsonb argument will need to be recreated. Oversight in commit 03734a7fed. Reported-by: Dirkjan Bussink Author: Shihao Zhong Reviewed-by: Abraham Pérez Discussion: https://postgr.es/m/03583FE7-A5EE-407C-907D-67865F3F8BFC%40gmail.com Discussion: https://postgr.es/m/CAF6dGh2OL-8s6ra2fcLKboCJ4RoknhGkCMFRJ-EYgP4u8-L7Zg%40mail.gmail.com Backpatch-through: 17 --- src/backend/parser/parse_expr.c | 13 +++++++++++-- src/test/regress/expected/sqljson.out | 6 ++++++ src/test/regress/sql/sqljson.sql | 1 + 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/backend/parser/parse_expr.c b/src/backend/parser/parse_expr.c index e207b7a1978..84e906fa43d 100644 --- a/src/backend/parser/parse_expr.c +++ b/src/backend/parser/parse_expr.c @@ -3708,8 +3708,17 @@ makeJsonConstructorExpr(ParseState *pstate, JsonConstructorType type, { CaseTestExpr *cte = makeNode(CaseTestExpr); - cte->typeId = returning->format->format_type == JS_FORMAT_JSONB ? - JSONBOID : JSONOID; + /* + * The placeholder must have the type of the value that will be + * coerced. JSON_SERIALIZE() coerces its argument, which is either + * json or jsonb. The other constructors coerce a new value that the + * executor builds as json or jsonb according to the RETURNING format. + */ + if (type == JSCTOR_JSON_SERIALIZE) + cte->typeId = exprType(linitial(args)); + else + cte->typeId = returning->format->format_type == JS_FORMAT_JSONB ? + JSONBOID : JSONOID; cte->typeMod = -1; cte->collation = InvalidOid; diff --git a/src/test/regress/expected/sqljson.out b/src/test/regress/expected/sqljson.out index 1085475541b..7cacda8e06c 100644 --- a/src/test/regress/expected/sqljson.out +++ b/src/test/regress/expected/sqljson.out @@ -234,6 +234,12 @@ SELECT JSON_SERIALIZE(JSON('{ "a" : 1 } ')); { "a" : 1 } (1 row) +SELECT JSON_SERIALIZE('{ "a" : 1 } '::jsonb); + json_serialize +---------------- + {"a": 1} +(1 row) + SELECT JSON_SERIALIZE('{ "a" : 1 } '); json_serialize ---------------- diff --git a/src/test/regress/sql/sqljson.sql b/src/test/regress/sql/sqljson.sql index 43106fdda96..cbd7bee0e82 100644 --- a/src/test/regress/sql/sqljson.sql +++ b/src/test/regress/sql/sqljson.sql @@ -48,6 +48,7 @@ EXPLAIN (VERBOSE, COSTS OFF) SELECT JSON_SCALAR('123'); SELECT JSON_SERIALIZE(); SELECT JSON_SERIALIZE(NULL); SELECT JSON_SERIALIZE(JSON('{ "a" : 1 } ')); +SELECT JSON_SERIALIZE('{ "a" : 1 } '::jsonb); SELECT JSON_SERIALIZE('{ "a" : 1 } '); SELECT JSON_SERIALIZE('1'); SELECT JSON_SERIALIZE('1' FORMAT JSON); -- 2.55.0