PostgreSQL Source Code git master
Loading...
Searching...
No Matches
pg_overexplain.c File Reference
#include "postgres.h"
#include "catalog/pg_class.h"
#include "commands/defrem.h"
#include "commands/explain.h"
#include "commands/explain_format.h"
#include "commands/explain_state.h"
#include "fmgr.h"
#include "parser/parsetree.h"
#include "storage/lock.h"
#include "utils/builtins.h"
#include "utils/lsyscache.h"
Include dependency graph for pg_overexplain.c:

Go to the source code of this file.

Data Structures

struct  overexplain_options
 

Functions

 PG_MODULE_MAGIC_EXT (.name="pg_overexplain",.version=PG_VERSION)
 
static overexplain_optionsoverexplain_ensure_options (ExplainState *es)
 
static void overexplain_debug_handler (ExplainState *es, DefElem *opt, ParseState *pstate)
 
static void overexplain_range_table_handler (ExplainState *es, DefElem *opt, ParseState *pstate)
 
static void overexplain_per_node_hook (PlanState *planstate, List *ancestors, const char *relationship, const char *plan_name, ExplainState *es)
 
static void overexplain_per_plan_hook (PlannedStmt *plannedstmt, IntoClause *into, ExplainState *es, const char *queryString, ParamListInfo params, QueryEnvironment *queryEnv)
 
static void overexplain_debug (PlannedStmt *plannedstmt, ExplainState *es)
 
static void overexplain_range_table (PlannedStmt *plannedstmt, ExplainState *es)
 
static void overexplain_alias (const char *qlabel, Alias *alias, ExplainState *es)
 
static void overexplain_bitmapset (const char *qlabel, Bitmapset *bms, ExplainState *es)
 
static void overexplain_bitmapset_list (const char *qlabel, List *bms_list, ExplainState *es)
 
static void overexplain_intlist (const char *qlabel, List *list, ExplainState *es)
 
void _PG_init (void)
 

Variables

static int es_extension_id
 
static explain_per_node_hook_type prev_explain_per_node_hook
 
static explain_per_plan_hook_type prev_explain_per_plan_hook
 

Function Documentation

◆ _PG_init()

void _PG_init ( void  )

Definition at line 70 of file pg_overexplain.c.

71{
72 /* Get an ID that we can use to cache data in an ExplainState. */
73 es_extension_id = GetExplainExtensionId("pg_overexplain");
74
75 /* Register the new EXPLAIN options implemented by this module. */
81
82 /* Use the per-node and per-plan hooks to make our options do something. */
87}
explain_per_node_hook_type explain_per_node_hook
Definition explain.c:59
explain_per_plan_hook_type explain_per_plan_hook
Definition explain.c:58
int GetExplainExtensionId(const char *extension_name)
void RegisterExtensionExplainOption(const char *option_name, ExplainOptionHandler handler, ExplainOptionGUCCheckHandler guc_check_handler)
bool GUCCheckBooleanExplainOption(const char *option_name, const char *option_value, NodeTag option_type)
static void overexplain_per_plan_hook(PlannedStmt *plannedstmt, IntoClause *into, ExplainState *es, const char *queryString, ParamListInfo params, QueryEnvironment *queryEnv)
static explain_per_plan_hook_type prev_explain_per_plan_hook
static explain_per_node_hook_type prev_explain_per_node_hook
static void overexplain_per_node_hook(PlanState *planstate, List *ancestors, const char *relationship, const char *plan_name, ExplainState *es)
static void overexplain_debug_handler(ExplainState *es, DefElem *opt, ParseState *pstate)
static int es_extension_id
static void overexplain_range_table_handler(ExplainState *es, DefElem *opt, ParseState *pstate)

References es_extension_id, explain_per_node_hook, explain_per_plan_hook, GetExplainExtensionId(), GUCCheckBooleanExplainOption(), overexplain_debug_handler(), overexplain_per_node_hook(), overexplain_per_plan_hook(), overexplain_range_table_handler(), prev_explain_per_node_hook, prev_explain_per_plan_hook, and RegisterExtensionExplainOption().

◆ overexplain_alias()

static void overexplain_alias ( const char qlabel,
Alias alias,
ExplainState es 
)
static

Definition at line 805 of file pg_overexplain.c.

806{
808 bool first = true;
809
810 Assert(alias != NULL);
811
814
816 {
817 appendStringInfo(&buf, "%s%s",
818 first ? "" : ", ",
819 quote_identifier(cn->sval));
820 first = false;
821 }
822
824 ExplainPropertyText(qlabel, buf.data, es);
825 pfree(buf.data);
826}
#define Assert(condition)
Definition c.h:943
void ExplainPropertyText(const char *qlabel, const char *value, ExplainState *es)
void pfree(void *pointer)
Definition mcxt.c:1616
#define foreach_node(type, var, lst)
Definition pg_list.h:528
static char buf[DEFAULT_XLOG_SEG_SIZE]
static int fb(int x)
const char * quote_identifier(const char *ident)
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition stringinfo.c:145
void appendStringInfoChar(StringInfo str, char ch)
Definition stringinfo.c:242
void initStringInfo(StringInfo str)
Definition stringinfo.c:97
char * aliasname
Definition primnodes.h:52
List * colnames
Definition primnodes.h:53
Definition value.h:64

References Alias::aliasname, appendStringInfo(), appendStringInfoChar(), Assert, buf, Alias::colnames, ExplainPropertyText(), fb(), foreach_node, initStringInfo(), pfree(), and quote_identifier().

Referenced by overexplain_range_table().

◆ overexplain_bitmapset()

static void overexplain_bitmapset ( const char qlabel,
Bitmapset bms,
ExplainState es 
)
static

Definition at line 834 of file pg_overexplain.c.

835{
836 int x = -1;
837
839
840 if (bms_is_empty(bms))
841 {
842 ExplainPropertyText(qlabel, "none", es);
843 return;
844 }
845
847 while ((x = bms_next_member(bms, x)) >= 0)
848 appendStringInfo(&buf, " %d", x);
849 Assert(buf.data[0] == ' ');
850 ExplainPropertyText(qlabel, buf.data + 1, es);
851 pfree(buf.data);
852}
int bms_next_member(const Bitmapset *a, int prevbit)
Definition bitmapset.c:1290
#define bms_is_empty(a)
Definition bitmapset.h:118
int x
Definition isn.c:75

References appendStringInfo(), Assert, bms_is_empty, bms_next_member(), buf, ExplainPropertyText(), fb(), initStringInfo(), pfree(), and x.

Referenced by overexplain_debug(), overexplain_per_node_hook(), and overexplain_range_table().

◆ overexplain_bitmapset_list()

static void overexplain_bitmapset_list ( const char qlabel,
List bms_list,
ExplainState es 
)
static

Definition at line 862 of file pg_overexplain.c.

864{
866
868
870 {
873 else
874 {
875 int x = -1;
876 bool first = true;
877
879 while ((x = bms_next_member(bms, x)) >= 0)
880 {
881 if (first)
882 first = false;
883 else
885 appendStringInfo(&buf, "%d", x);
886 }
888 }
889 }
890
891 if (buf.len == 0)
892 {
893 ExplainPropertyText(qlabel, "none", es);
894 return;
895 }
896
897 Assert(buf.data[0] == ' ');
898 ExplainPropertyText(qlabel, buf.data + 1, es);
899 pfree(buf.data);
900}
int bms_singleton_member(const Bitmapset *a)
Definition bitmapset.c:665
BMS_Membership bms_membership(const Bitmapset *a)
Definition bitmapset.c:765
@ BMS_SINGLETON
Definition bitmapset.h:72
void appendStringInfoString(StringInfo str, const char *s)
Definition stringinfo.c:230

References appendStringInfo(), appendStringInfoChar(), appendStringInfoString(), Assert, bms_membership(), bms_next_member(), BMS_SINGLETON, bms_singleton_member(), buf, ExplainPropertyText(), fb(), foreach_node, initStringInfo(), pfree(), and x.

Referenced by overexplain_per_node_hook().

◆ overexplain_debug()

static void overexplain_debug ( PlannedStmt plannedstmt,
ExplainState es 
)
static

Definition at line 347 of file pg_overexplain.c.

348{
349 char *commandType = NULL;
350 StringInfoData flags;
351
352 /* Even in text mode, we want to set this output apart as its own group. */
353 ExplainOpenGroup("PlannedStmt", "PlannedStmt", true, es);
354 if (es->format == EXPLAIN_FORMAT_TEXT)
355 {
357 appendStringInfoString(es->str, "PlannedStmt:\n");
358 es->indent++;
359 }
360
361 /* Print the command type. */
362 switch (plannedstmt->commandType)
363 {
364 case CMD_UNKNOWN:
365 commandType = "unknown";
366 break;
367 case CMD_SELECT:
368 commandType = "select";
369 break;
370 case CMD_UPDATE:
371 commandType = "update";
372 break;
373 case CMD_INSERT:
374 commandType = "insert";
375 break;
376 case CMD_DELETE:
377 commandType = "delete";
378 break;
379 case CMD_MERGE:
380 commandType = "merge";
381 break;
382 case CMD_UTILITY:
383 commandType = "utility";
384 break;
385 case CMD_NOTHING:
386 commandType = "nothing";
387 break;
388 }
389 ExplainPropertyText("Command Type", commandType, es);
390
391 /* Print various properties as a comma-separated list of flags. */
392 initStringInfo(&flags);
393 if (plannedstmt->hasReturning)
394 appendStringInfoString(&flags, ", hasReturning");
395 if (plannedstmt->hasModifyingCTE)
396 appendStringInfoString(&flags, ", hasModifyingCTE");
397 if (plannedstmt->canSetTag)
398 appendStringInfoString(&flags, ", canSetTag");
399 if (plannedstmt->transientPlan)
400 appendStringInfoString(&flags, ", transientPlan");
401 if (plannedstmt->dependsOnRole)
402 appendStringInfoString(&flags, ", dependsOnRole");
403 if (plannedstmt->parallelModeNeeded)
404 appendStringInfoString(&flags, ", parallelModeNeeded");
405 if (flags.len == 0)
406 appendStringInfoString(&flags, ", none");
407 ExplainPropertyText("Flags", flags.data + 2, es);
408
409 /* Various lists of integers. */
410 overexplain_bitmapset("Subplans Needing Rewind",
411 plannedstmt->rewindPlanIDs, es);
412 overexplain_intlist("Relation OIDs",
413 plannedstmt->relationOids, es);
414 overexplain_intlist("Executor Parameter Types",
415 plannedstmt->paramExecTypes, es);
416
417 /*
418 * Print the statement location. (If desired, we could alternatively print
419 * stmt_location and stmt_len as two separate fields.)
420 */
421 if (plannedstmt->stmt_location == -1)
422 ExplainPropertyText("Parse Location", "Unknown", es);
423 else if (plannedstmt->stmt_len == 0)
424 ExplainPropertyText("Parse Location",
425 psprintf("%d to end", plannedstmt->stmt_location),
426 es);
427 else
428 ExplainPropertyText("Parse Location",
429 psprintf("%d for %d bytes",
430 plannedstmt->stmt_location,
431 plannedstmt->stmt_len),
432 es);
433
434 /* Done with this group. */
435 if (es->format == EXPLAIN_FORMAT_TEXT)
436 es->indent--;
437 ExplainCloseGroup("PlannedStmt", "PlannedStmt", true, es);
438}
void ExplainOpenGroup(const char *objtype, const char *labelname, bool labeled, ExplainState *es)
void ExplainIndentText(ExplainState *es)
void ExplainCloseGroup(const char *objtype, const char *labelname, bool labeled, ExplainState *es)
@ EXPLAIN_FORMAT_TEXT
@ CMD_MERGE
Definition nodes.h:279
@ CMD_UTILITY
Definition nodes.h:280
@ CMD_INSERT
Definition nodes.h:277
@ CMD_DELETE
Definition nodes.h:278
@ CMD_UNKNOWN
Definition nodes.h:274
@ CMD_UPDATE
Definition nodes.h:276
@ CMD_SELECT
Definition nodes.h:275
@ CMD_NOTHING
Definition nodes.h:282
static void overexplain_bitmapset(const char *qlabel, Bitmapset *bms, ExplainState *es)
static void overexplain_intlist(const char *qlabel, List *list, ExplainState *es)
char * psprintf(const char *fmt,...)
Definition psprintf.c:43
StringInfo str
ExplainFormat format
bool hasModifyingCTE
Definition plannodes.h:81
bool canSetTag
Definition plannodes.h:84
Bitmapset * rewindPlanIDs
Definition plannodes.h:135
ParseLoc stmt_len
Definition plannodes.h:171
bool hasReturning
Definition plannodes.h:78
ParseLoc stmt_location
Definition plannodes.h:169
bool transientPlan
Definition plannodes.h:87
List * relationOids
Definition plannodes.h:144
bool dependsOnRole
Definition plannodes.h:90
CmdType commandType
Definition plannodes.h:66
List * paramExecTypes
Definition plannodes.h:150
bool parallelModeNeeded
Definition plannodes.h:93

References appendStringInfoString(), PlannedStmt::canSetTag, CMD_DELETE, CMD_INSERT, CMD_MERGE, CMD_NOTHING, CMD_SELECT, CMD_UNKNOWN, CMD_UPDATE, CMD_UTILITY, PlannedStmt::commandType, StringInfoData::data, PlannedStmt::dependsOnRole, EXPLAIN_FORMAT_TEXT, ExplainCloseGroup(), ExplainIndentText(), ExplainOpenGroup(), ExplainPropertyText(), fb(), ExplainState::format, PlannedStmt::hasModifyingCTE, PlannedStmt::hasReturning, ExplainState::indent, initStringInfo(), StringInfoData::len, overexplain_bitmapset(), overexplain_intlist(), PlannedStmt::parallelModeNeeded, PlannedStmt::paramExecTypes, psprintf(), PlannedStmt::relationOids, PlannedStmt::rewindPlanIDs, PlannedStmt::stmt_len, PlannedStmt::stmt_location, ExplainState::str, and PlannedStmt::transientPlan.

Referenced by overexplain_per_plan_hook().

◆ overexplain_debug_handler()

static void overexplain_debug_handler ( ExplainState es,
DefElem opt,
ParseState pstate 
)
static

Definition at line 113 of file pg_overexplain.c.

114{
116
117 options->debug = defGetBoolean(opt);
118}
bool defGetBoolean(DefElem *def)
Definition define.c:93
static overexplain_options * overexplain_ensure_options(ExplainState *es)

References defGetBoolean(), and overexplain_ensure_options().

Referenced by _PG_init().

◆ overexplain_ensure_options()

static overexplain_options * overexplain_ensure_options ( ExplainState es)
static

Definition at line 94 of file pg_overexplain.c.

95{
97
99
100 if (options == NULL)
101 {
104 }
105
106 return options;
107}
void * GetExplainExtensionState(ExplainState *es, int extension_id)
void SetExplainExtensionState(ExplainState *es, int extension_id, void *opaque)
#define palloc0_object(type)
Definition fe_memutils.h:75

References es_extension_id, fb(), GetExplainExtensionState(), palloc0_object, and SetExplainExtensionState().

Referenced by overexplain_debug_handler(), and overexplain_range_table_handler().

◆ overexplain_intlist()

static void overexplain_intlist ( const char qlabel,
List list,
ExplainState es 
)
static

Definition at line 908 of file pg_overexplain.c.

909{
911
913
914 if (list == NIL)
915 {
916 ExplainPropertyText(qlabel, "none", es);
917 return;
918 }
919
920 if (IsA(list, IntList))
921 {
922 foreach_int(i, list)
923 appendStringInfo(&buf, " %d", i);
924 }
925 else if (IsA(list, OidList))
926 {
927 foreach_oid(o, list)
928 appendStringInfo(&buf, " %u", o);
929 }
930 else if (IsA(list, XidList))
931 {
932 foreach_xid(x, list)
933 appendStringInfo(&buf, " %u", x);
934 }
935 else
936 {
937 appendStringInfoString(&buf, " not an integer list");
938 Assert(false);
939 }
940
941 if (buf.len > 0)
942 ExplainPropertyText(qlabel, buf.data + 1, es);
943
944 pfree(buf.data);
945}
int i
Definition isn.c:77
#define IsA(nodeptr, _type_)
Definition nodes.h:164
#define NIL
Definition pg_list.h:68
#define foreach_xid(var, lst)
Definition pg_list.h:504
#define foreach_oid(var, lst)
Definition pg_list.h:503
#define foreach_int(var, lst)
Definition pg_list.h:502

References appendStringInfo(), appendStringInfoString(), Assert, buf, ExplainPropertyText(), fb(), foreach_int, foreach_oid, foreach_xid, i, initStringInfo(), IsA, NIL, pfree(), and x.

Referenced by overexplain_debug().

◆ overexplain_per_node_hook()

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

Definition at line 138 of file pg_overexplain.c.

141{
143 Plan *plan = planstate->plan;
144
146 (*prev_explain_per_node_hook) (planstate, ancestors, relationship,
147 plan_name, es);
148
150 if (options == NULL)
151 return;
152
153 /*
154 * If the "debug" option was given, display miscellaneous fields from the
155 * "Plan" node that would not otherwise be displayed.
156 */
157 if (options->debug)
158 {
159 /*
160 * Normal EXPLAIN will display "Disabled: true" if the node is
161 * disabled; but that is based on noticing that plan->disabled_nodes
162 * is higher than the sum of its children; here, we display the raw
163 * value, for debugging purposes.
164 */
165 ExplainPropertyInteger("Disabled Nodes", NULL, plan->disabled_nodes,
166 es);
167
168 /*
169 * Normal EXPLAIN will display the parallel_aware flag; here, we show
170 * the parallel_safe flag as well.
171 */
172 ExplainPropertyBool("Parallel Safe", plan->parallel_safe, es);
173
174 /*
175 * The plan node ID isn't normally displayed, since it is only useful
176 * for debugging.
177 */
178 ExplainPropertyInteger("Plan Node ID", NULL, plan->plan_node_id, es);
179
180 /*
181 * It is difficult to explain what extParam and allParam mean in plain
182 * language, so we simply display these fields labelled with the
183 * structure member name. For compactness, the text format omits the
184 * display of this information when the bitmapset is empty.
185 */
186 if (es->format != EXPLAIN_FORMAT_TEXT || !bms_is_empty(plan->extParam))
187 overexplain_bitmapset("extParam", plan->extParam, es);
188 if (es->format != EXPLAIN_FORMAT_TEXT || !bms_is_empty(plan->allParam))
189 overexplain_bitmapset("allParam", plan->allParam, es);
190 }
191
192 /*
193 * If the "range_table" option was specified, display information about
194 * the range table indexes for this node.
195 */
196 if (options->range_table)
197 {
198 bool opened_elided_nodes = false;
199
200 switch (nodeTag(plan))
201 {
202 case T_SeqScan:
203 case T_SampleScan:
204 case T_IndexScan:
205 case T_IndexOnlyScan:
206 case T_BitmapHeapScan:
207 case T_TidScan:
208 case T_TidRangeScan:
209 case T_SubqueryScan:
210 case T_FunctionScan:
211 case T_TableFuncScan:
212 case T_ValuesScan:
213 case T_CteScan:
215 case T_WorkTableScan:
216 ExplainPropertyInteger("Scan RTI", NULL,
217 ((Scan *) plan)->scanrelid, es);
218 break;
219 case T_ForeignScan:
220 overexplain_bitmapset("Scan RTIs",
221 ((ForeignScan *) plan)->fs_base_relids,
222 es);
223 break;
224 case T_CustomScan:
225 overexplain_bitmapset("Scan RTIs",
226 ((CustomScan *) plan)->custom_relids,
227 es);
228 break;
229 case T_ModifyTable:
230 ExplainPropertyInteger("Nominal RTI", NULL,
231 ((ModifyTable *) plan)->nominalRelation, es);
232 ExplainPropertyInteger("Exclude Relation RTI", NULL,
233 ((ModifyTable *) plan)->exclRelRTI, es);
234 break;
235 case T_Append:
236 overexplain_bitmapset("Append RTIs",
237 ((Append *) plan)->apprelids,
238 es);
239 overexplain_bitmapset_list("Child Append RTIs",
240 ((Append *) plan)->child_append_relid_sets,
241 es);
242 break;
243 case T_MergeAppend:
244 overexplain_bitmapset("Append RTIs",
245 ((MergeAppend *) plan)->apprelids,
246 es);
247 overexplain_bitmapset_list("Child Append RTIs",
248 ((MergeAppend *) plan)->child_append_relid_sets,
249 es);
250 break;
251 case T_Result:
252
253 /*
254 * 'relids' is only meaningful when plan->lefttree is NULL,
255 * but if somehow it ends up set when plan->lefttree is not
256 * NULL, print it anyway.
257 */
258 if (plan->lefttree == NULL ||
259 ((Result *) plan)->relids != NULL)
261 ((Result *) plan)->relids,
262 es);
263 break;
264 default:
265 break;
266 }
267
269 {
270 char *elidednodetag;
271
272 if (n->plan_node_id != plan->plan_node_id)
273 continue;
274
276 {
277 ExplainOpenGroup("Elided Nodes", "Elided Nodes", false, es);
278 opened_elided_nodes = true;
279 }
280
281 switch (n->elided_type)
282 {
283 case T_Append:
284 elidednodetag = "Append";
285 break;
286 case T_MergeAppend:
287 elidednodetag = "MergeAppend";
288 break;
289 case T_SubqueryScan:
290 elidednodetag = "SubqueryScan";
291 break;
292 default:
293 elidednodetag = psprintf("%d", n->elided_type);
294 break;
295 }
296
297 ExplainOpenGroup("Elided Node", NULL, true, es);
298 ExplainPropertyText("Elided Node Type", elidednodetag, es);
299 overexplain_bitmapset("Elided Node RTIs", n->relids, es);
300 ExplainCloseGroup("Elided Node", NULL, true, es);
301 }
303 ExplainCloseGroup("Elided Nodes", "Elided Nodes", false, es);
304 }
305}
void ExplainPropertyInteger(const char *qlabel, const char *unit, int64 value, ExplainState *es)
void ExplainPropertyBool(const char *qlabel, bool value, ExplainState *es)
#define nodeTag(nodeptr)
Definition nodes.h:139
static void overexplain_bitmapset_list(const char *qlabel, List *bms_list, ExplainState *es)
#define plan(x)
Definition pg_regress.c:164
PlannedStmt * pstmt
Plan * plan
Definition execnodes.h:1201
List * elidedNodes
Definition plannodes.h:156

References bms_is_empty, PlannedStmt::elidedNodes, es_extension_id, EXPLAIN_FORMAT_TEXT, ExplainCloseGroup(), ExplainOpenGroup(), ExplainPropertyBool(), ExplainPropertyInteger(), ExplainPropertyText(), fb(), foreach_node, ExplainState::format, GetExplainExtensionState(), nodeTag, overexplain_bitmapset(), overexplain_bitmapset_list(), PlanState::plan, plan, prev_explain_per_node_hook, psprintf(), and ExplainState::pstmt.

Referenced by _PG_init().

◆ overexplain_per_plan_hook()

static void overexplain_per_plan_hook ( PlannedStmt plannedstmt,
IntoClause into,
ExplainState es,
const char queryString,
ParamListInfo  params,
QueryEnvironment queryEnv 
)
static

Definition at line 314 of file pg_overexplain.c.

320{
322
324 (*prev_explain_per_plan_hook) (plannedstmt, into, es, queryString,
325 params, queryEnv);
326
328 if (options == NULL)
329 return;
330
331 if (options->debug)
332 overexplain_debug(plannedstmt, es);
333
334 if (options->range_table)
335 overexplain_range_table(plannedstmt, es);
336}
static void overexplain_range_table(PlannedStmt *plannedstmt, ExplainState *es)
static void overexplain_debug(PlannedStmt *plannedstmt, ExplainState *es)

References es_extension_id, fb(), GetExplainExtensionState(), overexplain_debug(), overexplain_range_table(), and prev_explain_per_plan_hook.

Referenced by _PG_init().

◆ overexplain_range_table()

static void overexplain_range_table ( PlannedStmt plannedstmt,
ExplainState es 
)
static

Definition at line 445 of file pg_overexplain.c.

446{
447 Index rti;
448 ListCell *lc_subrtinfo = list_head(plannedstmt->subrtinfos);
450
451 /* Open group, one entry per RangeTblEntry */
452 ExplainOpenGroup("Range Table", "Range Table", false, es);
453
454 /* Iterate over the range table */
455 for (rti = 1; rti <= list_length(plannedstmt->rtable); ++rti)
456 {
457 RangeTblEntry *rte = rt_fetch(rti, plannedstmt->rtable);
458 char *kind = NULL;
459 char *relkind;
461
462 /* Advance to next SubRTInfo, if it's time. */
463 if (lc_subrtinfo != NULL)
464 {
466 if (rti > next_rtinfo->rtoffset)
467 {
469 lc_subrtinfo = lnext(plannedstmt->subrtinfos, lc_subrtinfo);
470 }
471 }
472
473 /* NULL entries are possible; skip them */
474 if (rte == NULL)
475 continue;
476
477 /* Translate rtekind to a string */
478 switch (rte->rtekind)
479 {
480 case RTE_RELATION:
481 kind = "relation";
482 break;
483 case RTE_SUBQUERY:
484 kind = "subquery";
485 break;
486 case RTE_JOIN:
487 kind = "join";
488 break;
489 case RTE_FUNCTION:
490 kind = "function";
491 break;
492 case RTE_TABLEFUNC:
493 kind = "tablefunc";
494 break;
495 case RTE_VALUES:
496 kind = "values";
497 break;
498 case RTE_CTE:
499 kind = "cte";
500 break;
502 kind = "namedtuplestore";
503 break;
504 case RTE_RESULT:
505 kind = "result";
506 break;
507 case RTE_GROUP:
508 kind = "group";
509 break;
510 case RTE_GRAPH_TABLE:
511
512 /*
513 * We should not see RTE of this kind here since property
514 * graph RTE gets converted to subquery RTE in
515 * RewriteGraphTable(). In case we decide not to do the
516 * conversion and leave RTEkind unchanged in future, print
517 * correct name of RTE kind.
518 */
519 kind = "graph_table";
520 break;
521 }
522
523 /* Begin group for this specific RTE */
524 ExplainOpenGroup("Range Table Entry", NULL, true, es);
525
526 /*
527 * In text format, the summary line displays the range table index and
528 * rtekind, plus indications if rte->inh and/or rte->inFromCl are set.
529 * In other formats, we display those as separate properties.
530 */
531 if (es->format == EXPLAIN_FORMAT_TEXT)
532 {
534 appendStringInfo(es->str, "RTI %u (%s%s%s):\n", rti, kind,
535 rte->inh ? ", inherited" : "",
536 rte->inFromCl ? ", in-from-clause" : "");
537 es->indent++;
538 }
539 else
540 {
541 ExplainPropertyUInteger("RTI", NULL, rti, es);
542 ExplainPropertyText("Kind", kind, es);
543 ExplainPropertyBool("Inherited", rte->inh, es);
544 ExplainPropertyBool("In From Clause", rte->inFromCl, es);
545 }
546
547 /*
548 * Indicate which subplan is the origin of which RTE. Note dummy
549 * subplans. Here again, we crunch more onto one line in text format.
550 */
551 if (rtinfo != NULL)
552 {
553 if (es->format == EXPLAIN_FORMAT_TEXT)
554 {
555 if (!rtinfo->dummy)
556 ExplainPropertyText("Subplan", rtinfo->plan_name, es);
557 else
558 ExplainPropertyText("Subplan",
559 psprintf("%s (dummy)",
560 rtinfo->plan_name), es);
561 }
562 else
563 {
564 ExplainPropertyText("Subplan", rtinfo->plan_name, es);
565 ExplainPropertyBool("Subplan Is Dummy", rtinfo->dummy, es);
566 }
567 }
568
569 /* rte->alias is optional; rte->eref is requested */
570 if (rte->alias != NULL)
571 overexplain_alias("Alias", rte->alias, es);
572 overexplain_alias("Eref", rte->eref, es);
573
574 /*
575 * We adhere to the usual EXPLAIN convention that schema names are
576 * displayed only in verbose mode, and we emit nothing if there is no
577 * relation OID.
578 */
579 if (rte->relid != 0)
580 {
581 const char *relname;
582 const char *qualname;
583
585
586 if (es->verbose)
587 {
589 char *nspname;
590
592 qualname = psprintf("%s.%s", quote_identifier(nspname),
593 relname);
594 }
595 else
597
598 ExplainPropertyText("Relation", qualname, es);
599 }
600
601 /* Translate relkind, if any, to a string */
602 switch (rte->relkind)
603 {
604 case RELKIND_RELATION:
605 relkind = "relation";
606 break;
607 case RELKIND_INDEX:
608 relkind = "index";
609 break;
610 case RELKIND_SEQUENCE:
611 relkind = "sequence";
612 break;
614 relkind = "toastvalue";
615 break;
616 case RELKIND_VIEW:
617 relkind = "view";
618 break;
619 case RELKIND_MATVIEW:
620 relkind = "matview";
621 break;
623 relkind = "composite_type";
624 break;
626 relkind = "foreign_table";
627 break;
629 relkind = "partitioned_table";
630 break;
632 relkind = "partitioned_index";
633 break;
635 relkind = "property_graph";
636 break;
637 case '\0':
638 relkind = NULL;
639 break;
640 default:
641 relkind = psprintf("%c", rte->relkind);
642 break;
643 }
644
645 /* If there is a relkind, show it */
646 if (relkind != NULL)
647 ExplainPropertyText("Relation Kind", relkind, es);
648
649 /* If there is a lock mode, show it */
650 if (rte->rellockmode != 0)
651 ExplainPropertyText("Relation Lock Mode",
653 rte->rellockmode), es);
654
655 /*
656 * If there is a perminfoindex, show it. We don't try to display
657 * information from the RTEPermissionInfo node here because they are
658 * just indexes plannedstmt->permInfos which could be separately
659 * dumped if someone wants to add EXPLAIN (PERMISSIONS) or similar.
660 */
661 if (rte->perminfoindex != 0)
662 ExplainPropertyInteger("Permission Info Index", NULL,
663 rte->perminfoindex, es);
664
665 /*
666 * add_rte_to_flat_rtable will clear rte->tablesample and
667 * rte->subquery in the finished plan, so skip those fields.
668 *
669 * However, the security_barrier flag is not shown by the core code,
670 * so let's print it here.
671 */
672 if (es->format != EXPLAIN_FORMAT_TEXT || rte->security_barrier)
673 ExplainPropertyBool("Security Barrier", rte->security_barrier, es);
674
675 /*
676 * If this is a join, print out the fields that are specifically valid
677 * for joins.
678 */
679 if (rte->rtekind == RTE_JOIN)
680 {
681 char *jointype;
682
683 switch (rte->jointype)
684 {
685 case JOIN_INNER:
686 jointype = "Inner";
687 break;
688 case JOIN_LEFT:
689 jointype = "Left";
690 break;
691 case JOIN_FULL:
692 jointype = "Full";
693 break;
694 case JOIN_RIGHT:
695 jointype = "Right";
696 break;
697 case JOIN_SEMI:
698 jointype = "Semi";
699 break;
700 case JOIN_ANTI:
701 jointype = "Anti";
702 break;
703 case JOIN_RIGHT_SEMI:
704 jointype = "Right Semi";
705 break;
706 case JOIN_RIGHT_ANTI:
707 jointype = "Right Anti";
708 break;
709 default:
710 jointype = "???";
711 break;
712 }
713
714 /* Join type */
715 ExplainPropertyText("Join Type", jointype, es);
716
717 /* # of JOIN USING columns */
718 if (es->format != EXPLAIN_FORMAT_TEXT || rte->joinmergedcols != 0)
719 ExplainPropertyInteger("JOIN USING Columns", NULL,
720 rte->joinmergedcols, es);
721
722 /*
723 * add_rte_to_flat_rtable will clear joinaliasvars, joinleftcols,
724 * joinrightcols, and join_using_alias here, so skip those fields.
725 */
726 }
727
728 /*
729 * add_rte_to_flat_rtable will clear functions, tablefunc, and
730 * values_lists, but we can display funcordinality.
731 */
732 if (rte->rtekind == RTE_FUNCTION)
733 ExplainPropertyBool("WITH ORDINALITY", rte->funcordinality, es);
734
735 /*
736 * If this is a CTE, print out CTE-related properties.
737 */
738 if (rte->rtekind == RTE_CTE)
739 {
740 ExplainPropertyText("CTE Name", rte->ctename, es);
741 ExplainPropertyUInteger("CTE Levels Up", NULL, rte->ctelevelsup,
742 es);
743 ExplainPropertyBool("CTE Self-Reference", rte->self_reference, es);
744 }
745
746 /*
747 * add_rte_to_flat_rtable will clear coltypes, coltypmods, and
748 * colcollations, so skip those fields.
749 *
750 * If this is an ephemeral named relation, print out ENR-related
751 * properties.
752 */
753 if (rte->rtekind == RTE_NAMEDTUPLESTORE)
754 {
755 ExplainPropertyText("ENR Name", rte->enrname, es);
756 ExplainPropertyFloat("ENR Tuples", NULL, rte->enrtuples, 0, es);
757 }
758
759 /*
760 * rewriteGraphTable() clears graph_pattern and graph_table_columns
761 * fields, so skip them. No graph table specific fields are required
762 * to be printed.
763 */
764
765 /*
766 * add_rte_to_flat_rtable will clear groupexprs and securityQuals, so
767 * skip that field. We have handled inFromCl above, so the only thing
768 * left to handle here is rte->lateral.
769 */
770 if (es->format != EXPLAIN_FORMAT_TEXT || rte->lateral)
771 ExplainPropertyBool("Lateral", rte->lateral, es);
772
773 /* Done with this RTE */
774 if (es->format == EXPLAIN_FORMAT_TEXT)
775 es->indent--;
776 ExplainCloseGroup("Range Table Entry", NULL, true, es);
777 }
778
779 /* Close the Range Table array before emitting PlannedStmt-level fields. */
780 ExplainCloseGroup("Range Table", "Range Table", false, es);
781
782 /*
783 * Print PlannedStmt fields that contain RTIs. These are properties of
784 * the PlannedStmt, not of individual RTEs, so they belong outside the
785 * Range Table array.
786 */
787 if (es->format != EXPLAIN_FORMAT_TEXT ||
788 !bms_is_empty(plannedstmt->unprunableRelids))
789 overexplain_bitmapset("Unprunable RTIs", plannedstmt->unprunableRelids,
790 es);
791 if (es->format != EXPLAIN_FORMAT_TEXT ||
792 !bms_is_empty(plannedstmt->resultRelationRelids))
793 overexplain_bitmapset("Result RTIs", plannedstmt->resultRelationRelids,
794 es);
795}
unsigned int Index
Definition c.h:698
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)
const char * GetLockmodeName(LOCKMETHODID lockmethodid, LOCKMODE mode)
Definition lock.c:4236
#define DEFAULT_LOCKMETHOD
Definition locktag.h:25
char * get_rel_name(Oid relid)
Definition lsyscache.c:2121
Oid get_rel_namespace(Oid relid)
Definition lsyscache.c:2145
char * get_namespace_name_or_temp(Oid nspid)
Definition lsyscache.c:3585
@ JOIN_SEMI
Definition nodes.h:317
@ JOIN_FULL
Definition nodes.h:305
@ JOIN_INNER
Definition nodes.h:303
@ JOIN_RIGHT
Definition nodes.h:306
@ JOIN_RIGHT_SEMI
Definition nodes.h:319
@ JOIN_LEFT
Definition nodes.h:304
@ JOIN_RIGHT_ANTI
Definition nodes.h:320
@ JOIN_ANTI
Definition nodes.h:318
@ RTE_JOIN
@ RTE_CTE
@ RTE_NAMEDTUPLESTORE
@ RTE_VALUES
@ RTE_SUBQUERY
@ RTE_RESULT
@ RTE_FUNCTION
@ RTE_TABLEFUNC
@ RTE_GROUP
@ RTE_GRAPH_TABLE
@ RTE_RELATION
#define rt_fetch(rangetable_index, rangetable)
Definition parsetree.h:31
NameData relname
Definition pg_class.h:40
#define lfirst(lc)
Definition pg_list.h:172
static int list_length(const List *l)
Definition pg_list.h:152
static ListCell * list_head(const List *l)
Definition pg_list.h:128
static ListCell * lnext(const List *l, const ListCell *c)
Definition pg_list.h:375
static void overexplain_alias(const char *qlabel, Alias *alias, ExplainState *es)
unsigned int Oid
Bitmapset * resultRelationRelids
Definition plannodes.h:121
List * subrtinfos
Definition plannodes.h:132
Bitmapset * unprunableRelids
Definition plannodes.h:113
List * rtable
Definition plannodes.h:107

References appendStringInfo(), bms_is_empty, DEFAULT_LOCKMETHOD, EXPLAIN_FORMAT_TEXT, ExplainCloseGroup(), ExplainIndentText(), ExplainOpenGroup(), ExplainPropertyBool(), ExplainPropertyFloat(), ExplainPropertyInteger(), ExplainPropertyText(), ExplainPropertyUInteger(), fb(), ExplainState::format, get_namespace_name_or_temp(), get_rel_name(), get_rel_namespace(), GetLockmodeName(), ExplainState::indent, JOIN_ANTI, JOIN_FULL, JOIN_INNER, JOIN_LEFT, JOIN_RIGHT, JOIN_RIGHT_ANTI, JOIN_RIGHT_SEMI, JOIN_SEMI, lfirst, list_head(), list_length(), lnext(), overexplain_alias(), overexplain_bitmapset(), psprintf(), quote_identifier(), relname, PlannedStmt::resultRelationRelids, rt_fetch, PlannedStmt::rtable, RTE_CTE, RTE_FUNCTION, RTE_GRAPH_TABLE, RTE_GROUP, RTE_JOIN, RTE_NAMEDTUPLESTORE, RTE_RELATION, RTE_RESULT, RTE_SUBQUERY, RTE_TABLEFUNC, RTE_VALUES, ExplainState::str, PlannedStmt::subrtinfos, PlannedStmt::unprunableRelids, and ExplainState::verbose.

Referenced by overexplain_per_plan_hook().

◆ overexplain_range_table_handler()

static void overexplain_range_table_handler ( ExplainState es,
DefElem opt,
ParseState pstate 
)
static

Definition at line 124 of file pg_overexplain.c.

126{
128
129 options->range_table = defGetBoolean(opt);
130}

References defGetBoolean(), and overexplain_ensure_options().

Referenced by _PG_init().

◆ PG_MODULE_MAGIC_EXT()

PG_MODULE_MAGIC_EXT ( name = "pg_overexplain",
version = PG_VERSION 
)

Variable Documentation

◆ es_extension_id

int es_extension_id
static

◆ prev_explain_per_node_hook

explain_per_node_hook_type prev_explain_per_node_hook
static

Definition at line 63 of file pg_overexplain.c.

Referenced by _PG_init(), and overexplain_per_node_hook().

◆ prev_explain_per_plan_hook

explain_per_plan_hook_type prev_explain_per_plan_hook
static

Definition at line 64 of file pg_overexplain.c.

Referenced by _PG_init(), and overexplain_per_plan_hook().