Skip to content

Commit 7e1ed56

Browse files
knizhniktristan957
authored andcommitted
Show prefetch statistic in EXPLAIN (#249)
* Show prefetch statistic in EXPLAIN refer #2994 * Collect per-node prefetch statistics * Show number of prefetch duplicates in explain
1 parent 199c83e commit 7e1ed56

File tree

4 files changed

+54
-2
lines changed

4 files changed

+54
-2
lines changed

src/backend/commands/explain.c

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ ExplainOneQuery_hook_type ExplainOneQuery_hook = NULL;
4747
/* Hook for plugins to get control in explain_get_index_name() */
4848
explain_get_index_name_hook_type explain_get_index_name_hook = NULL;
4949

50-
5150
/* OR-able flags for ExplainXMLTag() */
5251
#define X_OPENING 0
5352
#define X_CLOSING 1
@@ -121,6 +120,7 @@ static void show_eval_params(Bitmapset *bms_params, ExplainState *es);
121120
static const char *explain_get_index_name(Oid indexId);
122121
static void show_buffer_usage(ExplainState *es, const BufferUsage *usage,
123122
bool planning);
123+
static void show_prefetch_info(ExplainState *es, const PrefetchInfo* prefetch_info);
124124
static void show_wal_usage(ExplainState *es, const WalUsage *usage);
125125
static void ExplainIndexScanDetails(Oid indexid, ScanDirection indexorderdir,
126126
ExplainState *es);
@@ -186,6 +186,8 @@ ExplainQuery(ParseState *pstate, ExplainStmt *stmt,
186186
es->costs = defGetBoolean(opt);
187187
else if (strcmp(opt->defname, "buffers") == 0)
188188
es->buffers = defGetBoolean(opt);
189+
else if (strcmp(opt->defname, "prefetch") == 0)
190+
es->prefetch = defGetBoolean(opt);
189191
else if (strcmp(opt->defname, "wal") == 0)
190192
es->wal = defGetBoolean(opt);
191193
else if (strcmp(opt->defname, "settings") == 0)
@@ -534,7 +536,7 @@ ExplainOnePlan(PlannedStmt *plannedstmt, IntoClause *into, ExplainState *es,
534536
else if (es->analyze)
535537
instrument_option |= INSTRUMENT_ROWS;
536538

537-
if (es->buffers)
539+
if (es->buffers || es->prefetch)
538540
instrument_option |= INSTRUMENT_BUFFERS;
539541
if (es->wal)
540542
instrument_option |= INSTRUMENT_WAL;
@@ -2066,6 +2068,10 @@ ExplainNode(PlanState *planstate, List *ancestors,
20662068
if (es->wal && planstate->instrument)
20672069
show_wal_usage(es, &planstate->instrument->walusage);
20682070

2071+
/* Show prefetch usage */
2072+
if (es->prefetch && planstate->instrument)
2073+
show_prefetch_info(es, &planstate->instrument->bufusage.prefetch);
2074+
20692075
/* Prepare per-worker buffer/WAL usage */
20702076
if (es->workers_state && (es->buffers || es->wal) && es->verbose)
20712077
{
@@ -3501,6 +3507,34 @@ explain_get_index_name(Oid indexId)
35013507
return result;
35023508
}
35033509

3510+
/*
3511+
* Show prefetch statistics
3512+
*/
3513+
static void
3514+
show_prefetch_info(ExplainState *es, const PrefetchInfo* prefetch_info)
3515+
{
3516+
if (es->format == EXPLAIN_FORMAT_TEXT)
3517+
{
3518+
ExplainIndentText(es);
3519+
appendStringInfo(es->str, "Prefetch: hits=%lld misses=%lld expired=%lld duplicates=%lld\n",
3520+
(long long) prefetch_info->hits,
3521+
(long long) prefetch_info->misses,
3522+
(long long) prefetch_info->expired,
3523+
(long long) prefetch_info->duplicates);
3524+
}
3525+
else
3526+
{
3527+
ExplainPropertyInteger("Prefetch Hits", NULL,
3528+
prefetch_info->hits, es);
3529+
ExplainPropertyInteger("Prefetch Misses", NULL,
3530+
prefetch_info->misses, es);
3531+
ExplainPropertyInteger("Prefetch Expired Requests", NULL,
3532+
prefetch_info->expired, es);
3533+
ExplainPropertyInteger("Prefetch Duplicated Requests", NULL,
3534+
prefetch_info->duplicates, es);
3535+
}
3536+
}
3537+
35043538
/*
35053539
* Show buffer usage details.
35063540
*/

src/backend/executor/instrument.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,10 @@ BufferUsageAdd(BufferUsage *dst, const BufferUsage *add)
235235
dst->local_blks_written += add->local_blks_written;
236236
dst->temp_blks_read += add->temp_blks_read;
237237
dst->temp_blks_written += add->temp_blks_written;
238+
dst->prefetch.hits += add->prefetch.hits;
239+
dst->prefetch.misses += add->prefetch.misses;
240+
dst->prefetch.expired += add->prefetch.expired;
241+
dst->prefetch.duplicates += add->prefetch.duplicates;
238242
INSTR_TIME_ADD(dst->blk_read_time, add->blk_read_time);
239243
INSTR_TIME_ADD(dst->blk_write_time, add->blk_write_time);
240244
INSTR_TIME_ADD(dst->temp_blk_read_time, add->temp_blk_read_time);
@@ -257,6 +261,10 @@ BufferUsageAccumDiff(BufferUsage *dst,
257261
dst->local_blks_written += add->local_blks_written - sub->local_blks_written;
258262
dst->temp_blks_read += add->temp_blks_read - sub->temp_blks_read;
259263
dst->temp_blks_written += add->temp_blks_written - sub->temp_blks_written;
264+
dst->prefetch.hits += add->prefetch.hits - sub->prefetch.hits;
265+
dst->prefetch.misses += add->prefetch.misses - sub->prefetch.misses;
266+
dst->prefetch.expired += add->prefetch.expired - sub->prefetch.expired;
267+
dst->prefetch.duplicates += add->prefetch.duplicates - sub->prefetch.duplicates;
260268
INSTR_TIME_ACCUM_DIFF(dst->blk_read_time,
261269
add->blk_read_time, sub->blk_read_time);
262270
INSTR_TIME_ACCUM_DIFF(dst->blk_write_time,

src/include/commands/explain.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ typedef struct ExplainState
4646
bool timing; /* print detailed node timing */
4747
bool summary; /* print total planning and execution timing */
4848
bool settings; /* print modified settings */
49+
bool prefetch; /* print prefetch statistic */
4950
ExplainFormat format; /* output format */
5051
/* state for output formatting --- not reset for each new plan tree */
5152
int indent; /* current indentation level */

src/include/executor/instrument.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@
1515

1616
#include "portability/instr_time.h"
1717

18+
/* Prefeth statistics */
19+
typedef struct
20+
{
21+
int64 hits;
22+
int64 misses;
23+
int64 expired;
24+
int64 duplicates;
25+
} PrefetchInfo;
1826

1927
/*
2028
* BufferUsage and WalUsage counters keep being incremented infinitely,
@@ -37,6 +45,7 @@ typedef struct BufferUsage
3745
instr_time blk_write_time; /* time spent writing blocks */
3846
instr_time temp_blk_read_time; /* time spent reading temp blocks */
3947
instr_time temp_blk_write_time; /* time spent writing temp blocks */
48+
PrefetchInfo prefetch; /* prefetch statistics */
4049
} BufferUsage;
4150

4251
/*

0 commit comments

Comments
 (0)