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 1qNvLr-006LjQ-EH for pgsql-hackers@arkaria.postgresql.org; Mon, 24 Jul 2023 13:11:51 +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 1qNvLp-00APLV-3t for pgsql-hackers@arkaria.postgresql.org; Mon, 24 Jul 2023 13:11:49 +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 1qNvLo-00APLM-Lu for pgsql-hackers@lists.postgresql.org; Mon, 24 Jul 2023 13:11:48 +0000 Received: from mail-ej1-x62d.google.com ([2a00:1450:4864:20::62d]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1qNvLf-001HxL-CD for pgsql-hackers@lists.postgresql.org; Mon, 24 Jul 2023 13:11:46 +0000 Received: by mail-ej1-x62d.google.com with SMTP id a640c23a62f3a-9891c73e0fbso910433566b.1 for ; Mon, 24 Jul 2023 06:11:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=enterprisedb.com; s=google; t=1690204297; x=1690809097; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=ao37wEcvDQJ3WhmlkYOtwnTFxjphH2Ho4RlOZvCf73E=; b=ZvQdOKcMxxwCbRkeNyjfJ0AtMWu2BShzOI1SUDxp9k/wCdtR6GTbnTxsxsPnqr4/tX GWN4vvNgdm2pB66tP1vocw0ESDJ08tjnWagwbTuGI67hBAFdTYFOxsBlqqQaxEjk/qhT tQgT5xoimOwMuLKcyAvRp+BcEPT2HkwCC6szAF6WfCfO/u41socMCT/+J8z/CcwFwHAM rSiYpzs1ipD05jg1QUHX/w95+TQBz6rWrTRndRS6v/PdBj5P20v3tb04RTcVysxGndEg XARFykt1Sb3McWyyaUG8JAXsm0R+D3xM5/ojZeP6L6GPMDljxqSl4Iag0vJfnmx7mG3j 2ysw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690204297; x=1690809097; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=ao37wEcvDQJ3WhmlkYOtwnTFxjphH2Ho4RlOZvCf73E=; b=Ow9i9QDo5WAjmSRQ98DlWcI/wflyaHKblcOQy3z0UOy2BH5j0rvZu8XXDNljllXbD6 GHdYbsoetuY49fZEHK7qam0oYrNvBLlRubQMjb53w7Hb3z9nMeL//BjtW2KGVX5gDDFr 3SM1s8OO83ru4jRVZ3/dHOTM4xQyzDkC0aptOyxm2MrNNOS7BArYSWAyZumX1z0/NvCL TAayuP0XXAX2hvBJzsJnl9gE08D3qVbs+etu74MuAMszF0AyUoEqLra28E71WcV4G6qa V0BHZ1GcWk0Tg/GvH4vW9Xalc0LF2FTApSJKvE1KheZtX934K5+Ta5PNDbhKj6GB3Rrj TlFA== X-Gm-Message-State: ABy/qLZEdtjsXtWXLO29z5JkDd/6P0+rySNco5ubcuDXnTuyXxt0ZyDJ 8e/gjHXk99WpEa87YLSHnI6+Uw== X-Google-Smtp-Source: APBJJlEwT0B29cBme4t7mcTIa5uw0ZOfJGt5coWYXIk22AuCg6VMbvNbX8N7i9Y2Y6bhqQD1hBPFiA== X-Received: by 2002:a17:906:779a:b0:989:450:e565 with SMTP id s26-20020a170906779a00b009890450e565mr9973065ejm.23.1690204297647; Mon, 24 Jul 2023 06:11:37 -0700 (PDT) Received: from [10.137.0.17] (ip-86-49-228-220.bb.vodafone.cz. [86.49.228.220]) by smtp.gmail.com with ESMTPSA id la6-20020a170906ad8600b00993c29eff7dsm6689335ejb.138.2023.07.24.06.11.36 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 24 Jul 2023 06:11:37 -0700 (PDT) Message-ID: Date: Mon, 24 Jul 2023 15:11:35 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.12.0 Subject: Re: POC: GROUP BY optimization Content-Language: en-US To: Andrey Lepikhov , PostgreSQL Developers , Tom Lane Cc: Zhihong Yu , Teodor Sigaev , Tomas Vondra , David Rowley , "a.rybakina" , =?UTF-8?B?0JHQtdC70Y/Qu9C+0LIg0JTQsNC80LjRgCDQndCw0LjQu9C10LLQuNGH?= 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> <3d30ae10-5c35-63c4-577c-d2e2479e15fd@postgrespro.ru> <29bd1f4f-6b69-df43-1f3a-6ef1fc501657@enterprisedb.com> From: Tomas Vondra In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 7/24/23 14:04, Andrey Lepikhov wrote: > On 24/7/2023 16:56, Tomas Vondra wrote: >> On 7/24/23 04:10, Andrey Lepikhov wrote: >>> On 20/7/2023 18:46, Tomas Vondra wrote: >>>> On 7/20/23 08:37, Andrey Lepikhov wrote: >>>>> On 3/10/2022 21:56, Tom Lane wrote: >>>>>> Revert "Optimize order of GROUP BY keys". >>>>>> >>>>>> This reverts commit db0d67db2401eb6238ccc04c6407a4fd4f985832 and >>>>>> several follow-on fixes. >>>>>> ... >>>>>> Since we're hard up against the release deadline for v15, let's >>>>>> revert these changes for now.  We can always try again later. >>>>> >>>>> It may be time to restart the project. As a first step, I rebased the >>>>> patch on the current master. It wasn't trivial because of some latest >>>>> optimizations (a29eab, 1349d27 and 8d83a5d). >>>>> Now, Let's repeat the review and rewrite the current path according to >>>>> the reasons uttered in the revert commit. >>>> >>>> I think the fundamental task is to make the costing more reliable, and >>>> the commit message 443df6e2db points out a couple challenges in this >>>> area. Not sure how feasible it is to address enough of them ... >>>> >>>> 1) procost = 1.0 - I guess we could make this more realistic by doing >>>> some microbenchmarks and tuning the costs for the most expensive cases. >>>> >>>> 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. >>> I read up on the history of this thread. >>> As I see, all the problems mentioned above can be beaten by excluding >>> the new cost model at all. We can sort GROUP BY columns according to the >>> 'ndistinct' value. >>> I see the reason for introducing the cost model in [1]. The main >>> supporting point here is that with this patch, people couldn't optimize >>> the query by themselves, organizing the order of the columns in a more >>> optimal way. But now we have at least the GUC to switch off the >>> behaviour introduced here. Also, some extensions, like the well-known >>> pg_hint_plan, can help with automation. >> >> I think the main concern is that if we reorder the group keys and get it >> wrong, it's a regression. If that happens often (due to costing based on >> poor stats), it's a problem. Yes, there's a GUC, but that's a rather >> blunt instrument, unfortunately. > I see. My point here is if the ndistinct of one column is much more than > the ndistinct of another, it is more probable that this correlation will > be kept in the grouping phase. Here we can get some regression, which > can be overweighed by the possibility below. I think the word "probable" hits what the problem is. Because what if it isn't? Also, we don't actually need ndistinct for individual attributes, but for the groups of attributes. Imagine you do GROUP BY a, b so you need to estimate whether to do (a,b) or (b,a). You need to calculate ndistinct(a,b) / ndistinct(a) and ndistinct(b,a) / ndistinct(b) And similarly for more grouping keys. This is why we have ndistinct extended statistics, mostly. >> >>> So, how about committing of the undoubted part of the feature and >>> working on the cost model in a new thread? >>> >> >> But Tom's commit message says this: >> >>      Worse, to arrive at estimates of the number of calls made to the >>      lower-order-column comparison functions, the code needs to make >>      estimates of the numbers of distinct values of multiple columns, >>      which are necessarily even less trustworthy than per-column stats. >> >> so I'm not sure this really counts as "undoubted". > Don't try to estimate multiple  columns - just sort columns according to > the value of ndistinct as a heuristic. Surely you realize how easy such simple heuristics can fail, right? > I think we should estimate the number of values of multiple columns only > if we have extended statistics on these columns. And this can extend the > applicability of extended statistics. > I don't see why this should estimate ndistinct differently than every other place. That'd certainly be surprising. I can be convinced, but there needs to be sound justification why that's the right way to do (i.e. why it's less likely to cause poor planning choices). > The suggestion above is just an attempt to gather low-hanging fruit > right now. If it is not applicable, we will go a long way. > I'm not sure this qualities as "low hanging fruit" - that generally means simple changes with little risk. But you're using it for rather simplistic heuristic that can easily misfire (I think). At least that's my impression, I can be convinced otherwise. regards -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company