Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sCXJk-006EcA-Sl for pgsql-hackers@arkaria.postgresql.org; Thu, 30 May 2024 04:23:11 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1sCXJk-0019wz-1s for pgsql-hackers@arkaria.postgresql.org; Thu, 30 May 2024 04:23:08 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sCXJj-0019wq-JK for pgsql-hackers@lists.postgresql.org; Thu, 30 May 2024 04:23:07 +0000 Received: from mail.postgrespro.ru ([93.174.131.139]) by magus.postgresql.org with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sCXJZ-001SCx-RB for pgsql-hackers@lists.postgresql.org; Thu, 30 May 2024 04:23:04 +0000 Received: from [10.10.20.94] (node-qz.pool-118-173.dynamic.totinternet.net [118.173.3.203]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client did not present a certificate) (Authenticated sender: a.lepikhov@postgrespro.ru) by mail.postgrespro.ru (Postfix/587) with ESMTPSA id E7770E2110B; Thu, 30 May 2024 07:22:51 +0300 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=postgrespro.ru; s=mx2023; t=1717042976; bh=RtLuT57vt+PBeOSczfFfpPvsGcpKSmSHvJnpegS97kw=; h=Message-ID:Date:User-Agent:Subject:To:Cc:References:From: In-Reply-To:From; b=MXjx/IgeS1sqbMpZmUMMka0VqAGAcpoRclW2YMXwGO3xlWEFYezL/esiNg8jA2lOd a6DT+DoPJOGKIRjHBRFX4j7yXxlnbXSew7DFTAMWC5WfQEk8pmD6TEBkJYKLVUdw2T Rt4lDCDdtM/HpDDw9Z2nU0Lm/JK9DoFtQlgFLpNYrKZAOJC8yYQMQKaR3ozIC0zfib VZ7YlUECCzqXoveFOv45AUzOEBfsJ7NwqzcK8dJMcOmSlebOQII1OlezpwS0ZKM2Oz nCXbFjEPcFcTNntcpMwkFeITVdyFefaTJ/f1qXl2m0DDm1/ppBOsQDjftaDfuzLHKz c3Z+85fVvnkLg== Message-ID: <3e755fd2-cf59-49d2-ae95-38c78daeba85@postgrespro.ru> Date: Thu, 30 May 2024 11:22:46 +0700 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: POC: GROUP BY optimization To: Alexander Korotkov Cc: Tom Lane , Richard Guo , Pavel Borisov , vignesh C , PostgreSQL Developers , Tomas Vondra , Teodor Sigaev , David Rowley , "a.rybakina" References: <890ed877-e2c0-448a-93b8-b07ccf3a2b37@postgrespro.ru> <266850.1712879082@sss.pgh.pa.us> Content-Language: en-AU From: Andrei Lepikhov Organization: Postgres Professional In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-KSMG-AntiPhishing: NotDetected X-KSMG-AntiSpam-Interceptor-Info: not scanned X-KSMG-AntiSpam-Status: not scanned, disabled by settings X-KSMG-AntiVirus: Kaspersky Secure Mail Gateway, version 2.1.0.7854, bases: 2024/05/30 02:56:00 #25361682 X-KSMG-AntiVirus-Status: NotDetected, skipped X-KSMG-LinksScanning: not scanned, disabled by settings X-KSMG-Message-Action: skipped X-KSMG-Rule-ID: 1 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 5/29/24 19:53, Alexander Korotkov wrote: > Hi, Andrei! > > Thank you for your feedback. > > On Wed, May 29, 2024 at 11:08 AM Andrei Lepikhov > wrote: >> On 5/27/24 19:41, Alexander Korotkov wrote: >>> Any thoughts? >> About 0001: >> Having overviewed it, I don't see any issues (but I'm the author), >> except grammatical ones - but I'm not a native to judge it. >> Also, the sentence 'turning GROUP BY clauses into pathkeys' is unclear >> to me. It may be better to write something like: 'building pathkeys by >> the list of grouping clauses'. > > OK, thank you. I'll run once again for the grammar issues. > >> 0002: >> The part under USE_ASSERT_CHECKING looks good to me. But the code in >> group_keys_reorder_by_pathkeys looks suspicious: of course, we do some >> doubtful work without any possible way to reproduce, but if we envision >> some duplicated elements in the group_clauses, we should avoid usage of >> the list_concat_unique_ptr. > > As I understand Tom, there is a risk that clauses list may contain > multiple instances of equivalent SortGroupClause, not duplicate > pointers. Maybe. I just implemented the worst-case scenario with the intention of maximum safety. So, it's up to you. > >> What's more, why do you not exit from >> foreach_ptr immediately after SortGroupClause has been found? I think >> the new_group_clauses should be consistent with the new_group_pathkeys. > > I wanted this to be consistent with preprocess_groupclause(), where > duplicate SortGroupClause'es are grouped together. Otherwise, we > could just delete redundant SortGroupClause'es. Hm, preprocess_groupclause is called before the standard_qp_callback forms the 'canonical form' of group_pathkeys and such behaviour needed. But the code that chooses the reordering strategy uses already processed grouping clauses, where we don't have duplicates in the first num_groupby_pathkeys elements, do we? >> 0004: >> I was also thinking about reintroducing the preprocess_groupclause >> because with the re-arrangement of GROUP-BY clauses according to >> incoming pathkeys, it doesn't make sense to have a user-defined order—at >> least while cost_sort doesn't differ costs for alternative column orderings. >> So, I'm okay with the code. But why don't you use the same approach with >> foreach_ptr as before? > > I restored the function as it was before 0452b461bc with minimal edits > to support the incremental sort. I think it would be more valuable to > keep the difference with pg16 code small rather than refactor to > simplify existing code. Got it -- regards, Andrei Lepikhov Postgres Professional