LCOV - differential code coverage report
Current view: top level - contrib/pg_overexplain - pg_overexplain.c (source / functions) Coverage Total Hit UNC UBC GIC GNC CBC ECB
Current: a2387c32f2f8a1643c7d71b951587e6bcb2d4744 vs 371a302eecdc82274b0ae2967d18fd726a0aa6a1 Lines: 61.2 % 345 211 1 133 3 208 1
Current Date: 2025-10-26 12:31:50 -0700 Functions: 100.0 % 12 12 1 11
Baseline: lcov-20251027-010456-baseline Branches: 61.4 % 197 121 3 73 1 1 119
Baseline Date: 2025-10-26 11:01:32 +1300 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(30,360] days: 61.2 % 345 211 1 133 3 208 1
Function coverage date bins:
(30,360] days: 100.0 % 12 12 1 11
Branch coverage date bins:
(30,360] days: 61.4 % 197 121 3 73 1 1 119

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

Generated by: LCOV version 2.4-beta