Skip to content

Commit c5e4e91

Browse files
committed
Add some code to EXPLAIN to show the targetlist (ie, output columns)
of each plan node. For the moment this is debug support only and is not enabled unless EXPLAIN_PRINT_TLISTS is defined at build time. Later I'll see about the idea of letting EXPLAIN VERBOSE do it.
1 parent d1cbd26 commit c5e4e91

File tree

1 file changed

+57
-2
lines changed

1 file changed

+57
-2
lines changed

src/backend/commands/explain.c

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1994-5, Regents of the University of California
88
*
99
* IDENTIFICATION
10-
* $PostgreSQL: pgsql/src/backend/commands/explain.c,v 1.171 2008/03/26 18:48:59 alvherre Exp $
10+
* $PostgreSQL: pgsql/src/backend/commands/explain.c,v 1.172 2008/04/17 18:30:18 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -61,6 +61,8 @@ static void explain_outNode(StringInfo str,
6161
Plan *plan, PlanState *planstate,
6262
Plan *outer_plan,
6363
int indent, ExplainState *es);
64+
static void show_plan_tlist(Plan *plan,
65+
StringInfo str, int indent, ExplainState *es);
6466
static void show_scan_qual(List *qual, const char *qlabel,
6567
int scanrelid, Plan *outer_plan, Plan *inner_plan,
6668
StringInfo str, int indent, ExplainState *es);
@@ -443,7 +445,7 @@ explain_outNode(StringInfo str,
443445
Plan *outer_plan,
444446
int indent, ExplainState *es)
445447
{
446-
char *pname;
448+
const char *pname;
447449
int i;
448450

449451
if (plan == NULL)
@@ -744,6 +746,9 @@ explain_outNode(StringInfo str,
744746
appendStringInfo(str, " (never executed)");
745747
appendStringInfoChar(str, '\n');
746748

749+
/* target list */
750+
show_plan_tlist(plan, str, indent, es);
751+
747752
/* quals, sort keys, etc */
748753
switch (nodeTag(plan))
749754
{
@@ -1043,6 +1048,56 @@ explain_outNode(StringInfo str,
10431048
}
10441049
}
10451050

1051+
/*
1052+
* Show the targetlist of a plan node
1053+
*/
1054+
static void
1055+
show_plan_tlist(Plan *plan,
1056+
StringInfo str, int indent, ExplainState *es)
1057+
{
1058+
#ifdef EXPLAIN_PRINT_TLISTS
1059+
List *context;
1060+
bool useprefix;
1061+
ListCell *lc;
1062+
int i;
1063+
1064+
/* No work if empty tlist (this occurs eg in bitmap indexscans) */
1065+
if (plan->targetlist == NIL)
1066+
return;
1067+
/* The tlist of an Append isn't real helpful, so suppress it */
1068+
if (IsA(plan, Append))
1069+
return;
1070+
1071+
/* Set up deparsing context */
1072+
context = deparse_context_for_plan((Node *) outerPlan(plan),
1073+
(Node *) innerPlan(plan),
1074+
es->rtable);
1075+
useprefix = list_length(es->rtable) > 1;
1076+
1077+
/* Emit line prefix */
1078+
for (i = 0; i < indent; i++)
1079+
appendStringInfo(str, " ");
1080+
appendStringInfo(str, " Output: ");
1081+
1082+
/* Deparse each non-junk result column */
1083+
i = 0;
1084+
foreach(lc, plan->targetlist)
1085+
{
1086+
TargetEntry *tle = (TargetEntry *) lfirst(lc);
1087+
1088+
if (tle->resjunk)
1089+
continue;
1090+
if (i++ > 0)
1091+
appendStringInfo(str, ", ");
1092+
appendStringInfoString(str,
1093+
deparse_expression((Node *) tle->expr, context,
1094+
useprefix, false));
1095+
}
1096+
1097+
appendStringInfoChar(str, '\n');
1098+
#endif /* EXPLAIN_PRINT_TLISTS */
1099+
}
1100+
10461101
/*
10471102
* Show a qualifier expression for a scan plan node
10481103
*

0 commit comments

Comments
 (0)