You could write a PL/pgSQL function to abstract that and post it to my PostgreSQL Cookbook: http://www.brasileiro.net/postgres/
-Roberto