LCOV - differential code coverage report
Current view: top level - src/backend/commands - explain_state.c (source / functions) Coverage Total Hit UBC CBC
Current: c70b6db34ffeab48beef1fb4ce61bcad3772b8dd vs 06473f5a344df8c9594ead90a609b86f6724cff8 Lines: 80.8 % 130 105 25 105
Current Date: 2025-09-06 07:49:51 +0900 Functions: 100.0 % 7 7 7
Baseline: lcov-20250906-005545-baseline Branches: 71.2 % 118 84 34 84
Baseline Date: 2025-09-05 08:21:35 +0100 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(30,360] days: 80.8 % 130 105 25 105
Function coverage date bins:
(30,360] days: 100.0 % 7 7 7
Branch coverage date bins:
(30,360] days: 71.2 % 118 84 34 84

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*-------------------------------------------------------------------------
                                  2                 :                :  *
                                  3                 :                :  * explain_state.c
                                  4                 :                :  *    Code for initializing and accessing ExplainState objects
                                  5                 :                :  *
                                  6                 :                :  * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
                                  7                 :                :  * Portions Copyright (c) 1994-5, Regents of the University of California
                                  8                 :                :  *
                                  9                 :                :  * In-core options have hard-coded fields inside ExplainState; e.g. if
                                 10                 :                :  * the user writes EXPLAIN (BUFFERS) then ExplainState's "buffers" member
                                 11                 :                :  * will be set to true. Extensions can also register options using
                                 12                 :                :  * RegisterExtensionExplainOption; so that e.g. EXPLAIN (BICYCLE 'red')
                                 13                 :                :  * will invoke a designated handler that knows what the legal values are
                                 14                 :                :  * for the BICYCLE option. However, it's not enough for an extension to be
                                 15                 :                :  * able to parse new options: it also needs a place to store the results
                                 16                 :                :  * of that parsing, and an ExplainState has no 'bicycle' field.
                                 17                 :                :  *
                                 18                 :                :  * To solve this problem, an ExplainState can contain an array of opaque
                                 19                 :                :  * pointers, one per extension. An extension can use GetExplainExtensionId
                                 20                 :                :  * to acquire an integer ID to acquire an offset into this array that is
                                 21                 :                :  * reserved for its exclusive use, and then use GetExplainExtensionState
                                 22                 :                :  * and SetExplainExtensionState to read and write its own private state
                                 23                 :                :  * within an ExplainState.
                                 24                 :                :  *
                                 25                 :                :  * Note that there is no requirement that the name of the option match
                                 26                 :                :  * the name of the extension; e.g. a pg_explain_conveyance extension could
                                 27                 :                :  * implement options for BICYCLE, MONORAIL, etc.
                                 28                 :                :  *
                                 29                 :                :  * IDENTIFICATION
                                 30                 :                :  *    src/backend/commands/explain_state.c
                                 31                 :                :  *
                                 32                 :                :  *-------------------------------------------------------------------------
                                 33                 :                :  */
                                 34                 :                : #include "postgres.h"
                                 35                 :                : 
                                 36                 :                : #include "commands/defrem.h"
                                 37                 :                : #include "commands/explain.h"
                                 38                 :                : #include "commands/explain_state.h"
                                 39                 :                : 
                                 40                 :                : /* Hook to perform additional EXPLAIN options validation */
                                 41                 :                : explain_validate_options_hook_type explain_validate_options_hook = NULL;
                                 42                 :                : 
                                 43                 :                : typedef struct
                                 44                 :                : {
                                 45                 :                :     const char *option_name;
                                 46                 :                :     ExplainOptionHandler option_handler;
                                 47                 :                : } ExplainExtensionOption;
                                 48                 :                : 
                                 49                 :                : static const char **ExplainExtensionNameArray = NULL;
                                 50                 :                : static int  ExplainExtensionNamesAssigned = 0;
                                 51                 :                : static int  ExplainExtensionNamesAllocated = 0;
                                 52                 :                : 
                                 53                 :                : static ExplainExtensionOption *ExplainExtensionOptionArray = NULL;
                                 54                 :                : static int  ExplainExtensionOptionsAssigned = 0;
                                 55                 :                : static int  ExplainExtensionOptionsAllocated = 0;
                                 56                 :                : 
                                 57                 :                : /*
                                 58                 :                :  * Create a new ExplainState struct initialized with default options.
                                 59                 :                :  */
                                 60                 :                : ExplainState *
  172 rhaas@postgresql.org       61                 :CBC       12041 : NewExplainState(void)
                                 62                 :                : {
                                 63                 :          12041 :     ExplainState *es = (ExplainState *) palloc0(sizeof(ExplainState));
                                 64                 :                : 
                                 65                 :                :     /* Set default options (most fields can be left as zeroes). */
                                 66                 :          12041 :     es->costs = true;
                                 67                 :                :     /* Prepare output buffer. */
                                 68                 :          12041 :     es->str = makeStringInfo();
                                 69                 :                : 
                                 70                 :          12041 :     return es;
                                 71                 :                : }
                                 72                 :                : 
                                 73                 :                : /*
                                 74                 :                :  * Parse a list of EXPLAIN options and update an ExplainState accordingly.
                                 75                 :                :  */
                                 76                 :                : void
                                 77                 :          12031 : ParseExplainOptionList(ExplainState *es, List *options, ParseState *pstate)
                                 78                 :                : {
                                 79                 :                :     ListCell   *lc;
                                 80                 :          12031 :     bool        timing_set = false;
                                 81                 :          12031 :     bool        buffers_set = false;
                                 82                 :          12031 :     bool        summary_set = false;
                                 83                 :                : 
                                 84                 :                :     /* Parse options list. */
                                 85   [ +  +  +  +  :          23444 :     foreach(lc, options)
                                              +  + ]
                                 86                 :                :     {
                                 87                 :          11417 :         DefElem    *opt = (DefElem *) lfirst(lc);
                                 88                 :                : 
                                 89         [ +  + ]:          11417 :         if (strcmp(opt->defname, "analyze") == 0)
                                 90                 :           1743 :             es->analyze = defGetBoolean(opt);
                                 91         [ +  + ]:           9674 :         else if (strcmp(opt->defname, "verbose") == 0)
                                 92                 :           1253 :             es->verbose = defGetBoolean(opt);
                                 93         [ +  + ]:           8421 :         else if (strcmp(opt->defname, "costs") == 0)
                                 94                 :           6826 :             es->costs = defGetBoolean(opt);
                                 95         [ +  + ]:           1595 :         else if (strcmp(opt->defname, "buffers") == 0)
                                 96                 :                :         {
                                 97                 :            489 :             buffers_set = true;
                                 98                 :            489 :             es->buffers = defGetBoolean(opt);
                                 99                 :                :         }
                                100         [ -  + ]:           1106 :         else if (strcmp(opt->defname, "wal") == 0)
  172 rhaas@postgresql.org      101                 :UBC           0 :             es->wal = defGetBoolean(opt);
  172 rhaas@postgresql.org      102         [ +  + ]:CBC        1106 :         else if (strcmp(opt->defname, "settings") == 0)
                                103                 :              6 :             es->settings = defGetBoolean(opt);
                                104         [ +  + ]:           1100 :         else if (strcmp(opt->defname, "generic_plan") == 0)
                                105                 :              9 :             es->generic = defGetBoolean(opt);
                                106         [ +  + ]:           1091 :         else if (strcmp(opt->defname, "timing") == 0)
                                107                 :                :         {
                                108                 :            451 :             timing_set = true;
                                109                 :            451 :             es->timing = defGetBoolean(opt);
                                110                 :                :         }
                                111         [ +  + ]:            640 :         else if (strcmp(opt->defname, "summary") == 0)
                                112                 :                :         {
                                113                 :            439 :             summary_set = true;
                                114                 :            439 :             es->summary = defGetBoolean(opt);
                                115                 :                :         }
                                116         [ +  + ]:            201 :         else if (strcmp(opt->defname, "memory") == 0)
                                117                 :             15 :             es->memory = defGetBoolean(opt);
                                118         [ +  + ]:            186 :         else if (strcmp(opt->defname, "serialize") == 0)
                                119                 :                :         {
                                120         [ +  + ]:             15 :             if (opt->arg)
                                121                 :                :             {
                                122                 :              6 :                 char       *p = defGetString(opt);
                                123                 :                : 
                                124   [ +  -  -  + ]:              6 :                 if (strcmp(p, "off") == 0 || strcmp(p, "none") == 0)
  172 rhaas@postgresql.org      125                 :UBC           0 :                     es->serialize = EXPLAIN_SERIALIZE_NONE;
  172 rhaas@postgresql.org      126         [ +  + ]:CBC           6 :                 else if (strcmp(p, "text") == 0)
                                127                 :              3 :                     es->serialize = EXPLAIN_SERIALIZE_TEXT;
                                128         [ +  - ]:              3 :                 else if (strcmp(p, "binary") == 0)
                                129                 :              3 :                     es->serialize = EXPLAIN_SERIALIZE_BINARY;
                                130                 :                :                 else
  172 rhaas@postgresql.org      131         [ #  # ]:UBC           0 :                     ereport(ERROR,
                                132                 :                :                             (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                                133                 :                :                              errmsg("unrecognized value for EXPLAIN option \"%s\": \"%s\"",
                                134                 :                :                                     opt->defname, p),
                                135                 :                :                              parser_errposition(pstate, opt->location)));
                                136                 :                :             }
                                137                 :                :             else
                                138                 :                :             {
                                139                 :                :                 /* SERIALIZE without an argument is taken as 'text' */
  172 rhaas@postgresql.org      140                 :CBC           9 :                 es->serialize = EXPLAIN_SERIALIZE_TEXT;
                                141                 :                :             }
                                142                 :                :         }
                                143         [ +  + ]:            171 :         else if (strcmp(opt->defname, "format") == 0)
                                144                 :                :         {
                                145                 :            156 :             char       *p = defGetString(opt);
                                146                 :                : 
                                147         [ +  + ]:            156 :             if (strcmp(p, "text") == 0)
                                148                 :              6 :                 es->format = EXPLAIN_FORMAT_TEXT;
                                149         [ +  + ]:            150 :             else if (strcmp(p, "xml") == 0)
                                150                 :              4 :                 es->format = EXPLAIN_FORMAT_XML;
                                151         [ +  + ]:            146 :             else if (strcmp(p, "json") == 0)
                                152                 :            140 :                 es->format = EXPLAIN_FORMAT_JSON;
                                153         [ +  - ]:              6 :             else if (strcmp(p, "yaml") == 0)
                                154                 :              6 :                 es->format = EXPLAIN_FORMAT_YAML;
                                155                 :                :             else
  172 rhaas@postgresql.org      156         [ #  # ]:UBC           0 :                 ereport(ERROR,
                                157                 :                :                         (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                                158                 :                :                          errmsg("unrecognized value for EXPLAIN option \"%s\": \"%s\"",
                                159                 :                :                                 opt->defname, p),
                                160                 :                :                          parser_errposition(pstate, opt->location)));
                                161                 :                :         }
  172 rhaas@postgresql.org      162         [ +  + ]:CBC          15 :         else if (!ApplyExtensionExplainOption(es, opt, pstate))
                                163         [ +  - ]:              4 :             ereport(ERROR,
                                164                 :                :                     (errcode(ERRCODE_SYNTAX_ERROR),
                                165                 :                :                      errmsg("unrecognized EXPLAIN option \"%s\"",
                                166                 :                :                             opt->defname),
                                167                 :                :                      parser_errposition(pstate, opt->location)));
                                168                 :                :     }
                                169                 :                : 
                                170                 :                :     /* check that WAL is used with EXPLAIN ANALYZE */
                                171   [ -  +  -  - ]:          12027 :     if (es->wal && !es->analyze)
  172 rhaas@postgresql.org      172         [ #  # ]:UBC           0 :         ereport(ERROR,
                                173                 :                :                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                                174                 :                :                  errmsg("EXPLAIN option %s requires ANALYZE", "WAL")));
                                175                 :                : 
                                176                 :                :     /* if the timing was not set explicitly, set default value */
  172 rhaas@postgresql.org      177         [ +  + ]:CBC       12027 :     es->timing = (timing_set) ? es->timing : es->analyze;
                                178                 :                : 
                                179                 :                :     /* if the buffers was not set explicitly, set default value */
                                180         [ +  + ]:          12027 :     es->buffers = (buffers_set) ? es->buffers : es->analyze;
                                181                 :                : 
                                182                 :                :     /* check that timing is used with EXPLAIN ANALYZE */
                                183   [ +  +  -  + ]:          12027 :     if (es->timing && !es->analyze)
  172 rhaas@postgresql.org      184         [ #  # ]:UBC           0 :         ereport(ERROR,
                                185                 :                :                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                                186                 :                :                  errmsg("EXPLAIN option %s requires ANALYZE", "TIMING")));
                                187                 :                : 
                                188                 :                :     /* check that serialize is used with EXPLAIN ANALYZE */
  172 rhaas@postgresql.org      189   [ +  +  -  + ]:CBC       12027 :     if (es->serialize != EXPLAIN_SERIALIZE_NONE && !es->analyze)
  172 rhaas@postgresql.org      190         [ #  # ]:UBC           0 :         ereport(ERROR,
                                191                 :                :                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                                192                 :                :                  errmsg("EXPLAIN option %s requires ANALYZE", "SERIALIZE")));
                                193                 :                : 
                                194                 :                :     /* check that GENERIC_PLAN is not used with EXPLAIN ANALYZE */
  172 rhaas@postgresql.org      195   [ +  +  +  + ]:CBC       12027 :     if (es->generic && es->analyze)
                                196         [ +  - ]:              3 :         ereport(ERROR,
                                197                 :                :                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                                198                 :                :                  errmsg("EXPLAIN options ANALYZE and GENERIC_PLAN cannot be used together")));
                                199                 :                : 
                                200                 :                :     /* if the summary was not set explicitly, set default value */
                                201         [ +  + ]:          12024 :     es->summary = (summary_set) ? es->summary : es->analyze;
                                202                 :                : 
                                203                 :                :     /* plugin specific option validation */
  170                           204         [ -  + ]:          12024 :     if (explain_validate_options_hook)
  170 rhaas@postgresql.org      205                 :UBC           0 :         (*explain_validate_options_hook) (es, options, pstate);
  172 rhaas@postgresql.org      206                 :CBC       12024 : }
                                207                 :                : 
                                208                 :                : /*
                                209                 :                :  * Map the name of an EXPLAIN extension to an integer ID.
                                210                 :                :  *
                                211                 :                :  * Within the lifetime of a particular backend, the same name will be mapped
                                212                 :                :  * to the same ID every time. IDs are not stable across backends. Use the ID
                                213                 :                :  * that you get from this function to call GetExplainExtensionState and
                                214                 :                :  * SetExplainExtensionState.
                                215                 :                :  *
                                216                 :                :  * extension_name is assumed to be a constant string or allocated in storage
                                217                 :                :  * that will never be freed.
                                218                 :                :  */
                                219                 :                : int
                                220                 :              1 : GetExplainExtensionId(const char *extension_name)
                                221                 :                : {
                                222                 :                :     /* Search for an existing extension by this name; if found, return ID. */
                                223         [ -  + ]:              1 :     for (int i = 0; i < ExplainExtensionNamesAssigned; ++i)
  172 rhaas@postgresql.org      224         [ #  # ]:UBC           0 :         if (strcmp(ExplainExtensionNameArray[i], extension_name) == 0)
                                225                 :              0 :             return i;
                                226                 :                : 
                                227                 :                :     /* If there is no array yet, create one. */
  172 rhaas@postgresql.org      228         [ +  - ]:CBC           1 :     if (ExplainExtensionNameArray == NULL)
                                229                 :                :     {
                                230                 :              1 :         ExplainExtensionNamesAllocated = 16;
                                231                 :              1 :         ExplainExtensionNameArray = (const char **)
                                232                 :              1 :             MemoryContextAlloc(TopMemoryContext,
                                233                 :                :                                ExplainExtensionNamesAllocated
                                234                 :                :                                * sizeof(char *));
                                235                 :                :     }
                                236                 :                : 
                                237                 :                :     /* If there's an array but it's currently full, expand it. */
                                238         [ -  + ]:              1 :     if (ExplainExtensionNamesAssigned >= ExplainExtensionNamesAllocated)
                                239                 :                :     {
  172 rhaas@postgresql.org      240                 :UBC           0 :         int         i = pg_nextpower2_32(ExplainExtensionNamesAssigned + 1);
                                241                 :                : 
                                242                 :              0 :         ExplainExtensionNameArray = (const char **)
                                243                 :              0 :             repalloc(ExplainExtensionNameArray, i * sizeof(char *));
                                244                 :              0 :         ExplainExtensionNamesAllocated = i;
                                245                 :                :     }
                                246                 :                : 
                                247                 :                :     /* Assign and return new ID. */
  172 rhaas@postgresql.org      248                 :CBC           1 :     ExplainExtensionNameArray[ExplainExtensionNamesAssigned] = extension_name;
                                249                 :              1 :     return ExplainExtensionNamesAssigned++;
                                250                 :                : }
                                251                 :                : 
                                252                 :                : /*
                                253                 :                :  * Get extension-specific state from an ExplainState.
                                254                 :                :  *
                                255                 :                :  * See comments for SetExplainExtensionState, below.
                                256                 :                :  */
                                257                 :                : void *
                                258                 :             50 : GetExplainExtensionState(ExplainState *es, int extension_id)
                                259                 :                : {
                                260         [ -  + ]:             50 :     Assert(extension_id >= 0);
                                261                 :                : 
                                262         [ +  + ]:             50 :     if (extension_id >= es->extension_state_allocated)
                                263                 :              9 :         return NULL;
                                264                 :                : 
                                265                 :             41 :     return es->extension_state[extension_id];
                                266                 :                : }
                                267                 :                : 
                                268                 :                : /*
                                269                 :                :  * Store extension-specific state into an ExplainState.
                                270                 :                :  *
                                271                 :                :  * To use this function, first obtain an integer extension_id using
                                272                 :                :  * GetExplainExtensionId. Then use this function to store an opaque pointer
                                273                 :                :  * in the ExplainState. Later, you can retrieve the opaque pointer using
                                274                 :                :  * GetExplainExtensionState.
                                275                 :                :  */
                                276                 :                : void
                                277                 :              9 : SetExplainExtensionState(ExplainState *es, int extension_id, void *opaque)
                                278                 :                : {
                                279         [ -  + ]:              9 :     Assert(extension_id >= 0);
                                280                 :                : 
                                281                 :                :     /* If there is no array yet, create one. */
                                282         [ +  - ]:              9 :     if (es->extension_state == NULL)
                                283                 :                :     {
                                284                 :              9 :         es->extension_state_allocated = 16;
                                285                 :              9 :         es->extension_state =
                                286                 :              9 :             palloc0(es->extension_state_allocated * sizeof(void *));
                                287                 :                :     }
                                288                 :                : 
                                289                 :                :     /* If there's an array but it's currently full, expand it. */
                                290         [ -  + ]:              9 :     if (extension_id >= es->extension_state_allocated)
                                291                 :                :     {
                                292                 :                :         int         i;
                                293                 :                : 
  172 rhaas@postgresql.org      294                 :UBC           0 :         i = pg_nextpower2_32(es->extension_state_allocated + 1);
                                295                 :              0 :         es->extension_state = (void **)
                                296                 :              0 :             repalloc0(es->extension_state,
                                297                 :              0 :                       es->extension_state_allocated * sizeof(void *),
                                298                 :                :                       i * sizeof(void *));
                                299                 :              0 :         es->extension_state_allocated = i;
                                300                 :                :     }
                                301                 :                : 
  172 rhaas@postgresql.org      302                 :CBC           9 :     es->extension_state[extension_id] = opaque;
                                303                 :              9 : }
                                304                 :                : 
                                305                 :                : /*
                                306                 :                :  * Register a new EXPLAIN option.
                                307                 :                :  *
                                308                 :                :  * When option_name is used as an EXPLAIN option, handler will be called and
                                309                 :                :  * should update the ExplainState passed to it. See comments at top of file
                                310                 :                :  * for a more detailed explanation.
                                311                 :                :  *
                                312                 :                :  * option_name is assumed to be a constant string or allocated in storage
                                313                 :                :  * that will never be freed.
                                314                 :                :  */
                                315                 :                : void
                                316                 :              2 : RegisterExtensionExplainOption(const char *option_name,
                                317                 :                :                                ExplainOptionHandler handler)
                                318                 :                : {
                                319                 :                :     ExplainExtensionOption *exopt;
                                320                 :                : 
                                321                 :                :     /* Search for an existing option by this name; if found, update handler. */
                                322         [ +  + ]:              3 :     for (int i = 0; i < ExplainExtensionOptionsAssigned; ++i)
                                323                 :                :     {
                                324         [ -  + ]:              1 :         if (strcmp(ExplainExtensionOptionArray[i].option_name,
                                325                 :                :                    option_name) == 0)
                                326                 :                :         {
  172 rhaas@postgresql.org      327                 :UBC           0 :             ExplainExtensionOptionArray[i].option_handler = handler;
                                328                 :              0 :             return;
                                329                 :                :         }
                                330                 :                :     }
                                331                 :                : 
                                332                 :                :     /* If there is no array yet, create one. */
  172 rhaas@postgresql.org      333         [ +  + ]:CBC           2 :     if (ExplainExtensionOptionArray == NULL)
                                334                 :                :     {
                                335                 :              1 :         ExplainExtensionOptionsAllocated = 16;
                                336                 :              1 :         ExplainExtensionOptionArray = (ExplainExtensionOption *)
                                337                 :              1 :             MemoryContextAlloc(TopMemoryContext,
                                338                 :                :                                ExplainExtensionOptionsAllocated
                                339                 :                :                                * sizeof(char *));
                                340                 :                :     }
                                341                 :                : 
                                342                 :                :     /* If there's an array but it's currently full, expand it. */
                                343         [ -  + ]:              2 :     if (ExplainExtensionOptionsAssigned >= ExplainExtensionOptionsAllocated)
                                344                 :                :     {
  172 rhaas@postgresql.org      345                 :UBC           0 :         int         i = pg_nextpower2_32(ExplainExtensionOptionsAssigned + 1);
                                346                 :                : 
                                347                 :              0 :         ExplainExtensionOptionArray = (ExplainExtensionOption *)
                                348                 :              0 :             repalloc(ExplainExtensionOptionArray, i * sizeof(char *));
                                349                 :              0 :         ExplainExtensionOptionsAllocated = i;
                                350                 :                :     }
                                351                 :                : 
                                352                 :                :     /* Assign and return new ID. */
  172 rhaas@postgresql.org      353                 :CBC           2 :     exopt = &ExplainExtensionOptionArray[ExplainExtensionOptionsAssigned++];
                                354                 :              2 :     exopt->option_name = option_name;
                                355                 :              2 :     exopt->option_handler = handler;
                                356                 :                : }
                                357                 :                : 
                                358                 :                : /*
                                359                 :                :  * Apply an EXPLAIN option registered by an extension.
                                360                 :                :  *
                                361                 :                :  * If no extension has registered the named option, returns false. Otherwise,
                                362                 :                :  * calls the appropriate handler function and then returns true.
                                363                 :                :  */
                                364                 :                : bool
                                365                 :             15 : ApplyExtensionExplainOption(ExplainState *es, DefElem *opt, ParseState *pstate)
                                366                 :                : {
                                367         [ +  + ]:             22 :     for (int i = 0; i < ExplainExtensionOptionsAssigned; ++i)
                                368                 :                :     {
                                369                 :             18 :         if (strcmp(ExplainExtensionOptionArray[i].option_name,
                                370         [ +  + ]:             18 :                    opt->defname) == 0)
                                371                 :                :         {
                                372                 :             11 :             ExplainExtensionOptionArray[i].option_handler(es, opt, pstate);
                                373                 :             11 :             return true;
                                374                 :                :         }
                                375                 :                :     }
                                376                 :                : 
                                377                 :              4 :     return false;
                                378                 :                : }
        

Generated by: LCOV version 2.4-beta