Paul Mendoza C# blog
Sunday, June 03, 2007
  PostgreSQL SQL function definition tables
Last night I was creating a code generator using Code Smith that would create C# functions for every database function in a schema but I needed a SQL statement that would get me the definition of the function. It wasn't obvious how to retrieve the parameters and the return type for the already existing functions that were in the database. At first I thought I would be able to find a column in the database that had the DDL and parse it out for the values I needed but no such column existed.

So here is what I discovered. When a database function is being added, PostgreSQL parses out the database function parameters, return type, namespace, name of the function and body and inserts them into some tables and columns. When the parse of the parameters is done, the parameter names are placed in one column surrounded on the ends by brackets and the parameter types are placed in another column. When the parameter types are stored, they are stored as their numerical representations instead of the names like "integer" and "numeric". Below is a list of some of the fairly commonly used types and their numbers.

If you want to look at a list of all of the functions for a schema, the following SQL query will return the data needed. This SQL query was mainly derived from looking at EMS.

SELECT p.proname AS name, p.oid, p.proargtypes AS args, ds.description , p.prorettype AS rettype,
p.proretset, p.probin, p.proisstrict AS strict, p.prosrc AS body, l.lanname AS lang,
u.usename, p.prosecdef, p.provolatile, p.proisagg, n.nspname, proargnames, p.proargmodes, p.proallargtypes
FROM pg_proc p
LEFT OUTER JOIN pg_description ds ON ds.objoid = p.oid
INNER JOIN pg_namespace n ON p.pronamespace = n.oid
INNER JOIN pg_language l ON l.oid = p.prolang
LEFT OUTER JOIN pg_user u ON u.usesysid = p.proowner
WHERE n.nspname = 'public'
ORDER BY p.proname, n.nspname


"nspname" is the schema name that the functions will be pulled from. The parameters list that has the integer type definitions will be in the "args" column and the parameter names will be in the "proargnames" column. The "body" column contains the body of the function.

I will try to post the Code Smith template that I created for generating the C# classes soon.
 
Comments:
From the command line, try:
\df functionname
\df+ functionname

I think the second one is quite useful, and much shorter than your query.
 
Post a Comment





<< Home
I am currently an ASP.NET, C# developer working on MangosteenNation.com, a XanGo website for helping people build their businesses. I am also pursuing a degree at CSU San Marcos in Southern California.

XanGo at Mangosteen Nation

Archives
October 2005 / November 2005 / December 2005 / January 2006 / February 2006 / March 2006 / April 2006 / May 2006 / June 2006 / July 2006 / August 2006 / September 2006 / October 2006 / November 2006 / December 2006 / January 2007 / April 2007 / May 2007 / June 2007 / August 2007 / February 2008 / August 2008 /


Powered by Blogger

Subscribe to
Posts [Atom]