Re: Avoiding evaluating functions twice.

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: han(dot)holl(at)informationslogik(dot)nl
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Avoiding evaluating functions twice.
Date: 2005-10-04 15:25:17
Message-ID: 8569.1128439517@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

han(dot)holl(at)informationslogik(dot)nl writes:
> select expensive_function(table) from table
> where expensive_function(table) is not null;

> Is there a way to avoid that expensive_function is evaluated twice (if it's
> not null) ?

You can do something like this:

select f from
(select expensive_function(table) as f from table offset 0) ss
where f is not null;

The "offset 0" bit is a hack that keeps the planner from flattening the
sub-select into the upper query, which would result in two copies of the
function expression, which is what you want to avoid.

regards, tom lane

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Sven Willenberger 2005-10-04 16:22:52 Re: strip zeros from fractional part
Previous Message A. Kretschmer 2005-10-04 15:00:09 Re: export a select result in a file ?