@@ -1926,3 +1926,129 @@ select * from x for update;
19261926 Output: subselect_tbl.f1, subselect_tbl.f2, subselect_tbl.f3
19271927(2 rows)
19281928
1929+ -- Pull-up the direct-correlated ANY_SUBLINK
1930+ explain (costs off)
1931+ select * from tenk1 A where hundred in (select hundred from tenk2 B where B.odd = A.odd);
1932+ QUERY PLAN
1933+ ------------------------------------------------------------
1934+ Hash Join
1935+ Hash Cond: ((a.odd = b.odd) AND (a.hundred = b.hundred))
1936+ -> Seq Scan on tenk1 a
1937+ -> Hash
1938+ -> HashAggregate
1939+ Group Key: b.odd, b.hundred
1940+ -> Seq Scan on tenk2 b
1941+ (7 rows)
1942+
1943+ explain (costs off)
1944+ select * from tenk1 A where exists
1945+ (select 1 from tenk2 B
1946+ where A.hundred in (select C.hundred FROM tenk2 C
1947+ WHERE c.odd = b.odd));
1948+ QUERY PLAN
1949+ ---------------------------------
1950+ Nested Loop Semi Join
1951+ Join Filter: (SubPlan 1)
1952+ -> Seq Scan on tenk1 a
1953+ -> Materialize
1954+ -> Seq Scan on tenk2 b
1955+ SubPlan 1
1956+ -> Seq Scan on tenk2 c
1957+ Filter: (odd = b.odd)
1958+ (8 rows)
1959+
1960+ -- we should only try to pull up the sublink into RHS of a left join
1961+ -- but a.hundred is not avaiable.
1962+ explain (costs off)
1963+ SELECT * FROM tenk1 A LEFT JOIN tenk2 B
1964+ ON A.hundred in (SELECT c.hundred FROM tenk2 C WHERE c.odd = b.odd);
1965+ QUERY PLAN
1966+ ---------------------------------
1967+ Nested Loop Left Join
1968+ Join Filter: (SubPlan 1)
1969+ -> Seq Scan on tenk1 a
1970+ -> Materialize
1971+ -> Seq Scan on tenk2 b
1972+ SubPlan 1
1973+ -> Seq Scan on tenk2 c
1974+ Filter: (odd = b.odd)
1975+ (8 rows)
1976+
1977+ -- we should only try to pull up the sublink into RHS of a left join
1978+ -- but a.odd is not avaiable for this.
1979+ explain (costs off)
1980+ SELECT * FROM tenk1 A LEFT JOIN tenk2 B
1981+ ON B.hundred in (SELECT c.hundred FROM tenk2 C WHERE c.odd = a.odd);
1982+ QUERY PLAN
1983+ ---------------------------------
1984+ Nested Loop Left Join
1985+ Join Filter: (SubPlan 1)
1986+ -> Seq Scan on tenk1 a
1987+ -> Materialize
1988+ -> Seq Scan on tenk2 b
1989+ SubPlan 1
1990+ -> Seq Scan on tenk2 c
1991+ Filter: (odd = a.odd)
1992+ (8 rows)
1993+
1994+ -- should be able to pull up since all the references is available
1995+ explain (costs off)
1996+ SELECT * FROM tenk1 A LEFT JOIN tenk2 B
1997+ ON B.hundred in (SELECT c.hundred FROM tenk2 C WHERE c.odd = b.odd);
1998+ QUERY PLAN
1999+ ------------------------------------------------------------------------
2000+ Nested Loop Left Join
2001+ -> Seq Scan on tenk1 a
2002+ -> Materialize
2003+ -> Hash Join
2004+ Hash Cond: ((b.odd = c.odd) AND (b.hundred = c.hundred))
2005+ -> Seq Scan on tenk2 b
2006+ -> Hash
2007+ -> HashAggregate
2008+ Group Key: c.odd, c.hundred
2009+ -> Seq Scan on tenk2 c
2010+ (10 rows)
2011+
2012+ -- we can pull up the sublink into the inner JoinExpr.
2013+ explain (costs off)
2014+ SELECT * FROM tenk1 A INNER JOIN tenk2 B
2015+ ON A.hundred in (SELECT c.hundred FROM tenk2 C WHERE c.odd = b.odd);
2016+ QUERY PLAN
2017+ -------------------------------------------------
2018+ Hash Join
2019+ Hash Cond: (c.odd = b.odd)
2020+ -> Hash Join
2021+ Hash Cond: (a.hundred = c.hundred)
2022+ -> Seq Scan on tenk1 a
2023+ -> Hash
2024+ -> HashAggregate
2025+ Group Key: c.odd, c.hundred
2026+ -> Seq Scan on tenk2 c
2027+ -> Hash
2028+ -> Seq Scan on tenk2 b
2029+ (11 rows)
2030+
2031+ -- we can pull up the aggregate sublink into RHS of a left join.
2032+ explain (costs off)
2033+ SELECT * FROM tenk1 A LEFT JOIN tenk2 B
2034+ ON B.hundred in (SELECT min(c.hundred) FROM tenk2 C WHERE c.odd = b.odd);
2035+ QUERY PLAN
2036+ ---------------------------------------------------------------------------------------
2037+ Nested Loop Left Join
2038+ -> Seq Scan on tenk1 a
2039+ -> Materialize
2040+ -> Nested Loop
2041+ -> Seq Scan on tenk2 b
2042+ -> Memoize
2043+ Cache Key: b.hundred, b.odd
2044+ Cache Mode: binary
2045+ -> Subquery Scan on "ANY_subquery"
2046+ Filter: (b.hundred = "ANY_subquery".min)
2047+ -> Result
2048+ InitPlan 1 (returns $1)
2049+ -> Limit
2050+ -> Index Scan using tenk2_hundred on tenk2 c
2051+ Index Cond: (hundred IS NOT NULL)
2052+ Filter: (odd = b.odd)
2053+ (16 rows)
2054+
0 commit comments