pgsql: Fix const-folding of JSON constructors inside a simple CASE

From: Richard Guo <rguo(at)postgresql(dot)org>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Fix const-folding of JSON constructors inside a simple CASE
Date: 2026-09-14 08:09:57
Message-ID: E1x61lF-00000000G0w-2ChL@gemulon.postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Fix const-folding of JSON constructors inside a simple CASE

A JSON constructor with a RETURNING clause uses a CaseTestExpr as the
placeholder for its result in the coercion expression. When such a
constructor appears in a WHEN clause of a simple CASE whose test
expression is a constant, eval_const_expressions substituted that
constant for the placeholder, so the coercion produced the CASE's test
value instead of the constructor's result. For instance,

CASE 'x' WHEN JSON_OBJECT('a': 'b' RETURNING text) THEN 1 ELSE 0 END

evaluated to 1.

To fix, keep case_val out of scope while simplifying the coercion, as
is already done for the elemexpr of an ArrayCoerceExpr.

Back-patch to v16, where the SQL/JSON constructor functions were
introduced.

Author: Richard Guo <guofenglinux(at)gmail(dot)com>
Discussion: https://postgr.es/m/CAMbWs48A=VCFbteTkuCoknO1_0-Cu0aMBT0M07dm7vj1QyixDg@mail.gmail.com
Backpatch-through: 16

Branch
------
REL_16_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/27ef9097b59bbdd059194bac30708eba518ab4d4

Modified Files
--------------
src/backend/optimizer/util/clauses.c | 35 ++++++++++++++++++++++++++++++++++-
src/include/nodes/primnodes.h | 10 +++++-----
src/test/regress/expected/sqljson.out | 7 +++++++
src/test/regress/sql/sqljson.sql | 2 ++
4 files changed, 48 insertions(+), 6 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Etsuro Fujita 2026-09-14 10:04:07 pgsql: postgres_fdw: Disable attribute statistics import from pre-9.1 s
Previous Message Michael Paquier 2026-09-14 07:04:41 pgsql: Fix attribute name of pg_stat_get_backend_subxact()