German sent this some time ago and it never reached the list. He sent
it again from Gmail but again it was lost in the void.
I am forwarding it to improve the chances of it being delivered ... The
patch in the fwd is not a nice MIME part but it should work without
problem anyway.
----- Forwarded message from Germán Poó Caamaño -----
From: Germán Poó Caamaño
To: Magnus Hagander
Cc: Alvaro Herrera ,
Dave Page ,
PostgreSQL-development
Date: Thu, 14 Jun 2007 17:53:37 -0400
Subject: Re: [HACKERS] EXPLAIN omits schema?
X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.1.5
On Wed, 2007-06-13 at 14:49 +0200, Magnus Hagander wrote:
> On Wed, Jun 13, 2007 at 08:47:30AM -0400, Alvaro Herrera wrote:
> > Magnus Hagander wrote:
> >
> > > Just to open a whole new can of worms ;-)
> > >
> > > I read an article a couple of days ago about the "machine readable showplan
> > > output" in SQL Server 2005 (basically, it's EXPLAIN output but in XML
> > > format). It does make a lot of sense if yourp rimary interface is !=
> > > commandline (psql), such as pgadmin or phppgadmin. The idea being that you
> > > can stick in *all* the details you want, since you can't possibly clutter
> > > up the display. And you stick them in a well-defined XML format (or another
> > > format if you happen to hate XML) where the client-side program can easily
> > > parse out whatever it needs. It's also future-proof - if you add a new
> > > field somewhere, the client program parser won't break.
> > >
> > > Something worth doing? Not to replace the current explain output, but as a
> > > second option (EXPLAIN XML whatever)?
> >
> > FYI a patch was posted for this some time ago, because a friend of mine
> > wanted to help a student to write an EXPLAIN parsing tool.
>
> Didn't see that one. Explain in XML format? Got an URL for it, I can't seem
> to find it on -patches.
I never send it, sorry. It was made against 8.0 beta. By the time of
the message, I was told that pgAdmin folks were working on parser the
text output. Hence, I thought it was useless after all.
Anyway, I made a break and I have the patch working against CVS HEAD.
Please note this is a 3 years old patch. Some stuff are missing, such
as show_sort_info, but it should be easy to add it.
By the time this code was written, the 'XML' token didn't exists.
Today, when I made the merge, I noted there is a XML_P. So, I
touched keywords "xml". I hope I'm not break anything.
Any comments are welcomed. Even if this patch is a total crap.
PS: I owe you the DTD.
--
Germán Poó Caamaño
Concepción - Chile
*** /dev/fd/63 2007-06-14 17:40:26.478023384 -0400
--- src/backend/commands/explain.c 2007-06-14 17:23:29.280056575 -0400
*************** typedef struct ExplainState
*** 49,54 ****
--- 49,61 ----
List *rtable; /* range table */
} ExplainState;
+ typedef struct ExplainXML
+ {
+ /* options */
+ StringInfo str;
+ int level; /* level of childs */
+ } ExplainXML;
+
static void ExplainOneQuery(Query *query, ExplainStmt *stmt,
const char *queryString,
ParamListInfo params, TupOutputState *tstate);
*************** static double elapsed_time(instr_time *s
*** 56,72 ****
static void explain_outNode(StringInfo str,
Plan *plan, PlanState *planstate,
Plan *outer_plan,
! int indent, ExplainState *es);
static void show_scan_qual(List *qual, const char *qlabel,
int scanrelid, Plan *outer_plan, Plan *inner_plan,
! StringInfo str, int indent, ExplainState *es);
static void show_upper_qual(List *qual, const char *qlabel, Plan *plan,
! StringInfo str, int indent, ExplainState *es);
static void show_sort_keys(Plan *sortplan, int nkeys, AttrNumber *keycols,
const char *qlabel,
! StringInfo str, int indent, ExplainState *es);
static void show_sort_info(SortState *sortstate,
! StringInfo str, int indent, ExplainState *es);
static const char *explain_get_index_name(Oid indexId);
--- 63,79 ----
static void explain_outNode(StringInfo str,
Plan *plan, PlanState *planstate,
Plan *outer_plan,
! int indent, ExplainState *es, ExplainXML *exml);
static void show_scan_qual(List *qual, const char *qlabel,
int scanrelid, Plan *outer_plan, Plan *inner_plan,
! StringInfo str, int indent, ExplainState *es, ExplainXML *exml);
static void show_upper_qual(List *qual, const char *qlabel, Plan *plan,
! StringInfo str, int indent, ExplainState *es, ExplainXML *exml);
static void show_sort_keys(Plan *sortplan, int nkeys, AttrNumber *keycols,
const char *qlabel,
! StringInfo str, int indent, ExplainState *es, ExplainXML *exml);
static void show_sort_info(SortState *sortstate,
! StringInfo str, int indent, ExplainState *es, ExplainXML *exml);
static const char *explain_get_index_name(Oid indexId);
*************** ExplainOnePlan(PlannedStmt *plannedstmt,
*** 222,227 ****
--- 229,235 ----
ExplainState *es;
StringInfoData buf;
int eflags;
+ ExplainXML *exml;
/*
* Update snapshot command ID to ensure this query sees results of any
*************** ExplainOnePlan(PlannedStmt *plannedstmt,
*** 263,268 ****
--- 271,282 ----
totaltime += elapsed_time(&starttime);
}
+ exml = (ExplainXML *) palloc0(sizeof(ExplainXML));
+
+ exml->str = makeStringInfo();
+ appendStringInfo (exml->str, "\n");
+ appendStringInfo (exml->str, "\n");
+
es = (ExplainState *) palloc0(sizeof(ExplainState));
es->printNodes = stmt->verbose;
*************** ExplainOnePlan(PlannedStmt *plannedstmt,
*** 292,298 ****
initStringInfo(&buf);
explain_outNode(&buf,
queryDesc->plannedstmt->planTree, queryDesc->planstate,
! NULL, 0, es);
/*
* If we ran the command, run any AFTER triggers it queued. (Note this
--- 306,312 ----
initStringInfo(&buf);
explain_outNode(&buf,
queryDesc->plannedstmt->planTree, queryDesc->planstate,
! NULL, 0, es, exml);
/*
* If we ran the command, run any AFTER triggers it queued. (Note this
*************** ExplainOnePlan(PlannedStmt *plannedstmt,
*** 325,330 ****
--- 339,347 ----
Trigger *trig = rInfo->ri_TrigDesc->triggers + nt;
Instrumentation *instr = rInfo->ri_TrigInstrument + nt;
char *conname;
+ StringInfo triggerStr;
+
+ triggerStr = makeStringInfo();
/* Must clean up instrumentation state */
InstrEndLoop(instr);
*************** ExplainOnePlan(PlannedStmt *plannedstmt,
*** 341,358 ****
--- 358,393 ----
{
appendStringInfo(&buf, "Trigger for constraint %s",
conname);
+ appendStringInfo(triggerStr, "constraint=\"%s\"",
+ conname);
pfree(conname);
}
else
+ {
appendStringInfo(&buf, "Trigger %s", trig->tgname);
+ appendStringInfo(triggerStr, "name=\"%s\"", trig->tgname);
+ }
if (numrels > 1)
+ {
appendStringInfo(&buf, " on %s",
RelationGetRelationName(rInfo->ri_RelationDesc));
+ appendStringInfo(triggerStr, " on=\"%s\"",
+ RelationGetRelationName(rInfo->ri_RelationDesc));
+ }
appendStringInfo(&buf, ": time=%.3f calls=%.0f\n",
1000.0 * instr->total,
instr->ntuples);
+ appendStringInfo(triggerStr, " \n",
+ triggerStr->data,
+ 1000.0 * instr->total,
+ instr->ntuples);
+ appendStringInfo(exml->str, triggerStr->data);
+
+ pfree(triggerStr->data);
+ pfree(triggerStr);
}
}
}
*************** ExplainOnePlan(PlannedStmt *plannedstmt,
*** 374,383 ****
totaltime += elapsed_time(&starttime);
if (stmt->analyze)
appendStringInfo(&buf, "Total runtime: %.3f ms\n",
1000.0 * totaltime);
! do_text_output_multiline(tstate, buf.data);
pfree(buf.data);
pfree(es);
}
--- 409,434 ----
totaltime += elapsed_time(&starttime);
if (stmt->analyze)
+ {
appendStringInfo(&buf, "Total runtime: %.3f ms\n",
1000.0 * totaltime);
! appendStringInfo(exml->str, "%.3f ms\n",
! 1000.0 * totaltime);
! }
! if (stmt->xml)
! {
! appendStringInfo(exml->str, "\n");
! do_text_output_multiline(tstate, exml->str->data);
! }
! else
! {
! do_text_output_multiline(tstate, buf.data);
! }
+ pfree(exml->str->data);
+ pfree(exml->str);
+ pfree(exml);
+
pfree(buf.data);
pfree(es);
}
*************** static void
*** 421,427 ****
explain_outNode(StringInfo str,
Plan *plan, PlanState *planstate,
Plan *outer_plan,
! int indent, ExplainState *es)
{
char *pname;
int i;
--- 472,479 ----
explain_outNode(StringInfo str,
Plan *plan, PlanState *planstate,
Plan *outer_plan,
! int indent, ExplainState *es,
! ExplainXML *exml)
{
char *pname;
int i;
*************** explain_outNode(StringInfo str,
*** 429,434 ****
--- 481,487 ----
if (plan == NULL)
{
appendStringInfoChar(str, '\n');
+ appendStringInfo(exml->str, "");
return;
}
*************** explain_outNode(StringInfo str,
*** 600,612 ****
}
appendStringInfoString(str, pname);
switch (nodeTag(plan))
{
case T_IndexScan:
! if (ScanDirectionIsBackward(((IndexScan *) plan)->indexorderdir))
! appendStringInfoString(str, " Backward");
! appendStringInfo(str, " using %s",
! explain_get_index_name(((IndexScan *) plan)->indexid));
/* FALL THRU */
case T_SeqScan:
case T_BitmapHeapScan:
--- 653,683 ----
}
appendStringInfoString(str, pname);
+ appendStringInfo(exml->str , "\n",
+ pname, exml->level);
+
switch (nodeTag(plan))
{
case T_IndexScan:
! {
! StringInfo index;
!
! index = makeStringInfo();
! appendStringInfo(index, "name=\"%s\"",
! explain_get_index_name(((IndexScan *) plan)->indexid));
!
! if (ScanDirectionIsBackward(((IndexScan *) plan)->indexorderdir))
! {
! appendStringInfoString(str, " Backward");
! appendStringInfoString(index, " backward");
! }
! appendStringInfo(str, " using %s",
! explain_get_index_name(((IndexScan *) plan)->indexid));
! appendStringInfo(exml->str, " \n",
! index->data);
! pfree(index->data);
! pfree(index);
! }
/* FALL THRU */
case T_SeqScan:
case T_BitmapHeapScan:
*************** explain_outNode(StringInfo str,
*** 616,621 ****
--- 687,695 ----
RangeTblEntry *rte = rt_fetch(((Scan *) plan)->scanrelid,
es->rtable);
char *relname;
+ StringInfo resname;
+
+ resname = makeStringInfo();
/* Assume it's on a real relation */
Assert(rte->rtekind == RTE_RELATION);
*************** explain_outNode(StringInfo str,
*** 625,638 ****
--- 699,727 ----
appendStringInfo(str, " on %s",
quote_identifier(relname));
+ appendStringInfo(resname, "name=\"%s\"",
+ quote_identifier(relname));
+
if (strcmp(rte->eref->aliasname, relname) != 0)
+ {
appendStringInfo(str, " %s",
quote_identifier(rte->eref->aliasname));
+
+ appendStringInfo(resname, " alias=\"%s\"",
+ quote_identifier(rte->eref->aliasname));
+ }
+
+ appendStringInfo(exml->str, "
Alvaro Herrera writes:
> I am forwarding it to improve the chances of it being delivered ... The
> patch in the fwd is not a nice MIME part but it should work without
> problem anyway.
I'm not sure why anyone would want *both* xml and regular output
produced at once. The patch's treatment of name quoting seems both
inconsistent and highly unlikely to be correct (how does XML deal
with embedded quotes in attribute values, anyway?). The submitter
appears to have no clue about the maintenance details required when
adding a field to a Node struct.
But the big question is: where's the DTD? Has he even tried to design
a sane XML representation, or just emitted whatever was convenient given
the existing code structure? I'm fairly suspicious that a patch that
doesn't rearrange the existing code at all is probably not producing
the ideal XML structure.
regards, tom lane