From 6ae7dba493a1a2dca891caa167c74942dd8c2d53 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 08/11] Fix domain-coercion strictness check

CoerceToDomain is not necessarily strict in its argument. A NOT NULL
constraint, or a CHECK constraint that rejects NULL, can raise an
error instead of returning NULL. Merely checking the coercion's
argument misses this behavior.

After pulling up the nullable side of an outer join, the planner may
otherwise evaluate the coercion on a null-extended row. Inlining a
STRICT SQL function can likewise evaluate a cast that the original
call would skip for a NULL argument. Both cases introduce constraint
errors on inputs the query never coerces.

Classify CoerceToDomain as nonstrict in contain_nonstrict_functions().
This lets pull-up protect the result with a PlaceHolderVar and stops
STRICT SQL-function inlining from exposing the coercion to NULL.

The treatment is conservative even for unconstrained domains, which
can lose inlining and acquire unnecessary placeholders. Update the
affected subselect expectation: it still uses indexes, but retains
additional domain relabeling.
---
 src/backend/optimizer/util/clauses.c    |  8 ++++++++
 src/test/regress/expected/subselect.out | 10 +++++-----
 src/test/regress/sql/subselect.sql      |  4 ++--
 3 files changed, 15 insertions(+), 7 deletions(-)

diff --git a/src/backend/optimizer/util/clauses.c b/src/backend/optimizer/util/clauses.c
index ca0e69c7870..1388d774eee 100644
--- a/src/backend/optimizer/util/clauses.c
+++ b/src/backend/optimizer/util/clauses.c
@@ -1161,6 +1161,14 @@ contain_nonstrict_functions_walker(Node *node, void *context)
 		return true;
 	else if (IsA(node, FieldStore))
 		return true;
+	else if (IsA(node, CoerceToDomain))
+	{
+		/*
+		 * Domain constraints can reject NULL.  Pull-up or strict-function
+		 * inlining must not introduce new constraint violations.
+		 */
+		return true;
+	}
 	else if (IsA(node, CoerceViaIO))
 	{
 		/*
diff --git a/src/test/regress/expected/subselect.out b/src/test/regress/expected/subselect.out
index ef73218d9db..ac7646a3961 100644
--- a/src/test/regress/expected/subselect.out
+++ b/src/test/regress/expected/subselect.out
@@ -2457,8 +2457,8 @@ select * from json_tab t1 left join (select json_array(1, a) from json_tab t2) s
 
 rollback;
 --
--- Verify that we correctly flatten cases involving a subquery output
--- expression that doesn't need to be wrapped in a PlaceHolderVar
+-- Verify that a PlaceHolderVar around a subquery output domain cast
+-- doesn't prevent index use after outer-join reduction
 --
 explain (costs off)
 select tname, attname from (
@@ -2466,11 +2466,11 @@ select relname::information_schema.sql_identifier as tname, * from
   (select * from pg_class c) ss1) ss2
   right join pg_attribute a on a.attrelid = ss2.oid
 where tname = 'tenk1' and attnum = 1;
-                                QUERY PLAN                                
---------------------------------------------------------------------------
+                                         QUERY PLAN                                         
+--------------------------------------------------------------------------------------------
  Nested Loop
    ->  Index Scan using pg_class_relname_nsp_index on pg_class c
-         Index Cond: (relname = 'tenk1'::name)
+         Index Cond: (((relname)::information_schema.sql_identifier)::name = 'tenk1'::name)
    ->  Index Scan using pg_attribute_relid_attnum_index on pg_attribute a
          Index Cond: ((attrelid = c.oid) AND (attnum = 1))
 (5 rows)
diff --git a/src/test/regress/sql/subselect.sql b/src/test/regress/sql/subselect.sql
index 6db56b24b60..5c4d047ea6a 100644
--- a/src/test/regress/sql/subselect.sql
+++ b/src/test/regress/sql/subselect.sql
@@ -1225,8 +1225,8 @@ select * from json_tab t1 left join (select json_array(1, a) from json_tab t2) s
 rollback;
 
 --
--- Verify that we correctly flatten cases involving a subquery output
--- expression that doesn't need to be wrapped in a PlaceHolderVar
+-- Verify that a PlaceHolderVar around a subquery output domain cast
+-- doesn't prevent index use after outer-join reduction
 --
 
 explain (costs off)
-- 
2.56.0

