Re: function not called if part of aggregate

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "Craig A(dot) James" <cjames(at)modgraph-usa(dot)com>
Cc: pgsql-performance(at)postgresql(dot)org
Subject: Re: function not called if part of aggregate
Date: 2006-06-11 17:39:30
Message-ID: 18419.1150047570@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

"Craig A. James" <cjames(at)modgraph-usa(dot)com> writes:
> select count(1) from (select foo_init(value) from foo_init_table order by value_id) as foo;

> And indeed, it count() returns 800, as expected. But my function foo_init() never gets called!

Really? With the ORDER BY in there, it does get called, in my
experiments. What PG version is this exactly?

However, the short answer to your question is that PG does not guarantee
to evaluate parts of the query not needed to determine the result. You
could do something like

select count(x) from (select foo_init(value) as x from foo_init_table order by value_id) as foo;

to ensure that foo_init() must be evaluated.

regards, tom lane

In response to

Browse pgsql-performance by date

  From Date Subject
Next Message Jim C. Nasby 2006-06-11 17:48:37 Re: function not called if part of aggregate
Previous Message Steinar H. Gunderson 2006-06-11 17:31:55 Re: function not called if part of aggregate