Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any way to list all user-defined PostgreSQL functions?

Tags:

postgresql

PostgreSQL provides the command \dv to list all views. Is there a similar way to list all user-defined functions or perhaps just those function defined by a particular user? \sf requires you to know a function's name and it will provide a function's definition. \df lists all functions (and there are a lot). I'd like a way to just show a list of the functions I've defined.

like image 393
Jim Avatar asked May 23 '17 19:05

Jim


People also ask

How do I view all DBS in PostgreSQL?

Step 1: Log in to the server using the SQL Shell (psql) app. Step 2: Run the following query: SELECT datname FROM pg_database; psql runs the query against the server and displays a list of existing databases in the output.

Where are Postgres functions stored?

A PostgreSQL function or a stored procedure is a set of SQL and procedural commands such as declarations, assignments, loops, flow-of-control etc. stored on the database server and can be involved using the SQL interface.

How can I list all foreign keys referencing a given table in Postgres?

To get a list of all foreign keys of the table using psql you can use the \d your_table_name command line.


1 Answers

The best way to find such a query is to use psql with the --echo-hidden option. Then run the psql meta-command and you will see the query that is used.

For \df this is:

SELECT n.nspname as "Schema",
  p.proname as "Name",
  pg_catalog.pg_get_function_result(p.oid) as "Result data type",
  pg_catalog.pg_get_function_arguments(p.oid) as "Argument data types",
 CASE
  WHEN p.proisagg THEN 'agg'
  WHEN p.proiswindow THEN 'window'
  WHEN p.prorettype = 'pg_catalog.trigger'::pg_catalog.regtype THEN 'trigger'
  ELSE 'normal'
 END as "Type"
FROM pg_catalog.pg_proc p
     LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace
WHERE pg_catalog.pg_function_is_visible(p.oid)
      AND n.nspname <> 'pg_catalog'
      AND n.nspname <> 'information_schema'
ORDER BY 1, 2, 4;

You could adjust that by e.g. changing the where clause to:

AND n.nspname = 'public'

Which is equivalent to \df public.*


If you check the documentation of pg_proc you will notice that there is a proowner column so you could also run:

SELECT n.nspname as "Schema",
       p.proname as "Name",
       pg_catalog.pg_get_function_result(p.oid) as "Result data type",
       pg_catalog.pg_get_function_arguments(p.oid) as "Argument data types"
FROM pg_catalog.pg_proc p
  JOIN pg_catalog.pg_roles u ON u.oid = p.proowner
  LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace
WHERE pg_catalog.pg_function_is_visible(p.oid)
  AND n.nspname = 'public' 
  AND u.rolname = current_user --<< this limits the functions to those that the current user owns.
like image 138
a_horse_with_no_name Avatar answered Oct 16 '22 20:10

a_horse_with_no_name