Re: Incorrect selectivity estimates for NOT on nullable boolean columns

From: Ilia Evdokimov <ilya(dot)evdokimov(at)tantorlabs(dot)com>
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
Date: 2026-11-23 15:00:46
Message-ID: 4c684f39-9868-47a2-9173-ad450e5f3fb2@tantorlabs.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

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/

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Ilia Evdokimov 2026-11-23 15:20:23 Re: Show 'Rows Removed by Filter' with two fractional digits in EXPLAIN
Previous Message Matheus Alcantara 2026-11-23 14:52:53 Re: Is there value in having optimizer stats for joins/foreignkeys?