How to get function parameter lists (so I can drop a function)

后端 未结 4 1238
死守一世寂寞
死守一世寂寞 2020-11-29 07:40

I want to get the SQL to drop a function in PostgreSQL. I write DROP FUNCTION and a get function name from pg_proc. That is not problem. However if

4条回答
  •  被撕碎了的回忆
    2020-11-29 07:49

    If you are working on an old previous version of postgres, for which pg_get_function_identity_arguments(func_oid) doesn't exist, I create my own function get the parameters from the function, you only need to pass the oid for the function, you need to deploy the function below to your postgres db.

    CREATE OR REPLACE FUNCTION public.getFunctionParameter(functionOid oid)
      RETURNS text AS
    $BODY$
    
    declare
        t_paras text;
        paras oid[];
        res text :='(';
    begin
    
    select proargtypes into t_paras from pg_proc where oid=functionOid;
    if t_paras is null or t_paras='' then
        return '()';
    else
        paras:=string_to_array(t_paras,' ');
        for i in  array_lower(paras,1) .. array_upper(paras,1)
        loop
            raise notice 'para is %',paras[i];
            select format_type(paras[i]::oid,NULL) into t_paras;
            res:=res||t_paras||',';
        end loop;
        res:=substring(res from 1 for char_length(res)-1);
        res:=res||')';
        return res;
    end if;
    
    end 
    
        $BODY$
          LANGUAGE plpgsql ;
    

    The function below will list the function name and parameters, change the schema name if you want to get function under some other schema, I am using public for example

        SELECT n.nspname||'.'||p.proname||public.getFunctionParameter(p.oid)
    FROM    pg_proc p JOIN   pg_namespace n ON n.oid = p.pronamespace
    WHERE  n.nspname='public' 
    

    You will the result like below

    1 "public.getfunctionparameter(integer,text)"
    2 "public.getfunctionparameter(oid)"
    

提交回复
热议问题