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


当前回答

下面是一些来自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$

其他回答

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

\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$

自版本: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似乎不适合我。

有很多可能性。最简单的方法是使用pgAdmin并从SQL窗口获取。然而,如果你想通过编程方式获得这些,那么检查pg_proc和pg_trigger系统目录或例程,并从信息模式中触发视图(这是SQL的标准方式,但它可能不包括所有特性,特别是特定于postgresql的特性)。例如:

SELECT
    routine_definition 
FROM
    information_schema.routines 
WHERE
    specific_schema LIKE 'public'
    AND routine_name LIKE 'functionName';

\sf function_name在PSQL中产生单个函数的可编辑源代码。

从https://www.postgresql.org/docs/9.6/static/app-psql.html:

\ [+] function_description瑞士法郎 该命令以CREATE或REPLACE function命令的形式获取并显示命名函数的定义。 如果将+追加到命令名后,则输出行编号,函数体的第一行为第1行。