From 3395c15eb2c3eb9f0476814e12fd643ce0e0def5 Mon Sep 17 00:00:00 2001
From: Sho Ito <i.sho.0628@gmail.com>
Date: Sun, 15 Nov 2026 18:22:45 -0500
Subject: [PATCH v1] Preserve implicit composite casts in ROW deparsing

A RowExpr implicitly coerced to a named composite type was printed without
its type even when showimplicit was requested. Function and operator
arguments need this decoration to preserve overload resolution on reparse.

After another function overload is added, the missing cast can make a view
definition ambiguous or silently bind it to the wrong function on restore.
Print the named type when implicit casts are requested, while leaving
anonymous records unchanged.

Add pretty and non-pretty view round trips for ambiguous and silently
rebound function calls, with explicit-cast and anonymous-record controls.
Update EXPLAIN output that now shows named composite types.
---
 src/backend/utils/adt/ruleutils.c             | 10 ++-
 src/test/regress/expected/alter_table.out     | 12 +--
 src/test/regress/expected/rowtypes.out        | 81 +++++++++++++++++++
 src/test/regress/expected/updatable_views.out |  6 +-
 src/test/regress/sql/rowtypes.sql             | 49 +++++++++++
 5 files changed, 148 insertions(+), 10 deletions(-)

diff --git a/src/backend/utils/adt/ruleutils.c b/src/backend/utils/adt/ruleutils.c
index e9c1c9db..ba03dafc 100644
--- a/src/backend/utils/adt/ruleutils.c
+++ b/src/backend/utils/adt/ruleutils.c
@@ -10018,7 +10018,15 @@ get_rule_expr(Node *node, deparse_context *context,
 					ReleaseTupleDesc(tupdesc);
 				}
 				appendStringInfoChar(buf, ')');
-				if (rowexpr->row_format == COERCE_EXPLICIT_CAST)
+
+				/*
+				 * Function and operator arguments need their actual types
+				 * preserved so that the same overload is selected on reparse.
+				 * A bare ROW() has type RECORD, even when the stored RowExpr
+				 * was implicitly coerced to a named composite type.
+				 */
+				if (rowexpr->row_format == COERCE_EXPLICIT_CAST ||
+					(showimplicit && rowexpr->row_typeid != RECORDOID))
 					appendStringInfo(buf, "::%s",
 									 format_type_with_typemod(rowexpr->row_typeid, -1));
 			}
diff --git a/src/test/regress/expected/alter_table.out b/src/test/regress/expected/alter_table.out
index f99b2b9e..543c6022 100644
--- a/src/test/regress/expected/alter_table.out
+++ b/src/test/regress/expected/alter_table.out
@@ -2962,10 +2962,10 @@ View definition:
    FROM at_view_1 v1;
 
 explain (verbose, costs off) select * from at_view_2;
-                        QUERY PLAN                        
-----------------------------------------------------------
+                             QUERY PLAN                              
+---------------------------------------------------------------------
  Seq Scan on public.at_base_table bt
-   Output: bt.id, bt.stuff, to_json(ROW(bt.id, bt.stuff))
+   Output: bt.id, bt.stuff, to_json(ROW(bt.id, bt.stuff)::at_view_1)
 (2 rows)
 
 select * from at_view_2;
@@ -3002,10 +3002,10 @@ View definition:
    FROM at_view_1 v1;
 
 explain (verbose, costs off) select * from at_view_2;
-                         QUERY PLAN                          
--------------------------------------------------------------
+                               QUERY PLAN                               
+------------------------------------------------------------------------
  Seq Scan on public.at_base_table bt
-   Output: bt.id, bt.stuff, to_json(ROW(bt.id, bt.stuff, 4))
+   Output: bt.id, bt.stuff, to_json(ROW(bt.id, bt.stuff, 4)::at_view_1)
 (2 rows)
 
 select * from at_view_2;
diff --git a/src/test/regress/expected/rowtypes.out b/src/test/regress/expected/rowtypes.out
index c402160d..e41c133d 100644
--- a/src/test/regress/expected/rowtypes.out
+++ b/src/test/regress/expected/rowtypes.out
@@ -1580,3 +1580,84 @@ SELECT expanded_record_field_text(ROW(5, 'field')::expanded_comp, 1),
 (1 row)
 
 DROP TYPE expanded_comp;
+-- Preserve implicitly coerced composite arguments when deparsing calls.
+CREATE TYPE rowtypes_deparse_a AS (a int, b int);
+CREATE TYPE rowtypes_deparse_b AS (a int, b int);
+CREATE FUNCTION rowtypes_deparse_pick(rowtypes_deparse_a) RETURNS int
+  LANGUAGE sql IMMUTABLE AS 'SELECT $1.a';
+CREATE VIEW rowtypes_deparse_named AS
+  SELECT rowtypes_deparse_pick(ROW(1, 2)) AS r;
+-- Adding an overload must not make the stored call ambiguous on recreation.
+CREATE FUNCTION rowtypes_deparse_pick(rowtypes_deparse_b) RETURNS int
+  LANGUAGE sql IMMUTABLE AS 'SELECT $1.b';
+SELECT pg_get_viewdef('rowtypes_deparse_named', false);
+                           pg_get_viewdef                           
+--------------------------------------------------------------------
+  SELECT rowtypes_deparse_pick(ROW(1, 2)::rowtypes_deparse_a) AS r;
+(1 row)
+
+SELECT 'CREATE VIEW rowtypes_deparse_named_plain AS ' ||
+  pg_get_viewdef('rowtypes_deparse_named', false) \gexec
+CREATE VIEW rowtypes_deparse_named_plain AS  SELECT rowtypes_deparse_pick(ROW(1, 2)::rowtypes_deparse_a) AS r;
+SELECT 'CREATE VIEW rowtypes_deparse_named_pretty AS ' ||
+  pg_get_viewdef('rowtypes_deparse_named', true) \gexec
+CREATE VIEW rowtypes_deparse_named_pretty AS  SELECT rowtypes_deparse_pick(ROW(1, 2)::rowtypes_deparse_a) AS r;
+SELECT o.r AS original, n.r AS plain, p.r AS pretty
+  FROM rowtypes_deparse_named o, rowtypes_deparse_named_plain n,
+       rowtypes_deparse_named_pretty p;
+ original | plain | pretty 
+----------+-------+--------
+        1 |     1 |      1
+(1 row)
+
+CREATE FUNCTION rowtypes_deparse_call(rowtypes_deparse_a) RETURNS int
+  LANGUAGE sql IMMUTABLE AS 'SELECT $1.a';
+CREATE VIEW rowtypes_deparse_record AS
+  SELECT rowtypes_deparse_call(ROW(1, 2)) AS r;
+CREATE VIEW rowtypes_deparse_explicit AS
+  SELECT rowtypes_deparse_call(ROW(1, 2)::rowtypes_deparse_a) AS r;
+-- A record overload must not silently replace the original function.
+CREATE FUNCTION rowtypes_deparse_call(record) RETURNS int
+  LANGUAGE plpgsql IMMUTABLE AS 'BEGIN RETURN 99; END';
+SELECT pg_get_viewdef('rowtypes_deparse_record', false);
+                           pg_get_viewdef                           
+--------------------------------------------------------------------
+  SELECT rowtypes_deparse_call(ROW(1, 2)::rowtypes_deparse_a) AS r;
+(1 row)
+
+SELECT 'CREATE VIEW rowtypes_deparse_record_plain AS ' ||
+  pg_get_viewdef('rowtypes_deparse_record', false) \gexec
+CREATE VIEW rowtypes_deparse_record_plain AS  SELECT rowtypes_deparse_call(ROW(1, 2)::rowtypes_deparse_a) AS r;
+SELECT 'CREATE VIEW rowtypes_deparse_record_pretty AS ' ||
+  pg_get_viewdef('rowtypes_deparse_record', true) \gexec
+CREATE VIEW rowtypes_deparse_record_pretty AS  SELECT rowtypes_deparse_call(ROW(1, 2)::rowtypes_deparse_a) AS r;
+SELECT o.r AS original, n.r AS plain, p.r AS pretty
+  FROM rowtypes_deparse_record o, rowtypes_deparse_record_plain n,
+       rowtypes_deparse_record_pretty p;
+ original | plain | pretty 
+----------+-------+--------
+        1 |     1 |      1
+(1 row)
+
+-- Explicit casts and anonymous records keep their existing behaviour.
+SELECT pg_get_viewdef('rowtypes_deparse_explicit', false);
+                           pg_get_viewdef                           
+--------------------------------------------------------------------
+  SELECT rowtypes_deparse_call(ROW(1, 2)::rowtypes_deparse_a) AS r;
+(1 row)
+
+CREATE VIEW rowtypes_deparse_anonymous AS
+  SELECT rowtypes_deparse_call(ROW(1, 2)) AS r;
+SELECT pg_get_viewdef('rowtypes_deparse_anonymous', false);
+                 pg_get_viewdef                 
+------------------------------------------------
+  SELECT rowtypes_deparse_call(ROW(1, 2)) AS r;
+(1 row)
+
+SELECT r FROM rowtypes_deparse_anonymous;
+ r  
+----
+ 99
+(1 row)
+
+-- Leave the objects in place for the pg_upgrade dump/restore test.
diff --git a/src/test/regress/expected/updatable_views.out b/src/test/regress/expected/updatable_views.out
index 58e7960b..b518cf84 100644
--- a/src/test/regress/expected/updatable_views.out
+++ b/src/test/regress/expected/updatable_views.out
@@ -4019,12 +4019,12 @@ select * from uv_iocu_view;
 explain (costs off)
 insert into uv_iocu_view (aa,bb) values (1,'Rejected: (y,1,"(1,y)")')
    on conflict (aa) do select where uv_iocu_view.* = excluded.* returning *;
-                                                           QUERY PLAN                                                            
----------------------------------------------------------------------------------------------------------------------------------
+                                                                         QUERY PLAN                                                                          
+-------------------------------------------------------------------------------------------------------------------------------------------------------------
  Insert on uv_iocu_tab
    Conflict Resolution: SELECT
    Conflict Arbiter Indexes: uv_iocu_tab_a_key
-   Conflict Filter: (ROW(uv_iocu_tab.b, uv_iocu_tab.a, (uv_iocu_tab.*)::text) = ROW(excluded.b, excluded.a, (excluded.*)::text))
+   Conflict Filter: (ROW(uv_iocu_tab.b, uv_iocu_tab.a, (uv_iocu_tab.*)::text)::uv_iocu_view = ROW(excluded.b, excluded.a, (excluded.*)::text)::uv_iocu_view)
    ->  Result
 (5 rows)
 
diff --git a/src/test/regress/sql/rowtypes.sql b/src/test/regress/sql/rowtypes.sql
index 5aef9f5a..643ff781 100644
--- a/src/test/regress/sql/rowtypes.sql
+++ b/src/test/regress/sql/rowtypes.sql
@@ -714,3 +714,52 @@ SELECT expanded_record_field_text(ROW(5, 'field')::expanded_comp, 1),
        expanded_record_field_text(ROW(5, 'field')::expanded_comp, 2);
 
 DROP TYPE expanded_comp;
+
+-- Preserve implicitly coerced composite arguments when deparsing calls.
+CREATE TYPE rowtypes_deparse_a AS (a int, b int);
+CREATE TYPE rowtypes_deparse_b AS (a int, b int);
+
+CREATE FUNCTION rowtypes_deparse_pick(rowtypes_deparse_a) RETURNS int
+  LANGUAGE sql IMMUTABLE AS 'SELECT $1.a';
+CREATE VIEW rowtypes_deparse_named AS
+  SELECT rowtypes_deparse_pick(ROW(1, 2)) AS r;
+
+-- Adding an overload must not make the stored call ambiguous on recreation.
+CREATE FUNCTION rowtypes_deparse_pick(rowtypes_deparse_b) RETURNS int
+  LANGUAGE sql IMMUTABLE AS 'SELECT $1.b';
+SELECT pg_get_viewdef('rowtypes_deparse_named', false);
+SELECT 'CREATE VIEW rowtypes_deparse_named_plain AS ' ||
+  pg_get_viewdef('rowtypes_deparse_named', false) \gexec
+SELECT 'CREATE VIEW rowtypes_deparse_named_pretty AS ' ||
+  pg_get_viewdef('rowtypes_deparse_named', true) \gexec
+SELECT o.r AS original, n.r AS plain, p.r AS pretty
+  FROM rowtypes_deparse_named o, rowtypes_deparse_named_plain n,
+       rowtypes_deparse_named_pretty p;
+
+CREATE FUNCTION rowtypes_deparse_call(rowtypes_deparse_a) RETURNS int
+  LANGUAGE sql IMMUTABLE AS 'SELECT $1.a';
+CREATE VIEW rowtypes_deparse_record AS
+  SELECT rowtypes_deparse_call(ROW(1, 2)) AS r;
+CREATE VIEW rowtypes_deparse_explicit AS
+  SELECT rowtypes_deparse_call(ROW(1, 2)::rowtypes_deparse_a) AS r;
+
+-- A record overload must not silently replace the original function.
+CREATE FUNCTION rowtypes_deparse_call(record) RETURNS int
+  LANGUAGE plpgsql IMMUTABLE AS 'BEGIN RETURN 99; END';
+SELECT pg_get_viewdef('rowtypes_deparse_record', false);
+SELECT 'CREATE VIEW rowtypes_deparse_record_plain AS ' ||
+  pg_get_viewdef('rowtypes_deparse_record', false) \gexec
+SELECT 'CREATE VIEW rowtypes_deparse_record_pretty AS ' ||
+  pg_get_viewdef('rowtypes_deparse_record', true) \gexec
+SELECT o.r AS original, n.r AS plain, p.r AS pretty
+  FROM rowtypes_deparse_record o, rowtypes_deparse_record_plain n,
+       rowtypes_deparse_record_pretty p;
+
+-- Explicit casts and anonymous records keep their existing behaviour.
+SELECT pg_get_viewdef('rowtypes_deparse_explicit', false);
+CREATE VIEW rowtypes_deparse_anonymous AS
+  SELECT rowtypes_deparse_call(ROW(1, 2)) AS r;
+SELECT pg_get_viewdef('rowtypes_deparse_anonymous', false);
+SELECT r FROM rowtypes_deparse_anonymous;
+
+-- Leave the objects in place for the pg_upgrade dump/restore test.
-- 
2.48.1

