why complicate so much ? this is a single sql query. It really shocks me, how people easily lean on plpgsql and for/loops - which are times slower than simple sql query: CREATE FUNCTION unnest(anyarray) RETURNS SETOF anyelement AS $_$ SELECT ($1)[i] FROM generate_series(array_lower($1,1),array_upper($1,1)) i; $_$ LANGUAGE sql IMMUTABLE; -- Sent via pgsql-general mailing list (pgsql-general@xxxxxxxxxxxxxx) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-general