49
49
* Portions Copyright (c) 1994, Regents of the University of California
50
50
*
51
51
* IDENTIFICATION
52
- * $PostgreSQL: pgsql/src/backend/optimizer/path/costsize.c,v 1.122 2004/01/06 04:31:01 tgl Exp $
52
+ * $PostgreSQL: pgsql/src/backend/optimizer/path/costsize.c,v 1.123 2004/01/19 03:52:28 tgl Exp $
53
53
*
54
54
*-------------------------------------------------------------------------
55
55
*/
@@ -763,9 +763,7 @@ cost_nestloop(NestPath *path, Query *root)
763
763
* an outer tuple as soon as we have one match. Account for the
764
764
* effects of this by scaling down the cost estimates in proportion to
765
765
* the JOIN_IN selectivity. (This assumes that all the quals
766
- * attached to the join are IN quals, which should be true.) This would
767
- * probably be the wrong approach if an input path is a UniquePath, but
768
- * we'd never have that with JOIN_IN join type.
766
+ * attached to the join are IN quals, which should be true.)
769
767
*/
770
768
joininfactor = join_in_selectivity (path , root );
771
769
@@ -1001,9 +999,7 @@ cost_mergejoin(MergePath *path, Query *root)
1001
999
* for an outer tuple as soon as we have one match. Account for the
1002
1000
* effects of this by scaling down the cost estimates in proportion to
1003
1001
* the expected output size. (This assumes that all the quals
1004
- * attached to the join are IN quals, which should be true.) This would
1005
- * probably be the wrong approach if an input path is a UniquePath, but
1006
- * we'd never have that with JOIN_IN join type.
1002
+ * attached to the join are IN quals, which should be true.)
1007
1003
*/
1008
1004
joininfactor = join_in_selectivity (& path -> jpath , root );
1009
1005
@@ -1208,9 +1204,7 @@ cost_hashjoin(HashPath *path, Query *root)
1208
1204
* an outer tuple as soon as we have one match. Account for the
1209
1205
* effects of this by scaling down the cost estimates in proportion to
1210
1206
* the expected output size. (This assumes that all the quals
1211
- * attached to the join are IN quals, which should be true.) This would
1212
- * probably be the wrong approach if an input path is a UniquePath, but
1213
- * we'd never have that with JOIN_IN join type.
1207
+ * attached to the join are IN quals, which should be true.)
1214
1208
*/
1215
1209
joininfactor = join_in_selectivity (& path -> jpath , root );
1216
1210
@@ -1779,12 +1773,31 @@ set_joinrel_size_estimates(Query *root, RelOptInfo *rel,
1779
1773
static Selectivity
1780
1774
join_in_selectivity (JoinPath * path , Query * root )
1781
1775
{
1776
+ RelOptInfo * innerrel ;
1777
+ UniquePath * innerunique ;
1782
1778
Selectivity selec ;
1783
1779
double nrows ;
1784
1780
1781
+ /* Return 1.0 whenever it's not JOIN_IN */
1785
1782
if (path -> jointype != JOIN_IN )
1786
1783
return 1.0 ;
1787
1784
1785
+ /*
1786
+ * Return 1.0 if the inner side is already known unique. The case where
1787
+ * the inner path is already a UniquePath probably cannot happen in
1788
+ * current usage, but check it anyway for completeness. The interesting
1789
+ * case is where we've determined the inner relation itself is unique,
1790
+ * which we can check by looking at the rows estimate for its UniquePath.
1791
+ */
1792
+ if (IsA (path -> innerjoinpath , UniquePath ))
1793
+ return 1.0 ;
1794
+ innerrel = path -> innerjoinpath -> parent ;
1795
+ innerunique = create_unique_path (root ,
1796
+ innerrel ,
1797
+ innerrel -> cheapest_total_path );
1798
+ if (innerunique -> rows >= innerrel -> rows )
1799
+ return 1.0 ;
1800
+
1788
1801
/*
1789
1802
* Compute same result set_joinrel_size_estimates would compute
1790
1803
* for JOIN_INNER. Note that we use the input rels' absolute size
@@ -1796,8 +1809,7 @@ join_in_selectivity(JoinPath *path, Query *root)
1796
1809
path -> joinrestrictinfo ,
1797
1810
0 ,
1798
1811
JOIN_INNER );
1799
- nrows = path -> outerjoinpath -> parent -> rows *
1800
- path -> innerjoinpath -> parent -> rows * selec ;
1812
+ nrows = path -> outerjoinpath -> parent -> rows * innerrel -> rows * selec ;
1801
1813
1802
1814
nrows = clamp_row_est (nrows );
1803
1815
0 commit comments