Skip to content

Commit 513f447

Browse files
author
Richard Guo
committed
Reduce memory used by partitionwise joins
In try_partitionwise_join, we aim to break down the join between two partitioned relations into joins between matching partitions. To achieve this, we iterate through each pair of partitions from the two joining relations and create child-join relations for them. With potentially thousands of partitions, the local objects allocated in each iteration can accumulate significant memory usage. Therefore, we opt to eagerly free these local objects at the end of each iteration. In line with this approach, this patch frees the bitmap set that represents the relids of child-join relations at the end of each iteration. Additionally, it modifies build_child_join_rel() to reuse the AppendRelInfo structures generated within each iteration. Author: Ashutosh Bapat Reviewed-by: David Christensen, Richard Guo Discussion: https://postgr.es/m/CAExHW5s4EqY43oB=ne6B2=-xLgrs9ZGeTr1NXwkGFt2j-OmaQQ@mail.gmail.com
1 parent f47b33a commit 513f447

File tree

3 files changed

+16
-20
lines changed

3 files changed

+16
-20
lines changed

src/backend/optimizer/path/joinrels.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1547,6 +1547,7 @@ try_partitionwise_join(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2,
15471547
RelOptInfo *child_joinrel;
15481548
AppendRelInfo **appinfos;
15491549
int nappinfos;
1550+
Relids child_relids;
15501551

15511552
if (joinrel->partbounds_merged)
15521553
{
@@ -1642,9 +1643,8 @@ try_partitionwise_join(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2,
16421643
child_rel2->relids);
16431644

16441645
/* Find the AppendRelInfo structures */
1645-
appinfos = find_appinfos_by_relids(root,
1646-
bms_union(child_rel1->relids,
1647-
child_rel2->relids),
1646+
child_relids = bms_union(child_rel1->relids, child_rel2->relids);
1647+
appinfos = find_appinfos_by_relids(root, child_relids,
16481648
&nappinfos);
16491649

16501650
/*
@@ -1662,7 +1662,7 @@ try_partitionwise_join(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2,
16621662
{
16631663
child_joinrel = build_child_join_rel(root, child_rel1, child_rel2,
16641664
joinrel, child_restrictlist,
1665-
child_sjinfo);
1665+
child_sjinfo, nappinfos, appinfos);
16661666
joinrel->part_rels[cnt_parts] = child_joinrel;
16671667
joinrel->live_parts = bms_add_member(joinrel->live_parts, cnt_parts);
16681668
joinrel->all_partrels = bms_add_members(joinrel->all_partrels,
@@ -1679,7 +1679,14 @@ try_partitionwise_join(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2,
16791679
child_joinrel, child_sjinfo,
16801680
child_restrictlist);
16811681

1682+
/*
1683+
* When there are thousands of partitions involved, this loop will
1684+
* accumulate a significant amount of memory usage from objects that
1685+
* are only needed within the loop. Free these local objects eagerly
1686+
* at the end of each iteration.
1687+
*/
16821688
pfree(appinfos);
1689+
bms_free(child_relids);
16831690
free_child_join_sjinfo(child_sjinfo);
16841691
}
16851692
}

src/backend/optimizer/util/relnode.c

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -876,32 +876,22 @@ build_join_rel(PlannerInfo *root,
876876
* 'restrictlist': list of RestrictInfo nodes that apply to this particular
877877
* pair of joinable relations
878878
* 'sjinfo': child join's join-type details
879+
* 'nappinfos' and 'appinfos': AppendRelInfo array for child relids
879880
*/
880881
RelOptInfo *
881882
build_child_join_rel(PlannerInfo *root, RelOptInfo *outer_rel,
882883
RelOptInfo *inner_rel, RelOptInfo *parent_joinrel,
883-
List *restrictlist, SpecialJoinInfo *sjinfo)
884+
List *restrictlist, SpecialJoinInfo *sjinfo,
885+
int nappinfos, AppendRelInfo **appinfos)
884886
{
885887
RelOptInfo *joinrel = makeNode(RelOptInfo);
886-
AppendRelInfo **appinfos;
887-
int nappinfos;
888888

889889
/* Only joins between "other" relations land here. */
890890
Assert(IS_OTHER_REL(outer_rel) && IS_OTHER_REL(inner_rel));
891891

892892
/* The parent joinrel should have consider_partitionwise_join set. */
893893
Assert(parent_joinrel->consider_partitionwise_join);
894894

895-
/*
896-
* Find the AppendRelInfo structures for the child baserels. We'll need
897-
* these for computing the child join's relid set, and later for mapping
898-
* Vars to the child rel.
899-
*/
900-
appinfos = find_appinfos_by_relids(root,
901-
bms_union(outer_rel->relids,
902-
inner_rel->relids),
903-
&nappinfos);
904-
905895
joinrel->reloptkind = RELOPT_OTHER_JOINREL;
906896
joinrel->relids = adjust_child_relids(parent_joinrel->relids,
907897
nappinfos, appinfos);
@@ -1017,8 +1007,6 @@ build_child_join_rel(PlannerInfo *root, RelOptInfo *outer_rel,
10171007
nappinfos, appinfos,
10181008
parent_joinrel, joinrel);
10191009

1020-
pfree(appinfos);
1021-
10221010
return joinrel;
10231011
}
10241012

src/include/optimizer/pathnode.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,7 @@ extern Bitmapset *get_param_path_clause_serials(Path *path);
346346
extern RelOptInfo *build_child_join_rel(PlannerInfo *root,
347347
RelOptInfo *outer_rel, RelOptInfo *inner_rel,
348348
RelOptInfo *parent_joinrel, List *restrictlist,
349-
SpecialJoinInfo *sjinfo);
349+
SpecialJoinInfo *sjinfo,
350+
int nappinfos, AppendRelInfo **appinfos);
350351

351352
#endif /* PATHNODE_H */

0 commit comments

Comments
 (0)