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 1rWUZK-005hty-F7 for pgsql-bugs@arkaria.postgresql.org; Sun, 04 Feb 2024 04:57:27 +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 1rWUZG-004UAw-G4 for pgsql-bugs@arkaria.postgresql.org; Sun, 04 Feb 2024 04:57: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 1rWUZG-004UAn-3W for pgsql-bugs@lists.postgresql.org; Sun, 04 Feb 2024 04:57:22 +0000 Received: from mail.postgrespro.ru ([93.174.131.139]) by magus.postgresql.org with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rWUZD-005MA5-Bq for pgsql-bugs@lists.postgresql.org; Sun, 04 Feb 2024 04:57:21 +0000 Received: from [192.168.1.35] (cm-27-145-170-221.revip12.asianet.co.th [27.145.170.221]) (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 9608EE21127; Sun, 4 Feb 2024 07:57:14 +0300 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=postgrespro.ru; s=mx2023; t=1707022637; bh=SCddIOS7qq+5dbAj8bb++bXkSCP2OxjfK7i/uCR/uvc=; h=Message-ID:Date:User-Agent:Subject:From:To:Cc:References: In-Reply-To:From; b=Nlss3zZP8KbrmJLekpUbTqWZqLP8pACJmuH0uuOJ5juuE57ipTvb7Czf/yu9o10h8 NUlH8m4gONQlXyclkgU9bfI9CESR8ghFL6S/5im9CFOm83QZ9bfqWxrWQ+2FPhCd7d yHlxfwEWK2VwIdzsrELYX2Snr9mW0iBUxG1RGGm3fgSjXOSwjX9dYN9aCEdhwt/3NO wYayyWMzwBJbTkpTfVpBWI9iwiIXqngyP1g+GxUyNAPaFoa8rTipbb60sqFhAASGS/ OTD2Ts3NZw8OpDPybHBTZj2OtZgxZ+O3CJy5NHryEiG3wxJgusjQP0dXsAT6QU1IyV RWIC6oZ2HaJiA== Content-Type: multipart/mixed; boundary="------------PHLSBMgPC0w0H0LgJhfbzOV7" Message-ID: Date: Sun, 4 Feb 2024 11:57:11 +0700 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: v17 Possible Union All Bug From: Andrei Lepikhov To: Alexander Korotkov , "David G. Johnston" , Tom Lane Cc: David Rowley , Alexander Korotkov , PostgreSQL Bug List , Richard Guo References: <41499cc3-31a1-4c9e-85d6-45e6d901ea6a@postgrespro.ru> Content-Language: en-US Organization: Postgres Professional In-Reply-To: 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. --------------PHLSBMgPC0w0H0LgJhfbzOV7 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit And finally, I've got the synthetic test: CREATE TABLE mess_grouping (x integer, y integer, z integer, w integer, f integer); INSERT INTO mess_grouping (x,y,z,w,f) (SELECT x%10, x % 2, x%2, 2, x%10 FROM generate_series(1,100) AS x); ANALYZE mess_grouping; SET enable_nestloop = 'off'; SET enable_hashjoin = 'off'; SET enable_hashagg = 'off'; SET enable_group_by_reordering = 'on'; SELECT c1.z, c1.w, string_agg(''::text, repeat(''::text, c1.f) ORDER BY c1.x,c1.y) FROM mess_grouping c1 JOIN mess_grouping c2 ON (c1.x = c2.f) GROUP BY c1.w, c1.z; SET enable_group_by_reordering = 'off'; SELECT c1.z, c1.w, string_agg(''::text, repeat(''::text, c1.f) ORDER BY c1.x,c1.y) FROM mess_grouping c1 JOIN mess_grouping c2 ON (c1.x = c2.f) GROUP BY c1.w, c1.z; DROP TABLE IF EXISTS mess_grouping CASCADE; You can see here, that first query execution produces: z | w | string_agg ---+---+------------ 0 | 2 | 1 | 2 | 0 | 2 | 1 | 2 | 0 | 2 | 1 | 2 | 0 | 2 | 1 | 2 | 0 | 2 | 1 | 2 | (10 rows) and second execution gives correct result: z | w | string_agg ---+---+------------ 0 | 2 | 1 | 2 | (2 rows) The simple fix is in the attachment. But I'm not sure we should fix GROUP-BY optimization instead of the more general issue. The source of the problem is root->group_pathkeys, which contains grouping pathkeys and aggregate pathkeys. For now, their 'sortref' values could intersect, and we can differ which one references the query target list and which one the target list of the aggregate. So, I would like to get advice here: should we make a quick fix here, or is such a mess in the sortref values not a mess and designed for some purposes? -- regards, Andrei Lepikhov Postgres Professional --------------PHLSBMgPC0w0H0LgJhfbzOV7 Content-Type: text/plain; charset=UTF-8; name="naive_fix.diff" Content-Disposition: attachment; filename="naive_fix.diff" Content-Transfer-Encoding: base64 ZGlmZiAtLWdpdCBhL3NyYy9iYWNrZW5kL29wdGltaXplci9wYXRoL3BhdGhrZXlzLmMgYi9z cmMvYmFja2VuZC9vcHRpbWl6ZXIvcGF0aC9wYXRoa2V5cy5jCmluZGV4IDgyZmYzMTI3M2Iu LmY4OTc0NWYwNjMgMTAwNjQ0Ci0tLSBhL3NyYy9iYWNrZW5kL29wdGltaXplci9wYXRoL3Bh dGhrZXlzLmMKKysrIGIvc3JjL2JhY2tlbmQvb3B0aW1pemVyL3BhdGgvcGF0aGtleXMuYwpA QCAtMzY5LDYgKzM2OSw3IEBAIGdyb3VwX2tleXNfcmVvcmRlcl9ieV9wYXRoa2V5cyhMaXN0 ICpwYXRoa2V5cywgTGlzdCAqKmdyb3VwX3BhdGhrZXlzLAogewogCUxpc3QJICAgKm5ld19n cm91cF9wYXRoa2V5cyA9IE5JTCwKIAkJCSAgICpuZXdfZ3JvdXBf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