Skip to content

Commit 9529b1e

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 dcef5b0 commit 9529b1e

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
@@ -2630,6 +2630,11 @@ finalize_plan(PlannerInfo *root, Plan *plan,
26302630
&context);
26312631
break;
26322632

2633+
case T_Hash:
2634+
finalize_primnode((Node *) ((Hash *) plan)->hashkeys,
2635+
&context);
2636+
break;
2637+
26332638
case T_Limit:
26342639
finalize_primnode(((Limit *) plan)->limitOffset,
26352640
&context);
@@ -2725,7 +2730,6 @@ finalize_plan(PlannerInfo *root, Plan *plan,
27252730
break;
27262731

27272732
case T_ProjectSet:
2728-
case T_Hash:
27292733
case T_Material:
27302734
case T_Sort:
27312735
case T_Unique:

src/test/regress/expected/join_hash.out

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

10151015
ROLLBACK;
1016+
-- Verify that we behave sanely when the inner hash keys contain parameters
1017+
-- (that is, outer or lateral references). This situation has to defeat
1018+
-- re-use of the inner hash table across rescans.
1019+
begin;
1020+
set local enable_hashjoin = on;
1021+
explain (costs off)
1022+
select i8.q2, ss.* from
1023+
int8_tbl i8,
1024+
lateral (select t1.fivethous, i4.f1 from tenk1 t1 join int4_tbl i4
1025+
on t1.fivethous = i4.f1+i8.q2 order by 1,2) ss;
1026+
QUERY PLAN
1027+
-----------------------------------------------------------
1028+
Nested Loop
1029+
-> Seq Scan on int8_tbl i8
1030+
-> Sort
1031+
Sort Key: t1.fivethous, i4.f1
1032+
-> Hash Join
1033+
Hash Cond: (t1.fivethous = (i4.f1 + i8.q2))
1034+
-> Seq Scan on tenk1 t1
1035+
-> Hash
1036+
-> Seq Scan on int4_tbl i4
1037+
(9 rows)
1038+
1039+
select i8.q2, ss.* from
1040+
int8_tbl i8,
1041+
lateral (select t1.fivethous, i4.f1 from tenk1 t1 join int4_tbl i4
1042+
on t1.fivethous = i4.f1+i8.q2 order by 1,2) ss;
1043+
q2 | fivethous | f1
1044+
-----+-----------+----
1045+
456 | 456 | 0
1046+
456 | 456 | 0
1047+
123 | 123 | 0
1048+
123 | 123 | 0
1049+
(4 rows)
1050+
1051+
rollback;

src/test/regress/sql/join_hash.sql

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

540540
ROLLBACK;
541+
542+
-- Verify that we behave sanely when the inner hash keys contain parameters
543+
-- (that is, outer or lateral references). This situation has to defeat
544+
-- re-use of the inner hash table across rescans.
545+
begin;
546+
set local enable_hashjoin = on;
547+
548+
explain (costs off)
549+
select i8.q2, ss.* from
550+
int8_tbl i8,
551+
lateral (select t1.fivethous, i4.f1 from tenk1 t1 join int4_tbl i4
552+
on t1.fivethous = i4.f1+i8.q2 order by 1,2) ss;
553+
554+
select i8.q2, ss.* from
555+
int8_tbl i8,
556+
lateral (select t1.fivethous, i4.f1 from tenk1 t1 join int4_tbl i4
557+
on t1.fivethous = i4.f1+i8.q2 order by 1,2) ss;
558+
559+
rollback;

0 commit comments

Comments
 (0)