From ec5eb86eae133d4889f99f8150e3fc6a966d77a2 Mon Sep 17 00:00:00 2001
From: xnervwang <xnervwang@gmail.com>
Date: Wed, 25 Nov 2026 23:36:22 +0000
Subject: [PATCH v3] Fix stale type lookups in pg_input_is_valid_common()

pg_input_is_valid_common() reused the first type name lookup when
get_fn_expr_arg_stable() returned true. That includes external Params,
which PL/pgSQL uses to pass variables.

PL/pgSQL simple expressions retain fn_extra across evaluations within a
transaction. As a result, pg_input_is_valid() and pg_input_error_info()
in a loop kept using the first iteration's type and typmod. A cached
lookup for a constant name could be stale after a search_path change or
after dropping and recreating the type under the same name.

Parse the type name on every call. Cache only the resolved type's input
function, and reuse it only while the type OID stays the same.

Add tests in plpgsql_simple.

Oversight in commit 1939d26282b.
---
 src/backend/utils/adt/misc.c                  | 44 ++++++-------
 .../plpgsql/src/expected/plpgsql_simple.out   | 63 +++++++++++++++++++
 src/pl/plpgsql/src/sql/plpgsql_simple.sql     | 52 +++++++++++++++
 3 files changed, 133 insertions(+), 26 deletions(-)

diff --git a/src/backend/utils/adt/misc.c b/src/backend/utils/adt/misc.c
index 5d1c0e0a7bf..add9ae29d11 100644
--- a/src/backend/utils/adt/misc.c
+++ b/src/backend/utils/adt/misc.c
@@ -57,7 +57,6 @@ typedef struct ValidIOData
 {
 	Oid			typoid;
 	int32		typmod;
-	bool		typname_constant;
 	Oid			typiofunc;
 	Oid			typioparam;
 	FmgrInfo	inputproc;
@@ -740,12 +739,16 @@ pg_input_is_valid_common(FunctionCallInfo fcinfo,
 						 ErrorSaveContext *escontext)
 {
 	char	   *str = text_to_cstring(txt);
+	char	   *typnamestr;
 	ValidIOData *my_extra;
+	Oid			typoid;
+	int32		typmod;
 	Datum		converted;
 
 	/*
-	 * We arrange to look up the needed I/O info just once per series of
-	 * calls, assuming the data type doesn't change underneath us.
+	 * Look up the type name on every call, since the same name can resolve
+	 * to a different type after DDL or a search_path change.  The input
+	 * function is looked up again only when the resolved type changes.
 	 */
 	my_extra = (ValidIOData *) fcinfo->flinfo->fn_extra;
 	if (my_extra == NULL)
@@ -755,33 +758,22 @@ pg_input_is_valid_common(FunctionCallInfo fcinfo,
 							   sizeof(ValidIOData));
 		my_extra = (ValidIOData *) fcinfo->flinfo->fn_extra;
 		my_extra->typoid = InvalidOid;
-		/* Detect whether typname argument is constant. */
-		my_extra->typname_constant = get_fn_expr_arg_stable(fcinfo->flinfo, 1);
 	}
 
-	/*
-	 * If the typname argument is constant, we only need to parse it the first
-	 * time through.
-	 */
-	if (my_extra->typoid == InvalidOid || !my_extra->typname_constant)
-	{
-		char	   *typnamestr = text_to_cstring(typname);
-		Oid			typoid;
-
-		/* Parse type-name argument to obtain type OID and encoded typmod. */
-		(void) parseTypeString(typnamestr, &typoid, &my_extra->typmod, NULL);
+	typnamestr = text_to_cstring(typname);
+	(void) parseTypeString(typnamestr, &typoid, &typmod, NULL);
 
-		/* Update type-specific info if typoid changed. */
-		if (my_extra->typoid != typoid)
-		{
-			getTypeInputInfo(typoid,
-							 &my_extra->typiofunc,
-							 &my_extra->typioparam);
-			fmgr_info_cxt(my_extra->typiofunc, &my_extra->inputproc,
-						  fcinfo->flinfo->fn_mcxt);
-			my_extra->typoid = typoid;
-		}
+	if (my_extra->typoid != typoid)
+	{
+		my_extra->typoid = InvalidOid;
+		getTypeInputInfo(typoid,
+						 &my_extra->typiofunc,
+						 &my_extra->typioparam);
+		fmgr_info_cxt(my_extra->typiofunc, &my_extra->inputproc,
+					  fcinfo->flinfo->fn_mcxt);
+		my_extra->typoid = typoid;
 	}
+	my_extra->typmod = typmod;
 
 	/* Now we can try to perform the conversion. */
 	return InputFunctionCallSafe(&my_extra->inputproc,
diff --git a/src/pl/plpgsql/src/expected/plpgsql_simple.out b/src/pl/plpgsql/src/expected/plpgsql_simple.out
index ccf15ea2200..0f2e10159f3 100644
--- a/src/pl/plpgsql/src/expected/plpgsql_simple.out
+++ b/src/pl/plpgsql/src/expected/plpgsql_simple.out
@@ -148,3 +148,66 @@ end $$;
 ERROR:  bigint out of range
 CONTEXT:  SQL statement "select x*x*x"
 PL/pgSQL function inline_code_block line 5 at SQL statement
+-- Check case where the type name passed to pg_input_is_valid() and
+-- pg_input_error_info() changes between evaluations
+do $$
+declare r record;
+begin
+  for r in select * from (values ('42', 'integer'), ('hello', 'text'),
+                                 ('100000', 'smallint'),
+                                 ('abcdefg', 'varchar(5)'),
+                                 ('abcdefg', 'varchar(10)'),
+                                 ('abcdefghijk', 'varchar(10)')) as v(val, typ) loop
+    raise notice '% as %: % %', r.val, r.typ, pg_input_is_valid(r.val, r.typ),
+      (pg_input_error_info(r.val, r.typ)).message;
+  end loop;
+end $$;
+NOTICE:  42 as integer: t <NULL>
+NOTICE:  hello as text: t <NULL>
+NOTICE:  100000 as smallint: f value "100000" is out of range for type smallint
+NOTICE:  abcdefg as varchar(5): f value too long for type character varying(5)
+NOTICE:  abcdefg as varchar(10): t <NULL>
+NOTICE:  abcdefghijk as varchar(10): f value too long for type character varying(10)
+-- Check that a type name is resolved again when the type is dropped and
+-- recreated, or when search_path changes
+create domain input_check_dom as int;
+do $$
+declare typ text := 'input_check_dom';
+begin
+  for i in 1..2 loop
+    if i = 2 then
+      drop domain input_check_dom;
+      create domain input_check_dom as text;
+    end if;
+    raise notice 'constant: %, variable: %',
+      pg_input_is_valid('abc', 'input_check_dom'),
+      pg_input_is_valid('abc', typ);
+  end loop;
+end $$;
+NOTICE:  constant: f, variable: f
+NOTICE:  constant: t, variable: t
+drop domain input_check_dom;
+create schema input_check_s1;
+create schema input_check_s2;
+create domain input_check_s1.dom as int;
+create domain input_check_s2.dom as text;
+do $$
+declare
+  c refcursor;
+  v bool;
+begin
+  perform set_config('search_path', 'input_check_s1', true);
+  open c for select pg_input_is_valid('abc', 'dom') from generate_series(1, 2);
+  fetch c into v;
+  raise notice 'input_check_s1: %', v;
+  perform set_config('search_path', 'input_check_s2', true);
+  fetch c into v;
+  raise notice 'input_check_s2: %', v;
+  close c;
+end $$;
+NOTICE:  input_check_s1: f
+NOTICE:  input_check_s2: t
+drop schema input_check_s1, input_check_s2 cascade;
+NOTICE:  drop cascades to 2 other objects
+DETAIL:  drop cascades to type input_check_s2.dom
+drop cascades to type input_check_s1.dom
diff --git a/src/pl/plpgsql/src/sql/plpgsql_simple.sql b/src/pl/plpgsql/src/sql/plpgsql_simple.sql
index d64e791800b..3ae83265c69 100644
--- a/src/pl/plpgsql/src/sql/plpgsql_simple.sql
+++ b/src/pl/plpgsql/src/sql/plpgsql_simple.sql
@@ -131,3 +131,55 @@ begin
   -- overflow during expression evaluation step does get an extra context line
   select x*x*x into y;
 end $$;
+
+-- Check case where the type name passed to pg_input_is_valid() and
+-- pg_input_error_info() changes between evaluations
+do $$
+declare r record;
+begin
+  for r in select * from (values ('42', 'integer'), ('hello', 'text'),
+                                 ('100000', 'smallint'),
+                                 ('abcdefg', 'varchar(5)'),
+                                 ('abcdefg', 'varchar(10)'),
+                                 ('abcdefghijk', 'varchar(10)')) as v(val, typ) loop
+    raise notice '% as %: % %', r.val, r.typ, pg_input_is_valid(r.val, r.typ),
+      (pg_input_error_info(r.val, r.typ)).message;
+  end loop;
+end $$;
+
+-- Check that a type name is resolved again when the type is dropped and
+-- recreated, or when search_path changes
+create domain input_check_dom as int;
+do $$
+declare typ text := 'input_check_dom';
+begin
+  for i in 1..2 loop
+    if i = 2 then
+      drop domain input_check_dom;
+      create domain input_check_dom as text;
+    end if;
+    raise notice 'constant: %, variable: %',
+      pg_input_is_valid('abc', 'input_check_dom'),
+      pg_input_is_valid('abc', typ);
+  end loop;
+end $$;
+drop domain input_check_dom;
+create schema input_check_s1;
+create schema input_check_s2;
+create domain input_check_s1.dom as int;
+create domain input_check_s2.dom as text;
+do $$
+declare
+  c refcursor;
+  v bool;
+begin
+  perform set_config('search_path', 'input_check_s1', true);
+  open c for select pg_input_is_valid('abc', 'dom') from generate_series(1, 2);
+  fetch c into v;
+  raise notice 'input_check_s1: %', v;
+  perform set_config('search_path', 'input_check_s2', true);
+  fetch c into v;
+  raise notice 'input_check_s2: %', v;
+  close c;
+end $$;
+drop schema input_check_s1, input_check_s2 cascade;
-- 
2.47.3

