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 1o196X-0002jB-3k for pgsql-general@arkaria.postgresql.org; Tue, 14 Jun 2022 16:09: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 1o196V-0005mu-VE for pgsql-general@arkaria.postgresql.org; Tue, 14 Jun 2022 16:09: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 1o196V-0005ml-L2 for pgsql-general@lists.postgresql.org; Tue, 14 Jun 2022 16:09:19 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1o196T-0001ye-E9 for pgsql-general@lists.postgresql.org; Tue, 14 Jun 2022 16:09:18 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 25EG9Gsk076345; Tue, 14 Jun 2022 12:09:16 -0400 From: Tom Lane To: "Dirschel, Steve" cc: "pgsql-general@lists.postgresql.org" Subject: Re: Postgres NOT IN vs NOT EXISTS optimization In-reply-to: References: Comments: In-reply-to "Dirschel, Steve" message dated "Tue, 14 Jun 2022 15:58:39 -0000" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <76343.1655222956.1@sss.pgh.pa.us> Date: Tue, 14 Jun 2022 12:09:16 -0400 Message-ID: <76344.1655222956@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk "Dirschel, Steve" writes: > Is Postgres able to drive the query the same way with the NOT IN as the > NOT EXISTS is doing or is that only available if the query has a NOT > EXISTS? NOT IN is not optimized very well in PG, because of the strange semantics that the SQL spec demands when the sub-query produces any null values. There's been some interest in detecting cases where we can prove that the subquery produces no nulls and then optimizing it into NOT EXISTS, but it seems like a lot of work for not-great return, so nothing's happened (yet). Perhaps Oracle does something like that already, or perhaps they're just ignoring the semantics problem; they do not have a reputation for hewing closely to the spec on behavior regarding nulls. regards, tom lane