Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit8d3c4aa

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, butalso for PlaceHolderVars. Per report from Oleg K.
1 parentecac5e6 commit8d3c4aa

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 fulljoin int4_tbl bon true;
569569
select*from int4_tbl a fulljoin int4_tbl bon false;
570+
571+
--
572+
-- test join removal
573+
--
574+
575+
create temp table parent (kintprimary key, pdint);
576+
create temp table child (kint unique, cdint);
577+
insert into parentvalues (1,10), (2,20), (3,30);
578+
insert into childvalues (1,100), (4,400);
579+
580+
-- this case is optimizable
581+
select p.*from parent pleft join child con (p.k=c.k);
582+
explain (costs off)
583+
select p.*from parent pleft join child con (p.k=c.k);
584+
585+
-- this case is not
586+
select p.*, linkedfrom parent p
587+
left join (select c.*, trueas linkedfrom child c)as ss
588+
on (p.k=ss.k);
589+
explain (costs off)
590+
select p.*, linkedfrom parent p
591+
left join (select c.*, trueas linkedfrom child c)as ss
592+
on (p.k=ss.k);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp