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 1pFA3A-0006l1-Tj for pgsql-hackers@arkaria.postgresql.org; Tue, 10 Jan 2023 08:32:04 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pFA39-0007cl-Pe for pgsql-hackers@arkaria.postgresql.org; Tue, 10 Jan 2023 08:32:03 +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 1pFA39-0007cc-ES for pgsql-hackers@lists.postgresql.org; Tue, 10 Jan 2023 08:32:03 +0000 Received: from mail-pj1-x102e.google.com ([2607:f8b0:4864:20::102e]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1pFA37-0003sg-7p for pgsql-hackers@lists.postgresql.org; Tue, 10 Jan 2023 08:32:03 +0000 Received: by mail-pj1-x102e.google.com with SMTP id v13-20020a17090a6b0d00b00219c3be9830so12600151pjj.4 for ; Tue, 10 Jan 2023 00:32:00 -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:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=LuGdbp4zOifgpOjlmeDJSf4Eu7ARrqQa3qNTwmKFg5w=; b=hTDc1tgmvmA1uDf7UbG1xjUh/EFDMK6UbkLuIT7bZxCDTpDaNUFmYXPizqRlVacfM8 qb8KF4VOYaDEjA9p5TRGEffWnhLghBZYxVyfILuVQD3A8CTDALqDNzjy/7ScuIR9t6za EuFqN2pwi6uQBuUqiyobwmGRQJg/4F9G7/Ilq3f9o1Rs7z4LcPZ+ugcGHd8O8DuTVc1k IrM51Fnk4AkJdSBXEGVx7yOhQptD/tjua2H2KYtn+ajF+LFAtYUKvqTXxK2+At7di+4O Y17/hpTEMIFvGx7EfpvI54gMJ51BBUIJ8rWrk8F0glSiB3LkWVBChBoJwvq92UjqQTOK pzOA== 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:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=LuGdbp4zOifgpOjlmeDJSf4Eu7ARrqQa3qNTwmKFg5w=; b=DrPLAa8NV6kc5rXElxy++cfwAWA/LCyhWhkYHKQDm4UDnJ5pbzHz/5GVnMdIzFubHK +ab8tXNU+g97B0TG1q1BCg1f8jCY/mo052HFUrjI6/aoUHZG/0UA995E8btTNBXh6dQY whIgbX2TGYG/cWsrXJ3+EZbQB5mXxF0jRmAMrm/ZaSGP0uzjXSoCnGX27E8/5wmQiadN 6Kat2/hs6KqjaBBu6lAuIs6zHX1JHmcJGKjdPJL7+Zhsmqi4oroM599/IJKzXCuY2FMa C7EAtBmZjJHTUbXZkFw187blbqHL2T5Jc0zThaxx9sGrMSymP1B4i/gBGnFCHVivhljl VH4w== X-Gm-Message-State: AFqh2kokN0SQIFTEknWb5DZLnmqo/P1DGiaqGdMUW0Z0O+jzRaLfWBv5 Phq/NSBY+EN8kmMV2qP3EvM= X-Google-Smtp-Source: AMrXdXu1oYUgiTSfgBJhRmtv1gU7kVxgdiRaN4tdi5i35WpNA92LK+tU2CU9j7/lwhJoiP9rcucKyw== X-Received: by 2002:a17:90a:2fc9:b0:227:1fa4:4aa1 with SMTP id n9-20020a17090a2fc900b002271fa44aa1mr4391399pjm.47.1673339517668; Tue, 10 Jan 2023 00:31:57 -0800 (PST) Received: from [10.0.2.15] ([106.201.23.151]) by smtp.gmail.com with ESMTPSA id fs7-20020a17090af28700b00218abadb6a8sm6712866pjb.49.2023.01.10.00.31.55 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 10 Jan 2023 00:31:57 -0800 (PST) Message-ID: Date: Tue, 10 Jan 2023 14:01:52 +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 To: David Rowley Cc: pghackers , Vik Fearing References: <83d80853-a45c-d85c-68eb-59acfe7fb5fb@gmail.com> <9620d994-b89a-2dcf-fca5-821e19d56858@gmail.com> <01248493-182b-0f92-f2f2-ff28dc4b2e83@gmail.com> <2040c902-32b1-46c8-0b26-d3f43a16b9bb@gmail.com> <12f6ef19-e06e-8ea9-ad98-3fd92afb7f0b@gmail.com> <8d0b24ce-f8ba-cd63-82ae-be853b4067e8@gmail.com> <4ef7f947-526b-c4ec-f7a0-8b4586e693e4@gmail.com> Content-Language: en-US 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 10/01/23 10:53, David Rowley wrote: > On Tue, 10 Jan 2023 at 06:15, Ankit Kumar Pandey wrote: > > Do we have any pending items for this patch now? > > I'm just wondering if not trying this when the query has a DISTINCT > clause is a copout. What I wanted to avoid was doing additional > sorting work for WindowAgg just to have it destroyed by Hash > Aggregate. I'm now wondering if adding both the original > slightly-less-sorted path plus the new slightly-more-sorted path then > if distinct decides to Hash Aggregate then it'll still be able to pick > the cheapest input path to do that on. Unfortunately, our sort > costing just does not seem to be advanced enough to know that sorting > by fewer columns might be cheaper, so adding the additional path is > likely just going to result in add_path() ditching the old > slightly-less-sorted path due to the new slightly-more-sorted path > having better pathkeys. So, we'd probably be wasting our time if we > added both paths with the current sort costing code. > Maybe we should try and do this for DISTINCT queries if the > distinct_pathkeys match the orderby_pathkeys. That seems a little less > copout-ish. If the ORDER BY is the same as the DISTINCT then it seems > likely that the ORDER BY might opt to use the Unique path for DISTINCT > since it'll already have the correct pathkeys. However, if the ORDER > BY has fewer columns then it might be cheaper to Hash Aggregate and > then sort all over again, especially so when the DISTINCT removes a > large proportion of the rows. > > Ideally, our sort costing would just be better, but I think that > raises the bar a little too high to start thinking of making > improvements to that for this patch. Let me take a stab at this. Depending on complexity, we can take a call to address this in current patch or a follow up. -- Regards, Ankit Kumar Pandey