LCOV - code coverage report
Current view: top level - contrib/pg_overexplain - pg_overexplain.c (source / functions) Coverage Total Hit
Test: PostgreSQL 20devel Lines: 66.3 % 413 274
Test Date: 2026-10-16 23:15:56 Functions: 100.0 % 13 13
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 66.1 % 242 160

             Branch data     Line data    Source code
       1                 :             : /*-------------------------------------------------------------------------
       2                 :             :  *
       3                 :             :  * pg_overexplain.c
       4                 :             :  *    allow EXPLAIN to dump even more details
       5                 :             :  *
       6                 :             :  * Copyright (c) 2016-2026, PostgreSQL Global Development Group
       7                 :             :  *
       8                 :             :  *    contrib/pg_overexplain/pg_overexplain.c
       9                 :             :  *-------------------------------------------------------------------------
      10                 :             :  */
      11                 :             : #include "postgres.h"
      12                 :             : 
      13                 :             : #include "catalog/pg_class.h"
      14                 :             : #include "commands/defrem.h"
      15                 :             : #include "commands/explain.h"
      16                 :             : #include "commands/explain_format.h"
      17                 :             : #include "commands/explain_state.h"
      18                 :             : #include "fmgr.h"
      19                 :             : #include "parser/parsetree.h"
      20                 :             : #include "storage/lock.h"
      21                 :             : #include "utils/builtins.h"
      22                 :             : #include "utils/lsyscache.h"
      23                 :             : 
      24                 :          16 : PG_MODULE_MAGIC_EXT(
      25                 :             :                     .name = "pg_overexplain",
      26                 :             :                     .version = PG_VERSION
      27                 :             : );
      28                 :             : 
      29                 :             : typedef struct
      30                 :             : {
      31                 :             :     bool        debug;
      32                 :             :     bool        range_table;
      33                 :             : } overexplain_options;
      34                 :             : 
      35                 :             : static overexplain_options *overexplain_ensure_options(ExplainState *es);
      36                 :             : static void overexplain_debug_handler(ExplainState *es, DefElem *opt,
      37                 :             :                                       ParseState *pstate);
      38                 :             : static void overexplain_range_table_handler(ExplainState *es, DefElem *opt,
      39                 :             :                                             ParseState *pstate);
      40                 :             : static void overexplain_per_node_hook(PlanState *planstate, List *ancestors,
      41                 :             :                                       const char *relationship,
      42                 :             :                                       const char *plan_name,
      43                 :             :                                       ExplainState *es);
      44                 :             : static void overexplain_per_plan_hook(PlannedStmt *plannedstmt,
      45                 :             :                                       IntoClause *into,
      46                 :             :                                       ExplainState *es,
      47                 :             :                                       const char *queryString,
      48                 :             :                                       ParamListInfo params,
      49                 :             :                                       QueryEnvironment *queryEnv);
      50                 :             : static void overexplain_debug(PlannedStmt *plannedstmt, ExplainState *es);
      51                 :             : static void overexplain_range_table(PlannedStmt *plannedstmt,
      52                 :             :                                     ExplainState *es);
      53                 :             : static void overexplain_alias(const char *qlabel, Alias *alias,
      54                 :             :                               ExplainState *es);
      55                 :             : static void overexplain_bitmapset(const char *qlabel, Bitmapset *bms,
      56                 :             :                                   ExplainState *es);
      57                 :             : static void overexplain_bitmapset_list(const char *qlabel, List *bms_list,
      58                 :             :                                        ExplainState *es);
      59                 :             : static void overexplain_intlist(const char *qlabel, List *list,
      60                 :             :                                 ExplainState *es);
      61                 :             : 
      62                 :             : static int  es_extension_id;
      63                 :             : static explain_per_node_hook_type prev_explain_per_node_hook;
      64                 :             : static explain_per_plan_hook_type prev_explain_per_plan_hook;
      65                 :             : 
      66                 :             : /*
      67                 :             :  * Initialization we do when this module is loaded.
      68                 :             :  */
      69                 :             : void
      70                 :          16 : _PG_init(void)
      71                 :             : {
      72                 :             :     /* Get an ID that we can use to cache data in an ExplainState. */
      73                 :          16 :     es_extension_id = GetExplainExtensionId("pg_overexplain");
      74                 :             : 
      75                 :             :     /* Register the new EXPLAIN options implemented by this module. */
      76                 :          16 :     RegisterExtensionExplainOption("debug", overexplain_debug_handler,
      77                 :             :                                    GUCCheckBooleanExplainOption);
      78                 :          16 :     RegisterExtensionExplainOption("range_table",
      79                 :             :                                    overexplain_range_table_handler,
      80                 :             :                                    GUCCheckBooleanExplainOption);
      81                 :             : 
      82                 :             :     /* Use the per-node and per-plan hooks to make our options do something. */
      83                 :          16 :     prev_explain_per_node_hook = explain_per_node_hook;
      84                 :          16 :     explain_per_node_hook = overexplain_per_node_hook;
      85                 :          16 :     prev_explain_per_plan_hook = explain_per_plan_hook;
      86                 :          16 :     explain_per_plan_hook = overexplain_per_plan_hook;
      87                 :          16 : }
      88                 :             : 
      89                 :             : /*
      90                 :             :  * Get the overexplain_options structure from an ExplainState; if there is
      91                 :             :  * none, create one, attach it to the ExplainState, and return it.
      92                 :             :  */
      93                 :             : static overexplain_options *
      94                 :          17 : overexplain_ensure_options(ExplainState *es)
      95                 :             : {
      96                 :             :     overexplain_options *options;
      97                 :             : 
      98                 :          17 :     options = GetExplainExtensionState(es, es_extension_id);
      99                 :             : 
     100         [ +  + ]:          17 :     if (options == NULL)
     101                 :             :     {
     102                 :          15 :         options = palloc0_object(overexplain_options);
     103                 :          15 :         SetExplainExtensionState(es, es_extension_id, options);
     104                 :             :     }
     105                 :             : 
     106                 :          17 :     return options;
     107                 :             : }
     108                 :             : 
     109                 :             : /*
     110                 :             :  * Parse handler for EXPLAIN (DEBUG).
     111                 :             :  */
     112                 :             : static void
     113                 :           7 : overexplain_debug_handler(ExplainState *es, DefElem *opt, ParseState *pstate)
     114                 :             : {
     115                 :           7 :     overexplain_options *options = overexplain_ensure_options(es);
     116                 :             : 
     117                 :           7 :     options->debug = defGetBoolean(opt);
     118                 :           7 : }
     119                 :             : 
     120                 :             : /*
     121                 :             :  * Parse handler for EXPLAIN (RANGE_TABLE).
     122                 :             :  */
     123                 :             : static void
     124                 :          10 : overexplain_range_table_handler(ExplainState *es, DefElem *opt,
     125                 :             :                                 ParseState *pstate)
     126                 :             : {
     127                 :          10 :     overexplain_options *options = overexplain_ensure_options(es);
     128                 :             : 
     129                 :          10 :     options->range_table = defGetBoolean(opt);
     130                 :          10 : }
     131                 :             : 
     132                 :             : /*
     133                 :             :  * Print out additional per-node information as appropriate. If the user didn't
     134                 :             :  * specify any of the options we support, do nothing; else, print whatever is
     135                 :             :  * relevant to the specified options.
     136                 :             :  */
     137                 :             : static void
     138                 :          64 : overexplain_per_node_hook(PlanState *planstate, List *ancestors,
     139                 :             :                           const char *relationship, const char *plan_name,
     140                 :             :                           ExplainState *es)
     141                 :             : {
     142                 :             :     overexplain_options *options;
     143                 :          64 :     Plan       *plan = planstate->plan;
     144                 :             : 
     145         [ -  + ]:          64 :     if (prev_explain_per_node_hook)
     146                 :           0 :         (*prev_explain_per_node_hook) (planstate, ancestors, relationship,
     147                 :             :                                        plan_name, es);
     148                 :             : 
     149                 :          64 :     options = GetExplainExtensionState(es, es_extension_id);
     150         [ +  + ]:          64 :     if (options == NULL)
     151                 :          11 :         return;
     152                 :             : 
     153                 :             :     /*
     154                 :             :      * If the "debug" option was given, display miscellaneous fields from the
     155                 :             :      * "Plan" node that would not otherwise be displayed.
     156                 :             :      */
     157         [ +  + ]:          53 :     if (options->debug)
     158                 :             :     {
     159                 :             :         /*
     160                 :             :          * Normal EXPLAIN will display "Disabled: true" if the node is
     161                 :             :          * disabled; but that is based on noticing that plan->disabled_nodes
     162                 :             :          * is higher than the sum of its children; here, we display the raw
     163                 :             :          * value, for debugging purposes.
     164                 :             :          */
     165                 :          27 :         ExplainPropertyInteger("Disabled Nodes", NULL, plan->disabled_nodes,
     166                 :             :                                es);
     167                 :             : 
     168                 :             :         /*
     169                 :             :          * Normal EXPLAIN will display the parallel_aware flag; here, we show
     170                 :             :          * the parallel_safe flag as well.
     171                 :             :          */
     172                 :          27 :         ExplainPropertyBool("Parallel Safe", plan->parallel_safe, es);
     173                 :             : 
     174                 :             :         /*
     175                 :             :          * The plan node ID isn't normally displayed, since it is only useful
     176                 :             :          * for debugging.
     177                 :             :          */
     178                 :          27 :         ExplainPropertyInteger("Plan Node ID", NULL, plan->plan_node_id, es);
     179                 :             : 
     180                 :             :         /*
     181                 :             :          * It is difficult to explain what extParam and allParam mean in plain
     182                 :             :          * language, so we simply display these fields labelled with the
     183                 :             :          * structure member name. For compactness, the text format omits the
     184                 :             :          * display of this information when the bitmapset is empty.
     185                 :             :          */
     186   [ +  +  +  + ]:          27 :         if (es->format != EXPLAIN_FORMAT_TEXT || !bms_is_empty(plan->extParam))
     187                 :           8 :             overexplain_bitmapset("extParam", plan->extParam, es);
     188   [ +  +  +  + ]:          27 :         if (es->format != EXPLAIN_FORMAT_TEXT || !bms_is_empty(plan->allParam))
     189                 :           8 :             overexplain_bitmapset("allParam", plan->allParam, es);
     190                 :             :     }
     191                 :             : 
     192                 :             :     /*
     193                 :             :      * If the "range_table" option was specified, display information about
     194                 :             :      * the range table indexes for this node.
     195                 :             :      */
     196         [ +  + ]:          53 :     if (options->range_table)
     197                 :             :     {
     198                 :          36 :         bool        opened_elided_nodes = false;
     199                 :             : 
     200   [ +  -  -  +  :          36 :         switch (nodeTag(plan))
             +  -  +  +  
                      + ]
     201                 :             :         {
     202                 :          18 :             case T_SeqScan:
     203                 :             :             case T_SampleScan:
     204                 :             :             case T_IndexScan:
     205                 :             :             case T_IndexOnlyScan:
     206                 :             :             case T_BitmapHeapScan:
     207                 :             :             case T_TidScan:
     208                 :             :             case T_TidRangeScan:
     209                 :             :             case T_SubqueryScan:
     210                 :             :             case T_FunctionScan:
     211                 :             :             case T_TableFuncScan:
     212                 :             :             case T_ValuesScan:
     213                 :             :             case T_CteScan:
     214                 :             :             case T_NamedTuplestoreScan:
     215                 :             :             case T_WorkTableScan:
     216                 :          18 :                 ExplainPropertyInteger("Scan RTI", NULL,
     217                 :          18 :                                        ((Scan *) plan)->scanrelid, es);
     218                 :          18 :                 break;
     219                 :           0 :             case T_ForeignScan:
     220                 :           0 :                 overexplain_bitmapset("Scan RTIs",
     221                 :             :                                       ((ForeignScan *) plan)->fs_base_relids,
     222                 :             :                                       es);
     223                 :           0 :                 break;
     224                 :           0 :             case T_CustomScan:
     225                 :           0 :                 overexplain_bitmapset("Scan RTIs",
     226                 :             :                                       ((CustomScan *) plan)->custom_relids,
     227                 :             :                                       es);
     228                 :           0 :                 break;
     229                 :           1 :             case T_ModifyTable:
     230                 :           1 :                 ExplainPropertyInteger("Nominal RTI", NULL,
     231                 :           1 :                                        ((ModifyTable *) plan)->nominalRelation, es);
     232                 :           1 :                 ExplainPropertyInteger("Exclude Relation RTI", NULL,
     233                 :           1 :                                        ((ModifyTable *) plan)->exclRelRTI, es);
     234                 :           1 :                 break;
     235                 :           6 :             case T_Append:
     236                 :           6 :                 overexplain_bitmapset("Append RTIs",
     237                 :             :                                       ((Append *) plan)->apprelids,
     238                 :             :                                       es);
     239                 :           6 :                 overexplain_bitmapset_list("Child Append RTIs",
     240                 :             :                                            ((Append *) plan)->child_append_relid_sets,
     241                 :             :                                            es);
     242                 :           6 :                 break;
     243                 :           0 :             case T_MergeAppend:
     244                 :           0 :                 overexplain_bitmapset("Append RTIs",
     245                 :             :                                       ((MergeAppend *) plan)->apprelids,
     246                 :             :                                       es);
     247                 :           0 :                 overexplain_bitmapset_list("Child Append RTIs",
     248                 :             :                                            ((MergeAppend *) plan)->child_append_relid_sets,
     249                 :             :                                            es);
     250                 :           0 :                 break;
     251                 :           2 :             case T_Result:
     252                 :             : 
     253                 :             :                 /*
     254                 :             :                  * 'relids' is only meaningful when plan->lefttree is NULL,
     255                 :             :                  * but if somehow it ends up set when plan->lefttree is not
     256                 :             :                  * NULL, print it anyway.
     257                 :             :                  */
     258         [ -  + ]:           2 :                 if (plan->lefttree == NULL ||
     259         [ #  # ]:           0 :                     ((Result *) plan)->relids != NULL)
     260                 :           2 :                     overexplain_bitmapset("RTIs",
     261                 :             :                                           ((Result *) plan)->relids,
     262                 :             :                                           es);
     263                 :           2 :                 break;
     264                 :           3 :             case T_MergeJoin:
     265                 :             :             case T_NestLoop:
     266                 :             :             case T_HashJoin:
     267                 :             : 
     268                 :             :                 /*
     269                 :             :                  * 'ojrelids' is only meaningful for non-inner joins, but if
     270                 :             :                  * it somehow ends up set for an inner join, print it anyway.
     271                 :             :                  */
     272         [ +  + ]:           3 :                 if (((Join *) plan)->jointype != JOIN_INNER ||
     273         [ -  + ]:           2 :                     ((Join *) plan)->ojrelids != NULL)
     274                 :           1 :                     overexplain_bitmapset("Outer Join RTIs",
     275                 :             :                                           ((Join *) plan)->ojrelids,
     276                 :             :                                           es);
     277                 :           3 :                 break;
     278                 :           6 :             default:
     279                 :           6 :                 break;
     280                 :             :         }
     281                 :             : 
     282   [ +  +  +  +  :          97 :         foreach_node(ElidedNode, n, es->pstmt->elidedNodes)
                   +  + ]
     283                 :             :         {
     284                 :             :             char       *elidednodetag;
     285                 :             : 
     286         [ +  + ]:          25 :             if (n->plan_node_id != plan->plan_node_id)
     287                 :          19 :                 continue;
     288                 :             : 
     289         [ +  + ]:           6 :             if (!opened_elided_nodes)
     290                 :             :             {
     291                 :           4 :                 ExplainOpenGroup("Elided Nodes", "Elided Nodes", false, es);
     292                 :           4 :                 opened_elided_nodes = true;
     293                 :             :             }
     294                 :             : 
     295   [ +  -  +  - ]:           6 :             switch (n->elided_type)
     296                 :             :             {
     297                 :           3 :                 case T_Append:
     298                 :           3 :                     elidednodetag = "Append";
     299                 :           3 :                     break;
     300                 :           0 :                 case T_MergeAppend:
     301                 :           0 :                     elidednodetag = "MergeAppend";
     302                 :           0 :                     break;
     303                 :           3 :                 case T_SubqueryScan:
     304                 :           3 :                     elidednodetag = "SubqueryScan";
     305                 :           3 :                     break;
     306                 :           0 :                 default:
     307                 :           0 :                     elidednodetag = psprintf("%d", n->elided_type);
     308                 :           0 :                     break;
     309                 :             :             }
     310                 :             : 
     311                 :           6 :             ExplainOpenGroup("Elided Node", NULL, true, es);
     312                 :           6 :             ExplainPropertyText("Elided Node Type", elidednodetag, es);
     313                 :           6 :             overexplain_bitmapset("Elided Node RTIs", n->relids, es);
     314   [ +  +  -  + ]:           6 :             if (n->elided_type == T_Append || n->elided_type == T_MergeAppend)
     315                 :           3 :                 overexplain_bitmapset_list("Elided Node Child Append RTIs",
     316                 :             :                                            n->child_append_relid_sets, es);
     317                 :           6 :             ExplainCloseGroup("Elided Node", NULL, true, es);
     318                 :             :         }
     319         [ +  + ]:          36 :         if (opened_elided_nodes)
     320                 :           4 :             ExplainCloseGroup("Elided Nodes", "Elided Nodes", false, es);
     321                 :             :     }
     322                 :             : }
     323                 :             : 
     324                 :             : /*
     325                 :             :  * Print out additional per-query information as appropriate. Here again, if
     326                 :             :  * the user didn't specify any of the options implemented by this module, do
     327                 :             :  * nothing; otherwise, call the appropriate function for each specified
     328                 :             :  * option.
     329                 :             :  */
     330                 :             : static void
     331                 :          26 : overexplain_per_plan_hook(PlannedStmt *plannedstmt,
     332                 :             :                           IntoClause *into,
     333                 :             :                           ExplainState *es,
     334                 :             :                           const char *queryString,
     335                 :             :                           ParamListInfo params,
     336                 :             :                           QueryEnvironment *queryEnv)
     337                 :             : {
     338                 :             :     overexplain_options *options;
     339                 :             : 
     340         [ -  + ]:          26 :     if (prev_explain_per_plan_hook)
     341                 :           0 :         (*prev_explain_per_plan_hook) (plannedstmt, into, es, queryString,
     342                 :             :                                        params, queryEnv);
     343                 :             : 
     344                 :          26 :     options = GetExplainExtensionState(es, es_extension_id);
     345         [ +  + ]:          26 :     if (options == NULL)
     346                 :          11 :         return;
     347                 :             : 
     348         [ +  + ]:          15 :     if (options->debug)
     349                 :           7 :         overexplain_debug(plannedstmt, es);
     350                 :             : 
     351         [ +  + ]:          15 :     if (options->range_table)
     352                 :          10 :         overexplain_range_table(plannedstmt, es);
     353                 :             : }
     354                 :             : 
     355                 :             : /*
     356                 :             :  * Print out various details from the PlannedStmt that wouldn't otherwise
     357                 :             :  * be displayed.
     358                 :             :  *
     359                 :             :  * We don't try to print everything here. Information that would be displayed
     360                 :             :  * anyway doesn't need to be printed again here, and things with lots of
     361                 :             :  * substructure probably should be printed via separate options, or not at all.
     362                 :             :  */
     363                 :             : static void
     364                 :           7 : overexplain_debug(PlannedStmt *plannedstmt, ExplainState *es)
     365                 :             : {
     366                 :           7 :     char       *commandType = NULL;
     367                 :             :     StringInfoData flags;
     368                 :             : 
     369                 :             :     /* Even in text mode, we want to set this output apart as its own group. */
     370                 :           7 :     ExplainOpenGroup("PlannedStmt", "PlannedStmt", true, es);
     371         [ +  + ]:           7 :     if (es->format == EXPLAIN_FORMAT_TEXT)
     372                 :             :     {
     373                 :           6 :         ExplainIndentText(es);
     374                 :           6 :         appendStringInfoString(es->str, "PlannedStmt:\n");
     375                 :           6 :         es->indent++;
     376                 :             :     }
     377                 :             : 
     378                 :             :     /* Print the command type. */
     379   [ -  +  -  +  :           7 :     switch (plannedstmt->commandType)
             -  -  -  -  
                      - ]
     380                 :             :     {
     381                 :           0 :         case CMD_UNKNOWN:
     382                 :           0 :             commandType = "unknown";
     383                 :           0 :             break;
     384                 :           6 :         case CMD_SELECT:
     385                 :           6 :             commandType = "select";
     386                 :           6 :             break;
     387                 :           0 :         case CMD_UPDATE:
     388                 :           0 :             commandType = "update";
     389                 :           0 :             break;
     390                 :           1 :         case CMD_INSERT:
     391                 :           1 :             commandType = "insert";
     392                 :           1 :             break;
     393                 :           0 :         case CMD_DELETE:
     394                 :           0 :             commandType = "delete";
     395                 :           0 :             break;
     396                 :           0 :         case CMD_MERGE:
     397                 :           0 :             commandType = "merge";
     398                 :           0 :             break;
     399                 :           0 :         case CMD_UTILITY:
     400                 :           0 :             commandType = "utility";
     401                 :           0 :             break;
     402                 :           0 :         case CMD_NOTHING:
     403                 :           0 :             commandType = "nothing";
     404                 :           0 :             break;
     405                 :             :     }
     406                 :           7 :     ExplainPropertyText("Command Type", commandType, es);
     407                 :             : 
     408                 :             :     /* Print various properties as a comma-separated list of flags. */
     409                 :           7 :     initStringInfo(&flags);
     410         [ +  + ]:           7 :     if (plannedstmt->hasReturning)
     411                 :           1 :         appendStringInfoString(&flags, ", hasReturning");
     412         [ -  + ]:           7 :     if (plannedstmt->hasModifyingCTE)
     413                 :           0 :         appendStringInfoString(&flags, ", hasModifyingCTE");
     414         [ +  - ]:           7 :     if (plannedstmt->canSetTag)
     415                 :           7 :         appendStringInfoString(&flags, ", canSetTag");
     416         [ -  + ]:           7 :     if (plannedstmt->transientPlan)
     417                 :           0 :         appendStringInfoString(&flags, ", transientPlan");
     418         [ -  + ]:           7 :     if (plannedstmt->dependsOnRole)
     419                 :           0 :         appendStringInfoString(&flags, ", dependsOnRole");
     420         [ +  + ]:           7 :     if (plannedstmt->parallelModeNeeded)
     421                 :           1 :         appendStringInfoString(&flags, ", parallelModeNeeded");
     422         [ -  + ]:           7 :     if (flags.len == 0)
     423                 :           0 :         appendStringInfoString(&flags, ", none");
     424                 :           7 :     ExplainPropertyText("Flags", flags.data + 2, es);
     425                 :             : 
     426                 :             :     /* Various lists of integers. */
     427                 :           7 :     overexplain_bitmapset("Subplans Needing Rewind",
     428                 :             :                           plannedstmt->rewindPlanIDs, es);
     429                 :           7 :     overexplain_intlist("Relation OIDs",
     430                 :             :                         plannedstmt->relationOids, es);
     431                 :           7 :     overexplain_intlist("Executor Parameter Types",
     432                 :             :                         plannedstmt->paramExecTypes, es);
     433                 :             : 
     434                 :             :     /*
     435                 :             :      * Print the statement location. (If desired, we could alternatively print
     436                 :             :      * stmt_location and stmt_len as two separate fields.)
     437                 :             :      */
     438         [ -  + ]:           7 :     if (plannedstmt->stmt_location == -1)
     439                 :           0 :         ExplainPropertyText("Parse Location", "Unknown", es);
     440         [ +  + ]:           7 :     else if (plannedstmt->stmt_len == 0)
     441                 :           6 :         ExplainPropertyText("Parse Location",
     442                 :           6 :                             psprintf("%d to end", plannedstmt->stmt_location),
     443                 :             :                             es);
     444                 :             :     else
     445                 :           1 :         ExplainPropertyText("Parse Location",
     446                 :           1 :                             psprintf("%d for %d bytes",
     447                 :             :                                      plannedstmt->stmt_location,
     448                 :             :                                      plannedstmt->stmt_len),
     449                 :             :                             es);
     450                 :             : 
     451                 :             :     /* Done with this group. */
     452         [ +  + ]:           7 :     if (es->format == EXPLAIN_FORMAT_TEXT)
     453                 :           6 :         es->indent--;
     454                 :           7 :     ExplainCloseGroup("PlannedStmt", "PlannedStmt", true, es);
     455                 :           7 : }
     456                 :             : 
     457                 :             : /*
     458                 :             :  * Provide detailed information about the contents of the PlannedStmt's
     459                 :             :  * range table.
     460                 :             :  */
     461                 :             : static void
     462                 :          10 : overexplain_range_table(PlannedStmt *plannedstmt, ExplainState *es)
     463                 :             : {
     464                 :             :     Index       rti;
     465                 :          10 :     ListCell   *lc_subrtinfo = list_head(plannedstmt->subrtinfos);
     466                 :          10 :     SubPlanRTInfo *rtinfo = NULL;
     467                 :             : 
     468                 :             :     /* Open group, one entry per RangeTblEntry */
     469                 :          10 :     ExplainOpenGroup("Range Table", "Range Table", false, es);
     470                 :             : 
     471                 :             :     /* Iterate over the range table */
     472         [ +  + ]:          48 :     for (rti = 1; rti <= list_length(plannedstmt->rtable); ++rti)
     473                 :             :     {
     474                 :          38 :         RangeTblEntry *rte = rt_fetch(rti, plannedstmt->rtable);
     475                 :          38 :         char       *kind = NULL;
     476                 :             :         char       *relkind;
     477                 :             :         SubPlanRTInfo *next_rtinfo;
     478                 :             : 
     479                 :             :         /* Advance to next SubPlanRTInfo, if it's time. */
     480         [ +  + ]:          38 :         if (lc_subrtinfo != NULL)
     481                 :             :         {
     482                 :          12 :             next_rtinfo = lfirst(lc_subrtinfo);
     483         [ +  + ]:          12 :             if (rti > next_rtinfo->rtoffset)
     484                 :             :             {
     485                 :           3 :                 rtinfo = next_rtinfo;
     486                 :           3 :                 lc_subrtinfo = lnext(plannedstmt->subrtinfos, lc_subrtinfo);
     487                 :             :             }
     488                 :             :         }
     489                 :             : 
     490                 :             :         /* NULL entries are possible; skip them */
     491         [ -  + ]:          38 :         if (rte == NULL)
     492                 :           0 :             continue;
     493                 :             : 
     494                 :             :         /* Translate rtekind to a string */
     495   [ +  +  +  -  :          38 :         switch (rte->rtekind)
          -  -  -  -  +  
                   +  - ]
     496                 :             :         {
     497                 :          29 :             case RTE_RELATION:
     498                 :          29 :                 kind = "relation";
     499                 :          29 :                 break;
     500                 :           3 :             case RTE_SUBQUERY:
     501                 :           3 :                 kind = "subquery";
     502                 :           3 :                 break;
     503                 :           1 :             case RTE_JOIN:
     504                 :           1 :                 kind = "join";
     505                 :           1 :                 break;
     506                 :           0 :             case RTE_FUNCTION:
     507                 :           0 :                 kind = "function";
     508                 :           0 :                 break;
     509                 :           0 :             case RTE_TABLEFUNC:
     510                 :           0 :                 kind = "tablefunc";
     511                 :           0 :                 break;
     512                 :           0 :             case RTE_VALUES:
     513                 :           0 :                 kind = "values";
     514                 :           0 :                 break;
     515                 :           0 :             case RTE_CTE:
     516                 :           0 :                 kind = "cte";
     517                 :           0 :                 break;
     518                 :           0 :             case RTE_NAMEDTUPLESTORE:
     519                 :           0 :                 kind = "namedtuplestore";
     520                 :           0 :                 break;
     521                 :           2 :             case RTE_RESULT:
     522                 :           2 :                 kind = "result";
     523                 :           2 :                 break;
     524                 :           3 :             case RTE_GROUP:
     525                 :           3 :                 kind = "group";
     526                 :           3 :                 break;
     527                 :             :         }
     528                 :             : 
     529                 :             :         /* Begin group for this specific RTE */
     530                 :          38 :         ExplainOpenGroup("Range Table Entry", NULL, true, es);
     531                 :             : 
     532                 :             :         /*
     533                 :             :          * In text format, the summary line displays the range table index and
     534                 :             :          * rtekind, plus indications if rte->inh and/or rte->inFromCl are set.
     535                 :             :          * In other formats, we display those as separate properties.
     536                 :             :          */
     537         [ +  + ]:          38 :         if (es->format == EXPLAIN_FORMAT_TEXT)
     538                 :             :         {
     539                 :          30 :             ExplainIndentText(es);
     540                 :          60 :             appendStringInfo(es->str, "RTI %u (%s%s%s):\n", rti, kind,
     541         [ +  + ]:          30 :                              rte->inh ? ", inherited" : "",
     542         [ +  + ]:          30 :                              rte->inFromCl ? ", in-from-clause" : "");
     543                 :          30 :             es->indent++;
     544                 :             :         }
     545                 :             :         else
     546                 :             :         {
     547                 :           8 :             ExplainPropertyUInteger("RTI", NULL, rti, es);
     548                 :           8 :             ExplainPropertyText("Kind", kind, es);
     549                 :           8 :             ExplainPropertyBool("Inherited", rte->inh, es);
     550                 :           8 :             ExplainPropertyBool("In From Clause", rte->inFromCl, es);
     551                 :             :         }
     552                 :             : 
     553                 :             :         /*
     554                 :             :          * Indicate which subplan is the origin of which RTE. Note dummy
     555                 :             :          * subplans. Here again, we crunch more onto one line in text format.
     556                 :             :          */
     557         [ +  + ]:          38 :         if (rtinfo != NULL)
     558                 :             :         {
     559         [ +  - ]:           9 :             if (es->format == EXPLAIN_FORMAT_TEXT)
     560                 :             :             {
     561         [ +  - ]:           9 :                 if (!rtinfo->dummy)
     562                 :           9 :                     ExplainPropertyText("Subplan", rtinfo->plan_name, es);
     563                 :             :                 else
     564                 :           0 :                     ExplainPropertyText("Subplan",
     565                 :           0 :                                         psprintf("%s (dummy)",
     566                 :             :                                                  rtinfo->plan_name), es);
     567                 :             :             }
     568                 :             :             else
     569                 :             :             {
     570                 :           0 :                 ExplainPropertyText("Subplan", rtinfo->plan_name, es);
     571                 :           0 :                 ExplainPropertyBool("Subplan Is Dummy", rtinfo->dummy, es);
     572                 :             :             }
     573                 :             :         }
     574                 :             : 
     575                 :             :         /* rte->alias is optional; rte->eref is requested */
     576         [ +  + ]:          38 :         if (rte->alias != NULL)
     577                 :          23 :             overexplain_alias("Alias", rte->alias, es);
     578                 :          38 :         overexplain_alias("Eref", rte->eref, es);
     579                 :             : 
     580                 :             :         /*
     581                 :             :          * We adhere to the usual EXPLAIN convention that schema names are
     582                 :             :          * displayed only in verbose mode, and we emit nothing if there is no
     583                 :             :          * relation OID.
     584                 :             :          */
     585         [ +  + ]:          38 :         if (rte->relid != 0)
     586                 :             :         {
     587                 :             :             const char *relname;
     588                 :             :             const char *qualname;
     589                 :             : 
     590                 :          29 :             relname = quote_identifier(get_rel_name(rte->relid));
     591                 :             : 
     592         [ -  + ]:          29 :             if (es->verbose)
     593                 :             :             {
     594                 :           0 :                 Oid         nspoid = get_rel_namespace(rte->relid);
     595                 :             :                 char       *nspname;
     596                 :             : 
     597                 :           0 :                 nspname = get_namespace_name_or_temp(nspoid);
     598                 :           0 :                 qualname = psprintf("%s.%s", quote_identifier(nspname),
     599                 :             :                                     relname);
     600                 :             :             }
     601                 :             :             else
     602                 :          29 :                 qualname = relname;
     603                 :             : 
     604                 :          29 :             ExplainPropertyText("Relation", qualname, es);
     605                 :             :         }
     606                 :             : 
     607                 :             :         /* Translate relkind, if any, to a string */
     608   [ +  -  -  -  :          38 :         switch (rte->relkind)
          -  -  -  -  +  
                -  +  - ]
     609                 :             :         {
     610                 :          18 :             case RELKIND_RELATION:
     611                 :          18 :                 relkind = "relation";
     612                 :          18 :                 break;
     613                 :           0 :             case RELKIND_INDEX:
     614                 :           0 :                 relkind = "index";
     615                 :           0 :                 break;
     616                 :           0 :             case RELKIND_SEQUENCE:
     617                 :           0 :                 relkind = "sequence";
     618                 :           0 :                 break;
     619                 :           0 :             case RELKIND_TOASTVALUE:
     620                 :           0 :                 relkind = "toastvalue";
     621                 :           0 :                 break;
     622                 :           0 :             case RELKIND_VIEW:
     623                 :           0 :                 relkind = "view";
     624                 :           0 :                 break;
     625                 :           0 :             case RELKIND_MATVIEW:
     626                 :           0 :                 relkind = "matview";
     627                 :           0 :                 break;
     628                 :           0 :             case RELKIND_COMPOSITE_TYPE:
     629                 :           0 :                 relkind = "composite_type";
     630                 :           0 :                 break;
     631                 :           0 :             case RELKIND_FOREIGN_TABLE:
     632                 :           0 :                 relkind = "foreign_table";
     633                 :           0 :                 break;
     634                 :          11 :             case RELKIND_PARTITIONED_TABLE:
     635                 :          11 :                 relkind = "partitioned_table";
     636                 :          11 :                 break;
     637                 :           0 :             case RELKIND_PARTITIONED_INDEX:
     638                 :           0 :                 relkind = "partitioned_index";
     639                 :           0 :                 break;
     640                 :           9 :             case '\0':
     641                 :           9 :                 relkind = NULL;
     642                 :           9 :                 break;
     643                 :           0 :             default:
     644                 :           0 :                 relkind = psprintf("%c", rte->relkind);
     645                 :           0 :                 break;
     646                 :             :         }
     647                 :             : 
     648                 :             :         /* If there is a relkind, show it */
     649         [ +  + ]:          38 :         if (relkind != NULL)
     650                 :          29 :             ExplainPropertyText("Relation Kind", relkind, es);
     651                 :             : 
     652                 :             :         /* If there is a lock mode, show it */
     653         [ +  + ]:          38 :         if (rte->rellockmode != 0)
     654                 :          29 :             ExplainPropertyText("Relation Lock Mode",
     655                 :             :                                 GetLockmodeName(DEFAULT_LOCKMETHOD,
     656                 :             :                                                 rte->rellockmode), es);
     657                 :             : 
     658                 :             :         /*
     659                 :             :          * If there is a perminfoindex, show it. We don't try to display
     660                 :             :          * information from the RTEPermissionInfo node here because they are
     661                 :             :          * just indexes plannedstmt->permInfos which could be separately
     662                 :             :          * dumped if someone wants to add EXPLAIN (PERMISSIONS) or similar.
     663                 :             :          */
     664         [ +  + ]:          38 :         if (rte->perminfoindex != 0)
     665                 :          12 :             ExplainPropertyInteger("Permission Info Index", NULL,
     666                 :          12 :                                    rte->perminfoindex, es);
     667                 :             : 
     668                 :             :         /*
     669                 :             :          * add_rte_to_flat_rtable will clear rte->tablesample and
     670                 :             :          * rte->subquery in the finished plan, so skip those fields.
     671                 :             :          *
     672                 :             :          * However, the security_barrier flag is not shown by the core code,
     673                 :             :          * so let's print it here.
     674                 :             :          */
     675   [ +  +  -  + ]:          38 :         if (es->format != EXPLAIN_FORMAT_TEXT || rte->security_barrier)
     676                 :           8 :             ExplainPropertyBool("Security Barrier", rte->security_barrier, es);
     677                 :             : 
     678                 :             :         /*
     679                 :             :          * If this is a join, print out the fields that are specifically valid
     680                 :             :          * for joins.
     681                 :             :          */
     682         [ +  + ]:          38 :         if (rte->rtekind == RTE_JOIN)
     683                 :             :         {
     684                 :             :             char       *jointype;
     685                 :             : 
     686   [ -  +  -  -  :           1 :             switch (rte->jointype)
             -  -  -  -  
                      - ]
     687                 :             :             {
     688                 :           0 :                 case JOIN_INNER:
     689                 :           0 :                     jointype = "Inner";
     690                 :           0 :                     break;
     691                 :           1 :                 case JOIN_LEFT:
     692                 :           1 :                     jointype = "Left";
     693                 :           1 :                     break;
     694                 :           0 :                 case JOIN_FULL:
     695                 :           0 :                     jointype = "Full";
     696                 :           0 :                     break;
     697                 :           0 :                 case JOIN_RIGHT:
     698                 :           0 :                     jointype = "Right";
     699                 :           0 :                     break;
     700                 :           0 :                 case JOIN_SEMI:
     701                 :           0 :                     jointype = "Semi";
     702                 :           0 :                     break;
     703                 :           0 :                 case JOIN_ANTI:
     704                 :           0 :                     jointype = "Anti";
     705                 :           0 :                     break;
     706                 :           0 :                 case JOIN_RIGHT_SEMI:
     707                 :           0 :                     jointype = "Right Semi";
     708                 :           0 :                     break;
     709                 :           0 :                 case JOIN_RIGHT_ANTI:
     710                 :           0 :                     jointype = "Right Anti";
     711                 :           0 :                     break;
     712                 :           0 :                 default:
     713                 :           0 :                     jointype = "???";
     714                 :           0 :                     break;
     715                 :             :             }
     716                 :             : 
     717                 :             :             /* Join type */
     718                 :           1 :             ExplainPropertyText("Join Type", jointype, es);
     719                 :             : 
     720                 :             :             /* # of JOIN USING columns */
     721   [ +  -  -  + ]:           1 :             if (es->format != EXPLAIN_FORMAT_TEXT || rte->joinmergedcols != 0)
     722                 :           0 :                 ExplainPropertyInteger("JOIN USING Columns", NULL,
     723                 :           0 :                                        rte->joinmergedcols, es);
     724                 :             : 
     725                 :             :             /*
     726                 :             :              * add_rte_to_flat_rtable will clear joinaliasvars, joinleftcols,
     727                 :             :              * joinrightcols, and join_using_alias here, so skip those fields.
     728                 :             :              */
     729                 :             :         }
     730                 :             : 
     731                 :             :         /*
     732                 :             :          * add_rte_to_flat_rtable will clear functions, tablefunc, and
     733                 :             :          * values_lists, but we can display funcordinality.
     734                 :             :          */
     735         [ -  + ]:          38 :         if (rte->rtekind == RTE_FUNCTION)
     736                 :           0 :             ExplainPropertyBool("WITH ORDINALITY", rte->funcordinality, es);
     737                 :             : 
     738                 :             :         /*
     739                 :             :          * If this is a CTE, print out CTE-related properties.
     740                 :             :          */
     741         [ -  + ]:          38 :         if (rte->rtekind == RTE_CTE)
     742                 :             :         {
     743                 :           0 :             ExplainPropertyText("CTE Name", rte->ctename, es);
     744                 :           0 :             ExplainPropertyUInteger("CTE Levels Up", NULL, rte->ctelevelsup,
     745                 :             :                                     es);
     746                 :           0 :             ExplainPropertyBool("CTE Self-Reference", rte->self_reference, es);
     747                 :             :         }
     748                 :             : 
     749                 :             :         /*
     750                 :             :          * add_rte_to_flat_rtable will clear coltypes, coltypmods, and
     751                 :             :          * colcollations, so skip those fields.
     752                 :             :          *
     753                 :             :          * If this is an ephemeral named relation, print out ENR-related
     754                 :             :          * properties.
     755                 :             :          */
     756         [ -  + ]:          38 :         if (rte->rtekind == RTE_NAMEDTUPLESTORE)
     757                 :             :         {
     758                 :           0 :             ExplainPropertyText("ENR Name", rte->enrname, es);
     759                 :           0 :             ExplainPropertyFloat("ENR Tuples", NULL, rte->enrtuples, 0, es);
     760                 :             :         }
     761                 :             : 
     762                 :             :         /*
     763                 :             :          * add_rte_to_flat_rtable will clear groupexprs and securityQuals, so
     764                 :             :          * skip that field. We have handled inFromCl above, so the only thing
     765                 :             :          * left to handle here is rte->lateral.
     766                 :             :          */
     767   [ +  +  -  + ]:          38 :         if (es->format != EXPLAIN_FORMAT_TEXT || rte->lateral)
     768                 :           8 :             ExplainPropertyBool("Lateral", rte->lateral, es);
     769                 :             : 
     770                 :             :         /* Done with this RTE */
     771         [ +  + ]:          38 :         if (es->format == EXPLAIN_FORMAT_TEXT)
     772                 :          30 :             es->indent--;
     773                 :          38 :         ExplainCloseGroup("Range Table Entry", NULL, true, es);
     774                 :             :     }
     775                 :             : 
     776                 :             :     /* Close the Range Table array before emitting PlannedStmt-level fields. */
     777                 :          10 :     ExplainCloseGroup("Range Table", "Range Table", false, es);
     778                 :             : 
     779                 :             :     /*
     780                 :             :      * Print PlannedStmt fields that contain RTIs.  These are properties of
     781                 :             :      * the PlannedStmt, not of individual RTEs, so they belong outside the
     782                 :             :      * Range Table array.
     783                 :             :      */
     784         [ +  + ]:          10 :     if (es->format != EXPLAIN_FORMAT_TEXT ||
     785         [ +  + ]:           8 :         !bms_is_empty(plannedstmt->unprunableRelids))
     786                 :           9 :         overexplain_bitmapset("Unprunable RTIs", plannedstmt->unprunableRelids,
     787                 :             :                               es);
     788         [ +  + ]:          10 :     if (es->format != EXPLAIN_FORMAT_TEXT ||
     789         [ +  + ]:           8 :         !bms_is_empty(plannedstmt->resultRelationRelids))
     790                 :           3 :         overexplain_bitmapset("Result RTIs", plannedstmt->resultRelationRelids,
     791                 :             :                               es);
     792                 :          10 : }
     793                 :             : 
     794                 :             : /*
     795                 :             :  * Emit a text property describing the contents of an Alias.
     796                 :             :  *
     797                 :             :  * Column lists can be quite long here, so perhaps we should have an option
     798                 :             :  * to limit the display length by # of column or # of characters, but for
     799                 :             :  * now, just display everything.
     800                 :             :  */
     801                 :             : static void
     802                 :          61 : overexplain_alias(const char *qlabel, Alias *alias, ExplainState *es)
     803                 :             : {
     804                 :             :     StringInfoData buf;
     805                 :          61 :     bool        first = true;
     806                 :             : 
     807                 :             :     Assert(alias != NULL);
     808                 :             : 
     809                 :          61 :     initStringInfo(&buf);
     810                 :          61 :     appendStringInfo(&buf, "%s (", quote_identifier(alias->aliasname));
     811                 :             : 
     812   [ +  +  +  +  :         288 :     foreach_node(String, cn, alias->colnames)
                   +  + ]
     813                 :             :     {
     814         [ +  + ]:         166 :         appendStringInfo(&buf, "%s%s",
     815                 :             :                          first ? "" : ", ",
     816                 :         166 :                          quote_identifier(cn->sval));
     817                 :         166 :         first = false;
     818                 :             :     }
     819                 :             : 
     820                 :          61 :     appendStringInfoChar(&buf, ')');
     821                 :          61 :     ExplainPropertyText(qlabel, buf.data, es);
     822                 :          61 :     pfree(buf.data);
     823                 :          61 : }
     824                 :             : 
     825                 :             : /*
     826                 :             :  * Emit a text property describing the contents of a bitmapset -- either a
     827                 :             :  * space-separated list of integer members, or the word "none" if the bitmapset
     828                 :             :  * is empty.
     829                 :             :  */
     830                 :             : static void
     831                 :          50 : overexplain_bitmapset(const char *qlabel, Bitmapset *bms, ExplainState *es)
     832                 :             : {
     833                 :          50 :     int         x = -1;
     834                 :             : 
     835                 :             :     StringInfoData buf;
     836                 :             : 
     837         [ +  + ]:          50 :     if (bms_is_empty(bms))
     838                 :             :     {
     839                 :          19 :         ExplainPropertyText(qlabel, "none", es);
     840                 :          19 :         return;
     841                 :             :     }
     842                 :             : 
     843                 :          31 :     initStringInfo(&buf);
     844         [ +  + ]:          82 :     while ((x = bms_next_member(bms, x)) >= 0)
     845                 :          51 :         appendStringInfo(&buf, " %d", x);
     846                 :             :     Assert(buf.data[0] == ' ');
     847                 :          31 :     ExplainPropertyText(qlabel, buf.data + 1, es);
     848                 :          31 :     pfree(buf.data);
     849                 :             : }
     850                 :             : 
     851                 :             : /*
     852                 :             :  * Emit a text property describing the contents of a list of bitmapsets.
     853                 :             :  * If a bitmapset contains exactly 1 member, we just print an integer;
     854                 :             :  * otherwise, we surround the list of members by parentheses.
     855                 :             :  *
     856                 :             :  * If there are no bitmapsets in the list, we print the word "none".
     857                 :             :  */
     858                 :             : static void
     859                 :           9 : overexplain_bitmapset_list(const char *qlabel, List *bms_list,
     860                 :             :                            ExplainState *es)
     861                 :             : {
     862                 :             :     StringInfoData buf;
     863                 :             : 
     864                 :           9 :     initStringInfo(&buf);
     865                 :             : 
     866   [ +  +  +  +  :          19 :     foreach_node(Bitmapset, bms, bms_list)
                   +  + ]
     867                 :             :     {
     868         [ +  - ]:           1 :         if (bms_membership(bms) == BMS_SINGLETON)
     869                 :           1 :             appendStringInfo(&buf, " %d", bms_singleton_member(bms));
     870                 :             :         else
     871                 :             :         {
     872                 :           0 :             int         x = -1;
     873                 :           0 :             bool        first = true;
     874                 :             : 
     875                 :           0 :             appendStringInfoString(&buf, " (");
     876         [ #  # ]:           0 :             while ((x = bms_next_member(bms, x)) >= 0)
     877                 :             :             {
     878         [ #  # ]:           0 :                 if (first)
     879                 :           0 :                     first = false;
     880                 :             :                 else
     881                 :           0 :                     appendStringInfoChar(&buf, ' ');
     882                 :           0 :                 appendStringInfo(&buf, "%d", x);
     883                 :             :             }
     884                 :           0 :             appendStringInfoChar(&buf, ')');
     885                 :             :         }
     886                 :             :     }
     887                 :             : 
     888         [ +  + ]:           9 :     if (buf.len == 0)
     889                 :             :     {
     890                 :           8 :         ExplainPropertyText(qlabel, "none", es);
     891                 :           8 :         return;
     892                 :             :     }
     893                 :             : 
     894                 :             :     Assert(buf.data[0] == ' ');
     895                 :           1 :     ExplainPropertyText(qlabel, buf.data + 1, es);
     896                 :           1 :     pfree(buf.data);
     897                 :             : }
     898                 :             : 
     899                 :             : /*
     900                 :             :  * Emit a text property describing the contents of a list of integers, OIDs,
     901                 :             :  * or XIDs -- either a space-separated list of integer members, or the word
     902                 :             :  * "none" if the list is empty.
     903                 :             :  */
     904                 :             : static void
     905                 :          14 : overexplain_intlist(const char *qlabel, List *list, ExplainState *es)
     906                 :             : {
     907                 :             :     StringInfoData buf;
     908                 :             : 
     909                 :          14 :     initStringInfo(&buf);
     910                 :             : 
     911         [ +  + ]:          14 :     if (list == NIL)
     912                 :             :     {
     913                 :           7 :         ExplainPropertyText(qlabel, "none", es);
     914                 :           7 :         return;
     915                 :             :     }
     916                 :             : 
     917         [ -  + ]:           7 :     if (IsA(list, IntList))
     918                 :             :     {
     919   [ #  #  #  #  :           0 :         foreach_int(i, list)
                   #  # ]
     920                 :           0 :             appendStringInfo(&buf, " %d", i);
     921                 :             :     }
     922         [ +  - ]:           7 :     else if (IsA(list, OidList))
     923                 :             :     {
     924   [ +  -  +  +  :          33 :         foreach_oid(o, list)
                   +  + ]
     925                 :          19 :             appendStringInfo(&buf, " %u", o);
     926                 :             :     }
     927         [ #  # ]:           0 :     else if (IsA(list, XidList))
     928                 :             :     {
     929   [ #  #  #  #  :           0 :         foreach_xid(x, list)
                   #  # ]
     930                 :           0 :             appendStringInfo(&buf, " %u", x);
     931                 :             :     }
     932                 :             :     else
     933                 :             :     {
     934                 :           0 :         appendStringInfoString(&buf, " not an integer list");
     935                 :             :         Assert(false);
     936                 :             :     }
     937                 :             : 
     938         [ +  - ]:           7 :     if (buf.len > 0)
     939                 :           7 :         ExplainPropertyText(qlabel, buf.data + 1, es);
     940                 :             : 
     941                 :           7 :     pfree(buf.data);
     942                 :             : }
        

Generated by: LCOV version 2.0-1