Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita04ddd0

Browse files
committed
Improve support for ExplainOneQuery() hook
There is a hook called ExplainOneQuery_hook that gives modules thepossibility to plug into this code path, but, like utility.c for utilitystatement execution, there is no corresponding "standard" routine inthe case of EXPLAIN executed for one Query.This commit adds a new standard_ExplainOneQuery() in explain.c, which isable to run explain on a non-utility Query without calling its hook.Per the feedback received from a couple of hackers, this change givesthe possibility to cut a few hundred lines of code in some of thepopular out-of-core modules as these maintained a copy ofExplainOneQuery(), adding custom extra information at the beginning orthe end of the EXPLAIN output.Author: Mats KindahlReviewed-by: Aleksander Alekseev, Jelte Fennema-Nio, Andrei LepikhovDiscussion:https://postgr.es/m/CA+14427V_B4EAoC_o-iYYucRdMSOTfpuH9k-QbexffY1HYJBiA@mail.gmail.com
1 parentc399248 commita04ddd0

File tree

2 files changed

+63
-47
lines changed

2 files changed

+63
-47
lines changed

‎src/backend/commands/explain.c

Lines changed: 59 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -394,60 +394,72 @@ ExplainOneQuery(Query *query, int cursorOptions,
394394
(*ExplainOneQuery_hook) (query,cursorOptions,into,es,
395395
queryString,params,queryEnv);
396396
else
397-
{
398-
PlannedStmt*plan;
399-
instr_timeplanstart,
400-
planduration;
401-
BufferUsagebufusage_start,
402-
bufusage;
403-
MemoryContextCountersmem_counters;
404-
MemoryContextplanner_ctx=NULL;
405-
MemoryContextsaved_ctx=NULL;
406-
407-
if (es->memory)
408-
{
409-
/*
410-
* Create a new memory context to measure planner's memory
411-
* consumption accurately. Note that if the planner were to be
412-
* modified to use a different memory context type, here we would
413-
* be changing that to AllocSet, which might be undesirable.
414-
* However, we don't have a way to create a context of the same
415-
* type as another, so we pray and hope that this is OK.
416-
*/
417-
planner_ctx=AllocSetContextCreate(CurrentMemoryContext,
418-
"explain analyze planner context",
419-
ALLOCSET_DEFAULT_SIZES);
420-
saved_ctx=MemoryContextSwitchTo(planner_ctx);
421-
}
397+
standard_ExplainOneQuery(query,cursorOptions,into,es,
398+
queryString,params,queryEnv);
399+
}
422400

423-
if (es->buffers)
424-
bufusage_start=pgBufferUsage;
425-
INSTR_TIME_SET_CURRENT(planstart);
401+
/*
402+
* standard_ExplainOneQuery -
403+
* print out the execution plan for one Query, without calling a hook.
404+
*/
405+
void
406+
standard_ExplainOneQuery(Query*query,intcursorOptions,
407+
IntoClause*into,ExplainState*es,
408+
constchar*queryString,ParamListInfoparams,
409+
QueryEnvironment*queryEnv)
410+
{
411+
PlannedStmt*plan;
412+
instr_timeplanstart,
413+
planduration;
414+
BufferUsagebufusage_start,
415+
bufusage;
416+
MemoryContextCountersmem_counters;
417+
MemoryContextplanner_ctx=NULL;
418+
MemoryContextsaved_ctx=NULL;
419+
420+
if (es->memory)
421+
{
422+
/*
423+
* Create a new memory context to measure planner's memory consumption
424+
* accurately. Note that if the planner were to be modified to use a
425+
* different memory context type, here we would be changing that to
426+
* AllocSet, which might be undesirable. However, we don't have a way
427+
* to create a context of the same type as another, so we pray and
428+
* hope that this is OK.
429+
*/
430+
planner_ctx=AllocSetContextCreate(CurrentMemoryContext,
431+
"explain analyze planner context",
432+
ALLOCSET_DEFAULT_SIZES);
433+
saved_ctx=MemoryContextSwitchTo(planner_ctx);
434+
}
426435

427-
/* plan the query */
428-
plan=pg_plan_query(query,queryString,cursorOptions,params);
436+
if (es->buffers)
437+
bufusage_start=pgBufferUsage;
438+
INSTR_TIME_SET_CURRENT(planstart);
429439

430-
INSTR_TIME_SET_CURRENT(planduration);
431-
INSTR_TIME_SUBTRACT(planduration,planstart);
440+
/* plan the query */
441+
plan=pg_plan_query(query,queryString,cursorOptions,params);
432442

433-
if (es->memory)
434-
{
435-
MemoryContextSwitchTo(saved_ctx);
436-
MemoryContextMemConsumed(planner_ctx,&mem_counters);
437-
}
443+
INSTR_TIME_SET_CURRENT(planduration);
444+
INSTR_TIME_SUBTRACT(planduration,planstart);
438445

439-
/* calc differences of buffer counters. */
440-
if (es->buffers)
441-
{
442-
memset(&bufusage,0,sizeof(BufferUsage));
443-
BufferUsageAccumDiff(&bufusage,&pgBufferUsage,&bufusage_start);
444-
}
446+
if (es->memory)
447+
{
448+
MemoryContextSwitchTo(saved_ctx);
449+
MemoryContextMemConsumed(planner_ctx,&mem_counters);
450+
}
445451

446-
/* run it (if needed) and produce output */
447-
ExplainOnePlan(plan,into,es,queryString,params,queryEnv,
448-
&planduration, (es->buffers ?&bufusage :NULL),
449-
es->memory ?&mem_counters :NULL);
452+
/* calc differences of buffer counters. */
453+
if (es->buffers)
454+
{
455+
memset(&bufusage,0,sizeof(BufferUsage));
456+
BufferUsageAccumDiff(&bufusage,&pgBufferUsage,&bufusage_start);
450457
}
458+
459+
/* run it (if needed) and produce output */
460+
ExplainOnePlan(plan,into,es,queryString,params,queryEnv,
461+
&planduration, (es->buffers ?&bufusage :NULL),
462+
es->memory ?&mem_counters :NULL);
451463
}
452464

453465
/*

‎src/include/commands/explain.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ extern PGDLLIMPORT explain_get_index_name_hook_type explain_get_index_name_hook;
8080

8181
externvoidExplainQuery(ParseState*pstate,ExplainStmt*stmt,
8282
ParamListInfoparams,DestReceiver*dest);
83+
externvoidstandard_ExplainOneQuery(Query*query,intcursorOptions,
84+
IntoClause*into,ExplainState*es,
85+
constchar*queryString,ParamListInfoparams,
86+
QueryEnvironment*queryEnv);
8387

8488
externExplainState*NewExplainState(void);
8589

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp