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: 47ED861E.2040301@desc.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
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.

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

Browse pgsql-general by date

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