Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1wz4MI-003nBQ-2M for pgsql-hackers@arkaria.postgresql.org; Wed, 26 Aug 2026 03:31:26 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wz4MH-00AvkH-2K for pgsql-hackers@arkaria.postgresql.org; Wed, 26 Aug 2026 03:31:25 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1wz4MH-00Avjl-1O for pgsql-hackers@lists.postgresql.org; Wed, 26 Aug 2026 03:31:25 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1wz4MF-00000001IG9-0BXc for pgsql-hackers@lists.postgresql.org; Wed, 26 Aug 2026 03:31:25 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.18.1/8.18.1) with ESMTP id 67Q3VIbe661905; Tue, 25 Aug 2026 23:31:18 -0400 From: Tom Lane To: William Bernbaum cc: "pgsql-hackers@lists.postgresql.org" Subject: Re: Plan a filtering inner join as a semijoin In-reply-to: References: Comments: In-reply-to William Bernbaum message dated "Wed, 26 Aug 2026 03:02:18 -0000" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <661903.1787715078.1@sss.pgh.pa.us> Date: Tue, 25 Aug 2026 23:31:18 -0400 Message-ID: <661904.1787715078@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk William Bernbaum writes: > Recently I improved a dynamic SQL compiler (an ORM-like layer) using a > pattern that seems to generalize to the Postgres planner. I taught the > layer to emit an EXISTS subquery when the target relation only served to > filter, rather than fanning out a join whose extra rows were discarded. > That led me into the join elimination machinery, and to the attached draft > patch. It adds convert_joins_to_semijoins(), which recognizes an inner > join that only restricts which rows survive and adds a JOIN_SEMI > SpecialJoinInfo for the filtering relation. I find myself a bit astonished by this proposal. Normally we try to convert semijoins to inner joins in order to gain join-order flexibility. I can't see a good reason to go in the other direction. There might be a reason to install some special case for the query pattern you have in mind, but I really doubt that this particular special case is the best option. regards, tom lane