Re: How can the Aggregation move to the outer query

Поиск
Список
Период
Сортировка
От Tom Lane
Тема Re: How can the Aggregation move to the outer query
Дата
Msg-id 1209511.1621952630@sss.pgh.pa.us
обсуждение исходный текст
Ответ на Re: How can the Aggregation move to the outer query  (David Rowley <dgrowleyml@gmail.com>)
Ответы Re: How can the Aggregation move to the outer query  (Andy Fan <zhihui.fan1213@gmail.com>)
Список pgsql-hackers
David Rowley <dgrowleyml@gmail.com> writes:
> On Tue, 25 May 2021 at 22:28, Andy Fan <zhihui.fan1213@gmail.com> wrote:
>> explain (costs off) select  (select count(*) filter (where t2.b = 1) from m1 t1)
>> from m1 t2 where t2.b % 2 = 1;
>>
>> This one is too confusing to me since the Aggregate happens
>> on t2 rather than t1.  What happens here? Would this query
>> generate 1 row all the time like SELECT aggfunc(a) FROM t?

> I think you're misreading the plan. There's a scan on t2 with a
> subplan then an aggregate on top of that. Because you made the
> subquery correlated by adding t2.b, it cannot be executed as an
> initplan.

Also keep in mind that adding that filter clause completely changed
the meaning of the aggregate.  Aggregates belong to the lowest
query level containing any Var used in their arguments, so that
where in your original query the count(*) was an aggregate of the
subquery, now it's an aggregate of the outer query (and the subquery
now perceives it as a constant outer reference).  AFAIR this is per
SQL spec.

            regards, tom lane



В списке pgsql-hackers по дате отправления:

Предыдущее
От: Andrew Dunstan
Дата:
Сообщение: Re: pg_rewind fails if there is a read only file.
Следующее
От: Tom Lane
Дата:
Сообщение: Re: pg_rewind fails if there is a read only file.