Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tqhhW-0095LV-R8 for pgsql-bugs@arkaria.postgresql.org; Sat, 08 Mar 2025 00:05:58 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1tqhhT-002wq6-5Q for pgsql-bugs@arkaria.postgresql.org; Sat, 08 Mar 2025 00:05:55 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tqhhS-002wpy-Su for pgsql-bugs@lists.postgresql.org; Sat, 08 Mar 2025 00:05:54 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1tqhhR-001aHP-0A for pgsql-bugs@lists.postgresql.org; Sat, 08 Mar 2025 00:05:54 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 52805pWp1147308; Fri, 7 Mar 2025 19:05:51 -0500 From: Tom Lane To: Christopher Inokuchi cc: Greg Sabino Mullane , pgsql-bugs@lists.postgresql.org Subject: Re: Window Functions with identical PARTITION BY and ORDER BY clauses evaluated separately In-reply-to: References: Comments: In-reply-to Christopher Inokuchi message dated "Fri, 07 Mar 2025 12:53:03 -0800" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <1147306.1741392351.1@sss.pgh.pa.us> Date: Fri, 07 Mar 2025 19:05:51 -0500 Message-ID: <1147307.1741392351@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Christopher Inokuchi writes: > Was it really not intentional that the docs explicitly name PARTITION BY > and ORDER BY rather than the entire window_definition? If I understand > correctly, only those two clauses control which records are hit and in what > order. Yeah, it's intentional, and in fact required by the SQL standard. However, you're misinterpreting what the guarantee is. The spec requirement is that window functions sharing PARTITION BY and ORDER BY all be evaluated on the same concrete ordering of the data, ie there can't be any re-sorting between them. And that's what we implement. We do use a separate WindowAgg node for each distinguishable window specification, but you'll notice there is not a Sort step between them unless the query involves entirely-incompatible PARTITION/ORDER BY specs. Perhaps the wording in section 7.2.5 could be improved; I agree that "evaluated in one pass" is capable of being read in more than one way, and it's not clear that it's referring to sorts. Do you have any suggestions for clearer wording? It's possible that we could restructure things so that window functions having distinct frame clauses were nonetheless done in one WindowAgg node. But it would complicate the code and it's far from obvious to me that it'd buy much in speed. Optimizing the sort steps is where most of the potential win lies. regards, tom lane