diff --git a/src/backend/commands/explain.c b/src/backend/commands/explain.c index 96b75de..e4f5332 100644 --- a/src/backend/commands/explain.c +++ b/src/backend/commands/explain.c @@ -1715,7 +1715,7 @@ ExplainPropertyList(const char *qlabel, List *data, ExplainState *es) * Explain a simple property. * * If "numeric" is true, the value is a number (or other value that - * doesn't need quoting in JSON). + * doesn't need quoting in JSON/YAML). * * This usually should not be invoked directly, but via one of the datatype * specific routines ExplainPropertyText, ExplainPropertyInteger, etc. @@ -1759,7 +1759,10 @@ ExplainProperty(const char *qlabel, const char *value, bool numeric, case EXPLAIN_FORMAT_YAML: ExplainYAMLLineStarting(es); appendStringInfo(es->str, "%s: ", qlabel); - escape_yaml(es->str, value); + if (numeric) + appendStringInfoString(es->str, value); + else + escape_yaml(es->str, value); break; } } @@ -2160,21 +2163,24 @@ escape_json(StringInfo buf, const char *str) * Unfortunately, the YAML quoting rules are ridiculously complicated -- as * documented in sections 5.3 and 7.3.3 of http://yaml.org/spec/1.2/spec.html * -- and it doesn't seem worth expending a large amount of energy to avoid - * all unnecessary quoting, so we just do something (sort of) simple: we quote - * any string which is empty; any string which contains characters other than - * alphanumerics, period, underscore, or space; or begins or ends with a - * space. The exception for period is mostly so that floating-point numbers - * (e.g., cost values) won't be quoted. + * all unnecessary quoting, so we just err on the side of quoting when in + * doubt. We quote empty strings, strings containing non-alphanumerics other + * than period, underscore, or space, and strings ending in a space. + * + * We also quote any string which begins with a character other than a letter + * or an underscore. We must quote any string which begins with a space; some + * strings beginning with a digit could be emitted unquoted, but it doesn't + * seem worth the extra code to try to figure out exactly which ones. A string + * like 123 or 0x2 shouldn't be emitted unquoted, else it may be parsed as an + * integer; on the other hand, 2x0 is unambiguously a string, but we just quote + * it anyway. */ static void escape_yaml(StringInfo buf, const char *str) { bool needs_quoting = false; -#define is_safe_yaml(x) \ - (isalnum(((unsigned char) x)) || (x) == '.' || (x) == '_') - - if (!is_safe_yaml(str[0])) + if (!isalpha(str[0]) && str[0] != '_') needs_quoting = true; else { @@ -2182,7 +2188,7 @@ escape_yaml(StringInfo buf, const char *str) for (p = str; *p; p++) { - if (*p != ' ' && !is_safe_yaml(*p)) + if (!isalnum((unsigned char) *p) && !strchr(" _.", *p)) { needs_quoting = true; break;