Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit efeb12e

Browse files
committed
Don't include outer join relids in lateral_relids bitmapsets.
This avoids an assertion failure when outer joins are rearranged per identity 3. Listing only the baserels from a PlaceHolderVar's ph_lateral set should be enough to ensure that the required values are available when we need to compute the PHV --- it's what we did before inventing nullingrel sets, after all. It's a bit unsatisfying; but with beta2 hard upon us, there's not time to look for an aesthetically cleaner fix. Richard Guo and Tom Lane Discussion: https://postgr.es/m/CAMbWs48Jcw-NvnxT23WiHP324wG44DvzcH1j4hc0Zn+3sR9cfg@mail.gmail.com
1 parent 0655c03 commit efeb12e

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

src/backend/optimizer/plan/initsplan.c

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -580,24 +580,34 @@ create_lateral_join_info(PlannerInfo *root)
580580
{
581581
PlaceHolderInfo *phinfo = (PlaceHolderInfo *) lfirst(lc);
582582
Relids eval_at = phinfo->ph_eval_at;
583+
Relids lateral_refs;
583584
int varno;
584585

585586
if (phinfo->ph_lateral == NULL)
586587
continue; /* PHV is uninteresting if no lateral refs */
587588

588589
found_laterals = true;
589590

591+
/*
592+
* Include only baserels not outer joins in the evaluation sites'
593+
* lateral relids. This avoids problems when outer join order gets
594+
* rearranged, and it should still ensure that the lateral values are
595+
* available when needed.
596+
*/
597+
lateral_refs = bms_intersect(phinfo->ph_lateral, root->all_baserels);
598+
Assert(!bms_is_empty(lateral_refs));
599+
590600
if (bms_get_singleton_member(eval_at, &varno))
591601
{
592602
/* Evaluation site is a baserel */
593603
RelOptInfo *brel = find_base_rel(root, varno);
594604

595605
brel->direct_lateral_relids =
596606
bms_add_members(brel->direct_lateral_relids,
597-
phinfo->ph_lateral);
607+
lateral_refs);
598608
brel->lateral_relids =
599609
bms_add_members(brel->lateral_relids,
600-
phinfo->ph_lateral);
610+
lateral_refs);
601611
}
602612
else
603613
{
@@ -610,7 +620,7 @@ create_lateral_join_info(PlannerInfo *root)
610620
if (brel == NULL)
611621
continue; /* ignore outer joins in eval_at */
612622
brel->lateral_relids = bms_add_members(brel->lateral_relids,
613-
phinfo->ph_lateral);
623+
lateral_refs);
614624
}
615625
}
616626
}

src/test/regress/expected/join.out

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2624,6 +2624,23 @@ select * from int8_tbl t1
26242624
-> Function Scan on generate_series
26252625
(7 rows)
26262626

2627+
explain (costs off)
2628+
select * from int8_tbl t1
2629+
left join int8_tbl t2 on true
2630+
left join lateral
2631+
(select t2.q1 from int8_tbl t3) s
2632+
on t2.q1 = 1;
2633+
QUERY PLAN
2634+
-------------------------------------------
2635+
Nested Loop Left Join
2636+
-> Seq Scan on int8_tbl t1
2637+
-> Materialize
2638+
-> Nested Loop Left Join
2639+
Join Filter: (t2.q1 = 1)
2640+
-> Seq Scan on int8_tbl t2
2641+
-> Seq Scan on int8_tbl t3
2642+
(7 rows)
2643+
26272644
explain (costs off)
26282645
select * from onek t1
26292646
left join onek t2 on true

src/test/regress/sql/join.sql

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,13 @@ select * from int8_tbl t1
528528
(select * from generate_series(t2.q1, 100)) s
529529
on t2.q1 = 1;
530530

531+
explain (costs off)
532+
select * from int8_tbl t1
533+
left join int8_tbl t2 on true
534+
left join lateral
535+
(select t2.q1 from int8_tbl t3) s
536+
on t2.q1 = 1;
537+
531538
explain (costs off)
532539
select * from onek t1
533540
left join onek t2 on true

0 commit comments

Comments
 (0)