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 AD38352E2A for ; Mon, 1 Aug 2005 18:43:53 -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 79069-03 for ; Mon, 1 Aug 2005 21:43:49 +0000 (GMT) Received: from mail.fruitfly.org (ci.lbl.gov [131.243.192.220]) by svr1.postgresql.org (Postfix) with ESMTP id 1996552E34 for ; Mon, 1 Aug 2005 18:43:48 -0300 (ADT) Received: from localhost (localhost [127.0.0.1]) by mail.fruitfly.org (Postfix on SuSE Linux eMail Server 3.0) with ESMTP id 4C7F120907B for ; Mon, 1 Aug 2005 14:43:50 -0700 (PDT) Received: from mail.fruitfly.org ([127.0.0.1]) by localhost (mail.fruitfly.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 28476-09 for ; Mon, 1 Aug 2005 14:43:49 -0700 (PDT) Received: from skerryvore.dhcp.lbl.gov (skerryvore.dhcp.lbl.gov [131.243.195.208]) by mail.fruitfly.org (Postfix on SuSE Linux eMail Server 3.0) with ESMTP id 2B94C45FA for ; Mon, 1 Aug 2005 14:43:49 -0700 (PDT) Date: Mon, 1 Aug 2005 14:43:52 -0700 (PDT) From: Chris Mungall X-X-Sender: cjm@skerryvore.dhcp.lbl.gov To: pgsql-sql@postgresql.org Subject: Calling SQL functions that return sets Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Virus-Scanned: by amavisd-new using ClamAV at fruitfly.org X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.05 tagged_above=0 required=5 tests=FORGED_RCVD_HELO X-Spam-Level: X-Archive-Number: 200508/6 X-Sequence-Number: 22234 Hi all, I have a question on functions returning sets. I have a lot of complex functionality I would like to keep in the DBMS rather than in an application, and I would like this functionality to be available as queries over standard SQL relations. Section 31.4.4 of the docs (http://www.postgresql.org/docs/8.0/static/xfunc-sql.html ) gives an example of calling a function which returns a set, with the function in the SELECT clause. It also states that this is deprecated, and that functions returning sets should be placed in the FROM clause of the query. However, unless I'm missing something it seems that arguments of a FROM-clause function cannot be used in the SELECT or WHERE clause. This seems to me a _huge_ limitation. The example given in 31.4.4 is a function CREATE FUNCTION listchildren(text) RETURNS SETOF text AS ... We can call it (using the deprecated syntax) like this: SELECT name, listchildren(name) FROM nodes; (where nodes is some relation with a text column called "name") I like this because I can implement a VIEW: CREATE VIEW name2child AS SELECT name, listchildren(name) FROM nodes; Of course, with the example given, it is easier to do this by directly querying the nodes relation. However, I am interested in the more general case whereby a relation can be implemented with an arbitrarily complex procedural pl/pgsql (or some other language) function, yet appear to have standard relational semantics. If I am now forced to move the function call to the FROM clause, it would appear to be impossible to implement the above view, or any equivalent for a function returning a set. This means that all the wonderful postgres machinery for defining complex functions is absolutely useless to me if I wish to retain standard relational semantics, and not have my code break with some future postgres version. What are the reasons for deprecating the use of the function in the SELECT clause? Am I missing something obvious? Thanks for any insight -- Chris Mungall