PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
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  ExplainState
 

Typedefs

typedef enum ExplainFormat ExplainFormat
 
typedef struct ExplainState ExplainState
 
typedef void(* ExplainOneQuery_hook_type )(Query *query, int cursorOptions, IntoClause *into, ExplainState *es, const char *queryString, ParamListInfo params)
 
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, const char *queryString, ParamListInfo params, DestReceiver *dest)
 
ExplainStateNewExplainState (void)
 
TupleDesc ExplainResultDesc (ExplainStmt *stmt)
 
void ExplainOneUtility (Node *utilityStmt, IntoClause *into, ExplainState *es, const char *queryString, ParamListInfo params)
 
void ExplainOnePlan (PlannedStmt *plannedstmt, IntoClause *into, ExplainState *es, const char *queryString, ParamListInfo params, const instr_time *planduration)
 
void ExplainPrintPlan (ExplainState *es, QueryDesc *queryDesc)
 
void ExplainPrintTriggers (ExplainState *es, QueryDesc *queryDesc)
 
void ExplainQueryText (ExplainState *es, QueryDesc *queryDesc)
 
void ExplainBeginOutput (ExplainState *es)
 
void ExplainEndOutput (ExplainState *es)
 
void ExplainSeparatePlans (ExplainState *es)
 
void ExplainPropertyList (const char *qlabel, List *data, ExplainState *es)
 
void ExplainPropertyListNested (const char *qlabel, List *data, ExplainState *es)
 
void ExplainPropertyText (const char *qlabel, const char *value, ExplainState *es)
 
void ExplainPropertyInteger (const char *qlabel, int value, ExplainState *es)
 
void ExplainPropertyLong (const char *qlabel, long value, ExplainState *es)
 
void ExplainPropertyFloat (const char *qlabel, double value, int ndigits, ExplainState *es)
 
void ExplainPropertyBool (const char *qlabel, bool value, ExplainState *es)
 

Variables

PGDLLIMPORT
ExplainOneQuery_hook_type 
ExplainOneQuery_hook
 
PGDLLIMPORT
explain_get_index_name_hook_type 
explain_get_index_name_hook
 

Typedef Documentation

typedef const char*(* explain_get_index_name_hook_type)(Oid indexId)

Definition at line 60 of file explain.h.

typedef void(* ExplainOneQuery_hook_type)(Query *query, int cursorOptions, IntoClause *into, ExplainState *es, const char *queryString, ParamListInfo params)

Definition at line 51 of file explain.h.

Enumeration Type Documentation

Enumerator
EXPLAIN_FORMAT_TEXT 
EXPLAIN_FORMAT_XML 
EXPLAIN_FORMAT_JSON 
EXPLAIN_FORMAT_YAML 

Definition at line 20 of file explain.h.

Function Documentation

void ExplainBeginOutput ( ExplainState es)

Definition at line 3312 of file explain.c.

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

Referenced by explain_ExecutorEnd(), and ExplainQuery().

3313 {
3314  switch (es->format)
3315  {
3316  case EXPLAIN_FORMAT_TEXT:
3317  /* nothing to do */
3318  break;
3319 
3320  case EXPLAIN_FORMAT_XML:
3322  "<explain xmlns=\"http://www.postgresql.org/2009/explain\">\n");
3323  es->indent++;
3324  break;
3325 
3326  case EXPLAIN_FORMAT_JSON:
3327  /* top-level structure is an array of plans */
3328  appendStringInfoChar(es->str, '[');
3329  es->grouping_stack = lcons_int(0, es->grouping_stack);
3330  es->indent++;
3331  break;
3332 
3333  case EXPLAIN_FORMAT_YAML:
3334  es->grouping_stack = lcons_int(0, es->grouping_stack);
3335  break;
3336  }
3337 }
List * lcons_int(int datum, List *list)
Definition: list.c:277
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:189
List * grouping_stack
Definition: explain.h:41
int indent
Definition: explain.h:40
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:201
ExplainFormat format
Definition: explain.h:38
StringInfo str
Definition: explain.h:30
void ExplainEndOutput ( ExplainState es)

Definition at line 3343 of file explain.c.

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

Referenced by explain_ExecutorEnd(), and ExplainQuery().

3344 {
3345  switch (es->format)
3346  {
3347  case EXPLAIN_FORMAT_TEXT:
3348  /* nothing to do */
3349  break;
3350 
3351  case EXPLAIN_FORMAT_XML:
3352  es->indent--;
3353  appendStringInfoString(es->str, "</explain>");
3354  break;
3355 
3356  case EXPLAIN_FORMAT_JSON:
3357  es->indent--;
3358  appendStringInfoString(es->str, "\n]");
3360  break;
3361 
3362  case EXPLAIN_FORMAT_YAML:
3364  break;
3365  }
3366 }
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:189
List * grouping_stack
Definition: explain.h:41
int indent
Definition: explain.h:40
ExplainFormat format
Definition: explain.h:38
StringInfo str
Definition: explain.h:30
List * list_delete_first(List *list)
Definition: list.c:666
void ExplainOnePlan ( PlannedStmt plannedstmt,
IntoClause into,
ExplainState es,
const char *  queryString,
ParamListInfo  params,
const instr_time planduration 
)

Definition at line 461 of file explain.c.

References ExplainState::analyze, appendStringInfo(), Assert, ExplainState::buffers, CMD_UTILITY, CommandCounterIncrement(), PlannedStmt::commandType, CreateIntoRelDestReceiver(), CreateQueryDesc(), elapsed_time(), EXEC_FLAG_EXPLAIN_ONLY, ExecutorEnd(), ExecutorFinish(), ExecutorRun(), ExecutorStart(), EXPLAIN_FORMAT_TEXT, ExplainCloseGroup(), ExplainOpenGroup(), ExplainPrintPlan(), ExplainPrintTriggers(), ExplainPropertyFloat(), ExplainState::format, ForwardScanDirection, FreeQueryDesc(), GetActiveSnapshot(), GetIntoRelEFlags(), INSTR_TIME_GET_DOUBLE, INSTR_TIME_SET_CURRENT, INSTRUMENT_BUFFERS, INSTRUMENT_ROWS, INSTRUMENT_TIMER, InvalidSnapshot, NoMovementScanDirection, None_Receiver, NULL, PopActiveSnapshot(), PushCopiedSnapshot(), IntoClause::skipData, ExplainState::str, ExplainState::summary, ExplainState::timing, and UpdateActiveSnapshotCommandId().

Referenced by ExplainExecuteQuery(), and ExplainOneQuery().

464 {
465  DestReceiver *dest;
466  QueryDesc *queryDesc;
467  instr_time starttime;
468  double totaltime = 0;
469  int eflags;
470  int instrument_option = 0;
471 
472  Assert(plannedstmt->commandType != CMD_UTILITY);
473 
474  if (es->analyze && es->timing)
475  instrument_option |= INSTRUMENT_TIMER;
476  else if (es->analyze)
477  instrument_option |= INSTRUMENT_ROWS;
478 
479  if (es->buffers)
480  instrument_option |= INSTRUMENT_BUFFERS;
481 
482  /*
483  * We always collect timing for the entire statement, even when node-level
484  * timing is off, so we don't look at es->timing here. (We could skip
485  * this if !es->summary, but it's hardly worth the complication.)
486  */
487  INSTR_TIME_SET_CURRENT(starttime);
488 
489  /*
490  * Use a snapshot with an updated command ID to ensure this query sees
491  * results of any previously executed queries.
492  */
495 
496  /*
497  * Normally we discard the query's output, but if explaining CREATE TABLE
498  * AS, we'd better use the appropriate tuple receiver.
499  */
500  if (into)
501  dest = CreateIntoRelDestReceiver(into);
502  else
503  dest = None_Receiver;
504 
505  /* Create a QueryDesc for the query */
506  queryDesc = CreateQueryDesc(plannedstmt, queryString,
508  dest, params, instrument_option);
509 
510  /* Select execution options */
511  if (es->analyze)
512  eflags = 0; /* default run-to-completion flags */
513  else
514  eflags = EXEC_FLAG_EXPLAIN_ONLY;
515  if (into)
516  eflags |= GetIntoRelEFlags(into);
517 
518  /* call ExecutorStart to prepare the plan for execution */
519  ExecutorStart(queryDesc, eflags);
520 
521  /* Execute the plan for statistics if asked for */
522  if (es->analyze)
523  {
524  ScanDirection dir;
525 
526  /* EXPLAIN ANALYZE CREATE TABLE AS WITH NO DATA is weird */
527  if (into && into->skipData)
529  else
530  dir = ForwardScanDirection;
531 
532  /* run the plan */
533  ExecutorRun(queryDesc, dir, 0L, true);
534 
535  /* run cleanup too */
536  ExecutorFinish(queryDesc);
537 
538  /* We can't run ExecutorEnd 'till we're done printing the stats... */
539  totaltime += elapsed_time(&starttime);
540  }
541 
542  ExplainOpenGroup("Query", NULL, true, es);
543 
544  /* Create textual dump of plan tree */
545  ExplainPrintPlan(es, queryDesc);
546 
547  if (es->summary && planduration)
548  {
549  double plantime = INSTR_TIME_GET_DOUBLE(*planduration);
550 
551  if (es->format == EXPLAIN_FORMAT_TEXT)
552  appendStringInfo(es->str, "Planning time: %.3f ms\n",
553  1000.0 * plantime);
554  else
555  ExplainPropertyFloat("Planning Time", 1000.0 * plantime, 3, es);
556  }
557 
558  /* Print info about runtime of triggers */
559  if (es->analyze)
560  ExplainPrintTriggers(es, queryDesc);
561 
562  /*
563  * Close down the query and free resources. Include time for this in the
564  * total execution time (although it should be pretty minimal).
565  */
566  INSTR_TIME_SET_CURRENT(starttime);
567 
568  ExecutorEnd(queryDesc);
569 
570  FreeQueryDesc(queryDesc);
571 
573 
574  /* We need a CCI just in case query expanded to multiple plans */
575  if (es->analyze)
577 
578  totaltime += elapsed_time(&starttime);
579 
580  /*
581  * We only report execution time if we actually ran the query (that is,
582  * the user specified ANALYZE), and if summary reporting is enabled (the
583  * user can set SUMMARY OFF to not have the timing information included in
584  * the output). By default, ANALYZE sets SUMMARY to true.
585  */
586  if (es->summary && es->analyze)
587  {
588  if (es->format == EXPLAIN_FORMAT_TEXT)
589  appendStringInfo(es->str, "Execution time: %.3f ms\n",
590  1000.0 * totaltime);
591  else
592  ExplainPropertyFloat("Execution Time", 1000.0 * totaltime,
593  3, es);
594  }
595 
596  ExplainCloseGroup("Query", NULL, true, es);
597 }
bool summary
Definition: explain.h:37
void UpdateActiveSnapshotCommandId(void)
Definition: snapmgr.c:776
void FreeQueryDesc(QueryDesc *qdesc)
Definition: pquery.c:103
struct timeval instr_time
Definition: instr_time.h:147
void ExplainPropertyFloat(const char *qlabel, double value, int ndigits, ExplainState *es)
Definition: explain.c:3139
void ExecutorStart(QueryDesc *queryDesc, int eflags)
Definition: execMain.c:144
Snapshot GetActiveSnapshot(void)
Definition: snapmgr.c:834
void PopActiveSnapshot(void)
Definition: snapmgr.c:807
#define INSTR_TIME_GET_DOUBLE(t)
Definition: instr_time.h:196
QueryDesc * CreateQueryDesc(PlannedStmt *plannedstmt, const char *sourceText, Snapshot snapshot, Snapshot crosscheck_snapshot, DestReceiver *dest, ParamListInfo params, int instrument_options)
Definition: pquery.c:66
bool skipData
Definition: primnodes.h:115
DestReceiver * None_Receiver
Definition: dest.c:91
bool analyze
Definition: explain.h:33
static void ExplainOpenGroup(const char *objtype, const char *labelname, bool labeled, ExplainState *es)
Definition: explain.c:3167
void ExecutorEnd(QueryDesc *queryDesc)
Definition: execMain.c:453
int GetIntoRelEFlags(IntoClause *intoClause)
Definition: createas.c:391
void ExplainPrintTriggers(ExplainState *es, QueryDesc *queryDesc)
Definition: explain.c:648
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:110
void ExplainPrintPlan(ExplainState *es, QueryDesc *queryDesc)
Definition: explain.c:611
void ExecutorRun(QueryDesc *queryDesc, ScanDirection direction, uint64 count, bool execute_once)
Definition: execMain.c:291
void PushCopiedSnapshot(Snapshot snapshot)
Definition: snapmgr.c:764
ScanDirection
Definition: sdir.h:22
DestReceiver * CreateIntoRelDestReceiver(IntoClause *intoClause)
Definition: createas.c:422
static void ExplainCloseGroup(const char *objtype, const char *labelname, bool labeled, ExplainState *es)
Definition: explain.c:3230
void ExecutorFinish(QueryDesc *queryDesc)
Definition: execMain.c:393
bool timing
Definition: explain.h:36
#define InvalidSnapshot
Definition: snapshot.h:25
void CommandCounterIncrement(void)
Definition: xact.c:922
CmdType commandType
Definition: plannodes.h:45
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
ExplainFormat format
Definition: explain.h:38
static double elapsed_time(instr_time *starttime)
Definition: explain.c:763
#define INSTR_TIME_SET_CURRENT(t)
Definition: instr_time.h:153
bool buffers
Definition: explain.h:35
StringInfo str
Definition: explain.h:30
#define EXEC_FLAG_EXPLAIN_ONLY
Definition: executor.h:58
void ExplainOneUtility ( Node utilityStmt,
IntoClause into,
ExplainState es,
const char *  queryString,
ParamListInfo  params 
)

Definition at line 385 of file explain.c.

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

Referenced by ExplainExecuteQuery(), and ExplainOneQuery().

387 {
388  if (utilityStmt == NULL)
389  return;
390 
391  if (IsA(utilityStmt, CreateTableAsStmt))
392  {
393  /*
394  * We have to rewrite the contained SELECT and then pass it back to
395  * ExplainOneQuery. It's probably not really necessary to copy the
396  * contained parsetree another time, but let's be safe.
397  *
398  * Like ExecCreateTableAs, disallow parallelism in the plan.
399  */
400  CreateTableAsStmt *ctas = (CreateTableAsStmt *) utilityStmt;
401  List *rewritten;
402 
403  rewritten = QueryRewrite(castNode(Query, copyObject(ctas->query)));
404  Assert(list_length(rewritten) == 1);
405  ExplainOneQuery(castNode(Query, linitial(rewritten)),
406  0, ctas->into, es,
407  queryString, params);
408  }
409  else if (IsA(utilityStmt, DeclareCursorStmt))
410  {
411  /*
412  * Likewise for DECLARE CURSOR.
413  *
414  * Notice that if you say EXPLAIN ANALYZE DECLARE CURSOR then we'll
415  * actually run the query. This is different from pre-8.3 behavior
416  * but seems more useful than not running the query. No cursor will
417  * be created, however.
418  */
419  DeclareCursorStmt *dcs = (DeclareCursorStmt *) utilityStmt;
420  List *rewritten;
421 
422  rewritten = QueryRewrite(castNode(Query, copyObject(dcs->query)));
423  Assert(list_length(rewritten) == 1);
424  ExplainOneQuery(castNode(Query, linitial(rewritten)),
425  dcs->options, NULL, es,
426  queryString, params);
427  }
428  else if (IsA(utilityStmt, ExecuteStmt))
429  ExplainExecuteQuery((ExecuteStmt *) utilityStmt, into, es,
430  queryString, params);
431  else if (IsA(utilityStmt, NotifyStmt))
432  {
433  if (es->format == EXPLAIN_FORMAT_TEXT)
434  appendStringInfoString(es->str, "NOTIFY\n");
435  else
436  ExplainDummyGroup("Notify", NULL, es);
437  }
438  else
439  {
440  if (es->format == EXPLAIN_FORMAT_TEXT)
442  "Utility statements have no plan structure\n");
443  else
444  ExplainDummyGroup("Utility Statement", NULL, es);
445  }
446 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:570
List * QueryRewrite(Query *parsetree)
#define castNode(_type_, nodeptr)
Definition: nodes.h:588
static void ExplainDummyGroup(const char *objtype, const char *labelname, ExplainState *es)
Definition: explain.c:3266
void * copyObject(const void *from)
Definition: copyfuncs.c:4595
#define linitial(l)
Definition: pg_list.h:110
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:189
IntoClause * into
Definition: parsenodes.h:3055
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
static int list_length(const List *l)
Definition: pg_list.h:89
void ExplainExecuteQuery(ExecuteStmt *execstmt, IntoClause *into, ExplainState *es, const char *queryString, ParamListInfo params)
Definition: prepare.c:631
ExplainFormat format
Definition: explain.h:38
static void ExplainOneQuery(Query *query, int cursorOptions, IntoClause *into, ExplainState *es, const char *queryString, ParamListInfo params)
Definition: explain.c:339
Definition: pg_list.h:45
StringInfo str
Definition: explain.h:30
void ExplainPrintPlan ( ExplainState es,
QueryDesc queryDesc 
)

Definition at line 611 of file explain.c.

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

Referenced by explain_ExecutorEnd(), and ExplainOnePlan().

612 {
613  Bitmapset *rels_used = NULL;
614  PlanState *ps;
615 
616  /* Set up ExplainState fields associated with this plan tree */
617  Assert(queryDesc->plannedstmt != NULL);
618  es->pstmt = queryDesc->plannedstmt;
619  es->rtable = queryDesc->plannedstmt->rtable;
620  ExplainPreScanNode(queryDesc->planstate, &rels_used);
623  es->rtable_names);
624  es->printed_subplans = NULL;
625 
626  /*
627  * Sometimes we mark a Gather node as "invisible", which means that it's
628  * not displayed in EXPLAIN output. The purpose of this is to allow
629  * running regression tests with force_parallel_mode=regress to get the
630  * same results as running the same tests with force_parallel_mode=off.
631  */
632  ps = queryDesc->planstate;
633  if (IsA(ps, GatherState) &&((Gather *) ps->plan)->invisible)
634  ps = outerPlanState(ps);
635  ExplainNode(ps, NIL, NULL, NULL, es);
636 }
#define NIL
Definition: pg_list.h:69
#define IsA(nodeptr, _type_)
Definition: nodes.h:570
Bitmapset * printed_subplans
Definition: explain.h:47
List * deparse_cxt
Definition: explain.h:46
PlannedStmt * pstmt
Definition: explain.h:43
List * rtable_names
Definition: explain.h:45
List * select_rtable_names_for_explain(List *rtable, Bitmapset *rels_used)
Definition: ruleutils.c:2992
static void ExplainNode(PlanState *planstate, List *ancestors, const char *relationship, const char *plan_name, ExplainState *es)
Definition: explain.c:846
PlanState * planstate
Definition: execdesc.h:48
#define outerPlanState(node)
Definition: execnodes.h:1092
Plan * plan
Definition: execnodes.h:1049
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
List * rtable
Definition: plannodes.h:63
static bool ExplainPreScanNode(PlanState *planstate, Bitmapset **rels_used)
Definition: explain.c:782
PlannedStmt * plannedstmt
Definition: execdesc.h:37
List * deparse_context_for_plan_rtable(List *rtable, List *rtable_names)
Definition: ruleutils.c:2917
List * rtable
Definition: explain.h:44
void ExplainPrintTriggers ( ExplainState es,
QueryDesc queryDesc 
)

Definition at line 648 of file explain.c.

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

Referenced by explain_ExecutorEnd(), and ExplainOnePlan().

649 {
650  ResultRelInfo *rInfo;
651  bool show_relname;
652  int numrels = queryDesc->estate->es_num_result_relations;
653  List *targrels = queryDesc->estate->es_trig_target_relations;
654  int nr;
655  ListCell *l;
656 
657  ExplainOpenGroup("Triggers", "Triggers", false, es);
658 
659  show_relname = (numrels > 1 || targrels != NIL);
660  rInfo = queryDesc->estate->es_result_relations;
661  for (nr = 0; nr < numrels; rInfo++, nr++)
662  report_triggers(rInfo, show_relname, es);
663 
664  foreach(l, targrels)
665  {
666  rInfo = (ResultRelInfo *) lfirst(l);
667  report_triggers(rInfo, show_relname, es);
668  }
669 
670  ExplainCloseGroup("Triggers", "Triggers", false, es);
671 }
#define NIL
Definition: pg_list.h:69
EState * estate
Definition: execdesc.h:47
static void ExplainOpenGroup(const char *objtype, const char *labelname, bool labeled, ExplainState *es)
Definition: explain.c:3167
ResultRelInfo * es_result_relations
Definition: execnodes.h:384
static void ExplainCloseGroup(const char *objtype, const char *labelname, bool labeled, ExplainState *es)
Definition: explain.c:3230
static void report_triggers(ResultRelInfo *rInfo, bool show_relname, ExplainState *es)
Definition: explain.c:693
List * es_trig_target_relations
Definition: execnodes.h:389
int es_num_result_relations
Definition: execnodes.h:385
#define lfirst(lc)
Definition: pg_list.h:106
Definition: pg_list.h:45
void ExplainPropertyBool ( const char *  qlabel,
bool  value,
ExplainState es 
)

Definition at line 3152 of file explain.c.

References ExplainProperty().

Referenced by ExplainNode().

3153 {
3154  ExplainProperty(qlabel, value ? "true" : "false", true, es);
3155 }
static struct @77 value
static void ExplainProperty(const char *qlabel, const char *value, bool numeric, ExplainState *es)
Definition: explain.c:3055
void ExplainPropertyFloat ( const char *  qlabel,
double  value,
int  ndigits,
ExplainState es 
)

Definition at line 3139 of file explain.c.

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

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

3141 {
3142  char buf[256];
3143 
3144  snprintf(buf, sizeof(buf), "%.*f", ndigits, value);
3145  ExplainProperty(qlabel, buf, true, es);
3146 }
static struct @77 value
static void ExplainProperty(const char *qlabel, const char *value, bool numeric, ExplainState *es)
Definition: explain.c:3055
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
static char * buf
Definition: pg_test_fsync.c:65
void ExplainPropertyInteger ( const char *  qlabel,
int  value,
ExplainState es 
)

Definition at line 3114 of file explain.c.

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

Referenced by ExplainNode().

3115 {
3116  char buf[32];
3117 
3118  snprintf(buf, sizeof(buf), "%d", value);
3119  ExplainProperty(qlabel, buf, true, es);
3120 }
static struct @77 value
static void ExplainProperty(const char *qlabel, const char *value, bool numeric, ExplainState *es)
Definition: explain.c:3055
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
static char * buf
Definition: pg_test_fsync.c:65
void ExplainPropertyList ( const char *  qlabel,
List data,
ExplainState es 
)

Definition at line 2934 of file explain.c.

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

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

2935 {
2936  ListCell *lc;
2937  bool first = true;
2938 
2939  switch (es->format)
2940  {
2941  case EXPLAIN_FORMAT_TEXT:
2942  appendStringInfoSpaces(es->str, es->indent * 2);
2943  appendStringInfo(es->str, "%s: ", qlabel);
2944  foreach(lc, data)
2945  {
2946  if (!first)
2947  appendStringInfoString(es->str, ", ");
2948  appendStringInfoString(es->str, (const char *) lfirst(lc));
2949  first = false;
2950  }
2951  appendStringInfoChar(es->str, '\n');
2952  break;
2953 
2954  case EXPLAIN_FORMAT_XML:
2955  ExplainXMLTag(qlabel, X_OPENING, es);
2956  foreach(lc, data)
2957  {
2958  char *str;
2959 
2960  appendStringInfoSpaces(es->str, es->indent * 2 + 2);
2961  appendStringInfoString(es->str, "<Item>");
2962  str = escape_xml((const char *) lfirst(lc));
2963  appendStringInfoString(es->str, str);
2964  pfree(str);
2965  appendStringInfoString(es->str, "</Item>\n");
2966  }
2967  ExplainXMLTag(qlabel, X_CLOSING, es);
2968  break;
2969 
2970  case EXPLAIN_FORMAT_JSON:
2972  appendStringInfoSpaces(es->str, es->indent * 2);
2973  escape_json(es->str, qlabel);
2974  appendStringInfoString(es->str, ": [");
2975  foreach(lc, data)
2976  {
2977  if (!first)
2978  appendStringInfoString(es->str, ", ");
2979  escape_json(es->str, (const char *) lfirst(lc));
2980  first = false;
2981  }
2982  appendStringInfoChar(es->str, ']');
2983  break;
2984 
2985  case EXPLAIN_FORMAT_YAML:
2987  appendStringInfo(es->str, "%s: ", qlabel);
2988  foreach(lc, data)
2989  {
2990  appendStringInfoChar(es->str, '\n');
2991  appendStringInfoSpaces(es->str, es->indent * 2 + 2);
2992  appendStringInfoString(es->str, "- ");
2993  escape_yaml(es->str, (const char *) lfirst(lc));
2994  }
2995  break;
2996  }
2997 }
void escape_json(StringInfo buf, const char *str)
Definition: json.c:2433
static void ExplainXMLTag(const char *tagname, int flags, ExplainState *es)
Definition: explain.c:3401
#define X_CLOSING
Definition: explain.c:52
void pfree(void *pointer)
Definition: mcxt.c:950
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:110
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:189
static void ExplainYAMLLineStarting(ExplainState *es)
Definition: explain.c:3448
int indent
Definition: explain.h:40
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:201
static void escape_yaml(StringInfo buf, const char *str)
Definition: explain.c:3473
void appendStringInfoSpaces(StringInfo str, int count)
Definition: stringinfo.c:219
static void ExplainJSONLineEnding(ExplainState *es)
Definition: explain.c:3428
#define X_OPENING
Definition: explain.c:51
#define lfirst(lc)
Definition: pg_list.h:106
ExplainFormat format
Definition: explain.h:38
char * escape_xml(const char *str)
Definition: xml.c:2229
StringInfo str
Definition: explain.h:30
void ExplainPropertyListNested ( const char *  qlabel,
List data,
ExplainState es 
)

Definition at line 3004 of file explain.c.

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

Referenced by show_grouping_set_keys().

3005 {
3006  ListCell *lc;
3007  bool first = true;
3008 
3009  switch (es->format)
3010  {
3011  case EXPLAIN_FORMAT_TEXT:
3012  case EXPLAIN_FORMAT_XML:
3013  ExplainPropertyList(qlabel, data, es);
3014  return;
3015 
3016  case EXPLAIN_FORMAT_JSON:
3018  appendStringInfoSpaces(es->str, es->indent * 2);
3019  appendStringInfoChar(es->str, '[');
3020  foreach(lc, data)
3021  {
3022  if (!first)
3023  appendStringInfoString(es->str, ", ");
3024  escape_json(es->str, (const char *) lfirst(lc));
3025  first = false;
3026  }
3027  appendStringInfoChar(es->str, ']');
3028  break;
3029 
3030  case EXPLAIN_FORMAT_YAML:
3032  appendStringInfoString(es->str, "- [");
3033  foreach(lc, data)
3034  {
3035  if (!first)
3036  appendStringInfoString(es->str, ", ");
3037  escape_yaml(es->str, (const char *) lfirst(lc));
3038  first = false;
3039  }
3040  appendStringInfoChar(es->str, ']');
3041  break;
3042  }
3043 }
void escape_json(StringInfo buf, const char *str)
Definition: json.c:2433
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:189
static void ExplainYAMLLineStarting(ExplainState *es)
Definition: explain.c:3448
int indent
Definition: explain.h:40
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:201
static void escape_yaml(StringInfo buf, const char *str)
Definition: explain.c:3473
void appendStringInfoSpaces(StringInfo str, int count)
Definition: stringinfo.c:219
static void ExplainJSONLineEnding(ExplainState *es)
Definition: explain.c:3428
void ExplainPropertyList(const char *qlabel, List *data, ExplainState *es)
Definition: explain.c:2934
#define lfirst(lc)
Definition: pg_list.h:106
ExplainFormat format
Definition: explain.h:38
StringInfo str
Definition: explain.h:30
void ExplainPropertyLong ( const char *  qlabel,
long  value,
ExplainState es 
)

Definition at line 3126 of file explain.c.

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

Referenced by ExplainNode(), fileExplainForeignScan(), show_buffer_usage(), show_hash_info(), show_sort_info(), and show_tidbitmap_info().

3127 {
3128  char buf[32];
3129 
3130  snprintf(buf, sizeof(buf), "%ld", value);
3131  ExplainProperty(qlabel, buf, true, es);
3132 }
static struct @77 value
static void ExplainProperty(const char *qlabel, const char *value, bool numeric, ExplainState *es)
Definition: explain.c:3055
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
static char * buf
Definition: pg_test_fsync.c:65
void ExplainPropertyText ( const char *  qlabel,
const char *  value,
ExplainState es 
)

Definition at line 3105 of file explain.c.

References ExplainProperty().

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

3106 {
3107  ExplainProperty(qlabel, value, false, es);
3108 }
static struct @77 value
static void ExplainProperty(const char *qlabel, const char *value, bool numeric, ExplainState *es)
Definition: explain.c:3055
void ExplainQuery ( ParseState pstate,
ExplainStmt stmt,
const char *  queryString,
ParamListInfo  params,
DestReceiver dest 
)

Definition at line 144 of file explain.c.

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

Referenced by standard_ProcessUtility().

146 {
148  TupOutputState *tstate;
149  List *rewritten;
150  ListCell *lc;
151  bool timing_set = false;
152  bool summary_set = false;
153 
154  /* Parse options list. */
155  foreach(lc, stmt->options)
156  {
157  DefElem *opt = (DefElem *) lfirst(lc);
158 
159  if (strcmp(opt->defname, "analyze") == 0)
160  es->analyze = defGetBoolean(opt);
161  else if (strcmp(opt->defname, "verbose") == 0)
162  es->verbose = defGetBoolean(opt);
163  else if (strcmp(opt->defname, "costs") == 0)
164  es->costs = defGetBoolean(opt);
165  else if (strcmp(opt->defname, "buffers") == 0)
166  es->buffers = defGetBoolean(opt);
167  else if (strcmp(opt->defname, "timing") == 0)
168  {
169  timing_set = true;
170  es->timing = defGetBoolean(opt);
171  }
172  else if (strcmp(opt->defname, "summary") == 0)
173  {
174  summary_set = true;
175  es->summary = defGetBoolean(opt);
176  }
177  else if (strcmp(opt->defname, "format") == 0)
178  {
179  char *p = defGetString(opt);
180 
181  if (strcmp(p, "text") == 0)
183  else if (strcmp(p, "xml") == 0)
185  else if (strcmp(p, "json") == 0)
187  else if (strcmp(p, "yaml") == 0)
189  else
190  ereport(ERROR,
191  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
192  errmsg("unrecognized value for EXPLAIN option \"%s\": \"%s\"",
193  opt->defname, p),
194  parser_errposition(pstate, opt->location)));
195  }
196  else
197  ereport(ERROR,
198  (errcode(ERRCODE_SYNTAX_ERROR),
199  errmsg("unrecognized EXPLAIN option \"%s\"",
200  opt->defname),
201  parser_errposition(pstate, opt->location)));
202  }
203 
204  if (es->buffers && !es->analyze)
205  ereport(ERROR,
206  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
207  errmsg("EXPLAIN option BUFFERS requires ANALYZE")));
208 
209  /* if the timing was not set explicitly, set default value */
210  es->timing = (timing_set) ? es->timing : es->analyze;
211 
212  /* check that timing is used with EXPLAIN ANALYZE */
213  if (es->timing && !es->analyze)
214  ereport(ERROR,
215  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
216  errmsg("EXPLAIN option TIMING requires ANALYZE")));
217 
218  /* if the summary was not set explicitly, set default value */
219  es->summary = (summary_set) ? es->summary : es->analyze;
220 
221  /*
222  * Parse analysis was done already, but we still have to run the rule
223  * rewriter. We do not do AcquireRewriteLocks: we assume the query either
224  * came straight from the parser, or suitable locks were acquired by
225  * plancache.c.
226  *
227  * Because the rewriter and planner tend to scribble on the input, we make
228  * a preliminary copy of the source querytree. This prevents problems in
229  * the case that the EXPLAIN is in a portal or plpgsql function and is
230  * executed repeatedly. (See also the same hack in DECLARE CURSOR and
231  * PREPARE.) XXX FIXME someday.
232  */
233  rewritten = QueryRewrite(castNode(Query, copyObject(stmt->query)));
234 
235  /* emit opening boilerplate */
236  ExplainBeginOutput(es);
237 
238  if (rewritten == NIL)
239  {
240  /*
241  * In the case of an INSTEAD NOTHING, tell at least that. But in
242  * non-text format, the output is delimited, so this isn't necessary.
243  */
244  if (es->format == EXPLAIN_FORMAT_TEXT)
245  appendStringInfoString(es->str, "Query rewrites to nothing\n");
246  }
247  else
248  {
249  ListCell *l;
250 
251  /* Explain every plan */
252  foreach(l, rewritten)
253  {
256  queryString, params);
257 
258  /* Separate plans with an appropriate separator */
259  if (lnext(l) != NULL)
261  }
262  }
263 
264  /* emit closing boilerplate */
265  ExplainEndOutput(es);
266  Assert(es->indent == 0);
267 
268  /* output tuples */
269  tstate = begin_tup_output_tupdesc(dest, ExplainResultDesc(stmt));
270  if (es->format == EXPLAIN_FORMAT_TEXT)
271  do_text_output_multiline(tstate, es->str->data);
272  else
273  do_text_output_oneline(tstate, es->str->data);
274  end_tup_output(tstate);
275 
276  pfree(es->str->data);
277 }
#define NIL
Definition: pg_list.h:69
bool summary
Definition: explain.h:37
ExplainState * NewExplainState(void)
Definition: explain.c:283
List * QueryRewrite(Query *parsetree)
void ExplainSeparatePlans(ExplainState *es)
Definition: explain.c:3372
#define castNode(_type_, nodeptr)
Definition: nodes.h:588
int errcode(int sqlerrcode)
Definition: elog.c:575
List * options
Definition: parsenodes.h:3035
bool costs
Definition: explain.h:34
bool analyze
Definition: explain.h:33
void * copyObject(const void *from)
Definition: copyfuncs.c:4595
Node * query
Definition: parsenodes.h:3034
void ExplainEndOutput(ExplainState *es)
Definition: explain.c:3343
bool defGetBoolean(DefElem *def)
Definition: define.c:111
void pfree(void *pointer)
Definition: mcxt.c:950
void end_tup_output(TupOutputState *tstate)
Definition: execTuples.c:1308
void ExplainBeginOutput(ExplainState *es)
Definition: explain.c:3312
#define ERROR
Definition: elog.h:43
TupleDesc ExplainResultDesc(ExplainStmt *stmt)
Definition: explain.c:300
char * defGetString(DefElem *def)
Definition: define.c:49
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:189
int location
Definition: parsenodes.h:711
int indent
Definition: explain.h:40
#define lnext(lc)
Definition: pg_list.h:105
#define ereport(elevel, rest)
Definition: elog.h:122
bool timing
Definition: explain.h:36
TupOutputState * begin_tup_output_tupdesc(DestReceiver *dest, TupleDesc tupdesc)
Definition: execTuples.c:1232
void do_text_output_multiline(TupOutputState *tstate, const char *txt)
Definition: execTuples.c:1278
bool verbose
Definition: explain.h:32
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
#define lfirst(lc)
Definition: pg_list.h:106
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:109
ExplainFormat format
Definition: explain.h:38
#define do_text_output_oneline(tstate, str_to_emit)
Definition: executor.h:312
static void ExplainOneQuery(Query *query, int cursorOptions, IntoClause *into, ExplainState *es, const char *queryString, ParamListInfo params)
Definition: explain.c:339
#define CURSOR_OPT_PARALLEL_OK
Definition: parsenodes.h:2567
int errmsg(const char *fmt,...)
Definition: elog.c:797
bool buffers
Definition: explain.h:35
char * defname
Definition: parsenodes.h:708
Definition: pg_list.h:45
StringInfo str
Definition: explain.h:30
void ExplainQueryText ( ExplainState es,
QueryDesc queryDesc 
)

Definition at line 682 of file explain.c.

References ExplainPropertyText(), and QueryDesc::sourceText.

Referenced by explain_ExecutorEnd().

683 {
684  if (queryDesc->sourceText)
685  ExplainPropertyText("Query Text", queryDesc->sourceText, es);
686 }
void ExplainPropertyText(const char *qlabel, const char *value, ExplainState *es)
Definition: explain.c:3105
const char * sourceText
Definition: execdesc.h:38
TupleDesc ExplainResultDesc ( ExplainStmt stmt)

Definition at line 300 of file explain.c.

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

Referenced by ExplainQuery(), and UtilityTupleDescriptor().

301 {
302  TupleDesc tupdesc;
303  ListCell *lc;
304  Oid result_type = TEXTOID;
305 
306  /* Check for XML format option */
307  foreach(lc, stmt->options)
308  {
309  DefElem *opt = (DefElem *) lfirst(lc);
310 
311  if (strcmp(opt->defname, "format") == 0)
312  {
313  char *p = defGetString(opt);
314 
315  if (strcmp(p, "xml") == 0)
316  result_type = XMLOID;
317  else if (strcmp(p, "json") == 0)
318  result_type = JSONOID;
319  else
320  result_type = TEXTOID;
321  /* don't "break", as ExplainQuery will use the last value */
322  }
323  }
324 
325  /* Need a tuple descriptor representing a single TEXT or XML column */
326  tupdesc = CreateTemplateTupleDesc(1, false);
327  TupleDescInitEntry(tupdesc, (AttrNumber) 1, "QUERY PLAN",
328  result_type, -1, 0);
329  return tupdesc;
330 }
#define TEXTOID
Definition: pg_type.h:324
#define JSONOID
Definition: pg_type.h:356
List * options
Definition: parsenodes.h:3035
unsigned int Oid
Definition: postgres_ext.h:31
char * defGetString(DefElem *def)
Definition: define.c:49
#define XMLOID
Definition: pg_type.h:359
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition: tupdesc.c:493
#define lfirst(lc)
Definition: pg_list.h:106
TupleDesc CreateTemplateTupleDesc(int natts, bool hasoid)
Definition: tupdesc.c:41
char * defname
Definition: parsenodes.h:708
int16 AttrNumber
Definition: attnum.h:21
void ExplainSeparatePlans ( ExplainState es)

Definition at line 3372 of file explain.c.

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

Referenced by ExplainExecuteQuery(), and ExplainQuery().

3373 {
3374  switch (es->format)
3375  {
3376  case EXPLAIN_FORMAT_TEXT:
3377  /* add a blank line */
3378  appendStringInfoChar(es->str, '\n');
3379  break;
3380 
3381  case EXPLAIN_FORMAT_XML:
3382  case EXPLAIN_FORMAT_JSON:
3383  case EXPLAIN_FORMAT_YAML:
3384  /* nothing to do */
3385  break;
3386  }
3387 }
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:201
ExplainFormat format
Definition: explain.h:38
StringInfo str
Definition: explain.h:30
ExplainState* NewExplainState ( void  )

Definition at line 283 of file explain.c.

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

Referenced by explain_ExecutorEnd(), and ExplainQuery().

284 {
285  ExplainState *es = (ExplainState *) palloc0(sizeof(ExplainState));
286 
287  /* Set default options (most fields can be left as zeroes). */
288  es->costs = true;
289  /* Prepare output buffer. */
290  es->str = makeStringInfo();
291 
292  return es;
293 }
StringInfo makeStringInfo(void)
Definition: stringinfo.c:29
bool costs
Definition: explain.h:34
void * palloc0(Size size)
Definition: mcxt.c:878
StringInfo str
Definition: explain.h:30

Variable Documentation

PGDLLIMPORT explain_get_index_name_hook_type explain_get_index_name_hook

Definition at line 47 of file explain.c.

Referenced by explain_get_index_name().

PGDLLIMPORT ExplainOneQuery_hook_type ExplainOneQuery_hook

Definition at line 44 of file explain.c.

Referenced by ExplainOneQuery().