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

Started by Matt Blewittabout 1 month ago3 messageshackers
Jump to latest
#1Matt Blewitt
mble@planetscale.com

Hi folks,

Related to:
/messages/by-id/03583FE7-A5EE-407C-907D-67865F3F8BFC@gmail.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

Attachments:

0001-Fix-JSON_SERIALIZE-coercion-placeholder-type-for-jso.patchapplication/octet-stream; name=0001-Fix-JSON_SERIALIZE-coercion-placeholder-type-for-jso.patchDownload+80-4
#2Zsolt Parragi
zsolt.parragi@percona.com
In reply to: Matt Blewitt (#1)
Re: [PATCH] Fix JSON_SERIALIZE() coercion placeholder type for jsonb input

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.

#3Matt Blewitt
mble@planetscale.com
In reply to: Zsolt Parragi (#2)
Re: [PATCH] Fix JSON_SERIALIZE() coercion placeholder type for jsonb input

Hi Zsolt,

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

Matt

On Fri, Mar 13, 2026 at 11:20 PM Zsolt Parragi <zsolt.parragi@percona.com>
wrote:

Show quoted text

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.