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 1vnsYb-00CwAh-00 for pgsql-hackers@arkaria.postgresql.org; Thu, 05 Feb 2026 06:09:37 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vnsYZ-00FWXT-08 for pgsql-hackers@arkaria.postgresql.org; Thu, 05 Feb 2026 06:09:34 +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 1vnsYY-00FWXK-2N for pgsql-hackers@lists.postgresql.org; Thu, 05 Feb 2026 06:09:34 +0000 Received: from mail-oa1-x30.google.com ([2001:4860:4864:20::30]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1vnsYV-000000019A9-3eyQ for pgsql-hackers@lists.postgresql.org; Thu, 05 Feb 2026 06:09:33 +0000 Received: by mail-oa1-x30.google.com with SMTP id 586e51a60fabf-4042cd2a336so440564fac.0 for ; Wed, 04 Feb 2026 22:09:31 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1770271770; cv=none; d=google.com; s=arc-20240605; b=a/VLciSF+tg68uxxASKnz+P5EaUlLogNr4+0yeMpZBcTGTAQi1vBGIub0+eDID3gkT Vh1eVfigBFaegHiESmziK1VaUXhZRy/IKnWCSvMCagpjLyW4IM8PN7RGzf1wHm8tegaC GJrEtBEEl/9U1uWpgHowwabtwZX+X0Q6BKjaFdd8K66M27HnLr6fP05iHld59EpWMwqw 98866wGtut5txwFyEXVi2Nc2Ao6GI6kGen4mO6RZ9IN75kBf4pXpwWuVxmUTKxoYcnH+ 4iAkrwRwXmvwEWJM400vpW7lPIEPcpSUKsr11M8+7KiS+ZT4Ue0iaBDp2qU9mrOUqVp0 54MQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=zTWg0h/aqoYhO3k3RrWyD0ZYbXYBqch3wd1fEpbLVug=; fh=MopyST+JPi7eqQVu6gRjxYOVIKR7XNKfB9ofLKgO66k=; b=H1bWY78xqN4eDSULK0B1QKXSi/pRtDY2ggjrElbw8oXo6rzuuQZJlee5B0oMRzxp3P imWYD863K8ptp4GUTEPDZlUVCRB0yaIFITybYTVepllbcDCDe6+M+P0RDIip7uM6LfOX cbL13FqUipYZ+WyN+vo6BvKq922fwiwG7IigmCG+MQxiMNidcc+DHjEFmtk2xmxttOB1 CgpiJMFqf5irC7601hpbMvssqsnkFMGE7669I98CavljWQe8r9WWnrj6QCN9rQPVhHtY skNHi9wlYmC2UvZFoLDrfkG9QWXpttRCMLhEceEuCZMHLLg660oMraKBCoBMwBJnGV0h dy6A==; darn=lists.postgresql.org ARC-Authentication-Results: i=1; mx.google.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1770271770; x=1770876570; darn=lists.postgresql.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=zTWg0h/aqoYhO3k3RrWyD0ZYbXYBqch3wd1fEpbLVug=; b=cVkn6nqWVwfdLaLj/QC9K53mK9Ir9L4z5p93RDaeT90fbryuZ48kBmcEKom3+NimPS x0A8/QJvtX2EJelAg3xqJssvNnVf+vio+3OLDIbQCmQsycx2YgRNKwMLTg8RgQSxec0t ykJC8Sd/FdPqkiEWO79PH71yCqrQNFsTyOHrzgQGGNLEXggwE6DhqxN5VLQKp8PE465U SB1TavbiremJQOLuzlqdtW515OEYjkWZ8f9lAZxIBAYL93LhUtkT2sQIIOxPdFEd9wgI p8YUchkRnbHQOJ2iDOEZIOG9FXppR0Q9lHv8VSLfMpSjxe0K+vjSBGY6xVJiK+Uifxe2 FALQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1770271770; x=1770876570; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=zTWg0h/aqoYhO3k3RrWyD0ZYbXYBqch3wd1fEpbLVug=; b=FEYu7PJZUHpo3PcPaztT7d2chZ4VT+3Oh1xtzTFUv5VshEvnr4Nw6Po+Qaq0GqEBNG IHwn6DGtjI9fQeytHgZ5gV7J8qaz+Uyw+6AxY6r90dAVbjIy69JAyByutiOxwreCCVG6 0i2uzPwKK5/wKtckOwalFYHtHW4clnVZdajyR6QlIsFbjEIkhdalfgQ4KX21N6j06uOc SiUPr9SiazcbWdrJKZyTY39kLpLCv2XlwCeTnICHa1byDYdufwDHriBVHuOqx6bTtG91 1ob5PPaAmRvqKpb1aMt4CEw89aFg18hLGbpGe9x8rZRdZsCQ0V5Mv2mY2qQdCPMRMiEs qF0g== X-Gm-Message-State: AOJu0Yzx7IGu5QBARNnI+3aRocN0Lv/sH6mB38FNgRYJdepQqwMHxX3J fpCxM269qTCYdP9MYV7rSBtbBKBO08S3Um0oMdDqkL7hNAVX5VQ1AFcyu27YIVc/Ig/gdrkN94a jArsHwHoakVY/lFm9yvFlFyXdAp+83CM= X-Gm-Gg: AZuq6aL/bZIGGfRYTsvW8QtwRNSKZ314UnEIjk9ettqbKrj39V95zXevn+2IanybuB/ 22Iho9waZco9QA+VblKL3/uKgymkEBKoEsv5wQ5DyhIdzPnQCyrU2K7lgP3iM2GYpTySsiFEglr gN/mlREOs7hES2f0Ym4/pUPHJwc+1mTHgEuUsPXbCPE+9PGbyp/2KpQyGBJYRyY0F+Q+8D7r/3C G+wU9aMcC8pQl71f8sxMJDy57xT2b17EisPBp10MnP50N0d/fcv/fp82uuFOxCfGsvXyfuJNg== X-Received: by 2002:a05:6820:151d:b0:663:42d:6264 with SMTP id 006d021491bc7-66a233368aemr2452854eaf.62.1770271769970; Wed, 04 Feb 2026 22:09:29 -0800 (PST) MIME-Version: 1.0 References: <2df5912f-8888-4f27-9384-4c69ba22105c@gmail.com> In-Reply-To: <2df5912f-8888-4f27-9384-4c69ba22105c@gmail.com> From: Richard Guo Date: Thu, 5 Feb 2026 15:09:17 +0900 X-Gm-Features: AZwV_Qi_tRfk26klWr_FkUMCIbeEMov39QKNyyb9FPTTLMaRev3LnijYK5JC29g Message-ID: Subject: Re: Convert NOT IN sublinks to anti-joins when safe To: David Geier Cc: Pg Hackers Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Wed, Feb 4, 2026 at 11:59=E2=80=AFPM David Geier w= rote: > If the sub-select can yield NULLs, the rewrite can be fixed by adding an > OR t2.c1 IS NULL clause, such as: > > SELECT t1.c1 FROM t1 WHERE > NOT EXISTS (SELECT 1 FROM t2 WHERE t1.c1 =3D t2.c1 OR t2.c1 IS NULL) I'm not sure if this rewrite results in a better plan. The OR clause would force a nested loop join, which could be much slower than a hashed-subplan plan. > If the outer expression can yield NULLs, the rewrite can be fixed by > adding a t1.c1 IS NOT NULL clause, such as: > > SELECT t1.c1 FROM T1 WHERE > t1.c1 IS NOT NULL AND > NOT EXISTS (SELECT 1 FROM t2 WHERE t1.c1 =3D t2.c1) This rewrite doesn't seem correct to me. If t2 is empty, you would incorrectly lose the NULL rows from t1 in the final result. > What's our today's take on doing more involved transformations inside > the planner to support such cases? It would greatly open up the scope of > the optimization. As mentioned in my initial email, the goal of this patch is not to handle every possible case, but rather only to handle the basic form where both sides of NOT IN are provably non-nullable. This keeps the code complexity to a minimum, and I believe this would cover the most common use cases in real world. - Richard