pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Andreas <maps.on@gmx.net>
Cc: pgsql-sql@postgresql.org
Subject: Re: How to access multicolumn function results?
Date: Wed, 23 Jan 2013 20:42:47 -0500
Message-ID: <17722.1358991767@sss.pgh.pa.us> (raw)
In-Reply-To: <51008731.8020006@gmx.net>
References: <51008731.8020006@gmx.net>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-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


-- 
Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql



view thread (2+ messages)

Message-ID: <17722.1358991767@sss.pgh.pa.us>
Permalink:  ../17722.1358991767@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/17722.1358991767@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: tgl@sss.pgh.pa.us, maps.on@gmx.net
  Subject: Re: How to access multicolumn function results?
  In-Reply-To: <17722.1358991767@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox