From a757c4e3a4f171ab1c4c2391b353097d5c75e875 Mon Sep 17 00:00:00 2001 From: Nathan Bossart Date: Wed, 2 Dec 2026 16:35:37 -0600 Subject: [PATCH v4 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 | 12 +++++++++--- src/test/regress/expected/sqljson.out | 6 ++++++ src/test/regress/sql/sqljson.sql | 1 + 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/backend/parser/parse_expr.c b/src/backend/parser/parse_expr.c index e207b7a1978..d8c080bcb7f 100644 --- a/src/backend/parser/parse_expr.c +++ b/src/backend/parser/parse_expr.c @@ -3692,7 +3692,10 @@ makeJsonConstructorExpr(ParseState *pstate, JsonConstructorType type, * Coerce to the RETURNING type and format, if needed. We abuse * CaseTestExpr here as placeholder to pass the result of either * evaluating 'fexpr' or whatever is produced by ExecEvalJsonConstructor() - * that is of type JSON or JSONB to the coercion function. + * that is of type JSON or JSONB to the coercion function. Note that for + * JSON_SERIALIZE(), the argument is passed to the coercion function as + * is, so we instead use the argument's type, which is either json or + * jsonb. */ if (fexpr) { @@ -3708,8 +3711,11 @@ makeJsonConstructorExpr(ParseState *pstate, JsonConstructorType type, { CaseTestExpr *cte = makeNode(CaseTestExpr); - cte->typeId = returning->format->format_type == JS_FORMAT_JSONB ? - JSONBOID : JSONOID; + 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