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 *hashstate, ExplainState *es)
 
static void show_tidbitmap_info (BitmapHeapScanState *planstate, ExplainState *es)
 
static void show_instrumentation_count (const char *qlabel, int which, PlanState *planstate, ExplainState *es)
 
static void show_foreignscan_info (ForeignScanState *fsstate, ExplainState *es)
 
static void show_eval_params (Bitmapset *bms_params, ExplainState *es)
 
static const char * explain_get_index_name (Oid indexId)
 
static void show_buffer_usage (ExplainState *es, const BufferUsage *usage, bool planning)
 
static void show_wal_usage (ExplainState *es, const WalUsage *usage)
 
static void ExplainIndexScanDetails (Oid indexid, ScanDirection indexorderdir, ExplainState *es)
 
static void ExplainScanTarget (Scan *plan, ExplainState *es)
 
static void ExplainModifyTarget (ModifyTable *plan, ExplainState *es)
 
static void ExplainTargetRel (Plan *plan, Index rti, ExplainState *es)
 
static void show_modifytable_info (ModifyTableState *mtstate, List *ancestors, ExplainState *es)
 
static void ExplainMemberNodes (PlanState **planstates, int nplans, List *ancestors, ExplainState *es)
 
static void ExplainMissingMembers (int nplans, int nchildren, ExplainState *es)
 
static void ExplainSubPlans (List *plans, List *ancestors, const char *relationship, ExplainState *es)
 
static void ExplainCustomChildren (CustomScanState *css, List *ancestors, ExplainState *es)
 
static ExplainWorkersStateExplainCreateWorkersState (int num_workers)
 
static void ExplainOpenWorker (int n, ExplainState *es)
 
static void ExplainCloseWorker (int n, ExplainState *es)
 
static void ExplainFlushWorkersState (ExplainState *es)
 
static void ExplainProperty (const char *qlabel, const char *unit, const char *value, bool numeric, ExplainState *es)
 
static void ExplainOpenSetAsideGroup (const char *objtype, const char *labelname, bool labeled, int depth, ExplainState *es)
 
static void ExplainSaveGroup (ExplainState *es, int depth, int *state_save)
 
static void ExplainRestoreGroup (ExplainState *es, int depth, int *state_save)
 
static void ExplainDummyGroup (const char *objtype, const char *labelname, ExplainState *es)
 
static void ExplainXMLTag (const char *tagname, int flags, ExplainState *es)
 
static void ExplainIndentText (ExplainState *es)
 
static void ExplainJSONLineEnding (ExplainState *es)
 
static void ExplainYAMLLineStarting (ExplainState *es)
 
static void escape_yaml (StringInfo buf, const char *str)
 
void ExplainQuery (ParseState *pstate, ExplainStmt *stmt, ParamListInfo params, DestReceiver *dest)
 
ExplainStateNewExplainState (void)
 
TupleDesc ExplainResultDesc (ExplainStmt *stmt)
 
void ExplainOneUtility (Node *utilityStmt, IntoClause *into, ExplainState *es, const char *queryString, ParamListInfo params, QueryEnvironment *queryEnv)
 
void ExplainOnePlan (PlannedStmt *plannedstmt, IntoClause *into, ExplainState *es, const char *queryString, ParamListInfo params, QueryEnvironment *queryEnv, const instr_time *planduration, const BufferUsage *bufusage)
 
static void ExplainPrintSettings (ExplainState *es)
 
void ExplainPrintPlan (ExplainState *es, QueryDesc *queryDesc)
 
void ExplainPrintTriggers (ExplainState *es, QueryDesc *queryDesc)
 
void ExplainPrintJITSummary (ExplainState *es, QueryDesc *queryDesc)
 
void ExplainQueryText (ExplainState *es, QueryDesc *queryDesc)
 
static void show_incremental_sort_group_info (IncrementalSortGroupInfo *groupInfo, const char *groupLabel, bool indent, ExplainState *es)
 
void ExplainPropertyList (const char *qlabel, List *data, ExplainState *es)
 
void ExplainPropertyListNested (const char *qlabel, List *data, ExplainState *es)
 
void ExplainPropertyText (const char *qlabel, const char *value, ExplainState *es)
 
void ExplainPropertyInteger (const char *qlabel, const char *unit, int64 value, ExplainState *es)
 
void ExplainPropertyUInteger (const char *qlabel, const char *unit, uint64 value, ExplainState *es)
 
void ExplainPropertyFloat (const char *qlabel, const char *unit, double value, int ndigits, ExplainState *es)
 
void ExplainPropertyBool (const char *qlabel, bool value, ExplainState *es)
 
void ExplainOpenGroup (const char *objtype, const char *labelname, bool labeled, ExplainState *es)
 
void ExplainCloseGroup (const char *objtype, const char *labelname, bool labeled, ExplainState *es)
 
void ExplainBeginOutput (ExplainState *es)
 
void ExplainEndOutput (ExplainState *es)
 
void ExplainSeparatePlans (ExplainState *es)
 

Variables

ExplainOneQuery_hook_type ExplainOneQuery_hook = NULL
 
explain_get_index_name_hook_type explain_get_index_name_hook = NULL
 

Macro Definition Documentation

◆ X_CLOSE_IMMEDIATE

#define X_CLOSE_IMMEDIATE   2

Definition at line 54 of file explain.c.

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

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

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

5026 {
5027  escape_json(buf, str);
5028 }
void escape_json(StringInfo buf, const char *str)
Definition: json.c:1580
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 3486 of file explain.c.

3487 {
3488  const char *result;
3489 
3491  result = (*explain_get_index_name_hook) (indexId);
3492  else
3493  result = NULL;
3494  if (result == NULL)
3495  {
3496  /* default behavior: look it up in the catalogs */
3497  result = get_rel_name(indexId);
3498  if (result == NULL)
3499  elog(ERROR, "cache lookup failed for index %u", indexId);
3500  }
3501  return result;
3502 }
#define ERROR
Definition: elog.h:33
explain_get_index_name_hook_type explain_get_index_name_hook
Definition: explain.c:48
char * get_rel_name(Oid relid)
Definition: lsyscache.c:1909

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

4850 {
4851  switch (es->format)
4852  {
4853  case EXPLAIN_FORMAT_TEXT:
4854  /* nothing to do */
4855  break;
4856 
4857  case EXPLAIN_FORMAT_XML:
4859  "<explain xmlns=\"http://www.postgresql.org/2009/explain\">\n");
4860  es->indent++;
4861  break;
4862 
4863  case EXPLAIN_FORMAT_JSON:
4864  /* top-level structure is an array of plans */
4865  appendStringInfoChar(es->str, '[');
4866  es->grouping_stack = lcons_int(0, es->grouping_stack);
4867  es->indent++;
4868  break;
4869 
4870  case EXPLAIN_FORMAT_YAML:
4871  es->grouping_stack = lcons_int(0, es->grouping_stack);
4872  break;
4873  }
4874 }
@ 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:52
StringInfo str
Definition: explain.h:39
ExplainFormat format
Definition: explain.h:49
int indent
Definition: explain.h:51

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

4658 {
4659  switch (es->format)
4660  {
4661  case EXPLAIN_FORMAT_TEXT:
4662  /* nothing to do */
4663  break;
4664 
4665  case EXPLAIN_FORMAT_XML:
4666  es->indent--;
4667  ExplainXMLTag(objtype, X_CLOSING, es);
4668  break;
4669 
4670  case EXPLAIN_FORMAT_JSON:
4671  es->indent--;
4672  appendStringInfoChar(es->str, '\n');
4673  appendStringInfoSpaces(es->str, 2 * es->indent);
4674  appendStringInfoChar(es->str, labeled ? '}' : ']');
4676  break;
4677 
4678  case EXPLAIN_FORMAT_YAML:
4679  es->indent--;
4681  break;
4682  }
4683 }
static void ExplainXMLTag(const char *tagname, int flags, ExplainState *es)
Definition: explain.c:4938
#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 4286 of file explain.c.

4287 {
4288  ExplainWorkersState *wstate = es->workers_state;
4289 
4290  Assert(wstate);
4291  Assert(n >= 0 && n < wstate->num_workers);
4292  Assert(wstate->worker_inited[n]);
4293 
4294  /*
4295  * Save formatting state in case we do another ExplainOpenWorker(), then
4296  * pop the formatting stack.
4297  */
4298  ExplainSaveGroup(es, 2, &wstate->worker_state_save[n]);
4299 
4300  /*
4301  * In TEXT format, if we didn't actually produce any output line(s) then
4302  * truncate off the partial line emitted by ExplainOpenWorker. (This is
4303  * to avoid bogus output if, say, show_buffer_usage chooses not to print
4304  * anything for the worker.) Also fix up the indent level.
4305  */
4306  if (es->format == EXPLAIN_FORMAT_TEXT)
4307  {
4308  while (es->str->len > 0 && es->str->data[es->str->len - 1] != '\n')
4309  es->str->data[--(es->str->len)] = '\0';
4310 
4311  es->indent--;
4312  }
4313 
4314  /* Restore prior output buffer pointer */
4315  es->str = wstate->prev_str;
4316 }
static void ExplainSaveGroup(ExplainState *es, int depth, int *state_save)
Definition: explain.c:4742
Assert(fmt[strlen(fmt) - 1] !='\n')
ExplainWorkersState * workers_state
Definition: explain.h:61
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 4207 of file explain.c.

4208 {
4209  ExplainWorkersState *wstate;
4210 
4211  wstate = (ExplainWorkersState *) palloc(sizeof(ExplainWorkersState));
4212  wstate->num_workers = num_workers;
4213  wstate->worker_inited = (bool *) palloc0(num_workers * sizeof(bool));
4214  wstate->worker_str = (StringInfoData *)
4215  palloc0(num_workers * sizeof(StringInfoData));
4216  wstate->worker_state_save = (int *) palloc(num_workers * sizeof(int));
4217  return wstate;
4218 }
void * palloc0(Size size)
Definition: mcxt.c:1099
void * palloc(Size size)
Definition: mcxt.c:1068
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 4185 of file explain.c.

4186 {
4187  ListCell *cell;
4188  const char *label =
4189  (list_length(css->custom_ps) != 1 ? "children" : "child");
4190 
4191  foreach(cell, css->custom_ps)
4192  ExplainNode((PlanState *) lfirst(cell), ancestors, label, NULL, es);
4193 }
static void ExplainNode(PlanState *planstate, List *ancestors, const char *relationship, const char *plan_name, ExplainState *es)
Definition: explain.c:1145
static char * label
#define lfirst(lc)
Definition: pg_list.h:170
static int list_length(const List *l)
Definition: pg_list.h:150
List * custom_ps
Definition: execnodes.h:1932

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

4804 {
4805  switch (es->format)
4806  {
4807  case EXPLAIN_FORMAT_TEXT:
4808  /* nothing to do */
4809  break;
4810 
4811  case EXPLAIN_FORMAT_XML:
4812  ExplainXMLTag(objtype, X_CLOSE_IMMEDIATE, es);
4813  break;
4814 
4815  case EXPLAIN_FORMAT_JSON:
4817  appendStringInfoSpaces(es->str, 2 * es->indent);
4818  if (labelname)
4819  {
4820  escape_json(es->str, labelname);
4821  appendStringInfoString(es->str, ": ");
4822  }
4823  escape_json(es->str, objtype);
4824  break;
4825 
4826  case EXPLAIN_FORMAT_YAML:
4828  if (labelname)
4829  {
4830  escape_yaml(es->str, labelname);
4831  appendStringInfoString(es->str, ": ");
4832  }
4833  else
4834  {
4835  appendStringInfoString(es->str, "- ");
4836  }
4837  escape_yaml(es->str, objtype);
4838  break;
4839  }
4840 }
#define X_CLOSE_IMMEDIATE
Definition: explain.c:54
static void ExplainYAMLLineStarting(ExplainState *es)
Definition: explain.c:5000
static void ExplainJSONLineEnding(ExplainState *es)
Definition: explain.c:4980
static void escape_yaml(StringInfo buf, const char *str)
Definition: explain.c:5025

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

4881 {
4882  switch (es->format)
4883  {
4884  case EXPLAIN_FORMAT_TEXT:
4885  /* nothing to do */
4886  break;
4887 
4888  case EXPLAIN_FORMAT_XML:
4889  es->indent--;
4890  appendStringInfoString(es->str, "</explain>");
4891  break;
4892 
4893  case EXPLAIN_FORMAT_JSON:
4894  es->indent--;
4895  appendStringInfoString(es->str, "\n]");
4897  break;
4898 
4899  case EXPLAIN_FORMAT_YAML:
4901  break;
4902  }
4903 }

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

4323 {
4324  ExplainWorkersState *wstate = es->workers_state;
4325 
4326  ExplainOpenGroup("Workers", "Workers", false, es);
4327  for (int i = 0; i < wstate->num_workers; i++)
4328  {
4329  if (wstate->worker_inited[i])
4330  {
4331  /* This must match previous ExplainOpenSetAsideGroup call */
4332  ExplainOpenGroup("Worker", NULL, true, es);
4333  appendStringInfoString(es->str, wstate->worker_str[i].data);
4334  ExplainCloseGroup("Worker", NULL, true, es);
4335 
4336  pfree(wstate->worker_str[i].data);
4337  }
4338  }
4339  ExplainCloseGroup("Workers", "Workers", false, es);
4340 
4341  pfree(wstate->worker_inited);
4342  pfree(wstate->worker_str);
4343  pfree(wstate->worker_state_save);
4344  pfree(wstate);
4345 }
void ExplainOpenGroup(const char *objtype, const char *labelname, bool labeled, ExplainState *es)
Definition: explain.c:4593
void ExplainCloseGroup(const char *objtype, const char *labelname, bool labeled, ExplainState *es)
Definition: explain.c:4656
int i
Definition: isn.c:73
void pfree(void *pointer)
Definition: mcxt.c:1175

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

3710 {
3711  const char *indexname = explain_get_index_name(indexid);
3712 
3713  if (es->format == EXPLAIN_FORMAT_TEXT)
3714  {
3715  if (ScanDirectionIsBackward(indexorderdir))
3716  appendStringInfoString(es->str, " Backward");
3717  appendStringInfo(es->str, " using %s", quote_identifier(indexname));
3718  }
3719  else
3720  {
3721  const char *scandir;
3722 
3723  switch (indexorderdir)
3724  {
3725  case BackwardScanDirection:
3726  scandir = "Backward";
3727  break;
3729  scandir = "NoMovement";
3730  break;
3731  case ForwardScanDirection:
3732  scandir = "Forward";
3733  break;
3734  default:
3735  scandir = "???";
3736  break;
3737  }
3738  ExplainPropertyText("Scan Direction", scandir, es);
3739  ExplainPropertyText("Index Name", indexname, es);
3740  }
3741 }
void ExplainPropertyText(const char *qlabel, const char *value, ExplainState *es)
Definition: explain.c:4528
static const char * explain_get_index_name(Oid indexId)
Definition: explain.c:3486
const char * quote_identifier(const char *ident)
Definition: ruleutils.c:12189
#define ScanDirectionIsBackward(direction)
Definition: sdir.h:41
@ NoMovementScanDirection
Definition: sdir.h:25
@ BackwardScanDirection
Definition: sdir.h:24
@ ForwardScanDirection
Definition: sdir.h:26
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, NoMovementScanDirection, 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 4110 of file explain.c.

4112 {
4113  int j;
4114 
4115  for (j = 0; j < nplans; j++)
4116  ExplainNode(planstates[j], ancestors,
4117  "Member", NULL, es);
4118 }
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 4128 of file explain.c.

4129 {
4130  if (nplans < nchildren || es->format != EXPLAIN_FORMAT_TEXT)
4131  ExplainPropertyInteger("Subplans Removed", NULL,
4132  nchildren - nplans, es);
4133 }
void ExplainPropertyInteger(const char *qlabel, const char *unit, int64 value, ExplainState *es)
Definition: explain.c:4537
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 3760 of file explain.c.

3761 {
3762  ExplainTargetRel((Plan *) plan, plan->nominalRelation, es);
3763 }
static void ExplainTargetRel(Plan *plan, Index rti, ExplainState *es)
Definition: explain.c:3769

References ExplainTargetRel(), and ModifyTable::nominalRelation.

Referenced by ExplainNode().

◆ ExplainNode()

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

Definition at line 1145 of file explain.c.

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

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

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

522 {
524  QueryDesc *queryDesc;
525  instr_time starttime;
526  double totaltime = 0;
527  int eflags;
528  int instrument_option = 0;
529 
530  Assert(plannedstmt->commandType != CMD_UTILITY);
531 
532  if (es->analyze && es->timing)
533  instrument_option |= INSTRUMENT_TIMER;
534  else if (es->analyze)
535  instrument_option |= INSTRUMENT_ROWS;
536 
537  if (es->buffers)
538  instrument_option |= INSTRUMENT_BUFFERS;
539  if (es->wal)
540  instrument_option |= INSTRUMENT_WAL;
541 
542  /*
543  * We always collect timing for the entire statement, even when node-level
544  * timing is off, so we don't look at es->timing here. (We could skip
545  * this if !es->summary, but it's hardly worth the complication.)
546  */
547  INSTR_TIME_SET_CURRENT(starttime);
548 
549  /*
550  * Use a snapshot with an updated command ID to ensure this query sees
551  * results of any previously executed queries.
552  */
555 
556  /*
557  * Normally we discard the query's output, but if explaining CREATE TABLE
558  * AS, we'd better use the appropriate tuple receiver.
559  */
560  if (into)
562  else
564 
565  /* Create a QueryDesc for the query */
566  queryDesc = CreateQueryDesc(plannedstmt, queryString,
568  dest, params, queryEnv, instrument_option);
569 
570  /* Select execution options */
571  if (es->analyze)
572  eflags = 0; /* default run-to-completion flags */
573  else
574  eflags = EXEC_FLAG_EXPLAIN_ONLY;
575  if (into)
576  eflags |= GetIntoRelEFlags(into);
577 
578  /* call ExecutorStart to prepare the plan for execution */
579  ExecutorStart(queryDesc, eflags);
580 
581  /* Execute the plan for statistics if asked for */
582  if (es->analyze)
583  {
584  ScanDirection dir;
585 
586  /* EXPLAIN ANALYZE CREATE TABLE AS WITH NO DATA is weird */
587  if (into && into->skipData)
589  else
590  dir = ForwardScanDirection;
591 
592  /* run the plan */
593  ExecutorRun(queryDesc, dir, 0L, true);
594 
595  /* run cleanup too */
596  ExecutorFinish(queryDesc);
597 
598  /* We can't run ExecutorEnd 'till we're done printing the stats... */
599  totaltime += elapsed_time(&starttime);
600  }
601 
602  ExplainOpenGroup("Query", NULL, true, es);
603 
604  /* Create textual dump of plan tree */
605  ExplainPrintPlan(es, queryDesc);
606 
607  /*
608  * COMPUTE_QUERY_ID_REGRESS means COMPUTE_QUERY_ID_AUTO, but we don't show
609  * the queryid in any of the EXPLAIN plans to keep stable the results
610  * generated by regression test suites.
611  */
612  if (es->verbose && plannedstmt->queryId != UINT64CONST(0) &&
614  {
615  /*
616  * Output the queryid as an int64 rather than a uint64 so we match
617  * what would be seen in the BIGINT pg_stat_statements.queryid column.
618  */
619  ExplainPropertyInteger("Query Identifier", NULL, (int64)
620  plannedstmt->queryId, es);
621  }
622 
623  /* Show buffer usage in planning */
624  if (bufusage)
625  {
626  ExplainOpenGroup("Planning", "Planning", true, es);
627  show_buffer_usage(es, bufusage, true);
628  ExplainCloseGroup("Planning", "Planning", true, es);
629  }
630 
631  if (es->summary && planduration)
632  {
633  double plantime = INSTR_TIME_GET_DOUBLE(*planduration);
634 
635  ExplainPropertyFloat("Planning Time", "ms", 1000.0 * plantime, 3, es);
636  }
637 
638  /* Print info about runtime of triggers */
639  if (es->analyze)
640  ExplainPrintTriggers(es, queryDesc);
641 
642  /*
643  * Print info about JITing. Tied to es->costs because we don't want to
644  * display this in regression tests, as it'd cause output differences
645  * depending on build options. Might want to separate that out from COSTS
646  * at a later stage.
647  */
648  if (es->costs)
649  ExplainPrintJITSummary(es, queryDesc);
650 
651  /*
652  * Close down the query and free resources. Include time for this in the
653  * total execution time (although it should be pretty minimal).
654  */
655  INSTR_TIME_SET_CURRENT(starttime);
656 
657  ExecutorEnd(queryDesc);
658 
659  FreeQueryDesc(queryDesc);
660 
662 
663  /* We need a CCI just in case query expanded to multiple plans */
664  if (es->analyze)
666 
667  totaltime += elapsed_time(&starttime);
668 
669  /*
670  * We only report execution time if we actually ran the query (that is,
671  * the user specified ANALYZE), and if summary reporting is enabled (the
672  * user can set SUMMARY OFF to not have the timing information included in
673  * the output). By default, ANALYZE sets SUMMARY to true.
674  */
675  if (es->summary && es->analyze)
676  ExplainPropertyFloat("Execution Time", "ms", 1000.0 * totaltime, 3,
677  es);
678 
679  ExplainCloseGroup("Query", NULL, true, es);
680 }
DestReceiver * CreateIntoRelDestReceiver(IntoClause *intoClause)
Definition: createas.c:428
int GetIntoRelEFlags(IntoClause *intoClause)
Definition: createas.c:375
DestReceiver * None_Receiver
Definition: dest.c:96
void ExecutorEnd(QueryDesc *queryDesc)
Definition: execMain.c:461
void ExecutorFinish(QueryDesc *queryDesc)
Definition: execMain.c:401
void ExecutorStart(QueryDesc *queryDesc, int eflags)
Definition: execMain.c:131
void ExecutorRun(QueryDesc *queryDesc, ScanDirection direction, uint64 count, bool execute_once)
Definition: execMain.c:300
#define EXEC_FLAG_EXPLAIN_ONLY
Definition: executor.h:56
void ExplainPrintJITSummary(ExplainState *es, QueryDesc *queryDesc)
Definition: explain.c:849
static double elapsed_time(instr_time *starttime)
Definition: explain.c:1051
void ExplainPrintPlan(ExplainState *es, QueryDesc *queryDesc)
Definition: explain.c:757
void ExplainPrintTriggers(ExplainState *es, QueryDesc *queryDesc)
Definition: explain.c:806
@ 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:727
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
int compute_query_id
Definition: queryjumble.c:43
@ COMPUTE_QUERY_ID_REGRESS
Definition: queryjumble.h:61
ScanDirection
Definition: sdir.h:23
void UpdateActiveSnapshotCommandId(void)
Definition: snapmgr.c:745
void PopActiveSnapshot(void)
Definition: snapmgr.c:776
void PushCopiedSnapshot(Snapshot snapshot)
Definition: snapmgr.c:733
Snapshot GetActiveSnapshot(void)
Definition: snapmgr.c:803
#define InvalidSnapshot
Definition: snapshot.h:123
bool summary
Definition: explain.h:47
bool skipData
Definition: primnodes.h:141
CmdType commandType
Definition: plannodes.h:47
uint64 queryId
Definition: plannodes.h:49
void CommandCounterIncrement(void)
Definition: xact.c:1074

References ExplainState::analyze, Assert(), ExplainState::buffers, CMD_UTILITY, CommandCounterIncrement(), PlannedStmt::commandType, compute_query_id, COMPUTE_QUERY_ID_REGRESS, ExplainState::costs, CreateIntoRelDestReceiver(), CreateQueryDesc(), generate_unaccent_rules::dest, elapsed_time(), EXEC_FLAG_EXPLAIN_ONLY, ExecutorEnd(), ExecutorFinish(), ExecutorRun(), ExecutorStart(), ExplainCloseGroup(), ExplainOpenGroup(), ExplainPrintJITSummary(), ExplainPrintPlan(), ExplainPrintTriggers(), ExplainPropertyFloat(), ExplainPropertyInteger(), ForwardScanDirection, FreeQueryDesc(), GetActiveSnapshot(), GetIntoRelEFlags(), INSTR_TIME_GET_DOUBLE, INSTR_TIME_SET_CURRENT, INSTRUMENT_BUFFERS, INSTRUMENT_ROWS, INSTRUMENT_TIMER, INSTRUMENT_WAL, InvalidSnapshot, NoMovementScanDirection, None_Receiver, PopActiveSnapshot(), PushCopiedSnapshot(), PlannedStmt::queryId, show_buffer_usage(), IntoClause::skipData, ExplainState::summary, ExplainState::timing, UpdateActiveSnapshotCommandId(), ExplainState::verbose, 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 367 of file explain.c.

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

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

Referenced by ExplainOneUtility(), and ExplainQuery().

◆ ExplainOneUtility()

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

Definition at line 428 of file explain.c.

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

4595 {
4596  switch (es->format)
4597  {
4598  case EXPLAIN_FORMAT_TEXT:
4599  /* nothing to do */
4600  break;
4601 
4602  case EXPLAIN_FORMAT_XML:
4603  ExplainXMLTag(objtype, X_OPENING, es);
4604  es->indent++;
4605  break;
4606 
4607  case EXPLAIN_FORMAT_JSON:
4609  appendStringInfoSpaces(es->str, 2 * es->indent);
4610  if (labelname)
4611  {
4612  escape_json(es->str, labelname);
4613  appendStringInfoString(es->str, ": ");
4614  }
4615  appendStringInfoChar(es->str, labeled ? '{' : '[');
4616 
4617  /*
4618  * In JSON format, the grouping_stack is an integer list. 0 means
4619  * we've emitted nothing at this grouping level, 1 means we've
4620  * emitted something (and so the next item needs a comma). See
4621  * ExplainJSONLineEnding().
4622  */
4623  es->grouping_stack = lcons_int(0, es->grouping_stack);
4624  es->indent++;
4625  break;
4626 
4627  case EXPLAIN_FORMAT_YAML:
4628 
4629  /*
4630  * In YAML format, the grouping stack is an integer list. 0 means
4631  * we've emitted nothing at this grouping level AND this grouping
4632  * level is unlabeled and must be marked with "- ". See
4633  * ExplainYAMLLineStarting().
4634  */
4636  if (labelname)
4637  {
4638  appendStringInfo(es->str, "%s: ", labelname);
4639  es->grouping_stack = lcons_int(1, es->grouping_stack);
4640  }
4641  else
4642  {
4643  appendStringInfoString(es->str, "- ");
4644  es->grouping_stack = lcons_int(0, es->grouping_stack);
4645  }
4646  es->indent++;
4647  break;
4648  }
4649 }
#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 4703 of file explain.c.

4705 {
4706  switch (es->format)
4707  {
4708  case EXPLAIN_FORMAT_TEXT:
4709  /* nothing to do */
4710  break;
4711 
4712  case EXPLAIN_FORMAT_XML:
4713  es->indent += depth;
4714  break;
4715 
4716  case EXPLAIN_FORMAT_JSON:
4717  es->grouping_stack = lcons_int(0, es->grouping_stack);
4718  es->indent += depth;
4719  break;
4720 
4721  case EXPLAIN_FORMAT_YAML:
4722  if (labelname)
4723  es->grouping_stack = lcons_int(1, es->grouping_stack);
4724  else
4725  es->grouping_stack = lcons_int(0, es->grouping_stack);
4726  es->indent += depth;
4727  break;
4728  }
4729 }

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

4225 {
4226  ExplainWorkersState *wstate = es->workers_state;
4227 
4228  Assert(wstate);
4229  Assert(n >= 0 && n < wstate->num_workers);
4230 
4231  /* Save prior output buffer pointer */
4232  wstate->prev_str = es->str;
4233 
4234  if (!wstate->worker_inited[n])
4235  {
4236  /* First time through, so create the buffer for this worker */
4237  initStringInfo(&wstate->worker_str[n]);
4238  es->str = &wstate->worker_str[n];
4239 
4240  /*
4241  * Push suitable initial formatting state for this worker's field
4242  * group. We allow one extra logical nesting level, since this group
4243  * will eventually be wrapped in an outer "Workers" group.
4244  */
4245  ExplainOpenSetAsideGroup("Worker", NULL, true, 2, es);
4246 
4247  /*
4248  * In non-TEXT formats we always emit a "Worker Number" field, even if
4249  * there's no other data for this worker.
4250  */
4251  if (es->format != EXPLAIN_FORMAT_TEXT)
4252  ExplainPropertyInteger("Worker Number", NULL, n, es);
4253 
4254  wstate->worker_inited[n] = true;
4255  }
4256  else
4257  {
4258  /* Resuming output for a worker we've already emitted some data for */
4259  es->str = &wstate->worker_str[n];
4260 
4261  /* Restore formatting state saved by last ExplainCloseWorker() */
4262  ExplainRestoreGroup(es, 2, &wstate->worker_state_save[n]);
4263  }
4264 
4265  /*
4266  * In TEXT format, prefix the first output line for this worker with
4267  * "Worker N:". Then, any additional lines should be indented one more
4268  * stop than the "Worker N" line is.
4269  */
4270  if (es->format == EXPLAIN_FORMAT_TEXT)
4271  {
4272  if (es->str->len == 0)
4273  {
4274  ExplainIndentText(es);
4275  appendStringInfo(es->str, "Worker %d: ", n);
4276  }
4277 
4278  es->indent++;
4279  }
4280 }
static void ExplainRestoreGroup(ExplainState *es, int depth, int *state_save)
Definition: explain.c:4772
static void ExplainOpenSetAsideGroup(const char *objtype, const char *labelname, bool labeled, int depth, ExplainState *es)
Definition: explain.c:4703
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 1070 of file explain.c.

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

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

Referenced by ExplainPrintPlan().

◆ ExplainPrintJIT()

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

Definition at line 875 of file explain.c.

876 {
877  instr_time total_time;
878 
879  /* don't print information if no JITing happened */
880  if (!ji || ji->created_functions == 0)
881  return;
882 
883  /* calculate total time */
884  INSTR_TIME_SET_ZERO(total_time);
885  INSTR_TIME_ADD(total_time, ji->generation_counter);
886  INSTR_TIME_ADD(total_time, ji->inlining_counter);
887  INSTR_TIME_ADD(total_time, ji->optimization_counter);
888  INSTR_TIME_ADD(total_time, ji->emission_counter);
889 
890  ExplainOpenGroup("JIT", "JIT", true, es);
891 
892  /* for higher density, open code the text output format */
893  if (es->format == EXPLAIN_FORMAT_TEXT)
894  {
895  ExplainIndentText(es);
896  appendStringInfoString(es->str, "JIT:\n");
897  es->indent++;
898 
899  ExplainPropertyInteger("Functions", NULL, ji->created_functions, es);
900 
901  ExplainIndentText(es);
902  appendStringInfo(es->str, "Options: %s %s, %s %s, %s %s, %s %s\n",
903  "Inlining", jit_flags & PGJIT_INLINE ? "true" : "false",
904  "Optimization", jit_flags & PGJIT_OPT3 ? "true" : "false",
905  "Expressions", jit_flags & PGJIT_EXPR ? "true" : "false",
906  "Deforming", jit_flags & PGJIT_DEFORM ? "true" : "false");
907 
908  if (es->analyze && es->timing)
909  {
910  ExplainIndentText(es);
911  appendStringInfo(es->str,
912  "Timing: %s %.3f ms, %s %.3f ms, %s %.3f ms, %s %.3f ms, %s %.3f ms\n",
913  "Generation", 1000.0 * INSTR_TIME_GET_DOUBLE(ji->generation_counter),
914  "Inlining", 1000.0 * INSTR_TIME_GET_DOUBLE(ji->inlining_counter),
915  "Optimization", 1000.0 * INSTR_TIME_GET_DOUBLE(ji->optimization_counter),
916  "Emission", 1000.0 * INSTR_TIME_GET_DOUBLE(ji->emission_counter),
917  "Total", 1000.0 * INSTR_TIME_GET_DOUBLE(total_time));
918  }
919 
920  es->indent--;
921  }
922  else
923  {
924  ExplainPropertyInteger("Functions", NULL, ji->created_functions, es);
925 
926  ExplainOpenGroup("Options", "Options", true, es);
927  ExplainPropertyBool("Inlining", jit_flags & PGJIT_INLINE, es);
928  ExplainPropertyBool("Optimization", jit_flags & PGJIT_OPT3, es);
929  ExplainPropertyBool("Expressions", jit_flags & PGJIT_EXPR, es);
930  ExplainPropertyBool("Deforming", jit_flags & PGJIT_DEFORM, es);
931  ExplainCloseGroup("Options", "Options", true, es);
932 
933  if (es->analyze && es->timing)
934  {
935  ExplainOpenGroup("Timing", "Timing", true, es);
936 
937  ExplainPropertyFloat("Generation", "ms",
939  3, es);
940  ExplainPropertyFloat("Inlining", "ms",
942  3, es);
943  ExplainPropertyFloat("Optimization", "ms",
945  3, es);
946  ExplainPropertyFloat("Emission", "ms",
948  3, es);
949  ExplainPropertyFloat("Total", "ms",
950  1000.0 * INSTR_TIME_GET_DOUBLE(total_time),
951  3, es);
952 
953  ExplainCloseGroup("Timing", "Timing", true, es);
954  }
955  }
956 
957  ExplainCloseGroup("JIT", "JIT", true, es);
958 }
#define INSTR_TIME_ADD(x, y)
Definition: instr_time.h:158
#define INSTR_TIME_SET_ZERO(t)
Definition: instr_time.h:154
#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:39
instr_time emission_counter
Definition: jit.h:42
instr_time inlining_counter
Definition: jit.h:36

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

Referenced by ExplainNode(), and ExplainPrintJITSummary().

◆ ExplainPrintJITSummary()

void ExplainPrintJITSummary ( ExplainState es,
QueryDesc queryDesc 
)

Definition at line 849 of file explain.c.

850 {
851  JitInstrumentation ji = {0};
852 
853  if (!(queryDesc->estate->es_jit_flags & PGJIT_PERFORM))
854  return;
855 
856  /*
857  * Work with a copy instead of modifying the leader state, since this
858  * function may be called twice
859  */
860  if (queryDesc->estate->es_jit)
861  InstrJitAgg(&ji, &queryDesc->estate->es_jit->instr);
862 
863  /* If this process has done JIT in parallel workers, merge stats */
864  if (queryDesc->estate->es_jit_worker_instr)
865  InstrJitAgg(&ji, queryDesc->estate->es_jit_worker_instr);
866 
867  ExplainPrintJIT(es, queryDesc->estate->es_jit_flags, &ji);
868 }
void InstrJitAgg(JitInstrumentation *dst, JitInstrumentation *add)
Definition: jit.c:184
#define PGJIT_PERFORM
Definition: jit.h:20
struct JitContext * es_jit
Definition: execnodes.h:678
struct JitInstrumentation * es_jit_worker_instr
Definition: execnodes.h:679
JitInstrumentation instr
Definition: jit.h:61
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 757 of file explain.c.

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

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

Referenced by explain_ExecutorEnd(), and ExplainOnePlan().

◆ ExplainPrintSettings()

static void ExplainPrintSettings ( ExplainState es)
static

Definition at line 687 of file explain.c.

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

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

807 {
808  ResultRelInfo *rInfo;
809  bool show_relname;
810  List *resultrels;
811  List *routerels;
812  List *targrels;
813  ListCell *l;
814 
815  resultrels = queryDesc->estate->es_opened_result_relations;
816  routerels = queryDesc->estate->es_tuple_routing_result_relations;
817  targrels = queryDesc->estate->es_trig_target_relations;
818 
819  ExplainOpenGroup("Triggers", "Triggers", false, es);
820 
821  show_relname = (list_length(resultrels) > 1 ||
822  routerels != NIL || targrels != NIL);
823  foreach(l, resultrels)
824  {
825  rInfo = (ResultRelInfo *) lfirst(l);
826  report_triggers(rInfo, show_relname, es);
827  }
828 
829  foreach(l, routerels)
830  {
831  rInfo = (ResultRelInfo *) lfirst(l);
832  report_triggers(rInfo, show_relname, es);
833  }
834 
835  foreach(l, targrels)
836  {
837  rInfo = (ResultRelInfo *) lfirst(l);
838  report_triggers(rInfo, show_relname, es);
839  }
840 
841  ExplainCloseGroup("Triggers", "Triggers", false, es);
842 }
static void report_triggers(ResultRelInfo *rInfo, bool show_relname, ExplainState *es)
Definition: explain.c:980
List * es_tuple_routing_result_relations
Definition: execnodes.h:620
List * es_trig_target_relations
Definition: execnodes.h:623
List * es_opened_result_relations
Definition: execnodes.h:610

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

4477 {
4478  switch (es->format)
4479  {
4480  case EXPLAIN_FORMAT_TEXT:
4481  ExplainIndentText(es);
4482  if (unit)
4483  appendStringInfo(es->str, "%s: %s %s\n", qlabel, value, unit);
4484  else
4485  appendStringInfo(es->str, "%s: %s\n", qlabel, value);
4486  break;
4487 
4488  case EXPLAIN_FORMAT_XML:
4489  {
4490  char *str;
4491 
4492  appendStringInfoSpaces(es->str, es->indent * 2);
4493  ExplainXMLTag(qlabel, X_OPENING | X_NOWHITESPACE, es);
4494  str = escape_xml(value);
4496  pfree(str);
4497  ExplainXMLTag(qlabel, X_CLOSING | X_NOWHITESPACE, es);
4498  appendStringInfoChar(es->str, '\n');
4499  }
4500  break;
4501 
4502  case EXPLAIN_FORMAT_JSON:
4504  appendStringInfoSpaces(es->str, es->indent * 2);
4505  escape_json(es->str, qlabel);
4506  appendStringInfoString(es->str, ": ");
4507  if (numeric)
4509  else
4510  escape_json(es->str, value);
4511  break;
4512 
4513  case EXPLAIN_FORMAT_YAML:
4515  appendStringInfo(es->str, "%s: ", qlabel);
4516  if (numeric)
4518  else
4519  escape_yaml(es->str, value);
4520  break;
4521  }
4522 }
#define X_NOWHITESPACE
Definition: explain.c:55
static struct @151 value
char * escape_xml(const char *str)
Definition: xml.c:2352

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

4579 {
4580  ExplainProperty(qlabel, NULL, value ? "true" : "false", true, es);
4581 }
static void ExplainProperty(const char *qlabel, const char *unit, const char *value, bool numeric, ExplainState *es)
Definition: explain.c:4475

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

4566 {
4567  char *buf;
4568 
4569  buf = psprintf("%.*f", ndigits, value);
4570  ExplainProperty(qlabel, unit, buf, true, es);
4571  pfree(buf);
4572 }

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

4353 {
4354  ListCell *lc;
4355  bool first = true;
4356 
4357  switch (es->format)
4358  {
4359  case EXPLAIN_FORMAT_TEXT:
4360  ExplainIndentText(es);
4361  appendStringInfo(es->str, "%s: ", qlabel);
4362  foreach(lc, data)
4363  {
4364  if (!first)
4365  appendStringInfoString(es->str, ", ");
4366  appendStringInfoString(es->str, (const char *) lfirst(lc));
4367  first = false;
4368  }
4369  appendStringInfoChar(es->str, '\n');
4370  break;
4371 
4372  case EXPLAIN_FORMAT_XML:
4373  ExplainXMLTag(qlabel, X_OPENING, es);
4374  foreach(lc, data)
4375  {
4376  char *str;
4377 
4378  appendStringInfoSpaces(es->str, es->indent * 2 + 2);
4379  appendStringInfoString(es->str, "<Item>");
4380  str = escape_xml((const char *) lfirst(lc));
4382  pfree(str);
4383  appendStringInfoString(es->str, "</Item>\n");
4384  }
4385  ExplainXMLTag(qlabel, X_CLOSING, es);
4386  break;
4387 
4388  case EXPLAIN_FORMAT_JSON:
4390  appendStringInfoSpaces(es->str, es->indent * 2);
4391  escape_json(es->str, qlabel);
4392  appendStringInfoString(es->str, ": [");
4393  foreach(lc, data)
4394  {
4395  if (!first)
4396  appendStringInfoString(es->str, ", ");
4397  escape_json(es->str, (const char *) lfirst(lc));
4398  first = false;
4399  }
4400  appendStringInfoChar(es->str, ']');
4401  break;
4402 
4403  case EXPLAIN_FORMAT_YAML:
4405  appendStringInfo(es->str, "%s: ", qlabel);
4406  foreach(lc, data)
4407  {
4408  appendStringInfoChar(es->str, '\n');
4409  appendStringInfoSpaces(es->str, es->indent * 2 + 2);
4410  appendStringInfoString(es->str, "- ");
4411  escape_yaml(es->str, (const char *) lfirst(lc));
4412  }
4413  break;
4414  }
4415 }
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 4422 of file explain.c.

4423 {
4424  ListCell *lc;
4425  bool first = true;
4426 
4427  switch (es->format)
4428  {
4429  case EXPLAIN_FORMAT_TEXT:
4430  case EXPLAIN_FORMAT_XML:
4431  ExplainPropertyList(qlabel, data, es);
4432  return;
4433 
4434  case EXPLAIN_FORMAT_JSON:
4436  appendStringInfoSpaces(es->str, es->indent * 2);
4437  appendStringInfoChar(es->str, '[');
4438  foreach(lc, data)
4439  {
4440  if (!first)
4441  appendStringInfoString(es->str, ", ");
4442  escape_json(es->str, (const char *) lfirst(lc));
4443  first = false;
4444  }
4445  appendStringInfoChar(es->str, ']');
4446  break;
4447 
4448  case EXPLAIN_FORMAT_YAML:
4450  appendStringInfoString(es->str, "- [");
4451  foreach(lc, data)
4452  {
4453  if (!first)
4454  appendStringInfoString(es->str, ", ");
4455  escape_yaml(es->str, (const char *) lfirst(lc));
4456  first = false;
4457  }
4458  appendStringInfoChar(es->str, ']');
4459  break;
4460  }
4461 }
void ExplainPropertyList(const char *qlabel, List *data, ExplainState *es)
Definition: explain.c:4352

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

4552 {
4553  char buf[32];
4554 
4555  snprintf(buf, sizeof(buf), UINT64_FORMAT, value);
4556  ExplainProperty(qlabel, unit, buf, true, es);
4557 }
#define UINT64_FORMAT
Definition: c.h:495

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, "timing") == 0)
194  {
195  timing_set = true;
196  es->timing = defGetBoolean(opt);
197  }
198  else if (strcmp(opt->defname, "summary") == 0)
199  {
200  summary_set = true;
201  es->summary = defGetBoolean(opt);
202  }
203  else if (strcmp(opt->defname, "format") == 0)
204  {
205  char *p = defGetString(opt);
206 
207  if (strcmp(p, "text") == 0)
209  else if (strcmp(p, "xml") == 0)
211  else if (strcmp(p, "json") == 0)
213  else if (strcmp(p, "yaml") == 0)
215  else
216  ereport(ERROR,
217  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
218  errmsg("unrecognized value for EXPLAIN option \"%s\": \"%s\"",
219  opt->defname, p),
220  parser_errposition(pstate, opt->location)));
221  }
222  else
223  ereport(ERROR,
224  (errcode(ERRCODE_SYNTAX_ERROR),
225  errmsg("unrecognized EXPLAIN option \"%s\"",
226  opt->defname),
227  parser_errposition(pstate, opt->location)));
228  }
229 
230  if (es->wal && !es->analyze)
231  ereport(ERROR,
232  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
233  errmsg("EXPLAIN option WAL requires ANALYZE")));
234 
235  /* if the timing was not set explicitly, set default value */
236  es->timing = (timing_set) ? es->timing : es->analyze;
237 
238  /* check that timing is used with EXPLAIN ANALYZE */
239  if (es->timing && !es->analyze)
240  ereport(ERROR,
241  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
242  errmsg("EXPLAIN option TIMING requires ANALYZE")));
243 
244  /* if the summary was not set explicitly, set default value */
245  es->summary = (summary_set) ? es->summary : es->analyze;
246 
247  query = castNode(Query, stmt->query);
248  if (IsQueryIdEnabled())
249  jstate = JumbleQuery(query, pstate->p_sourcetext);
250 
252  (*post_parse_analyze_hook) (pstate, query, jstate);
253 
254  /*
255  * Parse analysis was done already, but we still have to run the rule
256  * rewriter. We do not do AcquireRewriteLocks: we assume the query either
257  * came straight from the parser, or suitable locks were acquired by
258  * plancache.c.
259  */
260  rewritten = QueryRewrite(castNode(Query, stmt->query));
261 
262  /* emit opening boilerplate */
263  ExplainBeginOutput(es);
264 
265  if (rewritten == NIL)
266  {
267  /*
268  * In the case of an INSTEAD NOTHING, tell at least that. But in
269  * non-text format, the output is delimited, so this isn't necessary.
270  */
271  if (es->format == EXPLAIN_FORMAT_TEXT)
272  appendStringInfoString(es->str, "Query rewrites to nothing\n");
273  }
274  else
275  {
276  ListCell *l;
277 
278  /* Explain every plan */
279  foreach(l, rewritten)
280  {
282  CURSOR_OPT_PARALLEL_OK, NULL, es,
283  pstate->p_sourcetext, params, pstate->p_queryEnv);
284 
285  /* Separate plans with an appropriate separator */
286  if (lnext(rewritten, l) != NULL)
288  }
289  }
290 
291  /* emit closing boilerplate */
292  ExplainEndOutput(es);
293  Assert(es->indent == 0);
294 
295  /* output tuples */
297  &TTSOpsVirtual);
298  if (es->format == EXPLAIN_FORMAT_TEXT)
299  do_text_output_multiline(tstate, es->str->data);
300  else
301  do_text_output_oneline(tstate, es->str->data);
302  end_tup_output(tstate);
303 
304  pfree(es->str->data);
305 }
bool defGetBoolean(DefElem *def)
Definition: define.c:108
char * defGetString(DefElem *def)
Definition: define.c:49
int errcode(int sqlerrcode)
Definition: elog.c:693
int errmsg(const char *fmt,...)
Definition: elog.c:904
#define ereport(elevel,...)
Definition: elog.h:143
const TupleTableSlotOps TTSOpsVirtual
Definition: execTuples.c:83
void end_tup_output(TupOutputState *tstate)
Definition: execTuples.c:2333
void do_text_output_multiline(TupOutputState *tstate, const char *txt)
Definition: execTuples.c:2303
TupOutputState * begin_tup_output_tupdesc(DestReceiver *dest, TupleDesc tupdesc, const TupleTableSlotOps *tts_ops)
Definition: execTuples.c:2255
#define do_text_output_oneline(tstate, str_to_emit)
Definition: executor.h:509
void ExplainSeparatePlans(ExplainState *es)
Definition: explain.c:4909
void ExplainEndOutput(ExplainState *es)
Definition: explain.c:4880
TupleDesc ExplainResultDesc(ExplainStmt *stmt)
Definition: explain.c:328
void ExplainBeginOutput(ExplainState *es)
Definition: explain.c:4849
ExplainState * NewExplainState(void)
Definition: explain.c:311
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:110
post_parse_analyze_hook_type post_parse_analyze_hook
Definition: analyze.c:59
#define lfirst_node(type, lc)
Definition: pg_list.h:174
static ListCell * lnext(const List *l, const ListCell *c)
Definition: pg_list.h:341
JumbleState * JumbleQuery(Query *query, const char *querytext)
Definition: queryjumble.c:101
static bool IsQueryIdEnabled(void)
Definition: queryjumble.h:79
char * defname
Definition: parsenodes.h:766
int location
Definition: parsenodes.h:770
Node * query
Definition: parsenodes.h:3745
List * options
Definition: parsenodes.h:3746
const char * p_sourcetext
Definition: parse_node.h:182

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, if(), ExplainState::indent, IsQueryIdEnabled(), JumbleQuery(), lfirst, lfirst_node, lnext(), DefElem::location, NewExplainState(), NIL, ExplainStmt::options, ParseState::p_queryEnv, ParseState::p_sourcetext, parser_errposition(), pfree(), post_parse_analyze_hook, ExplainStmt::query, QueryRewrite(), ExplainState::settings, ExplainState::str, ExplainState::summary, ExplainState::timing, TTSOpsVirtual, ExplainState::verbose, and ExplainState::wal.

Referenced by standard_ProcessUtility().

◆ ExplainQueryText()

void ExplainQueryText ( ExplainState es,
QueryDesc queryDesc 
)

Definition at line 969 of file explain.c.

970 {
971  if (queryDesc->sourceText)
972  ExplainPropertyText("Query Text", queryDesc->sourceText, es);
973 }
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 4772 of file explain.c.

4773 {
4774  switch (es->format)
4775  {
4776  case EXPLAIN_FORMAT_TEXT:
4777  /* nothing to do */
4778  break;
4779 
4780  case EXPLAIN_FORMAT_XML:
4781  es->indent += depth;
4782  break;
4783 
4784  case EXPLAIN_FORMAT_JSON:
4785  es->grouping_stack = lcons_int(*state_save, es->grouping_stack);
4786  es->indent += depth;
4787  break;
4788 
4789  case EXPLAIN_FORMAT_YAML:
4790  es->grouping_stack = lcons_int(*state_save, es->grouping_stack);
4791  es->indent += depth;
4792  break;
4793  }
4794 }

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

329 {
330  TupleDesc tupdesc;
331  ListCell *lc;
332  Oid result_type = TEXTOID;
333 
334  /* Check for XML format option */
335  foreach(lc, stmt->options)
336  {
337  DefElem *opt = (DefElem *) lfirst(lc);
338 
339  if (strcmp(opt->defname, "format") == 0)
340  {
341  char *p = defGetString(opt);
342 
343  if (strcmp(p, "xml") == 0)
344  result_type = XMLOID;
345  else if (strcmp(p, "json") == 0)
346  result_type = JSONOID;
347  else
348  result_type = TEXTOID;
349  /* don't "break", as ExplainQuery will use the last value */
350  }
351  }
352 
353  /* Need a tuple descriptor representing a single TEXT or XML column */
354  tupdesc = CreateTemplateTupleDesc(1);
355  TupleDescInitEntry(tupdesc, (AttrNumber) 1, "QUERY PLAN",
356  result_type, -1, 0);
357  return tupdesc;
358 }
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, ExplainStmt::options, and TupleDescInitEntry().

Referenced by ExplainQuery(), and UtilityTupleDescriptor().

◆ ExplainSaveGroup()

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

Definition at line 4742 of file explain.c.

4743 {
4744  switch (es->format)
4745  {
4746  case EXPLAIN_FORMAT_TEXT:
4747  /* nothing to do */
4748  break;
4749 
4750  case EXPLAIN_FORMAT_XML:
4751  es->indent -= depth;
4752  break;
4753 
4754  case EXPLAIN_FORMAT_JSON:
4755  es->indent -= depth;
4756  *state_save = linitial_int(es->grouping_stack);
4758  break;
4759 
4760  case EXPLAIN_FORMAT_YAML:
4761  es->indent -= depth;
4762  *state_save = linitial_int(es->grouping_stack);
4764  break;
4765  }
4766 }

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

3748 {
3749  ExplainTargetRel((Plan *) plan, plan->scanrelid, es);
3750 }

References ExplainTargetRel(), and Scan::scanrelid.

Referenced by ExplainNode().

◆ ExplainSeparatePlans()

void ExplainSeparatePlans ( ExplainState es)

Definition at line 4909 of file explain.c.

4910 {
4911  switch (es->format)
4912  {
4913  case EXPLAIN_FORMAT_TEXT:
4914  /* add a blank line */
4915  appendStringInfoChar(es->str, '\n');
4916  break;
4917 
4918  case EXPLAIN_FORMAT_XML:
4919  case EXPLAIN_FORMAT_JSON:
4920  case EXPLAIN_FORMAT_YAML:
4921  /* nothing to do */
4922  break;
4923  }
4924 }

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

4144 {
4145  ListCell *lst;
4146 
4147  foreach(lst, plans)
4148  {
4149  SubPlanState *sps = (SubPlanState *) lfirst(lst);
4150  SubPlan *sp = sps->subplan;
4151 
4152  /*
4153  * There can be multiple SubPlan nodes referencing the same physical
4154  * subplan (same plan_id, which is its index in PlannedStmt.subplans).
4155  * We should print a subplan only once, so track which ones we already
4156  * printed. This state must be global across the plan tree, since the
4157  * duplicate nodes could be in different plan nodes, eg both a bitmap
4158  * indexscan's indexqual and its parent heapscan's recheck qual. (We
4159  * do not worry too much about which plan node we show the subplan as
4160  * attached to in such cases.)
4161  */
4162  if (bms_is_member(sp->plan_id, es->printed_subplans))
4163  continue;
4165  sp->plan_id);
4166 
4167  /*
4168  * Treat the SubPlan node as an ancestor of the plan node(s) within
4169  * it, so that ruleutils.c can find the referents of subplan
4170  * parameters.
4171  */
4172  ancestors = lcons(sp, ancestors);
4173 
4174  ExplainNode(sps->planstate, ancestors,
4175  relationship, sp->plan_name, es);
4176 
4177  ancestors = list_delete_first(ancestors);
4178  }
4179 }
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:427
struct PlanState * planstate
Definition: execnodes.h:916
SubPlan * subplan
Definition: execnodes.h:915
int plan_id
Definition: primnodes.h:873
char * plan_name
Definition: primnodes.h:875

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

3770 {
3771  char *objectname = NULL;
3772  char *namespace = NULL;
3773  const char *objecttag = NULL;
3774  RangeTblEntry *rte;
3775  char *refname;
3776 
3777  rte = rt_fetch(rti, es->rtable);
3778  refname = (char *) list_nth(es->rtable_names, rti - 1);
3779  if (refname == NULL)
3780  refname = rte->eref->aliasname;
3781 
3782  switch (nodeTag(plan))
3783  {
3784  case T_SeqScan:
3785  case T_SampleScan:
3786  case T_IndexScan:
3787  case T_IndexOnlyScan:
3788  case T_BitmapHeapScan:
3789  case T_TidScan:
3790  case T_TidRangeScan:
3791  case T_ForeignScan:
3792  case T_CustomScan:
3793  case T_ModifyTable:
3794  /* Assert it's on a real relation */
3795  Assert(rte->rtekind == RTE_RELATION);
3796  objectname = get_rel_name(rte->relid);
3797  if (es->verbose)
3798  namespace = get_namespace_name_or_temp(get_rel_namespace(rte->relid));
3799  objecttag = "Relation Name";
3800  break;
3801  case T_FunctionScan:
3802  {
3803  FunctionScan *fscan = (FunctionScan *) plan;
3804 
3805  /* Assert it's on a RangeFunction */
3806  Assert(rte->rtekind == RTE_FUNCTION);
3807 
3808  /*
3809  * If the expression is still a function call of a single
3810  * function, we can get the real name of the function.
3811  * Otherwise, punt. (Even if it was a single function call
3812  * originally, the optimizer could have simplified it away.)
3813  */
3814  if (list_length(fscan->functions) == 1)
3815  {
3816  RangeTblFunction *rtfunc = (RangeTblFunction *) linitial(fscan->functions);
3817 
3818  if (IsA(rtfunc->funcexpr, FuncExpr))
3819  {
3820  FuncExpr *funcexpr = (FuncExpr *) rtfunc->funcexpr;
3821  Oid funcid = funcexpr->funcid;
3822 
3823  objectname = get_func_name(funcid);
3824  if (es->verbose)
3825  namespace = get_namespace_name_or_temp(get_func_namespace(funcid));
3826  }
3827  }
3828  objecttag = "Function Name";
3829  }
3830  break;
3831  case T_TableFuncScan:
3832  Assert(rte->rtekind == RTE_TABLEFUNC);
3833  if (rte->tablefunc)
3834  if (rte->tablefunc->functype == TFT_XMLTABLE)
3835  objectname = "xmltable";
3836  else /* Must be TFT_JSON_TABLE */
3837  objectname = "json_table";
3838  else
3839  objectname = NULL;
3840  objecttag = "Table Function Name";
3841  break;
3842  case T_ValuesScan:
3843  Assert(rte->rtekind == RTE_VALUES);
3844  break;
3845  case T_CteScan:
3846  /* Assert it's on a non-self-reference CTE */
3847  Assert(rte->rtekind == RTE_CTE);
3848  Assert(!rte->self_reference);
3849  objectname = rte->ctename;
3850  objecttag = "CTE Name";
3851  break;
3852  case T_NamedTuplestoreScan:
3854  objectname = rte->enrname;
3855  objecttag = "Tuplestore Name";
3856  break;
3857  case T_WorkTableScan:
3858  /* Assert it's on a self-reference CTE */
3859  Assert(rte->rtekind == RTE_CTE);
3860  Assert(rte->self_reference);
3861  objectname = rte->ctename;
3862  objecttag = "CTE Name";
3863  break;
3864  default:
3865  break;
3866  }
3867 
3868  if (es->format == EXPLAIN_FORMAT_TEXT)
3869  {
3870  appendStringInfoString(es->str, " on");
3871  if (namespace != NULL)
3872  appendStringInfo(es->str, " %s.%s", quote_identifier(namespace),
3873  quote_identifier(objectname));
3874  else if (objectname != NULL)
3875  appendStringInfo(es->str, " %s", quote_identifier(objectname));
3876  if (objectname == NULL || strcmp(refname, objectname) != 0)
3877  appendStringInfo(es->str, " %s", quote_identifier(refname));
3878  }
3879  else
3880  {
3881  if (objecttag != NULL && objectname != NULL)
3882  ExplainPropertyText(objecttag, objectname, es);
3883  if (namespace != NULL)
3884  ExplainPropertyText("Schema", namespace, es);
3885  ExplainPropertyText("Alias", refname, es);
3886  }
3887 }
char * get_namespace_name_or_temp(Oid nspid)
Definition: lsyscache.c:3350
Oid get_rel_namespace(Oid relid)
Definition: lsyscache.c:1933
char * get_func_name(Oid funcid)
Definition: lsyscache.c:1589
Oid get_func_namespace(Oid funcid)
Definition: lsyscache.c:1613
@ RTE_CTE
Definition: parsenodes.h:1005
@ RTE_NAMEDTUPLESTORE
Definition: parsenodes.h:1006
@ RTE_VALUES
Definition: parsenodes.h:1004
@ RTE_FUNCTION
Definition: parsenodes.h:1002
@ RTE_TABLEFUNC
Definition: parsenodes.h:1003
@ RTE_RELATION
Definition: parsenodes.h:999
#define rt_fetch(rangetable_index, rangetable)
Definition: parsetree.h:31
#define linitial(l)
Definition: pg_list.h:176
static void * list_nth(const List *list, int n)
Definition: pg_list.h:297
@ TFT_XMLTABLE
Definition: primnodes.h:91
char * aliasname
Definition: primnodes.h:42
Oid funcid
Definition: primnodes.h:585
List * functions
Definition: plannodes.h:595
char * ctename
Definition: parsenodes.h:1126
TableFunc * tablefunc
Definition: parsenodes.h:1116
bool self_reference
Definition: parsenodes.h:1128
Alias * eref
Definition: parsenodes.h:1162
char * enrname
Definition: parsenodes.h:1155
RTEKind rtekind
Definition: parsenodes.h:1016
TableFuncType functype
Definition: primnodes.h:104

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, quote_identifier(), RangeTblEntry::relid, rt_fetch, ExplainState::rtable, ExplainState::rtable_names, RTE_CTE, RTE_FUNCTION, RTE_NAMEDTUPLESTORE, RTE_RELATION, RTE_TABLEFUNC, RTE_VALUES, RangeTblEntry::rtekind, ExplainState::str, T_BitmapHeapScan, T_CteScan, T_CustomScan, T_ForeignScan, T_FunctionScan, T_IndexOnlyScan, T_IndexScan, T_ModifyTable, T_NamedTuplestoreScan, T_SampleScan, T_SeqScan, T_TableFuncScan, T_TidRangeScan, T_TidScan, T_ValuesScan, T_WorkTableScan, TFT_XMLTABLE, 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 4938 of file explain.c.

4939 {
4940  const char *s;
4941  const char *valid = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.";
4942 
4943  if ((flags & X_NOWHITESPACE) == 0)
4944  appendStringInfoSpaces(es->str, 2 * es->indent);
4945  appendStringInfoCharMacro(es->str, '<');
4946  if ((flags & X_CLOSING) != 0)
4947  appendStringInfoCharMacro(es->str, '/');
4948  for (s = tagname; *s; s++)
4949  appendStringInfoChar(es->str, strchr(valid, *s) ? *s : '-');
4950  if ((flags & X_CLOSE_IMMEDIATE) != 0)
4951  appendStringInfoString(es->str, " /");
4952  appendStringInfoCharMacro(es->str, '>');
4953  if ((flags & X_NOWHITESPACE) == 0)
4954  appendStringInfoCharMacro(es->str, '\n');
4955 }
#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 311 of file explain.c.

312 {
313  ExplainState *es = (ExplainState *) palloc0(sizeof(ExplainState));
314 
315  /* Set default options (most fields can be left as zeroes). */
316  es->costs = true;
317  /* Prepare output buffer. */
318  es->str = makeStringInfo();
319 
320  return es;
321 }
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 980 of file explain.c.

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

2392 {
2393  Agg *plan = (Agg *) astate->ss.ps.plan;
2394 
2395  if (plan->numCols > 0 || plan->groupingSets)
2396  {
2397  /* The key columns refer to the tlist of the child plan */
2398  ancestors = lcons(plan, ancestors);
2399 
2400  if (plan->groupingSets)
2401  show_grouping_sets(outerPlanState(astate), plan, ancestors, es);
2402  else
2403  show_sort_group_keys(outerPlanState(astate), "Group Key",
2404  plan->numCols, 0, plan->grpColIdx,
2405  NULL, NULL, NULL,
2406  ancestors, es);
2407 
2408  ancestors = list_delete_first(ancestors);
2409  }
2410 }
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:2542
static void show_grouping_sets(PlanState *planstate, Agg *agg, List *ancestors, ExplainState *es)
Definition: explain.c:2413
ScanState ss
Definition: execnodes.h:2326
List * groupingSets
Definition: plannodes.h:997
AttrNumber * grpColIdx
Definition: plannodes.h:979
int numCols
Definition: plannodes.h:976
PlanState ps
Definition: execnodes.h:1423

References Agg::groupingSets, Agg::grpColIdx, if(), lcons(), list_delete_first(), Agg::numCols, outerPlanState, PlanState::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 3508 of file explain.c.

3509 {
3510  if (es->format == EXPLAIN_FORMAT_TEXT)
3511  {
3512  bool has_shared = (usage->shared_blks_hit > 0 ||
3513  usage->shared_blks_read > 0 ||
3514  usage->shared_blks_dirtied > 0 ||
3515  usage->shared_blks_written > 0);
3516  bool has_local = (usage->local_blks_hit > 0 ||
3517  usage->local_blks_read > 0 ||
3518  usage->local_blks_dirtied > 0 ||
3519  usage->local_blks_written > 0);
3520  bool has_temp = (usage->temp_blks_read > 0 ||
3521  usage->temp_blks_written > 0);
3522  bool has_timing = (!INSTR_TIME_IS_ZERO(usage->blk_read_time) ||
3523  !INSTR_TIME_IS_ZERO(usage->blk_write_time));
3524  bool has_temp_timing = (!INSTR_TIME_IS_ZERO(usage->temp_blk_read_time) ||
3525  !INSTR_TIME_IS_ZERO(usage->temp_blk_write_time));
3526  bool show_planning = (planning && (has_shared ||
3527  has_local || has_temp || has_timing ||
3528  has_temp_timing));
3529 
3530  if (show_planning)
3531  {
3532  ExplainIndentText(es);
3533  appendStringInfoString(es->str, "Planning:\n");
3534  es->indent++;
3535  }
3536 
3537  /* Show only positive counter values. */
3538  if (has_shared || has_local || has_temp)
3539  {
3540  ExplainIndentText(es);
3541  appendStringInfoString(es->str, "Buffers:");
3542 
3543  if (has_shared)
3544  {
3545  appendStringInfoString(es->str, " shared");
3546  if (usage->shared_blks_hit > 0)
3547  appendStringInfo(es->str, " hit=%lld",
3548  (long long) usage->shared_blks_hit);
3549  if (usage->shared_blks_read > 0)
3550  appendStringInfo(es->str, " read=%lld",
3551  (long long) usage->shared_blks_read);
3552  if (usage->shared_blks_dirtied > 0)
3553  appendStringInfo(es->str, " dirtied=%lld",
3554  (long long) usage->shared_blks_dirtied);
3555  if (usage->shared_blks_written > 0)
3556  appendStringInfo(es->str, " written=%lld",
3557  (long long) usage->shared_blks_written);
3558  if (has_local || has_temp)
3559  appendStringInfoChar(es->str, ',');
3560  }
3561  if (has_local)
3562  {
3563  appendStringInfoString(es->str, " local");
3564  if (usage->local_blks_hit > 0)
3565  appendStringInfo(es->str, " hit=%lld",
3566  (long long) usage->local_blks_hit);
3567  if (usage->local_blks_read > 0)
3568  appendStringInfo(es->str, " read=%lld",
3569  (long long) usage->local_blks_read);
3570  if (usage->local_blks_dirtied > 0)
3571  appendStringInfo(es->str, " dirtied=%lld",
3572  (long long) usage->local_blks_dirtied);
3573  if (usage->local_blks_written > 0)
3574  appendStringInfo(es->str, " written=%lld",
3575  (long long) usage->local_blks_written);
3576  if (has_temp)
3577  appendStringInfoChar(es->str, ',');
3578  }
3579  if (has_temp)
3580  {
3581  appendStringInfoString(es->str, " temp");
3582  if (usage->temp_blks_read > 0)
3583  appendStringInfo(es->str, " read=%lld",
3584  (long long) usage->temp_blks_read);
3585  if (usage->temp_blks_written > 0)
3586  appendStringInfo(es->str, " written=%lld",
3587  (long long) usage->temp_blks_written);
3588  }
3589  appendStringInfoChar(es->str, '\n');
3590  }
3591 
3592  /* As above, show only positive counter values. */
3593  if (has_timing || has_temp_timing)
3594  {
3595  ExplainIndentText(es);
3596  appendStringInfoString(es->str, "I/O Timings:");
3597 
3598  if (has_timing)
3599  {
3600  appendStringInfoString(es->str, " shared/local");
3601  if (!INSTR_TIME_IS_ZERO(usage->blk_read_time))
3602  appendStringInfo(es->str, " read=%0.3f",
3603  INSTR_TIME_GET_MILLISEC(usage->blk_read_time));
3604  if (!INSTR_TIME_IS_ZERO(usage->blk_write_time))
3605  appendStringInfo(es->str, " write=%0.3f",
3606  INSTR_TIME_GET_MILLISEC(usage->blk_write_time));
3607  if (has_temp_timing)
3608  appendStringInfoChar(es->str, ',');
3609  }
3610  if (has_temp_timing)
3611  {
3612  appendStringInfoString(es->str, " temp");
3613  if (!INSTR_TIME_IS_ZERO(usage->temp_blk_read_time))
3614  appendStringInfo(es->str, " read=%0.3f",
3615  INSTR_TIME_GET_MILLISEC(usage->temp_blk_read_time));
3616  if (!INSTR_TIME_IS_ZERO(usage->temp_blk_write_time))
3617  appendStringInfo(es->str, " write=%0.3f",
3618  INSTR_TIME_GET_MILLISEC(usage->temp_blk_write_time));
3619  }
3620  appendStringInfoChar(es->str, '\n');
3621  }
3622 
3623  if (show_planning)
3624  es->indent--;
3625  }
3626  else
3627  {
3628  ExplainPropertyInteger("Shared Hit Blocks", NULL,
3629  usage->shared_blks_hit, es);
3630  ExplainPropertyInteger("Shared Read Blocks", NULL,
3631  usage->shared_blks_read, es);
3632  ExplainPropertyInteger("Shared Dirtied Blocks", NULL,
3633  usage->shared_blks_dirtied, es);
3634  ExplainPropertyInteger("Shared Written Blocks", NULL,
3635  usage->shared_blks_written, es);
3636  ExplainPropertyInteger("Local Hit Blocks", NULL,
3637  usage->local_blks_hit, es);
3638  ExplainPropertyInteger("Local Read Blocks", NULL,
3639  usage->local_blks_read, es);
3640  ExplainPropertyInteger("Local Dirtied Blocks", NULL,
3641  usage->local_blks_dirtied, es);
3642  ExplainPropertyInteger("Local Written Blocks", NULL,
3643  usage->local_blks_written, es);
3644  ExplainPropertyInteger("Temp Read Blocks", NULL,
3645  usage->temp_blks_read, es);
3646  ExplainPropertyInteger("Temp Written Blocks", NULL,
3647  usage->temp_blks_written, es);
3648  if (track_io_timing)
3649  {
3650  ExplainPropertyFloat("I/O Read Time", "ms",
3651  INSTR_TIME_GET_MILLISEC(usage->blk_read_time),
3652  3, es);
3653  ExplainPropertyFloat("I/O Write Time", "ms",
3654  INSTR_TIME_GET_MILLISEC(usage->blk_write_time),
3655  3, es);
3656  ExplainPropertyFloat("Temp I/O Read Time", "ms",
3657  INSTR_TIME_GET_MILLISEC(usage->temp_blk_read_time),
3658  3, es);
3659  ExplainPropertyFloat("Temp I/O Write Time", "ms",
3660  INSTR_TIME_GET_MILLISEC(usage->temp_blk_write_time),
3661  3, es);
3662  }
3663  }
3664 }
bool track_io_timing
Definition: bufmgr.c:137
#define INSTR_TIME_IS_ZERO(t)
Definition: instr_time.h:152
#define INSTR_TIME_GET_MILLISEC(t)
Definition: instr_time.h:202
static void usage(const char *progname)
Definition: vacuumlo.c:417

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

3457 {
3458  int paramid = -1;
3459  List *params = NIL;
3460 
3461  Assert(bms_params);
3462 
3463  while ((paramid = bms_next_member(bms_params, paramid)) >= 0)
3464  {
3465  char param[32];
3466 
3467  snprintf(param, sizeof(param), "$%d", paramid);
3468  params = lappend(params, pstrdup(param));
3469  }
3470 
3471  if (params)
3472  ExplainPropertyList("Params Evaluated", params, es);
3473 }
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1045
char * pstrdup(const char *in)
Definition: mcxt.c:1305

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

2273 {
2274  List *context;
2275  char *exprstr;
2276 
2277  /* Set up deparsing context */
2278  context = set_deparse_context_plan(es->deparse_cxt,
2279  planstate->plan,
2280  ancestors);
2281 
2282  /* Deparse the expression */
2283  exprstr = deparse_expression(node, context, useprefix, false);
2284 
2285  /* And add to es->str */
2286  ExplainPropertyText(qlabel, exprstr, es);
2287 }
char * deparse_expression(Node *expr, List *dpcontext, bool forceprefix, bool showimplicit)
Definition: ruleutils.c:3607
List * set_deparse_context_plan(List *dpcontext, Plan *plan, List *ancestors)
Definition: ruleutils.c:3781

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

3436 {
3437  FdwRoutine *fdwroutine = fsstate->fdwroutine;
3438 
3439  /* Let the FDW emit whatever fields it wants */
3440  if (((ForeignScan *) fsstate->ss.ps.plan)->operation != CMD_SELECT)
3441  {
3442  if (fdwroutine->ExplainDirectModify != NULL)
3443  fdwroutine->ExplainDirectModify(fsstate, es);
3444  }
3445  else
3446  {
3447  if (fdwroutine->ExplainForeignScan != NULL)
3448  fdwroutine->ExplainForeignScan(fsstate, es);
3449  }
3450 }
ExplainForeignScan_function ExplainForeignScan
Definition: fdwapi.h:252
ExplainDirectModify_function ExplainDirectModify
Definition: fdwapi.h:254
struct FdwRoutine * fdwroutine
Definition: execnodes.h:1908
ScanState ss
Definition: execnodes.h:1903

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

2524 {
2525  Group *plan = (Group *) gstate->ss.ps.plan;
2526 
2527  /* The key columns refer to the tlist of the child plan */
2528  ancestors = lcons(plan, ancestors);
2529  show_sort_group_keys(outerPlanState(gstate), "Group Key",
2530  plan->numCols, 0, plan->grpColIdx,
2531  NULL, NULL, NULL,
2532  ancestors, es);
2533  ancestors = list_delete_first(ancestors);
2534 }
ScanState ss
Definition: execnodes.h:2279
int numCols
Definition: plannodes.h:941
AttrNumber * grpColIdx
Definition: plannodes.h:944

References Group::grpColIdx, lcons(), list_delete_first(), Group::numCols, outerPlanState, PlanState::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 2444 of file explain.c.

2448 {
2449  Plan *plan = planstate->plan;
2450  char *exprstr;
2451  ListCell *lc;
2452  List *gsets = aggnode->groupingSets;
2453  AttrNumber *keycols = aggnode->grpColIdx;
2454  const char *keyname;
2455  const char *keysetname;
2456 
2457  if (aggnode->aggstrategy == AGG_HASHED || aggnode->aggstrategy == AGG_MIXED)
2458  {
2459  keyname = "Hash Key";
2460  keysetname = "Hash Keys";
2461  }
2462  else
2463  {
2464  keyname = "Group Key";
2465  keysetname = "Group Keys";
2466  }
2467 
2468  ExplainOpenGroup("Grouping Set", NULL, true, es);
2469 
2470  if (sortnode)
2471  {
2472  show_sort_group_keys(planstate, "Sort Key",
2473  sortnode->numCols, 0, sortnode->sortColIdx,
2474  sortnode->sortOperators, sortnode->collations,
2475  sortnode->nullsFirst,
2476  ancestors, es);
2477  if (es->format == EXPLAIN_FORMAT_TEXT)
2478  es->indent++;
2479  }
2480 
2481  ExplainOpenGroup(keysetname, keysetname, false, es);
2482 
2483  foreach(lc, gsets)
2484  {
2485  List *result = NIL;
2486  ListCell *lc2;
2487 
2488  foreach(lc2, (List *) lfirst(lc))
2489  {
2490  Index i = lfirst_int(lc2);
2491  AttrNumber keyresno = keycols[i];
2492  TargetEntry *target = get_tle_by_resno(plan->targetlist,
2493  keyresno);
2494 
2495  if (!target)
2496  elog(ERROR, "no tlist entry for key %d", keyresno);
2497  /* Deparse the expression, showing any top-level cast */
2498  exprstr = deparse_expression((Node *) target->expr, context,
2499  useprefix, true);
2500 
2501  result = lappend(result, exprstr);
2502  }
2503 
2504  if (!result && es->format == EXPLAIN_FORMAT_TEXT)
2505  ExplainPropertyText(keyname, "()", es);
2506  else
2507  ExplainPropertyListNested(keyname, result, es);
2508  }
2509 
2510  ExplainCloseGroup(keysetname, keysetname, false, es);
2511 
2512  if (sortnode && es->format == EXPLAIN_FORMAT_TEXT)
2513  es->indent--;
2514 
2515  ExplainCloseGroup("Grouping Set", NULL, true, es);
2516 }
unsigned int Index
Definition: c.h:560
void ExplainPropertyListNested(const char *qlabel, List *data, ExplainState *es)
Definition: explain.c:4422
TargetEntry * get_tle_by_resno(List *tlist, AttrNumber resno)
#define lfirst_int(lc)
Definition: pg_list.h:171
List * targetlist
Definition: plannodes.h:142
Oid * sortOperators
Definition: plannodes.h:911
int numCols
Definition: plannodes.h:905
bool * nullsFirst
Definition: plannodes.h:917
AttrNumber * sortColIdx
Definition: plannodes.h:908
Oid * collations
Definition: plannodes.h:914
Expr * expr
Definition: primnodes.h:1828

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

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

2415 {
2416  List *context;
2417  bool useprefix;
2418  ListCell *lc;
2419 
2420  /* Set up deparsing context */
2421  context = set_deparse_context_plan(es->deparse_cxt,
2422  planstate->plan,
2423  ancestors);
2424  useprefix = (list_length(es->rtable) > 1 || es->verbose);
2425 
2426  ExplainOpenGroup("Grouping Sets", "Grouping Sets", false, es);
2427 
2428  show_grouping_set_keys(planstate, agg, NULL,
2429  context, useprefix, ancestors, es);
2430 
2431  foreach(lc, agg->chain)
2432  {
2433  Agg *aggnode = lfirst(lc);
2434  Sort *sortnode = (Sort *) aggnode->plan.lefttree;
2435 
2436  show_grouping_set_keys(planstate, aggnode, sortnode,
2437  context, useprefix, ancestors, es);
2438  }
2439 
2440  ExplainCloseGroup("Grouping Sets", "Grouping Sets", false, es);
2441 }
static void show_grouping_set_keys(PlanState *planstate, Agg *aggnode, Sort *sortnode, List *context, bool useprefix, List *ancestors, ExplainState *es)
Definition: explain.c:2444
List * chain
Definition: plannodes.h:1000
Plan plan
Definition: plannodes.h:967
struct Plan * lefttree
Definition: plannodes.h:144

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

3020 {
3021  HashInstrumentation hinstrument = {0};
3022 
3023  /*
3024  * Collect stats from the local process, even when it's a parallel query.
3025  * In a parallel query, the leader process may or may not have run the
3026  * hash join, and even if it did it may not have built a hash table due to
3027  * timing (if it started late it might have seen no tuples in the outer
3028  * relation and skipped building the hash table). Therefore we have to be
3029  * prepared to get instrumentation data from all participants.
3030  */
3031  if (hashstate->hinstrument)
3032  memcpy(&hinstrument, hashstate->hinstrument,
3033  sizeof(HashInstrumentation));
3034 
3035  /*
3036  * Merge results from workers. In the parallel-oblivious case, the
3037  * results from all participants should be identical, except where
3038  * participants didn't run the join at all so have no data. In the
3039  * parallel-aware case, we need to consider all the results. Each worker
3040  * may have seen a different subset of batches and we want to report the
3041  * highest memory usage across all batches. We take the maxima of other
3042  * values too, for the same reasons as in ExecHashAccumInstrumentation.
3043  */
3044  if (hashstate->shared_info)
3045  {
3046  SharedHashInfo *shared_info = hashstate->shared_info;
3047  int i;
3048 
3049  for (i = 0; i < shared_info->num_workers; ++i)
3050  {
3051  HashInstrumentation *worker_hi = &shared_info->hinstrument[i];
3052 
3053  hinstrument.nbuckets = Max(hinstrument.nbuckets,
3054  worker_hi->nbuckets);
3055  hinstrument.nbuckets_original = Max(hinstrument.nbuckets_original,
3056  worker_hi->nbuckets_original);
3057  hinstrument.nbatch = Max(hinstrument.nbatch,
3058  worker_hi->nbatch);
3059  hinstrument.nbatch_original = Max(hinstrument.nbatch_original,
3060  worker_hi->nbatch_original);
3061  hinstrument.space_peak = Max(hinstrument.space_peak,
3062  worker_hi->space_peak);
3063  }
3064  }
3065 
3066  if (hinstrument.nbatch > 0)
3067  {
3068  long spacePeakKb = (hinstrument.space_peak + 1023) / 1024;
3069 
3070  if (es->format != EXPLAIN_FORMAT_TEXT)
3071  {
3072  ExplainPropertyInteger("Hash Buckets", NULL,
3073  hinstrument.nbuckets, es);
3074  ExplainPropertyInteger("Original Hash Buckets", NULL,
3075  hinstrument.nbuckets_original, es);
3076  ExplainPropertyInteger("Hash Batches", NULL,
3077  hinstrument.nbatch, es);
3078  ExplainPropertyInteger("Original Hash Batches", NULL,
3079  hinstrument.nbatch_original, es);
3080  ExplainPropertyInteger("Peak Memory Usage", "kB",
3081  spacePeakKb, es);
3082  }
3083  else if (hinstrument.nbatch_original != hinstrument.nbatch ||
3084  hinstrument.nbuckets_original != hinstrument.nbuckets)
3085  {
3086  ExplainIndentText(es);
3087  appendStringInfo(es->str,
3088  "Buckets: %d (originally %d) Batches: %d (originally %d) Memory Usage: %ldkB\n",
3089  hinstrument.nbuckets,
3090  hinstrument.nbuckets_original,
3091  hinstrument.nbatch,
3092  hinstrument.nbatch_original,
3093  spacePeakKb);
3094  }
3095  else
3096  {
3097  ExplainIndentText(es);
3098  appendStringInfo(es->str,
3099  "Buckets: %d Batches: %d Memory Usage: %ldkB\n",
3100  hinstrument.nbuckets, hinstrument.nbatch,
3101  spacePeakKb);
3102  }
3103  }
3104 }
#define Max(x, y)
Definition: c.h:991
SharedHashInfo * shared_info
Definition: execnodes.h:2618
HashInstrumentation * hinstrument
Definition: execnodes.h:2625
HashInstrumentation hinstrument[FLEXIBLE_ARRAY_MEMBER]
Definition: execnodes.h:2599

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 hashstate,
ExplainState es 
)
static

Definition at line 3254 of file explain.c.

3255 {
3256  Agg *agg = (Agg *) aggstate->ss.ps.plan;
3257  int64 memPeakKb = (aggstate->hash_mem_peak + 1023) / 1024;
3258