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 1qNsIr-006DDh-QB for pgsql-hackers@arkaria.postgresql.org; Mon, 24 Jul 2023 09:56:33 +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 1qNsIq-009CyM-9a for pgsql-hackers@arkaria.postgresql.org; Mon, 24 Jul 2023 09:56:32 +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 1qNsIp-009CyE-Vo for pgsql-hackers@lists.postgresql.org; Mon, 24 Jul 2023 09:56:32 +0000 Received: from mail-ej1-x62c.google.com ([2a00:1450:4864:20::62c]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1qNsIl-001V40-PG for pgsql-hackers@lists.postgresql.org; Mon, 24 Jul 2023 09:56:30 +0000 Received: by mail-ej1-x62c.google.com with SMTP id a640c23a62f3a-99313a34b2dso612542266b.1 for ; Mon, 24 Jul 2023 02:56:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=enterprisedb.com; s=google; t=1690192587; x=1690797387; 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=a7j5kSjuugGQ0oeh6s8lld6MOY5aLVsDTqPRp1F3cFc=; b=j+FacxNAHVMyjQeKDiCLdFkVwa271wEP8pu0sChixSDZP2pWDTe8FkveaOMYB2N/08 FKGtNceEvYmlBqp3lctdHXlZYH4rNM8VgtejqVqhhPR2El4u3VmRfAmXbW7lSUsqJtcX 3BvYN6Oy+zRqGpZ8R3VDWzWKctkW11/PFJ8F6AG+thaUosgm2HYReTaHXRMpTTr+Zz52 DvKCFPCws1COk4CpbIBaZxjGMkFT+YWF2TcvZiCaTSkRRP+kWArs7x6JTelYD3n9wsVW 7bjSGBEBkKFNhAkn4dOxBKBzNrqMvWVclgAwfZWvnCKWJciGntpsSe7gtm58BpB9E1kU d5Rg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690192587; x=1690797387; 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=a7j5kSjuugGQ0oeh6s8lld6MOY5aLVsDTqPRp1F3cFc=; b=IYgvg4QZ7sYegz9EB0xrZXx9162Rgv1hUre0tCG3MKWay+zpIBdTUwBmiyMiYQhPrB m3LioAbD0R6sm8BmsKPXyIN7uO1IQMbEEBhoMCLGdxnbwVv64cnYD7y/E0LmeTt7FAH0 NqPq+iox1lGxIEcHN8xvZNjHSd0nRON7Fmp5O2OJG+nqVc+1RbFHqeBzqDh17do5fS5O Ud+246SqRlT+2THxeaiGjUFm/ZXV6Bem0K8RwXdJXXzbZK30zsZOT/HBeHGsr+VwO2Lk jptOn8WqY1mYm78oG8fCTDqPScG65rIkgjJBUoYopj8/I7SUTYRaoOGyzyC4z09jomyn yYag== X-Gm-Message-State: ABy/qLZFKgL80QfW2EHMcQwbDxT4Jlu59w4iPwjXFPypixTmCaZznVbC pqEp0ex4BPD4LLFEjqOSxs8zSA== X-Google-Smtp-Source: APBJJlE4fk6dbrx93rJenfcAEhXaiLkOSglhthvCD3WvtiJ4TJmXolq92GTjHFR4fUnkU1TiUU0wLQ== X-Received: by 2002:a17:906:3147:b0:994:1844:c7d1 with SMTP id e7-20020a170906314700b009941844c7d1mr9762787eje.13.1690192586750; Mon, 24 Jul 2023 02:56:26 -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 ov28-20020a170906fc1c00b00992ea405a79sm6489759ejb.166.2023.07.24.02.56.25 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 24 Jul 2023 02:56:26 -0700 (PDT) Message-ID: <29bd1f4f-6b69-df43-1f3a-6ef1fc501657@enterprisedb.com> Date: Mon, 24 Jul 2023 11:56:24 +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 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: <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> <3d30ae10-5c35-63c4-577c-d2e2479e15fd@postgrespro.ru> Content-Language: en-US From: Tomas Vondra In-Reply-To: <3d30ae10-5c35-63c4-577c-d2e2479e15fd@postgrespro.ru> 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 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. > 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". regards -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company