PostgreSQL Source Code git master
Loading...
Searching...
No Matches
explain.h File Reference
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 ExplainQuery (ParseState *pstate, ExplainStmt *stmt, ParamListInfo params, DestReceiver *dest)
 
void standard_ExplainOneQuery (Query *query, int cursorOptions, IntoClause *into, ExplainState *es, const char *queryString, ParamListInfo params, QueryEnvironment *queryEnv)
 
TupleDesc ExplainResultDesc (ExplainStmt *stmt)
 
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 ExplainPrintJITSummary (ExplainState *es, QueryDesc *queryDesc)
 
void ExplainQueryText (ExplainState *es, QueryDesc *queryDesc)
 
void ExplainQueryParameters (ExplainState *es, ParamListInfo params, int maxlen)
 

Variables

PGDLLIMPORT ExplainOneQuery_hook_type ExplainOneQuery_hook
 
PGDLLIMPORT explain_per_plan_hook_type explain_per_plan_hook
 
PGDLLIMPORT explain_per_node_hook_type explain_per_node_hook
 
PGDLLIMPORT explain_get_index_name_hook_type explain_get_index_name_hook
 

Typedef Documentation

◆ explain_get_index_name_hook_type

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

Definition at line 50 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 42 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 33 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 23 of file explain.h.

◆ ExplainState

Definition at line 20 of file explain.h.

Function Documentation

◆ ExplainOnePlan()

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 
)
extern

Definition at line 497 of file explain.c.

502{
504 QueryDesc *queryDesc;
505 instr_time starttime;
506 double totaltime = 0;
507 int eflags;
508 int instrument_option = 0;
510
511 Assert(plannedstmt->commandType != CMD_UTILITY);
512
513 if (es->analyze && es->timing)
515 else if (es->analyze)
517
518 if (es->buffers)
520 if (es->wal)
522
523 /*
524 * We always collect timing for the entire statement, even when node-level
525 * timing is off, so we don't look at es->timing here. (We could skip
526 * this if !es->summary, but it's hardly worth the complication.)
527 */
528 INSTR_TIME_SET_CURRENT(starttime);
529
530 /*
531 * Use a snapshot with an updated command ID to ensure this query sees
532 * results of any previously executed queries.
533 */
536
537 /*
538 * We discard the output if we have no use for it. If we're explaining
539 * CREATE TABLE AS, we'd better use the appropriate tuple receiver, while
540 * the SERIALIZE option requires its own tuple receiver. (If you specify
541 * SERIALIZE while explaining CREATE TABLE AS, you'll see zeroes for the
542 * results, which is appropriate since no data would have gone to the
543 * client.)
544 */
545 if (into)
547 else if (es->serialize != EXPLAIN_SERIALIZE_NONE)
549 else
551
552 /* Create a QueryDesc for the query */
553 queryDesc = CreateQueryDesc(plannedstmt, queryString,
555 dest, params, queryEnv, instrument_option);
556
557 /* Select execution options */
558 if (es->analyze)
559 eflags = 0; /* default run-to-completion flags */
560 else
561 eflags = EXEC_FLAG_EXPLAIN_ONLY;
562 if (es->generic)
564 if (into)
565 eflags |= GetIntoRelEFlags(into);
566
567 /* call ExecutorStart to prepare the plan for execution */
568 ExecutorStart(queryDesc, eflags);
569
570 /* Execute the plan for statistics if asked for */
571 if (es->analyze)
572 {
573 ScanDirection dir;
574
575 /* EXPLAIN ANALYZE CREATE TABLE AS WITH NO DATA is weird */
576 if (into && into->skipData)
578 else
580
581 /* run the plan */
582 ExecutorRun(queryDesc, dir, 0);
583
584 /* run cleanup too */
585 ExecutorFinish(queryDesc);
586
587 /* We can't run ExecutorEnd 'till we're done printing the stats... */
588 totaltime += elapsed_time(&starttime);
589 }
590
591 /* grab serialization metrics before we destroy the DestReceiver */
594
595 /* call the DestReceiver's destroy method even during explain */
596 dest->rDestroy(dest);
597
598 ExplainOpenGroup("Query", NULL, true, es);
599
600 /* Create textual dump of plan tree */
601 ExplainPrintPlan(es, queryDesc);
602
603 /* Show buffer and/or memory usage in planning */
604 if (peek_buffer_usage(es, bufusage) || mem_counters)
605 {
606 ExplainOpenGroup("Planning", "Planning", true, es);
607
608 if (es->format == EXPLAIN_FORMAT_TEXT)
609 {
611 appendStringInfoString(es->str, "Planning:\n");
612 es->indent++;
613 }
614
615 if (bufusage)
616 show_buffer_usage(es, bufusage);
617
618 if (mem_counters)
620
621 if (es->format == EXPLAIN_FORMAT_TEXT)
622 es->indent--;
623
624 ExplainCloseGroup("Planning", "Planning", true, es);
625 }
626
627 if (es->summary && planduration)
628 {
630
631 ExplainPropertyFloat("Planning Time", "ms", 1000.0 * plantime, 3, es);
632 }
633
634 /* Print info about runtime of triggers */
635 if (es->analyze)
636 ExplainPrintTriggers(es, queryDesc);
637
638 /*
639 * Print info about JITing. Tied to es->costs because we don't want to
640 * display this in regression tests, as it'd cause output differences
641 * depending on build options. Might want to separate that out from COSTS
642 * at a later stage.
643 */
644 if (es->costs)
645 ExplainPrintJITSummary(es, queryDesc);
646
647 /* Print info about serialization of output */
650
651 /* Allow plugins to print additional information */
653 (*explain_per_plan_hook) (plannedstmt, into, es, queryString,
654 params, queryEnv);
655
656 /*
657 * Close down the query and free resources. Include time for this in the
658 * total execution time (although it should be pretty minimal).
659 */
660 INSTR_TIME_SET_CURRENT(starttime);
661
662 ExecutorEnd(queryDesc);
663
664 FreeQueryDesc(queryDesc);
665
667
668 /* We need a CCI just in case query expanded to multiple plans */
669 if (es->analyze)
671
672 totaltime += elapsed_time(&starttime);
673
674 /*
675 * We only report execution time if we actually ran the query (that is,
676 * the user specified ANALYZE), and if summary reporting is enabled (the
677 * user can set SUMMARY OFF to not have the timing information included in
678 * the output). By default, ANALYZE sets SUMMARY to true.
679 */
680 if (es->summary && es->analyze)
681 ExplainPropertyFloat("Execution Time", "ms", 1000.0 * totaltime, 3,
682 es);
683
684 ExplainCloseGroup("Query", NULL, true, es);
685}
#define Assert(condition)
Definition c.h:945
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:468
void ExecutorFinish(QueryDesc *queryDesc)
Definition execMain.c:408
void ExecutorStart(QueryDesc *queryDesc, int eflags)
Definition execMain.c:124
void ExecutorRun(QueryDesc *queryDesc, ScanDirection direction, uint64 count)
Definition execMain.c:299
#define EXEC_FLAG_EXPLAIN_GENERIC
Definition executor.h:68
#define EXEC_FLAG_EXPLAIN_ONLY
Definition executor.h:67
static bool peek_buffer_usage(ExplainState *es, const BufferUsage *usage)
Definition explain.c:4075
void ExplainPrintJITSummary(ExplainState *es, QueryDesc *queryDesc)
Definition explain.c:878
static double elapsed_time(instr_time *starttime)
Definition explain.c:1168
explain_per_plan_hook_type explain_per_plan_hook
Definition explain.c:57
static void show_memory_counters(ExplainState *es, const MemoryContextCounters *mem_counters)
Definition explain.c:4333
void ExplainPrintPlan(ExplainState *es, QueryDesc *queryDesc)
Definition explain.c:762
static void ExplainPrintSerialize(ExplainState *es, SerializeMetrics *metrics)
Definition explain.c:1002
void ExplainPrintTriggers(ExplainState *es, QueryDesc *queryDesc)
Definition explain.c:835
static void show_buffer_usage(ExplainState *es, const BufferUsage *usage)
Definition explain.c:4115
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
@ EXPLAIN_FORMAT_TEXT
#define INSTR_TIME_SET_CURRENT(t)
Definition instr_time.h:122
#define INSTR_TIME_GET_DOUBLE(t)
Definition instr_time.h:186
@ INSTRUMENT_TIMER
Definition instrument.h:63
@ INSTRUMENT_BUFFERS
Definition instrument.h:64
@ INSTRUMENT_WAL
Definition instrument.h:66
@ INSTRUMENT_ROWS
Definition instrument.h:65
@ 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
static int fb(int x)
ScanDirection
Definition sdir.h:25
@ NoMovementScanDirection
Definition sdir.h:27
@ ForwardScanDirection
Definition sdir.h:28
void UpdateActiveSnapshotCommandId(void)
Definition snapmgr.c:744
void PopActiveSnapshot(void)
Definition snapmgr.c:775
void PushCopiedSnapshot(Snapshot snapshot)
Definition snapmgr.c:732
Snapshot GetActiveSnapshot(void)
Definition snapmgr.c:800
#define InvalidSnapshot
Definition snapshot.h:119
void appendStringInfoString(StringInfo str, const char *s)
Definition stringinfo.c:230
StringInfo str
ExplainFormat format
ExplainSerializeOption serialize
bool skipData
Definition primnodes.h:172
CmdType commandType
Definition plannodes.h:66
void CommandCounterIncrement(void)
Definition xact.c:1102

References ExplainState::analyze, appendStringInfoString(), Assert, ExplainState::buffers, CMD_UTILITY, CommandCounterIncrement(), PlannedStmt::commandType, ExplainState::costs, CreateExplainSerializeDestReceiver(), CreateIntoRelDestReceiver(), CreateQueryDesc(), 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(), fb(), 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 ( Node utilityStmt,
IntoClause into,
ExplainState es,
ParseState pstate,
ParamListInfo  params 
)
extern

Definition at line 393 of file explain.c.

395{
396 if (utilityStmt == NULL)
397 return;
398
399 if (IsA(utilityStmt, CreateTableAsStmt))
400 {
401 /*
402 * We have to rewrite the contained SELECT and then pass it back to
403 * ExplainOneQuery. Copy to be safe in the EXPLAIN EXECUTE case.
404 */
405 CreateTableAsStmt *ctas = (CreateTableAsStmt *) utilityStmt;
409
410 /*
411 * Check if the relation exists or not. This is done at this stage to
412 * avoid query planning or execution.
413 */
415 {
416 if (ctas->objtype == OBJECT_TABLE)
417 ExplainDummyGroup("CREATE TABLE AS", NULL, es);
418 else if (ctas->objtype == OBJECT_MATVIEW)
419 ExplainDummyGroup("CREATE MATERIALIZED VIEW", NULL, es);
420 else
421 elog(ERROR, "unexpected object type: %d",
422 (int) ctas->objtype);
423 return;
424 }
425
427 if (IsQueryIdEnabled())
430 (*post_parse_analyze_hook) (pstate, ctas_query, jstate);
434 CURSOR_OPT_PARALLEL_OK, ctas->into, es,
435 pstate, params);
436 }
437 else if (IsA(utilityStmt, DeclareCursorStmt))
438 {
439 /*
440 * Likewise for DECLARE CURSOR.
441 *
442 * Notice that if you say EXPLAIN ANALYZE DECLARE CURSOR then we'll
443 * actually run the query. This is different from pre-8.3 behavior
444 * but seems more useful than not running the query. No cursor will
445 * be created, however.
446 */
447 DeclareCursorStmt *dcs = (DeclareCursorStmt *) utilityStmt;
451
453 if (IsQueryIdEnabled())
456 (*post_parse_analyze_hook) (pstate, dcs_query, jstate);
457
461 dcs->options, NULL, es,
462 pstate, params);
463 }
464 else if (IsA(utilityStmt, ExecuteStmt))
465 ExplainExecuteQuery((ExecuteStmt *) utilityStmt, into, es,
466 pstate, params);
467 else if (IsA(utilityStmt, NotifyStmt))
468 {
469 if (es->format == EXPLAIN_FORMAT_TEXT)
470 appendStringInfoString(es->str, "NOTIFY\n");
471 else
472 ExplainDummyGroup("Notify", NULL, es);
473 }
474 else
475 {
476 if (es->format == EXPLAIN_FORMAT_TEXT)
478 "Utility statements have no plan structure\n");
479 else
480 ExplainDummyGroup("Utility Statement", NULL, es);
481 }
482}
void ExplainExecuteQuery(ExecuteStmt *execstmt, IntoClause *into, ExplainState *es, ParseState *pstate, ParamListInfo params)
Definition prepare.c:573
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:296
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
@ OBJECT_TABLE
#define CURSOR_OPT_PARALLEL_OK
post_parse_analyze_hook_type post_parse_analyze_hook
Definition analyze.c:68
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)
JumbleState * JumbleQuery(Query *query)
List * QueryRewrite(Query *parsetree)
Definition pg_list.h:54

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

Referenced by ExplainExecuteQuery(), and ExplainOneQuery().

◆ ExplainPrintJITSummary()

void ExplainPrintJITSummary ( ExplainState es,
QueryDesc queryDesc 
)
extern

Definition at line 878 of file explain.c.

879{
881
882 if (!(queryDesc->estate->es_jit_flags & PGJIT_PERFORM))
883 return;
884
885 /*
886 * Work with a copy instead of modifying the leader state, since this
887 * function may be called twice
888 */
889 if (queryDesc->estate->es_jit)
890 InstrJitAgg(&ji, &queryDesc->estate->es_jit->instr);
891
892 /* If this process has done JIT in parallel workers, merge stats */
893 if (queryDesc->estate->es_jit_worker_instr)
895
896 ExplainPrintJIT(es, queryDesc->estate->es_jit_flags, &ji);
897}
static void ExplainPrintJIT(ExplainState *es, int jit_flags, JitInstrumentation *ji)
Definition explain.c:904
void InstrJitAgg(JitInstrumentation *dst, JitInstrumentation *add)
Definition jit.c:183
#define PGJIT_PERFORM
Definition jit.h:20
struct JitContext * es_jit
Definition execnodes.h:776
struct JitInstrumentation * es_jit_worker_instr
Definition execnodes.h:777
int es_jit_flags
Definition execnodes.h:775
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(), fb(), JitContext::instr, InstrJitAgg(), and PGJIT_PERFORM.

Referenced by explain_ExecutorEnd(), and ExplainOnePlan().

◆ ExplainPrintPlan()

void ExplainPrintPlan ( ExplainState es,
QueryDesc queryDesc 
)
extern

Definition at line 762 of file explain.c.

763{
765 PlanState *ps;
766 ListCell *lc;
767
768 /* Set up ExplainState fields associated with this plan tree */
769 Assert(queryDesc->plannedstmt != NULL);
770 es->pstmt = queryDesc->plannedstmt;
771 es->rtable = queryDesc->plannedstmt->rtable;
775 es->rtable_names);
777 es->rtable_size = list_length(es->rtable);
778 foreach(lc, es->rtable)
779 {
781
782 if (rte->rtekind == RTE_GROUP)
783 {
784 es->rtable_size--;
785 break;
786 }
787 }
788
789 /*
790 * Sometimes we mark a Gather node as "invisible", which means that it's
791 * not to be displayed in EXPLAIN output. The purpose of this is to allow
792 * running regression tests with debug_parallel_query=regress to get the
793 * same results as running the same tests with debug_parallel_query=off.
794 * Such marking is currently only supported on a Gather at the top of the
795 * plan. We skip that node, and we must also hide per-worker detail data
796 * further down in the plan tree.
797 */
798 ps = queryDesc->planstate;
799 if (IsA(ps, GatherState) && ((Gather *) ps->plan)->invisible)
800 {
802 es->hide_workers = true;
803 }
804 ExplainNode(ps, NIL, NULL, NULL, es);
805
806 /*
807 * If requested, include information about GUC parameters with values that
808 * don't match the built-in defaults.
809 */
811
812 /*
813 * COMPUTE_QUERY_ID_REGRESS means COMPUTE_QUERY_ID_AUTO, but we don't show
814 * the queryid in any of the EXPLAIN plans to keep stable the results
815 * generated by regression test suites.
816 */
817 if (es->verbose && queryDesc->plannedstmt->queryId != INT64CONST(0) &&
819 {
820 ExplainPropertyInteger("Query Identifier", NULL,
821 queryDesc->plannedstmt->queryId, es);
822 }
823}
#define INT64CONST(x)
Definition c.h:632
#define outerPlanState(node)
Definition execnodes.h:1273
static void ExplainNode(PlanState *planstate, List *ancestors, const char *relationship, const char *plan_name, ExplainState *es)
Definition explain.c:1360
static bool ExplainPreScanNode(PlanState *planstate, Bitmapset **rels_used)
Definition explain.c:1187
static void ExplainPrintSettings(ExplainState *es)
Definition explain.c:692
void ExplainPropertyInteger(const char *qlabel, const char *unit, int64 value, ExplainState *es)
struct parser_state ps
@ RTE_GROUP
#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:4113
List * select_rtable_names_for_explain(List *rtable, Bitmapset *rels_used)
Definition ruleutils.c:4215
Bitmapset * printed_subplans
List * rtable_names
PlannedStmt * pstmt
List * deparse_cxt
int64 queryId
Definition plannodes.h:69
List * rtable
Definition plannodes.h:107
PlannedStmt * plannedstmt
Definition execdesc.h:37
PlanState * planstate
Definition execdesc.h:49

References Assert, compute_query_id, COMPUTE_QUERY_ID_REGRESS, deparse_context_for_plan_tree(), ExplainState::deparse_cxt, ExplainNode(), ExplainPreScanNode(), ExplainPrintSettings(), ExplainPropertyInteger(), fb(), 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, select_rtable_names_for_explain(), and ExplainState::verbose.

Referenced by explain_ExecutorEnd(), and ExplainOnePlan().

◆ ExplainPrintTriggers()

void ExplainPrintTriggers ( ExplainState es,
QueryDesc queryDesc 
)
extern

Definition at line 835 of file explain.c.

836{
838 bool show_relname;
841 List *targrels;
842 ListCell *l;
843
847
848 ExplainOpenGroup("Triggers", "Triggers", false, es);
849
851 routerels != NIL || targrels != NIL);
852 foreach(l, resultrels)
853 {
854 rInfo = (ResultRelInfo *) lfirst(l);
856 }
857
858 foreach(l, routerels)
859 {
860 rInfo = (ResultRelInfo *) lfirst(l);
862 }
863
864 foreach(l, targrels)
865 {
866 rInfo = (ResultRelInfo *) lfirst(l);
868 }
869
870 ExplainCloseGroup("Triggers", "Triggers", false, es);
871}
static void report_triggers(ResultRelInfo *rInfo, bool show_relname, ExplainState *es)
Definition explain.c:1095
#define lfirst(lc)
Definition pg_list.h:172
List * es_tuple_routing_result_relations
Definition execnodes.h:710
List * es_trig_target_relations
Definition execnodes.h:713
List * es_opened_result_relations
Definition execnodes.h:700

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

Referenced by explain_ExecutorEnd(), and ExplainOnePlan().

◆ ExplainQuery()

void ExplainQuery ( ParseState pstate,
ExplainStmt stmt,
ParamListInfo  params,
DestReceiver dest 
)
extern

Definition at line 178 of file explain.c.

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

References appendStringInfoString(), Assert, begin_tup_output_tupdesc(), castNode, CURSOR_OPT_PARALLEL_OK, StringInfoData::data, do_text_output_multiline(), do_text_output_oneline, end_tup_output(), EXPLAIN_FORMAT_TEXT, ExplainBeginOutput(), ExplainEndOutput(), ExplainOneQuery(), ExplainResultDesc(), ExplainSeparatePlans(), fb(), 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 ( ExplainState es,
ParamListInfo  params,
int  maxlen 
)
extern

Definition at line 1077 of file explain.c.

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

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

Referenced by explain_ExecutorEnd().

◆ ExplainQueryText()

void ExplainQueryText ( ExplainState es,
QueryDesc queryDesc 
)
extern

Definition at line 1062 of file explain.c.

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

References ExplainPropertyText(), and QueryDesc::sourceText.

Referenced by explain_ExecutorEnd().

◆ ExplainResultDesc()

TupleDesc ExplainResultDesc ( ExplainStmt stmt)
extern

Definition at line 256 of file explain.c.

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

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

Referenced by ExplainQuery(), and UtilityTupleDescriptor().

◆ standard_ExplainOneQuery()

void standard_ExplainOneQuery ( Query query,
int  cursorOptions,
IntoClause into,
ExplainState es,
const char queryString,
ParamListInfo  params,
QueryEnvironment queryEnv 
)
extern

Definition at line 321 of file explain.c.

325{
329 BufferUsage bufusage_start,
330 bufusage;
334
335 if (es->memory)
336 {
337 /*
338 * Create a new memory context to measure planner's memory consumption
339 * accurately. Note that if the planner were to be modified to use a
340 * different memory context type, here we would be changing that to
341 * AllocSet, which might be undesirable. However, we don't have a way
342 * to create a context of the same type as another, so we pray and
343 * hope that this is OK.
344 */
346 "explain analyze planner context",
349 }
350
351 if (es->buffers)
352 bufusage_start = pgBufferUsage;
354
355 /* plan the query */
356 plan = pg_plan_query(query, queryString, cursorOptions, params, es);
357
360
361 if (es->memory)
362 {
365 }
366
367 /* calc differences of buffer counters. */
368 if (es->buffers)
369 {
370 memset(&bufusage, 0, sizeof(BufferUsage));
371 BufferUsageAccumDiff(&bufusage, &pgBufferUsage, &bufusage_start);
372 }
373
374 /* run it (if needed) and produce output */
375 ExplainOnePlan(plan, into, es, queryString, params, queryEnv,
376 &planduration, (es->buffers ? &bufusage : NULL),
377 es->memory ? &mem_counters : NULL);
378}
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:497
#define INSTR_TIME_SUBTRACT(x, y)
Definition instr_time.h:177
BufferUsage pgBufferUsage
Definition instrument.c:20
void BufferUsageAccumDiff(BufferUsage *dst, const BufferUsage *add, const BufferUsage *sub)
Definition instrument.c:249
void MemoryContextMemConsumed(MemoryContext context, MemoryContextCounters *consumed)
Definition mcxt.c:835
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, ExplainState *es)
Definition postgres.c:887

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, ExplainState::buffers, BufferUsageAccumDiff(), CurrentMemoryContext, ExplainOnePlan(), fb(), 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 54 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 58 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 57 of file explain.c.

Referenced by _PG_init(), and ExplainOnePlan().

◆ ExplainOneQuery_hook

PGDLLIMPORT ExplainOneQuery_hook_type ExplainOneQuery_hook
extern

Definition at line 51 of file explain.c.

Referenced by ExplainOneQuery().