From b5568cbf9cc7d332ce5b26d8d8d46064334d88d0 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 04/11] Refine JsonExpr strictness checks

Treating every JsonExpr as nonstrict also rejects cases that do
propagate NULL from the context item or path. Constant PASSING values,
defaults and JSON_QUERY wrapper options do not by themselves prevent
that propagation, so the blanket check loses useful SQL inlining and
introduces unnecessary PlaceHolderVars.

Replace the unconditional rule with a scan of PASSING values and
ON EMPTY / ON ERROR expressions for Vars, PlaceHolderVars and Params.
A caller's only nullable input may occur in one of these positions,
where NULL does not imply a NULL result. Count a PlaceHolderVar even
if its contained expression is constant: outer-join null-extension
can still change the placeholder's value.

If no such variable input is found, let the ordinary expression walker
check all children for nonstrict constructs, including COALESCE in the
context item or path. This permits eligible STRICT SQL wrappers with
constant side inputs to inline, and avoids unnecessary PlaceHolderVars
during pull-up without preventing pull-up itself.
---
 src/backend/optimizer/util/clauses.c | 42 ++++++++++++++++++++++++----
 1 file changed, 37 insertions(+), 5 deletions(-)

diff --git a/src/backend/optimizer/util/clauses.c b/src/backend/optimizer/util/clauses.c
index ebc20815914..eab235d3f08 100644
--- a/src/backend/optimizer/util/clauses.c
+++ b/src/backend/optimizer/util/clauses.c
@@ -124,6 +124,8 @@ static bool contain_volatile_functions_not_nextval_walker(Node *node, void *cont
 static bool max_parallel_hazard_walker(Node *node,
 									   max_parallel_hazard_context *context);
 static bool contain_nonstrict_functions_walker(Node *node, void *context);
+static bool jsonexpr_has_nonstrict_inputs(JsonExpr *jsexpr);
+static bool contain_var_or_param_walker(Node *node, void *context);
 static bool contain_exec_param_walker(Node *node, List *param_ids);
 static bool contain_context_dependent_node(Node *clause);
 static bool contain_context_dependent_node_walker(Node *node, int *flags);
@@ -1199,11 +1201,9 @@ contain_nonstrict_functions_walker(Node *node, void *context)
 		return true;
 	else if (IsA(node, JsonExpr))
 	{
-		/*
-		 * PASSING and defaults are not strict inputs.  Callers may find
-		 * their only Var or Param there, so skipping them is unsafe.
-		 */
-		return true;
+		if (jsonexpr_has_nonstrict_inputs((JsonExpr *) node))
+			return true;
+		/* else fall through to check args */
 	}
 	else
 	{
@@ -1217,6 +1217,38 @@ contain_nonstrict_functions_walker(Node *node, void *context)
 								  context);
 }
 
+/*
+ * jsonexpr_has_nonstrict_inputs
+ *		Check for variable inputs in non-strict parts of a JsonExpr.
+ *
+ * A JsonExpr is strict in its context item and path specification, whose
+ * subexpressions the caller checks separately.  It is not strict in PASSING
+ * 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.
+ */
+static bool
+jsonexpr_has_nonstrict_inputs(JsonExpr *jsexpr)
+{
+	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))
+		return true;
+	return false;
+}
+
+static bool
+contain_var_or_param_walker(Node *node, void *context)
+{
+	if (node == NULL)
+		return false;
+	if (IsA(node, Var) || IsA(node, PlaceHolderVar) || IsA(node, Param))
+		return true;
+	return expression_tree_walker(node, contain_var_or_param_walker, context);
+}
+
 /*****************************************************************************
  *		Check clauses for Params
  *****************************************************************************/
-- 
2.56.0

