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

Commit4539262

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 parent8a300fc commit4539262

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+
caseT_Hash:
2652+
finalize_primnode((Node*) ((Hash*)plan)->hashkeys,
2653+
&context);
2654+
break;
2655+
26512656
caseT_Limit:
26522657
finalize_primnode(((Limit*)plan)->limitOffset,
26532658
&context);
@@ -2748,7 +2753,6 @@ finalize_plan(PlannerInfo *root, Plan *plan,
27482753
break;
27492754

27502755
caseT_ProjectSet:
2751-
caseT_Hash:
27522756
caseT_Material:
27532757
caseT_Sort:
27542758
caseT_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
ANDhjtest_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+
selecti8.q2, ss.*from
616+
int8_tbl i8,
617+
lateral (selectt1.fivethous,i4.f1from tenk1 t1join int4_tbl i4
618+
ont1.fivethous=i4.f1+i8.q2order by1,2) ss;
619+
620+
selecti8.q2, ss.*from
621+
int8_tbl i8,
622+
lateral (selectt1.fivethous,i4.f1from tenk1 t1join int4_tbl i4
623+
ont1.fivethous=i4.f1+i8.q2order by1,2) ss;
624+
625+
rollback;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp