pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Amit Langote <amitlan@postgresql.org>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: SQL/JSON: always rethrow soft errors from DEFAULT expressions
Date: Thu, 29 Oct 2026 00:25:27 +0000
Message-ID: <E1xMDxP-00000001BMe-0nrX@gemulon.postgresql.org> (raw)

SQL/JSON: always rethrow soft errors from DEFAULT expressions

The DEFAULT expression of an ON ERROR or ON EMPTY clause is compiled
with the JsonExpr's ErrorSaveContext in effect, so that any error
raised while evaluating it is saved rather than thrown and can be
reported later as "could not coerce ON ERROR expression (DEFAULT) to
the RETURNING type". That reporting is done by an
EEOP_JSONEXPR_COERCION_FINISH step, but ExecInitJsonExpr only added
that step when the DEFAULT expression needed a runtime coercion, or
when its top-level node was a CoerceViaIO or a CoerceToDomain.  Those
are not the only nodes that can save an error: the ErrorSaveContext
applies to the whole expression tree, so a cast nested inside, say,
an OpExpr saves its error just the same.  In that case there was
nothing to rethrow it and the query silently returned NULL instead
of failing, for example:

  SELECT JSON_VALUE('"a"', '$' RETURNING int
                    DEFAULT (('a' || (random() * 0)::int)::int + 0)
                    ON ERROR);

Fix by adding the COERCION_FINISH step after every DEFAULT expression
that is evaluated softly, that is, whenever the ON ERROR behavior is
not ERROR.

Reported-by: jian he <jian.universality@gmail.com>
Author: jian he <jian.universality@gmail.com>
Discussion: https://postgr.es/m/CACJufxHDK39zF+1zZF6prH_wtn1SDt74_mrqYk1Oo82cCu44ng@mail.gmail.com
Backpatch-through: 17

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/17b4e99638aa015a5046cf365bd923261d92c35b

Modified Files
--------------
src/backend/executor/execExpr.c                  | 35 ++++++++++--------------
src/test/regress/expected/sqljson_jsontable.out  | 13 +++++++++
src/test/regress/expected/sqljson_queryfuncs.out | 17 ++++++++++++
src/test/regress/sql/sqljson_jsontable.sql       |  5 ++++
src/test/regress/sql/sqljson_queryfuncs.sql      |  8 ++++++
5 files changed, 58 insertions(+), 20 deletions(-)



view thread (4+ messages)

Message-ID: <E1xMDxP-00000001BMe-0nrX@gemulon.postgresql.org>
Permalink:  ../E1xMDxP-00000001BMe-0nrX@gemulon.postgresql.org/
Also on:    postgresql.org/message-id/E1xMDxP-00000001BMe-0nrX@gemulon.postgresql.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-committers@postgresql.org
  Cc: amitlan@postgresql.org
  Subject: Re: pgsql: SQL/JSON: always rethrow soft errors from DEFAULT expressions
  In-Reply-To: <E1xMDxP-00000001BMe-0nrX@gemulon.postgresql.org>
  References: <E1xMDxP-00000001BMe-0nrX@gemulon.postgresql.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox