X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [64.117.224.130]) by svr1.postgresql.org (Postfix) with ESMTP id 3CDE4D1B8CE for ; Sun, 31 Aug 2003 01:15:21 +0000 (GMT) Received: from svr1.postgresql.org ([64.117.224.193]) by localhost (neptune.hub.org [64.117.224.130]) (amavisd-new, port 10024) with ESMTP id 74080-06 for ; Sat, 30 Aug 2003 22:15:14 -0300 (ADT) Received: from bast.unixathome.org (bast.unixathome.org [66.11.174.150]) by svr1.postgresql.org (Postfix) with ESMTP id 46647D1B56B for ; Sat, 30 Aug 2003 22:15:09 -0300 (ADT) Received: from wocker (wocker.unixathome.org [192.168.0.99]) by bast.unixathome.org (Postfix) with ESMTP id BB52F3D28; Sat, 30 Aug 2003 21:15:12 -0400 (EDT) From: "Dan Langille" To: Stephan Szabo Date: Sat, 30 Aug 2003 21:16:26 -0400 MIME-Version: 1.0 Subject: Re: Getting the return type right for SETOF Cc: Message-ID: <3F51142A.19665.30EA4C00@localhost> References: <3F50D3EF.27516.2FEF6552@localhost> In-reply-to: <20030830135853.I74114-100000@megazone.bigpanda.com> X-mailer: Pegasus Mail for Windows (v4.02a) Content-type: text/plain; charset=US-ASCII Content-transfer-encoding: 7BIT Content-description: Mail message body X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200308/361 X-Sequence-Number: 14977 On 30 Aug 2003 at 13:59, Stephan Szabo wrote: > On Sat, 30 Aug 2003, Dan Langille wrote: > > > Hi folks, > > > > I'm playing with SETOF on functions. But I can't get the return type > > correct. What have I missed? A cast? > > > > CREATE OR REPLACE FUNCTION elementGet (text) RETURNS SETOF > > element_type AS ' > > > > select 1, > > \'test\', > > \'F\' > > \'A\', > > FALSE, > > FALSE > > ' > > LANGUAGE sql stable; > > ERROR: function declared to return element_type returns "unknown" > > instead of text at column 2 > > I think you'll need to explicitly make the three text columns text rather > than just a plain literal (so ''test''::text for example) Right you are! Here is the real function: CREATE OR REPLACE FUNCTION elementGet (text) RETURNS SETOF element_type AS ' select id, name::text, directory_file_flag::text, status::text, case when IsPort( Pathname_ID($1)) IS NULL THEN FALSE ELSE TRUE END, case when IsCategory(Pathname_ID($1)) IS NULL THEN FALSE ELSE TRUE END FROM element WHERE id = PathName_ID($1); ' LANGUAGE sql stable; select * from elementGet('ports/security/logcheck'); id | name | type | status | iscategory | isport -------+----------+------+--------+------------+-------- 37342 | logcheck | D | A | t | f And it's fast too: explain analyse select * from elementGet('ports/security/logcheck'); QUERY PLAN ---------------------------------------------------------------------- ----------------------------------------- Function Scan on elementget (cost=0.00..12.50 rows=1000 width=102) (actual time=64.28..64.28 rows=1 loops=1) Total runtime: 64.35 msec Thank you. -- Dan Langille : http://www.langille.org/