X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 68E78329D76 for ; Thu, 16 Sep 2004 18:11:27 +0100 (BST) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 99805-08 for ; Thu, 16 Sep 2004 17:11:17 +0000 (GMT) Received: from is01.lnl.infn.it (is01.lnl.infn.it [192.135.30.245]) by svr1.postgresql.org (Postfix) with ESMTP id 09370329CFD for ; Thu, 16 Sep 2004 18:11:14 +0100 (BST) Received: from is01.lnl.infn.it (localhost.lnl.infn.it [127.0.0.1]) by is01.lnl.infn.it (8.12.2/8.12.2) with ESMTP id i8GHB3Wp016131 for ; Thu, 16 Sep 2004 19:11:04 +0200 (CEST) Received: from [192.135.30.64] (hogwarts.lnl.infn.it [192.135.30.64]) by is01.lnl.infn.it (8.12.2/8.12.2) with ESMTP id i8GHB3wT016124 for ; Thu, 16 Sep 2004 19:11:03 +0200 (CEST) Message-ID: <4149C90D.60806@lnl.infn.it> Date: Thu, 16 Sep 2004 19:10:37 +0200 From: Sergio Fantinel Reply-To: sergio.fantinel@lnl.infn.it Organization: INFN-LNL :: Nuclear Phisic National Institute - Legnaro National Laboratory User-Agent: Mozilla Thunderbird 0.7.3 (Windows/20040803) X-Accept-Language: en-us, en MIME-Version: 1.0 To: pgsql-sql@postgresql.org Subject: PL/pgSQL multidimension (matrix) array in function Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 8bit X-AntiVirus: checked by Vexira Milter 1.0.6; VAE 6.27.0.10; VDF 6.27.0.64 X-RAVMilter-Version: 8.4.1(snapshot 20020920) (is01.lnl.infn.it) X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.0 tagged_above=0.0 required=5.0 tests= X-Spam-Level: X-Archive-Number: 200409/116 X-Sequence-Number: 18962 I found how to use, inside a PL/pgSQL function, a two-dimensions array (matrix). There is a limitation: the number of the 'columns' of the matrix is fixed at declaration time (in DECLARE section) and you need to manually initialize all the elements in the first 'row' of the matrix. The number of rows is unlimited and can be sized at runtime. Here is the code that can help you. If someone know how to manage an NxN array without limitations, please replay to this thread. I'm using PostgreSQL 7.4.1 on RH7.3 Cheers Sergio CREATE OR REPLACE FUNCTION "testarray" (integer) RETURNS SETOF integer AS' DECLARE n alias for $1; -- number of rows is passed as argument i INTEGER; j integer; k INTEGER := 3; -- matrix columns number b integer[] := array[0,0,0]; -- need it to initialize the matrix!! a integer[][] := array[[0,0,0]]; -- need it to initialize the matrix!! begin for i in 1..n loop -- the i loop can start obviously from 2 (the first row is already present...) but for our purpose here we use 1 a := array_cat(a,b); for j in 1..k loop a[i][j] := i*j; end loop; end loop; for i in 1..n loop return next null; return next i; -- need it to format in some way the output :) return next null; for j in 1..k loop return next a[i][j]; end loop; end loop; return; end 'LANGUAGE 'plpgsql'; here is the output: => select * from testarray(8); testarray ----------- 1 1 2 3 2 2 4 6 3 3 6 9 4 4 8 12 5 5 10 15 6 6 12 18 7 7 14 21 8 8 16 24 (48 rows) -- --------------------------------------------------------------------- Sergio Fantinel EGEE Project --------------------------------------------------------------------- INFN - Lab. Naz. di Legnaro phone: +39 049 8068 489 viale dell'Università n. 2, 35020 Legnaro (PD) ITALY sergio.fantinel@lnl.infn.it ---------------------------------------------------------------------