From c91830c0538bfb9470da67c21752caf35adfacd6 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 11/11] Test domain-constraint plan invalidation

Cache generic plans for outer-join pull-up and STRICT SQL inlining,
then add NOT NULL to the domain. Check replanning and NULL results.

A coercion to a domain without constraints is strict, so it may be
computed above an outer join after pulling up a subquery, and a STRICT
SQL function applying it may be inlined.  Such plans depend on the
domain, and adding a constraint must invalidate them, so that the
rebuilt plans treat the coercion as nonstrict.

Test this with cached generic plans, before and after ALTER DOMAIN ...
SET NOT NULL.
---
 src/test/regress/expected/domain.out | 96 ++++++++++++++++++++++++++++
 src/test/regress/sql/domain.sql      | 46 +++++++++++++
 2 files changed, 142 insertions(+)

diff --git a/src/test/regress/expected/domain.out b/src/test/regress/expected/domain.out
index bb9e18f45f4..0f65f5192ce 100644
--- a/src/test/regress/expected/domain.out
+++ b/src/test/regress/expected/domain.out
@@ -1466,6 +1466,102 @@ select dom_coerce_strict_unconstrained(x) from (values (1), (null)) v(x);
 drop function dom_coerce_strict_unconstrained(int);
 drop domain dom_unconstrained;
 --
+-- A coercion to a domain without constraints is strict, so a subquery
+-- output applying it may be pulled up and computed above an outer join, and
+-- a STRICT SQL function applying it may be inlined.  Such plans depend on the
+-- domain, and adding a constraint to the domain must invalidate them, so that
+-- the rebuilt plans treat the coercion as not strict.
+--
+create domain dom_inval as int;
+create temp table dom_inval_tab (y int);
+insert into dom_inval_tab values (2), (3);
+analyze dom_inval_tab;
+create function dom_inval_strict(int) returns int
+  language sql immutable strict
+  as $$ select $1::dom_inval::int $$;
+-- generic plans, cached until invalidated
+set plan_cache_mode = force_generic_plan;
+prepare dom_inval_pullup as
+  select x, y, d
+    from (values (1), (2)) a(x)
+         left join (select y, y::dom_inval as d from dom_inval_tab) ss on x = y
+   order by x;
+prepare dom_inval_inline(int) as select dom_inval_strict($1);
+-- the coercion is computed above the join, and the function is inlined
+explain (verbose, costs off) execute dom_inval_pullup;
+                                 QUERY PLAN                                  
+-----------------------------------------------------------------------------
+ Merge Left Join
+   Output: "*VALUES*".column1, dom_inval_tab.y, (dom_inval_tab.y)::dom_inval
+   Merge Cond: ("*VALUES*".column1 = dom_inval_tab.y)
+   ->  Sort
+         Output: "*VALUES*".column1
+         Sort Key: "*VALUES*".column1
+         ->  Values Scan on "*VALUES*"
+               Output: "*VALUES*".column1
+   ->  Sort
+         Output: dom_inval_tab.y
+         Sort Key: dom_inval_tab.y
+         ->  Seq Scan on pg_temp.dom_inval_tab
+               Output: dom_inval_tab.y
+(13 rows)
+
+explain (verbose, costs off) execute dom_inval_inline(null);
+  QUERY PLAN  
+--------------
+ Result
+   Output: $1
+(2 rows)
+
+alter domain dom_inval set not null;
+-- the plans are rebuilt, computing the coercion below the join, and not
+-- inlining the function
+explain (verbose, costs off) execute dom_inval_pullup;
+                                  QUERY PLAN                                   
+-------------------------------------------------------------------------------
+ Merge Left Join
+   Output: "*VALUES*".column1, dom_inval_tab.y, ((dom_inval_tab.y)::dom_inval)
+   Merge Cond: ("*VALUES*".column1 = dom_inval_tab.y)
+   ->  Sort
+         Output: "*VALUES*".column1
+         Sort Key: "*VALUES*".column1
+         ->  Values Scan on "*VALUES*"
+               Output: "*VALUES*".column1
+   ->  Sort
+         Output: dom_inval_tab.y, ((dom_inval_tab.y)::dom_inval)
+         Sort Key: dom_inval_tab.y
+         ->  Seq Scan on pg_temp.dom_inval_tab
+               Output: dom_inval_tab.y, (dom_inval_tab.y)::dom_inval
+(13 rows)
+
+explain (verbose, costs off) execute dom_inval_inline(null);
+           QUERY PLAN           
+--------------------------------
+ Result
+   Output: dom_inval_strict($1)
+(2 rows)
+
+-- so d is NULL for the unmatched x = 1, and the function returns NULL
+execute dom_inval_pullup;
+ x | y | d 
+---+---+---
+ 1 |   |  
+ 2 | 2 | 2
+(2 rows)
+
+execute dom_inval_inline(null);
+ dom_inval_strict 
+------------------
+                 
+(1 row)
+
+deallocate dom_inval_pullup;
+deallocate dom_inval_inline;
+reset plan_cache_mode;
+drop function dom_inval_strict(int);
+drop table dom_inval_tab;
+drop domain dom_inval;
+--
 -- Renaming
 --
 create domain testdomain1 as int;
diff --git a/src/test/regress/sql/domain.sql b/src/test/regress/sql/domain.sql
index 7a85c23f916..e3d58fab523 100644
--- a/src/test/regress/sql/domain.sql
+++ b/src/test/regress/sql/domain.sql
@@ -958,6 +958,52 @@ select dom_coerce_strict_unconstrained(x) from (values (1), (null)) v(x);
 drop function dom_coerce_strict_unconstrained(int);
 drop domain dom_unconstrained;
 
+--
+-- A coercion to a domain without constraints is strict, so a subquery
+-- output applying it may be pulled up and computed above an outer join, and
+-- a STRICT SQL function applying it may be inlined.  Such plans depend on the
+-- domain, and adding a constraint to the domain must invalidate them, so that
+-- the rebuilt plans treat the coercion as not strict.
+--
+
+create domain dom_inval as int;
+create temp table dom_inval_tab (y int);
+insert into dom_inval_tab values (2), (3);
+analyze dom_inval_tab;
+create function dom_inval_strict(int) returns int
+  language sql immutable strict
+  as $$ select $1::dom_inval::int $$;
+
+-- generic plans, cached until invalidated
+set plan_cache_mode = force_generic_plan;
+prepare dom_inval_pullup as
+  select x, y, d
+    from (values (1), (2)) a(x)
+         left join (select y, y::dom_inval as d from dom_inval_tab) ss on x = y
+   order by x;
+prepare dom_inval_inline(int) as select dom_inval_strict($1);
+
+-- the coercion is computed above the join, and the function is inlined
+explain (verbose, costs off) execute dom_inval_pullup;
+explain (verbose, costs off) execute dom_inval_inline(null);
+
+alter domain dom_inval set not null;
+
+-- the plans are rebuilt, computing the coercion below the join, and not
+-- inlining the function
+explain (verbose, costs off) execute dom_inval_pullup;
+explain (verbose, costs off) execute dom_inval_inline(null);
+-- so d is NULL for the unmatched x = 1, and the function returns NULL
+execute dom_inval_pullup;
+execute dom_inval_inline(null);
+
+deallocate dom_inval_pullup;
+deallocate dom_inval_inline;
+reset plan_cache_mode;
+drop function dom_inval_strict(int);
+drop table dom_inval_tab;
+drop domain dom_inval;
+
 --
 -- Renaming
 --
-- 
2.56.0

