Re: BUG #19851: Adding a tautological CHECK(true) to a domain changes JSON_VALUE's result (silent NULL, value/type c

From: Amit Langote <amitlangote09(at)gmail(dot)com>
To: Ishjaap Singh <ishjaap(dot)singh07(at)gmail(dot)com>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, 303677365(at)qq(dot)com, pgsql-bugs(at)lists(dot)postgresql(dot)org, e1638616(at)u(dot)nus(dot)edu
Subject: Re: BUG #19851: Adding a tautological CHECK(true) to a domain changes JSON_VALUE's result (silent NULL, value/type c
Date: 2026-12-04 04:48:49
Message-ID: CA+HiwqEYqgU74+ykZHY0Z8RtcvicStK5UG=1QkcOiy=525sbbQ@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

Hi Ishjaap,

On Thu, Dec 3, 2026 at 10:50 PM Ishjaap Singh <ishjaap(dot)singh07(at)gmail(dot)com> wrote:
> BUG #19971 [1] came in yesterday and is close to this one. I checked
> it against v1, and it still reproduces:

Thanks for trying v1 and for the pointers to #19825 and #19971. So
many bugs (or reports) :-(

> SELECT JSON_VALUE(jsonb '{}', '$.a' RETURNING json
> DEFAULT '"123"'::jsonb ON EMPTY);
>
> It returns the number 123 instead of the string "123", and a '"abc"'
> DEFAULT fails. ON ERROR and a domain over jsonb behave the same way.

Right, v1 doesn't fix that, or perhaps I didn't think to fix that too.
Though maybe it's better to fix that, so the attached v2 does. For
JSON_VALUE(), coercing the ON EMPTY or ON ERROR DEFAULT value no
longer omits quotes when the RETURNING type is json or jsonb, or a
domain over one of them. Other RETURNING types still get the DEFAULT
without quotes.

I decided to leave JSON_QUERY() alone though. With OMIT QUOTES it has
a similar inconsistency, that is, a jsonb string DEFAULT keeps its
quotes with RETURNING jsonb, where no coercion is needed, but loses
them with RETURNING json or a domain over jsonb. Whether OMIT QUOTES
should apply to the DEFAULT at all seems like a separate question,
since the user asked for it there. We'll probably need to look into
that separately.

> The ON EMPTY and ON ERROR coercions still get omit_quotes, which is
> always true for JSON_VALUE(). With v1 that flag no longer affects the
> path result, only the DEFAULT. The path result keeps its quotes when
> the RETURNING base type is json or jsonb, so I think the DEFAULT
> should as well.
>
> My #19825 patch [2] fixes the same path-result case with a narrower
> change, and also this DEFAULT case. Yours fixes the cause, so I'll
> withdraw mine from the commitfest. Its DEFAULT tests can be reused if
> they help.

I added tests for the cases in #19971, including ON ERROR and domains
over json and jsonb, and listed you as a reviewer. I also ran the
tests from your #19825 patch against v2 and they pass with your
expected output. I didn't add them because I think they go through
the same code paths as the tests already in the patch. Let me know if
I've missed any.

Like v1, there's one patch each for master, 19, 18 and 17.

--
Thanks, Amit Langote

Attachment Content-Type Size
v2-REL_18-0001-SQL-JSON-Decide-how-to-coerce-JsonExpr-res.patch application/octet-stream 34.3 KB
v2-REL_17-0001-SQL-JSON-Decide-how-to-coerce-JsonExpr-res.patch application/octet-stream 34.3 KB
v2-master-0001-SQL-JSON-Decide-how-to-coerce-JsonExpr-res.patch application/octet-stream 35.5 KB
v2-REL_19-0001-SQL-JSON-Decide-how-to-coerce-JsonExpr-res.patch application/octet-stream 34.3 KB

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Kirill Reshke 2026-12-04 05:00:14 Re: BUG #19968: ALTER COLUMN TYPE fails with internal errors if a dependent object is dropped concurrently
Previous Message shihao zhong 2026-12-04 04:18:52 Re: BUG #19938: BRIN index on box silently drops ordinary matching rows when a NaN box shares the same page range