如何显示函数或过程的完整定义?

发布于 2024-09-14 23:05:02 字数 104 浏览 13 评论 0 原文

如何查看存储函数或过程?

假设我有一个没有原始定义的旧函数 - 我想看看它在做什么,但我似乎无法找到一种方法来做到这一点。

使用 Postgres 版本 8.4.1。

How do you view a stored function or procedure?

Say I have an old function without the original definition - I want to see what it is doing, but I can't seem to figure out a way to do that.

Using Postgres version 8.4.1.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

世界和平 2024-09-21 23:05:02

\df+ <function_name> in psql.

紫竹語嫣☆ 2024-09-21 23:05:02

psql 中的 \ef 。它将为整个功能提供可编辑的文本。

\ef <function_name> in psql. It will give the whole function with editable text.

欢烬 2024-09-21 23:05:02
SELECT prosrc FROM pg_proc WHERE proname = 'function_name';

这告诉函数处理程序如何调用该函数。它可能是解释语言函数的实际源代码、链接符号、文件名或其他任何内容,具体取决于实现语言/调用约定

SELECT prosrc FROM pg_proc WHERE proname = 'function_name';

This tells the function handler how to invoke the function. It might be the actual source code of the function for interpreted languages, a link symbol, a file name, or just about anything else, depending on the implementation language/call convention

十级心震 2024-09-21 23:05:02

使用 pgAdmin 或使用 pg_proc 获取您的存储过程。 pgAdmin 也做同样的事情。

use pgAdmin or use pg_proc to get the source of your stored procedures. pgAdmin does the same.

你的心境我的脸 2024-09-21 23:05:02

使用 \df 列出 Postgres 中的所有存储过程。

Use \df to list all the stored procedure in Postgres.

青巷忧颜 2024-09-21 23:05:02

如果有人想知道如何快速查询目录表并使用 pg_get_functiondef() 函数,这里是示例查询:

SELECT n.nspname AS schema
      ,proname AS fname
      ,proargnames AS args
      ,t.typname AS return_type
      ,d.description
      ,pg_get_functiondef(p.oid) as definition
--      ,CASE WHEN NOT p.proisagg THEN pg_get_functiondef(p.oid)
--            ELSE 'pg_get_functiondef() can''t be used with aggregate functions'
--       END as definition
  FROM pg_proc p
  JOIN pg_type t
    ON p.prorettype = t.oid
  LEFT OUTER
  JOIN pg_description d
    ON p.oid = d.objoid
  LEFT OUTER
  JOIN pg_namespace n
    ON n.oid = p.pronamespace
 WHERE NOT p.proisagg
   AND n.nspname~'<$SCHEMA_NAME_PATTERN>'
   AND proname~'<$FUNCTION_NAME_PATTERN>'

If anyone wonders how to quickly query catalog tables and make use of the pg_get_functiondef() function here's the sample query:

SELECT n.nspname AS schema
      ,proname AS fname
      ,proargnames AS args
      ,t.typname AS return_type
      ,d.description
      ,pg_get_functiondef(p.oid) as definition
--      ,CASE WHEN NOT p.proisagg THEN pg_get_functiondef(p.oid)
--            ELSE 'pg_get_functiondef() can''t be used with aggregate functions'
--       END as definition
  FROM pg_proc p
  JOIN pg_type t
    ON p.prorettype = t.oid
  LEFT OUTER
  JOIN pg_description d
    ON p.oid = d.objoid
  LEFT OUTER
  JOIN pg_namespace n
    ON n.oid = p.pronamespace
 WHERE NOT p.proisagg
   AND n.nspname~'<$SCHEMA_NAME_PATTERN>'
   AND proname~'<$FUNCTION_NAME_PATTERN>'
笑忘罢 2024-09-21 23:05:02

由于 PostgreSQL 9.1 \sf 可用。

Since PostgreSQL 9.1 \sf is available.

十二 2024-09-21 23:05:02
\sf function_name

(末尾不要加分号)

\sf function_name

(don't put a semicolon at the end)

孤单情人 2024-09-21 23:05:02
SELECT pg_get_functiondef(( SELECT oid 
                            FROM   pg_proc
                            WHERE  proname = 'function_name' ));
SELECT pg_get_functiondef(( SELECT oid 
                            FROM   pg_proc
                            WHERE  proname = 'function_name' ));
逆光飞翔i 2024-09-21 23:05:02

*在这个答案中,我介绍了几种显示函数代码的方法,如下所示,但您也可以使用下面的这些方法来显示过程的代码。

例如,您创建 my_func() 函数,如下所示。

CREATE FUNCTION my_func(v1 INT, v2 INT) RETURNS INT
AS $
BEGIN
  RETURN v1 + v2;
END;
$ LANGUAGE plpgsql;

然后,您可以使用 \sf 如下图:

postgres=# \sf public.my_func
CREATE OR REPLACE FUNCTION public.my_func(v1 integer, v2 integer)       
 RETURNS integer
 LANGUAGE plpgsql
AS $function$
BEGIN
  RETURN v1 + v2;
END;
$function$

postgres=# \sf+ public.my_func
        CREATE OR REPLACE FUNCTION public.my_func(v1 integer, v2 integer
)
         RETURNS integer
         LANGUAGE plpgsql
1       AS $function$
2       BEGIN
3         RETURN v1 + v2;
4       END;
5       $function$

*备注:

  • +可以显示行号。

  • 您可以省略架构public。

或者,您可以使用 my_func() 的代码>pg_get_functiondef()my_func() 的 OID(对象标识符)如下所示:

postgres=# SELECT pg_get_functiondef('public.my_func'::regproc);
                        pg_get_functiondef
-------------------------------------------------------------------
 CREATE OR REPLACE FUNCTION public.my_func(v1 integer, v2 integer)+
  RETURNS integer                                                 +
  LANGUAGE plpgsql                                                +
 AS $function$                                                    +
 BEGIN                                                            +
   RETURN v1 + v2;                                                +
 END;                                                             +
 $function$                                                       +

(1 row)

*备注:

  • 您可以省略 schema public.

或者,您可以使用 [pg_proc][4] 仅显示 my_func() 的主体代码,如下所示:

postgres=# SELECT prosrc FROM pg_proc WHERE proname = 'my_func';
      prosrc
-------------------
                  +
 BEGIN            +
   RETURN v1 + v2;+
 END;             +

(1 row)

postgres=# SELECT prosrc FROM pg_proc WHERE oid = 'public.my_func'::regproc;
      prosrc
-------------------
                  +
 BEGIN            +
   RETURN v1 + v2;+
 END;             +

(1 row)

或者,您可以仅显示 my_func() 的代码与 information_schema.routines 如下所示:

postgres=# SELECT routine_definition FROM information_schema.routines WHERE routine_name = 'my_func';
 routine_definition
--------------------
                   +
 BEGIN             +
   RETURN v1 + v2; +
 END;              +

(1 row)

*In this answer, I introduce several ways to show the code of a function as shown below but you can also use these ways below to show the code of a procedure.

For example, you create my_func() function as shown below.

CREATE FUNCTION my_func(v1 INT, v2 INT) RETURNS INT
AS $
BEGIN
  RETURN v1 + v2;
END;
$ LANGUAGE plpgsql;

Then, you can show the code of my_func() with \sf as shown below:

postgres=# \sf public.my_func
CREATE OR REPLACE FUNCTION public.my_func(v1 integer, v2 integer)       
 RETURNS integer
 LANGUAGE plpgsql
AS $function$
BEGIN
  RETURN v1 + v2;
END;
$function$

postgres=# \sf+ public.my_func
        CREATE OR REPLACE FUNCTION public.my_func(v1 integer, v2 integer
)
         RETURNS integer
         LANGUAGE plpgsql
1       AS $function$
2       BEGIN
3         RETURN v1 + v2;
4       END;
5       $function$

*Memos:

  • + can show line numbers.

  • You can omit the schema public..

Or, you can show the code of my_func() with pg_get_functiondef() and the OID(Object identifier) of my_func() as shown below:

postgres=# SELECT pg_get_functiondef('public.my_func'::regproc);
                        pg_get_functiondef
-------------------------------------------------------------------
 CREATE OR REPLACE FUNCTION public.my_func(v1 integer, v2 integer)+
  RETURNS integer                                                 +
  LANGUAGE plpgsql                                                +
 AS $function$                                                    +
 BEGIN                                                            +
   RETURN v1 + v2;                                                +
 END;                                                             +
 $function$                                                       +

(1 row)

*Memos:

  • You can omit the schema public..

Or, you can show only the body code of my_func() with [pg_proc][4] as shown below:

postgres=# SELECT prosrc FROM pg_proc WHERE proname = 'my_func';
      prosrc
-------------------
                  +
 BEGIN            +
   RETURN v1 + v2;+
 END;             +

(1 row)

postgres=# SELECT prosrc FROM pg_proc WHERE oid = 'public.my_func'::regproc;
      prosrc
-------------------
                  +
 BEGIN            +
   RETURN v1 + v2;+
 END;             +

(1 row)

Or, you can show only the code of my_func() with information_schema.routines as shown below:

postgres=# SELECT routine_definition FROM information_schema.routines WHERE routine_name = 'my_func';
 routine_definition
--------------------
                   +
 BEGIN             +
   RETURN v1 + v2; +
 END;              +

(1 row)
⒈起吃苦の倖褔 2024-09-21 23:05:02

如果您在系统中进行了配置,您也可以通过 phpPgAdmin 获取,

步骤 1:选择您的数据库

步骤 2:单击查找按钮

步骤 3:将搜索选项更改为功能,然后单击查找。

您将获得已定义函数的列表。您也可以按名称搜索函数,希望这个答案对其他人有帮助。

You can also get by phpPgAdmin if you are configured it in your system,

Step 1: Select your database

Step 2: Click on find button

Step 3: Change search option to functions then click on Find.

You will get the list of defined functions.You can search functions by name also, hope this answer will help others.

云仙小弟 2024-09-21 23:05:02
jdbcTemplate.queryForList("SELECT routine_name FROM information_schema.routines WHERE routine_type = 'PROCEDURE'", String.class);

jdbcTemplate.queryForList("SELECT routine_name FROM information_schema.routines WHERE routine_type = 'PROCEDURE'", String.class);

枫以 2024-09-21 23:05:02

通常来说,您会使用数据库管理器应用程序,例如 pgAdmin,浏览到您感兴趣的对象,并右键单击“脚本作为创建”或类似的方式。

您是否想在没有管理应用程序的情况下执行此操作?

Normally speaking you'd use a DB manager application like pgAdmin, browse to the object you're interested in, and right click your way to "script as create" or similar.

Are you trying to do this... without a management app?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文