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

4459 {
4460  switch (es->format)
4461  {
4462  case EXPLAIN_FORMAT_TEXT:
4463  /* nothing to do */
4464  break;
4465 
4466  case EXPLAIN_FORMAT_XML:
4468  "<explain xmlns=\"http://www.postgresql.org/2009/explain\">\n");
4469  es->indent++;
4470  break;
4471 
4472  case EXPLAIN_FORMAT_JSON:
4473  /* top-level structure is an array of plans */
4474  appendStringInfoChar(es->str, '[');
4475  es->grouping_stack = lcons_int(0, es->grouping_stack);
4476  es->indent++;
4477  break;
4478 
4479  case EXPLAIN_FORMAT_YAML:
4480  es->grouping_stack = lcons_int(0, es->grouping_stack);
4481  break;
4482  }
4483 }
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 4265 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().

4267 {
4268  switch (es->format)
4269  {
4270  case EXPLAIN_FORMAT_TEXT:
4271  /* nothing to do */
4272  break;
4273 
4274  case EXPLAIN_FORMAT_XML:
4275  es->indent--;
4276  ExplainXMLTag(objtype, X_CLOSING, es);
4277  break;
4278 
4279  case EXPLAIN_FORMAT_JSON:
4280  es->indent--;
4281  appendStringInfoChar(es->str, '\n');
4282  appendStringInfoSpaces(es->str, 2 * es->indent);
4283  appendStringInfoChar(es->str, labeled ? '}' : ']');
4285  break;
4286 
4287  case EXPLAIN_FORMAT_YAML:
4288  es->indent--;
4290  break;
4291  }
4292 }
static void ExplainXMLTag(const char *tagname, int flags, ExplainState *es)
Definition: explain.c:4547
#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 4489 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().

4490 {
4491  switch (es->format)
4492  {
4493  case EXPLAIN_FORMAT_TEXT:
4494  /* nothing to do */
4495  break;
4496 
4497  case EXPLAIN_FORMAT_XML:
4498  es->indent--;
4499  appendStringInfoString(es->str, "</explain>");
4500  break;
4501 
4502  case EXPLAIN_FORMAT_JSON:
4503  es->indent--;
4504  appendStringInfoString(es->str, "\n]");
4506  break;
4507 
4508  case EXPLAIN_FORMAT_YAML:
4510  break;
4511  }
4512 }
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 500 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(), EXPLAIN_FORMAT_TEXT, ExplainCloseGroup(), ExplainOpenGroup(), ExplainPrintJITSummary(), ExplainPrintPlan(), ExplainPrintTriggers(), ExplainPropertyFloat(), ExplainState::format, ForwardScanDirection, FreeQueryDesc(), GetActiveSnapshot(), GetIntoRelEFlags(), ExplainState::indent, 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().

504 {
506  QueryDesc *queryDesc;
507  instr_time starttime;
508  double totaltime = 0;
509  int eflags;
510  int instrument_option = 0;
511 
512  Assert(plannedstmt->commandType != CMD_UTILITY);
513 
514  if (es->analyze && es->timing)
515  instrument_option |= INSTRUMENT_TIMER;
516  else if (es->analyze)
517  instrument_option |= INSTRUMENT_ROWS;
518 
519  if (es->buffers)
520  instrument_option |= INSTRUMENT_BUFFERS;
521  if (es->wal)
522  instrument_option |= INSTRUMENT_WAL;
523 
524  /*
525  * We always collect timing for the entire statement, even when node-level
526  * timing is off, so we don't look at es->timing here. (We could skip
527  * this if !es->summary, but it's hardly worth the complication.)
528  */
529  INSTR_TIME_SET_CURRENT(starttime);
530 
531  /*
532  * Use a snapshot with an updated command ID to ensure this query sees
533  * results of any previously executed queries.
534  */
537 
538  /*
539  * Normally we discard the query's output, but if explaining CREATE TABLE
540  * AS, we'd better use the appropriate tuple receiver.
541  */
542  if (into)
543  dest = CreateIntoRelDestReceiver(into);
544  else
545  dest = None_Receiver;
546 
547  /* Create a QueryDesc for the query */
548  queryDesc = CreateQueryDesc(plannedstmt, queryString,
550  dest, params, queryEnv, instrument_option);
551 
552  /* Select execution options */
553  if (es->analyze)
554  eflags = 0; /* default run-to-completion flags */
555  else
556  eflags = EXEC_FLAG_EXPLAIN_ONLY;
557  if (into)
558  eflags |= GetIntoRelEFlags(into);
559 
560  /* call ExecutorStart to prepare the plan for execution */
561  ExecutorStart(queryDesc, eflags);
562 
563  /* Execute the plan for statistics if asked for */
564  if (es->analyze)
565  {
566  ScanDirection dir;
567 
568  /* EXPLAIN ANALYZE CREATE TABLE AS WITH NO DATA is weird */
569  if (into && into->skipData)
571  else
572  dir = ForwardScanDirection;
573 
574  /* run the plan */
575  ExecutorRun(queryDesc, dir, 0L, true);
576 
577  /* run cleanup too */
578  ExecutorFinish(queryDesc);
579 
580  /* We can't run ExecutorEnd 'till we're done printing the stats... */
581  totaltime += elapsed_time(&starttime);
582  }
583 
584  ExplainOpenGroup("Query", NULL, true, es);
585 
586  /* Create textual dump of plan tree */
587  ExplainPrintPlan(es, queryDesc);
588 
589  if (es->summary && (planduration || bufusage))
590  ExplainOpenGroup("Planning", "Planning", true, es);
591 
592  if (es->summary && planduration)
593  {
594  double plantime = INSTR_TIME_GET_DOUBLE(*planduration);
595 
596  ExplainPropertyFloat("Planning Time", "ms", 1000.0 * plantime, 3, es);
597  }
598 
599  /* Show buffer usage */
600  if (es->summary && bufusage)
601  {
602  if (es->format == EXPLAIN_FORMAT_TEXT)
603  es->indent++;
604  show_buffer_usage(es, bufusage);
605  if (es->format == EXPLAIN_FORMAT_TEXT)
606  es->indent--;
607  }
608 
609  if (es->summary && (planduration || bufusage))
610  ExplainCloseGroup("Planning", "Planning", true, es);
611 
612  /* Print info about runtime of triggers */
613  if (es->analyze)
614  ExplainPrintTriggers(es, queryDesc);
615 
616  /*
617  * Print info about JITing. Tied to es->costs because we don't want to
618  * display this in regression tests, as it'd cause output differences
619  * depending on build options. Might want to separate that out from COSTS
620  * at a later stage.
621  */
622  if (es->costs)
623  ExplainPrintJITSummary(es, queryDesc);
624 
625  /*
626  * Close down the query and free resources. Include time for this in the
627  * total execution time (although it should be pretty minimal).
628  */
629  INSTR_TIME_SET_CURRENT(starttime);
630 
631  ExecutorEnd(queryDesc);
632 
633  FreeQueryDesc(queryDesc);
634 
636 
637  /* We need a CCI just in case query expanded to multiple plans */
638  if (es->analyze)
640 
641  totaltime += elapsed_time(&starttime);
642 
643  /*
644  * We only report execution time if we actually ran the query (that is,
645  * the user specified ANALYZE), and if summary reporting is enabled (the
646  * user can set SUMMARY OFF to not have the timing information included in
647  * the output). By default, ANALYZE sets SUMMARY to true.
648  */
649  if (es->summary && es->analyze)
650  ExplainPropertyFloat("Execution Time", "ms", 1000.0 * totaltime, 3,
651  es);
652 
653  ExplainCloseGroup("Query", NULL, true, es);
654 }
bool summary
Definition: explain.h:47
void UpdateActiveSnapshotCommandId(void)
Definition: snapmgr.c:783
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:4173
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:841
void PopActiveSnapshot(void)
Definition: snapmgr.c:814
#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:393
void ExplainPrintTriggers(ExplainState *es, QueryDesc *queryDesc)
Definition: explain.c:780
void ExplainPrintPlan(ExplainState *es, QueryDesc *queryDesc)
Definition: explain.c:731
void ExecutorRun(QueryDesc *queryDesc, ScanDirection direction, uint64 count, bool execute_once)
Definition: execMain.c:301
void PushCopiedSnapshot(Snapshot snapshot)
Definition: snapmgr.c:771
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:411
int indent
Definition: explain.h:51
static void show_buffer_usage(ExplainState *es, const BufferUsage *usage)
Definition: explain.c:3213
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:1006
CmdType commandType
Definition: plannodes.h:46
#define Assert(condition)
Definition: c.h:738
bool wal
Definition: explain.h:45
ExplainFormat format
Definition: explain.h:49
static double elapsed_time(instr_time *starttime)
Definition: explain.c:1028
#define INSTR_TIME_SET_CURRENT(t)
Definition: instr_time.h:156
bool buffers
Definition: explain.h:44
void ExplainCloseGroup(const char *objtype, const char *labelname, bool labeled, ExplainState *es)
Definition: explain.c:4265
void ExplainPrintJITSummary(ExplainState *es, QueryDesc *queryDesc)
Definition: explain.c:826
void ExplainOpenGroup(const char *objtype, const char *labelname, bool labeled, ExplainState *es)
Definition: explain.c:4202
#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 425 of file explain.c.

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

Referenced by ExplainExecuteQuery(), and ExplainOneQuery().

428 {
429  if (utilityStmt == NULL)
430  return;
431 
432  if (IsA(utilityStmt, CreateTableAsStmt))
433  {
434  /*
435  * We have to rewrite the contained SELECT and then pass it back to
436  * ExplainOneQuery. It's probably not really necessary to copy the
437  * contained parsetree another time, but let's be safe.
438  */
439  CreateTableAsStmt *ctas = (CreateTableAsStmt *) utilityStmt;
440  List *rewritten;
441 
442  rewritten = QueryRewrite(castNode(Query, copyObject(ctas->query)));
443  Assert(list_length(rewritten) == 1);
444  ExplainOneQuery(linitial_node(Query, rewritten),
445  CURSOR_OPT_PARALLEL_OK, ctas->into, es,
446  queryString, params, queryEnv);
447  }
448  else if (IsA(utilityStmt, DeclareCursorStmt))
449  {
450  /*
451  * Likewise for DECLARE CURSOR.
452  *
453  * Notice that if you say EXPLAIN ANALYZE DECLARE CURSOR then we'll
454  * actually run the query. This is different from pre-8.3 behavior
455  * but seems more useful than not running the query. No cursor will
456  * be created, however.
457  */
458  DeclareCursorStmt *dcs = (DeclareCursorStmt *) utilityStmt;
459  List *rewritten;
460 
461  rewritten = QueryRewrite(castNode(Query, copyObject(dcs->query)));
462  Assert(list_length(rewritten) == 1);
463  ExplainOneQuery(linitial_node(Query, rewritten),
464  dcs->options, NULL, es,
465  queryString, params, queryEnv);
466  }
467  else if (IsA(utilityStmt, ExecuteStmt))
468  ExplainExecuteQuery((ExecuteStmt *) utilityStmt, into, es,
469  queryString, params, queryEnv);
470  else if (IsA(utilityStmt, NotifyStmt))
471  {
472  if (es->format == EXPLAIN_FORMAT_TEXT)
473  appendStringInfoString(es->str, "NOTIFY\n");
474  else
475  ExplainDummyGroup("Notify", NULL, es);
476  }
477  else
478  {
479  if (es->format == EXPLAIN_FORMAT_TEXT)
481  "Utility statements have no plan structure\n");
482  else
483  ExplainDummyGroup("Utility Statement", NULL, es);
484  }
485 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:580
List * QueryRewrite(Query *parsetree)
#define castNode(_type_, nodeptr)
Definition: nodes.h:598
#define linitial_node(type, l)
Definition: pg_list.h:198
static void ExplainDummyGroup(const char *objtype, const char *labelname, ExplainState *es)
Definition: explain.c:4412
void ExplainExecuteQuery(ExecuteStmt *execstmt, IntoClause *into, ExplainState *es, const char *queryString, ParamListInfo params, QueryEnvironment *queryEnv)
Definition: prepare.c:606
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
static void ExplainOneQuery(Query *query, int cursorOptions, IntoClause *into, ExplainState *es, const char *queryString, ParamListInfo params, QueryEnvironment *queryEnv)
Definition: explain.c:365
IntoClause * into
Definition: parsenodes.h:3277
#define Assert(condition)
Definition: c.h:738
static int list_length(const List *l)
Definition: pg_list.h:169
ExplainFormat format
Definition: explain.h:49
#define CURSOR_OPT_PARALLEL_OK
Definition: parsenodes.h:2715
#define copyObject(obj)
Definition: nodes.h:645
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 4202 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().

4204 {
4205  switch (es->format)
4206  {
4207  case EXPLAIN_FORMAT_TEXT:
4208  /* nothing to do */
4209  break;
4210 
4211  case EXPLAIN_FORMAT_XML:
4212  ExplainXMLTag(objtype, X_OPENING, es);
4213  es->indent++;
4214  break;
4215 
4216  case EXPLAIN_FORMAT_JSON:
4218  appendStringInfoSpaces(es->str, 2 * es->indent);
4219  if (labelname)
4220  {
4221  escape_json(es->str, labelname);
4222  appendStringInfoString(es->str, ": ");
4223  }
4224  appendStringInfoChar(es->str, labeled ? '{' : '[');
4225 
4226  /*
4227  * In JSON format, the grouping_stack is an integer list. 0 means
4228  * we've emitted nothing at this grouping level, 1 means we've
4229  * emitted something (and so the next item needs a comma). See
4230  * ExplainJSONLineEnding().
4231  */
4232  es->grouping_stack = lcons_int(0, es->grouping_stack);
4233  es->indent++;
4234  break;
4235 
4236  case EXPLAIN_FORMAT_YAML:
4237 
4238  /*
4239  * In YAML format, the grouping stack is an integer list. 0 means
4240  * we've emitted nothing at this grouping level AND this grouping
4241  * level is unlabelled and must be marked with "- ". See
4242  * ExplainYAMLLineStarting().
4243  */
4245  if (labelname)
4246  {
4247  appendStringInfo(es->str, "%s: ", labelname);
4248  es->grouping_stack = lcons_int(1, es->grouping_stack);
4249  }
4250  else
4251  {
4252  appendStringInfoString(es->str, "- ");
4253  es->grouping_stack = lcons_int(0, es->grouping_stack);
4254  }
4255  es->indent++;
4256  break;
4257  }
4258 }
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:4547
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:4609
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:4589
#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 826 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().

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

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

◆ ExplainPrintTriggers()

void ExplainPrintTriggers ( ExplainState es,
QueryDesc queryDesc 
)

Definition at line 780 of file explain.c.

References EState::es_num_result_relations, EState::es_num_root_result_relations, EState::es_result_relations, EState::es_root_result_relations, EState::es_trig_target_relations, EState::es_tuple_routing_result_relations, QueryDesc::estate, ExplainCloseGroup(), ExplainOpenGroup(), lfirst, NIL, and report_triggers().

Referenced by explain_ExecutorEnd(), and ExplainOnePlan().

781 {
782  ResultRelInfo *rInfo;
783  bool show_relname;
784  int numrels = queryDesc->estate->es_num_result_relations;
785  int numrootrels = queryDesc->estate->es_num_root_result_relations;
786  List *routerels;
787  List *targrels;
788  int nr;
789  ListCell *l;
790 
791  routerels = queryDesc->estate->es_tuple_routing_result_relations;
792  targrels = queryDesc->estate->es_trig_target_relations;
793 
794  ExplainOpenGroup("Triggers", "Triggers", false, es);
795 
796  show_relname = (numrels > 1 || numrootrels > 0 ||
797  routerels != NIL || targrels != NIL);
798  rInfo = queryDesc->estate->es_result_relations;
799  for (nr = 0; nr < numrels; rInfo++, nr++)
800  report_triggers(rInfo, show_relname, es);
801 
802  rInfo = queryDesc->estate->es_root_result_relations;
803  for (nr = 0; nr < numrootrels; rInfo++, nr++)
804  report_triggers(rInfo, show_relname, es);
805 
806  foreach(l, routerels)
807  {
808  rInfo = (ResultRelInfo *) lfirst(l);
809  report_triggers(rInfo, show_relname, es);
810  }
811 
812  foreach(l, targrels)
813  {
814  rInfo = (ResultRelInfo *) lfirst(l);
815  report_triggers(rInfo, show_relname, es);
816  }
817 
818  ExplainCloseGroup("Triggers", "Triggers", false, es);
819 }
#define NIL
Definition: pg_list.h:65
EState * estate
Definition: execdesc.h:48
ResultRelInfo * es_result_relations
Definition: execnodes.h:525
static void report_triggers(ResultRelInfo *rInfo, bool show_relname, ExplainState *es)
Definition: explain.c:957
int es_num_root_result_relations
Definition: execnodes.h:536
List * es_trig_target_relations
Definition: execnodes.h:546
int es_num_result_relations
Definition: execnodes.h:526
List * es_tuple_routing_result_relations
Definition: execnodes.h:543
#define lfirst(lc)
Definition: pg_list.h:190
ResultRelInfo * es_root_result_relations
Definition: execnodes.h:535
void ExplainCloseGroup(const char *objtype, const char *labelname, bool labeled, ExplainState *es)
Definition: explain.c:4265
void ExplainOpenGroup(const char *objtype, const char *labelname, bool labeled, ExplainState *es)
Definition: explain.c:4202
Definition: pg_list.h:50

◆ ExplainPropertyBool()

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

Definition at line 4187 of file explain.c.

References ExplainProperty().

Referenced by ExplainNode(), and ExplainPrintJIT().

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

◆ ExplainPropertyFloat()

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

Definition at line 4173 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().

4175 {
4176  char *buf;
4177 
4178  buf = psprintf("%.*f", ndigits, value);
4179  ExplainProperty(qlabel, unit, buf, true, es);
4180  pfree(buf);
4181 }
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
void pfree(void *pointer)
Definition: mcxt.c:1056
static char * buf
Definition: pg_test_fsync.c:67
static struct @143 value
static void ExplainProperty(const char *qlabel, const char *unit, const char *value, bool numeric, ExplainState *es)
Definition: explain.c:4084

◆ ExplainPropertyInteger()

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

Definition at line 4146 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().

4148 {
4149  char buf[32];
4150 
4151  snprintf(buf, sizeof(buf), INT64_FORMAT, value);
4152  ExplainProperty(qlabel, unit, buf, true, es);
4153 }
static char * buf
Definition: pg_test_fsync.c:67
static struct @143 value
#define INT64_FORMAT
Definition: c.h:409
#define snprintf
Definition: port.h:193
static void ExplainProperty(const char *qlabel, const char *unit, const char *value, bool numeric, ExplainState *es)
Definition: explain.c:4084

◆ ExplainPropertyList()

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

Definition at line 3961 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().

3962 {
3963  ListCell *lc;
3964  bool first = true;
3965 
3966  switch (es->format)
3967  {
3968  case EXPLAIN_FORMAT_TEXT:
3969  ExplainIndentText(es);
3970  appendStringInfo(es->str, "%s: ", qlabel);
3971  foreach(lc, data)
3972  {
3973  if (!first)
3974  appendStringInfoString(es->str, ", ");
3975  appendStringInfoString(es->str, (const char *) lfirst(lc));
3976  first = false;
3977  }
3978  appendStringInfoChar(es->str, '\n');
3979  break;
3980 
3981  case EXPLAIN_FORMAT_XML:
3982  ExplainXMLTag(qlabel, X_OPENING, es);
3983  foreach(lc, data)
3984  {
3985  char *str;
3986 
3987  appendStringInfoSpaces(es->str, es->indent * 2 + 2);
3988  appendStringInfoString(es->str, "<Item>");
3989  str = escape_xml((const char *) lfirst(lc));
3990  appendStringInfoString(es->str, str);
3991  pfree(str);
3992  appendStringInfoString(es->str, "</Item>\n");
3993  }
3994  ExplainXMLTag(qlabel, X_CLOSING, es);
3995  break;
3996 
3997  case EXPLAIN_FORMAT_JSON:
3999  appendStringInfoSpaces(es->str, es->indent * 2);
4000  escape_json(es->str, qlabel);
4001  appendStringInfoString(es->str, ": [");
4002  foreach(lc, data)
4003  {
4004  if (!first)
4005  appendStringInfoString(es->str, ", ");
4006  escape_json(es->str, (const char *) lfirst(lc));
4007  first = false;
4008  }
4009  appendStringInfoChar(es->str, ']');
4010  break;
4011 
4012  case EXPLAIN_FORMAT_YAML:
4014  appendStringInfo(es->str, "%s: ", qlabel);
4015  foreach(lc, data)
4016  {
4017  appendStringInfoChar(es->str, '\n');
4018  appendStringInfoSpaces(es->str, es->indent * 2 + 2);
4019  appendStringInfoString(es->str, "- ");
4020  escape_yaml(es->str, (const char *) lfirst(lc));
4021  }
4022  break;
4023  }
4024 }
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:4547
#define X_CLOSING
Definition: explain.c:52
void pfree(void *pointer)
Definition: mcxt.c:1056
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:4609
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:4634
void appendStringInfoSpaces(StringInfo str, int count)
Definition: stringinfo.c:206
static void ExplainJSONLineEnding(ExplainState *es)
Definition: explain.c:4589
#define X_OPENING
Definition: explain.c:51
static void ExplainIndentText(ExplainState *es)
Definition: explain.c:4574
#define lfirst(lc)
Definition: pg_list.h:190
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 4031 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().

4032 {
4033  ListCell *lc;
4034  bool first = true;
4035 
4036  switch (es->format)
4037  {
4038  case EXPLAIN_FORMAT_TEXT:
4039  case EXPLAIN_FORMAT_XML:
4040  ExplainPropertyList(qlabel, data, es);
4041  return;
4042 
4043  case EXPLAIN_FORMAT_JSON:
4045  appendStringInfoSpaces(es->str, es->indent * 2);
4046  appendStringInfoChar(es->str, '[');
4047  foreach(lc, data)
4048  {
4049  if (!first)
4050  appendStringInfoString(es->str, ", ");
4051  escape_json(es->str, (const char *) lfirst(lc));
4052  first = false;
4053  }
4054  appendStringInfoChar(es->str, ']');
4055  break;
4056 
4057  case EXPLAIN_FORMAT_YAML:
4059  appendStringInfoString(es->str, "- [");
4060  foreach(lc, data)
4061  {
4062  if (!first)
4063  appendStringInfoString(es->str, ", ");
4064  escape_yaml(es->str, (const char *) lfirst(lc));
4065  first = false;
4066  }
4067  appendStringInfoChar(es->str, ']');
4068  break;
4069  }
4070 }
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:4609
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:4634
void appendStringInfoSpaces(StringInfo str, int count)
Definition: stringinfo.c:206
static void ExplainJSONLineEnding(ExplainState *es)
Definition: explain.c:4589
void ExplainPropertyList(const char *qlabel, List *data, ExplainState *es)
Definition: explain.c:3961
#define lfirst(lc)
Definition: pg_list.h:190
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 4137 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().

4138 {
4139  ExplainProperty(qlabel, NULL, value, false, es);
4140 }
static struct @143 value
static void ExplainProperty(const char *qlabel, const char *unit, const char *value, bool numeric, ExplainState *es)
Definition: explain.c:4084

◆ ExplainPropertyUInteger()

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

Definition at line 4159 of file explain.c.

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

Referenced by show_wal_usage().

4161 {
4162  char buf[32];
4163 
4164  snprintf(buf, sizeof(buf), UINT64_FORMAT, value);
4165  ExplainProperty(qlabel, unit, buf, true, es);
4166 }
static char * buf
Definition: pg_test_fsync.c:67
static struct @143 value
#define snprintf
Definition: port.h:193
static void ExplainProperty(const char *qlabel, const char *unit, const char *value, bool numeric, ExplainState *es)
Definition: explain.c:4084
#define UINT64_FORMAT
Definition: c.h:410

◆ ExplainQuery()

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

Definition at line 160 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().

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

References ExplainPropertyText(), and QueryDesc::sourceText.

Referenced by explain_ExecutorEnd().

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

◆ ExplainResultDesc()

TupleDesc ExplainResultDesc ( ExplainStmt stmt)

Definition at line 326 of file explain.c.

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

Referenced by ExplainQuery(), and UtilityTupleDescriptor().

327 {
328  TupleDesc tupdesc;
329  ListCell *lc;
330  Oid result_type = TEXTOID;
331 
332  /* Check for XML format option */
333  foreach(lc, stmt->options)
334  {
335  DefElem *opt = (DefElem *) lfirst(lc);
336 
337  if (strcmp(opt->defname, "format") == 0)
338  {
339  char *p = defGetString(opt);
340 
341  if (strcmp(p, "xml") == 0)
342  result_type = XMLOID;
343  else if (strcmp(p, "json") == 0)
344  result_type = JSONOID;
345  else
346  result_type = TEXTOID;
347  /* don't "break", as ExplainQuery will use the last value */
348  }
349  }
350 
351  /* Need a tuple descriptor representing a single TEXT or XML column */
352  tupdesc = CreateTemplateTupleDesc(1);
353  TupleDescInitEntry(tupdesc, (AttrNumber) 1, "QUERY PLAN",
354  result_type, -1, 0);
355  return tupdesc;
356 }
TupleDesc CreateTemplateTupleDesc(int natts)
Definition: tupdesc.c:44
List * options
Definition: parsenodes.h:3257
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:190
char * defname
Definition: parsenodes.h:732
int16 AttrNumber
Definition: attnum.h:21

◆ ExplainSeparatePlans()

void ExplainSeparatePlans ( ExplainState es)

Definition at line 4518 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().

4519 {
4520  switch (es->format)
4521  {
4522  case EXPLAIN_FORMAT_TEXT:
4523  /* add a blank line */
4524  appendStringInfoChar(es->str, '\n');
4525  break;
4526 
4527  case EXPLAIN_FORMAT_XML:
4528  case EXPLAIN_FORMAT_JSON:
4529  case EXPLAIN_FORMAT_YAML:
4530  /* nothing to do */
4531  break;
4532  }
4533 }
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 309 of file explain.c.

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

Referenced by explain_ExecutorEnd(), and ExplainQuery().

310 {
311  ExplainState *es = (ExplainState *) palloc0(sizeof(ExplainState));
312 
313  /* Set default options (most fields can be left as zeroes). */
314  es->costs = true;
315  /* Prepare output buffer. */
316  es->str = makeStringInfo();
317 
318  return es;
319 }
StringInfo makeStringInfo(void)
Definition: stringinfo.c:41
bool costs
Definition: explain.h:43
void * palloc0(Size size)
Definition: mcxt.c:980
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().