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 1pDGfp-0007Jb-3p for pgsql-hackers@arkaria.postgresql.org; Thu, 05 Jan 2023 03:12:09 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pDGfo-0007gc-0u for pgsql-hackers@arkaria.postgresql.org; Thu, 05 Jan 2023 03:12:08 +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 1pDGfn-0007gS-OX for pgsql-hackers@lists.postgresql.org; Thu, 05 Jan 2023 03:12:07 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pDGfl-0007y7-Im for pgsql-hackers@lists.postgresql.org; Thu, 05 Jan 2023 03:12:06 +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 3053C4A92410905; Wed, 4 Jan 2023 22:12:04 -0500 From: Tom Lane To: David Rowley cc: Vik Fearing , Ankit Kumar Pandey , pghackers Subject: Re: Todo: Teach planner to evaluate multiple windows in the optimal order In-reply-to: References: <83d80853-a45c-d85c-68eb-59acfe7fb5fb@gmail.com> <7f77ee7d-bd04-d8e2-bb34-42395fd1f7c2@gmail.com> <6ebcc137-45ea-6373-e3e1-3c304c452e1f@gmail.com> <441d135e-1941-c3ef-1649-18c3e8811549@postgresfriends.org> Comments: In-reply-to David Rowley message dated "Thu, 05 Jan 2023 15:30:59 +1300" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <2410903.1672888324.1@sss.pgh.pa.us> Date: Wed, 04 Jan 2023 22:12:04 -0500 Message-ID: <2410904.1672888324@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk David Rowley writes: > Additionally, it's also not that clear to me that sorting by more > columns in the sort below the WindowAgg would always be a win over > doing the final sort for the ORDER BY. What if the WHERE clause (that > could not be pushed down before a join) filtered out the vast majority > of the rows before the ORDER BY. It might be cheaper to do the sort > then than to sort by the additional columns earlier. That's certainly a legitimate question to ask, but I don't quite see where you figure we'd be sorting more rows? WHERE filtering happens before window functions, which never eliminate any rows. So it seems like a sort just before the window functions must sort the same number of rows as one just after them. regards, tom lane