Function returning 2 columns evaluated twice when both columns are needed

From: Gerhard Wiesinger <lists(at)wiesinger(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Function returning 2 columns evaluated twice when both columns are needed
Date: 2009-10-18 20:05:29
Message-ID: alpine.LFD.2.00.0910182157050.9960@bbs.intern
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hello,

I'm having a problem with the following:
CREATE TYPE Sums AS (sum_m1 double precision, sum_m2 double precision);
CREATE TYPE date_m1_m2 AS (cur_date date, sum_m1 double precision, sum_m2 double precision);

CREATE OR REPLACE FUNCTION getSums(IN start_ts timestamp with time
zone, IN stop_ts timestamp with time zone) RETURNS Sums AS $$
...

CREATE OR REPLACE FUNCTION getsumInterval(date, date) RETURNS SETOF date_m1_m2 AS $$
SELECT
cur_date,
(getSums(start_ts, stop_ts)).* -- No optimal since function is evaluated 2 times => 24s
-- getSums(start_ts, stop_ts) -- in one column and not usable as I need 2 columns, but takes only 12s
FROM
getDatesTimestamps($1, $2)
;
$$ LANGUAGE SQL;

Since getSums() is a cursor and is complex and takes long time getSums
should only be evaluated once. Is there a better solution available to
get both columns from the function in the select?

Thnx.

Ciao,
Gerhard

--
http://www.wiesinger.com/

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2009-10-18 21:00:03 Re: Function returning 2 columns evaluated twice when both columns are needed
Previous Message Simon Riggs 2009-10-18 19:13:41 Re: Delete fails with out of memory