Skip site navigation (1) Skip section navigation (2)

Function for more readable function source code

From: Kenneth Tanzer <ktanzer(at)desc(dot)org>
To: pgsql-general(at)postgresql(dot)org
Subject: Function for more readable function source code
Date: 2008-03-28 23:58:22
Message-ID: (view raw or whole thread)
Lists: pgsql-general
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.


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


    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';

pgsql-general by date

Next:From: Tom LaneDate: 2008-03-29 00:07:15
Subject: Re: Out of memory
Previous:From: Tom LaneDate: 2008-03-28 23:40:22
Subject: Re: Merge Joins and Views

Privacy Policy | About PostgreSQL
Copyright © 1996-2015 The PostgreSQL Global Development Group