PostgreSQL Source Code  git master
explain.c File Reference
#include "postgres.h"
#include "access/xact.h"
#include "catalog/pg_type.h"
#include "commands/createas.h"
#include "commands/defrem.h"
#include "commands/prepare.h"
#include "executor/nodeHash.h"
#include "foreign/fdwapi.h"
#include "jit/jit.h"
#include "nodes/extensible.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "parser/analyze.h"
#include "parser/parsetree.h"
#include "rewrite/rewriteHandler.h"
#include "storage/bufmgr.h"
#include "tcop/tcopprot.h"
#include "utils/builtins.h"
#include "utils/guc_tables.h"
#include "utils/json.h"
#include "utils/lsyscache.h"
#include "utils/rel.h"
#include "utils/ruleutils.h"
#include "utils/snapmgr.h"
#include "utils/tuplesort.h"
#include "utils/typcache.h"
#include "utils/xml.h"
Include dependency graph for explain.c:

Go to the source code of this file.

Macros

#define X_OPENING   0
 
#define X_CLOSING   1
 
#define X_CLOSE_IMMEDIATE   2
 
#define X_NOWHITESPACE   4
 

Functions

static void ExplainOneQuery (Query *query, int cursorOptions, IntoClause *into, ExplainState *es, const char *queryString, ParamListInfo params, QueryEnvironment *queryEnv)
 
static void ExplainPrintJIT (ExplainState *es, int jit_flags, JitInstrumentation *ji)
 
static void report_triggers (ResultRelInfo *rInfo, bool show_relname, ExplainState *es)
 
static double elapsed_time (instr_time *starttime)
 
static bool ExplainPreScanNode (PlanState *planstate, Bitmapset **rels_used)
 
static void ExplainNode (PlanState *planstate, List *ancestors, const char *relationship, const char *plan_name, ExplainState *es)
 
static void show_plan_tlist (PlanState *planstate, List *ancestors, ExplainState *es)
 
static void show_expression (Node *node, const char *qlabel, PlanState *planstate, List *ancestors, bool useprefix, ExplainState *es)
 
static void show_qual (List *qual, const char *qlabel, PlanState *planstate, List *ancestors, bool useprefix, ExplainState *es)
 
static void show_scan_qual (List *qual, const char *qlabel, PlanState *planstate, List *ancestors, ExplainState *es)
 
static void show_upper_qual (List *qual, const char *qlabel, PlanState *planstate, List *ancestors, ExplainState *es)
 
static void show_sort_keys (SortState *sortstate, List *ancestors, ExplainState *es)
 
static void show_incremental_sort_keys (IncrementalSortState *incrsortstate, List *ancestors, ExplainState *es)
 
static void show_merge_append_keys (MergeAppendState *mstate, List *ancestors, ExplainState *es)
 
static void show_agg_keys (AggState *astate, List *ancestors, ExplainState *es)
 
static void show_grouping_sets (PlanState *planstate, Agg *agg, List *ancestors, ExplainState *es)
 
static void show_grouping_set_keys (PlanState *planstate, Agg *aggnode, Sort *sortnode, List *context, bool useprefix, List *ancestors, ExplainState *es)
 
static void show_group_keys (GroupState *gstate, List *ancestors, ExplainState *es)
 
static void show_sort_group_keys (PlanState *planstate, const char *qlabel, int nkeys, int nPresortedKeys, AttrNumber *keycols, Oid *sortOperators, Oid *collations, bool *nullsFirst, List *ancestors, ExplainState *es)
 
static void show_sortorder_options (StringInfo buf, Node *sortexpr, Oid sortOperator, Oid collation, bool nullsFirst)
 
static void show_tablesample (TableSampleClause *tsc, PlanState *planstate, List *ancestors, ExplainState *es)
 
static void show_sort_info (SortState *sortstate, ExplainState *es)
 
static void show_incremental_sort_info (IncrementalSortState *incrsortstate, ExplainState *es)
 
static void show_hash_info (HashState *hashstate, ExplainState *es)
 
static void show_resultcache_info (ResultCacheState *rcstate, List *ancestors, ExplainState *es)
 
static void show_hashagg_info (AggState *hashstate, ExplainState *es)
 
static void show_tidbitmap_info (BitmapHeapScanState *planstate, ExplainState *es)
 
static void show_instrumentation_count (const char *qlabel, int which, PlanState *planstate, ExplainState *es)
 
static void show_foreignscan_info (ForeignScanState *fsstate, ExplainState *es)
 
static void show_eval_params (Bitmapset *bms_params, ExplainState *es)
 
static const char * explain_get_index_name (Oid indexId)
 
static void show_buffer_usage (ExplainState *es, const BufferUsage *usage, bool planning)
 
static void show_wal_usage (ExplainState *es, const WalUsage *usage)
 
static void ExplainIndexScanDetails (Oid indexid, ScanDirection indexorderdir, ExplainState *es)
 
static void ExplainScanTarget (Scan *plan, ExplainState *es)
 
static void ExplainModifyTarget (ModifyTable *plan, ExplainState *es)
 
static void ExplainTargetRel (Plan *plan, Index rti, ExplainState *es)
 
static void show_modifytable_info (ModifyTableState *mtstate, List *ancestors, ExplainState *es)
 
static void ExplainMemberNodes (PlanState **planstates, int nplans, List *ancestors, ExplainState *es)
 
static void ExplainMissingMembers (int nplans, int nchildren, ExplainState *es)
 
static void ExplainSubPlans (List *plans, List *ancestors, const char *relationship, ExplainState *es)
 
static void ExplainCustomChildren (CustomScanState *css, List *ancestors, ExplainState *es)
 
static ExplainWorkersStateExplainCreateWorkersState (int num_workers)
 
static void ExplainOpenWorker (int n, ExplainState *es)
 
static void ExplainCloseWorker (int n, ExplainState *es)
 
static void ExplainFlushWorkersState (ExplainState *es)
 
static void ExplainProperty (const char *qlabel, const char *unit, const char *value, bool numeric, ExplainState *es)
 
static void ExplainOpenSetAsideGroup (const char *objtype, const char *labelname, bool labeled, int depth, ExplainState *es)
 
static void ExplainSaveGroup (ExplainState *es, int depth, int *state_save)
 
static void ExplainRestoreGroup (ExplainState *es, int depth, int *state_save)
 
static void ExplainDummyGroup (const char *objtype, const char *labelname, ExplainState *es)
 
static void ExplainXMLTag (const char *tagname, int flags, ExplainState *es)
 
static void ExplainIndentText (ExplainState *es)
 
static void ExplainJSONLineEnding (ExplainState *es)
 
static void ExplainYAMLLineStarting (ExplainState *es)
 
static void escape_yaml (StringInfo buf, const char *str)
 
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)
 
static void ExplainPrintSettings (ExplainState *es)
 
void ExplainPrintPlan (ExplainState *es, QueryDesc *queryDesc)
 
void ExplainPrintTriggers (ExplainState *es, QueryDesc *queryDesc)
 
void ExplainPrintJITSummary (ExplainState *es, QueryDesc *queryDesc)
 
void ExplainQueryText (ExplainState *es, QueryDesc *queryDesc)
 
static void show_incremental_sort_group_info (IncrementalSortGroupInfo *groupInfo, const char *groupLabel, bool indent, 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)
 
void ExplainBeginOutput (ExplainState *es)
 
void ExplainEndOutput (ExplainState *es)
 
void ExplainSeparatePlans (ExplainState *es)
 

Variables

ExplainOneQuery_hook_type ExplainOneQuery_hook = NULL
 
explain_get_index_name_hook_type explain_get_index_name_hook = NULL
 

Macro Definition Documentation

◆ X_CLOSE_IMMEDIATE

#define X_CLOSE_IMMEDIATE   2

Definition at line 54 of file explain.c.

Referenced by ExplainDummyGroup(), and ExplainXMLTag().

◆ X_CLOSING

#define X_CLOSING   1

Definition at line 53 of file explain.c.

Referenced by ExplainCloseGroup(), ExplainProperty(), ExplainPropertyList(), and ExplainXMLTag().

◆ X_NOWHITESPACE

#define X_NOWHITESPACE   4

Definition at line 55 of file explain.c.

Referenced by ExplainProperty(), and ExplainXMLTag().

◆ X_OPENING

#define X_OPENING   0

Definition at line 52 of file explain.c.

Referenced by ExplainOpenGroup(), ExplainProperty(), and ExplainPropertyList().

Function Documentation

◆ elapsed_time()

static double elapsed_time ( instr_time starttime)
static

Definition at line 1049 of file explain.c.

References INSTR_TIME_GET_DOUBLE, INSTR_TIME_SET_CURRENT, and INSTR_TIME_SUBTRACT.

Referenced by ExplainOnePlan(), and IsCheckpointOnSchedule().

1050 {
1051  instr_time endtime;
1052 
1053  INSTR_TIME_SET_CURRENT(endtime);
1054  INSTR_TIME_SUBTRACT(endtime, *starttime);
1055  return INSTR_TIME_GET_DOUBLE(endtime);
1056 }
struct timeval instr_time
Definition: instr_time.h:150
#define INSTR_TIME_GET_DOUBLE(t)
Definition: instr_time.h:199
#define INSTR_TIME_SUBTRACT(x, y)
Definition: instr_time.h:170
#define INSTR_TIME_SET_CURRENT(t)
Definition: instr_time.h:156

◆ escape_yaml()

static void escape_yaml ( StringInfo  buf,
const char *  str 
)
static

Definition at line 4920 of file explain.c.

References escape_json().

Referenced by ExplainDummyGroup(), ExplainProperty(), ExplainPropertyList(), and ExplainPropertyListNested().

4921 {
4922  escape_json(buf, str);
4923 }
void escape_json(StringInfo buf, const char *str)
Definition: json.c:1279

◆ explain_get_index_name()

static const char * explain_get_index_name ( Oid  indexId)
static

Definition at line 3464 of file explain.c.

References elog, ERROR, explain_get_index_name_hook, and get_rel_name().

Referenced by ExplainIndexScanDetails(), and ExplainNode().

3465 {
3466  const char *result;
3467 
3469  result = (*explain_get_index_name_hook) (indexId);
3470  else
3471  result = NULL;
3472  if (result == NULL)
3473  {
3474  /* default behavior: look it up in the catalogs */
3475  result = get_rel_name(indexId);
3476  if (result == NULL)
3477  elog(ERROR, "cache lookup failed for index %u", indexId);
3478  }
3479  return result;
3480 }
#define ERROR
Definition: elog.h:46
explain_get_index_name_hook_type explain_get_index_name_hook
Definition: explain.c:48
#define elog(elevel,...)
Definition: elog.h:232
char * get_rel_name(Oid relid)
Definition: lsyscache.c:1899

◆ ExplainBeginOutput()

void ExplainBeginOutput ( ExplainState es)

Definition at line 4744 of file explain.c.

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

Referenced by explain_ExecutorEnd(), and ExplainQuery().

4745 {
4746  switch (es->format)
4747  {
4748  case EXPLAIN_FORMAT_TEXT:
4749  /* nothing to do */
4750  break;
4751 
4752  case EXPLAIN_FORMAT_XML:
4754  "<explain xmlns=\"http://www.postgresql.org/2009/explain\">\n");
4755  es->indent++;
4756  break;
4757 
4758  case EXPLAIN_FORMAT_JSON:
4759  /* top-level structure is an array of plans */
4760  appendStringInfoChar(es->str, '[');
4761  es->grouping_stack = lcons_int(0, es->grouping_stack);
4762  es->indent++;
4763  break;
4764 
4765  case EXPLAIN_FORMAT_YAML:
4766  es->grouping_stack = lcons_int(0, es->grouping_stack);
4767  break;
4768  }
4769 }
List * lcons_int(int datum, List *list)
Definition: list.c:486
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
List * grouping_stack
Definition: explain.h:52
int indent
Definition: explain.h:51
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:188
ExplainFormat format
Definition: explain.h:49
StringInfo str
Definition: explain.h:39

◆ ExplainCloseGroup()

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

Definition at line 4551 of file explain.c.

References appendStringInfoChar(), appendStringInfoSpaces(), EXPLAIN_FORMAT_JSON, EXPLAIN_FORMAT_TEXT, EXPLAIN_FORMAT_XML, EXPLAIN_FORMAT_YAML, ExplainXMLTag(), ExplainState::format, ExplainState::grouping_stack, ExplainState::indent, list_delete_first(), ExplainState::str, and X_CLOSING.

Referenced by ExplainFlushWorkersState(), ExplainNode(), ExplainOnePlan(), ExplainPrintJIT(), ExplainPrintSettings(), ExplainPrintTriggers(), report_triggers(), show_grouping_set_keys(), show_grouping_sets(), show_incremental_sort_group_info(), and show_modifytable_info().

4553 {
4554  switch (es->format)
4555  {
4556  case EXPLAIN_FORMAT_TEXT:
4557  /* nothing to do */
4558  break;
4559 
4560  case EXPLAIN_FORMAT_XML:
4561  es->indent--;
4562  ExplainXMLTag(objtype, X_CLOSING, es);
4563  break;
4564 
4565  case EXPLAIN_FORMAT_JSON:
4566  es->indent--;
4567  appendStringInfoChar(es->str, '\n');
4568  appendStringInfoSpaces(es->str, 2 * es->indent);
4569  appendStringInfoChar(es->str, labeled ? '}' : ']');
4571  break;
4572 
4573  case EXPLAIN_FORMAT_YAML:
4574  es->indent--;
4576  break;
4577  }
4578 }
static void ExplainXMLTag(const char *tagname, int flags, ExplainState *es)
Definition: explain.c:4833
#define X_CLOSING
Definition: explain.c:53
List * grouping_stack
Definition: explain.h:52
int indent
Definition: explain.h:51
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:188
void appendStringInfoSpaces(StringInfo str, int count)
Definition: stringinfo.c:206
ExplainFormat format
Definition: explain.h:49
StringInfo str
Definition: explain.h:39
List * list_delete_first(List *list)
Definition: list.c:875

◆ ExplainCloseWorker()

static void ExplainCloseWorker ( int  n,
ExplainState es 
)
static

Definition at line 4181 of file explain.c.

References Assert, StringInfoData::data, EXPLAIN_FORMAT_TEXT, ExplainSaveGroup(), ExplainState::format, ExplainState::indent, StringInfoData::len, ExplainWorkersState::prev_str, ExplainState::str, ExplainWorkersState::worker_inited, ExplainWorkersState::worker_state_save, and ExplainState::workers_state.

Referenced by ExplainNode(), show_hashagg_info(), show_incremental_sort_info(), show_resultcache_info(), and show_sort_info().

4182 {
4183  ExplainWorkersState *wstate = es->workers_state;
4184 
4185  Assert(wstate);
4186  Assert(n >= 0 && n < wstate->num_workers);
4187  Assert(wstate->worker_inited[n]);
4188 
4189  /*
4190  * Save formatting state in case we do another ExplainOpenWorker(), then
4191  * pop the formatting stack.
4192  */
4193  ExplainSaveGroup(es, 2, &wstate->worker_state_save[n]);
4194 
4195  /*
4196  * In TEXT format, if we didn't actually produce any output line(s) then
4197  * truncate off the partial line emitted by ExplainOpenWorker. (This is
4198  * to avoid bogus output if, say, show_buffer_usage chooses not to print
4199  * anything for the worker.) Also fix up the indent level.
4200  */
4201  if (es->format == EXPLAIN_FORMAT_TEXT)
4202  {
4203  while (es->str->len > 0 && es->str->data[es->str->len - 1] != '\n')
4204  es->str->data[--(es->str->len)] = '\0';
4205 
4206  es->indent--;
4207  }
4208 
4209  /* Restore prior output buffer pointer */
4210  es->str = wstate->prev_str;
4211 }
int * worker_state_save
Definition: explain.h:33
int indent
Definition: explain.h:51
bool * worker_inited
Definition: explain.h:31
#define Assert(condition)
Definition: c.h:804
ExplainWorkersState * workers_state
Definition: explain.h:61
ExplainFormat format
Definition: explain.h:49
StringInfo prev_str
Definition: explain.h:34
static void ExplainSaveGroup(ExplainState *es, int depth, int *state_save)
Definition: explain.c:4637
StringInfo str
Definition: explain.h:39

◆ ExplainCreateWorkersState()

static ExplainWorkersState * ExplainCreateWorkersState ( int  num_workers)
static

Definition at line 4102 of file explain.c.

References ExplainWorkersState::num_workers, palloc(), palloc0(), ExplainWorkersState::worker_inited, ExplainWorkersState::worker_state_save, and ExplainWorkersState::worker_str.

Referenced by ExplainNode().

4103 {
4104  ExplainWorkersState *wstate;
4105 
4106  wstate = (ExplainWorkersState *) palloc(sizeof(ExplainWorkersState));
4107  wstate->num_workers = num_workers;
4108  wstate->worker_inited = (bool *) palloc0(num_workers * sizeof(bool));
4109  wstate->worker_str = (StringInfoData *)
4110  palloc0(num_workers * sizeof(StringInfoData));
4111  wstate->worker_state_save = (int *) palloc(num_workers * sizeof(int));
4112  return wstate;
4113 }
int * worker_state_save
Definition: explain.h:33
StringInfoData * worker_str
Definition: explain.h:32
void * palloc0(Size size)
Definition: mcxt.c:1093
bool * worker_inited
Definition: explain.h:31
void * palloc(Size size)
Definition: mcxt.c:1062

◆ ExplainCustomChildren()

static void ExplainCustomChildren ( CustomScanState css,
List ancestors,
ExplainState es 
)
static

Definition at line 4080 of file explain.c.

References CustomScanState::custom_ps, ExplainNode(), label, lfirst, and list_length().

Referenced by ExplainNode().

4081 {
4082  ListCell *cell;
4083  const char *label =
4084  (list_length(css->custom_ps) != 1 ? "children" : "child");
4085 
4086  foreach(cell, css->custom_ps)
4087  ExplainNode((PlanState *) lfirst(cell), ancestors, label, NULL, es);
4088 }
List * custom_ps
Definition: execnodes.h:1882
static void ExplainNode(PlanState *planstate, List *ancestors, const char *relationship, const char *plan_name, ExplainState *es)
Definition: explain.c:1143
static char * label
#define lfirst(lc)
Definition: pg_list.h:169
static int list_length(const List *l)
Definition: pg_list.h:149

◆ ExplainDummyGroup()

static void ExplainDummyGroup ( const char *  objtype,
const char *  labelname,
ExplainState es 
)
static

Definition at line 4698 of file explain.c.

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

Referenced by ExplainOneUtility().

4699 {
4700  switch (es->format)
4701  {
4702  case EXPLAIN_FORMAT_TEXT:
4703  /* nothing to do */
4704  break;
4705 
4706  case EXPLAIN_FORMAT_XML:
4707  ExplainXMLTag(objtype, X_CLOSE_IMMEDIATE, es);
4708  break;
4709 
4710  case EXPLAIN_FORMAT_JSON:
4712  appendStringInfoSpaces(es->str, 2 * es->indent);
4713  if (labelname)
4714  {
4715  escape_json(es->str, labelname);
4716  appendStringInfoString(es->str, ": ");
4717  }
4718  escape_json(es->str, objtype);
4719  break;
4720 
4721  case EXPLAIN_FORMAT_YAML:
4723  if (labelname)
4724  {
4725  escape_yaml(es->str, labelname);
4726  appendStringInfoString(es->str, ": ");
4727  }
4728  else
4729  {
4730  appendStringInfoString(es->str, "- ");
4731  }
4732  escape_yaml(es->str, objtype);
4733  break;
4734  }
4735 }
void escape_json(StringInfo buf, const char *str)
Definition: json.c:1279
static void ExplainXMLTag(const char *tagname, int flags, ExplainState *es)
Definition: explain.c:4833
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
static void ExplainYAMLLineStarting(ExplainState *es)
Definition: explain.c:4895
int indent
Definition: explain.h:51
static void escape_yaml(StringInfo buf, const char *str)
Definition: explain.c:4920
void appendStringInfoSpaces(StringInfo str, int count)
Definition: stringinfo.c:206
static void ExplainJSONLineEnding(ExplainState *es)
Definition: explain.c:4875
ExplainFormat format
Definition: explain.h:49
#define X_CLOSE_IMMEDIATE
Definition: explain.c:54
StringInfo str
Definition: explain.h:39

◆ ExplainEndOutput()

void ExplainEndOutput ( ExplainState es)

Definition at line 4775 of file explain.c.

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

Referenced by explain_ExecutorEnd(), and ExplainQuery().

4776 {
4777  switch (es->format)
4778  {
4779  case EXPLAIN_FORMAT_TEXT:
4780  /* nothing to do */
4781  break;
4782 
4783  case EXPLAIN_FORMAT_XML:
4784  es->indent--;
4785  appendStringInfoString(es->str, "</explain>");
4786  break;
4787 
4788  case EXPLAIN_FORMAT_JSON:
4789  es->indent--;
4790  appendStringInfoString(es->str, "\n]");
4792  break;
4793 
4794  case EXPLAIN_FORMAT_YAML:
4796  break;
4797  }
4798 }
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
List * grouping_stack
Definition: explain.h:52
int indent
Definition: explain.h:51
ExplainFormat format
Definition: explain.h:49
StringInfo str
Definition: explain.h:39
List * list_delete_first(List *list)
Definition: list.c:875

◆ ExplainFlushWorkersState()

static void ExplainFlushWorkersState ( ExplainState es)
static

Definition at line 4217 of file explain.c.

References appendStringInfoString(), StringInfoData::data, ExplainCloseGroup(), ExplainOpenGroup(), i, ExplainWorkersState::num_workers, pfree(), ExplainState::str, ExplainWorkersState::worker_inited, ExplainWorkersState::worker_state_save, ExplainWorkersState::worker_str, and ExplainState::workers_state.

Referenced by ExplainNode().

4218 {
4219  ExplainWorkersState *wstate = es->workers_state;
4220 
4221  ExplainOpenGroup("Workers", "Workers", false, es);
4222  for (int i = 0; i < wstate->num_workers; i++)
4223  {
4224  if (wstate->worker_inited[i])
4225  {
4226  /* This must match previous ExplainOpenSetAsideGroup call */
4227  ExplainOpenGroup("Worker", NULL, true, es);
4228  appendStringInfoString(es->str, wstate->worker_str[i].data);
4229  ExplainCloseGroup("Worker", NULL, true, es);
4230 
4231  pfree(wstate->worker_str[i].data);
4232  }
4233  }
4234  ExplainCloseGroup("Workers", "Workers", false, es);
4235 
4236  pfree(wstate->worker_inited);
4237  pfree(wstate->worker_str);
4238  pfree(wstate->worker_state_save);
4239  pfree(wstate);
4240 }
int * worker_state_save
Definition: explain.h:33
void pfree(void *pointer)
Definition: mcxt.c:1169
StringInfoData * worker_str
Definition: explain.h:32
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
bool * worker_inited
Definition: explain.h:31
ExplainWorkersState * workers_state
Definition: explain.h:61
int i
void ExplainCloseGroup(const char *objtype, const char *labelname, bool labeled, ExplainState *es)
Definition: explain.c:4551
void ExplainOpenGroup(const char *objtype, const char *labelname, bool labeled, ExplainState *es)
Definition: explain.c:4488
StringInfo str
Definition: explain.h:39

◆ ExplainIndentText()

static void ExplainIndentText ( ExplainState es)
static

◆ ExplainIndexScanDetails()

static void ExplainIndexScanDetails ( Oid  indexid,
ScanDirection  indexorderdir,
ExplainState es 
)
static

Definition at line 3660 of file explain.c.

References appendStringInfo(), appendStringInfoString(), BackwardScanDirection, EXPLAIN_FORMAT_TEXT, explain_get_index_name(), ExplainPropertyText(), ExplainState::format, ForwardScanDirection, NoMovementScanDirection, quote_identifier(), ScanDirectionIsBackward, and ExplainState::str.

Referenced by ExplainNode().

3662 {
3663  const char *indexname = explain_get_index_name(indexid);
3664 
3665  if (es->format == EXPLAIN_FORMAT_TEXT)
3666  {
3667  if (ScanDirectionIsBackward(indexorderdir))
3668  appendStringInfoString(es->str, " Backward");
3669  appendStringInfo(es->str, " using %s", quote_identifier(indexname));
3670  }
3671  else
3672  {
3673  const char *scandir;
3674 
3675  switch (indexorderdir)
3676  {
3677  case BackwardScanDirection:
3678  scandir = "Backward";
3679  break;
3681  scandir = "NoMovement";
3682  break;
3683  case ForwardScanDirection:
3684  scandir = "Forward";
3685  break;
3686  default:
3687  scandir = "???";
3688  break;
3689  }
3690  ExplainPropertyText("Scan Direction", scandir, es);
3691  ExplainPropertyText("Index Name", indexname, es);
3692  }
3693 }
const char * quote_identifier(const char *ident)
Definition: ruleutils.c:11323
#define ScanDirectionIsBackward(direction)
Definition: sdir.h:41
void ExplainPropertyText(const char *qlabel, const char *value, ExplainState *es)
Definition: explain.c:4423
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
ExplainFormat format
Definition: explain.h:49
static const char * explain_get_index_name(Oid indexId)
Definition: explain.c:3464
StringInfo str
Definition: explain.h:39

◆ ExplainJSONLineEnding()

static void ExplainJSONLineEnding ( ExplainState es)
static

Definition at line 4875 of file explain.c.

References appendStringInfoChar(), Assert, EXPLAIN_FORMAT_JSON, ExplainState::format, ExplainState::grouping_stack, linitial_int, and ExplainState::str.

Referenced by ExplainDummyGroup(), ExplainOpenGroup(), ExplainProperty(), ExplainPropertyList(), and ExplainPropertyListNested().

4876 {
4878  if (linitial_int(es->grouping_stack) != 0)
4879  appendStringInfoChar(es->str, ',');
4880  else
4881  linitial_int(es->grouping_stack) = 1;
4882  appendStringInfoChar(es->str, '\n');
4883 }
#define linitial_int(l)
Definition: pg_list.h:175
List * grouping_stack
Definition: explain.h:52
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:188
#define Assert(condition)
Definition: c.h:804
ExplainFormat format
Definition: explain.h:49
StringInfo str
Definition: explain.h:39

◆ ExplainMemberNodes()

static void ExplainMemberNodes ( PlanState **  planstates,
int  nplans,
List ancestors,
ExplainState es 
)
static

Definition at line 4005 of file explain.c.

References ExplainNode().

Referenced by ExplainNode().

4007 {
4008  int j;
4009 
4010  for (j = 0; j < nplans; j++)
4011  ExplainNode(planstates[j], ancestors,
4012  "Member", NULL, es);
4013 }
static void ExplainNode(PlanState *planstate, List *ancestors, const char *relationship, const char *plan_name, ExplainState *es)
Definition: explain.c:1143

◆ ExplainMissingMembers()

static void ExplainMissingMembers ( int  nplans,
int  nchildren,
ExplainState es 
)
static

Definition at line 4023 of file explain.c.

References EXPLAIN_FORMAT_TEXT, ExplainPropertyInteger(), and format.

Referenced by ExplainNode().

4024 {
4025  if (nplans < nchildren || es->format != EXPLAIN_FORMAT_TEXT)
4026  ExplainPropertyInteger("Subplans Removed", NULL,
4027  nchildren - nplans, es);
4028 }
void ExplainPropertyInteger(const char *qlabel, const char *unit, int64 value, ExplainState *es)
Definition: explain.c:4432
static char format

◆ ExplainModifyTarget()

static void ExplainModifyTarget ( ModifyTable plan,
ExplainState es 
)
static

Definition at line 3712 of file explain.c.

References ExplainTargetRel().

Referenced by ExplainNode().

3713 {
3714  ExplainTargetRel((Plan *) plan, plan->nominalRelation, es);
3715 }
static void ExplainTargetRel(Plan *plan, Index rti, ExplainState *es)
Definition: explain.c:3721

◆ ExplainNode()

static void ExplainNode ( PlanState planstate,
List ancestors,
const char *  relationship,
const char *  plan_name,
ExplainState es 
)
static

Definition at line 1143 of file explain.c.

References AGG_HASHED, AGG_MIXED, AGG_PLAIN, AGG_SORTED, Agg::aggsplit, Agg::aggstrategy, ExplainState::analyze, appendStringInfo(), appendStringInfoChar(), appendStringInfoString(), Plan::async_capable, ExplainState::buffers, Instrumentation::bufusage, castNode, CMD_DELETE, CMD_INSERT, CMD_SELECT, CMD_UPDATE, ExplainState::costs, DO_AGGSPLIT_COMBINE, DO_AGGSPLIT_SKIPFINAL, EState::es_jit_flags, EXPLAIN_FORMAT_TEXT, explain_get_index_name(), ExplainCloseGroup(), ExplainCloseWorker(), ExplainCreateWorkersState(), ExplainCustomChildren(), CustomExecMethods::ExplainCustomScan, ExplainFlushWorkersState(), ExplainIndentText(), ExplainIndexScanDetails(), ExplainMemberNodes(), ExplainMissingMembers(), ExplainModifyTarget(), ExplainOpenGroup(), ExplainOpenWorker(), ExplainPrintJIT(), ExplainPropertyBool(), ExplainPropertyFloat(), ExplainPropertyInteger(), ExplainPropertyText(), ExplainScanTarget(), ExplainSubPlans(), ExplainState::format, RangeTblFunction::funcexpr, functions, ExplainState::hide_workers, ExplainState::indent, IndexScan::indexid, IndexOnlyScan::indexid, BitmapIndexScan::indexid, IndexScan::indexorderdir, IndexOnlyScan::indexorderdir, Gather::initParam, GatherMerge::initParam, PlanState::initPlan, innerPlanState, InstrEndLoop(), WorkerInstrumentation::instrument, PlanState::instrument, IsA, SharedJitInstrumentation::jit_instr, JOIN_ANTI, JOIN_FULL, JOIN_INNER, JOIN_LEFT, JOIN_RIGHT, JOIN_SEMI, lappend(), lcons(), lfirst, list_delete_first(), list_length(), list_make1, make_andclause(), make_orclause(), CustomScanState::methods, NIL, Instrumentation::nloops, nodeTag, Instrumentation::ntuples, Instrumentation::ntuples2, SharedJitInstrumentation::num_workers, WorkerInstrumentation::num_workers, Gather::num_workers, GatherMerge::num_workers, outerPlanState, Plan::parallel_aware, PlanState::plan, Plan::plan_rows, Plan::plan_width, psprintf(), quote_identifier(), SETOP_HASHED, SETOP_SORTED, SETOPCMD_EXCEPT, SETOPCMD_EXCEPT_ALL, SETOPCMD_INTERSECT, SETOPCMD_INTERSECT_ALL, show_agg_keys(), show_buffer_usage(), show_eval_params(), show_expression(), show_foreignscan_info(), show_group_keys(), show_hash_info(), show_hashagg_info(), show_incremental_sort_info(), show_incremental_sort_keys(), show_instrumentation_count(), show_merge_append_keys(), show_modifytable_info(), show_plan_tlist(), show_resultcache_info(), show_scan_qual(), show_sort_info(), show_sort_keys(), show_tablesample(), show_tidbitmap_info(), show_upper_qual(), show_wal_usage(), Gather::single_copy, Instrumentation::startup, Plan::startup_cost, PlanState::state, ExplainState::str, PlanState::subPlan, T_Agg, T_Append, T_BitmapAnd, T_BitmapHeapScan, T_BitmapIndexScan, T_BitmapOr, T_CteScan, T_CustomScan, T_ForeignScan, T_FunctionScan, T_Gather, T_GatherMerge, T_Group, T_Hash, T_HashJoin, T_IncrementalSort, T_IndexOnlyScan, T_IndexScan, T_Limit, T_LockRows, T_Material, T_MergeAppend, T_MergeJoin, T_ModifyTable, T_NamedTuplestoreScan, T_NestLoop, T_ProjectSet, T_RecursiveUnion, T_Result, T_ResultCache, T_SampleScan, T_SeqScan, T_SetOp, T_Sort, T_SubqueryScan, T_TableFuncScan, T_TidRangeScan, T_TidScan, T_Unique, T_ValuesScan, T_WindowAgg, T_WorkTableScan, ExplainState::timing, Instrumentation::total, Plan::total_cost, ExplainState::verbose, ExplainState::wal, Instrumentation::walusage, PlanState::worker_instrument, PlanState::worker_jit_instrument, and ExplainState::workers_state.

Referenced by ExplainCustomChildren(), ExplainMemberNodes(), ExplainPrintPlan(), and ExplainSubPlans().

1146 {
1147  Plan *plan = planstate->plan;
1148  const char *pname; /* node type name for text output */
1149  const char *sname; /* node type name for non-text output */
1150  const char *strategy = NULL;
1151  const char *partialmode = NULL;
1152  const char *operation = NULL;
1153  const char *custom_name = NULL;
1154  ExplainWorkersState *save_workers_state = es->workers_state;
1155  int save_indent = es->indent;
1156  bool haschildren;
1157 
1158  /*
1159  * Prepare per-worker output buffers, if needed. We'll append the data in
1160  * these to the main output string further down.
1161  */
1162  if (planstate->worker_instrument && es->analyze && !es->hide_workers)
1164  else
1165  es->workers_state = NULL;
1166 
1167  /* Identify plan node type, and print generic details */
1168  switch (nodeTag(plan))
1169  {
1170  case T_Result:
1171  pname = sname = "Result";
1172  break;
1173  case T_ProjectSet:
1174  pname = sname = "ProjectSet";
1175  break;
1176  case T_ModifyTable:
1177  sname = "ModifyTable";
1178  switch (((ModifyTable *) plan)->operation)
1179  {
1180  case CMD_INSERT:
1181  pname = operation = "Insert";
1182  break;
1183  case CMD_UPDATE:
1184  pname = operation = "Update";
1185  break;
1186  case CMD_DELETE:
1187  pname = operation = "Delete";
1188  break;
1189  default:
1190  pname = "???";
1191  break;
1192  }
1193  break;
1194  case T_Append:
1195  pname = sname = "Append";
1196  break;
1197  case T_MergeAppend:
1198  pname = sname = "Merge Append";
1199  break;
1200  case T_RecursiveUnion:
1201  pname = sname = "Recursive Union";
1202  break;
1203  case T_BitmapAnd:
1204  pname = sname = "BitmapAnd";
1205  break;
1206  case T_BitmapOr:
1207  pname = sname = "BitmapOr";
1208  break;
1209  case T_NestLoop:
1210  pname = sname = "Nested Loop";
1211  break;
1212  case T_MergeJoin:
1213  pname = "Merge"; /* "Join" gets added by jointype switch */
1214  sname = "Merge Join";
1215  break;
1216  case T_HashJoin:
1217  pname = "Hash"; /* "Join" gets added by jointype switch */
1218  sname = "Hash Join";
1219  break;
1220  case T_SeqScan:
1221  pname = sname = "Seq Scan";
1222  break;
1223  case T_SampleScan:
1224  pname = sname = "Sample Scan";
1225  break;
1226  case T_Gather:
1227  pname = sname = "Gather";
1228  break;
1229  case T_GatherMerge:
1230  pname = sname = "Gather Merge";
1231  break;
1232  case T_IndexScan:
1233  pname = sname = "Index Scan";
1234  break;
1235  case T_IndexOnlyScan:
1236  pname = sname = "Index Only Scan";
1237  break;
1238  case T_BitmapIndexScan:
1239  pname = sname = "Bitmap Index Scan";
1240  break;
1241  case T_BitmapHeapScan:
1242  pname = sname = "Bitmap Heap Scan";
1243  break;
1244  case T_TidScan:
1245  pname = sname = "Tid Scan";
1246  break;
1247  case T_TidRangeScan:
1248  pname = sname = "Tid Range Scan";
1249  break;
1250  case T_SubqueryScan:
1251  pname = sname = "Subquery Scan";
1252  break;
1253  case T_FunctionScan:
1254  pname = sname = "Function Scan";
1255  break;
1256  case T_TableFuncScan:
1257  pname = sname = "Table Function Scan";
1258  break;
1259  case T_ValuesScan:
1260  pname = sname = "Values Scan";
1261  break;
1262  case T_CteScan:
1263  pname = sname = "CTE Scan";
1264  break;
1265  case T_NamedTuplestoreScan:
1266  pname = sname = "Named Tuplestore Scan";
1267  break;
1268  case T_WorkTableScan:
1269  pname = sname = "WorkTable Scan";
1270  break;
1271  case T_ForeignScan:
1272  sname = "Foreign Scan";
1273  switch (((ForeignScan *) plan)->operation)
1274  {
1275  case CMD_SELECT:
1276  pname = "Foreign Scan";
1277  operation = "Select";
1278  break;
1279  case CMD_INSERT:
1280  pname = "Foreign Insert";
1281  operation = "Insert";
1282  break;
1283  case CMD_UPDATE:
1284  pname = "Foreign Update";
1285  operation = "Update";
1286  break;
1287  case CMD_DELETE:
1288  pname = "Foreign Delete";
1289  operation = "Delete";
1290  break;
1291  default:
1292  pname = "???";
1293  break;
1294  }
1295  break;
1296  case T_CustomScan:
1297  sname = "Custom Scan";
1298  custom_name = ((CustomScan *) plan)->methods->CustomName;
1299  if (custom_name)
1300  pname = psprintf("Custom Scan (%s)", custom_name);
1301  else
1302  pname = sname;
1303  break;
1304  case T_Material:
1305  pname = sname = "Materialize";
1306  break;
1307  case T_ResultCache:
1308  pname = sname = "Result Cache";
1309  break;
1310  case T_Sort:
1311  pname = sname = "Sort";
1312  break;
1313  case T_IncrementalSort:
1314  pname = sname = "Incremental Sort";
1315  break;
1316  case T_Group:
1317  pname = sname = "Group";
1318  break;
1319  case T_Agg:
1320  {
1321  Agg *agg = (Agg *) plan;
1322 
1323  sname = "Aggregate";
1324  switch (agg->aggstrategy)
1325  {
1326  case AGG_PLAIN:
1327  pname = "Aggregate";
1328  strategy = "Plain";
1329  break;
1330  case AGG_SORTED:
1331  pname = "GroupAggregate";
1332  strategy = "Sorted";
1333  break;
1334  case AGG_HASHED:
1335  pname = "HashAggregate";
1336  strategy = "Hashed";
1337  break;
1338  case AGG_MIXED:
1339  pname = "MixedAggregate";
1340  strategy = "Mixed";
1341  break;
1342  default:
1343  pname = "Aggregate ???";
1344  strategy = "???";
1345  break;
1346  }
1347 
1348  if (DO_AGGSPLIT_SKIPFINAL(agg->aggsplit))
1349  {
1350  partialmode = "Partial";
1351  pname = psprintf("%s %s", partialmode, pname);
1352  }
1353  else if (DO_AGGSPLIT_COMBINE(agg->aggsplit))
1354  {
1355  partialmode = "Finalize";
1356  pname = psprintf("%s %s", partialmode, pname);
1357  }
1358  else
1359  partialmode = "Simple";
1360  }
1361  break;
1362  case T_WindowAgg:
1363  pname = sname = "WindowAgg";
1364  break;
1365  case T_Unique:
1366  pname = sname = "Unique";
1367  break;
1368  case T_SetOp:
1369  sname = "SetOp";
1370  switch (((SetOp *) plan)->strategy)
1371  {
1372  case SETOP_SORTED:
1373  pname = "SetOp";
1374  strategy = "Sorted";
1375  break;
1376  case SETOP_HASHED:
1377  pname = "HashSetOp";
1378  strategy = "Hashed";
1379  break;
1380  default:
1381  pname = "SetOp ???";
1382  strategy = "???";
1383  break;
1384  }
1385  break;
1386  case T_LockRows:
1387  pname = sname = "LockRows";
1388  break;
1389  case T_Limit:
1390  pname = sname = "Limit";
1391  break;
1392  case T_Hash:
1393  pname = sname = "Hash";
1394  break;
1395  default:
1396  pname = sname = "???";
1397  break;
1398  }
1399 
1400  ExplainOpenGroup("Plan",
1401  relationship ? NULL : "Plan",
1402  true, es);
1403 
1404  if (es->format == EXPLAIN_FORMAT_TEXT)
1405  {
1406  if (plan_name)
1407  {
1408  ExplainIndentText(es);
1409  appendStringInfo(es->str, "%s\n", plan_name);
1410  es->indent++;
1411  }
1412  if (es->indent)
1413  {
1414  ExplainIndentText(es);
1415  appendStringInfoString(es->str, "-> ");
1416  es->indent += 2;
1417  }
1418  if (plan->parallel_aware)
1419  appendStringInfoString(es->str, "Parallel ");
1420  if (plan->async_capable)
1421  appendStringInfoString(es->str, "Async ");
1422  appendStringInfoString(es->str, pname);
1423  es->indent++;
1424  }
1425  else
1426  {
1427  ExplainPropertyText("Node Type", sname, es);
1428  if (strategy)
1429  ExplainPropertyText("Strategy", strategy, es);
1430  if (partialmode)
1431  ExplainPropertyText("Partial Mode", partialmode, es);
1432  if (operation)
1433  ExplainPropertyText("Operation", operation, es);
1434  if (relationship)
1435  ExplainPropertyText("Parent Relationship", relationship, es);
1436  if (plan_name)
1437  ExplainPropertyText("Subplan Name", plan_name, es);
1438  if (custom_name)
1439  ExplainPropertyText("Custom Plan Provider", custom_name, es);
1440  ExplainPropertyBool("Parallel Aware", plan->parallel_aware, es);
1441  ExplainPropertyBool("Async Capable", plan->async_capable, es);
1442  }
1443 
1444  switch (nodeTag(plan))
1445  {
1446  case T_SeqScan:
1447  case T_SampleScan:
1448  case T_BitmapHeapScan:
1449  case T_TidScan:
1450  case T_TidRangeScan:
1451  case T_SubqueryScan:
1452  case T_FunctionScan:
1453  case T_TableFuncScan:
1454  case T_ValuesScan:
1455  case T_CteScan:
1456  case T_WorkTableScan:
1457  ExplainScanTarget((Scan *) plan, es);
1458  break;
1459  case T_ForeignScan:
1460  case T_CustomScan:
1461  if (((Scan *) plan)->scanrelid > 0)
1462  ExplainScanTarget((Scan *) plan, es);
1463  break;
1464  case T_IndexScan:
1465  {
1466  IndexScan *indexscan = (IndexScan *) plan;
1467 
1468  ExplainIndexScanDetails(indexscan->indexid,
1469  indexscan->indexorderdir,
1470  es);
1471  ExplainScanTarget((Scan *) indexscan, es);
1472  }
1473  break;
1474  case T_IndexOnlyScan:
1475  {
1476  IndexOnlyScan *indexonlyscan = (IndexOnlyScan *) plan;
1477 
1478  ExplainIndexScanDetails(indexonlyscan->indexid,
1479  indexonlyscan->indexorderdir,
1480  es);
1481  ExplainScanTarget((Scan *) indexonlyscan, es);
1482  }
1483  break;
1484  case T_BitmapIndexScan:
1485  {
1486  BitmapIndexScan *bitmapindexscan = (BitmapIndexScan *) plan;
1487  const char *indexname =
1488  explain_get_index_name(bitmapindexscan->indexid);
1489 
1490  if (es->format == EXPLAIN_FORMAT_TEXT)
1491  appendStringInfo(es->str, " on %s",
1492  quote_identifier(indexname));
1493  else
1494  ExplainPropertyText("Index Name", indexname, es);
1495  }
1496  break;
1497  case T_ModifyTable:
1498  ExplainModifyTarget((ModifyTable *) plan, es);
1499  break;
1500  case T_NestLoop:
1501  case T_MergeJoin:
1502  case T_HashJoin:
1503  {
1504  const char *jointype;
1505 
1506  switch (((Join *) plan)->jointype)
1507  {
1508  case JOIN_INNER:
1509  jointype = "Inner";
1510  break;
1511  case JOIN_LEFT:
1512  jointype = "Left";
1513  break;
1514  case JOIN_FULL:
1515  jointype = "Full";
1516  break;
1517  case JOIN_RIGHT:
1518  jointype = "Right";
1519  break;
1520  case JOIN_SEMI:
1521  jointype = "Semi";
1522  break;
1523  case JOIN_ANTI:
1524  jointype = "Anti";
1525  break;
1526  default:
1527  jointype = "???";
1528  break;
1529  }
1530  if (es->format == EXPLAIN_FORMAT_TEXT)
1531  {
1532  /*
1533  * For historical reasons, the join type is interpolated
1534  * into the node type name...
1535  */
1536  if (((Join *) plan)->jointype != JOIN_INNER)
1537  appendStringInfo(es->str, " %s Join", jointype);
1538  else if (!IsA(plan, NestLoop))
1539  appendStringInfoString(es->str, " Join");
1540  }
1541  else
1542  ExplainPropertyText("Join Type", jointype, es);
1543  }
1544  break;
1545  case T_SetOp:
1546  {
1547  const char *setopcmd;
1548 
1549  switch (((SetOp *) plan)->cmd)
1550  {
1551  case SETOPCMD_INTERSECT:
1552  setopcmd = "Intersect";
1553  break;
1555  setopcmd = "Intersect All";
1556  break;
1557  case SETOPCMD_EXCEPT:
1558  setopcmd = "Except";
1559  break;
1560  case SETOPCMD_EXCEPT_ALL:
1561  setopcmd = "Except All";
1562  break;
1563  default:
1564  setopcmd = "???";
1565  break;
1566  }
1567  if (es->format == EXPLAIN_FORMAT_TEXT)
1568  appendStringInfo(es->str, " %s", setopcmd);
1569  else
1570  ExplainPropertyText("Command", setopcmd, es);
1571  }
1572  break;
1573  default:
1574  break;
1575  }
1576 
1577  if (es->costs)
1578  {
1579  if (es->format == EXPLAIN_FORMAT_TEXT)
1580  {
1581  appendStringInfo(es->str, " (cost=%.2f..%.2f rows=%.0f width=%d)",
1582  plan->startup_cost, plan->total_cost,
1583  plan->plan_rows, plan->plan_width);
1584  }
1585  else
1586  {
1587  ExplainPropertyFloat("Startup Cost", NULL, plan->startup_cost,
1588  2, es);
1589  ExplainPropertyFloat("Total Cost", NULL, plan->total_cost,
1590  2, es);
1591  ExplainPropertyFloat("Plan Rows", NULL, plan->plan_rows,
1592  0, es);
1593  ExplainPropertyInteger("Plan Width", NULL, plan->plan_width,
1594  es);
1595  }
1596  }
1597 
1598  /*
1599  * We have to forcibly clean up the instrumentation state because we
1600  * haven't done ExecutorEnd yet. This is pretty grotty ...
1601  *
1602  * Note: contrib/auto_explain could cause instrumentation to be set up
1603  * even though we didn't ask for it here. Be careful not to print any
1604  * instrumentation results the user didn't ask for. But we do the
1605  * InstrEndLoop call anyway, if possible, to reduce the number of cases
1606  * auto_explain has to contend with.
1607  */
1608  if (planstate->instrument)
1609  InstrEndLoop(planstate->instrument);
1610 
1611  if (es->analyze &&
1612  planstate->instrument && planstate->instrument->nloops > 0)
1613  {
1614  double nloops = planstate->instrument->nloops;
1615  double startup_ms = 1000.0 * planstate->instrument->startup / nloops;
1616  double total_ms = 1000.0 * planstate->instrument->total / nloops;
1617  double rows = planstate->instrument->ntuples / nloops;
1618 
1619  if (es->format == EXPLAIN_FORMAT_TEXT)
1620  {
1621  if (es->timing)
1622  appendStringInfo(es->str,
1623  " (actual time=%.3f..%.3f rows=%.0f loops=%.0f)",
1624  startup_ms, total_ms, rows, nloops);
1625  else
1626  appendStringInfo(es->str,
1627  " (actual rows=%.0f loops=%.0f)",
1628  rows, nloops);
1629  }
1630  else
1631  {
1632  if (es->timing)
1633  {
1634  ExplainPropertyFloat("Actual Startup Time", "ms", startup_ms,
1635  3, es);
1636  ExplainPropertyFloat("Actual Total Time", "ms", total_ms,
1637  3, es);
1638  }
1639  ExplainPropertyFloat("Actual Rows", NULL, rows, 0, es);
1640  ExplainPropertyFloat("Actual Loops", NULL, nloops, 0, es);
1641  }
1642  }
1643  else if (es->analyze)
1644  {
1645  if (es->format == EXPLAIN_FORMAT_TEXT)
1646  appendStringInfoString(es->str, " (never executed)");
1647  else
1648  {
1649  if (es->timing)
1650  {
1651  ExplainPropertyFloat("Actual Startup Time", "ms", 0.0, 3, es);
1652  ExplainPropertyFloat("Actual Total Time", "ms", 0.0, 3, es);
1653  }
1654  ExplainPropertyFloat("Actual Rows", NULL, 0.0, 0, es);
1655  ExplainPropertyFloat("Actual Loops", NULL, 0.0, 0, es);
1656  }
1657  }
1658 
1659  /* in text format, first line ends here */
1660  if (es->format == EXPLAIN_FORMAT_TEXT)
1661  appendStringInfoChar(es->str, '\n');
1662 
1663  /* prepare per-worker general execution details */
1664  if (es->workers_state && es->verbose)
1665  {
1666  WorkerInstrumentation *w = planstate->worker_instrument;
1667 
1668  for (int n = 0; n < w->num_workers; n++)
1669  {
1670  Instrumentation *instrument = &w->instrument[n];
1671  double nloops = instrument->nloops;
1672  double startup_ms;
1673  double total_ms;
1674  double rows;
1675 
1676  if (nloops <= 0)
1677  continue;
1678  startup_ms = 1000.0 * instrument->startup / nloops;
1679  total_ms = 1000.0 * instrument->total / nloops;
1680  rows = instrument->ntuples / nloops;
1681 
1682  ExplainOpenWorker(n, es);
1683 
1684  if (es->format == EXPLAIN_FORMAT_TEXT)
1685  {
1686  ExplainIndentText(es);
1687  if (es->timing)
1688  appendStringInfo(es->str,
1689  "actual time=%.3f..%.3f rows=%.0f loops=%.0f\n",
1690  startup_ms, total_ms, rows, nloops);
1691  else
1692  appendStringInfo(es->str,
1693  "actual rows=%.0f loops=%.0f\n",
1694  rows, nloops);
1695  }
1696  else
1697  {
1698  if (es->timing)
1699  {
1700  ExplainPropertyFloat("Actual Startup Time", "ms",
1701  startup_ms, 3, es);
1702  ExplainPropertyFloat("Actual Total Time", "ms",
1703  total_ms, 3, es);
1704  }
1705  ExplainPropertyFloat("Actual Rows", NULL, rows, 0, es);
1706  ExplainPropertyFloat("Actual Loops", NULL, nloops, 0, es);
1707  }
1708 
1709  ExplainCloseWorker(n, es);
1710  }
1711  }
1712 
1713  /* target list */
1714  if (es->verbose)
1715  show_plan_tlist(planstate, ancestors, es);
1716 
1717  /* unique join */
1718  switch (nodeTag(plan))
1719  {
1720  case T_NestLoop:
1721  case T_MergeJoin:
1722  case T_HashJoin:
1723  /* try not to be too chatty about this in text mode */
1724  if (es->format != EXPLAIN_FORMAT_TEXT ||
1725  (es->verbose && ((Join *) plan)->inner_unique))
1726  ExplainPropertyBool("Inner Unique",
1727  ((Join *) plan)->inner_unique,
1728  es);
1729  break;
1730  default:
1731  break;
1732  }
1733 
1734  /* quals, sort keys, etc */
1735  switch (nodeTag(plan))
1736  {
1737  case T_IndexScan:
1738  show_scan_qual(((IndexScan *) plan)->indexqualorig,
1739  "Index Cond", planstate, ancestors, es);
1740  if (((IndexScan *) plan)->indexqualorig)
1741  show_instrumentation_count("Rows Removed by Index Recheck", 2,
1742  planstate, es);
1743  show_scan_qual(((IndexScan *) plan)->indexorderbyorig,
1744  "Order By", planstate, ancestors, es);
1745  show_scan_qual(plan->qual, "Filter", planstate, ancestors, es);
1746  if (plan->qual)
1747  show_instrumentation_count("Rows Removed by Filter", 1,
1748  planstate, es);
1749  break;
1750  case T_IndexOnlyScan:
1751  show_scan_qual(((IndexOnlyScan *) plan)->indexqual,
1752  "Index Cond", planstate, ancestors, es);
1753  if (((IndexOnlyScan *) plan)->indexqual)
1754  show_instrumentation_count("Rows Removed by Index Recheck", 2,
1755  planstate, es);
1756  show_scan_qual(((IndexOnlyScan *) plan)->indexorderby,
1757  "Order By", planstate, ancestors, es);
1758  show_scan_qual(plan->qual, "Filter", planstate, ancestors, es);
1759  if (plan->qual)
1760  show_instrumentation_count("Rows Removed by Filter", 1,
1761  planstate, es);
1762  if (es->analyze)
1763  ExplainPropertyFloat("Heap Fetches", NULL,
1764  planstate->instrument->ntuples2, 0, es);
1765  break;
1766  case T_BitmapIndexScan:
1767  show_scan_qual(((BitmapIndexScan *) plan)->indexqualorig,
1768  "Index Cond", planstate, ancestors, es);
1769  break;
1770  case T_BitmapHeapScan:
1771  show_scan_qual(((BitmapHeapScan *) plan)->bitmapqualorig,
1772  "Recheck Cond", planstate, ancestors, es);
1773  if (((BitmapHeapScan *) plan)->bitmapqualorig)
1774  show_instrumentation_count("Rows Removed by Index Recheck", 2,
1775  planstate, es);
1776  show_scan_qual(plan->qual, "Filter", planstate, ancestors, es);
1777  if (plan->qual)
1778  show_instrumentation_count("Rows Removed by Filter", 1,
1779  planstate, es);
1780  if (es->analyze)
1781  show_tidbitmap_info((BitmapHeapScanState *) planstate, es);
1782  break;
1783  case T_SampleScan:
1784  show_tablesample(((SampleScan *) plan)->tablesample,
1785  planstate, ancestors, es);
1786  /* fall through to print additional fields the same as SeqScan */
1787  /* FALLTHROUGH */
1788  case T_SeqScan:
1789  case T_ValuesScan:
1790  case T_CteScan:
1791  case T_NamedTuplestoreScan:
1792  case T_WorkTableScan:
1793  case T_SubqueryScan:
1794  show_scan_qual(plan->qual, "Filter", planstate, ancestors, es);
1795  if (plan->qual)
1796  show_instrumentation_count("Rows Removed by Filter", 1,
1797  planstate, es);
1798  break;
1799  case T_Gather:
1800  {
1801  Gather *gather = (Gather *) plan;
1802 
1803  show_scan_qual(plan->qual, "Filter", planstate, ancestors, es);
1804  if (plan->qual)
1805  show_instrumentation_count("Rows Removed by Filter", 1,
1806  planstate, es);
1807  ExplainPropertyInteger("Workers Planned", NULL,
1808  gather->num_workers, es);
1809 
1810  /* Show params evaluated at gather node */
1811  if (gather->initParam)
1812  show_eval_params(gather->initParam, es);
1813 
1814  if (es->analyze)
1815  {
1816  int nworkers;
1817 
1818  nworkers = ((GatherState *) planstate)->nworkers_launched;
1819  ExplainPropertyInteger("Workers Launched", NULL,
1820  nworkers, es);
1821  }
1822 
1823  if (gather->single_copy || es->format != EXPLAIN_FORMAT_TEXT)
1824  ExplainPropertyBool("Single Copy", gather->single_copy, es);
1825  }
1826  break;
1827  case T_GatherMerge:
1828  {
1829  GatherMerge *gm = (GatherMerge *) plan;
1830 
1831  show_scan_qual(plan->qual, "Filter", planstate, ancestors, es);
1832  if (plan->qual)
1833  show_instrumentation_count("Rows Removed by Filter", 1,
1834  planstate, es);
1835  ExplainPropertyInteger("Workers Planned", NULL,
1836  gm->num_workers, es);
1837 
1838  /* Show params evaluated at gather-merge node */
1839  if (gm->initParam)
1840  show_eval_params(gm->initParam, es);
1841 
1842  if (es->analyze)
1843  {
1844  int nworkers;
1845 
1846  nworkers = ((GatherMergeState *) planstate)->nworkers_launched;
1847  ExplainPropertyInteger("Workers Launched", NULL,
1848  nworkers, es);
1849  }
1850  }
1851  break;
1852  case T_FunctionScan:
1853  if (es->verbose)
1854  {
1855  List *fexprs = NIL;
1856  ListCell *lc;
1857 
1858  foreach(lc, ((FunctionScan *) plan)->functions)
1859  {
1860  RangeTblFunction *rtfunc = (RangeTblFunction *) lfirst(lc);
1861 
1862  fexprs = lappend(fexprs, rtfunc->funcexpr);
1863  }
1864  /* We rely on show_expression to insert commas as needed */
1865  show_expression((Node *) fexprs,
1866  "Function Call", planstate, ancestors,
1867  es->verbose, es);
1868  }
1869  show_scan_qual(plan->qual, "Filter", planstate, ancestors, es);
1870  if (plan->qual)
1871  show_instrumentation_count("Rows Removed by Filter", 1,
1872  planstate, es);
1873  break;
1874  case T_TableFuncScan:
1875  if (es->verbose)
1876  {
1877  TableFunc *tablefunc = ((TableFuncScan *) plan)->tablefunc;
1878 
1879  show_expression((Node *) tablefunc,
1880  "Table Function Call", planstate, ancestors,
1881  es->verbose, es);
1882  }
1883  show_scan_qual(plan->qual, "Filter", planstate, ancestors, es);
1884  if (plan->qual)
1885  show_instrumentation_count("Rows Removed by Filter", 1,
1886  planstate, es);
1887  break;
1888  case T_TidScan:
1889  {
1890  /*
1891  * The tidquals list has OR semantics, so be sure to show it
1892  * as an OR condition.
1893  */
1894  List *tidquals = ((TidScan *) plan)->tidquals;
1895 
1896  if (list_length(tidquals) > 1)
1897  tidquals = list_make1(make_orclause(tidquals));
1898  show_scan_qual(tidquals, "TID Cond", planstate, ancestors, es);
1899  show_scan_qual(plan->qual, "Filter", planstate, ancestors, es);
1900  if (plan->qual)
1901  show_instrumentation_count("Rows Removed by Filter", 1,
1902  planstate, es);
1903  }
1904  break;
1905  case T_TidRangeScan:
1906  {
1907  /*
1908  * The tidrangequals list has AND semantics, so be sure to
1909  * show it as an AND condition.
1910  */
1911  List *tidquals = ((TidRangeScan *) plan)->tidrangequals;
1912 
1913  if (list_length(tidquals) > 1)
1914  tidquals = list_make1(make_andclause(tidquals));
1915  show_scan_qual(tidquals, "TID Cond", planstate, ancestors, es);
1916  show_scan_qual(plan->qual, "Filter", planstate, ancestors, es);
1917  if (plan->qual)
1918  show_instrumentation_count("Rows Removed by Filter", 1,
1919  planstate, es);
1920  }
1921  break;
1922  case T_ForeignScan:
1923  show_scan_qual(plan->qual, "Filter", planstate, ancestors, es);
1924  if (plan->qual)
1925  show_instrumentation_count("Rows Removed by Filter", 1,
1926  planstate, es);
1927  show_foreignscan_info((ForeignScanState *) planstate, es);
1928  break;
1929  case T_CustomScan:
1930  {
1931  CustomScanState *css = (CustomScanState *) planstate;
1932 
1933  show_scan_qual(plan->qual, "Filter", planstate, ancestors, es);
1934  if (plan->qual)
1935  show_instrumentation_count("Rows Removed by Filter", 1,
1936  planstate, es);
1937  if (css->methods->ExplainCustomScan)
1938  css->methods->ExplainCustomScan(css, ancestors, es);
1939  }
1940  break;
1941  case T_NestLoop:
1942  show_upper_qual(((NestLoop *) plan)->join.joinqual,
1943  "Join Filter", planstate, ancestors, es);
1944  if (((NestLoop *) plan)->join.joinqual)
1945  show_instrumentation_count("Rows Removed by Join Filter", 1,
1946  planstate, es);
1947  show_upper_qual(plan->qual, "Filter", planstate, ancestors, es);
1948  if (plan->qual)
1949  show_instrumentation_count("Rows Removed by Filter", 2,
1950  planstate, es);
1951  break;
1952  case T_MergeJoin:
1953  show_upper_qual(((MergeJoin *) plan)->mergeclauses,
1954  "Merge Cond", planstate, ancestors, es);
1955  show_upper_qual(((MergeJoin *) plan)->join.joinqual,
1956  "Join Filter", planstate, ancestors, es);
1957  if (((MergeJoin *) plan)->join.joinqual)
1958  show_instrumentation_count("Rows Removed by Join Filter", 1,
1959  planstate, es);
1960  show_upper_qual(plan->qual, "Filter", planstate, ancestors, es);
1961  if (plan->qual)
1962  show_instrumentation_count("Rows Removed by Filter", 2,
1963  planstate, es);
1964  break;
1965  case T_HashJoin:
1966  show_upper_qual(((HashJoin *) plan)->hashclauses,
1967  "Hash Cond", planstate, ancestors, es);
1968  show_upper_qual(((HashJoin *) plan)->join.joinqual,
1969  "Join Filter", planstate, ancestors, es);
1970  if (((HashJoin *) plan)->join.joinqual)
1971  show_instrumentation_count("Rows Removed by Join Filter", 1,
1972  planstate, es);
1973  show_upper_qual(plan->qual, "Filter", planstate, ancestors, es);
1974  if (plan->qual)
1975  show_instrumentation_count("Rows Removed by Filter", 2,
1976  planstate, es);
1977  break;
1978  case T_Agg:
1979  show_agg_keys(castNode(AggState, planstate), ancestors, es);
1980  show_upper_qual(plan->qual, "Filter", planstate, ancestors, es);
1981  show_hashagg_info((AggState *) planstate, es);
1982  if (plan->qual)
1983  show_instrumentation_count("Rows Removed by Filter", 1,
1984  planstate, es);
1985  break;
1986  case T_Group:
1987  show_group_keys(castNode(GroupState, planstate), ancestors, es);
1988  show_upper_qual(plan->qual, "Filter", planstate, ancestors, es);
1989  if (plan->qual)
1990  show_instrumentation_count("Rows Removed by Filter", 1,
1991  planstate, es);
1992  break;
1993  case T_Sort:
1994  show_sort_keys(castNode(SortState, planstate), ancestors, es);
1995  show_sort_info(castNode(SortState, planstate), es);
1996  break;
1997  case T_IncrementalSort:
1999  ancestors, es);
2001  es);
2002  break;
2003  case T_MergeAppend:
2005  ancestors, es);
2006  break;
2007  case T_Result:
2008  show_upper_qual((List *) ((Result *) plan)->resconstantqual,
2009  "One-Time Filter", planstate, ancestors, es);
2010  show_upper_qual(plan->qual, "Filter", planstate, ancestors, es);
2011  if (plan->qual)
2012  show_instrumentation_count("Rows Removed by Filter", 1,
2013  planstate, es);
2014  break;
2015  case T_ModifyTable:
2016  show_modifytable_info(castNode(ModifyTableState, planstate), ancestors,
2017  es);
2018  break;
2019  case T_Hash:
2020  show_hash_info(castNode(HashState, planstate), es);
2021  break;
2022  case T_ResultCache:
2024  ancestors, es);
2025  break;
2026  default:
2027  break;
2028  }
2029 
2030  /*
2031  * Prepare per-worker JIT instrumentation. As with the overall JIT
2032  * summary, this is printed only if printing costs is enabled.
2033  */
2034  if (es->workers_state && es->costs && es->verbose)
2035  {
2037 
2038  if (w)
2039  {
2040  for (int n = 0; n < w->num_workers; n++)
2041  {
2042  ExplainOpenWorker(n, es);
2043  ExplainPrintJIT(es, planstate->state->es_jit_flags,
2044  &w->jit_instr[n]);
2045  ExplainCloseWorker(n, es);
2046  }
2047  }
2048  }
2049 
2050  /* Show buffer/WAL usage */
2051  if (es->buffers && planstate->instrument)
2052  show_buffer_usage(es, &planstate->instrument->bufusage, false);
2053  if (es->wal && planstate->instrument)
2054  show_wal_usage(es, &planstate->instrument->walusage);
2055 
2056  /* Prepare per-worker buffer/WAL usage */
2057  if (es->workers_state && (es->buffers || es->wal) && es->verbose)
2058  {
2059  WorkerInstrumentation *w = planstate->worker_instrument;
2060 
2061  for (int n = 0; n < w->num_workers; n++)
2062  {
2063  Instrumentation *instrument = &w->instrument[n];
2064  double nloops = instrument->nloops;
2065 
2066  if (nloops <= 0)
2067  continue;
2068 
2069  ExplainOpenWorker(n, es);
2070  if (es->buffers)
2071  show_buffer_usage(es, &instrument->bufusage, false);
2072  if (es->wal)
2073  show_wal_usage(es, &instrument->walusage);
2074  ExplainCloseWorker(n, es);
2075  }
2076  }
2077 
2078  /* Show per-worker details for this plan node, then pop that stack */
2079  if (es->workers_state)
2081  es->workers_state = save_workers_state;
2082 
2083  /*
2084  * If partition pruning was done during executor initialization, the
2085  * number of child plans we'll display below will be less than the number
2086  * of subplans that was specified in the plan. To make this a bit less
2087  * mysterious, emit an indication that this happened. Note that this
2088  * field is emitted now because we want it to be a property of the parent
2089  * node; it *cannot* be emitted within the Plans sub-node we'll open next.
2090  */
2091  switch (nodeTag(plan))
2092  {
2093  case T_Append:
2094  ExplainMissingMembers(((AppendState *) planstate)->as_nplans,
2095  list_length(((Append *) plan)->appendplans),
2096  es);
2097  break;
2098  case T_MergeAppend:
2099  ExplainMissingMembers(((MergeAppendState *) planstate)->ms_nplans,
2100  list_length(((MergeAppend *) plan)->mergeplans),
2101  es);
2102  break;
2103  default:
2104  break;
2105  }
2106 
2107  /* Get ready to display the child plans */
2108  haschildren = planstate->initPlan ||
2109  outerPlanState(planstate) ||
2110  innerPlanState(planstate) ||
2111  IsA(plan, Append) ||
2112  IsA(plan, MergeAppend) ||
2113  IsA(plan, BitmapAnd) ||
2114  IsA(plan, BitmapOr) ||
2115  IsA(plan, SubqueryScan) ||
2116  (IsA(planstate, CustomScanState) &&
2117  ((CustomScanState *) planstate)->custom_ps != NIL) ||
2118  planstate->subPlan;
2119  if (haschildren)
2120  {
2121  ExplainOpenGroup("Plans", "Plans", false, es);
2122  /* Pass current Plan as head of ancestors list for children */
2123  ancestors = lcons(plan, ancestors);
2124  }
2125 
2126  /* initPlan-s */
2127  if (planstate->initPlan)
2128  ExplainSubPlans(planstate->initPlan, ancestors, "InitPlan", es);
2129 
2130  /* lefttree */
2131  if (outerPlanState(planstate))
2132  ExplainNode(outerPlanState(planstate), ancestors,
2133  "Outer", NULL, es);
2134 
2135  /* righttree */
2136  if (innerPlanState(planstate))
2137  ExplainNode(innerPlanState(planstate), ancestors,
2138  "Inner", NULL, es);
2139 
2140  /* special child plans */
2141  switch (nodeTag(plan))
2142  {
2143  case T_Append:
2144  ExplainMemberNodes(((AppendState *) planstate)->appendplans,
2145  ((AppendState *) planstate)->as_nplans,
2146  ancestors, es);
2147  break;
2148  case T_MergeAppend:
2149  ExplainMemberNodes(((MergeAppendState *) planstate)->mergeplans,
2150  ((MergeAppendState *) planstate)->ms_nplans,
2151  ancestors, es);
2152  break;
2153  case T_BitmapAnd:
2154  ExplainMemberNodes(((BitmapAndState *) planstate)->bitmapplans,
2155  ((BitmapAndState *) planstate)->nplans,
2156  ancestors, es);
2157  break;
2158  case T_BitmapOr:
2159  ExplainMemberNodes(((BitmapOrState *) planstate)->bitmapplans,
2160  ((BitmapOrState *) planstate)->nplans,
2161  ancestors, es);
2162  break;
2163  case T_SubqueryScan:
2164  ExplainNode(((SubqueryScanState *) planstate)->subplan, ancestors,
2165  "Subquery", NULL, es);
2166  break;
2167  case T_CustomScan:
2168  ExplainCustomChildren((CustomScanState *) planstate,
2169  ancestors, es);
2170  break;
2171  default:
2172  break;
2173  }
2174 
2175  /* subPlan-s */
2176  if (planstate->subPlan)
2177  ExplainSubPlans(planstate->subPlan, ancestors, "SubPlan", es);
2178 
2179  /* end of child plans */
2180  if (haschildren)
2181  {
2182  ancestors = list_delete_first(ancestors);
2183  ExplainCloseGroup("Plans", "Plans", false, es);
2184  }
2185 
2186  /* in text format, undo whatever indentation we added */
2187  if (es->format == EXPLAIN_FORMAT_TEXT)
2188  es->indent = save_indent;
2189 
2190  ExplainCloseGroup("Plan",
2191  relationship ? NULL : "Plan",
2192  true, es);
2193 }
#define NIL
Definition: pg_list.h:65
static ExplainWorkersState * ExplainCreateWorkersState(int num_workers)
Definition: explain.c:4102
Definition: nodes.h:81
void ExplainPropertyBool(const char *qlabel, bool value, ExplainState *es)
Definition: explain.c:4473
ScanDirection indexorderdir
Definition: plannodes.h:409
#define IsA(nodeptr, _type_)
Definition: nodes.h:590
Bitmapset * initParam
Definition: plannodes.h:950
WorkerInstrumentation * worker_instrument
Definition: execnodes.h:975
WalUsage walusage
Definition: instrument.h:75
void ExplainPropertyInteger(const char *qlabel, const char *unit, int64 value, ExplainState *es)
Definition: explain.c:4432
Definition: nodes.h:83
bool hide_workers
Definition: explain.h:59
Instrumentation * instrument
Definition: execnodes.h:974
const char * quote_identifier(const char *ident)
Definition: ruleutils.c:11323
static void show_modifytable_info(ModifyTableState *mtstate, List *ancestors, ExplainState *es)
Definition: explain.c:3845
#define castNode(_type_, nodeptr)
Definition: nodes.h:608
void ExplainPropertyFloat(const char *qlabel, const char *unit, double value, int ndigits, ExplainState *es)
Definition: explain.c:4459
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
double startup
Definition: instrument.h:67
Definition: nodes.h:539
Definition: nodes.h:49
static void show_incremental_sort_info(IncrementalSortState *incrsortstate, ExplainState *es)
Definition: explain.c:2920
static void ExplainOpenWorker(int n, ExplainState *es)
Definition: explain.c:4119
static void ExplainCloseWorker(int n, ExplainState *es)
Definition: explain.c:4181
static void ExplainPrintJIT(ExplainState *es, int jit_flags, JitInstrumentation *ji)
Definition: explain.c:873
Definition: nodes.h:78
EState * state
Definition: execnodes.h:966
Expr * make_orclause(List *orclauses)
Definition: makefuncs.c:652
bool costs
Definition: explain.h:43
JitInstrumentation jit_instr[FLEXIBLE_ARRAY_MEMBER]
Definition: jit.h:51
static void show_scan_qual(List *qual, const char *qlabel, PlanState *planstate, List *ancestors, ExplainState *es)
Definition: explain.c:2301
#define DO_AGGSPLIT_COMBINE(as)
Definition: nodes.h:801
bool analyze
Definition: explain.h:42
static void show_sort_info(SortState *sortstate, ExplainState *es)
Definition: explain.c:2715
Oid indexid
Definition: plannodes.h:403
static void show_upper_qual(List *qual, const char *qlabel, PlanState *planstate, List *ancestors, ExplainState *es)
Definition: explain.c:2315
static void show_instrumentation_count(const char *qlabel, int which, PlanState *planstate, ExplainState *es)
Definition: explain.c:3384
void InstrEndLoop(Instrumentation *instr)
Definition: instrument.c:121
const struct CustomExecMethods * methods
Definition: execnodes.h:1884
#define list_make1(x1)
Definition: pg_list.h:206
void ExplainPropertyText(const char *qlabel, const char *value, ExplainState *es)
Definition: explain.c:4423
static void ExplainNode(PlanState *planstate, List *ancestors, const char *relationship, const char *plan_name, ExplainState *es)
Definition: explain.c:1143
AggStrategy aggstrategy
Definition: plannodes.h:859
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
Definition: nodes.h:46
bool single_copy
Definition: plannodes.h:929
Definition: nodes.h:80
#define outerPlanState(node)
Definition: execnodes.h:1058
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
Expr * make_andclause(List *andclauses)
Definition: makefuncs.c:636
int es_jit_flags
Definition: execnodes.h:643
static void show_tablesample(TableSampleClause *tsc, PlanState *planstate, List *ancestors, ExplainState *es)
Definition: explain.c:2649
ScanDirection indexorderdir
Definition: plannodes.h:436
double ntuples
Definition: instrument.h:69
int indent
Definition: explain.h:51
static void show_wal_usage(ExplainState *es, const WalUsage *usage)
Definition: explain.c:3622
static void show_tidbitmap_info(BitmapHeapScanState *planstate, ExplainState *es)
Definition: explain.c:3354
static void ExplainModifyTarget(ModifyTable *plan, ExplainState *es)
Definition: explain.c:3712
static void ExplainMissingMembers(int nplans, int nchildren, ExplainState *es)
Definition: explain.c:4023
static void ExplainIndexScanDetails(Oid indexid, ScanDirection indexorderdir, ExplainState *es)
Definition: explain.c:3660
static void show_group_keys(GroupState *gstate, List *ancestors, ExplainState *es)
Definition: explain.c:2509
struct SharedJitInstrumentation * worker_jit_instrument
Definition: execnodes.h:978
static void show_plan_tlist(PlanState *planstate, List *ancestors, ExplainState *es)
Definition: explain.c:2199
List * lappend(List *list, void *datum)
Definition: list.c:336
bool timing
Definition: explain.h:46
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:188
Instrumentation instrument[FLEXIBLE_ARRAY_MEMBER]
Definition: instrument.h:81
bool verbose
Definition: explain.h:41
BufferUsage bufusage
Definition: instrument.h:74
static void ExplainCustomChildren(CustomScanState *css, List *ancestors, ExplainState *es)
Definition: explain.c:4080
Definition: nodes.h:86
Plan * plan
Definition: execnodes.h:964
int num_workers
Definition: plannodes.h:927
void(* ExplainCustomScan)(CustomScanState *node, List *ancestors, ExplainState *es)
Definition: extensible.h:151
static void show_merge_append_keys(MergeAppendState *mstate, List *ancestors, ExplainState *es)
Definition: explain.c:2361
static void ExplainIndentText(ExplainState *es)
Definition: explain.c:4860
List * lcons(void *datum, List *list)
Definition: list.c:468
#define lfirst(lc)
Definition: pg_list.h:169
AggSplit aggsplit
Definition: plannodes.h:860
static void ExplainSubPlans(List *plans, List *ancestors, const char *relationship, ExplainState *es)
Definition: explain.c:4037
bool wal
Definition: explain.h:45
static void show_hash_info(HashState *hashstate, ExplainState *es)
Definition: explain.c:3006
ExplainWorkersState * workers_state
Definition: explain.h:61
static void show_eval_params(Bitmapset *bms_params, ExplainState *es)
Definition: explain.c:3434
static void show_sort_keys(SortState *sortstate, List *ancestors, ExplainState *es)
Definition: explain.c:2329
static int list_length(const List *l)
Definition: pg_list.h:149
ExplainFormat format
Definition: explain.h:49
#define DO_AGGSPLIT_SKIPFINAL(as)
Definition: nodes.h:802
static const struct fns functions
Definition: regcomp.c:314
static const char * explain_get_index_name(Oid indexId)
Definition: explain.c:3464
double ntuples2
Definition: instrument.h:70
#define nodeTag(nodeptr)
Definition: nodes.h:544
static void show_expression(Node *node, const char *qlabel, PlanState *planstate, List *ancestors, bool useprefix, ExplainState *es)
Definition: explain.c:2257
Bitmapset * initParam
Definition: plannodes.h:931
static void show_incremental_sort_keys(IncrementalSortState *incrsortstate, List *ancestors, ExplainState *es)
Definition: explain.c:2344
static void show_buffer_usage(ExplainState *es, const BufferUsage *usage, bool planning)
Definition: explain.c:3486
Definition: nodes.h:87
static void show_resultcache_info(ResultCacheState *rcstate, List *ancestors, ExplainState *es)
Definition: explain.c:3097
int num_workers
Definition: plannodes.h:942
static void ExplainFlushWorkersState(ExplainState *es)
Definition: explain.c:4217
Definition: nodes.h:84
bool buffers
Definition: explain.h:44
static void show_foreignscan_info(ForeignScanState *fsstate, ExplainState *es)
Definition: explain.c:3413
static void ExplainScanTarget(Scan *plan, ExplainState *es)
Definition: explain.c:3699
void ExplainCloseGroup(const char *objtype, const char *labelname, bool labeled, ExplainState *es)
Definition: explain.c:4551
Definition: plannodes.h:856
#define innerPlanState(node)
Definition: execnodes.h:1057
void ExplainOpenGroup(const char *objtype, const char *labelname, bool labeled, ExplainState *es)
Definition: explain.c:4488
Definition: pg_list.h:50
StringInfo str
Definition: explain.h:39
static void show_agg_keys(AggState *astate, List *ancestors, ExplainState *es)
Definition: explain.c:2377
static void ExplainMemberNodes(PlanState **planstates, int nplans, List *ancestors, ExplainState *es)
Definition: explain.c:4005
List * list_delete_first(List *list)
Definition: list.c:875
static void show_hashagg_info(AggState *hashstate, ExplainState *es)
Definition: explain.c:3232
Definition: nodes.h:89

◆ ExplainOnePlan()

void ExplainOnePlan ( PlannedStmt plannedstmt,
IntoClause into,
ExplainState es,
const char *  queryString,
ParamListInfo  params,
QueryEnvironment queryEnv,
const instr_time planduration,
const BufferUsage bufusage 
)

Definition at line 524 of file explain.c.

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

Referenced by ExplainExecuteQuery(), and ExplainOneQuery().

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

◆ ExplainOneQuery()

static void ExplainOneQuery ( Query query,
int  cursorOptions,
IntoClause into,
ExplainState es,
const char *  queryString,
ParamListInfo  params,
QueryEnvironment queryEnv 
)
static

Definition at line 373 of file explain.c.

References BufferUsageAccumDiff(), CMD_UTILITY, Query::commandType, ExplainOnePlan(), ExplainOneQuery_hook, ExplainOneUtility(), INSTR_TIME_SET_CURRENT, INSTR_TIME_SUBTRACT, pg_plan_query(), pgBufferUsage, and Query::utilityStmt.

Referenced by ExplainOneUtility(), and ExplainQuery().

377 {
378  /* planner will not cope with utility statements */
379  if (query->commandType == CMD_UTILITY)
380  {
381  ExplainOneUtility(query->utilityStmt, into, es, queryString, params,
382  queryEnv);
383  return;
384  }
385 
386  /* if an advisor plugin is present, let it manage things */
388  (*ExplainOneQuery_hook) (query, cursorOptions, into, es,
389  queryString, params, queryEnv);
390  else
391  {
392  PlannedStmt *plan;
393  instr_time planstart,
394  planduration;
395  BufferUsage bufusage_start,
396  bufusage;
397 
398  if (es->buffers)
399  bufusage_start = pgBufferUsage;
400  INSTR_TIME_SET_CURRENT(planstart);
401 
402  /* plan the query */
403  plan = pg_plan_query(query, queryString, cursorOptions, params);
404 
405  INSTR_TIME_SET_CURRENT(planduration);
406  INSTR_TIME_SUBTRACT(planduration, planstart);
407 
408  /* calc differences of buffer counters. */
409  if (es->buffers)
410  {
411  memset(&bufusage, 0, sizeof(BufferUsage));
412  BufferUsageAccumDiff(&bufusage, &pgBufferUsage, &bufusage_start);
413  }
414 
415  /* run it (if needed) and produce output */
416  ExplainOnePlan(plan, into, es, queryString, params, queryEnv,
417  &planduration, (es->buffers ? &bufusage : NULL));
418  }
419 }
struct timeval instr_time
Definition: instr_time.h:150
Node * utilityStmt
Definition: parsenodes.h:128
void BufferUsageAccumDiff(BufferUsage *dst, const BufferUsage *add, const BufferUsage *sub)
Definition: instrument.c:225
#define INSTR_TIME_SUBTRACT(x, y)
Definition: instr_time.h:170
ExplainOneQuery_hook_type ExplainOneQuery_hook
Definition: explain.c:45
CmdType commandType
Definition: parsenodes.h:120
void ExplainOnePlan(PlannedStmt *plannedstmt, IntoClause *into, ExplainState *es, const char *queryString, ParamListInfo params, QueryEnvironment *queryEnv, const instr_time *planduration, const BufferUsage *bufusage)
Definition: explain.c:524
#define INSTR_TIME_SET_CURRENT(t)
Definition: instr_time.h:156
PlannedStmt * pg_plan_query(Query *querytree, const char *query_string, int cursorOptions, ParamListInfo boundParams)
Definition: postgres.c:811
void ExplainOneUtility(Node *utilityStmt, IntoClause *into, ExplainState *es, const char *queryString, ParamListInfo params, QueryEnvironment *queryEnv)
Definition: explain.c:433
BufferUsage pgBufferUsage
Definition: instrument.c:20

◆ ExplainOneUtility()

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

Definition at line 433 of file explain.c.

References appendStringInfoString(), Assert, castNode, copyObject, CreateTableAsRelExists(), CURSOR_OPT_PARALLEL_OK, elog, ERROR, EXPLAIN_FORMAT_TEXT, ExplainDummyGroup(), ExplainExecuteQuery(), ExplainOneQuery(), ExplainState::format, CreateTableAsStmt::into, IsA, linitial_node, list_length(), OBJECT_MATVIEW, OBJECT_TABLE, CreateTableAsStmt::objtype, DeclareCursorStmt::options, DeclareCursorStmt::query, CreateTableAsStmt::query, QueryRewrite(), and ExplainState::str.

Referenced by ExplainExecuteQuery(), and ExplainOneQuery().

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

◆ ExplainOpenGroup()

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

Definition at line 4488 of file explain.c.

References appendStringInfo(), appendStringInfoChar(), appendStringInfoSpaces(), appendStringInfoString(), escape_json(), EXPLAIN_FORMAT_JSON, EXPLAIN_FORMAT_TEXT, EXPLAIN_FORMAT_XML, EXPLAIN_FORMAT_YAML, ExplainJSONLineEnding(), ExplainXMLTag(), ExplainYAMLLineStarting(), ExplainState::format, ExplainState::grouping_stack, ExplainState::indent, lcons_int(), ExplainState::str, and X_OPENING.

Referenced by ExplainFlushWorkersState(), ExplainNode(), ExplainOnePlan(), ExplainPrintJIT(), ExplainPrintSettings(), ExplainPrintTriggers(), report_triggers(), show_grouping_set_keys(), show_grouping_sets(), show_incremental_sort_group_info(), and show_modifytable_info().

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

◆ ExplainOpenSetAsideGroup()

static void ExplainOpenSetAsideGroup ( const char *  objtype,
const char *  labelname,
bool  labeled,
int  depth,
ExplainState es 
)
static

Definition at line 4598 of file explain.c.

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

Referenced by ExplainOpenWorker().

4600 {
4601  switch (es->format)
4602  {
4603  case EXPLAIN_FORMAT_TEXT:
4604  /* nothing to do */
4605  break;
4606 
4607  case EXPLAIN_FORMAT_XML:
4608  es->indent += depth;
4609  break;
4610 
4611  case EXPLAIN_FORMAT_JSON:
4612  es->grouping_stack = lcons_int(0, es->grouping_stack);
4613  es->indent += depth;
4614  break;
4615 
4616  case EXPLAIN_FORMAT_YAML:
4617  if (labelname)
4618  es->grouping_stack = lcons_int(1, es->grouping_stack);
4619  else
4620  es->grouping_stack = lcons_int(0, es->grouping_stack);
4621  es->indent += depth;
4622  break;
4623  }
4624 }
List * lcons_int(int datum, List *list)
Definition: list.c:486
List * grouping_stack
Definition: explain.h:52
int indent
Definition: explain.h:51
ExplainFormat format
Definition: explain.h:49

◆ ExplainOpenWorker()

static void ExplainOpenWorker ( int  n,
ExplainState es 
)
static

Definition at line 4119 of file explain.c.

References appendStringInfo(), Assert, EXPLAIN_FORMAT_TEXT, ExplainIndentText(), ExplainOpenSetAsideGroup(), ExplainPropertyInteger(), ExplainRestoreGroup(), ExplainState::format, ExplainState::indent, initStringInfo(), StringInfoData::len, ExplainWorkersState::prev_str, ExplainState::str, ExplainWorkersState::worker_inited, ExplainWorkersState::worker_state_save, ExplainWorkersState::worker_str, and ExplainState::workers_state.

Referenced by ExplainNode(), show_hashagg_info(), show_incremental_sort_info(), show_resultcache_info(), and show_sort_info().

4120 {
4121  ExplainWorkersState *wstate = es->workers_state;
4122 
4123  Assert(wstate);
4124  Assert(n >= 0 && n < wstate->num_workers);
4125 
4126  /* Save prior output buffer pointer */
4127  wstate->prev_str = es->str;
4128 
4129  if (!wstate->worker_inited[n])
4130  {
4131  /* First time through, so create the buffer for this worker */
4132  initStringInfo(&wstate->worker_str[n]);
4133  es->str = &wstate->worker_str[n];
4134 
4135  /*
4136  * Push suitable initial formatting state for this worker's field
4137  * group. We allow one extra logical nesting level, since this group
4138  * will eventually be wrapped in an outer "Workers" group.
4139  */
4140  ExplainOpenSetAsideGroup("Worker", NULL, true, 2, es);
4141 
4142  /*
4143  * In non-TEXT formats we always emit a "Worker Number" field, even if
4144  * there's no other data for this worker.
4145  */
4146  if (es->format != EXPLAIN_FORMAT_TEXT)
4147  ExplainPropertyInteger("Worker Number", NULL, n, es);
4148 
4149  wstate->worker_inited[n] = true;
4150  }
4151  else
4152  {
4153  /* Resuming output for a worker we've already emitted some data for */
4154  es->str = &wstate->worker_str[n];
4155 
4156  /* Restore formatting state saved by last ExplainCloseWorker() */
4157  ExplainRestoreGroup(es, 2, &wstate->worker_state_save[n]);
4158  }
4159 
4160  /*
4161  * In TEXT format, prefix the first output line for this worker with
4162  * "Worker N:". Then, any additional lines should be indented one more
4163  * stop than the "Worker N" line is.
4164  */
4165  if (es->format == EXPLAIN_FORMAT_TEXT)
4166  {
4167  if (es->str->len == 0)
4168  {
4169  ExplainIndentText(es);
4170  appendStringInfo(es->str, "Worker %d: ", n);
4171  }
4172 
4173  es->indent++;
4174  }
4175 }
int * worker_state_save
Definition: explain.h:33
void ExplainPropertyInteger(const char *qlabel, const char *unit, int64 value, ExplainState *es)
Definition: explain.c:4432
static void ExplainRestoreGroup(ExplainState *es, int depth, int *state_save)
Definition: explain.c:4667
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
StringInfoData * worker_str
Definition: explain.h:32
int indent
Definition: explain.h:51
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
bool * worker_inited
Definition: explain.h:31
static void ExplainIndentText(ExplainState *es)
Definition: explain.c:4860
#define Assert(condition)
Definition: c.h:804
ExplainWorkersState * workers_state
Definition: explain.h:61
ExplainFormat format
Definition: explain.h:49
StringInfo prev_str
Definition: explain.h:34
StringInfo str
Definition: explain.h:39
static void ExplainOpenSetAsideGroup(const char *objtype, const char *labelname, bool labeled, int depth, ExplainState *es)
Definition: explain.c:4598

◆ ExplainPreScanNode()

static bool ExplainPreScanNode ( PlanState planstate,
Bitmapset **  rels_used 
)
static

Definition at line 1068 of file explain.c.

References bms_add_member(), bms_add_members(), nodeTag, PlanState::plan, planstate_tree_walker(), T_Append, T_BitmapHeapScan, T_CteScan, T_CustomScan, T_ForeignScan, T_FunctionScan, T_IndexOnlyScan, T_IndexScan, T_MergeAppend, T_ModifyTable, T_NamedTuplestoreScan, T_SampleScan, T_SeqScan, T_SubqueryScan, T_TableFuncScan, T_TidRangeScan, T_TidScan, T_ValuesScan, and T_WorkTableScan.

Referenced by ExplainPrintPlan().

1069 {
1070  Plan *plan = planstate->plan;
1071 
1072  switch (nodeTag(plan))
1073  {
1074  case T_SeqScan:
1075  case T_SampleScan:
1076  case T_IndexScan:
1077  case T_IndexOnlyScan:
1078  case T_BitmapHeapScan:
1079  case T_TidScan:
1080  case T_TidRangeScan:
1081  case T_SubqueryScan:
1082  case T_FunctionScan:
1083  case T_TableFuncScan:
1084  case T_ValuesScan:
1085  case T_CteScan:
1086  case T_NamedTuplestoreScan:
1087  case T_WorkTableScan:
1088  *rels_used = bms_add_member(*rels_used,
1089  ((Scan *) plan)->scanrelid);
1090  break;
1091  case T_ForeignScan:
1092  *rels_used = bms_add_members(*rels_used,
1093  ((ForeignScan *) plan)->fs_relids);
1094  break;
1095  case T_CustomScan:
1096  *rels_used = bms_add_members(*rels_used,
1097  ((CustomScan *) plan)->custom_relids);
1098  break;
1099  case T_ModifyTable:
1100  *rels_used = bms_add_member(*rels_used,
1101  ((ModifyTable *) plan)->nominalRelation);
1102  if (((ModifyTable *) plan)->exclRelRTI)
1103  *rels_used = bms_add_member(*rels_used,
1104  ((ModifyTable *) plan)->exclRelRTI);
1105  break;
1106  case T_Append:
1107  *rels_used = bms_add_members(*rels_used,
1108  ((Append *) plan)->apprelids);
1109  break;
1110  case T_MergeAppend:
1111  *rels_used = bms_add_members(*rels_used,
1112  ((MergeAppend *) plan)->apprelids);
1113  break;
1114  default:
1115  break;
1116  }
1117 
1118  return planstate_tree_walker(planstate, ExplainPreScanNode, rels_used);
1119 }
Definition: nodes.h:49
Plan * plan
Definition: execnodes.h:964
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:736
#define nodeTag(nodeptr)
Definition: nodes.h:544
static bool ExplainPreScanNode(PlanState *planstate, Bitmapset **rels_used)
Definition: explain.c:1068
bool planstate_tree_walker(PlanState *planstate, bool(*walker)(), void *context)
Definition: nodeFuncs.c:3993
Bitmapset * bms_add_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:793

◆ ExplainPrintJIT()

static void ExplainPrintJIT ( ExplainState es,
int  jit_flags,
JitInstrumentation ji 
)
static

Definition at line 873 of file explain.c.

References ExplainState::analyze, appendStringInfo(), appendStringInfoString(), JitInstrumentation::created_functions, JitInstrumentation::emission_counter, EXPLAIN_FORMAT_TEXT, ExplainCloseGroup(), ExplainIndentText(), ExplainOpenGroup(), ExplainPropertyBool(), ExplainPropertyFloat(), ExplainPropertyInteger(), ExplainState::format, JitInstrumentation::generation_counter, ExplainState::indent, JitInstrumentation::inlining_counter, INSTR_TIME_ADD, INSTR_TIME_GET_DOUBLE, INSTR_TIME_SET_ZERO, JitInstrumentation::optimization_counter, PGJIT_DEFORM, PGJIT_EXPR, PGJIT_INLINE, PGJIT_OPT3, ExplainState::str, and ExplainState::timing.

Referenced by ExplainNode(), and ExplainPrintJITSummary().

874 {
875  instr_time total_time;
876 
877  /* don't print information if no JITing happened */
878  if (!ji || ji->created_functions == 0)
879  return;
880 
881  /* calculate total time */
882  INSTR_TIME_SET_ZERO(total_time);
883  INSTR_TIME_ADD(total_time, ji->generation_counter);
884  INSTR_TIME_ADD(total_time, ji->inlining_counter);
885  INSTR_TIME_ADD(total_time, ji->optimization_counter);
886  INSTR_TIME_ADD(total_time, ji->emission_counter);
887 
888  ExplainOpenGroup("JIT", "JIT", true, es);
889 
890  /* for higher density, open code the text output format */
891  if (es->format == EXPLAIN_FORMAT_TEXT)
892  {
893  ExplainIndentText(es);
894  appendStringInfoString(es->str, "JIT:\n");
895  es->indent++;
896 
897  ExplainPropertyInteger("Functions", NULL, ji->created_functions, es);
898 
899  ExplainIndentText(es);
900  appendStringInfo(es->str, "Options: %s %s, %s %s, %s %s, %s %s\n",
901  "Inlining", jit_flags & PGJIT_INLINE ? "true" : "false",
902  "Optimization", jit_flags & PGJIT_OPT3 ? "true" : "false",
903  "Expressions", jit_flags & PGJIT_EXPR ? "true" : "false",
904  "Deforming", jit_flags & PGJIT_DEFORM ? "true" : "false");
905 
906  if (es->analyze && es->timing)
907  {
908  ExplainIndentText(es);
909  appendStringInfo(es->str,
910  "Timing: %s %.3f ms, %s %.3f ms, %s %.3f ms, %s %.3f ms, %s %.3f ms\n",
911  "Generation", 1000.0 * INSTR_TIME_GET_DOUBLE(ji->generation_counter),
912  "Inlining", 1000.0 * INSTR_TIME_GET_DOUBLE(ji->inlining_counter),
913  "Optimization", 1000.0 * INSTR_TIME_GET_DOUBLE(ji->optimization_counter),
914  "Emission", 1000.0 * INSTR_TIME_GET_DOUBLE(ji->emission_counter),
915  "Total", 1000.0 * INSTR_TIME_GET_DOUBLE(total_time));
916  }
917 
918  es->indent--;
919  }
920  else
921  {
922  ExplainPropertyInteger("Functions", NULL, ji->created_functions, es);
923 
924  ExplainOpenGroup("Options", "Options", true, es);
925  ExplainPropertyBool("Inlining", jit_flags & PGJIT_INLINE, es);
926  ExplainPropertyBool("Optimization", jit_flags & PGJIT_OPT3, es);
927  ExplainPropertyBool("Expressions", jit_flags & PGJIT_EXPR, es);
928  ExplainPropertyBool("Deforming", jit_flags & PGJIT_DEFORM, es);
929  ExplainCloseGroup("Options", "Options", true, es);
930 
931  if (es->analyze && es->timing)
932  {
933  ExplainOpenGroup("Timing", "Timing", true, es);
934 
935  ExplainPropertyFloat("Generation", "ms",
937  3, es);
938  ExplainPropertyFloat("Inlining", "ms",
940  3, es);
941  ExplainPropertyFloat("Optimization", "ms",
943  3, es);
944  ExplainPropertyFloat("Emission", "ms",
946  3, es);
947  ExplainPropertyFloat("Total", "ms",
948  1000.0 * INSTR_TIME_GET_DOUBLE(total_time),
949  3, es);
950 
951  ExplainCloseGroup("Timing", "Timing", true, es);
952  }
953  }
954 
955  ExplainCloseGroup("JIT", "JIT", true, es);
956 }
void ExplainPropertyBool(const char *qlabel, bool value, ExplainState *es)
Definition: explain.c:4473
#define PGJIT_EXPR
Definition: jit.h:23
void ExplainPropertyInteger(const char *qlabel, const char *unit, int64 value, ExplainState *es)
Definition: explain.c:4432
instr_time generation_counter
Definition: jit.h:33
void ExplainPropertyFloat(const char *qlabel, const char *unit, double value, int ndigits, ExplainState *es)
Definition: explain.c:4459
struct timeval instr_time
Definition: instr_time.h:150
#define INSTR_TIME_SET_ZERO(t)
Definition: instr_time.h:154
#define INSTR_TIME_GET_DOUBLE(t)
Definition: instr_time.h:199
bool analyze
Definition: explain.h:42
#define PGJIT_OPT3
Definition: jit.h:21
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
size_t created_functions
Definition: jit.h:30
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
#define INSTR_TIME_ADD(x, y)
Definition: instr_time.h:158
instr_time optimization_counter
Definition: jit.h:39
int indent
Definition: explain.h:51
bool timing
Definition: explain.h:46
instr_time inlining_counter
Definition: jit.h:36
#define PGJIT_INLINE
Definition: jit.h:22
static void ExplainIndentText(ExplainState *es)
Definition: explain.c:4860
instr_time emission_counter
Definition: jit.h:42
ExplainFormat format
Definition: explain.h:49
void ExplainCloseGroup(const char *objtype, const char *labelname, bool labeled, ExplainState *es)
Definition: explain.c:4551
#define PGJIT_DEFORM
Definition: jit.h:24
void ExplainOpenGroup(const char *objtype, const char *labelname, bool labeled, ExplainState *es)
Definition: explain.c:4488
StringInfo str
Definition: explain.h:39

◆ ExplainPrintJITSummary()

void ExplainPrintJITSummary ( ExplainState es,
QueryDesc queryDesc 
)

Definition at line 847 of file explain.c.

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

Referenced by explain_ExecutorEnd(), and ExplainOnePlan().

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

◆ ExplainPrintPlan()

void ExplainPrintPlan ( ExplainState es,
QueryDesc queryDesc 
)

Definition at line 755 of file explain.c.

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

Referenced by explain_ExecutorEnd(), and ExplainOnePlan().

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

◆ ExplainPrintSettings()

static void ExplainPrintSettings ( ExplainState es)
static

Definition at line 685 of file explain.c.

References appendStringInfo(), appendStringInfoString(), StringInfoData::data, EXPLAIN_FORMAT_TEXT, ExplainCloseGroup(), ExplainOpenGroup(), ExplainPropertyText(), ExplainState::format, get_explain_guc_options(), GetConfigOptionByName(), i, initStringInfo(), config_generic::name, ExplainState::settings, and generate_unaccent_rules::str.

Referenced by ExplainPrintPlan().

686 {
687  int num;
688  struct config_generic **gucs;
689 
690  /* bail out if information about settings not requested */
691  if (!es->settings)
692  return;
693 
694  /* request an array of relevant settings */
695  gucs = get_explain_guc_options(&num);
696 
697  if (es->format != EXPLAIN_FORMAT_TEXT)
698  {
699  ExplainOpenGroup("Settings", "Settings", true, es);
700 
701  for (int i = 0; i < num; i++)
702  {
703  char *setting;
704  struct config_generic *conf = gucs[i];
705 
706  setting = GetConfigOptionByName(conf->name, NULL, true);
707 
708  ExplainPropertyText(conf->name, setting, es);
709  }
710 
711  ExplainCloseGroup("Settings", "Settings", true, es);
712  }
713  else
714  {
716 
717  /* In TEXT mode, print nothing if there are no options */
718  if (num <= 0)
719  return;
720 
721  initStringInfo(&str);
722 
723  for (int i = 0; i < num; i++)
724  {
725  char *setting;
726  struct config_generic *conf = gucs[i];
727 
728  if (i > 0)
729  appendStringInfoString(&str, ", ");
730 
731  setting = GetConfigOptionByName(conf->name, NULL, true);
732 
733  if (setting)
734  appendStringInfo(&str, "%s = '%s'", conf->name, setting);
735  else
736  appendStringInfo(&str, "%s = NULL", conf->name);
737  }
738 
739  ExplainPropertyText("Settings", str.data, es);
740  }
741 }
const char * name
Definition: guc_tables.h:149
struct config_generic ** get_explain_guc_options(int *num)
Definition: guc.c:9475
void ExplainPropertyText(const char *qlabel, const char *value, ExplainState *es)
Definition: explain.c:4423
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
char * GetConfigOptionByName(const char *name, const char **varname, bool missing_ok)
Definition: guc.c:9568
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
ExplainFormat format
Definition: explain.h:49
int i
void ExplainCloseGroup(const char *objtype, const char *labelname, bool labeled, ExplainState *es)
Definition: explain.c:4551
void ExplainOpenGroup(const char *objtype, const char *labelname, bool labeled, ExplainState *es)
Definition: explain.c:4488
bool settings
Definition: explain.h:48

◆ ExplainPrintTriggers()

void ExplainPrintTriggers ( ExplainState es,
QueryDesc queryDesc 
)

Definition at line 804 of file explain.c.

References EState::es_opened_result_relations, EState::es_trig_target_relations, EState::es_tuple_routing_result_relations, QueryDesc::estate, ExplainCloseGroup(), ExplainOpenGroup(), lfirst, list_length(), NIL, and report_triggers().

Referenced by explain_ExecutorEnd(), and ExplainOnePlan().

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

◆ ExplainProperty()

static void ExplainProperty ( const char *  qlabel,
const char *  unit,
const char *  value,
bool  numeric,
ExplainState es 
)
static

Definition at line 4370 of file explain.c.

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

Referenced by ExplainPropertyBool(), ExplainPropertyFloat(), ExplainPropertyInteger(), ExplainPropertyText(), and ExplainPropertyUInteger().

4372 {
4373  switch (es->format)
4374  {
4375  case EXPLAIN_FORMAT_TEXT:
4376  ExplainIndentText(es);
4377  if (unit)
4378  appendStringInfo(es->str, "%s: %s %s\n", qlabel, value, unit);
4379  else
4380  appendStringInfo(es->str, "%s: %s\n", qlabel, value);
4381  break;
4382 
4383  case EXPLAIN_FORMAT_XML:
4384  {
4385  char *str;
4386 
4387  appendStringInfoSpaces(es->str, es->indent * 2);
4388  ExplainXMLTag(qlabel, X_OPENING | X_NOWHITESPACE, es);
4389  str = escape_xml(value);
4390  appendStringInfoString(es->str, str);
4391  pfree(str);
4392  ExplainXMLTag(qlabel, X_CLOSING | X_NOWHITESPACE, es);
4393  appendStringInfoChar(es->str, '\n');
4394  }
4395  break;
4396 
4397  case EXPLAIN_FORMAT_JSON:
4399  appendStringInfoSpaces(es->str, es->indent * 2);
4400  escape_json(es->str, qlabel);
4401  appendStringInfoString(es->str, ": ");
4402  if (numeric)
4404  else
4405  escape_json(es->str, value);
4406  break;
4407 
4408  case EXPLAIN_FORMAT_YAML:
4410  appendStringInfo(es->str, "%s: ", qlabel);
4411  if (numeric)
4413  else
4414  escape_yaml(es->str, value);
4415  break;
4416  }
4417 }
void escape_json(StringInfo buf, const char *str)
Definition: json.c:1279
static struct @142 value
static void ExplainXMLTag(const char *tagname, int flags, ExplainState *es)
Definition: explain.c:4833
#define X_CLOSING
Definition: explain.c:53
void pfree(void *pointer)
Definition: mcxt.c:1169
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
static void ExplainYAMLLineStarting(ExplainState *es)
Definition: explain.c:4895
int indent
Definition: explain.h:51
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:188
static void escape_yaml(StringInfo buf, const char *str)
Definition: explain.c:4920
void appendStringInfoSpaces(StringInfo str, int count)
Definition: stringinfo.c:206
static void ExplainJSONLineEnding(ExplainState *es)
Definition: explain.c:4875
#define X_OPENING
Definition: explain.c:52
static void ExplainIndentText(ExplainState *es)
Definition: explain.c:4860
#define X_NOWHITESPACE
Definition: explain.c:55
ExplainFormat format
Definition: explain.h:49
char * escape_xml(const char *str)
Definition: xml.c:2353
StringInfo str
Definition: explain.h:39

◆ ExplainPropertyBool()

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

Definition at line 4473 of file explain.c.

References ExplainProperty().

Referenced by ExplainNode(), and ExplainPrintJIT().

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

◆ ExplainPropertyFloat()

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

Definition at line 4459 of file explain.c.

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

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

4461 {
4462  char *buf;
4463 
4464  buf = psprintf("%.*f", ndigits, value);
4465  ExplainProperty(qlabel, unit, buf, true, es);
4466  pfree(buf);
4467 }
static struct @142 value
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
void pfree(void *pointer)
Definition: mcxt.c:1169
static char * buf
Definition: pg_test_fsync.c:68
static void ExplainProperty(const char *qlabel, const char *unit, const char *value, bool numeric, ExplainState *es)
Definition: explain.c:4370

◆ ExplainPropertyInteger()

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

Definition at line 4432 of file explain.c.

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

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

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

◆ ExplainPropertyList()

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

Definition at line 4247 of file explain.c.

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

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

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

◆ ExplainPropertyListNested()

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

Definition at line 4317 of file explain.c.

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

Referenced by show_grouping_set_keys().

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

◆ ExplainPropertyText()

void ExplainPropertyText ( const char *  qlabel,
const char *  value,
ExplainState es 
)

Definition at line 4423 of file explain.c.

References ExplainProperty().

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

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

◆ ExplainPropertyUInteger()

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

Definition at line 4445 of file explain.c.

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

Referenced by show_wal_usage().

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

◆ ExplainQuery()

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

Definition at line 164 of file explain.c.

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

Referenced by standard_ProcessUtility().

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

◆ ExplainQueryText()

void ExplainQueryText ( ExplainState es,
QueryDesc queryDesc 
)

Definition at line 967 of file explain.c.

References ExplainPropertyText(), and QueryDesc::sourceText.

Referenced by explain_ExecutorEnd().

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

◆ ExplainRestoreGroup()

static void ExplainRestoreGroup ( ExplainState es,
int  depth,
int *  state_save 
)
static

Definition at line 4667 of file explain.c.

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

Referenced by ExplainOpenWorker().

4668 {
4669  switch (es->format)
4670  {
4671  case EXPLAIN_FORMAT_TEXT:
4672  /* nothing to do */
4673  break;
4674 
4675  case EXPLAIN_FORMAT_XML:
4676  es->indent += depth;
4677  break;
4678 
4679  case EXPLAIN_FORMAT_JSON:
4680  es->grouping_stack = lcons_int(*state_save, es->grouping_stack);
4681  es->indent += depth;
4682  break;
4683 
4684  case EXPLAIN_FORMAT_YAML:
4685  es->grouping_stack = lcons_int(*state_save, es->grouping_stack);
4686  es->indent += depth;
4687  break;
4688  }
4689 }
List * lcons_int(int datum, List *list)
Definition: list.c:486
List * grouping_stack
Definition: explain.h:52
int indent
Definition: explain.h:51
ExplainFormat format
Definition: explain.h:49

◆ ExplainResultDesc()

TupleDesc ExplainResultDesc ( ExplainStmt stmt)

Definition at line 334 of file explain.c.

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

Referenced by ExplainQuery(), and UtilityTupleDescriptor().

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

◆ ExplainSaveGroup()

static void ExplainSaveGroup ( ExplainState es,
int  depth,
int *  state_save 
)
static

Definition at line 4637 of file explain.c.

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

Referenced by ExplainCloseWorker().

4638 {
4639  switch (es->format)
4640  {
4641  case EXPLAIN_FORMAT_TEXT:
4642  /* nothing to do */
4643  break;
4644 
4645  case EXPLAIN_FORMAT_XML:
4646  es->indent -= depth;
4647  break;
4648 
4649  case EXPLAIN_FORMAT_JSON:
4650  es->indent -= depth;
4651  *state_save = linitial_int(es->grouping_stack);
4653  break;
4654 
4655  case EXPLAIN_FORMAT_YAML:
4656  es->indent -= depth;
4657  *state_save = linitial_int(es->grouping_stack);
4659  break;
4660  }
4661 }
#define linitial_int(l)
Definition: pg_list.h:175
List * grouping_stack
Definition: explain.h:52
int indent
Definition: explain.h:51
ExplainFormat format
Definition: explain.h:49
List * list_delete_first(List *list)
Definition: list.c:875

◆ ExplainScanTarget()

static void ExplainScanTarget ( Scan plan,
ExplainState es 
)
static

Definition at line 3699 of file explain.c.

References ExplainTargetRel().

Referenced by ExplainNode().

3700 {
3701  ExplainTargetRel((Plan *) plan, plan->scanrelid, es);
3702 }
static void ExplainTargetRel(Plan *plan, Index rti, ExplainState *es)
Definition: explain.c:3721

◆ ExplainSeparatePlans()

void ExplainSeparatePlans ( ExplainState es)

Definition at line 4804 of file explain.c.

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

Referenced by ExplainExecuteQuery(), and ExplainQuery().

4805 {
4806  switch (es->format)
4807  {
4808  case EXPLAIN_FORMAT_TEXT:
4809  /* add a blank line */
4810  appendStringInfoChar(es->str, '\n');
4811  break;
4812 
4813  case EXPLAIN_FORMAT_XML:
4814  case EXPLAIN_FORMAT_JSON:
4815  case EXPLAIN_FORMAT_YAML:
4816  /* nothing to do */
4817  break;
4818  }
4819 }
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:188
ExplainFormat format
Definition: explain.h:49
StringInfo str
Definition: explain.h:39

◆ ExplainSubPlans()

static void ExplainSubPlans ( List plans,
List ancestors,
const char *  relationship,
ExplainState es 
)
static

Definition at line 4037 of file explain.c.

References bms_add_member(), bms_is_member(), ExplainNode(), lcons(), lfirst, list_delete_first(), SubPlan::plan_id, SubPlan::plan_name, SubPlanState::planstate, ExplainState::printed_subplans, and SubPlanState::subplan.

Referenced by ExplainNode().

4039 {
4040  ListCell *lst;
4041 
4042  foreach(lst, plans)
4043  {
4044  SubPlanState *sps = (SubPlanState *) lfirst(lst);
4045  SubPlan *sp = sps->subplan;
4046 
4047  /*
4048  * There can be multiple SubPlan nodes referencing the same physical
4049  * subplan (same plan_id, which is its index in PlannedStmt.subplans).
4050  * We should print a subplan only once, so track which ones we already
4051  * printed. This state must be global across the plan tree, since the
4052  * duplicate nodes could be in different plan nodes, eg both a bitmap
4053  * indexscan's indexqual and its parent heapscan's recheck qual. (We
4054  * do not worry too much about which plan node we show the subplan as
4055  * attached to in such cases.)
4056  */
4057  if (bms_is_member(sp->plan_id, es->printed_subplans))
4058  continue;
4060  sp->plan_id);
4061 
4062  /*
4063  * Treat the SubPlan node as an ancestor of the plan node(s) within
4064  * it, so that ruleutils.c can find the referents of subplan
4065  * parameters.
4066  */
4067  ancestors = lcons(sp, ancestors);
4068 
4069  ExplainNode(sps->planstate, ancestors,
4070  relationship, sp->plan_name, es);
4071 
4072  ancestors = list_delete_first(ancestors);
4073  }
4074 }
int plan_id
Definition: primnodes.h:742
Bitmapset * printed_subplans
Definition: explain.h:58
static void ExplainNode(PlanState *planstate, List *ancestors, const char *relationship, const char *plan_name, ExplainState *es)
Definition: explain.c:1143
struct PlanState * planstate
Definition: execnodes.h:882
SubPlan * subplan
Definition: execnodes.h:881
List * lcons(void *datum, List *list)
Definition: list.c:468
char * plan_name
Definition: primnodes.h:744
#define lfirst(lc)
Definition: pg_list.h:169
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:736
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:427
List * list_delete_first(List *list)
Definition: list.c:875

◆ ExplainTargetRel()

static void ExplainTargetRel ( Plan plan,
Index  rti,
ExplainState es 
)
static

Definition at line 3721 of file explain.c.

References Alias::aliasname, appendStringInfo(), appendStringInfoString(), Assert, RangeTblEntry::eref, EXPLAIN_FORMAT_TEXT, ExplainPropertyText(), ExplainState::format, RangeTblFunction::funcexpr, FuncExpr::funcid, FunctionScan::functions, get_func_name(), get_namespace_name(), get_rel_name(), IsA, linitial, list_length(), list_nth(), nodeTag, quote_identifier(), RangeTblEntry::relid, rt_fetch, ExplainState::rtable, ExplainState::rtable_names, RTE_CTE, RTE_FUNCTION, RTE_NAMEDTUPLESTORE, RTE_RELATION, RTE_TABLEFUNC, RTE_VALUES, RangeTblEntry::rtekind, ExplainState::str, T_BitmapHeapScan, T_CteScan, T_CustomScan, T_ForeignScan, T_FunctionScan, T_IndexOnlyScan, T_IndexScan, T_ModifyTable, T_NamedTuplestoreScan, T_SampleScan, T_SeqScan, T_TableFuncScan, T_TidRangeScan, T_TidScan, T_ValuesScan, T_WorkTableScan, and ExplainState::verbose.

Referenced by ExplainModifyTarget(), ExplainScanTarget(), and show_modifytable_info().

3722 {
3723  char *objectname = NULL;
3724  char *namespace = NULL;
3725  const char *objecttag = NULL;
3726  RangeTblEntry *rte;
3727  char *refname;
3728 
3729  rte = rt_fetch(rti, es->rtable);
3730  refname = (char *) list_nth(es->rtable_names, rti - 1);
3731  if (refname == NULL)
3732  refname = rte->eref->aliasname;
3733 
3734  switch (nodeTag(plan))
3735  {
3736  case T_SeqScan:
3737  case T_SampleScan:
3738  case T_IndexScan:
3739  case T_IndexOnlyScan:
3740  case T_BitmapHeapScan:
3741  case T_TidScan:
3742  case T_TidRangeScan:
3743  case T_ForeignScan:
3744  case T_CustomScan:
3745  case T_ModifyTable:
3746  /* Assert it's on a real relation */
3747  Assert(rte->rtekind == RTE_RELATION);
3748  objectname = get_rel_name(rte->relid);
3749  if (es->verbose)
3750  namespace = get_namespace_name(get_rel_namespace(rte->relid));
3751  objecttag = "Relation Name";
3752  break;
3753  case T_FunctionScan:
3754  {
3755  FunctionScan *fscan = (FunctionScan *) plan;
3756 
3757  /* Assert it's on a RangeFunction */
3758  Assert(rte->rtekind == RTE_FUNCTION);
3759 
3760  /*
3761  * If the expression is still a function call of a single
3762  * function, we can get the real name of the function.
3763  * Otherwise, punt. (Even if it was a single function call
3764  * originally, the optimizer could have simplified it away.)
3765  */
3766  if (list_length(fscan->functions) == 1)
3767  {
3768  RangeTblFunction *rtfunc = (RangeTblFunction *) linitial(fscan->functions);
3769 
3770  if (IsA(rtfunc->funcexpr, FuncExpr))
3771  {
3772  FuncExpr *funcexpr = (FuncExpr *) rtfunc->funcexpr;
3773  Oid funcid = funcexpr->funcid;
3774 
3775  objectname = get_func_name(funcid);
3776  if (es->verbose)
3777  namespace =
3779  }
3780  }
3781  objecttag = "Function Name";
3782  }
3783  break;
3784  case T_TableFuncScan:
3785  Assert(rte->rtekind == RTE_TABLEFUNC);
3786  objectname = "xmltable";
3787  objecttag = "Table Function Name";
3788  break;
3789  case T_ValuesScan:
3790  Assert(rte->rtekind == RTE_VALUES);
3791  break;
3792  case T_CteScan:
3793  /* Assert it's on a non-self-reference CTE */
3794  Assert(rte->rtekind == RTE_CTE);
3795  Assert(!rte->self_reference);
3796  objectname = rte->ctename;
3797  objecttag = "CTE Name";
3798  break;
3799  case T_NamedTuplestoreScan:
3801  objectname = rte->enrname;
3802  objecttag = "Tuplestore Name";
3803  break;
3804  case T_WorkTableScan:
3805  /* Assert it's on a self-reference CTE */
3806  Assert(rte->rtekind == RTE_CTE);
3807  Assert(rte->self_reference);
3808  objectname = rte->ctename;
3809  objecttag = "CTE Name";
3810  break;
3811  default:
3812  break;
3813  }
3814 
3815  if (es->format == EXPLAIN_FORMAT_TEXT)
3816  {
3817  appendStringInfoString(es->str, " on");
3818  if (namespace != NULL)
3819  appendStringInfo(es->str, " %s.%s", quote_identifier(namespace),
3820  quote_identifier(objectname));
3821  else if (objectname != NULL)
3822  appendStringInfo(es->str, " %s", quote_identifier(objectname));
3823  if (objectname == NULL || strcmp(refname, objectname) != 0)
3824  appendStringInfo(es->str, " %s", quote_identifier(refname));
3825  }
3826  else
3827  {
3828  if (objecttag != NULL && objectname != NULL)
3829  ExplainPropertyText(objecttag, objectname, es);
3830  if (namespace != NULL)
3831  ExplainPropertyText("Schema", namespace, es);
3832  ExplainPropertyText("Alias", refname, es);
3833  }
3834 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:590
const char * quote_identifier(const char *ident)
Definition: ruleutils.c:11323
Oid get_func_namespace(Oid funcid)
Definition: lsyscache.c:1603
List * functions
Definition: plannodes.h:536
Oid get_rel_namespace(Oid relid)
Definition: lsyscache.c:1923
unsigned int Oid
Definition: postgres_ext.h:31
List * rtable_names
Definition: explain.h:56
void ExplainPropertyText(const char *qlabel, const char *value, ExplainState *es)
Definition: explain.c:4423
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
#define linitial(l)
Definition: pg_list.h:174
Oid funcid
Definition: primnodes.h:495
static void * list_nth(const List *list, int n)
Definition: pg_list.h:278
char * get_func_name(Oid funcid)
Definition: lsyscache.c:1579
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3316
char * enrname
Definition: parsenodes.h:1134
#define rt_fetch(rangetable_index, rangetable)
Definition: parsetree.h:31
bool self_reference
Definition: parsenodes.h:1107
bool verbose
Definition: explain.h:41
#define Assert(condition)
Definition: c.h:804
char * aliasname
Definition: primnodes.h:42
static int list_length(const List *l)
Definition: pg_list.h:149
ExplainFormat format
Definition: explain.h:49
#define nodeTag(nodeptr)
Definition: nodes.h:544
RTEKind rtekind
Definition: parsenodes.h:995
char * ctename
Definition: parsenodes.h:1105
Alias * eref
Definition: parsenodes.h:1141
char * get_rel_name(Oid relid)
Definition: lsyscache.c:1899
StringInfo str
Definition: explain.h:39
List * rtable
Definition: explain.h:55

◆ ExplainXMLTag()

static void ExplainXMLTag ( const char *  tagname,
int  flags,
ExplainState es 
)
static

Definition at line 4833 of file explain.c.

References appendStringInfoChar(), appendStringInfoCharMacro, appendStringInfoSpaces(), appendStringInfoString(), ExplainState::indent, ExplainState::str, X_CLOSE_IMMEDIATE, X_CLOSING, and X_NOWHITESPACE.

Referenced by ExplainCloseGroup(), ExplainDummyGroup(), ExplainOpenGroup(), ExplainProperty(), and ExplainPropertyList().

4834 {
4835  const char *s;
4836  const char *valid = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.";
4837 
4838  if ((flags & X_NOWHITESPACE) == 0)
4839  appendStringInfoSpaces(es->str, 2 * es->indent);
4840  appendStringInfoCharMacro(es->str, '<');
4841  if ((flags & X_CLOSING) != 0)
4842  appendStringInfoCharMacro(es->str, '/');
4843  for (s = tagname; *s; s++)
4844  appendStringInfoChar(es->str, strchr(valid, *s) ? *s : '-');
4845  if ((flags & X_CLOSE_IMMEDIATE) != 0)
4846  appendStringInfoString(es->str, " /");
4847  appendStringInfoCharMacro(es->str, '>');
4848  if ((flags & X_NOWHITESPACE) == 0)
4849  appendStringInfoCharMacro(es->str, '\n');
4850 }
#define X_CLOSING
Definition: explain.c:53
#define appendStringInfoCharMacro(str, ch)
Definition: stringinfo.h:128
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
int indent
Definition: explain.h:51
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:188
void appendStringInfoSpaces(StringInfo str, int count)
Definition: stringinfo.c:206
#define X_NOWHITESPACE
Definition: explain.c:55
#define X_CLOSE_IMMEDIATE
Definition: explain.c:54
StringInfo str
Definition: explain.h:39

◆ ExplainYAMLLineStarting()

static void ExplainYAMLLineStarting ( ExplainState es)
static

Definition at line 4895 of file explain.c.

References appendStringInfoChar(), appendStringInfoSpaces(), Assert, EXPLAIN_FORMAT_YAML, ExplainState::format, ExplainState::grouping_stack, ExplainState::indent, linitial_int, and ExplainState::str.

Referenced by ExplainDummyGroup(), ExplainOpenGroup(), ExplainProperty(), ExplainPropertyList(), and ExplainPropertyListNested().

4896 {
4898  if (linitial_int(es->grouping_stack) == 0)
4899  {
4900  linitial_int(es->grouping_stack) = 1;
4901  }
4902  else
4903  {
4904  appendStringInfoChar(es->str, '\n');
4905  appendStringInfoSpaces(es->str, es->indent * 2);
4906  }
4907 }
#define linitial_int(l)
Definition: pg_list.h:175
List * grouping_stack
Definition: explain.h:52
int indent
Definition: explain.h:51
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:188
void appendStringInfoSpaces(StringInfo str, int count)
Definition: stringinfo.c:206
#define Assert(condition)
Definition: c.h:804
ExplainFormat format
Definition: explain.h:49
StringInfo str
Definition: explain.h:39

◆ NewExplainState()

ExplainState* NewExplainState ( void  )

Definition at line 317 of file explain.c.

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

Referenced by explain_ExecutorEnd(), and ExplainQuery().

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

◆ report_triggers()

static void report_triggers ( ResultRelInfo rInfo,
bool  show_relname,
ExplainState es 
)
static

Definition at line 978 of file explain.c.

References appendStringInfo(), appendStringInfoString(), EXPLAIN_FORMAT_TEXT, ExplainCloseGroup(), ExplainOpenGroup(), ExplainPropertyFloat(), ExplainPropertyText(), ExplainState::format, get_constraint_name(), InstrEndLoop(), Instrumentation::ntuples, TriggerDesc::numtriggers, OidIsValid, pfree(), RelationGetRelationName, relname, ResultRelInfo::ri_RelationDesc, ResultRelInfo::ri_TrigDesc, ResultRelInfo::ri_TrigInstrument, ExplainState::str, Trigger::tgconstraint, Trigger::tgname, ExplainState::timing, Instrumentation::total, TriggerDesc::triggers, and ExplainState::verbose.

Referenced by ExplainPrintTriggers().

979 {
980  int nt;
981 
982  if (!rInfo->ri_TrigDesc || !rInfo->ri_TrigInstrument)
983  return;
984  for (nt = 0; nt < rInfo->ri_TrigDesc->numtriggers; nt++)
985  {
986  Trigger *trig = rInfo->ri_TrigDesc->triggers + nt;
987  Instrumentation *instr = rInfo->ri_TrigInstrument + nt;
988  char *relname;
989  char *conname = NULL;
990 
991  /* Must clean up instrumentation state */
992  InstrEndLoop(instr);
993 
994  /*
995  * We ignore triggers that were never invoked; they likely aren't
996  * relevant to the current query type.
997  */
998  if (instr->ntuples == 0)
999  continue;
1000 
1001  ExplainOpenGroup("Trigger", NULL, true, es);
1002 
1003  relname = RelationGetRelationName(rInfo->ri_RelationDesc);
1004  if (OidIsValid(trig->tgconstraint))
1005  conname = get_constraint_name(trig->tgconstraint);
1006 
1007  /*
1008  * In text format, we avoid printing both the trigger name and the
1009  * constraint name unless VERBOSE is specified. In non-text formats
1010  * we just print everything.
1011  */
1012  if (es->format == EXPLAIN_FORMAT_TEXT)
1013  {
1014  if (es->verbose || conname == NULL)
1015  appendStringInfo(es->str, "Trigger %s", trig->tgname);
1016  else
1017  appendStringInfoString(es->str, "Trigger");
1018  if (conname)
1019  appendStringInfo(es->str, " for constraint %s", conname);
1020  if (show_relname)
1021  appendStringInfo(es->str, " on %s", relname);
1022  if (es->timing)
1023  appendStringInfo(es->str, ": time=%.3f calls=%.0f\n",
1024  1000.0 * instr->total, instr->ntuples);
1025  else
1026  appendStringInfo(es->str, ": calls=%.0f\n", instr->ntuples);
1027  }
1028  else
1029  {
1030  ExplainPropertyText("Trigger Name", trig->tgname, es);
1031  if (conname)
1032  ExplainPropertyText("Constraint Name", conname, es);
1033  ExplainPropertyText("Relation", relname, es);
1034  if (es->timing)
1035  ExplainPropertyFloat("Time", "ms", 1000.0 * instr->total, 3,
1036  es);
1037  ExplainPropertyFloat("Calls", NULL, instr->ntuples, 0, es);
1038  }
1039 
1040  if (conname)
1041  pfree(conname);
1042 
1043  ExplainCloseGroup("Trigger", NULL, true, es);
1044  }
1045 }
Relation ri_RelationDesc
Definition: execnodes.h:411
char * get_constraint_name(Oid conoid)
Definition: lsyscache.c:1106
void ExplainPropertyFloat(const char *qlabel, const char *unit, double value, int ndigits, ExplainState *es)
Definition: explain.c:4459