From a5d8492517121ddc0366a4a853e041e3d4ab663d Mon Sep 17 00:00:00 2001 From: Ashutosh Bapat Date: Mon, 24 Jul 2023 20:20:53 +0530 Subject: [PATCH 2/2] Reduce memory used by child SpecialJoinInfo The SpecialJoinInfo applicable to a child join is computed by translating the same applicable to the parent join in try_partitionwise_join(). The child SpecialJoinInfo is not needed once the child join RelOptInfo is created and paths are added to it. Use a local variable to hold child SpecialJoinInfo so that it doesn't need memory allocated separately. Also free the memory allocated to various Bitmapsets in SpecialJoinInfo. Those are not referenced anywhere. But we do not free the memory allocated to expression trees of operator OID list since those may be referenced in paths or other objects. --- src/backend/optimizer/path/joinrels.c | 66 +++++++++++++++++++-------- 1 file changed, 46 insertions(+), 20 deletions(-) diff --git a/src/backend/optimizer/path/joinrels.c b/src/backend/optimizer/path/joinrels.c index 015a0b3cbe..51a811c6d9 100644 --- a/src/backend/optimizer/path/joinrels.c +++ b/src/backend/optimizer/path/joinrels.c @@ -42,9 +42,11 @@ static void try_partitionwise_join(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2, RelOptInfo *joinrel, SpecialJoinInfo *parent_sjinfo, List *parent_restrictlist); -static SpecialJoinInfo *build_child_join_sjinfo(PlannerInfo *root, - SpecialJoinInfo *parent_sjinfo, - Relids left_relids, Relids right_relids); +static void build_child_join_sjinfo(PlannerInfo *root, + SpecialJoinInfo *parent_sjinfo, + Relids left_relids, Relids right_relids, + SpecialJoinInfo *child_sjinfo); +static void free_child_sjinfo_members(SpecialJoinInfo *child_sjinfo); static void compute_partition_bounds(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2, RelOptInfo *joinrel, SpecialJoinInfo *parent_sjinfo, @@ -1540,7 +1542,7 @@ try_partitionwise_join(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2, RelOptInfo *child_rel2; bool rel1_empty; bool rel2_empty; - SpecialJoinInfo *child_sjinfo; + SpecialJoinInfo child_sjinfo; List *child_restrictlist; RelOptInfo *child_joinrel; AppendRelInfo **appinfos; @@ -1635,9 +1637,8 @@ try_partitionwise_join(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2, * Construct SpecialJoinInfo from parent join relations's * SpecialJoinInfo. */ - child_sjinfo = build_child_join_sjinfo(root, parent_sjinfo, - child_rel1->relids, - child_rel2->relids); + build_child_join_sjinfo(root, parent_sjinfo, child_rel1->relids, + child_rel2->relids, &child_sjinfo); /* Find the AppendRelInfo structures */ appinfos = find_appinfos_by_relids(root, @@ -1660,7 +1661,7 @@ try_partitionwise_join(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2, { child_joinrel = build_child_join_rel(root, child_rel1, child_rel2, joinrel, child_restrictlist, - child_sjinfo); + &child_sjinfo); joinrel->part_rels[cnt_parts] = child_joinrel; joinrel->live_parts = bms_add_member(joinrel->live_parts, cnt_parts); joinrel->all_partrels = bms_add_members(joinrel->all_partrels, @@ -1674,10 +1675,11 @@ try_partitionwise_join(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2, /* And make paths for the child join */ populate_joinrel_with_paths(root, child_rel1, child_rel2, - child_joinrel, child_sjinfo, + child_joinrel, &child_sjinfo, child_restrictlist); pfree(appinfos); + free_child_sjinfo_members(&child_sjinfo); } } @@ -1686,42 +1688,66 @@ try_partitionwise_join(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2, * SpecialJoinInfo for the join between parents. left_relids and right_relids * are the relids of left and right side of the join respectively. */ -static SpecialJoinInfo * +static void build_child_join_sjinfo(PlannerInfo *root, SpecialJoinInfo *parent_sjinfo, - Relids left_relids, Relids right_relids) + Relids left_relids, Relids right_relids, + SpecialJoinInfo *child_sjinfo) { - SpecialJoinInfo *sjinfo = makeNode(SpecialJoinInfo); AppendRelInfo **left_appinfos; int left_nappinfos; AppendRelInfo **right_appinfos; int right_nappinfos; - memcpy(sjinfo, parent_sjinfo, sizeof(SpecialJoinInfo)); + memcpy(child_sjinfo, parent_sjinfo, sizeof(SpecialJoinInfo)); left_appinfos = find_appinfos_by_relids(root, left_relids, &left_nappinfos); right_appinfos = find_appinfos_by_relids(root, right_relids, &right_nappinfos); - sjinfo->min_lefthand = adjust_child_relids(sjinfo->min_lefthand, + child_sjinfo->min_lefthand = adjust_child_relids(child_sjinfo->min_lefthand, left_nappinfos, left_appinfos); - sjinfo->min_righthand = adjust_child_relids(sjinfo->min_righthand, + child_sjinfo->min_righthand = adjust_child_relids(child_sjinfo->min_righthand, right_nappinfos, right_appinfos); - sjinfo->syn_lefthand = adjust_child_relids(sjinfo->syn_lefthand, + child_sjinfo->syn_lefthand = adjust_child_relids(child_sjinfo->syn_lefthand, left_nappinfos, left_appinfos); - sjinfo->syn_righthand = adjust_child_relids(sjinfo->syn_righthand, + child_sjinfo->syn_righthand = adjust_child_relids(child_sjinfo->syn_righthand, right_nappinfos, right_appinfos); /* outer-join relids need no adjustment */ - sjinfo->semi_rhs_exprs = (List *) adjust_appendrel_attrs(root, - (Node *) sjinfo->semi_rhs_exprs, + child_sjinfo->semi_rhs_exprs = (List *) adjust_appendrel_attrs(root, + (Node *) child_sjinfo->semi_rhs_exprs, right_nappinfos, right_appinfos); pfree(left_appinfos); pfree(right_appinfos); +} + +/* + * free_child_sjinfo_members + * Free memory consumed by members of a child SpecialJoinInfo. + */ +static void +free_child_sjinfo_members(SpecialJoinInfo *child_sjinfo) +{ + /* + * The relids are used only for comparison and their references are not + * stored anywhere. Free those. + */ + bms_free(child_sjinfo->min_lefthand); + bms_free(child_sjinfo->min_righthand); + bms_free(child_sjinfo->syn_lefthand); + bms_free(child_sjinfo->syn_righthand); + bms_free(child_sjinfo->commute_above_l); + bms_free(child_sjinfo->commute_above_r); + bms_free(child_sjinfo->commute_below_l); + bms_free(child_sjinfo->commute_below_r); - return sjinfo; + /* + * But the list of operator OIDs and the list of expressions may be + * referenced somewhere else. Do not free those. + */ } /* -- 2.25.1