Robert Haas <robertmh...@gmail.com> writes: > But that's also assuming that you're correct here about how to descend > through a JoinExpr, which I'm not quite sure whether is true. It's > also assuming that we should solve the problem here rather than in > some other part of the code e.g. the join removal code, and I'm not > sure about that either.
The actual problem here is that remove_useless_joins hasn't run yet. It's called inside query_planner which happens only after we do preprocess_minmax_aggregates. So I think this patch is a dead end. It's not possible for it to correctly predict whether remove_useless_joins will remove the join, short of repeating all that work which we surely don't want. (I'm a bit surprised that it hasn't visibly broken existing test cases.) It might be possible to move preprocess_minmax_aggregates to happen after join removal, but I fear it'd require some pretty fundamental rethinking of how it generates indexscan paths --- recursively calling query_planner seems dubious. (But maybe that'd work? The modified query should no longer contain aggs, so we wouldn't recurse again.) preprocess_minmax_aggregates is pretty much of a hack anyway. If you read the comments, it's just full of weird stuff that it has to duplicate from other places, or things that magically work because the relevant stuff isn't possible in this query, etc. Maybe it's time to think about nuking it from orbit and doing a fresh implementation in some other place that's a better fit. I have no immediate ideas about what that should look like, other than it'd be better if it happened after join removal. regards, tom lane