Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1h8PuY-0007qg-Kq for pgsql-hackers@arkaria.postgresql.org; Mon, 25 Mar 2019 13:45:11 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1h8PuV-0000D2-GY for pgsql-hackers@arkaria.postgresql.org; Mon, 25 Mar 2019 13:45:07 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1h8PuV-0000Cv-3p for pgsql-hackers@lists.postgresql.org; Mon, 25 Mar 2019 13:45:07 +0000 Received: from cyclops.postgrespro.ru ([93.174.131.138] helo=mail.postgrespro.ru) by magus.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1h8PuR-0006aK-RM for pgsql-hackers@postgresql.org; Mon, 25 Mar 2019 13:45:06 +0000 Received: from localhost (localhost [127.0.0.1]) by mail.postgrespro.ru (Postfix) with ESMTP id EC7F321C3FC1; Mon, 25 Mar 2019 16:45:02 +0300 (MSK) X-Virus-Scanned: Debian amavisd-new at postgrespro.ru X-Spam-Flag: NO X-Spam-Score: 0 X-Spam-Level: X-Spam-Status: No, score=x tagged_above=-99 required=4 WHITELISTED tests=[] autolearn=unavailable Received: from [192.168.27.191] (gw.postgrespro.ru [93.174.131.141]) (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 BDB9921C3ED8; Mon, 25 Mar 2019 16:45:02 +0300 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=postgrespro.ru; s=mail; t=1553521502; bh=ucaptNas2l1tBukV4Lp8/qwAICv72IUEUEMuwF1BHyI=; h=Subject:To:Cc:References:From:Date:In-Reply-To; b=Ls9gFn8V1g3XC6+NKf03w0ZJZU7TMQ/yQlu14egMSbkcHDEueAStb8oV+oMzpi6xl zDc5kG68IDMNFdwj76rzgsCwxqaLR1yhYypyZ7Hoy39DDibMgX17Slha5F8jVLQ1jo Pna1+H0+IPmXsFc8mJYgE0ZQ7QNin6NjRBEu5veM= Subject: Re: Removing unneeded self joins To: David Rowley Cc: Tom Lane , PostgreSQL-development References: <64486b0b-0404-e39e-322d-0801154901f3@postgrespro.ru> <8a17cc3d-f933-ba32-6110-f2d8481070e7@postgrespro.ru> <2285d5d0-f330-e8b6-9ee5-b2b90e44409b@postgrespro.ru> <35fd3789-bde8-f290-9b55-da3d54cd6af5@postgrespro.ru> <23f6cf6d-32ca-faa2-f4ec-da690ba42482@postgrespro.ru> <22996.1550795112@sss.pgh.pa.us> <2b74af78-3417-167d-ad72-d54ec558d8be@postgrespro.ru> <552e481b-2feb-75fd-4e9f-4199bfd1c1f3@postgrespro.ru> <31c2ee33-5db1-a43c-b964-8e2624020ae9@postgrespro.ru> From: Alexander Kuzmenkov Message-ID: <89c8a17d-01f8-a03d-7fb8-0a8a070be47c@postgrespro.ru> Date: Mon, 25 Mar 2019 16:45:02 +0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.2.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 3/25/19 07:07, David Rowley wrote: > You had commented the test with: > > -- If index conditions are different for each side, we won't select the same > -- row on both sides, so the join can't be removed. > > but I don't quite understand why we can't remove the join in this > situation. My rationale was that we're not exactly removing the join, but replacing it with a scan. So it is not enough to just have a single row on each side, it must be the same physical row. In this example, before the transformation, t1.b is not equal to t2.b, but they become equal afterwards. This looks somewhat wrong. On the other hand, if the conditions are different, the resulting condition is going to evaluate to constant false and we won't get any rows, so maybe it's OK. This brings me again to the question of what are the conditions for join removal. If the formulation with indexes is not general enough, what do we use instead? I guess it could be something like this: 1. Given the (btree equality) join and restriction clauses, both sides are unique on the same set of columns. That is, if we fix the values of these columns, both sides have at most one matching row.     a. For each of these columns, we have either         i) a join clause that equates some expression referencing the outer column to the same expression referencing the same inner column.         ii) a clause for each relation that equates the same expression referencing the outer and inner column to some other arbitrary expression, possibly a different one for each side. This expression may be a Const or some expression that references a Var of some third relation. 2. All the resulting columns can be calculated using either side of the join. For now, just require that both sides are base relations that refer to the same physical relation. Two points are not clear to me here: 1. We don't handle join clauses to third relations, but can they be treated the same way we treat Consts? 2. Can we simplify the join when we don't have any join clauses and only have Consts? Or should we have at least one join clause that equates the same inner and outer column? Why is one join clause enough? -- Alexander Kuzmenkov Postgres Professional: http://www.postgrespro.com The Russian Postgres Company