Skip to content

Commit

Permalink
Extend EXPLAIN to support output in XML or JSON format.
Browse files Browse the repository at this point in the history
There are probably still some adjustments to be made in the details
of the output, but this gets the basic structure in place.

Robert Haas
  • Loading branch information
tglsfdc committed Aug 10, 2009
1 parent 18894c4 commit 9bd27b7
Show file tree
Hide file tree
Showing 12 changed files with 1,150 additions and 361 deletions.
22 changes: 21 additions & 1 deletion contrib/auto_explain/auto_explain.c
Expand Up @@ -6,7 +6,7 @@
* Copyright (c) 2008-2009, PostgreSQL Global Development Group
*
* IDENTIFICATION
* $PostgreSQL: pgsql/contrib/auto_explain/auto_explain.c,v 1.6 2009/07/26 23:34:17 tgl Exp $
* $PostgreSQL: pgsql/contrib/auto_explain/auto_explain.c,v 1.7 2009/08/10 05:46:49 tgl Exp $
*
*-------------------------------------------------------------------------
*/
Expand All @@ -22,8 +22,16 @@ PG_MODULE_MAGIC;
static int auto_explain_log_min_duration = -1; /* msec or -1 */
static bool auto_explain_log_analyze = false;
static bool auto_explain_log_verbose = false;
static int auto_explain_log_format = EXPLAIN_FORMAT_TEXT;
static bool auto_explain_log_nested_statements = false;

static const struct config_enum_entry format_options[] = {
{"text", EXPLAIN_FORMAT_TEXT, false},
{"xml", EXPLAIN_FORMAT_XML, false},
{"json", EXPLAIN_FORMAT_JSON, false},
{NULL, 0, false}
};

/* Current nesting depth of ExecutorRun calls */
static int nesting_level = 0;

Expand Down Expand Up @@ -84,6 +92,17 @@ _PG_init(void)
NULL,
NULL);

DefineCustomEnumVariable("auto_explain.log_format",
"EXPLAIN format to be used for plan logging.",
NULL,
&auto_explain_log_format,
EXPLAIN_FORMAT_TEXT,
format_options,
PGC_SUSET,
0,
NULL,
NULL);

DefineCustomBoolVariable("auto_explain.log_nested_statements",
"Log nested statements.",
NULL,
Expand Down Expand Up @@ -201,6 +220,7 @@ explain_ExecutorEnd(QueryDesc *queryDesc)
ExplainInitState(&es);
es.analyze = (queryDesc->doInstrument && auto_explain_log_analyze);
es.verbose = auto_explain_log_verbose;
es.format = auto_explain_log_format;

ExplainPrintPlan(&es, queryDesc);

Expand Down
20 changes: 19 additions & 1 deletion doc/src/sgml/auto-explain.sgml
@@ -1,4 +1,4 @@
<!-- $PostgreSQL: pgsql/doc/src/sgml/auto-explain.sgml,v 1.3 2009/01/02 01:16:02 tgl Exp $ -->
<!-- $PostgreSQL: pgsql/doc/src/sgml/auto-explain.sgml,v 1.4 2009/08/10 05:46:50 tgl Exp $ -->

<sect1 id="auto-explain">
<title>auto_explain</title>
Expand Down Expand Up @@ -102,6 +102,24 @@ LOAD 'auto_explain';
</listitem>
</varlistentry>

<varlistentry>
<term>
<varname>auto_explain.log_format</varname> (<type>enum</type>)
</term>
<indexterm>
<primary><varname>auto_explain.log_format</> configuration parameter</primary>
</indexterm>
<listitem>
<para>
<varname>auto_explain.log_format</varname> selects the
<command>EXPLAIN</> output format to be used.
The allowed values are <literal>text</literal>, <literal>xml</literal>,
and <literal>json</literal>. The default is text.
Only superusers can change this setting.
</para>
</listitem>
</varlistentry>

<varlistentry>
<term>
<varname>auto_explain.log_nested_statements</varname> (<type>boolean</type>)
Expand Down
28 changes: 22 additions & 6 deletions doc/src/sgml/ref/explain.sgml
@@ -1,5 +1,5 @@
<!--
$PostgreSQL: pgsql/doc/src/sgml/ref/explain.sgml,v 1.45 2009/07/26 23:34:17 tgl Exp $
$PostgreSQL: pgsql/doc/src/sgml/ref/explain.sgml,v 1.46 2009/08/10 05:46:50 tgl Exp $
PostgreSQL documentation
-->

Expand Down Expand Up @@ -31,7 +31,7 @@ PostgreSQL documentation

<refsynopsisdiv>
<synopsis>
EXPLAIN [ ( { ANALYZE <replaceable class="parameter">boolean</replaceable> | VERBOSE <replaceable class="parameter">boolean</replaceable> | COSTS <replaceable class="parameter">boolean</replaceable> } [, ...] ) ] <replaceable class="parameter">statement</replaceable>
EXPLAIN [ ( { ANALYZE <replaceable class="parameter">boolean</replaceable> | VERBOSE <replaceable class="parameter">boolean</replaceable> | COSTS <replaceable class="parameter">boolean</replaceable> | FORMAT { TEXT | XML | JSON } } [, ...] ) ] <replaceable class="parameter">statement</replaceable>
EXPLAIN [ ANALYZE ] [ VERBOSE ] <replaceable class="parameter">statement</replaceable>
</synopsis>
</refsynopsisdiv>
Expand Down Expand Up @@ -109,7 +109,7 @@ ROLLBACK;
<listitem>
<para>
Carry out the command and show the actual run times. This
parameter defaults to <command>FALSE</command>.
parameter defaults to <literal>FALSE</literal>.
</para>
</listitem>
</varlistentry>
Expand All @@ -118,8 +118,12 @@ ROLLBACK;
<term><literal>VERBOSE</literal></term>
<listitem>
<para>
Include the output column list for each node in the plan tree. This
parameter defaults to <command>FALSE</command>.
Display additional information regarding the plan. Specifically, include
the output column list for each node in the plan tree, schema-qualify
table and function names, always label variables in expressions with
their range table alias, and always print the name of each trigger for
which statistics are displayed. This parameter defaults to
<literal>FALSE</literal>.
</para>
</listitem>
</varlistentry>
Expand All @@ -130,7 +134,19 @@ ROLLBACK;
<para>
Include information on the estimated startup and total cost of each
plan node, as well as the estimated number of rows and the estimated
width of each row. This parameter defaults to <command>TRUE</command>.
width of each row. This parameter defaults to <literal>TRUE</literal>.
</para>
</listitem>
</varlistentry>

<varlistentry>
<term><literal>FORMAT</literal></term>
<listitem>
<para>
Specify the output format, which can be TEXT, XML, or JSON.
XML or JSON output contains the same information as the text output
format, but is easier for programs to parse. This parameter defaults to
<literal>TEXT</literal>.
</para>
</listitem>
</varlistentry>
Expand Down

0 comments on commit 9bd27b7

Please sign in to comment.