PostgreSQL Source Code git master
Typedefs | Functions | Variables
explain.h File Reference
#include "executor/executor.h"
#include "parser/parse_node.h"
Include dependency graph for explain.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Typedefs

typedef struct ExplainState  ExplainState
 
typedef void(*  ExplainOneQuery_hook_type) (Query *query, int cursorOptions, IntoClause *into, ExplainState *es, const char *queryString, ParamListInfo params, QueryEnvironment *queryEnv)
 
typedef void(*  explain_per_plan_hook_type) (PlannedStmt *plannedstmt, IntoClause *into, ExplainState *es, const char *queryString, ParamListInfo params, QueryEnvironment *queryEnv)
 
typedef void(*  explain_per_node_hook_type) (PlanState *planstate, List *ancestors, const char *relationship, const char *plan_name, ExplainState *es)
 
typedef const char *(*  explain_get_index_name_hook_type) (Oid indexId)
 

Functions

 
void  standard_ExplainOneQuery (Query *query, int cursorOptions, IntoClause *into, ExplainState *es, const char *queryString, ParamListInfo params, QueryEnvironment *queryEnv)
 
 
void  ExplainOneUtility (Node *utilityStmt, IntoClause *into, ExplainState *es, ParseState *pstate, ParamListInfo params)
 
void  ExplainOnePlan (PlannedStmt *plannedstmt, IntoClause *into, ExplainState *es, const char *queryString, ParamListInfo params, QueryEnvironment *queryEnv, const instr_time *planduration, const BufferUsage *bufusage, const MemoryContextCounters *mem_counters)
 
void  ExplainPrintPlan (ExplainState *es, QueryDesc *queryDesc)
 
void  ExplainPrintTriggers (ExplainState *es, QueryDesc *queryDesc)
 
 
void  ExplainQueryText (ExplainState *es, QueryDesc *queryDesc)
 
void  ExplainQueryParameters (ExplainState *es, ParamListInfo params, int maxlen)
 

Variables

 
 
 
 

Typedef Documentation

explain_get_index_name_hook_type

typedef const char *(* explain_get_index_name_hook_type) (Oid indexId)

Definition at line 49 of file explain.h.

explain_per_node_hook_type

typedef void(* explain_per_node_hook_type) (PlanState *planstate, List *ancestors, const char *relationship, const char *plan_name, ExplainState *es)

Definition at line 41 of file explain.h.

explain_per_plan_hook_type

typedef void(* explain_per_plan_hook_type) (PlannedStmt *plannedstmt, IntoClause *into, ExplainState *es, const char *queryString, ParamListInfo params, QueryEnvironment *queryEnv)

Definition at line 32 of file explain.h.

ExplainOneQuery_hook_type

typedef void(* ExplainOneQuery_hook_type) (Query *query, int cursorOptions, IntoClause *into, ExplainState *es, const char *queryString, ParamListInfo params, QueryEnvironment *queryEnv)

Definition at line 22 of file explain.h.

ExplainState

typedef struct ExplainState ExplainState

Definition at line 19 of file explain.h.

Function Documentation

ExplainOnePlan()

void ExplainOnePlan ( PlannedStmtplannedstmt,
IntoClauseinto,
ExplainStatees,
const char *  queryString,
ParamListInfo  params,
QueryEnvironmentqueryEnv,
const instr_timeplanduration,
const BufferUsagebufusage,
const MemoryContextCountersmem_counters 
)

Definition at line 495 of file explain.c.

500{
502 QueryDesc *queryDesc;
503 instr_time starttime;
504 double totaltime = 0;
505 int eflags;
506 int instrument_option = 0;
507 SerializeMetrics serializeMetrics = {0};
508
509 Assert(plannedstmt->commandType != CMD_UTILITY);
510
511 if (es->analyze && es->timing)
512 instrument_option |= INSTRUMENT_TIMER;
513 else if (es->analyze)
514 instrument_option |= INSTRUMENT_ROWS;
515
516 if (es->buffers)
517 instrument_option |= INSTRUMENT_BUFFERS;
518 if (es->wal)
519 instrument_option |= INSTRUMENT_WAL;
520
521 /*
522 * We always collect timing for the entire statement, even when node-level
523 * timing is off, so we don't look at es->timing here. (We could skip
524 * this if !es->summary, but it's hardly worth the complication.)
525 */
526 INSTR_TIME_SET_CURRENT(starttime);
527
528 /*
529 * Use a snapshot with an updated command ID to ensure this query sees
530 * results of any previously executed queries.
531 */
534
535 /*
536 * We discard the output if we have no use for it. If we're explaining
537 * CREATE TABLE AS, we'd better use the appropriate tuple receiver, while
538 * the SERIALIZE option requires its own tuple receiver. (If you specify
539 * SERIALIZE while explaining CREATE TABLE AS, you'll see zeroes for the
540 * results, which is appropriate since no data would have gone to the
541 * client.)
542 */
543 if (into)
545 else if (es->serialize != EXPLAIN_SERIALIZE_NONE)
547 else
549
550 /* Create a QueryDesc for the query */
551 queryDesc = CreateQueryDesc(plannedstmt, queryString,
553 dest, params, queryEnv, instrument_option);
554
555 /* Select execution options */
556 if (es->analyze)
557 eflags = 0; /* default run-to-completion flags */
558 else
559 eflags = EXEC_FLAG_EXPLAIN_ONLY;
560 if (es->generic)
562 if (into)
563 eflags |= GetIntoRelEFlags(into);
564
565 /* call ExecutorStart to prepare the plan for execution */
566 ExecutorStart(queryDesc, eflags);
567
568 /* Execute the plan for statistics if asked for */
569 if (es->analyze)
570 {
571 ScanDirection dir;
572
573 /* EXPLAIN ANALYZE CREATE TABLE AS WITH NO DATA is weird */
574 if (into && into->skipData)
576 else
578
579 /* run the plan */
580 ExecutorRun(queryDesc, dir, 0);
581
582 /* run cleanup too */
583 ExecutorFinish(queryDesc);
584
585 /* We can't run ExecutorEnd 'till we're done printing the stats... */
586 totaltime += elapsed_time(&starttime);
587 }
588
589 /* grab serialization metrics before we destroy the DestReceiver */
591 serializeMetrics = GetSerializationMetrics(dest);
592
593 /* call the DestReceiver's destroy method even during explain */
594 dest->rDestroy(dest);
595
596 ExplainOpenGroup("Query", NULL, true, es);
597
598 /* Create textual dump of plan tree */
599 ExplainPrintPlan(es, queryDesc);
600
601 /* Show buffer and/or memory usage in planning */
602 if (peek_buffer_usage(es, bufusage) || mem_counters)
603 {
604 ExplainOpenGroup("Planning", "Planning", true, es);
605
606 if (es->format == EXPLAIN_FORMAT_TEXT)
607 {
609 appendStringInfoString(es->str, "Planning:\n");
610 es->indent++;
611 }
612
613 if (bufusage)
614 show_buffer_usage(es, bufusage);
615
616 if (mem_counters)
617 show_memory_counters(es, mem_counters);
618
619 if (es->format == EXPLAIN_FORMAT_TEXT)
620 es->indent--;
621
622 ExplainCloseGroup("Planning", "Planning", true, es);
623 }
624
625 if (es->summary && planduration)
626 {
627 double plantime = INSTR_TIME_GET_DOUBLE(*planduration);
628
629 ExplainPropertyFloat("Planning Time", "ms", 1000.0 * plantime, 3, es);
630 }
631
632 /* Print info about runtime of triggers */
633 if (es->analyze)
634 ExplainPrintTriggers(es, queryDesc);
635
636 /*
637 * Print info about JITing. Tied to es->costs because we don't want to
638 * display this in regression tests, as it'd cause output differences
639 * depending on build options. Might want to separate that out from COSTS
640 * at a later stage.
641 */
642 if (es->costs)
643 ExplainPrintJITSummary(es, queryDesc);
644
645 /* Print info about serialization of output */
647 ExplainPrintSerialize(es, &serializeMetrics);
648
649 /* Allow plugins to print additional information */
651 (*explain_per_plan_hook) (plannedstmt, into, es, queryString,
652 params, queryEnv);
653
654 /*
655 * Close down the query and free resources. Include time for this in the
656 * total execution time (although it should be pretty minimal).
657 */
658 INSTR_TIME_SET_CURRENT(starttime);
659
660 ExecutorEnd(queryDesc);
661
662 FreeQueryDesc(queryDesc);
663
665
666 /* We need a CCI just in case query expanded to multiple plans */
667 if (es->analyze)
669
670 totaltime += elapsed_time(&starttime);
671
672 /*
673 * We only report execution time if we actually ran the query (that is,
674 * the user specified ANALYZE), and if summary reporting is enabled (the
675 * user can set SUMMARY OFF to not have the timing information included in
676 * the output). By default, ANALYZE sets SUMMARY to true.
677 */
678 if (es->summary && es->analyze)
679 ExplainPropertyFloat("Execution Time", "ms", 1000.0 * totaltime, 3,
680 es);
681
682 ExplainCloseGroup("Query", NULL, true, es);
683}
int GetIntoRelEFlags(IntoClause *intoClause)
Definition: createas.c:375
DestReceiver * CreateIntoRelDestReceiver(IntoClause *intoClause)
Definition: createas.c:440
DestReceiver * None_Receiver
Definition: dest.c:96
void ExecutorEnd(QueryDesc *queryDesc)
Definition: execMain.c:466
void ExecutorFinish(QueryDesc *queryDesc)
Definition: execMain.c:406
void ExecutorStart(QueryDesc *queryDesc, int eflags)
Definition: execMain.c:122
void ExecutorRun(QueryDesc *queryDesc, ScanDirection direction, uint64 count)
Definition: execMain.c:297
#define EXEC_FLAG_EXPLAIN_GENERIC
Definition: executor.h:67
#define EXEC_FLAG_EXPLAIN_ONLY
Definition: executor.h:66
static bool peek_buffer_usage(ExplainState *es, const BufferUsage *usage)
Definition: explain.c:4071
void ExplainPrintJITSummary(ExplainState *es, QueryDesc *queryDesc)
Definition: explain.c:876
static double elapsed_time(instr_time *starttime)
Definition: explain.c:1164
explain_per_plan_hook_type explain_per_plan_hook
Definition: explain.c:56
static void show_memory_counters(ExplainState *es, const MemoryContextCounters *mem_counters)
Definition: explain.c:4323
void ExplainPrintPlan(ExplainState *es, QueryDesc *queryDesc)
Definition: explain.c:760
static void ExplainPrintSerialize(ExplainState *es, SerializeMetrics *metrics)
Definition: explain.c:1000
void ExplainPrintTriggers(ExplainState *es, QueryDesc *queryDesc)
Definition: explain.c:833
static void show_buffer_usage(ExplainState *es, const BufferUsage *usage)
Definition: explain.c:4111
SerializeMetrics GetSerializationMetrics(DestReceiver *dest)
Definition: explain_dr.c:300
DestReceiver * CreateExplainSerializeDestReceiver(ExplainState *es)
Definition: explain_dr.c:275
void ExplainOpenGroup(const char *objtype, const char *labelname, bool labeled, ExplainState *es)
void ExplainIndentText(ExplainState *es)
void ExplainPropertyFloat(const char *qlabel, const char *unit, double value, int ndigits, ExplainState *es)
void ExplainCloseGroup(const char *objtype, const char *labelname, bool labeled, ExplainState *es)
@ EXPLAIN_SERIALIZE_NONE
Definition: explain_state.h:22
@ EXPLAIN_FORMAT_TEXT
Definition: explain_state.h:29
Assert(PointerIsAligned(start, uint64))
#define INSTR_TIME_SET_CURRENT(t)
Definition: instr_time.h:122
#define INSTR_TIME_GET_DOUBLE(t)
Definition: instr_time.h:188
@ INSTRUMENT_TIMER
Definition: instrument.h:62
@ INSTRUMENT_BUFFERS
Definition: instrument.h:63
@ INSTRUMENT_WAL
Definition: instrument.h:65
@ INSTRUMENT_ROWS
Definition: instrument.h:64
@ CMD_UTILITY
Definition: nodes.h:280
void FreeQueryDesc(QueryDesc *qdesc)
Definition: pquery.c:106
QueryDesc * CreateQueryDesc(PlannedStmt *plannedstmt, const char *sourceText, Snapshot snapshot, Snapshot crosscheck_snapshot, DestReceiver *dest, ParamListInfo params, QueryEnvironment *queryEnv, int instrument_options)
Definition: pquery.c:68
ScanDirection
Definition: sdir.h:25
@ NoMovementScanDirection
Definition: sdir.h:27
@ ForwardScanDirection
Definition: sdir.h:28
void UpdateActiveSnapshotCommandId(void)
Definition: snapmgr.c:742
void PopActiveSnapshot(void)
Definition: snapmgr.c:773
void PushCopiedSnapshot(Snapshot snapshot)
Definition: snapmgr.c:730
Snapshot GetActiveSnapshot(void)
Definition: snapmgr.c:798
#define InvalidSnapshot
Definition: snapshot.h:119
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:230
bool analyze
Definition: explain_state.h:49
StringInfo str
Definition: explain_state.h:46
ExplainFormat format
Definition: explain_state.h:59
bool generic
Definition: explain_state.h:57
bool summary
Definition: explain_state.h:54
ExplainSerializeOption serialize
Definition: explain_state.h:58
bool buffers
Definition: explain_state.h:51
bool skipData
Definition: primnodes.h:171
CmdType commandType
Definition: plannodes.h:68
void CommandCounterIncrement(void)
Definition: xact.c:1100

References ExplainState::analyze, appendStringInfoString(), Assert(), ExplainState::buffers, CMD_UTILITY, CommandCounterIncrement(), PlannedStmt::commandType, ExplainState::costs, CreateExplainSerializeDestReceiver(), CreateIntoRelDestReceiver(), CreateQueryDesc(), generate_unaccent_rules::dest, elapsed_time(), EXEC_FLAG_EXPLAIN_GENERIC, EXEC_FLAG_EXPLAIN_ONLY, ExecutorEnd(), ExecutorFinish(), ExecutorRun(), ExecutorStart(), EXPLAIN_FORMAT_TEXT, explain_per_plan_hook, EXPLAIN_SERIALIZE_NONE, ExplainCloseGroup(), ExplainIndentText(), ExplainOpenGroup(), ExplainPrintJITSummary(), ExplainPrintPlan(), ExplainPrintSerialize(), ExplainPrintTriggers(), ExplainPropertyFloat(), ExplainState::format, ForwardScanDirection, FreeQueryDesc(), ExplainState::generic, GetActiveSnapshot(), GetIntoRelEFlags(), GetSerializationMetrics(), ExplainState::indent, INSTR_TIME_GET_DOUBLE, INSTR_TIME_SET_CURRENT, INSTRUMENT_BUFFERS, INSTRUMENT_ROWS, INSTRUMENT_TIMER, INSTRUMENT_WAL, InvalidSnapshot, NoMovementScanDirection, None_Receiver, peek_buffer_usage(), PopActiveSnapshot(), PushCopiedSnapshot(), ExplainState::serialize, show_buffer_usage(), show_memory_counters(), IntoClause::skipData, ExplainState::str, ExplainState::summary, ExplainState::timing, UpdateActiveSnapshotCommandId(), and ExplainState::wal.

Referenced by ExplainExecuteQuery(), and standard_ExplainOneQuery().

ExplainOneUtility()

void ExplainOneUtility ( NodeutilityStmt,
IntoClauseinto,
ExplainStatees,
ParseStatepstate,
ParamListInfo  params 
)

Definition at line 391 of file explain.c.

393{
394 if (utilityStmt == NULL)
395 return;
396
397 if (IsA(utilityStmt, CreateTableAsStmt))
398 {
399 /*
400 * We have to rewrite the contained SELECT and then pass it back to
401 * ExplainOneQuery. Copy to be safe in the EXPLAIN EXECUTE case.
402 */
403 CreateTableAsStmt *ctas = (CreateTableAsStmt *) utilityStmt;
404 Query *ctas_query;
405 List *rewritten;
406 JumbleState *jstate = NULL;
407
408 /*
409 * Check if the relation exists or not. This is done at this stage to
410 * avoid query planning or execution.
411 */
412 if (CreateTableAsRelExists(ctas))
413 {
414 if (ctas->objtype == OBJECT_TABLE)
415 ExplainDummyGroup("CREATE TABLE AS", NULL, es);
416 else if (ctas->objtype == OBJECT_MATVIEW)
417 ExplainDummyGroup("CREATE MATERIALIZED VIEW", NULL, es);
418 else
419 elog(ERROR, "unexpected object type: %d",
420 (int) ctas->objtype);
421 return;
422 }
423
424 ctas_query = castNode(Query, copyObject(ctas->query));
425 if (IsQueryIdEnabled())
426 jstate = JumbleQuery(ctas_query);
428 (*post_parse_analyze_hook) (pstate, ctas_query, jstate);
429 rewritten = QueryRewrite(ctas_query);
430 Assert(list_length(rewritten) == 1);
432 CURSOR_OPT_PARALLEL_OK, ctas->into, es,
433 pstate, params);
434 }
435 else if (IsA(utilityStmt, DeclareCursorStmt))
436 {
437 /*
438 * Likewise for DECLARE CURSOR.
439 *
440 * Notice that if you say EXPLAIN ANALYZE DECLARE CURSOR then we'll
441 * actually run the query. This is different from pre-8.3 behavior
442 * but seems more useful than not running the query. No cursor will
443 * be created, however.
444 */
445 DeclareCursorStmt *dcs = (DeclareCursorStmt *) utilityStmt;
446 Query *dcs_query;
447 List *rewritten;
448 JumbleState *jstate = NULL;
449
450 dcs_query = castNode(Query, copyObject(dcs->query));
451 if (IsQueryIdEnabled())
452 jstate = JumbleQuery(dcs_query);
454 (*post_parse_analyze_hook) (pstate, dcs_query, jstate);
455
456 rewritten = QueryRewrite(dcs_query);
457 Assert(list_length(rewritten) == 1);
459 dcs->options, NULL, es,
460 pstate, params);
461 }
462 else if (IsA(utilityStmt, ExecuteStmt))
463 ExplainExecuteQuery((ExecuteStmt *) utilityStmt, into, es,
464 pstate, params);
465 else if (IsA(utilityStmt, NotifyStmt))
466 {
467 if (es->format == EXPLAIN_FORMAT_TEXT)
468 appendStringInfoString(es->str, "NOTIFY\n");
469 else
470 ExplainDummyGroup("Notify", NULL, es);
471 }
472 else
473 {
474 if (es->format == EXPLAIN_FORMAT_TEXT)
476 "Utility statements have no plan structure\n");
477 else
478 ExplainDummyGroup("Utility Statement", NULL, es);
479 }
480}
void ExplainExecuteQuery(ExecuteStmt *execstmt, IntoClause *into, ExplainState *es, ParseState *pstate, ParamListInfo params)
Definition: prepare.c:571
bool CreateTableAsRelExists(CreateTableAsStmt *ctas)
Definition: createas.c:393
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:226
static void ExplainOneQuery(Query *query, int cursorOptions, IntoClause *into, ExplainState *es, ParseState *pstate, ParamListInfo params)
Definition: explain.c:294
void ExplainDummyGroup(const char *objtype, const char *labelname, ExplainState *es)
#define IsA(nodeptr, _type_)
Definition: nodes.h:164
#define copyObject(obj)
Definition: nodes.h:232
#define castNode(_type_, nodeptr)
Definition: nodes.h:182
@ OBJECT_MATVIEW
Definition: parsenodes.h:2348
@ OBJECT_TABLE
Definition: parsenodes.h:2366
#define CURSOR_OPT_PARALLEL_OK
Definition: parsenodes.h:3396
post_parse_analyze_hook_type post_parse_analyze_hook
Definition: analyze.c:67
static int list_length(const List *l)
Definition: pg_list.h:152
#define linitial_node(type, l)
Definition: pg_list.h:181
static bool IsQueryIdEnabled(void)
Definition: queryjumble.h:104
JumbleState * JumbleQuery(Query *query)
List * QueryRewrite(Query *parsetree)
IntoClause * into
Definition: parsenodes.h:4028
ObjectType objtype
Definition: parsenodes.h:4029
Definition: pg_list.h:54
Definition: parsenodes.h:118

References appendStringInfoString(), Assert(), castNode, copyObject, CreateTableAsRelExists(), CURSOR_OPT_PARALLEL_OK, elog, ERROR, EXPLAIN_FORMAT_TEXT, ExplainDummyGroup(), ExplainExecuteQuery(), ExplainOneQuery(), ExplainState::format, CreateTableAsStmt::into, IsA, IsQueryIdEnabled(), JumbleQuery(), linitial_node, list_length(), OBJECT_MATVIEW, OBJECT_TABLE, CreateTableAsStmt::objtype, DeclareCursorStmt::options, post_parse_analyze_hook, DeclareCursorStmt::query, CreateTableAsStmt::query, QueryRewrite(), and ExplainState::str.

Referenced by ExplainExecuteQuery(), and ExplainOneQuery().

ExplainPrintJITSummary()

void ExplainPrintJITSummary ( ExplainStatees,
QueryDescqueryDesc 
)

Definition at line 876 of file explain.c.

877{
878 JitInstrumentation ji = {0};
879
880 if (!(queryDesc->estate->es_jit_flags & PGJIT_PERFORM))
881 return;
882
883 /*
884 * Work with a copy instead of modifying the leader state, since this
885 * function may be called twice
886 */
887 if (queryDesc->estate->es_jit)
888 InstrJitAgg(&ji, &queryDesc->estate->es_jit->instr);
889
890 /* If this process has done JIT in parallel workers, merge stats */
891 if (queryDesc->estate->es_jit_worker_instr)
892 InstrJitAgg(&ji, queryDesc->estate->es_jit_worker_instr);
893
894 ExplainPrintJIT(es, queryDesc->estate->es_jit_flags, &ji);
895}
static void ExplainPrintJIT(ExplainState *es, int jit_flags, JitInstrumentation *ji)
Definition: explain.c:902
void InstrJitAgg(JitInstrumentation *dst, JitInstrumentation *add)
Definition: jit.c:182
#define PGJIT_PERFORM
Definition: jit.h:20
struct JitContext * es_jit
Definition: execnodes.h:764
struct JitInstrumentation * es_jit_worker_instr
Definition: execnodes.h:765
int es_jit_flags
Definition: execnodes.h:763
JitInstrumentation instr
Definition: jit.h:62
EState * estate
Definition: execdesc.h:48

References EState::es_jit, EState::es_jit_flags, EState::es_jit_worker_instr, QueryDesc::estate, ExplainPrintJIT(), JitContext::instr, InstrJitAgg(), and PGJIT_PERFORM.

Referenced by explain_ExecutorEnd(), and ExplainOnePlan().

ExplainPrintPlan()

void ExplainPrintPlan ( ExplainStatees,
QueryDescqueryDesc 
)

Definition at line 760 of file explain.c.

761{
762 Bitmapset *rels_used = NULL;
763 PlanState *ps;
764 ListCell *lc;
765
766 /* Set up ExplainState fields associated with this plan tree */
767 Assert(queryDesc->plannedstmt != NULL);
768 es->pstmt = queryDesc->plannedstmt;
769 es->rtable = queryDesc->plannedstmt->rtable;
770 ExplainPreScanNode(queryDesc->planstate, &rels_used);
773 es->rtable_names);
774 es->printed_subplans = NULL;
775 es->rtable_size = list_length(es->rtable);
776 foreach(lc, es->rtable)
777 {
779
780 if (rte->rtekind == RTE_GROUP)
781 {
782 es->rtable_size--;
783 break;
784 }
785 }
786
787 /*
788 * Sometimes we mark a Gather node as "invisible", which means that it's
789 * not to be displayed in EXPLAIN output. The purpose of this is to allow
790 * running regression tests with debug_parallel_query=regress to get the
791 * same results as running the same tests with debug_parallel_query=off.
792 * Such marking is currently only supported on a Gather at the top of the
793 * plan. We skip that node, and we must also hide per-worker detail data
794 * further down in the plan tree.
795 */
796 ps = queryDesc->planstate;
797 if (IsA(ps, GatherState) && ((Gather *) ps->plan)->invisible)
798 {
800 es->hide_workers = true;
801 }
802 ExplainNode(ps, NIL, NULL, NULL, es);
803
804 /*
805 * If requested, include information about GUC parameters with values that
806 * don't match the built-in defaults.
807 */
809
810 /*
811 * COMPUTE_QUERY_ID_REGRESS means COMPUTE_QUERY_ID_AUTO, but we don't show
812 * the queryid in any of the EXPLAIN plans to keep stable the results
813 * generated by regression test suites.
814 */
815 if (es->verbose && queryDesc->plannedstmt->queryId != INT64CONST(0) &&
817 {
818 ExplainPropertyInteger("Query Identifier", NULL,
819 queryDesc->plannedstmt->queryId, es);
820 }
821}
#define INT64CONST(x)
Definition: c.h:552
#define outerPlanState(node)
Definition: execnodes.h:1255
static void ExplainNode(PlanState *planstate, List *ancestors, const char *relationship, const char *plan_name, ExplainState *es)
Definition: explain.c:1356
static bool ExplainPreScanNode(PlanState *planstate, Bitmapset **rels_used)
Definition: explain.c:1183
static void ExplainPrintSettings(ExplainState *es)
Definition: explain.c:690
void ExplainPropertyInteger(const char *qlabel, const char *unit, int64 value, ExplainState *es)
struct parser_state ps
@ RTE_GROUP
Definition: parsenodes.h:1054
#define lfirst_node(type, lc)
Definition: pg_list.h:176
#define NIL
Definition: pg_list.h:68
@ COMPUTE_QUERY_ID_REGRESS
Definition: queryjumble.h:86
int compute_query_id
List * deparse_context_for_plan_tree(PlannedStmt *pstmt, List *rtable_names)
Definition: ruleutils.c:3752
List * select_rtable_names_for_explain(List *rtable, Bitmapset *rels_used)
Definition: ruleutils.c:3854
bool verbose
Definition: explain_state.h:48
int rtable_size
Definition: explain_state.h:70
Bitmapset * printed_subplans
Definition: explain_state.h:68
bool hide_workers
Definition: explain_state.h:69
List * rtable_names
Definition: explain_state.h:66
List * rtable
Definition: explain_state.h:65
PlannedStmt * pstmt
Definition: explain_state.h:64
List * deparse_cxt
Definition: explain_state.h:67
int64 queryId
Definition: plannodes.h:71
List * rtable
Definition: plannodes.h:109
PlannedStmt * plannedstmt
Definition: execdesc.h:37
PlanState * planstate
Definition: execdesc.h:49
RTEKind rtekind
Definition: parsenodes.h:1078
Definition: pg_list.h:46

References Assert(), compute_query_id, COMPUTE_QUERY_ID_REGRESS, deparse_context_for_plan_tree(), ExplainState::deparse_cxt, ExplainNode(), ExplainPreScanNode(), ExplainPrintSettings(), ExplainPropertyInteger(), ExplainState::hide_workers, INT64CONST, IsA, lfirst_node, list_length(), NIL, outerPlanState, QueryDesc::plannedstmt, QueryDesc::planstate, ExplainState::printed_subplans, ps, ExplainState::pstmt, PlannedStmt::queryId, ExplainState::rtable, PlannedStmt::rtable, ExplainState::rtable_names, ExplainState::rtable_size, RTE_GROUP, RangeTblEntry::rtekind, select_rtable_names_for_explain(), and ExplainState::verbose.

Referenced by explain_ExecutorEnd(), and ExplainOnePlan().

ExplainPrintTriggers()

void ExplainPrintTriggers ( ExplainStatees,
QueryDescqueryDesc 
)

Definition at line 833 of file explain.c.

834{
835 ResultRelInfo *rInfo;
836 bool show_relname;
837 List *resultrels;
838 List *routerels;
839 List *targrels;
840 ListCell *l;
841
842 resultrels = queryDesc->estate->es_opened_result_relations;
843 routerels = queryDesc->estate->es_tuple_routing_result_relations;
844 targrels = queryDesc->estate->es_trig_target_relations;
845
846 ExplainOpenGroup("Triggers", "Triggers", false, es);
847
848 show_relname = (list_length(resultrels) > 1 ||
849 routerels != NIL || targrels != NIL);
850 foreach(l, resultrels)
851 {
852 rInfo = (ResultRelInfo *) lfirst(l);
853 report_triggers(rInfo, show_relname, es);
854 }
855
856 foreach(l, routerels)
857 {
858 rInfo = (ResultRelInfo *) lfirst(l);
859 report_triggers(rInfo, show_relname, es);
860 }
861
862 foreach(l, targrels)
863 {
864 rInfo = (ResultRelInfo *) lfirst(l);
865 report_triggers(rInfo, show_relname, es);
866 }
867
868 ExplainCloseGroup("Triggers", "Triggers", false, es);
869}
static void report_triggers(ResultRelInfo *rInfo, bool show_relname, ExplainState *es)
Definition: explain.c:1093
#define lfirst(lc)
Definition: pg_list.h:172
List * es_tuple_routing_result_relations
Definition: execnodes.h:698
List * es_trig_target_relations
Definition: execnodes.h:701
List * es_opened_result_relations
Definition: execnodes.h:688

References EState::es_opened_result_relations, EState::es_trig_target_relations, EState::es_tuple_routing_result_relations, QueryDesc::estate, ExplainCloseGroup(), ExplainOpenGroup(), lfirst, list_length(), NIL, and report_triggers().

Referenced by explain_ExecutorEnd(), and ExplainOnePlan().

ExplainQuery()

void ExplainQuery ( ParseStatepstate,
ExplainStmtstmt,
ParamListInfo  params,
DestReceiverdest 
)

Definition at line 177 of file explain.c.

179{
181 TupOutputState *tstate;
182 JumbleState *jstate = NULL;
183 Query *query;
184 List *rewritten;
185
186 /* Configure the ExplainState based on the provided options */
187 ParseExplainOptionList(es, stmt->options, pstate);
188
189 /* Extract the query and, if enabled, jumble it */
190 query = castNode(Query, stmt->query);
191 if (IsQueryIdEnabled())
192 jstate = JumbleQuery(query);
193
195 (*post_parse_analyze_hook) (pstate, query, jstate);
196
197 /*
198 * Parse analysis was done already, but we still have to run the rule
199 * rewriter. We do not do AcquireRewriteLocks: we assume the query either
200 * came straight from the parser, or suitable locks were acquired by
201 * plancache.c.
202 */
203 rewritten = QueryRewrite(castNode(Query, stmt->query));
204
205 /* emit opening boilerplate */
207
208 if (rewritten == NIL)
209 {
210 /*
211 * In the case of an INSTEAD NOTHING, tell at least that. But in
212 * non-text format, the output is delimited, so this isn't necessary.
213 */
214 if (es->format == EXPLAIN_FORMAT_TEXT)
215 appendStringInfoString(es->str, "Query rewrites to nothing\n");
216 }
217 else
218 {
219 ListCell *l;
220
221 /* Explain every plan */
222 foreach(l, rewritten)
223 {
225 CURSOR_OPT_PARALLEL_OK, NULL, es,
226 pstate, params);
227
228 /* Separate plans with an appropriate separator */
229 if (lnext(rewritten, l) != NULL)
231 }
232 }
233
234 /* emit closing boilerplate */
236 Assert(es->indent == 0);
237
238 /* output tuples */
241 if (es->format == EXPLAIN_FORMAT_TEXT)
242 do_text_output_multiline(tstate, es->str->data);
243 else
244 do_text_output_oneline(tstate, es->str->data);
245 end_tup_output(tstate);
246
247 pfree(es->str->data);
248}
const TupleTableSlotOps TTSOpsVirtual
Definition: execTuples.c:84
void end_tup_output(TupOutputState *tstate)
Definition: execTuples.c:2522
void do_text_output_multiline(TupOutputState *tstate, const char *txt)
Definition: execTuples.c:2492
TupOutputState * begin_tup_output_tupdesc(DestReceiver *dest, TupleDesc tupdesc, const TupleTableSlotOps *tts_ops)
Definition: execTuples.c:2444
#define do_text_output_oneline(tstate, str_to_emit)
Definition: executor.h:625
TupleDesc ExplainResultDesc(ExplainStmt *stmt)
Definition: explain.c:255
void ExplainSeparatePlans(ExplainState *es)
void ExplainEndOutput(ExplainState *es)
void ExplainBeginOutput(ExplainState *es)
ExplainState * NewExplainState(void)
Definition: explain_state.c:61
void ParseExplainOptionList(ExplainState *es, List *options, ParseState *pstate)
Definition: explain_state.c:77
#define stmt
Definition: indent_codes.h:59
void pfree(void *pointer)
Definition: mcxt.c:1594
static ListCell * lnext(const List *l, const ListCell *c)
Definition: pg_list.h:343
char * data
Definition: stringinfo.h:48

References appendStringInfoString(), Assert(), begin_tup_output_tupdesc(), castNode, CURSOR_OPT_PARALLEL_OK, StringInfoData::data, generate_unaccent_rules::dest, do_text_output_multiline(), do_text_output_oneline, end_tup_output(), EXPLAIN_FORMAT_TEXT, ExplainBeginOutput(), ExplainEndOutput(), ExplainOneQuery(), ExplainResultDesc(), ExplainSeparatePlans(), ExplainState::format, ExplainState::indent, IsQueryIdEnabled(), JumbleQuery(), lfirst_node, lnext(), NewExplainState(), NIL, ParseExplainOptionList(), pfree(), post_parse_analyze_hook, QueryRewrite(), stmt, ExplainState::str, and TTSOpsVirtual.

Referenced by standard_ProcessUtility().

ExplainQueryParameters()

void ExplainQueryParameters ( ExplainStatees,
ParamListInfo  params,
int  maxlen 
)

Definition at line 1075 of file explain.c.

1076{
1077 char *str;
1078
1079 /* This check is consistent with errdetail_params() */
1080 if (params == NULL || params->numParams <= 0 || maxlen == 0)
1081 return;
1082
1083 str = BuildParamLogString(params, NULL, maxlen);
1084 if (str && str[0] != '0円')
1085 ExplainPropertyText("Query Parameters", str, es);
1086}
void ExplainPropertyText(const char *qlabel, const char *value, ExplainState *es)
const char * str
char * BuildParamLogString(ParamListInfo params, char **knownTextValues, int maxlen)
Definition: params.c:335
int numParams
Definition: params.h:118

References BuildParamLogString(), ExplainPropertyText(), ParamListInfoData::numParams, and str.

Referenced by explain_ExecutorEnd().

ExplainQueryText()

void ExplainQueryText ( ExplainStatees,
QueryDescqueryDesc 
)

Definition at line 1060 of file explain.c.

1061{
1062 if (queryDesc->sourceText)
1063 ExplainPropertyText("Query Text", queryDesc->sourceText, es);
1064}
const char * sourceText
Definition: execdesc.h:38

References ExplainPropertyText(), and QueryDesc::sourceText.

Referenced by explain_ExecutorEnd().

ExplainResultDesc()

TupleDesc ExplainResultDesc ( ExplainStmtstmt )

Definition at line 255 of file explain.c.

256{
257 TupleDesc tupdesc;
258 ListCell *lc;
259 Oid result_type = TEXTOID;
260
261 /* Check for XML format option */
262 foreach(lc, stmt->options)
263 {
264 DefElem *opt = (DefElem *) lfirst(lc);
265
266 if (strcmp(opt->defname, "format") == 0)
267 {
268 char *p = defGetString(opt);
269
270 if (strcmp(p, "xml") == 0)
271 result_type = XMLOID;
272 else if (strcmp(p, "json") == 0)
273 result_type = JSONOID;
274 else
275 result_type = TEXTOID;
276 /* don't "break", as ExplainQuery will use the last value */
277 }
278 }
279
280 /* Need a tuple descriptor representing a single TEXT or XML column */
281 tupdesc = CreateTemplateTupleDesc(1);
282 TupleDescInitEntry(tupdesc, (AttrNumber) 1, "QUERY PLAN",
283 result_type, -1, 0);
284 return tupdesc;
285}
int16 AttrNumber
Definition: attnum.h:21
char * defGetString(DefElem *def)
Definition: define.c:35
unsigned int Oid
Definition: postgres_ext.h:32
char * defname
Definition: parsenodes.h:843
TupleDesc CreateTemplateTupleDesc(int natts)
Definition: tupdesc.c:182
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition: tupdesc.c:842

References CreateTemplateTupleDesc(), defGetString(), DefElem::defname, lfirst, stmt, and TupleDescInitEntry().

Referenced by ExplainQuery(), and UtilityTupleDescriptor().

standard_ExplainOneQuery()

void standard_ExplainOneQuery ( Queryquery,
int  cursorOptions,
IntoClauseinto,
ExplainStatees,
const char *  queryString,
ParamListInfo  params,
QueryEnvironmentqueryEnv 
)

Definition at line 319 of file explain.c.

323{
325 instr_time planstart,
326 planduration;
327 BufferUsage bufusage_start,
328 bufusage;
329 MemoryContextCounters mem_counters;
330 MemoryContext planner_ctx = NULL;
331 MemoryContext saved_ctx = NULL;
332
333 if (es->memory)
334 {
335 /*
336 * Create a new memory context to measure planner's memory consumption
337 * accurately. Note that if the planner were to be modified to use a
338 * different memory context type, here we would be changing that to
339 * AllocSet, which might be undesirable. However, we don't have a way
340 * to create a context of the same type as another, so we pray and
341 * hope that this is OK.
342 */
344 "explain analyze planner context",
346 saved_ctx = MemoryContextSwitchTo(planner_ctx);
347 }
348
349 if (es->buffers)
350 bufusage_start = pgBufferUsage;
351 INSTR_TIME_SET_CURRENT(planstart);
352
353 /* plan the query */
354 plan = pg_plan_query(query, queryString, cursorOptions, params);
355
356 INSTR_TIME_SET_CURRENT(planduration);
357 INSTR_TIME_SUBTRACT(planduration, planstart);
358
359 if (es->memory)
360 {
361 MemoryContextSwitchTo(saved_ctx);
362 MemoryContextMemConsumed(planner_ctx, &mem_counters);
363 }
364
365 /* calc differences of buffer counters. */
366 if (es->buffers)
367 {
368 memset(&bufusage, 0, sizeof(BufferUsage));
369 BufferUsageAccumDiff(&bufusage, &pgBufferUsage, &bufusage_start);
370 }
371
372 /* run it (if needed) and produce output */
373 ExplainOnePlan(plan, into, es, queryString, params, queryEnv,
374 &planduration, (es->buffers ? &bufusage : NULL),
375 es->memory ? &mem_counters : NULL);
376}
void ExplainOnePlan(PlannedStmt *plannedstmt, IntoClause *into, ExplainState *es, const char *queryString, ParamListInfo params, QueryEnvironment *queryEnv, const instr_time *planduration, const BufferUsage *bufusage, const MemoryContextCounters *mem_counters)
Definition: explain.c:495
#define INSTR_TIME_SUBTRACT(x, y)
Definition: instr_time.h:181
BufferUsage pgBufferUsage
Definition: instrument.c:20
void BufferUsageAccumDiff(BufferUsage *dst, const BufferUsage *add, const BufferUsage *sub)
Definition: instrument.c:248
void MemoryContextMemConsumed(MemoryContext context, MemoryContextCounters *consumed)
Definition: mcxt.c:832
MemoryContext CurrentMemoryContext
Definition: mcxt.c:160
#define AllocSetContextCreate
Definition: memutils.h:129
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:160
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:124
#define plan(x)
Definition: pg_regress.c:161
PlannedStmt * pg_plan_query(Query *querytree, const char *query_string, int cursorOptions, ParamListInfo boundParams)
Definition: postgres.c:886

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, ExplainState::buffers, BufferUsageAccumDiff(), CurrentMemoryContext, ExplainOnePlan(), INSTR_TIME_SET_CURRENT, INSTR_TIME_SUBTRACT, ExplainState::memory, MemoryContextMemConsumed(), MemoryContextSwitchTo(), pg_plan_query(), pgBufferUsage, and plan.

Referenced by ExplainOneQuery().

Variable Documentation

explain_get_index_name_hook

PGDLLIMPORT explain_get_index_name_hook_type explain_get_index_name_hook
extern

Definition at line 53 of file explain.c.

Referenced by explain_get_index_name().

explain_per_node_hook

PGDLLIMPORT explain_per_node_hook_type explain_per_node_hook
extern

Definition at line 57 of file explain.c.

Referenced by _PG_init(), and ExplainNode().

explain_per_plan_hook

PGDLLIMPORT explain_per_plan_hook_type explain_per_plan_hook
extern

Definition at line 56 of file explain.c.

Referenced by _PG_init(), and ExplainOnePlan().

ExplainOneQuery_hook

PGDLLIMPORT ExplainOneQuery_hook_type ExplainOneQuery_hook
extern

Definition at line 50 of file explain.c.

Referenced by ExplainOneQuery().

AltStyle によって変換されたページ (->オリジナル) /