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/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_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 53 of file explain.c.

Referenced by ExplainDummyGroup(), and ExplainXMLTag().

◆ X_CLOSING

#define X_CLOSING   1

Definition at line 52 of file explain.c.

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

◆ X_NOWHITESPACE

#define X_NOWHITESPACE   4

Definition at line 54 of file explain.c.

Referenced by ExplainProperty(), and ExplainXMLTag().

◆ X_OPENING

#define X_OPENING   0

Definition at line 51 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 1013 of file explain.c.

References INSTR_TIME_GET_DOUBLE, INSTR_TIME_SET_CURRENT, and INSTR_TIME_SUBTRACT.

Referenced by ExplainOnePlan(), and IsCheckpointOnSchedule().

1014 {
1015  instr_time endtime;
1016 
1017  INSTR_TIME_SET_CURRENT(endtime);
1018  INSTR_TIME_SUBTRACT(endtime, *starttime);
1019  return INSTR_TIME_GET_DOUBLE(endtime);
1020 }
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 4722 of file explain.c.

References escape_json().

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

4723 {
4724  escape_json(buf, str);
4725 }
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 3267 of file explain.c.

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

Referenced by ExplainIndexScanDetails(), and ExplainNode().

3268 {
3269  const char *result;
3270 
3272  result = (*explain_get_index_name_hook) (indexId);
3273  else
3274  result = NULL;
3275  if (result == NULL)
3276  {
3277  /* default behavior: look it up in the catalogs */
3278  result = get_rel_name(indexId);
3279  if (result == NULL)
3280  elog(ERROR, "cache lookup failed for index %u", indexId);
3281  }
3282  return result;
3283 }
#define ERROR
Definition: elog.h:43
explain_get_index_name_hook_type explain_get_index_name_hook
Definition: explain.c:47
#define elog(elevel,...)
Definition: elog.h:214
char * get_rel_name(Oid relid)
Definition: lsyscache.c:1840

◆ ExplainBeginOutput()

void ExplainBeginOutput ( ExplainState es)

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

4547 {
4548  switch (es->format)
4549  {
4550  case EXPLAIN_FORMAT_TEXT:
4551  /* nothing to do */
4552  break;
4553 
4554  case EXPLAIN_FORMAT_XML:
4556  "<explain xmlns=\"http://www.postgresql.org/2009/explain\">\n");
4557  es->indent++;
4558  break;
4559 
4560  case EXPLAIN_FORMAT_JSON:
4561  /* top-level structure is an array of plans */
4562  appendStringInfoChar(es->str, '[');
4563  es->grouping_stack = lcons_int(0, es->grouping_stack);
4564  es->indent++;
4565  break;
4566 
4567  case EXPLAIN_FORMAT_YAML:
4568  es->grouping_stack = lcons_int(0, es->grouping_stack);
4569  break;
4570  }
4571 }
List * lcons_int(int datum, List *list)
Definition: list.c:471
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 4353 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().

4355 {
4356  switch (es->format)
4357  {
4358  case EXPLAIN_FORMAT_TEXT:
4359  /* nothing to do */
4360  break;
4361 
4362  case EXPLAIN_FORMAT_XML:
4363  es->indent--;
4364  ExplainXMLTag(objtype, X_CLOSING, es);
4365  break;
4366 
4367  case EXPLAIN_FORMAT_JSON:
4368  es->indent--;
4369  appendStringInfoChar(es->str, '\n');
4370  appendStringInfoSpaces(es->str, 2 * es->indent);
4371  appendStringInfoChar(es->str, labeled ? '}' : ']');
4373  break;
4374 
4375  case EXPLAIN_FORMAT_YAML:
4376  es->indent--;
4378  break;
4379  }
4380 }
static void ExplainXMLTag(const char *tagname, int flags, ExplainState *es)
Definition: explain.c:4635
#define X_CLOSING
Definition: explain.c:52
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:860

◆ ExplainCloseWorker()

static void ExplainCloseWorker ( int  n,
ExplainState es 
)
static

Definition at line 3983 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(), and show_sort_info().

3984 {
3985  ExplainWorkersState *wstate = es->workers_state;
3986 
3987  Assert(wstate);
3988  Assert(n >= 0 && n < wstate->num_workers);
3989  Assert(wstate->worker_inited[n]);
3990 
3991  /*
3992  * Save formatting state in case we do another ExplainOpenWorker(), then
3993  * pop the formatting stack.
3994  */
3995  ExplainSaveGroup(es, 2, &wstate->worker_state_save[n]);
3996 
3997  /*
3998  * In TEXT format, if we didn't actually produce any output line(s) then
3999  * truncate off the partial line emitted by ExplainOpenWorker. (This is
4000  * to avoid bogus output if, say, show_buffer_usage chooses not to print
4001  * anything for the worker.) Also fix up the indent level.
4002  */
4003  if (es->format == EXPLAIN_FORMAT_TEXT)
4004  {
4005  while (es->str->len > 0 && es->str->data[es->str->len - 1] != '\n')
4006  es->str->data[--(es->str->len)] = '\0';
4007 
4008  es->indent--;
4009  }
4010 
4011  /* Restore prior output buffer pointer */
4012  es->str = wstate->prev_str;
4013 }
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:746
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:4439
StringInfo str
Definition: explain.h:39

◆ ExplainCreateWorkersState()

static ExplainWorkersState * ExplainCreateWorkersState ( int  num_workers)
static

Definition at line 3904 of file explain.c.

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

Referenced by ExplainNode().

3905 {
3906  ExplainWorkersState *wstate;
3907 
3908  wstate = (ExplainWorkersState *) palloc(sizeof(ExplainWorkersState));
3909  wstate->num_workers = num_workers;
3910  wstate->worker_inited = (bool *) palloc0(num_workers * sizeof(bool));
3911  wstate->worker_str = (StringInfoData *)
3912  palloc0(num_workers * sizeof(StringInfoData));
3913  wstate->worker_state_save = (int *) palloc(num_workers * sizeof(int));
3914  return wstate;
3915 }
int * worker_state_save
Definition: explain.h:33
StringInfoData * worker_str
Definition: explain.h:32
void * palloc0(Size size)
Definition: mcxt.c:981
bool * worker_inited
Definition: explain.h:31
void * palloc(Size size)
Definition: mcxt.c:950

◆ ExplainCustomChildren()

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

Definition at line 3882 of file explain.c.

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

Referenced by ExplainNode().

3883 {
3884  ListCell *cell;
3885  const char *label =
3886  (list_length(css->custom_ps) != 1 ? "children" : "child");
3887 
3888  foreach(cell, css->custom_ps)
3889  ExplainNode((PlanState *) lfirst(cell), ancestors, label, NULL, es);
3890 }
List * custom_ps
Definition: execnodes.h:1819
static void ExplainNode(PlanState *planstate, List *ancestors, const char *relationship, const char *plan_name, ExplainState *es)
Definition: explain.c:1106
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 4500 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().

4501 {
4502  switch (es->format)
4503  {
4504  case EXPLAIN_FORMAT_TEXT:
4505  /* nothing to do */
4506  break;
4507 
4508  case EXPLAIN_FORMAT_XML:
4509  ExplainXMLTag(objtype, X_CLOSE_IMMEDIATE, es);
4510  break;
4511 
4512  case EXPLAIN_FORMAT_JSON:
4514  appendStringInfoSpaces(es->str, 2 * es->indent);
4515  if (labelname)
4516  {
4517  escape_json(es->str, labelname);
4518  appendStringInfoString(es->str, ": ");
4519  }
4520  escape_json(es->str, objtype);
4521  break;
4522 
4523  case EXPLAIN_FORMAT_YAML:
4525  if (labelname)
4526  {
4527  escape_yaml(es->str, labelname);
4528  appendStringInfoString(es->str, ": ");
4529  }
4530  else
4531  {
4532  appendStringInfoString(es->str, "- ");
4533  }
4534  escape_yaml(es->str, objtype);
4535  break;
4536  }
4537 }
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:4635
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
static void ExplainYAMLLineStarting(ExplainState *es)
Definition: explain.c:4697
int indent
Definition: explain.h:51
static void escape_yaml(StringInfo buf, const char *str)
Definition: explain.c:4722
void appendStringInfoSpaces(StringInfo str, int count)
Definition: stringinfo.c:206
static void ExplainJSONLineEnding(ExplainState *es)
Definition: explain.c:4677
ExplainFormat format
Definition: explain.h:49
#define X_CLOSE_IMMEDIATE
Definition: explain.c:53
StringInfo str
Definition: explain.h:39

◆ ExplainEndOutput()

void ExplainEndOutput ( ExplainState es)

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

4578 {
4579  switch (es->format)
4580  {
4581  case EXPLAIN_FORMAT_TEXT:
4582  /* nothing to do */
4583  break;
4584 
4585  case EXPLAIN_FORMAT_XML:
4586  es->indent--;
4587  appendStringInfoString(es->str, "</explain>");
4588  break;
4589 
4590  case EXPLAIN_FORMAT_JSON:
4591  es->indent--;
4592  appendStringInfoString(es->str, "\n]");
4594  break;
4595 
4596  case EXPLAIN_FORMAT_YAML:
4598  break;
4599  }
4600 }
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:860

◆ ExplainFlushWorkersState()

static void ExplainFlushWorkersState ( ExplainState es)
static

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

4020 {
4021  ExplainWorkersState *wstate = es->workers_state;
4022 
4023  ExplainOpenGroup("Workers", "Workers", false, es);
4024  for (int i = 0; i < wstate->num_workers; i++)
4025  {
4026  if (wstate->worker_inited[i])
4027  {
4028  /* This must match previous ExplainOpenSetAsideGroup call */
4029  ExplainOpenGroup("Worker", NULL, true, es);
4030  appendStringInfoString(es->str, wstate->worker_str[i].data);
4031  ExplainCloseGroup("Worker", NULL, true, es);
4032 
4033  pfree(wstate->worker_str[i].data);
4034  }
4035  }
4036  ExplainCloseGroup("Workers", "Workers", false, es);
4037 
4038  pfree(wstate->worker_inited);
4039  pfree(wstate->worker_str);
4040  pfree(wstate->worker_state_save);
4041  pfree(wstate);
4042 }
int * worker_state_save
Definition: explain.h:33
void pfree(void *pointer)
Definition: mcxt.c:1057
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:4353
void ExplainOpenGroup(const char *objtype, const char *labelname, bool labeled, ExplainState *es)
Definition: explain.c:4290
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 3463 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().

3465 {
3466  const char *indexname = explain_get_index_name(indexid);
3467 
3468  if (es->format == EXPLAIN_FORMAT_TEXT)
3469  {
3470  if (ScanDirectionIsBackward(indexorderdir))
3471  appendStringInfoString(es->str, " Backward");
3472  appendStringInfo(es->str, " using %s", quote_identifier(indexname));
3473  }
3474  else
3475  {
3476  const char *scandir;
3477 
3478  switch (indexorderdir)
3479  {
3480  case BackwardScanDirection:
3481  scandir = "Backward";
3482  break;
3484  scandir = "NoMovement";
3485  break;
3486  case ForwardScanDirection:
3487  scandir = "Forward";
3488  break;
3489  default:
3490  scandir = "???";
3491  break;
3492  }
3493  ExplainPropertyText("Scan Direction", scandir, es);
3494  ExplainPropertyText("Index Name", indexname, es);
3495  }
3496 }
const char * quote_identifier(const char *ident)
Definition: ruleutils.c:10709
#define ScanDirectionIsBackward(direction)
Definition: sdir.h:41
void ExplainPropertyText(const char *qlabel, const char *value, ExplainState *es)
Definition: explain.c:4225
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:3267
StringInfo str
Definition: explain.h:39

◆ ExplainJSONLineEnding()

static void ExplainJSONLineEnding ( ExplainState es)
static

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

4678 {
4680  if (linitial_int(es->grouping_stack) != 0)
4681  appendStringInfoChar(es->str, ',');
4682  else
4683  linitial_int(es->grouping_stack) = 1;
4684  appendStringInfoChar(es->str, '\n');
4685 }
#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:746
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 3807 of file explain.c.

References ExplainNode().

Referenced by ExplainNode().

3809 {
3810  int j;
3811 
3812  for (j = 0; j < nplans; j++)
3813  ExplainNode(planstates[j], ancestors,
3814  "Member", NULL, es);
3815 }
static void ExplainNode(PlanState *planstate, List *ancestors, const char *relationship, const char *plan_name, ExplainState *es)
Definition: explain.c:1106

◆ ExplainMissingMembers()

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

Definition at line 3825 of file explain.c.

References EXPLAIN_FORMAT_TEXT, ExplainPropertyInteger(), and format.

Referenced by ExplainNode().

3826 {
3827  if (nplans < nchildren || es->format != EXPLAIN_FORMAT_TEXT)
3828  ExplainPropertyInteger("Subplans Removed", NULL,
3829  nchildren - nplans, es);
3830 }
void ExplainPropertyInteger(const char *qlabel, const char *unit, int64 value, ExplainState *es)
Definition: explain.c:4234
static char format

◆ ExplainModifyTarget()

static void ExplainModifyTarget ( ModifyTable plan,
ExplainState es 
)
static

Definition at line 3515 of file explain.c.

References ExplainTargetRel().

Referenced by ExplainNode().

3516 {
3517  ExplainTargetRel((Plan *) plan, plan->nominalRelation, es);
3518 }
static void ExplainTargetRel(Plan *plan, Index rti, ExplainState *es)
Definition: explain.c:3524

◆ ExplainNode()

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

Definition at line 1106 of file explain.c.

References AGG_HASHED, AGG_MIXED, AGG_PLAIN, AGG_SORTED, Agg::aggsplit, Agg::aggstrategy, ExplainState::analyze, appendStringInfo(), appendStringInfoChar(), appendStringInfoString(), 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_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_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_SampleScan, T_SeqScan, T_SetOp, T_Sort, T_SubqueryScan, T_TableFuncScan, 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().

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

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

References ExplainState::analyze, Assert, 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(), ForwardScanDirection, FreeQueryDesc(), GetActiveSnapshot(), GetIntoRelEFlags(), INSTR_TIME_GET_DOUBLE, INSTR_TIME_SET_CURRENT, INSTRUMENT_BUFFERS, INSTRUMENT_ROWS, INSTRUMENT_TIMER, INSTRUMENT_WAL, InvalidSnapshot, NoMovementScanDirection, None_Receiver, PopActiveSnapshot(), PushCopiedSnapshot(), show_buffer_usage(), IntoClause::skipData, ExplainState::summary, ExplainState::timing, UpdateActiveSnapshotCommandId(), and ExplainState::wal.

Referenced by ExplainExecuteQuery(), and ExplainOneQuery().

500 {
502  QueryDesc *queryDesc;
503  instr_time starttime;
504  double totaltime = 0;
505  int eflags;
506  int instrument_option = 0;
507 
508  Assert(plannedstmt->commandType != CMD_UTILITY);
509 
510  if (es->analyze && es->timing)
511  instrument_option |= INSTRUMENT_TIMER;
512  else if (es->analyze)
513  instrument_option |= INSTRUMENT_ROWS;
514 
515  if (es->buffers)
516  instrument_option |= INSTRUMENT_BUFFERS;
517  if (es->wal)
518  instrument_option |= INSTRUMENT_WAL;
519 
520  /*
521  * We always collect timing for the entire statement, even when node-level
522  * timing is off, so we don't look at es->timing here. (We could skip
523  * this if !es->summary, but it's hardly worth the complication.)
524  */
525  INSTR_TIME_SET_CURRENT(starttime);
526 
527  /*
528  * Use a snapshot with an updated command ID to ensure this query sees
529  * results of any previously executed queries.
530  */
533 
534  /*
535  * Normally we discard the query's output, but if explaining CREATE TABLE
536  * AS, we'd better use the appropriate tuple receiver.
537  */
538  if (into)
539  dest = CreateIntoRelDestReceiver(into);
540  else
541  dest = None_Receiver;
542 
543  /* Create a QueryDesc for the query */
544  queryDesc = CreateQueryDesc(plannedstmt, queryString,
546  dest, params, queryEnv, instrument_option);
547 
548  /* Select execution options */
549  if (es->analyze)
550  eflags = 0; /* default run-to-completion flags */
551  else
552  eflags = EXEC_FLAG_EXPLAIN_ONLY;
553  if (into)
554  eflags |= GetIntoRelEFlags(into);
555 
556  /* call ExecutorStart to prepare the plan for execution */
557  ExecutorStart(queryDesc, eflags);
558 
559  /* Execute the plan for statistics if asked for */
560  if (es->analyze)
561  {
562  ScanDirection dir;
563 
564  /* EXPLAIN ANALYZE CREATE TABLE AS WITH NO DATA is weird */
565  if (into && into->skipData)
567  else
568  dir = ForwardScanDirection;
569 
570  /* run the plan */
571  ExecutorRun(queryDesc, dir, 0L, true);
572 
573  /* run cleanup too */
574  ExecutorFinish(queryDesc);
575 
576  /* We can't run ExecutorEnd 'till we're done printing the stats... */
577  totaltime += elapsed_time(&starttime);
578  }
579 
580  ExplainOpenGroup("Query", NULL, true, es);
581 
582  /* Create textual dump of plan tree */
583  ExplainPrintPlan(es, queryDesc);
584 
585  /* Show buffer usage in planning */
586  if (bufusage)
587  {
588  ExplainOpenGroup("Planning", "Planning", true, es);
589  show_buffer_usage(es, bufusage, true);
590  ExplainCloseGroup("Planning", "Planning", true, es);
591  }
592 
593  if (es->summary && planduration)
594  {
595  double plantime = INSTR_TIME_GET_DOUBLE(*planduration);
596 
597  ExplainPropertyFloat("Planning Time", "ms", 1000.0 * plantime, 3, es);
598  }
599 
600  /* Print info about runtime of triggers */
601  if (es->analyze)
602  ExplainPrintTriggers(es, queryDesc);
603 
604  /*
605  * Print info about JITing. Tied to es->costs because we don't want to
606  * display this in regression tests, as it'd cause output differences
607  * depending on build options. Might want to separate that out from COSTS
608  * at a later stage.
609  */
610  if (es->costs)
611  ExplainPrintJITSummary(es, queryDesc);
612 
613  /*
614  * Close down the query and free resources. Include time for this in the
615  * total execution time (although it should be pretty minimal).
616  */
617  INSTR_TIME_SET_CURRENT(starttime);
618 
619  ExecutorEnd(queryDesc);
620 
621  FreeQueryDesc(queryDesc);
622 
624 
625  /* We need a CCI just in case query expanded to multiple plans */
626  if (es->analyze)
628 
629  totaltime += elapsed_time(&starttime);
630 
631  /*
632  * We only report execution time if we actually ran the query (that is,
633  * the user specified ANALYZE), and if summary reporting is enabled (the
634  * user can set SUMMARY OFF to not have the timing information included in
635  * the output). By default, ANALYZE sets SUMMARY to true.
636  */
637  if (es->summary && es->analyze)
638  ExplainPropertyFloat("Execution Time", "ms", 1000.0 * totaltime, 3,
639  es);
640 
641  ExplainCloseGroup("Query", NULL, true, es);
642 }
bool summary
Definition: explain.h:47
void UpdateActiveSnapshotCommandId(void)
Definition: snapmgr.c:728
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:4261
struct timeval instr_time
Definition: instr_time.h:150
void ExecutorStart(QueryDesc *queryDesc, int eflags)
Definition: execMain.c:143
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:462
int GetIntoRelEFlags(IntoClause *intoClause)
Definition: createas.c:393
void ExplainPrintTriggers(ExplainState *es, QueryDesc *queryDesc)
Definition: explain.c:768
void ExplainPrintPlan(ExplainState *es, QueryDesc *queryDesc)
Definition: explain.c:719
void ExecutorRun(QueryDesc *queryDesc, ScanDirection direction, uint64 count, bool execute_once)
Definition: execMain.c:301
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
ScanDirection
Definition: sdir.h:22
DestReceiver * CreateIntoRelDestReceiver(IntoClause *intoClause)
Definition: createas.c:411
void ExecutorFinish(QueryDesc *queryDesc)
Definition: execMain.c:402
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
#define Assert(condition)
Definition: c.h:746
bool wal
Definition: explain.h:45
static double elapsed_time(instr_time *starttime)
Definition: explain.c:1013
#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:3289
bool buffers
Definition: explain.h:44
void ExplainCloseGroup(const char *objtype, const char *labelname, bool labeled, ExplainState *es)
Definition: explain.c:4353
void ExplainPrintJITSummary(ExplainState *es, QueryDesc *queryDesc)
Definition: explain.c:811
void ExplainOpenGroup(const char *objtype, const char *labelname, bool labeled, ExplainState *es)
Definition: explain.c:4290
#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 361 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().

365 {
366  /* planner will not cope with utility statements */
367  if (query->commandType == CMD_UTILITY)
368  {
369  ExplainOneUtility(query->utilityStmt, into, es, queryString, params,
370  queryEnv);
371  return;
372  }
373 
374  /* if an advisor plugin is present, let it manage things */
376  (*ExplainOneQuery_hook) (query, cursorOptions, into, es,
377  queryString, params, queryEnv);
378  else
379  {
380  PlannedStmt *plan;
381  instr_time planstart,
382  planduration;
383  BufferUsage bufusage_start,
384  bufusage;
385 
386  if (es->buffers)
387  bufusage_start = pgBufferUsage;
388  INSTR_TIME_SET_CURRENT(planstart);
389 
390  /* plan the query */
391  plan = pg_plan_query(query, queryString, cursorOptions, params);
392 
393  INSTR_TIME_SET_CURRENT(planduration);
394  INSTR_TIME_SUBTRACT(planduration, planstart);
395 
396  /* calc differences of buffer counters. */
397  if (es->buffers)
398  {
399  memset(&bufusage, 0, sizeof(BufferUsage));
400  BufferUsageAccumDiff(&bufusage, &pgBufferUsage, &bufusage_start);
401  }
402 
403  /* run it (if needed) and produce output */
404  ExplainOnePlan(plan, into, es, queryString, params, queryEnv,
405  &planduration, (es->buffers ? &bufusage : NULL));
406  }
407 }
struct timeval instr_time
Definition: instr_time.h:150
Node * utilityStmt
Definition: parsenodes.h:120
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:44
CmdType commandType
Definition: parsenodes.h:112
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:496
#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:857
void ExplainOneUtility(Node *utilityStmt, IntoClause *into, ExplainState *es, const char *queryString, ParamListInfo params, QueryEnvironment *queryEnv)
Definition: explain.c:421
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 421 of file explain.c.

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

Referenced by ExplainExecuteQuery(), and ExplainOneQuery().

424 {
425  if (utilityStmt == NULL)
426  return;
427 
428  if (IsA(utilityStmt, CreateTableAsStmt))
429  {
430  /*
431  * We have to rewrite the contained SELECT and then pass it back to
432  * ExplainOneQuery. It's probably not really necessary to copy the
433  * contained parsetree another time, but let's be safe.
434  */
435  CreateTableAsStmt *ctas = (CreateTableAsStmt *) utilityStmt;
436  List *rewritten;
437 
438  rewritten = QueryRewrite(castNode(Query, copyObject(ctas->query)));
439  Assert(list_length(rewritten) == 1);
440  ExplainOneQuery(linitial_node(Query, rewritten),
441  CURSOR_OPT_PARALLEL_OK, ctas->into, es,
442  queryString, params, queryEnv);
443  }
444  else if (IsA(utilityStmt, DeclareCursorStmt))
445  {
446  /*
447  * Likewise for DECLARE CURSOR.
448  *
449  * Notice that if you say EXPLAIN ANALYZE DECLARE CURSOR then we'll
450  * actually run the query. This is different from pre-8.3 behavior
451  * but seems more useful than not running the query. No cursor will
452  * be created, however.
453  */
454  DeclareCursorStmt *dcs = (DeclareCursorStmt *) utilityStmt;
455  List *rewritten;
456 
457  rewritten = QueryRewrite(castNode(Query, copyObject(dcs->query)));
458  Assert(list_length(rewritten) == 1);
459  ExplainOneQuery(linitial_node(Query, rewritten),
460  dcs->options, NULL, es,
461  queryString, params, queryEnv);
462  }
463  else if (IsA(utilityStmt, ExecuteStmt))
464  ExplainExecuteQuery((ExecuteStmt *) utilityStmt, into, es,
465  queryString, params, queryEnv);
466  else if (IsA(utilityStmt, NotifyStmt))
467  {
468  if (es->format == EXPLAIN_FORMAT_TEXT)
469  appendStringInfoString(es->str, "NOTIFY\n");
470  else
471  ExplainDummyGroup("Notify", NULL, es);
472  }
473  else
474  {
475  if (es->format == EXPLAIN_FORMAT_TEXT)
477  "Utility statements have no plan structure\n");
478  else
479  ExplainDummyGroup("Utility Statement", NULL, es);
480  }
481 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:579
List * QueryRewrite(Query *parsetree)
#define castNode(_type_, nodeptr)
Definition: nodes.h:597
#define linitial_node(type, l)
Definition: pg_list.h:177
static void ExplainDummyGroup(const char *objtype, const char *labelname, ExplainState *es)
Definition: explain.c:4500
void ExplainExecuteQuery(ExecuteStmt *execstmt, IntoClause *into, ExplainState *es, const char *queryString, ParamListInfo params, QueryEnvironment *queryEnv)
Definition: prepare.c:606
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
static void ExplainOneQuery(Query *query, int cursorOptions, IntoClause *into, ExplainState *es, const char *queryString, ParamListInfo params, QueryEnvironment *queryEnv)
Definition: explain.c:361
IntoClause * into
Definition: parsenodes.h:3279
#define Assert(condition)
Definition: c.h:746
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:2717
#define copyObject(obj)
Definition: nodes.h:644
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 4290 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().

4292 {
4293  switch (es->format)
4294  {
4295  case EXPLAIN_FORMAT_TEXT:
4296  /* nothing to do */
4297  break;
4298 
4299  case EXPLAIN_FORMAT_XML:
4300  ExplainXMLTag(objtype, X_OPENING, es);
4301  es->indent++;
4302  break;
4303 
4304  case EXPLAIN_FORMAT_JSON:
4306  appendStringInfoSpaces(es->str, 2 * es->indent);
4307  if (labelname)
4308  {
4309  escape_json(es->str, labelname);
4310  appendStringInfoString(es->str, ": ");
4311  }
4312  appendStringInfoChar(es->str, labeled ? '{' : '[');
4313 
4314  /*
4315  * In JSON format, the grouping_stack is an integer list. 0 means
4316  * we've emitted nothing at this grouping level, 1 means we've
4317  * emitted something (and so the next item needs a comma). See
4318  * ExplainJSONLineEnding().
4319  */
4320  es->grouping_stack = lcons_int(0, es->grouping_stack);
4321  es->indent++;
4322  break;
4323 
4324  case EXPLAIN_FORMAT_YAML:
4325 
4326  /*
4327  * In YAML format, the grouping stack is an integer list. 0 means
4328  * we've emitted nothing at this grouping level AND this grouping
4329  * level is unlabeled and must be marked with "- ". See
4330  * ExplainYAMLLineStarting().
4331  */
4333  if (labelname)
4334  {
4335  appendStringInfo(es->str, "%s: ", labelname);
4336  es->grouping_stack = lcons_int(1, es->grouping_stack);
4337  }
4338  else
4339  {
4340  appendStringInfoString(es->str, "- ");
4341  es->grouping_stack = lcons_int(0, es->grouping_stack);
4342  }
4343  es->indent++;
4344  break;
4345  }
4346 }
void escape_json(StringInfo buf, const char *str)
Definition: json.c:1279
List * lcons_int(int datum, List *list)
Definition: list.c:471
static void ExplainXMLTag(const char *tagname, int flags, ExplainState *es)
Definition: explain.c:4635
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:4697
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:4677
#define X_OPENING
Definition: explain.c:51
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 4400 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().

4402 {
4403  switch (es->format)
4404  {
4405  case EXPLAIN_FORMAT_TEXT:
4406  /* nothing to do */
4407  break;
4408 
4409  case EXPLAIN_FORMAT_XML:
4410  es->indent += depth;
4411  break;
4412 
4413  case EXPLAIN_FORMAT_JSON:
4414  es->grouping_stack = lcons_int(0, es->grouping_stack);
4415  es->indent += depth;
4416  break;
4417 
4418  case EXPLAIN_FORMAT_YAML:
4419  if (labelname)
4420  es->grouping_stack = lcons_int(1, es->grouping_stack);
4421  else
4422  es->grouping_stack = lcons_int(0, es->grouping_stack);
4423  es->indent += depth;
4424  break;
4425  }
4426 }
List * lcons_int(int datum, List *list)
Definition: list.c:471
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 3921 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(), and show_sort_info().

3922 {
3923  ExplainWorkersState *wstate = es->workers_state;
3924 
3925  Assert(wstate);
3926  Assert(n >= 0 && n < wstate->num_workers);
3927 
3928  /* Save prior output buffer pointer */
3929  wstate->prev_str = es->str;
3930 
3931  if (!wstate->worker_inited[n])
3932  {
3933  /* First time through, so create the buffer for this worker */
3934  initStringInfo(&wstate->worker_str[n]);
3935  es->str = &wstate->worker_str[n];
3936 
3937  /*
3938  * Push suitable initial formatting state for this worker's field
3939  * group. We allow one extra logical nesting level, since this group
3940  * will eventually be wrapped in an outer "Workers" group.
3941  */
3942  ExplainOpenSetAsideGroup("Worker", NULL, true, 2, es);
3943 
3944  /*
3945  * In non-TEXT formats we always emit a "Worker Number" field, even if
3946  * there's no other data for this worker.
3947  */
3948  if (es->format != EXPLAIN_FORMAT_TEXT)
3949  ExplainPropertyInteger("Worker Number", NULL, n, es);
3950 
3951  wstate->worker_inited[n] = true;
3952  }
3953  else
3954  {
3955  /* Resuming output for a worker we've already emitted some data for */
3956  es->str = &wstate->worker_str[n];
3957 
3958  /* Restore formatting state saved by last ExplainCloseWorker() */
3959  ExplainRestoreGroup(es, 2, &wstate->worker_state_save[n]);
3960  }
3961 
3962  /*
3963  * In TEXT format, prefix the first output line for this worker with
3964  * "Worker N:". Then, any additional lines should be indented one more
3965  * stop than the "Worker N" line is.
3966  */
3967  if (es->format == EXPLAIN_FORMAT_TEXT)
3968  {
3969  if (es->str->len == 0)
3970  {
3971  ExplainIndentText(es);
3972  appendStringInfo(es->str, "Worker %d: ", n);
3973  }
3974 
3975  es->indent++;
3976  }
3977 }
int * worker_state_save
Definition: explain.h:33
void ExplainPropertyInteger(const char *qlabel, const char *unit, int64 value, ExplainState *es)
Definition: explain.c:4234
static void ExplainRestoreGroup(ExplainState *es, int depth, int *state_save)
Definition: explain.c:4469
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:4662
#define Assert(condition)
Definition: c.h:746
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:4400

◆ ExplainPreScanNode()

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

Definition at line 1032 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_TidScan, T_ValuesScan, and T_WorkTableScan.

Referenced by ExplainPrintPlan().

1033 {
1034  Plan *plan = planstate->plan;
1035 
1036  switch (nodeTag(plan))
1037  {
1038  case T_SeqScan:
1039  case T_SampleScan:
1040  case T_IndexScan:
1041  case T_IndexOnlyScan:
1042  case T_BitmapHeapScan:
1043  case T_TidScan:
1044  case T_SubqueryScan:
1045  case T_FunctionScan:
1046  case T_TableFuncScan:
1047  case T_ValuesScan:
1048  case T_CteScan:
1049  case T_NamedTuplestoreScan:
1050  case T_WorkTableScan:
1051  *rels_used = bms_add_member(*rels_used,
1052  ((Scan *) plan)->scanrelid);
1053  break;
1054  case T_ForeignScan:
1055  *rels_used = bms_add_members(*rels_used,
1056  ((ForeignScan *) plan)->fs_relids);
1057  break;
1058  case T_CustomScan:
1059  *rels_used = bms_add_members(*rels_used,
1060  ((CustomScan *) plan)->custom_relids);
1061  break;
1062  case T_ModifyTable:
1063  *rels_used = bms_add_member(*rels_used,
1064  ((ModifyTable *) plan)->nominalRelation);
1065  if (((ModifyTable *) plan)->exclRelRTI)
1066  *rels_used = bms_add_member(*rels_used,
1067  ((ModifyTable *) plan)->exclRelRTI);
1068  break;
1069  case T_Append:
1070  *rels_used = bms_add_members(*rels_used,
1071  ((Append *) plan)->apprelids);
1072  break;
1073  case T_MergeAppend:
1074  *rels_used = bms_add_members(*rels_used,
1075  ((MergeAppend *) plan)->apprelids);
1076  break;
1077  default:
1078  break;
1079  }
1080 
1081  return planstate_tree_walker(planstate, ExplainPreScanNode, rels_used);
1082 }
Definition: nodes.h:49
Plan * plan
Definition: execnodes.h:939
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:736
#define nodeTag(nodeptr)
Definition: nodes.h:533
static bool ExplainPreScanNode(PlanState *planstate, Bitmapset **rels_used)
Definition: explain.c:1032
bool planstate_tree_walker(PlanState *planstate, bool(*walker)(), void *context)
Definition: nodeFuncs.c:3931
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 837 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().

838 {
839  instr_time total_time;
840 
841  /* don't print information if no JITing happened */
842  if (!ji || ji->created_functions == 0)
843  return;
844 
845  /* calculate total time */
846  INSTR_TIME_SET_ZERO(total_time);
847  INSTR_TIME_ADD(total_time, ji->generation_counter);
848  INSTR_TIME_ADD(total_time, ji->inlining_counter);
849  INSTR_TIME_ADD(total_time, ji->optimization_counter);
850  INSTR_TIME_ADD(total_time, ji->emission_counter);
851 
852  ExplainOpenGroup("JIT", "JIT", true, es);
853 
854  /* for higher density, open code the text output format */
855  if (es->format == EXPLAIN_FORMAT_TEXT)
856  {
857  ExplainIndentText(es);
858  appendStringInfoString(es->str, "JIT:\n");
859  es->indent++;
860 
861  ExplainPropertyInteger("Functions", NULL, ji->created_functions, es);
862 
863  ExplainIndentText(es);
864  appendStringInfo(es->str, "Options: %s %s, %s %s, %s %s, %s %s\n",
865  "Inlining", jit_flags & PGJIT_INLINE ? "true" : "false",
866  "Optimization", jit_flags & PGJIT_OPT3 ? "true" : "false",
867  "Expressions", jit_flags & PGJIT_EXPR ? "true" : "false",
868  "Deforming", jit_flags & PGJIT_DEFORM ? "true" : "false");
869 
870  if (es->analyze && es->timing)
871  {
872  ExplainIndentText(es);
873  appendStringInfo(es->str,
874  "Timing: %s %.3f ms, %s %.3f ms, %s %.3f ms, %s %.3f ms, %s %.3f ms\n",
875  "Generation", 1000.0 * INSTR_TIME_GET_DOUBLE(ji->generation_counter),
876  "Inlining", 1000.0 * INSTR_TIME_GET_DOUBLE(ji->inlining_counter),
877  "Optimization", 1000.0 * INSTR_TIME_GET_DOUBLE(ji->optimization_counter),
878  "Emission", 1000.0 * INSTR_TIME_GET_DOUBLE(ji->emission_counter),
879  "Total", 1000.0 * INSTR_TIME_GET_DOUBLE(total_time));
880  }
881 
882  es->indent--;
883  }
884  else
885  {
886  ExplainPropertyInteger("Functions", NULL, ji->created_functions, es);
887 
888  ExplainOpenGroup("Options", "Options", true, es);
889  ExplainPropertyBool("Inlining", jit_flags & PGJIT_INLINE, es);
890  ExplainPropertyBool("Optimization", jit_flags & PGJIT_OPT3, es);
891  ExplainPropertyBool("Expressions", jit_flags & PGJIT_EXPR, es);
892  ExplainPropertyBool("Deforming", jit_flags & PGJIT_DEFORM, es);
893  ExplainCloseGroup("Options", "Options", true, es);
894 
895  if (es->analyze && es->timing)
896  {
897  ExplainOpenGroup("Timing", "Timing", true, es);
898 
899  ExplainPropertyFloat("Generation", "ms",
901  3, es);
902  ExplainPropertyFloat("Inlining", "ms",
904  3, es);
905  ExplainPropertyFloat("Optimization", "ms",
907  3, es);
908  ExplainPropertyFloat("Emission", "ms",
910  3, es);
911  ExplainPropertyFloat("Total", "ms",
912  1000.0 * INSTR_TIME_GET_DOUBLE(total_time),
913  3, es);
914 
915  ExplainCloseGroup("Timing", "Timing", true, es);
916  }
917  }
918 
919  ExplainCloseGroup("JIT", "JIT", true, es);
920 }
void ExplainPropertyBool(const char *qlabel, bool value, ExplainState *es)
Definition: explain.c:4275
#define PGJIT_EXPR
Definition: jit.h:23
void ExplainPropertyInteger(const char *qlabel, const char *unit, int64 value, ExplainState *es)
Definition: explain.c:4234
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:4261
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:4662
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:4353
#define PGJIT_DEFORM
Definition: jit.h:24
void ExplainOpenGroup(const char *objtype, const char *labelname, bool labeled, ExplainState *es)
Definition: explain.c:4290
StringInfo str
Definition: explain.h:39

◆ ExplainPrintJITSummary()

void ExplainPrintJITSummary ( ExplainState es,
QueryDesc queryDesc 
)

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

812 {
813  JitInstrumentation ji = {0};
814 
815  if (!(queryDesc->estate->es_jit_flags & PGJIT_PERFORM))
816  return;
817 
818  /*
819  * Work with a copy instead of modifying the leader state, since this
820  * function may be called twice
821  */
822  if (queryDesc->estate->es_jit)
823  InstrJitAgg(&ji, &queryDesc->estate->es_jit->instr);
824 
825  /* If this process has done JIT in parallel workers, merge stats */
826  if (queryDesc->estate->es_jit_worker_instr)
827  InstrJitAgg(&ji, queryDesc->estate->es_jit_worker_instr);
828 
829  ExplainPrintJIT(es, queryDesc->estate->es_jit_flags, &ji);
830 }
EState * estate
Definition: execdesc.h:48
struct JitContext * es_jit
Definition: execnodes.h:605
struct JitInstrumentation * es_jit_worker_instr
Definition: execnodes.h:606
static void ExplainPrintJIT(ExplainState *es, int jit_flags, JitInstrumentation *ji)
Definition: explain.c:837
JitInstrumentation instr
Definition: jit.h:61
int es_jit_flags
Definition: execnodes.h:604
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 719 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().

720 {
721  Bitmapset *rels_used = NULL;
722  PlanState *ps;
723 
724  /* Set up ExplainState fields associated with this plan tree */
725  Assert(queryDesc->plannedstmt != NULL);
726  es->pstmt = queryDesc->plannedstmt;
727  es->rtable = queryDesc->plannedstmt->rtable;
728  ExplainPreScanNode(queryDesc->planstate, &rels_used);
731  es->rtable_names);
732  es->printed_subplans = NULL;
733 
734  /*
735  * Sometimes we mark a Gather node as "invisible", which means that it's
736  * not to be displayed in EXPLAIN output. The purpose of this is to allow
737  * running regression tests with force_parallel_mode=regress to get the
738  * same results as running the same tests with force_parallel_mode=off.
739  * Such marking is currently only supported on a Gather at the top of the
740  * plan. We skip that node, and we must also hide per-worker detail data
741  * further down in the plan tree.
742  */
743  ps = queryDesc->planstate;
744  if (IsA(ps, GatherState) && ((Gather *) ps->plan)->invisible)
745  {
746  ps = outerPlanState(ps);
747  es->hide_workers = true;
748  }
749  ExplainNode(ps, NIL, NULL, NULL, es);
750 
751  /*
752  * If requested, include information about GUC parameters with values that
753  * don't match the built-in defaults.
754  */
756 }
#define NIL
Definition: pg_list.h:65
static void ExplainPrintSettings(ExplainState *es)
Definition: explain.c:649
#define IsA(nodeptr, _type_)
Definition: nodes.h:579
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:3420
static void ExplainNode(PlanState *planstate, List *ancestors, const char *relationship, const char *plan_name, ExplainState *es)
Definition: explain.c:1106
PlanState * planstate
Definition: execdesc.h:49
#define outerPlanState(node)
Definition: execnodes.h:1033
List * deparse_context_for_plan_tree(PlannedStmt *pstmt, List *rtable_names)
Definition: ruleutils.c:3328
Plan * plan
Definition: execnodes.h:939
#define Assert(condition)
Definition: c.h:746
List * rtable
Definition: plannodes.h:66
static bool ExplainPreScanNode(PlanState *planstate, Bitmapset **rels_used)
Definition: explain.c:1032
PlannedStmt * plannedstmt
Definition: execdesc.h:37
List * rtable
Definition: explain.h:55

◆ ExplainPrintSettings()

static void ExplainPrintSettings ( ExplainState es)
static

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

650 {
651  int num;
652  struct config_generic **gucs;
653 
654  /* bail out if information about settings not requested */
655  if (!es->settings)
656  return;
657 
658  /* request an array of relevant settings */
659  gucs = get_explain_guc_options(&num);
660 
661  if (es->format != EXPLAIN_FORMAT_TEXT)
662  {
663  ExplainOpenGroup("Settings", "Settings", true, es);
664 
665  for (int i = 0; i < num; i++)
666  {
667  char *setting;
668  struct config_generic *conf = gucs[i];
669 
670  setting = GetConfigOptionByName(conf->name, NULL, true);
671 
672  ExplainPropertyText(conf->name, setting, es);
673  }
674 
675  ExplainCloseGroup("Settings", "Settings", true, es);
676  }
677  else
678  {
680 
681  /* In TEXT mode, print nothing if there are no options */
682  if (num <= 0)
683  return;
684 
685  initStringInfo(&str);
686 
687  for (int i = 0; i < num; i++)
688  {
689  char *setting;
690  struct config_generic *conf = gucs[i];
691 
692  if (i > 0)
693  appendStringInfoString(&str, ", ");
694 
695  setting = GetConfigOptionByName(conf->name, NULL, true);
696 
697  if (setting)
698  appendStringInfo(&str, "%s = '%s'", conf->name, setting);
699  else
700  appendStringInfo(&str, "%s = NULL", conf->name);
701  }
702 
703  ExplainPropertyText("Settings", str.data, es);
704  }
705 }
const char * name
Definition: guc_tables.h:149
struct config_generic ** get_explain_guc_options(int *num)
Definition: guc.c:9135
void ExplainPropertyText(const char *qlabel, const char *value, ExplainState *es)
Definition: explain.c:4225
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:9228
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:4353
void ExplainOpenGroup(const char *objtype, const char *labelname, bool labeled, ExplainState *es)
Definition: explain.c:4290
bool settings
Definition: explain.h:48

◆ ExplainPrintTriggers()

void ExplainPrintTriggers ( ExplainState es,
QueryDesc queryDesc 
)

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

769 {
770  ResultRelInfo *rInfo;
771  bool show_relname;
772  List *resultrels;
773  List *routerels;
774  List *targrels;
775  ListCell *l;
776 
777  resultrels = queryDesc->estate->es_opened_result_relations;
778  routerels = queryDesc->estate->es_tuple_routing_result_relations;
779  targrels = queryDesc->estate->es_trig_target_relations;
780 
781  ExplainOpenGroup("Triggers", "Triggers", false, es);
782 
783  show_relname = (list_length(resultrels) > 1 ||
784  routerels != NIL || targrels != NIL);
785  foreach(l, resultrels)
786  {
787  rInfo = (ResultRelInfo *) lfirst(l);
788  report_triggers(rInfo, show_relname, es);
789  }
790 
791  foreach(l, routerels)
792  {
793  rInfo = (ResultRelInfo *) lfirst(l);
794  report_triggers(rInfo, show_relname, es);
795  }
796 
797  foreach(l, targrels)
798  {
799  rInfo = (ResultRelInfo *) lfirst(l);
800  report_triggers(rInfo, show_relname, es);
801  }
802 
803  ExplainCloseGroup("Triggers", "Triggers", false, es);
804 }
#define NIL
Definition: pg_list.h:65
EState * estate
Definition: execdesc.h:48
List * es_opened_result_relations
Definition: execnodes.h:537
static void report_triggers(ResultRelInfo *rInfo, bool show_relname, ExplainState *es)
Definition: explain.c:942
List * es_trig_target_relations
Definition: execnodes.h:550
List * es_tuple_routing_result_relations
Definition: execnodes.h:547
#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:4353
void ExplainOpenGroup(const char *objtype, const char *labelname, bool labeled, ExplainState *es)
Definition: explain.c:4290
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 4172 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().

4174 {
4175  switch (es->format)
4176  {
4177  case EXPLAIN_FORMAT_TEXT:
4178  ExplainIndentText(es);
4179  if (unit)
4180  appendStringInfo(es->str, "%s: %s %s\n", qlabel, value, unit);
4181  else
4182  appendStringInfo(es->str, "%s: %s\n", qlabel, value);
4183  break;
4184 
4185  case EXPLAIN_FORMAT_XML:
4186  {
4187  char *str;
4188 
4189  appendStringInfoSpaces(es->str, es->indent * 2);
4190  ExplainXMLTag(qlabel, X_OPENING | X_NOWHITESPACE, es);
4191  str = escape_xml(value);
4192  appendStringInfoString(es->str, str);
4193  pfree(str);
4194  ExplainXMLTag(qlabel, X_CLOSING | X_NOWHITESPACE, es);
4195  appendStringInfoChar(es->str, '\n');
4196  }
4197  break;
4198 
4199  case EXPLAIN_FORMAT_JSON:
4201  appendStringInfoSpaces(es->str, es->indent * 2);
4202  escape_json(es->str, qlabel);
4203  appendStringInfoString(es->str, ": ");
4204  if (numeric)
4206  else
4207  escape_json(es->str, value);
4208  break;
4209 
4210  case EXPLAIN_FORMAT_YAML:
4212  appendStringInfo(es->str, "%s: ", qlabel);
4213  if (numeric)
4215  else
4216  escape_yaml(es->str, value);
4217  break;
4218  }
4219 }
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:4635
#define X_CLOSING
Definition: explain.c:52
void pfree(void *pointer)
Definition: mcxt.c:1057
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:4697
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:4722
void appendStringInfoSpaces(StringInfo str, int count)
Definition: stringinfo.c:206
static void ExplainJSONLineEnding(ExplainState *es)
Definition: explain.c:4677
static struct @143 value
#define X_OPENING
Definition: explain.c:51
static void ExplainIndentText(ExplainState *es)
Definition: explain.c:4662
#define X_NOWHITESPACE
Definition: explain.c:54
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 4275 of file explain.c.

References ExplainProperty().

Referenced by ExplainNode(), and ExplainPrintJIT().

4276 {
4277  ExplainProperty(qlabel, NULL, value ? "true" : "false", true, es);
4278 }
static struct @143 value
static void ExplainProperty(const char *qlabel, const char *unit, const char *value, bool numeric, ExplainState *es)
Definition: explain.c:4172

◆ ExplainPropertyFloat()

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

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

4263 {
4264  char *buf;
4265 
4266  buf = psprintf("%.*f", ndigits, value);
4267  ExplainProperty(qlabel, unit, buf, true, es);
4268  pfree(buf);
4269 }
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
void pfree(void *pointer)
Definition: mcxt.c:1057
static char * buf
Definition: pg_test_fsync.c:68
static struct @143 value
static void ExplainProperty(const char *qlabel, const char *unit, const char *value, bool numeric, ExplainState *es)
Definition: explain.c:4172

◆ ExplainPropertyInteger()

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

Definition at line 4234 of file explain.c.

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

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

4236 {
4237  char buf[32];
4238 
4239  snprintf(buf, sizeof(buf), INT64_FORMAT, value);
4240  ExplainProperty(qlabel, unit, buf, true, es);
4241 }
static char * buf
Definition: pg_test_fsync.c:68
static struct @143 value
#define INT64_FORMAT
Definition: c.h:417
#define snprintf
Definition: port.h:215
static void ExplainProperty(const char *qlabel, const char *unit, const char *value, bool numeric, ExplainState *es)
Definition: explain.c:4172

◆ ExplainPropertyList()

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

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

4050 {
4051  ListCell *lc;
4052  bool first = true;
4053 
4054  switch (es->format)
4055  {
4056  case EXPLAIN_FORMAT_TEXT:
4057  ExplainIndentText(es);
4058  appendStringInfo(es->str, "%s: ", qlabel);
4059  foreach(lc, data)
4060  {
4061  if (!first)
4062  appendStringInfoString(es->str, ", ");
4063  appendStringInfoString(es->str, (const char *) lfirst(lc));
4064  first = false;
4065  }
4066  appendStringInfoChar(es->str, '\n');
4067  break;
4068 
4069  case EXPLAIN_FORMAT_XML:
4070  ExplainXMLTag(qlabel, X_OPENING, es);
4071  foreach(lc, data)
4072  {
4073  char *str;
4074 
4075  appendStringInfoSpaces(es->str, es->indent * 2 + 2);
4076  appendStringInfoString(es->str, "<Item>");
4077  str = escape_xml((const char *) lfirst(lc));
4078  appendStringInfoString(es->str, str);
4079  pfree(str);
4080  appendStringInfoString(es->str, "</Item>\n");
4081  }
4082  ExplainXMLTag(qlabel, X_CLOSING, es);
4083  break;
4084 
4085  case EXPLAIN_FORMAT_JSON:
4087  appendStringInfoSpaces(es->str, es->indent * 2);
4088  escape_json(es->str, qlabel);
4089  appendStringInfoString(es->str, ": [");
4090  foreach(lc, data)
4091  {
4092  if (!first)
4093  appendStringInfoString(es->str, ", ");
4094  escape_json(es->str, (const char *) lfirst(lc));
4095  first = false;
4096  }
4097  appendStringInfoChar(es->str, ']');
4098  break;
4099 
4100  case EXPLAIN_FORMAT_YAML:
4102  appendStringInfo(es->str, "%s: ", qlabel);
4103  foreach(lc, data)
4104  {
4105  appendStringInfoChar(es->str, '\n');
4106  appendStringInfoSpaces(es->str, es->indent * 2 + 2);
4107  appendStringInfoString(es->str, "- ");
4108  escape_yaml(es->str, (const char *) lfirst(lc));
4109  }
4110  break;
4111  }
4112 }
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:4635
#define X_CLOSING
Definition: explain.c:52
void pfree(void *pointer)
Definition: mcxt.c:1057
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:4697
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:4722
void appendStringInfoSpaces(StringInfo str, int count)
Definition: stringinfo.c:206
static void ExplainJSONLineEnding(ExplainState *es)
Definition: explain.c:4677
#define X_OPENING
Definition: explain.c:51
static void ExplainIndentText(ExplainState *es)
Definition: explain.c:4662
#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 4119 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().

4120 {
4121  ListCell *lc;
4122  bool first = true;
4123 
4124  switch (es->format)
4125  {
4126  case EXPLAIN_FORMAT_TEXT:
4127  case EXPLAIN_FORMAT_XML:
4128  ExplainPropertyList(qlabel, data, es);
4129  return;
4130 
4131  case EXPLAIN_FORMAT_JSON:
4133  appendStringInfoSpaces(es->str, es->indent * 2);
4134  appendStringInfoChar(es->str, '[');
4135  foreach(lc, data)
4136  {
4137  if (!first)
4138  appendStringInfoString(es->str, ", ");
4139  escape_json(es->str, (const char *) lfirst(lc));
4140  first = false;
4141  }
4142  appendStringInfoChar(es->str, ']');
4143  break;
4144 
4145  case EXPLAIN_FORMAT_YAML:
4147  appendStringInfoString(es->str, "- [");
4148  foreach(lc, data)
4149  {
4150  if (!first)
4151  appendStringInfoString(es->str, ", ");
4152  escape_yaml(es->str, (const char *) lfirst(lc));
4153  first = false;
4154  }
4155  appendStringInfoChar(es->str, ']');
4156  break;
4157  }
4158 }
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:4697
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:4722
void appendStringInfoSpaces(StringInfo str, int count)
Definition: stringinfo.c:206
static void ExplainJSONLineEnding(ExplainState *es)
Definition: explain.c:4677
void ExplainPropertyList(const char *qlabel, List *data, ExplainState *es)
Definition: explain.c:4049
#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 4225 of file explain.c.

References ExplainProperty().

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

4226 {
4227  ExplainProperty(qlabel, NULL, value, false, es);
4228 }
static struct @143 value
static void ExplainProperty(const char *qlabel, const char *unit, const char *value, bool numeric, ExplainState *es)
Definition: explain.c:4172

◆ ExplainPropertyUInteger()

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

Definition at line 4247 of file explain.c.

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

Referenced by show_wal_usage().

4249 {
4250  char buf[32];
4251 
4252  snprintf(buf, sizeof(buf), UINT64_FORMAT, value);
4253  ExplainProperty(qlabel, unit, buf, true, es);
4254 }
static char * buf
Definition: pg_test_fsync.c:68
static struct @143 value
#define snprintf
Definition: port.h:215
static void ExplainProperty(const char *qlabel, const char *unit, const char *value, bool numeric, ExplainState *es)
Definition: explain.c:4172
#define UINT64_FORMAT
Definition: c.h:418

◆ ExplainQuery()

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

Definition at line 161 of file explain.c.

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

Referenced by standard_ProcessUtility().

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

◆ ExplainQueryText()

void ExplainQueryText ( ExplainState es,
QueryDesc queryDesc 
)

Definition at line 931 of file explain.c.

References ExplainPropertyText(), and QueryDesc::sourceText.

Referenced by explain_ExecutorEnd().

932 {
933  if (queryDesc->sourceText)
934  ExplainPropertyText("Query Text", queryDesc->sourceText, es);
935 }
void ExplainPropertyText(const char *qlabel, const char *value, ExplainState *es)
Definition: explain.c:4225
const char * sourceText
Definition: execdesc.h:38

◆ ExplainRestoreGroup()

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

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

4470 {
4471  switch (es->format)
4472  {
4473  case EXPLAIN_FORMAT_TEXT:
4474  /* nothing to do */
4475  break;
4476 
4477  case EXPLAIN_FORMAT_XML:
4478  es->indent += depth;
4479  break;
4480 
4481  case EXPLAIN_FORMAT_JSON:
4482  es->grouping_stack = lcons_int(*state_save, es->grouping_stack);
4483  es->indent += depth;
4484  break;
4485 
4486  case EXPLAIN_FORMAT_YAML:
4487  es->grouping_stack = lcons_int(*state_save, es->grouping_stack);
4488  es->indent += depth;
4489  break;
4490  }
4491 }
List * lcons_int(int datum, List *list)
Definition: list.c:471
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 322 of file explain.c.

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

Referenced by ExplainQuery(), and UtilityTupleDescriptor().

323 {
324  TupleDesc tupdesc;
325  ListCell *lc;
326  Oid result_type = TEXTOID;
327 
328  /* Check for XML format option */
329  foreach(lc, stmt->options)
330  {
331  DefElem *opt = (DefElem *) lfirst(lc);
332 
333  if (strcmp(opt->defname, "format") == 0)
334  {
335  char *p = defGetString(opt);
336 
337  if (strcmp(p, "xml") == 0)
338  result_type = XMLOID;
339  else if (strcmp(p, "json") == 0)
340  result_type = JSONOID;
341  else
342  result_type = TEXTOID;
343  /* don't "break", as ExplainQuery will use the last value */
344  }
345  }
346 
347  /* Need a tuple descriptor representing a single TEXT or XML column */
348  tupdesc = CreateTemplateTupleDesc(1);
349  TupleDescInitEntry(tupdesc, (AttrNumber) 1, "QUERY PLAN",
350  result_type, -1, 0);
351  return tupdesc;
352 }
TupleDesc CreateTemplateTupleDesc(int natts)
Definition: tupdesc.c:44
List * options
Definition: parsenodes.h:3259
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:603
#define lfirst(lc)
Definition: pg_list.h:169
char * defname
Definition: parsenodes.h:733
int16 AttrNumber
Definition: attnum.h:21

◆ ExplainSaveGroup()

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

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

4440 {
4441  switch (es->format)
4442  {
4443  case EXPLAIN_FORMAT_TEXT:
4444  /* nothing to do */
4445  break;
4446 
4447  case EXPLAIN_FORMAT_XML:
4448  es->indent -= depth;
4449  break;
4450 
4451  case EXPLAIN_FORMAT_JSON:
4452  es->indent -= depth;
4453  *state_save = linitial_int(es->grouping_stack);
4455  break;
4456 
4457  case EXPLAIN_FORMAT_YAML:
4458  es->indent -= depth;
4459  *state_save = linitial_int(es->grouping_stack);
4461  break;
4462  }
4463 }
#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:860

◆ ExplainScanTarget()

static void ExplainScanTarget ( Scan plan,
ExplainState es 
)
static

Definition at line 3502 of file explain.c.

References ExplainTargetRel().

Referenced by ExplainNode().

3503 {
3504  ExplainTargetRel((Plan *) plan, plan->scanrelid, es);
3505 }
static void ExplainTargetRel(Plan *plan, Index rti, ExplainState *es)
Definition: explain.c:3524

◆ ExplainSeparatePlans()

void ExplainSeparatePlans ( ExplainState es)

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

4607 {
4608  switch (es->format)
4609  {
4610  case EXPLAIN_FORMAT_TEXT:
4611  /* add a blank line */
4612  appendStringInfoChar(es->str, '\n');
4613  break;
4614 
4615  case EXPLAIN_FORMAT_XML:
4616  case EXPLAIN_FORMAT_JSON:
4617  case EXPLAIN_FORMAT_YAML:
4618  /* nothing to do */
4619  break;
4620  }
4621 }
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 3839 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().

3841 {
3842  ListCell *lst;
3843 
3844  foreach(lst, plans)
3845  {
3846  SubPlanState *sps = (SubPlanState *) lfirst(lst);
3847  SubPlan *sp = sps->subplan;
3848 
3849  /*
3850  * There can be multiple SubPlan nodes referencing the same physical
3851  * subplan (same plan_id, which is its index in PlannedStmt.subplans).
3852  * We should print a subplan only once, so track which ones we already
3853  * printed. This state must be global across the plan tree, since the
3854  * duplicate nodes could be in different plan nodes, eg both a bitmap
3855  * indexscan's indexqual and its parent heapscan's recheck qual. (We
3856  * do not worry too much about which plan node we show the subplan as
3857  * attached to in such cases.)
3858  */
3859  if (bms_is_member(sp->plan_id, es->printed_subplans))
3860  continue;
3862  sp->plan_id);
3863 
3864  /*
3865  * Treat the SubPlan node as an ancestor of the plan node(s) within
3866  * it, so that ruleutils.c can find the referents of subplan
3867  * parameters.
3868  */
3869  ancestors = lcons(sp, ancestors);
3870 
3871  ExplainNode(sps->planstate, ancestors,
3872  relationship, sp->plan_name, es);
3873 
3874  ancestors = list_delete_first(ancestors);
3875  }
3876 }
int plan_id
Definition: primnodes.h:709
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:1106
struct PlanState * planstate
Definition: execnodes.h:857
SubPlan * subplan
Definition: execnodes.h:856
List * lcons(void *datum, List *list)
Definition: list.c:453
char * plan_name
Definition: primnodes.h:711
#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:860

◆ ExplainTargetRel()

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

Definition at line 3524 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_TidScan, T_ValuesScan, T_WorkTableScan, and ExplainState::verbose.

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

3525 {
3526  char *objectname = NULL;
3527  char *namespace = NULL;
3528  const char *objecttag = NULL;
3529  RangeTblEntry *rte;
3530  char *refname;
3531 
3532  rte = rt_fetch(rti, es->rtable);
3533  refname = (char *) list_nth(es->rtable_names, rti - 1);
3534  if (refname == NULL)
3535  refname = rte->eref->aliasname;
3536 
3537  switch (nodeTag(plan))
3538  {
3539  case T_SeqScan:
3540  case T_SampleScan:
3541  case T_IndexScan:
3542  case T_IndexOnlyScan:
3543  case T_BitmapHeapScan:
3544  case T_TidScan:
3545  case T_ForeignScan:
3546  case T_CustomScan:
3547  case T_ModifyTable:
3548  /* Assert it's on a real relation */
3549  Assert(rte->rtekind == RTE_RELATION);
3550  objectname = get_rel_name(rte->relid);
3551  if (es->verbose)
3552  namespace = get_namespace_name(get_rel_namespace(rte->relid));
3553  objecttag = "Relation Name";
3554  break;
3555  case T_FunctionScan:
3556  {
3557  FunctionScan *fscan = (FunctionScan *) plan;
3558 
3559  /* Assert it's on a RangeFunction */
3560  Assert(rte->rtekind == RTE_FUNCTION);
3561 
3562  /*
3563  * If the expression is still a function call of a single
3564  * function, we can get the real name of the function.
3565  * Otherwise, punt. (Even if it was a single function call
3566  * originally, the optimizer could have simplified it away.)
3567  */
3568  if (list_length(fscan->functions) == 1)
3569  {
3570  RangeTblFunction *rtfunc = (RangeTblFunction *) linitial(fscan->functions);
3571 
3572  if (IsA(rtfunc->funcexpr, FuncExpr))
3573  {
3574  FuncExpr *funcexpr = (FuncExpr *) rtfunc->funcexpr;
3575  Oid funcid = funcexpr->funcid;
3576 
3577  objectname = get_func_name(funcid);
3578  if (es->verbose)
3579  namespace =
3581  }
3582  }
3583  objecttag = "Function Name";
3584  }
3585  break;
3586  case T_TableFuncScan:
3587  Assert(rte->rtekind == RTE_TABLEFUNC);
3588  objectname = "xmltable";
3589  objecttag = "Table Function Name";
3590  break;
3591  case T_ValuesScan:
3592  Assert(rte->rtekind == RTE_VALUES);
3593  break;
3594  case T_CteScan:
3595  /* Assert it's on a non-self-reference CTE */
3596  Assert(rte->rtekind == RTE_CTE);
3597  Assert(!rte->self_reference);
3598  objectname = rte->ctename;
3599  objecttag = "CTE Name";
3600  break;
3601  case T_NamedTuplestoreScan:
3603  objectname = rte->enrname;
3604  objecttag = "Tuplestore Name";
3605  break;
3606  case T_WorkTableScan:
3607  /* Assert it's on a self-reference CTE */
3608  Assert(rte->rtekind == RTE_CTE);
3609  Assert(rte->self_reference);
3610  objectname = rte->ctename;
3611  objecttag = "CTE Name";
3612  break;
3613  default:
3614  break;
3615  }
3616 
3617  if (es->format == EXPLAIN_FORMAT_TEXT)
3618  {
3619  appendStringInfoString(es->str, " on");
3620  if (namespace != NULL)
3621  appendStringInfo(es->str, " %s.%s", quote_identifier(namespace),
3622  quote_identifier(objectname));
3623  else if (objectname != NULL)
3624  appendStringInfo(es->str, " %s", quote_identifier(objectname));
3625  if (objectname == NULL || strcmp(refname, objectname) != 0)
3626  appendStringInfo(es->str, " %s", quote_identifier(refname));
3627  }
3628  else
3629  {
3630  if (objecttag != NULL && objectname != NULL)
3631  ExplainPropertyText(objecttag, objectname, es);
3632  if (namespace != NULL)
3633  ExplainPropertyText("Schema", namespace, es);
3634  ExplainPropertyText("Alias", refname, es);
3635  }
3636 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:579
const char * quote_identifier(const char *ident)
Definition: ruleutils.c:10709
Oid get_func_namespace(Oid funcid)
Definition: lsyscache.c:1544
List * functions
Definition: plannodes.h:517
Oid get_rel_namespace(Oid relid)
Definition: lsyscache.c:1864
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:4225
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
#define linitial(l)
Definition: pg_list.h:174
Oid funcid
Definition: primnodes.h:469
static void * list_nth(const List *list, int n)
Definition: pg_list.h:266
char * get_func_name(Oid funcid)
Definition: lsyscache.c:1520
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3191
char * enrname
Definition: parsenodes.h:1109
#define rt_fetch(rangetable_index, rangetable)
Definition: parsetree.h:31
bool self_reference
Definition: parsenodes.h:1082
bool verbose
Definition: explain.h:41
#define Assert(condition)
Definition: c.h:746
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:533
RTEKind rtekind
Definition: parsenodes.h:977
char * ctename
Definition: parsenodes.h:1080
Alias * eref
Definition: parsenodes.h:1116
char * get_rel_name(Oid relid)
Definition: lsyscache.c:1840
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 4635 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().

4636 {
4637  const char *s;
4638  const char *valid = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.";
4639 
4640  if ((flags & X_NOWHITESPACE) == 0)
4641  appendStringInfoSpaces(es->str, 2 * es->indent);
4642  appendStringInfoCharMacro(es->str, '<');
4643  if ((flags & X_CLOSING) != 0)
4644  appendStringInfoCharMacro(es->str, '/');
4645  for (s = tagname; *s; s++)
4646  appendStringInfoChar(es->str, strchr(valid, *s) ? *s : '-');
4647  if ((flags & X_CLOSE_IMMEDIATE) != 0)
4648  appendStringInfoString(es->str, " /");
4649  appendStringInfoCharMacro(es->str, '>');
4650  if ((flags & X_NOWHITESPACE) == 0)
4651  appendStringInfoCharMacro(es->str, '\n');
4652 }
#define X_CLOSING
Definition: explain.c:52
#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:54
#define X_CLOSE_IMMEDIATE
Definition: explain.c:53
StringInfo str
Definition: explain.h:39

◆ ExplainYAMLLineStarting()

static void ExplainYAMLLineStarting ( ExplainState es)
static

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

4698 {
4700  if (linitial_int(es->grouping_stack) == 0)
4701  {
4702  linitial_int(es->grouping_stack) = 1;
4703  }
4704  else
4705  {
4706  appendStringInfoChar(es->str, '\n');
4707  appendStringInfoSpaces(es->str, es->indent * 2);
4708  }
4709 }
#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:746
ExplainFormat format
Definition: explain.h:49
StringInfo str
Definition: explain.h:39

◆ NewExplainState()

ExplainState* NewExplainState ( void  )

Definition at line 305 of file explain.c.

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

Referenced by explain_ExecutorEnd(), and ExplainQuery().

306 {
307  ExplainState *es = (ExplainState *) palloc0(sizeof(ExplainState));
308 
309  /* Set default options (most fields can be left as zeroes). */
310  es->costs = true;
311  /* Prepare output buffer. */
312  es->str = makeStringInfo();
313 
314  return es;
315 }
StringInfo makeStringInfo(void)
Definition: stringinfo.c:41
bool costs
Definition: explain.h:43
void * palloc0(Size size)
Definition: mcxt.c:981
StringInfo str
Definition: explain.h:39

◆ report_triggers()

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

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

943 {
944  int nt;
945 
946  if (!rInfo->ri_TrigDesc || !rInfo->ri_TrigInstrument)
947  return;
948  for (nt = 0; nt < rInfo->ri_TrigDesc->numtriggers; nt++)
949  {
950  Trigger *trig = rInfo->ri_TrigDesc->triggers + nt;
951  Instrumentation *instr = rInfo->ri_TrigInstrument + nt;
952  char *relname;
953  char *conname = NULL;
954 
955  /* Must clean up instrumentation state */
956  InstrEndLoop(instr);
957 
958  /*
959  * We ignore triggers that were never invoked; they likely aren't
960  * relevant to the current query type.
961  */
962  if (instr->ntuples == 0)
963  continue;
964 
965  ExplainOpenGroup("Trigger", NULL, true, es);
966 
967  relname = RelationGetRelationName(rInfo->ri_RelationDesc);
968  if (OidIsValid(trig->tgconstraint))
969  conname = get_constraint_name(trig->tgconstraint);
970 
971  /*
972  * In text format, we avoid printing both the trigger name and the
973  * constraint name unless VERBOSE is specified. In non-text formats
974  * we just print everything.
975  */
976  if (es->format == EXPLAIN_FORMAT_TEXT)
977  {
978  if (es->verbose || conname == NULL)
979  appendStringInfo(es->str, "Trigger %s", trig->tgname);
980  else
981  appendStringInfoString(es->str, "Trigger");
982  if (conname)
983  appendStringInfo(es->str, " for constraint %s", conname);
984  if (show_relname)
985  appendStringInfo(es->str, " on %s", relname);
986  if (es->timing)
987  appendStringInfo(es->str, ": time=%.3f calls=%.0f\n",
988  1000.0 * instr->total, instr->ntuples);
989  else
990  appendStringInfo(es->str, ": calls=%.0f\n", instr->ntuples);
991  }
992  else
993  {
994  ExplainPropertyText("Trigger Name", trig->tgname, es);
995  if (conname)
996  ExplainPropertyText("Constraint Name", conname, es);
997  ExplainPropertyText("Relation", relname, es);
998  if (es->timing)
999  ExplainPropertyFloat("Time", "ms", 1000.0 * instr->total, 3,
1000  es);
1001  ExplainPropertyFloat("Calls", NULL, instr->ntuples, 0, es);
1002  }
1003 
1004  if (conname)
1005  pfree(conname);
1006 
1007  ExplainCloseGroup("Trigger", NULL, true, es);
1008  }
1009 }
Relation ri_RelationDesc
Definition: execnodes.h:412
char * get_constraint_name(Oid conoid)
Definition: lsyscache.c:1079
void ExplainPropertyFloat(const char *qlabel, const char *unit, double value, int ndigits, ExplainState *es)
Definition: explain.c:4261
Instrumentation * ri_TrigInstrument
Definition: execnodes.h:433
NameData relname
Definition: pg_class.h:38
#define OidIsValid(objectId)
Definition: c.h:652
void InstrEndLoop(Instrumentation *instr)
Definition: instrument.c:121
void ExplainPropertyText(const char *qlabel, const char *value, ExplainState *es)
Definition: explain.c:4225
void pfree(void *pointer)
Definition: mcxt.c:1057
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
Oid tgconstraint
Definition: reltrigger.h:35
char * tgname
Definition: reltrigger.h:27
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
Trigger * triggers
Definition: reltrigger.h:49
#define RelationGetRelationName(relation)
Definition: rel.h:490
double ntuples
Definition: instrument.h:69
TriggerDesc * ri_TrigDesc
Definition: execnodes.h:424
bool timing
Definition: explain.h:46
int numtriggers
Definition: reltrigger.h:50
bool verbose
Definition: explain.h:41
ExplainFormat format
Definition: explain.h:49
void ExplainCloseGroup(const char *objtype, const char *labelname, bool labeled, ExplainState *es)
Definition: explain.c:4353
void ExplainOpenGroup(const char *objtype, const char *labelname, bool labeled, ExplainState *es)
Definition: explain.c:4290
StringInfo str
Definition: explain.h:39

◆ show_agg_keys()

static void show_agg_keys ( AggState astate,
List ancestors,
ExplainState es 
)
static

Definition at line 2315 of file explain.c.

References Agg::groupingSets, Agg::grpColIdx, lcons(), list_delete_first(), Agg::numCols, outerPlanState, PlanState::plan, ScanState::ps, show_grouping_sets(), show_sort_group_keys(), and AggState::ss.

Referenced by ExplainNode().

2317 {
2318  Agg *plan = (Agg *) astate->ss.ps.plan;
2319 
2320  if (plan->numCols > 0 || plan->groupingSets)
2321  {
2322  /* The key columns refer to the tlist of the child plan */
2323  ancestors = lcons(plan, ancestors);
2324 
2325  if (plan->groupingSets)
2326  show_grouping_sets(outerPlanState(astate), plan, ancestors, es);
2327  else
2328  show_sort_group_keys(outerPlanState(astate), "Group Key",
2329  plan->numCols, 0, plan->grpColIdx,
2330  NULL, NULL, NULL,
2331  ancestors, es);
2332 
2333  ancestors = list_delete_first(ancestors);
2334  }
2335 }
int numCols
Definition: plannodes.h:821
AttrNumber * grpColIdx
Definition: plannodes.h:822
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)
Definition: explain.c:2467
ScanState ss
Definition: execnodes.h:2143
PlanState ps
Definition: execnodes.h:1328
#define outerPlanState(node)
Definition: execnodes.h:1033
static void show_grouping_sets(PlanState *planstate, Agg *agg, List *ancestors, ExplainState *es)
Definition: explain.c:2338
List * groupingSets
Definition: plannodes.h:829
Plan * plan
Definition: execnodes.h:939
List * lcons(void *datum, List *list)
Definition: list.c:453
Definition: plannodes.h:816
List * list_delete_first(List *list)
Definition: list.c:860

◆ show_buffer_usage()