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 1vsiHf-00G2IY-17 for pgsql-hackers@arkaria.postgresql.org; Wed, 18 Feb 2026 14:12:07 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vsiHc-00G7jw-0y for pgsql-hackers@arkaria.postgresql.org; Wed, 18 Feb 2026 14:12:04 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vsiHc-00G7jo-00 for pgsql-hackers@lists.postgresql.org; Wed, 18 Feb 2026 14:12:04 +0000 Received: from forward101d.mail.yandex.net ([2a02:6b8:c41:1300:1:45:d181:d101]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1vsiHY-00000001DzG-1KsC for pgsql-hackers@lists.postgresql.org; Wed, 18 Feb 2026 14:12:03 +0000 Received: from mail-nwsmtp-smtp-production-main-95.klg.yp-c.yandex.net (mail-nwsmtp-smtp-production-main-95.klg.yp-c.yandex.net [IPv6:2a02:6b8:c42:2a21:0:640:9c41:0]) by forward101d.mail.yandex.net (Yandex) with ESMTPS id DEC92C0055 for ; Wed, 18 Feb 2026 17:11:56 +0300 (MSK) Received: by mail-nwsmtp-smtp-production-main-95.klg.yp-c.yandex.net (smtp/Yandex) with ESMTPSA id uBbLe2MGxa60-mD5NqudY; Wed, 18 Feb 2026 17:11:56 +0300 X-Yandex-Fwd: 1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tantorlabs.com; s=mail; t=1771423916; bh=xljsNnOd64W68SPxsIb7Gdndtv1H5S7EC5CpSKJnOro=; h=Subject:To:Message-ID:Date:From; b=aZqmfPPTzldApEdyHjxj4TARo6I/CROX2nPu0q0l5w5Q4srnoFk6GG7vBV+cJzr3B lVYzL4Ug0eV+HWxbU0rjMXu7vAmO2jE4cs3Av7lzFbDnGjY660vhBlcsauxgnqRAi5 +tBVg5bkJVJLorvDZXQgoZ46sLZ/nMtBOhMhW8kQ= Authentication-Results: mail-nwsmtp-smtp-production-main-95.klg.yp-c.yandex.net; dkim=pass header.i=@tantorlabs.com Content-Type: multipart/mixed; boundary="------------ZOD4mPzUBE9egabd8NrhumG7" Message-ID: Date: Wed, 18 Feb 2026 17:11:56 +0300 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Content-Language: en-US To: PostgreSQL Developers From: Ilia Evdokimov Subject: Reduce planning time for large NOT IN lists containing NULL List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is a multi-part message in MIME format. --------------ZOD4mPzUBE9egabd8NrhumG7 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit 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. 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. 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. Thoughts? [0]: https://www.postgresql.org/message-id/CAHza6qdAH99C0xa27YDTixiNVFa99j90QYquUPAxL0JwahmggA%40mail.gmail.com -- Best regards. Ilia Evdokimov, Tantor Labs LLC, https://tantorlabs.com/ --------------ZOD4mPzUBE9egabd8NrhumG7 Content-Type: text/x-patch; charset=UTF-8; name="v1-0001-Reduce-planning-time-for-large-NOT-IN-lists-conta.patch" Content-Disposition: attachment; filename*0="v1-0001-Reduce-planning-time-for-large-NOT-IN-lists-conta.pa"; filename*1="tch" Content-Transfer-Encoding: base64 RnJvbSA4NjkxNGY0NGEzN2IzYzlhZmJkMThiMzMzZWI1YzQ1MWY0MWNlNjViIE1vbiBTZXAg MTcgMDA6MDA6MDAgMjAwMQpGcm9tOiBJbGlhIEV2ZG9raW1vdiA8aWx5YS5ldmRva2ltb3ZA dGFudG9ybGFicy5ydT4KRGF0ZTogV2VkLCAxOCBGZWIgMjAyNiAxNzowNjozNSArMDMwMApT dWJqZWN0OiBbUEFUQ0ggdjFdIFJlZHVjZSBwbGFubmluZyB0aW1lIGZvciBsYXJnZSBOT1Qg SU4gbGlzdHMgY29udGFpbmluZwogTlVMTAoKRm9yIHggPD4gQUxMICguLi4pIC8geCBOT1Qg SU4gKC4uLiksIHRoZSBwcmVzZW5jZSBvZiBhIE5VTEwgZWxlbWVudAptYWtlcyB0aGUgc2Vs ZWN0aXZpdHkgMC4wLgoKVGhlIHBsYW5uZXIgY3VycmVudGx5IHN0aWxsIGl0ZXJhdGVzIG92 ZXIgYWxsIGVsZW1lbnRzIGFuZCBjb21wdXRlcwpwZXItZWxlbWVudCBzZWxlY3Rpdml0eSwg ZXZlbiB0aG91Z2ggdGhlIGZpbmFsIHJlc3VsdCBpcyBrbm93bi4KCkFkZCBhbiBlYXJseSBO VUxMIGNoZWNrIGZvciBjb25zdGFudCBhcnJheXMgYW5kIGltbWVkaWF0ZWx5IHJldHVybgow LjAgdW5kZXIgQUxMIHNlbWFudGljcy4KClRoaXMgcmVkdWNlcyBwbGFubmluZyB0aW1lIGZv ciBsYXJnZSBOT1QgSU4gLyA8PiBBTEwgbGlzdHMg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