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 1qiSZ7-006U4T-AP for pgsql-hackers@arkaria.postgresql.org; Tue, 19 Sep 2023 04:42:25 +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 1qiSZ4-006Qe3-Or for pgsql-hackers@arkaria.postgresql.org; Tue, 19 Sep 2023 04:42:22 +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 1qiSZ4-006Qdk-8x for pgsql-hackers@lists.postgresql.org; Tue, 19 Sep 2023 04:42:22 +0000 Received: from mail.postgrespro.ru ([93.174.131.139]) by magus.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1qiSZ1-0062dY-Bl for pgsql-hackers@lists.postgresql.org; Tue, 19 Sep 2023 04:42:21 +0000 Received: from [172.30.0.166] (unknown [172.30.0.166]) (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 3994EE20E8E; Tue, 19 Sep 2023 07:42:15 +0300 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=postgrespro.ru; s=mx2023; t=1695098538; bh=vHrdnilMRyj2Q5C8giVsNj8sgVDM9hXlJjElPT2g4nI=; h=Message-ID:Date:User-Agent:Subject:To:Cc:References:From: In-Reply-To:From; b=dv++5IqRv72IFOk7Sb3WXPbuXG8w1/icE9to+vsfT4t2WsvmDQfngf8PLizJOFreT 8snAHVO12U+JcRlCiWmd0OHhAxKTj4rqlRiK9Ejb+nAkrR6s5sLfVPGpNoZl6fqgcO /x88fvRbC+R343UVxY9/UFtJhO/NoYgW5OpLdK1iS3kUgNYny0MjkallNT1n5k3cVE 3iYMub/7c/RYVEo4yVGgMyzHN8Jg1mAtoy+6t2hGyntD3j8B3siLMJPwHfyI25cAeM 1iWBj71AErt9X0fWzLR09adiyNn4RHuhV3fr9KI/Q5N0hl7xHPfzURP2ye1MBVh1Io 2rQ13mXUEQTSA== Message-ID: Date: Tue, 19 Sep 2023 11:42:13 +0700 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:102.0) Gecko/20100101 Thunderbird/102.15.1 Subject: Re: POC: GROUP BY optimization To: Tomas Vondra Cc: Teodor Sigaev , PostgreSQL Developers , David Rowley , "a.rybakina" , Tom Lane , =?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> Content-Language: en-US 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: 8bit 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: > 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. > 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. Ok, some thoughts on this part of the task. As I see, we have not so many different operators: 26 with fixed width and 16 with variable width: SELECT o.oid,oprcode,typname,typlen FROM pg_operator o JOIN pg_type t ON (oprleft = t.oid) WHERE (oprname='<') AND oprleft=oprright AND typlen>0 ORDER BY o.oid; SELECT o.oid,oprcode,typname,typlen FROM pg_operator o JOIN pg_type t ON (oprleft = t.oid) WHERE (oprname='<') AND oprleft=oprright AND typlen<0 ORDER BY o.oid; Benchmarking procedure of types with fixed length can be something like below: CREATE OR REPLACE FUNCTION pass_sort(typ regtype) RETURNS TABLE ( nrows integer, exec_time float ) AS $$ DECLARE data json; step integer; BEGIN SET work_mem='1GB'; FOR step IN 0..3 LOOP SELECT pow(100, step)::integer INTO nrows; DROP TABLE IF EXISTS test CASCADE; EXECUTE format('CREATE TABLE test AS SELECT gs::%s AS x FROM generate_series(1,%s) AS gs;', typ, nrows); EXPLAIN (ANALYZE, COSTS OFF, TIMING OFF, FORMAT JSON) SELECT * FROM test ORDER BY (x) DESC INTO data; SELECT data->0->'Execution Time' INTO exec_time; RETURN NEXT; END LOOP; END; $$ LANGUAGE plpgsql VOLATILE; Execution of SELECT * FROM pass_sort('integer'); shows quite linear grow of the execution time. So, using '2.0 * cpu_operator_cost' as a cost for the integer type (as a basis) we can calculate costs for other operators. Variable-width types, i think, could require more complex technique to check dependency on the length. Does this way look worthwhile? -- regards, Andrey Lepikhov Postgres Professional