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 1ql0yl-000aL1-Ka for pgsql-hackers@arkaria.postgresql.org; Tue, 26 Sep 2023 05:51:28 +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 1ql0yk-008vtf-51 for pgsql-hackers@arkaria.postgresql.org; Tue, 26 Sep 2023 05:51:26 +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 1ql0yj-008vsy-Lf for pgsql-hackers@lists.postgresql.org; Tue, 26 Sep 2023 05:51:25 +0000 Received: from mail.postgrespro.ru ([93.174.131.139]) by magus.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1ql0yg-007ceS-Tn for pgsql-hackers@lists.postgresql.org; Tue, 26 Sep 2023 05:51:25 +0000 Received: from [172.30.8.38] (unknown [172.30.8.38]) (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 8910CE21015; Tue, 26 Sep 2023 08:51:19 +0300 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=postgrespro.ru; s=mx2023; t=1695707481; bh=xbAAmzuQa+gScROrE6nCKkCit9HDb/5w5K8mo9zQe5Y=; h=Message-ID:Date:User-Agent:Subject:To:Cc:References:From: In-Reply-To:From; b=wY4cRxCrFrGy5S54hMIEPnrwPxToWEFZ3WfRu2RMK+Z1Bc2CXetVl4YpfEqbHdri/ LZRMkn0rir/jCE7A0KL6rEJGvLERsWleA4UJQM6NGyUScewo0+o5BpErdPyk32M2PR E8iH3/6mqpXBmwYxPQ+IjVEA8UFzhrUwBaETdUehWkgxJwuMT9/vS7OkR6W5g9BSVD ST9tGOXdHZZ3D4OaxQkBfkAdM8oRkbjy77p9nB7ADqT1AvUe4HQHlnBQ6Zjt3BywlC dSGUO037lmVL0QgvsVmvxp64UyvRnId/8kDJ7+DryPhgdlgbpknUgFUa8+jOvY8cCw IbvVZlB0S8knA== Message-ID: <9acd27e9-3372-4d78-b9bc-73e407b8a007@postgrespro.ru> Date: Tue, 26 Sep 2023 12:51:16 +0700 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: POC: GROUP BY optimization Content-Language: en-US To: Tomas Vondra , PostgreSQL Developers , Tom Lane Cc: Teodor Sigaev , David Rowley , "a.rybakina" , =?UTF-8?B?0JHQtdC70Y/Qu9C+0LIg0JTQsNC80LjRgCDQndCw0LjQu9C10LLQuNGH?= References: <006667af-fc50-0627-4be7-5d9cf665219f@enterprisedb.com> <4ee5b15c-1ce0-59c7-8d4a-a1fd68ee0d12@postgrespro.ru> <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> From: Andrey Lepikhov Organization: Postgres Professional In-Reply-To: <60610df1-c32f-ebdf-e58c-7a664431f452@enterprisedb.com> 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 20/7/2023 18:46, Tomas Vondra wrote: > 2) estimating quicksort comparisons - This relies on ndistinct > estimates, and I'm not sure how much more reliable we can make those. > Probably not much :-( Not sure what to do about this, the only thing I > can think of is to track "reliability" of the estimates and only do the > reordering if we have high confidence in the estimates. That means we'll > miss some optimization opportunities, but it should limit the risk. According to this issue, I see two options: 1. Go through the grouping column list and find the most reliable one. If we have a unique column or column with statistics on the number of distinct values, which is significantly more than ndistincts for other grouping columns, we can place this column as the first in the grouping. It should guarantee the reliability of such a decision, isn't it? 2. If we have extended statistics on distinct values and these statistics cover some set of first columns in the grouping list, we can optimize these positions. It also looks reliable. Any thoughts? -- regards, Andrey Lepikhov Postgres Professional