From 2282f523e3b266d82666deb0689371c3dffaf144 Mon Sep 17 00:00:00 2001 From: Ewan Young Date: Mon, 31 Aug 2026 21:51:42 +0800 Subject: [PATCH] Don't create a shell type for a function returning SETOF compute_return_type() creates a shell type when a C function's return type doesn't exist yet, so that input functions for a new type can be defined. But a type input function never returns a set, so if the return type says SETOF, this cannot be an input function definition. A typo in "RETURNS SETOF sometype" would silently leave a shell type behind, which persists even after dropping the function. Refuse shell type creation in that case, like we already do for array syntax, %TYPE and typmod decoration; the user now gets "type does not exist". --- src/backend/commands/functioncmds.c | 2 ++ src/test/regress/expected/create_type.out | 7 +++++++ src/test/regress/sql/create_type.sql | 7 +++++++ 3 files changed, 16 insertions(+) diff --git a/src/backend/commands/functioncmds.c b/src/backend/commands/functioncmds.c index 4d46013d7de..ed3068d0760 100644 --- a/src/backend/commands/functioncmds.c +++ b/src/backend/commands/functioncmds.c @@ -101,12 +101,14 @@ compute_return_type(TypeName *returnType, Oid languageOid, * If the type name contains any modifiers like %TYPE, type[] array * syntax, or typmod decoration, it's not an input function, or at least * not one for which we'd want to automatically create a shell type. + * Likewise, an input function never returns a set. * * Only C-coded functions can be I/O functions. We enforce this * restriction here mainly to prevent littering the catalogs with shell * types due to simple typos in user-defined function definitions. */ attempt_shell_creation = + !returnType->setof && !returnType->pct_type && returnType->arrayBounds == NULL && returnType->typmods == NIL && (languageOid == INTERNALlanguageId || languageOid == ClanguageId); diff --git a/src/test/regress/expected/create_type.out b/src/test/regress/expected/create_type.out index 09cfa8da22c..26652de0a86 100644 --- a/src/test/regress/expected/create_type.out +++ b/src/test/regress/expected/create_type.out @@ -62,6 +62,13 @@ CREATE FUNCTION bogus_in(cstring) AS :'regresslib', 'widget_in' LANGUAGE C STRICT IMMUTABLE; ERROR: type "bogus_shell[]" does not exist +-- If the function returns a set, it's not an input function, +-- so don't create a shell type +CREATE FUNCTION bogus_in(cstring) + RETURNS SETOF bogus_shell + AS :'regresslib', 'widget_in' + LANGUAGE C STRICT IMMUTABLE; +ERROR: type "bogus_shell" does not exist -- If the column specified with %TYPE does not exist, don't try to create a shell type CREATE TEMP TABLE bogus_tbl (col int); CREATE FUNCTION bogus_in(cstring) diff --git a/src/test/regress/sql/create_type.sql b/src/test/regress/sql/create_type.sql index 4fd1facdffe..0ad6cc9c929 100644 --- a/src/test/regress/sql/create_type.sql +++ b/src/test/regress/sql/create_type.sql @@ -62,6 +62,13 @@ CREATE FUNCTION bogus_in(cstring) AS :'regresslib', 'widget_in' LANGUAGE C STRICT IMMUTABLE; +-- If the function returns a set, it's not an input function, +-- so don't create a shell type +CREATE FUNCTION bogus_in(cstring) + RETURNS SETOF bogus_shell + AS :'regresslib', 'widget_in' + LANGUAGE C STRICT IMMUTABLE; + -- If the column specified with %TYPE does not exist, don't try to create a shell type CREATE TEMP TABLE bogus_tbl (col int); CREATE FUNCTION bogus_in(cstring) -- 2.47.3