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 1pCbQl-0001FL-1H for pgsql-hackers@arkaria.postgresql.org; Tue, 03 Jan 2023 07:09:51 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pCbQj-00016r-C9 for pgsql-hackers@arkaria.postgresql.org; Tue, 03 Jan 2023 07:09:49 +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 1pCbQj-00016i-0z for pgsql-hackers@lists.postgresql.org; Tue, 03 Jan 2023 07:09:49 +0000 Received: from mail-pf1-x42a.google.com ([2607:f8b0:4864:20::42a]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1pCbQg-0000MB-0J for pgsql-hackers@lists.postgresql.org; Tue, 03 Jan 2023 07:09:47 +0000 Received: by mail-pf1-x42a.google.com with SMTP id a30so4316678pfr.6 for ; Mon, 02 Jan 2023 23:09:45 -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=cKcDQUxyzUb/nZ4tLLEs9M8zIQMiYkLvHtf/HTfxW60=; b=dKy4+F47Jpsw2pJlRdCyIB/Y1MWZC0WZzr7vd5RT7WlrUNgM+4a5URzL8z2VdbTIR4 WTEuV8sH2rNHAE7cOwCLhVEBTvXwrfbnr19IOejDQWDIvTXS+AW4iLlqGWY09deoptal CV9JJq54hHZZsOlhbPEa5QK8+l48x8JR5S4KOXWjBuYafkoGtGu9AaEWzufg0M9yOiuE DmCdt4CXzSIGUFVAIc+IDgTTU5gpsltbwbmdh4P4SALxZ1vmjH4iJ0y9vcuk2Xe0hgme OOIZpUK8QLv+/oSfLYDIeT02tfbmErWlqq6T7uo3MbRAk7mH8m+e0VYCwBWpRv0ue1AR gyGQ== 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=cKcDQUxyzUb/nZ4tLLEs9M8zIQMiYkLvHtf/HTfxW60=; b=bs3Wfkac/KnhcSAEoWjCr17WThmXk0jwfsXSWry3epyFRrhXIMteyMTjGFTCxCvE9u /Sg386sL1WB5Vx1YjIOzHYFwTr6PsEUbmmk9qURyq4hXjD0aCiY8izMRWTVUAoMnYUTu kd7RJvnu/8EfOPDkGGbBZUMsOshO43Dzkv8boadb7E5uV3T9bf1238+4lXHpNwar+E6X RrMv0HE6/iE7orkx4I3a6peT6P1ct+x/g+D0bDOMkM6zgAES0UOzw+eFNDRNFFfBFf6l hA4o4a9NG36N/pebahXj+ClwlLfsW2FwxDbP3OkuogEOoW0T2fN3fc9mu22/eP4EGtvy Gvqg== X-Gm-Message-State: AFqh2kprWqGecU2qqiQWBOkwH+wua2qlqcPY1vUpnrXLkIXs6soAMreg DEo0CBoPzQKALDdfruAEnhMUXvFrx2w= X-Google-Smtp-Source: AMrXdXtPmh10UmHjzr1h9Kuxeb7/YwONagPMXj77IwoMkWCv+UUP14HHD3nEmSeG8gkrS7K8I0WJqA== X-Received: by 2002:a05:6a00:1f04:b0:582:ef:9c8c with SMTP id be4-20020a056a001f0400b0058200ef9c8cmr13584674pfb.18.1672729784973; Mon, 02 Jan 2023 23:09:44 -0800 (PST) Received: from [10.0.2.15] ([110.227.136.168]) by smtp.gmail.com with ESMTPSA id m129-20020a625887000000b00562677968aesm19714973pfb.72.2023.01.02.23.09.43 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 02 Jan 2023 23:09:44 -0800 (PST) Message-ID: <398f39bf-54c0-e510-5507-6dab1b65d5e7@gmail.com> Date: Tue, 3 Jan 2023 12:39:41 +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 References: <83d80853-a45c-d85c-68eb-59acfe7fb5fb@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 03/01/23 08:21, David Rowley wrote: > On Mon, 26 Dec 2022 at 02:04, Ankit Kumar Pandey wrote: >> Point #1 >> >> In the above query Oracle 10g performs 2 sorts, DB2 and Sybase perform 3 >> sorts. We also perform 3. > This shouldn't be too hard to do. See the code in > select_active_windows(). You'll likely want to pay attention to the > DISTINCT pathkeys if they exist and just use the ORDER BY pathkeys if > the query has no DISTINCT clause. DISTINCT is evaluated after Window > and before ORDER BY. > > One idea to implement this would be to adjust the loop in > select_active_windows() so that we record any WindowClauses which have > the pathkeys contained in the ORDER BY / DISTINCT pathkeys then record > those separately and append those onto the end of the actives array > after the sort. > > I do think you'll likely want to put any WindowClauses which have > pathkeys which are a true subset or true superset of the ORDER BY / > DISTINCT pathkeys last. If they're a superset then we won't need to > perform any additional ordering for the DISTINCT / ORDER BY clause. > If they're a subset then we might be able to perform an Incremental > Sort, which is likely much cheaper than a full sort. The existing > code should handle that part. You just need to make > select_active_windows() more intelligent. > > You might also think that we could perform additional optimisations > and also adjust the ORDER BY clause of a WindowClause if it contains > the pathkeys of the DISTINCT / ORDER BY clause. For example: > > SELECT *,row_number() over (order by a,b) from tab order by a,b,c; > > However, if you were to adjust the WindowClauses ORDER BY to become > a,b,c then you could produce incorrect results for window functions > that change their result based on peer rows. > > Note the difference in results from: > > create table ab(a int, b int); > insert into ab select x,y from generate_series(1,5) x, generate_Series(1,5)y; > > select a,b,count(*) over (order by a) from ab order by a,b; > select a,b,count(*) over (order by a,b) from ab order by a,b; > Thanks, let me try this. >> and Point #2 >> >> Teach planner to decide which window to evaluate first based on costs. >> Currently the first window in the query is evaluated first, there may be no >> index to help sort the first window, but perhaps there are for other windows >> in the query. This may allow an index scan instead of a seqscan -> sort. > What Tom wrote about that in the first paragraph of [1] still applies. > The problem is that if the query contains many joins that to properly > find the cheapest way of executing the query we'd have to perform the > join search once for each unique sort order of each WindowClause. > That's just not practical to do from a performance standpoint. The > join search can be very expensive. There may be something that could > be done to better determine the most likely candidate for the first > WindowClause using some heuristics, but I've no idea what those would > be. You should look into point #1 first. Point #2 is significantly > more difficult to solve in a way that would be acceptable to the > project. > Okay, leaving this out for now. -- Regards, Ankit Kumar Pandey