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 1rVwLN-002FdD-SQ for pgsql-bugs@arkaria.postgresql.org; Fri, 02 Feb 2024 16:24:46 +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 1rVwLM-00ERjc-1b for pgsql-bugs@arkaria.postgresql.org; Fri, 02 Feb 2024 16:24:44 +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 1rVwLL-00ERjK-Kt for pgsql-bugs@lists.postgresql.org; Fri, 02 Feb 2024 16:24:43 +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 1rVwLE-0056xY-Gp for pgsql-bugs@lists.postgresql.org; Fri, 02 Feb 2024 16:24:42 +0000 Received: from [192.168.1.35] (cm-171-100-79-228.revip10.asianet.co.th [171.100.79.228]) (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 AB51FE21140; Fri, 2 Feb 2024 19:24:32 +0300 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=postgrespro.ru; s=mx2023; t=1706891074; bh=R3FgA9CCaJbj4CptN4Lt9SLfDIXzAWLNKxgk4XKdRes=; h=Message-ID:Date:User-Agent:Subject:From:To:Cc:References: In-Reply-To:From; b=z/52DhRHT4BhXqqbCoQu4CRjvqXO12+PSxCYVNPWDZYB0SuTzE9Mr0+fmmw/5ApGj zvx2gKOJTPHw/J04nvhINiItLiD9p7Ob/sY2Yg2PeRPmRlqR1W4bSwY+wZOb8a/les JlfNNnAeHlRCW8KnL32X031V26lAGjDXozvOfDZTS/j9LCZPDgWuKBEGG0YgTO2q7z Z+/FASAOZP1IM46aN1ob0AzVCp5AAVC7zKO41090SBlY8Ci6kLk3M2PsF0cFNdbduA ViVEmPMOzPoV1R0Yqf42Q/SGu8PAh/Gu6ZsLE9U+r/kVz0cOubxD6yFy4HPsD68L2E Qh5zIketkzncA== Message-ID: Date: Fri, 2 Feb 2024 23:24:29 +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" Cc: David Rowley , Alexander Korotkov , PostgreSQL Bug List References: <41499cc3-31a1-4c9e-85d6-45e6d901ea6a@postgrespro.ru> Content-Language: en-US Organization: Postgres Professional In-Reply-To: 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 1/2/2024 16:53, Andrei Lepikhov wrote: > On 1/2/2024 11:06, Andrei Lepikhov wrote: >>> Thank you for noticing.  I'm investigating this. >> Very curious bug. I simplified the test a bit (see in attachment), but >> still can't replace system tables, like pg_authid, with a plain table. >> Will try further. > Just for speedup the bug scrutiny - new replay script attached. A bit closer to the end. The symptom of the problem in incorrect order of the columns in IncrementalSort, look: -> GroupAggregate (actual time=1.136..1.157 rows=5 loops=1) Output: format('%I from %s'::text, other_role.rolname,... Group Key: grant_instance.via, other_role.rolname -> Incremental Sort (actual time=1.098..1.102 rows=5 loops=1) Output: other_role.rolname, grant_instance.via,... Sort Key: grant_instance.grantor, other_role.rolname,... Presorted Key: grant_instance.grantor -> Merge Join (rows=5 loops=1) Output: other_role.rolname, grant_instance.via,... Merge Cond: (grant_role.oid = grant_instance.grantor) Correct variant (without changing grouping order): -> GroupAggregate (actual time=0.638..0.655 rows=4 loops=1) Output: format('%I from %s'::text, other_role.rolname, ... Group Key: other_role.rolname, grant_instance.via -> Sort (actual time=0.626..0.630 rows=5 loops=1) Output: other_role.rolname, grant_instance.via, ... Sort Key: other_role.rolname, grant_instance.via, ... -> Merge Join (rows=5 loops=1) Output: other_role.rolname, grant_instance.via, ... Merge Cond: (grant_role.oid = grant_instance.grantor) But it is only a symptom. I can fix it easily, but what is the source? As I see, we have the same value of sortref for the grouping column other_role.rolname and for EquivalenceClass "grant_role.oid = grant_instance.grantor". We create sortref for other_role.rolname and grant_instance.via in adjust_group_pathkeys_for_groupagg, because aggregate string_agg() in the aggref->aggorder list contains both these columns. I don't see ORDER BY for these columns in the query. So Why is it happened? May it be a core bug? -- regards, Andrei Lepikhov Postgres Professional