From 48a992da6970cbfa5e5f9ece716e03935d9ad001 Mon Sep 17 00:00:00 2001
From: Tomas Vondra <tomas@vondra.me>
Date: Sun, 15 Nov 2026 21:03:11 +0100
Subject: [PATCH v1 06/11] Treat SQL/JSON domain error fallbacks as nonstrict

JSON_VALUE and JSON_QUERY can still apply a domain coercion after a
NULL context item or path has skipped JSON path evaluation. When
use_json_coercion is set and RETURNING names a domain, the NULL is
passed through the domain constraint checks. A rejecting constraint
can therefore invoke ON ERROR and produce a non-NULL fallback such
as DEFAULT 0 or EMPTY ARRAY.

The context/path strictness assumption can consequently produce a
non-NULL value for an unmatched outer-join row or a NULL argument of
a STRICT SQL function. Add an exception for JsonExpr nodes using
JSON coercion to a domain when ON ERROR can return a non-NULL value.

Do not test the domain's current constraints before applying the
exception. This JsonExpr coercion records no domain dependency, so a
later ALTER DOMAIN could invalidate a strictness assumption without
causing replanning.

ERROR ON ERROR and error behaviors with a SQL NULL constant remain
unchanged. They can still raise a domain error for NULL input; this
commit addresses only the non-NULL fallback case.
---
 src/backend/optimizer/util/clauses.c | 28 +++++++++++++++++++++++++++-
 1 file changed, 27 insertions(+), 1 deletion(-)

diff --git a/src/backend/optimizer/util/clauses.c b/src/backend/optimizer/util/clauses.c
index eab235d3f08..ca0e69c7870 100644
--- a/src/backend/optimizer/util/clauses.c
+++ b/src/backend/optimizer/util/clauses.c
@@ -1226,15 +1226,41 @@ contain_nonstrict_functions_walker(Node *node, void *context)
  * arguments or ON EMPTY / ON ERROR defaults.  Callers may find their only
  * Var, PlaceHolderVar or Param there, so reject those cases.  Variable-free
  * inputs such as DEFAULT 0 do not preclude strictness.
+ *
+ * So the JsonExpr has to be considered nonstrict if any of those contains a
+ * Var, PlaceHolderVar or Param. Constants are harmless there, and common
+ * enough that we don't want to reject them (e.g. it would prevent inlining
+ * SQL functions with DEFAULT 0).
+ *
+ * NULL propagation from the evaluated context/path does not prove strictness
+ * of arbitrary subexpressions inside them.
+ *
+ * The exception is a JsonExpr with use_json_coercion and a domain RETURNING
+ * type, as ExecEvalJsonExprPath() coerces even the NULL result to the domain
+ * to check its constraints.  If the domain rejects NULL, ON ERROR applies,
+ * so a NULL context item or path specification may produce a non-NULL ON
+ * ERROR value, and the JsonExpr is not strict in any of its inputs.  We
+ * don't check whether the domain actually rejects NULL, as its constraints
+ * might change after planning.  If ON ERROR is ERROR or a NULL constant, the
+ * result is either NULL or an error, which is fine (we treat CoerceToDomain
+ * as strict too).
  */
 static bool
 jsonexpr_has_nonstrict_inputs(JsonExpr *jsexpr)
 {
+	JsonBehavior *on_error = jsexpr->on_error;
+
 	if (contain_var_or_param_walker((Node *) jsexpr->passing_values, NULL))
 		return true;
 	if (contain_var_or_param_walker((Node *) jsexpr->on_empty, NULL))
 		return true;
-	if (contain_var_or_param_walker((Node *) jsexpr->on_error, NULL))
+	if (contain_var_or_param_walker((Node *) on_error, NULL))
+		return true;
+	if (jsexpr->use_json_coercion &&
+		on_error->btype != JSON_BEHAVIOR_ERROR &&
+		!(IsA(on_error->expr, Const) &&
+		  ((Const *) on_error->expr)->constisnull) &&
+		get_typtype(jsexpr->returning->typid) == TYPTYPE_DOMAIN)
 		return true;
 	return false;
 }
-- 
2.56.0

