Received: from malur.postgresql.org ([217.196.149.56])
by arkaria.postgresql.org with esmtps
(TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256)
(Exim 4.92)
(envelope-from )
id 1p9QgO-0004a9-Oq
for pgsql-hackers@arkaria.postgresql.org; Sun, 25 Dec 2022 13:04:52 +0000
Received: from localhost ([127.0.0.1] helo=malur.postgresql.org)
by malur.postgresql.org with esmtp (Exim 4.92)
(envelope-from )
id 1p9QgN-0001am-Lu
for pgsql-hackers@arkaria.postgresql.org; Sun, 25 Dec 2022 13:04:51 +0000
Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29])
by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256)
(Exim 4.92)
(envelope-from )
id 1p9QgN-0001ad-9v
for pgsql-hackers@lists.postgresql.org; Sun, 25 Dec 2022 13:04:51 +0000
Received: from mail-pj1-x1031.google.com ([2607:f8b0:4864:20::1031])
by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128)
(Exim 4.92)
(envelope-from )
id 1p9QgG-000406-Tw
for pgsql-hackers@lists.postgresql.org; Sun, 25 Dec 2022 13:04:50 +0000
Received: by mail-pj1-x1031.google.com with SMTP id
gv5-20020a17090b11c500b00223f01c73c3so10762567pjb.0
for ;
Sun, 25 Dec 2022 05:04:44 -0800 (PST)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=gmail.com; s=20210112;
h=subject:to:from:content-language:user-agent:mime-version:date
:message-id:from:to:cc:subject:date:message-id:reply-to;
bh=oS68atrkxgHf5/7/q33lwfwFlrqHrKqXtefhAY4u//I=;
b=e2TVcXBp4ubcY3xuQOobIy26VFxE7MLoqm+33HfIBbFTsRXgFonRv79kUsXJcnrEPu
b5X5pKJvavtKUUrhCjS8gOp4v8id9qXSib1f7TzS2/vD9egOL6VCiiEovemNLeTEkwr0
mAgyUZ6UJZL9lKuVGBfu3ZvaD7klb08yRbAjxFSiEhXpI6hKzqHrvEazUDigBqNDC39D
yKIPlU85yWKWDE29jh5hPyWbsZk9XgAlqfFHxXVwzCxVa0kEYzM6Bi61aKm+D+9x/yQc
7BQbB6F2It8CxfGcLPQMHrYTvmjrGY4p4YgVTumkP/cycBsyrE9/xRMZcE54unpWU5wU
uaBg==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20210112;
h=subject:to:from:content-language:user-agent:mime-version:date
:message-id:x-gm-message-state:from:to:cc:subject:date:message-id
:reply-to;
bh=oS68atrkxgHf5/7/q33lwfwFlrqHrKqXtefhAY4u//I=;
b=exM3dHxCDP9pof1Gr7vDi38BXPbtKIRundFfJJ53wJ2yX6LRy+blAuBHW1GawWANIh
zN+0sVFFER0ny23XO8obIPyFyq6b/ndM64ipiC/8YSjCLl4vDpvVBCOKbT0zFsFdtM6+
seOLwayN9yRDSXX5atx004kljEGJbadfxYpOH04JIKEUEzXEoM1WSHDmzqxlCv6dws29
8wrjdM9qjKg+MRlYViQk35cRdEko4tXSCdJtcSWq+tRyO1bUt73TpoDTK129a98lYlaG
eUZrH6wHsQInhafQ8+8ZXGHl/wWeJ4ltJ/0qtWz8hdhqI6jvXVRVKoqbGPvc3kQftuPG
u+yg==
X-Gm-Message-State: AFqh2ko4y0ONncBShl0Tr225LgmIRZDCTAsHy8jUd94Jr4sPAvLzZ6bM
GWsXIAUIxit55uGRrUsOAznTCjX/P1w=
X-Google-Smtp-Source:
AMrXdXteh2a6/ww7dS026WCYFFvEe9nQnIY23NCkCQ43OTA8uVsfY+NyopTDP0TyfYfuuwgzwUiFQw==
X-Received: by 2002:a17:90a:8d0d:b0:223:f35a:be29 with SMTP id
c13-20020a17090a8d0d00b00223f35abe29mr18182491pjo.3.1671973482254;
Sun, 25 Dec 2022 05:04:42 -0800 (PST)
Received: from [10.0.2.15] ([110.226.219.83])
by smtp.gmail.com with ESMTPSA id
i5-20020a17090a3d8500b002191a64b5d5sm7216954pjc.18.2022.12.25.05.04.40
(version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128);
Sun, 25 Dec 2022 05:04:41 -0800 (PST)
Content-Type: multipart/alternative;
boundary="------------0GJdxpMnNviE0JuE5SkBDE00"
Message-ID: <83d80853-a45c-d85c-68eb-59acfe7fb5fb@gmail.com>
Date: Sun, 25 Dec 2022 18:34:38 +0530
MIME-Version: 1.0
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.4.2
Content-Language: en-US
From: Ankit Kumar Pandey
To: pghackers ,
David Rowley
Subject: Todo: Teach planner to evaluate multiple windows in the optimal order
List-Id:
List-Help:
List-Subscribe:
List-Post:
List-Owner:
List-Archive:
Archived-At:
Precedence: bulk
This is a multi-part message in MIME format.
--------------0GJdxpMnNviE0JuE5SkBDE00
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Hi,
While looking at one of the todo item in Window function, namely:
/Teach planner to evaluate multiple windows in the optimal order
Currently windows are always evaluated in the query-specified order./
From threads, relevant points.
Point #1
> In the above query Oracle 10g performs 2 sorts, DB2 and Sybase perform 3
> sorts. We also perform 3.
and Point #2
> Teach planner to decide which window to evaluate first based on costs.
> Currently the first window in the query is evaluated first, there may
> be no
> index to help sort the first window, but perhaps there are for other
> windows
> in the query. This may allow an index scan instead of a seqscan -> sort.
Repro:
select pg_catalog.version();
/version //
//----------------------------------------------------------------------------------------------------//
// PostgreSQL 16devel on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu
12.2.0-3ubuntu1) 12.2.0, 64-bit//
//(1 row)/
create table empsalary(depname text, empno int, salary int);
insert into empsalary values (select substr(md5(random()::text), 0, 25),
generate_series(1,10), generate_series(10000,12000));
explain SELECT depname, SUM(salary) OVER (ORDER BY salary), SUM(salary)
OVER (ORDER BY empno) FROM empsalary ORDER BY salary;
/ QUERY PLAN //
//--------------------------------------------------------------------------------------//
// WindowAgg (cost=289.47..324.48 rows=2001 width=49)//
// -> Sort (cost=289.47..294.47 rows=2001 width=41)//
// Sort Key: salary//
// -> WindowAgg (cost=144.73..179.75 rows=2001 width=41)//
// -> Sort (cost=144.73..149.73 rows=2001 width=33)//
// Sort Key: empno//
// -> Seq Scan on empsalary (cost=0.00..35.01
rows=2001 width=33)//
//(7 rows)/
As it is seen, for case #1, issue looks like resolved and only 2 sorts
are performed.
For #2, index col ordering is changed.
create index idx_emp on empsalary (empno);
explain SELECT depname, SUM(salary) OVER (ORDER BY salary), SUM(salary)
OVER (ORDER BY empno) FROM empsalary ORDER BY salary;
/ QUERY PLAN //
//------------------------------------------------------------------------------------------------//
// WindowAgg (cost=204.03..239.04 rows=2001 width=49)//
// -> Sort (cost=204.03..209.03 rows=2001 width=41)//
// Sort Key: salary//
// -> WindowAgg (cost=0.28..94.31 rows=2001 width=41)//
// -> Index Scan using idx_emp on empsalary
(cost=0.28..64.29 rows=2001 width=33)//
//(5 rows)/
explain SELECT depname, SUM(salary) OVER (ORDER BY empno), SUM(salary)
OVER (ORDER BY salary) FROM empsalary ORDER BY salary;
/ QUERY PLAN //
//------------------------------------------------------------------------------------------------//
// WindowAgg (cost=204.03..239.04 rows=2001 width=49)//
// -> Sort (cost=204.03..209.03 rows=2001 width=41)//
// Sort Key: salary//
// -> WindowAgg (cost=0.28..94.31 rows=2001 width=41)//
// -> Index Scan using idx_emp on empsalary
(cost=0.28..64.29 rows=2001 width=33)//
//(5 rows)/
In both cases, index scan is performed, which means this issue is
resolved as well.
Is this todo still relevant?
Further down the threads:
> I do think the patch has probably left some low-hanging fruit on the
> simpler end of the difficulty spectrum, namely when the window stuff
> requires only one ordering that could be done either explicitly or
> by an indexscan. That choice should ideally be done with a proper
> cost comparison taking any LIMIT into account. I think right now
> the LIMIT might not be accounted for, or might be considered even
> when it shouldn't be because another sort is needed anyway.
I am not sure if I understand this fully but does it means proposed todo
(to use indexes) should be refined to
teach planner to take into account of cost model while deciding to use
index or not in window functions? Meaning not always go with index route
(modify point #2)?
--
Regards,
Ankit Kumar Pandey
--------------0GJdxpMnNviE0JuE5SkBDE00
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: 8bit
Hi,
While looking at one of the todo item in Window function, namely:
Teach planner to evaluate multiple windows in the optimal
order
Currently windows are always evaluated in the query-specified
order.
From threads, relevant points.
Point #1
In the above query Oracle 10g performs 2 sorts,
DB2 and Sybase perform 3
sorts. We also perform 3.
and Point #2
Teach planner to decide which window to evaluate
first based on costs.
Currently the first window in the query is evaluated first,
there may be no
index to help sort the first window, but perhaps there are for
other windows
in the query. This may allow an index scan instead of a
seqscan -> sort.
Repro:
select pg_catalog.version();
version
----------------------------------------------------------------------------------------------------
PostgreSQL 16devel on x86_64-pc-linux-gnu, compiled by gcc
(Ubuntu 12.2.0-3ubuntu1) 12.2.0, 64-bit
(1 row)
create table empsalary(depname text, empno int, salary int);
insert into empsalary values (select substr(md5(random()::text),
0, 25), generate_series(1,10), generate_series(10000,12000));
explain SELECT depname, SUM(salary) OVER (ORDER BY salary),
SUM(salary) OVER (ORDER BY empno) FROM empsalary ORDER BY salary;
QUERY
PLAN
--------------------------------------------------------------------------------------
WindowAgg (cost=289.47..324.48 rows=2001 width=49)
-> Sort (cost=289.47..294.47 rows=2001 width=41)
Sort Key: salary
-> WindowAgg (cost=144.73..179.75 rows=2001
width=41)
-> Sort (cost=144.73..149.73 rows=2001
width=33)
Sort Key: empno
-> Seq Scan on empsalary
(cost=0.00..35.01 rows=2001 width=33)
(7 rows)
As it is seen, for case #1, issue looks like resolved and only 2
sorts are performed.
For #2, index col ordering is changed.
create index idx_emp on empsalary (empno);
explain SELECT depname, SUM(salary) OVER (ORDER BY salary),
SUM(salary) OVER (ORDER BY empno) FROM empsalary ORDER BY salary;
QUERY
PLAN
------------------------------------------------------------------------------------------------
WindowAgg (cost=204.03..239.04 rows=2001 width=49)
-> Sort (cost=204.03..209.03 rows=2001 width=41)
Sort Key: salary
-> WindowAgg (cost=0.28..94.31 rows=2001
width=41)
-> Index Scan using idx_emp on
empsalary (cost=0.28..64.29 rows=2001 width=33)
(5 rows)
explain SELECT depname, SUM(salary) OVER (ORDER BY empno),
SUM(salary) OVER (ORDER BY salary) FROM empsalary ORDER BY salary;
QUERY
PLAN
------------------------------------------------------------------------------------------------
WindowAgg (cost=204.03..239.04 rows=2001 width=49)
-> Sort (cost=204.03..209.03 rows=2001 width=41)
Sort Key: salary
-> WindowAgg (cost=0.28..94.31 rows=2001
width=41)
-> Index Scan using idx_emp on
empsalary (cost=0.28..64.29 rows=2001 width=33)
(5 rows)
In both cases, index scan is performed, which means this issue is
resolved as well.
Is this todo still relevant?
Further down the threads:
I do think the patch has probably left some
low-hanging fruit on the
simpler end of the difficulty spectrum, namely when the window
stuff
requires only one ordering that could be done either
explicitly or
by an indexscan. That choice should ideally be done with a
proper
cost comparison taking any LIMIT into account. I think right
now
the LIMIT might not be accounted for, or might be considered
even
when it shouldn't be because another sort is needed anyway.
I am not sure if I understand this fully but does it means
proposed todo (to use indexes) should be refined to
teach planner to take into account of cost model while deciding
to use index or not in window functions? Meaning not always go
with index route (modify point #2)?
--
Regards,
Ankit Kumar Pandey
--------------0GJdxpMnNviE0JuE5SkBDE00--