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 1lyYHS-0007G9-J2 for pgsql-hackers@arkaria.postgresql.org; Wed, 30 Jun 2021 11:21:23 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lyYHR-0006QB-Bi for pgsql-hackers@arkaria.postgresql.org; Wed, 30 Jun 2021 11:21:21 +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 1lyYHR-0006Q3-29 for pgsql-hackers@lists.postgresql.org; Wed, 30 Jun 2021 11:21:21 +0000 Received: from mail.postgrespro.ru ([93.174.131.139]) by makus.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lyYHO-0007jP-6C for pgsql-hackers@lists.postgresql.org; Wed, 30 Jun 2021 11:21:20 +0000 Received: from Andreys-MacBook-Pro.local (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 EF4C521C8A6D; Wed, 30 Jun 2021 14:21:15 +0300 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=postgrespro.ru; s=mail; t=1625052076; bh=/4om4WZ5WcV88ZTok7WDCQZW+Xou3Gm11jKdodx6EG4=; h=Subject:To:Cc:References:From:Date:In-Reply-To; b=oc+SLWZfPuCS6q4xNqeAKhOguweLRae67E1pEUgQPNyp1FHV2EfWMrvYGqyLodMTl Hl6KKiMFAm5vWgDnp41zfTOEOLg0QEhrglR5Po5kTcn/d8EabAQhnTQduGswwxNCEm lsOeq4ycNVKl0+kQL/dFUoJgDfClukR338q9NgJ4= Subject: Re: Removing unneeded self joins To: Hywel Carver , pgsql-hackers@lists.postgresql.org Cc: Konstantin Knizhnik References: <64486b0b-0404-e39e-322d-0801154901f3@postgrespro.ru> <21e15759-dba5-2d2b-dd03-06943ad40378@postgrespro.ru> <161553990603.28624.7702383984090736980.pgcf@coridan.postgresql.org> From: Andrey Lepikhov Organization: Postgres Professional Message-ID: <2a6e540a-4c9f-9402-2e02-71e35fcd4dd1@postgrespro.ru> Date: Wed, 30 Jun 2021 14:21:15 +0300 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:78.0) Gecko/20100101 Thunderbird/78.11.0 MIME-Version: 1.0 In-Reply-To: <161553990603.28624.7702383984090736980.pgcf@coridan.postgresql.org> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 12/3/21 12:05, Hywel Carver wrote: > I've built and tested this, and it seems to function correctly to me. One question I have is whether the added "IS NOT NULL" filters can be omitted when they're unnecessary. Some of the resulting plans included an "IS NOT NULL" filter on a non-nullable column. To be clear, this is still an improvement (to me) without that. I think, here we could ask more general question: do we want to remove a 'IS NOT NULL' clause from the clause list if the rest of the list implicitly implies it? Right now we don't analyze list of clauses at all: CREATE TABLE a (x int); EXPLAIN (COSTS OFF) SELECT * FROM a WHERE (x < 1) AND (X IS NOT NULL) AND (x < 1) AND (X IS NOT NULL); QUERY PLAN ------------------------------------------------------------------------- Seq Scan on a Filter: ((x IS NOT NULL) AND (x IS NOT NULL) AND (x < 1) AND (x < 1)) And even worse: EXPLAIN (ANALYZE, VERBOSE) SELECT * FROM a WHERE (X IS NOT NULL) AND (X IS NULL); QUERY PLAN ---------------------------------------------------------------------------------------------------- Seq Scan on public.a (cost=0.00..15.00 rows=87 width=4) (actual time=0.136..0.136 rows=0 loops=1) Output: x Filter: ((a.x IS NOT NULL) AND (a.x IS NULL)) Rows Removed by Filter: 1000 It could reduce a number of selectivity mistakes, but increase CPU consumption. If we had such a clause analyzing machinery, we could trivially remove this unneeded qual. -- regards, Andrey Lepikhov Postgres Professional