pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
pgsql: SQL/JSON: always rethrow soft errors from DEFAULT expressions
4+ messages / 1 participants
[nested] [flat]

* pgsql: SQL/JSON: always rethrow soft errors from DEFAULT expressions
@ 2026-10-29 00:24 Amit Langote <amitlan@postgresql.org>
  0 siblings, 0 replies; 4+ messages in thread

From: Amit Langote @ 2026-10-29 00:24 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

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
------
REL_17_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/6dfe31d48a9b401f4b05fef63f6eb5b990823368

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(-)



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* pgsql: SQL/JSON: always rethrow soft errors from DEFAULT expressions
@ 2026-10-29 00:24 Amit Langote <amitlan@postgresql.org>
  0 siblings, 0 replies; 4+ messages in thread

From: Amit Langote @ 2026-10-29 00:24 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

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
------
REL_18_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/5209a736a385513fff4be0d79eaaa7a3309458c9

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(-)



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* pgsql: SQL/JSON: always rethrow soft errors from DEFAULT expressions
@ 2026-10-29 00:25 Amit Langote <amitlan@postgresql.org>
  0 siblings, 0 replies; 4+ messages in thread

From: Amit Langote @ 2026-10-29 00:25 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

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
------
REL_19_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/6839997e332281576c02c926a711df1edc50e371

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(-)



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* pgsql: SQL/JSON: always rethrow soft errors from DEFAULT expressions
@ 2026-10-29 00:25 Amit Langote <amitlan@postgresql.org>
  0 siblings, 0 replies; 4+ messages in thread

From: Amit Langote @ 2026-10-29 00:25 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

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(-)



^ permalink  raw  reply  [nested|flat] 4+ messages in thread


end of thread, other threads:[~2026-10-29 00:25 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-10-29 00:24 pgsql: SQL/JSON: always rethrow soft errors from DEFAULT expressions Amit Langote <amitlan@postgresql.org>
2026-10-29 00:24 pgsql: SQL/JSON: always rethrow soft errors from DEFAULT expressions Amit Langote <amitlan@postgresql.org>
2026-10-29 00:25 pgsql: SQL/JSON: always rethrow soft errors from DEFAULT expressions Amit Langote <amitlan@postgresql.org>
2026-10-29 00:25 pgsql: SQL/JSON: always rethrow soft errors from DEFAULT expressions Amit Langote <amitlan@postgresql.org>

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