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

Commit2f97105

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 parentc360594 commit2f97105

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

2635+
caseT_Hash:
2636+
finalize_primnode((Node*) ((Hash*)plan)->hashkeys,
2637+
&context);
2638+
break;
2639+
26352640
caseT_Limit:
26362641
finalize_primnode(((Limit*)plan)->limitOffset,
26372642
&context);
@@ -2727,7 +2732,6 @@ finalize_plan(PlannerInfo *root, Plan *plan,
27272732
break;
27282733

27292734
caseT_ProjectSet:
2730-
caseT_Hash:
27312735
caseT_Material:
27322736
caseT_Sort:
27332737
caseT_IncrementalSort:

‎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
ANDhjtest_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+
selecti8.q2, ss.*from
550+
int8_tbl i8,
551+
lateral (selectt1.fivethous,i4.f1from tenk1 t1join int4_tbl i4
552+
ont1.fivethous=i4.f1+i8.q2order by1,2) ss;
553+
554+
selecti8.q2, ss.*from
555+
int8_tbl i8,
556+
lateral (selectt1.fivethous,i4.f1from tenk1 t1join int4_tbl i4
557+
ont1.fivethous=i4.f1+i8.q2order by1,2) ss;
558+
559+
rollback;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp