Original
>From: Ilia Evdokimov <[email protected]>
>Date: 2026-11-23 23:00
>To: ZizhuanLiu X-MAN <[email protected]>, PostgreSQL Developers 
><[email protected]>
>Cc: solai v <[email protected]>, Pierre Forstmann 
><[email protected]>
>Subject: Re: Incorrect selectivity estimates for NOT on nullable boolean 
>columns
>On 11/11/26 14:37, ZizhuanLiu X-MAN wrote:
>
>> The main differences are where the responsibility for handling this logic 
>> resides:
>> - [2] keeps the handling within the current clause_selectivity_ext() 
>> function by constructing a BooleanTest node.
>>
>> - [1] delegates the handling to booltestsel(IS_FALSE) in selfuncs.c.
>>
>> - [3] keeps the handling within the current if branch and reimplements the 
>> relevant parts of booltestsel() locally.
>>     This avoids the additional call chain booltestsel() -> 
>> clause_selectivity() -> clause_selectivity_ext() when
>>      statistics are unavailable, while retaining the same logic.
>>
>> Personally, I prefer approach [1]. I think centralizing the semantics in 
>> booltestsel() could help keep the handling consistent,
>> reduce maintenance overhead, and make future extensions to the function 
>> easier to manage.
>>
>> Please let me know if I've overlooked anything or if you see any issues with 
>> this reasoning.
>>
>> What do you think?
>
>I don't think approaches [1] and [2] are equivalent to [3] in all cases.
>
>The difference is in the fallback path when no statistics tuple is 
>available for the argument of NOT. In [3], the recursive call preserves 
>the caller's `use_extended_stats` value:
>
>```
>s1 = 1.0 - clause_selectivity_ext(..., use_extended_stats);
>```
>
>In [1], `booltestsel()` instead calls `clause_selectivity()`, which 
>calls `clause_selectivity_ext()` with use_extended_stats = true:
>
>```
>booltestsel(IS_FALSE, arg) -> clause_selectivity(arg, ...) -> 
>clause_selectivity_ext(arg, ..., true)
>```
>
>Therefore, if the original call had use_extended_stats = false, [1] can 
>enable extended statistics when estimating the argument. Approach [2] 
>has the same issue.
>
>For that reason, I think v1 patch is needed to preserve the existing 
>behavior.
>
>--
>Best regards,
>Ilia Evdokimov,
>Tantor Labs LLC,
>https://tantorlabs.com/


Hi Ilia,

Sorry, I overlooked that clause_selectivity_ext() is not a static function
and can also be called by functions other than clause_selectivity(). 
Therefore, use_extended_stats is not necessarily true.

Your analysis is thorough. Thank you for pointing this out. 
I really appreciate your clarification.


regards,
--
ZizhuanLiu (X-MAN) 
[email protected]

Reply via email to