| From: | Ilia Evdokimov <ilya(dot)evdokimov(at)tantorlabs(dot)com> |
|---|---|
| To: | 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>, PostgreSQL Developers <pgsql-hackers(at)lists(dot)postgresql(dot)org> |
| Subject: | Re: Incorrect selectivity estimates for NOT on nullable boolean columns |
| Date: | 2026-12-03 13:58:52 |
| Message-ID: | 63c9938e-2e62-4cf3-b39d-1bc2577a7c6a@tantorlabs.com |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-hackers |
While looking into extended statistics, I found a similar issue in
mcv_get_match_bitmap().
For a NOT clause, the function builds a match bitmap for its argument
and then inverts it. However, the bitmap does not distinguish FALSE from
NULL. As a result, for NOT applied to a nullable Boolean column, MCV
items containing NULL are treated as matches.
Example:
```
CREATE TABLE t (a boolean, b integer);
INSERT INTO t
SELECT CASE WHEN i <= 400 THEN false
WHEN i <= 800 THEN NULL
ELSE true END,
CASE WHEN i <= 800 THEN 1 ELSE 2 END
FROM generate_series(1, 1000) AS s(i);
CREATE STATISTICS (mcv) ON a, b FROM t;
ANALYZE t;
-- before v2 patch
EXPLAIN ANALYZE SELECT * FROM t WHERE NOT a AND b = 1;
QUERY PLAN
---------------------------------------------------------------------------------------------------
Seq Scan on t (cost=0.00..17.50 *rows=320* width=5) (actual
time=0.131..0.193 *rows=400.00* loops=1)
Filter: ((NOT a) AND (b = 1))
Rows Removed by Filter: 600
Buffers: shared hit=9 dirtied=1 written=1
Planning:
Buffers: shared hit=12
Planning Time: 0.102 ms
Execution Time: 0.214 ms
(8 rows)
EXPLAIN ANALYZE SELECT * FROM t WHERE NOT a OR b = 1;
QUERY PLAN
---------------------------------------------------------------------------------------------------
Seq Scan on t (cost=0.00..17.50 *rows=880* width=5) (actual
time=0.020..0.109 *rows=800.00* loops=1)
Filter: ((NOT a) OR (b = 1))
Rows Removed by Filter: 200
Buffers: shared hit=5
Planning Time: 0.079 ms
Execution Time: 0.150 ms
(6 rows)
-- after v2 patch
EXPLAIN ANALYZE SELECT * FROM t WHERE NOT a AND b = 1;
QUERY PLAN
---------------------------------------------------------------------------------------------------
Seq Scan on t (cost=0.00..17.50 *rows=400* width=5) (actual
time=0.015..0.093 *rows=400.00* loops=1)
Filter: ((NOT a) AND (b = 1))
Rows Removed by Filter: 600
Buffers: shared hit=5
Planning:
Buffers: shared hit=13
Planning Time: 0.166 ms
Execution Time: 0.121 ms
(8 rows)
EXPLAIN ANALYZE SELECT * FROM t WHERE NOT a OR b = 1;
QUERY PLAN
---------------------------------------------------------------------------------------------------
Seq Scan on t (cost=0.00..17.50 *rows=800* width=5) (actual
time=0.015..0.063 *rows=800.00* loops=1)
Filter: ((NOT a) OR (b = 1))
Rows Removed by Filter: 200
Buffers: shared hit=5
Planning Time: 0.080 ms
Execution Time: 0.096 ms
(6 rows)
```
For the AND case, the MCV entry with a = NULL and b = 1 is incorrectly
counted as matching NOT a. For the OR case, the incorrect match bitmap
also affects the estimated overlap between the conditions.
I have tried handling NOT over a bare Boolean column or expression by
checking the MCV value directly: it matches only when the value is
non-NULL and FALSE.
--
Best regards,
Ilia Evdokimov,
Tantor Labs LLC,
https://tantorlabs.com/
| Attachment | Content-Type | Size |
|---|---|---|
| v2-0001-Fix-NOT-selectivity-estimates-for-nullable-boolea.patch | text/x-patch | 6.3 KB |
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Atsushi Torikoshi | 2026-12-03 14:06:19 | Re: RFC: Logging plan of the running query |
| Previous Message | ZizhuanLiu X-MAN | 2026-12-03 13:54:05 | Re: Optimize MCV stats for sortable types and utilize sorted-order properties |