initialisation: FOR p_tmp IN SELECT DISTINCT ON(ttc_id) ttc_id FROM ttc_ids LOOP -- get next value for index i = array_upper(p_ttc_ids, 1) + 1; IF i IS NULL THEN i := 0; END IF; --RAISE NOTICE '[%]', i; p_ttc_ids[i] := p_tmp.ttc_id; p_bytes_in[i] := 0; p_bytes_out[i] := 0; END LOOP;
This isn't well style for plpgsql. It's slow for bigger arrays. create or replace function filltest1(int) returns void as $$ declare a int[]; begin for i in 1..$1 loop a[i] := 0; end loop; end; $$ language plpgsql; -- with trick, its necessary for $1> 8000 create or replace function filltest2(int) returns void as $$ declare a int[]; begin a := case $1 when 0 then '{}' when 1 then '{0}' else '{0'||repeat(',0', $1-1) || '}' end; end; $$ language plpgsql; filltest2 is more cryptographic , but is 10-20% faster and for sizeof(a) > ~ 8000 is 30x faster. If you now max size of array you can do: declare a int[] = '{0,0,0,0..........}'; b int[] = '{....}' every array update generates new version of array -> update is more expensive than you can know from classic languages.