简体   繁体   English

Postgres 获取所有用户定义的触发函数的列表

[英]Postgres get a list of all user defined trigger functions

I want to achieve a sql query to make a list of all user defined trigger functions.我想实现一个 sql 查询来列出所有用户定义的触发函数。 Like the list from pg_admin below.就像下面 pg_admin 的列表一样。

在此处输入图像描述

When I call:当我打电话时:

SELECT proname, prorettype,proowner from pg_proc WHERE  prorettype = 2279;

I also get predefined functions我也得到了预定义的函数

在此处输入图像描述

I hope you can help me我希望你能帮帮我

If you want to select all functions and leave out the ones created by the system, you can add a JOIN with the table pg_user and select the functions that were not created by postgres :如果你想 select 所有函数并省略系统创建的函数,你可以添加一个JOINpg_user和 select 不是由postgres创建的函数:

SELECT p.oid,proname,prosrc,u.usename
FROM  pg_proc p  
JOIN  pg_user u ON u.usesysid = p.proowner  
WHERE usename <> 'postgres' AND prorettype = 2279;

Alternatively, you could also specify a user (or many) to the where clause within a IN expression:或者,您还可以为IN表达式中的 where 子句指定一个用户(或多个用户):

WHERE usename IN ('user1','user2')

EDIT : see comments.编辑:见评论。 If the sysadmin created functions using the user postgres I would suggest to change the owner to another user.如果系统管理员使用用户postgres创建函数,我建议将所有者更改为另一个用户。

ALTER TABLE public.mytable OWNER TO myuser;

If for some reason ALTER TABLE is not possible, try selecting by language (sql, plpgsql, etc) or just filter out the functions containing the language internal :如果由于某种原因无法使用ALTER TABLE ,请尝试按语言(sql、plpgsql 等)进行选择,或者只过滤掉包含语言internal的函数:

SELECT p.oid,proname,prosrc,u.usename
FROM  pg_proc p  
JOIN  pg_user u ON u.usesysid = p.proowner  
WHERE prolang = 13436 AND prorettype = 2279;

Or maybe..或者可能..

SELECT p.oid,proname,prosrc,u.usename
FROM  pg_proc p  
JOIN  pg_user u ON u.usesysid = p.proowner  
WHERE prolang <> 12 AND prorettype = 2279; --12=internal

See table pg_language to get the oid of your function's language, eg of my instance:请参阅表pg_language以获取函数语言的 oid,例如我的实例:

SELECT oid, lanname FROM pg_language;

  oid  | lanname  
-------+----------
    12 | internal
    13 | c
    14 | sql
 13436 | plpgsql

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM