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 ExplainQueryParameters (ExplainState *es, ParamListInfo params, int maxlen)
 
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 76 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 66 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.

21 {
ExplainFormat
Definition: explain.h:21
@ EXPLAIN_FORMAT_XML
Definition: explain.h:23
@ EXPLAIN_FORMAT_YAML
Definition: explain.h:25
@ EXPLAIN_FORMAT_TEXT
Definition: explain.h:22
@ EXPLAIN_FORMAT_JSON
Definition: explain.h:24

Function Documentation

◆ ExplainBeginOutput()

void ExplainBeginOutput ( ExplainState es)

Definition at line 4883 of file explain.c.

4884 {
4885  switch (es->format)
4886  {
4887  case EXPLAIN_FORMAT_TEXT:
4888  /* nothing to do */
4889  break;
4890 
4891  case EXPLAIN_FORMAT_XML:
4893  "<explain xmlns=\"http://www.postgresql.org/2009/explain\">\n");
4894  es->indent++;
4895  break;
4896 
4897  case EXPLAIN_FORMAT_JSON:
4898  /* top-level structure is an array of plans */
4899  appendStringInfoChar(es->str, '[');
4900  es->grouping_stack = lcons_int(0, es->grouping_stack);
4901  es->indent++;
4902  break;
4903 
4904  case EXPLAIN_FORMAT_YAML:
4905  es->grouping_stack = lcons_int(0, es->grouping_stack);
4906  break;
4907  }
4908 }
List * lcons_int(int datum, List *list)
Definition: list.c:512
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:188
List * grouping_stack
Definition: explain.h:53
StringInfo str
Definition: explain.h:39
ExplainFormat format
Definition: explain.h:50
int indent
Definition: explain.h:52

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

◆ ExplainCloseGroup()

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

Definition at line 4690 of file explain.c.

4692 {
4693  switch (es->format)
4694  {
4695  case EXPLAIN_FORMAT_TEXT:
4696  /* nothing to do */
4697  break;
4698 
4699  case EXPLAIN_FORMAT_XML:
4700  es->indent--;
4701  ExplainXMLTag(objtype, X_CLOSING, es);
4702  break;
4703 
4704  case EXPLAIN_FORMAT_JSON:
4705  es->indent--;
4706  appendStringInfoChar(es->str, '\n');
4707  appendStringInfoSpaces(es->str, 2 * es->indent);
4708  appendStringInfoChar(es->str, labeled ? '}' : ']');
4710  break;
4711 
4712  case EXPLAIN_FORMAT_YAML:
4713  es->indent--;
4715  break;
4716  }
4717 }
static void ExplainXMLTag(const char *tagname, int flags, ExplainState *es)
Definition: explain.c:4972
#define X_CLOSING
Definition: explain.c:53
List * list_delete_first(List *list)
Definition: list.c:942
void appendStringInfoSpaces(StringInfo str, int count)
Definition: stringinfo.c:206

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

◆ ExplainEndOutput()

void ExplainEndOutput ( ExplainState es)

Definition at line 4914 of file explain.c.

4915 {
4916  switch (es->format)
4917  {
4918  case EXPLAIN_FORMAT_TEXT:
4919  /* nothing to do */
4920  break;
4921 
4922  case EXPLAIN_FORMAT_XML:
4923  es->indent--;
4924  appendStringInfoString(es->str, "</explain>");
4925  break;
4926 
4927  case EXPLAIN_FORMAT_JSON:
4928  es->indent--;
4929  appendStringInfoString(es->str, "\n]");
4931  break;
4932 
4933  case EXPLAIN_FORMAT_YAML:
4935  break;
4936  }
4937 }

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

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

531 {
533  QueryDesc *queryDesc;
534  instr_time starttime;
535  double totaltime = 0;
536  int eflags;
537  int instrument_option = 0;
538 
539  Assert(plannedstmt->commandType != CMD_UTILITY);
540 
541  if (es->analyze && es->timing)
542  instrument_option |= INSTRUMENT_TIMER;
543  else if (es->analyze)
544  instrument_option |= INSTRUMENT_ROWS;
545 
546  if (es->buffers)
547  instrument_option |= INSTRUMENT_BUFFERS;
548  if (es->wal)
549  instrument_option |= INSTRUMENT_WAL;
550 
551  /*
552  * We always collect timing for the entire statement, even when node-level
553  * timing is off, so we don't look at es->timing here. (We could skip
554  * this if !es->summary, but it's hardly worth the complication.)
555  */
556  INSTR_TIME_SET_CURRENT(starttime);
557 
558  /*
559  * Use a snapshot with an updated command ID to ensure this query sees
560  * results of any previously executed queries.
561  */
564 
565  /*
566  * Normally we discard the query's output, but if explaining CREATE TABLE
567  * AS, we'd better use the appropriate tuple receiver.
568  */
569  if (into)
571  else
573 
574  /* Create a QueryDesc for the query */
575  queryDesc = CreateQueryDesc(plannedstmt, queryString,
577  dest, params, queryEnv, instrument_option);
578 
579  /* Select execution options */
580  if (es->analyze)
581  eflags = 0; /* default run-to-completion flags */
582  else
583  eflags = EXEC_FLAG_EXPLAIN_ONLY;
584  if (es->generic)
585  eflags |= EXEC_FLAG_EXPLAIN_GENERIC;
586  if (into)
587  eflags |= GetIntoRelEFlags(into);
588 
589  /* call ExecutorStart to prepare the plan for execution */
590  ExecutorStart(queryDesc, eflags);
591 
592  /* Execute the plan for statistics if asked for */
593  if (es->analyze)
594  {
595  ScanDirection dir;
596 
597  /* EXPLAIN ANALYZE CREATE TABLE AS WITH NO DATA is weird */
598  if (into && into->skipData)
600  else
601  dir = ForwardScanDirection;
602 
603  /* run the plan */
604  ExecutorRun(queryDesc, dir, 0, true);
605 
606  /* run cleanup too */
607  ExecutorFinish(queryDesc);
608 
609  /* We can't run ExecutorEnd 'till we're done printing the stats... */
610  totaltime += elapsed_time(&starttime);
611  }
612 
613  ExplainOpenGroup("Query", NULL, true, es);
614 
615  /* Create textual dump of plan tree */
616  ExplainPrintPlan(es, queryDesc);
617 
618  /* Show buffer usage in planning */
619  if (bufusage)
620  {
621  ExplainOpenGroup("Planning", "Planning", true, es);
622  show_buffer_usage(es, bufusage, true);
623  ExplainCloseGroup("Planning", "Planning", true, es);
624  }
625 
626  if (es->summary && planduration)
627  {
628  double plantime = INSTR_TIME_GET_DOUBLE(*planduration);
629 
630  ExplainPropertyFloat("Planning Time", "ms", 1000.0 * plantime, 3, es);
631  }
632 
633  /* Print info about runtime of triggers */
634  if (es->analyze)
635  ExplainPrintTriggers(es, queryDesc);
636 
637  /*
638  * Print info about JITing. Tied to es->costs because we don't want to
639  * display this in regression tests, as it'd cause output differences
640  * depending on build options. Might want to separate that out from COSTS
641  * at a later stage.
642  */
643  if (es->costs)
644  ExplainPrintJITSummary(es, queryDesc);
645 
646  /*
647  * Close down the query and free resources. Include time for this in the
648  * total execution time (although it should be pretty minimal).
649  */
650  INSTR_TIME_SET_CURRENT(starttime);
651 
652  ExecutorEnd(queryDesc);
653 
654  FreeQueryDesc(queryDesc);
655 
657 
658  /* We need a CCI just in case query expanded to multiple plans */
659  if (es->analyze)
661 
662  totaltime += elapsed_time(&starttime);
663 
664  /*
665  * We only report execution time if we actually ran the query (that is,
666  * the user specified ANALYZE), and if summary reporting is enabled (the
667  * user can set SUMMARY OFF to not have the timing information included in
668  * the output). By default, ANALYZE sets SUMMARY to true.
669  */
670  if (es->summary && es->analyze)
671  ExplainPropertyFloat("Execution Time", "ms", 1000.0 * totaltime, 3,
672  es);
673 
674  ExplainCloseGroup("Query", NULL, true, es);
675 }
DestReceiver * CreateIntoRelDestReceiver(IntoClause *intoClause)
Definition: createas.c:440
int GetIntoRelEFlags(IntoClause *intoClause)
Definition: createas.c:375
DestReceiver * None_Receiver
Definition: dest.c:96
void ExecutorEnd(QueryDesc *queryDesc)
Definition: execMain.c:469
void ExecutorFinish(QueryDesc *queryDesc)
Definition: execMain.c:409
void ExecutorStart(QueryDesc *queryDesc, int eflags)
Definition: execMain.c:132
void ExecutorRun(QueryDesc *queryDesc, ScanDirection direction, uint64 count, bool execute_once)
Definition: execMain.c:302
#define EXEC_FLAG_EXPLAIN_GENERIC
Definition: executor.h:66
#define EXEC_FLAG_EXPLAIN_ONLY
Definition: executor.h:65
void ExplainOpenGroup(const char *objtype, const char *labelname, bool labeled, ExplainState *es)
Definition: explain.c:4627
void ExplainPrintJITSummary(ExplainState *es, QueryDesc *queryDesc)
Definition: explain.c:860
void ExplainPropertyFloat(const char *qlabel, const char *unit, double value, int ndigits, ExplainState *es)
Definition: explain.c:4598
void ExplainCloseGroup(const char *objtype, const char *labelname, bool labeled, ExplainState *es)
Definition: explain.c:4690
static void show_buffer_usage(ExplainState *es, const BufferUsage *usage, bool planning)
Definition: explain.c:3551
static double elapsed_time(instr_time *starttime)
Definition: explain.c:1092
void ExplainPrintPlan(ExplainState *es, QueryDesc *queryDesc)
Definition: explain.c:752
void ExplainPrintTriggers(ExplainState *es, QueryDesc *queryDesc)
Definition: explain.c:817
#define INSTR_TIME_SET_CURRENT(t)
Definition: instr_time.h:122
#define INSTR_TIME_GET_DOUBLE(t)
Definition: instr_time.h:188
@ INSTRUMENT_TIMER
Definition: instrument.h:59
@ INSTRUMENT_BUFFERS
Definition: instrument.h:60
@ INSTRUMENT_WAL
Definition: instrument.h:62
@ INSTRUMENT_ROWS
Definition: instrument.h:61
Assert(fmt[strlen(fmt) - 1] !='\n')
@ CMD_UTILITY
Definition: nodes.h:281
void FreeQueryDesc(QueryDesc *qdesc)
Definition: pquery.c:105
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:25
@ NoMovementScanDirection
Definition: sdir.h:27
@ ForwardScanDirection
Definition: sdir.h:28
void UpdateActiveSnapshotCommandId(void)
Definition: snapmgr.c:693
void PopActiveSnapshot(void)
Definition: snapmgr.c:724
void PushCopiedSnapshot(Snapshot snapshot)
Definition: snapmgr.c:681
Snapshot GetActiveSnapshot(void)
Definition: snapmgr.c:751
#define InvalidSnapshot
Definition: snapshot.h:123
bool timing
Definition: explain.h:46
bool analyze
Definition: explain.h:42
bool wal
Definition: explain.h:45
bool generic
Definition: explain.h:49
bool summary
Definition: explain.h:47
bool costs
Definition: explain.h:43
bool buffers
Definition: explain.h:44
bool skipData
Definition: primnodes.h:148
CmdType commandType
Definition: plannodes.h:53
void CommandCounterIncrement(void)
Definition: xact.c:1078

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

Referenced by ExplainExecuteQuery(), and ExplainOneQuery().

◆ ExplainOneUtility()

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

Definition at line 437 of file explain.c.

440 {
441  if (utilityStmt == NULL)
442  return;
443 
444  if (IsA(utilityStmt, CreateTableAsStmt))
445  {
446  /*
447  * We have to rewrite the contained SELECT and then pass it back to
448  * ExplainOneQuery. Copy to be safe in the EXPLAIN EXECUTE case.
449  */
450  CreateTableAsStmt *ctas = (CreateTableAsStmt *) utilityStmt;
451  List *rewritten;
452 
453  /*
454  * Check if the relation exists or not. This is done at this stage to
455  * avoid query planning or execution.
456  */
457  if (CreateTableAsRelExists(ctas))
458  {
459  if (ctas->objtype == OBJECT_TABLE)
460  ExplainDummyGroup("CREATE TABLE AS", NULL, es);
461  else if (ctas->objtype == OBJECT_MATVIEW)
462  ExplainDummyGroup("CREATE MATERIALIZED VIEW", NULL, es);
463  else
464  elog(ERROR, "unexpected object type: %d",
465  (int) ctas->objtype);
466  return;
467  }
468 
469  rewritten = QueryRewrite(castNode(Query, copyObject(ctas->query)));
470  Assert(list_length(rewritten) == 1);
471  ExplainOneQuery(linitial_node(Query, rewritten),
472  CURSOR_OPT_PARALLEL_OK, ctas->into, es,
473  queryString, params, queryEnv);
474  }
475  else if (IsA(utilityStmt, DeclareCursorStmt))
476  {
477  /*
478  * Likewise for DECLARE CURSOR.
479  *
480  * Notice that if you say EXPLAIN ANALYZE DECLARE CURSOR then we'll
481  * actually run the query. This is different from pre-8.3 behavior
482  * but seems more useful than not running the query. No cursor will
483  * be created, however.
484  */
485  DeclareCursorStmt *dcs = (DeclareCursorStmt *) utilityStmt;
486  List *rewritten;
487 
488  rewritten = QueryRewrite(castNode(Query, copyObject(dcs->query)));
489  Assert(list_length(rewritten) == 1);
490  ExplainOneQuery(linitial_node(Query, rewritten),
491  dcs->options, NULL, es,
492  queryString, params, queryEnv);
493  }
494  else if (IsA(utilityStmt, ExecuteStmt))
495  ExplainExecuteQuery((ExecuteStmt *) utilityStmt, into, es,
496  queryString, params, queryEnv);
497  else if (IsA(utilityStmt, NotifyStmt))
498  {
499  if (es->format == EXPLAIN_FORMAT_TEXT)
500  appendStringInfoString(es->str, "NOTIFY\n");
501  else
502  ExplainDummyGroup("Notify", NULL, es);
503  }
504  else
505  {
506  if (es->format == EXPLAIN_FORMAT_TEXT)
508  "Utility statements have no plan structure\n");
509  else
510  ExplainDummyGroup("Utility Statement", NULL, es);
511  }
512 }
void ExplainExecuteQuery(ExecuteStmt *execstmt, IntoClause *into, ExplainState *es, const char *queryString, ParamListInfo params, QueryEnvironment *queryEnv)
Definition: prepare.c:571
bool CreateTableAsRelExists(CreateTableAsStmt *ctas)
Definition: createas.c:393
#define ERROR
Definition: elog.h:39
static void ExplainDummyGroup(const char *objtype, const char *labelname, ExplainState *es)
Definition: explain.c:4837
static void ExplainOneQuery(Query *query, int cursorOptions, IntoClause *into, ExplainState *es, const char *queryString, ParamListInfo params, QueryEnvironment *queryEnv)
Definition: explain.c:376
#define IsA(nodeptr, _type_)
Definition: nodes.h:179
#define copyObject(obj)
Definition: nodes.h:244
#define castNode(_type_, nodeptr)
Definition: nodes.h:197
@ OBJECT_MATVIEW
Definition: parsenodes.h:2143
@ OBJECT_TABLE
Definition: parsenodes.h:2161
#define CURSOR_OPT_PARALLEL_OK
Definition: parsenodes.h:3165
static int list_length(const List *l)
Definition: pg_list.h:152
#define linitial_node(type, l)
Definition: pg_list.h:181
List * QueryRewrite(Query *parsetree)
IntoClause * into
Definition: parsenodes.h:3766
ObjectType objtype
Definition: parsenodes.h:3767
Definition: pg_list.h:54

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

◆ ExplainOpenGroup()

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

Definition at line 4627 of file explain.c.

4629 {
4630  switch (es->format)
4631  {
4632  case EXPLAIN_FORMAT_TEXT:
4633  /* nothing to do */
4634  break;
4635 
4636  case EXPLAIN_FORMAT_XML:
4637  ExplainXMLTag(objtype, X_OPENING, es);
4638  es->indent++;
4639  break;
4640 
4641  case EXPLAIN_FORMAT_JSON:
4643  appendStringInfoSpaces(es->str, 2 * es->indent);
4644  if (labelname)
4645  {
4646  escape_json(es->str, labelname);
4647  appendStringInfoString(es->str, ": ");
4648  }
4649  appendStringInfoChar(es->str, labeled ? '{' : '[');
4650 
4651  /*
4652  * In JSON format, the grouping_stack is an integer list. 0 means
4653  * we've emitted nothing at this grouping level, 1 means we've
4654  * emitted something (and so the next item needs a comma). See
4655  * ExplainJSONLineEnding().
4656  */
4657  es->grouping_stack = lcons_int(0, es->grouping_stack);
4658  es->indent++;
4659  break;
4660 
4661  case EXPLAIN_FORMAT_YAML:
4662 
4663  /*
4664  * In YAML format, the grouping stack is an integer list. 0 means
4665  * we've emitted nothing at this grouping level AND this grouping
4666  * level is unlabeled and must be marked with "- ". See
4667  * ExplainYAMLLineStarting().
4668  */
4670  if (labelname)
4671  {
4672  appendStringInfo(es->str, "%s: ", labelname);
4673  es->grouping_stack = lcons_int(1, es->grouping_stack);
4674  }
4675  else
4676  {
4677  appendStringInfoString(es->str, "- ");
4678  es->grouping_stack = lcons_int(0, es->grouping_stack);
4679  }
4680  es->indent++;
4681  break;
4682  }
4683 }
#define X_OPENING
Definition: explain.c:52
static void ExplainYAMLLineStarting(ExplainState *es)
Definition: explain.c:5034
static void ExplainJSONLineEnding(ExplainState *es)
Definition: explain.c:5014
void escape_json(StringInfo buf, const char *str)
Definition: json.c:1525
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91

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

◆ ExplainPrintJITSummary()

void ExplainPrintJITSummary ( ExplainState es,
QueryDesc queryDesc 
)

Definition at line 860 of file explain.c.

861 {
862  JitInstrumentation ji = {0};
863 
864  if (!(queryDesc->estate->es_jit_flags & PGJIT_PERFORM))
865  return;
866 
867  /*
868  * Work with a copy instead of modifying the leader state, since this
869  * function may be called twice
870  */
871  if (queryDesc->estate->es_jit)
872  InstrJitAgg(&ji, &queryDesc->estate->es_jit->instr);
873 
874  /* If this process has done JIT in parallel workers, merge stats */
875  if (queryDesc->estate->es_jit_worker_instr)
876  InstrJitAgg(&ji, queryDesc->estate->es_jit_worker_instr);
877 
878  ExplainPrintJIT(es, queryDesc->estate->es_jit_flags, &ji);
879 }
static void ExplainPrintJIT(ExplainState *es, int jit_flags, JitInstrumentation *ji)
Definition: explain.c:886
void InstrJitAgg(JitInstrumentation *dst, JitInstrumentation *add)
Definition: jit.c:184
#define PGJIT_PERFORM
Definition: jit.h:20
struct JitContext * es_jit
Definition: execnodes.h:708
struct JitInstrumentation * es_jit_worker_instr
Definition: execnodes.h:709
int es_jit_flags
Definition: execnodes.h:707
JitInstrumentation instr
Definition: jit.h:64
EState * estate
Definition: execdesc.h:48

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

Referenced by explain_ExecutorEnd(), and ExplainOnePlan().

◆ ExplainPrintPlan()

void ExplainPrintPlan ( ExplainState es,
QueryDesc queryDesc 
)

Definition at line 752 of file explain.c.

753 {
754  Bitmapset *rels_used = NULL;
755  PlanState *ps;
756 
757  /* Set up ExplainState fields associated with this plan tree */
758  Assert(queryDesc->plannedstmt != NULL);
759  es->pstmt = queryDesc->plannedstmt;
760  es->rtable = queryDesc->plannedstmt->rtable;
761  ExplainPreScanNode(queryDesc->planstate, &rels_used);
764  es->rtable_names);
765  es->printed_subplans = NULL;
766 
767  /*
768  * Sometimes we mark a Gather node as "invisible", which means that it's
769  * not to be displayed in EXPLAIN output. The purpose of this is to allow
770  * running regression tests with debug_parallel_query=regress to get the
771  * same results as running the same tests with debug_parallel_query=off.
772  * Such marking is currently only supported on a Gather at the top of the
773  * plan. We skip that node, and we must also hide per-worker detail data
774  * further down in the plan tree.
775  */
776  ps = queryDesc->planstate;
777  if (IsA(ps, GatherState) && ((Gather *) ps->plan)->invisible)
778  {
779  ps = outerPlanState(ps);
780  es->hide_workers = true;
781  }
782  ExplainNode(ps, NIL, NULL, NULL, es);
783 
784  /*
785  * If requested, include information about GUC parameters with values that
786  * don't match the built-in defaults.
787  */
789 
790  /*
791  * COMPUTE_QUERY_ID_REGRESS means COMPUTE_QUERY_ID_AUTO, but we don't show
792  * the queryid in any of the EXPLAIN plans to keep stable the results
793  * generated by regression test suites.
794  */
795  if (es->verbose && queryDesc->plannedstmt->queryId != UINT64CONST(0) &&
797  {
798  /*
799  * Output the queryid as an int64 rather than a uint64 so we match
800  * what would be seen in the BIGINT pg_stat_statements.queryid column.
801  */
802  ExplainPropertyInteger("Query Identifier", NULL, (int64)
803  queryDesc->plannedstmt->queryId, es);
804  }
805 }
#define outerPlanState(node)
Definition: execnodes.h:1133
static void ExplainNode(PlanState *planstate, List *ancestors, const char *relationship, const char *plan_name, ExplainState *es)
Definition: explain.c:1186
static bool ExplainPreScanNode(PlanState *planstate, Bitmapset **rels_used)
Definition: explain.c:1111
void ExplainPropertyInteger(const char *qlabel, const char *unit, int64 value, ExplainState *es)
Definition: explain.c:4571
static void ExplainPrintSettings(ExplainState *es)
Definition: explain.c:682
struct parser_state ps
#define NIL
Definition: pg_list.h:68
@ COMPUTE_QUERY_ID_REGRESS
Definition: queryjumble.h:59
int compute_query_id
List * select_rtable_names_for_explain(List *rtable, Bitmapset *rels_used)
Definition: ruleutils.c:3797
List * deparse_context_for_plan_tree(PlannedStmt *pstmt, List *rtable_names)
Definition: ruleutils.c:3705
bool verbose
Definition: explain.h:41
Bitmapset * printed_subplans
Definition: explain.h:59
bool hide_workers
Definition: explain.h:60
List * rtable_names
Definition: explain.h:57
List * rtable
Definition: explain.h:56
PlannedStmt * pstmt
Definition: explain.h:55
List * deparse_cxt
Definition: explain.h:58
List * rtable
Definition: plannodes.h:73
uint64 queryId
Definition: plannodes.h:55
PlannedStmt * plannedstmt
Definition: execdesc.h:37
PlanState * planstate
Definition: execdesc.h:49

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

Referenced by explain_ExecutorEnd(), and ExplainOnePlan().

◆ ExplainPrintTriggers()

void ExplainPrintTriggers ( ExplainState es,
QueryDesc queryDesc 
)

Definition at line 817 of file explain.c.

818 {
819  ResultRelInfo *rInfo;
820  bool show_relname;
821  List *resultrels;
822  List *routerels;
823  List *targrels;
824  ListCell *l;
825 
826  resultrels = queryDesc->estate->es_opened_result_relations;
827  routerels = queryDesc->estate->es_tuple_routing_result_relations;
828  targrels = queryDesc->estate->es_trig_target_relations;
829 
830  ExplainOpenGroup("Triggers", "Triggers", false, es);
831 
832  show_relname = (list_length(resultrels) > 1 ||
833  routerels != NIL || targrels != NIL);
834  foreach(l, resultrels)
835  {
836  rInfo = (ResultRelInfo *) lfirst(l);
837  report_triggers(rInfo, show_relname, es);
838  }
839 
840  foreach(l, routerels)
841  {
842  rInfo = (ResultRelInfo *) lfirst(l);
843  report_triggers(rInfo, show_relname, es);
844  }
845 
846  foreach(l, targrels)
847  {
848  rInfo = (ResultRelInfo *) lfirst(l);
849  report_triggers(rInfo, show_relname, es);
850  }
851 
852  ExplainCloseGroup("Triggers", "Triggers", false, es);
853 }
static void report_triggers(ResultRelInfo *rInfo, bool show_relname, ExplainState *es)
Definition: explain.c:1021
#define lfirst(lc)
Definition: pg_list.h:172
List * es_tuple_routing_result_relations
Definition: execnodes.h:647
List * es_trig_target_relations
Definition: execnodes.h:650
List * es_opened_result_relations
Definition: execnodes.h:637

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

◆ ExplainPropertyBool()

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

Definition at line 4612 of file explain.c.

4613 {
4614  ExplainProperty(qlabel, NULL, value ? "true" : "false", true, es);
4615 }
static void ExplainProperty(const char *qlabel, const char *unit, const char *value, bool numeric, ExplainState *es)
Definition: explain.c:4509
static struct @148 value

References ExplainProperty(), and value.

Referenced by ExplainNode(), and ExplainPrintJIT().

◆ ExplainPropertyFloat()

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

Definition at line 4598 of file explain.c.

4600 {
4601  char *buf;
4602 
4603  buf = psprintf("%.*f", ndigits, value);
4604  ExplainProperty(qlabel, unit, buf, true, es);
4605  pfree(buf);
4606 }
void pfree(void *pointer)
Definition: mcxt.c:1456
static char * buf
Definition: pg_test_fsync.c:67
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46

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

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

◆ ExplainPropertyInteger()

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

◆ ExplainPropertyList()

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

Definition at line 4386 of file explain.c.

4387 {
4388  ListCell *lc;
4389  bool first = true;
4390 
4391  switch (es->format)
4392  {
4393  case EXPLAIN_FORMAT_TEXT:
4394  ExplainIndentText(es);
4395  appendStringInfo(es->str, "%s: ", qlabel);
4396  foreach(lc, data)
4397  {
4398  if (!first)
4399  appendStringInfoString(es->str, ", ");
4400  appendStringInfoString(es->str, (const char *) lfirst(lc));
4401  first = false;
4402  }
4403  appendStringInfoChar(es->str, '\n');
4404  break;
4405 
4406  case EXPLAIN_FORMAT_XML:
4407  ExplainXMLTag(qlabel, X_OPENING, es);
4408  foreach(lc, data)
4409  {
4410  char *str;
4411 
4412  appendStringInfoSpaces(es->str, es->indent * 2 + 2);
4413  appendStringInfoString(es->str, "<Item>");
4414  str = escape_xml((const char *) lfirst(lc));
4416  pfree(str);
4417  appendStringInfoString(es->str, "</Item>\n");
4418  }
4419  ExplainXMLTag(qlabel, X_CLOSING, es);
4420  break;
4421 
4422  case EXPLAIN_FORMAT_JSON:
4424  appendStringInfoSpaces(es->str, es->indent * 2);
4425  escape_json(es->str, qlabel);
4426  appendStringInfoString(es->str, ": [");
4427  foreach(lc, data)
4428  {
4429  if (!first)
4430  appendStringInfoString(es->str, ", ");
4431  escape_json(es->str, (const char *) lfirst(lc));
4432  first = false;
4433  }
4434  appendStringInfoChar(es->str, ']');
4435  break;
4436 
4437  case EXPLAIN_FORMAT_YAML:
4439  appendStringInfo(es->str, "%s: ", qlabel);
4440  foreach(lc, data)
4441  {
4442  appendStringInfoChar(es->str, '\n');
4443  appendStringInfoSpaces(es->str, es->indent * 2 + 2);
4444  appendStringInfoString(es->str, "- ");
4445  escape_yaml(es->str, (const char *) lfirst(lc));
4446  }
4447  break;
4448  }
4449 }
static void ExplainIndentText(ExplainState *es)
Definition: explain.c:4999
static void escape_yaml(StringInfo buf, const char *str)
Definition: explain.c:5059
const void * data
char * escape_xml(const char *str)
Definition: xml.c:2603

References appendStringInfo(), appendStringInfoChar(), appendStringInfoSpaces(), appendStringInfoString(), data, 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(), generate_unaccent_rules::str, ExplainState::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().

◆ ExplainPropertyListNested()

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

Definition at line 4456 of file explain.c.

4457 {
4458  ListCell *lc;
4459  bool first = true;
4460 
4461  switch (es->format)
4462  {
4463  case EXPLAIN_FORMAT_TEXT:
4464  case EXPLAIN_FORMAT_XML:
4465  ExplainPropertyList(qlabel, data, es);
4466  return;
4467 
4468  case EXPLAIN_FORMAT_JSON:
4470  appendStringInfoSpaces(es->str, es->indent * 2);
4471  appendStringInfoChar(es->str, '[');
4472  foreach(lc, data)
4473  {
4474  if (!first)
4475  appendStringInfoString(es->str, ", ");
4476  escape_json(es->str, (const char *) lfirst(lc));
4477  first = false;
4478  }
4479  appendStringInfoChar(es->str, ']');
4480  break;
4481 
4482  case EXPLAIN_FORMAT_YAML:
4484  appendStringInfoString(es->str, "- [");
4485  foreach(lc, data)
4486  {
4487  if (!first)
4488  appendStringInfoString(es->str, ", ");
4489  escape_yaml(es->str, (const char *) lfirst(lc));
4490  first = false;
4491  }
4492  appendStringInfoChar(es->str, ']');
4493  break;
4494  }
4495 }
void ExplainPropertyList(const char *qlabel, List *data, ExplainState *es)
Definition: explain.c:4386

References appendStringInfoChar(), appendStringInfoSpaces(), appendStringInfoString(), data, 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().

◆ ExplainPropertyText()

◆ ExplainPropertyUInteger()

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

Definition at line 4584 of file explain.c.

4586 {
4587  char buf[32];
4588 
4589  snprintf(buf, sizeof(buf), UINT64_FORMAT, value);
4590  ExplainProperty(qlabel, unit, buf, true, es);
4591 }
#define UINT64_FORMAT
Definition: c.h:538

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

Referenced by show_wal_usage().

◆ ExplainQuery()

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

Definition at line 164 of file explain.c.

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, "generic_plan") == 0)
194  es->generic = defGetBoolean(opt);
195  else if (strcmp(opt->defname, "timing") == 0)
196  {
197  timing_set = true;
198  es->timing = defGetBoolean(opt);
199  }
200  else if (strcmp(opt->defname, "summary") == 0)
201  {
202  summary_set = true;
203  es->summary = defGetBoolean(opt);
204  }
205  else if (strcmp(opt->defname, "format") == 0)
206  {
207  char *p = defGetString(opt);
208 
209  if (strcmp(p, "text") == 0)
211  else if (strcmp(p, "xml") == 0)
213  else if (strcmp(p, "json") == 0)
215  else if (strcmp(p, "yaml") == 0)
217  else
218  ereport(ERROR,
219  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
220  errmsg("unrecognized value for EXPLAIN option \"%s\": \"%s\"",
221  opt->defname, p),
222  parser_errposition(pstate, opt->location)));
223  }
224  else
225  ereport(ERROR,
226  (errcode(ERRCODE_SYNTAX_ERROR),
227  errmsg("unrecognized EXPLAIN option \"%s\"",
228  opt->defname),
229  parser_errposition(pstate, opt->location)));
230  }
231 
232  /* check that WAL is used with EXPLAIN ANALYZE */
233  if (es->wal && !es->analyze)
234  ereport(ERROR,
235  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
236  errmsg("EXPLAIN option WAL requires ANALYZE")));
237 
238  /* if the timing was not set explicitly, set default value */
239  es->timing = (timing_set) ? es->timing : es->analyze;
240 
241  /* check that timing is used with EXPLAIN ANALYZE */
242  if (es->timing && !es->analyze)
243  ereport(ERROR,
244  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
245  errmsg("EXPLAIN option TIMING requires ANALYZE")));
246 
247  /* check that GENERIC_PLAN is not used with EXPLAIN ANALYZE */
248  if (es->generic && es->analyze)
249  ereport(ERROR,
250  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
251  errmsg("EXPLAIN options ANALYZE and GENERIC_PLAN cannot be used together")));
252 
253  /* if the summary was not set explicitly, set default value */
254  es->summary = (summary_set) ? es->summary : es->analyze;
255 
256  query = castNode(Query, stmt->query);
257  if (IsQueryIdEnabled())
258  jstate = JumbleQuery(query);
259 
261  (*post_parse_analyze_hook) (pstate, query, jstate);
262 
263  /*
264  * Parse analysis was done already, but we still have to run the rule
265  * rewriter. We do not do AcquireRewriteLocks: we assume the query either
266  * came straight from the parser, or suitable locks were acquired by
267  * plancache.c.
268  */
269  rewritten = QueryRewrite(castNode(Query, stmt->query));
270 
271  /* emit opening boilerplate */
272  ExplainBeginOutput(es);
273 
274  if (rewritten == NIL)
275  {
276  /*
277  * In the case of an INSTEAD NOTHING, tell at least that. But in
278  * non-text format, the output is delimited, so this isn't necessary.
279  */
280  if (es->format == EXPLAIN_FORMAT_TEXT)
281  appendStringInfoString(es->str, "Query rewrites to nothing\n");
282  }
283  else
284  {
285  ListCell *l;
286 
287  /* Explain every plan */
288  foreach(l, rewritten)
289  {
291  CURSOR_OPT_PARALLEL_OK, NULL, es,
292  pstate->p_sourcetext, params, pstate->p_queryEnv);
293 
294  /* Separate plans with an appropriate separator */
295  if (lnext(rewritten, l) != NULL)
297  }
298  }
299 
300  /* emit closing boilerplate */
301  ExplainEndOutput(es);
302  Assert(es->indent == 0);
303 
304  /* output tuples */
306  &TTSOpsVirtual);
307  if (es->format == EXPLAIN_FORMAT_TEXT)
308  do_text_output_multiline(tstate, es->str->data);
309  else
310  do_text_output_oneline(tstate, es->str->data);
311  end_tup_output(tstate);
312 
313  pfree(es->str->data);
314 }
bool defGetBoolean(DefElem *def)
Definition: define.c:108
char * defGetString(DefElem *def)
Definition: define.c:49
int errcode(int sqlerrcode)
Definition: elog.c:858
int errmsg(const char *fmt,...)
Definition: elog.c:1069
#define ereport(elevel,...)
Definition: elog.h:149
const TupleTableSlotOps TTSOpsVirtual
Definition: execTuples.c:83
void end_tup_output(TupOutputState *tstate)
Definition: execTuples.c:2334
void do_text_output_multiline(TupOutputState *tstate, const char *txt)
Definition: execTuples.c:2304
TupOutputState * begin_tup_output_tupdesc(DestReceiver *dest, TupleDesc tupdesc, const TupleTableSlotOps *tts_ops)
Definition: execTuples.c:2256
#define do_text_output_oneline(tstate, str_to_emit)
Definition: executor.h:521
void ExplainSeparatePlans(ExplainState *es)
Definition: explain.c:4943
void ExplainEndOutput(ExplainState *es)
Definition: explain.c:4914
TupleDesc ExplainResultDesc(ExplainStmt *stmt)
Definition: explain.c:337
void ExplainBeginOutput(ExplainState *es)
Definition: explain.c:4883
ExplainState * NewExplainState(void)
Definition: explain.c:320
#define stmt
Definition: indent_codes.h:59
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:77
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
post_parse_analyze_hook_type post_parse_analyze_hook
Definition: analyze.c:60
#define lfirst_node(type, lc)
Definition: pg_list.h:176
static ListCell * lnext(const List *l, const ListCell *c)
Definition: pg_list.h:343
static bool IsQueryIdEnabled(void)
Definition: queryjumble.h:77
JumbleState * JumbleQuery(Query *query)
char * defname
Definition: parsenodes.h:809
int location
Definition: parsenodes.h:813
bool settings
Definition: explain.h:48

References ExplainState::analyze, appendStringInfoString(), Assert(), begin_tup_output_tupdesc(), ExplainState::buffers, castNode, ExplainState::costs, CURSOR_OPT_PARALLEL_OK, StringInfoData::data, defGetBoolean(), defGetString(), DefElem::defname, generate_unaccent_rules::dest, 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::generic, if(), ExplainState::indent, IsQueryIdEnabled(), JumbleQuery(), lfirst, lfirst_node, lnext(), DefElem::location, NewExplainState(), NIL, ParseState::p_queryEnv, ParseState::p_sourcetext, parser_errposition(), pfree(), post_parse_analyze_hook, QueryRewrite(), ExplainState::settings, stmt, ExplainState::str, ExplainState::summary, ExplainState::timing, TTSOpsVirtual, ExplainState::verbose, and ExplainState::wal.

Referenced by standard_ProcessUtility().

◆ ExplainQueryParameters()

void ExplainQueryParameters ( ExplainState es,
ParamListInfo  params,
int  maxlen 
)

Definition at line 1003 of file explain.c.

1004 {
1005  char *str;
1006 
1007  /* This check is consistent with errdetail_params() */
1008  if (params == NULL || params->numParams <= 0 || maxlen == 0)
1009  return;
1010 
1011  str = BuildParamLogString(params, NULL, maxlen);
1012  if (str && str[0] != '\0')
1013  ExplainPropertyText("Query Parameters", str, es);
1014 }
void ExplainPropertyText(const char *qlabel, const char *value, ExplainState *es)
Definition: explain.c:4562
char * BuildParamLogString(ParamListInfo params, char **knownTextValues, int maxlen)
Definition: params.c:335

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

Referenced by explain_ExecutorEnd().

◆ ExplainQueryText()

void ExplainQueryText ( ExplainState es,
QueryDesc queryDesc 
)

Definition at line 988 of file explain.c.

989 {
990  if (queryDesc->sourceText)
991  ExplainPropertyText("Query Text", queryDesc->sourceText, es);
992 }
const char * sourceText
Definition: execdesc.h:38

References ExplainPropertyText(), and QueryDesc::sourceText.

Referenced by explain_ExecutorEnd().

◆ ExplainResultDesc()

TupleDesc ExplainResultDesc ( ExplainStmt stmt)

Definition at line 337 of file explain.c.

338 {
339  TupleDesc tupdesc;
340  ListCell *lc;
341  Oid result_type = TEXTOID;
342 
343  /* Check for XML format option */
344  foreach(lc, stmt->options)
345  {
346  DefElem *opt = (DefElem *) lfirst(lc);
347 
348  if (strcmp(opt->defname, "format") == 0)
349  {
350  char *p = defGetString(opt);
351 
352  if (strcmp(p, "xml") == 0)
353  result_type = XMLOID;
354  else if (strcmp(p, "json") == 0)
355  result_type = JSONOID;
356  else
357  result_type = TEXTOID;
358  /* don't "break", as ExplainQuery will use the last value */
359  }
360  }
361 
362  /* Need a tuple descriptor representing a single TEXT or XML column */
363  tupdesc = CreateTemplateTupleDesc(1);
364  TupleDescInitEntry(tupdesc, (AttrNumber) 1, "QUERY PLAN",
365  result_type, -1, 0);
366  return tupdesc;
367 }
int16 AttrNumber
Definition: attnum.h:21
unsigned int Oid
Definition: postgres_ext.h:31
TupleDesc CreateTemplateTupleDesc(int natts)
Definition: tupdesc.c:45
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition: tupdesc.c:583

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

Referenced by ExplainQuery(), and UtilityTupleDescriptor().

◆ ExplainSeparatePlans()

void ExplainSeparatePlans ( ExplainState es)

Definition at line 4943 of file explain.c.

4944 {
4945  switch (es->format)
4946  {
4947  case EXPLAIN_FORMAT_TEXT:
4948  /* add a blank line */
4949  appendStringInfoChar(es->str, '\n');
4950  break;
4951 
4952  case EXPLAIN_FORMAT_XML:
4953  case EXPLAIN_FORMAT_JSON:
4954  case EXPLAIN_FORMAT_YAML:
4955  /* nothing to do */
4956  break;
4957  }
4958 }

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

Referenced by ExplainExecuteQuery(), and ExplainQuery().

◆ NewExplainState()

ExplainState* NewExplainState ( void  )

Definition at line 320 of file explain.c.

321 {
322  ExplainState *es = (ExplainState *) palloc0(sizeof(ExplainState));
323 
324  /* Set default options (most fields can be left as zeroes). */
325  es->costs = true;
326  /* Prepare output buffer. */
327  es->str = makeStringInfo();
328 
329  return es;
330 }
void * palloc0(Size size)
Definition: mcxt.c:1257
StringInfo makeStringInfo(void)
Definition: stringinfo.c:41

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

Referenced by explain_ExecutorEnd(), and ExplainQuery().

Variable Documentation

◆ explain_get_index_name_hook

PGDLLIMPORT explain_get_index_name_hook_type explain_get_index_name_hook
extern

Definition at line 48 of file explain.c.

Referenced by explain_get_index_name().

◆ ExplainOneQuery_hook

PGDLLIMPORT ExplainOneQuery_hook_type ExplainOneQuery_hook
extern

Definition at line 45 of file explain.c.

Referenced by ExplainOneQuery().