Skip to content

Instantly share code, notes, and snippets.

@everilae
Created May 16, 2014 11:51
Show Gist options
  • Save everilae/76e584e611ae9ffec2ea to your computer and use it in GitHub Desktop.
Save everilae/76e584e611ae9ffec2ea to your computer and use it in GitHub Desktop.
PostgreSQL PL/pgSQL array_foreach
create function array_foreach(text, anyarray) returns anyarray as $$
declare
funcname alias for $1;
vals alias for $2;
results alias for $0;
begin
execute format('select array_agg(s.t) from (select %I(unnest($1))) as s(t)', funcname) into results using vals;
return results;
end;
$$ language plpgsql;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment