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 1rI5kK-006EGw-Ji for pgsql-hackers@arkaria.postgresql.org; Tue, 26 Dec 2023 11:37:16 +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 1rI5kJ-00BKjR-19 for pgsql-hackers@arkaria.postgresql.org; Tue, 26 Dec 2023 11:37:15 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rI5kI-00BKjJ-Nw for pgsql-hackers@lists.postgresql.org; Tue, 26 Dec 2023 11:37:14 +0000 Received: from mail.postgrespro.ru ([93.174.131.139]) by makus.postgresql.org with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rI5kC-00CH0l-Nr for pgsql-hackers@lists.postgresql.org; Tue, 26 Dec 2023 11:37:13 +0000 Received: from [10.10.20.39] (node-ko0.pool-101-109.dynamic.totinternet.net [101.109.104.160]) (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 1F99EE20C32; Tue, 26 Dec 2023 14:37:03 +0300 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=postgrespro.ru; s=mx2023; t=1703590626; bh=qvo0KEQR9G6DaoEZwX2N9ON2pBjErV+8dJ4Yx+LVfYc=; h=Message-ID:Date:User-Agent:Subject:To:Cc:References:From: In-Reply-To:From; b=pcwW+DjtcCPi4X6nSd0rV/t+3OXXkXeBnvO9etD0UkzmDzicTwZ3Lstbowklld/Ns 4peDvCn5jHpPYEKHpPDhdqDx6uiP5puCl/vOfK0cqtF2dPJtCXr6QOC3sW0Jvr3TqX 88KDWCZB0kL786Amg5xrBpT0qL6g5y0KYGg+yvodGrN/3Mdtk5++Bo5IOWlX0dEBEB dHcSFYlmIJhmyYkM39SVZhhNrH1RFAuCdDI50gaYlngMGlMj7ivkdEAeFXyK7ihfCU WNIcBKR7MBGr0P7oJII/18qjPt+MZ50IlYJa1VWM8YPH1uV9NbPzNdQ8A9S3PdsCx6 O3a0N5T/2M4BA== Message-ID: <6c67b7bb-5469-475f-8988-0a50b4c64852@postgrespro.ru> Date: Tue, 26 Dec 2023 18:37:01 +0700 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: POC: GROUP BY optimization Content-Language: en-US To: Alexander Korotkov Cc: PostgreSQL Developers , Tomas Vondra , Teodor Sigaev , David Rowley , "a.rybakina" , =?UTF-8?B?0JHQtdC70Y/Qu9C+0LIg0JTQsNC80LjRgCDQndCw0LjQu9C10LLQuNGH?= , Tom Lane References: <721b3ff9-f214-f5d4-86c7-bae515bbec18@enterprisedb.com> <7bea462e-8c3e-0f8c-c7d5-f4daa12f3baf@postgrespro.ru> <9af89543-3d17-4c98-59de-1daa5bceeb06@enterprisedb.com> <9408e450-60c6-6fbc-d5c4-467bb0abfe67@postgrespro.ru> <44472925-f6c6-e8de-6f72-0451458532c4@enterprisedb.com> <7a80a207-5e8e-b80c-476c-2d290b0526e9@enterprisedb.com> <65941e87-86c1-c09c-8ad5-8e102aebe8ee@enterprisedb.com> <7256159a-bb83-7ef3-c9c9-b2188a29aaf8@postgrespro.ru> <60610df1-c32f-ebdf-e58c-7a664431f452@enterprisedb.com> <9acd27e9-3372-4d78-b9bc-73e407b8a007@postgrespro.ru> From: Andrei Lepikhov Organization: Postgres Professional 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 21/12/2023 17:53, Alexander Korotkov wrote: > On Sun, Oct 1, 2023 at 11:45 AM Andrei Lepikhov > wrote: >> New version of the patch. Fixed minor inconsistencies and rebased onto >> current master. > Thank you (and other authors) for working on this subject. Indeed to > GROUP BY clauses are order-agnostic. Reordering them in the most > suitable order could give up significant query planning benefits. I > went through the thread: I see significant work has been already made > on this patch, the code is quite polished. Maybe, but issues, mentioned in [1], still not resolved. It is the only reason, why this thread hasn't been active. > I'd like to make some notes. > 1) As already mentioned, there is clearly a repetitive pattern for the > code following after get_useful_group_keys_orderings() calls. I think > it would be good to extract it into a separate function. Please, do > this as a separate patch coming before the group-by patch. That would > simplify the review. Yeah, these parts of code a bit different. I will try to make common routine. > 2) I wonder what planning overhead this patch could introduce? Could > you try to measure the worst case? What if we have a table with a lot > of indexes and a long list of group-by clauses partially patching > every index. This should give us an understanding on whether we need > a separate GUC to control this feature. Ok> 3) I see that get_useful_group_keys_orderings() makes 3 calls to > get_cheapest_group_keys_order() function. Each time > get_cheapest_group_keys_order() performs the cost estimate and > reorders the free keys. However, cost estimation implies the system > catalog lookups (that is quite expensive). I wonder if we could > change the algorithm. Could we just sort the group-by keys by cost > once, save this ordering and then just re-use it. So, every time we > need to reorder a group by, we can just pull the required keys to the > top and use saved ordering for the rest. I also wonder if we could do > this once for add_paths_to_grouping_rel() and > create_partial_grouping_paths() calls. So, it probably should be > somewhere in create_ordinary_grouping_paths(). Thanks for the idea!> 4) I think we can do some optimizations when enable_incremental_sort > == off. Then in get_useful_group_keys_orderings() we should only deal > with input_path fully matching the group-by clause, and try only full > match of group-by output to the required order. Oh, we had designed before the incremental sort was invented. Will see what we can do here. [1] https://www.postgresql.org/message-id/60610df1-c32f-ebdf-e58c-7a664431f452%40enterprisedb.com -- regards, Andrei Lepikhov Postgres Professional