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

Commitc959f84

Browse files
committed
Fix Memoize to work with partitionwise joining.
A couple of places weren't up to speed for this. By sheer goodluck, we didn't fail but just selected a non-memoized join plan,at least in the test case we have. Nonetheless, it's a bug,and I'm not quite sure that it couldn't have worse consequencesin other examples. So back-patch to v14 where Memoize came in.Richard GuoDiscussion:https://postgr.es/m/CAMbWs48GkNom272sfp0-WeD6_0HSR19BJ4H1c9ZKSfbVnJsvRg@mail.gmail.com
1 parent74a600a commitc959f84

File tree

5 files changed

+74
-3
lines changed

5 files changed

+74
-3
lines changed

‎src/backend/optimizer/path/joinpath.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,7 @@ get_memoize_path(PlannerInfo *root, RelOptInfo *innerrel,
505505
Path*outer_path,JoinTypejointype,
506506
JoinPathExtraData*extra)
507507
{
508+
RelOptInfo*top_outerrel;
508509
List*param_exprs;
509510
List*hash_operators;
510511
ListCell*lc;
@@ -594,10 +595,21 @@ get_memoize_path(PlannerInfo *root, RelOptInfo *innerrel,
594595
returnNULL;
595596
}
596597

598+
/*
599+
* When considering a partitionwise join, we have clauses that reference
600+
* the outerrel's top parent not outerrel itself.
601+
*/
602+
if (outerrel->reloptkind==RELOPT_OTHER_MEMBER_REL)
603+
top_outerrel=find_base_rel(root,bms_singleton_member(outerrel->top_parent_relids));
604+
elseif (outerrel->reloptkind==RELOPT_OTHER_JOINREL)
605+
top_outerrel=find_join_rel(root,outerrel->top_parent_relids);
606+
else
607+
top_outerrel=outerrel;
608+
597609
/* Check if we have hash ops for each parameter to the path */
598610
if (paraminfo_get_equal_hashops(root,
599611
inner_path->param_info,
600-
outerrel,
612+
top_outerrel,
601613
innerrel,
602614
&param_exprs,
603615
&hash_operators,

‎src/backend/optimizer/util/pathnode.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4193,6 +4193,7 @@ do { \
41934193

41944194
FLAT_COPY_PATH(mpath,path,MemoizePath);
41954195
REPARAMETERIZE_CHILD_PATH(mpath->subpath);
4196+
ADJUST_CHILD_ATTRS(mpath->param_exprs);
41964197
new_path= (Path*)mpath;
41974198
}
41984199
break;

‎src/include/nodes/pathnodes.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1513,8 +1513,8 @@ typedef struct MemoizePath
15131513
{
15141514
Pathpath;
15151515
Path*subpath;/* outerpath to cache tuples from */
1516-
List*hash_operators;/* hashoperatorsforeach key */
1517-
List*param_exprs;/* cache keys */
1516+
List*hash_operators;/*OIDs ofhashequality opsforcache keys */
1517+
List*param_exprs;/*expressions that arecache keys */
15181518
boolsinglerow;/* true if the cache entry is to be marked as
15191519
* complete after caching the first record. */
15201520
boolbinary_mode;/* true when cache key should be compared bit

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,45 @@ SELECT * FROM strtest s1 INNER JOIN strtest s2 ON s1.t >= s2.t;', false);
197197
(8 rows)
198198

199199
DROP TABLE strtest;
200+
-- Ensure memoize works with partitionwise join
201+
SET enable_partitionwise_join TO on;
202+
CREATE TABLE prt (a int) PARTITION BY RANGE(a);
203+
CREATE TABLE prt_p1 PARTITION OF prt FOR VALUES FROM (0) TO (10);
204+
CREATE TABLE prt_p2 PARTITION OF prt FOR VALUES FROM (10) TO (20);
205+
INSERT INTO prt VALUES (0), (0), (0), (0);
206+
INSERT INTO prt VALUES (10), (10), (10), (10);
207+
CREATE INDEX iprt_p1_a ON prt_p1 (a);
208+
CREATE INDEX iprt_p2_a ON prt_p2 (a);
209+
ANALYZE prt;
210+
SELECT explain_memoize('
211+
SELECT * FROM prt t1 INNER JOIN prt t2 ON t1.a = t2.a;', false);
212+
explain_memoize
213+
------------------------------------------------------------------------------------------
214+
Append (actual rows=32 loops=N)
215+
-> Nested Loop (actual rows=16 loops=N)
216+
-> Index Only Scan using iprt_p1_a on prt_p1 t1_1 (actual rows=4 loops=N)
217+
Heap Fetches: N
218+
-> Memoize (actual rows=4 loops=N)
219+
Cache Key: t1_1.a
220+
Cache Mode: logical
221+
Hits: 3 Misses: 1 Evictions: Zero Overflows: 0 Memory Usage: NkB
222+
-> Index Only Scan using iprt_p1_a on prt_p1 t2_1 (actual rows=4 loops=N)
223+
Index Cond: (a = t1_1.a)
224+
Heap Fetches: N
225+
-> Nested Loop (actual rows=16 loops=N)
226+
-> Index Only Scan using iprt_p2_a on prt_p2 t1_2 (actual rows=4 loops=N)
227+
Heap Fetches: N
228+
-> Memoize (actual rows=4 loops=N)
229+
Cache Key: t1_2.a
230+
Cache Mode: logical
231+
Hits: 3 Misses: 1 Evictions: Zero Overflows: 0 Memory Usage: NkB
232+
-> Index Only Scan using iprt_p2_a on prt_p2 t2_2 (actual rows=4 loops=N)
233+
Index Cond: (a = t1_2.a)
234+
Heap Fetches: N
235+
(21 rows)
236+
237+
DROP TABLE prt;
238+
RESET enable_partitionwise_join;
200239
-- Exercise Memoize code that flushes the cache when a parameter changes which
201240
-- is not part of the cache key.
202241
-- Ensure we get a Memoize plan

‎src/test/regress/sql/memoize.sql

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,25 @@ SELECT * FROM strtest s1 INNER JOIN strtest s2 ON s1.t >= s2.t;', false);
104104

105105
DROPTABLE strtest;
106106

107+
-- Ensure memoize works with partitionwise join
108+
SET enable_partitionwise_join TOon;
109+
110+
CREATETABLEprt (aint) PARTITION BY RANGE(a);
111+
CREATETABLEprt_p1 PARTITION OF prt FORVALUESFROM (0) TO (10);
112+
CREATETABLEprt_p2 PARTITION OF prt FORVALUESFROM (10) TO (20);
113+
INSERT INTO prtVALUES (0), (0), (0), (0);
114+
INSERT INTO prtVALUES (10), (10), (10), (10);
115+
CREATEINDEXiprt_p1_aON prt_p1 (a);
116+
CREATEINDEXiprt_p2_aON prt_p2 (a);
117+
ANALYZE prt;
118+
119+
SELECT explain_memoize('
120+
SELECT * FROM prt t1 INNER JOIN prt t2 ON t1.a = t2.a;', false);
121+
122+
DROPTABLE prt;
123+
124+
RESET enable_partitionwise_join;
125+
107126
-- Exercise Memoize code that flushes the cache when a parameter changes which
108127
-- is not part of the cache key.
109128

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp