Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the command to find script of a existing function in postgresql?

Tags:

postgresql

As we are using sp_helptext [procedure name] to get the SP script in Sqlserver, I need the command that i can use to retrieve a function script from postgresql.

Please help....

like image 487
Ranjan Panigrahi Avatar asked Jul 03 '12 06:07

Ranjan Panigrahi


People also ask

How do I run a script in PostgreSQL?

Another easiest and most used way to run any SQL file in PostgreSQL is via its SQL shell. Open the SQL shell from the menu bar of Windows 10. Add your server name, database name where you want to import the file, the port number you are currently active on, PostgreSQL username, and password to start using SQL shell.

How do I open a function in PostgreSQL?

For function:select proname,prosrc from pg_proc where proname= your_function_name; Another way is that just execute the commont \df and \ef which can list the functions. It will show the source code of the function.


2 Answers

If you are using psql (the commandline interface) you can use \df+ as tobixen has already stated (and which is clearly documented in the manual).

If you need to do this from within a SQL query, take a look a the system information functions. You are looking for pg_get_functiondef()

select pg_get_functiondef(oid)
from pg_proc
where proname = 'your_function';

If you are dealing with overloaded functions having a different number of parameters, you need to include the parameter signature into the name:

select pg_get_functiondef('public.foo(int)'::regprocedure);
select pg_get_functiondef('public.foo(int,int)'::regprocedure);

will retrieve the overloaded versions of the function foo (one version with a single int parameter, the other version with two int parameters).

like image 84
a_horse_with_no_name Avatar answered Sep 24 '22 06:09

a_horse_with_no_name


At the psql command line it's \df+ foo, isn't it?

Eventually, select prosrc from pg_proc where proname='foo';

like image 41
tobixen Avatar answered Sep 23 '22 06:09

tobixen