List stored functions that reference a table in PostgreSQL

后端 未结 9 1064
深忆病人
深忆病人 2020-12-07 14:10

Just a quick and simple question: in PostgreSQL, how do you list the names of all stored functions/stored procedures using a table using just a SELECT statement, if possible

相关标签:
9条回答
  • 2020-12-07 14:40
    SELECT  p.proname
    FROM    pg_catalog.pg_namespace n
    JOIN    pg_catalog.pg_proc p
    ON      p.pronamespace = n.oid
    WHERE   n.nspname = 'public';
    
    0 讨论(0)
  • 2020-12-07 14:44

    Have a look at my recipe. It reads functions and triggers. It is based on informations from: Extracting META information from PostgreSQL (INFORMATION_SCHEMA)

    0 讨论(0)
  • 2020-12-07 14:46

    Excluding the system stuff:

    select proname from pg_proc where proowner <> 1;
    
    0 讨论(0)
  • 2020-12-07 14:52

    Please change the schema_name and table_name in the below query:

    SELECT n.nspname AS schema_name
         , p.proname AS function_name
         , pg_get_function_arguments(p.oid) AS args
         , pg_get_functiondef(p.oid) AS func_def
    FROM   pg_proc p
    JOIN   pg_namespace n ON n.oid = p.pronamespace
    AND    n.nspname = 'schema_name'
    AND    p.prosrc like '%table_name%'
    

    Since the table name is case sensitive, so need to define the exact table name.

    0 讨论(0)
  • 2020-12-07 14:53

    Same as @quassnoi and @davidwhthomas, except I added the argument names in there:

    SELECT  proname, proargnames, prosrc 
    FROM    pg_catalog.pg_namespace n
    JOIN    pg_catalog.pg_proc p
    ON      pronamespace = n.oid
    WHERE   nspname = 'public';
    

    If the purpose behind listing the functions is to clean them up or iterate a new function with a changing params list, you will frequently need to drop functions:

    DROP FUNCTION <name>(<args>);
    

    By adding proargnames, I am able to construct the applicable function name for the drop.

    Additionally, it's nice to see a more complete picture when evaluating the functions.

    0 讨论(0)
  • 2020-12-07 14:56

    If you are using psql, try \df

    From the man page:

    Tip
    To look up functions taking arguments or returning values of a specific type, use your pager's search capability to scroll through the \df output.
    

    Running \set ECHO_HIDDEN will reveal what \df is running behind the scenes.

    0 讨论(0)
提交回复
热议问题