# Re: [HACKERS] WIP patch: distinguish selectivity of < from <= and > from >=

```On Tue, Jul 4, 2017 at 9:23 AM, Tom Lane <t...@sss.pgh.pa.us> wrote:

> Aside from the mind-bendingly-tedious changes in pg_operator.h, the meat
> of the patch is in selfuncs.c's ineq_histogram_selectivity(), which now
> applies a correction for equal values in the cases where we were getting
> it wrong before.  While this logic seems experimentally correct (see
> above), I have to admit that I've failed to wrap my brain around exactly
> why it's correct.  The arguments that I've constructed so far seem to
> point in the direction of applying the opposite correction, which is
> demonstrably wrong.  Perhaps someone whose college statistics class
> wasn't quite so long ago can explain this satisfactorily?
>
I guess that you're referring the last case, i.e.
explain analyze select * from tenk1 where thousand between 10 and 10;```
```
IMHO, following are the things that I've understood.
The predicate internally got translated to predicate A (p >= 10) and
predicate B (p <=10);
In ineq_histogram_selectivity,
For predicate A, hist_selec = p
For predicate B, hist_selec = 1-p
In clauselist_selectivity,
we calculate the selectivity as = ((p) + (1 - p)) - 1= 0, rounded of to 1.0e-10.

In ineq_histogram_selectivity,
For predicate A, hist_selec = p + correction (since, isgt=iseq)
For predicate B, hist_selec = 1-p
In clauselist_selectivity,
we calculate the selectivity as = ((p + correction) + (1 - p)) - 1= correction,

The correction is calculated as = 1 / num_distinct_values = .001.

Since, the thousand column of tenk1 is uniformly distributed, this
turns out to be the exact selectivity. (rows = .001 * 1000 = 10)

Thoughts?

--
Thanks & Regards,
Kuntal Ghosh
EnterpriseDB: http://www.enterprisedb.com

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers
```