From 281f42ffacc0d66eea051eb3c8af73e60394ebba 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 02/11] Treat JsonExpr as nonstrict

A NULL PASSING argument need not make a SQL/JSON query function return
SQL NULL. A JSON path can ignore it, and JSON null is not SQL NULL.
Likewise, JSON_VALUE/JSON_QUERY defaults for ON EMPTY or ON ERROR may
never be evaluated. Strict child expressions therefore do not prove
that the JsonExpr is strict in every Var or Param it contains.

Teach contain_nonstrict_functions() to classify JsonExpr as nonstrict.
This makes outer-join pull-up protect the whole result with a
PlaceHolderVar where needed, and prevents STRICT SQL-function
inlining from exposing a non-NULL result for a NULL argument.

The check is intentionally conservative. It also inhibits otherwise
valid inlining with constant side inputs, and can add unnecessary
PlaceHolderVars. Recovering those cases requires more selective
analysis of the JsonExpr inputs.
---
 src/backend/optimizer/util/clauses.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/src/backend/optimizer/util/clauses.c b/src/backend/optimizer/util/clauses.c
index 702fa0aefdd..ebc20815914 100644
--- a/src/backend/optimizer/util/clauses.c
+++ b/src/backend/optimizer/util/clauses.c
@@ -1197,6 +1197,14 @@ contain_nonstrict_functions_walker(Node *node, void *context)
 		return true;
 	else if (IsA(node, JsonConstructorExpr))
 		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;
+	}
 	else
 	{
 		/* Check other function-containing nodes */
-- 
2.56.0

