Skip to content

Commit 4539262

Browse files
committed
Fix hash join when inner hashkey expressions contain Params.
If the inner-side expressions contain PARAM_EXEC Params, we must re-hash whenever the values of those Params change. The executor mechanism for that exists already, but we failed to invoke it because finalize_plan() neglected to search the Hash.hashkeys field for Params. This allowed a previous scan's hash table to be re-used when it should not be, leading to rows missing from the join's output. (I believe incorrectly-included join rows are impossible however, since checking the real hashclauses would reject false matches.) This bug is very ancient, dating probably to d24d75f of 7.4. Sadly, this simple fix depends on the plan representational changes made by 2abd7ae, so it will only work back to v12. I thought about trying to make some kind of hack for v11, but I'm leery of putting code significantly different from what is used in the newer branches into a nearly-EOL branch. Seeing that the bug escaped detection for a full twenty years, problematic cases must be rare; so I don't feel too awful about leaving v11 as-is. Per bug #17985 from Zuming Jiang. Back-patch to v12. Discussion: https://postgr.es/m/17985-748b66607acd432e@postgresql.org
1 parent 8a300fc commit 4539262

File tree

3 files changed

+60
-1
lines changed

3 files changed

+60
-1
lines changed

src/backend/optimizer/plan/subselect.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2648,6 +2648,11 @@ finalize_plan(PlannerInfo *root, Plan *plan,
26482648
&context);
26492649
break;
26502650

2651+
case T_Hash:
2652+
finalize_primnode((Node *) ((Hash *) plan)->hashkeys,
2653+
&context);
2654+
break;
2655+
26512656
case T_Limit:
26522657
finalize_primnode(((Limit *) plan)->limitOffset,
26532658
&context);
@@ -2748,7 +2753,6 @@ finalize_plan(PlannerInfo *root, Plan *plan,
27482753
break;
27492754

27502755
case T_ProjectSet:
2751-
case T_Hash:
27522756
case T_Material:
27532757
case T_Sort:
27542758
case T_IncrementalSort:

src/test/regress/expected/join_hash.out

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1128,3 +1128,39 @@ WHERE
11281128
(1 row)
11291129

11301130
ROLLBACK;
1131+
-- Verify that we behave sanely when the inner hash keys contain parameters
1132+
-- (that is, outer or lateral references). This situation has to defeat
1133+
-- re-use of the inner hash table across rescans.
1134+
begin;
1135+
set local enable_hashjoin = on;
1136+
explain (costs off)
1137+
select i8.q2, ss.* from
1138+
int8_tbl i8,
1139+
lateral (select t1.fivethous, i4.f1 from tenk1 t1 join int4_tbl i4
1140+
on t1.fivethous = i4.f1+i8.q2 order by 1,2) ss;
1141+
QUERY PLAN
1142+
-----------------------------------------------------------
1143+
Nested Loop
1144+
-> Seq Scan on int8_tbl i8
1145+
-> Sort
1146+
Sort Key: t1.fivethous, i4.f1
1147+
-> Hash Join
1148+
Hash Cond: (t1.fivethous = (i4.f1 + i8.q2))
1149+
-> Seq Scan on tenk1 t1
1150+
-> Hash
1151+
-> Seq Scan on int4_tbl i4
1152+
(9 rows)
1153+
1154+
select i8.q2, ss.* from
1155+
int8_tbl i8,
1156+
lateral (select t1.fivethous, i4.f1 from tenk1 t1 join int4_tbl i4
1157+
on t1.fivethous = i4.f1+i8.q2 order by 1,2) ss;
1158+
q2 | fivethous | f1
1159+
-----+-----------+----
1160+
456 | 456 | 0
1161+
456 | 456 | 0
1162+
123 | 123 | 0
1163+
123 | 123 | 0
1164+
(4 rows)
1165+
1166+
rollback;

src/test/regress/sql/join_hash.sql

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,3 +604,22 @@ WHERE
604604
AND hjtest_1.a <> hjtest_2.b;
605605

606606
ROLLBACK;
607+
608+
-- Verify that we behave sanely when the inner hash keys contain parameters
609+
-- (that is, outer or lateral references). This situation has to defeat
610+
-- re-use of the inner hash table across rescans.
611+
begin;
612+
set local enable_hashjoin = on;
613+
614+
explain (costs off)
615+
select i8.q2, ss.* from
616+
int8_tbl i8,
617+
lateral (select t1.fivethous, i4.f1 from tenk1 t1 join int4_tbl i4
618+
on t1.fivethous = i4.f1+i8.q2 order by 1,2) ss;
619+
620+
select i8.q2, ss.* from
621+
int8_tbl i8,
622+
lateral (select t1.fivethous, i4.f1 from tenk1 t1 join int4_tbl i4
623+
on t1.fivethous = i4.f1+i8.q2 order by 1,2) ss;
624+
625+
rollback;

0 commit comments

Comments
 (0)