如何使用自定义枚举输入调用此 Postgres 函数?

How do I call this Postgres function with custom enum input?

我原以为这很简单,但我尝试了多种组合,但无法让 PostgreSQL 接受它们。它声称没有匹配的函数,但列出的类型(如下例所示)与我定义的函数明显匹配。

                                     List of data types
 Schema |  Name  | Internal name | Size | Elements | Access privileges | Description
--------+--------+---------------+------+----------+-------------------+-------------
 public | levels | levels        | 4    | debug   +|                   |
        |        |               |      | info    +|                   |
        |        |               |      | warn    +|                   |
        |        |               |      | critical |                   |

CREATE FUNCTION public."logEvent"(IN in_type text,IN in_priority public.levels,IN in_message text)
    RETURNS void
    LANGUAGE 'sql'
    NOT LEAKPROOF 
AS $function$
INSERT INTO public.log (type,priority,message) VALUES (in_type, in_priority, in_message);
$function$;

失败的查询:

SELECT 1 FROM public.logEvent('test'::text,'debug'::public.levels,'test from sql prompt'::text);
ERROR:  function public.logevent(text, levels, text) does not exist
LINE 1: SELECT 1 FROM public.logEvent('test'::text,'debug'::public.l...
                      ^
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.

替代函数定义:我在尝试执行此创建函数调用时遇到同样的错误:

CREATE FUNCTION public."logEvent"(IN in_type text, IN in_priority text, IN in_message text)
    RETURNS boolean
    LANGUAGE 'sql'
    NOT LEAKPROOF
AS $function$
SELECT public.logEvent( in_type, CAST(lower(in_priority) AS public.levels), in_message);
$function$;

您知道双引号标识符区分大小写吗?

  • Are PostgreSQL column names case-sensitive?
CREATE FUNCTION public.<b>"logEvent"</b>

但是:

SELECT 1 FROM public.<b>logEvent</b>

这应该有效:

SELECT 1 FROM public."logEvent"('test', 'debug', 'test from sql prompt');

仅当重载函数存在歧义时才需要显式类型转换。

要么在 "logEvent" 的其余部分保留双引号,要么(更聪明地)使用不带引号(实际上是小写)的合法标识符。