PostgreSQL Source Code  git master
explain.h File Reference
#include "executor/executor.h"
#include "lib/stringinfo.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.

Data Structures

struct  ExplainWorkersState
 
struct  ExplainState
 

Typedefs

typedef enum ExplainFormat ExplainFormat
 
typedef struct ExplainWorkersState ExplainWorkersState
 
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 const char *(* explain_get_index_name_hook_type) (Oid indexId)
 

Enumerations

enum  ExplainFormat { EXPLAIN_FORMAT_TEXT, EXPLAIN_FORMAT_XML, EXPLAIN_FORMAT_JSON, EXPLAIN_FORMAT_YAML }
 

Functions

void ExplainQuery (ParseState *pstate, ExplainStmt *stmt, ParamListInfo params, DestReceiver *dest)
 
ExplainStateNewExplainState (void)
 
TupleDesc ExplainResultDesc (ExplainStmt *stmt)
 
void ExplainOneUtility (Node *utilityStmt, IntoClause *into, ExplainState *es, const char *queryString, ParamListInfo params, QueryEnvironment *queryEnv)
 
void ExplainOnePlan (PlannedStmt *plannedstmt, IntoClause *into, ExplainState *es, const char *queryString, ParamListInfo params, QueryEnvironment *queryEnv, const instr_time *planduration, const BufferUsage *bufusage)
 
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 ExplainBeginOutput (ExplainState *es)
 
void ExplainEndOutput (ExplainState *es)
 
void ExplainSeparatePlans (ExplainState *es)
 
void ExplainPropertyList (const char *qlabel, List *data, ExplainState *es)
 
void ExplainPropertyListNested (const char *qlabel, List *data, ExplainState *es)
 
void ExplainPropertyText (const char *qlabel, const char *value, ExplainState *es)
 
void ExplainPropertyInteger (const char *qlabel, const char *unit, int64 value, ExplainState *es)
 
void ExplainPropertyUInteger (const char *qlabel, const char *unit, uint64 value, ExplainState *es)
 
void ExplainPropertyFloat (const char *qlabel, const char *unit, double value, int ndigits, ExplainState *es)
 
void ExplainPropertyBool (const char *qlabel, bool value, ExplainState *es)
 
void ExplainOpenGroup (const char *objtype, const char *labelname, bool labeled, ExplainState *es)
 
void ExplainCloseGroup (const char *objtype, const char *labelname, bool labeled, ExplainState *es)
 

Variables

PGDLLIMPORT ExplainOneQuery_hook_type ExplainOneQuery_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 75 of file explain.h.

◆ ExplainFormat

◆ 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 65 of file explain.h.

◆ ExplainState

typedef struct ExplainState ExplainState

◆ ExplainWorkersState

Enumeration Type Documentation

◆ ExplainFormat

Enumerator
EXPLAIN_FORMAT_TEXT 
EXPLAIN_FORMAT_XML 
EXPLAIN_FORMAT_JSON 
EXPLAIN_FORMAT_YAML 

Definition at line 20 of file explain.h.

Function Documentation

◆ ExplainBeginOutput()

void ExplainBeginOutput ( ExplainState es)

Definition at line 4562 of file explain.c.

References appendStringInfoChar(), appendStringInfoString(), EXPLAIN_FORMAT_JSON, EXPLAIN_FORMAT_TEXT, EXPLAIN_FORMAT_XML, EXPLAIN_FORMAT_YAML, ExplainState::format, ExplainState::grouping_stack, ExplainState::indent, lcons_int(), and ExplainState::str.

Referenced by explain_ExecutorEnd(), and ExplainQuery().

4563 {
4564  switch (es->format)
4565  {
4566  case EXPLAIN_FORMAT_TEXT:
4567  /* nothing to do */
4568  break;
4569 
4570  case EXPLAIN_FORMAT_XML:
4572  "<explain xmlns=\"http://www.postgresql.org/2009/explain\">\n");
4573  es->indent++;
4574  break;
4575 
4576  case EXPLAIN_FORMAT_JSON:
4577  /* top-level structure is an array of plans */
4578  appendStringInfoChar(es->str, '[');
4579  es->grouping_stack = lcons_int(0, es->grouping_stack);
4580  es->indent++;
4581  break;
4582 
4583  case EXPLAIN_FORMAT_YAML:
4584  es->grouping_stack = lcons_int(0, es->grouping_stack);
4585  break;
4586  }
4587 }
List * lcons_int(int datum, List *list)
Definition: list.c:471
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
List * grouping_stack
Definition: explain.h:52
int indent
Definition: explain.h:51
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:188
ExplainFormat format
Definition: explain.h:49
StringInfo str
Definition: explain.h:39

◆ ExplainCloseGroup()

void ExplainCloseGroup ( const char *  objtype,
const char *  labelname,
bool  labeled,
ExplainState es 
)

Definition at line 4369 of file explain.c.

References appendStringInfoChar(), appendStringInfoSpaces(), EXPLAIN_FORMAT_JSON, EXPLAIN_FORMAT_TEXT, EXPLAIN_FORMAT_XML, EXPLAIN_FORMAT_YAML, ExplainXMLTag(), ExplainState::format, ExplainState::grouping_stack, ExplainState::indent, list_delete_first(), ExplainState::str, and X_CLOSING.

Referenced by ExplainFlushWorkersState(), ExplainNode(), ExplainOnePlan(), ExplainPrintJIT(), ExplainPrintSettings(), ExplainPrintTriggers(), report_triggers(), show_grouping_set_keys(), show_grouping_sets(), show_incremental_sort_group_info(), and show_modifytable_info().

4371 {
4372  switch (es->format)
4373  {
4374  case EXPLAIN_FORMAT_TEXT:
4375  /* nothing to do */
4376  break;
4377 
4378  case EXPLAIN_FORMAT_XML:
4379  es->indent--;
4380  ExplainXMLTag(objtype, X_CLOSING, es);
4381  break;
4382 
4383  case EXPLAIN_FORMAT_JSON:
4384  es->indent--;
4385  appendStringInfoChar(es->str, '\n');
4386  appendStringInfoSpaces(es->str, 2 * es->indent);
4387  appendStringInfoChar(es->str, labeled ? '}' : ']');
4389  break;
4390 
4391  case EXPLAIN_FORMAT_YAML:
4392  es->indent--;
4394  break;
4395  }
4396 }
static void ExplainXMLTag(const char *tagname, int flags, ExplainState *es)
Definition: explain.c:4651
#define X_CLOSING
Definition: explain.c:52
List * grouping_stack
Definition: explain.h:52
int indent
Definition: explain.h:51
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:188
void appendStringInfoSpaces(StringInfo str, int count)
Definition: stringinfo.c:206
ExplainFormat format
Definition: explain.h:49
StringInfo str
Definition: explain.h:39
List * list_delete_first(List *list)
Definition: list.c:860

◆ ExplainEndOutput()

void ExplainEndOutput ( ExplainState es)

Definition at line 4593 of file explain.c.

References appendStringInfoString(), EXPLAIN_FORMAT_JSON, EXPLAIN_FORMAT_TEXT, EXPLAIN_FORMAT_XML, EXPLAIN_FORMAT_YAML, ExplainState::format, ExplainState::grouping_stack, ExplainState::indent, list_delete_first(), and ExplainState::str.

Referenced by explain_ExecutorEnd(), and ExplainQuery().

4594 {
4595  switch (es->format)
4596  {
4597  case EXPLAIN_FORMAT_TEXT:
4598  /* nothing to do */
4599  break;
4600 
4601  case EXPLAIN_FORMAT_XML:
4602  es->indent--;
4603  appendStringInfoString(es->str, "</explain>");
4604  break;
4605 
4606  case EXPLAIN_FORMAT_JSON:
4607  es->indent--;
4608  appendStringInfoString(es->str, "\n]");
4610  break;
4611 
4612  case EXPLAIN_FORMAT_YAML:
4614  break;
4615  }
4616 }
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
List * grouping_stack
Definition: explain.h:52
int indent
Definition: explain.h:51
ExplainFormat format
Definition: explain.h:49
StringInfo str
Definition: explain.h:39
List * list_delete_first(List *list)
Definition: list.c:860

◆ ExplainOnePlan()

void ExplainOnePlan ( PlannedStmt plannedstmt,
IntoClause into,
ExplainState es,
const char *  queryString,
ParamListInfo  params,
QueryEnvironment queryEnv,
const instr_time planduration,
const BufferUsage bufusage 
)

Definition at line 512 of file explain.c.

References ExplainState::analyze, Assert, ExplainState::buffers, CMD_UTILITY, CommandCounterIncrement(), PlannedStmt::commandType, ExplainState::costs, CreateIntoRelDestReceiver(), CreateQueryDesc(), generate_unaccent_rules::dest, elapsed_time(), EXEC_FLAG_EXPLAIN_ONLY, ExecutorEnd(), ExecutorFinish(), ExecutorRun(), ExecutorStart(), ExplainCloseGroup(), ExplainOpenGroup(), ExplainPrintJITSummary(), ExplainPrintPlan(), ExplainPrintTriggers(), ExplainPropertyFloat(), ForwardScanDirection, FreeQueryDesc(), GetActiveSnapshot(), GetIntoRelEFlags(), INSTR_TIME_GET_DOUBLE, INSTR_TIME_SET_CURRENT, INSTRUMENT_BUFFERS, INSTRUMENT_ROWS, INSTRUMENT_TIMER, INSTRUMENT_WAL, InvalidSnapshot, NoMovementScanDirection, None_Receiver, PopActiveSnapshot(), PushCopiedSnapshot(), show_buffer_usage(), IntoClause::skipData, ExplainState::summary, ExplainState::timing, UpdateActiveSnapshotCommandId(), and ExplainState::wal.

Referenced by ExplainExecuteQuery(), and ExplainOneQuery().

516 {
518  QueryDesc *queryDesc;
519  instr_time starttime;
520  double totaltime = 0;
521  int eflags;
522  int instrument_option = 0;
523 
524  Assert(plannedstmt->commandType != CMD_UTILITY);
525 
526  if (es->analyze && es->timing)
527  instrument_option |= INSTRUMENT_TIMER;
528  else if (es->analyze)
529  instrument_option |= INSTRUMENT_ROWS;
530 
531  if (es->buffers)
532  instrument_option |= INSTRUMENT_BUFFERS;
533  if (es->wal)
534  instrument_option |= INSTRUMENT_WAL;
535 
536  /*
537  * We always collect timing for the entire statement, even when node-level
538  * timing is off, so we don't look at es->timing here. (We could skip
539  * this if !es->summary, but it's hardly worth the complication.)
540  */
541  INSTR_TIME_SET_CURRENT(starttime);
542 
543  /*
544  * Use a snapshot with an updated command ID to ensure this query sees
545  * results of any previously executed queries.
546  */
549 
550  /*
551  * Normally we discard the query's output, but if explaining CREATE TABLE
552  * AS, we'd better use the appropriate tuple receiver.
553  */
554  if (into)
555  dest = CreateIntoRelDestReceiver(into);
556  else
557  dest = None_Receiver;
558 
559  /* Create a QueryDesc for the query */
560  queryDesc = CreateQueryDesc(plannedstmt, queryString,
562  dest, params, queryEnv, instrument_option);
563 
564  /* Select execution options */
565  if (es->analyze)
566  eflags = 0; /* default run-to-completion flags */
567  else
568  eflags = EXEC_FLAG_EXPLAIN_ONLY;
569  if (into)
570  eflags |= GetIntoRelEFlags(into);
571 
572  /* call ExecutorStart to prepare the plan for execution */
573  ExecutorStart(queryDesc, eflags);
574 
575  /* Execute the plan for statistics if asked for */
576  if (es->analyze)
577  {
578  ScanDirection dir;
579 
580  /* EXPLAIN ANALYZE CREATE TABLE AS WITH NO DATA is weird */
581  if (into && into->skipData)
583  else
584  dir = ForwardScanDirection;
585 
586  /* run the plan */
587  ExecutorRun(queryDesc, dir, 0L, true);
588 
589  /* run cleanup too */
590  ExecutorFinish(queryDesc);
591 
592  /* We can't run ExecutorEnd 'till we're done printing the stats... */
593  totaltime += elapsed_time(&starttime);
594  }
595 
596  ExplainOpenGroup("Query", NULL, true, es);
597 
598  /* Create textual dump of plan tree */
599  ExplainPrintPlan(es, queryDesc);
600 
601  /* Show buffer usage in planning */
602  if (bufusage)
603  {
604  ExplainOpenGroup("Planning", "Planning", true, es);
605  show_buffer_usage(es, bufusage, true);
606  ExplainCloseGroup("Planning", "Planning", true, es);
607  }
608 
609  if (es->summary && planduration)
610  {
611  double plantime = INSTR_TIME_GET_DOUBLE(*planduration);
612 
613  ExplainPropertyFloat("Planning Time", "ms", 1000.0 * plantime, 3, es);
614  }
615 
616  /* Print info about runtime of triggers */
617  if (es->analyze)
618  ExplainPrintTriggers(es, queryDesc);
619 
620  /*
621  * Print info about JITing. Tied to es->costs because we don't want to
622  * display this in regression tests, as it'd cause output differences
623  * depending on build options. Might want to separate that out from COSTS
624  * at a later stage.
625  */
626  if (es->costs)
627  ExplainPrintJITSummary(es, queryDesc);
628 
629  /*
630  * Close down the query and free resources. Include time for this in the
631  * total execution time (although it should be pretty minimal).
632  */
633  INSTR_TIME_SET_CURRENT(starttime);
634 
635  ExecutorEnd(queryDesc);
636 
637  FreeQueryDesc(queryDesc);
638 
640 
641  /* We need a CCI just in case query expanded to multiple plans */
642  if (es->analyze)
644 
645  totaltime += elapsed_time(&starttime);
646 
647  /*
648  * We only report execution time if we actually ran the query (that is,
649  * the user specified ANALYZE), and if summary reporting is enabled (the
650  * user can set SUMMARY OFF to not have the timing information included in
651  * the output). By default, ANALYZE sets SUMMARY to true.
652  */
653  if (es->summary && es->analyze)
654  ExplainPropertyFloat("Execution Time", "ms", 1000.0 * totaltime, 3,
655  es);
656 
657  ExplainCloseGroup("Query", NULL, true, es);
658 }
bool summary
Definition: explain.h:47
void UpdateActiveSnapshotCommandId(void)
Definition: snapmgr.c:728
void FreeQueryDesc(QueryDesc *qdesc)
Definition: pquery.c:105
void ExplainPropertyFloat(const char *qlabel, const char *unit, double value, int ndigits, ExplainState *es)
Definition: explain.c:4277
struct timeval instr_time
Definition: instr_time.h:150
void ExecutorStart(QueryDesc *queryDesc, int eflags)
Definition: execMain.c:143
Snapshot GetActiveSnapshot(void)
Definition: snapmgr.c:786
void PopActiveSnapshot(void)
Definition: snapmgr.c:759
#define INSTR_TIME_GET_DOUBLE(t)
Definition: instr_time.h:199
bool skipData
Definition: primnodes.h:119
bool costs
Definition: explain.h:43
DestReceiver * None_Receiver
Definition: dest.c:96
bool analyze
Definition: explain.h:42
void ExecutorEnd(QueryDesc *queryDesc)
Definition: execMain.c:462
int GetIntoRelEFlags(IntoClause *intoClause)
Definition: createas.c:381
void ExplainPrintTriggers(ExplainState *es, QueryDesc *queryDesc)
Definition: explain.c:784
void ExplainPrintPlan(ExplainState *es, QueryDesc *queryDesc)
Definition: explain.c:735
void ExecutorRun(QueryDesc *queryDesc, ScanDirection direction, uint64 count, bool execute_once)
Definition: execMain.c:301
void PushCopiedSnapshot(Snapshot snapshot)
Definition: snapmgr.c:716
QueryDesc * CreateQueryDesc(PlannedStmt *plannedstmt, const char *sourceText, Snapshot snapshot, Snapshot crosscheck_snapshot, DestReceiver *dest, ParamListInfo params, QueryEnvironment *queryEnv, int instrument_options)
Definition: pquery.c:67
ScanDirection
Definition: sdir.h:22
DestReceiver * CreateIntoRelDestReceiver(IntoClause *intoClause)
Definition: createas.c:434
void ExecutorFinish(QueryDesc *queryDesc)
Definition: execMain.c:402
bool timing
Definition: explain.h:46
#define InvalidSnapshot
Definition: snapshot.h:123
void CommandCounterIncrement(void)
Definition: xact.c:1021
CmdType commandType
Definition: plannodes.h:46
#define Assert(condition)
Definition: c.h:792
bool wal
Definition: explain.h:45
static double elapsed_time(instr_time *starttime)
Definition: explain.c:1029
#define INSTR_TIME_SET_CURRENT(t)
Definition: instr_time.h:156
static void show_buffer_usage(ExplainState *es, const BufferUsage *usage, bool planning)
Definition: explain.c:3305
bool buffers
Definition: explain.h:44
void ExplainCloseGroup(const char *objtype, const char *labelname, bool labeled, ExplainState *es)
Definition: explain.c:4369
void ExplainPrintJITSummary(ExplainState *es, QueryDesc *queryDesc)
Definition: explain.c:827
void ExplainOpenGroup(const char *objtype, const char *labelname, bool labeled, ExplainState *es)
Definition: explain.c:4306
#define EXEC_FLAG_EXPLAIN_ONLY
Definition: executor.h:56

◆ ExplainOneUtility()

void ExplainOneUtility ( Node utilityStmt,
IntoClause into,
ExplainState es,
const char *  queryString,
ParamListInfo  params,
QueryEnvironment queryEnv 
)

Definition at line 421 of file explain.c.

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

Referenced by ExplainExecuteQuery(), and ExplainOneQuery().

424 {
425  if (utilityStmt == NULL)
426  return;
427 
428  if (IsA(utilityStmt, CreateTableAsStmt))
429  {
430  /*
431  * We have to rewrite the contained SELECT and then pass it back to
432  * ExplainOneQuery. It's probably not really necessary to copy the
433  * contained parsetree another time, but let's be safe.
434  */
435  CreateTableAsStmt *ctas = (CreateTableAsStmt *) utilityStmt;
436  List *rewritten;
437 
438  /*
439  * Check if the relation exists or not. This is done at this stage to
440  * avoid query planning or execution.
441  */
442  if (CreateTableAsRelExists(ctas))
443  {
444  if (ctas->objtype == OBJECT_TABLE)
445  ExplainDummyGroup("CREATE TABLE AS", NULL, es);
446  else if (ctas->objtype == OBJECT_MATVIEW)
447  ExplainDummyGroup("CREATE MATERIALIZED VIEW", NULL, es);
448  else
449  elog(ERROR, "unexpected object type: %d",
450  (int) ctas->objtype);
451  return;
452  }
453 
454  rewritten = QueryRewrite(castNode(Query, copyObject(ctas->query)));
455  Assert(list_length(rewritten) == 1);
456  ExplainOneQuery(linitial_node(Query, rewritten),
457  CURSOR_OPT_PARALLEL_OK, ctas->into, es,
458  queryString, params, queryEnv);
459  }
460  else if (IsA(utilityStmt, DeclareCursorStmt))
461  {
462  /*
463  * Likewise for DECLARE CURSOR.
464  *
465  * Notice that if you say EXPLAIN ANALYZE DECLARE CURSOR then we'll
466  * actually run the query. This is different from pre-8.3 behavior
467  * but seems more useful than not running the query. No cursor will
468  * be created, however.
469  */
470  DeclareCursorStmt *dcs = (DeclareCursorStmt *) utilityStmt;
471  List *rewritten;
472 
473  rewritten = QueryRewrite(castNode(Query, copyObject(dcs->query)));
474  Assert(list_length(rewritten) == 1);
475  ExplainOneQuery(linitial_node(Query, rewritten),
476  dcs->options, NULL, es,
477  queryString, params, queryEnv);
478  }
479  else if (IsA(utilityStmt, ExecuteStmt))
480  ExplainExecuteQuery((ExecuteStmt *) utilityStmt, into, es,
481  queryString, params, queryEnv);
482  else if (IsA(utilityStmt, NotifyStmt))
483  {
484  if (es->format == EXPLAIN_FORMAT_TEXT)
485  appendStringInfoString(es->str, "NOTIFY\n");
486  else
487  ExplainDummyGroup("Notify", NULL, es);
488  }
489  else
490  {
491  if (es->format == EXPLAIN_FORMAT_TEXT)
493  "Utility statements have no plan structure\n");
494  else
495  ExplainDummyGroup("Utility Statement", NULL, es);
496  }
497 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:579
List * QueryRewrite(Query *parsetree)
#define castNode(_type_, nodeptr)
Definition: nodes.h:597
#define linitial_node(type, l)
Definition: pg_list.h:177
ObjectType objtype
Definition: parsenodes.h:3289
static void ExplainDummyGroup(const char *objtype, const char *labelname, ExplainState *es)
Definition: explain.c:4516
#define ERROR
Definition: elog.h:45
void ExplainExecuteQuery(ExecuteStmt *execstmt, IntoClause *into, ExplainState *es, const char *queryString, ParamListInfo params, QueryEnvironment *queryEnv)
Definition: prepare.c:604
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
bool CreateTableAsRelExists(CreateTableAsStmt *ctas)
Definition: createas.c:399
static void ExplainOneQuery(Query *query, int cursorOptions, IntoClause *into, ExplainState *es, const char *queryString, ParamListInfo params, QueryEnvironment *queryEnv)
Definition: explain.c:361
IntoClause * into
Definition: parsenodes.h:3288
#define Assert(condition)
Definition: c.h:792
static int list_length(const List *l)
Definition: pg_list.h:149
ExplainFormat format
Definition: explain.h:49
#define CURSOR_OPT_PARALLEL_OK
Definition: parsenodes.h:2732
#define elog(elevel,...)
Definition: elog.h:228
#define copyObject(obj)
Definition: nodes.h:644
Definition: pg_list.h:50
StringInfo str
Definition: explain.h:39

◆ ExplainOpenGroup()

void ExplainOpenGroup ( const char *  objtype,
const char *  labelname,
bool  labeled,
ExplainState es 
)

Definition at line 4306 of file explain.c.

References appendStringInfo(), appendStringInfoChar(), appendStringInfoSpaces(), appendStringInfoString(), escape_json(), EXPLAIN_FORMAT_JSON, EXPLAIN_FORMAT_TEXT, EXPLAIN_FORMAT_XML, EXPLAIN_FORMAT_YAML, ExplainJSONLineEnding(), ExplainXMLTag(), ExplainYAMLLineStarting(), ExplainState::format, ExplainState::grouping_stack, ExplainState::indent, lcons_int(), ExplainState::str, and X_OPENING.

Referenced by ExplainFlushWorkersState(), ExplainNode(), ExplainOnePlan(), ExplainPrintJIT(), ExplainPrintSettings(), ExplainPrintTriggers(), report_triggers(), show_grouping_set_keys(), show_grouping_sets(), show_incremental_sort_group_info(), and show_modifytable_info().

4308 {
4309  switch (es->format)
4310  {
4311  case EXPLAIN_FORMAT_TEXT:
4312  /* nothing to do */
4313  break;
4314 
4315  case EXPLAIN_FORMAT_XML:
4316  ExplainXMLTag(objtype, X_OPENING, es);
4317  es->indent++;
4318  break;
4319 
4320  case EXPLAIN_FORMAT_JSON:
4322  appendStringInfoSpaces(es->str, 2 * es->indent);
4323  if (labelname)
4324  {
4325  escape_json(es->str, labelname);
4326  appendStringInfoString(es->str, ": ");
4327  }
4328  appendStringInfoChar(es->str, labeled ? '{' : '[');
4329 
4330  /*
4331  * In JSON format, the grouping_stack is an integer list. 0 means
4332  * we've emitted nothing at this grouping level, 1 means we've
4333  * emitted something (and so the next item needs a comma). See
4334  * ExplainJSONLineEnding().
4335  */
4336  es->grouping_stack = lcons_int(0, es->grouping_stack);
4337  es->indent++;
4338  break;
4339 
4340  case EXPLAIN_FORMAT_YAML:
4341 
4342  /*
4343  * In YAML format, the grouping stack is an integer list. 0 means
4344  * we've emitted nothing at this grouping level AND this grouping
4345  * level is unlabeled and must be marked with "- ". See
4346  * ExplainYAMLLineStarting().
4347  */
4349  if (labelname)
4350  {
4351  appendStringInfo(es->str, "%s: ", labelname);
4352  es->grouping_stack = lcons_int(1, es->grouping_stack);
4353  }
4354  else
4355  {
4356  appendStringInfoString(es->str, "- ");
4357  es->grouping_stack = lcons_int(0, es->grouping_stack);
4358  }
4359  es->indent++;
4360  break;
4361  }
4362 }
void escape_json(StringInfo buf, const char *str)
Definition: json.c:1279
List * lcons_int(int datum, List *list)
Definition: list.c:471
static void ExplainXMLTag(const char *tagname, int flags, ExplainState *es)
Definition: explain.c:4651
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
static void ExplainYAMLLineStarting(ExplainState *es)
Definition: explain.c:4713
List * grouping_stack
Definition: explain.h:52
int indent
Definition: explain.h:51
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:188
void appendStringInfoSpaces(StringInfo str, int count)
Definition: stringinfo.c:206
static void ExplainJSONLineEnding(ExplainState *es)
Definition: explain.c:4693
#define X_OPENING
Definition: explain.c:51
ExplainFormat format
Definition: explain.h:49
StringInfo str
Definition: explain.h:39

◆ ExplainPrintJITSummary()

void ExplainPrintJITSummary ( ExplainState es,
QueryDesc queryDesc 
)

Definition at line 827 of file explain.c.

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().

828 {
829  JitInstrumentation ji = {0};
830 
831  if (!(queryDesc->estate->es_jit_flags & PGJIT_PERFORM))
832  return;
833 
834  /*
835  * Work with a copy instead of modifying the leader state, since this
836  * function may be called twice
837  */
838  if (queryDesc->estate->es_jit)
839  InstrJitAgg(&ji, &queryDesc->estate->es_jit->instr);
840 
841  /* If this process has done JIT in parallel workers, merge stats */
842  if (queryDesc->estate->es_jit_worker_instr)
843  InstrJitAgg(&ji, queryDesc->estate->es_jit_worker_instr);
844 
845  ExplainPrintJIT(es, queryDesc->estate->es_jit_flags, &ji);
846 }
EState * estate
Definition: execdesc.h:48
struct JitContext * es_jit
Definition: execnodes.h:605
struct JitInstrumentation * es_jit_worker_instr
Definition: execnodes.h:606
static void ExplainPrintJIT(ExplainState *es, int jit_flags, JitInstrumentation *ji)
Definition: explain.c:853
JitInstrumentation instr
Definition: jit.h:61
int es_jit_flags
Definition: execnodes.h:604
void InstrJitAgg(JitInstrumentation *dst, JitInstrumentation *add)
Definition: jit.c:184
#define PGJIT_PERFORM
Definition: jit.h:20

◆ ExplainPrintPlan()

void ExplainPrintPlan ( ExplainState es,
QueryDesc queryDesc 
)

Definition at line 735 of file explain.c.

References Assert, deparse_context_for_plan_tree(), ExplainState::deparse_cxt, ExplainNode(), ExplainPreScanNode(), ExplainPrintSettings(), ExplainState::hide_workers, IsA, NIL, outerPlanState, PlanState::plan, QueryDesc::plannedstmt, QueryDesc::planstate, ExplainState::printed_subplans, ExplainState::pstmt, ExplainState::rtable, PlannedStmt::rtable, ExplainState::rtable_names, and select_rtable_names_for_explain().

Referenced by explain_ExecutorEnd(), and ExplainOnePlan().

736 {
737  Bitmapset *rels_used = NULL;
738  PlanState *ps;
739 
740  /* Set up ExplainState fields associated with this plan tree */
741  Assert(queryDesc->plannedstmt != NULL);
742  es->pstmt = queryDesc->plannedstmt;
743  es->rtable = queryDesc->plannedstmt->rtable;
744  ExplainPreScanNode(queryDesc->planstate, &rels_used);
747  es->rtable_names);
748  es->printed_subplans = NULL;
749 
750  /*
751  * Sometimes we mark a Gather node as "invisible", which means that it's
752  * not to be displayed in EXPLAIN output. The purpose of this is to allow
753  * running regression tests with force_parallel_mode=regress to get the
754  * same results as running the same tests with force_parallel_mode=off.
755  * Such marking is currently only supported on a Gather at the top of the
756  * plan. We skip that node, and we must also hide per-worker detail data
757  * further down in the plan tree.
758  */
759  ps = queryDesc->planstate;
760  if (IsA(ps, GatherState) && ((Gather *) ps->plan)->invisible)
761  {
762  ps = outerPlanState(ps);
763  es->hide_workers = true;
764  }
765  ExplainNode(ps, NIL, NULL, NULL, es);
766 
767  /*
768  * If requested, include information about GUC parameters with values that
769  * don't match the built-in defaults.
770  */
772 }
#define NIL
Definition: pg_list.h:65
static void ExplainPrintSettings(ExplainState *es)
Definition: explain.c:665
#define IsA(nodeptr, _type_)
Definition: nodes.h:579
bool hide_workers
Definition: explain.h:59
Bitmapset * printed_subplans
Definition: explain.h:58
List * deparse_cxt
Definition: explain.h:57
PlannedStmt * pstmt
Definition: explain.h:54
List * rtable_names
Definition: explain.h:56
List * select_rtable_names_for_explain(List *rtable, Bitmapset *rels_used)
Definition: ruleutils.c:3419
static void ExplainNode(PlanState *planstate, List *ancestors, const char *relationship, const char *plan_name, ExplainState *es)
Definition: explain.c:1122
PlanState * planstate
Definition: execdesc.h:49
#define outerPlanState(node)
Definition: execnodes.h:1022
List * deparse_context_for_plan_tree(PlannedStmt *pstmt, List *rtable_names)
Definition: ruleutils.c:3327
Plan * plan
Definition: execnodes.h:928
#define Assert(condition)
Definition: c.h:792
List * rtable
Definition: plannodes.h:66
static bool ExplainPreScanNode(PlanState *planstate, Bitmapset **rels_used)
Definition: explain.c:1048
PlannedStmt * plannedstmt
Definition: execdesc.h:37
List * rtable
Definition: explain.h:55

◆ ExplainPrintTriggers()

void ExplainPrintTriggers ( ExplainState es,
QueryDesc queryDesc 
)

Definition at line 784 of file explain.c.

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().

785 {
786  ResultRelInfo *rInfo;
787  bool show_relname;
788  List *resultrels;
789  List *routerels;
790  List *targrels;
791  ListCell *l;
792 
793  resultrels = queryDesc->estate->es_opened_result_relations;
794  routerels = queryDesc->estate->es_tuple_routing_result_relations;
795  targrels = queryDesc->estate->es_trig_target_relations;
796 
797  ExplainOpenGroup("Triggers", "Triggers", false, es);
798 
799  show_relname = (list_length(resultrels) > 1 ||
800  routerels != NIL || targrels != NIL);
801  foreach(l, resultrels)
802  {
803  rInfo = (ResultRelInfo *) lfirst(l);
804  report_triggers(rInfo, show_relname, es);
805  }
806 
807  foreach(l, routerels)
808  {
809  rInfo = (ResultRelInfo *) lfirst(l);
810  report_triggers(rInfo, show_relname, es);
811  }
812 
813  foreach(l, targrels)
814  {
815  rInfo = (ResultRelInfo *) lfirst(l);
816  report_triggers(rInfo, show_relname, es);
817  }
818 
819  ExplainCloseGroup("Triggers", "Triggers", false, es);
820 }
#define NIL
Definition: pg_list.h:65
EState * estate
Definition: execdesc.h:48
List * es_opened_result_relations
Definition: execnodes.h:537
static void report_triggers(ResultRelInfo *rInfo, bool show_relname, ExplainState *es)
Definition: explain.c:958
List * es_trig_target_relations
Definition: execnodes.h:550
List * es_tuple_routing_result_relations
Definition: execnodes.h:547
#define lfirst(lc)
Definition: pg_list.h:169
static int list_length(const List *l)
Definition: pg_list.h:149
void ExplainCloseGroup(const char *objtype, const char *labelname, bool labeled, ExplainState *es)
Definition: explain.c:4369
void ExplainOpenGroup(const char *objtype, const char *labelname, bool labeled, ExplainState *es)
Definition: explain.c:4306
Definition: pg_list.h:50

◆ ExplainPropertyBool()

void ExplainPropertyBool ( const char *  qlabel,
bool  value,
ExplainState es 
)

Definition at line 4291 of file explain.c.

References ExplainProperty().

Referenced by ExplainNode(), and ExplainPrintJIT().

4292 {
4293  ExplainProperty(qlabel, NULL, value ? "true" : "false", true, es);
4294 }
static struct @144 value
static void ExplainProperty(const char *qlabel, const char *unit, const char *value, bool numeric, ExplainState *es)
Definition: explain.c:4188

◆ ExplainPropertyFloat()

void ExplainPropertyFloat ( const char *  qlabel,
const char *  unit,
double  value,
int  ndigits,
ExplainState es 
)

Definition at line 4277 of file explain.c.

References buf, ExplainProperty(), pfree(), and psprintf().

Referenced by ExplainNode(), ExplainOnePlan(), ExplainPrintJIT(), report_triggers(), show_buffer_usage(), show_instrumentation_count(), and show_modifytable_info().

4279 {
4280  char *buf;
4281 
4282  buf = psprintf("%.*f", ndigits, value);
4283  ExplainProperty(qlabel, unit, buf, true, es);
4284  pfree(buf);
4285 }
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
static struct @144 value
void pfree(void *pointer)
Definition: mcxt.c:1057
static char * buf
Definition: pg_test_fsync.c:68
static void ExplainProperty(const char *qlabel, const char *unit, const char *value, bool numeric, ExplainState *es)
Definition: explain.c:4188

◆ ExplainPropertyInteger()

void ExplainPropertyInteger ( const char *  qlabel,
const char *  unit,
int64  value,
ExplainState es 
)

Definition at line 4250 of file explain.c.

References buf, ExplainProperty(), INT64_FORMAT, and snprintf.

Referenced by ExplainMissingMembers(), ExplainNode(), ExplainOpenWorker(), ExplainPrintJIT(), fileExplainForeignScan(), show_buffer_usage(), show_hash_info(), show_hashagg_info(), show_incremental_sort_group_info(), show_sort_info(), show_tidbitmap_info(), and show_wal_usage().

4252 {
4253  char buf[32];
4254 
4255  snprintf(buf, sizeof(buf), INT64_FORMAT, value);
4256  ExplainProperty(qlabel, unit, buf, true, es);
4257 }
static struct @144 value
static char * buf
Definition: pg_test_fsync.c:68
#define INT64_FORMAT
Definition: c.h:471
#define snprintf
Definition: port.h:215
static void ExplainProperty(const char *qlabel, const char *unit, const char *value, bool numeric, ExplainState *es)
Definition: explain.c:4188

◆ ExplainPropertyList()

void ExplainPropertyList ( const char *  qlabel,
List data,
ExplainState es 
)

Definition at line 4065 of file explain.c.

References appendStringInfo(), appendStringInfoChar(), appendStringInfoSpaces(), appendStringInfoString(), escape_json(), escape_xml(), escape_yaml(), EXPLAIN_FORMAT_JSON, EXPLAIN_FORMAT_TEXT, EXPLAIN_FORMAT_XML, EXPLAIN_FORMAT_YAML, ExplainIndentText(), ExplainJSONLineEnding(), ExplainXMLTag(), ExplainYAMLLineStarting(), ExplainState::format, ExplainState::indent, lfirst, pfree(), ExplainState::str, generate_unaccent_rules::str, X_CLOSING, and X_OPENING.

Referenced by ExplainPropertyListNested(), show_eval_params(), show_incremental_sort_group_info(), show_modifytable_info(), show_plan_tlist(), show_sort_group_keys(), and show_tablesample().

4066 {
4067  ListCell *lc;
4068  bool first = true;
4069 
4070  switch (es->format)
4071  {
4072  case EXPLAIN_FORMAT_TEXT:
4073  ExplainIndentText(es);
4074  appendStringInfo(es->str, "%s: ", qlabel);
4075  foreach(lc, data)
4076  {
4077  if (!first)
4078  appendStringInfoString(es->str, ", ");
4079  appendStringInfoString(es->str, (const char *) lfirst(lc));
4080  first = false;
4081  }
4082  appendStringInfoChar(es->str, '\n');
4083  break;
4084 
4085  case EXPLAIN_FORMAT_XML:
4086  ExplainXMLTag(qlabel, X_OPENING, es);
4087  foreach(lc, data)
4088  {
4089  char *str;
4090 
4091  appendStringInfoSpaces(es->str, es->indent * 2 + 2);
4092  appendStringInfoString(es->str, "<Item>");
4093  str = escape_xml((const char *) lfirst(lc));
4094  appendStringInfoString(es->str, str);
4095  pfree(str);
4096  appendStringInfoString(es->str, "</Item>\n");
4097  }
4098  ExplainXMLTag(qlabel, X_CLOSING, es);
4099  break;
4100 
4101  case EXPLAIN_FORMAT_JSON:
4103  appendStringInfoSpaces(es->str, es->indent * 2);
4104  escape_json(es->str, qlabel);
4105  appendStringInfoString(es->str, ": [");
4106  foreach(lc, data)
4107  {
4108  if (!first)
4109  appendStringInfoString(es->str, ", ");
4110  escape_json(es->str, (const char *) lfirst(lc));
4111  first = false;
4112  }
4113  appendStringInfoChar(es->str, ']');
4114  break;
4115 
4116  case EXPLAIN_FORMAT_YAML:
4118  appendStringInfo(es->str, "%s: ", qlabel);
4119  foreach(lc, data)
4120  {
4121  appendStringInfoChar(es->str, '\n');
4122  appendStringInfoSpaces(es->str, es->indent * 2 + 2);
4123  appendStringInfoString(es->str, "- ");
4124  escape_yaml(es->str, (const char *) lfirst(lc));
4125  }
4126  break;
4127  }
4128 }
void escape_json(StringInfo buf, const char *str)
Definition: json.c:1279
static void ExplainXMLTag(const char *tagname, int flags, ExplainState *es)
Definition: explain.c:4651
#define X_CLOSING
Definition: explain.c:52
void pfree(void *pointer)
Definition: mcxt.c:1057
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
static void ExplainYAMLLineStarting(ExplainState *es)
Definition: explain.c:4713
int indent
Definition: explain.h:51
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:188
static void escape_yaml(StringInfo buf, const char *str)
Definition: explain.c:4738
void appendStringInfoSpaces(StringInfo str, int count)
Definition: stringinfo.c:206
static void ExplainJSONLineEnding(ExplainState *es)
Definition: explain.c:4693
#define X_OPENING
Definition: explain.c:51
static void ExplainIndentText(ExplainState *es)
Definition: explain.c:4678
#define lfirst(lc)
Definition: pg_list.h:169
ExplainFormat format
Definition: explain.h:49
char * escape_xml(const char *str)
Definition: xml.c:2353
StringInfo str
Definition: explain.h:39

◆ ExplainPropertyListNested()

void ExplainPropertyListNested ( const char *  qlabel,
List data,
ExplainState es 
)

Definition at line 4135 of file explain.c.

References appendStringInfoChar(), appendStringInfoSpaces(), appendStringInfoString(), escape_json(), escape_yaml(), EXPLAIN_FORMAT_JSON, EXPLAIN_FORMAT_TEXT, EXPLAIN_FORMAT_XML, EXPLAIN_FORMAT_YAML, ExplainJSONLineEnding(), ExplainPropertyList(), ExplainYAMLLineStarting(), ExplainState::format, ExplainState::indent, lfirst, and ExplainState::str.

Referenced by show_grouping_set_keys().

4136 {
4137  ListCell *lc;
4138  bool first = true;
4139 
4140  switch (es->format)
4141  {
4142  case EXPLAIN_FORMAT_TEXT:
4143  case EXPLAIN_FORMAT_XML:
4144  ExplainPropertyList(qlabel, data, es);
4145  return;
4146 
4147  case EXPLAIN_FORMAT_JSON:
4149  appendStringInfoSpaces(es->str, es->indent * 2);
4150  appendStringInfoChar(es->str, '[');
4151  foreach(lc, data)
4152  {
4153  if (!first)
4154  appendStringInfoString(es->str, ", ");
4155  escape_json(es->str, (const char *) lfirst(lc));
4156  first = false;
4157  }
4158  appendStringInfoChar(es->str, ']');
4159  break;
4160 
4161  case EXPLAIN_FORMAT_YAML:
4163  appendStringInfoString(es->str, "- [");
4164  foreach(lc, data)
4165  {
4166  if (!first)
4167  appendStringInfoString(es->str, ", ");
4168  escape_yaml(es->str, (const char *) lfirst(lc));
4169  first = false;
4170  }
4171  appendStringInfoChar(es->str, ']');
4172  break;
4173  }
4174 }
void escape_json(StringInfo buf, const char *str)
Definition: json.c:1279
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
static void ExplainYAMLLineStarting(ExplainState *es)
Definition: explain.c:4713
int indent
Definition: explain.h:51
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:188
static void escape_yaml(StringInfo buf, const char *str)
Definition: explain.c:4738
void appendStringInfoSpaces(StringInfo str, int count)
Definition: stringinfo.c:206
static void ExplainJSONLineEnding(ExplainState *es)
Definition: explain.c:4693
void ExplainPropertyList(const char *qlabel, List *data, ExplainState *es)
Definition: explain.c:4065
#define lfirst(lc)
Definition: pg_list.h:169
ExplainFormat format
Definition: explain.h:49
StringInfo str
Definition: explain.h:39

◆ ExplainPropertyText()

void ExplainPropertyText ( const char *  qlabel,
const char *  value,
ExplainState es 
)

Definition at line 4241 of file explain.c.

References ExplainProperty().

Referenced by ExplainIndexScanDetails(), ExplainNode(), ExplainPrintSettings(), ExplainQueryText(), ExplainTargetRel(), fileExplainForeignScan(), postgresExplainDirectModify(), postgresExplainForeignModify(), postgresExplainForeignScan(), report_triggers(), show_expression(), show_grouping_set_keys(), show_modifytable_info(), show_sort_info(), and show_tablesample().

4242 {
4243  ExplainProperty(qlabel, NULL, value, false, es);
4244 }
static struct @144 value
static void ExplainProperty(const char *qlabel, const char *unit, const char *value, bool numeric, ExplainState *es)
Definition: explain.c:4188

◆ ExplainPropertyUInteger()

void ExplainPropertyUInteger ( const char *  qlabel,
const char *  unit,
uint64  value,
ExplainState es 
)

Definition at line 4263 of file explain.c.

References buf, ExplainProperty(), snprintf, and UINT64_FORMAT.

Referenced by show_wal_usage().

4265 {
4266  char buf[32];
4267 
4268  snprintf(buf, sizeof(buf), UINT64_FORMAT, value);
4269  ExplainProperty(qlabel, unit, buf, true, es);
4270 }
static struct @144 value
static char * buf
Definition: pg_test_fsync.c:68
#define snprintf
Definition: port.h:215
static void ExplainProperty(const char *qlabel, const char *unit, const char *value, bool numeric, ExplainState *es)
Definition: explain.c:4188
#define UINT64_FORMAT
Definition: c.h:472

◆ ExplainQuery()

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

Definition at line 161 of file explain.c.

References ExplainState::analyze, appendStringInfoString(), Assert, begin_tup_output_tupdesc(), ExplainState::buffers, castNode, copyObject, ExplainState::costs, CURSOR_OPT_PARALLEL_OK, StringInfoData::data, defGetBoolean(), defGetString(), DefElem::defname, do_text_output_multiline(), do_text_output_oneline, end_tup_output(), ereport, errcode(), errmsg(), ERROR, EXPLAIN_FORMAT_JSON, EXPLAIN_FORMAT_TEXT, EXPLAIN_FORMAT_XML, EXPLAIN_FORMAT_YAML, ExplainBeginOutput(), ExplainEndOutput(), ExplainOneQuery(), ExplainResultDesc(), ExplainSeparatePlans(), ExplainState::format, ExplainState::indent, lfirst, lfirst_node, lnext(), DefElem::location, NewExplainState(), NIL, ExplainStmt::options, ParseState::p_queryEnv, ParseState::p_sourcetext, parser_errposition(), pfree(), ExplainStmt::query, QueryRewrite(), ExplainState::settings, ExplainState::str, ExplainState::summary, ExplainState::timing, TTSOpsVirtual, ExplainState::verbose, and ExplainState::wal.

Referenced by standard_ProcessUtility().

163 {
165  TupOutputState *tstate;
166  List *rewritten;
167  ListCell *lc;
168  bool timing_set = false;
169  bool summary_set = false;
170 
171  /* Parse options list. */
172  foreach(lc, stmt->options)
173  {
174  DefElem *opt = (DefElem *) lfirst(lc);
175 
176  if (strcmp(opt->defname, "analyze") == 0)
177  es->analyze = defGetBoolean(opt);
178  else if (strcmp(opt->defname, "verbose") == 0)
179  es->verbose = defGetBoolean(opt);
180  else if (strcmp(opt->defname, "costs") == 0)
181  es->costs = defGetBoolean(opt);
182  else if (strcmp(opt->defname, "buffers") == 0)
183  es->buffers = defGetBoolean(opt);
184  else if (strcmp(opt->defname, "wal") == 0)
185  es->wal = defGetBoolean(opt);
186  else if (strcmp(opt->defname, "settings") == 0)
187  es->settings = defGetBoolean(opt);
188  else if (strcmp(opt->defname, "timing") == 0)
189  {
190  timing_set = true;
191  es->timing = defGetBoolean(opt);
192  }
193  else if (strcmp(opt->defname, "summary") == 0)
194  {
195  summary_set = true;
196  es->summary = defGetBoolean(opt);
197  }
198  else if (strcmp(opt->defname, "format") == 0)
199  {
200  char *p = defGetString(opt);
201 
202  if (strcmp(p, "text") == 0)
204  else if (strcmp(p, "xml") == 0)
206  else if (strcmp(p, "json") == 0)
208  else if (strcmp(p, "yaml") == 0)
210  else
211  ereport(ERROR,
212  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
213  errmsg("unrecognized value for EXPLAIN option \"%s\": \"%s\"",
214  opt->defname, p),
215  parser_errposition(pstate, opt->location)));
216  }
217  else
218  ereport(ERROR,
219  (errcode(ERRCODE_SYNTAX_ERROR),
220  errmsg("unrecognized EXPLAIN option \"%s\"",
221  opt->defname),
222  parser_errposition(pstate, opt->location)));
223  }
224 
225  if (es->wal && !es->analyze)
226  ereport(ERROR,
227  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
228  errmsg("EXPLAIN option WAL requires ANALYZE")));
229 
230  /* if the timing was not set explicitly, set default value */
231  es->timing = (timing_set) ? es->timing : es->analyze;
232 
233  /* check that timing is used with EXPLAIN ANALYZE */
234  if (es->timing && !es->analyze)
235  ereport(ERROR,
236  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
237  errmsg("EXPLAIN option TIMING requires ANALYZE")));
238 
239  /* if the summary was not set explicitly, set default value */
240  es->summary = (summary_set) ? es->summary : es->analyze;
241 
242  /*
243  * Parse analysis was done already, but we still have to run the rule
244  * rewriter. We do not do AcquireRewriteLocks: we assume the query either
245  * came straight from the parser, or suitable locks were acquired by
246  * plancache.c.
247  *
248  * Because the rewriter and planner tend to scribble on the input, we make
249  * a preliminary copy of the source querytree. This prevents problems in
250  * the case that the EXPLAIN is in a portal or plpgsql function and is
251  * executed repeatedly. (See also the same hack in DECLARE CURSOR and
252  * PREPARE.) XXX FIXME someday.
253  */
254  rewritten = QueryRewrite(castNode(Query, copyObject(stmt->query)));
255 
256  /* emit opening boilerplate */
257  ExplainBeginOutput(es);
258 
259  if (rewritten == NIL)
260  {
261  /*
262  * In the case of an INSTEAD NOTHING, tell at least that. But in
263  * non-text format, the output is delimited, so this isn't necessary.
264  */
265  if (es->format == EXPLAIN_FORMAT_TEXT)
266  appendStringInfoString(es->str, "Query rewrites to nothing\n");
267  }
268  else
269  {
270  ListCell *l;
271 
272  /* Explain every plan */
273  foreach(l, rewritten)
274  {
276  CURSOR_OPT_PARALLEL_OK, NULL, es,
277  pstate->p_sourcetext, params, pstate->p_queryEnv);
278 
279  /* Separate plans with an appropriate separator */
280  if (lnext(rewritten, l) != NULL)
282  }
283  }
284 
285  /* emit closing boilerplate */
286  ExplainEndOutput(es);
287  Assert(es->indent == 0);
288 
289  /* output tuples */
290  tstate = begin_tup_output_tupdesc(dest, ExplainResultDesc(stmt),
291  &TTSOpsVirtual);
292  if (es->format == EXPLAIN_FORMAT_TEXT)
293  do_text_output_multiline(tstate, es->str->data);
294  else
295  do_text_output_oneline(tstate, es->str->data);
296  end_tup_output(tstate);
297 
298  pfree(es->str->data);
299 }
#define NIL
Definition: pg_list.h:65
bool summary
Definition: explain.h:47
ExplainState * NewExplainState(void)
Definition: explain.c:305
List * QueryRewrite(Query *parsetree)
void ExplainSeparatePlans(ExplainState *es)
Definition: explain.c:4622
static ListCell * lnext(const List *l, const ListCell *c)
Definition: pg_list.h:310
#define castNode(_type_, nodeptr)
Definition: nodes.h:597
QueryEnvironment * p_queryEnv
Definition: parse_node.h:203
const TupleTableSlotOps TTSOpsVirtual
Definition: execTuples.c:83
int errcode(int sqlerrcode)
Definition: elog.c:704
List * options
Definition: parsenodes.h:3268
bool costs
Definition: explain.h:43
bool analyze
Definition: explain.h:42
Node * query
Definition: parsenodes.h:3267
void ExplainEndOutput(ExplainState *es)
Definition: explain.c:4593
bool defGetBoolean(DefElem *def)
Definition: define.c:111
void pfree(void *pointer)
Definition: mcxt.c:1057
void end_tup_output(TupOutputState *tstate)
Definition: execTuples.c:2314
void ExplainBeginOutput(ExplainState *es)
Definition: explain.c:4562
#define ERROR
Definition: elog.h:45
TupleDesc ExplainResultDesc(ExplainStmt *stmt)
Definition: explain.c:322
char * defGetString(DefElem *def)
Definition: define.c:49
TupOutputState * begin_tup_output_tupdesc(DestReceiver *dest, TupleDesc tupdesc, const TupleTableSlotOps *tts_ops)
Definition: execTuples.c:2236
#define lfirst_node(type, lc)
Definition: pg_list.h:172
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
int location
Definition: parsenodes.h:736
int indent
Definition: explain.h:51
const char * p_sourcetext
Definition: parse_node.h:179
bool timing
Definition: explain.h:46
static void ExplainOneQuery(Query *query, int cursorOptions, IntoClause *into, ExplainState *es, const char *queryString, ParamListInfo params, QueryEnvironment *queryEnv)
Definition: explain.c:361
void do_text_output_multiline(TupOutputState *tstate, const char *txt)
Definition: execTuples.c:2284
bool verbose
Definition: explain.h:41
#define ereport(elevel,...)
Definition: elog.h:155
#define Assert(condition)
Definition: c.h:792
#define lfirst(lc)
Definition: pg_list.h:169
bool wal
Definition: explain.h:45
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
ExplainFormat format
Definition: explain.h:49
#define do_text_output_oneline(tstate, str_to_emit)
Definition: executor.h:481
#define CURSOR_OPT_PARALLEL_OK
Definition: parsenodes.h:2732
int errmsg(const char *fmt,...)
Definition: elog.c:915
bool buffers
Definition: explain.h:44
char * defname
Definition: parsenodes.h:733
#define copyObject(obj)
Definition: nodes.h:644
Definition: pg_list.h:50
StringInfo str
Definition: explain.h:39
bool settings
Definition: explain.h:48

◆ ExplainQueryText()

void ExplainQueryText ( ExplainState es,
QueryDesc queryDesc 
)

Definition at line 947 of file explain.c.

References ExplainPropertyText(), and QueryDesc::sourceText.

Referenced by explain_ExecutorEnd().

948 {
949  if (queryDesc->sourceText)
950  ExplainPropertyText("Query Text", queryDesc->sourceText, es);
951 }
void ExplainPropertyText(const char *qlabel, const char *value, ExplainState *es)
Definition: explain.c:4241
const char * sourceText
Definition: execdesc.h:38

◆ ExplainResultDesc()

TupleDesc ExplainResultDesc ( ExplainStmt stmt)

Definition at line 322 of file explain.c.

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

Referenced by ExplainQuery(), and UtilityTupleDescriptor().

323 {
324  TupleDesc tupdesc;
325  ListCell *lc;
326  Oid result_type = TEXTOID;
327 
328  /* Check for XML format option */
329  foreach(lc, stmt->options)
330  {
331  DefElem *opt = (DefElem *) lfirst(lc);
332 
333  if (strcmp(opt->defname, "format") == 0)
334  {
335  char *p = defGetString(opt);
336 
337  if (strcmp(p, "xml") == 0)
338  result_type = XMLOID;
339  else if (strcmp(p, "json") == 0)
340  result_type = JSONOID;
341  else
342  result_type = TEXTOID;
343  /* don't "break", as ExplainQuery will use the last value */
344  }
345  }
346 
347  /* Need a tuple descriptor representing a single TEXT or XML column */
348  tupdesc = CreateTemplateTupleDesc(1);
349  TupleDescInitEntry(tupdesc, (AttrNumber) 1, "QUERY PLAN",
350  result_type, -1, 0);
351  return tupdesc;
352 }
TupleDesc CreateTemplateTupleDesc(int natts)
Definition: tupdesc.c:44
List * options
Definition: parsenodes.h:3268
unsigned int Oid
Definition: postgres_ext.h:31
char * defGetString(DefElem *def)
Definition: define.c:49
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition: tupdesc.c:603
#define lfirst(lc)
Definition: pg_list.h:169
char * defname
Definition: parsenodes.h:733
int16 AttrNumber
Definition: attnum.h:21

◆ ExplainSeparatePlans()

void ExplainSeparatePlans ( ExplainState es)

Definition at line 4622 of file explain.c.

References appendStringInfoChar(), EXPLAIN_FORMAT_JSON, EXPLAIN_FORMAT_TEXT, EXPLAIN_FORMAT_XML, EXPLAIN_FORMAT_YAML, ExplainState::format, and ExplainState::str.

Referenced by ExplainExecuteQuery(), and ExplainQuery().

4623 {
4624  switch (es->format)
4625  {
4626  case EXPLAIN_FORMAT_TEXT:
4627  /* add a blank line */
4628  appendStringInfoChar(es->str, '\n');
4629  break;
4630 
4631  case EXPLAIN_FORMAT_XML:
4632  case EXPLAIN_FORMAT_JSON:
4633  case EXPLAIN_FORMAT_YAML:
4634  /* nothing to do */
4635  break;
4636  }
4637 }
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:188
ExplainFormat format
Definition: explain.h:49
StringInfo str
Definition: explain.h:39

◆ NewExplainState()

ExplainState* NewExplainState ( void  )

Definition at line 305 of file explain.c.

References ExplainState::costs, makeStringInfo(), palloc0(), and ExplainState::str.

Referenced by explain_ExecutorEnd(), and ExplainQuery().

306 {
307  ExplainState *es = (ExplainState *) palloc0(sizeof(ExplainState));
308 
309  /* Set default options (most fields can be left as zeroes). */
310  es->costs = true;
311  /* Prepare output buffer. */
312  es->str = makeStringInfo();
313 
314  return es;
315 }
StringInfo makeStringInfo(void)
Definition: stringinfo.c:41
bool costs
Definition: explain.h:43
void * palloc0(Size size)
Definition: mcxt.c:981
StringInfo str
Definition: explain.h:39

Variable Documentation

◆ explain_get_index_name_hook

PGDLLIMPORT explain_get_index_name_hook_type explain_get_index_name_hook

Definition at line 47 of file explain.c.

Referenced by explain_get_index_name().

◆ ExplainOneQuery_hook

PGDLLIMPORT ExplainOneQuery_hook_type ExplainOneQuery_hook

Definition at line 44 of file explain.c.

Referenced by ExplainOneQuery().