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 1q7Xvs-0000Qp-DE for pgsql-hackers@arkaria.postgresql.org; Fri, 09 Jun 2023 08:57:20 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1q7Xvr-0006jc-3w for pgsql-hackers@arkaria.postgresql.org; Fri, 09 Jun 2023 08:57:19 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1q7Xvq-0006bL-J6 for pgsql-hackers@lists.postgresql.org; Fri, 09 Jun 2023 08:57:18 +0000 Received: from mail-yb1-xb32.google.com ([2607:f8b0:4864:20::b32]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1q7Xvm-0013Ao-Fl for pgsql-hackers@lists.postgresql.org; Fri, 09 Jun 2023 08:57:17 +0000 Received: by mail-yb1-xb32.google.com with SMTP id 3f1490d57ef6-b9e6ec482b3so1565698276.3 for ; Fri, 09 Jun 2023 01:57:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1686301033; x=1688893033; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=AAfxLVHwOENxs8qmsfh597wsf6WihJ2+9/TZnMz0fyo=; b=ctvmLHGd4ZILlVfuAjC0+A1rYMLChEfhUGuXJctqRgdZSu+Tpa4LjVMMch6K+1qLPN kKmD8az6QvOx5GSBPc2WKjIxbgxKRPTMBYZfaos4aMl356wBWp1agDRuAVzWvFvc9S4g 5Y3t5PujqJZpy6Cirabn0kISXR8vf4Si22ywDVMNpuyFB/ZZNO55+2CrCK63yX5Y8JlI mZFsMMjW8XLZwiKvIQ9eKNPTjcWC8wnC6W5cqETcPuxkFSOil/e+ijkcsAQiAiIcjN/z 5/KeFcl6VrWf/dYqZbmz8b5+jVN9f0kqBAlTPvRhQ2RkfNVK4bFoKXAYSeJCQ/fyahV1 L3/g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686301033; x=1688893033; 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=AAfxLVHwOENxs8qmsfh597wsf6WihJ2+9/TZnMz0fyo=; b=DNMSyuxNrP9sWdA08OGmPRBFaFXoQh6hXFWb3FV9eimIYfdokWLbDibPdlkM2IsAh1 JUkl2Jd7camCc8WzPH6Td9ysNm/CKU8DfeGJn+lZpAT7rG19PAw1IOOQ/ZHNDqPWF3r1 icSempFDJW7begV9Me6C4Wd+q07S7ZZZcx+xHByQhTrW27bLilFzcfSnYnelqf+QV0N/ dFe8ROXBxufDMkRhpR8rp84ithyWkqmbtMBktCK9KUfHnZjZsj5G3MSRJZLsHauM9YOM yc3j1gMvKe6eeqKyBkn1jZHR/j5rcINwpMA2rblychw68Fj58m6KDd8ekSYSASG2IG1I +Cnw== X-Gm-Message-State: AC+VfDziEpYK75jTC4irdVBEmhFJN5TL7qY+zsgFrVrh4BE6A7db2Jvt u0YhXkzWmHCNIVKnXuSldeczPUknthxOfYQipjKyRNvGfOw= X-Google-Smtp-Source: ACHHUZ6+V1yCBC5HhgcENAf1jgoHPjZBFIQtBjQ1C98s8K+0blIrVLWBXMOq1vTDFsuSk1j+jM4MpPSe4RHHmj6MDag= X-Received: by 2002:a25:d656:0:b0:bad:871:3fce with SMTP id n83-20020a25d656000000b00bad08713fcemr527007ybg.20.1686301033560; Fri, 09 Jun 2023 01:57:13 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Richard Guo Date: Fri, 9 Jun 2023 16:57:02 +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="00000000000016fddc05fdae8e6b" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --00000000000016fddc05fdae8e6b Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Fri, Jun 9, 2023 at 8:13=E2=80=AFAM David Rowley = wrote: > After looking again at nodeWindowAgg.c, I think it might be possible > to do a bit more work and apply this to ORDER BY items too. Without > an ORDER BY clause, all rows in the partition are peers of each other, > and if the ORDER BY column is redundant due to belonging to a > redundant pathkey, then those rows must also be peers too since the > redundant pathkey must mean all rows have an equal value in the > redundant column. > > However, there is a case where we must be much more careful. The > comment you highlighted in create_windowagg_plan() does mention this. > It reads "we must *not* remove the ordering column for RANGE OFFSET > cases". I see. I tried to run the query below select a, b, sum(a) over (order by b range between 10 preceding and current row) from t where b =3D 2; server closed the connection unexpectedly and if we've removed redundant items in wc->orderClause the query would trigger the Assert in update_frameheadpos(). /* We must have an ordering column */ Assert(node->ordNumCols =3D=3D 1); > > It might be possible to make adjustments in nodeWindowAgg.c to have > the equality checks come out as true when there is no ORDER BY. > update_frameheadpos() is one location that would need to be adjusted. > It would need further study to ensure we don't accidentally break > anything. I've not done that study, so won't be adjusting the patch > for now. I'm also not sure if doing that is safe in all cases. Hmm, do you think we can instead check wc->frameOptions to see if it is the RANGE OFFSET case in make_pathkeys_for_window(), and decide to not remove or remove redundant ORDER BY items according to whether it is or not RANGE OFFSET? Thanks Richard --00000000000016fddc05fdae8e6b Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

On Fri, Jun 9, 2023 at 8:13=E2=80=AFAM Da= vid Rowley <dgrowleyml@gmail.com= > wrote:
After looking again at nodeWindowAgg.c, I think it might be possible
to do a bit more work and apply this to ORDER BY items too.=C2=A0 Without an ORDER BY clause, all rows in the partition are peers of each other,
and if the ORDER BY column is redundant due to belonging to a
redundant pathkey, then those rows must also be peers too since the
redundant pathkey must mean all rows have an equal value in the
redundant column.

However, there is a case where we must be much more careful.=C2=A0 The
comment you highlighted in create_windowagg_plan() does mention this.
It reads "we must *not* remove the ordering column for RANGE OFFSET cases".

I see.=C2=A0 I tried to run th= e query below

select a, b, sum(a) over (order by b range between 10 = preceding and current row) from t where b =3D 2;
server closed the conne= ction unexpectedly

and if we've removed redundant items in wc-&g= t;orderClause the query would
trigger the Assert in update_frameheadpos(= ).

=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 /* We must have an orde= ring column */
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 Assert(node->= ;ordNumCols =3D=3D 1);
=C2=A0

It might be possible to make adjustments in nodeWindowAgg.c to have
the equality checks come out as true when there is no ORDER BY.
update_frameheadpos() is one location that would need to be adjusted.
It would need further study to ensure we don't accidentally break
anything. I've not done that study, so won't be adjusting the patch=
for now.

I'm also not sure if doing tha= t is safe in all cases.=C2=A0 Hmm, do you think
we can instead check wc-= >frameOptions to see if it is the RANGE OFFSET
case in make_pathkeys_= for_window(), and decide to not remove or remove
redundant ORDER BY item= s according to whether it is or not RANGE OFFSET?

Thanks
Richard<= br>
--00000000000016fddc05fdae8e6b--