Re: pgsql: Add more SQL/JSON constructor functions - Mailing list pgsql-hackers

From Amit Langote
Subject Re: pgsql: Add more SQL/JSON constructor functions
Date
Msg-id CA+HiwqE+y_HmGJpKk2_w+CUzmAw=Z5zd6j3_LmCPiGpFyN1gzQ@mail.gmail.com
Whole thread Raw
In response to Re: pgsql: Add more SQL/JSON constructor functions  (Alvaro Herrera <alvherre@alvh.no-ip.org>)
Responses Re: pgsql: Add more SQL/JSON constructor functions
List pgsql-hackers
Hi Alvaro,

On Mon, May 27, 2024 at 7:10 PM Alvaro Herrera <alvherre@alvh.no-ip.org> wrote:
>
> On 2024-May-27, Alvaro Herrera wrote:
>
> > > JSON_SERIALIZE()
>
> I just noticed this behavior, which looks like a bug to me:
>
> select json_serialize('{"a":1, "a":2}' returning varchar(5));
>  json_serialize
> ────────────────
>  {"a":
>
> I think this function should throw an error if the destination type
> doesn't have room for the output json.  Otherwise, what good is the
> serialization function?

I remember using the reasoning mentioned by David G when testing
json_query() et al with varchar(n), so you get:

select json_query('{"a":1, "a":2}', '$' returning varchar(5));
 json_query
------------
 {"a":
(1 row)

which is the same as:

select '{"a":1, "a":2}'::varchar(5);
 varchar
---------
 {"a":
(1 row)

Also,

select json_value('{"a":"abcdef"}', '$.a' returning varchar(5) error on error);
 json_value
------------
 abcde
(1 row)

This behavior comes from using COERCE_EXPLICIT_CAST when creating the
coercion expression to convert json_*() functions' argument to the
RETURNING type.

--
Thanks, Amit Langote



pgsql-hackers by date:

Previous
From: Hannu Krosing
Date:
Subject: Re: DROP OWNED BY fails to clean out pg_init_privs grants
Next
From: Ashutosh Bapat
Date:
Subject: Re: Reducing memory consumed by RestrictInfo list translations in partitionwise join planning