Re: Incorrect selectivity estimates for NOT on nullable boolean columns

From: ZizhuanLiu X-MAN <44973863(at)qq(dot)com>
To: Ilia Evdokimov <ilya(dot)evdokimov(at)tantorlabs(dot)com>
Cc: solai v <solai(dot)cdac(at)gmail(dot)com>, Pierre Forstmann <pierre(dot)forstmann(at)gmail(dot)com>, PostgreSQL Developers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: Incorrect selectivity estimates for NOT on nullable boolean columns
Date: 2026-11-24 02:08:28
Message-ID: tencent_63E2E01BC0D284DAA051115EA06D84D66F05@qq.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Original
>From: Ilia Evdokimov <ilya(dot)evdokimov(at)tantorlabs(dot)com>
>Date: 2026-11-23 23:00
>To: ZizhuanLiu X-MAN <44973863(at)qq(dot)com>, PostgreSQL Developers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
>Cc: solai v <solai(dot)cdac(at)gmail(dot)com>, Pierre Forstmann <pierre(dot)forstmann(at)gmail(dot)com>
>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) 
44973863(at)qq(dot)com

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message shihao zhong 2026-11-24 02:13:59 Re: Fix two race conditions in processing interrupts
Previous Message shihao zhong 2026-11-24 02:07:29 Re: Prevent foreign key references to unlogged partitions