Skip to content

Commit f088bf8

Browse files
committed
Fixes
1 parent 39690c1 commit f088bf8

3 files changed

Lines changed: 37 additions & 24 deletions

File tree

src/backend/commands/explain.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -324,15 +324,16 @@ standard_ExplainOneQuery(Query *query, int cursorOptions,
324324
QueryEnvironment *queryEnv)
325325
{
326326
PlannedStmt *plan;
327-
QueryInstrumentation *instr = NULL;
327+
QueryInstrumentation *plan_instr = NULL;
328+
InstrumentOption plan_instr_opts = INSTRUMENT_TIMER;
328329
MemoryContextCounters mem_counters;
329330
MemoryContext planner_ctx = NULL;
330331
MemoryContext saved_ctx = NULL;
331332

332333
if (es->buffers)
333-
instr = InstrQueryAlloc(INSTRUMENT_TIMER | INSTRUMENT_BUFFERS);
334-
else
335-
instr = InstrQueryAlloc(INSTRUMENT_TIMER);
334+
plan_instr_opts |= INSTRUMENT_BUFFERS;
335+
336+
plan_instr = InstrQueryAlloc(plan_instr_opts);
336337

337338
if (es->memory)
338339
{
@@ -350,12 +351,12 @@ standard_ExplainOneQuery(Query *query, int cursorOptions,
350351
saved_ctx = MemoryContextSwitchTo(planner_ctx);
351352
}
352353

353-
InstrQueryStart(instr);
354+
InstrQueryStart(plan_instr);
354355

355356
/* plan the query */
356357
plan = pg_plan_query(query, queryString, cursorOptions, params, es);
357358

358-
InstrQueryStopFinalize(instr);
359+
InstrQueryStopFinalize(plan_instr);
359360

360361
if (es->memory)
361362
{
@@ -365,7 +366,7 @@ standard_ExplainOneQuery(Query *query, int cursorOptions,
365366

366367
/* run it (if needed) and produce output */
367368
ExplainOnePlan(plan, into, es, queryString, params, queryEnv,
368-
&instr->instr.total, (es->buffers ? &instr->instr.bufusage : NULL),
369+
&plan_instr->instr.total, (es->buffers ? &plan_instr->instr.bufusage : NULL),
369370
es->memory ? &mem_counters : NULL);
370371
}
371372

src/backend/executor/execMain.c

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -467,26 +467,28 @@ standard_ExecutorFinish(QueryDesc *queryDesc)
467467
if (!(estate->es_top_eflags & EXEC_FLAG_SKIP_TRIGGERS))
468468
AfterTriggerEndQuery(estate);
469469

470-
/*
471-
* Accumulate per-node and trigger statistics to their respective parent
472-
* instrumentation stacks.
473-
*
474-
* We skip this in parallel workers because their per-node stats are
475-
* reported individually via ExecParallelReportInstrumentation, and the
476-
* leader's own ExecFinalizeNodeInstrumentation handles propagation. If
477-
* we accumulated here, the leader would double-count: worker parent nodes
478-
* would already include their children's stats, and then the leader's
479-
* accumulation would add the children again.
480-
*/
481-
if (queryDesc->totaltime && estate->es_instrument && !IsParallelWorker())
470+
if (queryDesc->totaltime)
482471
{
483-
ExecFinalizeNodeInstrumentation(queryDesc->planstate);
472+
/*
473+
* Accumulate per-node and trigger statistics to their respective parent
474+
* instrumentation stacks.
475+
*
476+
* We skip this in parallel workers because their per-node stats are
477+
* reported individually via ExecParallelReportInstrumentation, and the
478+
* leader's own ExecFinalizeNodeInstrumentation handles propagation. If
479+
* we accumulated here, the leader would double-count: worker parent nodes
480+
* would already include their children's stats, and then the leader's
481+
* accumulation would add the children again.
482+
*/
483+
if (estate->es_instrument && !IsParallelWorker())
484+
{
485+
ExecFinalizeNodeInstrumentation(queryDesc->planstate);
484486

485-
ExecFinalizeTriggerInstrumentation(estate);
486-
}
487+
ExecFinalizeTriggerInstrumentation(estate);
488+
}
487489

488-
if (queryDesc->totaltime)
489490
InstrQueryStopFinalize(queryDesc->totaltime);
491+
}
490492

491493
MemoryContextSwitchTo(oldcontext);
492494

src/backend/executor/instrument.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,20 @@
2121

2222
WalUsage pgWalUsage;
2323
Instrumentation instr_top;
24-
InstrStackState instr_stack = {0, 0, NULL, &instr_top};
24+
InstrStackState instr_stack = {
25+
.stack_space = 0,
26+
.stack_size = 0,
27+
.entries = NULL,
28+
.current = &instr_top,
29+
};
2530

2631
void
2732
InstrStackGrow(void)
2833
{
2934
int space = instr_stack.stack_space;
3035

36+
Assert(instr_stack.stack_size >= instr_stack.stack_space);
37+
3138
if (instr_stack.entries == NULL)
3239
{
3340
space = 10; /* Allocate sufficient initial space for
@@ -283,7 +290,10 @@ InstrQueryStopFinalize(QueryInstrumentation *qinstr)
283290
InstrStopFinalize(&qinstr->instr);
284291

285292
if (!qinstr->instr.need_stack)
293+
{
294+
Assert(qinstr->owner == NULL);
286295
return;
296+
}
287297

288298
Assert(qinstr->owner != NULL);
289299
ResourceOwnerForgetInstrumentation(qinstr->owner, qinstr);

0 commit comments

Comments
 (0)