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, QueryEnvironment *queryEnv, 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)
 
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 3362 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().

3363 {
3364  switch (es->format)
3365  {
3366  case EXPLAIN_FORMAT_TEXT:
3367  /* nothing to do */
3368  break;
3369 
3370  case EXPLAIN_FORMAT_XML:
3372  "<explain xmlns=\"http://www.postgresql.org/2009/explain\">\n");
3373  es->indent++;
3374  break;
3375 
3376  case EXPLAIN_FORMAT_JSON:
3377  /* top-level structure is an array of plans */
3378  appendStringInfoChar(es->str, '[');
3379  es->grouping_stack = lcons_int(0, es->grouping_stack);
3380  es->indent++;
3381  break;
3382 
3383  case EXPLAIN_FORMAT_YAML:
3384  es->grouping_stack = lcons_int(0, es->grouping_stack);
3385  break;
3386  }
3387 }
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 3393 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().

3394 {
3395  switch (es->format)
3396  {
3397  case EXPLAIN_FORMAT_TEXT:
3398  /* nothing to do */
3399  break;
3400 
3401  case EXPLAIN_FORMAT_XML:
3402  es->indent--;
3403  appendStringInfoString(es->str, "</explain>");
3404  break;
3405 
3406  case EXPLAIN_FORMAT_JSON:
3407  es->indent--;
3408  appendStringInfoString(es->str, "\n]");
3410  break;
3411 
3412  case EXPLAIN_FORMAT_YAML:
3414  break;
3415  }
3416 }
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,
QueryEnvironment queryEnv,
const instr_time planduration 
)

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

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

Definition at line 390 of file explain.c.

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

Referenced by ExplainExecuteQuery(), and ExplainOneQuery().

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

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

618 {
619  Bitmapset *rels_used = NULL;
620  PlanState *ps;
621 
622  /* Set up ExplainState fields associated with this plan tree */
623  Assert(queryDesc->plannedstmt != NULL);
624  es->pstmt = queryDesc->plannedstmt;
625  es->rtable = queryDesc->plannedstmt->rtable;
626  ExplainPreScanNode(queryDesc->planstate, &rels_used);
629  es->rtable_names);
630  es->printed_subplans = NULL;
631 
632  /*
633  * Sometimes we mark a Gather node as "invisible", which means that it's
634  * not displayed in EXPLAIN output. The purpose of this is to allow
635  * running regression tests with force_parallel_mode=regress to get the
636  * same results as running the same tests with force_parallel_mode=off.
637  */
638  ps = queryDesc->planstate;
639  if (IsA(ps, GatherState) &&((Gather *) ps->plan)->invisible)
640  ps = outerPlanState(ps);
641  ExplainNode(ps, NIL, NULL, NULL, es);
642 }
#define NIL
Definition: pg_list.h:69
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
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:3115
static void ExplainNode(PlanState *planstate, List *ancestors, const char *relationship, const char *plan_name, ExplainState *es)
Definition: explain.c:853
PlanState * planstate
Definition: execdesc.h:49
#define outerPlanState(node)
Definition: execnodes.h:845
Plan * plan
Definition: execnodes.h:803
#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:788
PlannedStmt * plannedstmt
Definition: execdesc.h:37
List * deparse_context_for_plan_rtable(List *rtable, List *rtable_names)
Definition: ruleutils.c:3040
List * rtable
Definition: explain.h:44
void ExplainPrintTriggers ( ExplainState es,
QueryDesc queryDesc 
)

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

655 {
656  ResultRelInfo *rInfo;
657  bool show_relname;
658  int numrels = queryDesc->estate->es_num_result_relations;
659  List *targrels = queryDesc->estate->es_trig_target_relations;
660  int nr;
661  ListCell *l;
662 
663  ExplainOpenGroup("Triggers", "Triggers", false, es);
664 
665  show_relname = (numrels > 1 || targrels != NIL);
666  rInfo = queryDesc->estate->es_result_relations;
667  for (nr = 0; nr < numrels; rInfo++, nr++)
668  report_triggers(rInfo, show_relname, es);
669 
670  foreach(l, targrels)
671  {
672  rInfo = (ResultRelInfo *) lfirst(l);
673  report_triggers(rInfo, show_relname, es);
674  }
675 
676  ExplainCloseGroup("Triggers", "Triggers", false, es);
677 }
#define NIL
Definition: pg_list.h:69
EState * estate
Definition: execdesc.h:48
static void ExplainOpenGroup(const char *objtype, const char *labelname, bool labeled, ExplainState *es)
Definition: explain.c:3217
ResultRelInfo * es_result_relations
Definition: execnodes.h:421
static void ExplainCloseGroup(const char *objtype, const char *labelname, bool labeled, ExplainState *es)
Definition: explain.c:3280
static void report_triggers(ResultRelInfo *rInfo, bool show_relname, ExplainState *es)
Definition: explain.c:699
List * es_trig_target_relations
Definition: execnodes.h:426
int es_num_result_relations
Definition: execnodes.h:422
#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 3202 of file explain.c.

References ExplainProperty().

Referenced by ExplainNode().

3203 {
3204  ExplainProperty(qlabel, value ? "true" : "false", true, es);
3205 }
static void ExplainProperty(const char *qlabel, const char *value, bool numeric, ExplainState *es)
Definition: explain.c:3105
static struct @121 value
void ExplainPropertyFloat ( const char *  qlabel,
double  value,
int  ndigits,
ExplainState es 
)

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

3191 {
3192  char buf[256];
3193 
3194  snprintf(buf, sizeof(buf), "%.*f", ndigits, value);
3195  ExplainProperty(qlabel, buf, true, es);
3196 }
static void ExplainProperty(const char *qlabel, const char *value, bool numeric, ExplainState *es)
Definition: explain.c:3105
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
static char * buf
Definition: pg_test_fsync.c:66
static struct @121 value
void ExplainPropertyInteger ( const char *  qlabel,
int  value,
ExplainState es 
)

Definition at line 3164 of file explain.c.

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

Referenced by ExplainNode().

3165 {
3166  char buf[32];
3167 
3168  snprintf(buf, sizeof(buf), "%d", value);
3169  ExplainProperty(qlabel, buf, true, es);
3170 }
static void ExplainProperty(const char *qlabel, const char *value, bool numeric, ExplainState *es)
Definition: explain.c:3105
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
static char * buf
Definition: pg_test_fsync.c:66
static struct @121 value
void ExplainPropertyList ( const char *  qlabel,
List data,
ExplainState es 
)

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

2985 {
2986  ListCell *lc;
2987  bool first = true;
2988 
2989  switch (es->format)
2990  {
2991  case EXPLAIN_FORMAT_TEXT:
2992  appendStringInfoSpaces(es->str, es->indent * 2);
2993  appendStringInfo(es->str, "%s: ", qlabel);
2994  foreach(lc, data)
2995  {
2996  if (!first)
2997  appendStringInfoString(es->str, ", ");
2998  appendStringInfoString(es->str, (const char *) lfirst(lc));
2999  first = false;
3000  }
3001  appendStringInfoChar(es->str, '\n');
3002  break;
3003 
3004  case EXPLAIN_FORMAT_XML:
3005  ExplainXMLTag(qlabel, X_OPENING, es);
3006  foreach(lc, data)
3007  {
3008  char *str;
3009 
3010  appendStringInfoSpaces(es->str, es->indent * 2 + 2);
3011  appendStringInfoString(es->str, "<Item>");
3012  str = escape_xml((const char *) lfirst(lc));
3013  appendStringInfoString(es->str, str);
3014  pfree(str);
3015  appendStringInfoString(es->str, "</Item>\n");
3016  }
3017  ExplainXMLTag(qlabel, X_CLOSING, es);
3018  break;
3019 
3020  case EXPLAIN_FORMAT_JSON:
3022  appendStringInfoSpaces(es->str, es->indent * 2);
3023  escape_json(es->str, qlabel);
3024  appendStringInfoString(es->str, ": [");
3025  foreach(lc, data)
3026  {
3027  if (!first)
3028  appendStringInfoString(es->str, ", ");
3029  escape_json(es->str, (const char *) lfirst(lc));
3030  first = false;
3031  }
3032  appendStringInfoChar(es->str, ']');
3033  break;
3034 
3035  case EXPLAIN_FORMAT_YAML:
3037  appendStringInfo(es->str, "%s: ", qlabel);
3038  foreach(lc, data)
3039  {
3040  appendStringInfoChar(es->str, '\n');
3041  appendStringInfoSpaces(es->str, es->indent * 2 + 2);
3042  appendStringInfoString(es->str, "- ");
3043  escape_yaml(es->str, (const char *) lfirst(lc));
3044  }
3045  break;
3046  }
3047 }
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:3451
#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:3498
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:3523
void appendStringInfoSpaces(StringInfo str, int count)
Definition: stringinfo.c:219
static void ExplainJSONLineEnding(ExplainState *es)
Definition: explain.c:3478
#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:2228
StringInfo str
Definition: explain.h:30
void ExplainPropertyListNested ( const char *  qlabel,
List data,
ExplainState es 
)

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

3055 {
3056  ListCell *lc;
3057  bool first = true;
3058 
3059  switch (es->format)
3060  {
3061  case EXPLAIN_FORMAT_TEXT:
3062  case EXPLAIN_FORMAT_XML:
3063  ExplainPropertyList(qlabel, data, es);
3064  return;
3065 
3066  case EXPLAIN_FORMAT_JSON:
3068  appendStringInfoSpaces(es->str, es->indent * 2);
3069  appendStringInfoChar(es->str, '[');
3070  foreach(lc, data)
3071  {
3072  if (!first)
3073  appendStringInfoString(es->str, ", ");
3074  escape_json(es->str, (const char *) lfirst(lc));
3075  first = false;
3076  }
3077  appendStringInfoChar(es->str, ']');
3078  break;
3079 
3080  case EXPLAIN_FORMAT_YAML:
3082  appendStringInfoString(es->str, "- [");
3083  foreach(lc, data)
3084  {
3085  if (!first)
3086  appendStringInfoString(es->str, ", ");
3087  escape_yaml(es->str, (const char *) lfirst(lc));
3088  first = false;
3089  }
3090  appendStringInfoChar(es->str, ']');
3091  break;
3092  }
3093 }
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:3498
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:3523
void appendStringInfoSpaces(StringInfo str, int count)
Definition: stringinfo.c:219
static void ExplainJSONLineEnding(ExplainState *es)
Definition: explain.c:3478
void ExplainPropertyList(const char *qlabel, List *data, ExplainState *es)
Definition: explain.c:2984
#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 3176 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().

3177 {
3178  char buf[32];
3179 
3180  snprintf(buf, sizeof(buf), "%ld", value);
3181  ExplainProperty(qlabel, buf, true, es);
3182 }
static void ExplainProperty(const char *qlabel, const char *value, bool numeric, ExplainState *es)
Definition: explain.c:3105
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
static char * buf
Definition: pg_test_fsync.c:66
static struct @121 value
void ExplainPropertyText ( const char *  qlabel,
const char *  value,
ExplainState es 
)

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

3156 {
3157  ExplainProperty(qlabel, value, false, es);
3158 }
static void ExplainProperty(const char *qlabel, const char *value, bool numeric, ExplainState *es)
Definition: explain.c:3105
static struct @121 value
void ExplainQuery ( ParseState pstate,
ExplainStmt stmt,
const char *  queryString,
ParamListInfo  params,
QueryEnvironment queryEnv,
DestReceiver dest 
)

Definition at line 145 of file explain.c.

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

Referenced by standard_ProcessUtility().

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

Definition at line 688 of file explain.c.

References ExplainPropertyText(), and QueryDesc::sourceText.

Referenced by explain_ExecutorEnd().

689 {
690  if (queryDesc->sourceText)
691  ExplainPropertyText("Query Text", queryDesc->sourceText, es);
692 }
void ExplainPropertyText(const char *qlabel, const char *value, ExplainState *es)
Definition: explain.c:3155
const char * sourceText
Definition: execdesc.h:38
TupleDesc ExplainResultDesc ( ExplainStmt stmt)

Definition at line 302 of file explain.c.

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

Referenced by ExplainQuery(), and UtilityTupleDescriptor().

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

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

3423 {
3424  switch (es->format)
3425  {
3426  case EXPLAIN_FORMAT_TEXT:
3427  /* add a blank line */
3428  appendStringInfoChar(es->str, '\n');
3429  break;
3430 
3431  case EXPLAIN_FORMAT_XML:
3432  case EXPLAIN_FORMAT_JSON:
3433  case EXPLAIN_FORMAT_YAML:
3434  /* nothing to do */
3435  break;
3436  }
3437 }
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 285 of file explain.c.

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

Referenced by explain_ExecutorEnd(), and ExplainQuery().

286 {
287  ExplainState *es = (ExplainState *) palloc0(sizeof(ExplainState));
288 
289  /* Set default options (most fields can be left as zeroes). */
290  es->costs = true;
291  /* Prepare output buffer. */
292  es->str = makeStringInfo();
293 
294  return es;
295 }
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().