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 1jZ6hJ-0007Wd-10 for pgsql-performance@arkaria.postgresql.org; Thu, 14 May 2020 05:46:21 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jZ6hH-0006av-VZ for pgsql-performance@arkaria.postgresql.org; Thu, 14 May 2020 05:46:19 +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 1jZ6hG-0006aH-PH for pgsql-performance@lists.postgresql.org; Thu, 14 May 2020 05:46:19 +0000 Received: from [64.147.123.25] (helo=wout2-smtp.messagingengine.com) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jZ6hE-0004Gh-1t for pgsql-performance@lists.postgresql.org; Thu, 14 May 2020 05:46:17 +0000 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.west.internal (Postfix) with ESMTP id B4ECD973 for ; Thu, 14 May 2020 01:46:04 -0400 (EDT) Received: from imap36 ([10.202.2.86]) by compute4.internal (MEProxy); Thu, 14 May 2020 01:46:04 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=binarykeep.com; h=mime-version:message-id:date:from:to:subject:content-type; s= mesmtp; bh=AXFV0X+in4WGsEMwiCjPsFBzWvuN/FNquh3mEu62zm8=; b=68Vzo RMyKnByoxeOPIRDBh1Zhv5pFU75zLSK/ZMtZ4cEDLfEHlBjeNacieWKpZTiLd862 HXy/CaezK+Y0pb/Qhpu1N/wrZnlYW98jl6wdCsWVl/OOjuna2EJwZkH2/aihP/eQ B9ak9xMKgu5XQGUfgOQpDg5V78EAzKHbXmBYjo= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-type:date:from:message-id :mime-version:subject:to:x-me-proxy:x-me-proxy:x-me-sender :x-me-sender:x-sasl-enc; s=fm2; bh=AXFV0X+in4WGsEMwiCjPsFBzWvuN/ FNquh3mEu62zm8=; b=vrBGUs1/8pceDvoBv2mX73vWhDdXP44PEuxWOx1watKvJ J43dv3mPQZ7Db7Tw6XANAnzOg1CFHCmkwe0nrcEkz7Q7/B7COG0tEnGF0YPT8Z86 u67nlKiu3lBZhdCyggfuAgWWoMehfMdHkJVNNOwr3AScCJ9VWtuDIQHgU8QgxGUC 9XZQZTAhW0YIfNOpkKjylpID1bjpCjksuLELm/3OOQYvyOq+PT1IvWkSPprfxVsE VTcljWIUN+5fyaaLHPFq1AX8neBuEft/WeAgxQQ0EQIG30RGms4LEjZ1gi94u+/s Y3aIq78/lIWBOE/1TSEA5edf8cJbnaOvHdeJ2q0QA== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduhedrleehgdelkecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecunecujfgurhepofgfggfkfffhvffutgesthdtredtre ertdenucfhrhhomhepfdforghtthhhvgifucfpvghlshhonhdfuceophhoshhtghhrvghs segsihhnrghrhihkvggvphdrtghomheqnecuggftrfgrthhtvghrnhepteelgeegkeduve fhgfefffdttdfhhfejueeiueejkeefieeuvedujeefieeiteevnecuvehluhhsthgvrhfu ihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomhepphhoshhtghhrvghssegsihhnrg hrhihkvggvphdrtghomh X-ME-Proxy: Received: by mailuser.nyi.internal (Postfix, from userid 501) id A52DB188037F; Thu, 14 May 2020 01:46:02 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.3.0-dev0-413-g750b809-fmstable-20200507v1 Mime-Version: 1.0 Message-Id: <677075a1-50bf-48b7-b7b6-ecebb8786741@www.fastmail.com> Date: Wed, 13 May 2020 22:44:56 -0700 From: "Matthew Nelson" To: pgsql-performance@lists.postgresql.org Subject: Plan not skipping unnecessary inner join Content-Type: text/plain X-Host-Lookup-Failed: Reverse DNS lookup failed for 64.147.123.25 (deferred) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk I noticed something peculiar while optimizing complex views today. The query planner does not skip inner joins that, to my understanding, can have no impact on the result. Am I missing a situation where these joins could impact the result? The following demonstrates the problem without the complex views. It also demonstrates how the planner simplifies a LEFT JOIN in the same situation. The left and right sides of an inner join could be swapped, obviously, but here I kept the unique constraint on the right. CREATE TABLE foo ( id INTEGER PRIMARY KEY ); CREATE TABLE bar ( foo_id INTEGER NOT NULL REFERENCES foo ); -- This simplifies to SELECT COUNT(*) FROM bar; EXPLAIN SELECT COUNT(*) FROM bar LEFT JOIN foo ON bar.foo_id = foo.id; -- This should simplify to SELECT COUNT(*) FROM bar WHERE foo_id IS NOT NULL; -- The presence of a NOT NULL constraint on foo_id has no effect. EXPLAIN SELECT COUNT(*) FROM bar INNER JOIN foo ON bar.foo_id = foo.id; QUERY PLAN ------------------------------------------------------------- Aggregate (cost=38.25..38.26 rows=1 width=8) -> Seq Scan on bar (cost=0.00..32.60 rows=2260 width=0) (2 rows) QUERY PLAN ------------------------------------------------------------------------- Aggregate (cost=111.57..111.58 rows=1 width=8) -> Hash Join (cost=67.38..105.92 rows=2260 width=0) Hash Cond: (bar.foo_id_not_null = foo.id) -> Seq Scan on bar (cost=0.00..32.60 rows=2260 width=4) -> Hash (cost=35.50..35.50 rows=2550 width=4) -> Seq Scan on foo (cost=0.00..35.50 rows=2550 width=4) (6 rows) version ------------------------------------------------------------------------------------------------------------------- PostgreSQL 12.2 on x86_64-apple-darwin19.4.0, compiled by Apple clang version 11.0.3 (clang-1103.0.32.59), 64-bit (1 row)