How can the Aggregation move to the outer query

From: Andy Fan <zhihui(dot)fan1213(at)gmail(dot)com>
To: PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: How can the Aggregation move to the outer query
Date: 2021-05-25 10:28:40
Message-ID: CAKU4AWoGHOJ2+DxN4yHHMWH6fHXDG6099+ZW16A1WtcQWd=UsA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

My question can be demonstrated with the below example:

create table m1(a int, b int);
explain (costs off) select (select count(*) filter (*where true*) from m1
t1)
from m1 t2 where t2.b % 2 = 1;

QUERY PLAN
---------------------------------
Seq Scan on m1 t2
Filter: ((b % 2) = 1)
InitPlan 1 (returns $0)
-> Aggregate
-> Seq Scan on m1 t1
(5 rows)

The above is good to me. The aggregate is run in the subPlan/InitPlan.

explain (costs off) select (select count(*) filter (*where t2.b = 1*) from
m1 t1)
from m1 t2 where t2.b % 2 = 1;

QUERY PLAN
-------------------------------
Aggregate
-> Seq Scan on m1 t2
Filter: ((b % 2) = 1)
SubPlan 1
-> Seq Scan on m1 t1
(5 rows)

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?

--
Best Regards
Andy Fan (https://www.aliyun.com/)

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Aleksander Alekseev 2021-05-25 10:55:13 Add ZSON extension to /contrib/
Previous Message Bharath Rupireddy 2021-05-25 10:21:09 Re: Skipping logical replication transactions on subscriber side