BUG #17545: Incorrect selectivity for IS NOT DISTINCT FROM and NULLs
The following bug has been logged on the website:
Bug reference: 17545
Logged by: Roope Salmi
Email address: rpsalmi@gmail.com
PostgreSQL version: 14.4
Operating system: Ubuntu 22.04
Description:
Hi,
It appears that selectivity for "IS NOT DISTINCT FROM" clauses regard the
proportion of NULLs in the same way as "=", leading to polar opposite row
count
estimates.
In practice this causes nested loop joins to be used when that is not
appropriate. Not sure if this is a known issue, but I was unable to find
any
previous mentions of it.
Here I create a table with one column and 1000 rows of NULL. A cartesian
product
with "WHERE x.a = y.a" correctly estimates that there are around zero
matching
rows. "x.a IS NOT DISTINCT FROM y.a" incorrectly gives the same estimate,
whereas "x.a = y.a OR (x.a IS NULL AND y.a IS NULL)", which should be
equivalent, gives the correct 1000000.
postgres=# CREATE TABLE test(a INTEGER);
CREATE TABLE
postgres=# INSERT INTO test(a) SELECT NULL FROM generate_series(1, 1000);
INSERT 0 1000
postgres=# ANALYZE test;
ANALYZE
postgres=# EXPLAIN SELECT FROM test x, test y WHERE x.a = y.a;
QUERY PLAN
----------------------------------------------------------------------
Merge Join (cost=127.66..137.67 rows=1 width=0)
Merge Cond: (x.a = y.a)
-> Sort (cost=63.83..66.33 rows=1000 width=4)
Sort Key: x.a
-> Seq Scan on test x (cost=0.00..14.00 rows=1000 width=4)
-> Sort (cost=63.83..66.33 rows=1000 width=4)
Sort Key: y.a
-> Seq Scan on test y (cost=0.00..14.00 rows=1000 width=4)
(8 rows)
postgres=# EXPLAIN SELECT FROM test x, test y
postgres=# WHERE x.a IS NOT DISTINCT FROM y.a;
QUERY PLAN
----------------------------------------------------------------------
Nested Loop (cost=0.00..15030.50 rows=1 width=0)
Join Filter: (NOT (x.a IS DISTINCT FROM y.a))
-> Seq Scan on test x (cost=0.00..14.00 rows=1000 width=4)
-> Materialize (cost=0.00..19.00 rows=1000 width=4)
-> Seq Scan on test y (cost=0.00..14.00 rows=1000 width=4)
(5 rows)
postgres=# EXPLAIN SELECT FROM test x, test y
postgres-# WHERE x.a = y.a OR (x.a IS NULL AND y.a IS NULL);
QUERY PLAN
----------------------------------------------------------------------
Nested Loop (cost=0.00..15030.50 rows=1000000 width=0)
Join Filter: ((x.a = y.a) OR ((x.a IS NULL) AND (y.a IS NULL)))
-> Seq Scan on test x (cost=0.00..14.00 rows=1000 width=4)
-> Materialize (cost=0.00..19.00 rows=1000 width=4)
-> Seq Scan on test y (cost=0.00..14.00 rows=1000 width=4)
(5 rows)
On Mon, Jul 11, 2022 at 10:36 PM PG Bug reporting form <
noreply@postgresql.org> wrote:
Hi,
It appears that selectivity for "IS NOT DISTINCT FROM" clauses regard the
proportion of NULLs in the same way as "=", leading to polar opposite row
count
estimates.In practice this causes nested loop joins to be used when that is not
appropriate. Not sure if this is a known issue, but I was unable to find
any
previous mentions of it.
This can also happen to 'IS DISTINCT FROM'.
# EXPLAIN SELECT FROM test x, test y WHERE x.a IS DISTINCT FROM y.a;
QUERY PLAN
----------------------------------------------------------------------
Nested Loop (cost=0.00..15030.50 rows=1000000 width=0)
Join Filter: (x.a IS DISTINCT FROM y.a)
-> Seq Scan on test x (cost=0.00..14.00 rows=1000 width=4)
-> Materialize (cost=0.00..19.00 rows=1000 width=4)
-> Seq Scan on test y (cost=0.00..14.00 rows=1000 width=4)
(5 rows)
DistinctExpr has the same representation as OpExpr with operator "=". So
we estimate its selectivity just as if it is 'x.a = y.a', and then
invert the result.
For OpExpr 'x.a = y.a', because no MCV lists are available, we estimate
its selectivity as MIN(1/nd1,1/nd2)*(1-nullfrac1)*(1-nullfrac2). Note
that both nullfrac1 and nullfrac2 are 100% in the case, so the
selectivity is calculated as zero, and the inverted result is 1, which
is for DistinctExpr.
The comment in the codes has explained this behavior, as it says:
/*
* DistinctExpr has the same representation as OpExpr, but the
* contained operator is "=" not "<>", so we must negate the result.
* This estimation method doesn't give the right behavior for nulls,
* but it's better than doing nothing.
*/
if (IsA(clause, DistinctExpr))
s1 = 1.0 - s1;
So maybe this is a known issue?
Thanks
Richard
Richard Guo <guofenglinux@gmail.com> writes:
The comment in the codes has explained this behavior, as it says:
* This estimation method doesn't give the right behavior for nulls,
* but it's better than doing nothing.
So maybe this is a known issue?
It's a nobodys-bothered-to-make-it-better issue. In general, PG's
treatment of IS [NOT] DISTINCT FROM is pretty lame --- for example,
you might wish that IS NOT DISTINCT FROM could use an index in
the way that "=" can, but that's never been made to happen.
regards, tom lane
Hi,
Here is a patch for the estimate. IS NOT DISTINCT FROM is estimated
like "=", so row pairs with NULL on both sides are never counted. The
patch adds the product of the two null fractions to the "=" estimate.
Two tables of 2000 rows, 30% NULL, joined on
x.a IS NOT DISTINCT FROM y.a:
master rows=1400 actual rows=361400
patched rows=361400 actual rows=361400
Thanks,
Shihao
Attachments:
t71387_4v1-0001-Count-NULL-pairs-in-IS-NOT-DISTINCT-FROM-selectiv.patchapplication/octet-stream; name=v1-0001-Count-NULL-pairs-in-IS-NOT-DISTINCT-FROM-selectiv.patchDownload+35-3
v1-0002-Add-row-estimate-tests-for-IS-NOT-DISTINCT-FROM.patchapplication/octet-stream; name=v1-0002-Add-row-estimate-tests-for-IS-NOT-DISTINCT-FROM.patchDownload+61-1
Hi Shihao,
Thanks for picking this up. I tested v1 on master (a464d31ff6). It
builds without warnings, the regression tests pass, and no existing
plan changes. Estimated vs actual rows, on two 2000-row tables with
30% NULLs and on the report's 1000 all-NULL rows:
master v1 actual
JOIN ON a IS NOT DISTINCT FROM 28000 388000 388000
JOIN ON a IS DISTINCT FROM 3972000 3612000 3612000
LEFT JOIN, IS NOT DISTINCT FROM 28000 388000 388700
report, all NULLs 1 1000000 1000000
IS DISTINCT FROM, which Richard pointed out has the same problem, is
fixed as well. That may be worth a sentence in the commit message,
which only describes the IS NOT DISTINCT FROM side.
EXISTS and NOT EXISTS are still off, since 0001 leaves semijoins and
antijoins alone:
master v1 actual
EXISTS 700 700 1300
NOT EXISTS 1300 1300 700
I think they're worth handling in the same patch. NOT EXISTS with IS
NOT DISTINCT FROM is a common way to find rows missing from another
table when NULLs should match. It also affects costing:
compute_semi_anti_join_factors() divides the inner join estimate,
which v1 raises, by the semijoin estimate, which it doesn't change.
So the average number of matches per matching outer row goes from 40
on master to 554 with v1, against 298.5 actual.
The attached patch on top of v1 handles them (it's a .txt, so cfbot
stays on v1). For a semijoin or antijoin the "=" estimate is the
fraction of outer rows that have a match, so it adds the fraction of
outer rows whose input is NULL, since they all have a match if any
inner row's input is NULL. It assumes there is one if it expects at
least one, and scales down if it expects fewer. The two cases above
then come out at 1300 and 700, and the average matches at 298.5. It
adds two tests on your tables:
v1 v1+patch actual
EXISTS 70 170 170
NOT EXISTS 130 30 30
It works where the NOT around the DistinctExpr is estimated, not in
the DistinctExpr branch. A plain IS DISTINCT FROM inside EXISTS is
estimated as the complement of the IS NOT DISTINCT FROM estimate, so
changing the DistinctExpr branch would lower that one too, and it is
already too low (1300 against 2000 actual rows). With the patch it
doesn't change.
I also tried the arguments in either order, an extra key column,
all-NULL tables and the inner side cut to 200 rows, and those match
the actual rows too. With the inner side cut to 2 rows it's 388
against 600 (28 with v1). With NULLs on only one side nothing
changes, as expected.
One case neither version gets right is two columns of the same table
whose NULLs are in the same rows: 330 against 2000 actual rows (10 on
master), since the two null fractions are taken as independent. I
don't think that needs handling here.
Regards,
Rahul Yadav
Attachments:
semijoin-null-matches-on-v1.txttext/plain; charset=us-ascii; name=semijoin-null-matches-on-v1.txtDownload+96-3
Hi Rahul,
Thanks for testing, and for the patch. I agree that EXISTS and NOT
EXISTS should be handled too.
v2 is attached, with your patch merged in and you as co-author. The
commit message of 0001 now mentions IS DISTINCT FROM too.
My test get the same numbers as you.
Thanks,
Shihao
Attachments:
t71387_6v2-0001-Count-NULL-pairs-in-IS-NOT-DISTINCT-FROM-selectiv.patchapplication/octet-stream; name=v2-0001-Count-NULL-pairs-in-IS-NOT-DISTINCT-FROM-selectiv.patchDownload+100-4
v2-0002-Add-row-estimate-tests-for-IS-NOT-DISTINCT-FROM.patchapplication/octet-stream; name=v2-0002-Add-row-estimate-tests-for-IS-NOT-DISTINCT-FROM.patchDownload+91-1
Hi Shihao,
Thanks for merging it. v2 gives exactly the same code as v1 plus my
patch, which is what I tested, and it applies cleanly to current
master.
Could you add it to the next commitfest so it doesn't get lost?
Regards,
Rahul Yadav
Import Notes
Reply to msg id not found: CAGRkXqRDQXP-3fv7B8_6E=J+Sq2UZbTUFD4WkNn2wBxCmBm0fw@mail.gmail.com-P3BbeQa----9