Skip to content

Show prefetch statistic in EXPLAIN #248

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 12, 2023
Merged
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
38 changes: 36 additions & 2 deletions src/backend/commands/explain.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ ExplainOneQuery_hook_type ExplainOneQuery_hook = NULL;
/* Hook for plugins to get control in explain_get_index_name() */
explain_get_index_name_hook_type explain_get_index_name_hook = NULL;


/* OR-able flags for ExplainXMLTag() */
#define X_OPENING 0
#define X_CLOSING 1
Expand Down Expand Up @@ -121,6 +120,7 @@ static void show_eval_params(Bitmapset *bms_params, ExplainState *es);
static const char *explain_get_index_name(Oid indexId);
static void show_buffer_usage(ExplainState *es, const BufferUsage *usage,
bool planning);
static void show_prefetch_info(ExplainState *es, const PrefetchInfo* prefetch_info);
static void show_wal_usage(ExplainState *es, const WalUsage *usage);
static void ExplainIndexScanDetails(Oid indexid, ScanDirection indexorderdir,
ExplainState *es);
Expand Down Expand Up @@ -186,6 +186,8 @@ ExplainQuery(ParseState *pstate, ExplainStmt *stmt,
es->costs = defGetBoolean(opt);
else if (strcmp(opt->defname, "buffers") == 0)
es->buffers = defGetBoolean(opt);
else if (strcmp(opt->defname, "prefetch") == 0)
es->prefetch = defGetBoolean(opt);
else if (strcmp(opt->defname, "wal") == 0)
es->wal = defGetBoolean(opt);
else if (strcmp(opt->defname, "settings") == 0)
Expand Down Expand Up @@ -534,7 +536,7 @@ ExplainOnePlan(PlannedStmt *plannedstmt, IntoClause *into, ExplainState *es,
else if (es->analyze)
instrument_option |= INSTRUMENT_ROWS;

if (es->buffers)
if (es->buffers || es->prefetch)
instrument_option |= INSTRUMENT_BUFFERS;
if (es->wal)
instrument_option |= INSTRUMENT_WAL;
Expand Down Expand Up @@ -2055,6 +2057,10 @@ ExplainNode(PlanState *planstate, List *ancestors,
if (es->wal && planstate->instrument)
show_wal_usage(es, &planstate->instrument->walusage);

/* Show prefetch usage */
if (es->prefetch && planstate->instrument)
show_prefetch_info(es, &planstate->instrument->bufusage.prefetch);

/* Prepare per-worker buffer/WAL usage */
if (es->workers_state && (es->buffers || es->wal) && es->verbose)
{
Expand Down Expand Up @@ -3490,6 +3496,34 @@ explain_get_index_name(Oid indexId)
return result;
}

/*
* Show prefetch statistics
*/
static void
show_prefetch_info(ExplainState *es, const PrefetchInfo* prefetch_info)
{
if (es->format == EXPLAIN_FORMAT_TEXT)
{
ExplainIndentText(es);
appendStringInfo(es->str, "Prefetch: hits=%lld misses=%lld expired=%lld duplicates=%lld\n",
(long long) prefetch_info->hits,
(long long) prefetch_info->misses,
(long long) prefetch_info->expired,
(long long) prefetch_info->duplicates);
}
else
{
ExplainPropertyInteger("Prefetch Hits", NULL,
prefetch_info->hits, es);
ExplainPropertyInteger("Prefetch Misses", NULL,
prefetch_info->misses, es);
ExplainPropertyInteger("Prefetch Expired Requests", NULL,
prefetch_info->expired, es);
ExplainPropertyInteger("Prefetch Duplicated Requests", NULL,
prefetch_info->duplicates, es);
}
}

/*
* Show buffer usage details.
*/
Expand Down
8 changes: 8 additions & 0 deletions src/backend/executor/instrument.c
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,10 @@ BufferUsageAdd(BufferUsage *dst, const BufferUsage *add)
dst->local_blks_written += add->local_blks_written;
dst->temp_blks_read += add->temp_blks_read;
dst->temp_blks_written += add->temp_blks_written;
dst->prefetch.hits += add->prefetch.hits;
dst->prefetch.misses += add->prefetch.misses;
dst->prefetch.expired += add->prefetch.expired;
dst->prefetch.duplicates += add->prefetch.duplicates;
INSTR_TIME_ADD(dst->blk_read_time, add->blk_read_time);
INSTR_TIME_ADD(dst->blk_write_time, add->blk_write_time);
}
Expand All @@ -255,6 +259,10 @@ BufferUsageAccumDiff(BufferUsage *dst,
dst->local_blks_written += add->local_blks_written - sub->local_blks_written;
dst->temp_blks_read += add->temp_blks_read - sub->temp_blks_read;
dst->temp_blks_written += add->temp_blks_written - sub->temp_blks_written;
dst->prefetch.hits += add->prefetch.hits - sub->prefetch.hits;
dst->prefetch.misses += add->prefetch.misses - sub->prefetch.misses;
dst->prefetch.expired += add->prefetch.expired - sub->prefetch.expired;
dst->prefetch.duplicates += add->prefetch.duplicates - sub->prefetch.duplicates;
INSTR_TIME_ACCUM_DIFF(dst->blk_read_time,
add->blk_read_time, sub->blk_read_time);
INSTR_TIME_ACCUM_DIFF(dst->blk_write_time,
Expand Down
1 change: 1 addition & 0 deletions src/include/commands/explain.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ typedef struct ExplainState
bool timing; /* print detailed node timing */
bool summary; /* print total planning and execution timing */
bool settings; /* print modified settings */
bool prefetch; /* print prefetch statistic */
ExplainFormat format; /* output format */
/* state for output formatting --- not reset for each new plan tree */
int indent; /* current indentation level */
Expand Down
9 changes: 9 additions & 0 deletions src/include/executor/instrument.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@

#include "portability/instr_time.h"

/* Prefeth statistics */
typedef struct
{
int64 hits;
int64 misses;
int64 expired;
int64 duplicates;
} PrefetchInfo;

/*
* BufferUsage and WalUsage counters keep being incremented infinitely,
Expand All @@ -35,6 +43,7 @@ typedef struct BufferUsage
int64 temp_blks_written; /* # of temp blocks written */
instr_time blk_read_time; /* time spent reading */
instr_time blk_write_time; /* time spent writing */
PrefetchInfo prefetch; /* prefetch statistics */
} BufferUsage;

/*
Expand Down