Re: Wrong result from JSON constructor in a simple CASE

From: Richard Guo <guofenglinux(at)gmail(dot)com>
To: Pg Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: Wrong result from JSON constructor in a simple CASE
Date: 2026-09-03 02:39:34
Message-ID: CAMbWs4_N88cAWUBX7iwTO8c+JcUp3FUTt6H_BZ5dqi-0eZY=SQ@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Fri, Aug 28, 2026 at 10:29 PM Richard Guo <guofenglinux(at)gmail(dot)com> wrote:
> While working on the fix for qual pushdown past grouping through a
> simple CASE [1], I modeled the v2 patch's handling of CaseTestExpr on
> what eval_const_expressions does. Ewan Young pointed out that v2
> mishandled the CaseTestExpr in a JSON constructor's coercion
> expression, which led me to notice that eval_const_expressions has the
> same bug:

I found another related but separate problem by grepping all the
places that handle CaseTestExpr.

contain_context_dependent_node_walker() knows that a CaseTestExpr is
expected under a simple CaseExpr or in the elemexpr of an
ArrayCoerceExpr, but not that a JsonConstructorExpr whose RETURNING
type needs a coercion also carries one in that coercion. So
inline_function() refuses to inline any SQL function that is passed
such a constructor as an argument. As an example, consider:

create function f(text) returns text language sql immutable
as $$ select $1 || '!' $$;

explain (verbose, costs off)
select f(json_object('a': x returning text)) from generate_series(1,1) x;
QUERY PLAN
--------------------------------------------------
Function Scan on pg_catalog.generate_series x
Output: f(JSON_OBJECT('a' : x RETURNING text))
Function Call: generate_series(1, 1)
(3 rows)

The attached 0002 teaches the walker about the coercion of a
JsonConstructorExpr. It's basically just mirroring what it already
does for ArrayCoerceExpr.

0001 is a bug fix and should be back-patched to v16 where JSON
constructor was introduced. 0002 is a missed optimization so maybe we
can leave it to master-only?

- Richard

Attachment Content-Type Size
v2-0001-Fix-const-folding-of-JSON-constructors-inside-a-s.patch application/octet-stream 5.7 KB
v2-0002-Don-t-let-JSON-constructor-coercions-block-SQL-fu.patch application/octet-stream 5.2 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Corey Huinker 2026-09-03 02:57:15 Re: Remove fcinfo from statistics update internal functions
Previous Message xliu19163 2026-09-03 02:30:23 Re: Many of psql's describe functions bloat cache / waste mem