postgresql/src/include/nodes
Tom Lane ad1c36b070 Fix foreign-key selectivity estimation in the presence of constants.
get_foreign_key_join_selectivity() looks for join clauses that equate
the two sides of the FK constraint.  However, if we have a query like
"WHERE fktab.a = pktab.a and fktab.a = 1", it won't find any such join
clause, because equivclass.c replaces the given clauses with "fktab.a
= 1 and pktab.a = 1", which can be enforced at the scan level, leaving
nothing to be done for column "a" at the join level.

We can fix that expectation without much trouble, but then a new problem
arises: applying the foreign-key-based selectivity rule produces a
rowcount underestimate, because we're effectively double-counting the
selectivity of the "fktab.a = 1" clause.  So we have to cancel that
selectivity out of the estimate.

To fix, refactor process_implied_equality() so that it can pass back the
new RestrictInfo to its callers in equivclass.c, allowing the generated
"fktab.a = 1" clause to be saved in the EquivalenceClass's ec_derives
list.  Then it's not much trouble to dig out the relevant RestrictInfo
when we need to adjust an FK selectivity estimate.  (While at it, we
can also remove the expensive use of initialize_mergeclause_eclasses()
to set up the new RestrictInfo's left_ec and right_ec pointers.
The equivclass.c code can set those basically for free.)

This seems like clearly a bug fix, but I'm hesitant to back-patch it,
first because there's some API/ABI risk for extensions and second because
we're usually loath to destabilize plan choices in stable branches.

Per report from Sigrid Ehrenreich.

Discussion: https://postgr.es/m/1019549.1603770457@sss.pgh.pa.us
Discussion: https://postgr.es/m/AM6PR02MB5287A0ADD936C1FA80973E72AB190@AM6PR02MB5287.eurprd02.prod.outlook.com
2020-10-28 11:15:47 -04:00
..
bitmapset.h Move bitmap_hash and bitmap_match to bitmapset.c. 2020-02-24 17:17:43 +05:30
execnodes.h Remove PartitionRoutingInfo struct. 2020-10-19 14:42:55 +03:00
extensible.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
lockoptions.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
makefuncs.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
memnodes.h Revert "Specialize MemoryContextMemAllocated()." 2020-03-19 12:21:50 -07:00
nodeFuncs.h Suppress unnecessary RelabelType nodes in yet more cases. 2020-08-19 14:07:49 -04:00
nodes.h Move resolution of AlternativeSubPlan choices to the planner. 2020-09-27 12:51:28 -04:00
params.h Initial pgindent and pgperltidy run for v13. 2020-05-14 13:06:50 -04:00
parsenodes.h Allow CURRENT_ROLE where CURRENT_USER is accepted 2020-09-17 11:40:08 +02:00
pathnodes.h Fix foreign-key selectivity estimation in the presence of constants. 2020-10-28 11:15:47 -04:00
pg_list.h Add for_each_from, to simplify loops starting from non-first list cells. 2020-09-28 20:33:13 -04:00
plannodes.h Include result relation info in direct modify ForeignScan nodes. 2020-10-14 10:58:38 +03:00
primnodes.h Move resolution of AlternativeSubPlan choices to the planner. 2020-09-27 12:51:28 -04:00
print.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
readfuncs.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
replnodes.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
supportnodes.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
tidbitmap.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00
value.h Update copyrights for 2020 2020-01-01 12:21:45 -05:00