Subject: [PATCH] Fix JSON_SERIALIZE() coercion placeholder type for jsonb input

From: Matt Blewitt <[email-masked]>
Date: Fri, 13 Mar 2026 11:02:01 +0000

Hi folks,

Related to:
https://www.postgresql.org/message-id/03583FE7-A5EE-407C-907D-67865F3F8BFC%40gmail.com

When JSON_SERIALIZE() receives a jsonb-typed argument, the CaseTestExpr
placeholder used to set up coercion was unconditionally assigned JSONOID
(derived from the RETURNING format, which defaults to JS_FORMAT_JSON).
However, the executor passes the input argument value through directly
for JSON_SERIALIZE (see ExecInitExprRec in execExpr.c), so the actual
datum at runtime is jsonb, not json.  This type mismatch between the
placeholder and the runtime value caused the wrong coercion path to be
selected.

As Dirkjan mentioned, this results in confusing output.

Attached is a patch to address this, by deriving the placeholder type from
the
actual argument type via exprType(linitial(args)) when the constructor type
is
JSCTOR_JSON_SERIALIZE, rather than from returning->format->format_type.

Added a regression test to ensure JSONB is serialized correctly.

Cheers,

Matt

---

From: Zsolt Parragi <[email-masked]>
Date: Fri, 13 Mar 2026 23:20:15 +0000

Hello!

This is a simple fix and it does what it says, it looks good to me.

I did test it with a few more queries and compared it against master,
all looks good.

---

From: Matt Blewitt <[email-masked]>
Date: Mon, 16 Mar 2026 18:48:41 +0000

Hi Zsolt,

Thanks for testing that out and confirming it looks good against master.
Submitted to commitfest for further review and integration consideration.

Matt