Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all procedural , user defined functions

How to get list of all user defined functions via SQL query ?


I find this code here

SELECT p.proname, p.pronargs, t.typname
 FROM pg_proc p, pg_language l, pg_type t
 WHERE p.prolang = l.oid
 and p.prorettype = t.oid
 and l.lanname = 'c'
ORDER BY proname;

but this gets C-functions

How to get user defined, procedural language functions, writen for example in plpgsql language?

like image 284
Oto Shavadze Avatar asked May 19 '13 06:05

Oto Shavadze


People also ask

How do you see all the UDFs defined in the current database?

Lists all user-defined functions (UDFs) for which you have access privileges. This command can be used to list the UDFs for a specified database or schema (or the current database/schema for the session), or across your entire account.

How do I get all functions in SQL Server?

To get the list of all the functions in a database, you can use the transact SQL statement against the system objects like Sys. Objects, Information_Schema. Routines, syscomments or Sys. Sql_Modules.

What is different between UDF & SP?

SPs can change database objects. Inline User-Defined Functions can be treated like views with parameters and can be used in row set operations and JOINs. Cannot JOIN the output of a Stored procedure. UDF can be used in the SQL statements anywhere in the WHERE / HAVING / SELECT sections.


1 Answers

Consider:

select 
    pp.proname,
    pl.lanname,
    pn.nspname,
    pg_get_functiondef(pp.oid)
from pg_proc pp
inner join pg_namespace pn on (pp.pronamespace = pn.oid)
inner join pg_language pl on (pp.prolang = pl.oid)
where pl.lanname NOT IN ('c','internal') 
  and pn.nspname NOT LIKE 'pg_%'
  and pn.nspname <> 'information_schema';

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

Use pg_get_functiondef or the prosrc column from pg_proc directly. The key idea is to join on pg_namespace and filter out the PostgreSQL catalog functions, which will probably be adequate for most purposes:

FROM pg_proc pp INNER JOIN pg_namespace ON (pp.pronamespace = pn.oid)
WHERE pn.nspname <> 'pg_catalog'

The trouble with obtaining the source code for user defined functions is deciding what user means. Many types of functions can be created:

  • Functions using CREATE EXTENSION.
  • Functions created by PostgreSQL.
  • Functions compiled and installed by an administrator.

Superusers with sufficent grants can define functions in pg_proc, but usually don't.

Since only superusers can create C language functions, exclude them. Such functions can be custom-installed on a particular database by the admin, but not a normal user.

like image 161
Craig Ringer Avatar answered Oct 04 '22 13:10

Craig Ringer