Search Postgresql Archives

Re: how to use array with "holes" ?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux