Received: from localhost (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id 19EE8632C9A for ; Sat, 24 Oct 2009 09:01:23 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 04427-07 for ; Sat, 24 Oct 2009 12:01:06 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from relay.ptn-ipout02.plus.net (relay.ptn-ipout02.plus.net [212.159.7.36]) by mail.postgresql.org (Postfix) with ESMTP id 4F4D6632A90 for ; Sat, 24 Oct 2009 09:01:11 -0300 (ADT) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: ApoEAFuJ4kpUXebi/2dsb2JhbADWWoQ/BIFe Received: from relay03.plus.net ([84.93.230.226]) by relay.ptn-ipout02.plus.net with ESMTP; 24 Oct 2009 13:01:08 +0100 Received: from [84.51.143.99] (helo=server3.office.archonet.com) by relay03.plus.net with esmtp (Exim) id 1N1fIx-0007tB-Si; Sat, 24 Oct 2009 13:01:07 +0100 Received: from [192.168.1.36] (dell36.office.archonet.com [192.168.1.36]) by server3.office.archonet.com (Postfix) with ESMTPA id A44B727408D; Sat, 24 Oct 2009 13:01:07 +0100 (BST) Message-ID: <4AE2EC83.6060908@archonet.com> Date: Sat, 24 Oct 2009 13:01:07 +0100 From: Richard Huxton User-Agent: Thunderbird 2.0.0.23 (X11/20090817) MIME-Version: 1.0 To: Andrew Hall CC: pgsql-sql@postgresql.org Subject: Re: Table Valued Parameters References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Plusnet-Relay: 95fddd219988908b681cc6a4b8b85baa X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.39 tagged_above=-10 required=5 tests=AWL=0.209, BAYES_00=-2.599 X-Spam-Level: X-Archive-Number: 200910/125 X-Sequence-Number: 33324 Andrew Hall wrote: > Hi, > > I was wondering whether anybody would be able to advise me on how (if it is possible) to port some functionality from Oracle? > > This is just an example - in Oracle, I am able to do the following > > -- > -- Create a data type which replicates the data structure of a single user in my application. > -- I know that this can be done using PostgreSQL. > -- Create a data type which can store many instances of a single 'TY_APP_USER' > -- [essentially this is a table valued data type]. An instance of this data type can be > -- created and populated by the client application [a java based one in my case]. > -- > -- I can't find any reference to something > -- similar to this using postgreSQL. The following may not do anything interesting, but it does show arrays of composite types, which is what you are after. To prevent quoting insanity, I recommend the ARRAY[] constructor rather than array literals. You do need the explicit typecasts. Oh - and version 8.3 or higher for arrays of compound types. BEGIN; CREATE TYPE typ1 AS (i integer, t text); CREATE FUNCTION print_array(a typ1[]) RETURNS void AS $$ DECLARE m int; n int; i int; e typ1; BEGIN m := array_lower(a, 1); n := array_upper(a, 1); FOR i IN m .. n LOOP e := a[i]; RAISE NOTICE '% - %', e.i, e.t; END LOOP; RETURN; END; $$ LANGUAGE plpgsql; SELECT print_array(ARRAY[ '(1,"abc")'::typ1, '(2,"def")'::typ1 ]); SELECT print_array(ARRAY[ '(1,"abc")', '(2,"def")' ]::typ1[]); ROLLBACK; -- Richard Huxton Archonet Ltd