From PostgreSQL wiki
Revision as of 14:35, 27 October 2009 by Intgr
by Scott Bailey 'Artacus'
PostgreSQL 8.4 includes a function for expanding an array into a set of rows. It is extremely helpful when working with arrays. Here is how to add it to 8.3 and below.
CREATE OR REPLACE FUNCTION unnest(anyarray) RETURNS SETOF anyelement AS $BODY$ SELECT $1[i] FROM generate_series(array_lower($1,1), array_upper($1,1)) i; $BODY$ LANGUAGE 'sql' IMMUTABLE