Skip to content

Commit a04ddd0

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

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_time planstart,
400-
planduration;
401-
BufferUsage bufusage_start,
402-
bufusage;
403-
MemoryContextCounters mem_counters;
404-
MemoryContext planner_ctx = NULL;
405-
MemoryContext saved_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, int cursorOptions,
407+
IntoClause *into, ExplainState *es,
408+
const char *queryString, ParamListInfo params,
409+
QueryEnvironment *queryEnv)
410+
{
411+
PlannedStmt *plan;
412+
instr_time planstart,
413+
planduration;
414+
BufferUsage bufusage_start,
415+
bufusage;
416+
MemoryContextCounters mem_counters;
417+
MemoryContext planner_ctx = NULL;
418+
MemoryContext saved_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
extern void ExplainQuery(ParseState *pstate, ExplainStmt *stmt,
8282
ParamListInfo params, DestReceiver *dest);
83+
extern void standard_ExplainOneQuery(Query *query, int cursorOptions,
84+
IntoClause *into, ExplainState *es,
85+
const char *queryString, ParamListInfo params,
86+
QueryEnvironment *queryEnv);
8387

8488
extern ExplainState *NewExplainState(void);
8589

0 commit comments

Comments
 (0)