Re: BUG #19418: SQL/JSON JSON_VALUE() does not conform to ISO/IEC 9075-2:2023(E) 6.34 <JSON value constructor>

Richard Guo <guofenglinux@gmail.com>

From: Richard Guo <guofenglinux@gmail.com>
To: Ayush Tiwari <ayushtiwari.slg01@gmail.com>
Cc: Melanie Plageman <melanieplageman@gmail.com>, Nathan Bossart <nathandbossart@gmail.com>, Tom Lane <tgl@sss.pgh.pa.us>, Amit Langote <amitlangote09@gmail.com>, Vik Fearing <vik@postgresfriends.org>, lukas.eder@gmail.com, pgsql-bugs@lists.postgresql.org, rmt@lists.postgresql.org, Álvaro Herrera <alvherre@kurilemu.de>, Heikki Linnakangas <hlinnaka@iki.fi>
Date: 2026-05-08T08:35:27Z
Lists: pgsql-bugs
On Thu, May 7, 2026 at 11:45 PM Ayush Tiwari
<ayushtiwari.slg01@gmail.com> wrote:
> The non-empty query form enforces the RETURNING typmod:
>
>   SELECT JSON_ARRAY(SELECT 1 RETURNING varchar(1));
>   ERROR:  value too long for type character varying(1)
>
> but the empty query form returns a value that does not fit the declared type:
>
>   SELECT JSON_ARRAY(SELECT 1 WHERE false RETURNING varchar(1));
>    json_array
>   ------------
>    []
>   (1 row)

Nice catch.  The empty-array Const was built with typmod -1, and the
type input function was invoked with typmod -1, so any RETURNING
length restriction was silently bypassed.

I've pushed a fix that builds Const and calls the input function with
the typmod of the non-empty COALESCE argument.

- Richard



Commits

  1. Enforce RETURNING typmod for empty-set JSON_ARRAY(query)

  2. Fix JSON_ARRAY(query) empty set handling and view deparsing