Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit b56ee6c

Browse files
author
Commitfest Bot
committed
[CF 5697] v9 - Add planner expectations for Memoize in EXPLAIN
This branch was automatically generated by a robot using patches from an email thread registered at: https://commitfest.postgresql.org/patch/5697 The branch will be overwritten each time a new patch version is posted to the thread, and also periodically to check for bitrot caused by changes on the master branch. Patch(es): https://www.postgresql.org/message-id/15e0422a-31ec-4e82-b0cf-0839f636b185@tantorlabs.com Author(s): Ilia Evdokimov
2 parents e050af2 + 90b0519 commit b56ee6c

File tree

6 files changed

+65
-3
lines changed

6 files changed

+65
-3
lines changed

src/backend/commands/explain.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3616,6 +3616,26 @@ show_memoize_info(MemoizeState *mstate, List *ancestors, ExplainState *es)
36163616
ExplainPropertyText("Cache Key", keystr.data, es);
36173617
ExplainPropertyText("Cache Mode", mstate->binary_mode ? "binary" : "logical", es);
36183618

3619+
if (es->costs)
3620+
{
3621+
if (es->format == EXPLAIN_FORMAT_TEXT)
3622+
{
3623+
ExplainIndentText(es);
3624+
appendStringInfo(es->str, "Estimates: capacity=%u distinct keys=%.0f lookups=%.0f hit percent=%.2f%%\n",
3625+
((Memoize *) plan)->est_entries,
3626+
((Memoize *) plan)->est_unique_keys,
3627+
((Memoize *) plan)->est_calls,
3628+
((Memoize *) plan)->est_hit_ratio * 100.0);
3629+
}
3630+
else
3631+
{
3632+
ExplainPropertyUInteger("Estimated Capacity", NULL, ((Memoize *) plan)->est_entries, es);
3633+
ExplainPropertyFloat("Estimated Distinct Lookup Keys", NULL, ((Memoize *) plan)->est_unique_keys, 0, es);
3634+
ExplainPropertyFloat("Estimated Lookups", NULL, ((Memoize *) plan)->est_calls, 0, es);
3635+
ExplainPropertyFloat("Estimated Hit Percent", NULL, ((Memoize *) plan)->est_hit_ratio * 100.0, 2, es);
3636+
}
3637+
}
3638+
36193639
pfree(keystr.data);
36203640

36213641
if (!es->analyze)

src/backend/optimizer/path/costsize.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2604,6 +2604,9 @@ cost_memoize_rescan(PlannerInfo *root, MemoizePath *mpath,
26042604
mpath->est_entries = Min(Min(ndistinct, est_cache_entries),
26052605
PG_UINT32_MAX);
26062606

2607+
/* Remember ndistinct for a potential EXPLAIN later */
2608+
mpath->est_unique_keys = ndistinct;
2609+
26072610
/*
26082611
* When the number of distinct parameter values is above the amount we can
26092612
* store in the cache, then we'll have to evict some entries from the
@@ -2621,6 +2624,9 @@ cost_memoize_rescan(PlannerInfo *root, MemoizePath *mpath,
26212624
hit_ratio = ((calls - ndistinct) / calls) *
26222625
(est_cache_entries / Max(ndistinct, est_cache_entries));
26232626

2627+
/* Remember cache hit ratio for a potential EXPLAIN later */
2628+
mpath->est_hit_ratio = hit_ratio;
2629+
26242630
Assert(hit_ratio >= 0 && hit_ratio <= 1.0);
26252631

26262632
/*

src/backend/optimizer/plan/createplan.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,9 @@ static Material *make_material(Plan *lefttree);
284284
static Memoize *make_memoize(Plan *lefttree, Oid *hashoperators,
285285
Oid *collations, List *param_exprs,
286286
bool singlerow, bool binary_mode,
287-
uint32 est_entries, Bitmapset *keyparamids);
287+
uint32 est_entries, Bitmapset *keyparamids,
288+
double est_unique_keys, double est_hit_ratio,
289+
double est_calls);
288290
static WindowAgg *make_windowagg(List *tlist, WindowClause *wc,
289291
int partNumCols, AttrNumber *partColIdx, Oid *partOperators, Oid *partCollations,
290292
int ordNumCols, AttrNumber *ordColIdx, Oid *ordOperators, Oid *ordCollations,
@@ -1703,7 +1705,9 @@ create_memoize_plan(PlannerInfo *root, MemoizePath *best_path, int flags)
17031705

17041706
plan = make_memoize(subplan, operators, collations, param_exprs,
17051707
best_path->singlerow, best_path->binary_mode,
1706-
best_path->est_entries, keyparamids);
1708+
best_path->est_entries, keyparamids,
1709+
best_path->est_unique_keys, best_path->est_hit_ratio,
1710+
best_path->calls);
17071711

17081712
copy_generic_path_info(&plan->plan, (Path *) best_path);
17091713

@@ -6639,7 +6643,9 @@ materialize_finished_plan(Plan *subplan)
66396643
static Memoize *
66406644
make_memoize(Plan *lefttree, Oid *hashoperators, Oid *collations,
66416645
List *param_exprs, bool singlerow, bool binary_mode,
6642-
uint32 est_entries, Bitmapset *keyparamids)
6646+
uint32 est_entries, Bitmapset *keyparamids,
6647+
double est_unique_keys, double est_hit_ratio,
6648+
double est_calls)
66436649
{
66446650
Memoize *node = makeNode(Memoize);
66456651
Plan *plan = &node->plan;
@@ -6657,6 +6663,9 @@ make_memoize(Plan *lefttree, Oid *hashoperators, Oid *collations,
66576663
node->binary_mode = binary_mode;
66586664
node->est_entries = est_entries;
66596665
node->keyparamids = keyparamids;
6666+
node->est_unique_keys = est_unique_keys;
6667+
node->est_hit_ratio = est_hit_ratio;
6668+
node->est_calls = est_calls;
66606669

66616670
return node;
66626671
}

src/backend/optimizer/util/pathnode.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1701,6 +1701,18 @@ create_memoize_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath,
17011701
Assert(enable_memoize);
17021702
pathnode->path.disabled_nodes = subpath->disabled_nodes;
17031703

1704+
/*
1705+
* Estimated number of distinct memoization keys,
1706+
* computed using estimate_num_groups()
1707+
*/
1708+
pathnode->est_unique_keys = 0;
1709+
1710+
/*
1711+
* The estimated cache hit ratio will calculated later
1712+
* by cost_memoize_rescan().
1713+
*/
1714+
pathnode->est_hit_ratio = 0;
1715+
17041716
/*
17051717
* Add a small additional charge for caching the first entry. All the
17061718
* harder calculations for rescans are performed in cost_memoize_rescan().

src/include/nodes/pathnodes.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2135,6 +2135,9 @@ typedef struct MemoizePath
21352135
uint32 est_entries; /* The maximum number of entries that the
21362136
* planner expects will fit in the cache, or 0
21372137
* if unknown */
2138+
double est_unique_keys; /* Estimated number of distinct memoization keys,
2139+
* used for cache size evaluation. Kept for EXPLAIN */
2140+
double est_hit_ratio; /* Estimated cache hit ratio. Kept for EXPLAIN */
21382141
} MemoizePath;
21392142

21402143
/*

src/include/nodes/plannodes.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,6 +1056,18 @@ typedef struct Memoize
10561056

10571057
/* paramids from param_exprs */
10581058
Bitmapset *keyparamids;
1059+
1060+
/*
1061+
* Estimated number of distinct memoization keys,
1062+
* used for cache size evaluation. Kept for EXPLAIN
1063+
*/
1064+
double est_unique_keys;
1065+
1066+
/* Estimated cache hit ratio. Kept for EXPLAIN */
1067+
double est_hit_ratio;
1068+
1069+
/* Estimated number of rescans. Kept for EXPLAIN */
1070+
double est_calls;
10591071
} Memoize;
10601072

10611073
/* ----------------

0 commit comments

Comments
 (0)