Re: how to use array with "holes" ?

From: Anton <anton200(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: how to use array with "holes" ?
Date: 2007-06-01 08:35:10
Message-ID: 8cac8dd0706010135i2f28f7dfw9c5f4e2e7ca86480@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

> you have to initialise array before using. Like:
>
> declare a int[] = '{0,0,0,0,0, .................}';
> begin
> a[10] := 11;

Ok, I got it, thanks! Now I can work with simle arrays.

May I ask some more complex? I want to use ONE multidimensial array -
the "id", "bytes_in" and "bytes_out". By another words, I need an
array, each element of which must contain 3 values: ttc_id, bytes_in,
bytes_out.

I think it can be done like this:

CREATE OR REPLACE FUNCTION myf_test() RETURNS void
AS $$
DECLARE
p_tmp RECORD;
p_last_cpnt RECORD;
p_bytes bigint[][][][];
i int;
BEGIN

SELECT * INTO p_last_cpnt FROM nn_cpnt WHERE account_id = 5 ORDER BY
date_time DESC, cpnt_id DESC LIMIT 1;
IF FOUND THEN
FOR p_tmp IN SELECT ttc_id, bytes_in, bytes_out FROM nn_cpnt_traf
WHERE cpnt_id = p_last_cpnt.cpnt_id ORDER BY ttc_id LOOP
--RAISE NOTICE 'ttc_id[%] -> in[%] out[%]', p_tmp.ttc_id,
p_tmp.bytes_in, p_tmp.bytes_out;
-- get the next number for array index, ugly but works
i = array_upper(p_bytes, 1) + 1;
IF i IS NULL THEN
i := 0;
END IF;
-- here I try to add element
p_bytes[i] := ARRAY[p_tmp.ttc_id,p_tmp.bytes_in,p_tmp.bytes_out];
END LOOP;
END IF;
-- ... some work. and here is the beauty of multidimensial. As I think...
FOR i IN COALESCE(array_lower(p_bytes,1),1) ..
COALESCE(array_upper(p_bytes,1),-1) LOOP
RAISE NOTICE 'p_bytes[%] = [%] / [%] / [%]', i, p_bytes[i][0],
p_bytes[i][1], p_bytes[i][2];
END LOOP;
END;
$$
LANGUAGE plpgsql;

But I make an error somewhere, again...
=# SELECT myf_test();
ERROR: invalid input syntax for integer: "{1,1907262814,9308877139}"
CONTEXT: PL/pgSQL function "myf_test" line 18 at assignment

I think I not understand somewhat...
--
engineer

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Dmitry Koterov 2007-06-01 09:32:35 Does slonik EXECUTE SCRIPT call waits for comands termination?
Previous Message Martijn van Oosterhout 2007-06-01 07:56:49 Re: user restriction