Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TyBq9-0007e4-Aq for pgsql-sql@arkaria.postgresql.org; Thu, 24 Jan 2013 01:42:53 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1TyBq8-0000uv-O8 for pgsql-sql@arkaria.postgresql.org; Thu, 24 Jan 2013 01:42:52 +0000 Received: from makus.postgresql.org ([98.129.198.125]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TyBq8-0000uV-2R for pgsql-sql@postgresql.org; Thu, 24 Jan 2013 01:42:52 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TyBq6-0003WN-Ia for pgsql-sql@postgresql.org; Thu, 24 Jan 2013 01:42:51 +0000 Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.5/8.14.5) with ESMTP id r0O1glsk017723; Wed, 23 Jan 2013 20:42:47 -0500 (EST) From: Tom Lane To: Andreas cc: pgsql-sql@postgresql.org Subject: Re: How to access multicolumn function results? In-reply-to: <51008731.8020006@gmx.net> References: <51008731.8020006@gmx.net> Comments: In-reply-to Andreas message dated "Thu, 24 Jan 2013 01:58:25 +0100" Date: Wed, 23 Jan 2013 20:42:47 -0500 Message-ID: <17722.1358991767@sss.pgh.pa.us> X-Pg-Spam-Score: -1.9 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-sql Precedence: bulk Sender: pgsql-sql-owner@postgresql.org Andreas writes: > SELECT some_fct( some_id ) FROM some_other_table; > How can I split this up to look like a normal table or view with the > column names that are defined in the RETURNS TABLE ( ... ) expression of > the function. The easy way is SELECT (some_fct(some_id)).* FROM some_other_table; If you're not too concerned about efficiency, you're done. However this isn't very efficient, because the way the parser deals with expanding the "*" is to make N copies of the function call, as you can see with EXPLAIN VERBOSE --- you'll see something similar to Output: (some_fct(some_id)).fld1, (some_fct(some_id)).fld2, ... If the function is expensive enough that that's a problem, the basic way to fix it is SELECT (ss.x).* FROM (SELECT some_fct(some_id) AS x FROM some_other_table) ss; With a RETURNS TABLE function, this should be good enough. With simpler functions you might have to insert OFFSET 0 into the sub-select to keep the planner from "flattening" it into the upper query and producing the same multiple-evaluation situation. regards, tom lane -- Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql