Skip to content

Commit 8d3c4aa

Browse files
committed
Fix an oversight in join-removal optimization: we have to check not only for
plain Vars that are generated in the inner rel and used above the join, but also for PlaceHolderVars. Per report from Oleg K.
1 parent ecac5e6 commit 8d3c4aa

File tree

3 files changed

+89
-2
lines changed

3 files changed

+89
-2
lines changed

src/backend/optimizer/path/joinpath.c

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/optimizer/path/joinpath.c,v 1.130 2010/02/26 02:00:44 momjian Exp $
11+
* $PostgreSQL: pgsql/src/backend/optimizer/path/joinpath.c,v 1.131 2010/03/22 13:57:15 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -192,7 +192,9 @@ clause_sides_match_join(RestrictInfo *rinfo, RelOptInfo *outerrel,
192192
*
193193
* This is true for a left join for which the join condition cannot match
194194
* more than one inner-side row. (There are other possibly interesting
195-
* cases, but we don't have the infrastructure to prove them.)
195+
* cases, but we don't have the infrastructure to prove them.) We also
196+
* have to check that the inner side doesn't generate any variables needed
197+
* above the join.
196198
*
197199
* Note: there is no need to consider the symmetrical case of duplicating the
198200
* right input, because add_paths_to_joinrel() will be called with each rel
@@ -245,6 +247,19 @@ join_is_removable(PlannerInfo *root,
245247
return false;
246248
}
247249

250+
/*
251+
* Similarly check that the inner rel doesn't produce any PlaceHolderVars
252+
* that will be used above the join.
253+
*/
254+
foreach(l, root->placeholder_list)
255+
{
256+
PlaceHolderInfo *phinfo = (PlaceHolderInfo *) lfirst(l);
257+
258+
if (bms_is_subset(phinfo->ph_eval_at, innerrel->relids) &&
259+
!bms_is_subset(phinfo->ph_needed, joinrel->relids))
260+
return false;
261+
}
262+
248263
/*
249264
* Search for mergejoinable clauses that constrain the inner rel against
250265
* either the outer rel or a pseudoconstant. If an operator is

src/test/regress/expected/join.out

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2491,3 +2491,52 @@ select * from int4_tbl a full join int4_tbl b on false;
24912491
-2147483647 |
24922492
(10 rows)
24932493

2494+
--
2495+
-- test join removal
2496+
--
2497+
create temp table parent (k int primary key, pd int);
2498+
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "parent_pkey" for table "parent"
2499+
create temp table child (k int unique, cd int);
2500+
NOTICE: CREATE TABLE / UNIQUE will create implicit index "child_k_key" for table "child"
2501+
insert into parent values (1, 10), (2, 20), (3, 30);
2502+
insert into child values (1, 100), (4, 400);
2503+
-- this case is optimizable
2504+
select p.* from parent p left join child c on (p.k = c.k);
2505+
k | pd
2506+
---+----
2507+
1 | 10
2508+
2 | 20
2509+
3 | 30
2510+
(3 rows)
2511+
2512+
explain (costs off)
2513+
select p.* from parent p left join child c on (p.k = c.k);
2514+
QUERY PLAN
2515+
----------------------
2516+
Seq Scan on parent p
2517+
(1 row)
2518+
2519+
-- this case is not
2520+
select p.*, linked from parent p
2521+
left join (select c.*, true as linked from child c) as ss
2522+
on (p.k = ss.k);
2523+
k | pd | linked
2524+
---+----+--------
2525+
1 | 10 | t
2526+
2 | 20 |
2527+
3 | 30 |
2528+
(3 rows)
2529+
2530+
explain (costs off)
2531+
select p.*, linked from parent p
2532+
left join (select c.*, true as linked from child c) as ss
2533+
on (p.k = ss.k);
2534+
QUERY PLAN
2535+
---------------------------------
2536+
Hash Left Join
2537+
Hash Cond: (p.k = c.k)
2538+
-> Seq Scan on parent p
2539+
-> Hash
2540+
-> Seq Scan on child c
2541+
(5 rows)
2542+

src/test/regress/sql/join.sql

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,3 +567,26 @@ group by t1.q2 order by 1;
567567
--
568568
select * from int4_tbl a full join int4_tbl b on true;
569569
select * from int4_tbl a full join int4_tbl b on false;
570+
571+
--
572+
-- test join removal
573+
--
574+
575+
create temp table parent (k int primary key, pd int);
576+
create temp table child (k int unique, cd int);
577+
insert into parent values (1, 10), (2, 20), (3, 30);
578+
insert into child values (1, 100), (4, 400);
579+
580+
-- this case is optimizable
581+
select p.* from parent p left join child c on (p.k = c.k);
582+
explain (costs off)
583+
select p.* from parent p left join child c on (p.k = c.k);
584+
585+
-- this case is not
586+
select p.*, linked from parent p
587+
left join (select c.*, true as linked from child c) as ss
588+
on (p.k = ss.k);
589+
explain (costs off)
590+
select p.*, linked from parent p
591+
left join (select c.*, true as linked from child c) as ss
592+
on (p.k = ss.k);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy