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: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: 303677365(at)qq(dot)com, pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Re: BUG #19851: Adding a tautological CHECK(true) to a domain changes JSON_VALUE's result (silent NULL, value/type c
Date: 2026-11-18 06:08:07
Message-ID: CA+HiwqEE-tkcz9oBKVEYGy7sXrMb3kSUNguWh5ptQ4_wGr9e8A@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

On Fri, Nov 13, 2026 at 10:28 PM Amit Langote <amitlangote09(at)gmail(dot)com> wrote:
> On Fri, Nov 13, 2026 at 6:32 AM Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
> > PG Bug reporting form <noreply(at)postgresql(dot)org> writes:
> > > Two domains with the *identical value set* — one declared without
> > > constraints, one with only a tautological `CHECK (true)` that can never
> > > reject anything — give *different results* for the same `JSON_VALUE`
> > > expression. `CHECK(true)` is a no-op semantically, so the two results must
> > > agree, and both must agree with the base type.
> >
> > Yeah, this is a mess. The entire design of JsonExpr in this area
> > is a disaster, starting here:
> >
> > /*
> > * Information about converting the result of jsonpath functions
> > * JsonPathQuery() and JsonPathValue() to the RETURNING type.
> > */
> > bool use_io_coercion;
> > bool use_json_coercion;
> >
> > Possibly, if these flags hadn't been so utterly undocumented,
> > transformJsonFuncExpr wouldn't be so confused about what it needs
> > to do with domains. But there are worse problems. The broken
> > logic is in parse_expr.c:
> >
> > if (get_typtype(jsexpr->returning->typid) == TYPTYPE_DOMAIN &&
> > DomainHasConstraints(jsexpr->returning->typid, NULL))
> > jsexpr->use_json_coercion = true;
> > else
> > jsexpr->use_io_coercion = true;
> >
> > Quite aside from whether this is the right thing, testing
> > DomainHasConstraints *at parse time* is insane. The situation
> > could be completely different by the time we execute the expression.
> > At the earliest this decision should be made in the planner; but
> > probably doing it at executor startup would be better.
>
> Yeah, I should have noticed that the result of a parse-time
> DomainHasConstraints() test gets frozen into the stored JsonExpr,
> while the domain's constraints can change at any time after that.
> Those flags are derivable from the rest of the node and shouldn't have
> been stored in it to begin with.
>
> > Another reason for doing it later is that we cannot fix the behavior
> > in released versions given this structure: even if we end up still
> > using exactly two booleans, any stored JsonExpr will already have
> > locked down the values of these flags. What I'm contemplating as a
> > potential fix design is to remove these flags in master, leave them
> > present-but-unused in back branches, and adjust the runtime
> > expression representation instead.
>
> I've started working on a patch, which I'll post in a couple of days.

Just a note: I got busy with a family matter in the last few days but
I'm working on the patch and will post it after working on it a bit
more.

--
Thanks, Amit Langote

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message Dilip Kumar 2026-11-18 09:57:45 Re: BUG #19858: pg_dump --clean --if-exists fails on NOT VALID domain constraint
Previous Message Henson Choi 2026-11-18 03:51:33 Re: BUG #19911: Window aggregate over ROWS n FOLLOWING: "unexpected end of tuplestore" after spill