Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/backend/optimizer/path/allpaths.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
#include "port/pg_bitutils.h"
#include "rewrite/rewriteManip.h"
#include "utils/lsyscache.h"
#include "utils/memutils.h"
#include "nodes/memnodes.h"


/* Bitmask flags for pushdown_safety_info.unsafeFlags */
Expand Down Expand Up @@ -4302,6 +4304,10 @@ generate_partitionwise_join_paths(PlannerInfo *root, RelOptInfo *rel)
int num_parts;
RelOptInfo **part_rels;

MemoryContextCounters mem_start;

MemoryContextFuncStatsStart(CurrentMemoryContext, &mem_start, __FUNCTION__);

/* Handle only join relations here. */
if (!IS_JOIN_REL(rel))
return;
Expand Down Expand Up @@ -4366,6 +4372,8 @@ generate_partitionwise_join_paths(PlannerInfo *root, RelOptInfo *rel)
/* Build additional paths for this rel from child-join paths. */
add_paths_to_append_rel(root, rel, live_children);
list_free(live_children);

MemoryContextFuncStatsEnd(CurrentMemoryContext, &mem_start, __FUNCTION__);
}


Expand Down
31 changes: 30 additions & 1 deletion src/backend/optimizer/path/joinrels.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "optimizer/paths.h"
#include "partitioning/partbounds.h"
#include "utils/memutils.h"
#include "nodes/memnodes.h"


static void make_rels_by_clause_joins(PlannerInfo *root,
Expand Down Expand Up @@ -895,6 +896,13 @@ populate_joinrel_with_paths(PlannerInfo *root, RelOptInfo *rel1,
RelOptInfo *rel2, RelOptInfo *joinrel,
SpecialJoinInfo *sjinfo, List *restrictlist)
{
MemoryContextCounters mem_start;
char *label;

label = joinrel->reloptkind == RELOPT_OTHER_JOINREL ? "child_join_path_creation" : "parent_join_path_creation";

MemoryContextFuncStatsStart(CurrentMemoryContext, &mem_start, label);

/*
* Consider paths using each rel as both outer and inner. Depending on
* the join type, a provably empty outer or inner rel might mean the join
Expand Down Expand Up @@ -1045,6 +1053,8 @@ populate_joinrel_with_paths(PlannerInfo *root, RelOptInfo *rel1,

/* Apply partitionwise join technique, if possible. */
try_partitionwise_join(root, rel1, rel2, joinrel, sjinfo, restrictlist);

MemoryContextFuncStatsEnd(CurrentMemoryContext, &mem_start, label);
}


Expand Down Expand Up @@ -1487,6 +1497,10 @@ try_partitionwise_join(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2,
ListCell *lcr1 = NULL;
ListCell *lcr2 = NULL;
int cnt_parts;
MemoryContextCounters start_mem;

/* Start measuring memory */
MemoryContextFuncStatsStart(CurrentMemoryContext, &start_mem, __FUNCTION__);

/* Guard against stack overflow due to overly deep partition hierarchy. */
check_stack_depth();
Expand Down Expand Up @@ -1546,6 +1560,7 @@ try_partitionwise_join(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2,
Relids child_joinrelids;
AppendRelInfo **appinfos;
int nappinfos;
MemoryContextCounters restrict_mem;

if (joinrel->partbounds_merged)
{
Expand Down Expand Up @@ -1648,6 +1663,8 @@ try_partitionwise_join(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2,
/* Find the AppendRelInfo structures */
appinfos = find_appinfos_by_relids(root, child_joinrelids, &nappinfos);

MemoryContextFuncStatsStart(CurrentMemoryContext, &restrict_mem, "restrictlist translation");

/*
* Construct restrictions applicable to the child join from those
* applicable to the parent join.
Expand All @@ -1656,6 +1673,9 @@ try_partitionwise_join(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2,
(List *) adjust_appendrel_attrs(root,
(Node *) parent_restrictlist,
nappinfos, appinfos);

MemoryContextFuncStatsEnd(CurrentMemoryContext, &restrict_mem, "restrictlist translation");

pfree(appinfos);

child_joinrel = joinrel->part_rels[cnt_parts];
Expand All @@ -1676,6 +1696,10 @@ try_partitionwise_join(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2,
child_joinrel, child_sjinfo,
child_restrictlist);
}


/* Stop measuring memory and print the stats. */
MemoryContextFuncStatsEnd(CurrentMemoryContext, &start_mem, __FUNCTION__);
}

/*
Expand All @@ -1687,12 +1711,15 @@ static SpecialJoinInfo *
build_child_join_sjinfo(PlannerInfo *root, SpecialJoinInfo *parent_sjinfo,
Relids left_relids, Relids right_relids)
{
SpecialJoinInfo *sjinfo = makeNode(SpecialJoinInfo);
AppendRelInfo **left_appinfos;
int left_nappinfos;
AppendRelInfo **right_appinfos;
int right_nappinfos;
MemoryContextCounters mem_start;
SpecialJoinInfo *sjinfo;

MemoryContextFuncStatsStart(CurrentMemoryContext, &mem_start, __FUNCTION__);
sjinfo = makeNode(SpecialJoinInfo);
memcpy(sjinfo, parent_sjinfo, sizeof(SpecialJoinInfo));
left_appinfos = find_appinfos_by_relids(root, left_relids,
&left_nappinfos);
Expand All @@ -1718,6 +1745,8 @@ build_child_join_sjinfo(PlannerInfo *root, SpecialJoinInfo *parent_sjinfo,
pfree(left_appinfos);
pfree(right_appinfos);

MemoryContextFuncStatsEnd(CurrentMemoryContext, &mem_start, __FUNCTION__);

return sjinfo;
}

Expand Down
9 changes: 9 additions & 0 deletions src/backend/optimizer/plan/planner.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@
#include "utils/rel.h"
#include "utils/selfuncs.h"
#include "utils/syscache.h"
#include "utils/memutils.h"
#include "nodes/memnodes.h"

/* GUC parameters */
double cursor_tuple_fraction = DEFAULT_CURSOR_TUPLE_FRACTION;
Expand Down Expand Up @@ -295,6 +297,9 @@ standard_planner(Query *parse, const char *query_string, int cursorOptions,
Plan *top_plan;
ListCell *lp,
*lr;
MemoryContextCounters mem_start;

MemoryContextFuncStatsStart(CurrentMemoryContext, &mem_start, __FUNCTION__);

/*
* Set up global state for this planner invocation. This data is needed
Expand Down Expand Up @@ -565,6 +570,10 @@ standard_planner(Query *parse, const char *query_string, int cursorOptions,
if (glob->partition_directory != NULL)
DestroyPartitionDirectory(glob->partition_directory);

MemoryContextFuncStatsEnd(CurrentMemoryContext, &mem_start, __FUNCTION__);

MemoryContextFuncStatsReport();

return result;
}

Expand Down
47 changes: 46 additions & 1 deletion src/backend/optimizer/util/appendinfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ adjust_appendrel_attrs_mutator(Node *node,
{
AppendRelInfo **appinfos = context->appinfos;
int nappinfos = context->nappinfos;
PlannerInfo *root = context->root;
int cnt;

if (node == NULL)
Expand Down Expand Up @@ -445,7 +446,46 @@ adjust_appendrel_attrs_mutator(Node *node,
if (IsA(node, RestrictInfo))
{
RestrictInfo *oldinfo = (RestrictInfo *) node;
RestrictInfo *newinfo = makeNode(RestrictInfo);
Relids child_required_relids = adjust_child_relids(oldinfo->required_relids,
nappinfos, appinfos);
RestrictInfo *parent_rinfo;
ListCell *lc;
RestrictInfo *newinfo;
MemoryContext old_context;

/*
* If the adjusted RestrictInfo already exists, use it otherwise create
* a new one. This avoids translating the same RestrictInfo again and
* again wasting memory especially in partitionwise joins.
*/

if (bms_equal(child_required_relids, oldinfo->required_relids))
{
/* If the clause does not need any translation. */
bms_free(child_required_relids);
return (Node *) oldinfo;
}

/*
* Check if we already have the RestrictInfo for the given child in the
* topmost parent's RestrictInfo.
*/
parent_rinfo = oldinfo->parent_rinfo ? oldinfo->parent_rinfo : oldinfo;
foreach (lc, parent_rinfo->child_rinfos)
{
newinfo = lfirst(lc);

if (bms_equal(newinfo->required_relids, child_required_relids))
{
bms_free(child_required_relids);
return (Node *) newinfo;
}
}
bms_free(child_required_relids);

/* Translate in a long lasting memory context. */
old_context = MemoryContextSwitchTo(root->planner_cxt);
newinfo = makeNode(RestrictInfo);

/* Copy all flat-copiable fields, notably including rinfo_serial */
memcpy(newinfo, oldinfo, sizeof(RestrictInfo));
Expand Down Expand Up @@ -491,6 +531,11 @@ adjust_appendrel_attrs_mutator(Node *node,
newinfo->right_bucketsize = -1;
newinfo->left_mcvfreq = -1;
newinfo->right_mcvfreq = -1;
newinfo->parent_rinfo = parent_rinfo;
parent_rinfo->child_rinfos = lappend(parent_rinfo->child_rinfos,
newinfo);

MemoryContextSwitchTo(old_context);

return (Node *) newinfo;
}
Expand Down
5 changes: 5 additions & 0 deletions src/backend/optimizer/util/pathnode.c
Original file line number Diff line number Diff line change
Expand Up @@ -4063,6 +4063,9 @@ do { \
ParamPathInfo *new_ppi;
ParamPathInfo *old_ppi;
Relids required_outer;
MemoryContextCounters mem_start;

MemoryContextFuncStatsStart(CurrentMemoryContext, &mem_start, __FUNCTION__);

/*
* If the path is not parameterized by parent of the given relation, it
Expand Down Expand Up @@ -4311,6 +4314,8 @@ do { \
ADJUST_CHILD_ATTRS(new_path->pathtarget->exprs);
}

MemoryContextFuncStatsEnd(CurrentMemoryContext, &mem_start, __FUNCTION__);

return new_path;
}

Expand Down
18 changes: 16 additions & 2 deletions src/backend/optimizer/util/relnode.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
#include "parser/parse_relation.h"
#include "utils/hsearch.h"
#include "utils/lsyscache.h"
#include "nodes/memnodes.h"
#include "utils/memutils.h"


typedef struct JoinHashEntry
Expand Down Expand Up @@ -861,16 +863,22 @@ build_child_join_rel(PlannerInfo *root, RelOptInfo *outer_rel,
RelOptInfo *inner_rel, RelOptInfo *parent_joinrel,
List *restrictlist, SpecialJoinInfo *sjinfo)
{
RelOptInfo *joinrel = makeNode(RelOptInfo);
RelOptInfo *joinrel;
AppendRelInfo **appinfos;
int nappinfos;

MemoryContextCounters mem_start;
MemoryContextCounters tlist_mem;
MemoryContextCounters jlist_mem;

MemoryContextFuncStatsStart(CurrentMemoryContext, &mem_start, __FUNCTION__);

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

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

joinrel = makeNode(RelOptInfo);
joinrel->reloptkind = RELOPT_OTHER_JOINREL;
joinrel->relids = bms_union(outer_rel->relids, inner_rel->relids);
joinrel->relids = add_outer_joins_to_relids(root, joinrel->relids, sjinfo,
Expand Down Expand Up @@ -939,14 +947,18 @@ build_child_join_rel(PlannerInfo *root, RelOptInfo *outer_rel,
appinfos = find_appinfos_by_relids(root, joinrel->relids, &nappinfos);

/* Set up reltarget struct */
MemoryContextFuncStatsStart(CurrentMemoryContext, &tlist_mem, "targetlist");
build_child_join_reltarget(root, parent_joinrel, joinrel,
nappinfos, appinfos);
MemoryContextFuncStatsEnd(CurrentMemoryContext, &tlist_mem, "targetlist");

/* Construct joininfo list. */
MemoryContextFuncStatsStart(CurrentMemoryContext, &jlist_mem, "joininfo");
joinrel->joininfo = (List *) adjust_appendrel_attrs(root,
(Node *) parent_joinrel->joininfo,
nappinfos,
appinfos);
MemoryContextFuncStatsEnd(CurrentMemoryContext, &jlist_mem, "joininfo");

/*
* Lateral relids referred in child join will be same as that referred in
Expand Down Expand Up @@ -991,6 +1003,8 @@ build_child_join_rel(PlannerInfo *root, RelOptInfo *outer_rel,

pfree(appinfos);

MemoryContextFuncStatsEnd(CurrentMemoryContext, &mem_start, __FUNCTION__);

return joinrel;
}

Expand Down
2 changes: 2 additions & 0 deletions src/backend/optimizer/util/restrictinfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@ make_restrictinfo_internal(PlannerInfo *root,

restrictinfo->left_hasheqoperator = InvalidOid;
restrictinfo->right_hasheqoperator = InvalidOid;
restrictinfo->child_rinfos = NIL;
restrictinfo->parent_rinfo = NULL;

return restrictinfo;
}
Expand Down
Loading