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 1q7Bg6-0005oy-Md for pgsql-hackers@arkaria.postgresql.org; Thu, 08 Jun 2023 09:11:34 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1q7Bg5-000538-A8 for pgsql-hackers@arkaria.postgresql.org; Thu, 08 Jun 2023 09:11:33 +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 1q7Bg5-00052x-0R for pgsql-hackers@lists.postgresql.org; Thu, 08 Jun 2023 09:11:33 +0000 Received: from mail-yw1-x1131.google.com ([2607:f8b0:4864:20::1131]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1q7Bg2-000zSa-Ck for pgsql-hackers@lists.postgresql.org; Thu, 08 Jun 2023 09:11:32 +0000 Received: by mail-yw1-x1131.google.com with SMTP id 00721157ae682-565de553de1so6248337b3.0 for ; Thu, 08 Jun 2023 02:11:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1686215489; x=1688807489; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=Ur2mZwNAfaGUv3NQP11q5vPw3vzf9nDOpYBWJvzKqEs=; b=GaioeTi+bKMv3Ogj6k6gCVZRSGg8mMeHx321vmHYMjn/c4QEQPBP7jowZFVjUCiC1a +L2gqASddRkehzvmtSYKKNQpkC97WkieLFvV3LNZ8BPuz+G+ZJ90ClN4qhwKV50P2taA wp5n2YovwlX5hxY0nQ3MgBDik58a8fPRFLa8BqhmrcPr2BQZsNwvjqC7fLGwlWumkqvN 24CU1gGY+wwOjCcvcC7wjKKU6byfmHR5s5YTQeew7vg/7RKgyqQ8J1iBbZ5Kw2LjvQ7g Uk5Dit2LgKratjfFvwZbDGRIYqWZpN2zcNWzuqQPOwapiHJb8WBminu9DNYLKOq88h0G fFAQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686215489; x=1688807489; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=Ur2mZwNAfaGUv3NQP11q5vPw3vzf9nDOpYBWJvzKqEs=; b=cT7j2F/GP0mO//XBy/86paoeUKLHTVXU0wXl8l2AvWIsRr9C9c5UuBq0Kds6+nLN4I nZtlmfenjOM76BEftT/zHO+tJccSFXf40fHoVltAYT4OZhIBJ1P4mVfKsQkrYWdd4+dz VZzbLkXuv2D8hSzZd0xvVh53zuSx+zRZHrmlvJcIWMyUTCmWZJpwWqmn5vJNSWkUScR7 j4BHcHkiEMSDFHuJ30q+ccWQlXE511oyPPh9aNg02qBi00HhQjeF61CHtUc0ft16CH6K TkeD+JdYBHi60nqaCBd+6g5Ayh1r2uTvxKCg6xHV2hoKC3DUB1K8KEjJ652vbRd/TE4V JOrQ== X-Gm-Message-State: AC+VfDxWtxdcCfQEQUgw4rWQ0gRLBtEglkLK54OmuVBrgYQunEh/zbA2 E/dXbMsF7iHlzJWo3subk7Kf+4SEsB7Kx74GPorqO6UfCrI= X-Google-Smtp-Source: ACHHUZ7SHzRr8oRulZ4Ls2Vjl5TZnUckYLXyIz91ryu/LBwu01aW813uBFEdLDOLj/qNYZV2FSGgzf22f/y8qyhhYpo= X-Received: by 2002:a81:4896:0:b0:561:1cb6:f3d6 with SMTP id v144-20020a814896000000b005611cb6f3d6mr1969299ywa.0.1686215488871; Thu, 08 Jun 2023 02:11:28 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Richard Guo Date: Thu, 8 Jun 2023 17:11:17 +0800 Message-ID: Subject: Re: Remove WindowClause PARTITION BY items belonging to redundant pathkeys To: David Rowley Cc: PostgreSQL Developers Content-Type: multipart/alternative; boundary="0000000000003a9fcc05fd9aa324" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --0000000000003a9fcc05fd9aa324 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Thu, Jun 8, 2023 at 7:37=E2=80=AFAM David Rowley = wrote: > What the attached patch does is process each WindowClause and removes > any items from the PARTITION BY clause that are columns or expressions > relating to redundant PathKeys. > > Effectively, this allows the nodeWindowAgg.c code which stops > processing WindowAgg rows when the run condition is met to work as the > PARTITION BY clause is completely removed in the case of the above > query. Removing the redundant PARTITION BY items also has the added > benefit of not having to needlessly check if the next row belongs to > the same partition as the last row. For the above, that check is a > waste of time as all rows have relkind =3D 'r' This is a nice optimization. I reviewed it and here are my findings. In create_windowagg_plan there is such comment that says * ... Note: in principle, it's possible * to drop some of the sort columns, if they were proved redundant by * pathkey logic. However, it doesn't seem worth going out of our way to * optimize such cases. Since this patch removes any clauses from the wc->partitionClause for redundant pathkeys, this comment seems outdated, at least for the sort columns in partitionClause. Also I'm wondering if we can do the same optimization to wc->orderClause. I tested it with the query below and saw performance gains. create table t (a int, b int); insert into t select 1,2 from generate_series(1,100000)i; analyze t; explain analyze select * from (select a, b, rank() over (PARTITION BY a order by b) rank from t where b =3D 2) where a =3D 1 and rank <=3D 10; With and without this optimization to wc->orderClause the execution time is 67.279 ms VS. 119.120 ms (both best of 3). I notice you comment in the patch that doing this is unsafe because it would change the semantics of peer rows during execution. Would you please elaborate on that? Thanks Richard --0000000000003a9fcc05fd9aa324 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

On Thu, Jun 8, 2023 at 7:37=E2=80=AFAM Da= vid Rowley <dgrowleyml@gmail.com= > wrote:
What the attached patch does is process each WindowClause and removes
any items from the PARTITION BY clause that are columns or expressions
relating to redundant PathKeys.

Effectively, this allows the nodeWindowAgg.c code which stops
processing WindowAgg rows when the run condition is met to work as the
PARTITION BY clause is completely removed in the case of the above
query.=C2=A0 Removing the redundant PARTITION BY items also has the added benefit of not having to needlessly check if the next row belongs to
the same partition as the last row. For the above, that check is a
waste of time as all rows have relkind =3D 'r'
This is a nice optimization.=C2=A0 I reviewed it and here are m= y findings.

In create_windowagg_plan there is such comment that says=

* ... Note: in principle, it's possible
* to drop some of th= e sort columns, if they were proved redundant by
* pathkey logic.=C2=A0 = However, it doesn't seem worth going out of our way to
* optimize su= ch cases.

Since this patch removes any clauses from the wc->parti= tionClause for
redundant pathkeys, this comment seems outdated, at least= for the sort
columns in partitionClause.

Also I'm wondering = if we can do the same optimization to
wc->orderClause.=C2=A0 I tested= it with the query below and saw performance
gains.

create table = t (a int, b int);
insert into t select 1,2 from generate_series(1,100000= )i;
analyze t;

explain analyze
select * from
=C2=A0(select = a, b, rank() over (PARTITION BY a order by b) rank
=C2=A0 =C2=A0 from t = where b =3D 2)
where a =3D 1 and rank <=3D 10;

With and withou= t this optimization to wc->orderClause the execution time
is 67.279 m= s VS. 119.120 ms (both best of 3).

I notice you comment in the patch= that doing this is unsafe because it
would change the semantics of peer= rows during execution.=C2=A0 Would you
please elaborate on that?
Thanks
Richard
--0000000000003a9fcc05fd9aa324--