Skip to content

Commit ff49961

Browse files
committed
Several fixes for EXPLAIN (FORMAT YAML), plus one for EXPLAIN (FORMAT JSON).
ExplainSeparatePlans() was busted for both JSON and YAML output - the present code is a holdover from the original version of my machine-readable explain patch, which didn't have the grouping_stack machinery. Also, fix an odd distribution of labor between ExplainBeginGroup() and ExplainYAMLLineStarting() when marking lists with "- ", with each providing one character. This broke the output format for multi-query statements. Also, fix ExplainDummyGroup() for the YAML output format. Along the way, make the YAML format use escape_yaml() in situations where the JSON format uses escape_json(). Right now, it doesn't matter because all the values are known not to need escaping, but it seems safer this way. Finally, I added some comments to better explain what the YAML output format is doing. Greg Sabino Mullane reported the issues with multi-query statements. Analysis and remaining cleanups by me.
1 parent 3dfe7e8 commit ff49961

File tree

1 file changed

+31
-17
lines changed

1 file changed

+31
-17
lines changed

src/backend/commands/explain.c

Lines changed: 31 additions & 17 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.196 2009/12/15 04:57:47 rhaas Exp $
10+
* $PostgreSQL: pgsql/src/backend/commands/explain.c,v 1.197 2009/12/16 22:16:16 rhaas Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -1694,10 +1694,7 @@ ExplainProperty(const char *qlabel, const char *value, bool numeric,
16941694
case EXPLAIN_FORMAT_YAML:
16951695
ExplainYAMLLineStarting(es);
16961696
appendStringInfo(es->str, "%s: ", qlabel);
1697-
if (numeric)
1698-
appendStringInfoString(es->str, value);
1699-
else
1700-
escape_yaml(es->str, value);
1697+
escape_yaml(es->str, value);
17011698
break;
17021699
}
17031700
}
@@ -1785,15 +1782,23 @@ ExplainOpenGroup(const char *objtype, const char *labelname,
17851782
break;
17861783

17871784
case EXPLAIN_FORMAT_YAML:
1785+
1786+
/*
1787+
* In YAML format, the grouping stack is an integer list. 0 means
1788+
* we've emitted nothing at this grouping level AND this grouping
1789+
* level is unlabelled and must be marked with "- ". See
1790+
* ExplainYAMLLineStarting().
1791+
*/
17881792
ExplainYAMLLineStarting(es);
17891793
if (labelname)
17901794
{
1791-
appendStringInfo(es->str, "%s:", labelname);
1795+
escape_yaml(es->str, labelname);
1796+
appendStringInfoChar(es->str, ':');
17921797
es->grouping_stack = lcons_int(1, es->grouping_stack);
17931798
}
17941799
else
17951800
{
1796-
appendStringInfoChar(es->str, '-');
1801+
appendStringInfoString(es->str, "- ");
17971802
es->grouping_stack = lcons_int(0, es->grouping_stack);
17981803
}
17991804
es->indent++;
@@ -1868,8 +1873,15 @@ ExplainDummyGroup(const char *objtype, const char *labelname, ExplainState *es)
18681873
case EXPLAIN_FORMAT_YAML:
18691874
ExplainYAMLLineStarting(es);
18701875
if (labelname)
1871-
appendStringInfo(es->str, "%s:", labelname);
1872-
appendStringInfoString(es->str, objtype);
1876+
{
1877+
escape_yaml(es->str, labelname);
1878+
appendStringInfoString(es->str, ": ");
1879+
}
1880+
else
1881+
{
1882+
appendStringInfoString(es->str, "- ");
1883+
}
1884+
escape_yaml(es->str, objtype);
18731885
break;
18741886
}
18751887
}
@@ -1946,18 +1958,14 @@ ExplainSeparatePlans(ExplainState *es)
19461958
switch (es->format)
19471959
{
19481960
case EXPLAIN_FORMAT_TEXT:
1949-
case EXPLAIN_FORMAT_YAML:
19501961
/* add a blank line */
19511962
appendStringInfoChar(es->str, '\n');
19521963
break;
19531964

19541965
case EXPLAIN_FORMAT_XML:
1955-
/* nothing to do */
1956-
break;
1957-
19581966
case EXPLAIN_FORMAT_JSON:
1959-
/* must have a comma between array elements */
1960-
appendStringInfoChar(es->str, ',');
1967+
case EXPLAIN_FORMAT_YAML:
1968+
/* nothing to do */
19611969
break;
19621970
}
19631971
}
@@ -2011,14 +2019,19 @@ ExplainJSONLineEnding(ExplainState *es)
20112019

20122020
/*
20132021
* Indent a YAML line.
2022+
*
2023+
* YAML lines are ordinarily indented by two spaces per indentation level.
2024+
* The text emitted for each property begins just prior to the preceding
2025+
* line-break, except for the first property in an unlabelled group, for which
2026+
* it begins immediately after the "- " that introduces the group. The first
2027+
* property of the group appears on the same line as the opening "- ".
20142028
*/
20152029
static void
20162030
ExplainYAMLLineStarting(ExplainState *es)
20172031
{
20182032
Assert(es->format == EXPLAIN_FORMAT_YAML);
20192033
if (linitial_int(es->grouping_stack) == 0)
20202034
{
2021-
appendStringInfoChar(es->str, ' ');
20222035
linitial_int(es->grouping_stack) = 1;
20232036
}
20242037
else
@@ -2074,7 +2087,8 @@ escape_json(StringInfo buf, const char *str)
20742087
}
20752088

20762089
/*
2077-
* YAML is a superset of JSON: if we find quotable characters, we call escape_json
2090+
* YAML is a superset of JSON: if we find quotable characters, we call
2091+
* escape_json. If not, we emit the property unquoted for better readability.
20782092
*/
20792093
static void
20802094
escape_yaml(StringInfo buf, const char *str)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy