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 1pFK7v-0003WF-HU for pgsql-hackers@arkaria.postgresql.org; Tue, 10 Jan 2023 19:17:39 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pFK7u-0003zN-47 for pgsql-hackers@arkaria.postgresql.org; Tue, 10 Jan 2023 19:17:38 +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 1pFK7t-0003xE-Nv for pgsql-hackers@lists.postgresql.org; Tue, 10 Jan 2023 19:17:37 +0000 Received: from mail-pl1-x62a.google.com ([2607:f8b0:4864:20::62a]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1pFK7n-0002AZ-Mp for pgsql-hackers@lists.postgresql.org; Tue, 10 Jan 2023 19:17:37 +0000 Received: by mail-pl1-x62a.google.com with SMTP id jn22so14203713plb.13 for ; Tue, 10 Jan 2023 11:17:31 -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=BOit09jRhqeJmN6y3e/B0Cyazl7K6fSXDLmVSChb618=; b=JKRFsZBlG9nJXje5+cBYoF8qlDpCrKneyAoYai754+Sz1Ef2Ksb5xxCNL38Ec1oY7i qUe14CACKdaxQxFxFSMDV4DDBUl2e4MvLqdOqTTNjbWxt5aB1lOCkoEN8H5v6WaMQ4IU xx4Y4EIba5HqZUge1un2enk9gLf3uAt+4G01GTRCm4xgp4lluqRJRydNsPx1i1FlpgPL BOmyMpvknQcDTP2mDUDVzbh1Y7X2N4dAFZC+jKXmCD+8bs4Ch3l5dWwNauMMZRMX8EN6 bWmHfw2AePw6R96ZdTZM4S6jYkhOMasWi7z6+SYrUh49tKs0iw4x/3ls5QCYn3K4yJVt nTCw== 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=BOit09jRhqeJmN6y3e/B0Cyazl7K6fSXDLmVSChb618=; b=FKvqYgSyz38Bk3aV1VRGogYqv8+VbTu3MtUSq1CsXixTLqT81U89hNixHIzZzucfqc 1XScL2poHiAAi6hSh0wgORR3H/oQzwhDA6jz8LdLDqcg7R+ggE1ej9ooPa0MNESztIls 0mnlhiVJR/chavtMVwlpk2ctuQlAlkznnV8zHNaG30LoOC+vXzQ3aCk88nWvIOxbmzWJ WLmGPF+ocfGc/vEdUXPrEm2qRnyzCb39l+CtopQqf7MzmNxdoZRFIcAEd2S7JTryyOOy YrB4iPSQp3ePso0rq7IzKVtRt57FLQDghm1ESbDuhcvPRg4ewh4tyuKA3+hdeIkHYV4E 9Xyg== X-Gm-Message-State: AFqh2kpFq19RPhSmmVXMik0lOaFibWnvWBIhLRPN/qvrGvWmFAan3Kj2 ESuGgotFxI9j3zvJe60C0qk= X-Google-Smtp-Source: AMrXdXtmlEFkLyKKz0QUyuKj+7HcMgziH8+IRse/1Ztas9B6dD4KkqTGj7g7HiVvPAcR2QjKC/9YAQ== X-Received: by 2002:a17:902:b20b:b0:186:7a6b:24d9 with SMTP id t11-20020a170902b20b00b001867a6b24d9mr74949253plr.23.1673378249119; Tue, 10 Jan 2023 11:17:29 -0800 (PST) Received: from [10.0.2.15] ([106.201.23.151]) by smtp.gmail.com with ESMTPSA id d7-20020a170903230700b0018913417ba2sm8477677plh.130.2023.01.10.11.17.27 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 10 Jan 2023 11:17:28 -0800 (PST) Message-ID: <2f9f632d-3297-09dd-1d01-af3e0961c446@gmail.com> Date: Wed, 11 Jan 2023 00:47:25 +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: 8bit 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: > the total cost is the same for both of these, but the execution time > seems to vary quite a bit. This is really weird, I tried it different ways (to rule out any issues due to caching) and execution time varied in spite of having same cost. > 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. Isn't order by pathkeys are always fewer than distinct pathkeys? distinct pathkeys = order by pathkeys + window functions pathkeys Again, I got your point which that it is okay to pushdown order by clause if distinct is doing unique sort. But problem is (atleast from what I am facing), distinct is not caring about pushed down sortkeys, it goes with hashagg or unique with some other logic (mentioned below). Consider following (with distinct clause restriction removed) if (parse->distinctClause) { List* distinct_pathkeys = make_pathkeys_for_sortclauses(root, parse->distinctClause, root->processed_tlist); if (!compare_pathkeys(distinct_pathkeys, orderby_pathkeys)==1) // distinct key > order by key skip = true; // this is used to skip order by pushdown } CASE #1: explain (costs off) select distinct a,b, min(a) over (partition by a), sum (a) over (partition by a) from abcd order by a,b; QUERY PLAN ----------------------------------------------------------- Sort Sort Key: a, b -> HashAggregate Group Key: a, b, min(a) OVER (?), sum(a) OVER (?) -> WindowAgg -> Sort Sort Key: a, b -> Seq Scan on abcd (8 rows) explain (costs off) select distinct a,b,c, min(a) over (partition by a), sum (a) over (partition by a) from abcd order by a,b,c; QUERY PLAN -------------------------------------------------------------- Sort Sort Key: a, b, c -> HashAggregate Group Key: a, b, c, min(a) OVER (?), sum(a) OVER (?) -> WindowAgg -> Sort Sort Key: a, b, c -> Seq Scan on abcd (8 rows) No matter how many columns are pushed down, it does hashagg. On the other hand: CASE #2: EXPLAIN (costs off) SELECT DISTINCT depname, empno, min(salary) OVER (PARTITION BY depname) depminsalary,sum(salary) OVER (PARTITION BY depname) depsalary FROM empsalary ORDER BY depname, empno; QUERY PLAN ---------------------------------------------------------------------------------- Unique -> Sort Sort Key: depname, empno, (min(salary) OVER (?)), (sum(salary) OVER (?)) -> WindowAgg -> Sort Sort Key: depname, empno -> Seq Scan on empsalary (7 rows) EXPLAIN (costs off) SELECT DISTINCT depname, empno, enroll_date, min(salary) OVER (PARTITION BY depname) depminsalary,sum(salary) OVER (PARTITION BY depname) depsalary FROM empsalary ORDER BY depname, empno, enroll_date; QUERY PLAN ----------------------------------------------------------------------------------------------- Unique -> Sort Sort Key: depname, empno, enroll_date, (min(salary) OVER (?)), (sum(salary) OVER (?)) -> WindowAgg -> Sort Sort Key: depname, empno, enroll_date -> Seq Scan on empsalary (7 rows) It keeps doing Unique. In both of the cases, compare_pathkeys(distinct_pathkeys, orderby_pathkeys) returns 1 Looking bit further, planner is choosing things correctly. I could see cost of unique being higher in 1st case and lower in 2nd case. But the point is, if sort for orderby is pushdown, shouldn't there be some discount on cost of Unique sort (so that there is more possibility of it being favorable compared to HashAgg in certain cases)? Again, cost of Unqiue node is taken as cost of sort node as it is, but for HashAgg, new cost is being computed. If we do incremental sort here (for unique node), as we have pushed down orderby's, unique cost could be reduced and our optimization could be made worthwhile (I assume this is what you intended here) in case of distinct. Eg: EXPLAIN SELECT DISTINCT depname, empno, enroll_date, min(salary) OVER (PARTITION BY depname) depminsalary,sum(salary) OVER (PARTITION BY depname) depsalary FROM empsalary ORDER BY depname, empno, enroll_date;                                           QUERY PLAN -----------------------------------------------------------------------------------------------  Unique  (cost=1.63..1.78 rows=10 width=56)    ->  Sort  (cost=1.63..1.66 rows=10 width=56)          Sort Key: depname, empno, enroll_date, (min(salary) OVER (?)), (sum(salary) OVER (?))          ->  WindowAgg  (cost=1.27..1.47 rows=10 width=56)                ->  Sort  (cost=1.27..1.29 rows=10 width=48)                      Sort Key: depname, empno, enroll_date                      ->  Seq Scan on empsalary  (cost=0.00..1.10 rows=10 width=48) depname, empno, enroll_date are presorted but still strict sorting is done on all columns. Additionally, > the total cost is the same for both of these, but the execution time > seems to vary quite a bit. Even if I pushdown one or two path keys, end result is same cost (which isn't helping). -- Regards, Ankit Kumar Pandey