Re: How to access multicolumn function results? - Mailing list pgsql-sql

From Tom Lane
Subject Re: How to access multicolumn function results?
Date
Msg-id 17722.1358991767@sss.pgh.pa.us
Whole thread Raw
In response to How to access multicolumn function results?  (Andreas <maps.on@gmx.net>)
List pgsql-sql
Andreas <maps.on@gmx.net> 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



pgsql-sql by date:

Previous
From: Andreas
Date:
Subject: How to access multicolumn function results?
Next
From: Kong Man
Date:
Subject: Writeable CTE Not Working?