| 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-12-02 12:54:51 |
| Message-ID: | CA+HiwqHBFy_=kxpqnbs2xcN36VZGy5s_ozOs21r=U+GYtoZJdw@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.
> The main change is that the choice of coercion method will depend only
> on which function it is (JSON_VALUE, JSON_QUERY or JSON_EXISTS) and
> the RETURNING type, not on whether the domain has constraints, so a
> domain gets the same result as its base type. The constraint test was
> there so that a NULL result would still get the domain's constraints
> checked; the patch will pass the NULL to domain_in() instead.
>
> With that, nothing in the decision can change after parsing, but I'd
> still make it at executor startup, because the flags already stored in
> back-branch JsonExprs were computed by the old rule and have to be
> ignored.
>
> In master, as you say, the patch will remove the two flags from
> JsonExpr and also the parser code (in transformJsonFuncExpr()) that
> sets them, while leaving all of that in place in the back branches.
Sorry for the delay.
Attached are patches implementing what is mentioned above. In the
back branches I kept the parser setting the JsonExpr flags, so that a
server on an older minor release that reads a JsonExpr stored by a
newer one still sees what it expects. I am not sure how careful we
are about that sort of thing but Claude suggested it in a review and I
thought it made sense. Haven't tested that though.
The patches fix two separate problems underlying the reported results:
* JSON_VALUE() used the RETURNING type's input function, except for
domains with constraints, where it used json_populate_type(). The two
convert some items differently, e.g. a boolean is 't' with the input
function and 'true' with json_populate_type(). It now always uses the
input function, so that domains match their base types.
* The executor passes the item as JSON text only if the RETURNING type
is json or jsonb, and it tested that by comparing type OIDs, which a
domain over json/jsonb doesn't match. So the item was passed as plain
text ('t', or a string without its quotes), which gave the NULL for
true and the 1 for "1" in the report. It now checks the base type
instead. The check is done once at executor startup and its result is
stored in a new flag, returning_json, in the EEOP_JSONEXPR_PATH step's
member of ExprEvalStep. That member had room, so no struct changes
size in the back branches.
I also expanded the comments about JsonExpr evaluation while I was at it.
This changes results in the back branches. Constrained domains over
string types now return 't' for a JSON true where they returned 'true'
(or NULL if that didn't fit), and domains over json/jsonb now return
what plain json/jsonb do. That will need a release note about
rebuilding indexes and stored generated columns that use such
expressions. Details are in the commit message.
Note that 't' isn't what the standard asks for either. It specifies a
cast from the item's type (going by what I read in [1], [2]), which
would give 'true'. The 't' comes from my earlier change to always go
through the input function (716bd12d), instead of fiddling around in
the parser to build a cast expression where a cast exists and falling
back to the input function where it doesn't. I did that because the
casts silently truncated over-length strings and some of them couldn't
report errors softly for ON ERROR. The patch leaves that alone.
--
Thanks, Amit Langote
[1] https://postgresql.org/message-id/49537b27-6c0b-49cc-b168-361b0c9ada09%40eisentraut.org
[2] https://www.postgresql.org/message-id/225d341b-f310-eed0-ceff-b8ec61669d9f@postgrespro.ru
| Attachment | Content-Type | Size |
|---|---|---|
| v1-REL_19-0001-SQL-JSON-Decide-how-to-coerce-JsonExpr-res.patch | application/octet-stream | 29.6 KB |
| v1-master-0001-SQL-JSON-Decide-how-to-coerce-JsonExpr-res.patch | application/octet-stream | 30.9 KB |
| v1-REL_18-0001-SQL-JSON-Decide-how-to-coerce-JsonExpr-res.patch | application/octet-stream | 29.6 KB |
| v1-REL_17-0001-SQL-JSON-Decide-how-to-coerce-JsonExpr-res.patch | application/octet-stream | 29.6 KB |
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Shrihari Danawe | 2026-12-02 14:25:17 | Re: pg_rewind WAL segments deletion pitfall |
| Previous Message | Konstantin Knizhnik | 2026-12-02 12:23:00 | Re: BUG #19519: REPACK can fail due to missing chunk for toast value |