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 1pFUUA-00024v-5o for pgsql-hackers@arkaria.postgresql.org; Wed, 11 Jan 2023 06:21:18 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pFUU9-0006oG-1G for pgsql-hackers@arkaria.postgresql.org; Wed, 11 Jan 2023 06:21:17 +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 1pFUU8-0006o6-L5 for pgsql-hackers@lists.postgresql.org; Wed, 11 Jan 2023 06:21:16 +0000 Received: from mail-pj1-x1030.google.com ([2607:f8b0:4864:20::1030]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1pFUU5-0000dO-Fh for pgsql-hackers@lists.postgresql.org; Wed, 11 Jan 2023 06:21:16 +0000 Received: by mail-pj1-x1030.google.com with SMTP id o8-20020a17090a9f8800b00223de0364beso18914512pjp.4 for ; Tue, 10 Jan 2023 22:21:13 -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=AMN7pdOm1iFX7q6On70ucPaqa9R5Bo6bjgEm6jVVjRg=; b=aKmmz9aIdUGN32Bnud/8JXqAaa7h9PvbG3RBUdJyMAFktMTIANBIzUXr8kr+TmtCo4 jsQFq+WrM3ZsBxeWCfHS1SCOFMKVrdCBzaBQhg7Dwzgh0HPhNjLMmT0P4lfM8faePfP5 oYo4XPAxjFrFDsg8EFX/yMAo8w95/KZJt3vNdyxH7YFavTD880EuThqPZT4NcCOYAnml 7YrkpqxeMj9pYSBm7IwpC+xJ9TgjMTuOdJWwGUQeSxEULOLAeuYAyOiKfqe2S1PFbCes P3UuMDYCjJr6St13+oBQlDswI+ihIDTQPogFevYStEYIMBlZTbPfRvXsLdixYMRf43zN hKZQ== 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=AMN7pdOm1iFX7q6On70ucPaqa9R5Bo6bjgEm6jVVjRg=; b=wDzaJcmLrIoU3KOUjMVbhgvvPZ6z8Na3VEnLieZolYfgWbqiTvyGBMuSTXNcz/bLDU oi0bterwNLpuJtDuRkmB0aZ2yfYt8xR/QBDhNTIdS1g43EZNnPaeyiCXz3wfovhhBNLr ++n7XscCyd+qoJRz8TGC/yooerUktsWsSNWgaIKsiSfj/yBB8ZsEOYbqLvhUJr0Sbmpr zMbAAA+sE9td1yjVBbu3liFwxAPRLRYya/5U3u995oxqVXFLR788INtw35ClqeUeuOCx N5GlTVfm0f+ZHwpUA3VrslTJIHFNNwz9nkMTAXzghWBR1grQWkzty1DvR6U8ifh0Jp1O L4rQ== X-Gm-Message-State: AFqh2kqA4usroJ9EQNXzgVWyoJWTT2tYKtIRfOav3w5ZQwB3ftUaUfnu PbI7MoppxCrrwdN80mQa8UoUMML9HEo= X-Google-Smtp-Source: AMrXdXs0f7LjTIlwKzvTZmCrwk/NXRqjf5ScTkU9GsbB7zDWiU3pd0c7/OYojftZxZlYJC/lbNpasw== X-Received: by 2002:a17:90a:9917:b0:227:25e1:c61d with SMTP id b23-20020a17090a991700b0022725e1c61dmr6596927pjp.26.1673418071070; Tue, 10 Jan 2023 22:21:11 -0800 (PST) Received: from [10.0.2.15] ([106.201.23.151]) by smtp.gmail.com with ESMTPSA id ob10-20020a17090b390a00b002187a4dd830sm10072352pjb.46.2023.01.10.22.21.08 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 10 Jan 2023 22:21:10 -0800 (PST) Message-ID: <7d5e8038-0417-2d5b-3db7-c96106bd4cb0@gmail.com> Date: Wed, 11 Jan 2023 11:51:06 +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> <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> <2f9f632d-3297-09dd-1d01-af3e0961c446@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 11/01/23 06:18, David Rowley wrote: > > Not sure if we should be trying to improve that in this patch. I just > wanted to identify it as something else that perhaps could be done. This could be within reach but still original problem of having hashagg removing any gains from this remains. eg set enable_hashagg=0; explain select distinct relkind, relname, count(*) over (partition by relkind) from pg_Class; QUERY PLAN ------------------------------------------------------------------------------------ Unique (cost=41.26..65.32 rows=412 width=73) -> Incremental Sort (cost=41.26..62.23 rows=412 width=73) Sort Key: relkind, relname, (count(*) OVER (?)) Presorted Key: relkind -> WindowAgg (cost=36.01..43.22 rows=412 width=73) -> Sort (cost=36.01..37.04 rows=412 width=65) Sort Key: relkind -> Seq Scan on pg_class (cost=0.00..18.12 rows=412 width=65) (8 rows) reset enable_hashagg; explain select distinct relkind, relname, count(*) over (partition by relkind) from pg_Class; QUERY PLAN ------------------------------------------------------------------------------ HashAggregate (cost=46.31..50.43 rows=412 width=73) Group Key: relkind, relname, count(*) OVER (?) -> WindowAgg (cost=36.01..43.22 rows=412 width=73) -> Sort (cost=36.01..37.04 rows=412 width=65) Sort Key: relkind -> Seq Scan on pg_class (cost=0.00..18.12 rows=412 width=65) (6 rows) HashAgg has better cost than Unique even with incremental sort (tried with other case where we have more columns pushed down but still hashAgg wins). explain select distinct a, b, count(*) over (partition by a order by b) from abcd;                                       QUERY PLAN --------------------------------------------------------------------------------------  Unique  (cost=345712.12..400370.25 rows=1595 width=16)    ->  Incremental Sort  (cost=345712.12..395456.14 rows=655214 width=16)          Sort Key: a, b, (count(*) OVER (?))          Presorted Key: a, b          ->  WindowAgg  (cost=345686.08..358790.36 rows=655214 width=16)                ->  Sort  (cost=345686.08..347324.11 rows=655214 width=8)                      Sort Key: a, b                      ->  Seq Scan on abcd  (cost=0.00..273427.14 rows=655214 width=8) explain select distinct a, b, count(*) over (partition by a order by b) from abcd;                                    QUERY PLAN --------------------------------------------------------------------------------  HashAggregate  (cost=363704.46..363720.41 rows=1595 width=16)    Group Key: a, b, count(*) OVER (?)    ->  WindowAgg  (cost=345686.08..358790.36 rows=655214 width=16)          ->  Sort  (cost=345686.08..347324.11 rows=655214 width=8)                Sort Key: a, b                ->  Seq Scan on abcd  (cost=0.00..273427.14 rows=655214 width=8) (6 rows) > I'm not really all that sure the above query shape makes much sense in > the real world. Would anyone ever want to use DISTINCT on some results > containing WindowFuncs? This could still have been good to have if there were no negative impact and some benefit in few cases but as mentioned before, if hashagg removes any sort (which happened due to push down), all gains will be lost and we will be probably worse off than before. -- Regards, Ankit Kumar Pandey