Re: Todo: Teach planner to evaluate multiple windows in the optimal order

From: Ankit Kumar Pandey <itsankitkp(at)gmail(dot)com>
To: Vik Fearing <vik(at)postgresfriends(dot)org>, David Rowley <dgrowleyml(at)gmail(dot)com>
Cc: pghackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: Todo: Teach planner to evaluate multiple windows in the optimal order
Date: 2023-01-08 17:05:08
Message-ID: 2eb403f8-9bc8-a743-2ddf-2cd96e73536d@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers


> On 08/01/23 21:36, Vik Fearing wrote:

> On 1/8/23 11:21, Ankit Kumar Pandey wrote:
>>
>> Please find attached patch with addressed issues mentioned before.

> I am curious about this plan:

> +-- ORDER BY's in multiple Window functions can be combined into one
> +-- if they are subset of QUERY's ORDER BY
> +EXPLAIN (COSTS OFF)
> +SELECT empno,
> + depname,
> + min(salary) OVER (PARTITION BY depname ORDER BY empno) depminsalary,
> + sum(salary) OVER (PARTITION BY depname) depsalary,
> + count(*) OVER (ORDER BY enroll_date DESC) c
> +FROM empsalary
> +ORDER BY depname, empno, enroll_date;
> + QUERY PLAN
> +------------------------------------------------------
> + WindowAgg
> + -> WindowAgg
> + -> Sort
> + Sort Key: depname, empno, enroll_date
> + -> WindowAgg
> + -> Sort
> + Sort Key: enroll_date DESC
> + -> Seq Scan on empsalary
> +(8 rows)
> +

> Why aren't min() and sum() calculated on the same WindowAgg run?

Isn't that exactly what is happening here? First count() with sort on
enroll_date is run and

then min() and sum()?

Only difference between this and plan generated by master(given below)
is a sort in the end.

QUERY PLAN
------------------------------------------------------------
Incremental Sort
Sort Key: depname, empno, enroll_date
Presorted Key: depname, empno
-> WindowAgg
-> WindowAgg
-> Sort
Sort Key: depname, empno
-> WindowAgg
-> Sort
Sort Key: enroll_date DESC
-> Seq Scan on empsalary

Let me know if I am missing anything. Thanks.

--
Regards,
Ankit Kumar Pandey

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Ankit Kumar Pandey 2023-01-08 17:17:19 Re: Todo: Teach planner to evaluate multiple windows in the optimal order
Previous Message Justin Pryzby 2023-01-08 16:48:49 Re: Progress report of CREATE INDEX for nested partitioned tables