LCOV - code coverage report
Current view: top level - src/backend/commands - explain_format.c (source / functions) Coverage Total Hit
Test: PostgreSQL 20devel Lines: 76.1 % 326 248
Test Date: 2026-07-03 19:57:34 Functions: 100.0 % 22 22
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 59.6 % 151 90

             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                 :       13981 : ExplainPropertyList(const char *qlabel, List *data, ExplainState *es)
      39                 :             : {
      40                 :             :     ListCell   *lc;
      41                 :       13981 :     bool        first = true;
      42                 :             : 
      43   [ +  +  +  -  :       13981 :     switch (es->format)
                      - ]
      44                 :             :     {
      45                 :       13869 :         case EXPLAIN_FORMAT_TEXT:
      46                 :       13869 :             ExplainIndentText(es);
      47                 :       13869 :             appendStringInfo(es->str, "%s: ", qlabel);
      48   [ +  -  +  +  :       41575 :             foreach(lc, data)
                   +  + ]
      49                 :             :             {
      50         [ +  + ]:       27706 :                 if (!first)
      51                 :       13837 :                     appendStringInfoString(es->str, ", ");
      52                 :       27706 :                 appendStringInfoString(es->str, (const char *) lfirst(lc));
      53                 :       27706 :                 first = false;
      54                 :             :             }
      55                 :       13869 :             appendStringInfoChar(es->str, '\n');
      56                 :       13869 :             break;
      57                 :             : 
      58                 :           2 :         case EXPLAIN_FORMAT_XML:
      59                 :           2 :             ExplainXMLTag(qlabel, X_OPENING, es);
      60   [ +  -  +  +  :           5 :             foreach(lc, data)
                   +  + ]
      61                 :             :             {
      62                 :             :                 char       *str;
      63                 :             : 
      64                 :           3 :                 appendStringInfoSpaces(es->str, es->indent * 2 + 2);
      65                 :           3 :                 appendStringInfoString(es->str, "<Item>");
      66                 :           3 :                 str = escape_xml((const char *) lfirst(lc));
      67                 :           3 :                 appendStringInfoString(es->str, str);
      68                 :           3 :                 pfree(str);
      69                 :           3 :                 appendStringInfoString(es->str, "</Item>\n");
      70                 :             :             }
      71                 :           2 :             ExplainXMLTag(qlabel, X_CLOSING, es);
      72                 :           2 :             break;
      73                 :             : 
      74                 :         110 :         case EXPLAIN_FORMAT_JSON:
      75                 :         110 :             ExplainJSONLineEnding(es);
      76                 :         110 :             appendStringInfoSpaces(es->str, es->indent * 2);
      77                 :         110 :             escape_json(es->str, qlabel);
      78                 :         110 :             appendStringInfoString(es->str, ": [");
      79   [ +  -  +  +  :         449 :             foreach(lc, data)
                   +  + ]
      80                 :             :             {
      81         [ +  + ]:         339 :                 if (!first)
      82                 :         229 :                     appendStringInfoString(es->str, ", ");
      83                 :         339 :                 escape_json(es->str, (const char *) lfirst(lc));
      84                 :         339 :                 first = false;
      85                 :             :             }
      86                 :         110 :             appendStringInfoChar(es->str, ']');
      87                 :         110 :             break;
      88                 :             : 
      89                 :           0 :         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                 :             :             }
      99                 :           0 :             break;
     100                 :             :     }
     101                 :       13981 : }
     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                 :         500 : ExplainPropertyListNested(const char *qlabel, List *data, ExplainState *es)
     109                 :             : {
     110                 :             :     ListCell   *lc;
     111                 :         500 :     bool        first = true;
     112                 :             : 
     113   [ +  -  -  - ]:         500 :     switch (es->format)
     114                 :             :     {
     115                 :         500 :         case EXPLAIN_FORMAT_TEXT:
     116                 :             :         case EXPLAIN_FORMAT_XML:
     117                 :         500 :             ExplainPropertyList(qlabel, data, es);
     118                 :         500 :             return;
     119                 :             : 
     120                 :           0 :         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                 :             :             }
     131                 :           0 :             appendStringInfoChar(es->str, ']');
     132                 :           0 :             break;
     133                 :             : 
     134                 :           0 :         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                 :             :             }
     144                 :           0 :             appendStringInfoChar(es->str, ']');
     145                 :           0 :             break;
     146                 :             :     }
     147                 :             : }
     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                 :       52951 : ExplainProperty(const char *qlabel, const char *unit, const char *value,
     162                 :             :                 bool numeric, ExplainState *es)
     163                 :             : {
     164   [ +  +  +  +  :       52951 :     switch (es->format)
                      - ]
     165                 :             :     {
     166                 :       33498 :         case EXPLAIN_FORMAT_TEXT:
     167                 :       33498 :             ExplainIndentText(es);
     168         [ +  + ]:       33498 :             if (unit)
     169                 :        3240 :                 appendStringInfo(es->str, "%s: %s %s\n", qlabel, value, unit);
     170                 :             :             else
     171                 :       30258 :                 appendStringInfo(es->str, "%s: %s\n", qlabel, value);
     172                 :       33498 :             break;
     173                 :             : 
     174                 :         280 :         case EXPLAIN_FORMAT_XML:
     175                 :             :             {
     176                 :             :                 char       *str;
     177                 :             : 
     178                 :         280 :                 appendStringInfoSpaces(es->str, es->indent * 2);
     179                 :         280 :                 ExplainXMLTag(qlabel, X_OPENING | X_NOWHITESPACE, es);
     180                 :         280 :                 str = escape_xml(value);
     181                 :         280 :                 appendStringInfoString(es->str, str);
     182                 :         280 :                 pfree(str);
     183                 :         280 :                 ExplainXMLTag(qlabel, X_CLOSING | X_NOWHITESPACE, es);
     184                 :         280 :                 appendStringInfoChar(es->str, '\n');
     185                 :             :             }
     186                 :         280 :             break;
     187                 :             : 
     188                 :       18897 :         case EXPLAIN_FORMAT_JSON:
     189                 :       18897 :             ExplainJSONLineEnding(es);
     190                 :       18897 :             appendStringInfoSpaces(es->str, es->indent * 2);
     191                 :       18897 :             escape_json(es->str, qlabel);
     192                 :       18897 :             appendStringInfoString(es->str, ": ");
     193         [ +  + ]:       18897 :             if (numeric)
     194                 :       16425 :                 appendStringInfoString(es->str, value);
     195                 :             :             else
     196                 :        2472 :                 escape_json(es->str, value);
     197                 :       18897 :             break;
     198                 :             : 
     199                 :         276 :         case EXPLAIN_FORMAT_YAML:
     200                 :         276 :             ExplainYAMLLineStarting(es);
     201                 :         276 :             appendStringInfo(es->str, "%s: ", qlabel);
     202         [ +  + ]:         276 :             if (numeric)
     203                 :         248 :                 appendStringInfoString(es->str, value);
     204                 :             :             else
     205                 :          28 :                 escape_yaml(es->str, value);
     206                 :         276 :             break;
     207                 :             :     }
     208                 :       52951 : }
     209                 :             : 
     210                 :             : /*
     211                 :             :  * Explain a string-valued property.
     212                 :             :  */
     213                 :             : void
     214                 :       29781 : ExplainPropertyText(const char *qlabel, const char *value, ExplainState *es)
     215                 :             : {
     216                 :       29781 :     ExplainProperty(qlabel, NULL, value, false, es);
     217                 :       29781 : }
     218                 :             : 
     219                 :             : /*
     220                 :             :  * Explain an integer-valued property.
     221                 :             :  */
     222                 :             : void
     223                 :       10127 : ExplainPropertyInteger(const char *qlabel, const char *unit, int64 value,
     224                 :             :                        ExplainState *es)
     225                 :             : {
     226                 :             :     char        buf[32];
     227                 :             : 
     228                 :       10127 :     snprintf(buf, sizeof(buf), INT64_FORMAT, value);
     229                 :       10127 :     ExplainProperty(qlabel, unit, buf, true, es);
     230                 :       10127 : }
     231                 :             : 
     232                 :             : /*
     233                 :             :  * Explain an unsigned integer-valued property.
     234                 :             :  */
     235                 :             : void
     236                 :        1046 : ExplainPropertyUInteger(const char *qlabel, const char *unit, uint64 value,
     237                 :             :                         ExplainState *es)
     238                 :             : {
     239                 :             :     char        buf[32];
     240                 :             : 
     241                 :        1046 :     snprintf(buf, sizeof(buf), UINT64_FORMAT, value);
     242                 :        1046 :     ExplainProperty(qlabel, unit, buf, true, es);
     243                 :        1046 : }
     244                 :             : 
     245                 :             : /*
     246                 :             :  * Explain a float-valued property, using the specified number of
     247                 :             :  * fractional digits.
     248                 :             :  */
     249                 :             : void
     250                 :        9445 : ExplainPropertyFloat(const char *qlabel, const char *unit, double value,
     251                 :             :                      int ndigits, ExplainState *es)
     252                 :             : {
     253                 :             :     char       *buf;
     254                 :             : 
     255                 :        9445 :     buf = psprintf("%.*f", ndigits, value);
     256                 :        9445 :     ExplainProperty(qlabel, unit, buf, true, es);
     257                 :        9445 :     pfree(buf);
     258                 :        9445 : }
     259                 :             : 
     260                 :             : /*
     261                 :             :  * Explain a bool-valued property.
     262                 :             :  */
     263                 :             : void
     264                 :        2552 : ExplainPropertyBool(const char *qlabel, bool value, ExplainState *es)
     265                 :             : {
     266         [ +  + ]:        2552 :     ExplainProperty(qlabel, NULL, value ? "true" : "false", true, es);
     267                 :        2552 : }
     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                 :      111293 : ExplainOpenGroup(const char *objtype, const char *labelname,
     280                 :             :                  bool labeled, ExplainState *es)
     281                 :             : {
     282   [ +  +  +  +  :      111293 :     switch (es->format)
                      - ]
     283                 :             :     {
     284                 :      109255 :         case EXPLAIN_FORMAT_TEXT:
     285                 :             :             /* nothing to do */
     286                 :      109255 :             break;
     287                 :             : 
     288                 :          31 :         case EXPLAIN_FORMAT_XML:
     289                 :          31 :             ExplainXMLTag(objtype, X_OPENING, es);
     290                 :          31 :             es->indent++;
     291                 :          31 :             break;
     292                 :             : 
     293                 :        1975 :         case EXPLAIN_FORMAT_JSON:
     294                 :        1975 :             ExplainJSONLineEnding(es);
     295                 :        1975 :             appendStringInfoSpaces(es->str, 2 * es->indent);
     296         [ +  + ]:        1975 :             if (labelname)
     297                 :             :             {
     298                 :        1237 :                 escape_json(es->str, labelname);
     299                 :        1237 :                 appendStringInfoString(es->str, ": ");
     300                 :             :             }
     301         [ +  + ]:        1975 :             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                 :        1975 :             es->grouping_stack = lcons_int(0, es->grouping_stack);
     310                 :        1975 :             es->indent++;
     311                 :        1975 :             break;
     312                 :             : 
     313                 :          32 :         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                 :          32 :             ExplainYAMLLineStarting(es);
     322         [ +  + ]:          32 :             if (labelname)
     323                 :             :             {
     324                 :          24 :                 appendStringInfo(es->str, "%s: ", labelname);
     325                 :          24 :                 es->grouping_stack = lcons_int(1, es->grouping_stack);
     326                 :             :             }
     327                 :             :             else
     328                 :             :             {
     329                 :           8 :                 appendStringInfoString(es->str, "- ");
     330                 :           8 :                 es->grouping_stack = lcons_int(0, es->grouping_stack);
     331                 :             :             }
     332                 :          32 :             es->indent++;
     333                 :          32 :             break;
     334                 :             :     }
     335                 :      111293 : }
     336                 :             : 
     337                 :             : /*
     338                 :             :  * Close a group of related objects.
     339                 :             :  * Parameters must match the corresponding ExplainOpenGroup call.
     340                 :             :  */
     341                 :             : void
     342                 :      111293 : ExplainCloseGroup(const char *objtype, const char *labelname,
     343                 :             :                   bool labeled, ExplainState *es)
     344                 :             : {
     345   [ +  +  +  +  :      111293 :     switch (es->format)
                      - ]
     346                 :             :     {
     347                 :      109255 :         case EXPLAIN_FORMAT_TEXT:
     348                 :             :             /* nothing to do */
     349                 :      109255 :             break;
     350                 :             : 
     351                 :          31 :         case EXPLAIN_FORMAT_XML:
     352                 :          31 :             es->indent--;
     353                 :          31 :             ExplainXMLTag(objtype, X_CLOSING, es);
     354                 :          31 :             break;
     355                 :             : 
     356                 :        1975 :         case EXPLAIN_FORMAT_JSON:
     357                 :        1975 :             es->indent--;
     358                 :        1975 :             appendStringInfoChar(es->str, '\n');
     359                 :        1975 :             appendStringInfoSpaces(es->str, 2 * es->indent);
     360         [ +  + ]:        1975 :             appendStringInfoChar(es->str, labeled ? '}' : ']');
     361                 :        1975 :             es->grouping_stack = list_delete_first(es->grouping_stack);
     362                 :        1975 :             break;
     363                 :             : 
     364                 :          32 :         case EXPLAIN_FORMAT_YAML:
     365                 :          32 :             es->indent--;
     366                 :          32 :             es->grouping_stack = list_delete_first(es->grouping_stack);
     367                 :          32 :             break;
     368                 :             :     }
     369                 :      111293 : }
     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                 :          48 : ExplainOpenSetAsideGroup(const char *objtype, const char *labelname,
     390                 :             :                          bool labeled, int depth, ExplainState *es)
     391                 :             : {
     392   [ +  -  +  -  :          48 :     switch (es->format)
                      - ]
     393                 :             :     {
     394                 :          16 :         case EXPLAIN_FORMAT_TEXT:
     395                 :             :             /* nothing to do */
     396                 :          16 :             break;
     397                 :             : 
     398                 :           0 :         case EXPLAIN_FORMAT_XML:
     399                 :           0 :             es->indent += depth;
     400                 :           0 :             break;
     401                 :             : 
     402                 :          32 :         case EXPLAIN_FORMAT_JSON:
     403                 :          32 :             es->grouping_stack = lcons_int(0, es->grouping_stack);
     404                 :          32 :             es->indent += depth;
     405                 :          32 :             break;
     406                 :             : 
     407                 :           0 :         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                 :          48 : }
     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                 :          96 : ExplainSaveGroup(ExplainState *es, int depth, int *state_save)
     429                 :             : {
     430   [ +  -  +  -  :          96 :     switch (es->format)
                      - ]
     431                 :             :     {
     432                 :          16 :         case EXPLAIN_FORMAT_TEXT:
     433                 :             :             /* nothing to do */
     434                 :          16 :             break;
     435                 :             : 
     436                 :           0 :         case EXPLAIN_FORMAT_XML:
     437                 :           0 :             es->indent -= depth;
     438                 :           0 :             break;
     439                 :             : 
     440                 :          80 :         case EXPLAIN_FORMAT_JSON:
     441                 :          80 :             es->indent -= depth;
     442                 :          80 :             *state_save = linitial_int(es->grouping_stack);
     443                 :          80 :             es->grouping_stack = list_delete_first(es->grouping_stack);
     444                 :          80 :             break;
     445                 :             : 
     446                 :           0 :         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                 :          96 : }
     453                 :             : 
     454                 :             : /*
     455                 :             :  * Re-push one level of grouping state, undoing the effects of ExplainSaveGroup.
     456                 :             :  */
     457                 :             : void
     458                 :          48 : ExplainRestoreGroup(ExplainState *es, int depth, int *state_save)
     459                 :             : {
     460   [ -  -  +  -  :          48 :     switch (es->format)
                      - ]
     461                 :             :     {
     462                 :           0 :         case EXPLAIN_FORMAT_TEXT:
     463                 :             :             /* nothing to do */
     464                 :           0 :             break;
     465                 :             : 
     466                 :           0 :         case EXPLAIN_FORMAT_XML:
     467                 :           0 :             es->indent += depth;
     468                 :           0 :             break;
     469                 :             : 
     470                 :          48 :         case EXPLAIN_FORMAT_JSON:
     471                 :          48 :             es->grouping_stack = lcons_int(*state_save, es->grouping_stack);
     472                 :          48 :             es->indent += depth;
     473                 :          48 :             break;
     474                 :             : 
     475                 :           0 :         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                 :          48 : }
     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                 :          20 : ExplainDummyGroup(const char *objtype, const char *labelname, ExplainState *es)
     490                 :             : {
     491   [ +  -  -  -  :          20 :     switch (es->format)
                      - ]
     492                 :             :     {
     493                 :          20 :         case EXPLAIN_FORMAT_TEXT:
     494                 :             :             /* nothing to do */
     495                 :          20 :             break;
     496                 :             : 
     497                 :           0 :         case EXPLAIN_FORMAT_XML:
     498                 :           0 :             ExplainXMLTag(objtype, X_CLOSE_IMMEDIATE, es);
     499                 :           0 :             break;
     500                 :             : 
     501                 :           0 :         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                 :             :             }
     509                 :           0 :             escape_json(es->str, objtype);
     510                 :           0 :             break;
     511                 :             : 
     512                 :           0 :         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                 :             :             }
     519                 :             :             else
     520                 :             :             {
     521                 :           0 :                 appendStringInfoString(es->str, "- ");
     522                 :             :             }
     523                 :           0 :             escape_yaml(es->str, objtype);
     524                 :           0 :             break;
     525                 :             :     }
     526                 :          20 : }
     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                 :       16530 : ExplainBeginOutput(ExplainState *es)
     536                 :             : {
     537   [ +  +  +  +  :       16530 :     switch (es->format)
                      - ]
     538                 :             :     {
     539                 :       16332 :         case EXPLAIN_FORMAT_TEXT:
     540                 :             :             /* nothing to do */
     541                 :       16332 :             break;
     542                 :             : 
     543                 :           5 :         case EXPLAIN_FORMAT_XML:
     544                 :           5 :             appendStringInfoString(es->str,
     545                 :             :                                    "<explain xmlns=\"http://www.postgresql.org/2009/explain\">\n");
     546                 :           5 :             es->indent++;
     547                 :           5 :             break;
     548                 :             : 
     549                 :         185 :         case EXPLAIN_FORMAT_JSON:
     550                 :             :             /* top-level structure is an array of plans */
     551                 :         185 :             appendStringInfoChar(es->str, '[');
     552                 :         185 :             es->grouping_stack = lcons_int(0, es->grouping_stack);
     553                 :         185 :             es->indent++;
     554                 :         185 :             break;
     555                 :             : 
     556                 :           8 :         case EXPLAIN_FORMAT_YAML:
     557                 :           8 :             es->grouping_stack = lcons_int(0, es->grouping_stack);
     558                 :           8 :             break;
     559                 :             :     }
     560                 :       16530 : }
     561                 :             : 
     562                 :             : /*
     563                 :             :  * Emit the end-of-output boilerplate.
     564                 :             :  */
     565                 :             : void
     566                 :       16452 : ExplainEndOutput(ExplainState *es)
     567                 :             : {
     568   [ +  +  +  +  :       16452 :     switch (es->format)
                      - ]
     569                 :             :     {
     570                 :       16254 :         case EXPLAIN_FORMAT_TEXT:
     571                 :             :             /* nothing to do */
     572                 :       16254 :             break;
     573                 :             : 
     574                 :           5 :         case EXPLAIN_FORMAT_XML:
     575                 :           5 :             es->indent--;
     576                 :           5 :             appendStringInfoString(es->str, "</explain>");
     577                 :           5 :             break;
     578                 :             : 
     579                 :         185 :         case EXPLAIN_FORMAT_JSON:
     580                 :         185 :             es->indent--;
     581                 :         185 :             appendStringInfoString(es->str, "\n]");
     582                 :         185 :             es->grouping_stack = list_delete_first(es->grouping_stack);
     583                 :         185 :             break;
     584                 :             : 
     585                 :           8 :         case EXPLAIN_FORMAT_YAML:
     586                 :           8 :             es->grouping_stack = list_delete_first(es->grouping_stack);
     587                 :           8 :             break;
     588                 :             :     }
     589                 :       16452 : }
     590                 :             : 
     591                 :             : /*
     592                 :             :  * Put an appropriate separator between multiple plans
     593                 :             :  */
     594                 :             : void
     595                 :           8 : ExplainSeparatePlans(ExplainState *es)
     596                 :             : {
     597      [ +  -  - ]:           8 :     switch (es->format)
     598                 :             :     {
     599                 :           8 :         case EXPLAIN_FORMAT_TEXT:
     600                 :             :             /* add a blank line */
     601                 :           8 :             appendStringInfoChar(es->str, '\n');
     602                 :           8 :             break;
     603                 :             : 
     604                 :           0 :         case EXPLAIN_FORMAT_XML:
     605                 :             :         case EXPLAIN_FORMAT_JSON:
     606                 :             :         case EXPLAIN_FORMAT_YAML:
     607                 :             :             /* nothing to do */
     608                 :           0 :             break;
     609                 :             :     }
     610                 :           8 : }
     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                 :         626 : ExplainXMLTag(const char *tagname, int flags, ExplainState *es)
     625                 :             : {
     626                 :             :     const char *s;
     627                 :         626 :     const char *valid = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.";
     628                 :             : 
     629         [ +  + ]:         626 :     if ((flags & X_NOWHITESPACE) == 0)
     630                 :          66 :         appendStringInfoSpaces(es->str, 2 * es->indent);
     631         [ -  + ]:         626 :     appendStringInfoCharMacro(es->str, '<');
     632         [ +  + ]:         626 :     if ((flags & X_CLOSING) != 0)
     633         [ -  + ]:         313 :         appendStringInfoCharMacro(es->str, '/');
     634         [ +  + ]:        8920 :     for (s = tagname; *s; s++)
     635         [ +  + ]:        8294 :         appendStringInfoChar(es->str, strchr(valid, *s) ? *s : '-');
     636         [ -  + ]:         626 :     if ((flags & X_CLOSE_IMMEDIATE) != 0)
     637                 :           0 :         appendStringInfoString(es->str, " /");
     638         [ +  + ]:         626 :     appendStringInfoCharMacro(es->str, '>');
     639         [ +  + ]:         626 :     if ((flags & X_NOWHITESPACE) == 0)
     640         [ -  + ]:          66 :         appendStringInfoCharMacro(es->str, '\n');
     641                 :         626 : }
     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                 :       95948 : ExplainIndentText(ExplainState *es)
     652                 :             : {
     653                 :             :     Assert(es->format == EXPLAIN_FORMAT_TEXT);
     654   [ +  +  +  + ]:       95948 :     if (es->str->len == 0 || es->str->data[es->str->len - 1] == '\n')
     655                 :       95932 :         appendStringInfoSpaces(es->str, es->indent * 2);
     656                 :       95948 : }
     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                 :       20982 : ExplainJSONLineEnding(ExplainState *es)
     667                 :             : {
     668                 :             :     Assert(es->format == EXPLAIN_FORMAT_JSON);
     669         [ +  + ]:       20982 :     if (linitial_int(es->grouping_stack) != 0)
     670                 :       19238 :         appendStringInfoChar(es->str, ',');
     671                 :             :     else
     672                 :        1744 :         linitial_int(es->grouping_stack) = 1;
     673                 :       20982 :     appendStringInfoChar(es->str, '\n');
     674                 :       20982 : }
     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                 :         308 : ExplainYAMLLineStarting(ExplainState *es)
     687                 :             : {
     688                 :             :     Assert(es->format == EXPLAIN_FORMAT_YAML);
     689         [ +  + ]:         308 :     if (linitial_int(es->grouping_stack) == 0)
     690                 :             :     {
     691                 :          16 :         linitial_int(es->grouping_stack) = 1;
     692                 :             :     }
     693                 :             :     else
     694                 :             :     {
     695                 :         292 :         appendStringInfoChar(es->str, '\n');
     696                 :         292 :         appendStringInfoSpaces(es->str, es->indent * 2);
     697                 :             :     }
     698                 :         308 : }
     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                 :          28 : escape_yaml(StringInfo buf, const char *str)
     712                 :             : {
     713                 :          28 :     escape_json(buf, str);
     714                 :          28 : }
        

Generated by: LCOV version 2.0-1