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