Skip to content

Commit 5760668

Browse files
committed
WIP
1 parent eb74a19 commit 5760668

17 files changed

Lines changed: 342 additions & 373 deletions

File tree

contrib/auto_explain/auto_explain.c

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -305,19 +305,9 @@ explain_ExecutorStart(QueryDesc *queryDesc, int eflags)
305305

306306
if (auto_explain_enabled())
307307
{
308-
/*
309-
* Set up to track total elapsed time in ExecutorRun. Make sure the
310-
* space is allocated in the per-query context so it will go away at
311-
* ExecutorEnd.
312-
*/
308+
/* Set up to track total elapsed time in ExecutorRun. */
313309
if (queryDesc->totaltime == NULL)
314-
{
315-
MemoryContext oldcxt;
316-
317-
oldcxt = MemoryContextSwitchTo(queryDesc->estate->es_query_cxt);
318310
queryDesc->totaltime = InstrAlloc(INSTRUMENT_ALL);
319-
MemoryContextSwitchTo(oldcxt);
320-
}
321311
}
322312
}
323313

contrib/pg_session_buffer_usage/pg_session_buffer_usage.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ pg_session_buffer_usage(PG_FUNCTION_ARGS)
5252
memset(nulls, 0, sizeof(nulls));
5353

5454
#if HAVE_INSTR_STACK
55-
usage = &TopInstrStack.bufusage;
55+
usage = &TopInstrumentation.bufusage;
5656
#else
5757
usage = &pgBufferUsage;
5858
#endif
@@ -87,7 +87,7 @@ Datum
8787
pg_session_buffer_usage_reset(PG_FUNCTION_ARGS)
8888
{
8989
#if HAVE_INSTR_STACK
90-
memset(&TopInstrStack.bufusage, 0, sizeof(BufferUsage));
90+
memset(&TopInstrumentation.bufusage, 0, sizeof(BufferUsage));
9191
#else
9292
memset(&pgBufferUsage, 0, sizeof(BufferUsage));
9393
#endif

contrib/pg_stat_statements/pg_stat_statements.c

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -911,12 +911,12 @@ pgss_planner(Query *parse,
911911
{
912912
instr_time start;
913913
instr_time duration;
914-
InstrStack stack = {0};
914+
Instrumentation instr = {0};
915915

916916
INSTR_TIME_SET_CURRENT(start);
917917

918918
/* We need to track buffer/WAL usage as the planner can access them. */
919-
InstrPushStack(&stack);
919+
InstrPushStack(&instr);
920920

921921
nesting_level++;
922922
PG_TRY();
@@ -930,7 +930,7 @@ pgss_planner(Query *parse,
930930
}
931931
PG_FINALLY();
932932
{
933-
InstrPopAndFinalizeStack(&stack);
933+
InstrPopAndFinalizeStack(&instr);
934934
nesting_level--;
935935
}
936936
PG_END_TRY();
@@ -945,8 +945,8 @@ pgss_planner(Query *parse,
945945
PGSS_PLAN,
946946
INSTR_TIME_GET_MILLISEC(duration),
947947
0,
948-
&stack.bufusage,
949-
&stack.walusage,
948+
&instr.bufusage,
949+
&instr.walusage,
950950
NULL,
951951
NULL,
952952
0,
@@ -998,19 +998,9 @@ pgss_ExecutorStart(QueryDesc *queryDesc, int eflags)
998998
*/
999999
if (pgss_enabled(nesting_level) && queryDesc->plannedstmt->queryId != INT64CONST(0))
10001000
{
1001-
/*
1002-
* Set up to track total elapsed time in ExecutorRun. Make sure the
1003-
* space is allocated in the per-query context so it will go away at
1004-
* ExecutorEnd.
1005-
*/
1001+
/* Set up to track total elapsed time in ExecutorRun. */
10061002
if (queryDesc->totaltime == NULL)
1007-
{
1008-
MemoryContext oldcxt;
1009-
1010-
oldcxt = MemoryContextSwitchTo(queryDesc->estate->es_query_cxt);
10111003
queryDesc->totaltime = InstrAlloc(INSTRUMENT_ALL);
1012-
MemoryContextSwitchTo(oldcxt);
1013-
}
10141004
}
10151005
}
10161006

@@ -1071,8 +1061,8 @@ pgss_ExecutorEnd(QueryDesc *queryDesc)
10711061
* Check if stack is initialized - it is not when ExecutorRun wasn't
10721062
* called
10731063
*/
1074-
const BufferUsage *bufusage = queryDesc->totaltime->stack ? &queryDesc->totaltime->stack->bufusage : NULL;
1075-
const WalUsage *walusage = queryDesc->totaltime->stack ? &queryDesc->totaltime->stack->walusage : NULL;
1064+
const BufferUsage *bufusage = (queryDesc->totaltime->need_bufusage) ? &queryDesc->totaltime->bufusage : NULL;
1065+
const WalUsage *walusage = (queryDesc->totaltime->need_walusage) ? &queryDesc->totaltime->walusage : NULL;
10761066

10771067
pgss_store(queryDesc->sourceText,
10781068
queryId,
@@ -1149,10 +1139,10 @@ pgss_ProcessUtility(PlannedStmt *pstmt, const char *queryString,
11491139
instr_time start;
11501140
instr_time duration;
11511141
uint64 rows;
1152-
InstrStack stack = {0};
1142+
Instrumentation instr = {0};
11531143

11541144
INSTR_TIME_SET_CURRENT(start);
1155-
InstrPushStack(&stack);
1145+
InstrPushStack(&instr);
11561146

11571147
nesting_level++;
11581148
PG_TRY();
@@ -1168,7 +1158,7 @@ pgss_ProcessUtility(PlannedStmt *pstmt, const char *queryString,
11681158
}
11691159
PG_FINALLY();
11701160
{
1171-
InstrPopAndFinalizeStack(&stack);
1161+
InstrPopAndFinalizeStack(&instr);
11721162
nesting_level--;
11731163
}
11741164
PG_END_TRY();
@@ -1204,8 +1194,8 @@ pgss_ProcessUtility(PlannedStmt *pstmt, const char *queryString,
12041194
PGSS_EXEC,
12051195
INSTR_TIME_GET_MILLISEC(duration),
12061196
rows,
1207-
&stack.bufusage,
1208-
&stack.walusage,
1197+
&instr.bufusage,
1198+
&instr.walusage,
12091199
NULL,
12101200
NULL,
12111201
0,

src/backend/access/heap/vacuumlazy.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -984,7 +984,7 @@ heap_vacuum_rel(Relation rel, const VacuumParams params,
984984
{
985985
TimestampTz endtime = GetCurrentTimestamp();
986986

987-
InstrStop(instr, true);
987+
instr = InstrStop(instr, true);
988988

989989
if (verbose || params.log_vacuum_min_duration == 0 ||
990990
TimestampDifferenceExceeds(starttime, endtime,
@@ -1000,8 +1000,8 @@ heap_vacuum_rel(Relation rel, const VacuumParams params,
10001000
int64 total_blks_hit;
10011001
int64 total_blks_read;
10021002
int64 total_blks_dirtied;
1003-
BufferUsage bufferusage = instr->stack->bufusage;
1004-
WalUsage walusage = instr->stack->walusage;
1003+
BufferUsage bufferusage = instr->bufusage;
1004+
WalUsage walusage = instr->walusage;
10051005

10061006
TimestampDifference(starttime, endtime, &secs_dur, &usecs_dur);
10071007

src/backend/commands/analyze.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -742,7 +742,7 @@ do_analyze_rel(Relation onerel, const VacuumParams params,
742742
{
743743
TimestampTz endtime = GetCurrentTimestamp();
744744

745-
InstrStop(instr, true);
745+
instr = InstrStop(instr, true);
746746

747747
if (verbose || params.log_analyze_min_duration == 0 ||
748748
TimestampDifferenceExceeds(starttime, endtime,
@@ -756,8 +756,8 @@ do_analyze_rel(Relation onerel, const VacuumParams params,
756756
int64 total_blks_hit;
757757
int64 total_blks_read;
758758
int64 total_blks_dirtied;
759-
BufferUsage bufusage = instr->stack->bufusage;
760-
WalUsage walusage = instr->stack->walusage;
759+
BufferUsage bufusage = instr->bufusage;
760+
WalUsage walusage = instr->walusage;
761761

762762
total_blks_hit = bufusage.shared_blks_hit +
763763
bufusage.local_blks_hit;

src/backend/commands/explain.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ standard_ExplainOneQuery(Query *query, int cursorOptions,
353353
/* plan the query */
354354
plan = pg_plan_query(query, queryString, cursorOptions, params, es);
355355

356-
InstrStop(instr, true);
356+
instr = InstrStop(instr, true);
357357

358358
if (es->memory)
359359
{
@@ -363,7 +363,7 @@ standard_ExplainOneQuery(Query *query, int cursorOptions,
363363

364364
/* run it (if needed) and produce output */
365365
ExplainOnePlan(plan, into, es, queryString, params, queryEnv,
366-
&instr->total, (es->buffers ? &instr->stack->bufusage : NULL),
366+
&instr->total, (es->buffers ? &instr->bufusage : NULL),
367367
es->memory ? &mem_counters : NULL);
368368
}
369369

@@ -1827,7 +1827,7 @@ ExplainNode(PlanState *planstate, List *ancestors,
18271827
{
18281828
double nloops = planstate->instrument->nloops;
18291829
double startup_ms = INSTR_TIME_GET_MILLISEC(planstate->instrument->startup) / nloops;
1830-
double total_ms = INSTR_TIME_GET_MILLISEC(planstate->instrument->total) / nloops;
1830+
double total_ms = INSTR_TIME_GET_MILLISEC(planstate->instrument->instr.total) / nloops;
18311831
double rows = planstate->instrument->ntuples / nloops;
18321832

18331833
if (es->format == EXPLAIN_FORMAT_TEXT)
@@ -1893,7 +1893,7 @@ ExplainNode(PlanState *planstate, List *ancestors,
18931893
if (nloops <= 0)
18941894
continue;
18951895
startup_ms = INSTR_TIME_GET_MILLISEC(instrument->startup) / nloops;
1896-
total_ms = INSTR_TIME_GET_MILLISEC(instrument->total) / nloops;
1896+
total_ms = INSTR_TIME_GET_MILLISEC(instrument->instr.total) / nloops;
18971897
rows = instrument->ntuples / nloops;
18981898

18991899
ExplainOpenWorker(n, es);
@@ -2283,9 +2283,9 @@ ExplainNode(PlanState *planstate, List *ancestors,
22832283

22842284
/* Show buffer/WAL usage */
22852285
if (es->buffers && planstate->instrument)
2286-
show_buffer_usage(es, &planstate->instrument->stack.bufusage, NULL);
2286+
show_buffer_usage(es, &planstate->instrument->instr.bufusage, NULL);
22872287
if (es->wal && planstate->instrument)
2288-
show_wal_usage(es, &planstate->instrument->stack.walusage);
2288+
show_wal_usage(es, &planstate->instrument->instr.walusage);
22892289

22902290
/* Prepare per-worker buffer/WAL usage */
22912291
if (es->workers_state && (es->buffers || es->wal) && es->verbose)
@@ -2302,9 +2302,9 @@ ExplainNode(PlanState *planstate, List *ancestors,
23022302

23032303
ExplainOpenWorker(n, es);
23042304
if (es->buffers)
2305-
show_buffer_usage(es, &instrument->stack.bufusage, NULL);
2305+
show_buffer_usage(es, &instrument->instr.bufusage, NULL);
23062306
if (es->wal)
2307-
show_wal_usage(es, &instrument->stack.walusage);
2307+
show_wal_usage(es, &instrument->instr.walusage);
23082308
ExplainCloseWorker(n, es);
23092309
}
23102310
}

src/backend/commands/explain_dr.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,15 +192,15 @@ serializeAnalyzeReceive(TupleTableSlot *slot, DestReceiver *self)
192192
MemoryContextReset(myState->tmpcontext);
193193

194194
if (myState->es->timing || myState->es->buffers)
195-
InstrStop(instr, true);
195+
instr = InstrStop(instr, true);
196196

197197
/* Update timing data */
198198
if (myState->es->timing)
199199
INSTR_TIME_ADD(myState->metrics.timeSpent, instr->total);
200200

201201
/* Update buffer metrics */
202202
if (myState->es->buffers)
203-
BufferUsageAdd(&myState->metrics.bufferUsage, &instr->stack->bufusage);
203+
BufferUsageAdd(&myState->metrics.bufferUsage, &instr->bufusage);
204204

205205
return true;
206206
}

src/backend/commands/prepare.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,7 @@ ExplainExecuteQuery(ExecuteStmt *execstmt, IntoClause *into, ExplainState *es,
634634
cplan = GetCachedPlan(entry->plansource, paramLI,
635635
CurrentResourceOwner, pstate->p_queryEnv);
636636

637-
InstrStop(instr, true);
637+
instr = InstrStop(instr, true);
638638

639639
if (es->memory)
640640
{
@@ -651,7 +651,7 @@ ExplainExecuteQuery(ExecuteStmt *execstmt, IntoClause *into, ExplainState *es,
651651

652652
if (pstmt->commandType != CMD_UTILITY)
653653
ExplainOnePlan(pstmt, into, es, query_string, paramLI, pstate->p_queryEnv,
654-
&instr->total, (es->buffers ? &instr->stack->bufusage : NULL),
654+
&instr->total, (es->buffers ? &instr->bufusage : NULL),
655655
es->memory ? &mem_counters : NULL);
656656
else
657657
ExplainOneUtility(pstmt->utilityStmt, into, es, pstate, paramLI);

src/backend/executor/execMain.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -343,12 +343,13 @@ standard_ExecutorRun(QueryDesc *queryDesc,
343343
InstrStart(queryDesc->totaltime);
344344

345345
/*
346-
* Remember all node stacks for abort recovery. We do this once here
347-
* after the first call to InstrStart has allocated the parent stack.
346+
* Remember all node entries for abort recovery. We do this once here
347+
* after the first call to InstrStart has pushed the parent entry.
348348
*/
349-
if (queryDesc->totaltime->stack && !queryDesc->already_executed)
349+
if ((queryDesc->totaltime->need_bufusage || queryDesc->totaltime->need_walusage) &&
350+
!queryDesc->already_executed)
350351
ExecRememberNodeInstrumentation(queryDesc->planstate,
351-
queryDesc->totaltime->stack);
352+
queryDesc->totaltime);
352353
}
353354

354355
/*
@@ -479,7 +480,7 @@ standard_ExecutorFinish(QueryDesc *queryDesc)
479480
}
480481

481482
if (queryDesc->totaltime)
482-
InstrStop(queryDesc->totaltime, true);
483+
queryDesc->totaltime = InstrStop(queryDesc->totaltime, true);
483484

484485
MemoryContextSwitchTo(oldcontext);
485486

@@ -1533,8 +1534,8 @@ ExecFinalizeTriggerInstrumentation(EState *estate)
15331534
{
15341535
TriggerInstrumentation *ti = rInfo->ri_TrigInstrument;
15351536

1536-
if (ti && ti->instr.stack)
1537-
InstrStackAdd(CurrentInstrStack, ti->instr.stack);
1537+
if (ti && (ti->instr.need_bufusage || ti->instr.need_walusage))
1538+
InstrAccum(instr_stack.current, &ti->instr);
15381539
}
15391540
}
15401541

0 commit comments

Comments
 (0)