Skip to content

Commit bca3d34

Browse files
knizhniktristan957
authored andcommitted
Show prefetch statistic in EXPLAIN (#248)
* Show prefetch statistic in EXPLAIN refer #2994 * Collect per-node prefetch statistics * Show number of prefetch duplicates in explain
1 parent 712b9b2 commit bca3d34

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;
@@ -2055,6 +2057,10 @@ ExplainNode(PlanState *planstate, List *ancestors,
20552057
if (es->wal && planstate->instrument)
20562058
show_wal_usage(es, &planstate->instrument->walusage);
20572059

2060+
/* Show prefetch usage */
2061+
if (es->prefetch && planstate->instrument)
2062+
show_prefetch_info(es, &planstate->instrument->bufusage.prefetch);
2063+
20582064
/* Prepare per-worker buffer/WAL usage */
20592065
if (es->workers_state && (es->buffers || es->wal) && es->verbose)
20602066
{
@@ -3490,6 +3496,34 @@ explain_get_index_name(Oid indexId)
34903496
return result;
34913497
}
34923498

3499+
/*
3500+
* Show prefetch statistics
3501+
*/
3502+
static void
3503+
show_prefetch_info(ExplainState *es, const PrefetchInfo* prefetch_info)
3504+
{
3505+
if (es->format == EXPLAIN_FORMAT_TEXT)
3506+
{
3507+
ExplainIndentText(es);
3508+
appendStringInfo(es->str, "Prefetch: hits=%lld misses=%lld expired=%lld duplicates=%lld\n",
3509+
(long long) prefetch_info->hits,
3510+
(long long) prefetch_info->misses,
3511+
(long long) prefetch_info->expired,
3512+
(long long) prefetch_info->duplicates);
3513+
}
3514+
else
3515+
{
3516+
ExplainPropertyInteger("Prefetch Hits", NULL,
3517+
prefetch_info->hits, es);
3518+
ExplainPropertyInteger("Prefetch Misses", NULL,
3519+
prefetch_info->misses, es);
3520+
ExplainPropertyInteger("Prefetch Expired Requests", NULL,
3521+
prefetch_info->expired, es);
3522+
ExplainPropertyInteger("Prefetch Duplicated Requests", NULL,
3523+
prefetch_info->duplicates, es);
3524+
}
3525+
}
3526+
34933527
/*
34943528
* Show buffer usage details.
34953529
*/

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
}
@@ -255,6 +259,10 @@ BufferUsageAccumDiff(BufferUsage *dst,
255259
dst->local_blks_written += add->local_blks_written - sub->local_blks_written;
256260
dst->temp_blks_read += add->temp_blks_read - sub->temp_blks_read;
257261
dst->temp_blks_written += add->temp_blks_written - sub->temp_blks_written;
262+
dst->prefetch.hits += add->prefetch.hits - sub->prefetch.hits;
263+
dst->prefetch.misses += add->prefetch.misses - sub->prefetch.misses;
264+
dst->prefetch.expired += add->prefetch.expired - sub->prefetch.expired;
265+
dst->prefetch.duplicates += add->prefetch.duplicates - sub->prefetch.duplicates;
258266
INSTR_TIME_ACCUM_DIFF(dst->blk_read_time,
259267
add->blk_read_time, sub->blk_read_time);
260268
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,
@@ -35,6 +43,7 @@ typedef struct BufferUsage
3543
int64 temp_blks_written; /* # of temp blocks written */
3644
instr_time blk_read_time; /* time spent reading */
3745
instr_time blk_write_time; /* time spent writing */
46+
PrefetchInfo prefetch; /* prefetch statistics */
3847
} BufferUsage;
3948

4049
/*

0 commit comments

Comments
 (0)