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

4745 {
4746  switch (es->format)
4747  {
4748  case EXPLAIN_FORMAT_TEXT:
4749  /* nothing to do */
4750  break;
4751 
4752  case EXPLAIN_FORMAT_XML:
4754  "<explain xmlns=\"http://www.postgresql.org/2009/explain\">\n");
4755  es->indent++;
4756  break;
4757 
4758  case EXPLAIN_FORMAT_JSON:
4759  /* top-level structure is an array of plans */
4760  appendStringInfoChar(es->str, '[');
4761  es->grouping_stack = lcons_int(0, es->grouping_stack);
4762  es->indent++;
4763  break;
4764 
4765  case EXPLAIN_FORMAT_YAML:
4766  es->grouping_stack = lcons_int(0, es->grouping_stack);
4767  break;
4768  }
4769 }
List * lcons_int(int datum, List *list)
Definition: list.c:486
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 4551 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().

4553 {
4554  switch (es->format)
4555  {
4556  case EXPLAIN_FORMAT_TEXT:
4557  /* nothing to do */
4558  break;
4559 
4560  case EXPLAIN_FORMAT_XML:
4561  es->indent--;
4562  ExplainXMLTag(objtype, X_CLOSING, es);
4563  break;
4564 
4565  case EXPLAIN_FORMAT_JSON:
4566  es->indent--;
4567  appendStringInfoChar(es->str, '\n');
4568  appendStringInfoSpaces(es->str, 2 * es->indent);
4569  appendStringInfoChar(es->str, labeled ? '}' : ']');
4571  break;
4572 
4573  case EXPLAIN_FORMAT_YAML:
4574  es->indent--;
4576  break;
4577  }
4578 }
static void ExplainXMLTag(const char *tagname, int flags, ExplainState *es)
Definition: explain.c:4833
#define X_CLOSING
Definition: explain.c:53
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:875

◆ ExplainEndOutput()

void ExplainEndOutput ( ExplainState es)

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

4776 {
4777  switch (es->format)
4778  {
4779  case EXPLAIN_FORMAT_TEXT:
4780  /* nothing to do */
4781  break;
4782 
4783  case EXPLAIN_FORMAT_XML:
4784  es->indent--;
4785  appendStringInfoString(es->str, "</explain>");
4786  break;
4787 
4788  case EXPLAIN_FORMAT_JSON:
4789  es->indent--;
4790  appendStringInfoString(es->str, "\n]");
4792  break;
4793 
4794  case EXPLAIN_FORMAT_YAML:
4796  break;
4797  }
4798 }
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:875

◆ 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 524 of file explain.c.

References ExplainState::analyze, Assert, buf, 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(), ExplainPropertyText(), ForwardScanDirection, FreeQueryDesc(), GetActiveSnapshot(), GetIntoRelEFlags(), INSTR_TIME_GET_DOUBLE, INSTR_TIME_SET_CURRENT, INSTRUMENT_BUFFERS, INSTRUMENT_ROWS, INSTRUMENT_TIMER, INSTRUMENT_WAL, InvalidSnapshot, MAXINT8LEN, NoMovementScanDirection, None_Receiver, pg_lltoa(), PopActiveSnapshot(), PushCopiedSnapshot(), PlannedStmt::queryId, show_buffer_usage(), IntoClause::skipData, ExplainState::summary, ExplainState::timing, UpdateActiveSnapshotCommandId(), ExplainState::verbose, and ExplainState::wal.

Referenced by ExplainExecuteQuery(), and ExplainOneQuery().

528 {
530  QueryDesc *queryDesc;
531  instr_time starttime;
532  double totaltime = 0;
533  int eflags;
534  int instrument_option = 0;
535 
536  Assert(plannedstmt->commandType != CMD_UTILITY);
537 
538  if (es->analyze && es->timing)
539  instrument_option |= INSTRUMENT_TIMER;
540  else if (es->analyze)
541  instrument_option |= INSTRUMENT_ROWS;
542 
543  if (es->buffers)
544  instrument_option |= INSTRUMENT_BUFFERS;
545  if (es->wal)
546  instrument_option |= INSTRUMENT_WAL;
547 
548  /*
549  * We always collect timing for the entire statement, even when node-level
550  * timing is off, so we don't look at es->timing here. (We could skip
551  * this if !es->summary, but it's hardly worth the complication.)
552  */
553  INSTR_TIME_SET_CURRENT(starttime);
554 
555  /*
556  * Use a snapshot with an updated command ID to ensure this query sees
557  * results of any previously executed queries.
558  */
561 
562  /*
563  * Normally we discard the query's output, but if explaining CREATE TABLE
564  * AS, we'd better use the appropriate tuple receiver.
565  */
566  if (into)
567  dest = CreateIntoRelDestReceiver(into);
568  else
569  dest = None_Receiver;
570 
571  /* Create a QueryDesc for the query */
572  queryDesc = CreateQueryDesc(plannedstmt, queryString,
574  dest, params, queryEnv, instrument_option);
575 
576  /* Select execution options */
577  if (es->analyze)
578  eflags = 0; /* default run-to-completion flags */
579  else
580  eflags = EXEC_FLAG_EXPLAIN_ONLY;
581  if (into)
582  eflags |= GetIntoRelEFlags(into);
583 
584  /* call ExecutorStart to prepare the plan for execution */
585  ExecutorStart(queryDesc, eflags);
586 
587  /* Execute the plan for statistics if asked for */
588  if (es->analyze)
589  {
590  ScanDirection dir;
591 
592  /* EXPLAIN ANALYZE CREATE TABLE AS WITH NO DATA is weird */
593  if (into && into->skipData)
595  else
596  dir = ForwardScanDirection;
597 
598  /* run the plan */
599  ExecutorRun(queryDesc, dir, 0L, true);
600 
601  /* run cleanup too */
602  ExecutorFinish(queryDesc);
603 
604  /* We can't run ExecutorEnd 'till we're done printing the stats... */
605  totaltime += elapsed_time(&starttime);
606  }
607 
608  ExplainOpenGroup("Query", NULL, true, es);
609 
610  /* Create textual dump of plan tree */
611  ExplainPrintPlan(es, queryDesc);
612 
613  if (es->verbose && plannedstmt->queryId != UINT64CONST(0))
614  {
615  char buf[MAXINT8LEN+1];
616 
617  pg_lltoa(plannedstmt->queryId, buf);
618  ExplainPropertyText("Query Identifier", buf, es);
619  }
620 
621  /* Show buffer usage in planning */
622  if (bufusage)
623  {
624  ExplainOpenGroup("Planning", "Planning", true, es);
625  show_buffer_usage(es, bufusage, true);
626  ExplainCloseGroup("Planning", "Planning", true, es);
627  }
628 
629  if (es->summary && planduration)
630  {
631  double plantime = INSTR_TIME_GET_DOUBLE(*planduration);
632 
633  ExplainPropertyFloat("Planning Time", "ms", 1000.0 * plantime, 3, es);
634  }
635 
636  /* Print info about runtime of triggers */
637  if (es->analyze)
638  ExplainPrintTriggers(es, queryDesc);
639 
640  /*
641  * Print info about JITing. Tied to es->costs because we don't want to
642  * display this in regression tests, as it'd cause output differences
643  * depending on build options. Might want to separate that out from COSTS
644  * at a later stage.
645  */
646  if (es->costs)
647  ExplainPrintJITSummary(es, queryDesc);
648 
649  /*
650  * Close down the query and free resources. Include time for this in the
651  * total execution time (although it should be pretty minimal).
652  */
653  INSTR_TIME_SET_CURRENT(starttime);
654 
655  ExecutorEnd(queryDesc);
656 
657  FreeQueryDesc(queryDesc);
658 
660 
661  /* We need a CCI just in case query expanded to multiple plans */
662  if (es->analyze)
664 
665  totaltime += elapsed_time(&starttime);
666 
667  /*
668  * We only report execution time if we actually ran the query (that is,
669  * the user specified ANALYZE), and if summary reporting is enabled (the
670  * user can set SUMMARY OFF to not have the timing information included in
671  * the output). By default, ANALYZE sets SUMMARY to true.
672  */
673  if (es->summary && es->analyze)
674  ExplainPropertyFloat("Execution Time", "ms", 1000.0 * totaltime, 3,
675  es);
676 
677  ExplainCloseGroup("Query", NULL, true, es);
678 }
bool summary
Definition: explain.h:47
void UpdateActiveSnapshotCommandId(void)
Definition: snapmgr.c:728
#define MAXINT8LEN
Definition: builtins.h:22
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:4459
struct timeval instr_time
Definition: instr_time.h:150
void ExecutorStart(QueryDesc *queryDesc, int eflags)
Definition: execMain.c:130
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:459
void ExplainPropertyText(const char *qlabel, const char *value, ExplainState *es)
Definition: explain.c:4423
int GetIntoRelEFlags(IntoClause *intoClause)
Definition: createas.c:381
void ExplainPrintTriggers(ExplainState *es, QueryDesc *queryDesc)
Definition: explain.c:804
void ExplainPrintPlan(ExplainState *es, QueryDesc *queryDesc)
Definition: explain.c:755
void ExecutorRun(QueryDesc *queryDesc, ScanDirection direction, uint64 count, bool execute_once)
Definition: execMain.c:298
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
static char * buf
Definition: pg_test_fsync.c:68
ScanDirection
Definition: sdir.h:22
DestReceiver * CreateIntoRelDestReceiver(IntoClause *intoClause)
Definition: createas.c:434
void ExecutorFinish(QueryDesc *queryDesc)
Definition: execMain.c:399
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
bool verbose
Definition: explain.h:41
#define Assert(condition)
Definition: c.h:804
bool wal
Definition: explain.h:45
static double elapsed_time(instr_time *starttime)
Definition: explain.c:1049
#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:3486
uint64 queryId
Definition: plannodes.h:48
int pg_lltoa(int64 value, char *a)
Definition: numutils.c:523
bool buffers
Definition: explain.h:44
void ExplainCloseGroup(const char *objtype, const char *labelname, bool labeled, ExplainState *es)
Definition: explain.c:4551
void ExplainPrintJITSummary(ExplainState *es, QueryDesc *queryDesc)
Definition: explain.c:847
void ExplainOpenGroup(const char *objtype, const char *labelname, bool labeled, ExplainState *es)
Definition: explain.c:4488
#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 433 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().

436 {
437  if (utilityStmt == NULL)
438  return;
439 
440  if (IsA(utilityStmt, CreateTableAsStmt))
441  {
442  /*
443  * We have to rewrite the contained SELECT and then pass it back to
444  * ExplainOneQuery. It's probably not really necessary to copy the
445  * contained parsetree another time, but let's be safe.
446  */
447  CreateTableAsStmt *ctas = (CreateTableAsStmt *) utilityStmt;
448  List *rewritten;
449 
450  /*
451  * Check if the relation exists or not. This is done at this stage to
452  * avoid query planning or execution.
453  */
454  if (CreateTableAsRelExists(ctas))
455  {
456  if (ctas->objtype == OBJECT_TABLE)
457  ExplainDummyGroup("CREATE TABLE AS", NULL, es);
458  else if (ctas->objtype == OBJECT_MATVIEW)
459  ExplainDummyGroup("CREATE MATERIALIZED VIEW", NULL, es);
460  else
461  elog(ERROR, "unexpected object type: %d",
462  (int) ctas->objtype);
463  return;
464  }
465 
466  rewritten = QueryRewrite(castNode(Query, copyObject(ctas->query)));
467  Assert(list_length(rewritten) == 1);
468  ExplainOneQuery(linitial_node(Query, rewritten),
469  CURSOR_OPT_PARALLEL_OK, ctas->into, es,
470  queryString, params, queryEnv);
471  }
472  else if (IsA(utilityStmt, DeclareCursorStmt))
473  {
474  /*
475  * Likewise for DECLARE CURSOR.
476  *
477  * Notice that if you say EXPLAIN ANALYZE DECLARE CURSOR then we'll
478  * actually run the query. This is different from pre-8.3 behavior
479  * but seems more useful than not running the query. No cursor will
480  * be created, however.
481  */
482  DeclareCursorStmt *dcs = (DeclareCursorStmt *) utilityStmt;
483  List *rewritten;
484 
485  rewritten = QueryRewrite(castNode(Query, copyObject(dcs->query)));
486  Assert(list_length(rewritten) == 1);
487  ExplainOneQuery(linitial_node(Query, rewritten),
488  dcs->options, NULL, es,
489  queryString, params, queryEnv);
490  }
491  else if (IsA(utilityStmt, ExecuteStmt))
492  ExplainExecuteQuery((ExecuteStmt *) utilityStmt, into, es,
493  queryString, params, queryEnv);
494  else if (IsA(utilityStmt, NotifyStmt))
495  {
496  if (es->format == EXPLAIN_FORMAT_TEXT)
497  appendStringInfoString(es->str, "NOTIFY\n");
498  else
499  ExplainDummyGroup("Notify", NULL, es);
500  }
501  else
502  {
503  if (es->format == EXPLAIN_FORMAT_TEXT)
505  "Utility statements have no plan structure\n");
506  else
507  ExplainDummyGroup("Utility Statement", NULL, es);
508  }
509 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:590
List * QueryRewrite(Query *parsetree)
#define castNode(_type_, nodeptr)
Definition: nodes.h:608
#define linitial_node(type, l)
Definition: pg_list.h:177
ObjectType objtype
Definition: parsenodes.h:3369
static void ExplainDummyGroup(const char *objtype, const char *labelname, ExplainState *es)
Definition: explain.c:4698
#define ERROR
Definition: elog.h:46
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:373
IntoClause * into
Definition: parsenodes.h:3368
#define Assert(condition)
Definition: c.h:804
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:2795
#define elog(elevel,...)
Definition: elog.h:232
#define copyObject(obj)
Definition: nodes.h:655
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 4488 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().

4490 {
4491  switch (es->format)
4492  {
4493  case EXPLAIN_FORMAT_TEXT:
4494  /* nothing to do */
4495  break;
4496 
4497  case EXPLAIN_FORMAT_XML:
4498  ExplainXMLTag(objtype, X_OPENING, es);
4499  es->indent++;
4500  break;
4501 
4502  case EXPLAIN_FORMAT_JSON:
4504  appendStringInfoSpaces(es->str, 2 * es->indent);
4505  if (labelname)
4506  {
4507  escape_json(es->str, labelname);
4508  appendStringInfoString(es->str, ": ");
4509  }
4510  appendStringInfoChar(es->str, labeled ? '{' : '[');
4511 
4512  /*
4513  * In JSON format, the grouping_stack is an integer list. 0 means
4514  * we've emitted nothing at this grouping level, 1 means we've
4515  * emitted something (and so the next item needs a comma). See
4516  * ExplainJSONLineEnding().
4517  */
4518  es->grouping_stack = lcons_int(0, es->grouping_stack);
4519  es->indent++;
4520  break;
4521 
4522  case EXPLAIN_FORMAT_YAML:
4523 
4524  /*
4525  * In YAML format, the grouping stack is an integer list. 0 means
4526  * we've emitted nothing at this grouping level AND this grouping
4527  * level is unlabeled and must be marked with "- ". See
4528  * ExplainYAMLLineStarting().
4529  */
4531  if (labelname)
4532  {
4533  appendStringInfo(es->str, "%s: ", labelname);
4534  es->grouping_stack = lcons_int(1, es->grouping_stack);
4535  }
4536  else
4537  {
4538  appendStringInfoString(es->str, "- ");
4539  es->grouping_stack = lcons_int(0, es->grouping_stack);
4540  }
4541  es->indent++;
4542  break;
4543  }
4544 }
void escape_json(StringInfo buf, const char *str)
Definition: json.c:1279
List * lcons_int(int datum, List *list)
Definition: list.c:486
static void ExplainXMLTag(const char *tagname, int flags, ExplainState *es)
Definition: explain.c:4833
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:4895
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:4875
#define X_OPENING
Definition: explain.c:52
ExplainFormat format
Definition: explain.h:49
StringInfo str
Definition: explain.h:39

◆ ExplainPrintJITSummary()

void ExplainPrintJITSummary ( ExplainState es,
QueryDesc queryDesc 
)

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

848 {
849  JitInstrumentation ji = {0};
850 
851  if (!(queryDesc->estate->es_jit_flags & PGJIT_PERFORM))
852  return;
853 
854  /*
855  * Work with a copy instead of modifying the leader state, since this
856  * function may be called twice
857  */
858  if (queryDesc->estate->es_jit)
859  InstrJitAgg(&ji, &queryDesc->estate->es_jit->instr);
860 
861  /* If this process has done JIT in parallel workers, merge stats */
862  if (queryDesc->estate->es_jit_worker_instr)
863  InstrJitAgg(&ji, queryDesc->estate->es_jit_worker_instr);
864 
865  ExplainPrintJIT(es, queryDesc->estate->es_jit_flags, &ji);
866 }
EState * estate
Definition: execdesc.h:48
struct JitContext * es_jit
Definition: execnodes.h:644
struct JitInstrumentation * es_jit_worker_instr
Definition: execnodes.h:645
static void ExplainPrintJIT(ExplainState *es, int jit_flags, JitInstrumentation *ji)
Definition: explain.c:873
JitInstrumentation instr
Definition: jit.h:61
int es_jit_flags
Definition: execnodes.h:643
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 755 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().

756 {
757  Bitmapset *rels_used = NULL;
758  PlanState *ps;
759 
760  /* Set up ExplainState fields associated with this plan tree */
761  Assert(queryDesc->plannedstmt != NULL);
762  es->pstmt = queryDesc->plannedstmt;
763  es->rtable = queryDesc->plannedstmt->rtable;
764  ExplainPreScanNode(queryDesc->planstate, &rels_used);
767  es->rtable_names);
768  es->printed_subplans = NULL;
769 
770  /*
771  * Sometimes we mark a Gather node as "invisible", which means that it's
772  * not to be displayed in EXPLAIN output. The purpose of this is to allow
773  * running regression tests with force_parallel_mode=regress to get the
774  * same results as running the same tests with force_parallel_mode=off.
775  * Such marking is currently only supported on a Gather at the top of the
776  * plan. We skip that node, and we must also hide per-worker detail data
777  * further down in the plan tree.
778  */
779  ps = queryDesc->planstate;
780  if (IsA(ps, GatherState) && ((Gather *) ps->plan)->invisible)
781  {
782  ps = outerPlanState(ps);
783  es->hide_workers = true;
784  }
785  ExplainNode(ps, NIL, NULL, NULL, es);
786 
787  /*
788  * If requested, include information about GUC parameters with values that
789  * don't match the built-in defaults.
790  */
792 }
#define NIL
Definition: pg_list.h:65
static void ExplainPrintSettings(ExplainState *es)
Definition: explain.c:685
#define IsA(nodeptr, _type_)
Definition: nodes.h:590
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:3679
static void ExplainNode(PlanState *planstate, List *ancestors, const char *relationship, const char *plan_name, ExplainState *es)
Definition: explain.c:1143
PlanState * planstate
Definition: execdesc.h:49
#define outerPlanState(node)
Definition: execnodes.h:1058
List * deparse_context_for_plan_tree(PlannedStmt *pstmt, List *rtable_names)
Definition: ruleutils.c:3587
Plan * plan
Definition: execnodes.h:964
#define Assert(condition)
Definition: c.h:804
List * rtable
Definition: plannodes.h:66
static bool ExplainPreScanNode(PlanState *planstate, Bitmapset **rels_used)
Definition: explain.c:1068
PlannedStmt * plannedstmt
Definition: execdesc.h:37
List * rtable
Definition: explain.h:55

◆ ExplainPrintTriggers()

void ExplainPrintTriggers ( ExplainState es,
QueryDesc queryDesc 
)

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

805 {
806  ResultRelInfo *rInfo;
807  bool show_relname;
808  List *resultrels;
809  List *routerels;
810  List *targrels;
811  ListCell *l;
812 
813  resultrels = queryDesc->estate->es_opened_result_relations;
814  routerels = queryDesc->estate->es_tuple_routing_result_relations;
815  targrels = queryDesc->estate->es_trig_target_relations;
816 
817  ExplainOpenGroup("Triggers", "Triggers", false, es);
818 
819  show_relname = (list_length(resultrels) > 1 ||
820  routerels != NIL || targrels != NIL);
821  foreach(l, resultrels)
822  {
823  rInfo = (ResultRelInfo *) lfirst(l);
824  report_triggers(rInfo, show_relname, es);
825  }
826 
827  foreach(l, routerels)
828  {
829  rInfo = (ResultRelInfo *) lfirst(l);
830  report_triggers(rInfo, show_relname, es);
831  }
832 
833  foreach(l, targrels)
834  {
835  rInfo = (ResultRelInfo *) lfirst(l);
836  report_triggers(rInfo, show_relname, es);
837  }
838 
839  ExplainCloseGroup("Triggers", "Triggers", false, es);
840 }
#define NIL
Definition: pg_list.h:65
EState * estate
Definition: execdesc.h:48
List * es_opened_result_relations
Definition: execnodes.h:576
static void report_triggers(ResultRelInfo *rInfo, bool show_relname, ExplainState *es)
Definition: explain.c:978
List * es_trig_target_relations
Definition: execnodes.h:589
List * es_tuple_routing_result_relations
Definition: execnodes.h:586
#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:4551
void ExplainOpenGroup(const char *objtype, const char *labelname, bool labeled, ExplainState *es)
Definition: explain.c:4488
Definition: pg_list.h:50

◆ ExplainPropertyBool()

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

Definition at line 4473 of file explain.c.

References ExplainProperty().

Referenced by ExplainNode(), and ExplainPrintJIT().

4474 {
4475  ExplainProperty(qlabel, NULL, value ? "true" : "false", true, es);
4476 }
static struct @142 value
static void ExplainProperty(const char *qlabel, const char *unit, const char *value, bool numeric, ExplainState *es)
Definition: explain.c:4370

◆ ExplainPropertyFloat()

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

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

4461 {
4462  char *buf;
4463 
4464  buf = psprintf("%.*f", ndigits, value);
4465  ExplainProperty(qlabel, unit, buf, true, es);
4466  pfree(buf);
4467 }
static struct @142 value
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
void pfree(void *pointer)
Definition: mcxt.c:1169
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:4370

◆ ExplainPropertyInteger()

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

Definition at line 4432 of file explain.c.

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

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

4434 {
4435  char buf[32];
4436 
4437  snprintf(buf, sizeof(buf), INT64_FORMAT, value);
4438  ExplainProperty(qlabel, unit, buf, true, es);
4439 }
static struct @142 value
static char * buf
Definition: pg_test_fsync.c:68
#define INT64_FORMAT
Definition: c.h:483
#define snprintf
Definition: port.h:216
static void ExplainProperty(const char *qlabel, const char *unit, const char *value, bool numeric, ExplainState *es)
Definition: explain.c:4370

◆ ExplainPropertyList()

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

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

4248 {
4249  ListCell *lc;
4250  bool first = true;
4251 
4252  switch (es->format)
4253  {
4254  case EXPLAIN_FORMAT_TEXT:
4255  ExplainIndentText(es);
4256  appendStringInfo(es->str, "%s: ", qlabel);
4257  foreach(lc, data)
4258  {
4259  if (!first)
4260  appendStringInfoString(es->str, ", ");
4261  appendStringInfoString(es->str, (const char *) lfirst(lc));
4262  first = false;
4263  }
4264  appendStringInfoChar(es->str, '\n');
4265  break;
4266 
4267  case EXPLAIN_FORMAT_XML:
4268  ExplainXMLTag(qlabel, X_OPENING, es);
4269  foreach(lc, data)
4270  {
4271  char *str;
4272 
4273  appendStringInfoSpaces(es->str, es->indent * 2 + 2);
4274  appendStringInfoString(es->str, "<Item>");
4275  str = escape_xml((const char *) lfirst(lc));
4276  appendStringInfoString(es->str, str);
4277  pfree(str);
4278  appendStringInfoString(es->str, "</Item>\n");
4279  }
4280  ExplainXMLTag(qlabel, X_CLOSING, es);
4281  break;
4282 
4283  case EXPLAIN_FORMAT_JSON:
4285  appendStringInfoSpaces(es->str, es->indent * 2);
4286  escape_json(es->str, qlabel);
4287  appendStringInfoString(es->str, ": [");
4288  foreach(lc, data)
4289  {
4290  if (!first)
4291  appendStringInfoString(es->str, ", ");
4292  escape_json(es->str, (const char *) lfirst(lc));
4293  first = false;
4294  }
4295  appendStringInfoChar(es->str, ']');
4296  break;
4297 
4298  case EXPLAIN_FORMAT_YAML:
4300  appendStringInfo(es->str, "%s: ", qlabel);
4301  foreach(lc, data)
4302  {
4303  appendStringInfoChar(es->str, '\n');
4304  appendStringInfoSpaces(es->str, es->indent * 2 + 2);
4305  appendStringInfoString(es->str, "- ");
4306  escape_yaml(es->str, (const char *) lfirst(lc));
4307  }
4308  break;
4309  }
4310 }
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:4833
#define X_CLOSING
Definition: explain.c:53
void pfree(void *pointer)
Definition: mcxt.c:1169
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:4895
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:4920
void appendStringInfoSpaces(StringInfo str, int count)
Definition: stringinfo.c:206
static void ExplainJSONLineEnding(ExplainState *es)
Definition: explain.c:4875
#define X_OPENING
Definition: explain.c:52
static void ExplainIndentText(ExplainState *es)
Definition: explain.c:4860
#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 4317 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().

4318 {
4319  ListCell *lc;
4320  bool first = true;
4321 
4322  switch (es->format)
4323  {
4324  case EXPLAIN_FORMAT_TEXT:
4325  case EXPLAIN_FORMAT_XML:
4326  ExplainPropertyList(qlabel, data, es);
4327  return;
4328 
4329  case EXPLAIN_FORMAT_JSON:
4331  appendStringInfoSpaces(es->str, es->indent * 2);
4332  appendStringInfoChar(es->str, '[');
4333  foreach(lc, data)
4334  {
4335  if (!first)
4336  appendStringInfoString(es->str, ", ");
4337  escape_json(es->str, (const char *) lfirst(lc));
4338  first = false;
4339  }
4340  appendStringInfoChar(es->str, ']');
4341  break;
4342 
4343  case EXPLAIN_FORMAT_YAML:
4345  appendStringInfoString(es->str, "- [");
4346  foreach(lc, data)
4347  {
4348  if (!first)
4349  appendStringInfoString(es->str, ", ");
4350  escape_yaml(es->str, (const char *) lfirst(lc));
4351  first = false;
4352  }
4353  appendStringInfoChar(es->str, ']');
4354  break;
4355  }
4356 }
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:4895
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:4920
void appendStringInfoSpaces(StringInfo str, int count)
Definition: stringinfo.c:206
static void ExplainJSONLineEnding(ExplainState *es)
Definition: explain.c:4875
void ExplainPropertyList(const char *qlabel, List *data, ExplainState *es)
Definition: explain.c:4247
#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 4423 of file explain.c.

References ExplainProperty().

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

4424 {
4425  ExplainProperty(qlabel, NULL, value, false, es);
4426 }
static struct @142 value
static void ExplainProperty(const char *qlabel, const char *unit, const char *value, bool numeric, ExplainState *es)
Definition: explain.c:4370

◆ ExplainPropertyUInteger()

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

Definition at line 4445 of file explain.c.

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

Referenced by show_wal_usage().

4447 {
4448  char buf[32];
4449 
4450  snprintf(buf, sizeof(buf), UINT64_FORMAT, value);
4451  ExplainProperty(qlabel, unit, buf, true, es);
4452 }
static struct @142 value
static char * buf
Definition: pg_test_fsync.c:68
#define snprintf
Definition: port.h:216
static void ExplainProperty(const char *qlabel, const char *unit, const char *value, bool numeric, ExplainState *es)
Definition: explain.c:4370
#define UINT64_FORMAT
Definition: c.h:484

◆ ExplainQuery()

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

Definition at line 164 of file explain.c.

References ExplainState::analyze, appendStringInfoString(), Assert, begin_tup_output_tupdesc(), ExplainState::buffers, castNode, compute_query_id, 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, JumbleQuery(), lfirst, lfirst_node, lnext(), DefElem::location, NewExplainState(), NIL, ExplainStmt::options, ParseState::p_sourcetext, parser_errposition(), pfree(), post_parse_analyze_hook, ExplainStmt::query, QueryRewrite(), ExplainState::settings, ExplainState::str, ExplainState::summary, ExplainState::timing, TTSOpsVirtual, ExplainState::verbose, and ExplainState::wal.

Referenced by standard_ProcessUtility().

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

◆ ExplainQueryText()

void ExplainQueryText ( ExplainState es,
QueryDesc queryDesc 
)

Definition at line 967 of file explain.c.

References ExplainPropertyText(), and QueryDesc::sourceText.

Referenced by explain_ExecutorEnd().

968 {
969  if (queryDesc->sourceText)
970  ExplainPropertyText("Query Text", queryDesc->sourceText, es);
971 }
void ExplainPropertyText(const char *qlabel, const char *value, ExplainState *es)
Definition: explain.c:4423
const char * sourceText
Definition: execdesc.h:38

◆ ExplainResultDesc()

TupleDesc ExplainResultDesc ( ExplainStmt stmt)

Definition at line 334 of file explain.c.

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

Referenced by ExplainQuery(), and UtilityTupleDescriptor().

335 {
336  TupleDesc tupdesc;
337  ListCell *lc;
338  Oid result_type = TEXTOID;
339 
340  /* Check for XML format option */
341  foreach(lc, stmt->options)
342  {
343  DefElem *opt = (DefElem *) lfirst(lc);
344 
345  if (strcmp(opt->defname, "format") == 0)
346  {
347  char *p = defGetString(opt);
348 
349  if (strcmp(p, "xml") == 0)
350  result_type = XMLOID;
351  else if (strcmp(p, "json") == 0)
352  result_type = JSONOID;
353  else
354  result_type = TEXTOID;
355  /* don't "break", as ExplainQuery will use the last value */
356  }
357  }
358 
359  /* Need a tuple descriptor representing a single TEXT or XML column */
360  tupdesc = CreateTemplateTupleDesc(1);
361  TupleDescInitEntry(tupdesc, (AttrNumber) 1, "QUERY PLAN",
362  result_type, -1, 0);
363  return tupdesc;
364 }
TupleDesc CreateTemplateTupleDesc(int natts)
Definition: tupdesc.c:45
List * options
Definition: parsenodes.h:3348
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:580
#define lfirst(lc)
Definition: pg_list.h:169
char * defname
Definition: parsenodes.h:746
int16 AttrNumber
Definition: attnum.h:21

◆ ExplainSeparatePlans()

void ExplainSeparatePlans ( ExplainState es)

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

4805 {
4806  switch (es->format)
4807  {
4808  case EXPLAIN_FORMAT_TEXT:
4809  /* add a blank line */
4810  appendStringInfoChar(es->str, '\n');
4811  break;
4812 
4813  case EXPLAIN_FORMAT_XML:
4814  case EXPLAIN_FORMAT_JSON:
4815  case EXPLAIN_FORMAT_YAML:
4816  /* nothing to do */
4817  break;
4818  }
4819 }
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 317 of file explain.c.

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

Referenced by explain_ExecutorEnd(), and ExplainQuery().

318 {
319  ExplainState *es = (ExplainState *) palloc0(sizeof(ExplainState));
320 
321  /* Set default options (most fields can be left as zeroes). */
322  es->costs = true;
323  /* Prepare output buffer. */
324  es->str = makeStringInfo();
325 
326  return es;
327 }
StringInfo makeStringInfo(void)
Definition: stringinfo.c:41
bool costs
Definition: explain.h:43
void * palloc0(Size size)
Definition: mcxt.c:1093
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 48 of file explain.c.

Referenced by explain_get_index_name().

◆ ExplainOneQuery_hook

PGDLLIMPORT ExplainOneQuery_hook_type ExplainOneQuery_hook

Definition at line 45 of file explain.c.

Referenced by ExplainOneQuery().