Pav*_* V. 14 postgresql function plpgsql
与表或序列不同,无法通过pg_class找到用户定义的函数.上有问题是如何找到的所有功能的列表中删除或授予他们,而是如何找到一个单独的功能(具有已知的名称和参数类型)是不言自明从他们.那么如何找到一个函数是否存在?
编辑:我想以自动方式在函数中使用它.哪种解决方案性能最佳?陷阱错误是非常昂贵的,所以我想对我来说最好的解决方案是没有将错误转换为错误的额外步骤,但我可能在这个假设中错了.
Pav*_*ule 32
是的,您无法找到函数,pg_class
因为函数存储在系统表中pg_proc
postgres-# \df
List of functions
Schema | Name | Result data type | Argument data types | Type
--------+--------------------+------------------+----------------------+--------
public | foo | integer | a integer, b integer | normal
public | function_arguments | text | oid | normal
(2 rows)
Run Code Online (Sandbox Code Playgroud)
基于pg_proc
简单查询自定义函数列表
postgres=# select p.oid::regprocedure
from pg_proc p
join pg_namespace n
on p.pronamespace = n.oid
where n.nspname not in ('pg_catalog', 'information_schema');
oid
-------------------------
foo(integer,integer)
function_arguments(oid)
(2 rows)
Run Code Online (Sandbox Code Playgroud)
对函数存在的最简单和最快速的测试是将(无参数)转换为regproc或regprocedure(带参数):
postgres=# select 'foo'::regproc;
regproc
---------
foo
(1 row)
postgres=# select 'foox'::regproc;
ERROR: function "foox" does not exist
LINE 1: select 'foox'::regproc;
^
postgres=# select 'foo(int, int)'::regprocedure;
regprocedure
----------------------
foo(integer,integer)
(1 row)
postgres=# select 'foo(int, text)'::regprocedure;
ERROR: function "foo(int, text)" does not exist
LINE 1: select 'foo(int, text)'::regprocedure;
^
Run Code Online (Sandbox Code Playgroud)
或者你可以做一些类似的测试 pg_proc
postgres=# select exists(select * from pg_proc where proname = 'foo');
exists
--------
t
(1 row)
postgres=# select exists(select *
from pg_proc
where proname = 'foo'
and function_arguments(oid) = 'integer, integer');
exists
--------
t
(1 row)
Run Code Online (Sandbox Code Playgroud)
哪里:
CREATE OR REPLACE FUNCTION public.function_arguments(oid)
RETURNS text LANGUAGE sql AS $function$
select string_agg(par, ', ')
from (select format_type(unnest(proargtypes), null) par
from pg_proc where oid = $1) x
$function$
Run Code Online (Sandbox Code Playgroud)
或者你可以使用buildin函数:pg_get_function_arguments
用于在系统目录中简单定位的ps技巧.使用psql
选项-E
:
[pavel@localhost ~]$ psql -E postgres
psql (9.2.8, server 9.5devel)
Type "help" for help.
postgres=# \df
********* QUERY **********
SELECT n.nspname as "Schema",
p.proname as "Name",
pg_catalog.pg_get_function_result(p.oid) as "Result data type",
pg_catalog.pg_get_function_arguments(p.oid) as "Argument data types",
CASE
WHEN p.proisagg THEN 'agg'
WHEN p.proiswindow THEN 'window'
WHEN p.prorettype = 'pg_catalog.trigger'::pg_catalog.regtype THEN 'trigger'
ELSE 'normal'
END as "Type"
FROM pg_catalog.pg_proc p
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace
WHERE pg_catalog.pg_function_is_visible(p.oid)
AND n.nspname <> 'pg_catalog'
AND n.nspname <> 'information_schema'
ORDER BY 1, 2, 4;
**************************
List of functions
Schema | Name | Result data type | Argument data types | Type
--------+--------------------+------------------+----------------------+--------
public | foo | integer | a integer, b integer | normal
public | function_arguments | text | oid | normal
(2 rows)
Run Code Online (Sandbox Code Playgroud)
a_h*_*ame 11
我认为最简单的方法是使用pg_get_functiondef()
.
如果它返回一些东西,则该函数存在,否则该函数不存在:
select pg_get_functiondef('some_function()'::regprocedure);
select pg_get_functiondef('some_function(integer)'::regprocedure);
Run Code Online (Sandbox Code Playgroud)
缺点是如果函数不存在,它会产生错误,而不是简单地返回一个空结果。但这可以通过编写一个 PL/pgSQL 函数来捕获异常并返回 false 来克服。
归档时间: |
|
查看次数: |
19523 次 |
最近记录: |