Re: Function returning 2 columns evaluated twice when both columns are needed

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Gerhard Wiesinger <lists(at)wiesinger(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Function returning 2 columns evaluated twice when both columns are needed
Date: 2009-10-18 21:00:03
Message-ID: 10515.1255899603@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Gerhard Wiesinger <lists(at)wiesinger(dot)com> writes:
> 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?

You need a sub-select, along the lines of

SELECT
cur_date,
(gs).sum_m1,
(gs).sum_m2
FROM
(
SELECT
cur_date,
getSums(start_ts, stop_ts) AS gs
FROM
getDatesTimestamps($1, $2)
OFFSET 0
) AS ss
;

The OFFSET bit is a kluge, but is needed to keep the planner from
flattening the subquery and undoing your work.

regards, tom lane

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Brent Wood 2009-10-18 21:32:13 Re: db not dumping properly, or at least not restoring
Previous Message Gerhard Wiesinger 2009-10-18 20:05:29 Function returning 2 columns evaluated twice when both columns are needed