Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 324385d

Browse files
committed
Add YAML to list of EXPLAIN formats. Greg Sabino Mullane, reviewed by Takahiro Itagaki.
1 parent db73861 commit 324385d

File tree

6 files changed

+115
-12
lines changed

6 files changed

+115
-12
lines changed

contrib/auto_explain/auto_explain.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Copyright (c) 2008-2009, PostgreSQL Global Development Group
77
*
88
* IDENTIFICATION
9-
* $PostgreSQL: pgsql/contrib/auto_explain/auto_explain.c,v 1.7 2009/08/10 05:46:49 tgl Exp $
9+
* $PostgreSQL: pgsql/contrib/auto_explain/auto_explain.c,v 1.8 2009/12/11 01:33:35 adunstan Exp $
1010
*
1111
*-------------------------------------------------------------------------
1212
*/
@@ -29,6 +29,7 @@ static const struct config_enum_entry format_options[] = {
2929
{"text", EXPLAIN_FORMAT_TEXT, false},
3030
{"xml", EXPLAIN_FORMAT_XML, false},
3131
{"json", EXPLAIN_FORMAT_JSON, false},
32+
{"yaml", EXPLAIN_FORMAT_YAML, false},
3233
{NULL, 0, false}
3334
};
3435

doc/src/sgml/auto-explain.sgml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!-- $PostgreSQL: pgsql/doc/src/sgml/auto-explain.sgml,v 1.4 2009/08/10 05:46:50 tgl Exp $ -->
1+
<!-- $PostgreSQL: pgsql/doc/src/sgml/auto-explain.sgml,v 1.5 2009/12/11 01:33:35 adunstan Exp $ -->
22

33
<sect1 id="auto-explain">
44
<title>auto_explain</title>
@@ -114,7 +114,7 @@ LOAD 'auto_explain';
114114
<varname>auto_explain.log_format</varname> selects the
115115
<command>EXPLAIN</> output format to be used.
116116
The allowed values are <literal>text</literal>, <literal>xml</literal>,
117-
and <literal>json</literal>. The default is text.
117+
<literal>json</literal>, and <literal>yaml</literal>. The default is text.
118118
Only superusers can change this setting.
119119
</para>
120120
</listitem>

doc/src/sgml/ref/explain.sgml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$PostgreSQL: pgsql/doc/src/sgml/ref/explain.sgml,v 1.46 2009/08/10 05:46:50 tgl Exp $
2+
$PostgreSQL: pgsql/doc/src/sgml/ref/explain.sgml,v 1.47 2009/12/11 01:33:35 adunstan Exp $
33
PostgreSQL documentation
44
-->
55

@@ -31,7 +31,7 @@ PostgreSQL documentation
3131

3232
<refsynopsisdiv>
3333
<synopsis>
34-
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>
34+
EXPLAIN [ ( { ANALYZE <replaceable class="parameter">boolean</replaceable> | VERBOSE <replaceable class="parameter">boolean</replaceable> | COSTS <replaceable class="parameter">boolean</replaceable> | FORMAT { TEXT | XML | JSON | YAML } } [, ...] ) ] <replaceable class="parameter">statement</replaceable>
3535
EXPLAIN [ ANALYZE ] [ VERBOSE ] <replaceable class="parameter">statement</replaceable>
3636
</synopsis>
3737
</refsynopsisdiv>
@@ -143,8 +143,8 @@ ROLLBACK;
143143
<term><literal>FORMAT</literal></term>
144144
<listitem>
145145
<para>
146-
Specify the output format, which can be TEXT, XML, or JSON.
147-
XML or JSON output contains the same information as the text output
146+
Specify the output format, which can be TEXT, XML, JSON, or YAML.
147+
Non-text output contains the same information as the text output
148148
format, but is easier for programs to parse. This parameter defaults to
149149
<literal>TEXT</literal>.
150150
</para>

doc/src/sgml/release-8.5.sgml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!-- $PostgreSQL: pgsql/doc/src/sgml/release-8.5.sgml,v 1.8 2009/11/26 21:20:12 tgl Exp $ -->
1+
<!-- $PostgreSQL: pgsql/doc/src/sgml/release-8.5.sgml,v 1.9 2009/12/11 01:33:35 adunstan Exp $ -->
22

33
<sect1 id="release-8-5">
44
<title>Release 8.5alpha2</title>
@@ -176,7 +176,7 @@
176176
</listitem>
177177
<listitem>
178178
<para>
179-
EXPLAIN allows output of plans in XML or JSON format for automated
179+
EXPLAIN allows output of plans in XML, JSON, or YAML format for automated
180180
processing of explain plans by analysis or visualization tools.
181181
</para>
182182
</listitem>

src/backend/commands/explain.c

Lines changed: 102 additions & 1 deletion
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.193 2009/11/04 22:26:04 tgl Exp $
10+
* $PostgreSQL: pgsql/src/backend/commands/explain.c,v 1.194 2009/12/11 01:33:35 adunstan Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -95,7 +95,9 @@ static void ExplainBeginOutput(ExplainState *es);
9595
static void ExplainEndOutput(ExplainState *es);
9696
static void ExplainXMLTag(const char *tagname, int flags, ExplainState *es);
9797
static void ExplainJSONLineEnding(ExplainState *es);
98+
static void ExplainYAMLLineStarting(ExplainState *es);
9899
static void escape_json(StringInfo buf, const char *str);
100+
static void escape_yaml(StringInfo buf, const char *str);
99101

100102

101103
/*
@@ -135,6 +137,8 @@ ExplainQuery(ExplainStmt *stmt, const char *queryString,
135137
es.format = EXPLAIN_FORMAT_XML;
136138
else if (strcmp(p, "json") == 0)
137139
es.format = EXPLAIN_FORMAT_JSON;
140+
else if (strcmp(p, "yaml") == 0)
141+
es.format = EXPLAIN_FORMAT_YAML;
138142
else
139143
ereport(ERROR,
140144
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
@@ -1537,6 +1541,19 @@ ExplainPropertyList(const char *qlabel, List *data, ExplainState *es)
15371541
}
15381542
appendStringInfoChar(es->str, ']');
15391543
break;
1544+
1545+
case EXPLAIN_FORMAT_YAML:
1546+
ExplainYAMLLineStarting(es);
1547+
escape_yaml(es->str, qlabel);
1548+
appendStringInfoChar(es->str, ':');
1549+
foreach(lc, data)
1550+
{
1551+
appendStringInfoChar(es->str, '\n');
1552+
appendStringInfoSpaces(es->str, es->indent * 2 + 2);
1553+
appendStringInfoString(es->str, "- ");
1554+
escape_yaml(es->str, (const char *) lfirst(lc));
1555+
}
1556+
break;
15401557
}
15411558
}
15421559

@@ -1584,6 +1601,15 @@ ExplainProperty(const char *qlabel, const char *value, bool numeric,
15841601
else
15851602
escape_json(es->str, value);
15861603
break;
1604+
1605+
case EXPLAIN_FORMAT_YAML:
1606+
ExplainYAMLLineStarting(es);
1607+
appendStringInfo(es->str, "%s: ", qlabel);
1608+
if (numeric)
1609+
appendStringInfoString(es->str, value);
1610+
else
1611+
escape_yaml(es->str, value);
1612+
break;
15871613
}
15881614
}
15891615

@@ -1668,6 +1694,21 @@ ExplainOpenGroup(const char *objtype, const char *labelname,
16681694
es->grouping_stack = lcons_int(0, es->grouping_stack);
16691695
es->indent++;
16701696
break;
1697+
1698+
case EXPLAIN_FORMAT_YAML:
1699+
ExplainYAMLLineStarting(es);
1700+
if (labelname)
1701+
{
1702+
appendStringInfo(es->str, "%s:", labelname);
1703+
es->grouping_stack = lcons_int(1, es->grouping_stack);
1704+
}
1705+
else
1706+
{
1707+
appendStringInfoChar(es->str, '-');
1708+
es->grouping_stack = lcons_int(0, es->grouping_stack);
1709+
}
1710+
es->indent++;
1711+
break;
16711712
}
16721713
}
16731714

@@ -1697,6 +1738,11 @@ ExplainCloseGroup(const char *objtype, const char *labelname,
16971738
appendStringInfoChar(es->str, labeled ? '}' : ']');
16981739
es->grouping_stack = list_delete_first(es->grouping_stack);
16991740
break;
1741+
1742+
case EXPLAIN_FORMAT_YAML:
1743+
es->indent--;
1744+
es->grouping_stack = list_delete_first(es->grouping_stack);
1745+
break;
17001746
}
17011747
}
17021748

@@ -1729,6 +1775,13 @@ ExplainDummyGroup(const char *objtype, const char *labelname, ExplainState *es)
17291775
}
17301776
escape_json(es->str, objtype);
17311777
break;
1778+
1779+
case EXPLAIN_FORMAT_YAML:
1780+
ExplainYAMLLineStarting(es);
1781+
if (labelname)
1782+
appendStringInfo(es->str, "%s:", labelname);
1783+
appendStringInfoString(es->str, objtype);
1784+
break;
17321785
}
17331786
}
17341787

@@ -1759,6 +1812,10 @@ ExplainBeginOutput(ExplainState *es)
17591812
es->grouping_stack = lcons_int(0, es->grouping_stack);
17601813
es->indent++;
17611814
break;
1815+
1816+
case EXPLAIN_FORMAT_YAML:
1817+
es->grouping_stack = lcons_int(0, es->grouping_stack);
1818+
break;
17621819
}
17631820
}
17641821

@@ -1784,6 +1841,10 @@ ExplainEndOutput(ExplainState *es)
17841841
appendStringInfoString(es->str, "\n]");
17851842
es->grouping_stack = list_delete_first(es->grouping_stack);
17861843
break;
1844+
1845+
case EXPLAIN_FORMAT_YAML:
1846+
es->grouping_stack = list_delete_first(es->grouping_stack);
1847+
break;
17871848
}
17881849
}
17891850

@@ -1796,6 +1857,7 @@ ExplainSeparatePlans(ExplainState *es)
17961857
switch (es->format)
17971858
{
17981859
case EXPLAIN_FORMAT_TEXT:
1860+
case EXPLAIN_FORMAT_YAML:
17991861
/* add a blank line */
18001862
appendStringInfoChar(es->str, '\n');
18011863
break;
@@ -1858,6 +1920,25 @@ ExplainJSONLineEnding(ExplainState *es)
18581920
appendStringInfoChar(es->str, '\n');
18591921
}
18601922

1923+
/*
1924+
* Indent a YAML line.
1925+
*/
1926+
static void
1927+
ExplainYAMLLineStarting(ExplainState *es)
1928+
{
1929+
Assert(es->format == EXPLAIN_FORMAT_YAML);
1930+
if (linitial_int(es->grouping_stack) == 0)
1931+
{
1932+
appendStringInfoChar(es->str, ' ');
1933+
linitial_int(es->grouping_stack) = 1;
1934+
}
1935+
else
1936+
{
1937+
appendStringInfoChar(es->str, '\n');
1938+
appendStringInfoSpaces(es->str, es->indent * 2);
1939+
}
1940+
}
1941+
18611942
/*
18621943
* Produce a JSON string literal, properly escaping characters in the text.
18631944
*/
@@ -1902,3 +1983,23 @@ escape_json(StringInfo buf, const char *str)
19021983
}
19031984
appendStringInfoCharMacro(buf, '\"');
19041985
}
1986+
1987+
/*
1988+
* YAML is a superset of JSON: if we find quotable characters, we call escape_json
1989+
*/
1990+
static void
1991+
escape_yaml(StringInfo buf, const char *str)
1992+
{
1993+
const char *p;
1994+
1995+
for (p = str; *p; p++)
1996+
{
1997+
if ((unsigned char) *p < ' ' || strchr("\"\\\b\f\n\r\t", *p))
1998+
{
1999+
escape_json(buf, str);
2000+
return;
2001+
}
2002+
}
2003+
2004+
appendStringInfo(buf, "%s", str);
2005+
}

src/include/commands/explain.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
77
* Portions Copyright (c) 1994-5, Regents of the University of California
88
*
9-
* $PostgreSQL: pgsql/src/include/commands/explain.h,v 1.41 2009/08/10 05:46:50 tgl Exp $
9+
* $PostgreSQL: pgsql/src/include/commands/explain.h,v 1.42 2009/12/11 01:33:35 adunstan Exp $
1010
*
1111
*-------------------------------------------------------------------------
1212
*/
@@ -19,7 +19,8 @@ typedef enum ExplainFormat
1919
{
2020
EXPLAIN_FORMAT_TEXT,
2121
EXPLAIN_FORMAT_XML,
22-
EXPLAIN_FORMAT_JSON
22+
EXPLAIN_FORMAT_JSON,
23+
EXPLAIN_FORMAT_YAML
2324
} ExplainFormat;
2425

2526
typedef struct ExplainState

0 commit comments

Comments
 (0)