postgresql/src/backend
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
..
access Fix typos in comments 2017-06-17 10:17:28 +02:00
bootstrap In initdb, defend against assignment of NULL values to not-null columns. 2017-06-13 10:54:43 -04:00
catalog Use DEFACLOBJ_ macros in error message instead of hardcoding 2017-06-14 14:44:24 -04:00
commands Use RangeVarGetRelidExtended() in AlterSequence() 2017-06-16 10:24:50 -04:00
executor Disallow set-returning functions inside CASE or COALESCE. 2017-06-13 23:46:39 -04:00
foreign Abstract logic to allow for multiple kinds of child rels. 2017-04-03 22:41:31 -04:00
lib Post-PG 10 beta1 pgindent run 2017-05-17 16:31:56 -04:00
libpq Re-run pgindent. 2017-06-13 13:05:59 -04:00
main Update copyright via script for 2017 2017-01-03 13:48:53 -05:00
nodes Reconcile nodes/*funcs.c with PostgreSQL 10 work. 2017-06-16 00:16:11 -07:00
optimizer Avoid regressions in foreign-key-based selectivity estimates. 2017-06-19 15:33:41 -04:00
parser Disallow set-returning functions inside CASE or COALESCE. 2017-06-13 23:46:39 -04:00
po Translation updates 2017-05-15 12:19:54 -04:00
port Post-PG 10 beta1 pgindent run 2017-05-17 16:31:56 -04:00
postmaster Clean up latch related code. 2017-06-06 16:13:00 -07:00
regex Fix regexport.c to behave sanely with lookaround constraints. 2017-04-13 17:18:35 -04:00
replication Fix leaking of small spilled subtransactions during logical decoding. 2017-06-18 19:12:56 -07:00
rewrite Re-run pgindent. 2017-06-13 13:05:59 -04:00
snowball Add ICU_CFLAGS to global CPPFLAGS 2017-06-12 15:57:22 -04:00
statistics Rename function for consistency 2017-06-15 11:44:33 -04:00
storage Don't force-assign transaction id when exporting a snapshot. 2017-06-14 11:57:21 -07:00
tcop Re-run pgindent. 2017-06-13 13:05:59 -04:00
tsearch Post-PG 10 beta1 pgindent run 2017-05-17 16:31:56 -04:00
utils Fix ICU collation use on Windows 2017-06-16 10:08:54 -04:00
.gitignore Add .gitignore entries for AIX-specific intermediate build artifacts. 2015-07-08 20:44:22 -04:00
common.mk Add ICU_CFLAGS to global CPPFLAGS 2017-06-12 15:57:22 -04:00
Makefile Implement multivariate n-distinct coefficients 2017-03-24 14:06:10 -03:00
nls.mk Translation updates 2017-05-15 12:19:54 -04:00