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

From David G. Johnston
Subject Re: pgsql: Add more SQL/JSON constructor functions
Date
Msg-id CAKFQuwYFaXRcJLb5CkcFHYrwqMCQX2+zPJTQUN1nodHb0DriJw@mail.gmail.com
Whole thread Raw
In response to Re: pgsql: Add more SQL/JSON constructor functions  (Alvaro Herrera <alvherre@alvh.no-ip.org>)
List pgsql-hackers
On Monday, May 27, 2024, 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?


It’s not a self-evident bug given that this is exactly how casting data to varchar(n) behaves as directed by the SQL Standard.

I'd probably leave the internal consistency and take the opportunity to educate the reader that text is the preferred type in PostgreSQL and, especially here, there is little good reason to use anything else.

David J.

pgsql-hackers by date:

Previous
From: "Long Song"
Date:
Subject: Re:[PATCH]A minor improvement to the error-report in SimpleLruWriteAll()
Next
From: James Coleman
Date:
Subject: Re: commitfest.postgresql.org is no longer fit for purpose