Thread: Function for more readable function source code

Function for more readable function source code

From
Kenneth Tanzer
Date:
The only way I knew to display the source code of a function was with
\df+, which produces very hard-to-read output, because it returns
several columns about the function, but usually all I want is the source.

So I "created" a function (i.e., shameless copied the psql interpreter)
to display just the source code, which makes it much more readable.  I'm
passing it along in case it's useful to others as well.

Ken

CREATE FUNCTION function_source( char ) RETURNS text AS $$
DECLARE
    funcname ALIAS FOR $1;
    source TEXT;

BEGIN

    SELECT INTO source replace(p.prosrc,E'\x09',' ')
    FROM pg_catalog.pg_proc p
        LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace
        LEFT JOIN pg_catalog.pg_language l ON l.oid = p.prolang
        JOIN pg_catalog.pg_roles r ON r.oid = p.proowner
    WHERE p.prorettype <> 'pg_catalog.cstring'::pg_catalog.regtype
        AND (p.proargtypes[0] IS NULL
        OR   p.proargtypes[0] <> 'pg_catalog.cstring'::pg_catalog.regtype)
        AND NOT p.proisagg
        AND p.proname ~ ( '^(' || funcname || ')$' )
        AND pg_catalog.pg_function_is_visible(p.oid) ;
    RETURN source;
END;$$ language 'plpgsql';