Code Snippets

Writing a function for frequently used SQL (returning custom columns) in PostgreSQL

There’s no real easy way to do this, but I’d like to write a database function (in this case a PL/pgSQL function) to run a SQL SELECT statement with custom columns in the output.

First, you need to create a custom data type for the custom columns (apparently, this step won’t be necessary when PostgreSQL 8.4 supports the RETURNS TABLE syntax in functions).  For example