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 1kj3ug-0004WJ-OK for pgsql-hackers@arkaria.postgresql.org; Sat, 28 Nov 2020 17:21:35 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kj3ue-0005im-4q for pgsql-hackers@arkaria.postgresql.org; Sat, 28 Nov 2020 17:21:32 +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 1kj3ud-0005if-QH for pgsql-hackers@lists.postgresql.org; Sat, 28 Nov 2020 17:21:31 +0000 Received: from mail.postgrespro.ru ([93.174.131.139]) by makus.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kj3uZ-0006to-Ka for pgsql-hackers@postgresql.org; Sat, 28 Nov 2020 17:21:30 +0000 Received: from ip-192-168-0-106.is74.loc (pool-77-222-105-100.is74.ru [77.222.105.100]) (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 9016F21C6A34; Sat, 28 Nov 2020 20:21:18 +0300 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=postgrespro.ru; s=mail; t=1606584083; bh=/CY3NtaXKo3e5aOVFwz0ZD8rXv71nkw1VxGO3j2XgWs=; h=Subject:To:Cc:References:From:Date:In-Reply-To; b=bnMQfKBuecomJPlziGPVVHkHT+SM3J7Q7Gi2ZN7r/kNGrWpuNDtft0VtFPUniU863 LdizWWR5MSvn5Y1XDBaVJEfJJPbByH/E49ndbbCYDjtpJBxVu9/11pc/AQbRYAKZzs mti0+g8+tZqq2wQORDN0OqHzN5MUZ3v+Hzszw8kQ= Subject: Re: Removing unneeded self joins To: Heikki Linnakangas , David Rowley Cc: David Steele , PostgreSQL-development References: <64486b0b-0404-e39e-322d-0801154901f3@postgrespro.ru> <89c8a17d-01f8-a03d-7fb8-0a8a070be47c@postgrespro.ru> <9b226ea2-bce0-90b2-9909-7964b7f353f7@postgrespro.ru> <8556348d-b5fa-728a-e4db-8dcad9d11e4b@postgrespro.ru> <9aa6421b-fa8c-b578-6f18-0174c51f0884@postgrespro.ru> <2c08741c-3d58-f517-23de-549d6c15e14e@postgrespro.ru> <9315b1bc-f134-93aa-ed38-0d6a08719058@postgrespro.ru> <031ab7da-c943-cd65-590e-671add024e3f@pgmasters.net> <8a427a9a-50cd-51e9-3c1c-2176eceb39b0@postgrespro.ru> <30b38869-9430-31fb-39e1-c735074b9259@postgrespro.ru> <16aa5d06-d8c6-416a-cb71-21663019944e@iki.fi> From: Andrey Lepikhov Organization: Postgres Professional Message-ID: Date: Sat, 28 Nov 2020 22:21:17 +0500 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:78.0) Gecko/20100101 Thunderbird/78.5.0 MIME-Version: 1.0 In-Reply-To: <16aa5d06-d8c6-416a-cb71-21663019944e@iki.fi> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Thank you for the review, On 27.11.2020 21:49, Heikki Linnakangas wrote: > On 31/10/2020 11:26, Andrey V. Lepikhov wrote: >> +            /* >> +             * Process restrictlist to seperate out the self join >> quals from >> +             * the other quals. e.g x = x goes to selfjoinquals and a >> = b to >> +             * otherjoinquals. >> +             */ >> +            split_selfjoin_quals(root, restrictlist, &selfjoinquals, >> +                                 &otherjoinquals); >> + >> +            if (list_length(selfjoinquals) == 0) >> +            { >> +                /* >> +                 * Have a chance to remove join if target list >> contains vars from >> +                 * the only one relation. >> +                 */ > > I don't understand the logic here. If 'selfjoinquals' is empty, it means > that there is no join qual between the two relations, right? How can we > ever remove the join in that case? And how does the target list affect > that? Can you give an example query of that? Maybe it is a problem of variable naming. Following the idea of David Rowley, we split quals into two subsets: {x==x} and another, for example {x=y}. First set is an trivial case of self-join: if we have unique index on the attribute 'x', then this join is self-join. Second set is give us a chance: if right side is unique for right side of the qual and no vars from right side end up in the target list of the join, then this is a self-join case. Example: CREATE TABLE a(x int, y int); CREATE UNIQUE INDEX ON a(x); SELECT a1.* FROM a a1, a a2 WHERE a1.x = a2.x; -- self-join CREATE UNIQUE INDEX ON a(y); SELECT a1.* FROM a a1, a a2 WHERE a1.x = a2.y; -- self-join too > >> --- a/src/test/regress/expected/join.out >> +++ b/src/test/regress/expected/join.out >> @@ -4553,11 +4553,13 @@ explain (costs off) >>  select p.* from >>    (parent p left join child c on (p.k = c.k)) join parent x on p.k = x.k >>    where p.k = 1 and p.k = 2; >> -        QUERY PLAN --------------------------- >> +                   QUERY PLAN >> +------------------------------------------------ >>   Result >>     One-Time Filter: false >> -(2 rows) >> +   ->  Index Scan using parent_pkey on parent x >> +         Index Cond: (k = 1) >> +(4 rows) >> >>  -- bug 5255: this is not optimizable by join removal >>  begin; > > That doesn't seem like an improvement... I investigated this case. It is a planner feature: it simplifies dummy joins and dummy scans to different plans. Maybe it can cause some discussion, but this example so rare and doesn't make a problem. > My general impression of this patch is that it's a lot of code to deal > with a rare special case. At the beginning of this thread there was > discussion on the overhead that this might add to planning queries that > don't benefit, but adding a lot of code isn't nice either, even if the > performance is acceptable. That's not necessarily a show-stopper, but it > does make me much less excited about this. I'm not sure what to suggest > to do about that, except a really vague "Can you make is simpler?" Currently planner reduces useless outer joins and unique semijoins. Reduce self join feature continues the development of the planner in the same direction. For example, it is needed for ORM software. Most of the code dedicated to removing unnecessary relation and replacing of one oid with another. We are trying to use remove_rel_from_query() machinery. Perhaps this will allow us to make the code shorter. -- regards, Andrey Lepikhov Postgres Professional