Function returning 2 columns evaluated twice when both columns are needed - Mailing list pgsql-general

From Gerhard Wiesinger
Subject Function returning 2 columns evaluated twice when both columns are needed
Date
Msg-id alpine.LFD.2.00.0910182157050.9960@bbs.intern
Whole thread Raw
Responses Re: Function returning 2 columns evaluated twice when both columns are needed
List 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/

pgsql-general by date:

Previous
From: Simon Riggs
Date:
Subject: Re: Delete fails with out of memory
Next
From: Tom Lane
Date:
Subject: Re: Function returning 2 columns evaluated twice when both columns are needed