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

Commitc2f974f

Browse files
committed
Fix hash join when inner hashkey expressions contain Params.
If the inner-side expressions contain PARAM_EXEC Params, we mustre-hash whenever the values of those Params change. The executormechanism for that exists already, but we failed to invoke it becausefinalize_plan() neglected to search the Hash.hashkeys field forParams. This allowed a previous scan's hash table to be re-usedwhen 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 tod24d75f of 7.4.Sadly, this simple fix depends on the plan representational changesmade by2abd7ae, so it will only work back to v12. I thoughtabout trying to make some kind of hack for v11, but I'm leeryof putting code significantly different from what is used in thenewer branches into a nearly-EOL branch. Seeing that the bugescaped detection for a full twenty years, problematic casesmust 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 parent39abe14 commitc2f974f

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
@@ -2653,6 +2653,11 @@ finalize_plan(PlannerInfo *root, Plan *plan,
26532653
&context);
26542654
break;
26552655

2656+
caseT_Hash:
2657+
finalize_primnode((Node*) ((Hash*)plan)->hashkeys,
2658+
&context);
2659+
break;
2660+
26562661
caseT_Limit:
26572662
finalize_primnode(((Limit*)plan)->limitOffset,
26582663
&context);
@@ -2753,7 +2758,6 @@ finalize_plan(PlannerInfo *root, Plan *plan,
27532758
break;
27542759

27552760
caseT_ProjectSet:
2756-
caseT_Hash:
27572761
caseT_Material:
27582762
caseT_Sort:
27592763
caseT_IncrementalSort:

‎src/test/regress/expected/join_hash.out

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

10331033
ROLLBACK;
1034+
-- Verify that we behave sanely when the inner hash keys contain parameters
1035+
-- (that is, outer or lateral references). This situation has to defeat
1036+
-- re-use of the inner hash table across rescans.
1037+
begin;
1038+
set local enable_hashjoin = on;
1039+
explain (costs off)
1040+
select i8.q2, ss.* from
1041+
int8_tbl i8,
1042+
lateral (select t1.fivethous, i4.f1 from tenk1 t1 join int4_tbl i4
1043+
on t1.fivethous = i4.f1+i8.q2 order by 1,2) ss;
1044+
QUERY PLAN
1045+
-----------------------------------------------------------
1046+
Nested Loop
1047+
-> Seq Scan on int8_tbl i8
1048+
-> Sort
1049+
Sort Key: t1.fivethous, i4.f1
1050+
-> Hash Join
1051+
Hash Cond: (t1.fivethous = (i4.f1 + i8.q2))
1052+
-> Seq Scan on tenk1 t1
1053+
-> Hash
1054+
-> Seq Scan on int4_tbl i4
1055+
(9 rows)
1056+
1057+
select i8.q2, ss.* from
1058+
int8_tbl i8,
1059+
lateral (select t1.fivethous, i4.f1 from tenk1 t1 join int4_tbl i4
1060+
on t1.fivethous = i4.f1+i8.q2 order by 1,2) ss;
1061+
q2 | fivethous | f1
1062+
-----+-----------+----
1063+
456 | 456 | 0
1064+
456 | 456 | 0
1065+
123 | 123 | 0
1066+
123 | 123 | 0
1067+
(4 rows)
1068+
1069+
rollback;

‎src/test/regress/sql/join_hash.sql

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,3 +556,22 @@ WHERE
556556
ANDhjtest_1.a<>hjtest_2.b;
557557

558558
ROLLBACK;
559+
560+
-- Verify that we behave sanely when the inner hash keys contain parameters
561+
-- (that is, outer or lateral references). This situation has to defeat
562+
-- re-use of the inner hash table across rescans.
563+
begin;
564+
set local enable_hashjoin=on;
565+
566+
explain (costs off)
567+
selecti8.q2, ss.*from
568+
int8_tbl i8,
569+
lateral (selectt1.fivethous,i4.f1from tenk1 t1join int4_tbl i4
570+
ont1.fivethous=i4.f1+i8.q2order by1,2) ss;
571+
572+
selecti8.q2, ss.*from
573+
int8_tbl i8,
574+
lateral (selectt1.fivethous,i4.f1from tenk1 t1join int4_tbl i4
575+
ont1.fivethous=i4.f1+i8.q2order by1,2) ss;
576+
577+
rollback;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp