LCOV - code coverage report
Current view: top level - src/backend/commands - explain_format.c (source / functions) Coverage Total Hit
Test: Code coverage Lines: 73.1 % 297 217
Test Date: 2026-01-26 10:56:24 Functions: 100.0 % 22 22
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 55.9 % 145 81

             Branch data     Line data    Source code
       1                 :             : /*-------------------------------------------------------------------------
       2                 :             :  *
       3                 :             :  * explain_format.c
       4                 :             :  *        Format routines for explaining query execution plans
       5                 :             :  *
       6                 :             :  * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
       7                 :             :  * Portions Copyright (c) 1994-5, Regents of the University of California
       8                 :             :  *
       9                 :             :  * IDENTIFICATION
      10                 :             :  *        src/backend/commands/explain_format.c
      11                 :             :  *
      12                 :             :  *-------------------------------------------------------------------------
      13                 :             :  */
      14                 :             : #include "postgres.h"
      15                 :             : 
      16                 :             : #include "commands/explain.h"
      17                 :             : #include "commands/explain_format.h"
      18                 :             : #include "commands/explain_state.h"
      19                 :             : #include "utils/json.h"
      20                 :             : #include "utils/xml.h"
      21                 :             : 
      22                 :             : /* OR-able flags for ExplainXMLTag() */
      23                 :             : #define X_OPENING 0
      24                 :             : #define X_CLOSING 1
      25                 :             : #define X_CLOSE_IMMEDIATE 2
      26                 :             : #define X_NOWHITESPACE 4
      27                 :             : 
      28                 :             : static void ExplainJSONLineEnding(ExplainState *es);
      29                 :             : static void ExplainXMLTag(const char *tagname, int flags, ExplainState *es);
      30                 :             : static void ExplainYAMLLineStarting(ExplainState *es);
      31                 :             : static void escape_yaml(StringInfo buf, const char *str);
      32                 :             : 
      33                 :             : /*
      34                 :             :  * Explain a property, such as sort keys or targets, that takes the form of
      35                 :             :  * a list of unlabeled items.  "data" is a list of C strings.
      36                 :             :  */
      37                 :             : void
      38                 :        3034 : ExplainPropertyList(const char *qlabel, List *data, ExplainState *es)
      39                 :             : {
      40                 :        3034 :         ListCell   *lc;
      41                 :        3034 :         bool            first = true;
      42                 :             : 
      43   [ -  -  +  -  :        3034 :         switch (es->format)
                      + ]
      44                 :             :         {
      45                 :             :                 case EXPLAIN_FORMAT_TEXT:
      46                 :        3007 :                         ExplainIndentText(es);
      47                 :        3007 :                         appendStringInfo(es->str, "%s: ", qlabel);
      48   [ +  -  +  +  :        8811 :                         foreach(lc, data)
                   +  + ]
      49                 :             :                         {
      50         [ +  + ]:        5804 :                                 if (!first)
      51                 :        2797 :                                         appendStringInfoString(es->str, ", ");
      52                 :        5804 :                                 appendStringInfoString(es->str, (const char *) lfirst(lc));
      53                 :        5804 :                                 first = false;
      54                 :        5804 :                         }
      55                 :        3007 :                         appendStringInfoChar(es->str, '\n');
      56                 :        3007 :                         break;
      57                 :             : 
      58                 :             :                 case EXPLAIN_FORMAT_XML:
      59                 :           0 :                         ExplainXMLTag(qlabel, X_OPENING, es);
      60   [ #  #  #  #  :           0 :                         foreach(lc, data)
                   #  # ]
      61                 :             :                         {
      62                 :           0 :                                 char       *str;
      63                 :             : 
      64                 :           0 :                                 appendStringInfoSpaces(es->str, es->indent * 2 + 2);
      65                 :           0 :                                 appendStringInfoString(es->str, "<Item>");
      66                 :           0 :                                 str = escape_xml((const char *) lfirst(lc));
      67                 :           0 :                                 appendStringInfoString(es->str, str);
      68                 :           0 :                                 pfree(str);
      69                 :           0 :                                 appendStringInfoString(es->str, "</Item>\n");
      70                 :           0 :                         }
      71                 :           0 :                         ExplainXMLTag(qlabel, X_CLOSING, es);
      72                 :           0 :                         break;
      73                 :             : 
      74                 :             :                 case EXPLAIN_FORMAT_JSON:
      75                 :          27 :                         ExplainJSONLineEnding(es);
      76                 :          27 :                         appendStringInfoSpaces(es->str, es->indent * 2);
      77                 :          27 :                         escape_json(es->str, qlabel);
      78                 :          27 :                         appendStringInfoString(es->str, ": [");
      79   [ +  -  +  +  :         111 :                         foreach(lc, data)
                   +  + ]
      80                 :             :                         {
      81         [ +  + ]:          84 :                                 if (!first)
      82                 :          57 :                                         appendStringInfoString(es->str, ", ");
      83                 :          84 :                                 escape_json(es->str, (const char *) lfirst(lc));
      84                 :          84 :                                 first = false;
      85                 :          84 :                         }
      86                 :          27 :                         appendStringInfoChar(es->str, ']');
      87                 :          27 :                         break;
      88                 :             : 
      89                 :             :                 case EXPLAIN_FORMAT_YAML:
      90                 :           0 :                         ExplainYAMLLineStarting(es);
      91                 :           0 :                         appendStringInfo(es->str, "%s: ", qlabel);
      92   [ #  #  #  #  :           0 :                         foreach(lc, data)
                   #  # ]
      93                 :             :                         {
      94                 :           0 :                                 appendStringInfoChar(es->str, '\n');
      95                 :           0 :                                 appendStringInfoSpaces(es->str, es->indent * 2 + 2);
      96                 :           0 :                                 appendStringInfoString(es->str, "- ");
      97                 :           0 :                                 escape_yaml(es->str, (const char *) lfirst(lc));
      98                 :           0 :                         }
      99                 :           0 :                         break;
     100                 :             :         }
     101                 :        3034 : }
     102                 :             : 
     103                 :             : /*
     104                 :             :  * Explain a property that takes the form of a list of unlabeled items within
     105                 :             :  * another list.  "data" is a list of C strings.
     106                 :             :  */
     107                 :             : void
     108                 :         112 : ExplainPropertyListNested(const char *qlabel, List *data, ExplainState *es)
     109                 :             : {
     110                 :         112 :         ListCell   *lc;
     111                 :         112 :         bool            first = true;
     112                 :             : 
     113   [ -  +  -  - ]:         112 :         switch (es->format)
     114                 :             :         {
     115                 :             :                 case EXPLAIN_FORMAT_TEXT:
     116                 :             :                 case EXPLAIN_FORMAT_XML:
     117                 :         112 :                         ExplainPropertyList(qlabel, data, es);
     118                 :         112 :                         return;
     119                 :             : 
     120                 :             :                 case EXPLAIN_FORMAT_JSON:
     121                 :           0 :                         ExplainJSONLineEnding(es);
     122                 :           0 :                         appendStringInfoSpaces(es->str, es->indent * 2);
     123                 :           0 :                         appendStringInfoChar(es->str, '[');
     124   [ #  #  #  #  :           0 :                         foreach(lc, data)
                   #  # ]
     125                 :             :                         {
     126         [ #  # ]:           0 :                                 if (!first)
     127                 :           0 :                                         appendStringInfoString(es->str, ", ");
     128                 :           0 :                                 escape_json(es->str, (const char *) lfirst(lc));
     129                 :           0 :                                 first = false;
     130                 :           0 :                         }
     131                 :           0 :                         appendStringInfoChar(es->str, ']');
     132                 :           0 :                         break;
     133                 :             : 
     134                 :             :                 case EXPLAIN_FORMAT_YAML:
     135                 :           0 :                         ExplainYAMLLineStarting(es);
     136                 :           0 :                         appendStringInfoString(es->str, "- [");
     137   [ #  #  #  #  :           0 :                         foreach(lc, data)
                   #  # ]
     138                 :             :                         {
     139         [ #  # ]:           0 :                                 if (!first)
     140                 :           0 :                                         appendStringInfoString(es->str, ", ");
     141                 :           0 :                                 escape_yaml(es->str, (const char *) lfirst(lc));
     142                 :           0 :                                 first = false;
     143                 :           0 :                         }
     144                 :           0 :                         appendStringInfoChar(es->str, ']');
     145                 :           0 :                         break;
     146                 :             :         }
     147         [ -  + ]:         112 : }
     148                 :             : 
     149                 :             : /*
     150                 :             :  * Explain a simple property.
     151                 :             :  *
     152                 :             :  * If "numeric" is true, the value is a number (or other value that
     153                 :             :  * doesn't need quoting in JSON).
     154                 :             :  *
     155                 :             :  * If unit is non-NULL the text format will display it after the value.
     156                 :             :  *
     157                 :             :  * This usually should not be invoked directly, but via one of the datatype
     158                 :             :  * specific routines ExplainPropertyText, ExplainPropertyInteger, etc.
     159                 :             :  */
     160                 :             : static void
     161                 :       12279 : ExplainProperty(const char *qlabel, const char *unit, const char *value,
     162                 :             :                                 bool numeric, ExplainState *es)
     163                 :             : {
     164   [ -  +  +  +  :       12279 :         switch (es->format)
                      + ]
     165                 :             :         {
     166                 :             :                 case EXPLAIN_FORMAT_TEXT:
     167                 :        7686 :                         ExplainIndentText(es);
     168         [ +  + ]:        7686 :                         if (unit)
     169                 :         794 :                                 appendStringInfo(es->str, "%s: %s %s\n", qlabel, value, unit);
     170                 :             :                         else
     171                 :        6892 :                                 appendStringInfo(es->str, "%s: %s\n", qlabel, value);
     172                 :        7686 :                         break;
     173                 :             : 
     174                 :             :                 case EXPLAIN_FORMAT_XML:
     175                 :             :                         {
     176                 :          36 :                                 char       *str;
     177                 :             : 
     178                 :          36 :                                 appendStringInfoSpaces(es->str, es->indent * 2);
     179                 :          36 :                                 ExplainXMLTag(qlabel, X_OPENING | X_NOWHITESPACE, es);
     180                 :          36 :                                 str = escape_xml(value);
     181                 :          36 :                                 appendStringInfoString(es->str, str);
     182                 :          36 :                                 pfree(str);
     183                 :          36 :                                 ExplainXMLTag(qlabel, X_CLOSING | X_NOWHITESPACE, es);
     184                 :          36 :                                 appendStringInfoChar(es->str, '\n');
     185                 :          36 :                         }
     186                 :          36 :                         break;
     187                 :             : 
     188                 :             :                 case EXPLAIN_FORMAT_JSON:
     189                 :        4495 :                         ExplainJSONLineEnding(es);
     190                 :        4495 :                         appendStringInfoSpaces(es->str, es->indent * 2);
     191                 :        4495 :                         escape_json(es->str, qlabel);
     192                 :        4495 :                         appendStringInfoString(es->str, ": ");
     193         [ +  + ]:        4495 :                         if (numeric)
     194                 :        3914 :                                 appendStringInfoString(es->str, value);
     195                 :             :                         else
     196                 :         581 :                                 escape_json(es->str, value);
     197                 :        4495 :                         break;
     198                 :             : 
     199                 :             :                 case EXPLAIN_FORMAT_YAML:
     200                 :          62 :                         ExplainYAMLLineStarting(es);
     201                 :          62 :                         appendStringInfo(es->str, "%s: ", qlabel);
     202         [ +  + ]:          62 :                         if (numeric)
     203                 :          55 :                                 appendStringInfoString(es->str, value);
     204                 :             :                         else
     205                 :           7 :                                 escape_yaml(es->str, value);
     206                 :          62 :                         break;
     207                 :             :         }
     208                 :       12279 : }
     209                 :             : 
     210                 :             : /*
     211                 :             :  * Explain a string-valued property.
     212                 :             :  */
     213                 :             : void
     214                 :        6745 : ExplainPropertyText(const char *qlabel, const char *value, ExplainState *es)
     215                 :             : {
     216                 :        6745 :         ExplainProperty(qlabel, NULL, value, false, es);
     217                 :        6745 : }
     218                 :             : 
     219                 :             : /*
     220                 :             :  * Explain an integer-valued property.
     221                 :             :  */
     222                 :             : void
     223                 :        2407 : ExplainPropertyInteger(const char *qlabel, const char *unit, int64 value,
     224                 :             :                                            ExplainState *es)
     225                 :             : {
     226                 :        2407 :         char            buf[32];
     227                 :             : 
     228                 :        2407 :         snprintf(buf, sizeof(buf), INT64_FORMAT, value);
     229                 :        2407 :         ExplainProperty(qlabel, unit, buf, true, es);
     230                 :        2407 : }
     231                 :             : 
     232                 :             : /*
     233                 :             :  * Explain an unsigned integer-valued property.
     234                 :             :  */
     235                 :             : void
     236                 :         243 : ExplainPropertyUInteger(const char *qlabel, const char *unit, uint64 value,
     237                 :             :                                                 ExplainState *es)
     238                 :             : {
     239                 :         243 :         char            buf[32];
     240                 :             : 
     241                 :         243 :         snprintf(buf, sizeof(buf), UINT64_FORMAT, value);
     242                 :         243 :         ExplainProperty(qlabel, unit, buf, true, es);
     243                 :         243 : }
     244                 :             : 
     245                 :             : /*
     246                 :             :  * Explain a float-valued property, using the specified number of
     247                 :             :  * fractional digits.
     248                 :             :  */
     249                 :             : void
     250                 :        2274 : ExplainPropertyFloat(const char *qlabel, const char *unit, double value,
     251                 :             :                                          int ndigits, ExplainState *es)
     252                 :             : {
     253                 :        2274 :         char       *buf;
     254                 :             : 
     255                 :        2274 :         buf = psprintf("%.*f", ndigits, value);
     256                 :        2274 :         ExplainProperty(qlabel, unit, buf, true, es);
     257                 :        2274 :         pfree(buf);
     258                 :        2274 : }
     259                 :             : 
     260                 :             : /*
     261                 :             :  * Explain a bool-valued property.
     262                 :             :  */
     263                 :             : void
     264                 :         610 : ExplainPropertyBool(const char *qlabel, bool value, ExplainState *es)
     265                 :             : {
     266                 :         610 :         ExplainProperty(qlabel, NULL, value ? "true" : "false", true, es);
     267                 :         610 : }
     268                 :             : 
     269                 :             : /*
     270                 :             :  * Open a group of related objects.
     271                 :             :  *
     272                 :             :  * objtype is the type of the group object, labelname is its label within
     273                 :             :  * a containing object (if any).
     274                 :             :  *
     275                 :             :  * If labeled is true, the group members will be labeled properties,
     276                 :             :  * while if it's false, they'll be unlabeled objects.
     277                 :             :  */
     278                 :             : void
     279                 :       25149 : ExplainOpenGroup(const char *objtype, const char *labelname,
     280                 :             :                                  bool labeled, ExplainState *es)
     281                 :             : {
     282   [ +  +  +  + ]:       25149 :         switch (es->format)
     283                 :             :         {
     284                 :             :                 case EXPLAIN_FORMAT_TEXT:
     285                 :             :                         /* nothing to do */
     286                 :             :                         break;
     287                 :             : 
     288                 :             :                 case EXPLAIN_FORMAT_XML:
     289                 :           4 :                         ExplainXMLTag(objtype, X_OPENING, es);
     290                 :           4 :                         es->indent++;
     291                 :           4 :                         break;
     292                 :             : 
     293                 :             :                 case EXPLAIN_FORMAT_JSON:
     294                 :         471 :                         ExplainJSONLineEnding(es);
     295                 :         471 :                         appendStringInfoSpaces(es->str, 2 * es->indent);
     296         [ +  + ]:         471 :                         if (labelname)
     297                 :             :                         {
     298                 :         293 :                                 escape_json(es->str, labelname);
     299                 :         293 :                                 appendStringInfoString(es->str, ": ");
     300                 :         293 :                         }
     301                 :         471 :                         appendStringInfoChar(es->str, labeled ? '{' : '[');
     302                 :             : 
     303                 :             :                         /*
     304                 :             :                          * In JSON format, the grouping_stack is an integer list.  0 means
     305                 :             :                          * we've emitted nothing at this grouping level, 1 means we've
     306                 :             :                          * emitted something (and so the next item needs a comma). See
     307                 :             :                          * ExplainJSONLineEnding().
     308                 :             :                          */
     309                 :         471 :                         es->grouping_stack = lcons_int(0, es->grouping_stack);
     310                 :         471 :                         es->indent++;
     311                 :         471 :                         break;
     312                 :             : 
     313                 :             :                 case EXPLAIN_FORMAT_YAML:
     314                 :             : 
     315                 :             :                         /*
     316                 :             :                          * In YAML format, the grouping stack is an integer list.  0 means
     317                 :             :                          * we've emitted nothing at this grouping level AND this grouping
     318                 :             :                          * level is unlabeled and must be marked with "- ".  See
     319                 :             :                          * ExplainYAMLLineStarting().
     320                 :             :                          */
     321                 :           8 :                         ExplainYAMLLineStarting(es);
     322         [ +  + ]:           8 :                         if (labelname)
     323                 :             :                         {
     324                 :           6 :                                 appendStringInfo(es->str, "%s: ", labelname);
     325                 :           6 :                                 es->grouping_stack = lcons_int(1, es->grouping_stack);
     326                 :           6 :                         }
     327                 :             :                         else
     328                 :             :                         {
     329                 :           2 :                                 appendStringInfoString(es->str, "- ");
     330                 :           2 :                                 es->grouping_stack = lcons_int(0, es->grouping_stack);
     331                 :             :                         }
     332                 :           8 :                         es->indent++;
     333                 :           8 :                         break;
     334                 :             :         }
     335                 :       25149 : }
     336                 :             : 
     337                 :             : /*
     338                 :             :  * Close a group of related objects.
     339                 :             :  * Parameters must match the corresponding ExplainOpenGroup call.
     340                 :             :  */
     341                 :             : void
     342                 :       25149 : ExplainCloseGroup(const char *objtype, const char *labelname,
     343                 :             :                                   bool labeled, ExplainState *es)
     344                 :             : {
     345   [ +  +  +  + ]:       25149 :         switch (es->format)
     346                 :             :         {
     347                 :             :                 case EXPLAIN_FORMAT_TEXT:
     348                 :             :                         /* nothing to do */
     349                 :             :                         break;
     350                 :             : 
     351                 :             :                 case EXPLAIN_FORMAT_XML:
     352                 :           4 :                         es->indent--;
     353                 :           4 :                         ExplainXMLTag(objtype, X_CLOSING, es);
     354                 :           4 :                         break;
     355                 :             : 
     356                 :             :                 case EXPLAIN_FORMAT_JSON:
     357                 :         471 :                         es->indent--;
     358                 :         471 :                         appendStringInfoChar(es->str, '\n');
     359                 :         471 :                         appendStringInfoSpaces(es->str, 2 * es->indent);
     360                 :         471 :                         appendStringInfoChar(es->str, labeled ? '}' : ']');
     361                 :         471 :                         es->grouping_stack = list_delete_first(es->grouping_stack);
     362                 :         471 :                         break;
     363                 :             : 
     364                 :             :                 case EXPLAIN_FORMAT_YAML:
     365                 :           8 :                         es->indent--;
     366                 :           8 :                         es->grouping_stack = list_delete_first(es->grouping_stack);
     367                 :           8 :                         break;
     368                 :             :         }
     369                 :       25149 : }
     370                 :             : 
     371                 :             : /*
     372                 :             :  * Open a group of related objects, without emitting actual data.
     373                 :             :  *
     374                 :             :  * Prepare the formatting state as though we were beginning a group with
     375                 :             :  * the identified properties, but don't actually emit anything.  Output
     376                 :             :  * subsequent to this call can be redirected into a separate output buffer,
     377                 :             :  * and then eventually appended to the main output buffer after doing a
     378                 :             :  * regular ExplainOpenGroup call (with the same parameters).
     379                 :             :  *
     380                 :             :  * The extra "depth" parameter is the new group's depth compared to current.
     381                 :             :  * It could be more than one, in case the eventual output will be enclosed
     382                 :             :  * in additional nesting group levels.  We assume we don't need to track
     383                 :             :  * formatting state for those levels while preparing this group's output.
     384                 :             :  *
     385                 :             :  * There is no ExplainCloseSetAsideGroup --- in current usage, we always
     386                 :             :  * pop this state with ExplainSaveGroup.
     387                 :             :  */
     388                 :             : void
     389                 :          12 : ExplainOpenSetAsideGroup(const char *objtype, const char *labelname,
     390                 :             :                                                  bool labeled, int depth, ExplainState *es)
     391                 :             : {
     392   [ +  -  +  - ]:          12 :         switch (es->format)
     393                 :             :         {
     394                 :             :                 case EXPLAIN_FORMAT_TEXT:
     395                 :             :                         /* nothing to do */
     396                 :             :                         break;
     397                 :             : 
     398                 :             :                 case EXPLAIN_FORMAT_XML:
     399                 :           0 :                         es->indent += depth;
     400                 :           0 :                         break;
     401                 :             : 
     402                 :             :                 case EXPLAIN_FORMAT_JSON:
     403                 :           8 :                         es->grouping_stack = lcons_int(0, es->grouping_stack);
     404                 :           8 :                         es->indent += depth;
     405                 :           8 :                         break;
     406                 :             : 
     407                 :             :                 case EXPLAIN_FORMAT_YAML:
     408         [ #  # ]:           0 :                         if (labelname)
     409                 :           0 :                                 es->grouping_stack = lcons_int(1, es->grouping_stack);
     410                 :             :                         else
     411                 :           0 :                                 es->grouping_stack = lcons_int(0, es->grouping_stack);
     412                 :           0 :                         es->indent += depth;
     413                 :           0 :                         break;
     414                 :             :         }
     415                 :          12 : }
     416                 :             : 
     417                 :             : /*
     418                 :             :  * Pop one level of grouping state, allowing for a re-push later.
     419                 :             :  *
     420                 :             :  * This is typically used after ExplainOpenSetAsideGroup; pass the
     421                 :             :  * same "depth" used for that.
     422                 :             :  *
     423                 :             :  * This should not emit any output.  If state needs to be saved,
     424                 :             :  * save it at *state_save.  Currently, an integer save area is sufficient
     425                 :             :  * for all formats, but we might need to revisit that someday.
     426                 :             :  */
     427                 :             : void
     428                 :          24 : ExplainSaveGroup(ExplainState *es, int depth, int *state_save)
     429                 :             : {
     430   [ +  -  +  - ]:          24 :         switch (es->format)
     431                 :             :         {
     432                 :             :                 case EXPLAIN_FORMAT_TEXT:
     433                 :             :                         /* nothing to do */
     434                 :             :                         break;
     435                 :             : 
     436                 :             :                 case EXPLAIN_FORMAT_XML:
     437                 :           0 :                         es->indent -= depth;
     438                 :           0 :                         break;
     439                 :             : 
     440                 :             :                 case EXPLAIN_FORMAT_JSON:
     441                 :          20 :                         es->indent -= depth;
     442                 :          20 :                         *state_save = linitial_int(es->grouping_stack);
     443                 :          20 :                         es->grouping_stack = list_delete_first(es->grouping_stack);
     444                 :          20 :                         break;
     445                 :             : 
     446                 :             :                 case EXPLAIN_FORMAT_YAML:
     447                 :           0 :                         es->indent -= depth;
     448                 :           0 :                         *state_save = linitial_int(es->grouping_stack);
     449                 :           0 :                         es->grouping_stack = list_delete_first(es->grouping_stack);
     450                 :           0 :                         break;
     451                 :             :         }
     452                 :          24 : }
     453                 :             : 
     454                 :             : /*
     455                 :             :  * Re-push one level of grouping state, undoing the effects of ExplainSaveGroup.
     456                 :             :  */
     457                 :             : void
     458                 :          12 : ExplainRestoreGroup(ExplainState *es, int depth, int *state_save)
     459                 :             : {
     460   [ -  -  +  - ]:          12 :         switch (es->format)
     461                 :             :         {
     462                 :             :                 case EXPLAIN_FORMAT_TEXT:
     463                 :             :                         /* nothing to do */
     464                 :             :                         break;
     465                 :             : 
     466                 :             :                 case EXPLAIN_FORMAT_XML:
     467                 :           0 :                         es->indent += depth;
     468                 :           0 :                         break;
     469                 :             : 
     470                 :             :                 case EXPLAIN_FORMAT_JSON:
     471                 :          12 :                         es->grouping_stack = lcons_int(*state_save, es->grouping_stack);
     472                 :          12 :                         es->indent += depth;
     473                 :          12 :                         break;
     474                 :             : 
     475                 :             :                 case EXPLAIN_FORMAT_YAML:
     476                 :           0 :                         es->grouping_stack = lcons_int(*state_save, es->grouping_stack);
     477                 :           0 :                         es->indent += depth;
     478                 :           0 :                         break;
     479                 :             :         }
     480                 :          12 : }
     481                 :             : 
     482                 :             : /*
     483                 :             :  * Emit a "dummy" group that never has any members.
     484                 :             :  *
     485                 :             :  * objtype is the type of the group object, labelname is its label within
     486                 :             :  * a containing object (if any).
     487                 :             :  */
     488                 :             : void
     489                 :           5 : ExplainDummyGroup(const char *objtype, const char *labelname, ExplainState *es)
     490                 :             : {
     491   [ +  -  -  - ]:           5 :         switch (es->format)
     492                 :             :         {
     493                 :             :                 case EXPLAIN_FORMAT_TEXT:
     494                 :             :                         /* nothing to do */
     495                 :             :                         break;
     496                 :             : 
     497                 :             :                 case EXPLAIN_FORMAT_XML:
     498                 :           0 :                         ExplainXMLTag(objtype, X_CLOSE_IMMEDIATE, es);
     499                 :           0 :                         break;
     500                 :             : 
     501                 :             :                 case EXPLAIN_FORMAT_JSON:
     502                 :           0 :                         ExplainJSONLineEnding(es);
     503                 :           0 :                         appendStringInfoSpaces(es->str, 2 * es->indent);
     504         [ #  # ]:           0 :                         if (labelname)
     505                 :             :                         {
     506                 :           0 :                                 escape_json(es->str, labelname);
     507                 :           0 :                                 appendStringInfoString(es->str, ": ");
     508                 :           0 :                         }
     509                 :           0 :                         escape_json(es->str, objtype);
     510                 :           0 :                         break;
     511                 :             : 
     512                 :             :                 case EXPLAIN_FORMAT_YAML:
     513                 :           0 :                         ExplainYAMLLineStarting(es);
     514         [ #  # ]:           0 :                         if (labelname)
     515                 :             :                         {
     516                 :           0 :                                 escape_yaml(es->str, labelname);
     517                 :           0 :                                 appendStringInfoString(es->str, ": ");
     518                 :           0 :                         }
     519                 :             :                         else
     520                 :             :                         {
     521                 :           0 :                                 appendStringInfoString(es->str, "- ");
     522                 :             :                         }
     523                 :           0 :                         escape_yaml(es->str, objtype);
     524                 :           0 :                         break;
     525                 :             :         }
     526                 :           5 : }
     527                 :             : 
     528                 :             : /*
     529                 :             :  * Emit the start-of-output boilerplate.
     530                 :             :  *
     531                 :             :  * This is just enough different from processing a subgroup that we need
     532                 :             :  * a separate pair of subroutines.
     533                 :             :  */
     534                 :             : void
     535                 :        3632 : ExplainBeginOutput(ExplainState *es)
     536                 :             : {
     537   [ +  +  +  + ]:        3632 :         switch (es->format)
     538                 :             :         {
     539                 :             :                 case EXPLAIN_FORMAT_TEXT:
     540                 :             :                         /* nothing to do */
     541                 :             :                         break;
     542                 :             : 
     543                 :             :                 case EXPLAIN_FORMAT_XML:
     544                 :           1 :                         appendStringInfoString(es->str,
     545                 :             :                                                                    "<explain xmlns=\"http://www.postgresql.org/2009/explain\">\n");
     546                 :           1 :                         es->indent++;
     547                 :           1 :                         break;
     548                 :             : 
     549                 :             :                 case EXPLAIN_FORMAT_JSON:
     550                 :             :                         /* top-level structure is an array of plans */
     551                 :          41 :                         appendStringInfoChar(es->str, '[');
     552                 :          41 :                         es->grouping_stack = lcons_int(0, es->grouping_stack);
     553                 :          41 :                         es->indent++;
     554                 :          41 :                         break;
     555                 :             : 
     556                 :             :                 case EXPLAIN_FORMAT_YAML:
     557                 :           2 :                         es->grouping_stack = lcons_int(0, es->grouping_stack);
     558                 :           2 :                         break;
     559                 :             :         }
     560                 :        3632 : }
     561                 :             : 
     562                 :             : /*
     563                 :             :  * Emit the end-of-output boilerplate.
     564                 :             :  */
     565                 :             : void
     566                 :        3613 : ExplainEndOutput(ExplainState *es)
     567                 :             : {
     568   [ +  +  +  + ]:        3613 :         switch (es->format)
     569                 :             :         {
     570                 :             :                 case EXPLAIN_FORMAT_TEXT:
     571                 :             :                         /* nothing to do */
     572                 :             :                         break;
     573                 :             : 
     574                 :             :                 case EXPLAIN_FORMAT_XML:
     575                 :           1 :                         es->indent--;
     576                 :           1 :                         appendStringInfoString(es->str, "</explain>");
     577                 :           1 :                         break;
     578                 :             : 
     579                 :             :                 case EXPLAIN_FORMAT_JSON:
     580                 :          41 :                         es->indent--;
     581                 :          41 :                         appendStringInfoString(es->str, "\n]");
     582                 :          41 :                         es->grouping_stack = list_delete_first(es->grouping_stack);
     583                 :          41 :                         break;
     584                 :             : 
     585                 :             :                 case EXPLAIN_FORMAT_YAML:
     586                 :           2 :                         es->grouping_stack = list_delete_first(es->grouping_stack);
     587                 :           2 :                         break;
     588                 :             :         }
     589                 :        3613 : }
     590                 :             : 
     591                 :             : /*
     592                 :             :  * Put an appropriate separator between multiple plans
     593                 :             :  */
     594                 :             : void
     595                 :           2 : ExplainSeparatePlans(ExplainState *es)
     596                 :             : {
     597      [ -  -  + ]:           2 :         switch (es->format)
     598                 :             :         {
     599                 :             :                 case EXPLAIN_FORMAT_TEXT:
     600                 :             :                         /* add a blank line */
     601                 :           2 :                         appendStringInfoChar(es->str, '\n');
     602                 :           2 :                         break;
     603                 :             : 
     604                 :             :                 case EXPLAIN_FORMAT_XML:
     605                 :             :                 case EXPLAIN_FORMAT_JSON:
     606                 :             :                 case EXPLAIN_FORMAT_YAML:
     607                 :             :                         /* nothing to do */
     608                 :           0 :                         break;
     609                 :             :         }
     610                 :           2 : }
     611                 :             : 
     612                 :             : /*
     613                 :             :  * Emit opening or closing XML tag.
     614                 :             :  *
     615                 :             :  * "flags" must contain X_OPENING, X_CLOSING, or X_CLOSE_IMMEDIATE.
     616                 :             :  * Optionally, OR in X_NOWHITESPACE to suppress the whitespace we'd normally
     617                 :             :  * add.
     618                 :             :  *
     619                 :             :  * XML restricts tag names more than our other output formats, eg they can't
     620                 :             :  * contain white space or slashes.  Replace invalid characters with dashes,
     621                 :             :  * so that for example "I/O Read Time" becomes "I-O-Read-Time".
     622                 :             :  */
     623                 :             : static void
     624                 :          80 : ExplainXMLTag(const char *tagname, int flags, ExplainState *es)
     625                 :             : {
     626                 :          80 :         const char *s;
     627                 :          80 :         const char *valid = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.";
     628                 :             : 
     629         [ +  + ]:          80 :         if ((flags & X_NOWHITESPACE) == 0)
     630                 :           8 :                 appendStringInfoSpaces(es->str, 2 * es->indent);
     631         [ -  + ]:          80 :         appendStringInfoCharMacro(es->str, '<');
     632         [ +  + ]:          80 :         if ((flags & X_CLOSING) != 0)
     633         [ -  + ]:          40 :                 appendStringInfoCharMacro(es->str, '/');
     634         [ +  + ]:        1248 :         for (s = tagname; *s; s++)
     635         [ +  + ]:        1168 :                 appendStringInfoChar(es->str, strchr(valid, *s) ? *s : '-');
     636         [ +  - ]:          80 :         if ((flags & X_CLOSE_IMMEDIATE) != 0)
     637                 :           0 :                 appendStringInfoString(es->str, " /");
     638         [ -  + ]:          80 :         appendStringInfoCharMacro(es->str, '>');
     639         [ +  + ]:          80 :         if ((flags & X_NOWHITESPACE) == 0)
     640         [ -  + ]:           8 :                 appendStringInfoCharMacro(es->str, '\n');
     641                 :          80 : }
     642                 :             : 
     643                 :             : /*
     644                 :             :  * Indent a text-format line.
     645                 :             :  *
     646                 :             :  * We indent by two spaces per indentation level.  However, when emitting
     647                 :             :  * data for a parallel worker there might already be data on the current line
     648                 :             :  * (cf. ExplainOpenWorker); in that case, don't indent any more.
     649                 :             :  */
     650                 :             : void
     651                 :       22439 : ExplainIndentText(ExplainState *es)
     652                 :             : {
     653         [ +  - ]:       22439 :         Assert(es->format == EXPLAIN_FORMAT_TEXT);
     654   [ +  +  +  + ]:       22439 :         if (es->str->len == 0 || es->str->data[es->str->len - 1] == '\n')
     655                 :       22435 :                 appendStringInfoSpaces(es->str, es->indent * 2);
     656                 :       22439 : }
     657                 :             : 
     658                 :             : /*
     659                 :             :  * Emit a JSON line ending.
     660                 :             :  *
     661                 :             :  * JSON requires a comma after each property but the last.  To facilitate this,
     662                 :             :  * in JSON format, the text emitted for each property begins just prior to the
     663                 :             :  * preceding line-break (and comma, if applicable).
     664                 :             :  */
     665                 :             : static void
     666                 :        4993 : ExplainJSONLineEnding(ExplainState *es)
     667                 :             : {
     668         [ +  - ]:        4993 :         Assert(es->format == EXPLAIN_FORMAT_JSON);
     669         [ +  + ]:        4993 :         if (linitial_int(es->grouping_stack) != 0)
     670                 :        4580 :                 appendStringInfoChar(es->str, ',');
     671                 :             :         else
     672                 :         413 :                 linitial_int(es->grouping_stack) = 1;
     673                 :        4993 :         appendStringInfoChar(es->str, '\n');
     674                 :        4993 : }
     675                 :             : 
     676                 :             : /*
     677                 :             :  * Indent a YAML line.
     678                 :             :  *
     679                 :             :  * YAML lines are ordinarily indented by two spaces per indentation level.
     680                 :             :  * The text emitted for each property begins just prior to the preceding
     681                 :             :  * line-break, except for the first property in an unlabeled group, for which
     682                 :             :  * it begins immediately after the "- " that introduces the group.  The first
     683                 :             :  * property of the group appears on the same line as the opening "- ".
     684                 :             :  */
     685                 :             : static void
     686                 :          70 : ExplainYAMLLineStarting(ExplainState *es)
     687                 :             : {
     688         [ +  - ]:          70 :         Assert(es->format == EXPLAIN_FORMAT_YAML);
     689         [ +  + ]:          70 :         if (linitial_int(es->grouping_stack) == 0)
     690                 :             :         {
     691                 :           4 :                 linitial_int(es->grouping_stack) = 1;
     692                 :           4 :         }
     693                 :             :         else
     694                 :             :         {
     695                 :          66 :                 appendStringInfoChar(es->str, '\n');
     696                 :          66 :                 appendStringInfoSpaces(es->str, es->indent * 2);
     697                 :             :         }
     698                 :          70 : }
     699                 :             : 
     700                 :             : /*
     701                 :             :  * YAML is a superset of JSON; unfortunately, the YAML quoting rules are
     702                 :             :  * ridiculously complicated -- as documented in sections 5.3 and 7.3.3 of
     703                 :             :  * http://yaml.org/spec/1.2/spec.html -- so we chose to just quote everything.
     704                 :             :  * Empty strings, strings with leading or trailing whitespace, and strings
     705                 :             :  * containing a variety of special characters must certainly be quoted or the
     706                 :             :  * output is invalid; and other seemingly harmless strings like "0xa" or
     707                 :             :  * "true" must be quoted, lest they be interpreted as a hexadecimal or Boolean
     708                 :             :  * constant rather than a string.
     709                 :             :  */
     710                 :             : static void
     711                 :           7 : escape_yaml(StringInfo buf, const char *str)
     712                 :             : {
     713                 :           7 :         escape_json(buf, str);
     714                 :           7 : }
        

Generated by: LCOV version 2.3.2-1