Re: Pull up aggregate sublink (was: Parameterized aggregate subquery (was: Pull up aggregate subquery))

Tom Lane <tgl@sss.pgh.pa.us>

From: Tom Lane <tgl@sss.pgh.pa.us>
To: Yeb Havinga <yebhavinga@gmail.com>
Cc: Robert Haas <robertmhaas@gmail.com>, Hitoshi Harada <umi.tanuki@gmail.com>, PostgreSQL-development <pgsql-hackers@postgresql.org>
Date: 2011-07-27T14:51:13Z
Lists: pgsql-hackers
Yeb Havinga <yebhavinga@gmail.com> writes:
> Tom and Robert, thank you both for your replies. I think I'm having some 
> blind spots and maybe false assumptions regarding the overal work in the 
> optimizer, as it is not clear to me what 'the same sort of query' would 
> look like. I was under the impression that using cost to select the best 
> paths is only done per simple query, and fail to see how a total 
> combined plan with pulled up subquery could be compared on cost with a 
> total plan where the subquery is still a separate subplan, since the 
> range tables / simple-queries to compare are different.

Well, you could look at what planagg.c does to decide whether an
indexscan optimization of MIN/MAX is worthwhile, or at the calculations
in planner.c that decide which way to do grouping/aggregation/ordering.

It could be fairly expensive to handle this type of problem because of
the need to cost out two fundamentally different scan/join trees, but
we're assuming the queries are expensive enough to make that worthwhile
...

			regards, tom lane