Line data Source code
1 : /*-------------------------------------------------------------------------
2 : *
3 : * pg_overexplain.c
4 : * allow EXPLAIN to dump even more details
5 : *
6 : * Copyright (c) 2016-2025, PostgreSQL Global Development Group
7 : *
8 : * contrib/pg_overexplain/pg_overexplain.c
9 : *-------------------------------------------------------------------------
10 : */
11 : #include "postgres.h"
12 :
13 : #include "catalog/pg_class.h"
14 : #include "commands/defrem.h"
15 : #include "commands/explain.h"
16 : #include "commands/explain_format.h"
17 : #include "commands/explain_state.h"
18 : #include "fmgr.h"
19 : #include "parser/parsetree.h"
20 : #include "storage/lock.h"
21 : #include "utils/builtins.h"
22 : #include "utils/lsyscache.h"
23 :
24 2 : PG_MODULE_MAGIC_EXT(
25 : .name = "pg_overexplain",
26 : .version = PG_VERSION
27 : );
28 :
29 : typedef struct
30 : {
31 : bool debug;
32 : bool range_table;
33 : } overexplain_options;
34 :
35 : static overexplain_options *overexplain_ensure_options(ExplainState *es);
36 : static void overexplain_debug_handler(ExplainState *es, DefElem *opt,
37 : ParseState *pstate);
38 : static void overexplain_range_table_handler(ExplainState *es, DefElem *opt,
39 : ParseState *pstate);
40 : static void overexplain_per_node_hook(PlanState *planstate, List *ancestors,
41 : const char *relationship,
42 : const char *plan_name,
43 : ExplainState *es);
44 : static void overexplain_per_plan_hook(PlannedStmt *plannedstmt,
45 : IntoClause *into,
46 : ExplainState *es,
47 : const char *queryString,
48 : ParamListInfo params,
49 : QueryEnvironment *queryEnv);
50 : static void overexplain_debug(PlannedStmt *plannedstmt, ExplainState *es);
51 : static void overexplain_range_table(PlannedStmt *plannedstmt,
52 : ExplainState *es);
53 : static void overexplain_alias(const char *qlabel, Alias *alias,
54 : ExplainState *es);
55 : static void overexplain_bitmapset(const char *qlabel, Bitmapset *bms,
56 : ExplainState *es);
57 : static void overexplain_intlist(const char *qlabel, List *intlist,
58 : ExplainState *es);
59 :
60 : static int es_extension_id;
61 : static explain_per_node_hook_type prev_explain_per_node_hook;
62 : static explain_per_plan_hook_type prev_explain_per_plan_hook;
63 :
64 : /*
65 : * Initialization we do when this module is loaded.
66 : */
67 : void
68 2 : _PG_init(void)
69 : {
70 : /* Get an ID that we can use to cache data in an ExplainState. */
71 2 : es_extension_id = GetExplainExtensionId("pg_overexplain");
72 :
73 : /* Register the new EXPLAIN options implemented by this module. */
74 2 : RegisterExtensionExplainOption("debug", overexplain_debug_handler);
75 2 : RegisterExtensionExplainOption("range_table",
76 : overexplain_range_table_handler);
77 :
78 : /* Use the per-node and per-plan hooks to make our options do something. */
79 2 : prev_explain_per_node_hook = explain_per_node_hook;
80 2 : explain_per_node_hook = overexplain_per_node_hook;
81 2 : prev_explain_per_plan_hook = explain_per_plan_hook;
82 2 : explain_per_plan_hook = overexplain_per_plan_hook;
83 2 : }
84 :
85 : /*
86 : * Get the overexplain_options structure from an ExplainState; if there is
87 : * none, create one, attach it to the ExplainState, and return it.
88 : */
89 : static overexplain_options *
90 22 : overexplain_ensure_options(ExplainState *es)
91 : {
92 : overexplain_options *options;
93 :
94 22 : options = GetExplainExtensionState(es, es_extension_id);
95 :
96 22 : if (options == NULL)
97 : {
98 18 : options = palloc0(sizeof(overexplain_options));
99 18 : SetExplainExtensionState(es, es_extension_id, options);
100 : }
101 :
102 22 : return options;
103 : }
104 :
105 : /*
106 : * Parse handler for EXPLAIN (DEBUG).
107 : */
108 : static void
109 12 : overexplain_debug_handler(ExplainState *es, DefElem *opt, ParseState *pstate)
110 : {
111 12 : overexplain_options *options = overexplain_ensure_options(es);
112 :
113 12 : options->debug = defGetBoolean(opt);
114 12 : }
115 :
116 : /*
117 : * Parse handler for EXPLAIN (RANGE_TABLE).
118 : */
119 : static void
120 10 : overexplain_range_table_handler(ExplainState *es, DefElem *opt,
121 : ParseState *pstate)
122 : {
123 10 : overexplain_options *options = overexplain_ensure_options(es);
124 :
125 10 : options->range_table = defGetBoolean(opt);
126 10 : }
127 :
128 : /*
129 : * Print out additional per-node information as appropriate. If the user didn't
130 : * specify any of the options we support, do nothing; else, print whatever is
131 : * relevant to the specified options.
132 : */
133 : static void
134 60 : overexplain_per_node_hook(PlanState *planstate, List *ancestors,
135 : const char *relationship, const char *plan_name,
136 : ExplainState *es)
137 : {
138 : overexplain_options *options;
139 60 : Plan *plan = planstate->plan;
140 :
141 60 : if (prev_explain_per_node_hook)
142 0 : (*prev_explain_per_node_hook) (planstate, ancestors, relationship,
143 : plan_name, es);
144 :
145 60 : options = GetExplainExtensionState(es, es_extension_id);
146 60 : if (options == NULL)
147 0 : return;
148 :
149 : /*
150 : * If the "debug" option was given, display miscellaneous fields from the
151 : * "Plan" node that would not otherwise be displayed.
152 : */
153 60 : if (options->debug)
154 : {
155 : /*
156 : * Normal EXPLAIN will display "Disabled: true" if the node is
157 : * disabled; but that is based on noticing that plan->disabled_nodes
158 : * is higher than the sum of its children; here, we display the raw
159 : * value, for debugging purposes.
160 : */
161 52 : ExplainPropertyInteger("Disabled Nodes", NULL, plan->disabled_nodes,
162 : es);
163 :
164 : /*
165 : * Normal EXPLAIN will display the parallel_aware flag; here, we show
166 : * the parallel_safe flag as well.
167 : */
168 52 : ExplainPropertyBool("Parallel Safe", plan->parallel_safe, es);
169 :
170 : /*
171 : * The plan node ID isn't normally displayed, since it is only useful
172 : * for debugging.
173 : */
174 52 : ExplainPropertyInteger("Plan Node ID", NULL, plan->plan_node_id, es);
175 :
176 : /*
177 : * It is difficult to explain what extParam and allParam mean in plain
178 : * language, so we simply display these fields labelled with the
179 : * structure member name. For compactness, the text format omits the
180 : * display of this information when the bitmapset is empty.
181 : */
182 52 : if (es->format != EXPLAIN_FORMAT_TEXT || !bms_is_empty(plan->extParam))
183 16 : overexplain_bitmapset("extParam", plan->extParam, es);
184 52 : if (es->format != EXPLAIN_FORMAT_TEXT || !bms_is_empty(plan->allParam))
185 16 : overexplain_bitmapset("allParam", plan->allParam, es);
186 : }
187 :
188 : /*
189 : * If the "range_table" option was specified, display information about
190 : * the range table indexes for this node.
191 : */
192 60 : if (options->range_table)
193 : {
194 28 : switch (nodeTag(plan))
195 : {
196 10 : case T_SeqScan:
197 : case T_SampleScan:
198 : case T_IndexScan:
199 : case T_IndexOnlyScan:
200 : case T_BitmapHeapScan:
201 : case T_TidScan:
202 : case T_TidRangeScan:
203 : case T_SubqueryScan:
204 : case T_FunctionScan:
205 : case T_TableFuncScan:
206 : case T_ValuesScan:
207 : case T_CteScan:
208 : case T_NamedTuplestoreScan:
209 : case T_WorkTableScan:
210 10 : ExplainPropertyInteger("Scan RTI", NULL,
211 10 : ((Scan *) plan)->scanrelid, es);
212 10 : break;
213 0 : case T_ForeignScan:
214 0 : overexplain_bitmapset("Scan RTIs",
215 : ((ForeignScan *) plan)->fs_base_relids,
216 : es);
217 0 : break;
218 0 : case T_CustomScan:
219 0 : overexplain_bitmapset("Scan RTIs",
220 : ((CustomScan *) plan)->custom_relids,
221 : es);
222 0 : break;
223 2 : case T_ModifyTable:
224 2 : ExplainPropertyInteger("Nominal RTI", NULL,
225 2 : ((ModifyTable *) plan)->nominalRelation, es);
226 2 : ExplainPropertyInteger("Exclude Relation RTI", NULL,
227 2 : ((ModifyTable *) plan)->exclRelRTI, es);
228 2 : break;
229 4 : case T_Append:
230 4 : overexplain_bitmapset("Append RTIs",
231 : ((Append *) plan)->apprelids,
232 : es);
233 4 : break;
234 0 : case T_MergeAppend:
235 0 : overexplain_bitmapset("Append RTIs",
236 : ((MergeAppend *) plan)->apprelids,
237 : es);
238 0 : break;
239 12 : default:
240 12 : break;
241 : }
242 : }
243 : }
244 :
245 : /*
246 : * Print out additional per-query information as appropriate. Here again, if
247 : * the user didn't specify any of the options implemented by this module, do
248 : * nothing; otherwise, call the appropriate function for each specified
249 : * option.
250 : */
251 : static void
252 18 : overexplain_per_plan_hook(PlannedStmt *plannedstmt,
253 : IntoClause *into,
254 : ExplainState *es,
255 : const char *queryString,
256 : ParamListInfo params,
257 : QueryEnvironment *queryEnv)
258 : {
259 : overexplain_options *options;
260 :
261 18 : if (prev_explain_per_plan_hook)
262 0 : (*prev_explain_per_plan_hook) (plannedstmt, into, es, queryString,
263 : params, queryEnv);
264 :
265 18 : options = GetExplainExtensionState(es, es_extension_id);
266 18 : if (options == NULL)
267 0 : return;
268 :
269 18 : if (options->debug)
270 12 : overexplain_debug(plannedstmt, es);
271 :
272 18 : if (options->range_table)
273 10 : overexplain_range_table(plannedstmt, es);
274 : }
275 :
276 : /*
277 : * Print out various details from the PlannedStmt that wouldn't otherwise
278 : * be displayed.
279 : *
280 : * We don't try to print everything here. Information that would be displyed
281 : * anyway doesn't need to be printed again here, and things with lots of
282 : * substructure probably should be printed via separate options, or not at all.
283 : */
284 : static void
285 12 : overexplain_debug(PlannedStmt *plannedstmt, ExplainState *es)
286 : {
287 12 : char *commandType = NULL;
288 : StringInfoData flags;
289 :
290 : /* Even in text mode, we want to set this output apart as its own group. */
291 12 : ExplainOpenGroup("PlannedStmt", "PlannedStmt", true, es);
292 12 : if (es->format == EXPLAIN_FORMAT_TEXT)
293 : {
294 10 : ExplainIndentText(es);
295 10 : appendStringInfo(es->str, "PlannedStmt:\n");
296 10 : es->indent++;
297 : }
298 :
299 : /* Print the command type. */
300 12 : switch (plannedstmt->commandType)
301 : {
302 0 : case CMD_UNKNOWN:
303 0 : commandType = "unknown";
304 0 : break;
305 10 : case CMD_SELECT:
306 10 : commandType = "select";
307 10 : break;
308 0 : case CMD_UPDATE:
309 0 : commandType = "update";
310 0 : break;
311 2 : case CMD_INSERT:
312 2 : commandType = "insert";
313 2 : break;
314 0 : case CMD_DELETE:
315 0 : commandType = "delete";
316 0 : break;
317 0 : case CMD_MERGE:
318 0 : commandType = "merge";
319 0 : break;
320 0 : case CMD_UTILITY:
321 0 : commandType = "utility";
322 0 : break;
323 0 : case CMD_NOTHING:
324 0 : commandType = "nothing";
325 0 : break;
326 : }
327 12 : ExplainPropertyText("Command Type", commandType, es);
328 :
329 : /* Print various properties as a comma-separated list of flags. */
330 12 : initStringInfo(&flags);
331 12 : if (plannedstmt->hasReturning)
332 2 : appendStringInfo(&flags, ", hasReturning");
333 12 : if (plannedstmt->hasModifyingCTE)
334 0 : appendStringInfo(&flags, ", hasModifyingCTE");
335 12 : if (plannedstmt->canSetTag)
336 12 : appendStringInfo(&flags, ", canSetTag");
337 12 : if (plannedstmt->transientPlan)
338 0 : appendStringInfo(&flags, ", transientPlan");
339 12 : if (plannedstmt->dependsOnRole)
340 0 : appendStringInfo(&flags, ", dependsOnRole");
341 12 : if (plannedstmt->parallelModeNeeded)
342 2 : appendStringInfo(&flags, ", parallelModeNeeded");
343 12 : if (flags.len == 0)
344 0 : appendStringInfo(&flags, ", none");
345 12 : ExplainPropertyText("Flags", flags.data + 2, es);
346 :
347 : /* Various lists of integers. */
348 12 : overexplain_bitmapset("Subplans Needing Rewind",
349 : plannedstmt->rewindPlanIDs, es);
350 12 : overexplain_intlist("Relation OIDs",
351 : plannedstmt->relationOids, es);
352 12 : overexplain_intlist("Executor Parameter Types",
353 : plannedstmt->paramExecTypes, es);
354 :
355 : /*
356 : * Print the statement location. (If desired, we could alternatively print
357 : * stmt_location and stmt_len as two separate fields.)
358 : */
359 12 : if (plannedstmt->stmt_location == -1)
360 0 : ExplainPropertyText("Parse Location", "Unknown", es);
361 12 : else if (plannedstmt->stmt_len == 0)
362 8 : ExplainPropertyText("Parse Location",
363 8 : psprintf("%d to end", plannedstmt->stmt_location),
364 : es);
365 : else
366 4 : ExplainPropertyText("Parse Location",
367 4 : psprintf("%d for %d bytes",
368 : plannedstmt->stmt_location,
369 : plannedstmt->stmt_len),
370 : es);
371 :
372 : /* Done with this group. */
373 12 : if (es->format == EXPLAIN_FORMAT_TEXT)
374 10 : es->indent--;
375 12 : ExplainCloseGroup("PlannedStmt", "PlannedStmt", true, es);
376 12 : }
377 :
378 : /*
379 : * Provide detailed information about the contents of the PlannedStmt's
380 : * range table.
381 : */
382 : static void
383 10 : overexplain_range_table(PlannedStmt *plannedstmt, ExplainState *es)
384 : {
385 : Index rti;
386 :
387 : /* Open group, one entry per RangeTblEntry */
388 10 : ExplainOpenGroup("Range Table", "Range Table", false, es);
389 :
390 : /* Iterate over the range table */
391 36 : for (rti = 1; rti <= list_length(plannedstmt->rtable); ++rti)
392 : {
393 26 : RangeTblEntry *rte = rt_fetch(rti, plannedstmt->rtable);
394 26 : char *kind = NULL;
395 : char *relkind;
396 :
397 : /* NULL entries are possible; skip them */
398 26 : if (rte == NULL)
399 0 : continue;
400 :
401 : /* Translate rtekind to a string */
402 26 : switch (rte->rtekind)
403 : {
404 18 : case RTE_RELATION:
405 18 : kind = "relation";
406 18 : break;
407 0 : case RTE_SUBQUERY:
408 0 : kind = "subquery";
409 0 : break;
410 0 : case RTE_JOIN:
411 0 : kind = "join";
412 0 : break;
413 0 : case RTE_FUNCTION:
414 0 : kind = "function";
415 0 : break;
416 0 : case RTE_TABLEFUNC:
417 0 : kind = "tablefunc";
418 0 : break;
419 0 : case RTE_VALUES:
420 0 : kind = "values";
421 0 : break;
422 0 : case RTE_CTE:
423 0 : kind = "cte";
424 0 : break;
425 0 : case RTE_NAMEDTUPLESTORE:
426 0 : kind = "namedtuplestore";
427 0 : break;
428 4 : case RTE_RESULT:
429 4 : kind = "result";
430 4 : break;
431 4 : case RTE_GROUP:
432 4 : kind = "group";
433 4 : break;
434 : }
435 :
436 : /* Begin group for this specific RTE */
437 26 : ExplainOpenGroup("Range Table Entry", NULL, true, es);
438 :
439 : /*
440 : * In text format, the summary line displays the range table index and
441 : * rtekind, plus indications if rte->inh and/or rte->inFromCl are set.
442 : * In other formats, we display those as separate properties.
443 : */
444 26 : if (es->format == EXPLAIN_FORMAT_TEXT)
445 : {
446 18 : ExplainIndentText(es);
447 36 : appendStringInfo(es->str, "RTI %u (%s%s%s):\n", rti, kind,
448 18 : rte->inh ? ", inherited" : "",
449 18 : rte->inFromCl ? ", in-from-clause" : "");
450 18 : es->indent++;
451 : }
452 : else
453 : {
454 8 : ExplainPropertyUInteger("RTI", NULL, rti, es);
455 8 : ExplainPropertyText("Kind", kind, es);
456 8 : ExplainPropertyBool("Inherited", rte->inh, es);
457 8 : ExplainPropertyBool("In From Clause", rte->inFromCl, es);
458 : }
459 :
460 : /* rte->alias is optional; rte->eref is requested */
461 26 : if (rte->alias != NULL)
462 10 : overexplain_alias("Alias", rte->alias, es);
463 26 : overexplain_alias("Eref", rte->eref, es);
464 :
465 : /*
466 : * We adhere to the usual EXPLAIN convention that schema names are
467 : * displayed only in verbose mode, and we emit nothing if there is no
468 : * relation OID.
469 : */
470 26 : if (rte->relid != 0)
471 : {
472 : const char *relname;
473 : const char *qualname;
474 :
475 18 : relname = quote_identifier(get_rel_name(rte->relid));
476 :
477 18 : if (es->verbose)
478 : {
479 0 : Oid nspoid = get_rel_namespace(rte->relid);
480 : char *nspname;
481 :
482 0 : nspname = get_namespace_name_or_temp(nspoid);
483 0 : qualname = psprintf("%s.%s", quote_identifier(nspname),
484 : relname);
485 : }
486 : else
487 18 : qualname = relname;
488 :
489 18 : ExplainPropertyText("Relation", qualname, es);
490 : }
491 :
492 : /* Translate relkind, if any, to a string */
493 26 : switch (rte->relkind)
494 : {
495 10 : case RELKIND_RELATION:
496 10 : relkind = "relation";
497 10 : break;
498 0 : case RELKIND_INDEX:
499 0 : relkind = "index";
500 0 : break;
501 0 : case RELKIND_SEQUENCE:
502 0 : relkind = "sequence";
503 0 : break;
504 0 : case RELKIND_TOASTVALUE:
505 0 : relkind = "toastvalue";
506 0 : break;
507 0 : case RELKIND_VIEW:
508 0 : relkind = "view";
509 0 : break;
510 0 : case RELKIND_MATVIEW:
511 0 : relkind = "matview";
512 0 : break;
513 0 : case RELKIND_COMPOSITE_TYPE:
514 0 : relkind = "composite_type";
515 0 : break;
516 0 : case RELKIND_FOREIGN_TABLE:
517 0 : relkind = "foreign_table";
518 0 : break;
519 8 : case RELKIND_PARTITIONED_TABLE:
520 8 : relkind = "parititioned_table";
521 8 : break;
522 0 : case RELKIND_PARTITIONED_INDEX:
523 0 : relkind = "parititioned_index";
524 0 : break;
525 8 : case '\0':
526 8 : relkind = NULL;
527 8 : break;
528 0 : default:
529 0 : relkind = psprintf("%c", rte->relkind);
530 0 : break;
531 : }
532 :
533 : /* If there is a relkind, show it */
534 26 : if (relkind != NULL)
535 18 : ExplainPropertyText("Relation Kind", relkind, es);
536 :
537 : /* If there is a lock mode, show it */
538 26 : if (rte->rellockmode != 0)
539 18 : ExplainPropertyText("Relation Lock Mode",
540 : GetLockmodeName(DEFAULT_LOCKMETHOD,
541 : rte->rellockmode), es);
542 :
543 : /*
544 : * If there is a perminfoindex, show it. We don't try to display
545 : * information from the RTEPermissionInfo node here because they are
546 : * just indexes plannedstmt->permInfos which could be separately
547 : * dumped if someone wants to add EXPLAIN (PERMISSIONS) or similar.
548 : */
549 26 : if (rte->perminfoindex != 0)
550 8 : ExplainPropertyInteger("Permission Info Index", NULL,
551 8 : rte->perminfoindex, es);
552 :
553 : /*
554 : * add_rte_to_flat_rtable will clear rte->tablesample and
555 : * rte->subquery in the finished plan, so skip those fields.
556 : *
557 : * However, the security_barrier flag is not shown by the core code,
558 : * so let's print it here.
559 : */
560 26 : if (es->format != EXPLAIN_FORMAT_TEXT || rte->security_barrier)
561 8 : ExplainPropertyBool("Security Barrier", rte->security_barrier, es);
562 :
563 : /*
564 : * If this is a join, print out the fields that are specifically valid
565 : * for joins.
566 : */
567 26 : if (rte->rtekind == RTE_JOIN)
568 : {
569 : char *jointype;
570 :
571 0 : switch (rte->jointype)
572 : {
573 0 : case JOIN_INNER:
574 0 : jointype = "Inner";
575 0 : break;
576 0 : case JOIN_LEFT:
577 0 : jointype = "Left";
578 0 : break;
579 0 : case JOIN_FULL:
580 0 : jointype = "Full";
581 0 : break;
582 0 : case JOIN_RIGHT:
583 0 : jointype = "Right";
584 0 : break;
585 0 : case JOIN_SEMI:
586 0 : jointype = "Semi";
587 0 : break;
588 0 : case JOIN_ANTI:
589 0 : jointype = "Anti";
590 0 : break;
591 0 : case JOIN_RIGHT_SEMI:
592 0 : jointype = "Right Semi";
593 0 : break;
594 0 : case JOIN_RIGHT_ANTI:
595 0 : jointype = "Right Anti";
596 0 : break;
597 0 : default:
598 0 : jointype = "???";
599 0 : break;
600 : }
601 :
602 : /* Join type */
603 0 : ExplainPropertyText("Join Type", jointype, es);
604 :
605 : /* # of JOIN USING columns */
606 0 : if (es->format != EXPLAIN_FORMAT_TEXT || rte->joinmergedcols != 0)
607 0 : ExplainPropertyInteger("JOIN USING Columns", NULL,
608 0 : rte->joinmergedcols, es);
609 :
610 : /*
611 : * add_rte_to_flat_rtable will clear joinaliasvars, joinleftcols,
612 : * joinrightcols, and join_using_alias here, so skip those fields.
613 : */
614 : }
615 :
616 : /*
617 : * add_rte_to_flat_rtable will clear functions, tablefunc, and
618 : * values_lists, but we can display funcordinality.
619 : */
620 26 : if (rte->rtekind == RTE_FUNCTION)
621 0 : ExplainPropertyBool("WITH ORDINALITY", rte->funcordinality, es);
622 :
623 : /*
624 : * If this is a CTE, print out CTE-related properties.
625 : */
626 26 : if (rte->rtekind == RTE_CTE)
627 : {
628 0 : ExplainPropertyText("CTE Name", rte->ctename, es);
629 0 : ExplainPropertyUInteger("CTE Levels Up", NULL, rte->ctelevelsup,
630 : es);
631 0 : ExplainPropertyBool("CTE Self-Reference", rte->self_reference, es);
632 : }
633 :
634 : /*
635 : * add_rte_to_flat_rtable will clear coltypes, coltypemods, and
636 : * colcollations, so skip those fields.
637 : *
638 : * If this is an ephemeral named relation, print out ENR-related
639 : * properties.
640 : */
641 26 : if (rte->rtekind == RTE_NAMEDTUPLESTORE)
642 : {
643 0 : ExplainPropertyText("ENR Name", rte->enrname, es);
644 0 : ExplainPropertyFloat("ENR Tuples", NULL, rte->enrtuples, 0, es);
645 : }
646 :
647 : /*
648 : * add_rte_to_flat_rtable will clear groupexprs and securityQuals, so
649 : * skip that field. We have handled inFromCl above, so the only thing
650 : * left to handle here is rte->lateral.
651 : */
652 26 : if (es->format != EXPLAIN_FORMAT_TEXT || rte->lateral)
653 8 : ExplainPropertyBool("Lateral", rte->lateral, es);
654 :
655 : /* Done with this RTE */
656 26 : if (es->format == EXPLAIN_FORMAT_TEXT)
657 18 : es->indent--;
658 26 : ExplainCloseGroup("Range Table Entry", NULL, true, es);
659 : }
660 :
661 : /* Print PlannedStmt fields that contain RTIs. */
662 10 : if (es->format != EXPLAIN_FORMAT_TEXT ||
663 8 : !bms_is_empty(plannedstmt->unprunableRelids))
664 8 : overexplain_bitmapset("Unprunable RTIs", plannedstmt->unprunableRelids,
665 : es);
666 10 : if (es->format != EXPLAIN_FORMAT_TEXT ||
667 8 : plannedstmt->resultRelations != NIL)
668 4 : overexplain_intlist("Result RTIs", plannedstmt->resultRelations, es);
669 :
670 : /* Close group, we're all done */
671 10 : ExplainCloseGroup("Range Table", "Range Table", false, es);
672 10 : }
673 :
674 : /*
675 : * Emit a text property describing the contents of an Alias.
676 : *
677 : * Column lists can be quite long here, so perhaps we should have an option
678 : * to limit the display length by # of columsn or # of characters, but for
679 : * now, just display everything.
680 : */
681 : static void
682 36 : overexplain_alias(const char *qlabel, Alias *alias, ExplainState *es)
683 : {
684 : StringInfoData buf;
685 36 : bool first = true;
686 :
687 : Assert(alias != NULL);
688 :
689 36 : initStringInfo(&buf);
690 36 : appendStringInfo(&buf, "%s (", quote_identifier(alias->aliasname));
691 :
692 160 : foreach_node(String, cn, alias->colnames)
693 : {
694 88 : appendStringInfo(&buf, "%s%s",
695 : first ? "" : ", ",
696 88 : quote_identifier(cn->sval));
697 88 : first = false;
698 : }
699 :
700 36 : appendStringInfoChar(&buf, ')');
701 36 : ExplainPropertyText(qlabel, buf.data, es);
702 36 : pfree(buf.data);
703 36 : }
704 :
705 : /*
706 : * Emit a text property describing the contents of a bitmapset -- either a
707 : * space-separated list of integer members, or the word "none" if the bitmapset
708 : * is empty.
709 : */
710 : static void
711 56 : overexplain_bitmapset(const char *qlabel, Bitmapset *bms, ExplainState *es)
712 : {
713 56 : int x = -1;
714 :
715 : StringInfoData buf;
716 :
717 56 : if (bms_is_empty(bms))
718 : {
719 32 : ExplainPropertyText(qlabel, "none", es);
720 32 : return;
721 : }
722 :
723 24 : initStringInfo(&buf);
724 58 : while ((x = bms_next_member(bms, x)) >= 0)
725 34 : appendStringInfo(&buf, " %d", x);
726 : Assert(buf.data[0] == ' ');
727 24 : ExplainPropertyText(qlabel, buf.data + 1, es);
728 24 : pfree(buf.data);
729 : }
730 :
731 : /*
732 : * Emit a text property describing the contents of a list of integers, OIDs,
733 : * or XIDs -- either a space-separated list of integer members, or the word
734 : * "none" if the list is empty.
735 : */
736 : static void
737 28 : overexplain_intlist(const char *qlabel, List *list, ExplainState *es)
738 : {
739 : StringInfoData buf;
740 :
741 28 : initStringInfo(&buf);
742 :
743 28 : if (list == NIL)
744 : {
745 12 : ExplainPropertyText(qlabel, "none", es);
746 12 : return;
747 : }
748 :
749 16 : if (IsA(list, IntList))
750 : {
751 6 : foreach_int(i, list)
752 2 : appendStringInfo(&buf, " %d", i);
753 : }
754 14 : else if (IsA(list, OidList))
755 : {
756 66 : foreach_oid(o, list)
757 38 : appendStringInfo(&buf, " %u", o);
758 : }
759 0 : else if (IsA(list, XidList))
760 : {
761 0 : foreach_xid(x, list)
762 0 : appendStringInfo(&buf, " %u", x);
763 : }
764 : else
765 : {
766 0 : appendStringInfo(&buf, " not an integer list");
767 : Assert(false);
768 : }
769 :
770 16 : if (buf.len > 0)
771 16 : ExplainPropertyText(qlabel, buf.data + 1, es);
772 :
773 16 : pfree(buf.data);
774 : }
|