postgresql/src
Tom Lane d8e6b84bd2 Avoid regressions in foreign-key-based selectivity estimates.
David Rowley found that the "use the smallest per-column selectivity"
heuristic applied in some cases by get_foreign_key_join_selectivity()
was badly off if the FK columns are independent, producing estimates
much worse than we got before that code was added in 9.6.

One case where that heuristic was used was for LEFT and FULL outer joins
with the referenced rel on the outside of the join.  But we should not
really need to special-case those here.  eqjoinsel() never has had such a
special case; the correction is applied by calc_joinrel_size_estimate()
instead.  Let's just estimate such cases like inner joins and rely on that
later adjustment.  (I think there was something of a thinko here, in that
the comments seem to be thinking about the selectivity as defined for
semi/anti joins; but that shouldn't apply to left/full joins.)  Add a
regression test exercising such a case to show that this is sane in
at least some cases.

The other case where we used that heuristic was for SEMI/ANTI outer joins,
either if the referenced rel was on the outside, or if it was on the inside
but was part of a join within the RHS.  In either case, the FK doesn't give
us a lot of traction towards estimating the selectivity.  To ensure that
we don't have regressions from what happened before 9.6, let's punt by
ignoring the FK in such cases and applying the traditional selectivity
calculation.  (We might be able to improve on that later, but for now
I just want to be sure it's not worse than 9.5.)

Report and patch by David Rowley, simplified a bit by me.  Back-patch
to 9.6 where this code was added.

Discussion: https://postgr.es/m/CAKJS1f8NO8oCDcxrteohG6O72uU1saEVT9qX=R8pENr5QWerXw@mail.gmail.com
2017-06-19 15:33:41 -04:00
..
backend Avoid regressions in foreign-key-based selectivity estimates. 2017-06-19 15:33:41 -04:00
bin On Windows, make pg_dump use binary mode for compressed plain text output. 2017-06-19 11:02:45 -04:00
common Post-PG 10 beta1 pgperltidy run 2017-05-17 19:01:23 -04:00
fe_utils
include Fix leaking of small spilled subtransactions during logical decoding. 2017-06-18 19:12:56 -07:00
interfaces Fix typo in code comment 2017-06-15 09:45:13 -04:00
makefiles Always use -fPIC, not -fpic, when building shared libraries with gcc. 2017-06-01 13:32:55 -04:00
pl Teach PL/pgSQL about partitioned tables. 2017-06-14 09:00:01 +01:00
port Post-PG 10 beta1 pgindent run 2017-05-17 16:31:56 -04:00
template
test Avoid regressions in foreign-key-based selectivity estimates. 2017-06-19 15:33:41 -04:00
timezone Guard against null t->tm_zone in strftime.c. 2017-05-07 12:33:12 -04:00
tools Teach pgindent to skip files generated by bison or flex automatically. 2017-06-16 23:14:40 -04:00
tutorial
.gitignore
DEVELOPERS
Makefile
Makefile.global.in Add ICU_CFLAGS to global CPPFLAGS 2017-06-12 15:57:22 -04:00
Makefile.shlib Remove support for bcc and msvc standalone libpq builds 2017-04-11 15:22:21 +02:00
nls-global.mk