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

References INSTR_TIME_GET_DOUBLE, INSTR_TIME_SET_CURRENT, and INSTR_TIME_SUBTRACT.

Referenced by ExplainOnePlan(), and IsCheckpointOnSchedule().

1030 {
1031  instr_time endtime;
1032 
1033  INSTR_TIME_SET_CURRENT(endtime);
1034  INSTR_TIME_SUBTRACT(endtime, *starttime);
1035  return INSTR_TIME_GET_DOUBLE(endtime);
1036 }
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 4738 of file explain.c.

References escape_json().

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

4739 {
4740  escape_json(buf, str);
4741 }
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 3283 of file explain.c.

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

Referenced by ExplainIndexScanDetails(), and ExplainNode().

3284 {
3285  const char *result;
3286 
3288  result = (*explain_get_index_name_hook) (indexId);
3289  else
3290  result = NULL;
3291  if (result == NULL)
3292  {
3293  /* default behavior: look it up in the catalogs */
3294  result = get_rel_name(indexId);
3295  if (result == NULL)
3296  elog(ERROR, "cache lookup failed for index %u", indexId);
3297  }
3298  return result;
3299 }
#define ERROR
Definition: elog.h:45
explain_get_index_name_hook_type explain_get_index_name_hook
Definition: explain.c:47
#define elog(elevel,...)
Definition: elog.h:228
char * get_rel_name(Oid relid)
Definition: lsyscache.c:1872

◆ ExplainBeginOutput()

void ExplainBeginOutput ( ExplainState es)

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

4563 {
4564  switch (es->format)
4565  {
4566  case EXPLAIN_FORMAT_TEXT:
4567  /* nothing to do */
4568  break;
4569 
4570  case EXPLAIN_FORMAT_XML:
4572  "<explain xmlns=\"http://www.postgresql.org/2009/explain\">\n");
4573  es->indent++;
4574  break;
4575 
4576  case EXPLAIN_FORMAT_JSON:
4577  /* top-level structure is an array of plans */
4578  appendStringInfoChar(es->str, '[');
4579  es->grouping_stack = lcons_int(0, es->grouping_stack);
4580  es->indent++;
4581  break;
4582 
4583  case EXPLAIN_FORMAT_YAML:
4584  es->grouping_stack = lcons_int(0, es->grouping_stack);
4585  break;
4586  }
4587 }
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 4369 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().

4371 {
4372  switch (es->format)
4373  {
4374  case EXPLAIN_FORMAT_TEXT:
4375  /* nothing to do */
4376  break;
4377 
4378  case EXPLAIN_FORMAT_XML:
4379  es->indent--;
4380  ExplainXMLTag(objtype, X_CLOSING, es);
4381  break;
4382 
4383  case EXPLAIN_FORMAT_JSON:
4384  es->indent--;
4385  appendStringInfoChar(es->str, '\n');
4386  appendStringInfoSpaces(es->str, 2 * es->indent);
4387  appendStringInfoChar(es->str, labeled ? '}' : ']');
4389  break;
4390 
4391  case EXPLAIN_FORMAT_YAML:
4392  es->indent--;
4394  break;
4395  }
4396 }
static void ExplainXMLTag(const char *tagname, int flags, ExplainState *es)
Definition: explain.c:4651
#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 3999 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().

4000 {
4001  ExplainWorkersState *wstate = es->workers_state;
4002 
4003  Assert(wstate);
4004  Assert(n >= 0 && n < wstate->num_workers);
4005  Assert(wstate->worker_inited[n]);
4006 
4007  /*
4008  * Save formatting state in case we do another ExplainOpenWorker(), then
4009  * pop the formatting stack.
4010  */
4011  ExplainSaveGroup(es, 2, &wstate->worker_state_save[n]);
4012 
4013  /*
4014  * In TEXT format, if we didn't actually produce any output line(s) then
4015  * truncate off the partial line emitted by ExplainOpenWorker. (This is
4016  * to avoid bogus output if, say, show_buffer_usage chooses not to print
4017  * anything for the worker.) Also fix up the indent level.
4018  */
4019  if (es->format == EXPLAIN_FORMAT_TEXT)
4020  {
4021  while (es->str->len > 0 && es->str->data[es->str->len - 1] != '\n')
4022  es->str->data[--(es->str->len)] = '\0';
4023 
4024  es->indent--;
4025  }
4026 
4027  /* Restore prior output buffer pointer */
4028  es->str = wstate->prev_str;
4029 }
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:792
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:4455
StringInfo str
Definition: explain.h:39

◆ ExplainCreateWorkersState()

static ExplainWorkersState * ExplainCreateWorkersState ( int  num_workers)
static

Definition at line 3920 of file explain.c.

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

Referenced by ExplainNode().

3921 {
3922  ExplainWorkersState *wstate;
3923 
3924  wstate = (ExplainWorkersState *) palloc(sizeof(ExplainWorkersState));
3925  wstate->num_workers = num_workers;
3926  wstate->worker_inited = (bool *) palloc0(num_workers * sizeof(bool));
3927  wstate->worker_str = (StringInfoData *)
3928  palloc0(num_workers * sizeof(StringInfoData));
3929  wstate->worker_state_save = (int *) palloc(num_workers * sizeof(int));
3930  return wstate;
3931 }
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 3898 of file explain.c.

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

Referenced by ExplainNode().

3899 {
3900  ListCell *cell;
3901  const char *label =
3902  (list_length(css->custom_ps) != 1 ? "children" : "child");
3903 
3904  foreach(cell, css->custom_ps)
3905  ExplainNode((PlanState *) lfirst(cell), ancestors, label, NULL, es);
3906 }
List * custom_ps
Definition: execnodes.h:1808
static void ExplainNode(PlanState *planstate, List *ancestors, const char *relationship, const char *plan_name, ExplainState *es)
Definition: explain.c:1122
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 4516 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().

4517 {
4518  switch (es->format)
4519  {
4520  case EXPLAIN_FORMAT_TEXT:
4521  /* nothing to do */
4522  break;
4523 
4524  case EXPLAIN_FORMAT_XML:
4525  ExplainXMLTag(objtype, X_CLOSE_IMMEDIATE, es);
4526  break;
4527 
4528  case EXPLAIN_FORMAT_JSON:
4530  appendStringInfoSpaces(es->str, 2 * es->indent);
4531  if (labelname)
4532  {
4533  escape_json(es->str, labelname);
4534  appendStringInfoString(es->str, ": ");
4535  }
4536  escape_json(es->str, objtype);
4537  break;
4538 
4539  case EXPLAIN_FORMAT_YAML:
4541  if (labelname)
4542  {
4543  escape_yaml(es->str, labelname);
4544  appendStringInfoString(es->str, ": ");
4545  }
4546  else
4547  {
4548  appendStringInfoString(es->str, "- ");
4549  }
4550  escape_yaml(es->str, objtype);
4551  break;
4552  }
4553 }
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:4651
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
static void ExplainYAMLLineStarting(ExplainState *es)
Definition: explain.c:4713
int indent
Definition: explain.h:51
static void escape_yaml(StringInfo buf, const char *str)
Definition: explain.c:4738
void appendStringInfoSpaces(StringInfo str, int count)
Definition: stringinfo.c:206
static void ExplainJSONLineEnding(ExplainState *es)
Definition: explain.c:4693
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 4593 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().

4594 {
4595  switch (es->format)
4596  {
4597  case EXPLAIN_FORMAT_TEXT:
4598  /* nothing to do */
4599  break;
4600 
4601  case EXPLAIN_FORMAT_XML:
4602  es->indent--;
4603  appendStringInfoString(es->str, "</explain>");
4604  break;
4605 
4606  case EXPLAIN_FORMAT_JSON:
4607  es->indent--;
4608  appendStringInfoString(es->str, "\n]");
4610  break;
4611 
4612  case EXPLAIN_FORMAT_YAML:
4614  break;
4615  }
4616 }
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 4035 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().

4036 {
4037  ExplainWorkersState *wstate = es->workers_state;
4038 
4039  ExplainOpenGroup("Workers", "Workers", false, es);
4040  for (int i = 0; i < wstate->num_workers; i++)
4041  {
4042  if (wstate->worker_inited[i])
4043  {
4044  /* This must match previous ExplainOpenSetAsideGroup call */
4045  ExplainOpenGroup("Worker", NULL, true, es);
4046  appendStringInfoString(es->str, wstate->worker_str[i].data);
4047  ExplainCloseGroup("Worker", NULL, true, es);
4048 
4049  pfree(wstate->worker_str[i].data);
4050  }
4051  }
4052  ExplainCloseGroup("Workers", "Workers", false, es);
4053 
4054  pfree(wstate->worker_inited);
4055  pfree(wstate->worker_str);
4056  pfree(wstate->worker_state_save);
4057  pfree(wstate);
4058 }
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:4369
void ExplainOpenGroup(const char *objtype, const char *labelname, bool labeled, ExplainState *es)
Definition: explain.c:4306
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 3479 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().

3481 {
3482  const char *indexname = explain_get_index_name(indexid);
3483 
3484  if (es->format == EXPLAIN_FORMAT_TEXT)
3485  {
3486  if (ScanDirectionIsBackward(indexorderdir))
3487  appendStringInfoString(es->str, " Backward");
3488  appendStringInfo(es->str, " using %s", quote_identifier(indexname));
3489  }
3490  else
3491  {
3492  const char *scandir;
3493 
3494  switch (indexorderdir)
3495  {
3496  case BackwardScanDirection:
3497  scandir = "Backward";
3498  break;
3500  scandir = "NoMovement";
3501  break;
3502  case ForwardScanDirection:
3503  scandir = "Forward";
3504  break;
3505  default:
3506  scandir = "???";
3507  break;
3508  }
3509  ExplainPropertyText("Scan Direction", scandir, es);
3510  ExplainPropertyText("Index Name", indexname, es);
3511  }
3512 }
const char * quote_identifier(const char *ident)
Definition: ruleutils.c:10933
#define ScanDirectionIsBackward(direction)
Definition: sdir.h:41
void ExplainPropertyText(const char *qlabel, const char *value, ExplainState *es)
Definition: explain.c:4241
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:3283
StringInfo str
Definition: explain.h:39

◆ ExplainJSONLineEnding()

static void ExplainJSONLineEnding ( ExplainState es)
static

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

4694 {
4696  if (linitial_int(es->grouping_stack) != 0)
4697  appendStringInfoChar(es->str, ',');
4698  else
4699  linitial_int(es->grouping_stack) = 1;
4700  appendStringInfoChar(es->str, '\n');
4701 }
#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:792
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 3823 of file explain.c.

References ExplainNode().

Referenced by ExplainNode().

3825 {
3826  int j;
3827 
3828  for (j = 0; j < nplans; j++)
3829  ExplainNode(planstates[j], ancestors,
3830  "Member", NULL, es);
3831 }
static void ExplainNode(PlanState *planstate, List *ancestors, const char *relationship, const char *plan_name, ExplainState *es)
Definition: explain.c:1122

◆ ExplainMissingMembers()

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

Definition at line 3841 of file explain.c.

References EXPLAIN_FORMAT_TEXT, ExplainPropertyInteger(), and format.

Referenced by ExplainNode().

3842 {
3843  if (nplans < nchildren || es->format != EXPLAIN_FORMAT_TEXT)
3844  ExplainPropertyInteger("Subplans Removed", NULL,
3845  nchildren - nplans, es);
3846 }
void ExplainPropertyInteger(const char *qlabel, const char *unit, int64 value, ExplainState *es)
Definition: explain.c:4250
static char format

◆ ExplainModifyTarget()

static void ExplainModifyTarget ( ModifyTable plan,
ExplainState es 
)
static

Definition at line 3531 of file explain.c.

References ExplainTargetRel().

Referenced by ExplainNode().

3532 {
3533  ExplainTargetRel((Plan *) plan, plan->nominalRelation, es);
3534 }
static void ExplainTargetRel(Plan *plan, Index rti, ExplainState *es)
Definition: explain.c:3540

◆ ExplainNode()

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

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

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

516 {
518  QueryDesc *queryDesc;
519  instr_time starttime;
520  double totaltime = 0;
521  int eflags;
522  int instrument_option = 0;
523 
524  Assert(plannedstmt->commandType != CMD_UTILITY);
525 
526  if (es->analyze && es->timing)
527  instrument_option |= INSTRUMENT_TIMER;
528  else if (es->analyze)
529  instrument_option |= INSTRUMENT_ROWS;
530 
531  if (es->buffers)
532  instrument_option |= INSTRUMENT_BUFFERS;
533  if (es->wal)
534  instrument_option |= INSTRUMENT_WAL;
535 
536  /*
537  * We always collect timing for the entire statement, even when node-level
538  * timing is off, so we don't look at es->timing here. (We could skip
539  * this if !es->summary, but it's hardly worth the complication.)
540  */
541  INSTR_TIME_SET_CURRENT(starttime);
542 
543  /*
544  * Use a snapshot with an updated command ID to ensure this query sees
545  * results of any previously executed queries.
546  */
549 
550  /*
551  * Normally we discard the query's output, but if explaining CREATE TABLE
552  * AS, we'd better use the appropriate tuple receiver.
553  */
554  if (into)
555  dest = CreateIntoRelDestReceiver(into);
556  else
557  dest = None_Receiver;
558 
559  /* Create a QueryDesc for the query */
560  queryDesc = CreateQueryDesc(plannedstmt, queryString,
562  dest, params, queryEnv, instrument_option);
563 
564  /* Select execution options */
565  if (es->analyze)
566  eflags = 0; /* default run-to-completion flags */
567  else
568  eflags = EXEC_FLAG_EXPLAIN_ONLY;
569  if (into)
570  eflags |= GetIntoRelEFlags(into);
571 
572  /* call ExecutorStart to prepare the plan for execution */
573  ExecutorStart(queryDesc, eflags);
574 
575  /* Execute the plan for statistics if asked for */
576  if (es->analyze)
577  {
578  ScanDirection dir;
579 
580  /* EXPLAIN ANALYZE CREATE TABLE AS WITH NO DATA is weird */
581  if (into && into->skipData)
583  else
584  dir = ForwardScanDirection;
585 
586  /* run the plan */
587  ExecutorRun(queryDesc, dir, 0L, true);
588 
589  /* run cleanup too */
590  ExecutorFinish(queryDesc);
591 
592  /* We can't run ExecutorEnd 'till we're done printing the stats... */
593  totaltime += elapsed_time(&starttime);
594  }
595 
596  ExplainOpenGroup("Query", NULL, true, es);
597 
598  /* Create textual dump of plan tree */
599  ExplainPrintPlan(es, queryDesc);
600 
601  /* Show buffer usage in planning */
602  if (bufusage)
603  {
604  ExplainOpenGroup("Planning", "Planning", true, es);
605  show_buffer_usage(es, bufusage, true);
606  ExplainCloseGroup("Planning", "Planning", true, es);
607  }
608 
609  if (es->summary && planduration)
610  {
611  double plantime = INSTR_TIME_GET_DOUBLE(*planduration);
612 
613  ExplainPropertyFloat("Planning Time", "ms", 1000.0 * plantime, 3, es);
614  }
615 
616  /* Print info about runtime of triggers */
617  if (es->analyze)
618  ExplainPrintTriggers(es, queryDesc);
619 
620  /*
621  * Print info about JITing. Tied to es->costs because we don't want to
622  * display this in regression tests, as it'd cause output differences
623  * depending on build options. Might want to separate that out from COSTS
624  * at a later stage.
625  */
626  if (es->costs)
627  ExplainPrintJITSummary(es, queryDesc);
628 
629  /*
630  * Close down the query and free resources. Include time for this in the
631  * total execution time (although it should be pretty minimal).
632  */
633  INSTR_TIME_SET_CURRENT(starttime);
634 
635  ExecutorEnd(queryDesc);
636 
637  FreeQueryDesc(queryDesc);
638 
640 
641  /* We need a CCI just in case query expanded to multiple plans */
642  if (es->analyze)
644 
645  totaltime += elapsed_time(&starttime);
646 
647  /*
648  * We only report execution time if we actually ran the query (that is,
649  * the user specified ANALYZE), and if summary reporting is enabled (the
650  * user can set SUMMARY OFF to not have the timing information included in
651  * the output). By default, ANALYZE sets SUMMARY to true.
652  */
653  if (es->summary && es->analyze)
654  ExplainPropertyFloat("Execution Time", "ms", 1000.0 * totaltime, 3,
655  es);
656 
657  ExplainCloseGroup("Query", NULL, true, es);
658 }
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:4277
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:381
void ExplainPrintTriggers(ExplainState *es, QueryDesc *queryDesc)
Definition: explain.c:784
void ExplainPrintPlan(ExplainState *es, QueryDesc *queryDesc)
Definition: explain.c:735
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:434
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:792
bool wal
Definition: explain.h:45
static double elapsed_time(instr_time *starttime)
Definition: explain.c:1029
#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:3305
bool buffers
Definition: explain.h:44
void ExplainCloseGroup(const char *objtype, const char *labelname, bool labeled, ExplainState *es)
Definition: explain.c:4369
void ExplainPrintJITSummary(ExplainState *es, QueryDesc *queryDesc)
Definition: explain.c:827
void ExplainOpenGroup(const char *objtype, const char *labelname, bool labeled, ExplainState *es)
Definition: explain.c:4306
#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:512
#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:858
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, CreateTableAsRelExists(), CURSOR_OPT_PARALLEL_OK, elog, ERROR, EXPLAIN_FORMAT_TEXT, ExplainDummyGroup(), ExplainExecuteQuery(), ExplainOneQuery(), ExplainState::format, CreateTableAsStmt::into, IsA, linitial_node, list_length(), OBJECT_MATVIEW, OBJECT_TABLE, CreateTableAsStmt::objtype, DeclareCursorStmt::options, DeclareCursorStmt::query, CreateTableAsStmt::query, QueryRewrite(), and ExplainState::str.

Referenced by ExplainExecuteQuery(), and ExplainOneQuery().

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  /*
439  * Check if the relation exists or not. This is done at this stage to
440  * avoid query planning or execution.
441  */
442  if (CreateTableAsRelExists(ctas))
443  {
444  if (ctas->objtype == OBJECT_TABLE)
445  ExplainDummyGroup("CREATE TABLE AS", NULL, es);
446  else if (ctas->objtype == OBJECT_MATVIEW)
447  ExplainDummyGroup("CREATE MATERIALIZED VIEW", NULL, es);
448  else
449  elog(ERROR, "unexpected object type: %d",
450  (int) ctas->objtype);
451  return;
452  }
453 
454  rewritten = QueryRewrite(castNode(Query, copyObject(ctas->query)));
455  Assert(list_length(rewritten) == 1);
456  ExplainOneQuery(linitial_node(Query, rewritten),
457  CURSOR_OPT_PARALLEL_OK, ctas->into, es,
458  queryString, params, queryEnv);
459  }
460  else if (IsA(utilityStmt, DeclareCursorStmt))
461  {
462  /*
463  * Likewise for DECLARE CURSOR.
464  *
465  * Notice that if you say EXPLAIN ANALYZE DECLARE CURSOR then we'll
466  * actually run the query. This is different from pre-8.3 behavior
467  * but seems more useful than not running the query. No cursor will
468  * be created, however.
469  */
470  DeclareCursorStmt *dcs = (DeclareCursorStmt *) utilityStmt;
471  List *rewritten;
472 
473  rewritten = QueryRewrite(castNode(Query, copyObject(dcs->query)));
474  Assert(list_length(rewritten) == 1);
475  ExplainOneQuery(linitial_node(Query, rewritten),
476  dcs->options, NULL, es,
477  queryString, params, queryEnv);
478  }
479  else if (IsA(utilityStmt, ExecuteStmt))
480  ExplainExecuteQuery((ExecuteStmt *) utilityStmt, into, es,
481  queryString, params, queryEnv);
482  else if (IsA(utilityStmt, NotifyStmt))
483  {
484  if (es->format == EXPLAIN_FORMAT_TEXT)
485  appendStringInfoString(es->str, "NOTIFY\n");
486  else
487  ExplainDummyGroup("Notify", NULL, es);
488  }
489  else
490  {
491  if (es->format == EXPLAIN_FORMAT_TEXT)
493  "Utility statements have no plan structure\n");
494  else
495  ExplainDummyGroup("Utility Statement", NULL, es);
496  }
497 }
#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
ObjectType objtype
Definition: parsenodes.h:3289
static void ExplainDummyGroup(const char *objtype, const char *labelname, ExplainState *es)
Definition: explain.c:4516
#define ERROR
Definition: elog.h:45
void ExplainExecuteQuery(ExecuteStmt *execstmt, IntoClause *into, ExplainState *es, const char *queryString, ParamListInfo params, QueryEnvironment *queryEnv)
Definition: prepare.c:604
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
bool CreateTableAsRelExists(CreateTableAsStmt *ctas)
Definition: createas.c:399
static void ExplainOneQuery(Query *query, int cursorOptions, IntoClause *into, ExplainState *es, const char *queryString, ParamListInfo params, QueryEnvironment *queryEnv)
Definition: explain.c:361
IntoClause * into
Definition: parsenodes.h:3288
#define Assert(condition)
Definition: c.h:792
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:2732
#define elog(elevel,...)
Definition: elog.h:228
#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 4306 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().

4308 {
4309  switch (es->format)
4310  {
4311  case EXPLAIN_FORMAT_TEXT:
4312  /* nothing to do */
4313  break;
4314 
4315  case EXPLAIN_FORMAT_XML:
4316  ExplainXMLTag(objtype, X_OPENING, es);
4317  es->indent++;
4318  break;
4319 
4320  case EXPLAIN_FORMAT_JSON:
4322  appendStringInfoSpaces(es->str, 2 * es->indent);
4323  if (labelname)
4324  {
4325  escape_json(es->str, labelname);
4326  appendStringInfoString(es->str, ": ");
4327  }
4328  appendStringInfoChar(es->str, labeled ? '{' : '[');
4329 
4330  /*
4331  * In JSON format, the grouping_stack is an integer list. 0 means
4332  * we've emitted nothing at this grouping level, 1 means we've
4333  * emitted something (and so the next item needs a comma). See
4334  * ExplainJSONLineEnding().
4335  */
4336  es->grouping_stack = lcons_int(0, es->grouping_stack);
4337  es->indent++;
4338  break;
4339 
4340  case EXPLAIN_FORMAT_YAML:
4341 
4342  /*
4343  * In YAML format, the grouping stack is an integer list. 0 means
4344  * we've emitted nothing at this grouping level AND this grouping
4345  * level is unlabeled and must be marked with "- ". See
4346  * ExplainYAMLLineStarting().
4347  */
4349  if (labelname)
4350  {
4351  appendStringInfo(es->str, "%s: ", labelname);
4352  es->grouping_stack = lcons_int(1, es->grouping_stack);
4353  }
4354  else
4355  {
4356  appendStringInfoString(es->str, "- ");
4357  es->grouping_stack = lcons_int(0, es->grouping_stack);
4358  }
4359  es->indent++;
4360  break;
4361  }
4362 }
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:4651
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:4713
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:4693
#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 4416 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().

4418 {
4419  switch (es->format)
4420  {
4421  case EXPLAIN_FORMAT_TEXT:
4422  /* nothing to do */
4423  break;
4424 
4425  case EXPLAIN_FORMAT_XML:
4426  es->indent += depth;
4427  break;
4428 
4429  case EXPLAIN_FORMAT_JSON:
4430  es->grouping_stack = lcons_int(0, es->grouping_stack);
4431  es->indent += depth;
4432  break;
4433 
4434  case EXPLAIN_FORMAT_YAML:
4435  if (labelname)
4436  es->grouping_stack = lcons_int(1, es->grouping_stack);
4437  else
4438  es->grouping_stack = lcons_int(0, es->grouping_stack);
4439  es->indent += depth;
4440  break;
4441  }
4442 }
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 3937 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().

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

◆ ExplainPreScanNode()

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

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

1049 {
1050  Plan *plan = planstate->plan;
1051 
1052  switch (nodeTag(plan))
1053  {
1054  case T_SeqScan:
1055  case T_SampleScan:
1056  case T_IndexScan:
1057  case T_IndexOnlyScan:
1058  case T_BitmapHeapScan:
1059  case T_TidScan:
1060  case T_SubqueryScan:
1061  case T_FunctionScan:
1062  case T_TableFuncScan:
1063  case T_ValuesScan:
1064  case T_CteScan:
1065  case T_NamedTuplestoreScan:
1066  case T_WorkTableScan:
1067  *rels_used = bms_add_member(*rels_used,
1068  ((Scan *) plan)->scanrelid);
1069  break;
1070  case T_ForeignScan:
1071  *rels_used = bms_add_members(*rels_used,
1072  ((ForeignScan *) plan)->fs_relids);
1073  break;
1074  case T_CustomScan:
1075  *rels_used = bms_add_members(*rels_used,
1076  ((CustomScan *) plan)->custom_relids);
1077  break;
1078  case T_ModifyTable:
1079  *rels_used = bms_add_member(*rels_used,
1080  ((ModifyTable *) plan)->nominalRelation);
1081  if (((ModifyTable *) plan)->exclRelRTI)
1082  *rels_used = bms_add_member(*rels_used,
1083  ((ModifyTable *) plan)->exclRelRTI);
1084  break;
1085  case T_Append:
1086  *rels_used = bms_add_members(*rels_used,
1087  ((Append *) plan)->apprelids);
1088  break;
1089  case T_MergeAppend:
1090  *rels_used = bms_add_members(*rels_used,
1091  ((MergeAppend *) plan)->apprelids);
1092  break;
1093  default:
1094  break;
1095  }
1096 
1097  return planstate_tree_walker(planstate, ExplainPreScanNode, rels_used);
1098 }
Definition: nodes.h:49
Plan * plan
Definition: execnodes.h:928
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:1048
bool planstate_tree_walker(PlanState *planstate, bool(*walker)(), void *context)
Definition: nodeFuncs.c:3932
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 853 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().

854 {
855  instr_time total_time;
856 
857  /* don't print information if no JITing happened */
858  if (!ji || ji->created_functions == 0)
859  return;
860 
861  /* calculate total time */
862  INSTR_TIME_SET_ZERO(total_time);
863  INSTR_TIME_ADD(total_time, ji->generation_counter);
864  INSTR_TIME_ADD(total_time, ji->inlining_counter);
865  INSTR_TIME_ADD(total_time, ji->optimization_counter);
866  INSTR_TIME_ADD(total_time, ji->emission_counter);
867 
868  ExplainOpenGroup("JIT", "JIT", true, es);
869 
870  /* for higher density, open code the text output format */
871  if (es->format == EXPLAIN_FORMAT_TEXT)
872  {
873  ExplainIndentText(es);
874  appendStringInfoString(es->str, "JIT:\n");
875  es->indent++;
876 
877  ExplainPropertyInteger("Functions", NULL, ji->created_functions, es);
878 
879  ExplainIndentText(es);
880  appendStringInfo(es->str, "Options: %s %s, %s %s, %s %s, %s %s\n",
881  "Inlining", jit_flags & PGJIT_INLINE ? "true" : "false",
882  "Optimization", jit_flags & PGJIT_OPT3 ? "true" : "false",
883  "Expressions", jit_flags & PGJIT_EXPR ? "true" : "false",
884  "Deforming", jit_flags & PGJIT_DEFORM ? "true" : "false");
885 
886  if (es->analyze && es->timing)
887  {
888  ExplainIndentText(es);
889  appendStringInfo(es->str,
890  "Timing: %s %.3f ms, %s %.3f ms, %s %.3f ms, %s %.3f ms, %s %.3f ms\n",
891  "Generation", 1000.0 * INSTR_TIME_GET_DOUBLE(ji->generation_counter),
892  "Inlining", 1000.0 * INSTR_TIME_GET_DOUBLE(ji->inlining_counter),
893  "Optimization", 1000.0 * INSTR_TIME_GET_DOUBLE(ji->optimization_counter),
894  "Emission", 1000.0 * INSTR_TIME_GET_DOUBLE(ji->emission_counter),
895  "Total", 1000.0 * INSTR_TIME_GET_DOUBLE(total_time));
896  }
897 
898  es->indent--;
899  }
900  else
901  {
902  ExplainPropertyInteger("Functions", NULL, ji->created_functions, es);
903 
904  ExplainOpenGroup("Options", "Options", true, es);
905  ExplainPropertyBool("Inlining", jit_flags & PGJIT_INLINE, es);
906  ExplainPropertyBool("Optimization", jit_flags & PGJIT_OPT3, es);
907  ExplainPropertyBool("Expressions", jit_flags & PGJIT_EXPR, es);
908  ExplainPropertyBool("Deforming", jit_flags & PGJIT_DEFORM, es);
909  ExplainCloseGroup("Options", "Options", true, es);
910 
911  if (es->analyze && es->timing)
912  {
913  ExplainOpenGroup("Timing", "Timing", true, es);
914 
915  ExplainPropertyFloat("Generation", "ms",
917  3, es);
918  ExplainPropertyFloat("Inlining", "ms",
920  3, es);
921  ExplainPropertyFloat("Optimization", "ms",
923  3, es);
924  ExplainPropertyFloat("Emission", "ms",
926  3, es);
927  ExplainPropertyFloat("Total", "ms",
928  1000.0 * INSTR_TIME_GET_DOUBLE(total_time),
929  3, es);
930 
931  ExplainCloseGroup("Timing", "Timing", true, es);
932  }
933  }
934 
935  ExplainCloseGroup("JIT", "JIT", true, es);
936 }
void ExplainPropertyBool(const char *qlabel, bool value, ExplainState *es)
Definition: explain.c:4291
#define PGJIT_EXPR
Definition: jit.h:23
void ExplainPropertyInteger(const char *qlabel, const char *unit, int64 value, ExplainState *es)
Definition: explain.c:4250
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:4277
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:4678
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:4369
#define PGJIT_DEFORM
Definition: jit.h:24
void ExplainOpenGroup(const char *objtype, const char *labelname, bool labeled, ExplainState *es)
Definition: explain.c:4306
StringInfo str
Definition: explain.h:39

◆ ExplainPrintJITSummary()

void ExplainPrintJITSummary ( ExplainState es,
QueryDesc queryDesc 
)

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

828 {
829  JitInstrumentation ji = {0};
830 
831  if (!(queryDesc->estate->es_jit_flags & PGJIT_PERFORM))
832  return;
833 
834  /*
835  * Work with a copy instead of modifying the leader state, since this
836  * function may be called twice
837  */
838  if (queryDesc->estate->es_jit)
839  InstrJitAgg(&ji, &queryDesc->estate->es_jit->instr);
840 
841  /* If this process has done JIT in parallel workers, merge stats */
842  if (queryDesc->estate->es_jit_worker_instr)
843  InstrJitAgg(&ji, queryDesc->estate->es_jit_worker_instr);
844 
845  ExplainPrintJIT(es, queryDesc->estate->es_jit_flags, &ji);
846 }
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:853
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 735 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().

736 {
737  Bitmapset *rels_used = NULL;
738  PlanState *ps;
739 
740  /* Set up ExplainState fields associated with this plan tree */
741  Assert(queryDesc->plannedstmt != NULL);
742  es->pstmt = queryDesc->plannedstmt;
743  es->rtable = queryDesc->plannedstmt->rtable;
744  ExplainPreScanNode(queryDesc->planstate, &rels_used);
747  es->rtable_names);
748  es->printed_subplans = NULL;
749 
750  /*
751  * Sometimes we mark a Gather node as "invisible", which means that it's
752  * not to be displayed in EXPLAIN output. The purpose of this is to allow
753  * running regression tests with force_parallel_mode=regress to get the
754  * same results as running the same tests with force_parallel_mode=off.
755  * Such marking is currently only supported on a Gather at the top of the
756  * plan. We skip that node, and we must also hide per-worker detail data
757  * further down in the plan tree.
758  */
759  ps = queryDesc->planstate;
760  if (IsA(ps, GatherState) && ((Gather *) ps->plan)->invisible)
761  {
762  ps = outerPlanState(ps);
763  es->hide_workers = true;
764  }
765  ExplainNode(ps, NIL, NULL, NULL, es);
766 
767  /*
768  * If requested, include information about GUC parameters with values that
769  * don't match the built-in defaults.
770  */
772 }
#define NIL
Definition: pg_list.h:65
static void ExplainPrintSettings(ExplainState *es)
Definition: explain.c:665
#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:3419
static void ExplainNode(PlanState *planstate, List *ancestors, const char *relationship, const char *plan_name, ExplainState *es)
Definition: explain.c:1122
PlanState * planstate
Definition: execdesc.h:49
#define outerPlanState(node)
Definition: execnodes.h:1022
List * deparse_context_for_plan_tree(PlannedStmt *pstmt, List *rtable_names)
Definition: ruleutils.c:3327
Plan * plan
Definition: execnodes.h:928
#define Assert(condition)
Definition: c.h:792
List * rtable
Definition: plannodes.h:66
static bool ExplainPreScanNode(PlanState *planstate, Bitmapset **rels_used)
Definition: explain.c:1048
PlannedStmt * plannedstmt
Definition: execdesc.h:37
List * rtable
Definition: explain.h:55

◆ ExplainPrintSettings()

static void ExplainPrintSettings ( ExplainState es)
static

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

666 {
667  int num;
668  struct config_generic **gucs;
669 
670  /* bail out if information about settings not requested */
671  if (!es->settings)
672  return;
673 
674  /* request an array of relevant settings */
675  gucs = get_explain_guc_options(&num);
676 
677  if (es->format != EXPLAIN_FORMAT_TEXT)
678  {
679  ExplainOpenGroup("Settings", "Settings", true, es);
680 
681  for (int i = 0; i < num; i++)
682  {
683  char *setting;
684  struct config_generic *conf = gucs[i];
685 
686  setting = GetConfigOptionByName(conf->name, NULL, true);
687 
688  ExplainPropertyText(conf->name, setting, es);
689  }
690 
691  ExplainCloseGroup("Settings", "Settings", true, es);
692  }
693  else
694  {
696 
697  /* In TEXT mode, print nothing if there are no options */
698  if (num <= 0)
699  return;
700 
701  initStringInfo(&str);
702 
703  for (int i = 0; i < num; i++)
704  {
705  char *setting;
706  struct config_generic *conf = gucs[i];
707 
708  if (i > 0)
709  appendStringInfoString(&str, ", ");
710 
711  setting = GetConfigOptionByName(conf->name, NULL, true);
712 
713  if (setting)
714  appendStringInfo(&str, "%s = '%s'", conf->name, setting);
715  else
716  appendStringInfo(&str, "%s = NULL", conf->name);
717  }
718 
719  ExplainPropertyText("Settings", str.data, es);
720  }
721 }
const char * name
Definition: guc_tables.h:149
struct config_generic ** get_explain_guc_options(int *num)
Definition: guc.c:9272
void ExplainPropertyText(const char *qlabel, const char *value, ExplainState *es)
Definition: explain.c:4241
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:9365
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:4369
void ExplainOpenGroup(const char *objtype, const char *labelname, bool labeled, ExplainState *es)
Definition: explain.c:4306
bool settings
Definition: explain.h:48

◆ ExplainPrintTriggers()

void ExplainPrintTriggers ( ExplainState es,
QueryDesc queryDesc 
)

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

785 {
786  ResultRelInfo *rInfo;
787  bool show_relname;
788  List *resultrels;
789  List *routerels;
790  List *targrels;
791  ListCell *l;
792 
793  resultrels = queryDesc->estate->es_opened_result_relations;
794  routerels = queryDesc->estate->es_tuple_routing_result_relations;
795  targrels = queryDesc->estate->es_trig_target_relations;
796 
797  ExplainOpenGroup("Triggers", "Triggers", false, es);
798 
799  show_relname = (list_length(resultrels) > 1 ||
800  routerels != NIL || targrels != NIL);
801  foreach(l, resultrels)
802  {
803  rInfo = (ResultRelInfo *) lfirst(l);
804  report_triggers(rInfo, show_relname, es);
805  }
806 
807  foreach(l, routerels)
808  {
809  rInfo = (ResultRelInfo *) lfirst(l);
810  report_triggers(rInfo, show_relname, es);
811  }
812 
813  foreach(l, targrels)
814  {
815  rInfo = (ResultRelInfo *) lfirst(l);
816  report_triggers(rInfo, show_relname, es);
817  }
818 
819  ExplainCloseGroup("Triggers", "Triggers", false, es);
820 }
#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:958
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:4369
void ExplainOpenGroup(const char *objtype, const char *labelname, bool labeled, ExplainState *es)
Definition: explain.c:4306
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 4188 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().

4190 {
4191  switch (es->format)
4192  {
4193  case EXPLAIN_FORMAT_TEXT:
4194  ExplainIndentText(es);
4195  if (unit)
4196  appendStringInfo(es->str, "%s: %s %s\n", qlabel, value, unit);
4197  else
4198  appendStringInfo(es->str, "%s: %s\n", qlabel, value);
4199  break;
4200 
4201  case EXPLAIN_FORMAT_XML:
4202  {
4203  char *str;
4204 
4205  appendStringInfoSpaces(es->str, es->indent * 2);
4206  ExplainXMLTag(qlabel, X_OPENING | X_NOWHITESPACE, es);
4207  str = escape_xml(value);
4208  appendStringInfoString(es->str, str);
4209  pfree(str);
4210  ExplainXMLTag(qlabel, X_CLOSING | X_NOWHITESPACE, es);
4211  appendStringInfoChar(es->str, '\n');
4212  }
4213  break;
4214 
4215  case EXPLAIN_FORMAT_JSON:
4217  appendStringInfoSpaces(es->str, es->indent * 2);
4218  escape_json(es->str, qlabel);
4219  appendStringInfoString(es->str, ": ");
4220  if (numeric)
4222  else
4223  escape_json(es->str, value);
4224  break;
4225 
4226  case EXPLAIN_FORMAT_YAML:
4228  appendStringInfo(es->str, "%s: ", qlabel);
4229  if (numeric)
4231  else
4232  escape_yaml(es->str, value);
4233  break;
4234  }
4235 }
void escape_json(StringInfo buf, const char *str)
Definition: json.c:1279
static struct @144 value
static void ExplainXMLTag(const char *tagname, int flags, ExplainState *es)
Definition: explain.c:4651
#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:4713
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:4738
void appendStringInfoSpaces(StringInfo str, int count)
Definition: stringinfo.c:206
static void ExplainJSONLineEnding(ExplainState *es)
Definition: explain.c:4693
#define X_OPENING
Definition: explain.c:51
static void ExplainIndentText(ExplainState *es)
Definition: explain.c:4678
#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 4291 of file explain.c.

References ExplainProperty().

Referenced by ExplainNode(), and ExplainPrintJIT().

4292 {
4293  ExplainProperty(qlabel, NULL, value ? "true" : "false", true, es);
4294 }
static struct @144 value
static void ExplainProperty(const char *qlabel, const char *unit, const char *value, bool numeric, ExplainState *es)
Definition: explain.c:4188

◆ ExplainPropertyFloat()

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

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

4279 {
4280  char *buf;
4281 
4282  buf = psprintf("%.*f", ndigits, value);
4283  ExplainProperty(qlabel, unit, buf, true, es);
4284  pfree(buf);
4285 }
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
static struct @144 value
void pfree(void *pointer)
Definition: mcxt.c:1057
static char * buf
Definition: pg_test_fsync.c:68
static void ExplainProperty(const char *qlabel, const char *unit, const char *value, bool numeric, ExplainState *es)
Definition: explain.c:4188

◆ ExplainPropertyInteger()

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

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

4252 {
4253  char buf[32];
4254 
4255  snprintf(buf, sizeof(buf), INT64_FORMAT, value);
4256  ExplainProperty(qlabel, unit, buf, true, es);
4257 }
static struct @144 value
static char * buf
Definition: pg_test_fsync.c:68
#define INT64_FORMAT
Definition: c.h:471
#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:4188

◆ ExplainPropertyList()

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

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

4066 {
4067  ListCell *lc;
4068  bool first = true;
4069 
4070  switch (es->format)
4071  {
4072  case EXPLAIN_FORMAT_TEXT:
4073  ExplainIndentText(es);
4074  appendStringInfo(es->str, "%s: ", qlabel);
4075  foreach(lc, data)
4076  {
4077  if (!first)
4078  appendStringInfoString(es->str, ", ");
4079  appendStringInfoString(es->str, (const char *) lfirst(lc));
4080  first = false;
4081  }
4082  appendStringInfoChar(es->str, '\n');
4083  break;
4084 
4085  case EXPLAIN_FORMAT_XML:
4086  ExplainXMLTag(qlabel, X_OPENING, es);
4087  foreach(lc, data)
4088  {
4089  char *str;
4090 
4091  appendStringInfoSpaces(es->str, es->indent * 2 + 2);
4092  appendStringInfoString(es->str, "<Item>");
4093  str = escape_xml((const char *) lfirst(lc));
4094  appendStringInfoString(es->str, str);
4095  pfree(str);
4096  appendStringInfoString(es->str, "</Item>\n");
4097  }
4098  ExplainXMLTag(qlabel, X_CLOSING, es);
4099  break;
4100 
4101  case EXPLAIN_FORMAT_JSON:
4103  appendStringInfoSpaces(es->str, es->indent * 2);
4104  escape_json(es->str, qlabel);
4105  appendStringInfoString(es->str, ": [");
4106  foreach(lc, data)
4107  {
4108  if (!first)
4109  appendStringInfoString(es->str, ", ");
4110  escape_json(es->str, (const char *) lfirst(lc));
4111  first = false;
4112  }
4113  appendStringInfoChar(es->str, ']');
4114  break;
4115 
4116  case EXPLAIN_FORMAT_YAML:
4118  appendStringInfo(es->str, "%s: ", qlabel);
4119  foreach(lc, data)
4120  {
4121  appendStringInfoChar(es->str, '\n');
4122  appendStringInfoSpaces(es->str, es->indent * 2 + 2);
4123  appendStringInfoString(es->str, "- ");
4124  escape_yaml(es->str, (const char *) lfirst(lc));
4125  }
4126  break;
4127  }
4128 }
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:4651
#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:4713
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:4738
void appendStringInfoSpaces(StringInfo str, int count)
Definition: stringinfo.c:206
static void ExplainJSONLineEnding(ExplainState *es)
Definition: explain.c:4693
#define X_OPENING
Definition: explain.c:51
static void ExplainIndentText(ExplainState *es)
Definition: explain.c:4678
#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 4135 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().

4136 {
4137  ListCell *lc;
4138  bool first = true;
4139 
4140  switch (es->format)
4141  {
4142  case EXPLAIN_FORMAT_TEXT:
4143  case EXPLAIN_FORMAT_XML:
4144  ExplainPropertyList(qlabel, data, es);
4145  return;
4146 
4147  case EXPLAIN_FORMAT_JSON:
4149  appendStringInfoSpaces(es->str, es->indent * 2);
4150  appendStringInfoChar(es->str, '[');
4151  foreach(lc, data)
4152  {
4153  if (!first)
4154  appendStringInfoString(es->str, ", ");
4155  escape_json(es->str, (const char *) lfirst(lc));
4156  first = false;
4157  }
4158  appendStringInfoChar(es->str, ']');
4159  break;
4160 
4161  case EXPLAIN_FORMAT_YAML:
4163  appendStringInfoString(es->str, "- [");
4164  foreach(lc, data)
4165  {
4166  if (!first)
4167  appendStringInfoString(es->str, ", ");
4168  escape_yaml(es->str, (const char *) lfirst(lc));
4169  first = false;
4170  }
4171  appendStringInfoChar(es->str, ']');
4172  break;
4173  }
4174 }
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:4713
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:4738
void appendStringInfoSpaces(StringInfo str, int count)
Definition: stringinfo.c:206
static void ExplainJSONLineEnding(ExplainState *es)
Definition: explain.c:4693
void ExplainPropertyList(const char *qlabel, List *data, ExplainState *es)
Definition: explain.c:4065
#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 4241 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().

4242 {
4243  ExplainProperty(qlabel, NULL, value, false, es);
4244 }
static struct @144 value
static void ExplainProperty(const char *qlabel, const char *unit, const char *value, bool numeric, ExplainState *es)
Definition: explain.c:4188

◆ ExplainPropertyUInteger()

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

Definition at line 4263 of file explain.c.

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

Referenced by show_wal_usage().

4265 {
4266  char buf[32];
4267 
4268  snprintf(buf, sizeof(buf), UINT64_FORMAT, value);
4269  ExplainProperty(qlabel, unit, buf, true, es);
4270 }
static struct @144 value
static char * buf
Definition: pg_test_fsync.c:68
#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:4188
#define UINT64_FORMAT
Definition: c.h:472

◆ 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:4622
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:704
List * options
Definition: parsenodes.h:3268
bool costs
Definition: explain.h:43
bool analyze
Definition: explain.h:42
Node * query
Definition: parsenodes.h:3267
void ExplainEndOutput(ExplainState *es)
Definition: explain.c:4593
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:4562
#define ERROR
Definition: elog.h:45
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:155
#define Assert(condition)
Definition: c.h:792
#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:111
ExplainFormat format
Definition: explain.h:49
#define do_text_output_oneline(tstate, str_to_emit)
Definition: executor.h:481
#define CURSOR_OPT_PARALLEL_OK
Definition: parsenodes.h:2732
int errmsg(const char *fmt,...)
Definition: elog.c:915
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 947 of file explain.c.

References ExplainPropertyText(), and QueryDesc::sourceText.

Referenced by explain_ExecutorEnd().

948 {
949  if (queryDesc->sourceText)
950  ExplainPropertyText("Query Text", queryDesc->sourceText, es);
951 }
void ExplainPropertyText(const char *qlabel, const char *value, ExplainState *es)
Definition: explain.c:4241
const char * sourceText
Definition: execdesc.h:38

◆ ExplainRestoreGroup()

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

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

4486 {
4487  switch (es->format)
4488  {
4489  case EXPLAIN_FORMAT_TEXT:
4490  /* nothing to do */
4491  break;
4492 
4493  case EXPLAIN_FORMAT_XML:
4494  es->indent += depth;
4495  break;
4496 
4497  case EXPLAIN_FORMAT_JSON:
4498  es->grouping_stack = lcons_int(*state_save, es->grouping_stack);
4499  es->indent += depth;
4500  break;
4501 
4502  case EXPLAIN_FORMAT_YAML:
4503  es->grouping_stack = lcons_int(*state_save, es->grouping_stack);
4504  es->indent += depth;
4505  break;
4506  }
4507 }
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:3268
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 4455 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().

4456 {
4457  switch (es->format)
4458  {
4459  case EXPLAIN_FORMAT_TEXT:
4460  /* nothing to do */
4461  break;
4462 
4463  case EXPLAIN_FORMAT_XML:
4464  es->indent -= depth;
4465  break;
4466 
4467  case EXPLAIN_FORMAT_JSON:
4468  es->indent -= depth;
4469  *state_save = linitial_int(es->grouping_stack);
4471  break;
4472 
4473  case EXPLAIN_FORMAT_YAML:
4474  es->indent -= depth;
4475  *state_save = linitial_int(es->grouping_stack);
4477  break;
4478  }
4479 }
#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 3518 of file explain.c.

References ExplainTargetRel().

Referenced by ExplainNode().

3519 {
3520  ExplainTargetRel((Plan *) plan, plan->scanrelid, es);
3521 }
static void ExplainTargetRel(Plan *plan, Index rti, ExplainState *es)
Definition: explain.c:3540

◆ ExplainSeparatePlans()

void ExplainSeparatePlans ( ExplainState es)

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

4623 {
4624  switch (es->format)
4625  {
4626  case EXPLAIN_FORMAT_TEXT:
4627  /* add a blank line */
4628  appendStringInfoChar(es->str, '\n');
4629  break;
4630 
4631  case EXPLAIN_FORMAT_XML:
4632  case EXPLAIN_FORMAT_JSON:
4633  case EXPLAIN_FORMAT_YAML:
4634  /* nothing to do */
4635  break;
4636  }
4637 }
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 3855 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().

3857 {
3858  ListCell *lst;
3859 
3860  foreach(lst, plans)
3861  {
3862  SubPlanState *sps = (SubPlanState *) lfirst(lst);
3863  SubPlan *sp = sps->subplan;
3864 
3865  /*
3866  * There can be multiple SubPlan nodes referencing the same physical
3867  * subplan (same plan_id, which is its index in PlannedStmt.subplans).
3868  * We should print a subplan only once, so track which ones we already
3869  * printed. This state must be global across the plan tree, since the
3870  * duplicate nodes could be in different plan nodes, eg both a bitmap
3871  * indexscan's indexqual and its parent heapscan's recheck qual. (We
3872  * do not worry too much about which plan node we show the subplan as
3873  * attached to in such cases.)
3874  */
3875  if (bms_is_member(sp->plan_id, es->printed_subplans))
3876  continue;
3878  sp->plan_id);
3879 
3880  /*
3881  * Treat the SubPlan node as an ancestor of the plan node(s) within
3882  * it, so that ruleutils.c can find the referents of subplan
3883  * parameters.
3884  */
3885  ancestors = lcons(sp, ancestors);
3886 
3887  ExplainNode(sps->planstate, ancestors,
3888  relationship, sp->plan_name, es);
3889 
3890  ancestors = list_delete_first(ancestors);
3891  }
3892 }
int plan_id
Definition: primnodes.h:730
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:1122
struct PlanState * planstate
Definition: execnodes.h:846
SubPlan * subplan
Definition: execnodes.h:845
List * lcons(void *datum, List *list)
Definition: list.c:453
char * plan_name
Definition: primnodes.h:732
#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 3540 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().

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

4652 {
4653  const char *s;
4654  const char *valid = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.";
4655 
4656  if ((flags & X_NOWHITESPACE) == 0)
4657  appendStringInfoSpaces(es->str, 2 * es->indent);
4658  appendStringInfoCharMacro(es->str, '<');
4659  if ((flags & X_CLOSING) != 0)
4660  appendStringInfoCharMacro(es->str, '/');
4661  for (s = tagname; *s; s++)
4662  appendStringInfoChar(es->str, strchr(valid, *s) ? *s : '-');
4663  if ((flags & X_CLOSE_IMMEDIATE) != 0)
4664  appendStringInfoString(es->str, " /");
4665  appendStringInfoCharMacro(es->str, '>');
4666  if ((flags & X_NOWHITESPACE) == 0)
4667  appendStringInfoCharMacro(es->str, '\n');
4668 }
#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 4713 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().

4714 {
4716  if (linitial_int(es->grouping_stack) == 0)
4717  {
4718  linitial_int(es->grouping_stack) = 1;
4719  }
4720  else
4721  {
4722  appendStringInfoChar(es->str, '\n');
4723  appendStringInfoSpaces(es->str, es->indent * 2);
4724  }
4725 }
#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:792
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 958 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().

959 {
960  int nt;
961 
962  if (!rInfo->ri_TrigDesc || !rInfo->ri_TrigInstrument)
963  return;
964  for (nt = 0; nt < rInfo->ri_TrigDesc->numtriggers; nt++)
965  {
966  Trigger *trig = rInfo->ri_TrigDesc->triggers + nt;
967  Instrumentation *instr = rInfo->ri_TrigInstrument + nt;
968  char *relname;
969  char *conname = NULL;
970 
971  /* Must clean up instrumentation state */
972  InstrEndLoop(instr);
973 
974  /*
975  * We ignore triggers that were never invoked; they likely aren't
976  * relevant to the current query type.
977  */
978  if (instr->ntuples == 0)
979  continue;
980 
981  ExplainOpenGroup("Trigger", NULL, true, es);
982 
983  relname = RelationGetRelationName(rInfo->ri_RelationDesc);
984  if (OidIsValid(trig->tgconstraint))
985  conname = get_constraint_name(trig->tgconstraint);
986 
987  /*
988  * In text format, we avoid printing both the trigger name and the
989  * constraint name unless VERBOSE is specified. In non-text formats
990  * we just print everything.
991  */
992  if (es->format == EXPLAIN_FORMAT_TEXT)
993  {
994  if (es->verbose || conname == NULL)
995  appendStringInfo(es->str, "Trigger %s", trig->tgname);
996  else
997  appendStringInfoString(es->str, "Trigger");
998  if (conname)
999  appendStringInfo(es->str, " for constraint %s", conname);
1000  if (show_relname)
1001  appendStringInfo(es->str, " on %s", relname);
1002  if (es->timing)
1003  appendStringInfo(es->str, ": time=%.3f calls=%.0f\n",
1004  1000.0 * instr->total, instr->ntuples);
1005  else
1006  appendStringInfo(es->str, ": calls=%.0f\n", instr->ntuples);
1007  }
1008  else
1009  {
1010  ExplainPropertyText("Trigger Name", trig->tgname, es);
1011  if (conname)
1012  ExplainPropertyText("Constraint Name", conname, es);
1013  ExplainPropertyText("Relation", relname, es);
1014  if (es->timing)
1015  ExplainPropertyFloat("Time", "ms", 1000.0 * instr->total, 3,
1016  es);
1017  ExplainPropertyFloat("Calls", NULL, instr->ntuples, 0, es);
1018  }
1019 
1020  if (conname)
1021  pfree(conname);
1022 
1023  ExplainCloseGroup("Trigger", NULL, true, es);
1024  }
1025 }
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:4277
Instrumentation * ri_TrigInstrument
Definition: execnodes.h:433
NameData relname
Definition: pg_class.h:38
#define OidIsValid(objectId)
Definition: c.h:698
void InstrEndLoop(Instrumentation *instr)
Definition: instrument.c:121
void ExplainPropertyText(const char *qlabel, const char *value, ExplainState *es)
Definition: explain.c:4241
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:491
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:4369
void ExplainOpenGroup(const char *objtype, const char *labelname, bool labeled, ExplainState *es)
Definition: explain.c:4306
StringInfo str
Definition: explain.h:39

◆ show_agg_keys()

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

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

2333 {
2334  Agg *plan = (Agg *) astate->ss.ps.plan;
2335 
2336  if (plan->numCols > 0 || plan->groupingSets)
2337  {
2338  /* The key columns refer to the tlist of the child plan */
2339  ancestors = lcons(plan, ancestors);
2340 
2341  if (plan->groupingSets)
2342  show_grouping_sets(outerPlanState(astate), plan, ancestors, es);
2343  else
2344  show_sort_group_keys(outerPlanState(astate), "Group Key",
2345  plan->numCols, 0, plan->grpColIdx,
2346  NULL, NULL, NULL,
2347  ancestors, es);
2348 
2349  ancestors = list_delete_first(ancestors);
2350  }
2351 }
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:2483
ScanState ss
Definition: execnodes.h:2132
PlanState ps
Definition: execnodes.h:1317
#define outerPlanState(node)
Definition: execnodes.h:1022