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 1pEP0i-0004iN-OZ for pgsql-hackers@arkaria.postgresql.org; Sun, 08 Jan 2023 06:18:24 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pEP0g-0002p9-LW for pgsql-hackers@arkaria.postgresql.org; Sun, 08 Jan 2023 06:18:22 +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 1pEP0g-0002oz-Au for pgsql-hackers@lists.postgresql.org; Sun, 08 Jan 2023 06:18:22 +0000 Received: from mail-pl1-x631.google.com ([2607:f8b0:4864:20::631]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1pEP0W-0001QW-KI for pgsql-hackers@lists.postgresql.org; Sun, 08 Jan 2023 06:18:21 +0000 Received: by mail-pl1-x631.google.com with SMTP id d15so6144157pls.6 for ; Sat, 07 Jan 2023 22:18:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=W5bjf9+Ex+RegmcG2FUEI2Ls8g+CeCoTmsbEyu+5anw=; b=KWO8f/8gaNDiBzExiYPZFe8hph969AZ1fH6xuyCbR4muhROdINSj0c8/KX1ajzpq7x OCXi9v0XcYn4MELQQCIA3ssI5S67QxWm8WLgvkX2FoCraQUoLQKJKq/wj30idzJa07i2 NcCuvbWSgRZhsX2FXvjJO5riwmbYKHBnTiaurohG7YX24FyGm6/6huMZ1I/s2Gk09N7Z CEAnqRyiF1am1C0AomFXx4XjF6hqaZYPuJpoYsE3LN24EgGk7yyfveukvYYhsOp2OkOs B18jTGLPcH8nXfWXbm0lpvqLP8KJ5X0Eg2UR3U0lkYVJ2LCwus7niMjIGqz7llPOLF7z 5HCw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=W5bjf9+Ex+RegmcG2FUEI2Ls8g+CeCoTmsbEyu+5anw=; b=HcXVX2CMIWmwvz7md/lC2UbsAwq7bZjXfs1wz2/FCNwStHCqdgu5qOJXGgx3Wz3G10 cLJ5hvewqnBOT1jWkisLeRYuJYefcBbjgdvPngjnXTY759f9yZdsjYjer5p2xBMyqAG1 b6DVmHhFzgLJZL5iik2p1hiSJcWgkAZqnZFhoaaO/D4p1kGht6p42dWWOisDQ1HSUvmO nuZSnbCiw7c1jVEqh7aHjuQgoHCXM297zeHyIq1iSEoJbe+if0eHEGztiKWTCDi+q7z1 zTKT8S9y3Zn3G7T+CBX0F8koUVA4h5t3IY2cnWuN2YIbR9rE4fUyddzgnWYU0GnycbnC Wb2A== X-Gm-Message-State: AFqh2kqwWvMY2HgqEOAJJTblFBHWB+PiS8RLWW4EgXN7Q8dE4ECoIvbE gwcSEvE+H88HJ49F1Zx0tvk= X-Google-Smtp-Source: AMrXdXsAzrSV4exQWy6buRZIIql/EWPu2CmBqXUcSxMNm53yH0GgPnVffUgirBJuO+msEx/oog8OKw== X-Received: by 2002:a17:90a:77c5:b0:219:b330:7f6d with SMTP id e5-20020a17090a77c500b00219b3307f6dmr65345175pjs.24.1673158690141; Sat, 07 Jan 2023 22:18:10 -0800 (PST) Received: from [10.0.2.15] ([106.202.139.208]) by smtp.gmail.com with ESMTPSA id b2-20020a655782000000b0047915d582ccsm3161241pgr.20.2023.01.07.22.18.08 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sat, 07 Jan 2023 22:18:09 -0800 (PST) Message-ID: <2040c902-32b1-46c8-0b26-d3f43a16b9bb@gmail.com> Date: Sun, 8 Jan 2023 11:48:05 +0530 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.4.2 Subject: Re: Todo: Teach planner to evaluate multiple windows in the optimal order Content-Language: en-US To: David Rowley Cc: pghackers , Vik Fearing 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> <9620d994-b89a-2dcf-fca5-821e19d56858@gmail.com> <01248493-182b-0f92-f2f2-ff28dc4b2e83@gmail.com> From: Ankit Kumar Pandey In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 08/01/23 03:56, David Rowley wrote: > (your email client still seems broken) I am looking at this again, will be changing client for here onward. > You might need to have another loop before the foreach loop that loops > backwards through the WindowClauses and remembers the index of the > WindowClause which has pathkeys contained in the query's ORDER BY > pathkeys then apply the optimisation from that point in the main > foreach loop. Also, if the condition within the foreach loop which > checks when we want to apply this optimisation is going to be run > 1 > time, then you should probably have boolean variable that's set > before the loop which saves if we're going to try to apply the > optimisation. That'll save from having to check things like if the > query has a LIMIT clause multiple times. Thanks, this should do the trick. > a) looks like the best plan to me. What's the point of pushing the > sort below the WindowAgg in this case? The point of this optimisation > is to reduce the number of sorts not to push them as deep into the > plan as possible. We should only be pushing them down when it can > reduce the number of sorts. There's no reduction in the number of > sorts in the above plan. Yes, you are right, not in this case. I actually mentioned wrong case here, real problematic case is: EXPLAIN (COSTS OFF) SELECT empno, depname, min(salary) OVER (PARTITION BY depname ORDER BY empno) depminsalary, sum(salary) OVER (PARTITION BY depname) depsalary FROM empsalary ORDER BY depname, empno, enroll_date; QUERY PLAN ------------------------------------------------------------------- Incremental Sort Sort Key: depname, empno, enroll_date Presorted Key: depname, empno -> WindowAgg -> WindowAgg -> Incremental Sort Sort Key: depname, empno Presorted Key: depname -> Index Scan using depname_idx on empsalary (9 rows) Here, it could have sorted on depname, empno, enroll_date. Again, as I mentioned before, this is implementation issue. We shouldn't be skipping optimization if pre-sorted keys are present. -- Regards, Ankit Kumar Pandey