* ExecutorEnd hook: log results if needed */
| 384 | * ExecutorEnd hook: log results if needed |
| 385 | */ |
| 386 | static void |
| 387 | explain_ExecutorEnd(QueryDesc *queryDesc) |
| 388 | { |
| 389 | if (queryDesc->totaltime && auto_explain_enabled()) |
| 390 | { |
| 391 | MemoryContext oldcxt; |
| 392 | double msec; |
| 393 | |
| 394 | /* Wait for completion of all qExec processes. */ |
| 395 | if (queryDesc->estate->dispatcherState && queryDesc->estate->dispatcherState->primaryResults) |
| 396 | cdbdisp_checkDispatchResult(queryDesc->estate->dispatcherState, DISPATCH_WAIT_NONE); |
| 397 | |
| 398 | /* |
| 399 | * Make sure we operate in the per-query context, so any cruft will be |
| 400 | * discarded later during ExecutorEnd. |
| 401 | */ |
| 402 | oldcxt = MemoryContextSwitchTo(queryDesc->estate->es_query_cxt); |
| 403 | |
| 404 | /* |
| 405 | * Make sure stats accumulation is done. (Note: it's okay if several |
| 406 | * levels of hook all do this.) |
| 407 | */ |
| 408 | InstrEndLoop(queryDesc->totaltime); |
| 409 | |
| 410 | /* Log plan if duration is exceeded. */ |
| 411 | msec = queryDesc->totaltime->total * 1000.0; |
| 412 | if (msec >= auto_explain_log_min_duration) |
| 413 | { |
| 414 | ExplainState *es = NewExplainState(); |
| 415 | |
| 416 | es->analyze = (queryDesc->instrument_options && auto_explain_log_analyze); |
| 417 | es->verbose = auto_explain_log_verbose; |
| 418 | es->buffers = (es->analyze && auto_explain_log_buffers); |
| 419 | es->wal = (es->analyze && auto_explain_log_wal); |
| 420 | es->timing = (es->analyze && auto_explain_log_timing); |
| 421 | es->summary = es->analyze; |
| 422 | es->format = auto_explain_log_format; |
| 423 | es->settings = auto_explain_log_settings; |
| 424 | |
| 425 | ExplainBeginOutput(es); |
| 426 | ExplainQueryText(es, queryDesc); |
| 427 | ExplainPrintPlan(es, queryDesc); |
| 428 | if (es->analyze && auto_explain_log_triggers) |
| 429 | ExplainPrintTriggers(es, queryDesc); |
| 430 | if (es->costs) |
| 431 | ExplainPrintJITSummary(es, queryDesc); |
| 432 | if (es->analyze) |
| 433 | ExplainPrintExecStatsEnd(es, queryDesc); |
| 434 | ExplainEndOutput(es); |
| 435 | |
| 436 | /* Remove last line break */ |
| 437 | if (es->str->len > 0 && es->str->data[es->str->len - 1] == '\n') |
| 438 | es->str->data[--es->str->len] = '\0'; |
| 439 | |
| 440 | /* Fix JSON to output an object */ |
| 441 | if (auto_explain_log_format == EXPLAIN_FORMAT_JSON) |
| 442 | { |
| 443 | es->str->data[0] = '{'; |
nothing calls this directly
no test coverage detected