How to get the list of functions in database in PostgreSQL along with its parameters?

后端 未结 2 842
野性不改
野性不改 2021-01-21 05:05

I have a database which has over 1000 functions. I want to GRANT execution permission to certain users. I can\'t do it manually, therefore I want to get a list of functions and

2条回答
  •  时光取名叫无心
    2021-01-21 05:29

    There's a handy function to help you out: oidvectortypes.

    SELECT format('%I.%I(%s)', ns.nspname, p.proname, oidvectortypes(p.proargtypes)) 
    FROM pg_proc p INNER JOIN pg_namespace ns ON (p.pronamespace = ns.oid);
    

    shows all functions with arguments. You can adapt that format incantation to generate any desired statements, and if you like, LOOP over it to feed the generated statement into EXECUTE in PL/PgSQL.

    Credit to Leo Hsu and Regina Obe at Postgres Online for pointing out oidvectortypes. I wrote similar functions before, but used complex nested expressions that this function gets rid of the need for.

    Note that in this case you don't have to do any custom SQL generation at all, though. Just use GRANT EXECUTE ON ALL FUNCTIONS IN SCHEMA myschema TO ... if you're on a vaguely recent PostgreSQL.

提交回复
热议问题