> Okay, I'm convinced: the problem is that the test for ungrouped vars
> used inside subselects is too simplistic.
Not only was that true, but the handling of GROUP BY expressions was
pretty grotty in general: they'd be re-evaluated at multiple levels of
the resulting plan tree. Which is not too bad for "GROUP BY a+b",
but it's unpleasant when a complex subselect is involved.
I've committed fixes to CVS.
regards, tom lane