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 3217214B2821 for ; Thu, 12 Aug 2004 11:36:41 -0300 (ADT) 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 29524-01 for ; Thu, 12 Aug 2004 14:36:35 +0000 (GMT) Received: from megazone.bigpanda.com (megazone.bigpanda.com [64.147.171.210]) by svr1.postgresql.org (Postfix) with ESMTP id 5F3D414B2806 for ; Thu, 12 Aug 2004 11:36:32 -0300 (ADT) Received: by megazone.bigpanda.com (Postfix, from userid 1001) id 43A87357CC; Thu, 12 Aug 2004 07:36:37 -0700 (PDT) Received: from localhost (localhost [127.0.0.1]) by megazone.bigpanda.com (Postfix) with ESMTP id 4236134D59; Thu, 12 Aug 2004 07:36:37 -0700 (PDT) Date: Thu, 12 Aug 2004 07:36:37 -0700 (PDT) From: Stephan Szabo To: Philippe Lang Cc: pgsql-sql@postgresql.org Subject: Re: function expression in FROM may not refer to other relations In-Reply-To: <6C0CF58A187DA5479245E0830AF84F420803B7@poweredge.attiksystem.ch> Message-ID: <20040812073151.G56533@megazone.bigpanda.com> References: <6C0CF58A187DA5479245E0830AF84F420803B7@poweredge.attiksystem.ch> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII 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: 200408/116 X-Sequence-Number: 18613 On Thu, 12 Aug 2004, Philippe Lang wrote: > > > The problem now is that get_lines is being called twice per line. > > > > Is get_lines() defined as IMMUTABLE? Should it be? > > I have tried defining get_lines as "IMMUTABLE", or "WITH (iscachable)", > but it keeps on getting called twice per line in the following query... > > select > id, > usr, > code, > (get_lines(code)).line1, > (get_lines(code)).line2 > from tbl; > > I wish there was a way to run the query like this: > > select > id, > usr, > code, > CAST(get_lines(code) as lines) > from tbl; Note that select id, usr, code, get_lines(code) from tbl; should work in 8.0beta although you get the composite type as the last column, not two columns. sszabo=# select id, usr, code, get_lines(code) from tbl; id | usr | code | get_lines ----+-------+------+----------- 1 | one | 1 | (A,B) 2 | two | 2 | (Z,Z) 3 | three | 1 | (A,B) (3 rows) You can get individual columns with a bit of work and a subselect in from but you need to trick the system to not pull up the subselect to not get the function called twice per row.