Re: Incorrect selectivity estimates for NOT on nullable boolean columns

From: Ilia Evdokimov <ilya(dot)evdokimov(at)tantorlabs(dot)com>
To: Zsolt Parragi <zsolt(dot)parragi(at)percona(dot)com>
Cc: pgsql-hackers(at)lists(dot)postgresql(dot)org, ZizhuanLiu X-MAN <44973863(at)qq(dot)com>, 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-12-04 09:34:41
Message-ID: 7d61a73e-3a8b-473d-983e-ac048fa6af9c@tantorlabs.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi Zsolt,

Thank you for testing and for the reproductions.

On 12/4/26 01:36, Zsolt Parragi wrote:
> 1. DISTINCT with 100% true 50% not true
>
> CREATE TABLE t AS SELECT true AS a, i AS b FROM
> generate_series(1,10000) AS g(i);
> ANALYZE t;
> EXPLAIN ANALYZE WITH s AS MATERIALIZED (SELECT DISTINCT a,b FROM t)
> SELECT * FROM s WHERE NOT a;
> -- patched: CTE Scan on s ... rows=5000 ... actual rows=0.00
> -- master: CTE Scan on s ... rows=1 ... actual rows=0.00

In this case, the statistics for the CTE output do not contain MCV
frequencies: DISTINCT can change the frequency distribution, so those
frequencies are discarded. Without them, booltestsel(IS_FALSE) assumes
an even split between TRUE and FALSE.

I changed the NOT estimate to use boolvarsel() for the TRUE fraction and
subtract both that estimate and the NULL fraction from 1. This allows
the estimate to use the number of distinct values when MCV frequencies
are unavailable.

With this change, the estimate for your example is back to 1 row, as on
master. This addresses the reported regression, although it does not
generally solve estimation after DISTINCT when the values themselves are
no longer available in the statistics.

> 2. NOT inside BooleanTest (IS FALSE / IS NOT FALSE) regresses on
> nullable booleans
>
> CREATE TABLE t (id int, deleted boolean) WITH (autovacuum_enabled = off);
> INSERT INTO t
> SELECT i, CASE WHEN i <= 9500 THEN NULL WHEN i <= 9900 THEN true ELSE false END
> FROM generate_series(1, 10000) AS g(i);
> ANALYZE t;
> CREATE VIEW v AS SELECT id, NOT deleted AS active FROM t;
>
> EXPLAIN ANALYZE SELECT * FROM v WHERE active IS FALSE;
> -- patched: rows=9900 (actual rows=400.00)
> -- master: rows=400 (actual rows=400.00)
>
> EXPLAIN ANALYZE SELECT * FROM v WHERE active IS NOT FALSE;
> -- patched: rows=100 (actual rows=9600.00)
> -- master: rows=9600 (actual rows=9600.00)

The issue here is in the fallback path of booltestsel(). When it has no
statistics for the NOT expression itself, it estimates the test using
the selectivity of that expression or its complement. After the patch,
the selectivity of NOT excludes NULLs, so this no longer gives the
correct result for BooleanTest cases that include NULLs or take the
complement.

I added handling for NOT in that fallback path: the BooleanTest is
converted to the equivalent test on the argument of NOT. For example,
(NOT deleted) IS FALSE is estimated as deleted IS TRUE, and (NOT
deleted) IS NOT FALSE as deleted IS NOT TRUE.

With this change, the estimates for your examples are 400 and 9600 rows,
respectively, matching the actual row counts.

I also added regression tests for both reported cases, the original NOT
on a nullable Boolean column, and all six BooleanTest types over NOT.
The test_setup and stats_ext regression tests pass.

Does this approach look reasonable?

--
Best regards,
Ilia Evdokimov,
Tantor Labs LLC,
https://tantorlabs.com/

Attachment Content-Type Size
v3-0001-Fix-NOT-selectivity-estimates-for-nullable-boolea.patch text/x-patch 11.3 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Daniel Gustafsson 2026-12-04 09:40:27 Re: Credits For v19
Previous Message Koshino Taiki 2026-12-04 09:32:03 Re: [PATCH] psql: Add tab completion for \dO, \dc and \det