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

Go to the source code of this file.

Macros

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

Functions

static void ExplainOneQuery (Query *query, int cursorOptions, IntoClause *into, ExplainState *es, const char *queryString, ParamListInfo params, QueryEnvironment *queryEnv)
 
static void ExplainPrintJIT (ExplainState *es, int jit_flags, JitInstrumentation *ji)
 
static void report_triggers (ResultRelInfo *rInfo, bool show_relname, ExplainState *es)
 
static double elapsed_time (instr_time *starttime)
 
static bool ExplainPreScanNode (PlanState *planstate, Bitmapset **rels_used)
 
static void ExplainNode (PlanState *planstate, List *ancestors, const char *relationship, const char *plan_name, ExplainState *es)
 
static void show_plan_tlist (PlanState *planstate, List *ancestors, ExplainState *es)
 
static void show_expression (Node *node, const char *qlabel, PlanState *planstate, List *ancestors, bool useprefix, ExplainState *es)
 
static void show_qual (List *qual, const char *qlabel, PlanState *planstate, List *ancestors, bool useprefix, ExplainState *es)
 
static void show_scan_qual (List *qual, const char *qlabel, PlanState *planstate, List *ancestors, ExplainState *es)
 
static void show_upper_qual (List *qual, const char *qlabel, PlanState *planstate, List *ancestors, ExplainState *es)
 
static void show_sort_keys (SortState *sortstate, List *ancestors, ExplainState *es)
 
static void show_incremental_sort_keys (IncrementalSortState *incrsortstate, List *ancestors, ExplainState *es)
 
static void show_merge_append_keys (MergeAppendState *mstate, List *ancestors, ExplainState *es)
 
static void show_agg_keys (AggState *astate, List *ancestors, ExplainState *es)
 
static void show_grouping_sets (PlanState *planstate, Agg *agg, List *ancestors, ExplainState *es)
 
static void show_grouping_set_keys (PlanState *planstate, Agg *aggnode, Sort *sortnode, List *context, bool useprefix, List *ancestors, ExplainState *es)
 
static void show_group_keys (GroupState *gstate, List *ancestors, ExplainState *es)
 
static void show_sort_group_keys (PlanState *planstate, const char *qlabel, int nkeys, int nPresortedKeys, AttrNumber *keycols, Oid *sortOperators, Oid *collations, bool *nullsFirst, List *ancestors, ExplainState *es)
 
static void show_sortorder_options (StringInfo buf, Node *sortexpr, Oid sortOperator, Oid collation, bool nullsFirst)
 
static void show_tablesample (TableSampleClause *tsc, PlanState *planstate, List *ancestors, ExplainState *es)
 
static void show_sort_info (SortState *sortstate, ExplainState *es)
 
static void show_incremental_sort_info (IncrementalSortState *incrsortstate, ExplainState *es)
 
static void show_hash_info (HashState *hashstate, ExplainState *es)
 
static void show_memoize_info (MemoizeState *mstate, List *ancestors, ExplainState *es)
 
static void show_hashagg_info (AggState *aggstate, 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)
 
void ExplainQueryParameters (ExplainState *es, ParamListInfo params, int maxlen)
 
static void show_incremental_sort_group_info (IncrementalSortGroupInfo *groupInfo, const char *groupLabel, bool indent, ExplainState *es)
 
void ExplainPropertyList (const char *qlabel, List *data, ExplainState *es)
 
void ExplainPropertyListNested (const char *qlabel, List *data, ExplainState *es)
 
void ExplainPropertyText (const char *qlabel, const char *value, ExplainState *es)
 
void ExplainPropertyInteger (const char *qlabel, const char *unit, int64 value, ExplainState *es)
 
void ExplainPropertyUInteger (const char *qlabel, const char *unit, uint64 value, ExplainState *es)
 
void ExplainPropertyFloat (const char *qlabel, const char *unit, double value, int ndigits, ExplainState *es)
 
void ExplainPropertyBool (const char *qlabel, bool value, ExplainState *es)
 
void ExplainOpenGroup (const char *objtype, const char *labelname, bool labeled, ExplainState *es)
 
void ExplainCloseGroup (const char *objtype, const char *labelname, bool labeled, ExplainState *es)
 
void ExplainBeginOutput (ExplainState *es)
 
void ExplainEndOutput (ExplainState *es)
 
void ExplainSeparatePlans (ExplainState *es)
 

Variables

ExplainOneQuery_hook_type ExplainOneQuery_hook = NULL
 
explain_get_index_name_hook_type explain_get_index_name_hook = NULL
 

Macro Definition Documentation

◆ X_CLOSE_IMMEDIATE

#define X_CLOSE_IMMEDIATE   2

Definition at line 54 of file explain.c.

◆ X_CLOSING

#define X_CLOSING   1

Definition at line 53 of file explain.c.

◆ X_NOWHITESPACE

#define X_NOWHITESPACE   4

Definition at line 55 of file explain.c.

◆ X_OPENING

#define X_OPENING   0

Definition at line 52 of file explain.c.

Function Documentation

◆ elapsed_time()

static double elapsed_time ( instr_time starttime)
static

Definition at line 1092 of file explain.c.

1093 {
1094  instr_time endtime;
1095 
1096  INSTR_TIME_SET_CURRENT(endtime);
1097  INSTR_TIME_SUBTRACT(endtime, *starttime);
1098  return INSTR_TIME_GET_DOUBLE(endtime);
1099 }
#define INSTR_TIME_SET_CURRENT(t)
Definition: instr_time.h:122
#define INSTR_TIME_GET_DOUBLE(t)
Definition: instr_time.h:188
#define INSTR_TIME_SUBTRACT(x, y)
Definition: instr_time.h:181

References INSTR_TIME_GET_DOUBLE, INSTR_TIME_SET_CURRENT, and INSTR_TIME_SUBTRACT.

Referenced by ExplainOnePlan(), and IsCheckpointOnSchedule().

◆ escape_yaml()

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

Definition at line 5059 of file explain.c.

5060 {
5061  escape_json(buf, str);
5062 }
void escape_json(StringInfo buf, const char *str)
Definition: json.c:1525
static char * buf
Definition: pg_test_fsync.c:67

References buf, escape_json(), and generate_unaccent_rules::str.

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

◆ explain_get_index_name()

static const char * explain_get_index_name ( Oid  indexId)
static

Definition at line 3529 of file explain.c.

3530 {
3531  const char *result;
3532 
3534  result = (*explain_get_index_name_hook) (indexId);
3535  else
3536  result = NULL;
3537  if (result == NULL)
3538  {
3539  /* default behavior: look it up in the catalogs */
3540  result = get_rel_name(indexId);
3541  if (result == NULL)
3542  elog(ERROR, "cache lookup failed for index %u", indexId);
3543  }
3544  return result;
3545 }
#define ERROR
Definition: elog.h:39
explain_get_index_name_hook_type explain_get_index_name_hook
Definition: explain.c:48
char * get_rel_name(Oid relid)
Definition: lsyscache.c:1932

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

Referenced by ExplainIndexScanDetails(), and ExplainNode().

◆ ExplainBeginOutput()

void ExplainBeginOutput ( ExplainState es)

Definition at line 4883 of file explain.c.

4884 {
4885  switch (es->format)
4886  {
4887  case EXPLAIN_FORMAT_TEXT:
4888  /* nothing to do */
4889  break;
4890 
4891  case EXPLAIN_FORMAT_XML:
4893  "<explain xmlns=\"http://www.postgresql.org/2009/explain\">\n");
4894  es->indent++;
4895  break;
4896 
4897  case EXPLAIN_FORMAT_JSON:
4898  /* top-level structure is an array of plans */
4899  appendStringInfoChar(es->str, '[');
4900  es->grouping_stack = lcons_int(0, es->grouping_stack);
4901  es->indent++;
4902  break;
4903 
4904  case EXPLAIN_FORMAT_YAML:
4905  es->grouping_stack = lcons_int(0, es->grouping_stack);
4906  break;
4907  }
4908 }
@ EXPLAIN_FORMAT_XML
Definition: explain.h:23
@ EXPLAIN_FORMAT_YAML
Definition: explain.h:25
@ EXPLAIN_FORMAT_TEXT
Definition: explain.h:22
@ EXPLAIN_FORMAT_JSON
Definition: explain.h:24
List * lcons_int(int datum, List *list)
Definition: list.c:512
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:188
List * grouping_stack
Definition: explain.h:53
StringInfo str
Definition: explain.h:39
ExplainFormat format
Definition: explain.h:50
int indent
Definition: explain.h:52

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().

◆ ExplainCloseGroup()

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

Definition at line 4690 of file explain.c.

4692 {
4693  switch (es->format)
4694  {
4695  case EXPLAIN_FORMAT_TEXT:
4696  /* nothing to do */
4697  break;
4698 
4699  case EXPLAIN_FORMAT_XML:
4700  es->indent--;
4701  ExplainXMLTag(objtype, X_CLOSING, es);
4702  break;
4703 
4704  case EXPLAIN_FORMAT_JSON:
4705  es->indent--;
4706  appendStringInfoChar(es->str, '\n');
4707  appendStringInfoSpaces(es->str, 2 * es->indent);
4708  appendStringInfoChar(es->str, labeled ? '}' : ']');
4710  break;
4711 
4712  case EXPLAIN_FORMAT_YAML:
4713  es->indent--;
4715  break;
4716  }
4717 }
static void ExplainXMLTag(const char *tagname, int flags, ExplainState *es)
Definition: explain.c:4972
#define X_CLOSING
Definition: explain.c:53
List * list_delete_first(List *list)
Definition: list.c:942
void appendStringInfoSpaces(StringInfo str, int count)
Definition: stringinfo.c:206

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().

◆ ExplainCloseWorker()

static void ExplainCloseWorker ( int  n,
ExplainState es 
)
static

Definition at line 4320 of file explain.c.

4321 {
4322  ExplainWorkersState *wstate = es->workers_state;
4323 
4324  Assert(wstate);
4325  Assert(n >= 0 && n < wstate->num_workers);
4326  Assert(wstate->worker_inited[n]);
4327 
4328  /*
4329  * Save formatting state in case we do another ExplainOpenWorker(), then
4330  * pop the formatting stack.
4331  */
4332  ExplainSaveGroup(es, 2, &wstate->worker_state_save[n]);
4333 
4334  /*
4335  * In TEXT format, if we didn't actually produce any output line(s) then
4336  * truncate off the partial line emitted by ExplainOpenWorker. (This is
4337  * to avoid bogus output if, say, show_buffer_usage chooses not to print
4338  * anything for the worker.) Also fix up the indent level.
4339  */
4340  if (es->format == EXPLAIN_FORMAT_TEXT)
4341  {
4342  while (es->str->len > 0 && es->str->data[es->str->len - 1] != '\n')
4343  es->str->data[--(es->str->len)] = '\0';
4344 
4345  es->indent--;
4346  }
4347 
4348  /* Restore prior output buffer pointer */
4349  es->str = wstate->prev_str;
4350 }
static void ExplainSaveGroup(ExplainState *es, int depth, int *state_save)
Definition: explain.c:4776
Assert(fmt[strlen(fmt) - 1] !='\n')
ExplainWorkersState * workers_state
Definition: explain.h:62
bool * worker_inited
Definition: explain.h:31
int * worker_state_save
Definition: explain.h:33
StringInfo prev_str
Definition: explain.h:34

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

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

◆ ExplainCreateWorkersState()

static ExplainWorkersState * ExplainCreateWorkersState ( int  num_workers)
static

Definition at line 4241 of file explain.c.

4242 {
4243  ExplainWorkersState *wstate;
4244 
4245  wstate = (ExplainWorkersState *) palloc(sizeof(ExplainWorkersState));
4246  wstate->num_workers = num_workers;
4247  wstate->worker_inited = (bool *) palloc0(num_workers * sizeof(bool));
4248  wstate->worker_str = (StringInfoData *)
4249  palloc0(num_workers * sizeof(StringInfoData));
4250  wstate->worker_state_save = (int *) palloc(num_workers * sizeof(int));
4251  return wstate;
4252 }
void * palloc0(Size size)
Definition: mcxt.c:1257
void * palloc(Size size)
Definition: mcxt.c:1226
StringInfoData * worker_str
Definition: explain.h:32

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

Referenced by ExplainNode().

◆ ExplainCustomChildren()

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

Definition at line 4219 of file explain.c.

4220 {
4221  ListCell *cell;
4222  const char *label =
4223  (list_length(css->custom_ps) != 1 ? "children" : "child");
4224 
4225  foreach(cell, css->custom_ps)
4226  ExplainNode((PlanState *) lfirst(cell), ancestors, label, NULL, es);
4227 }
static void ExplainNode(PlanState *planstate, List *ancestors, const char *relationship, const char *plan_name, ExplainState *es)
Definition: explain.c:1186
static char * label
#define lfirst(lc)
Definition: pg_list.h:172
static int list_length(const List *l)
Definition: pg_list.h:152
List * custom_ps
Definition: execnodes.h:1983

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

Referenced by ExplainNode().

◆ ExplainDummyGroup()

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

Definition at line 4837 of file explain.c.

4838 {
4839  switch (es->format)
4840  {
4841  case EXPLAIN_FORMAT_TEXT:
4842  /* nothing to do */
4843  break;
4844 
4845  case EXPLAIN_FORMAT_XML:
4846  ExplainXMLTag(objtype, X_CLOSE_IMMEDIATE, es);
4847  break;
4848 
4849  case EXPLAIN_FORMAT_JSON:
4851  appendStringInfoSpaces(es->str, 2 * es->indent);
4852  if (labelname)
4853  {
4854  escape_json(es->str, labelname);
4855  appendStringInfoString(es->str, ": ");
4856  }
4857  escape_json(es->str, objtype);
4858  break;
4859 
4860  case EXPLAIN_FORMAT_YAML:
4862  if (labelname)
4863  {
4864  escape_yaml(es->str, labelname);
4865  appendStringInfoString(es->str, ": ");
4866  }
4867  else
4868  {
4869  appendStringInfoString(es->str, "- ");
4870  }
4871  escape_yaml(es->str, objtype);
4872  break;
4873  }
4874 }
#define X_CLOSE_IMMEDIATE
Definition: explain.c:54
static void ExplainYAMLLineStarting(ExplainState *es)
Definition: explain.c:5034
static void ExplainJSONLineEnding(ExplainState *es)
Definition: explain.c:5014
static void escape_yaml(StringInfo buf, const char *str)
Definition: explain.c:5059

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().

◆ ExplainEndOutput()

void ExplainEndOutput ( ExplainState es)

Definition at line 4914 of file explain.c.

4915 {
4916  switch (es->format)
4917  {
4918  case EXPLAIN_FORMAT_TEXT:
4919  /* nothing to do */
4920  break;
4921 
4922  case EXPLAIN_FORMAT_XML:
4923  es->indent--;
4924  appendStringInfoString(es->str, "</explain>");
4925  break;
4926 
4927  case EXPLAIN_FORMAT_JSON:
4928  es->indent--;
4929  appendStringInfoString(es->str, "\n]");
4931  break;
4932 
4933  case EXPLAIN_FORMAT_YAML:
4935  break;
4936  }
4937 }

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().

◆ ExplainFlushWorkersState()

static void ExplainFlushWorkersState ( ExplainState es)
static

Definition at line 4356 of file explain.c.

4357 {
4358  ExplainWorkersState *wstate = es->workers_state;
4359 
4360  ExplainOpenGroup("Workers", "Workers", false, es);
4361  for (int i = 0; i < wstate->num_workers; i++)
4362  {
4363  if (wstate->worker_inited[i])
4364  {
4365  /* This must match previous ExplainOpenSetAsideGroup call */
4366  ExplainOpenGroup("Worker", NULL, true, es);
4367  appendStringInfoString(es->str, wstate->worker_str[i].data);
4368  ExplainCloseGroup("Worker", NULL, true, es);
4369 
4370  pfree(wstate->worker_str[i].data);
4371  }
4372  }
4373  ExplainCloseGroup("Workers", "Workers", false, es);
4374 
4375  pfree(wstate->worker_inited);
4376  pfree(wstate->worker_str);
4377  pfree(wstate->worker_state_save);
4378  pfree(wstate);
4379 }
void ExplainOpenGroup(const char *objtype, const char *labelname, bool labeled, ExplainState *es)
Definition: explain.c:4627
void ExplainCloseGroup(const char *objtype, const char *labelname, bool labeled, ExplainState *es)
Definition: explain.c:4690
int i
Definition: isn.c:73
void pfree(void *pointer)
Definition: mcxt.c:1456

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().

◆ ExplainIndentText()

◆ ExplainIndexScanDetails()

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

Definition at line 3751 of file explain.c.

3753 {
3754  const char *indexname = explain_get_index_name(indexid);
3755 
3756  if (es->format == EXPLAIN_FORMAT_TEXT)
3757  {
3758  if (ScanDirectionIsBackward(indexorderdir))
3759  appendStringInfoString(es->str, " Backward");
3760  appendStringInfo(es->str, " using %s", quote_identifier(indexname));
3761  }
3762  else
3763  {
3764  const char *scandir;
3765 
3766  switch (indexorderdir)
3767  {
3768  case BackwardScanDirection:
3769  scandir = "Backward";
3770  break;
3771  case ForwardScanDirection:
3772  scandir = "Forward";
3773  break;
3774  default:
3775  scandir = "???";
3776  break;
3777  }
3778  ExplainPropertyText("Scan Direction", scandir, es);
3779  ExplainPropertyText("Index Name", indexname, es);
3780  }
3781 }
void ExplainPropertyText(const char *qlabel, const char *value, ExplainState *es)
Definition: explain.c:4562
static const char * explain_get_index_name(Oid indexId)
Definition: explain.c:3529
const char * quote_identifier(const char *ident)
Definition: ruleutils.c:11965
#define ScanDirectionIsBackward(direction)
Definition: sdir.h:50
@ BackwardScanDirection
Definition: sdir.h:26
@ ForwardScanDirection
Definition: sdir.h:28
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91

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

Referenced by ExplainNode().

◆ ExplainJSONLineEnding()

static void ExplainJSONLineEnding ( ExplainState es)
static

◆ ExplainMemberNodes()

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

Definition at line 4144 of file explain.c.

4146 {
4147  int j;
4148 
4149  for (j = 0; j < nplans; j++)
4150  ExplainNode(planstates[j], ancestors,
4151  "Member", NULL, es);
4152 }
int j
Definition: isn.c:74

References ExplainNode(), and j.

Referenced by ExplainNode().

◆ ExplainMissingMembers()

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

Definition at line 4162 of file explain.c.

4163 {
4164  if (nplans < nchildren || es->format != EXPLAIN_FORMAT_TEXT)
4165  ExplainPropertyInteger("Subplans Removed", NULL,
4166  nchildren - nplans, es);
4167 }
void ExplainPropertyInteger(const char *qlabel, const char *unit, int64 value, ExplainState *es)
Definition: explain.c:4571
static char format

References EXPLAIN_FORMAT_TEXT, ExplainPropertyInteger(), and format.

Referenced by ExplainNode().

◆ ExplainModifyTarget()

static void ExplainModifyTarget ( ModifyTable plan,
ExplainState es 
)
static

Definition at line 3800 of file explain.c.

3801 {
3802  ExplainTargetRel((Plan *) plan, plan->nominalRelation, es);
3803 }
static void ExplainTargetRel(Plan *plan, Index rti, ExplainState *es)
Definition: explain.c:3809
#define plan(x)
Definition: pg_regress.c:154

References ExplainTargetRel(), and plan.

Referenced by ExplainNode().

◆ ExplainNode()

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

Definition at line 1186 of file explain.c.

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

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_MERGE, 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, if(), 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_RIGHT_ANTI, JOIN_SEMI, lappend(), lcons(), lfirst, list_delete_first(), list_length(), list_make1, make_andclause(), make_orclause(), CustomScanState::methods, NIL, Instrumentation::nloops, nodeTag, Instrumentation::ntuples, Instrumentation::ntuples2, WorkerInstrumentation::num_workers, SharedJitInstrumentation::num_workers, Gather::num_workers, GatherMerge::num_workers, outerPlanState, PlanState::plan, plan, 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_memoize_info(), 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, PlanState::state, ExplainState::str, PlanState::subPlan, ExplainState::timing, Instrumentation::total, ExplainState::verbose, ExplainState::wal, Instrumentation::walusage, PlanState::worker_instrument, PlanState::worker_jit_instrument, and ExplainState::workers_state.

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

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

531 {
533  QueryDesc *queryDesc;
534  instr_time starttime;
535  double totaltime = 0;
536  int eflags;
537  int instrument_option = 0;
538 
539  Assert(plannedstmt->commandType != CMD_UTILITY);
540 
541  if (es->analyze && es->timing)
542  instrument_option |= INSTRUMENT_TIMER;
543  else if (es->analyze)
544  instrument_option |= INSTRUMENT_ROWS;
545 
546  if (es->buffers)
547  instrument_option |= INSTRUMENT_BUFFERS;
548  if (es->wal)
549  instrument_option |= INSTRUMENT_WAL;
550 
551  /*
552  * We always collect timing for the entire statement, even when node-level
553  * timing is off, so we don't look at es->timing here. (We could skip
554  * this if !es->summary, but it's hardly worth the complication.)
555  */
556  INSTR_TIME_SET_CURRENT(starttime);
557 
558  /*
559  * Use a snapshot with an updated command ID to ensure this query sees
560  * results of any previously executed queries.
561  */
564 
565  /*
566  * Normally we discard the query's output, but if explaining CREATE TABLE
567  * AS, we'd better use the appropriate tuple receiver.
568  */
569  if (into)
571  else
573 
574  /* Create a QueryDesc for the query */
575  queryDesc = CreateQueryDesc(plannedstmt, queryString,
577  dest, params, queryEnv, instrument_option);
578 
579  /* Select execution options */
580  if (es->analyze)
581  eflags = 0; /* default run-to-completion flags */
582  else
583  eflags = EXEC_FLAG_EXPLAIN_ONLY;
584  if (es->generic)
585  eflags |= EXEC_FLAG_EXPLAIN_GENERIC;
586  if (into)
587  eflags |= GetIntoRelEFlags(into);
588 
589  /* call ExecutorStart to prepare the plan for execution */
590  ExecutorStart(queryDesc, eflags);
591 
592  /* Execute the plan for statistics if asked for */
593  if (es->analyze)
594  {
595  ScanDirection dir;
596 
597  /* EXPLAIN ANALYZE CREATE TABLE AS WITH NO DATA is weird */
598  if (into && into->skipData)
600  else
601  dir = ForwardScanDirection;
602 
603  /* run the plan */
604  ExecutorRun(queryDesc, dir, 0, true);
605 
606  /* run cleanup too */
607  ExecutorFinish(queryDesc);
608 
609  /* We can't run ExecutorEnd 'till we're done printing the stats... */
610  totaltime += elapsed_time(&starttime);
611  }
612 
613  ExplainOpenGroup("Query", NULL, true, es);
614 
615  /* Create textual dump of plan tree */
616  ExplainPrintPlan(es, queryDesc);
617 
618  /* Show buffer usage in planning */
619  if (bufusage)
620  {
621  ExplainOpenGroup("Planning", "Planning", true, es);
622  show_buffer_usage(es, bufusage, true);
623  ExplainCloseGroup("Planning", "Planning", true, es);
624  }
625 
626  if (es->summary && planduration)
627  {
628  double plantime = INSTR_TIME_GET_DOUBLE(*planduration);
629 
630  ExplainPropertyFloat("Planning Time", "ms", 1000.0 * plantime, 3, es);
631  }
632 
633  /* Print info about runtime of triggers */
634  if (es->analyze)
635  ExplainPrintTriggers(es, queryDesc);
636 
637  /*
638  * Print info about JITing. Tied to es->costs because we don't want to
639  * display this in regression tests, as it'd cause output differences
640  * depending on build options. Might want to separate that out from COSTS
641  * at a later stage.
642  */
643  if (es->costs)
644  ExplainPrintJITSummary(es, queryDesc);
645 
646  /*
647  * Close down the query and free resources. Include time for this in the
648  * total execution time (although it should be pretty minimal).
649  */
650  INSTR_TIME_SET_CURRENT(starttime);
651 
652  ExecutorEnd(queryDesc);
653 
654  FreeQueryDesc(queryDesc);
655 
657 
658  /* We need a CCI just in case query expanded to multiple plans */
659  if (es->analyze)
661 
662  totaltime += elapsed_time(&starttime);
663 
664  /*
665  * We only report execution time if we actually ran the query (that is,
666  * the user specified ANALYZE), and if summary reporting is enabled (the
667  * user can set SUMMARY OFF to not have the timing information included in
668  * the output). By default, ANALYZE sets SUMMARY to true.
669  */
670  if (es->summary && es->analyze)
671  ExplainPropertyFloat("Execution Time", "ms", 1000.0 * totaltime, 3,
672  es);
673 
674  ExplainCloseGroup("Query", NULL, true, es);
675 }
DestReceiver * CreateIntoRelDestReceiver(IntoClause *intoClause)
Definition: createas.c:440
int GetIntoRelEFlags(IntoClause *intoClause)
Definition: createas.c:375
DestReceiver * None_Receiver
Definition: dest.c:96
void ExecutorEnd(QueryDesc *queryDesc)
Definition: execMain.c:469
void ExecutorFinish(QueryDesc *queryDesc)
Definition: execMain.c:409
void ExecutorStart(QueryDesc *queryDesc, int eflags)
Definition: execMain.c:132
void ExecutorRun(QueryDesc *queryDesc, ScanDirection direction, uint64 count, bool execute_once)
Definition: execMain.c:302
#define EXEC_FLAG_EXPLAIN_GENERIC
Definition: executor.h:66
#define EXEC_FLAG_EXPLAIN_ONLY
Definition: executor.h:65
void ExplainPrintJITSummary(ExplainState *es, QueryDesc *queryDesc)
Definition: explain.c:860
static double elapsed_time(instr_time *starttime)
Definition: explain.c:1092
void ExplainPrintPlan(ExplainState *es, QueryDesc *queryDesc)
Definition: explain.c:752
void ExplainPrintTriggers(ExplainState *es, QueryDesc *queryDesc)
Definition: explain.c:817
@ INSTRUMENT_TIMER
Definition: instrument.h:59
@ INSTRUMENT_BUFFERS
Definition: instrument.h:60
@ INSTRUMENT_WAL
Definition: instrument.h:62
@ INSTRUMENT_ROWS
Definition: instrument.h:61
@ CMD_UTILITY
Definition: nodes.h:281
void FreeQueryDesc(QueryDesc *qdesc)
Definition: pquery.c:105
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:25
@ NoMovementScanDirection
Definition: sdir.h:27
void UpdateActiveSnapshotCommandId(void)
Definition: snapmgr.c:693
void PopActiveSnapshot(void)
Definition: snapmgr.c:724
void PushCopiedSnapshot(Snapshot snapshot)
Definition: snapmgr.c:681
Snapshot GetActiveSnapshot(void)
Definition: snapmgr.c:751
#define InvalidSnapshot
Definition: snapshot.h:123
bool generic
Definition: explain.h:49
bool summary
Definition: explain.h:47
bool skipData
Definition: primnodes.h:148
CmdType commandType
Definition: plannodes.h:53
void CommandCounterIncrement(void)
Definition: xact.c:1078

References ExplainState::analyze, Assert(), ExplainState::buffers, CMD_UTILITY, CommandCounterIncrement(), PlannedStmt::commandType, ExplainState::costs, CreateIntoRelDestReceiver(), CreateQueryDesc(), generate_unaccent_rules::dest, elapsed_time(), EXEC_FLAG_EXPLAIN_GENERIC, EXEC_FLAG_EXPLAIN_ONLY, ExecutorEnd(), ExecutorFinish(), ExecutorRun(), ExecutorStart(), ExplainCloseGroup(), ExplainOpenGroup(), ExplainPrintJITSummary(), ExplainPrintPlan(), ExplainPrintTriggers(), ExplainPropertyFloat(), ForwardScanDirection, FreeQueryDesc(), ExplainState::generic, 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().

◆ ExplainOneQuery()

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

Definition at line 376 of file explain.c.

380 {
381  /* planner will not cope with utility statements */
382  if (query->commandType == CMD_UTILITY)
383  {
384  ExplainOneUtility(query->utilityStmt, into, es, queryString, params,
385  queryEnv);
386  return;
387  }
388 
389  /* if an advisor plugin is present, let it manage things */
391  (*ExplainOneQuery_hook) (query, cursorOptions, into, es,
392  queryString, params, queryEnv);
393  else
394  {
395  PlannedStmt *plan;
396  instr_time planstart,
397  planduration;
398  BufferUsage bufusage_start,
399  bufusage;
400 
401  if (es->buffers)
402  bufusage_start = pgBufferUsage;
403  INSTR_TIME_SET_CURRENT(planstart);
404 
405  /* plan the query */
406  plan = pg_plan_query(query, queryString, cursorOptions, params);
407 
408  INSTR_TIME_SET_CURRENT(planduration);
409  INSTR_TIME_SUBTRACT(planduration, planstart);
410 
411  /* calc differences of buffer counters. */
412  if (es->buffers)
413  {
414  memset(&bufusage, 0, sizeof(BufferUsage));
415  BufferUsageAccumDiff(&bufusage, &pgBufferUsage, &bufusage_start);
416  }
417 
418  /* run it (if needed) and produce output */
419  ExplainOnePlan(plan, into, es, queryString, params, queryEnv,
420  &planduration, (es->buffers ? &bufusage : NULL));
421  }
422 }
ExplainOneQuery_hook_type ExplainOneQuery_hook
Definition: explain.c:45
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:527
void ExplainOneUtility(Node *utilityStmt, IntoClause *into, ExplainState *es, const char *queryString, ParamListInfo params, QueryEnvironment *queryEnv)
Definition: explain.c:437
BufferUsage pgBufferUsage
Definition: instrument.c:20
void BufferUsageAccumDiff(BufferUsage *dst, const BufferUsage *add, const BufferUsage *sub)
Definition: instrument.c:246
PlannedStmt * pg_plan_query(Query *querytree, const char *query_string, int cursorOptions, ParamListInfo boundParams)
Definition: postgres.c:884
CmdType commandType
Definition: parsenodes.h:127
Node * utilityStmt
Definition: parsenodes.h:142

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

Referenced by ExplainOneUtility(), and ExplainQuery().

◆ ExplainOneUtility()

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

Definition at line 437 of file explain.c.

440 {
441  if (utilityStmt == NULL)
442  return;
443 
444  if (IsA(utilityStmt, CreateTableAsStmt))
445  {
446  /*
447  * We have to rewrite the contained SELECT and then pass it back to
448  * ExplainOneQuery. Copy to be safe in the EXPLAIN EXECUTE case.
449  */
450  CreateTableAsStmt *ctas = (CreateTableAsStmt *) utilityStmt;
451  List *rewritten;
452 
453  /*
454  * Check if the relation exists or not. This is done at this stage to
455  * avoid query planning or execution.
456  */
457  if (CreateTableAsRelExists(ctas))
458  {
459  if (ctas->objtype == OBJECT_TABLE)
460  ExplainDummyGroup("CREATE TABLE AS", NULL, es);
461  else if (ctas->objtype == OBJECT_MATVIEW)
462  ExplainDummyGroup("CREATE MATERIALIZED VIEW", NULL, es);
463  else
464  elog(ERROR, "unexpected object type: %d",
465  (int) ctas->objtype);
466  return;
467  }
468 
469  rewritten = QueryRewrite(castNode(Query, copyObject(ctas->query)));
470  Assert(list_length(rewritten) == 1);
471  ExplainOneQuery(linitial_node(Query, rewritten),
472  CURSOR_OPT_PARALLEL_OK, ctas->into, es,
473  queryString, params, queryEnv);
474  }
475  else if (IsA(utilityStmt, DeclareCursorStmt))
476  {
477  /*
478  * Likewise for DECLARE CURSOR.
479  *
480  * Notice that if you say EXPLAIN ANALYZE DECLARE CURSOR then we'll
481  * actually run the query. This is different from pre-8.3 behavior
482  * but seems more useful than not running the query. No cursor will
483  * be created, however.
484  */
485  DeclareCursorStmt *dcs = (DeclareCursorStmt *) utilityStmt;
486  List *rewritten;
487 
488  rewritten = QueryRewrite(castNode(Query, copyObject(dcs->query)));
489  Assert(list_length(rewritten) == 1);
490  ExplainOneQuery(linitial_node(Query, rewritten),
491  dcs->options, NULL, es,
492  queryString, params, queryEnv);
493  }
494  else if (IsA(utilityStmt, ExecuteStmt))
495  ExplainExecuteQuery((ExecuteStmt *) utilityStmt, into, es,
496  queryString, params, queryEnv);
497  else if (IsA(utilityStmt, NotifyStmt))
498  {
499  if (es->format == EXPLAIN_FORMAT_TEXT)
500  appendStringInfoString(es->str, "NOTIFY\n");
501  else
502  ExplainDummyGroup("Notify", NULL, es);
503  }
504  else
505  {
506  if (es->format == EXPLAIN_FORMAT_TEXT)
508  "Utility statements have no plan structure\n");
509  else
510  ExplainDummyGroup("Utility Statement", NULL, es);
511  }
512 }
void ExplainExecuteQuery(ExecuteStmt *execstmt, IntoClause *into, ExplainState *es, const char *queryString, ParamListInfo params, QueryEnvironment *queryEnv)
Definition: prepare.c:571
bool CreateTableAsRelExists(CreateTableAsStmt *ctas)
Definition: createas.c:393
static void ExplainDummyGroup(const char *objtype, const char *labelname, ExplainState *es)
Definition: explain.c:4837
static void ExplainOneQuery(Query *query, int cursorOptions, IntoClause *into, ExplainState *es, const char *queryString, ParamListInfo params, QueryEnvironment *queryEnv)
Definition: explain.c:376
#define copyObject(obj)
Definition: nodes.h:244
@ OBJECT_MATVIEW
Definition: parsenodes.h:2143
@ OBJECT_TABLE
Definition: parsenodes.h:2161
#define CURSOR_OPT_PARALLEL_OK
Definition: parsenodes.h:3165
#define linitial_node(type, l)
Definition: pg_list.h:181
List * QueryRewrite(Query *parsetree)
IntoClause * into
Definition: parsenodes.h:3766
ObjectType objtype
Definition: parsenodes.h:3767

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().

◆ ExplainOpenGroup()

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

Definition at line 4627 of file explain.c.

4629 {
4630  switch (es->format)
4631  {
4632  case EXPLAIN_FORMAT_TEXT:
4633  /* nothing to do */
4634  break;
4635 
4636  case EXPLAIN_FORMAT_XML:
4637  ExplainXMLTag(objtype, X_OPENING, es);
4638  es->indent++;
4639  break;
4640 
4641  case EXPLAIN_FORMAT_JSON:
4643  appendStringInfoSpaces(es->str, 2 * es->indent);
4644  if (labelname)
4645  {
4646  escape_json(es->str, labelname);
4647  appendStringInfoString(es->str, ": ");
4648  }
4649  appendStringInfoChar(es->str, labeled ? '{' : '[');
4650 
4651  /*
4652  * In JSON format, the grouping_stack is an integer list. 0 means
4653  * we've emitted nothing at this grouping level, 1 means we've
4654  * emitted something (and so the next item needs a comma). See
4655  * ExplainJSONLineEnding().
4656  */
4657  es->grouping_stack = lcons_int(0, es->grouping_stack);
4658  es->indent++;
4659  break;
4660 
4661  case EXPLAIN_FORMAT_YAML:
4662 
4663  /*
4664  * In YAML format, the grouping stack is an integer list. 0 means
4665  * we've emitted nothing at this grouping level AND this grouping
4666  * level is unlabeled and must be marked with "- ". See
4667  * ExplainYAMLLineStarting().
4668  */
4670  if (labelname)
4671  {
4672  appendStringInfo(es->str, "%s: ", labelname);
4673  es->grouping_stack = lcons_int(1, es->grouping_stack);
4674  }
4675  else
4676  {
4677  appendStringInfoString(es->str, "- ");
4678  es->grouping_stack = lcons_int(0, es->grouping_stack);
4679  }
4680  es->indent++;
4681  break;
4682  }
4683 }
#define X_OPENING
Definition: explain.c:52

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().

◆ ExplainOpenSetAsideGroup()

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

Definition at line 4737 of file explain.c.

4739 {
4740  switch (es->format)
4741  {
4742  case EXPLAIN_FORMAT_TEXT:
4743  /* nothing to do */
4744  break;
4745 
4746  case EXPLAIN_FORMAT_XML:
4747  es->indent += depth;
4748  break;
4749 
4750  case EXPLAIN_FORMAT_JSON:
4751  es->grouping_stack = lcons_int(0, es->grouping_stack);
4752  es->indent += depth;
4753  break;
4754 
4755  case EXPLAIN_FORMAT_YAML:
4756  if (labelname)
4757  es->grouping_stack = lcons_int(1, es->grouping_stack);
4758  else
4759  es->grouping_stack = lcons_int(0, es->grouping_stack);
4760  es->indent += depth;
4761  break;
4762  }
4763 }

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().

◆ ExplainOpenWorker()

static void ExplainOpenWorker ( int  n,
ExplainState es 
)
static

Definition at line 4258 of file explain.c.

4259 {
4260  ExplainWorkersState *wstate = es->workers_state;
4261 
4262  Assert(wstate);
4263  Assert(n >= 0 && n < wstate->num_workers);
4264 
4265  /* Save prior output buffer pointer */
4266  wstate->prev_str = es->str;
4267 
4268  if (!wstate->worker_inited[n])
4269  {
4270  /* First time through, so create the buffer for this worker */
4271  initStringInfo(&wstate->worker_str[n]);
4272  es->str = &wstate->worker_str[n];
4273 
4274  /*
4275  * Push suitable initial formatting state for this worker's field
4276  * group. We allow one extra logical nesting level, since this group
4277  * will eventually be wrapped in an outer "Workers" group.
4278  */
4279  ExplainOpenSetAsideGroup("Worker", NULL, true, 2, es);
4280 
4281  /*
4282  * In non-TEXT formats we always emit a "Worker Number" field, even if
4283  * there's no other data for this worker.
4284  */
4285  if (es->format != EXPLAIN_FORMAT_TEXT)
4286  ExplainPropertyInteger("Worker Number", NULL, n, es);
4287 
4288  wstate->worker_inited[n] = true;
4289  }
4290  else
4291  {
4292  /* Resuming output for a worker we've already emitted some data for */
4293  es->str = &wstate->worker_str[n];
4294 
4295  /* Restore formatting state saved by last ExplainCloseWorker() */
4296  ExplainRestoreGroup(es, 2, &wstate->worker_state_save[n]);
4297  }
4298 
4299  /*
4300  * In TEXT format, prefix the first output line for this worker with
4301  * "Worker N:". Then, any additional lines should be indented one more
4302  * stop than the "Worker N" line is.
4303  */
4304  if (es->format == EXPLAIN_FORMAT_TEXT)
4305  {
4306  if (es->str->len == 0)
4307  {
4308  ExplainIndentText(es);
4309  appendStringInfo(es->str, "Worker %d: ", n);
4310  }
4311 
4312  es->indent++;
4313  }
4314 }
static void ExplainRestoreGroup(ExplainState *es, int depth, int *state_save)
Definition: explain.c:4806
static void ExplainOpenSetAsideGroup(const char *objtype, const char *labelname, bool labeled, int depth, ExplainState *es)
Definition: explain.c:4737
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59

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

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

◆ ExplainPreScanNode()

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

Definition at line 1111 of file explain.c.

1112 {
1113  Plan *plan = planstate->plan;
1114 
1115  switch (nodeTag(plan))
1116  {
1117  case T_SeqScan:
1118  case T_SampleScan:
1119  case T_IndexScan:
1120  case T_IndexOnlyScan:
1121  case T_BitmapHeapScan:
1122  case T_TidScan:
1123  case T_TidRangeScan:
1124  case T_SubqueryScan:
1125  case T_FunctionScan:
1126  case T_TableFuncScan:
1127  case T_ValuesScan:
1128  case T_CteScan:
1129  case T_NamedTuplestoreScan:
1130  case T_WorkTableScan:
1131  *rels_used = bms_add_member(*rels_used,
1132  ((Scan *) plan)->scanrelid);
1133  break;
1134  case T_ForeignScan:
1135  *rels_used = bms_add_members(*rels_used,
1136  ((ForeignScan *) plan)->fs_base_relids);
1137  break;
1138  case T_CustomScan:
1139  *rels_used = bms_add_members(*rels_used,
1140  ((CustomScan *) plan)->custom_relids);
1141  break;
1142  case T_ModifyTable:
1143  *rels_used = bms_add_member(*rels_used,
1144  ((ModifyTable *) plan)->nominalRelation);
1145  if (((ModifyTable *) plan)->exclRelRTI)
1146  *rels_used = bms_add_member(*rels_used,
1147  ((ModifyTable *) plan)->exclRelRTI);
1148  break;
1149  case T_Append:
1150  *rels_used = bms_add_members(*rels_used,
1151  ((Append *) plan)->apprelids);
1152  break;
1153  case T_MergeAppend:
1154  *rels_used = bms_add_members(*rels_used,
1155  ((MergeAppend *) plan)->apprelids);
1156  break;
1157  default:
1158  break;
1159  }
1160 
1161  return planstate_tree_walker(planstate, ExplainPreScanNode, rels_used);
1162 }
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:753
Bitmapset * bms_add_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:835
static bool ExplainPreScanNode(PlanState *planstate, Bitmapset **rels_used)
Definition: explain.c:1111
#define planstate_tree_walker(ps, w, c)
Definition: nodeFuncs.h:177

References bms_add_member(), bms_add_members(), nodeTag, PlanState::plan, plan, and planstate_tree_walker.

Referenced by ExplainPrintPlan().

◆ ExplainPrintJIT()

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

Definition at line 886 of file explain.c.

887 {
888  instr_time total_time;
889 
890  /* don't print information if no JITing happened */
891  if (!ji || ji->created_functions == 0)
892  return;
893 
894  /* calculate total time */
895  INSTR_TIME_SET_ZERO(total_time);
896  /* don't add deform_counter, it's included in generation_counter */
897  INSTR_TIME_ADD(total_time, ji->generation_counter);
898  INSTR_TIME_ADD(total_time, ji->inlining_counter);
899  INSTR_TIME_ADD(total_time, ji->optimization_counter);
900  INSTR_TIME_ADD(total_time, ji->emission_counter);
901 
902  ExplainOpenGroup("JIT", "JIT", true, es);
903 
904  /* for higher density, open code the text output format */
905  if (es->format == EXPLAIN_FORMAT_TEXT)
906  {
907  ExplainIndentText(es);
908  appendStringInfoString(es->str, "JIT:\n");
909  es->indent++;
910 
911  ExplainPropertyInteger("Functions", NULL, ji->created_functions, es);
912 
913  ExplainIndentText(es);
914  appendStringInfo(es->str, "Options: %s %s, %s %s, %s %s, %s %s\n",
915  "Inlining", jit_flags & PGJIT_INLINE ? "true" : "false",
916  "Optimization", jit_flags & PGJIT_OPT3 ? "true" : "false",
917  "Expressions", jit_flags & PGJIT_EXPR ? "true" : "false",
918  "Deforming", jit_flags & PGJIT_DEFORM ? "true" : "false");
919 
920  if (es->analyze && es->timing)
921  {
922  ExplainIndentText(es);
923  appendStringInfo(es->str,
924  "Timing: %s %.3f ms (%s %.3f ms), %s %.3f ms, %s %.3f ms, %s %.3f ms, %s %.3f ms\n",
925  "Generation", 1000.0 * INSTR_TIME_GET_DOUBLE(ji->generation_counter),
926  "Deform", 1000.0 * INSTR_TIME_GET_DOUBLE(ji->deform_counter),
927  "Inlining", 1000.0 * INSTR_TIME_GET_DOUBLE(ji->inlining_counter),
928  "Optimization", 1000.0 * INSTR_TIME_GET_DOUBLE(ji->optimization_counter),
929  "Emission", 1000.0 * INSTR_TIME_GET_DOUBLE(ji->emission_counter),
930  "Total", 1000.0 * INSTR_TIME_GET_DOUBLE(total_time));
931  }
932 
933  es->indent--;
934  }
935  else
936  {
937  ExplainPropertyInteger("Functions", NULL, ji->created_functions, es);
938 
939  ExplainOpenGroup("Options", "Options", true, es);
940  ExplainPropertyBool("Inlining", jit_flags & PGJIT_INLINE, es);
941  ExplainPropertyBool("Optimization", jit_flags & PGJIT_OPT3, es);
942  ExplainPropertyBool("Expressions", jit_flags & PGJIT_EXPR, es);
943  ExplainPropertyBool("Deforming", jit_flags & PGJIT_DEFORM, es);
944  ExplainCloseGroup("Options", "Options", true, es);
945 
946  if (es->analyze && es->timing)
947  {
948  ExplainOpenGroup("Timing", "Timing", true, es);
949 
950  ExplainOpenGroup("Generation", "Generation", true, es);
951  ExplainPropertyFloat("Deform", "ms",
953  3, es);
954  ExplainPropertyFloat("Total", "ms",
956  3, es);
957  ExplainCloseGroup("Generation", "Generation", true, es);
958 
959  ExplainPropertyFloat("Inlining", "ms",
961  3, es);
962  ExplainPropertyFloat("Optimization", "ms",
964  3, es);
965  ExplainPropertyFloat("Emission", "ms",
967  3, es);
968  ExplainPropertyFloat("Total", "ms",
969  1000.0 * INSTR_TIME_GET_DOUBLE(total_time),
970  3, es);
971 
972  ExplainCloseGroup("Timing", "Timing", true, es);
973  }
974  }
975 
976  ExplainCloseGroup("JIT", "JIT", true, es);
977 }
#define INSTR_TIME_ADD(x, y)
Definition: instr_time.h:178
#define INSTR_TIME_SET_ZERO(t)
Definition: instr_time.h:172
#define PGJIT_OPT3
Definition: jit.h:21
#define PGJIT_EXPR
Definition: jit.h:23
#define PGJIT_DEFORM
Definition: jit.h:24
#define PGJIT_INLINE
Definition: jit.h:22
instr_time generation_counter
Definition: jit.h:33
size_t created_functions
Definition: jit.h:30
instr_time optimization_counter
Definition: jit.h:42
instr_time deform_counter
Definition: jit.h:36
instr_time emission_counter
Definition: jit.h:45
instr_time inlining_counter
Definition: jit.h:39

References ExplainState::analyze, appendStringInfo(), appendStringInfoString(), JitInstrumentation::created_functions, JitInstrumentation::deform_counter, 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().

◆ ExplainPrintJITSummary()

void ExplainPrintJITSummary ( ExplainState es,
QueryDesc queryDesc 
)

Definition at line 860 of file explain.c.

861 {
862  JitInstrumentation ji = {0};
863 
864  if (!(queryDesc->estate->es_jit_flags & PGJIT_PERFORM))
865  return;
866 
867  /*
868  * Work with a copy instead of modifying the leader state, since this
869  * function may be called twice
870  */
871  if (queryDesc->estate->es_jit)
872  InstrJitAgg(&ji, &queryDesc->estate->es_jit->instr);
873 
874  /* If this process has done JIT in parallel workers, merge stats */
875  if (queryDesc->estate->es_jit_worker_instr)
876  InstrJitAgg(&ji, queryDesc->estate->es_jit_worker_instr);
877 
878  ExplainPrintJIT(es, queryDesc->estate->es_jit_flags, &ji);
879 }
void InstrJitAgg(JitInstrumentation *dst, JitInstrumentation *add)
Definition: jit.c:184
#define PGJIT_PERFORM
Definition: jit.h:20
struct JitContext * es_jit
Definition: execnodes.h:708
struct JitInstrumentation * es_jit_worker_instr
Definition: execnodes.h:709
JitInstrumentation instr
Definition: jit.h:64
EState * estate
Definition: execdesc.h:48

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().

◆ ExplainPrintPlan()

void ExplainPrintPlan ( ExplainState es,
QueryDesc queryDesc 
)

Definition at line 752 of file explain.c.

753 {
754  Bitmapset *rels_used = NULL;
755  PlanState *ps;
756 
757  /* Set up ExplainState fields associated with this plan tree */
758  Assert(queryDesc->plannedstmt != NULL);
759  es->pstmt = queryDesc->plannedstmt;
760  es->rtable = queryDesc->plannedstmt->rtable;
761  ExplainPreScanNode(queryDesc->planstate, &rels_used);
764  es->rtable_names);
765  es->printed_subplans = NULL;
766 
767  /*
768  * Sometimes we mark a Gather node as "invisible", which means that it's
769  * not to be displayed in EXPLAIN output. The purpose of this is to allow
770  * running regression tests with debug_parallel_query=regress to get the
771  * same results as running the same tests with debug_parallel_query=off.
772  * Such marking is currently only supported on a Gather at the top of the
773  * plan. We skip that node, and we must also hide per-worker detail data
774  * further down in the plan tree.
775  */
776  ps = queryDesc->planstate;
777  if (IsA(ps, GatherState) && ((Gather *) ps->plan)->invisible)
778  {
779  ps = outerPlanState(ps);
780  es->hide_workers = true;
781  }
782  ExplainNode(ps, NIL, NULL, NULL, es);
783 
784  /*
785  * If requested, include information about GUC parameters with values that
786  * don't match the built-in defaults.
787  */
789 
790  /*
791  * COMPUTE_QUERY_ID_REGRESS means COMPUTE_QUERY_ID_AUTO, but we don't show
792  * the queryid in any of the EXPLAIN plans to keep stable the results
793  * generated by regression test suites.
794  */
795  if (es->verbose && queryDesc->plannedstmt->queryId != UINT64CONST(0) &&
797  {
798  /*
799  * Output the queryid as an int64 rather than a uint64 so we match
800  * what would be seen in the BIGINT pg_stat_statements.queryid column.
801  */
802  ExplainPropertyInteger("Query Identifier", NULL, (int64)
803  queryDesc->plannedstmt->queryId, es);
804  }
805 }
static void ExplainPrintSettings(ExplainState *es)
Definition: explain.c:682
struct parser_state ps
@ COMPUTE_QUERY_ID_REGRESS
Definition: queryjumble.h:59
int compute_query_id
List * select_rtable_names_for_explain(List *rtable, Bitmapset *rels_used)
Definition: ruleutils.c:3797
List * deparse_context_for_plan_tree(PlannedStmt *pstmt, List *rtable_names)
Definition: ruleutils.c:3705
Bitmapset * printed_subplans
Definition: explain.h:59
List * rtable_names
Definition: explain.h:57
List * rtable
Definition: explain.h:56
PlannedStmt * pstmt
Definition: explain.h:55
List * deparse_cxt
Definition: explain.h:58
List * rtable
Definition: plannodes.h:73
uint64 queryId
Definition: plannodes.h:55
PlannedStmt * plannedstmt
Definition: execdesc.h:37
PlanState * planstate
Definition: execdesc.h:49

References Assert(), compute_query_id, COMPUTE_QUERY_ID_REGRESS, deparse_context_for_plan_tree(), ExplainState::deparse_cxt, ExplainNode(), ExplainPreScanNode(), ExplainPrintSettings(), ExplainPropertyInteger(), ExplainState::hide_workers, IsA, NIL, outerPlanState, QueryDesc::plannedstmt, QueryDesc::planstate, ExplainState::printed_subplans, ps, ExplainState::pstmt, PlannedStmt::queryId, ExplainState::rtable, PlannedStmt::rtable, ExplainState::rtable_names, select_rtable_names_for_explain(), and ExplainState::verbose.

Referenced by explain_ExecutorEnd(), and ExplainOnePlan().

◆ ExplainPrintSettings()

static void ExplainPrintSettings ( ExplainState es)
static

Definition at line 682 of file explain.c.

683 {
684  int num;
685  struct config_generic **gucs;
686 
687  /* bail out if information about settings not requested */
688  if (!es->settings)
689  return;
690 
691  /* request an array of relevant settings */
692  gucs = get_explain_guc_options(&num);
693 
694  if (es->format != EXPLAIN_FORMAT_TEXT)
695  {
696  ExplainOpenGroup("Settings", "Settings", true, es);
697 
698  for (int i = 0; i < num; i++)
699  {
700  char *setting;
701  struct config_generic *conf = gucs[i];
702 
703  setting = GetConfigOptionByName(conf->name, NULL, true);
704 
705  ExplainPropertyText(conf->name, setting, es);
706  }
707 
708  ExplainCloseGroup("Settings", "Settings", true, es);
709  }
710  else
711  {
713 
714  /* In TEXT mode, print nothing if there are no options */
715  if (num <= 0)
716  return;
717 
719 
720  for (int i = 0; i < num; i++)
721  {
722  char *setting;
723  struct config_generic *conf = gucs[i];
724 
725  if (i > 0)
726  appendStringInfoString(&str, ", ");
727 
728  setting = GetConfigOptionByName(conf->name, NULL, true);
729 
730  if (setting)
731  appendStringInfo(&str, "%s = '%s'", conf->name, setting);
732  else
733  appendStringInfo(&str, "%s = NULL", conf->name);
734  }
735 
736  ExplainPropertyText("Settings", str.data, es);
737  }
738 }
struct config_generic ** get_explain_guc_options(int *num)
Definition: guc.c:5157
char * GetConfigOptionByName(const char *name, const char **varname, bool missing_ok)
Definition: guc.c:5251
bool settings
Definition: explain.h:48
const char * name
Definition: guc_tables.h:155

References appendStringInfo(), appendStringInfoString(), 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().

◆ ExplainPrintTriggers()

void ExplainPrintTriggers ( ExplainState es,
QueryDesc queryDesc 
)

Definition at line 817 of file explain.c.

818 {
819  ResultRelInfo *rInfo;
820  bool show_relname;
821  List *resultrels;
822  List *routerels;
823  List *targrels;
824  ListCell *l;
825 
826  resultrels = queryDesc->estate->es_opened_result_relations;
827  routerels = queryDesc->estate->es_tuple_routing_result_relations;
828  targrels = queryDesc->estate->es_trig_target_relations;
829 
830  ExplainOpenGroup("Triggers", "Triggers", false, es);
831 
832  show_relname = (list_length(resultrels) > 1 ||
833  routerels != NIL || targrels != NIL);
834  foreach(l, resultrels)
835  {
836  rInfo = (ResultRelInfo *) lfirst(l);
837  report_triggers(rInfo, show_relname, es);
838  }
839 
840  foreach(l, routerels)
841  {
842  rInfo = (ResultRelInfo *) lfirst(l);
843  report_triggers(rInfo, show_relname, es);
844  }
845 
846  foreach(l, targrels)
847  {
848  rInfo = (ResultRelInfo *) lfirst(l);
849  report_triggers(rInfo, show_relname, es);
850  }
851 
852  ExplainCloseGroup("Triggers", "Triggers", false, es);
853 }
static void report_triggers(ResultRelInfo *rInfo, bool show_relname, ExplainState *es)
Definition: explain.c:1021
List * es_tuple_routing_result_relations
Definition: execnodes.h:647
List * es_trig_target_relations
Definition: execnodes.h:650
List * es_opened_result_relations
Definition: execnodes.h:637

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().

◆ ExplainProperty()

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

Definition at line 4509 of file explain.c.

4511 {
4512  switch (es->format)
4513  {
4514  case EXPLAIN_FORMAT_TEXT:
4515  ExplainIndentText(es);
4516  if (unit)
4517  appendStringInfo(es->str, "%s: %s %s\n", qlabel, value, unit);
4518  else
4519  appendStringInfo(es->str, "%s: %s\n", qlabel, value);
4520  break;
4521 
4522  case EXPLAIN_FORMAT_XML:
4523  {
4524  char *str;
4525 
4526  appendStringInfoSpaces(es->str, es->indent * 2);
4527  ExplainXMLTag(qlabel, X_OPENING | X_NOWHITESPACE, es);
4528  str = escape_xml(value);
4530  pfree(str);
4531  ExplainXMLTag(qlabel, X_CLOSING | X_NOWHITESPACE, es);
4532  appendStringInfoChar(es->str, '\n');
4533  }
4534  break;
4535 
4536  case EXPLAIN_FORMAT_JSON:
4538  appendStringInfoSpaces(es->str, es->indent * 2);
4539  escape_json(es->str, qlabel);
4540  appendStringInfoString(es->str, ": ");
4541  if (numeric)
4543  else
4544  escape_json(es->str, value);
4545  break;
4546 
4547  case EXPLAIN_FORMAT_YAML:
4549  appendStringInfo(es->str, "%s: ", qlabel);
4550  if (numeric)
4552  else
4553  escape_yaml(es->str, value);
4554  break;
4555  }
4556 }
#define X_NOWHITESPACE
Definition: explain.c:55
static struct @148 value
char * escape_xml(const char *str)
Definition: xml.c:2603

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(), generate_unaccent_rules::str, ExplainState::str, value, X_CLOSING, X_NOWHITESPACE, and X_OPENING.

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

◆ ExplainPropertyBool()

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

Definition at line 4612 of file explain.c.

4613 {
4614  ExplainProperty(qlabel, NULL, value ? "true" : "false", true, es);
4615 }
static void ExplainProperty(const char *qlabel, const char *unit, const char *value, bool numeric, ExplainState *es)
Definition: explain.c:4509

References ExplainProperty(), and value.

Referenced by ExplainNode(), and ExplainPrintJIT().

◆ ExplainPropertyFloat()

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

Definition at line 4598 of file explain.c.

4600 {
4601  char *buf;
4602 
4603  buf = psprintf("%.*f", ndigits, value);
4604  ExplainProperty(qlabel, unit, buf, true, es);
4605  pfree(buf);
4606 }

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

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

◆ ExplainPropertyInteger()

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

◆ ExplainPropertyList()

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

Definition at line 4386 of file explain.c.

4387 {
4388  ListCell *lc;
4389  bool first = true;
4390 
4391  switch (es->format)
4392  {
4393  case EXPLAIN_FORMAT_TEXT:
4394  ExplainIndentText(es);
4395  appendStringInfo(es->str, "%s: ", qlabel);
4396  foreach(lc, data)
4397  {
4398  if (!first)
4399  appendStringInfoString(es->str, ", ");
4400  appendStringInfoString(es->str, (const char *) lfirst(lc));
4401  first = false;
4402  }
4403  appendStringInfoChar(es->str, '\n');
4404  break;
4405 
4406  case EXPLAIN_FORMAT_XML:
4407  ExplainXMLTag(qlabel, X_OPENING, es);
4408  foreach(lc, data)
4409  {
4410  char *str;
4411 
4412  appendStringInfoSpaces(es->str, es->indent * 2 + 2);
4413  appendStringInfoString(es->str, "<Item>");
4414  str = escape_xml((const char *) lfirst(lc));
4416  pfree(str);
4417  appendStringInfoString(es->str, "</Item>\n");
4418  }
4419  ExplainXMLTag(qlabel, X_CLOSING, es);
4420  break;
4421 
4422  case EXPLAIN_FORMAT_JSON:
4424  appendStringInfoSpaces(es->str, es->indent * 2);
4425  escape_json(es->str, qlabel);
4426  appendStringInfoString(es->str, ": [");
4427  foreach(lc, data)
4428  {
4429  if (!first)
4430  appendStringInfoString(es->str, ", ");
4431  escape_json(es->str, (const char *) lfirst(lc));
4432  first = false;
4433  }
4434  appendStringInfoChar(es->str, ']');
4435  break;
4436 
4437  case EXPLAIN_FORMAT_YAML:
4439  appendStringInfo(es->str, "%s: ", qlabel);
4440  foreach(lc, data)
4441  {
4442  appendStringInfoChar(es->str, '\n');
4443  appendStringInfoSpaces(es->str, es->indent * 2 + 2);
4444  appendStringInfoString(es->str, "- ");
4445  escape_yaml(es->str, (const char *) lfirst(lc));
4446  }
4447  break;
4448  }
4449 }
const void * data

References appendStringInfo(), appendStringInfoChar(), appendStringInfoSpaces(), appendStringInfoString(), data, 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(), generate_unaccent_rules::str, ExplainState::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().

◆ ExplainPropertyListNested()

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

Definition at line 4456 of file explain.c.

4457 {
4458  ListCell *lc;
4459  bool first = true;
4460 
4461  switch (es->format)
4462  {
4463  case EXPLAIN_FORMAT_TEXT:
4464  case EXPLAIN_FORMAT_XML:
4465  ExplainPropertyList(qlabel, data, es);
4466  return;
4467 
4468  case EXPLAIN_FORMAT_JSON:
4470  appendStringInfoSpaces(es->str, es->indent * 2);
4471  appendStringInfoChar(es->str, '[');
4472  foreach(lc, data)
4473  {
4474  if (!first)
4475  appendStringInfoString(es->str, ", ");
4476  escape_json(es->str, (const char *) lfirst(lc));
4477  first = false;
4478  }
4479  appendStringInfoChar(es->str, ']');
4480  break;
4481 
4482  case EXPLAIN_FORMAT_YAML:
4484  appendStringInfoString(es->str, "- [");
4485  foreach(lc, data)
4486  {
4487  if (!first)
4488  appendStringInfoString(es->str, ", ");
4489  escape_yaml(es->str, (const char *) lfirst(lc));
4490  first = false;
4491  }
4492  appendStringInfoChar(es->str, ']');
4493  break;
4494  }
4495 }
void ExplainPropertyList(const char *qlabel, List *data, ExplainState *es)
Definition: explain.c:4386

References appendStringInfoChar(), appendStringInfoSpaces(), appendStringInfoString(), data, 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().

◆ ExplainPropertyText()

◆ ExplainPropertyUInteger()

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

Definition at line 4584 of file explain.c.

4586 {
4587  char buf[32];
4588 
4589  snprintf(buf, sizeof(buf), UINT64_FORMAT, value);
4590  ExplainProperty(qlabel, unit, buf, true, es);
4591 }
#define UINT64_FORMAT
Definition: c.h:538

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

Referenced by show_wal_usage().

◆ ExplainQuery()

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

Definition at line 164 of file explain.c.

166 {
168  TupOutputState *tstate;
169  JumbleState *jstate = NULL;
170  Query *query;
171  List *rewritten;
172  ListCell *lc;
173  bool timing_set = false;
174  bool summary_set = false;
175 
176  /* Parse options list. */
177  foreach(lc, stmt->options)
178  {
179  DefElem *opt = (DefElem *) lfirst(lc);
180 
181  if (strcmp(opt->defname, "analyze") == 0)
182  es->analyze = defGetBoolean(opt);
183  else if (strcmp(opt->defname, "verbose") == 0)
184  es->verbose = defGetBoolean(opt);
185  else if (strcmp(opt->defname, "costs") == 0)
186  es->costs = defGetBoolean(opt);
187  else if (strcmp(opt->defname, "buffers") == 0)
188  es->buffers = defGetBoolean(opt);
189  else if (strcmp(opt->defname, "wal") == 0)
190  es->wal = defGetBoolean(opt);
191  else if (strcmp(opt->defname, "settings") == 0)
192  es->settings = defGetBoolean(opt);
193  else if (strcmp(opt->defname, "generic_plan") == 0)
194  es->generic = defGetBoolean(opt);
195  else if (strcmp(opt->defname, "timing") == 0)
196  {
197  timing_set = true;
198  es->timing = defGetBoolean(opt);
199  }
200  else if (strcmp(opt->defname, "summary") == 0)
201  {
202  summary_set = true;
203  es->summary = defGetBoolean(opt);
204  }
205  else if (strcmp(opt->defname, "format") == 0)
206  {
207  char *p = defGetString(opt);
208 
209  if (strcmp(p, "text") == 0)
211  else if (strcmp(p, "xml") == 0)
213  else if (strcmp(p, "json") == 0)
215  else if (strcmp(p, "yaml") == 0)
217  else
218  ereport(ERROR,
219  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
220  errmsg("unrecognized value for EXPLAIN option \"%s\": \"%s\"",
221  opt->defname, p),
222  parser_errposition(pstate, opt->location)));
223  }
224  else
225  ereport(ERROR,
226  (errcode(ERRCODE_SYNTAX_ERROR),
227  errmsg("unrecognized EXPLAIN option \"%s\"",
228  opt->defname),
229  parser_errposition(pstate, opt->location)));
230  }
231 
232  /* check that WAL is used with EXPLAIN ANALYZE */
233  if (es->wal && !es->analyze)
234  ereport(ERROR,
235  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
236  errmsg("EXPLAIN option WAL requires ANALYZE")));
237 
238  /* if the timing was not set explicitly, set default value */
239  es->timing = (timing_set) ? es->timing : es->analyze;
240 
241  /* check that timing is used with EXPLAIN ANALYZE */
242  if (es->timing && !es->analyze)
243  ereport(ERROR,
244  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
245  errmsg("EXPLAIN option TIMING requires ANALYZE")));
246 
247  /* check that GENERIC_PLAN is not used with EXPLAIN ANALYZE */
248  if (es->generic && es->analyze)
249  ereport(ERROR,
250  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
251  errmsg("EXPLAIN options ANALYZE and GENERIC_PLAN cannot be used together")));
252 
253  /* if the summary was not set explicitly, set default value */
254  es->summary = (summary_set) ? es->summary : es->analyze;
255 
256  query = castNode(Query, stmt->query);
257  if (IsQueryIdEnabled())
258  jstate = JumbleQuery(query);
259 
261  (*post_parse_analyze_hook) (pstate, query, jstate);
262 
263  /*
264  * Parse analysis was done already, but we still have to run the rule
265  * rewriter. We do not do AcquireRewriteLocks: we assume the query either
266  * came straight from the parser, or suitable locks were acquired by
267  * plancache.c.
268  */
269  rewritten = QueryRewrite(castNode(Query, stmt->query));
270 
271  /* emit opening boilerplate */
272  ExplainBeginOutput(es);
273 
274  if (rewritten == NIL)
275  {
276  /*
277  * In the case of an INSTEAD NOTHING, tell at least that. But in
278  * non-text format, the output is delimited, so this isn't necessary.
279  */
280  if (es->format == EXPLAIN_FORMAT_TEXT)
281  appendStringInfoString(es->str, "Query rewrites to nothing\n");
282  }
283  else
284  {
285  ListCell *l;
286 
287  /* Explain every plan */
288  foreach(l, rewritten)
289  {
291  CURSOR_OPT_PARALLEL_OK, NULL, es,
292  pstate->p_sourcetext, params, pstate->p_queryEnv);
293 
294  /* Separate plans with an appropriate separator */
295  if (lnext(rewritten, l) != NULL)
297  }
298  }
299 
300  /* emit closing boilerplate */
301  ExplainEndOutput(es);
302  Assert(es->indent == 0);
303 
304  /* output tuples */
306  &TTSOpsVirtual);
307  if (es->format == EXPLAIN_FORMAT_TEXT)
308  do_text_output_multiline(tstate, es->str->data);
309  else
310  do_text_output_oneline(tstate, es->str->data);
311  end_tup_output(tstate);
312 
313  pfree(es->str->data);
314 }
bool defGetBoolean(DefElem *def)
Definition: define.c:108
char * defGetString(DefElem *def)
Definition: define.c:49
int errcode(int sqlerrcode)
Definition: elog.c:858
int errmsg(const char *fmt,...)
Definition: elog.c:1069
#define ereport(elevel,...)
Definition: elog.h:149
const TupleTableSlotOps TTSOpsVirtual
Definition: execTuples.c:83
void end_tup_output(TupOutputState *tstate)
Definition: execTuples.c:2334
void do_text_output_multiline(TupOutputState *tstate, const char *txt)
Definition: execTuples.c:2304
TupOutputState * begin_tup_output_tupdesc(DestReceiver *dest, TupleDesc tupdesc, const TupleTableSlotOps *tts_ops)
Definition: execTuples.c:2256
#define do_text_output_oneline(tstate, str_to_emit)
Definition: executor.h:521
void ExplainSeparatePlans(ExplainState *es)
Definition: explain.c:4943
void ExplainEndOutput(ExplainState *es)
Definition: explain.c:4914
TupleDesc ExplainResultDesc(ExplainStmt *stmt)
Definition: explain.c:337
void ExplainBeginOutput(ExplainState *es)
Definition: explain.c:4883
ExplainState * NewExplainState(void)
Definition: explain.c:320
#define stmt
Definition: indent_codes.h:59
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
post_parse_analyze_hook_type post_parse_analyze_hook
Definition: analyze.c:60
#define lfirst_node(type, lc)
Definition: pg_list.h:176
static ListCell * lnext(const List *l, const ListCell *c)
Definition: pg_list.h:343
static bool IsQueryIdEnabled(void)
Definition: queryjumble.h:77
JumbleState * JumbleQuery(Query *query)
char * defname
Definition: parsenodes.h:809
int location
Definition: parsenodes.h:813

References ExplainState::analyze, appendStringInfoString(), Assert(), begin_tup_output_tupdesc(), ExplainState::buffers, castNode, ExplainState::costs, CURSOR_OPT_PARALLEL_OK, StringInfoData::data, defGetBoolean(), defGetString(), DefElem::defname, generate_unaccent_rules::dest, 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::generic, if(), ExplainState::indent, IsQueryIdEnabled(), JumbleQuery(), lfirst, lfirst_node, lnext(), DefElem::location, NewExplainState(), NIL, ParseState::p_queryEnv, ParseState::p_sourcetext, parser_errposition(), pfree(), post_parse_analyze_hook, QueryRewrite(), ExplainState::settings, stmt, ExplainState::str, ExplainState::summary, ExplainState::timing, TTSOpsVirtual, ExplainState::verbose, and ExplainState::wal.

Referenced by standard_ProcessUtility().

◆ ExplainQueryParameters()

void ExplainQueryParameters ( ExplainState es,
ParamListInfo  params,
int  maxlen 
)

Definition at line 1003 of file explain.c.

1004 {
1005  char *str;
1006 
1007  /* This check is consistent with errdetail_params() */
1008  if (params == NULL || params->numParams <= 0 || maxlen == 0)
1009  return;
1010 
1011  str = BuildParamLogString(params, NULL, maxlen);
1012  if (str && str[0] != '\0')
1013  ExplainPropertyText("Query Parameters", str, es);
1014 }
char * BuildParamLogString(ParamListInfo params, char **knownTextValues, int maxlen)
Definition: params.c:335

References BuildParamLogString(), ExplainPropertyText(), ParamListInfoData::numParams, and generate_unaccent_rules::str.

Referenced by explain_ExecutorEnd().

◆ ExplainQueryText()

void ExplainQueryText ( ExplainState es,
QueryDesc queryDesc 
)

Definition at line 988 of file explain.c.

989 {
990  if (queryDesc->sourceText)
991  ExplainPropertyText("Query Text", queryDesc->sourceText, es);
992 }
const char * sourceText
Definition: execdesc.h:38

References ExplainPropertyText(), and QueryDesc::sourceText.

Referenced by explain_ExecutorEnd().

◆ ExplainRestoreGroup()

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

Definition at line 4806 of file explain.c.

4807 {
4808  switch (es->format)
4809  {
4810  case EXPLAIN_FORMAT_TEXT:
4811  /* nothing to do */
4812  break;
4813 
4814  case EXPLAIN_FORMAT_XML:
4815  es->indent += depth;
4816  break;
4817 
4818  case EXPLAIN_FORMAT_JSON:
4819  es->grouping_stack = lcons_int(*state_save, es->grouping_stack);
4820  es->indent += depth;
4821  break;
4822 
4823  case EXPLAIN_FORMAT_YAML:
4824  es->grouping_stack = lcons_int(*state_save, es->grouping_stack);
4825  es->indent += depth;
4826  break;
4827  }
4828 }

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().

◆ ExplainResultDesc()

TupleDesc ExplainResultDesc ( ExplainStmt stmt)

Definition at line 337 of file explain.c.

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

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

Referenced by ExplainQuery(), and UtilityTupleDescriptor().

◆ ExplainSaveGroup()

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

Definition at line 4776 of file explain.c.

4777 {
4778  switch (es->format)
4779  {
4780  case EXPLAIN_FORMAT_TEXT:
4781  /* nothing to do */
4782  break;
4783 
4784  case EXPLAIN_FORMAT_XML:
4785  es->indent -= depth;
4786  break;
4787 
4788  case EXPLAIN_FORMAT_JSON:
4789  es->indent -= depth;
4790  *state_save = linitial_int(es->grouping_stack);
4792  break;
4793 
4794  case EXPLAIN_FORMAT_YAML:
4795  es->indent -= depth;
4796  *state_save = linitial_int(es->grouping_stack);
4798  break;
4799  }
4800 }

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().

◆ ExplainScanTarget()

static void ExplainScanTarget ( Scan plan,
ExplainState es 
)
static

Definition at line 3787 of file explain.c.

3788 {
3789  ExplainTargetRel((Plan *) plan, plan->scanrelid, es);
3790 }

References ExplainTargetRel(), and plan.

Referenced by ExplainNode().

◆ ExplainSeparatePlans()

void ExplainSeparatePlans ( ExplainState es)

Definition at line 4943 of file explain.c.

4944 {
4945  switch (es->format)
4946  {
4947  case EXPLAIN_FORMAT_TEXT:
4948  /* add a blank line */
4949  appendStringInfoChar(es->str, '\n');
4950  break;
4951 
4952  case EXPLAIN_FORMAT_XML:
4953  case EXPLAIN_FORMAT_JSON:
4954  case EXPLAIN_FORMAT_YAML:
4955  /* nothing to do */
4956  break;
4957  }
4958 }

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

Referenced by ExplainExecuteQuery(), and ExplainQuery().

◆ ExplainSubPlans()

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

Definition at line 4176 of file explain.c.

4178 {
4179  ListCell *lst;
4180 
4181  foreach(lst, plans)
4182  {
4183  SubPlanState *sps = (SubPlanState *) lfirst(lst);
4184  SubPlan *sp = sps->subplan;
4185 
4186  /*
4187  * There can be multiple SubPlan nodes referencing the same physical
4188  * subplan (same plan_id, which is its index in PlannedStmt.subplans).
4189  * We should print a subplan only once, so track which ones we already
4190  * printed. This state must be global across the plan tree, since the
4191  * duplicate nodes could be in different plan nodes, eg both a bitmap
4192  * indexscan's indexqual and its parent heapscan's recheck qual. (We
4193  * do not worry too much about which plan node we show the subplan as
4194  * attached to in such cases.)
4195  */
4196  if (bms_is_member(sp->plan_id, es->printed_subplans))
4197  continue;
4199  sp->plan_id);
4200 
4201  /*
4202  * Treat the SubPlan node as an ancestor of the plan node(s) within
4203  * it, so that ruleutils.c can find the referents of subplan
4204  * parameters.
4205  */
4206  ancestors = lcons(sp, ancestors);
4207 
4208  ExplainNode(sps->planstate, ancestors,
4209  relationship, sp->plan_name, es);
4210 
4211  ancestors = list_delete_first(ancestors);
4212  }
4213 }
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:460
struct PlanState * planstate
Definition: execnodes.h:953
SubPlan * subplan
Definition: execnodes.h:952
int plan_id
Definition: primnodes.h:997
char * plan_name
Definition: primnodes.h:999

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().

◆ ExplainTargetRel()

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

Definition at line 3809 of file explain.c.

3810 {
3811  char *objectname = NULL;
3812  char *namespace = NULL;
3813  const char *objecttag = NULL;
3814  RangeTblEntry *rte;
3815  char *refname;
3816 
3817  rte = rt_fetch(rti, es->rtable);
3818  refname = (char *) list_nth(es->rtable_names, rti - 1);
3819  if (refname == NULL)
3820  refname = rte->eref->aliasname;
3821 
3822  switch (nodeTag(plan))
3823  {
3824  case T_SeqScan:
3825  case T_SampleScan:
3826  case T_IndexScan:
3827  case T_IndexOnlyScan:
3828  case T_BitmapHeapScan:
3829  case T_TidScan:
3830  case T_TidRangeScan:
3831  case T_ForeignScan:
3832  case T_CustomScan:
3833  case T_ModifyTable:
3834  /* Assert it's on a real relation */
3835  Assert(rte->rtekind == RTE_RELATION);
3836  objectname = get_rel_name(rte->relid);
3837  if (es->verbose)
3838  namespace = get_namespace_name_or_temp(get_rel_namespace(rte->relid));
3839  objecttag = "Relation Name";
3840  break;
3841  case T_FunctionScan:
3842  {
3843  FunctionScan *fscan = (FunctionScan *) plan;
3844 
3845  /* Assert it's on a RangeFunction */
3846  Assert(rte->rtekind == RTE_FUNCTION);
3847 
3848  /*
3849  * If the expression is still a function call of a single
3850  * function, we can get the real name of the function.
3851  * Otherwise, punt. (Even if it was a single function call
3852  * originally, the optimizer could have simplified it away.)
3853  */
3854  if (list_length(fscan->functions) == 1)
3855  {
3856  RangeTblFunction *rtfunc = (RangeTblFunction *) linitial(fscan->functions);
3857 
3858  if (IsA(rtfunc->funcexpr, FuncExpr))
3859  {
3860  FuncExpr *funcexpr = (FuncExpr *) rtfunc->funcexpr;
3861  Oid funcid = funcexpr->funcid;
3862 
3863  objectname = get_func_name(funcid);
3864  if (es->verbose)
3865  namespace = get_namespace_name_or_temp(get_func_namespace(funcid));
3866  }
3867  }
3868  objecttag = "Function Name";
3869  }
3870  break;
3871  case T_TableFuncScan:
3872  Assert(rte->rtekind == RTE_TABLEFUNC);
3873  objectname = "xmltable";
3874  objecttag = "Table Function Name";
3875  break;
3876  case T_ValuesScan:
3877  Assert(rte->rtekind == RTE_VALUES);
3878  break;
3879  case T_CteScan:
3880  /* Assert it's on a non-self-reference CTE */
3881  Assert(rte->rtekind == RTE_CTE);
3882  Assert(!rte->self_reference);
3883  objectname = rte->ctename;
3884  objecttag = "CTE Name";
3885  break;
3886  case T_NamedTuplestoreScan:
3888  objectname = rte->enrname;
3889  objecttag = "Tuplestore Name";
3890  break;
3891  case T_WorkTableScan:
3892  /* Assert it's on a self-reference CTE */
3893  Assert(rte->rtekind == RTE_CTE);
3894  Assert(rte->self_reference);
3895  objectname = rte->ctename;
3896  objecttag = "CTE Name";
3897  break;
3898  default:
3899  break;
3900  }
3901 
3902  if (es->format == EXPLAIN_FORMAT_TEXT)
3903  {
3904  appendStringInfoString(es->str, " on");
3905  if (namespace != NULL)
3906  appendStringInfo(es->str, " %s.%s", quote_identifier(namespace),
3907  quote_identifier(objectname));
3908  else if (objectname != NULL)
3909  appendStringInfo(es->str, " %s", quote_identifier(objectname));
3910  if (objectname == NULL || strcmp(refname, objectname) != 0)
3911  appendStringInfo(es->str, " %s", quote_identifier(refname));
3912  }
3913  else
3914  {
3915  if (objecttag != NULL && objectname != NULL)
3916  ExplainPropertyText(objecttag, objectname, es);
3917  if (namespace != NULL)
3918  ExplainPropertyText("Schema", namespace, es);
3919  ExplainPropertyText("Alias", refname, es);
3920  }
3921 }
char * get_namespace_name_or_temp(Oid nspid)
Definition: lsyscache.c:3372
Oid get_rel_namespace(Oid relid)
Definition: lsyscache.c:1956
char * get_func_name(Oid funcid)
Definition: lsyscache.c:1612
Oid get_func_namespace(Oid funcid)
Definition: lsyscache.c:1636
@ RTE_CTE
Definition: parsenodes.h:1019
@ RTE_NAMEDTUPLESTORE
Definition: parsenodes.h:1020
@ RTE_VALUES
Definition: parsenodes.h:1018
@ RTE_FUNCTION
Definition: parsenodes.h:1016
@ RTE_TABLEFUNC
Definition: parsenodes.h:1017
@ RTE_RELATION
Definition: parsenodes.h:1013
#define rt_fetch(rangetable_index, rangetable)
Definition: parsetree.h:31
#define linitial(l)
Definition: pg_list.h:178
static void * list_nth(const List *list, int n)
Definition: pg_list.h:299
char * aliasname
Definition: primnodes.h:42
Oid funcid
Definition: primnodes.h:677
List * functions
Definition: plannodes.h:610
char * ctename
Definition: parsenodes.h:1163
bool self_reference
Definition: parsenodes.h:1165
Alias * eref
Definition: parsenodes.h:1199
char * enrname
Definition: parsenodes.h:1192
RTEKind rtekind
Definition: parsenodes.h:1032

References Alias::aliasname, appendStringInfo(), appendStringInfoString(), Assert(), RangeTblEntry::eref, EXPLAIN_FORMAT_TEXT, ExplainPropertyText(), ExplainState::format, RangeTblFunction::funcexpr, FuncExpr::funcid, FunctionScan::functions, get_func_name(), get_func_namespace(), get_namespace_name_or_temp(), get_rel_name(), get_rel_namespace(), IsA, linitial, list_length(), list_nth(), nodeTag, plan, 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, and ExplainState::verbose.

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

◆ ExplainXMLTag()

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

Definition at line 4972 of file explain.c.

4973 {
4974  const char *s;
4975  const char *valid = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.";
4976 
4977  if ((flags & X_NOWHITESPACE) == 0)
4978  appendStringInfoSpaces(es->str, 2 * es->indent);
4979  appendStringInfoCharMacro(es->str, '<');
4980  if ((flags & X_CLOSING) != 0)
4981  appendStringInfoCharMacro(es->str, '/');
4982  for (s = tagname; *s; s++)
4983  appendStringInfoChar(es->str, strchr(valid, *s) ? *s : '-');
4984  if ((flags & X_CLOSE_IMMEDIATE) != 0)
4985  appendStringInfoString(es->str, " /");
4986  appendStringInfoCharMacro(es->str, '>');
4987  if ((flags & X_NOWHITESPACE) == 0)
4988  appendStringInfoCharMacro(es->str, '\n');
4989 }
#define appendStringInfoCharMacro(str, ch)
Definition: stringinfo.h:128

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().

◆ ExplainYAMLLineStarting()

static void ExplainYAMLLineStarting ( ExplainState es)
static

◆ NewExplainState()

ExplainState* NewExplainState ( void  )

Definition at line 320 of file explain.c.

321 {
322  ExplainState *es = (ExplainState *) palloc0(sizeof(ExplainState));
323 
324  /* Set default options (most fields can be left as zeroes). */
325  es->costs = true;
326  /* Prepare output buffer. */
327  es->str = makeStringInfo();
328 
329  return es;
330 }
StringInfo makeStringInfo(void)
Definition: stringinfo.c:41

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

Referenced by explain_ExecutorEnd(), and ExplainQuery().

◆ report_triggers()

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

Definition at line 1021 of file explain.c.

1022 {
1023  int nt;
1024 
1025  if (!rInfo->ri_TrigDesc || !rInfo->ri_TrigInstrument)
1026  return;
1027  for (nt = 0; nt < rInfo->ri_TrigDesc->numtriggers; nt++)
1028  {
1029  Trigger *trig = rInfo->ri_TrigDesc->triggers + nt;
1030  Instrumentation *instr = rInfo->ri_TrigInstrument + nt;
1031  char *relname;
1032  char *conname = NULL;
1033 
1034  /* Must clean up instrumentation state */
1035  InstrEndLoop(instr);
1036 
1037  /*
1038  * We ignore triggers that were never invoked; they likely aren't
1039  * relevant to the current query type.
1040  */
1041  if (instr->ntuples == 0)
1042  continue;
1043 
1044  ExplainOpenGroup("Trigger", NULL, true, es);
1045 
1047  if (OidIsValid(trig->tgconstraint))
1048  conname = get_constraint_name(trig->tgconstraint);
1049 
1050  /*
1051  * In text format, we avoid printing both the trigger name and the
1052  * constraint name unless VERBOSE is specified. In non-text formats
1053  * we just print everything.
1054  */
1055  if (es->format == EXPLAIN_FORMAT_TEXT)
1056  {
1057  if (es->verbose || conname == NULL)
1058  appendStringInfo(es->str, "Trigger %s", trig->tgname);
1059  else
1060  appendStringInfoString(es->str, "Trigger");
1061  if (conname)
1062  appendStringInfo(es->str, " for constraint %s", conname);
1063  if (show_relname)
1064  appendStringInfo(es->str, " on %s", relname);
1065  if (es->timing)
1066  appendStringInfo(es->str, ": time=%.3f calls=%.0f\n",
1067  1000.0 * instr->total, instr->ntuples);
1068  else
1069  appendStringInfo(es->str, ": calls=%.0f\n", instr->ntuples);
1070  }
1071  else
1072  {
1073  ExplainPropertyText("Trigger Name", trig->tgname, es);
1074  if (conname)
1075  ExplainPropertyText("Constraint Name", conname, es);
1076  ExplainPropertyText("Relation", relname, es);
1077  if (es->timing)
1078  ExplainPropertyFloat("Time", "ms", 1000.0 * instr->total, 3,
1079  es);
1080  ExplainPropertyFloat("Calls", NULL, instr->ntuples, 0, es);
1081  }
1082 
1083  if (conname)
1084  pfree(conname);
1085 
1086  ExplainCloseGroup("Trigger", NULL, true, es);
1087  }
1088 }
#define OidIsValid(objectId)
Definition: c.h:764
char * get_constraint_name(Oid conoid)
Definition: lsyscache.c:1107
NameData relname
Definition: pg_class.h:38
#define RelationGetRelationName(relation)
Definition: rel.h:538
Instrumentation * ri_TrigInstrument
Definition: execnodes.h:489
Relation ri_RelationDesc
Definition: execnodes.h:450
TriggerDesc * ri_TrigDesc
Definition: execnodes.h:480
int numtriggers
Definition: reltrigger.h:50
Trigger * triggers
Definition: reltrigger.h:49
Oid tgconstraint
Definition: reltrigger.h:35
char * tgname
Definition: reltrigger.h:27

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().

◆ show_agg_keys()

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

Definition at line 2434 of file explain.c.

2436 {
2437  Agg *plan = (Agg *) astate->ss.ps.plan;
2438 
2439  if (plan->numCols > 0 || plan->groupingSets)
2440  {
2441  /* The key columns refer to the tlist of the child plan */
2442  ancestors = lcons(plan, ancestors);
2443 
2444  if (plan->groupingSets)
2445  show_grouping_sets(outerPlanState(astate), plan, ancestors, es);
2446  else
2447  show_sort_group_keys(outerPlanState(astate), "Group Key",
2448  plan->numCols, 0, plan->grpColIdx,
2449  NULL, NULL, NULL,
2450  ancestors, es);
2451 
2452  ancestors = list_delete_first(ancestors);
2453  }
2454 }
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:2586
static void show_grouping_sets(PlanState *planstate, Agg *agg, List *ancestors, ExplainState *es)
Definition: explain.c:2457
ScanState ss
Definition: execnodes.h:2379
PlanState ps
Definition: execnodes.h:1474

References if(), lcons(), list_delete_first(), outerPlanState, PlanState::plan, plan, ScanState::ps, show_grouping_sets(), show_sort_group_keys(), and AggState::ss.

Referenced by ExplainNode().

◆ show_buffer_usage()

static void show_buffer_usage ( ExplainState es,
const BufferUsage usage,
bool  planning 
)
static

Definition at line 3551 of file explain.c.

3552 {
3553  if (es->format == EXPLAIN_FORMAT_TEXT)
3554  {
3555  bool has_shared = (usage->shared_blks_hit > 0 ||
3556  usage->shared_blks_read > 0 ||
3557  usage->shared_blks_dirtied > 0 ||
3558  usage->shared_blks_written > 0);
3559  bool has_local = (usage->local_blks_hit > 0 ||
3560  usage->local_blks_read > 0 ||
3561  usage->local_blks_dirtied > 0 ||
3562  usage->local_blks_written > 0);
3563  bool has_temp = (usage->temp_blks_read > 0 ||
3564  usage->temp_blks_written > 0);
3565  bool has_timing = (!INSTR_TIME_IS_ZERO(usage->blk_read_time) ||
3566  !INSTR_TIME_IS_ZERO(usage->blk_write_time));
3567  bool has_temp_timing = (!INSTR_TIME_IS_ZERO(usage->temp_blk_read_time) ||
3568  !INSTR_TIME_IS_ZERO(usage->temp_blk_write_time));
3569  bool show_planning = (planning && (has_shared ||
3570  has_local || has_temp || has_timing ||
3571  has_temp_timing));
3572 
3573  if (show_planning)
3574  {
3575  ExplainIndentText(es);
3576  appendStringInfoString(es->str, "Planning:\n");
3577  es->indent++;
3578  }
3579 
3580  /* Show only positive counter values. */
3581  if (has_shared || has_local || has_temp)
3582  {
3583  ExplainIndentText(es);
3584  appendStringInfoString(es->str, "Buffers:");
3585 
3586  if (has_shared)
3587  {
3588  appendStringInfoString(es->str, " shared");
3589  if (usage->shared_blks_hit > 0)
3590  appendStringInfo(es->str, " hit=%lld",
3591  (long long) usage->shared_blks_hit);
3592  if (usage->shared_blks_read > 0)
3593  appendStringInfo(es->str, " read=%lld",
3594  (long long) usage->shared_blks_read);
3595  if (usage->shared_blks_dirtied > 0)
3596  appendStringInfo(es->str, " dirtied=%lld",
3597  (long long) usage->shared_blks_dirtied);
3598  if (usage->shared_blks_written > 0)
3599  appendStringInfo(es->str, " written=%lld",
3600  (long long) usage->shared_blks_written);
3601  if (has_local || has_temp)
3602  appendStringInfoChar(es->str, ',');
3603  }
3604  if (has_local)
3605  {
3606  appendStringInfoString(es->str, " local");
3607  if (usage->local_blks_hit > 0)
3608  appendStringInfo(es->str, " hit=%lld",
3609  (long long) usage->local_blks_hit);
3610  if (usage->local_blks_read > 0)
3611  appendStringInfo(es->str, " read=%lld",
3612  (long long) usage->local_blks_read);
3613  if (usage->local_blks_dirtied > 0)
3614  appendStringInfo(es->str, " dirtied=%lld",
3615  (long long) usage->local_blks_dirtied);
3616  if (usage->local_blks_written > 0)
3617  appendStringInfo(es->str, " written=%lld",
3618  (long long) usage->local_blks_written);
3619  if (has_temp)
3620  appendStringInfoChar(es->str, ',');
3621  }
3622  if (has_temp)
3623  {
3624  appendStringInfoString(es->str, " temp");
3625  if (usage->temp_blks_read > 0)
3626  appendStringInfo(es->str, " read=%lld",
3627  (long long) usage->temp_blks_read);
3628  if (usage->temp_blks_written > 0)
3629  appendStringInfo(es->str, " written=%lld",
3630  (long long) usage->temp_blks_written);
3631  }
3632  appendStringInfoChar(es->str, '\n');
3633  }
3634 
3635  /* As above, show only positive counter values. */
3636  if (has_timing || has_temp_timing)
3637  {
3638  ExplainIndentText(es);
3639  appendStringInfoString(es->str, "I/O Timings:");
3640 
3641  if (has_timing)
3642  {
3643  appendStringInfoString(es->str, " shared/local");
3644  if (!INSTR_TIME_IS_ZERO(usage->blk_read_time))
3645  appendStringInfo(es->str, " read=%0.3f",
3646  INSTR_TIME_GET_MILLISEC(usage->blk_read_time));
3647  if (!INSTR_TIME_IS_ZERO(usage->blk_write_time))
3648  appendStringInfo(es->str, " write=%0.3f",
3649  INSTR_TIME_GET_MILLISEC(usage->blk_write_time));
3650  if (has_temp_timing)
3651  appendStringInfoChar(es->str, ',');
3652  }
3653  if (has_temp_timing)
3654  {
3655  appendStringInfoString(es->str, " temp");
3656  if (!INSTR_TIME_IS_ZERO(usage->temp_blk_read_time))
3657  appendStringInfo(es->str, " read=%0.3f",
3658  INSTR_TIME_GET_MILLISEC(usage->temp_blk_read_time));
3659  if (!INSTR_TIME_IS_ZERO(usage->temp_blk_write_time))
3660  appendStringInfo(es->str, " write=%0.3f",
3661  INSTR_TIME_GET_MILLISEC(usage->temp_blk_write_time));
3662  }
3663  appendStringInfoChar(es->str, '\n');
3664  }
3665 
3666  if (show_planning)
3667  es->indent--;
3668  }
3669  else
3670  {
3671  ExplainPropertyInteger("Shared Hit Blocks", NULL,
3672  usage->shared_blks_hit, es);
3673  ExplainPropertyInteger("Shared Read Blocks", NULL,
3674  usage->shared_blks_read, es);
3675  ExplainPropertyInteger("Shared Dirtied Blocks", NULL,
3676  usage->shared_blks_dirtied, es);
3677  ExplainPropertyInteger("Shared Written Blocks", NULL,
3678  usage->shared_blks_written, es);
3679  ExplainPropertyInteger("Local Hit Blocks", NULL,
3680  usage->local_blks_hit, es);
3681  ExplainPropertyInteger("Local Read Blocks", NULL,
3682  usage->local_blks_read, es);
3683  ExplainPropertyInteger("Local Dirtied Blocks", NULL,
3684  usage->local_blks_dirtied, es);
3685  ExplainPropertyInteger("Local Written Blocks", NULL,
3686  usage->local_blks_written, es);
3687  ExplainPropertyInteger("Temp Read Blocks", NULL,
3688  usage->temp_blks_read, es);
3689  ExplainPropertyInteger("Temp Written Blocks", NULL,
3690  usage->temp_blks_written, es);
3691  if (track_io_timing)
3692  {
3693  ExplainPropertyFloat("I/O Read Time", "ms",
3694  INSTR_TIME_GET_MILLISEC(usage->blk_read_time),
3695  3, es);
3696  ExplainPropertyFloat("I/O Write Time", "ms",
3697  INSTR_TIME_GET_MILLISEC(usage->blk_write_time),
3698  3, es);
3699  ExplainPropertyFloat("Temp I/O Read Time", "ms",
3700  INSTR_TIME_GET_MILLISEC(usage->temp_blk_read_time),
3701  3, es);
3702  ExplainPropertyFloat("Temp I/O Write Time", "ms",
3703  INSTR_TIME_GET_MILLISEC(usage->temp_blk_write_time),
3704  3, es);
3705  }
3706  }
3707 }
bool track_io_timing
Definition: bufmgr.c:138
#define INSTR_TIME_IS_ZERO(t)
Definition: instr_time.h:169
#define INSTR_TIME_GET_MILLISEC(t)
Definition: instr_time.h:191
static void usage(const char *progname)
Definition: vacuumlo.c:414

References appendStringInfo(), appendStringInfoChar(), appendStringInfoString(), EXPLAIN_FORMAT_TEXT, ExplainIndentText(), ExplainPropertyFloat(), ExplainPropertyInteger(), ExplainState::format, ExplainState::indent, INSTR_TIME_GET_MILLISEC, INSTR_TIME_IS_ZERO, ExplainState::str, track_io_timing, and usage().

Referenced by ExplainNode(), and ExplainOnePlan().

◆ show_eval_params()

static void show_eval_params ( Bitmapset bms_params,
ExplainState es 
)
static

Definition at line 3499 of file explain.c.

3500 {
3501  int paramid = -1;
3502  List *params = NIL;
3503 
3504  Assert(bms_params);
3505 
3506  while ((paramid = bms_next_member(bms_params, paramid)) >= 0)
3507  {
3508  char param[32];
3509 
3510  snprintf(param, sizeof(param), "$%d", paramid);
3511  params = lappend(params, pstrdup(param));
3512  }
3513 
3514  if (params)
3515  ExplainPropertyList("Params Evaluated", params, es);
3516 }
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1106
char * pstrdup(const char *in)
Definition: mcxt.c:1644

References Assert(), bms_next_member(), ExplainPropertyList(), lappend(), NIL, pstrdup(), and snprintf.

Referenced by ExplainNode().

◆ show_expression()

static void show_expression ( Node node,
const char *  qlabel,
PlanState planstate,
List ancestors,
bool  useprefix,
ExplainState es 
)
static

Definition at line 2314 of file explain.c.

2317 {
2318  List *context;
2319  char *exprstr;
2320 
2321  /* Set up deparsing context */
2322  context = set_deparse_context_plan(es->deparse_cxt,
2323  planstate->plan,
2324  ancestors);
2325 
2326  /* Deparse the expression */
2327  exprstr = deparse_expression(node, context, useprefix, false);
2328 
2329  /* And add to es->str */
2330  ExplainPropertyText(qlabel, exprstr, es);
2331 }
char * deparse_expression(Node *expr, List *dpcontext, bool forceprefix, bool showimplicit)
Definition: ruleutils.c:3600
List * set_deparse_context_plan(List *dpcontext, Plan *plan, List *ancestors)
Definition: ruleutils.c:3774

References config_generic::context, ExplainState::deparse_cxt, deparse_expression(), ExplainPropertyText(), PlanState::plan, and set_deparse_context_plan().

Referenced by ExplainNode(), and show_qual().

◆ show_foreignscan_info()

static void show_foreignscan_info ( ForeignScanState fsstate,
ExplainState es 
)
static

Definition at line 3478 of file explain.c.

3479 {
3480  FdwRoutine *fdwroutine = fsstate->fdwroutine;
3481 
3482  /* Let the FDW emit whatever fields it wants */
3483  if (((ForeignScan *) fsstate->ss.ps.plan)->operation != CMD_SELECT)
3484  {
3485  if (fdwroutine->ExplainDirectModify != NULL)
3486  fdwroutine->ExplainDirectModify(fsstate, es);
3487  }
3488  else
3489  {
3490  if (fdwroutine->ExplainForeignScan != NULL)
3491  fdwroutine->ExplainForeignScan(fsstate, es);
3492  }
3493 }
ExplainForeignScan_function ExplainForeignScan
Definition: fdwapi.h:252
ExplainDirectModify_function ExplainDirectModify
Definition: fdwapi.h:254
struct FdwRoutine * fdwroutine
Definition: execnodes.h:1959
ScanState ss
Definition: execnodes.h:1954

References CMD_SELECT, FdwRoutine::ExplainDirectModify, FdwRoutine::ExplainForeignScan, ForeignScanState::fdwroutine, PlanState::plan, ScanState::ps, and ForeignScanState::ss.

Referenced by ExplainNode().

◆ show_group_keys()

static void show_group_keys ( GroupState gstate,
List ancestors,
ExplainState es 
)
static

Definition at line 2566 of file explain.c.

2568 {
2569  Group *plan = (Group *) gstate->ss.ps.plan;
2570 
2571  /* The key columns refer to the tlist of the child plan */
2572  ancestors = lcons(plan, ancestors);
2573  show_sort_group_keys(outerPlanState(gstate), "Group Key",
2574  plan->numCols, 0, plan->grpColIdx,
2575  NULL, NULL, NULL,
2576  ancestors, es);
2577  ancestors = list_delete_first(ancestors);
2578 }
ScanState ss
Definition: execnodes.h:2332

References lcons(), list_delete_first(), outerPlanState, PlanState::plan, plan, ScanState::ps, show_sort_group_keys(), and GroupState::ss.

Referenced by ExplainNode().

◆ show_grouping_set_keys()

static void show_grouping_set_keys ( PlanState planstate,
Agg aggnode,
Sort sortnode,
List context,
bool  useprefix,
List ancestors,
ExplainState es 
)
static

Definition at line 2488 of file explain.c.

2492 {
2493  Plan *plan = planstate->plan;
2494  char *exprstr;
2495  ListCell *lc;
2496  List *gsets = aggnode->groupingSets;
2497  AttrNumber *keycols = aggnode->grpColIdx;
2498  const char *keyname;
2499  const char *keysetname;
2500 
2501  if (aggnode->aggstrategy == AGG_HASHED || aggnode->aggstrategy == AGG_MIXED)
2502  {
2503  keyname = "Hash Key";
2504  keysetname = "Hash Keys";
2505  }
2506  else
2507  {
2508  keyname = "Group Key";
2509  keysetname = "Group Keys";
2510  }
2511 
2512  ExplainOpenGroup("Grouping Set", NULL, true, es);
2513 
2514  if (sortnode)
2515  {
2516  show_sort_group_keys(planstate, "Sort Key",
2517  sortnode->numCols, 0, sortnode->sortColIdx,
2518  sortnode->sortOperators, sortnode->collations,
2519  sortnode->nullsFirst,
2520  ancestors, es);
2521  if (es->format == EXPLAIN_FORMAT_TEXT)
2522  es->indent++;
2523  }
2524 
2525  ExplainOpenGroup(keysetname, keysetname, false, es);
2526 
2527  foreach(lc, gsets)
2528  {
2529  List *result = NIL;
2530  ListCell *lc2;
2531 
2532  foreach(lc2, (List *) lfirst(lc))
2533  {
2534  Index i = lfirst_int(lc2);
2535  AttrNumber keyresno = keycols[i];
2536  TargetEntry *target = get_tle_by_resno(plan->targetlist,
2537  keyresno);
2538 
2539  if (!target)
2540  elog(ERROR, "no tlist entry for key %d", keyresno);
2541  /* Deparse the expression, showing any top-level cast */
2542  exprstr = deparse_expression((Node *) target->expr, context,
2543  useprefix, true);
2544 
2545  result = lappend(result, exprstr);
2546  }
2547 
2548  if (!result && es->format == EXPLAIN_FORMAT_TEXT)
2549  ExplainPropertyText(keyname, "()", es);
2550  else
2551  ExplainPropertyListNested(keyname, result, es);
2552  }
2553 
2554  ExplainCloseGroup(keysetname, keysetname, false, es);
2555 
2556  if (sortnode && es->format == EXPLAIN_FORMAT_TEXT)
2557  es->indent--;
2558 
2559  ExplainCloseGroup("Grouping Set", NULL, true, es);
2560 }
unsigned int Index
Definition: c.h:603
void ExplainPropertyListNested(const char *qlabel, List *data, ExplainState *es)
Definition: explain.c:4456
TargetEntry * get_tle_by_resno(List *tlist, AttrNumber resno)
#define lfirst_int(lc)
Definition: pg_list.h:173
List * groupingSets
Definition: plannodes.h:1026
int numCols
Definition: plannodes.h:934
Expr * expr
Definition: primnodes.h:1895

References AGG_HASHED, AGG_MIXED, Agg::aggstrategy, config_generic::context, deparse_expression(), elog(), ERROR, EXPLAIN_FORMAT_TEXT, ExplainCloseGroup(), ExplainOpenGroup(), ExplainPropertyListNested(), ExplainPropertyText(), TargetEntry::expr, ExplainState::format, get_tle_by_resno(), Agg::groupingSets, i, ExplainState::indent, lappend(), lfirst, lfirst_int, NIL, Sort::numCols, PlanState::plan, plan, and show_sort_group_keys().

Referenced by show_grouping_sets().

◆ show_grouping_sets()

static void show_grouping_sets ( PlanState planstate,
Agg agg,
List ancestors,
ExplainState es 
)
static

Definition at line 2457 of file explain.c.

2459 {
2460  List *context;
2461  bool useprefix;
2462  ListCell *lc;
2463 
2464  /* Set up deparsing context */
2465  context = set_deparse_context_plan(es->deparse_cxt,
2466  planstate->plan,
2467  ancestors);
2468  useprefix = (list_length(es->rtable) > 1 || es->verbose);
2469 
2470  ExplainOpenGroup("Grouping Sets", "Grouping Sets", false, es);
2471 
2472  show_grouping_set_keys(planstate, agg, NULL,
2473  context, useprefix, ancestors, es);
2474 
2475  foreach(lc, agg->chain)
2476  {
2477  Agg *aggnode = lfirst(lc);
2478  Sort *sortnode = (Sort *) aggnode->plan.lefttree;
2479 
2480  show_grouping_set_keys(planstate, aggnode, sortnode,
2481  context, useprefix, ancestors, es);
2482  }
2483 
2484  ExplainCloseGroup("Grouping Sets", "Grouping Sets", false, es);
2485 }
static void show_grouping_set_keys(PlanState *planstate, Agg *aggnode, Sort *sortnode, List *context, bool useprefix, List *ancestors, ExplainState *es)
Definition: explain.c:2488
List * chain
Definition: plannodes.h:1029
Plan plan
Definition: plannodes.h:996
struct Plan * lefttree
Definition: plannodes.h:155

References Agg::chain, config_generic::context, ExplainState::deparse_cxt, ExplainCloseGroup(), ExplainOpenGroup(), Plan::lefttree, lfirst, list_length(), PlanState::plan, Agg::plan, ExplainState::rtable, set_deparse_context_plan(), show_grouping_set_keys(), and ExplainState::verbose.

Referenced by show_agg_keys().

◆ show_hash_info()

static void show_hash_info ( HashState hashstate,
ExplainState es 
)
static

Definition at line 3063 of file explain.c.

3064 {
3065  HashInstrumentation hinstrument = {0};
3066 
3067  /*
3068  * Collect stats from the local process, even when it's a parallel query.
3069  * In a parallel query, the leader process may or may not have run the
3070  * hash join, and even if it did it may not have built a hash table due to
3071  * timing (if it started late it might have seen no tuples in the outer
3072  * relation and skipped building the hash table). Therefore we have to be
3073  * prepared to get instrumentation data from all participants.
3074  */
3075  if (hashstate->hinstrument)
3076  memcpy(&hinstrument, hashstate->hinstrument,
3077  sizeof(HashInstrumentation));
3078 
3079  /*
3080  * Merge results from workers. In the parallel-oblivious case, the
3081  * results from all participants should be identical, except where
3082  * participants didn't run the join at all so have no data. In the
3083  * parallel-aware case, we need to consider all the results. Each worker
3084  * may have seen a different subset of batches and we want to report the
3085  * highest memory usage across all batches. We take the maxima of other
3086  * values too, for the same reasons as in ExecHashAccumInstrumentation.
3087  */
3088  if (hashstate->shared_info)
3089  {
3090  SharedHashInfo *shared_info = hashstate->shared_info;
3091  int i;
3092 
3093  for (i = 0; i < shared_info->num_workers; ++i)
3094  {
3095  HashInstrumentation *worker_hi = &shared_info->hinstrument[i];
3096 
3097  hinstrument.nbuckets = Max(hinstrument.nbuckets,
3098  worker_hi->nbuckets);
3099  hinstrument.nbuckets_original = Max(hinstrument.nbuckets_original,
3100  worker_hi->nbuckets_original);
3101  hinstrument.nbatch = Max(hinstrument.nbatch,
3102  worker_hi->nbatch);
3103  hinstrument.nbatch_original = Max(hinstrument.nbatch_original,
3104  worker_hi->nbatch_original);
3105  hinstrument.space_peak = Max(hinstrument.space_peak,
3106  worker_hi->space_peak);
3107  }
3108  }
3109 
3110  if (hinstrument.nbatch > 0)
3111  {
3112  long spacePeakKb = (hinstrument.space_peak + 1023) / 1024;
3113 
3114  if (es->format != EXPLAIN_FORMAT_TEXT)
3115  {
3116  ExplainPropertyInteger("Hash Buckets", NULL,
3117  hinstrument.nbuckets, es);
3118  ExplainPropertyInteger("Original Hash Buckets", NULL,
3119  hinstrument.nbuckets_original, es);
3120  ExplainPropertyInteger("Hash Batches", NULL,
3121  hinstrument.nbatch, es);
3122  ExplainPropertyInteger("Original Hash Batches", NULL,
3123  hinstrument.nbatch_original, es);
3124  ExplainPropertyInteger("Peak Memory Usage", "kB",
3125  spacePeakKb, es);
3126  }
3127  else if (hinstrument.nbatch_original != hinstrument.nbatch ||
3128  hinstrument.nbuckets_original != hinstrument.nbuckets)
3129  {
3130  ExplainIndentText(es);
3131  appendStringInfo(es->str,
3132  "Buckets: %d (originally %d) Batches: %d (originally %d) Memory Usage: %ldkB\n",
3133  hinstrument.nbuckets,
3134  hinstrument.nbuckets_original,
3135  hinstrument.nbatch,
3136  hinstrument.nbatch_original,
3137  spacePeakKb);
3138  }
3139  else
3140  {
3141  ExplainIndentText(es);
3142  appendStringInfo(es->str,
3143  "Buckets: %d Batches: %d Memory Usage: %ldkB\n",
3144  hinstrument.nbuckets, hinstrument.nbatch,
3145  spacePeakKb);
3146  }
3147  }
3148 }
#define Max(x, y)
Definition: c.h:987
SharedHashInfo * shared_info
Definition: execnodes.h:2671
HashInstrumentation * hinstrument
Definition: execnodes.h:2678
HashInstrumentation hinstrument[FLEXIBLE_ARRAY_MEMBER]
Definition: execnodes.h:2652

References appendStringInfo(), EXPLAIN_FORMAT_TEXT, ExplainIndentText(), ExplainPropertyInteger(), ExplainState::format, SharedHashInfo::hinstrument, HashState::hinstrument, i, Max, HashInstrumentation::nbatch, HashInstrumentation::nbatch_original, HashInstrumentation::nbuckets, HashInstrumentation::nbuckets_original, SharedHashInfo::num_workers, HashState::shared_info, HashInstrumentation::space_peak, and ExplainState::str.

Referenced by ExplainNode().

◆ show_hashagg_info()

static void show_hashagg_info ( AggState aggstate,
ExplainState es 
)
static

Definition at line 3298 of file explain.c.

3299 {
3300  Agg *agg = (Agg *) aggstate->ss.ps.plan;
3301  int64 memPeakKb = (aggstate->hash_mem_peak + 1023) / 1024;
3302 
3303  if (agg->aggstrategy != AGG_HASHED &&
3304  agg->aggstrategy != AGG_MIXED)
3305  return;
3306 
3307  if (es->format != EXPLAIN_FORMAT_TEXT)
3308  {
3309  if (es->costs)
3310  ExplainPropertyInteger("Planned Partitions", NULL,
3311  aggstate->hash_planned_partitions, es);
3312 
3313  /*
3314  * During parallel query the leader may have not helped out. We
3315  * detect this by checking how much memory it used. If we find it
3316  * didn't do any work then we don't show its properties.
3317  */
3318  if (es->analyze && aggstate->hash_mem_peak > 0)
3319  {
3320  ExplainPropertyInteger("HashAgg Batches", NULL,
3321  aggstate->hash_batches_used, es);
3322  ExplainPropertyInteger("Peak Memory Usage", "kB", memPeakKb, es);
3323  ExplainPropertyInteger("Disk Usage", "kB",
3324  aggstate->hash_disk_used, es);
3325  }
3326  }
3327  else
3328  {
3329  bool gotone = false;
3330 
3331  if (es->costs && aggstate->hash_planned_partitions > 0)
3332  {
3333  ExplainIndentText(es);
3334  appendStringInfo(es->str, "Planned Partitions: %d",
3335  aggstate->hash_planned_partitions);
3336  gotone = true;
3337  }
3338 
3339  /*
3340  * During parallel query the leader may have not helped out. We
3341  * detect this by checking how much memory it used. If we find it
3342  * didn't do any work then we don't show its properties.
3343  */
3344  if (es->analyze && aggstate->hash_mem_peak > 0)
3345  {
3346  if (!gotone)
3347  ExplainIndentText(es);
3348  else
3349  appendStringInfoSpaces(es->str, 2);
3350 
3351  appendStringInfo(es->str, "Batches: %d Memory Usage: " INT64_FORMAT "kB",
3352  aggstate->hash_batches_used, memPeakKb);
3353  gotone = true;
3354 
3355  /* Only display disk usage if we spilled to disk */
3356  if (aggstate->hash_batches_used > 1)
3357  {
3358  appendStringInfo(es->str, " Disk Usage: " UINT64_FORMAT "kB",
3359  aggstate->hash_disk_used);
3360  }
3361  }
3362 
3363  if (gotone)
3364  appendStringInfoChar(es->str, '\n');
3365  }
3366 
3367  /* Display stats for each parallel worker */
3368  if (es->analyze && aggstate->shared_info != NULL)
3369  {
3370  for (int n = 0; n < aggstate->shared_info->num_workers; n++)
3371  {
3372  AggregateInstrumentation *sinstrument;
3373  uint64 hash_disk_used;
3374  int hash_batches_used;
3375 
3376  sinstrument = &aggstate->shared_info->sinstrument[n];
3377  /* Skip workers that didn't do anything */
3378  if (sinstrument->hash_mem_peak == 0)
3379  continue;
3380  hash_disk_used = sinstrument->hash_disk_used;
3381  hash_batches_used = sinstrument->hash_batches_used;
3382  memPeakKb = (sinstrument->hash_mem_peak + 1023) / 1024;
3383 
3384  if (es->workers_state)
3385  ExplainOpenWorker(n, es);
3386 
3387  if (es->format == EXPLAIN_FORMAT_TEXT)
3388  {
3389  ExplainIndentText(es);
3390 
3391  appendStringInfo(es->str, "Batches: %d Memory Usage: " INT64_FORMAT "kB",
3392  hash_batches_used, memPeakKb);
3393 
3394  /* Only display disk usage if we spilled to disk */
3395  if (hash_batches_used > 1)
3396  appendStringInfo(es->str, " Disk Usage: " UINT64_FORMAT "kB",
3397  hash_disk_used);
3398  appendStringInfoChar(es->str, '\n');
3399  }
3400  else