I was looking at inequality joins and I think I found an opportunity to improve how they are estimated. The planner uses a fixed 1/3 (DEFAULT_INEQ_SEL) for them, which is a sensible default when nothing is known, but these columns often do have MCV lists and histograms, so I think we could use them. For a value v, scalarineqsel() already computes the fraction of the left side satisfying "x < v" for a constant; averaging that over the right side's distribution turns the join into an expectation, using statistics that are already there. It falls back to 1/3 when a side's statistics cannot describe it, and seven tests compare the estimate with the actual row count.
I would appreciate feedback on the fallbacks and on the planning cost of the extra lookups. The code is here for now https://github.com/lrweck/postgres/pull/5 @Tomas Vondra <[email protected]>, if you think this approach has any ground, I'd be more than happy to continue pursuing it! I know I need to format it as a patch and send it to the hackers mailing list. *Regards,* *--* *Luís R. Weck*
