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 1rxJtx-007fDs-O8 for pgsql-hackers@arkaria.postgresql.org; Thu, 18 Apr 2024 05:01:38 +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 1rxJtu-000PsA-Tl for pgsql-hackers@arkaria.postgresql.org; Thu, 18 Apr 2024 05:01:34 +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 1rxJtu-000Ps1-Dx for pgsql-hackers@lists.postgresql.org; Thu, 18 Apr 2024 05:01:34 +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 1rxJtm-003OzP-GF for pgsql-hackers@lists.postgresql.org; Thu, 18 Apr 2024 05:01:32 +0000 Received: from [10.10.20.179] (node-lzv.pool-101-109.dynamic.totinternet.net [101.109.111.91]) (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 DA49EE20C32; Thu, 18 Apr 2024 08:01:19 +0300 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=postgrespro.ru; s=mx2023; t=1713416483; bh=5nT+ani4c1LGsSjoFQ0ewwcJ5toRXeBVOBuacnyfYs8=; h=Message-ID:Date:User-Agent:Subject:To:Cc:References:From: In-Reply-To:From; b=60GHRNC+oyuAuVhbrYYczo26//ECgsz9tv66towKvUqR9CiZB4xNKh2QT8U93oiyG zs6bDjVFTtaX3Ye93qycsfF+9B1NYEhN0Gu5tMzxiOgUsyfNCh6ddDRCFJH/fHQS1g BUrYMRQdrbV15EAkwPDnA+l8y26p6WJvvvpQu4da+TrsJGDJSBcCwyzqnaDLeQs2Ac QJuwkngiMc4UwBGcr8TGqKSpMy7Gw/V7D2wKLZhTpMZcoiBkWmrkK+EbMQyBAjLan9 pFKQfJbfa0ehbnvUwvi8FJWeJjzMToJ4oQiputUXke423RcJ4ZR1nBO8PQBkuiefpq vlZmoSyg5f0pg== Content-Type: multipart/mixed; boundary="------------jKg8Rdd8p6XF2jpLBiq8ofhG" Message-ID: <50c1c1dc-057c-421b-a1f4-62622c7afe61@postgrespro.ru> Date: Thu, 18 Apr 2024 12:01:13 +0700 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: POC: GROUP BY optimization To: Tom Lane , Alexander Korotkov Cc: Richard Guo , Pavel Borisov , vignesh C , PostgreSQL Developers , Tomas Vondra , Teodor Sigaev , David Rowley , "a.rybakina" References: <3046975.1706839355@sss.pgh.pa.us> <890ed877-e2c0-448a-93b8-b07ccf3a2b37@postgrespro.ru> <266850.1712879082@sss.pgh.pa.us> Content-Language: en-AU From: Andrei Lepikhov Organization: Postgres Professional In-Reply-To: <266850.1712879082@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is a multi-part message in MIME format. --------------jKg8Rdd8p6XF2jpLBiq8ofhG Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 4/12/24 06:44, Tom Lane wrote: > * It seems like root->processed_groupClause no longer has much to do > with the grouping behavior, which is scary given how much code still > believes that it does. I suspect there are bugs lurking there, and 1. Analysing the code, processed_groupClause is used in: - adjust_foreign_grouping_path_cost - to decide, do we need to add cost of sort to the foreign grouping. - just for replacing relids during SJE process. - create_groupingsets_plan(), preprocess_grouping_sets, remove_useless_groupby_columns - we don't apply this feature in the case of grouping sets. - get_number_of_groups - estimation shouldn't depend on the order of clauses. - create_grouping_paths - to analyse hashability of grouping clause - make_group_input_target, make_partial_grouping_target - doesn't depend on the order of clauses planner.c: add_paths_to_grouping_rel, create_partial_grouping_paths - in the case of hash grouping. So, the only case we can impact current behavior lies in the postgres_fdw. But here we don't really know which plan will be chosen during planning on foreign node and stay the same behavior is legal for me. > am not comforted by the fact that the patch changed exactly nothing > in the pathnodes.h documentation of that field. This comment looks > pretty silly now too: > > /* Preprocess regular GROUP BY clause, if any */ > root->processed_groupClause = list_copy(parse->groupClause); > > What "preprocessing" is going on there? This comment was adequate > before, when the code line invoked preprocess_groupclause which had > a bunch of commentary; but now it has to stand on its own and it's > not doing a great job of that. Thanks for picking this place. I fixed it. More interesting here is that we move decisions on the order of group-by clauses to the stage, where we already have underlying subtree and incoming path keys. In principle, we could return the preprocessing routine and arrange GROUP-BY with the ORDER-BY clause as it was before. But looking into the code, I found only one reason to do this: adjust_group_pathkeys_for_groupagg. Curious about how much profit we get here, I think we can discover it later with no hurry. A good outcome here will be a test case that can show the importance of arranging GROUP-BY and ORDER-BY at an early stage. -- regards, Andrei Lepikhov Postgres Professional --------------jKg8Rdd8p6XF2jpLBiq8ofhG Content-Type: text/x-patch; charset=UTF-8; name="minor_comment.patch" Content-Disposition: attachment; filename="minor_comment.patch" Content-Transfer-Encoding: base64 ZGlmZiAtLWdpdCBhL3NyYy9iYWNrZW5kL29wdGltaXplci9wbGFuL3BsYW5uZXIuYyBiL3Ny Yy9iYWNrZW5kL29wdGltaXplci9wbGFuL3BsYW5uZXIuYwppbmRleCA1ZWEzNzA1Nzk2Li44 NjE2NTZhMTkyIDEwMDY0NAotLS0gYS9zcmMvYmFja2VuZC9vcHRpbWl6ZXIvcGxhbi9wbGFu bmVyLmMKKysrIGIvc3JjL2JhY2tlbmQvb3B0aW1pemVyL3BsYW4vcGxhbm5lci5jCkBAIC0x NDI0LDcgKzE0MjQsMTAgQEAgZ3JvdXBpbmdfcGxhbm5lcihQbGFubmVySW5mbyAqcm9vdCwg ZG91YmxlIHR1cGxlX2ZyYWN0aW9uLAogCQl9CiAJCWVsc2UgaWYgKHBhcnNlLT5ncm91cENs YXVzZSkKIAkJewotCQkJLyogUHJlcHJvY2VzcyByZWd1bGFyIEdST1VQIEJZIGNsYXVzZSwg aWYgYW55ICovCisJCQkvKgorCQkJICogTWFrZSBhIGNvcHkgb2Ygb3JpZ2luIGdyb3VwQ2xh dXNlIGJlY2F1c2Ugd2UgYXJlIGdvaW5nIHRvCisJCQkgKiByZW1vdmUgcmVkdW5kYW50IGNs YXVzZXMuCisJCQkgKi8KIAkJCXJvb3QtPnByb2Nlc3NlZF9ncm91cENsYXVzZSA9IGxpc3Rf Y29weShwYXJzZS0+Z3JvdXBDbGF1c2UpOwogCQkJLyogUmVtb3ZlIGFueSByZWR1bmRhbnQg R1JPVVAgQlkgY29sdW1ucyAqLwogCQkJcmVtb3ZlX3VzZWxlc3NfZ3JvdXBieV9jb2x1bW5z KHJvb3QpOwo= --------------jKg8Rdd8p6XF2jpLBiq8ofhG--