15

Trying to find out the stored functions and procedures on PostgreSQL. I found some of the solutions where "join" has been used. I am looking for an optimal solution/command to it.

3 Answers 3

22

You can get functions using meta command

\df
Sign up to request clarification or add additional context in comments.

3 Comments

Is there a meta command to dump the configuration of a function? Edit: nvm, you can use ' \sf [ function_name ]'
You might have to supply the db or schema to make this work: \df <schema> , or \df <schema>.*, so like \df my_db.*
21

If there is a command i really not know. I also use the join solution between pg_catalog.pg_proc and pg_catalog.pg_namespace for example, to list all the functions in one schema.

You can play with the join to get what you need. This query for example will provide you the commands to change the owner for all the functions on a schema:

SELECT 'ALTER FUNCTION '
            || quote_ident(n.nspname) || '.' 
            || quote_ident(p.proname) || '(' 
            || pg_catalog.pg_get_function_identity_arguments(p.oid)
            || ') OWNER TO owner_usr;' AS command
FROM   pg_catalog.pg_proc p
JOIN   pg_catalog.pg_namespace n ON n.oid = p.pronamespace 
WHERE  n.nspname = 'your_schema';

And this one should list you all your user defined functions :

SELECT quote_ident(n.nspname) as schema , quote_ident(p.proname) as function 
FROM   pg_catalog.pg_proc p
JOIN   pg_catalog.pg_namespace n ON n.oid = p.pronamespace 
WHERE  n.nspname not like 'pg_%'

Hope that helps.

2 Comments

Maybe the last line of code would better be modified to: WHERE n.nspname not like 'pg_%' (adding an underscore). Still not completely safe, but much safer already.
And now there's some functions with prefix _pg_ like information_schema._pg_expandarray
2

This can show all user-defined functions and procedures in the current database:

\df

*Use p to show all user-defined procedures in the current database:

\dfp

*Use S to show all user-defined and system functions and procedures in the current database:

\dfS

*Use + to show all user-defined functions and procedures in the current database in detail:

\df+

*You can use p, S and + together to show all user-defined and system procedures in the current database in detail:

\dfpS+

And, you can show all user-defined and system functions and procedures in the current database with information_schema.routines and pg_proc as shown below:

SELECT routine_name FROM information_schema.routines;
SELECT proname FROM pg_proc;

*There is no way to show all user-defined (and system) functions and procedures in all databases at once.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.