pg.ddx.io pgsql-committers@postgresql.org mailing list archivehelp / color / mirror / Atom feed
pgsql: SQL/JSON: fix error handling when coercing to a domain fails sof 4+ messages / 1 participants [nested] [flat]
* pgsql: SQL/JSON: fix error handling when coercing to a domain fails sof @ 2026-10-29 01:01 Amit Langote <amitlan@postgresql.org> 0 siblings, 0 replies; 4+ messages in thread From: Amit Langote @ 2026-10-29 01:01 UTC (permalink / raw) To: pgsql-committers@lists.postgresql.org SQL/JSON: fix error handling when coercing to a domain fails softly SQL/JSON query functions could throw an error, or report the wrong one, in cases where the ON ERROR or ON EMPTY clause said what to do instead. This happened when the RETURNING type, or a DEFAULT expression, involved a domain: if the value failed to convert to the domain's base type, that failure was saved to be handled by the ON ERROR behavior, but the domain's constraints were then checked anyway, against the dummy NULL left behind by the failed conversion. A CHECK expression can throw a hard error on that NULL, which ON ERROR cannot catch, and a NOT NULL constraint reports a violation that overwrites the saved error, so the error message ends up describing the wrong problem. For example, this failed with "division by zero" instead of returning 11: CREATE DOMAIN d AS int CHECK (1 / coalesce(VALUE, 0) = 0); SELECT JSON_VALUE(jsonb '"x"', '$' RETURNING d DEFAULT 11 ON ERROR); Fix by stopping as soon as a soft error has been saved, in each of the places that kept going: * The domain constraint checks of a CoerceToDomain in an ON ERROR or ON EMPTY DEFAULT expression, which is the only case where they are evaluated softly. ExecInitCoerceToDomain now emits a jump before each constraint check that skips the remaining checks if an error has already been saved. * populate_domain, called from json_populate_type when coercing to a domain. Return as soon as the conversion to the base type fails, instead of going on to check the domain's constraints. * populate_record, likewise. Stop populating the remaining columns as soon as one of them fails. * ExecEvalJsonCoercion. Return at once if the DEFAULT expression whose result it would coerce has already failed, and leave throwing the error to the COERCION_FINISH step that follows. 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/46798ab1adf34bf57b7585019033db89fb0641e2 Modified Files -------------- src/backend/executor/execExpr.c | 23 ++++++++++ src/backend/executor/execExprInterp.c | 14 ++++++ src/backend/utils/adt/jsonfuncs.c | 19 +++++++++ src/test/regress/expected/sqljson_queryfuncs.out | 54 ++++++++++++++++++++++++ src/test/regress/sql/sqljson_queryfuncs.sql | 18 ++++++++ 5 files changed, 128 insertions(+) ^ permalink raw reply [nested|flat] 4+ messages in thread
* pgsql: SQL/JSON: fix error handling when coercing to a domain fails sof @ 2026-10-29 01:01 Amit Langote <amitlan@postgresql.org> 0 siblings, 0 replies; 4+ messages in thread From: Amit Langote @ 2026-10-29 01:01 UTC (permalink / raw) To: pgsql-committers@lists.postgresql.org SQL/JSON: fix error handling when coercing to a domain fails softly SQL/JSON query functions could throw an error, or report the wrong one, in cases where the ON ERROR or ON EMPTY clause said what to do instead. This happened when the RETURNING type, or a DEFAULT expression, involved a domain: if the value failed to convert to the domain's base type, that failure was saved to be handled by the ON ERROR behavior, but the domain's constraints were then checked anyway, against the dummy NULL left behind by the failed conversion. A CHECK expression can throw a hard error on that NULL, which ON ERROR cannot catch, and a NOT NULL constraint reports a violation that overwrites the saved error, so the error message ends up describing the wrong problem. For example, this failed with "division by zero" instead of returning 11: CREATE DOMAIN d AS int CHECK (1 / coalesce(VALUE, 0) = 0); SELECT JSON_VALUE(jsonb '"x"', '$' RETURNING d DEFAULT 11 ON ERROR); Fix by stopping as soon as a soft error has been saved, in each of the places that kept going: * The domain constraint checks of a CoerceToDomain in an ON ERROR or ON EMPTY DEFAULT expression, which is the only case where they are evaluated softly. ExecInitCoerceToDomain now emits a jump before each constraint check that skips the remaining checks if an error has already been saved. * populate_domain, called from json_populate_type when coercing to a domain. Return as soon as the conversion to the base type fails, instead of going on to check the domain's constraints. * populate_record, likewise. Stop populating the remaining columns as soon as one of them fails. * ExecEvalJsonCoercion. Return at once if the DEFAULT expression whose result it would coerce has already failed, and leave throwing the error to the COERCION_FINISH step that follows. 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/b9a4153603760a0685ca101ba6b32d83a61490e0 Modified Files -------------- src/backend/executor/execExpr.c | 23 ++++++++++ src/backend/executor/execExprInterp.c | 14 ++++++ src/backend/utils/adt/jsonfuncs.c | 19 +++++++++ src/test/regress/expected/sqljson_queryfuncs.out | 54 ++++++++++++++++++++++++ src/test/regress/sql/sqljson_queryfuncs.sql | 18 ++++++++ 5 files changed, 128 insertions(+) ^ permalink raw reply [nested|flat] 4+ messages in thread
* pgsql: SQL/JSON: fix error handling when coercing to a domain fails sof @ 2026-10-29 01:01 Amit Langote <amitlan@postgresql.org> 0 siblings, 0 replies; 4+ messages in thread From: Amit Langote @ 2026-10-29 01:01 UTC (permalink / raw) To: pgsql-committers@lists.postgresql.org SQL/JSON: fix error handling when coercing to a domain fails softly SQL/JSON query functions could throw an error, or report the wrong one, in cases where the ON ERROR or ON EMPTY clause said what to do instead. This happened when the RETURNING type, or a DEFAULT expression, involved a domain: if the value failed to convert to the domain's base type, that failure was saved to be handled by the ON ERROR behavior, but the domain's constraints were then checked anyway, against the dummy NULL left behind by the failed conversion. A CHECK expression can throw a hard error on that NULL, which ON ERROR cannot catch, and a NOT NULL constraint reports a violation that overwrites the saved error, so the error message ends up describing the wrong problem. For example, this failed with "division by zero" instead of returning 11: CREATE DOMAIN d AS int CHECK (1 / coalesce(VALUE, 0) = 0); SELECT JSON_VALUE(jsonb '"x"', '$' RETURNING d DEFAULT 11 ON ERROR); Fix by stopping as soon as a soft error has been saved, in each of the places that kept going: * The domain constraint checks of a CoerceToDomain in an ON ERROR or ON EMPTY DEFAULT expression, which is the only case where they are evaluated softly. ExecInitCoerceToDomain now emits a jump before each constraint check that skips the remaining checks if an error has already been saved. * populate_domain, called from json_populate_type when coercing to a domain. Return as soon as the conversion to the base type fails, instead of going on to check the domain's constraints. * populate_record, likewise. Stop populating the remaining columns as soon as one of them fails. * ExecEvalJsonCoercion. Return at once if the DEFAULT expression whose result it would coerce has already failed, and leave throwing the error to the COERCION_FINISH step that follows. 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/67f375edb58ec7264019142c9702e0d67748b157 Modified Files -------------- src/backend/executor/execExpr.c | 23 ++++++++++ src/backend/executor/execExprInterp.c | 14 ++++++ src/backend/utils/adt/jsonfuncs.c | 19 +++++++++ src/test/regress/expected/sqljson_queryfuncs.out | 54 ++++++++++++++++++++++++ src/test/regress/sql/sqljson_queryfuncs.sql | 18 ++++++++ 5 files changed, 128 insertions(+) ^ permalink raw reply [nested|flat] 4+ messages in thread
* pgsql: SQL/JSON: fix error handling when coercing to a domain fails sof @ 2026-10-29 01:01 Amit Langote <amitlan@postgresql.org> 0 siblings, 0 replies; 4+ messages in thread From: Amit Langote @ 2026-10-29 01:01 UTC (permalink / raw) To: pgsql-committers@lists.postgresql.org SQL/JSON: fix error handling when coercing to a domain fails softly SQL/JSON query functions could throw an error, or report the wrong one, in cases where the ON ERROR or ON EMPTY clause said what to do instead. This happened when the RETURNING type, or a DEFAULT expression, involved a domain: if the value failed to convert to the domain's base type, that failure was saved to be handled by the ON ERROR behavior, but the domain's constraints were then checked anyway, against the dummy NULL left behind by the failed conversion. A CHECK expression can throw a hard error on that NULL, which ON ERROR cannot catch, and a NOT NULL constraint reports a violation that overwrites the saved error, so the error message ends up describing the wrong problem. For example, this failed with "division by zero" instead of returning 11: CREATE DOMAIN d AS int CHECK (1 / coalesce(VALUE, 0) = 0); SELECT JSON_VALUE(jsonb '"x"', '$' RETURNING d DEFAULT 11 ON ERROR); Fix by stopping as soon as a soft error has been saved, in each of the places that kept going: * The domain constraint checks of a CoerceToDomain in an ON ERROR or ON EMPTY DEFAULT expression, which is the only case where they are evaluated softly. ExecInitCoerceToDomain now emits a jump before each constraint check that skips the remaining checks if an error has already been saved. * populate_domain, called from json_populate_type when coercing to a domain. Return as soon as the conversion to the base type fails, instead of going on to check the domain's constraints. * populate_record, likewise. Stop populating the remaining columns as soon as one of them fails. * ExecEvalJsonCoercion. Return at once if the DEFAULT expression whose result it would coerce has already failed, and leave throwing the error to the COERCION_FINISH step that follows. 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/b6e41c16f8ccbfca0d51c94bdb6043b6e8393d6f Modified Files -------------- src/backend/executor/execExpr.c | 23 ++++++++++ src/backend/executor/execExprInterp.c | 14 ++++++ src/backend/utils/adt/jsonfuncs.c | 19 +++++++++ src/test/regress/expected/sqljson_queryfuncs.out | 54 ++++++++++++++++++++++++ src/test/regress/sql/sqljson_queryfuncs.sql | 18 ++++++++ 5 files changed, 128 insertions(+) ^ permalink raw reply [nested|flat] 4+ messages in thread
end of thread, other threads:[~2026-10-29 01:01 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 01:01 pgsql: SQL/JSON: fix error handling when coercing to a domain fails sof Amit Langote <amitlan@postgresql.org> 2026-10-29 01:01 pgsql: SQL/JSON: fix error handling when coercing to a domain fails sof Amit Langote <amitlan@postgresql.org> 2026-10-29 01:01 pgsql: SQL/JSON: fix error handling when coercing to a domain fails sof Amit Langote <amitlan@postgresql.org> 2026-10-29 01:01 pgsql: SQL/JSON: fix error handling when coercing to a domain fails sof 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