Philip Warner <pjw(at)rhyme(dot)com(dot)au> writes:
> Is there any way to get PG to know that it only needs to do the
> aggregate once?
It'd probably be possible to look for duplicated aggrefs being assigned
to the same Agg plan node during planning. However, I'm not entirely
convinced that it's worth the trouble --- the individual transition
function calls are not usually all that expensive.
But ... the example you are offering has nothing to do with aggregates.
Subplans are a different and much messier deal. The best I could offer
you (short of a complete redesign of subqueries) would be to not pull up
views that have any subqueries, which would probably be a net loss.
regards, tom lane