Re: Declaring multidimensional arrays in pl/pgsql

From: "Max Zorloff" <zorloff(at)gmail(dot)com>
To: "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: Declaring multidimensional arrays in pl/pgsql
Date: 2007-11-29 15:31:47
Message-ID: op.t2j729fhll0p5y@1-rtt202dnf3uds.mshome.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Thu, 29 Nov 2007 19:21:03 +0400, Pavel Stehule
<pavel(dot)stehule(at)gmail(dot)com> wrote:

> Hello
>
> arrays in PostgreSQL have to be regular allways. And before 8.3 array
> cannot contais NULL, so you cannot simpl resize two dim array :(. But
> your functions doesn't work in 8.3. too. So you can
>
> a) use 1D array and access to array like myarray[10*(n1-1)+n2]
>
> b) init array with string like
>
> create or replace function testfunc()
> returns void as $$
> DECLARE
> myArray int[][];
> BEGIN
> myArray := ('{'||array_to_string(array(select
> '{0,0,0,0,0,0,0,0,0,0}'::text from
> generate_series(1,10)),',')||'}')::int[][];
> FOR i IN 1..10 LOOP
> FOR j IN 1..10 LOOP
> RAISE NOTICE '% %', i, j;
> myArray[i][j] := 1;
> END LOOP;
> END LOOP;
> RETURN;
> END
> $$ language plpgsql;

Thanks for the info, but running the above gives me that :

ponline=# select testfunc();
ERROR: cannot cast type text to integer[]
CONTEXT: SQL statement "SELECT ('{'||array_to_string(array(select
'{0,0,0,0,0,0,0,0,0,0}'::text from
generate_series(1,10)),',')||'}')::int[][]"
PL/pgSQL function "testfunc" line 4 at assignment

I think 8.1 does not have text -> int[] cast available. I think I'm stuck
with option a.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Willy-Bas Loos 2007-11-29 15:36:21 Re: 1 cluster on several servers
Previous Message Hiroshi Saito 2007-11-29 15:12:46 Re: Slony-I creation scripts not available