Method 1:

psql -d <db_name> -c "\sf <schema.function_name">function_name_ddl.sql

psql>\sf  <schema.function_name>

 

Method 2:


select routine_definition  from information_schema.routines

where specific_schema =<schema_name>

and routine_name =<function_name>;

 

Method 3:


psql -d <dbname> -c "\ef <schema.function_name" to edit the function .

 

Method 4:  with trimmed spaces


select prosrc from pg_proc where proname=<function_name>;

 select 

          trim(n.nspname || '.' || proname ||

       '<' || coalesce(array_to_string(proargnames,','),' ') || '>' ||

          '<' || coalesce(array_to_string(array_agg(t.typname order by t.typname),','),' ')) || '>' as udf,

          trim(regexp_replace (prosrc, '[[:space:]]', ' ','g')) as src_code

        from pg_proc

        inner join pg_catalog.pg_namespace n

           on pronamespace = n.oid

        inner join pg_type t

           on t.oid = any(proargtypes)

      --where nspname not in ('gp_toolkit','information_schema')

        group by nspname, proname, proargnames, proargtypes, prosrc

        order by 1;