Re: how to use array with "holes" ?

From: "Pavel Stehule" <pavel(dot)stehule(at)gmail(dot)com>
To: Anton <anton200(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: how to use array with "holes" ?
Date: 2007-06-01 11:37:42
Message-ID: 162867790706010437m51e9cb35m53b165dc66cc88fd@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

> 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.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Simon Riggs 2007-06-01 11:44:31 Re: warm standby server stops doing checkpoints afterawhile
Previous Message Frank Wittig 2007-06-01 11:33:37 Re: warm standby server stops doing checkpoints after awhile