Attached patch adds YAML output option to explain:

explain (format YAML) select * from information_schema.columns;

--
Greg Sabino Mullane g...@turnstep.com
PGP Key: 0x14964AC8 200908281414
http://biglumber.com/x/web?pk=2529DF6AB8F79407E94445B4BC9B906714964AC8
Index: contrib/auto_explain/auto_explain.c
===================================================================
RCS file: /projects/cvsroot/pgsql/contrib/auto_explain/auto_explain.c,v
retrieving revision 1.7
diff -u -r1.7 auto_explain.c
--- contrib/auto_explain/auto_explain.c	10 Aug 2009 05:46:49 -0000	1.7
+++ contrib/auto_explain/auto_explain.c	28 Aug 2009 18:12:49 -0000
@@ -29,6 +29,7 @@
         {"text", EXPLAIN_FORMAT_TEXT, false},
         {"xml", EXPLAIN_FORMAT_XML, false},
         {"json", EXPLAIN_FORMAT_JSON, false},
+        {"yaml", EXPLAIN_FORMAT_YAML, false},
         {NULL, 0, false}
 };
 
Index: doc/src/sgml/auto-explain.sgml
===================================================================
RCS file: /projects/cvsroot/pgsql/doc/src/sgml/auto-explain.sgml,v
retrieving revision 1.4
diff -u -r1.4 auto-explain.sgml
--- doc/src/sgml/auto-explain.sgml	10 Aug 2009 05:46:50 -0000	1.4
+++ doc/src/sgml/auto-explain.sgml	28 Aug 2009 18:12:49 -0000
@@ -114,7 +114,7 @@
       <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.
+      <literal>json</literal>, and <literal>yaml</literal>.  The default is text.
       Only superusers can change this setting.
      </para>
     </listitem>
Index: doc/src/sgml/release-8.5.sgml
===================================================================
RCS file: /projects/cvsroot/pgsql/doc/src/sgml/release-8.5.sgml,v
retrieving revision 1.4
diff -u -r1.4 release-8.5.sgml
--- doc/src/sgml/release-8.5.sgml	19 Aug 2009 08:18:48 -0000	1.4
+++ doc/src/sgml/release-8.5.sgml	28 Aug 2009 18:12:49 -0000
@@ -96,7 +96,7 @@
       <itemizedlist>
         <listitem>
           <para>
-            EXPLAIN allows output of plans in XML or JSON format for automated
+            EXPLAIN allows output of plans in XML, JSON, or YAML format for automated
             processing of explain plans by analysis or visualization tools.
           </para>
         </listitem>
Index: doc/src/sgml/ref/explain.sgml
===================================================================
RCS file: /projects/cvsroot/pgsql/doc/src/sgml/ref/explain.sgml,v
retrieving revision 1.46
diff -u -r1.46 explain.sgml
--- doc/src/sgml/ref/explain.sgml	10 Aug 2009 05:46:50 -0000	1.46
+++ doc/src/sgml/ref/explain.sgml	28 Aug 2009 18:12:49 -0000
@@ -31,7 +31,7 @@
 
  <refsynopsisdiv>
 <synopsis>
-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 <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>
 EXPLAIN [ ANALYZE ] [ VERBOSE ] <replaceable class="parameter">statement</replaceable>
 </synopsis>
  </refsynopsisdiv>
@@ -143,8 +143,8 @@
     <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
+      Specify the output format, which can be TEXT, XML, JSON, or YAML.
+      Non-text 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>
Index: src/backend/commands/explain.c
===================================================================
RCS file: /projects/cvsroot/pgsql/src/backend/commands/explain.c,v
retrieving revision 1.190
diff -u -r1.190 explain.c
--- src/backend/commands/explain.c	22 Aug 2009 02:06:32 -0000	1.190
+++ src/backend/commands/explain.c	28 Aug 2009 18:12:49 -0000
@@ -96,6 +96,7 @@
 static void ExplainXMLTag(const char *tagname, int flags, ExplainState *es);
 static void ExplainJSONLineEnding(ExplainState *es);
 static void escape_json(StringInfo buf, const char *str);
+static void escape_yaml(StringInfo buf, const char *str);
 
 
 /*
@@ -137,6 +138,8 @@
 				es.format = EXPLAIN_FORMAT_XML;
 			else if (strcmp(p, "json") == 0)
 				es.format = EXPLAIN_FORMAT_JSON;
+			else if (strcmp(p, "yaml") == 0)
+				es.format = EXPLAIN_FORMAT_YAML;
 			else
 				ereport(ERROR,
 					(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
@@ -1509,6 +1512,18 @@
 			}
 			appendStringInfoChar(es->str, ']');
 			break;
+
+		case EXPLAIN_FORMAT_YAML:
+			appendStringInfoSpaces(es->str, es->indent * 2);
+			appendStringInfo(es->str, "%s:\n", qlabel);
+			foreach(lc, data)
+			{
+			    appendStringInfoSpaces(es->str, es->indent * 2 + 2);
+				appendStringInfoString(es->str, "- ");
+				escape_yaml(es->str, qlabel);
+				appendStringInfoChar(es->str, '\n');
+			}
+			break;
 	}
 }
 
@@ -1556,6 +1571,17 @@
 			else
 				escape_json(es->str, value);
 			break;
+
+		case EXPLAIN_FORMAT_YAML:
+			appendStringInfoSpaces(es->str, es->indent * 2);
+			appendStringInfo(es->str, "%s: ", qlabel);
+			if (numeric)
+				appendStringInfoString(es->str, value);
+			else
+				escape_yaml(es->str, value);
+			appendStringInfoChar(es->str, '\n');
+			break;
+
 	}
 }
 
@@ -1640,6 +1666,19 @@
 			es->grouping_stack = lcons_int(0, es->grouping_stack);
 			es->indent++;
 			break;
+
+		case EXPLAIN_FORMAT_YAML:
+			appendStringInfoSpaces(es->str, 2 * es->indent);
+			if (labelname)
+			{
+				appendStringInfo(es->str, "%s:\n", labelname);
+			}
+			else {
+				appendStringInfoString(es->str, "-\n");
+			}
+			es->indent++;
+			break;
+
 	}
 }
 
@@ -1669,6 +1708,10 @@
 			appendStringInfoChar(es->str, labeled ? '}' : ']');
 			es->grouping_stack = list_delete_first(es->grouping_stack);
 			break;
+
+		case EXPLAIN_FORMAT_YAML:
+			es->indent--;
+			break;
 	}
 }
 
@@ -1693,7 +1736,6 @@
 
 		case EXPLAIN_FORMAT_JSON:
 			ExplainJSONLineEnding(es);
-			appendStringInfoSpaces(es->str, 2 * es->indent);
 			if (labelname)
 			{
 				escape_json(es->str, labelname);
@@ -1701,6 +1743,15 @@
 			}
 			escape_json(es->str, objtype);
 			break;
+
+		case EXPLAIN_FORMAT_YAML:
+			appendStringInfoSpaces(es->str, 2 * es->indent);
+			if (labelname)
+			{
+				appendStringInfo(es->str, "%s:", labelname);
+			}
+			appendStringInfo(es->str, "%s\n", objtype);
+			break;
 	}
 }
 
@@ -1716,6 +1767,7 @@
 	switch (es->format)
 	{
 		case EXPLAIN_FORMAT_TEXT:
+		case EXPLAIN_FORMAT_YAML:
 			/* nothing to do */
 			break;
 
@@ -1756,6 +1808,10 @@
 			appendStringInfoString(es->str, "\n]");
 			es->grouping_stack = list_delete_first(es->grouping_stack);
 			break;
+
+		case EXPLAIN_FORMAT_YAML:
+			es->indent--;
+			break;
 	}
 }
 
@@ -1773,6 +1829,7 @@
 			break;
 
 		case EXPLAIN_FORMAT_XML:
+		case EXPLAIN_FORMAT_YAML:
 			/* nothing to do */
 			break;
 
@@ -1874,3 +1931,31 @@
 	}
 	appendStringInfoCharMacro(buf, '\"');
 }
+
+/*
+ * YAML is a superset of JSON: if we find quotable characters, we call escape_json
+ */
+static void
+escape_yaml(StringInfo buf, const char *str)
+{
+	const char *p;
+
+	for (p = str; *p; p++)
+	{
+		if ('"' == *p
+			|| '\\' == *p
+			|| '\t' == *p
+			|| '\n' == *p
+			|| '\r' == *p
+			|| '\b' == *p
+			|| '\f' == *p
+			|| (unsigned char) *p < ' '
+			)
+		{
+			escape_json(buf, str);
+			return;
+		}
+	}
+
+	appendStringInfo(buf, "%s", str);
+}
Index: src/include/commands/explain.h
===================================================================
RCS file: /projects/cvsroot/pgsql/src/include/commands/explain.h,v
retrieving revision 1.41
diff -u -r1.41 explain.h
--- src/include/commands/explain.h	10 Aug 2009 05:46:50 -0000	1.41
+++ src/include/commands/explain.h	28 Aug 2009 18:12:50 -0000
@@ -19,7 +19,8 @@
 {
 	EXPLAIN_FORMAT_TEXT,
 	EXPLAIN_FORMAT_XML,
-	EXPLAIN_FORMAT_JSON
+	EXPLAIN_FORMAT_JSON,
+	EXPLAIN_FORMAT_YAML
 } ExplainFormat;
 
 typedef struct ExplainState

Attachment: signature.asc
Description: OpenPGP digital signature

Reply via email to