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

4873 {
4874  switch (es->format)
4875  {
4876  case EXPLAIN_FORMAT_TEXT:
4877  /* nothing to do */
4878  break;
4879 
4880  case EXPLAIN_FORMAT_XML:
4882  "<explain xmlns=\"http://www.postgresql.org/2009/explain\">\n");
4883  es->indent++;
4884  break;
4885 
4886  case EXPLAIN_FORMAT_JSON:
4887  /* top-level structure is an array of plans */
4888  appendStringInfoChar(es->str, '[');
4889  es->grouping_stack = lcons_int(0, es->grouping_stack);
4890  es->indent++;
4891  break;
4892 
4893  case EXPLAIN_FORMAT_YAML:
4894  es->grouping_stack = lcons_int(0, es->grouping_stack);
4895  break;
4896  }
4897 }
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 4679 of file explain.c.

4681 {
4682  switch (es->format)
4683  {
4684  case EXPLAIN_FORMAT_TEXT:
4685  /* nothing to do */
4686  break;
4687 
4688  case EXPLAIN_FORMAT_XML:
4689  es->indent--;
4690  ExplainXMLTag(objtype, X_CLOSING, es);
4691  break;
4692 
4693  case EXPLAIN_FORMAT_JSON:
4694  es->indent--;
4695  appendStringInfoChar(es->str, '\n');
4696  appendStringInfoSpaces(es->str, 2 * es->indent);
4697  appendStringInfoChar(es->str, labeled ? '}' : ']');
4699  break;
4700 
4701  case EXPLAIN_FORMAT_YAML:
4702  es->indent--;
4704  break;
4705  }
4706 }
static void ExplainXMLTag(const char *tagname, int flags, ExplainState *es)
Definition: explain.c:4961
#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 4903 of file explain.c.

4904 {
4905  switch (es->format)
4906  {
4907  case EXPLAIN_FORMAT_TEXT:
4908  /* nothing to do */
4909  break;
4910 
4911  case EXPLAIN_FORMAT_XML:
4912  es->indent--;
4913  appendStringInfoString(es->str, "</explain>");
4914  break;
4915 
4916  case EXPLAIN_FORMAT_JSON:
4917  es->indent--;
4918  appendStringInfoString(es->str, "\n]");
4920  break;
4921 
4922  case EXPLAIN_FORMAT_YAML:
4924  break;
4925  }
4926 }

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:462
void ExecutorFinish(QueryDesc *queryDesc)
Definition: execMain.c:402
void ExecutorStart(QueryDesc *queryDesc, int eflags)
Definition: execMain.c:132
void ExecutorRun(QueryDesc *queryDesc, ScanDirection direction, uint64 count, bool execute_once)
Definition: execMain.c:301
#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:4616
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:4587
void ExplainCloseGroup(const char *objtype, const char *labelname, bool labeled, ExplainState *es)
Definition: explain.c:4679
static void show_buffer_usage(ExplainState *es, const BufferUsage *usage, bool planning)
Definition: explain.c:3540
static double elapsed_time(instr_time *starttime)
Definition: explain.c:1084
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:747
void PopActiveSnapshot(void)
Definition: snapmgr.c:778
void PushCopiedSnapshot(Snapshot snapshot)
Definition: snapmgr.c:735
Snapshot GetActiveSnapshot(void)
Definition: snapmgr.c:805
#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:4826
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:2105
@ OBJECT_TABLE
Definition: parsenodes.h:2123
#define CURSOR_OPT_PARALLEL_OK
Definition: parsenodes.h:3124
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:3720
ObjectType objtype
Definition: parsenodes.h:3721
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 4616 of file explain.c.

4618 {
4619  switch (es->format)
4620  {
4621  case EXPLAIN_FORMAT_TEXT:
4622  /* nothing to do */
4623  break;
4624 
4625  case EXPLAIN_FORMAT_XML:
4626  ExplainXMLTag(objtype, X_OPENING, es);
4627  es->indent++;
4628  break;
4629 
4630  case EXPLAIN_FORMAT_JSON:
4632  appendStringInfoSpaces(es->str, 2 * es->indent);
4633  if (labelname)
4634  {
4635  escape_json(es->str, labelname);
4636  appendStringInfoString(es->str, ": ");
4637  }
4638  appendStringInfoChar(es->str, labeled ? '{' : '[');
4639 
4640  /*
4641  * In JSON format, the grouping_stack is an integer list. 0 means
4642  * we've emitted nothing at this grouping level, 1 means we've
4643  * emitted something (and so the next item needs a comma). See
4644  * ExplainJSONLineEnding().
4645  */
4646  es->grouping_stack = lcons_int(0, es->grouping_stack);
4647  es->indent++;
4648  break;
4649 
4650  case EXPLAIN_FORMAT_YAML:
4651 
4652  /*
4653  * In YAML format, the grouping stack is an integer list. 0 means
4654  * we've emitted nothing at this grouping level AND this grouping
4655  * level is unlabeled and must be marked with "- ". See
4656  * ExplainYAMLLineStarting().
4657  */
4659  if (labelname)
4660  {
4661  appendStringInfo(es->str, "%s: ", labelname);
4662  es->grouping_stack = lcons_int(1, es->grouping_stack);
4663  }
4664  else
4665  {
4666  appendStringInfoString(es->str, "- ");
4667  es->grouping_stack = lcons_int(0, es->grouping_stack);
4668  }
4669  es->indent++;
4670  break;
4671  }
4672 }
#define X_OPENING
Definition: explain.c:52
static void ExplainYAMLLineStarting(ExplainState *es)
Definition: explain.c:5023
static void ExplainJSONLineEnding(ExplainState *es)
Definition: explain.c:5003
void escape_json(StringInfo buf, const char *str)
Definition: json.c:1610
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:706
struct JitInstrumentation * es_jit_worker_instr
Definition: execnodes.h:707
int es_jit_flags
Definition: execnodes.h:705
JitInstrumentation instr
Definition: jit.h:61
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:1131
static void ExplainNode(PlanState *planstate, List *ancestors, const char *relationship, const char *plan_name, ExplainState *es)
Definition: explain.c:1178
static bool ExplainPreScanNode(PlanState *planstate, Bitmapset **rels_used)
Definition: explain.c:1103
void ExplainPropertyInteger(const char *qlabel, const char *unit, int64 value, ExplainState *es)
Definition: explain.c:4560
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:3765
List * deparse_context_for_plan_tree(PlannedStmt *pstmt, List *rtable_names)
Definition: ruleutils.c:3673
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:76
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:1013
#define lfirst(lc)
Definition: pg_list.h:172
List * es_tuple_routing_result_relations
Definition: execnodes.h:648
List * es_trig_target_relations
Definition: execnodes.h:651
List * es_opened_result_relations
Definition: execnodes.h:638

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

4602 {
4603  ExplainProperty(qlabel, NULL, value ? "true" : "false", true, es);
4604 }
static void ExplainProperty(const char *qlabel, const char *unit, const char *value, bool numeric, ExplainState *es)
Definition: explain.c:4498
static struct @145 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 4587 of file explain.c.

4589 {
4590  char *buf;
4591 
4592  buf = psprintf("%.*f", ndigits, value);
4593  ExplainProperty(qlabel, unit, buf, true, es);
4594  pfree(buf);
4595 }
void pfree(void *pointer)
Definition: mcxt.c:1436
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 4375 of file explain.c.

4376 {
4377  ListCell *lc;
4378  bool first = true;
4379 
4380  switch (es->format)
4381  {
4382  case EXPLAIN_FORMAT_TEXT:
4383  ExplainIndentText(es);
4384  appendStringInfo(es->str, "%s: ", qlabel);
4385  foreach(lc, data)
4386  {
4387  if (!first)
4388  appendStringInfoString(es->str, ", ");
4389  appendStringInfoString(es->str, (const char *) lfirst(lc));
4390  first = false;
4391  }
4392  appendStringInfoChar(es->str, '\n');
4393  break;
4394 
4395  case EXPLAIN_FORMAT_XML:
4396  ExplainXMLTag(qlabel, X_OPENING, es);
4397  foreach(lc, data)
4398  {
4399  char *str;
4400 
4401  appendStringInfoSpaces(es->str, es->indent * 2 + 2);
4402  appendStringInfoString(es->str, "<Item>");
4403  str = escape_xml((const char *) lfirst(lc));
4405  pfree(str);
4406  appendStringInfoString(es->str, "</Item>\n");
4407  }
4408  ExplainXMLTag(qlabel, X_CLOSING, es);
4409  break;
4410 
4411  case EXPLAIN_FORMAT_JSON:
4413  appendStringInfoSpaces(es->str, es->indent * 2);
4414  escape_json(es->str, qlabel);
4415  appendStringInfoString(es->str, ": [");
4416  foreach(lc, data)
4417  {
4418  if (!first)
4419  appendStringInfoString(es->str, ", ");
4420  escape_json(es->str, (const char *) lfirst(lc));
4421  first = false;
4422  }
4423  appendStringInfoChar(es->str, ']');
4424  break;
4425 
4426  case EXPLAIN_FORMAT_YAML:
4428  appendStringInfo(es->str, "%s: ", qlabel);
4429  foreach(lc, data)
4430  {
4431  appendStringInfoChar(es->str, '\n');
4432  appendStringInfoSpaces(es->str, es->indent * 2 + 2);
4433  appendStringInfoString(es->str, "- ");
4434  escape_yaml(es->str, (const char *) lfirst(lc));
4435  }
4436  break;
4437  }
4438 }
static void ExplainIndentText(ExplainState *es)
Definition: explain.c:4988
static void escape_yaml(StringInfo buf, const char *str)
Definition: explain.c:5048
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 4445 of file explain.c.

4446 {
4447  ListCell *lc;
4448  bool first = true;
4449 
4450  switch (es->format)
4451  {
4452  case EXPLAIN_FORMAT_TEXT:
4453  case EXPLAIN_FORMAT_XML:
4454  ExplainPropertyList(qlabel, data, es);
4455  return;
4456 
4457  case EXPLAIN_FORMAT_JSON:
4459  appendStringInfoSpaces(es->str, es->indent * 2);
4460  appendStringInfoChar(es->str, '[');
4461  foreach(lc, data)
4462  {
4463  if (!first)
4464  appendStringInfoString(es->str, ", ");
4465  escape_json(es->str, (const char *) lfirst(lc));
4466  first = false;
4467  }
4468  appendStringInfoChar(es->str, ']');
4469  break;
4470 
4471  case EXPLAIN_FORMAT_YAML:
4473  appendStringInfoString(es->str, "- [");
4474  foreach(lc, data)
4475  {
4476  if (!first)
4477  appendStringInfoString(es->str, ", ");
4478  escape_yaml(es->str, (const char *) lfirst(lc));
4479  first = false;
4480  }
4481  appendStringInfoChar(es->str, ']');
4482  break;
4483  }
4484 }
void ExplainPropertyList(const char *qlabel, List *data, ExplainState *es)
Definition: explain.c:4375

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

4575 {
4576  char buf[32];
4577 
4578  snprintf(buf, sizeof(buf), UINT64_FORMAT, value);
4579  ExplainProperty(qlabel, unit, buf, true, es);
4580 }
#define UINT64_FORMAT
Definition: c.h:533

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, pstate->p_sourcetext);
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:520
void ExplainSeparatePlans(ExplainState *es)
Definition: explain.c:4932
void ExplainEndOutput(ExplainState *es)
Definition: explain.c:4903
TupleDesc ExplainResultDesc(ExplainStmt *stmt)
Definition: explain.c:337
void ExplainBeginOutput(ExplainState *es)
Definition: explain.c:4872
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, const char *querytext)
char * defname
Definition: parsenodes.h:810
int location
Definition: parsenodes.h:814
bool settings
Definition: explain.h:48
const char * p_sourcetext
Definition: parse_node.h:192

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

996 {
997  char *str;
998 
999  /* This check is consistent with errdetail_params() */
1000  if (params == NULL || params->numParams <= 0 || maxlen == 0)
1001  return;
1002 
1003  str = BuildParamLogString(params, NULL, maxlen);
1004  if (str && str[0] != '\0')
1005  ExplainPropertyText("Query Parameters", str, es);
1006 }
void ExplainPropertyText(const char *qlabel, const char *value, ExplainState *es)
Definition: explain.c:4551
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 980 of file explain.c.

981 {
982  if (queryDesc->sourceText)
983  ExplainPropertyText("Query Text", queryDesc->sourceText, es);
984 }
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 4932 of file explain.c.

4933 {
4934  switch (es->format)
4935  {
4936  case EXPLAIN_FORMAT_TEXT:
4937  /* add a blank line */
4938  appendStringInfoChar(es->str, '\n');
4939  break;
4940 
4941  case EXPLAIN_FORMAT_XML:
4942  case EXPLAIN_FORMAT_JSON:
4943  case EXPLAIN_FORMAT_YAML:
4944  /* nothing to do */
4945  break;
4946  }
4947 }

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