如何在postgresql打印函数和触发器源代码?请让我知道,如果有人知道查询显示的功能,触发器源代码。


当前回答

此外,对于@franc的答案,您可以使用SQL接口:

select 
    prosrc
from pg_trigger, pg_proc
where
 pg_proc.oid=pg_trigger.tgfoid
 and pg_trigger.tgname like '<name>'

(从这里取:http://www.postgresql.org/message-id/Pine.BSF.4.10.10009140858080.28013-100000@megazone23.bigpanda.com)

其他回答

\df+在PSQL给你源代码。

功能:

查询pg_proc视图,如下所示

select proname,prosrc from pg_proc where proname= your_function_name; 

另一种方法是只执行公共的\df和\ef,它们可以列出函数。

skytf=> \df           
                                             List of functions
 Schema |         Name         | Result data type |              Argument data types               |  Type  
--------+----------------------+------------------+------------------------------------------------+--------
 public | pg_buffercache_pages | SETOF record     |                                                | normal


skytf=> \ef  pg_buffercache_pages

它将显示函数的源代码。

触发器:

我不知道是否有一个直接的方法来获得源代码。只要知道下面的方法,也许会对你有所帮助!

步骤1:获取触发器的表oid:

    skytf=> select tgrelid from pg_trigger  where tgname='insert_tbl_tmp_trigger';
      tgrelid
    ---------
       26599
    (1 row)

步骤2:获取上述oid的表名!

    skytf=> select oid,relname  from pg_class where oid=26599;
      oid  |           relname           
    -------+-----------------------------
     26599 | tbl_tmp
    (1 row)

步骤3:列出表信息

    skytf=> \d tbl_tmp

它将向您显示表触发器的详细信息。触发器通常使用函数。因此,您可以获得触发器函数的源代码,就像上面我指出的那样!

自版本:psql (9.6.17, server 11.6)

我已经尝试了以上所有的答案,但对我来说

postgres=> \sf jsonb_extract_path_text
CREATE OR REPLACE FUNCTION pg_catalog.jsonb_extract_path_text(from_json jsonb, VARIADIC path_elems text[])
 RETURNS text
 LANGUAGE internal
 IMMUTABLE PARALLEL SAFE STRICT
AS $function$jsonb_extract_path_text$function$



postgres=> \df+
ERROR:  column p.proisagg does not exist
LINE 6:   WHEN p.proisagg THEN 'agg'
               ^
HINT:  Perhaps you meant to reference the column "p.prolang".

Df似乎不适合我。

不仅仅是显示函数,还可以获得就地编辑功能。

\ef <function_name>非常方便。它将以可编辑的格式打开函数的源代码。 您不仅可以查看它,还可以编辑和执行它。

只有\ef而没有function_name将打开可编辑的CREATE FUNCTION模板。

进一步参考-> https://www.postgresql.org/docs/9.6/static/app-psql.html

下面是一些来自PostgreSQL-9.5的例子

显示列表:

功能:\ df + 触发器:\dy+

显示的定义:

postgres=# \sf
function name is required

postgres=# \sf pg_reload_conf()
CREATE OR REPLACE FUNCTION pg_catalog.pg_reload_conf()
 RETURNS boolean
 LANGUAGE internal
 STRICT
AS $function$pg_reload_conf$function$

postgres=# \sf pg_encoding_to_char
CREATE OR REPLACE FUNCTION pg_catalog.pg_encoding_to_char(integer)
 RETURNS name
 LANGUAGE internal
 STABLE STRICT
AS $function$PG_encoding_to_char$function$