Hi,
I'm interested in data representation and debug of optimizer routines.
Thus, I've changed the debug functions of allpaths.c to make a
graphviz-like output of RelOptInfo structure.
Any idea about this?
Is there some project or improvement like this?
Tanks,
Adriano Lange
C3SL/UFPR - www.c3sl.ufpr.br
Re: graph representation of data structures in optimizer
От:
Gregory Stark <stark@enterprisedb.com>
Дата:
Adriano Lange writes:
> Hi,
>
> I'm interested in data representation and debug of optimizer routines. Thus,
> I've changed the debug functions of allpaths.c to make a graphviz-like output
> of RelOptInfo structure.
>
> Any idea about this?
> Is there some project or improvement like this?
Several people have asked about ways to see what possible plans were
considered and why the were rejected, it was one of the repeat offenders in
the recent Postgres Pet Peeves thread so this is a very interesting area to
explore.
However I have to say this graph you've generated is amazingly hard to
decipher :) It took me a while to even figure out what information it was
presenting.
Worse, it's not useful unless you add a lot more information to it such as
what relations are actually being scanned or joined at each path which is
going to make it a hell of a lot harder to read.
I'm not sure how to do any better but I would be fascinated to see any new
images you generate :)
-- Gregory Stark EnterpriseDB http://www.enterprisedb.com Ask me about EnterpriseDB's PostGIS support!
Re: graph representation of data structures in optimizer
От:
Robert Haas <robertmhaas@gmail.com>
Дата:
On Wed, Feb 18, 2009 at 10:22 AM, Adriano Lange wrote:
> Hi,
>
> I'm interested in data representation and debug of optimizer routines. Thus,
> I've changed the debug functions of allpaths.c to make a graphviz-like
> output of RelOptInfo structure.
>
> Any idea about this?
> Is there some project or improvement like this?
That is pretty cool.
It would help a lot to label the baserels with their names.
You might also want to move the RestrictInfo out of line so that it's
easier to see where the inner and outer joinpath arrows are going.
It would be really sweet if there were some compact way to see the pathkeys.
...Robert
Re: graph representation of data structures in optimizer
От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:
Gregory Stark writes:
> Adriano Lange writes:
>> I've changed the debug functions of allpaths.c to make a graphviz-like output
>> of RelOptInfo structure.
> However I have to say this graph you've generated is amazingly hard to
> decipher :) It took me a while to even figure out what information it was
> presenting.
> Worse, it's not useful unless you add a lot more information to it such as
> what relations are actually being scanned or joined at each path which is
> going to make it a hell of a lot harder to read.
Labeling the bottom-level scan paths with their relations would help a
lot. The label at the top level isn't real helpful.
But really I think the problem with this approach is that the
information density is too low --- imagine what it would look like in a
six-or-more-way join. I don't think the graphical approach is helpful
at all here.
Also, showing the final Path data structure has the problem that a lot
of the information someone might want is already gone, because we throw
away Paths that are determined to be dominated by other paths. The
question someone usually wants answered is "was a path of this structure
considered at all, and if so what was the estimated cost?". In a large
fraction of cases, that's not answerable from the paths that remain at
the end of the search. I think some sort of on-the-fly tracing of all
add_path calls might be a more useful approach.
regards, tom lane
Re: graph representation of data structures in optimizer
От:
Adriano Lange <adriano@c3sl.ufpr.br>
Дата:
Tom Lane escreveu:
> Gregory Stark writes:
>> Adriano Lange writes:
>>> I've changed the debug functions of allpaths.c to make a graphviz-like output
>>> of RelOptInfo structure.
>
>> However I have to say this graph you've generated is amazingly hard to
>> decipher :) It took me a while to even figure out what information it was
>> presenting.
>
>> Worse, it's not useful unless you add a lot more information to it such as
>> what relations are actually being scanned or joined at each path which is
>> going to make it a hell of a lot harder to read.
>
> Labeling the bottom-level scan paths with their relations would help a
> lot. The label at the top level isn't real helpful.
>
> But really I think the problem with this approach is that the
> information density is too low --- imagine what it would look like in a
> six-or-more-way join. I don't think the graphical approach is helpful
> at all here.
Certainly. That example had only three relations. Six relations in a
RelOptInfo will make a big graph and too hard to understand.
So, I will think about this for a while. A interesting thing for me is
to identify the in-degree pointers of each structure.
> Also, showing the final Path data structure has the problem that a lot
> of the information someone might want is already gone, because we throw
> away Paths that are determined to be dominated by other paths. The
> question someone usually wants answered is "was a path of this structure
> considered at all, and if so what was the estimated cost?". In a large
> fraction of cases, that's not answerable from the paths that remain at
> the end of the search. I think some sort of on-the-fly tracing of all
> add_path calls might be a more useful approach.
>
> regards, tom lane
Humm. This temporal approach may be dificult to represent in this
graphical mode. I guess that the text-like pretty_format_node_dump()
representation and diff are yet more usefull for this.
--
Adriano Lange
C3SL/UFPR - www.c3sl.ufpr.br
Re: graph representation of data structures in optimizer
Robert Haas escreveu:
> That is pretty cool.
>
> It would help a lot to label the baserels with their names.
>
> You might also want to move the RestrictInfo out of line so that it's
> easier to see where the inner and outer joinpath arrows are going.
Humm. Maybe this is not easy to do in dot command line graph generator.
Perhaps I should to try this in other application. The output generated
by debug is only a text plain description of vertex and edges, without
any information about position or path. See the attached file.
> It would be really sweet if there were some compact way to see the pathkeys.
Several attributes and objects are missing yet, but I will add them.
Thanks,
Adriano Lange
C3SL/UFPR - www.c3sl.ufpr.br
Re: graph representation of data structures in optimizer
От:
Adriano Lange <adriano@c3sl.ufpr.br>
Дата:
Tom Lane escreveu:
> But really I think the problem with this approach is that the
> information density is too low --- imagine what it would look like in a
> six-or-more-way join. I don't think the graphical approach is helpful
> at all here.
I was thinking about the hard visualization and navigability in the
graph. I think that a good solution for this would be a dynamic
navigability in their nodes and a rearrange their positions by focused
node. I'm not remembering now, but I've saw a tool like this.
Thanks
--
Adriano Lange
C3SL/UFPR - www.c3sl.ufpr.br
Re: graph representation of data structures in optimizer
От:
Peter Eisentraut <peter_e@gmx.net>
Дата:
On Thursday 19 February 2009 22:17:54 Adriano Lange wrote:
> Tom Lane escreveu:
> > But really I think the problem with this approach is that the
> > information density is too low --- imagine what it would look like in a
> > six-or-more-way join. I don't think the graphical approach is helpful
> > at all here.
>
> I was thinking about the hard visualization and navigability in the
> graph. I think that a good solution for this would be a dynamic
> navigability in their nodes and a rearrange their positions by focused
> node. I'm not remembering now, but I've saw a tool like this.
http://prefuse.org could be useful for that.
Re: graph representation of data structures in optimizer
От:
Adriano Lange <adriano@c3sl.ufpr.br>
Дата:
Hi,
The patch attached is an implementation for graph generation of data
structures in postgres. The file debuggraph.c is a simple library that
generate graphs in the format supported by graphviz. Example:
#include "nodes/debuggraph.h"
...
DebugGraph *graph;
DebugNode *node;
graph = createDebugGraph();
node = newDebugNode( graph, "node1", "Node 1" );
addDebugNodeAttribute( node, "Attribute 1", "Value 1" );
newDebugNode( graph, "node2", "Node 2" );
newDebugEdge( graph, "node1", "node2", "Edge 1" );
printGraphvizToFile( graph, stderr );
destroyDebugGraph( graph );
I've also made a reimplementation of outfuncs.c to outfuncs_graph.c for
an entire node output support.
#include "nodes/outfuncs_graph.h"
...
printGraphNodes( node, stderr );
I hope that this may be useful for somebody.
--
Adriano Lange
C3SL/UFPR - www.c3sl.ufpr.br
Index: src/include/nodes/print.h
===================================================================
--- src/include/nodes/print.h (revision 11)
+++ src/include/nodes/print.h (working copy)
@@ -28,6 +28,7 @@
extern char *pretty_format_node_dump(const char *dump);
extern void print_rt(List *rtable);
extern void print_expr(Node *expr, List *rtable);
+extern int print_expr_str(char *output, Node *expr, List *rtable);
extern void print_pathkeys(List *pathkeys, List *rtable);
extern void print_tl(List *tlist, List *rtable);
extern void print_slot(TupleTableSlot *slot);
Index: src/include/nodes/debuggraph.h
===================================================================
--- src/include/nodes/debuggraph.h (revision 0)
+++ src/include/nodes/debuggraph.h (revision 0)
@@ -0,0 +1,80 @@
+/*-------------------------------------------------------------------------
+ *
+ * debuggraph.h
+ *
+ * Copyright (c) 2009, Adriano Lange
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without modifi-
+ * cation, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright noti-
+ * ce, this list of conditions and the following disclaimer in the docu-
+ * mentation and/or other materials provided with the distribution.
+ *
+ * 3. Neither the name of the UNIVERSIDADE FEDERAL DO PARANÁ nor the names of
+ * its contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+2 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef PRINT_GRAPH_H
+#define PRINT_GRAPH_H
+
+typedef struct DebugNode {
+ char* internal_name;
+ char* name;
+ int attributeCount;
+ int attributeMemorySpace;
+ char** attributeNames;
+ char** attributeValues;
+} DebugNode;
+
+typedef struct DebugEdge {
+ char* source;
+ char* destination;
+ char* label;
+} DebugEdge;
+
+typedef struct DebugGraph {
+ DebugNode** nodes;
+ DebugEdge** edges;
+ int nodeCount;
+ int nodeMemorySpace;
+ int edgeCount;
+ int edgeMemorySpace;
+} DebugGraph;
+
+extern DebugGraph* createDebugGraph();
+extern void destroyDebugGraph(DebugGraph* graph);
+extern DebugNode* newDebugNode(DebugGraph* graph, const char* internal_name,
+ const char* name);
+extern void addDebugNodeAttribute(DebugNode* node, const char* name,
+ const char* value);
+extern void addDebugNodeAttributeArgs(DebugNode* node, const char* name,
+ const char* value,...);
+extern void newDebugEdge(DebugGraph* graph, const char* source,
+ const char* destination, const char* label);
+extern void printGraphvizToFile( DebugGraph* graph, FILE* file );
+extern char* addressToName( char* destination, void* address );
+extern DebugNode* findDebugNodeByInternalName( DebugGraph *graph,
+ const char *internal_name );
+extern DebugEdge* findDebugEdge( DebugGraph *graph, const char *source,
+ const char *destination, const char *label );
+
+
+#endif
Index: src/include/nodes/outfuncs_graph.h
===================================================================
--- src/include/nodes/outfuncs_graph.h (revision 0)
+++ src/include/nodes/outfuncs_graph.h (revision 0)
@@ -0,0 +1,14 @@
+/*-------------------------------------------------------------------------
+ *
+ * outfuncs_graph.h
+ *
+ *-------------------------------------------------------------------------
+ */
+#ifndef NODEGRAPH_H
+#define NODEGRAPH_H
+
+extern DebugGraph *createGraphNodes(void *obj);
+extern void printGraphNodes(void *obj, FILE *file);
+
+#endif
+
Index: src/backend/optimizer/path/allpaths.c
===================================================================
--- src/backend/optimizer/path/allpaths.c (revision 11)
+++ src/backend/optimizer/path/allpaths.c (working copy)
@@ -15,8 +15,11 @@
#include "postgres.h"
+#define OPTIMIZER_DEBUG
#ifdef OPTIMIZER_DEBUG
#include "nodes/print.h"
+#include "nodes/debuggraph.h"
+#include "nodes/outfuncs_graph.h"
#endif
#include "optimizer/clauses.h"
#include "optimizer/cost.h"
@@ -671,6 +674,7 @@
}
else
{
+ RelOptInfo *ret;
/*
* Consider the different orders in which we could join the rels,
* using a plugin, GEQO, or the regular join search code.
@@ -681,11 +685,16 @@
root->initial_rels = initial_rels;
if (join_search_hook)
- return (*join_search_hook) (root, levels_needed, initial_rels);
+ ret = (*join_search_hook) (root, levels_needed, initial_rels);
else if (enable_geqo && levels_needed >= geqo_threshold)
- return geqo(root, levels_needed, initial_rels);
+ ret = geqo(root, levels_needed, initial_rels);
else
- return standard_join_search(root, levels_needed, initial_rels);
+ ret = standard_join_search(root, levels_needed, initial_rels);
+
+# ifdef OPTIMIZER_DEBUG
+ //printGraphNodes( ret, stderr );
+# endif
+ return ret;
}
}
@@ -1108,47 +1117,95 @@
#ifdef OPTIMIZER_DEBUG
+static const char*
+get_renation_name(PlannerInfo *root, int relid)
+{
+ RangeTblEntry *rte;
+
+ Assert(relid <= list_length(root->parse->rtable));
+ rte = rt_fetch(relid, root->parse->rtable);
+ return rte->eref->aliasname;
+}
+
static void
-print_relids(Relids relids)
+print_relids(DebugNode *node, PlannerInfo *root, Relids relids)
{
Relids tmprelids;
int x;
- bool first = true;
+ char aux[30];
tmprelids = bms_copy(relids);
while ((x = bms_first_member(tmprelids)) >= 0)
{
- if (!first)
- printf(" ");
- printf("%d", x);
- first = false;
+ sprintf(aux, "relids[%d]", x);
+ addDebugNodeAttribute(node, aux, get_renation_name(root, x));
}
bms_free(tmprelids);
}
static void
-print_restrictclauses(PlannerInfo *root, List *clauses)
+print_restrictclauses(DebugGraph *graph, DebugNode *parent, PlannerInfo *root,
+ List *clauses)
{
+ DebugNode *node;
ListCell *l;
+ char aux[500];
foreach(l, clauses)
{
RestrictInfo *c = lfirst(l);
- print_expr((Node *) c->clause, root->parse->rtable);
- if (lnext(l))
- printf(", ");
+ node = newDebugNode( graph, addressToName(aux, c), "RestrictInfo" );
+ print_expr_str(aux, (Node *) c->clause, root->parse->rtable);
+ addDebugNodeAttribute( node, "clause", aux );
+ newDebugEdge( graph, parent->internal_name, node->internal_name, "" );
}
}
static void
-print_path(PlannerInfo *root, Path *path, int indent)
+print_pathkeys_node(DebugGraph *graph, DebugNode *parent, List *pathkeys,
+ List *rtable)
{
+ ListCell *i;
+
+ foreach(i, pathkeys)
+ {
+ PathKey *pathkey = (PathKey *) lfirst(i);
+ EquivalenceClass *eclass;
+ ListCell *k;
+ DebugNode *node;
+ char aux[500];
+
+ node = newDebugNode(graph, addressToName(aux, pathkey), "PathKey");
+ newDebugEdge(graph, parent->internal_name, node->internal_name, "");
+
+ eclass = pathkey->pk_eclass;
+ /* chase up, in case pathkey is non-canonical */
+ while (eclass->ec_merged)
+ eclass = eclass->ec_merged;
+
+ foreach(k, eclass->ec_members)
+ {
+ EquivalenceMember *mem = (EquivalenceMember *) lfirst(k);
+
+ print_expr_str(aux,(Node *) mem->em_expr, rtable);
+ addDebugNodeAttribute(node, "eclass", aux);
+ }
+ }
+}
+
+
+static DebugNode*
+print_path(DebugGraph *graph, PlannerInfo *root, Path *path)
+{
+ DebugNode *node;
+ char aux[500];
const char *ptype;
bool join = false;
Path *subpath = NULL;
- int i;
+ node = newDebugNode( graph, addressToName(aux, path), "Path" );
+
switch (nodeTag(path))
{
case T_Path:
@@ -1200,90 +1257,123 @@
break;
}
- for (i = 0; i < indent; i++)
- printf("\t");
- printf("%s", ptype);
+ addDebugNodeAttribute(node, "type", ptype );
- if (path->parent)
- {
- printf("(");
- print_relids(path->parent->relids);
- printf(") rows=%.0f", path->parent->rows);
- }
- printf(" cost=%.2f..%.2f\n", path->startup_cost, path->total_cost);
+ print_relids(node, root, path->parent->relids);
+ addDebugNodeAttributeArgs(node, "rows", "%.0f", path->parent->rows);
+ addDebugNodeAttributeArgs(node, "startup_cost", "%.2f", path->startup_cost);
+ addDebugNodeAttributeArgs(node, "total_cost", "%.2f", path->total_cost);
+
if (path->pathkeys)
{
- for (i = 0; i < indent; i++)
- printf("\t");
- printf(" pathkeys: ");
- print_pathkeys(path->pathkeys, root->parse->rtable);
+ DebugNode *node2;
+ node2 = newDebugNode( graph, addressToName(aux, path->pathkeys),
+ "List");
+ newDebugEdge( graph, node->internal_name, node2->internal_name,
+ "pathkeys" );
+ print_pathkeys_node(graph, node2, path->pathkeys, root->parse->rtable);
}
if (join)
{
+ DebugNode *node2;
JoinPath *jp = (JoinPath *) path;
- for (i = 0; i < indent; i++)
- printf("\t");
- printf(" clauses: ");
- print_restrictclauses(root, jp->joinrestrictinfo);
- printf("\n");
+ node2 = newDebugNode( graph, addressToName(aux, jp->joinrestrictinfo),
+ "List");
+ newDebugEdge( graph, node->internal_name, node2->internal_name,
+ "joinrestrictinfo" );
+ print_restrictclauses(graph, node2, root, jp->joinrestrictinfo);
+
if (IsA(path, MergePath))
{
MergePath *mp = (MergePath *) path;
if (mp->outersortkeys || mp->innersortkeys)
{
- for (i = 0; i < indent; i++)
- printf("\t");
- printf(" sortouter=%d sortinner=%d\n",
- ((mp->outersortkeys) ? 1 : 0),
- ((mp->innersortkeys) ? 1 : 0));
+ addDebugNodeAttributeArgs(node, "sortouter", "%d",
+ ((mp->outersortkeys) ? 1 : 0));
+ addDebugNodeAttributeArgs(node, "sortinner", "%d",
+ ((mp->innersortkeys) ? 1 : 0));
}
}
- print_path(root, jp->outerjoinpath, indent + 1);
- print_path(root, jp->innerjoinpath, indent + 1);
+ node2 = print_path(graph, root, jp->outerjoinpath);
+ newDebugEdge(graph, node->internal_name, node2->internal_name,
+ "outerjoinpath");
+ node2 = print_path(graph, root, jp->innerjoinpath);
+ newDebugEdge(graph, node->internal_name, node2->internal_name,
+ "innerjoinpath");
}
- if (subpath)
- print_path(root, subpath, indent + 1);
+ if (subpath) {
+ DebugNode *node2;
+ node2 = print_path(graph, root, subpath);
+ newDebugEdge(graph, node->internal_name, node2->internal_name,
+ "subpath");
+ }
+
+ return node;
}
void
debug_print_rel(PlannerInfo *root, RelOptInfo *rel)
{
- ListCell *l;
+ DebugGraph *graph;
+ DebugNode *node,
+ *node2;
+ char aux[500];
+ ListCell *l;
- printf("RELOPTINFO (");
- print_relids(rel->relids);
- printf("): rows=%.0f width=%d\n", rel->rows, rel->width);
+ graph = createDebugGraph();
+ node = newDebugNode(graph, addressToName(aux,rel), "RelOptInfo" );
+
+ print_relids(node, root, rel->relids);
+
+ addDebugNodeAttributeArgs(node, "rows", "%.0f", rel->rows);
+
+ addDebugNodeAttributeArgs(node, "width", "%d", rel->width);
+
if (rel->baserestrictinfo)
{
- printf("\tbaserestrictinfo: ");
- print_restrictclauses(root, rel->baserestrictinfo);
- printf("\n");
+ node2 = newDebugNode( graph, addressToName(aux,rel->baserestrictinfo),
+ "List" );
+ newDebugEdge( graph, node->internal_name, node2->internal_name,
+ "baserestrictinfo" );
+ print_restrictclauses(graph, node2, root, rel->baserestrictinfo);
}
if (rel->joininfo)
{
- printf("\tjoininfo: ");
- print_restrictclauses(root, rel->joininfo);
- printf("\n");
+ node2 = newDebugNode( graph, addressToName(aux,rel->joininfo),
+ "List" );
+ newDebugEdge( graph, node->internal_name, node2->internal_name,
+ "joininfo" );
+ print_restrictclauses(graph, node2, root, rel->joininfo);
}
- printf("\tpath list:\n");
- foreach(l, rel->pathlist)
- print_path(root, lfirst(l), 1);
- printf("\n\tcheapest startup path:\n");
- print_path(root, rel->cheapest_startup_path, 1);
- printf("\n\tcheapest total path:\n");
- print_path(root, rel->cheapest_total_path, 1);
- printf("\n");
- fflush(stdout);
+
+ node2 = newDebugNode( graph, addressToName(aux,rel->pathlist),
+ "List" );
+ newDebugEdge( graph, node->internal_name, node2->internal_name,
+ "pathlist" );
+ foreach(l, rel->pathlist) {
+ DebugNode *node3;
+ node3 = print_path(graph, root, lfirst(l));
+ newDebugEdge( graph, node2->internal_name, node3->internal_name, "");
+ }
+ newDebugEdge( graph, node->internal_name,
+ addressToName(aux, rel->cheapest_startup_path),
+ "cheapest_startup_path");
+ newDebugEdge( graph, node->internal_name,
+ addressToName(aux, rel->cheapest_total_path),
+ "cheapest_total_path");
+
+ printGraphvizToFile( graph, stderr );
+ destroyDebugGraph( graph );
}
#endif /* OPTIMIZER_DEBUG */
Index: src/backend/nodes/debuggraph.c
===================================================================
--- src/backend/nodes/debuggraph.c (revision 0)
+++ src/backend/nodes/debuggraph.c (revision 0)
@@ -0,0 +1,415 @@
+/*-------------------------------------------------------------------------
+ *
+ * debuggraph.c
+ *
+ * Copyright (c) 2009, Adriano Lange
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without modifi-
+ * cation, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright noti-
+ * ce, this list of conditions and the following disclaimer in the docu-
+ * mentation and/or other materials provided with the distribution.
+ *
+ * 3. Neither the name of the UNIVERSIDADE FEDERAL DO PARANÁ nor the names of
+ * its contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "postgres.h"
+
+#include "nodes/debuggraph.h"
+
+#define INITIAL_LIST_SIZE 100
+
+#define ALLOC_FUNC palloc
+#define ALLOC_FUNC0 palloc0
+#define FREE_FUNC pfree
+
+static void addDebugNode(DebugGraph* graph, DebugNode* node);
+static void addDebugEdge(DebugGraph* graph, DebugEdge* edge);
+static DebugNode* createDebugNode( const char* internal_name,
+ const char* name );
+static void destroyDebugNode(DebugNode* node);
+static DebugEdge* createDebugEdge( const char* source, const char* destination,
+ const char* label );
+static void destroyDebugEdge(DebugEdge* edge);
+static char* copyString( const char* source );
+
+
+DebugGraph*
+createDebugGraph()
+{
+ DebugGraph* graph;
+
+ graph = (DebugGraph*) ALLOC_FUNC0(sizeof(DebugGraph));
+
+ return graph;
+}
+
+void
+destroyDebugGraph(DebugGraph* graph)
+{
+ if( graph ){
+ int i;
+
+ if( graph->nodeMemorySpace ){
+ for( i=0; inodeCount; i++ ){
+ if( graph->nodes[i] )
+ destroyDebugNode( graph->nodes[i] );
+ }
+ FREE_FUNC( graph->nodes );
+ }
+
+ if( graph->edgeMemorySpace ){
+ for( i=0; iedgeCount; i++ ){
+ if( graph->edges[i] )
+ destroyDebugEdge( graph->edges[i] );
+ }
+ FREE_FUNC( graph->edges );
+ }
+
+ FREE_FUNC(graph);
+ }
+}
+
+DebugNode*
+newDebugNode(DebugGraph* graph, const char* internal_name, const char* name)
+{
+ DebugNode* node;
+
+ if( ! graph )
+ return NULL;
+
+ node = findDebugNodeByInternalName(graph, internal_name);
+ if( node )
+ return node;
+
+ node = createDebugNode( internal_name, name );
+ addDebugNode(graph, node);
+
+ return node;
+}
+
+void
+newDebugEdge(DebugGraph* graph, const char* source, const char* destination,
+ const char* label)
+{
+ DebugEdge* edge;
+
+ if( !graph )
+ return;
+
+ edge = findDebugEdge(graph, source, destination, label);
+ if( edge )
+ return;
+
+ edge = createDebugEdge( source, destination, label );
+ addDebugEdge( graph, edge );
+}
+
+void
+addDebugNodeAttributeArgs(DebugNode* node, const char* name,
+ const char* value,...)
+{
+ char *str_value;
+ va_list va;
+ int len = 100;
+ int print_return;
+
+ if( !node || !name || !value )
+ return;
+
+ va_start(va, value);
+ for(;;){
+ str_value = (char*) ALLOC_FUNC( sizeof(char) * len );
+
+ print_return = vsnprintf(str_value, len, value, va);
+
+ if( print_return >= 0 && print_return < len - 1 ) {
+ break;
+ } else {
+ FREE_FUNC(str_value);
+ len *= 2;
+ }
+ }
+ va_end(va);
+
+ addDebugNodeAttribute(node, name, str_value);
+
+ FREE_FUNC(str_value);
+}
+
+void
+addDebugNodeAttribute(DebugNode* node, const char* name, const char* value)
+{
+ int index;
+
+ if( !node || !name || !value )
+ return;
+ for( index=0; indexattributeCount; index++ ){
+ if( (!strcmp(node->attributeNames[index], name))
+ && (!strcmp(node->attributeValues[index], value)) )
+ return;
+ }
+
+ if( node->attributeCount >= node->attributeMemorySpace ){
+ int i;
+ int new_size;
+ char** new_names;
+ char** new_values;
+
+ if( node->attributeMemorySpace )
+ new_size = node->attributeMemorySpace *2;
+ else
+ new_size = INITIAL_LIST_SIZE;
+
+ new_names = (char**) ALLOC_FUNC( sizeof(char*) * new_size );
+ new_values = (char**) ALLOC_FUNC( sizeof(char*) * new_size );
+
+ for( i=0; iattributeCount; i++ ){
+ new_names[i] = node->attributeNames[i];
+ new_values[i] = node->attributeValues[i];
+ }
+ if( node->attributeMemorySpace ){
+ FREE_FUNC( node->attributeNames );
+ FREE_FUNC( node->attributeValues );
+ }
+ node->attributeNames = new_names;
+ node->attributeValues = new_values;
+ node->attributeMemorySpace = new_size;
+ }
+
+ index = node->attributeCount++;
+
+ node->attributeNames[index] = copyString(name);
+ node->attributeValues[index] = copyString(value);
+}
+
+void
+printGraphvizToFile( DebugGraph* graph, FILE* file )
+{
+ int i,j;
+
+ if( !graph )
+ return;
+
+ fprintf(file, "digraph g {\n");
+ fprintf(file, "\tgraph [fontsize=30 labelloc=\"t\" label=\"\" splines=true overlap=false rankdir = \"LR\"];\n");
+ fprintf(file, "\tnode [style = \"filled\" penwidth = 1 fillcolor = \"white\" fontname = \"Courier New\" shape = \"Mrecord\"];\n");
+ fprintf(file, "\tedge [ penwidth = 2 fontsize = 18 fontcolor = \"black\" ];\n");
+ fprintf(file, "\tratio = auto;\n");
+ for( i=0; inodeCount; i++ ){
+ fprintf(file, "\t\"%s\" [ label =<\\\n", graph->nodes[i]->internal_name);
+ fprintf(file, "\t\t
Re: graph representation of data structures in optimizer
От:
ITAGAKI Takahiro <itagaki.takahiro@oss.ntt.co.jp>
Дата:
Adriano Lange wrote:
> The patch attached is an implementation for graph generation of data
> structures in postgres. The file debuggraph.c is a simple library that
> generate graphs in the format supported by graphviz.
It's interesting, but I don't think it is suitable for a core feature.
Could you split the patch into a core-patch and an extension module?
The extension module would be put in contrib or in a pgFoundry project.
XML might be good for communications between the core and the module;
XML-explain was ongoingly discussed, but had not been completed yet.
Regards,
---
ITAGAKI Takahiro
NTT Open Source Software Center