Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oB9TO-000353-RU for pgsql-bugs@arkaria.postgresql.org; Tue, 12 Jul 2022 06:34:19 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1oB9TN-00056s-NX for pgsql-bugs@arkaria.postgresql.org; Tue, 12 Jul 2022 06:34:17 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oB9TN-00056g-98 for pgsql-bugs@lists.postgresql.org; Tue, 12 Jul 2022 06:34:17 +0000 Received: from mail.postgrespro.ru ([93.174.131.139]) by makus.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1oB9TI-00074L-IP for pgsql-bugs@lists.postgresql.org; Tue, 12 Jul 2022 06:34:16 +0000 Received: from [192.168.28.53] (cyclops.postgrespro.ru [93.174.131.138]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client did not present a certificate) by mail.postgrespro.ru (Postfix) with ESMTPSA id B09BC21C0360; Tue, 12 Jul 2022 09:34:09 +0300 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=postgrespro.ru; s=mail; t=1657607649; bh=7AVLJWkgFqtu/o07Lw434Hg/18y6e57iujII8XMSoLE=; h=Date:Subject:To:References:From:In-Reply-To; b=SUQ3nwpvRAmGnAaFnVWRIvnoTCqc/yH8PCZQtdrgSEIL8DhCzNDgVSQSVdQN4SF+Z +i7w7zZ7OzLxk1CHpO8XlvTPbHzMFHXf5Cqh6N3NUinXBO+iT2jf2V11WhT8PSPctV 3Uib3/gzoPVxiYsZMX/GVG0nRXY77lCRrjldqPCU= Message-ID: Date: Tue, 12 Jul 2022 11:34:09 +0500 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.9.1 Subject: Re: BUG #17544: Join order for INNER JOIN ... USING with GROUP BY on join column affects selected query plan Content-Language: en-US To: _@ericsheng.com, pgsql-bugs@lists.postgresql.org, PG Bug reporting form References: <17544-ebd06b00b8836a04@postgresql.org> From: Andrey Lepikhov Organization: Postgres Professional In-Reply-To: <17544-ebd06b00b8836a04@postgresql.org> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 7/10/22 09:52, PG Bug reporting form wrote: > All join orders for inner joins give semantically equivalent results, and > documentation at https://www.postgresql.org/docs/current/explicit-joins.html > indicates that the planner should explore all join orders unless there are > too many tables, so I would have expected the join order here to be > immaterial to the query plan chosen. > > Digging a bit deeper, this appears to be due to the USING clause causing the > GROUP BY group_id to be rewritten to `groups.group_id` in the first query > and `tasks.group_id` in the second query, and the former resulting in the > simpler plan not being used. The same difference can be observed running: > > EXPLAIN (ANALYZE, TIMING) SELECT groups.group_id, BOOL_AND(finished) > FROM tasks INNER JOIN groups ON tasks.group_id = groups.group_id GROUP BY > groups.group_id, priority ORDER BY priority ASC LIMIT 10; > (the slow plan) > > EXPLAIN (ANALYZE, TIMING) SELECT tasks.group_id, BOOL_AND(finished) FROM > tasks INNER JOIN groups ON tasks.group_id = groups.group_id GROUP BY > tasks.group_id, priority ORDER BY priority ASC LIMIT 10; > (the fast plan) Documentation speaks the truth - optimizer checks all join permutations. But USING clause doesn't pull both group_id vars and implicitly chooses only one according to an algorithm, described in parse_clause.c::buildMergedJoinVar(). So, because you use group_id in upper GROUP BY, optimizer is limited to specific set of strategies, because it must use the same grouping variable, as implicitly chosen in the JOIN USING clause. -- Regards Andrey Lepikhov Postgres Professional