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 1vt678-002Yzd-0T for pgsql-hackers@arkaria.postgresql.org; Thu, 19 Feb 2026 15:38:50 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vt677-0042Wg-0S for pgsql-hackers@arkaria.postgresql.org; Thu, 19 Feb 2026 15:38:49 +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 1vt676-0042WX-2n for pgsql-hackers@lists.postgresql.org; Thu, 19 Feb 2026 15:38:48 +0000 Received: from mail-oa1-x34.google.com ([2001:4860:4864:20::34]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1vt673-000000009ry-4BEi for pgsql-hackers@lists.postgresql.org; Thu, 19 Feb 2026 15:38:48 +0000 Received: by mail-oa1-x34.google.com with SMTP id 586e51a60fabf-40ee9b945d5so629458fac.0 for ; Thu, 19 Feb 2026 07:38:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1771515525; x=1772120325; darn=lists.postgresql.org; h=content-transfer-encoding:in-reply-to:from:content-language :references:to:subject:user-agent:mime-version:date:message-id:from :to:cc:subject:date:message-id:reply-to; bh=fHuf2ulWygPIK1R2lYcr2OddV6XOAuUGZ+1nUIq1taY=; b=TUKVScmodxvlhhg1Gy80pn8sZNFmFb/JRka8xKIpb8Xb34mdH8K03qzYAZPQvtoFmn uaGy5qrPdCCKWE7Dt7KhJTnpX94WsvN18AqXLZtGmd2sj5JX6KjLk49hzltjNs22tWb0 Zr1z8TCerlET2Ea0JDs+DNZ4oZ+srR5KO0P2zurHPK26ol0+EbXWVng3VESoevg/OnCH lLcriSMIkLBdVVKFjIdTC5APNB48HKFvXAjF9IAd3O114ppq0PXMFd/91kZZ/M3B4crY JG138gWN5+eqeIrwt3G7CcnMfBetONQVoZv3c6Yehx9RplBUbVz4yyxBkJUK5ODO/7eX WHCg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1771515525; x=1772120325; h=content-transfer-encoding:in-reply-to:from:content-language :references:to:subject:user-agent:mime-version:date:message-id :x-gm-gg:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=fHuf2ulWygPIK1R2lYcr2OddV6XOAuUGZ+1nUIq1taY=; b=jaFjVPV2ihlBLaoYD1KRF3CUUMl+6tVphCSug4pnIHr7sw/yZxQFi/5osivComZ2Pi SajRNWHdkbZpJYkKCQmH8t0Wnf9LSxdAlFv8WzpA5TEUlMagma8lZkTmMFd41gu5L4dH uiO+0ywRjsw1fc1P4p9kjg14bDfjHqpwJZnAgRo/hYL4JHwXDUA4ttvRVkJ8K+ZkFIIW QTyjn5QrpqwtT/6QJFXrMMTB7c8zjBpdN1egm59CU30AEL19DfVcmjbbng3j2uAJdw/3 h//znFRxVkHYjWjAfXVZXYj2Os75qp1n4tsM2IhlVrquMDeX3P9rZQ75eXJL3Wm864Az 1Nqw== X-Forwarded-Encrypted: i=1; AJvYcCW71Sp9T1C8E2ST6FIRFigdx7s6PM5BSu18UIgT9hwbV2EDzugmV3yMipnlaj3DVgrONISZ1q/L0MCOCCov@lists.postgresql.org X-Gm-Message-State: AOJu0Yz/coWytFSjbNXGRqhAAzGWYyYDtOjG6I0RN24uge4b4lI1zqst mpULury2g4aGE06gA7PGDd0GbD0cyZq3mUIWh/nLRTgOs91sDSfmAL1VgVjn+A== X-Gm-Gg: AZuq6aL/UJKrVKAkggAqlvV8NPzOOzHor8bADMVKvJy8Btyzr55i6ckgB7Eh2DuaHBm soakukKj8YoaptKHzgRVkUdbqAumhpjnXe7Gva0ngWrv0osjhSF5KmHNzeYJWtZcD6EVVF7drbu 7RZu+INNLiISUT2T0KtzGcogpmxEE3agpshjh9iMlNfE5pXzxMIt9+FGUX+apd0eElI5jY0ZYHi m6rOc+V7Z7dbCnmyWDkBACXuZhJf4IG4FxwlqomcgoLJnQ9pStnqlCV34xnkP3DZhLrlErk3esI ZrV3uu34Hj1j691o5K9minPrjbQndS1IrV/0eGGH73l7IhkxV8/BBxH3IhYk98YBlbLkPM/g2qg ejBC0AttJbeK7LQDQ+CuAKVX3PnBaQFlH+P9FNPLIjhPNhrBFQyudAnUOIroXvKqqBtYcXSWTin fcd+rEg241gwnu3nWi2H5DeT7H+nvRj8MPxEemHRVqzspzdEi2Y2vir6SLAxAMWOfg/LPrbPHDg tF/xQ== X-Received: by 2002:a05:6870:213:b0:3ff:4ab3:941c with SMTP id 586e51a60fabf-41529186447mr3847067fac.40.1771515524580; Thu, 19 Feb 2026 07:38:44 -0800 (PST) Received: from [172.31.5.233] (vip-148-139-2-229.cust.service-now.com. [148.139.2.229]) by smtp.gmail.com with ESMTPSA id 586e51a60fabf-40ef89944e2sm19459505fac.2.2026.02.19.07.38.43 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 19 Feb 2026 07:38:44 -0800 (PST) Message-ID: <52596e08-0902-475b-ad0f-265ec6918f19@gmail.com> Date: Thu, 19 Feb 2026 16:38:41 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Reduce planning time for large NOT IN lists containing NULL To: Ilia Evdokimov , PostgreSQL Developers References: Content-Language: en-US From: David Geier In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hi Ilia! On 18.02.2026 15:11, Ilia Evdokimov wrote: > Hi hackers, > > In this thread [0] an interesting idea came up about avoiding > unnecessary work during selectivity estimation for x <> ALL (NULL, ...) > or x NOT IN (NULL, ...) > > Semantically, if the array contains at least one NULL, the selectivity > of x NOT IN (...) is always 0.0, regardless of the other elements in the > list. > > Currently, the planner still iterates over all array elements and > invokes the operator's selectivity estimator for each of them. For large > IN / ALL lists, this increases planning time. +1 on the general idea. > For constant arrays I propose adding a simple pre-check before entering > the per-element loop: detect whether the array contains at least one > NULL element (e.g., via memchr() for the deconstructed array case). If > so, and we are in the ALL / NOT IN case, we can immediately return > selectivity = 0.0 and skip all further computation. This would avoid > extra per-element estimation work while preserving semantics. How much overhead does the memchr() call add? It seems like this approach optimizes the edge case at the expense of the common case, which doesn't seem like a good trade-off. How about instead adding a flag to ArrayType which indicates if the array contains NULL or not. This flag could be set in construct_md_array() which already iterates over all elements. The flag would need to be kept up-to-date, e.g. in array_set_element() and possibly other functions modifying the array. > In cases where array elements are not known to be constants in advance, > such a pre-check is less straightforward, because each element must > first be inspected to determine whether it is a Const and whether it is > NULL. That already requires iterating through the list, so introducing a > separate early pass would not actually reduce the amount of work. > Therefore, it like makes sense to keep the current behavior in that > situation. Agreed. -- David Geier