Imagine we have a simple table
create table a (id serial, data text);
and the following function:
create or replace function f()
returns SETOF int As
$BODY$
DECLARE
l_arr int[];
BEGIN
insert into a(data)
values ('a')
returning array_agg(data) into l_arr; --THIS DOES NOT WORK
RETURN l_arr;
END;
$BODY$
LANGUAGE 'plpgsql' VOLATILE SECURITY DEFINER
COST 100
How one may gather inserted values into an array in the same SQL statement?
It appears, that it is possible to use RETURN QUERY without intermediate array: