a21a05
diff -u -r postgresql-9.2.24_orig/src/backend/catalog/namespace.c postgresql-9.2.24/src/backend/catalog/namespace.c
a21a05
--- postgresql-9.2.24_orig/src/backend/catalog/namespace.c	2020-07-07 12:40:46.051801024 +0200
a21a05
+++ postgresql-9.2.24/src/backend/catalog/namespace.c	2020-07-07 12:41:06.373887594 +0200
a21a05
@@ -740,13 +740,23 @@
a21a05
 
a21a05
 /*
a21a05
  * TypenameGetTypid
a21a05
+ *		Wrapper for binary compatibility.
a21a05
+ */
a21a05
+Oid
a21a05
+TypenameGetTypid(const char *typname)
a21a05
+{
a21a05
+	return TypenameGetTypidExtended(typname, true);
a21a05
+}
a21a05
+
a21a05
+/*
a21a05
+ * TypenameGetTypidExtended
a21a05
  *		Try to resolve an unqualified datatype name.
a21a05
  *		Returns OID if type found in search path, else InvalidOid.
a21a05
  *
a21a05
  * This is essentially the same as RelnameGetRelid.
a21a05
  */
a21a05
 Oid
a21a05
-TypenameGetTypid(const char *typname)
a21a05
+TypenameGetTypidExtended(const char *typname, bool temp_ok)
a21a05
 {
a21a05
 	Oid			typid;
a21a05
 	ListCell   *l;
a21a05
@@ -757,6 +767,9 @@
a21a05
 	{
a21a05
 		Oid			namespaceId = lfirst_oid(l);
a21a05
 
a21a05
+		if (!temp_ok && namespaceId == myTempNamespace)
a21a05
+			continue;			/* do not look in temp namespace */
a21a05
+
a21a05
 		typid = GetSysCacheOid2(TYPENAMENSP,
a21a05
 								PointerGetDatum(typname),
a21a05
 								ObjectIdGetDatum(namespaceId));
a21a05
diff -u -r postgresql-9.2.24_orig/src/backend/parser/parse_func.c postgresql-9.2.24/src/backend/parser/parse_func.c
a21a05
--- postgresql-9.2.24_orig/src/backend/parser/parse_func.c	2020-07-07 12:40:46.010800850 +0200
a21a05
+++ postgresql-9.2.24/src/backend/parser/parse_func.c	2020-07-07 12:41:06.375887602 +0200
a21a05
@@ -1377,7 +1377,12 @@
a21a05
 	Oid			result;
a21a05
 	Type		typtup;
a21a05
 
a21a05
-	typtup = LookupTypeName(NULL, makeTypeNameFromNameList(funcname), NULL);
a21a05
+	/*
a21a05
+	 * temp_ok=false protects the <refsect1 id="sql-createfunction-security">
a21a05
+	 * contract for writing SECURITY DEFINER functions safely.
a21a05
+	 */
a21a05
+	typtup = LookupTypeNameExtended(NULL, makeTypeNameFromNameList(funcname),
a21a05
+									NULL, false, false);
a21a05
 	if (typtup == NULL)
a21a05
 		return InvalidOid;
a21a05
 
a21a05
diff -u -r postgresql-9.2.24_orig/src/backend/parser/parse_type.c postgresql-9.2.24/src/backend/parser/parse_type.c
a21a05
--- postgresql-9.2.24_orig/src/backend/parser/parse_type.c	2020-07-07 12:40:46.013800862 +0200
a21a05
+++ postgresql-9.2.24/src/backend/parser/parse_type.c	2020-07-07 12:44:09.366660007 +0200
a21a05
@@ -33,6 +33,18 @@
a21a05
 
a21a05
 /*
a21a05
  * LookupTypeName
a21a05
+ *		Wrapper for typical case.
a21a05
+ */
a21a05
+Type
a21a05
+LookupTypeName(ParseState *pstate, const TypeName *typeName,
a21a05
+			   int32 *typmod_p)
a21a05
+{
a21a05
+	return LookupTypeNameExtended(pstate,
a21a05
+								  typeName, typmod_p, true, false);
a21a05
+}
a21a05
+
a21a05
+/*
a21a05
+ * LookupTypeNameExtended
a21a05
  *		Given a TypeName object, lookup the pg_type syscache entry of the type.
a21a05
  *		Returns NULL if no such type can be found.  If the type is found,
a21a05
  *		the typmod value represented in the TypeName struct is computed and
a21a05
@@ -51,11 +63,17 @@
a21a05
  * found but is a shell, and there is typmod decoration, an error will be
a21a05
  * thrown --- this is intentional.
a21a05
  *
a21a05
+ * If temp_ok is false, ignore types in the temporary namespace.  Pass false
a21a05
+ * when the caller will decide, using goodness of fit criteria, whether the
a21a05
+ * typeName is actually a type or something else.  If typeName always denotes
a21a05
+ * a type (or denotes nothing), pass true.
a21a05
+ *
a21a05
  * pstate is only used for error location info, and may be NULL.
a21a05
  */
a21a05
 Type
a21a05
-LookupTypeName(ParseState *pstate, const TypeName *typeName,
a21a05
-			   int32 *typmod_p)
a21a05
+LookupTypeNameExtended(ParseState *pstate,
a21a05
+					   const TypeName *typeName, int32 *typmod_p,
a21a05
+					   bool temp_ok, bool missing_ok)
a21a05
 {
a21a05
 	Oid			typoid;
a21a05
 	HeapTuple	tup;
a21a05
@@ -156,7 +174,7 @@
a21a05
 		else
a21a05
 		{
a21a05
 			/* Unqualified type name, so search the search path */
a21a05
-			typoid = TypenameGetTypid(typname);
a21a05
+			typoid = TypenameGetTypidExtended(typname, temp_ok);
a21a05
 		}
a21a05
 
a21a05
 		/* If an array reference, return the array type instead */
a21a05
diff -u -r postgresql-9.2.24_orig/src/backend/utils/adt/ruleutils.c postgresql-9.2.24/src/backend/utils/adt/ruleutils.c
a21a05
--- postgresql-9.2.24_orig/src/backend/utils/adt/ruleutils.c	2020-07-07 12:40:46.022800901 +0200
a21a05
+++ postgresql-9.2.24/src/backend/utils/adt/ruleutils.c	2020-07-07 12:41:06.376887607 +0200
a21a05
@@ -6421,6 +6421,14 @@
a21a05
 		if (!PRETTY_PAREN(context))
a21a05
 			appendStringInfoChar(buf, ')');
a21a05
 	}
a21a05
+
a21a05
+	/*
a21a05
+	 * Never emit resulttype(arg) functional notation. A pg_proc entry could
a21a05
+	 * take precedence, and a resulttype in pg_temp would require schema
a21a05
+	 * qualification that format_type_with_typemod() would usually omit. We've
a21a05
+	 * standardized on arg::resulttype, but CAST(arg AS resulttype) notation
a21a05
+	 * would work fine.
a21a05
+	 */
a21a05
 	appendStringInfo(buf, "::%s",
a21a05
 					 format_type_with_typemod(resulttype, resulttypmod));
a21a05
 }
a21a05
diff -u -r postgresql-9.2.24_orig/src/include/catalog/namespace.h postgresql-9.2.24/src/include/catalog/namespace.h
a21a05
--- postgresql-9.2.24_orig/src/include/catalog/namespace.h	2020-07-07 12:40:45.955800615 +0200
a21a05
+++ postgresql-9.2.24/src/include/catalog/namespace.h	2020-07-07 12:41:06.376887607 +0200
a21a05
@@ -66,6 +66,7 @@
a21a05
 extern bool RelationIsVisible(Oid relid);
a21a05
 
a21a05
 extern Oid	TypenameGetTypid(const char *typname);
a21a05
+extern Oid	TypenameGetTypidExtended(const char *typname, bool temp_ok);
a21a05
 extern bool TypeIsVisible(Oid typid);
a21a05
 
a21a05
 extern FuncCandidateList FuncnameGetCandidates(List *names,
a21a05
diff -u -r postgresql-9.2.24_orig/src/include/parser/parse_type.h postgresql-9.2.24/src/include/parser/parse_type.h
a21a05
--- postgresql-9.2.24_orig/src/include/parser/parse_type.h	2020-07-07 12:40:45.944800568 +0200
a21a05
+++ postgresql-9.2.24/src/include/parser/parse_type.h	2020-07-07 13:29:21.426589374 +0200
a21a05
@@ -21,6 +21,9 @@
a21a05
 
a21a05
 extern Type LookupTypeName(ParseState *pstate, const TypeName *typeName,
a21a05
 			   int32 *typmod_p);
a21a05
+extern Type LookupTypeNameExtended(ParseState *pstate,
a21a05
+								   const TypeName *typeName, int32 *typmod_p,
a21a05
+								   bool temp_ok, bool missing_ok);
a21a05
 extern Type typenameType(ParseState *pstate, const TypeName *typeName,
a21a05
 			 int32 *typmod_p);
a21a05
 extern Oid	typenameTypeId(ParseState *pstate, const TypeName *typeName);
a21a05
diff -u -r postgresql-9.2.24_orig/src/test/regress/expected/temp.out postgresql-9.2.24/src/test/regress/expected/temp.out
a21a05
--- postgresql-9.2.24_orig/src/test/regress/expected/temp.out	2020-07-07 12:40:45.975800701 +0200
a21a05
+++ postgresql-9.2.24/src/test/regress/expected/temp.out	2020-07-07 12:41:06.377887611 +0200
a21a05
@@ -201,3 +201,18 @@
a21a05
 (1 row)
a21a05
 
a21a05
 drop table public.whereami;
a21a05
+-- types in temp schema
a21a05
+set search_path = pg_temp, public;
a21a05
+create domain pg_temp.nonempty as text check (value <> '');
a21a05
+-- function-syntax invocation of types matches rules for functions
a21a05
+select nonempty('');
a21a05
+ERROR:  function nonempty(unknown) does not exist
a21a05
+LINE 1: select nonempty('');
a21a05
+               ^
a21a05
+HINT:  No function matches the given name and argument types. You might need to add explicit type casts.
a21a05
+select pg_temp.nonempty('');
a21a05
+ERROR:  value for domain nonempty violates check constraint "nonempty_check"
a21a05
+-- other syntax matches rules for tables
a21a05
+select ''::nonempty;
a21a05
+ERROR:  value for domain nonempty violates check constraint "nonempty_check"
a21a05
+reset search_path;
a21a05
diff -u -r postgresql-9.2.24_orig/src/test/regress/sql/temp.sql postgresql-9.2.24/src/test/regress/sql/temp.sql
a21a05
--- postgresql-9.2.24_orig/src/test/regress/sql/temp.sql	2020-07-07 12:40:45.970800679 +0200
a21a05
+++ postgresql-9.2.24/src/test/regress/sql/temp.sql	2020-07-07 12:41:06.377887611 +0200
a21a05
@@ -151,3 +151,14 @@
a21a05
 select pg_temp.whoami();
a21a05
 
a21a05
 drop table public.whereami;
a21a05
+
a21a05
+-- types in temp schema
a21a05
+set search_path = pg_temp, public;
a21a05
+create domain pg_temp.nonempty as text check (value <> '');
a21a05
+-- function-syntax invocation of types matches rules for functions
a21a05
+select nonempty('');
a21a05
+select pg_temp.nonempty('');
a21a05
+-- other syntax matches rules for tables
a21a05
+select ''::nonempty;
a21a05
+
a21a05
+reset search_path;