Re: [PATCH]Fix planner's strictness check for JsonExpr

From: Keyerror Smart <smartkeyerror(at)gmail(dot)com>
To: ethantran(at)quwin(dot)dev
Cc: PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: [PATCH]Fix planner's strictness check for JsonExpr
Date: 2026-11-19 01:36:56
Message-ID: CAD=-kXYng28QTj8n5KWzdEZx5U06WdCK3fYn7ZagZJAxNWY4DA@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi Tomas,

Thanks for looking at this, and for cross-checking v4 against your
test suite.

> The one thing this v4 fix does not handle is RETURNING with
> null-rejecting domains, which is violating strictness (as it turns
> NULL into an ERROR).

You're right, and it's worse than a missed case: v4 even had a test
asserting that this gets inlined. My reasoning was that with NULL /
UNKNOWN / ERROR behaviors the result is "either NULL or an error", but
of course an error is not NULL -- the STRICT function returns NULL
without evaluating its body, while the inlined expression raises. With
a NOT NULL domain, both the default NULL ON ERROR ("could not coerce ON
ERROR expression (NULL) to the RETURNING type") and ERROR ON ERROR fail
for a NULL context item, for both the inlining and the outer-join
pull-up paths.

Attached v5 adopts your check as-is:

if (jsexpr->use_json_coercion &&
get_typtype(jsexpr->returning->typid) == TYPTYPE_DOMAIN)
return true;

so the rule is now: a JsonExpr is non-strict if it coerces its result
to a domain via json_populate_type(), if it has PASSING arguments, or
if either ON EMPTY / ON ERROR is anything other than NULL, UNKNOWN or
ERROR; otherwise its inputs are checked as usual. Like yours, it
doesn't look at DomainHasConstraints(), which also keeps it independent
of constraints added after planning.

> Expressions that stop being inlined in a STRICT SQL function are for
> example: [...]

Agreed, the list is longer than "narrow". For a back-patched fix I'd
still prefer the simple rule: it only looks at node fields, and it
errs on the side of not inlining, which seems the right trade-off
given that (as you say) STRICT SQL wrappers around JSON queries are
probably not that common.

Regards,
Zhenglong

Attachment Content-Type Size
v5-0001-Fix-planner-s-strictness-check-for-JsonExpr.patch application/octet-stream 14.9 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Bharath Rupireddy 2026-11-19 01:38:38 Re: pg_xmin_horizon: a system view of everything pinning the xmin horizon
Previous Message Joao Rito 2026-11-19 01:30:03 [PATCH] pg_upgrade: add hints to wal_level and max_replication_slots checks