LCOV - differential code coverage report
Current view: top level - src/backend/commands - explain.c (source / functions) Coverage Total Hit UNC UBC GIC GNC CBC ECB DUB DCB
Current: 7a15cff1f11193467898da1c1fabf06fd2caee04 vs 84a3778c79c2d28b4dc281d03ef2ab019b16483b Lines: 80.1 % 2335 1870 15 450 60 1810 3 1 4
Current Date: 2025-12-15 18:36:29 -0500 Functions: 97.2 % 71 69 1 1 10 59
Baseline: lcov-20251216-010103-baseline Branches: 69.9 % 1533 1072 14 447 1 36 1035
Baseline Date: 2025-12-15 13:30:48 -0800 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(1,7] days: 100.0 % 1 1 1
(30,360] days: 79.6 % 211 168 15 28 59 109
(360..) days: 80.1 % 2123 1701 422 1701 3
Function coverage date bins:
(30,360] days: 100.0 % 5 5 1 4
(360..) days: 97.0 % 66 64 1 1 9 55
Branch coverage date bins:
(30,360] days: 80.8 % 104 84 14 6 36 48
(360..) days: 69.1 % 1429 988 441 1 987

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*-------------------------------------------------------------------------
                                  2                 :                :  *
                                  3                 :                :  * explain.c
                                  4                 :                :  *    Explain query execution plans
                                  5                 :                :  *
                                  6                 :                :  * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
                                  7                 :                :  * Portions Copyright (c) 1994-5, Regents of the University of California
                                  8                 :                :  *
                                  9                 :                :  * IDENTIFICATION
                                 10                 :                :  *    src/backend/commands/explain.c
                                 11                 :                :  *
                                 12                 :                :  *-------------------------------------------------------------------------
                                 13                 :                :  */
                                 14                 :                : #include "postgres.h"
                                 15                 :                : 
                                 16                 :                : #include "access/xact.h"
                                 17                 :                : #include "catalog/pg_type.h"
                                 18                 :                : #include "commands/createas.h"
                                 19                 :                : #include "commands/defrem.h"
                                 20                 :                : #include "commands/explain.h"
                                 21                 :                : #include "commands/explain_dr.h"
                                 22                 :                : #include "commands/explain_format.h"
                                 23                 :                : #include "commands/explain_state.h"
                                 24                 :                : #include "commands/prepare.h"
                                 25                 :                : #include "foreign/fdwapi.h"
                                 26                 :                : #include "jit/jit.h"
                                 27                 :                : #include "libpq/pqformat.h"
                                 28                 :                : #include "libpq/protocol.h"
                                 29                 :                : #include "nodes/extensible.h"
                                 30                 :                : #include "nodes/makefuncs.h"
                                 31                 :                : #include "nodes/nodeFuncs.h"
                                 32                 :                : #include "parser/analyze.h"
                                 33                 :                : #include "parser/parsetree.h"
                                 34                 :                : #include "rewrite/rewriteHandler.h"
                                 35                 :                : #include "storage/bufmgr.h"
                                 36                 :                : #include "tcop/tcopprot.h"
                                 37                 :                : #include "utils/builtins.h"
                                 38                 :                : #include "utils/guc_tables.h"
                                 39                 :                : #include "utils/json.h"
                                 40                 :                : #include "utils/lsyscache.h"
                                 41                 :                : #include "utils/rel.h"
                                 42                 :                : #include "utils/ruleutils.h"
                                 43                 :                : #include "utils/snapmgr.h"
                                 44                 :                : #include "utils/tuplesort.h"
                                 45                 :                : #include "utils/typcache.h"
                                 46                 :                : #include "utils/xml.h"
                                 47                 :                : 
                                 48                 :                : 
                                 49                 :                : /* Hook for plugins to get control in ExplainOneQuery() */
                                 50                 :                : ExplainOneQuery_hook_type ExplainOneQuery_hook = NULL;
                                 51                 :                : 
                                 52                 :                : /* Hook for plugins to get control in explain_get_index_name() */
                                 53                 :                : explain_get_index_name_hook_type explain_get_index_name_hook = NULL;
                                 54                 :                : 
                                 55                 :                : /* per-plan and per-node hooks for plugins to print additional info */
                                 56                 :                : explain_per_plan_hook_type explain_per_plan_hook = NULL;
                                 57                 :                : explain_per_node_hook_type explain_per_node_hook = NULL;
                                 58                 :                : 
                                 59                 :                : /*
                                 60                 :                :  * Various places within need to convert bytes to kilobytes.  Round these up
                                 61                 :                :  * to the next whole kilobyte.
                                 62                 :                :  */
                                 63                 :                : #define BYTES_TO_KILOBYTES(b) (((b) + 1023) / 1024)
                                 64                 :                : 
                                 65                 :                : static void ExplainOneQuery(Query *query, int cursorOptions,
                                 66                 :                :                             IntoClause *into, ExplainState *es,
                                 67                 :                :                             ParseState *pstate, ParamListInfo params);
                                 68                 :                : static void ExplainPrintJIT(ExplainState *es, int jit_flags,
                                 69                 :                :                             JitInstrumentation *ji);
                                 70                 :                : static void ExplainPrintSerialize(ExplainState *es,
                                 71                 :                :                                   SerializeMetrics *metrics);
                                 72                 :                : static void report_triggers(ResultRelInfo *rInfo, bool show_relname,
                                 73                 :                :                             ExplainState *es);
                                 74                 :                : static double elapsed_time(instr_time *starttime);
                                 75                 :                : static bool ExplainPreScanNode(PlanState *planstate, Bitmapset **rels_used);
                                 76                 :                : static void ExplainNode(PlanState *planstate, List *ancestors,
                                 77                 :                :                         const char *relationship, const char *plan_name,
                                 78                 :                :                         ExplainState *es);
                                 79                 :                : static void show_plan_tlist(PlanState *planstate, List *ancestors,
                                 80                 :                :                             ExplainState *es);
                                 81                 :                : static void show_expression(Node *node, const char *qlabel,
                                 82                 :                :                             PlanState *planstate, List *ancestors,
                                 83                 :                :                             bool useprefix, ExplainState *es);
                                 84                 :                : static void show_qual(List *qual, const char *qlabel,
                                 85                 :                :                       PlanState *planstate, List *ancestors,
                                 86                 :                :                       bool useprefix, ExplainState *es);
                                 87                 :                : static void show_scan_qual(List *qual, const char *qlabel,
                                 88                 :                :                            PlanState *planstate, List *ancestors,
                                 89                 :                :                            ExplainState *es);
                                 90                 :                : static void show_upper_qual(List *qual, const char *qlabel,
                                 91                 :                :                             PlanState *planstate, List *ancestors,
                                 92                 :                :                             ExplainState *es);
                                 93                 :                : static void show_sort_keys(SortState *sortstate, List *ancestors,
                                 94                 :                :                            ExplainState *es);
                                 95                 :                : static void show_incremental_sort_keys(IncrementalSortState *incrsortstate,
                                 96                 :                :                                        List *ancestors, ExplainState *es);
                                 97                 :                : static void show_merge_append_keys(MergeAppendState *mstate, List *ancestors,
                                 98                 :                :                                    ExplainState *es);
                                 99                 :                : static void show_agg_keys(AggState *astate, List *ancestors,
                                100                 :                :                           ExplainState *es);
                                101                 :                : static void show_grouping_sets(PlanState *planstate, Agg *agg,
                                102                 :                :                                List *ancestors, ExplainState *es);
                                103                 :                : static void show_grouping_set_keys(PlanState *planstate,
                                104                 :                :                                    Agg *aggnode, Sort *sortnode,
                                105                 :                :                                    List *context, bool useprefix,
                                106                 :                :                                    List *ancestors, ExplainState *es);
                                107                 :                : static void show_group_keys(GroupState *gstate, List *ancestors,
                                108                 :                :                             ExplainState *es);
                                109                 :                : static void show_sort_group_keys(PlanState *planstate, const char *qlabel,
                                110                 :                :                                  int nkeys, int nPresortedKeys, AttrNumber *keycols,
                                111                 :                :                                  Oid *sortOperators, Oid *collations, bool *nullsFirst,
                                112                 :                :                                  List *ancestors, ExplainState *es);
                                113                 :                : static void show_sortorder_options(StringInfo buf, Node *sortexpr,
                                114                 :                :                                    Oid sortOperator, Oid collation, bool nullsFirst);
                                115                 :                : static void show_window_def(WindowAggState *planstate,
                                116                 :                :                             List *ancestors, ExplainState *es);
                                117                 :                : static void show_window_keys(StringInfo buf, PlanState *planstate,
                                118                 :                :                              int nkeys, AttrNumber *keycols,
                                119                 :                :                              List *ancestors, ExplainState *es);
                                120                 :                : static void show_storage_info(char *maxStorageType, int64 maxSpaceUsed,
                                121                 :                :                               ExplainState *es);
                                122                 :                : static void show_tablesample(TableSampleClause *tsc, PlanState *planstate,
                                123                 :                :                              List *ancestors, ExplainState *es);
                                124                 :                : static void show_sort_info(SortState *sortstate, ExplainState *es);
                                125                 :                : static void show_incremental_sort_info(IncrementalSortState *incrsortstate,
                                126                 :                :                                        ExplainState *es);
                                127                 :                : static void show_hash_info(HashState *hashstate, ExplainState *es);
                                128                 :                : static void show_material_info(MaterialState *mstate, ExplainState *es);
                                129                 :                : static void show_windowagg_info(WindowAggState *winstate, ExplainState *es);
                                130                 :                : static void show_ctescan_info(CteScanState *ctescanstate, ExplainState *es);
                                131                 :                : static void show_table_func_scan_info(TableFuncScanState *tscanstate,
                                132                 :                :                                       ExplainState *es);
                                133                 :                : static void show_recursive_union_info(RecursiveUnionState *rstate,
                                134                 :                :                                       ExplainState *es);
                                135                 :                : static void show_memoize_info(MemoizeState *mstate, List *ancestors,
                                136                 :                :                               ExplainState *es);
                                137                 :                : static void show_hashagg_info(AggState *aggstate, ExplainState *es);
                                138                 :                : static void show_indexsearches_info(PlanState *planstate, ExplainState *es);
                                139                 :                : static void show_tidbitmap_info(BitmapHeapScanState *planstate,
                                140                 :                :                                 ExplainState *es);
                                141                 :                : static void show_instrumentation_count(const char *qlabel, int which,
                                142                 :                :                                        PlanState *planstate, ExplainState *es);
                                143                 :                : static void show_foreignscan_info(ForeignScanState *fsstate, ExplainState *es);
                                144                 :                : static const char *explain_get_index_name(Oid indexId);
                                145                 :                : static bool peek_buffer_usage(ExplainState *es, const BufferUsage *usage);
                                146                 :                : static void show_buffer_usage(ExplainState *es, const BufferUsage *usage);
                                147                 :                : static void show_wal_usage(ExplainState *es, const WalUsage *usage);
                                148                 :                : static void show_memory_counters(ExplainState *es,
                                149                 :                :                                  const MemoryContextCounters *mem_counters);
                                150                 :                : static void show_result_replacement_info(Result *result, ExplainState *es);
                                151                 :                : static void ExplainIndexScanDetails(Oid indexid, ScanDirection indexorderdir,
                                152                 :                :                                     ExplainState *es);
                                153                 :                : static void ExplainScanTarget(Scan *plan, ExplainState *es);
                                154                 :                : static void ExplainModifyTarget(ModifyTable *plan, ExplainState *es);
                                155                 :                : static void ExplainTargetRel(Plan *plan, Index rti, ExplainState *es);
                                156                 :                : static void show_modifytable_info(ModifyTableState *mtstate, List *ancestors,
                                157                 :                :                                   ExplainState *es);
                                158                 :                : static void ExplainMemberNodes(PlanState **planstates, int nplans,
                                159                 :                :                                List *ancestors, ExplainState *es);
                                160                 :                : static void ExplainMissingMembers(int nplans, int nchildren, ExplainState *es);
                                161                 :                : static void ExplainSubPlans(List *plans, List *ancestors,
                                162                 :                :                             const char *relationship, ExplainState *es);
                                163                 :                : static void ExplainCustomChildren(CustomScanState *css,
                                164                 :                :                                   List *ancestors, ExplainState *es);
                                165                 :                : static ExplainWorkersState *ExplainCreateWorkersState(int num_workers);
                                166                 :                : static void ExplainOpenWorker(int n, ExplainState *es);
                                167                 :                : static void ExplainCloseWorker(int n, ExplainState *es);
                                168                 :                : static void ExplainFlushWorkersState(ExplainState *es);
                                169                 :                : 
                                170                 :                : 
                                171                 :                : 
                                172                 :                : /*
                                173                 :                :  * ExplainQuery -
                                174                 :                :  *    execute an EXPLAIN command
                                175                 :                :  */
                                176                 :                : void
 2173 peter@eisentraut.org      177                 :CBC       12333 : ExplainQuery(ParseState *pstate, ExplainStmt *stmt,
                                178                 :                :              ParamListInfo params, DestReceiver *dest)
                                179                 :                : {
 3988 tgl@sss.pgh.pa.us         180                 :          12333 :     ExplainState *es = NewExplainState();
                                181                 :                :     TupOutputState *tstate;
 1714 bruce@momjian.us          182                 :          12333 :     JumbleState *jstate = NULL;
                                183                 :                :     Query      *query;
                                184                 :                :     List       *rewritten;
                                185                 :                : 
                                186                 :                :     /* Configure the ExplainState based on the provided options */
  273 rhaas@postgresql.org      187                 :          12333 :     ParseExplainOptionList(es, stmt->options, pstate);
                                188                 :                : 
                                189                 :                :     /* Extract the query and, if enabled, jumble it */
 1714 bruce@momjian.us          190                 :          12326 :     query = castNode(Query, stmt->query);
 1676 alvherre@alvh.no-ip.      191         [ +  + ]:          12326 :     if (IsQueryIdEnabled())
  902 michael@paquier.xyz       192                 :           3578 :         jstate = JumbleQuery(query);
                                193                 :                : 
 1714 bruce@momjian.us          194         [ +  + ]:          12326 :     if (post_parse_analyze_hook)
                                195                 :           3558 :         (*post_parse_analyze_hook) (pstate, query, jstate);
                                196                 :                : 
                                197                 :                :     /*
                                198                 :                :      * Parse analysis was done already, but we still have to run the rule
                                199                 :                :      * rewriter.  We do not do AcquireRewriteLocks: we assume the query either
                                200                 :                :      * came straight from the parser, or suitable locks were acquired by
                                201                 :                :      * plancache.c.
                                202                 :                :      */
 1642 tgl@sss.pgh.pa.us         203                 :          12326 :     rewritten = QueryRewrite(castNode(Query, stmt->query));
                                204                 :                : 
                                205                 :                :     /* emit opening boilerplate */
 3988                           206                 :          12326 :     ExplainBeginOutput(es);
                                207                 :                : 
 6853                           208         [ -  + ]:          12326 :     if (rewritten == NIL)
                                209                 :                :     {
                                210                 :                :         /*
                                211                 :                :          * In the case of an INSTEAD NOTHING, tell at least that.  But in
                                212                 :                :          * non-text format, the output is delimited, so this isn't necessary.
                                213                 :                :          */
 3988 tgl@sss.pgh.pa.us         214         [ #  # ]:UBC           0 :         if (es->format == EXPLAIN_FORMAT_TEXT)
                                215                 :              0 :             appendStringInfoString(es->str, "Query rewrites to nothing\n");
                                216                 :                :     }
                                217                 :                :     else
                                218                 :                :     {
                                219                 :                :         ListCell   *l;
                                220                 :                : 
                                221                 :                :         /* Explain every plan */
 6853 tgl@sss.pgh.pa.us         222   [ +  -  +  +  :CBC       24599 :         foreach(l, rewritten)
                                              +  + ]
                                223                 :                :         {
 3172                           224                 :          12332 :             ExplainOneQuery(lfirst_node(Query, l),
                                225                 :                :                             CURSOR_OPT_PARALLEL_OK, NULL, es,
                                226                 :                :                             pstate, params);
                                227                 :                : 
                                228                 :                :             /* Separate plans with an appropriate separator */
 2346                           229         [ +  + ]:          12273 :             if (lnext(rewritten, l) != NULL)
 3988                           230                 :              6 :                 ExplainSeparatePlans(es);
                                231                 :                :         }
                                232                 :                :     }
                                233                 :                : 
                                234                 :                :     /* emit closing boilerplate */
                                235                 :          12267 :     ExplainEndOutput(es);
                                236         [ -  + ]:          12267 :     Assert(es->indent == 0);
                                237                 :                : 
                                238                 :                :     /* output tuples */
 2588 andres@anarazel.de        239                 :          12267 :     tstate = begin_tup_output_tupdesc(dest, ExplainResultDesc(stmt),
                                240                 :                :                                       &TTSOpsVirtual);
 3988 tgl@sss.pgh.pa.us         241         [ +  + ]:          12267 :     if (es->format == EXPLAIN_FORMAT_TEXT)
                                242                 :          12117 :         do_text_output_multiline(tstate, es->str->data);
                                243                 :                :     else
                                244                 :            150 :         do_text_output_oneline(tstate, es->str->data);
 8550 bruce@momjian.us          245                 :          12267 :     end_tup_output(tstate);
                                246                 :                : 
 3988 tgl@sss.pgh.pa.us         247                 :          12267 :     pfree(es->str->data);
 5987                           248                 :          12267 : }
                                249                 :                : 
                                250                 :                : /*
                                251                 :                :  * ExplainResultDesc -
                                252                 :                :  *    construct the result tupledesc for an EXPLAIN
                                253                 :                :  */
                                254                 :                : TupleDesc
 8260                           255                 :          28653 : ExplainResultDesc(ExplainStmt *stmt)
                                256                 :                : {
                                257                 :                :     TupleDesc   tupdesc;
                                258                 :                :     ListCell   *lc;
 5068 rhaas@postgresql.org      259                 :          28653 :     Oid         result_type = TEXTOID;
                                260                 :                : 
                                261                 :                :     /* Check for XML format option */
 5972 tgl@sss.pgh.pa.us         262   [ +  +  +  +  :          54496 :     foreach(lc, stmt->options)
                                              +  + ]
                                263                 :                :     {
 5772 bruce@momjian.us          264                 :          25843 :         DefElem    *opt = (DefElem *) lfirst(lc);
                                265                 :                : 
 5972 tgl@sss.pgh.pa.us         266         [ +  + ]:          25843 :         if (strcmp(opt->defname, "format") == 0)
                                267                 :                :         {
 5772 bruce@momjian.us          268                 :            397 :             char       *p = defGetString(opt);
                                269                 :                : 
 5068 rhaas@postgresql.org      270         [ +  + ]:            397 :             if (strcmp(p, "xml") == 0)
                                271                 :             12 :                 result_type = XMLOID;
                                272         [ +  + ]:            385 :             else if (strcmp(p, "json") == 0)
                                273                 :            349 :                 result_type = JSONOID;
                                274                 :                :             else
                                275                 :             36 :                 result_type = TEXTOID;
                                276                 :                :             /* don't "break", as ExplainQuery will use the last value */
                                277                 :                :         }
                                278                 :                :     }
                                279                 :                : 
                                280                 :                :     /* Need a tuple descriptor representing a single TEXT or XML column */
 2583 andres@anarazel.de        281                 :          28653 :     tupdesc = CreateTemplateTupleDesc(1);
 8260 tgl@sss.pgh.pa.us         282                 :          28653 :     TupleDescInitEntry(tupdesc, (AttrNumber) 1, "QUERY PLAN",
                                283                 :                :                        result_type, -1, 0);
                                284                 :          28653 :     return tupdesc;
                                285                 :                : }
                                286                 :                : 
                                287                 :                : /*
                                288                 :                :  * ExplainOneQuery -
                                289                 :                :  *    print out the execution plan for one Query
                                290                 :                :  *
                                291                 :                :  * "into" is NULL unless we are explaining the contents of a CreateTableAsStmt.
                                292                 :                :  */
                                293                 :                : static void
 3258                           294                 :          12415 : ExplainOneQuery(Query *query, int cursorOptions,
                                295                 :                :                 IntoClause *into, ExplainState *es,
                                296                 :                :                 ParseState *pstate, ParamListInfo params)
                                297                 :                : {
                                298                 :                :     /* planner will not cope with utility statements */
 9089                           299         [ +  + ]:          12415 :     if (query->commandType == CMD_UTILITY)
                                300                 :                :     {
  414 michael@paquier.xyz       301                 :            326 :         ExplainOneUtility(query->utilityStmt, into, es, pstate, params);
 6853 tgl@sss.pgh.pa.us         302                 :            311 :         return;
                                303                 :                :     }
                                304                 :                : 
                                305                 :                :     /* if an advisor plugin is present, let it manage things */
 6780                           306         [ -  + ]:          12089 :     if (ExplainOneQuery_hook)
 3258 tgl@sss.pgh.pa.us         307                 :UBC           0 :         (*ExplainOneQuery_hook) (query, cursorOptions, into, es,
                                308                 :                :                                  pstate->p_sourcetext, params, pstate->p_queryEnv);
                                309                 :                :     else
  645 michael@paquier.xyz       310                 :CBC       12089 :         standard_ExplainOneQuery(query, cursorOptions, into, es,
                                311                 :                :                                  pstate->p_sourcetext, params, pstate->p_queryEnv);
                                312                 :                : }
                                313                 :                : 
                                314                 :                : /*
                                315                 :                :  * standard_ExplainOneQuery -
                                316                 :                :  *    print out the execution plan for one Query, without calling a hook.
                                317                 :                :  */
                                318                 :                : void
                                319                 :          12089 : standard_ExplainOneQuery(Query *query, int cursorOptions,
                                320                 :                :                          IntoClause *into, ExplainState *es,
                                321                 :                :                          const char *queryString, ParamListInfo params,
                                322                 :                :                          QueryEnvironment *queryEnv)
                                323                 :                : {
                                324                 :                :     PlannedStmt *plan;
                                325                 :                :     instr_time  planstart,
                                326                 :                :                 planduration;
                                327                 :                :     BufferUsage bufusage_start,
                                328                 :                :                 bufusage;
                                329                 :                :     MemoryContextCounters mem_counters;
                                330                 :          12089 :     MemoryContext planner_ctx = NULL;
                                331                 :          12089 :     MemoryContext saved_ctx = NULL;
                                332                 :                : 
                                333         [ +  + ]:          12089 :     if (es->memory)
                                334                 :                :     {
                                335                 :                :         /*
                                336                 :                :          * Create a new memory context to measure planner's memory consumption
                                337                 :                :          * accurately.  Note that if the planner were to be modified to use a
                                338                 :                :          * different memory context type, here we would be changing that to
                                339                 :                :          * AllocSet, which might be undesirable.  However, we don't have a way
                                340                 :                :          * to create a context of the same type as another, so we pray and
                                341                 :                :          * hope that this is OK.
                                342                 :                :          */
                                343                 :             12 :         planner_ctx = AllocSetContextCreate(CurrentMemoryContext,
                                344                 :                :                                             "explain analyze planner context",
                                345                 :                :                                             ALLOCSET_DEFAULT_SIZES);
                                346                 :             12 :         saved_ctx = MemoryContextSwitchTo(planner_ctx);
                                347                 :                :     }
                                348                 :                : 
                                349         [ +  + ]:          12089 :     if (es->buffers)
                                350                 :           1292 :         bufusage_start = pgBufferUsage;
                                351                 :          12089 :     INSTR_TIME_SET_CURRENT(planstart);
                                352                 :                : 
                                353                 :                :     /* plan the query */
   69 rhaas@postgresql.org      354                 :GNC       12089 :     plan = pg_plan_query(query, queryString, cursorOptions, params, es);
                                355                 :                : 
  645 michael@paquier.xyz       356                 :CBC       12066 :     INSTR_TIME_SET_CURRENT(planduration);
                                357                 :          12066 :     INSTR_TIME_SUBTRACT(planduration, planstart);
                                358                 :                : 
                                359         [ +  + ]:          12066 :     if (es->memory)
                                360                 :                :     {
                                361                 :             12 :         MemoryContextSwitchTo(saved_ctx);
                                362                 :             12 :         MemoryContextMemConsumed(planner_ctx, &mem_counters);
                                363                 :                :     }
                                364                 :                : 
                                365                 :                :     /* calc differences of buffer counters. */
                                366         [ +  + ]:          12066 :     if (es->buffers)
                                367                 :                :     {
                                368                 :           1292 :         memset(&bufusage, 0, sizeof(BufferUsage));
                                369                 :           1292 :         BufferUsageAccumDiff(&bufusage, &pgBufferUsage, &bufusage_start);
                                370                 :                :     }
                                371                 :                : 
                                372                 :                :     /* run it (if needed) and produce output */
  208 amitlan@postgresql.o      373                 :          24132 :     ExplainOnePlan(plan, into, es, queryString, params, queryEnv,
  645 michael@paquier.xyz       374         [ +  + ]:          12066 :                    &planduration, (es->buffers ? &bufusage : NULL),
                                375         [ +  + ]:          12066 :                    es->memory ? &mem_counters : NULL);
 8353 tgl@sss.pgh.pa.us         376                 :          12045 : }
                                377                 :                : 
                                378                 :                : /*
                                379                 :                :  * ExplainOneUtility -
                                380                 :                :  *    print out the execution plan for one utility statement
                                381                 :                :  *    (In general, utility statements don't have plans, but there are some
                                382                 :                :  *    we treat as special cases)
                                383                 :                :  *
                                384                 :                :  * "into" is NULL unless we are explaining the contents of a CreateTableAsStmt.
                                385                 :                :  *
                                386                 :                :  * This is exported because it's called back from prepare.c in the
                                387                 :                :  * EXPLAIN EXECUTE case.  In that case, we'll be dealing with a statement
                                388                 :                :  * that's in the plan cache, so we have to ensure we don't modify it.
                                389                 :                :  */
                                390                 :                : void
 5020                           391                 :            326 : ExplainOneUtility(Node *utilityStmt, IntoClause *into, ExplainState *es,
                                392                 :                :                   ParseState *pstate, ParamListInfo params)
                                393                 :                : {
 6853                           394         [ -  + ]:            326 :     if (utilityStmt == NULL)
 6853 tgl@sss.pgh.pa.us         395                 :UBC           0 :         return;
                                396                 :                : 
 5020 tgl@sss.pgh.pa.us         397         [ +  + ]:CBC         326 :     if (IsA(utilityStmt, CreateTableAsStmt))
                                398                 :                :     {
                                399                 :                :         /*
                                400                 :                :          * We have to rewrite the contained SELECT and then pass it back to
                                401                 :                :          * ExplainOneQuery.  Copy to be safe in the EXPLAIN EXECUTE case.
                                402                 :                :          */
                                403                 :             83 :         CreateTableAsStmt *ctas = (CreateTableAsStmt *) utilityStmt;
                                404                 :                :         Query      *ctas_query;
                                405                 :                :         List       *rewritten;
  414 michael@paquier.xyz       406                 :             83 :         JumbleState *jstate = NULL;
                                407                 :                : 
                                408                 :                :         /*
                                409                 :                :          * Check if the relation exists or not.  This is done at this stage to
                                410                 :                :          * avoid query planning or execution.
                                411                 :                :          */
 1812                           412         [ +  + ]:             83 :         if (CreateTableAsRelExists(ctas))
                                413                 :                :         {
                                414         [ +  + ]:             15 :             if (ctas->objtype == OBJECT_TABLE)
                                415                 :              9 :                 ExplainDummyGroup("CREATE TABLE AS", NULL, es);
                                416         [ +  - ]:              6 :             else if (ctas->objtype == OBJECT_MATVIEW)
                                417                 :              6 :                 ExplainDummyGroup("CREATE MATERIALIZED VIEW", NULL, es);
                                418                 :                :             else
 1679 tgl@sss.pgh.pa.us         419         [ #  # ]:UBC           0 :                 elog(ERROR, "unexpected object type: %d",
                                420                 :                :                      (int) ctas->objtype);
 1812 michael@paquier.xyz       421                 :CBC          15 :             return;
                                422                 :                :         }
                                423                 :                : 
  414                           424                 :             53 :         ctas_query = castNode(Query, copyObject(ctas->query));
                                425         [ +  + ]:             53 :         if (IsQueryIdEnabled())
                                426                 :             23 :             jstate = JumbleQuery(ctas_query);
                                427         [ +  + ]:             53 :         if (post_parse_analyze_hook)
                                428                 :             19 :             (*post_parse_analyze_hook) (pstate, ctas_query, jstate);
                                429                 :             53 :         rewritten = QueryRewrite(ctas_query);
 4631 tgl@sss.pgh.pa.us         430         [ -  + ]:             53 :         Assert(list_length(rewritten) == 1);
 3172                           431                 :             53 :         ExplainOneQuery(linitial_node(Query, rewritten),
                                432                 :                :                         CURSOR_OPT_PARALLEL_OK, ctas->into, es,
                                433                 :                :                         pstate, params);
                                434                 :                :     }
 3258                           435         [ +  + ]:            243 :     else if (IsA(utilityStmt, DeclareCursorStmt))
                                436                 :                :     {
                                437                 :                :         /*
                                438                 :                :          * Likewise for DECLARE CURSOR.
                                439                 :                :          *
                                440                 :                :          * Notice that if you say EXPLAIN ANALYZE DECLARE CURSOR then we'll
                                441                 :                :          * actually run the query.  This is different from pre-8.3 behavior
                                442                 :                :          * but seems more useful than not running the query.  No cursor will
                                443                 :                :          * be created, however.
                                444                 :                :          */
                                445                 :             30 :         DeclareCursorStmt *dcs = (DeclareCursorStmt *) utilityStmt;
                                446                 :                :         Query      *dcs_query;
                                447                 :                :         List       *rewritten;
  414 michael@paquier.xyz       448                 :             30 :         JumbleState *jstate = NULL;
                                449                 :                : 
                                450                 :             30 :         dcs_query = castNode(Query, copyObject(dcs->query));
                                451         [ +  + ]:             30 :         if (IsQueryIdEnabled())
                                452                 :             13 :             jstate = JumbleQuery(dcs_query);
                                453         [ +  + ]:             30 :         if (post_parse_analyze_hook)
                                454                 :             11 :             (*post_parse_analyze_hook) (pstate, dcs_query, jstate);
                                455                 :                : 
                                456                 :             30 :         rewritten = QueryRewrite(dcs_query);
 3258 tgl@sss.pgh.pa.us         457         [ -  + ]:             30 :         Assert(list_length(rewritten) == 1);
 3172                           458                 :             30 :         ExplainOneQuery(linitial_node(Query, rewritten),
                                459                 :                :                         dcs->options, NULL, es,
                                460                 :                :                         pstate, params);
                                461                 :                :     }
 5020                           462         [ +  - ]:            213 :     else if (IsA(utilityStmt, ExecuteStmt))
                                463                 :            213 :         ExplainExecuteQuery((ExecuteStmt *) utilityStmt, into, es,
                                464                 :                :                             pstate, params);
 6853 tgl@sss.pgh.pa.us         465         [ #  # ]:UBC           0 :     else if (IsA(utilityStmt, NotifyStmt))
                                466                 :                :     {
 5972                           467         [ #  # ]:              0 :         if (es->format == EXPLAIN_FORMAT_TEXT)
                                468                 :              0 :             appendStringInfoString(es->str, "NOTIFY\n");
                                469                 :                :         else
                                470                 :              0 :             ExplainDummyGroup("Notify", NULL, es);
                                471                 :                :     }
                                472                 :                :     else
                                473                 :                :     {
                                474         [ #  # ]:              0 :         if (es->format == EXPLAIN_FORMAT_TEXT)
                                475                 :              0 :             appendStringInfoString(es->str,
                                476                 :                :                                    "Utility statements have no plan structure\n");
                                477                 :                :         else
                                478                 :              0 :             ExplainDummyGroup("Utility Statement", NULL, es);
                                479                 :                :     }
                                480                 :                : }
                                481                 :                : 
                                482                 :                : /*
                                483                 :                :  * ExplainOnePlan -
                                484                 :                :  *      given a planned query, execute it if needed, and then print
                                485                 :                :  *      EXPLAIN output
                                486                 :                :  *
                                487                 :                :  * "into" is NULL unless we are explaining the contents of a CreateTableAsStmt,
                                488                 :                :  * in which case executing the query should result in creating that table.
                                489                 :                :  *
                                490                 :                :  * This is exported because it's called back from prepare.c in the
                                491                 :                :  * EXPLAIN EXECUTE case, and because an index advisor plugin would need
                                492                 :                :  * to call it.
                                493                 :                :  */
                                494                 :                : void
  208 amitlan@postgresql.o      495                 :CBC       12279 : ExplainOnePlan(PlannedStmt *plannedstmt, IntoClause *into, ExplainState *es,
                                496                 :                :                const char *queryString, ParamListInfo params,
                                497                 :                :                QueryEnvironment *queryEnv, const instr_time *planduration,
                                498                 :                :                const BufferUsage *bufusage,
                                499                 :                :                const MemoryContextCounters *mem_counters)
                                500                 :                : {
                                501                 :                :     DestReceiver *dest;
                                502                 :                :     QueryDesc  *queryDesc;
                                503                 :                :     instr_time  starttime;
 8353 tgl@sss.pgh.pa.us         504                 :          12279 :     double      totaltime = 0;
                                505                 :                :     int         eflags;
 5845 rhaas@postgresql.org      506                 :          12279 :     int         instrument_option = 0;
  622 tgl@sss.pgh.pa.us         507                 :          12279 :     SerializeMetrics serializeMetrics = {0};
                                508                 :                : 
 3258                           509         [ -  + ]:          12279 :     Assert(plannedstmt->commandType != CMD_UTILITY);
                                510                 :                : 
 5061 rhaas@postgresql.org      511   [ +  +  +  + ]:          12279 :     if (es->analyze && es->timing)
 5845                           512                 :           1307 :         instrument_option |= INSTRUMENT_TIMER;
 5061                           513         [ +  + ]:          10972 :     else if (es->analyze)
                                514                 :            400 :         instrument_option |= INSTRUMENT_ROWS;
                                515                 :                : 
 5845                           516         [ +  + ]:          12279 :     if (es->buffers)
                                517                 :           1292 :         instrument_option |= INSTRUMENT_BUFFERS;
 2080 akapila@postgresql.o      518         [ -  + ]:          12279 :     if (es->wal)
 2080 akapila@postgresql.o      519                 :UBC           0 :         instrument_option |= INSTRUMENT_WAL;
                                520                 :                : 
                                521                 :                :     /*
                                522                 :                :      * We always collect timing for the entire statement, even when node-level
                                523                 :                :      * timing is off, so we don't look at es->timing here.  (We could skip
                                524                 :                :      * this if !es->summary, but it's hardly worth the complication.)
                                525                 :                :      */
 5406 tgl@sss.pgh.pa.us         526                 :CBC       12279 :     INSTR_TIME_SET_CURRENT(starttime);
                                527                 :                : 
                                528                 :                :     /*
                                529                 :                :      * Use a snapshot with an updated command ID to ensure this query sees
                                530                 :                :      * results of any previously executed queries.
                                531                 :                :      */
 5405                           532                 :          12279 :     PushCopiedSnapshot(GetActiveSnapshot());
                                533                 :          12279 :     UpdateActiveSnapshotCommandId();
                                534                 :                : 
                                535                 :                :     /*
                                536                 :                :      * We discard the output if we have no use for it.  If we're explaining
                                537                 :                :      * CREATE TABLE AS, we'd better use the appropriate tuple receiver, while
                                538                 :                :      * the SERIALIZE option requires its own tuple receiver.  (If you specify
                                539                 :                :      * SERIALIZE while explaining CREATE TABLE AS, you'll see zeroes for the
                                540                 :                :      * results, which is appropriate since no data would have gone to the
                                541                 :                :      * client.)
                                542                 :                :      */
 4631                           543         [ +  + ]:          12279 :     if (into)
                                544                 :             53 :         dest = CreateIntoRelDestReceiver(into);
  622                           545         [ +  + ]:          12226 :     else if (es->serialize != EXPLAIN_SERIALIZE_NONE)
                                546                 :             12 :         dest = CreateExplainSerializeDestReceiver(es);
                                547                 :                :     else
 4631                           548                 :          12214 :         dest = None_Receiver;
                                549                 :                : 
                                550                 :                :     /* Create a QueryDesc for the query */
  208 amitlan@postgresql.o      551                 :          12279 :     queryDesc = CreateQueryDesc(plannedstmt, queryString,
                                552                 :                :                                 GetActiveSnapshot(), InvalidSnapshot,
                                553                 :                :                                 dest, params, queryEnv, instrument_option);
                                554                 :                : 
                                555                 :                :     /* Select execution options */
 5987 tgl@sss.pgh.pa.us         556         [ +  + ]:          12279 :     if (es->analyze)
 7231                           557                 :           1707 :         eflags = 0;             /* default run-to-completion flags */
                                558                 :                :     else
                                559                 :          10572 :         eflags = EXEC_FLAG_EXPLAIN_ONLY;
  998                           560         [ +  + ]:          12279 :     if (es->generic)
                                561                 :              6 :         eflags |= EXEC_FLAG_EXPLAIN_GENERIC;
 5020                           562         [ +  + ]:          12279 :     if (into)
                                563                 :             53 :         eflags |= GetIntoRelEFlags(into);
                                564                 :                : 
                                565                 :                :     /* call ExecutorStart to prepare the plan for execution */
  208 amitlan@postgresql.o      566                 :          12279 :     ExecutorStart(queryDesc, eflags);
                                567                 :                : 
                                568                 :                :     /* Execute the plan for statistics if asked for */
 5987 tgl@sss.pgh.pa.us         569         [ +  + ]:          12261 :     if (es->analyze)
                                570                 :                :     {
                                571                 :                :         ScanDirection dir;
                                572                 :                : 
                                573                 :                :         /* EXPLAIN ANALYZE CREATE TABLE AS WITH NO DATA is weird */
 5020                           574   [ +  +  +  + ]:           1707 :         if (into && into->skipData)
                                575                 :             12 :             dir = NoMovementScanDirection;
                                576                 :                :         else
                                577                 :           1695 :             dir = ForwardScanDirection;
                                578                 :                : 
                                579                 :                :         /* run the plan */
  372                           580                 :           1707 :         ExecutorRun(queryDesc, dir, 0);
                                581                 :                : 
                                582                 :                :         /* run cleanup too */
 5406                           583                 :           1704 :         ExecutorFinish(queryDesc);
                                584                 :                : 
                                585                 :                :         /* We can't run ExecutorEnd 'till we're done printing the stats... */
 8412                           586                 :           1704 :         totaltime += elapsed_time(&starttime);
                                587                 :                :     }
                                588                 :                : 
                                589                 :                :     /* grab serialization metrics before we destroy the DestReceiver */
  622                           590         [ +  + ]:          12258 :     if (es->serialize != EXPLAIN_SERIALIZE_NONE)
                                591                 :             15 :         serializeMetrics = GetSerializationMetrics(dest);
                                592                 :                : 
                                593                 :                :     /* call the DestReceiver's destroy method even during explain */
                                594                 :          12258 :     dest->rDestroy(dest);
                                595                 :                : 
 5972                           596                 :          12258 :     ExplainOpenGroup("Query", NULL, true, es);
                                597                 :                : 
                                598                 :                :     /* Create textual dump of plan tree */
 5987                           599                 :          12258 :     ExplainPrintPlan(es, queryDesc);
                                600                 :                : 
                                601                 :                :     /* Show buffer and/or memory usage in planning */
  687 alvherre@alvh.no-ip.      602   [ +  +  +  + ]:          12258 :     if (peek_buffer_usage(es, bufusage) || mem_counters)
                                603                 :                :     {
 2082 fujii@postgresql.org      604                 :            410 :         ExplainOpenGroup("Planning", "Planning", true, es);
                                605                 :                : 
  687 alvherre@alvh.no-ip.      606         [ +  + ]:            410 :         if (es->format == EXPLAIN_FORMAT_TEXT)
                                607                 :                :         {
                                608                 :            285 :             ExplainIndentText(es);
                                609                 :            285 :             appendStringInfoString(es->str, "Planning:\n");
                                610                 :            285 :             es->indent++;
                                611                 :                :         }
                                612                 :                : 
                                613         [ +  + ]:            410 :         if (bufusage)
                                614                 :            398 :             show_buffer_usage(es, bufusage);
                                615                 :                : 
                                616         [ +  + ]:            410 :         if (mem_counters)
                                617                 :             15 :             show_memory_counters(es, mem_counters);
                                618                 :                : 
                                619         [ +  + ]:            410 :         if (es->format == EXPLAIN_FORMAT_TEXT)
                                620                 :            285 :             es->indent--;
                                621                 :                : 
 1943 fujii@postgresql.org      622                 :            410 :         ExplainCloseGroup("Planning", "Planning", true, es);
                                623                 :                :     }
                                624                 :                : 
 4080 tgl@sss.pgh.pa.us         625   [ +  +  +  - ]:          12258 :     if (es->summary && planduration)
                                626                 :                :     {
 4242 bruce@momjian.us          627                 :           1313 :         double      plantime = INSTR_TIME_GET_DOUBLE(*planduration);
                                628                 :                : 
 2832 andres@anarazel.de        629                 :           1313 :         ExplainPropertyFloat("Planning Time", "ms", 1000.0 * plantime, 3, es);
                                630                 :                :     }
                                631                 :                : 
                                632                 :                :     /* Print info about runtime of triggers */
 5987 tgl@sss.pgh.pa.us         633         [ +  + ]:          12258 :     if (es->analyze)
 4348 alvherre@alvh.no-ip.      634                 :           1704 :         ExplainPrintTriggers(es, queryDesc);
                                635                 :                : 
                                636                 :                :     /*
                                637                 :                :      * Print info about JITing. Tied to es->costs because we don't want to
                                638                 :                :      * display this in regression tests, as it'd cause output differences
                                639                 :                :      * depending on build options.  Might want to separate that out from COSTS
                                640                 :                :      * at a later stage.
                                641                 :                :      */
 2639 andres@anarazel.de        642         [ +  + ]:          12258 :     if (es->costs)
 2631                           643                 :           5277 :         ExplainPrintJITSummary(es, queryDesc);
                                644                 :                : 
                                645                 :                :     /* Print info about serialization of output */
  622 tgl@sss.pgh.pa.us         646         [ +  + ]:          12258 :     if (es->serialize != EXPLAIN_SERIALIZE_NONE)
                                647                 :             15 :         ExplainPrintSerialize(es, &serializeMetrics);
                                648                 :                : 
                                649                 :                :     /* Allow plugins to print additional information */
  273 rhaas@postgresql.org      650         [ +  + ]:          12258 :     if (explain_per_plan_hook)
                                651                 :              9 :         (*explain_per_plan_hook) (plannedstmt, into, es, queryString,
                                652                 :                :                                   params, queryEnv);
                                653                 :                : 
                                654                 :                :     /*
                                655                 :                :      * Close down the query and free resources.  Include time for this in the
                                656                 :                :      * total execution time (although it should be pretty minimal).
                                657                 :                :      */
 7576 tgl@sss.pgh.pa.us         658                 :          12258 :     INSTR_TIME_SET_CURRENT(starttime);
                                659                 :                : 
 8412                           660                 :          12258 :     ExecutorEnd(queryDesc);
                                661                 :                : 
 8402                           662                 :          12258 :     FreeQueryDesc(queryDesc);
                                663                 :                : 
 6427 alvherre@alvh.no-ip.      664                 :          12258 :     PopActiveSnapshot();
                                665                 :                : 
                                666                 :                :     /* We need a CCI just in case query expanded to multiple plans */
 5987 tgl@sss.pgh.pa.us         667         [ +  + ]:          12258 :     if (es->analyze)
 7764                           668                 :           1704 :         CommandCounterIncrement();
                                669                 :                : 
 8412                           670                 :          12258 :     totaltime += elapsed_time(&starttime);
                                671                 :                : 
                                672                 :                :     /*
                                673                 :                :      * We only report execution time if we actually ran the query (that is,
                                674                 :                :      * the user specified ANALYZE), and if summary reporting is enabled (the
                                675                 :                :      * user can set SUMMARY OFF to not have the timing information included in
                                676                 :                :      * the output).  By default, ANALYZE sets SUMMARY to true.
                                677                 :                :      */
 3205 sfrost@snowman.net        678   [ +  +  +  + ]:          12258 :     if (es->summary && es->analyze)
 2832 andres@anarazel.de        679                 :           1310 :         ExplainPropertyFloat("Execution Time", "ms", 1000.0 * totaltime, 3,
                                680                 :                :                              es);
                                681                 :                : 
 5972 tgl@sss.pgh.pa.us         682                 :          12258 :     ExplainCloseGroup("Query", NULL, true, es);
 6236                           683                 :          12258 : }
                                684                 :                : 
                                685                 :                : /*
                                686                 :                :  * ExplainPrintSettings -
                                687                 :                :  *    Print summary of modified settings affecting query planning.
                                688                 :                :  */
                                689                 :                : static void
 2448 tomas.vondra@postgre      690                 :          12268 : ExplainPrintSettings(ExplainState *es)
                                691                 :                : {
                                692                 :                :     int         num;
                                693                 :                :     struct config_generic **gucs;
                                694                 :                : 
                                695                 :                :     /* bail out if information about settings not requested */
                                696         [ +  + ]:          12268 :     if (!es->settings)
                                697                 :          12262 :         return;
                                698                 :                : 
                                699                 :                :     /* request an array of relevant settings */
                                700                 :              6 :     gucs = get_explain_guc_options(&num);
                                701                 :                : 
                                702         [ +  + ]:              6 :     if (es->format != EXPLAIN_FORMAT_TEXT)
                                703                 :                :     {
                                704                 :              3 :         ExplainOpenGroup("Settings", "Settings", true, es);
                                705                 :                : 
 2151 tgl@sss.pgh.pa.us         706         [ +  + ]:              9 :         for (int i = 0; i < num; i++)
                                707                 :                :         {
                                708                 :                :             char       *setting;
 2448 tomas.vondra@postgre      709                 :              6 :             struct config_generic *conf = gucs[i];
                                710                 :                : 
                                711                 :              6 :             setting = GetConfigOptionByName(conf->name, NULL, true);
                                712                 :                : 
                                713                 :              6 :             ExplainPropertyText(conf->name, setting, es);
                                714                 :                :         }
                                715                 :                : 
                                716                 :              3 :         ExplainCloseGroup("Settings", "Settings", true, es);
                                717                 :                :     }
                                718                 :                :     else
                                719                 :                :     {
                                720                 :                :         StringInfoData str;
                                721                 :                : 
                                722                 :                :         /* In TEXT mode, print nothing if there are no options */
 2151 tgl@sss.pgh.pa.us         723         [ -  + ]:              3 :         if (num <= 0)
 2151 tgl@sss.pgh.pa.us         724                 :UBC           0 :             return;
                                725                 :                : 
 2448 tomas.vondra@postgre      726                 :CBC           3 :         initStringInfo(&str);
                                727                 :                : 
 2151 tgl@sss.pgh.pa.us         728         [ +  + ]:              9 :         for (int i = 0; i < num; i++)
                                729                 :                :         {
                                730                 :                :             char       *setting;
 2448 tomas.vondra@postgre      731                 :              6 :             struct config_generic *conf = gucs[i];
                                732                 :                : 
                                733         [ +  + ]:              6 :             if (i > 0)
                                734                 :              3 :                 appendStringInfoString(&str, ", ");
                                735                 :                : 
                                736                 :              6 :             setting = GetConfigOptionByName(conf->name, NULL, true);
                                737                 :                : 
                                738         [ +  - ]:              6 :             if (setting)
                                739                 :              6 :                 appendStringInfo(&str, "%s = '%s'", conf->name, setting);
                                740                 :                :             else
 2448 tomas.vondra@postgre      741                 :UBC           0 :                 appendStringInfo(&str, "%s = NULL", conf->name);
                                742                 :                :         }
                                743                 :                : 
 2151 tgl@sss.pgh.pa.us         744                 :CBC           3 :         ExplainPropertyText("Settings", str.data, es);
                                745                 :                :     }
                                746                 :                : }
                                747                 :                : 
                                748                 :                : /*
                                749                 :                :  * ExplainPrintPlan -
                                750                 :                :  *    convert a QueryDesc's plan tree to text and append it to es->str
                                751                 :                :  *
                                752                 :                :  * The caller should have set up the options fields of *es, as well as
                                753                 :                :  * initializing the output buffer es->str.  Also, output formatting state
                                754                 :                :  * such as the indent level is assumed valid.  Plan-tree-specific fields
                                755                 :                :  * in *es are initialized here.
                                756                 :                :  *
                                757                 :                :  * NB: will not work on utility statements
                                758                 :                :  */
                                759                 :                : void
 5987                           760                 :          12268 : ExplainPrintPlan(ExplainState *es, QueryDesc *queryDesc)
                                761                 :                : {
 4834                           762                 :          12268 :     Bitmapset  *rels_used = NULL;
                                763                 :                :     PlanState  *ps;
                                764                 :                :     ListCell   *lc;
                                765                 :                : 
                                766                 :                :     /* Set up ExplainState fields associated with this plan tree */
 6236                           767         [ -  + ]:          12268 :     Assert(queryDesc->plannedstmt != NULL);
 5987                           768                 :          12268 :     es->pstmt = queryDesc->plannedstmt;
                                769                 :          12268 :     es->rtable = queryDesc->plannedstmt->rtable;
 4834                           770                 :          12268 :     ExplainPreScanNode(queryDesc->planstate, &rels_used);
                                771                 :          12268 :     es->rtable_names = select_rtable_names_for_explain(es->rtable, rels_used);
 2197                           772                 :          12268 :     es->deparse_cxt = deparse_context_for_plan_tree(queryDesc->plannedstmt,
                                773                 :                :                                                     es->rtable_names);
 3445                           774                 :          12268 :     es->printed_subplans = NULL;
  462 rguo@postgresql.org       775                 :          12268 :     es->rtable_size = list_length(es->rtable);
                                776   [ +  -  +  +  :          43831 :     foreach(lc, es->rtable)
                                              +  + ]
                                777                 :                :     {
                                778                 :          32494 :         RangeTblEntry *rte = lfirst_node(RangeTblEntry, lc);
                                779                 :                : 
                                780         [ +  + ]:          32494 :         if (rte->rtekind == RTE_GROUP)
                                781                 :                :         {
                                782                 :            931 :             es->rtable_size--;
                                783                 :            931 :             break;
                                784                 :                :         }
                                785                 :                :     }
                                786                 :                : 
                                787                 :                :     /*
                                788                 :                :      * Sometimes we mark a Gather node as "invisible", which means that it's
                                789                 :                :      * not to be displayed in EXPLAIN output.  The purpose of this is to allow
                                790                 :                :      * running regression tests with debug_parallel_query=regress to get the
                                791                 :                :      * same results as running the same tests with debug_parallel_query=off.
                                792                 :                :      * Such marking is currently only supported on a Gather at the top of the
                                793                 :                :      * plan.  We skip that node, and we must also hide per-worker detail data
                                794                 :                :      * further down in the plan tree.
                                795                 :                :      */
 3600 rhaas@postgresql.org      796                 :          12268 :     ps = queryDesc->planstate;
 2040 tgl@sss.pgh.pa.us         797   [ +  +  -  + ]:          12268 :     if (IsA(ps, GatherState) && ((Gather *) ps->plan)->invisible)
                                798                 :                :     {
 3600 rhaas@postgresql.org      799                 :UBC           0 :         ps = outerPlanState(ps);
 2192 tgl@sss.pgh.pa.us         800                 :              0 :         es->hide_workers = true;
                                801                 :                :     }
 3600 rhaas@postgresql.org      802                 :CBC       12268 :     ExplainNode(ps, NIL, NULL, NULL, es);
                                803                 :                : 
                                804                 :                :     /*
                                805                 :                :      * If requested, include information about GUC parameters with values that
                                806                 :                :      * don't match the built-in defaults.
                                807                 :                :      */
 2448 tomas.vondra@postgre      808                 :          12268 :     ExplainPrintSettings(es);
                                809                 :                : 
                                810                 :                :     /*
                                811                 :                :      * COMPUTE_QUERY_ID_REGRESS means COMPUTE_QUERY_ID_AUTO, but we don't show
                                812                 :                :      * the queryid in any of the EXPLAIN plans to keep stable the results
                                813                 :                :      * generated by regression test suites.
                                814                 :                :      */
  200 drowley@postgresql.o      815   [ +  +  +  + ]:          12268 :     if (es->verbose && queryDesc->plannedstmt->queryId != INT64CONST(0) &&
 1055 michael@paquier.xyz       816         [ +  + ]:            352 :         compute_query_id != COMPUTE_QUERY_ID_REGRESS)
                                817                 :                :     {
  200 drowley@postgresql.o      818                 :             10 :         ExplainPropertyInteger("Query Identifier", NULL,
 1055 michael@paquier.xyz       819                 :             10 :                                queryDesc->plannedstmt->queryId, es);
                                820                 :                :     }
10752 scrappy@hub.org           821                 :          12268 : }
                                822                 :                : 
                                823                 :                : /*
                                824                 :                :  * ExplainPrintTriggers -
                                825                 :                :  *    convert a QueryDesc's trigger statistics to text and append it to
                                826                 :                :  *    es->str
                                827                 :                :  *
                                828                 :                :  * The caller should have set up the options fields of *es, as well as
                                829                 :                :  * initializing the output buffer es->str.  Other fields in *es are
                                830                 :                :  * initialized here.
                                831                 :                :  */
                                832                 :                : void
 4348 alvherre@alvh.no-ip.      833                 :           1704 : ExplainPrintTriggers(ExplainState *es, QueryDesc *queryDesc)
                                834                 :                : {
                                835                 :                :     ResultRelInfo *rInfo;
                                836                 :                :     bool        show_relname;
                                837                 :                :     List       *resultrels;
                                838                 :                :     List       *routerels;
                                839                 :                :     List       *targrels;
                                840                 :                :     ListCell   *l;
                                841                 :                : 
 1890 heikki.linnakangas@i      842                 :           1704 :     resultrels = queryDesc->estate->es_opened_result_relations;
 2868 rhaas@postgresql.org      843                 :           1704 :     routerels = queryDesc->estate->es_tuple_routing_result_relations;
                                844                 :           1704 :     targrels = queryDesc->estate->es_trig_target_relations;
                                845                 :                : 
 4348 alvherre@alvh.no-ip.      846                 :           1704 :     ExplainOpenGroup("Triggers", "Triggers", false, es);
                                847                 :                : 
 1890 heikki.linnakangas@i      848         [ +  - ]:           3401 :     show_relname = (list_length(resultrels) > 1 ||
 2868 rhaas@postgresql.org      849   [ +  +  -  + ]:           3401 :                     routerels != NIL || targrels != NIL);
 1890 heikki.linnakangas@i      850   [ +  +  +  +  :           1764 :     foreach(l, resultrels)
                                              +  + ]
                                851                 :                :     {
                                852                 :             60 :         rInfo = (ResultRelInfo *) lfirst(l);
 3042 rhaas@postgresql.org      853                 :             60 :         report_triggers(rInfo, show_relname, es);
                                854                 :                :     }
                                855                 :                : 
 2868                           856   [ -  +  -  -  :           1704 :     foreach(l, routerels)
                                              -  + ]
                                857                 :                :     {
 3042 rhaas@postgresql.org      858                 :UBC           0 :         rInfo = (ResultRelInfo *) lfirst(l);
                                859                 :              0 :         report_triggers(rInfo, show_relname, es);
                                860                 :                :     }
                                861                 :                : 
 4348 alvherre@alvh.no-ip.      862   [ -  +  -  -  :CBC        1704 :     foreach(l, targrels)
                                              -  + ]
                                863                 :                :     {
 4348 alvherre@alvh.no-ip.      864                 :UBC           0 :         rInfo = (ResultRelInfo *) lfirst(l);
                                865                 :              0 :         report_triggers(rInfo, show_relname, es);
                                866                 :                :     }
                                867                 :                : 
 4348 alvherre@alvh.no-ip.      868                 :CBC        1704 :     ExplainCloseGroup("Triggers", "Triggers", false, es);
                                869                 :           1704 : }
                                870                 :                : 
                                871                 :                : /*
                                872                 :                :  * ExplainPrintJITSummary -
                                873                 :                :  *    Print summarized JIT instrumentation from leader and workers
                                874                 :                :  */
                                875                 :                : void
 2631 andres@anarazel.de        876                 :           5287 : ExplainPrintJITSummary(ExplainState *es, QueryDesc *queryDesc)
                                877                 :                : {
                                878                 :           5287 :     JitInstrumentation ji = {0};
                                879                 :                : 
                                880         [ +  + ]:           5287 :     if (!(queryDesc->estate->es_jit_flags & PGJIT_PERFORM))
                                881                 :           5275 :         return;
                                882                 :                : 
                                883                 :                :     /*
                                884                 :                :      * Work with a copy instead of modifying the leader state, since this
                                885                 :                :      * function may be called twice
                                886                 :                :      */
                                887         [ -  + ]:             12 :     if (queryDesc->estate->es_jit)
 2631 andres@anarazel.de        888                 :UBC           0 :         InstrJitAgg(&ji, &queryDesc->estate->es_jit->instr);
                                889                 :                : 
                                890                 :                :     /* If this process has done JIT in parallel workers, merge stats */
 2631 andres@anarazel.de        891         [ +  - ]:CBC          12 :     if (queryDesc->estate->es_jit_worker_instr)
                                892                 :             12 :         InstrJitAgg(&ji, queryDesc->estate->es_jit_worker_instr);
                                893                 :                : 
 2152 tgl@sss.pgh.pa.us         894                 :             12 :     ExplainPrintJIT(es, queryDesc->estate->es_jit_flags, &ji);
                                895                 :                : }
                                896                 :                : 
                                897                 :                : /*
                                898                 :                :  * ExplainPrintJIT -
                                899                 :                :  *    Append information about JITing to es->str.
                                900                 :                :  */
                                901                 :                : static void
                                902                 :             12 : ExplainPrintJIT(ExplainState *es, int jit_flags, JitInstrumentation *ji)
                                903                 :                : {
                                904                 :                :     instr_time  total_time;
                                905                 :                : 
                                906                 :                :     /* don't print information if no JITing happened */
 2639 andres@anarazel.de        907   [ +  -  +  - ]:             12 :     if (!ji || ji->created_functions == 0)
                                908                 :             12 :         return;
                                909                 :                : 
                                910                 :                :     /* calculate total time */
 2640 andres@anarazel.de        911                 :UBC           0 :     INSTR_TIME_SET_ZERO(total_time);
                                912                 :                :     /* don't add deform_counter, it's included in generation_counter */
 2639                           913                 :              0 :     INSTR_TIME_ADD(total_time, ji->generation_counter);
                                914                 :              0 :     INSTR_TIME_ADD(total_time, ji->inlining_counter);
                                915                 :              0 :     INSTR_TIME_ADD(total_time, ji->optimization_counter);
                                916                 :              0 :     INSTR_TIME_ADD(total_time, ji->emission_counter);
                                917                 :                : 
 2820                           918                 :              0 :     ExplainOpenGroup("JIT", "JIT", true, es);
                                919                 :                : 
                                920                 :                :     /* for higher density, open code the text output format */
                                921         [ #  # ]:              0 :     if (es->format == EXPLAIN_FORMAT_TEXT)
                                922                 :                :     {
 2152 tgl@sss.pgh.pa.us         923                 :              0 :         ExplainIndentText(es);
                                924                 :              0 :         appendStringInfoString(es->str, "JIT:\n");
                                925                 :              0 :         es->indent++;
                                926                 :                : 
 2639 andres@anarazel.de        927                 :              0 :         ExplainPropertyInteger("Functions", NULL, ji->created_functions, es);
                                928                 :                : 
 2152 tgl@sss.pgh.pa.us         929                 :              0 :         ExplainIndentText(es);
 2640 andres@anarazel.de        930                 :              0 :         appendStringInfo(es->str, "Options: %s %s, %s %s, %s %s, %s %s\n",
 2639                           931         [ #  # ]:              0 :                          "Inlining", jit_flags & PGJIT_INLINE ? "true" : "false",
                                932         [ #  # ]:              0 :                          "Optimization", jit_flags & PGJIT_OPT3 ? "true" : "false",
                                933         [ #  # ]:              0 :                          "Expressions", jit_flags & PGJIT_EXPR ? "true" : "false",
                                934         [ #  # ]:              0 :                          "Deforming", jit_flags & PGJIT_DEFORM ? "true" : "false");
                                935                 :                : 
 2640                           936   [ #  #  #  # ]:              0 :         if (es->analyze && es->timing)
                                937                 :                :         {
 2152 tgl@sss.pgh.pa.us         938                 :              0 :             ExplainIndentText(es);
 2640 andres@anarazel.de        939                 :              0 :             appendStringInfo(es->str,
                                940                 :                :                              "Timing: %s %.3f ms (%s %.3f ms), %s %.3f ms, %s %.3f ms, %s %.3f ms, %s %.3f ms\n",
 2639                           941                 :              0 :                              "Generation", 1000.0 * INSTR_TIME_GET_DOUBLE(ji->generation_counter),
  830 dgustafsson@postgres      942                 :              0 :                              "Deform", 1000.0 * INSTR_TIME_GET_DOUBLE(ji->deform_counter),
 2639 andres@anarazel.de        943                 :              0 :                              "Inlining", 1000.0 * INSTR_TIME_GET_DOUBLE(ji->inlining_counter),
                                944                 :              0 :                              "Optimization", 1000.0 * INSTR_TIME_GET_DOUBLE(ji->optimization_counter),
                                945                 :              0 :                              "Emission", 1000.0 * INSTR_TIME_GET_DOUBLE(ji->emission_counter),
 2640                           946                 :              0 :                              "Total", 1000.0 * INSTR_TIME_GET_DOUBLE(total_time));
                                947                 :                :         }
                                948                 :                : 
 2152 tgl@sss.pgh.pa.us         949                 :              0 :         es->indent--;
                                950                 :                :     }
                                951                 :                :     else
                                952                 :                :     {
 2639 andres@anarazel.de        953                 :              0 :         ExplainPropertyInteger("Functions", NULL, ji->created_functions, es);
                                954                 :                : 
 2640                           955                 :              0 :         ExplainOpenGroup("Options", "Options", true, es);
 2639                           956                 :              0 :         ExplainPropertyBool("Inlining", jit_flags & PGJIT_INLINE, es);
                                957                 :              0 :         ExplainPropertyBool("Optimization", jit_flags & PGJIT_OPT3, es);
                                958                 :              0 :         ExplainPropertyBool("Expressions", jit_flags & PGJIT_EXPR, es);
                                959                 :              0 :         ExplainPropertyBool("Deforming", jit_flags & PGJIT_DEFORM, es);
 2640                           960                 :              0 :         ExplainCloseGroup("Options", "Options", true, es);
                                961                 :                : 
                                962   [ #  #  #  # ]:              0 :         if (es->analyze && es->timing)
                                963                 :                :         {
                                964                 :              0 :             ExplainOpenGroup("Timing", "Timing", true, es);
                                965                 :                : 
  830 dgustafsson@postgres      966                 :              0 :             ExplainOpenGroup("Generation", "Generation", true, es);
                                967                 :              0 :             ExplainPropertyFloat("Deform", "ms",
                                968                 :              0 :                                  1000.0 * INSTR_TIME_GET_DOUBLE(ji->deform_counter),
                                969                 :                :                                  3, es);
                                970                 :              0 :             ExplainPropertyFloat("Total", "ms",
 2639 andres@anarazel.de        971                 :              0 :                                  1000.0 * INSTR_TIME_GET_DOUBLE(ji->generation_counter),
                                972                 :                :                                  3, es);
  830 dgustafsson@postgres      973                 :              0 :             ExplainCloseGroup("Generation", "Generation", true, es);
                                974                 :                : 
 2640 andres@anarazel.de        975                 :              0 :             ExplainPropertyFloat("Inlining", "ms",
 2639                           976                 :              0 :                                  1000.0 * INSTR_TIME_GET_DOUBLE(ji->inlining_counter),
                                977                 :                :                                  3, es);
 2640                           978                 :              0 :             ExplainPropertyFloat("Optimization", "ms",
 2639                           979                 :              0 :                                  1000.0 * INSTR_TIME_GET_DOUBLE(ji->optimization_counter),
                                980                 :                :                                  3, es);
 2640                           981                 :              0 :             ExplainPropertyFloat("Emission", "ms",
 2639                           982                 :              0 :                                  1000.0 * INSTR_TIME_GET_DOUBLE(ji->emission_counter),
                                983                 :                :                                  3, es);
 2640                           984                 :              0 :             ExplainPropertyFloat("Total", "ms",
                                985                 :              0 :                                  1000.0 * INSTR_TIME_GET_DOUBLE(total_time),
                                986                 :                :                                  3, es);
                                987                 :                : 
                                988                 :              0 :             ExplainCloseGroup("Timing", "Timing", true, es);
                                989                 :                :         }
                                990                 :                :     }
                                991                 :                : 
                                992                 :              0 :     ExplainCloseGroup("JIT", "JIT", true, es);
                                993                 :                : }
                                994                 :                : 
                                995                 :                : /*
                                996                 :                :  * ExplainPrintSerialize -
                                997                 :                :  *    Append information about query output volume to es->str.
                                998                 :                :  */
                                999                 :                : static void
  622 tgl@sss.pgh.pa.us        1000                 :CBC          15 : ExplainPrintSerialize(ExplainState *es, SerializeMetrics *metrics)
                               1001                 :                : {
                               1002                 :                :     const char *format;
                               1003                 :                : 
                               1004                 :                :     /* We shouldn't get called for EXPLAIN_SERIALIZE_NONE */
                               1005         [ +  + ]:             15 :     if (es->serialize == EXPLAIN_SERIALIZE_TEXT)
                               1006                 :             12 :         format = "text";
                               1007                 :                :     else
                               1008                 :                :     {
                               1009         [ -  + ]:              3 :         Assert(es->serialize == EXPLAIN_SERIALIZE_BINARY);
                               1010                 :              3 :         format = "binary";
                               1011                 :                :     }
                               1012                 :                : 
                               1013                 :             15 :     ExplainOpenGroup("Serialization", "Serialization", true, es);
                               1014                 :                : 
                               1015         [ +  + ]:             15 :     if (es->format == EXPLAIN_FORMAT_TEXT)
                               1016                 :                :     {
                               1017                 :             12 :         ExplainIndentText(es);
                               1018         [ +  + ]:             12 :         if (es->timing)
  616 peter@eisentraut.org     1019                 :              9 :             appendStringInfo(es->str, "Serialization: time=%.3f ms  output=" UINT64_FORMAT "kB  format=%s\n",
  622 tgl@sss.pgh.pa.us        1020                 :              9 :                              1000.0 * INSTR_TIME_GET_DOUBLE(metrics->timeSpent),
  579 drowley@postgresql.o     1021                 :              9 :                              BYTES_TO_KILOBYTES(metrics->bytesSent),
                               1022                 :                :                              format);
                               1023                 :                :         else
  616 peter@eisentraut.org     1024                 :              3 :             appendStringInfo(es->str, "Serialization: output=" UINT64_FORMAT "kB  format=%s\n",
  579 drowley@postgresql.o     1025                 :              3 :                              BYTES_TO_KILOBYTES(metrics->bytesSent),
                               1026                 :                :                              format);
                               1027                 :                : 
  622 tgl@sss.pgh.pa.us        1028   [ +  +  -  + ]:             12 :         if (es->buffers && peek_buffer_usage(es, &metrics->bufferUsage))
                               1029                 :                :         {
  622 tgl@sss.pgh.pa.us        1030                 :UBC           0 :             es->indent++;
                               1031                 :              0 :             show_buffer_usage(es, &metrics->bufferUsage);
                               1032                 :              0 :             es->indent--;
                               1033                 :                :         }
                               1034                 :                :     }
                               1035                 :                :     else
                               1036                 :                :     {
  622 tgl@sss.pgh.pa.us        1037         [ +  - ]:CBC           3 :         if (es->timing)
                               1038                 :              3 :             ExplainPropertyFloat("Time", "ms",
                               1039                 :              3 :                                  1000.0 * INSTR_TIME_GET_DOUBLE(metrics->timeSpent),
                               1040                 :                :                                  3, es);
                               1041                 :              3 :         ExplainPropertyUInteger("Output Volume", "kB",
  579 drowley@postgresql.o     1042                 :              3 :                                 BYTES_TO_KILOBYTES(metrics->bytesSent), es);
  622 tgl@sss.pgh.pa.us        1043                 :              3 :         ExplainPropertyText("Format", format, es);
                               1044         [ +  - ]:              3 :         if (es->buffers)
                               1045                 :              3 :             show_buffer_usage(es, &metrics->bufferUsage);
                               1046                 :                :     }
                               1047                 :                : 
                               1048                 :             15 :     ExplainCloseGroup("Serialization", "Serialization", true, es);
                               1049                 :             15 : }
                               1050                 :                : 
                               1051                 :                : /*
                               1052                 :                :  * ExplainQueryText -
                               1053                 :                :  *    add a "Query Text" node that contains the actual text of the query
                               1054                 :                :  *
                               1055                 :                :  * The caller should have set up the options fields of *es, as well as
                               1056                 :                :  * initializing the output buffer es->str.
                               1057                 :                :  *
                               1058                 :                :  */
                               1059                 :                : void
 5782 andrew@dunslane.net      1060                 :             10 : ExplainQueryText(ExplainState *es, QueryDesc *queryDesc)
                               1061                 :                : {
                               1062         [ +  - ]:             10 :     if (queryDesc->sourceText)
                               1063                 :             10 :         ExplainPropertyText("Query Text", queryDesc->sourceText, es);
                               1064                 :             10 : }
                               1065                 :                : 
                               1066                 :                : /*
                               1067                 :                :  * ExplainQueryParameters -
                               1068                 :                :  *    add a "Query Parameters" node that describes the parameters of the query
                               1069                 :                :  *
                               1070                 :                :  * The caller should have set up the options fields of *es, as well as
                               1071                 :                :  * initializing the output buffer es->str.
                               1072                 :                :  *
                               1073                 :                :  */
                               1074                 :                : void
 1259 michael@paquier.xyz      1075                 :             10 : ExplainQueryParameters(ExplainState *es, ParamListInfo params, int maxlen)
                               1076                 :                : {
                               1077                 :                :     char       *str;
                               1078                 :                : 
                               1079                 :                :     /* This check is consistent with errdetail_params() */
                               1080   [ +  +  +  -  :             10 :     if (params == NULL || params->numParams <= 0 || maxlen == 0)
                                              +  + ]
                               1081                 :              7 :         return;
                               1082                 :                : 
                               1083                 :              3 :     str = BuildParamLogString(params, NULL, maxlen);
                               1084   [ +  -  +  - ]:              3 :     if (str && str[0] != '\0')
                               1085                 :              3 :         ExplainPropertyText("Query Parameters", str, es);
                               1086                 :                : }
                               1087                 :                : 
                               1088                 :                : /*
                               1089                 :                :  * report_triggers -
                               1090                 :                :  *      report execution stats for a single relation's triggers
                               1091                 :                :  */
                               1092                 :                : static void
 5972 tgl@sss.pgh.pa.us        1093                 :             60 : report_triggers(ResultRelInfo *rInfo, bool show_relname, ExplainState *es)
                               1094                 :                : {
                               1095                 :                :     int         nt;
                               1096                 :                : 
 6698                          1097   [ -  +  -  - ]:             60 :     if (!rInfo->ri_TrigDesc || !rInfo->ri_TrigInstrument)
                               1098                 :             60 :         return;
 6698 tgl@sss.pgh.pa.us        1099         [ #  # ]:UBC           0 :     for (nt = 0; nt < rInfo->ri_TrigDesc->numtriggers; nt++)
                               1100                 :                :     {
                               1101                 :              0 :         Trigger    *trig = rInfo->ri_TrigDesc->triggers + nt;
                               1102                 :              0 :         Instrumentation *instr = rInfo->ri_TrigInstrument + nt;
                               1103                 :                :         char       *relname;
 5972                          1104                 :              0 :         char       *conname = NULL;
                               1105                 :                : 
                               1106                 :                :         /* Must clean up instrumentation state */
 6698                          1107                 :              0 :         InstrEndLoop(instr);
                               1108                 :                : 
                               1109                 :                :         /*
                               1110                 :                :          * We ignore triggers that were never invoked; they likely aren't
                               1111                 :                :          * relevant to the current query type.
                               1112                 :                :          */
                               1113         [ #  # ]:              0 :         if (instr->ntuples == 0)
                               1114                 :              0 :             continue;
                               1115                 :                : 
 5972                          1116                 :              0 :         ExplainOpenGroup("Trigger", NULL, true, es);
                               1117                 :                : 
                               1118                 :              0 :         relname = RelationGetRelationName(rInfo->ri_RelationDesc);
                               1119         [ #  # ]:              0 :         if (OidIsValid(trig->tgconstraint))
                               1120                 :              0 :             conname = get_constraint_name(trig->tgconstraint);
                               1121                 :                : 
                               1122                 :                :         /*
                               1123                 :                :          * In text format, we avoid printing both the trigger name and the
                               1124                 :                :          * constraint name unless VERBOSE is specified.  In non-text formats
                               1125                 :                :          * we just print everything.
                               1126                 :                :          */
                               1127         [ #  # ]:              0 :         if (es->format == EXPLAIN_FORMAT_TEXT)
                               1128                 :                :         {
                               1129   [ #  #  #  # ]:              0 :             if (es->verbose || conname == NULL)
                               1130                 :              0 :                 appendStringInfo(es->str, "Trigger %s", trig->tgname);
                               1131                 :                :             else
                               1132                 :              0 :                 appendStringInfoString(es->str, "Trigger");
                               1133         [ #  # ]:              0 :             if (conname)
                               1134                 :              0 :                 appendStringInfo(es->str, " for constraint %s", conname);
                               1135         [ #  # ]:              0 :             if (show_relname)
                               1136                 :              0 :                 appendStringInfo(es->str, " on %s", relname);
 3413                          1137         [ #  # ]:              0 :             if (es->timing)
                               1138                 :              0 :                 appendStringInfo(es->str, ": time=%.3f calls=%.0f\n",
                               1139                 :              0 :                                  1000.0 * instr->total, instr->ntuples);
                               1140                 :                :             else
                               1141                 :              0 :                 appendStringInfo(es->str, ": calls=%.0f\n", instr->ntuples);
                               1142                 :                :         }
                               1143                 :                :         else
                               1144                 :                :         {
 5972                          1145                 :              0 :             ExplainPropertyText("Trigger Name", trig->tgname, es);
                               1146         [ #  # ]:              0 :             if (conname)
                               1147                 :              0 :                 ExplainPropertyText("Constraint Name", conname, es);
                               1148                 :              0 :             ExplainPropertyText("Relation", relname, es);
 3413                          1149         [ #  # ]:              0 :             if (es->timing)
 2832 andres@anarazel.de       1150                 :              0 :                 ExplainPropertyFloat("Time", "ms", 1000.0 * instr->total, 3,
                               1151                 :                :                                      es);
                               1152                 :              0 :             ExplainPropertyFloat("Calls", NULL, instr->ntuples, 0, es);
                               1153                 :                :         }
                               1154                 :                : 
 5972 tgl@sss.pgh.pa.us        1155         [ #  # ]:              0 :         if (conname)
                               1156                 :              0 :             pfree(conname);
                               1157                 :                : 
                               1158                 :              0 :         ExplainCloseGroup("Trigger", NULL, true, es);
                               1159                 :                :     }
                               1160                 :                : }
                               1161                 :                : 
                               1162                 :                : /* Compute elapsed time in seconds since given timestamp */
                               1163                 :                : static double
 7549 tgl@sss.pgh.pa.us        1164                 :CBC       13962 : elapsed_time(instr_time *starttime)
                               1165                 :                : {
                               1166                 :                :     instr_time  endtime;
                               1167                 :                : 
 7576                          1168                 :          13962 :     INSTR_TIME_SET_CURRENT(endtime);
 6425                          1169                 :          13962 :     INSTR_TIME_SUBTRACT(endtime, *starttime);
 7576                          1170                 :          13962 :     return INSTR_TIME_GET_DOUBLE(endtime);
                               1171                 :                : }
                               1172                 :                : 
                               1173                 :                : /*
                               1174                 :                :  * ExplainPreScanNode -
                               1175                 :                :  *    Prescan the planstate tree to identify which RTEs are referenced
                               1176                 :                :  *
                               1177                 :                :  * Adds the relid of each referenced RTE to *rels_used.  The result controls
                               1178                 :                :  * which RTEs are assigned aliases by select_rtable_names_for_explain.
                               1179                 :                :  * This ensures that we don't confusingly assign un-suffixed aliases to RTEs
                               1180                 :                :  * that never appear in the EXPLAIN output (such as inheritance parents).
                               1181                 :                :  */
                               1182                 :                : static bool
 4834                          1183                 :          43948 : ExplainPreScanNode(PlanState *planstate, Bitmapset **rels_used)
                               1184                 :                : {
                               1185                 :          43948 :     Plan       *plan = planstate->plan;
                               1186                 :                : 
                               1187   [ +  +  -  +  :          43948 :     switch (nodeTag(plan))
                                        +  +  +  + ]
                               1188                 :                :     {
                               1189                 :          20200 :         case T_SeqScan:
                               1190                 :                :         case T_SampleScan:
                               1191                 :                :         case T_IndexScan:
                               1192                 :                :         case T_IndexOnlyScan:
                               1193                 :                :         case T_BitmapHeapScan:
                               1194                 :                :         case T_TidScan:
                               1195                 :                :         case T_TidRangeScan:
                               1196                 :                :         case T_SubqueryScan:
                               1197                 :                :         case T_FunctionScan:
                               1198                 :                :         case T_TableFuncScan:
                               1199                 :                :         case T_ValuesScan:
                               1200                 :                :         case T_CteScan:
                               1201                 :                :         case T_NamedTuplestoreScan:
                               1202                 :                :         case T_WorkTableScan:
                               1203                 :          40400 :             *rels_used = bms_add_member(*rels_used,
                               1204                 :          20200 :                                         ((Scan *) plan)->scanrelid);
                               1205                 :          20200 :             break;
 3882 rhaas@postgresql.org     1206                 :            431 :         case T_ForeignScan:
                               1207                 :            862 :             *rels_used = bms_add_members(*rels_used,
 1051 tgl@sss.pgh.pa.us        1208                 :            431 :                                          ((ForeignScan *) plan)->fs_base_relids);
 3882 rhaas@postgresql.org     1209                 :            431 :             break;
 3882 rhaas@postgresql.org     1210                 :UBC           0 :         case T_CustomScan:
                               1211                 :              0 :             *rels_used = bms_add_members(*rels_used,
 3100 tgl@sss.pgh.pa.us        1212                 :              0 :                                          ((CustomScan *) plan)->custom_relids);
 3882 rhaas@postgresql.org     1213                 :              0 :             break;
 4834 tgl@sss.pgh.pa.us        1214                 :CBC         556 :         case T_ModifyTable:
                               1215                 :           1112 :             *rels_used = bms_add_member(*rels_used,
 3100                          1216                 :            556 :                                         ((ModifyTable *) plan)->nominalRelation);
 3875 andres@anarazel.de       1217         [ +  + ]:            556 :             if (((ModifyTable *) plan)->exclRelRTI)
                               1218                 :             49 :                 *rels_used = bms_add_member(*rels_used,
 3100 tgl@sss.pgh.pa.us        1219                 :             49 :                                             ((ModifyTable *) plan)->exclRelRTI);
                               1220                 :                :             /* Ensure Vars used in RETURNING will have refnames */
  208                          1221         [ +  + ]:            556 :             if (plan->targetlist)
                               1222                 :            127 :                 *rels_used = bms_add_member(*rels_used,
                               1223                 :            127 :                                             linitial_int(((ModifyTable *) plan)->resultRelations));
 4834                          1224                 :            556 :             break;
 2197                          1225                 :           1795 :         case T_Append:
                               1226                 :           3590 :             *rels_used = bms_add_members(*rels_used,
                               1227                 :           1795 :                                          ((Append *) plan)->apprelids);
                               1228                 :           1795 :             break;
                               1229                 :            174 :         case T_MergeAppend:
                               1230                 :            348 :             *rels_used = bms_add_members(*rels_used,
                               1231                 :            174 :                                          ((MergeAppend *) plan)->apprelids);
                               1232                 :            174 :             break;
   84 rhaas@postgresql.org     1233                 :GNC        1561 :         case T_Result:
                               1234                 :           3122 :             *rels_used = bms_add_members(*rels_used,
                               1235                 :           1561 :                                          ((Result *) plan)->relids);
                               1236                 :           1561 :             break;
 4834 tgl@sss.pgh.pa.us        1237                 :CBC       19231 :         default:
                               1238                 :          19231 :             break;
                               1239                 :                :     }
                               1240                 :                : 
 3743 rhaas@postgresql.org     1241                 :          43948 :     return planstate_tree_walker(planstate, ExplainPreScanNode, rels_used);
                               1242                 :                : }
                               1243                 :                : 
                               1244                 :                : /*
                               1245                 :                :  * plan_is_disabled
                               1246                 :                :  *      Checks if the given plan node type was disabled during query planning.
                               1247                 :                :  *      This is evident by the disabled_nodes field being higher than the sum of
                               1248                 :                :  *      the disabled_nodes field from the plan's children.
                               1249                 :                :  */
                               1250                 :                : static bool
  431 drowley@postgresql.o     1251                 :          43858 : plan_is_disabled(Plan *plan)
                               1252                 :                : {
                               1253                 :                :     int         child_disabled_nodes;
                               1254                 :                : 
                               1255                 :                :     /* The node is certainly not disabled if this is zero */
                               1256         [ +  + ]:          43858 :     if (plan->disabled_nodes == 0)
                               1257                 :          43727 :         return false;
                               1258                 :                : 
                               1259                 :            131 :     child_disabled_nodes = 0;
                               1260                 :                : 
                               1261                 :                :     /*
                               1262                 :                :      * Handle special nodes first.  Children of BitmapOrs and BitmapAnds can't
                               1263                 :                :      * be disabled, so no need to handle those specifically.
                               1264                 :                :      */
                               1265         [ +  + ]:            131 :     if (IsA(plan, Append))
                               1266                 :                :     {
                               1267                 :                :         ListCell   *lc;
                               1268                 :              1 :         Append     *aplan = (Append *) plan;
                               1269                 :                : 
                               1270                 :                :         /*
                               1271                 :                :          * Sum the Append childrens' disabled_nodes.  This purposefully
                               1272                 :                :          * includes any run-time pruned children.  Ignoring those could give
                               1273                 :                :          * us the incorrect number of disabled nodes.
                               1274                 :                :          */
                               1275   [ +  -  +  +  :              3 :         foreach(lc, aplan->appendplans)
                                              +  + ]
                               1276                 :                :         {
                               1277                 :              2 :             Plan       *subplan = lfirst(lc);
                               1278                 :                : 
                               1279                 :              2 :             child_disabled_nodes += subplan->disabled_nodes;
                               1280                 :                :         }
                               1281                 :                :     }
                               1282         [ +  + ]:            130 :     else if (IsA(plan, MergeAppend))
                               1283                 :                :     {
                               1284                 :                :         ListCell   *lc;
                               1285                 :              3 :         MergeAppend *maplan = (MergeAppend *) plan;
                               1286                 :                : 
                               1287                 :                :         /*
                               1288                 :                :          * Sum the MergeAppend childrens' disabled_nodes.  This purposefully
                               1289                 :                :          * includes any run-time pruned children.  Ignoring those could give
                               1290                 :                :          * us the incorrect number of disabled nodes.
                               1291                 :                :          */
                               1292   [ +  -  +  +  :             15 :         foreach(lc, maplan->mergeplans)
                                              +  + ]
                               1293                 :                :         {
                               1294                 :             12 :             Plan       *subplan = lfirst(lc);
                               1295                 :                : 
                               1296                 :             12 :             child_disabled_nodes += subplan->disabled_nodes;
                               1297                 :                :         }
                               1298                 :                :     }
                               1299         [ -  + ]:            127 :     else if (IsA(plan, SubqueryScan))
  431 drowley@postgresql.o     1300                 :UBC           0 :         child_disabled_nodes += ((SubqueryScan *) plan)->subplan->disabled_nodes;
  431 drowley@postgresql.o     1301         [ -  + ]:CBC         127 :     else if (IsA(plan, CustomScan))
                               1302                 :                :     {
                               1303                 :                :         ListCell   *lc;
  431 drowley@postgresql.o     1304                 :UBC           0 :         CustomScan *cplan = (CustomScan *) plan;
                               1305                 :                : 
                               1306   [ #  #  #  #  :              0 :         foreach(lc, cplan->custom_plans)
                                              #  # ]
                               1307                 :                :         {
                               1308                 :              0 :             Plan       *subplan = lfirst(lc);
                               1309                 :                : 
                               1310                 :              0 :             child_disabled_nodes += subplan->disabled_nodes;
                               1311                 :                :         }
                               1312                 :                :     }
                               1313                 :                :     else
                               1314                 :                :     {
                               1315                 :                :         /*
                               1316                 :                :          * Else, sum up disabled_nodes from the plan's inner and outer side.
                               1317                 :                :          */
  431 drowley@postgresql.o     1318         [ +  + ]:CBC         127 :         if (outerPlan(plan))
                               1319                 :             83 :             child_disabled_nodes += outerPlan(plan)->disabled_nodes;
                               1320         [ +  + ]:            127 :         if (innerPlan(plan))
                               1321                 :             24 :             child_disabled_nodes += innerPlan(plan)->disabled_nodes;
                               1322                 :                :     }
                               1323                 :                : 
                               1324                 :                :     /*
                               1325                 :                :      * It's disabled if the plan's disabled_nodes is higher than the sum of
                               1326                 :                :      * its child's plan disabled_nodes.
                               1327                 :                :      */
                               1328         [ +  + ]:            131 :     if (plan->disabled_nodes > child_disabled_nodes)
                               1329                 :             50 :         return true;
                               1330                 :                : 
                               1331                 :             81 :     return false;
                               1332                 :                : }
                               1333                 :                : 
                               1334                 :                : /*
                               1335                 :                :  * ExplainNode -
                               1336                 :                :  *    Appends a description of a plan tree to es->str
                               1337                 :                :  *
                               1338                 :                :  * planstate points to the executor state node for the current plan node.
                               1339                 :                :  * We need to work from a PlanState node, not just a Plan node, in order to
                               1340                 :                :  * get at the instrumentation data (if any) as well as the list of subplans.
                               1341                 :                :  *
                               1342                 :                :  * ancestors is a list of parent Plan and SubPlan nodes, most-closely-nested
                               1343                 :                :  * first.  These are needed in order to interpret PARAM_EXEC Params.
                               1344                 :                :  *
                               1345                 :                :  * relationship describes the relationship of this plan node to its parent
                               1346                 :                :  * (eg, "Outer", "Inner"); it can be null at top level.  plan_name is an
                               1347                 :                :  * optional name to be attached to the node.
                               1348                 :                :  *
                               1349                 :                :  * In text format, es->indent is controlled in this function since we only
                               1350                 :                :  * want it to change at plan-node boundaries (but a few subroutines will
                               1351                 :                :  * transiently increment it).  In non-text formats, es->indent corresponds
                               1352                 :                :  * to the nesting depth of logical output groups, and therefore is controlled
                               1353                 :                :  * by ExplainOpenGroup/ExplainCloseGroup.
                               1354                 :                :  */
                               1355                 :                : static void
 5635 tgl@sss.pgh.pa.us        1356                 :          43858 : ExplainNode(PlanState *planstate, List *ancestors,
                               1357                 :                :             const char *relationship, const char *plan_name,
                               1358                 :                :             ExplainState *es)
                               1359                 :                : {
                               1360                 :          43858 :     Plan       *plan = planstate->plan;
                               1361                 :                :     const char *pname;          /* node type name for text output */
                               1362                 :                :     const char *sname;          /* node type name for non-text output */
 5972                          1363                 :          43858 :     const char *strategy = NULL;
 3457                          1364                 :          43858 :     const char *partialmode = NULL;
 5911                          1365                 :          43858 :     const char *operation = NULL;
 4057 rhaas@postgresql.org     1366                 :          43858 :     const char *custom_name = NULL;
 2152 tgl@sss.pgh.pa.us        1367                 :          43858 :     ExplainWorkersState *save_workers_state = es->workers_state;
 5972                          1368                 :          43858 :     int         save_indent = es->indent;
                               1369                 :                :     bool        haschildren;
                               1370                 :                :     bool        isdisabled;
                               1371                 :                : 
                               1372                 :                :     /*
                               1373                 :                :      * Prepare per-worker output buffers, if needed.  We'll append the data in
                               1374                 :                :      * these to the main output string further down.
                               1375                 :                :      */
 2152                          1376   [ +  +  +  -  :          43858 :     if (planstate->worker_instrument && es->analyze && !es->hide_workers)
                                              +  - ]
                               1377                 :            513 :         es->workers_state = ExplainCreateWorkersState(planstate->worker_instrument->num_workers);
                               1378                 :                :     else
                               1379                 :          43345 :         es->workers_state = NULL;
                               1380                 :                : 
                               1381                 :                :     /* Identify plan node type, and print generic details */
10327 bruce@momjian.us         1382   [ +  +  +  +  :          43858 :     switch (nodeTag(plan))
                                     +  +  +  +  +  
                                     +  +  +  +  +  
                                     +  +  +  +  +  
                                     +  +  +  +  +  
                                     +  +  +  +  +  
                                     -  +  +  +  +  
                                     +  +  +  +  +  
                                        +  +  +  - ]
                               1383                 :                :     {
10326                          1384                 :           1540 :         case T_Result:
 5972 tgl@sss.pgh.pa.us        1385                 :           1540 :             pname = sname = "Result";
10326 bruce@momjian.us         1386                 :           1540 :             break;
 3254 andres@anarazel.de       1387                 :            102 :         case T_ProjectSet:
                               1388                 :            102 :             pname = sname = "ProjectSet";
                               1389                 :            102 :             break;
 5911 tgl@sss.pgh.pa.us        1390                 :            556 :         case T_ModifyTable:
                               1391                 :            556 :             sname = "ModifyTable";
                               1392   [ +  +  +  +  :            556 :             switch (((ModifyTable *) plan)->operation)
                                                 - ]
                               1393                 :                :             {
                               1394                 :            137 :                 case CMD_INSERT:
                               1395                 :            137 :                     pname = operation = "Insert";
                               1396                 :            137 :                     break;
                               1397                 :            224 :                 case CMD_UPDATE:
                               1398                 :            224 :                     pname = operation = "Update";
                               1399                 :            224 :                     break;
                               1400                 :             93 :                 case CMD_DELETE:
                               1401                 :             93 :                     pname = operation = "Delete";
                               1402                 :             93 :                     break;
 1359 alvherre@alvh.no-ip.     1403                 :            102 :                 case CMD_MERGE:
                               1404                 :            102 :                     pname = operation = "Merge";
                               1405                 :            102 :                     break;
 5911 tgl@sss.pgh.pa.us        1406                 :UBC           0 :                 default:
                               1407                 :              0 :                     pname = "???";
                               1408                 :              0 :                     break;
                               1409                 :                :             }
 5911 tgl@sss.pgh.pa.us        1410                 :CBC         556 :             break;
10326 bruce@momjian.us         1411                 :           1777 :         case T_Append:
 5972 tgl@sss.pgh.pa.us        1412                 :           1777 :             pname = sname = "Append";
10326 bruce@momjian.us         1413                 :           1777 :             break;
 5542 tgl@sss.pgh.pa.us        1414                 :            174 :         case T_MergeAppend:
                               1415                 :            174 :             pname = sname = "Merge Append";
                               1416                 :            174 :             break;
 6282                          1417                 :             27 :         case T_RecursiveUnion:
 5972                          1418                 :             27 :             pname = sname = "Recursive Union";
 6282                          1419                 :             27 :             break;
 7546                          1420                 :             21 :         case T_BitmapAnd:
 5972                          1421                 :             21 :             pname = sname = "BitmapAnd";
 7546                          1422                 :             21 :             break;
                               1423                 :             73 :         case T_BitmapOr:
 5972                          1424                 :             73 :             pname = sname = "BitmapOr";
 7546                          1425                 :             73 :             break;
10326 bruce@momjian.us         1426                 :           1866 :         case T_NestLoop:
 5972 tgl@sss.pgh.pa.us        1427                 :           1866 :             pname = sname = "Nested Loop";
10326 bruce@momjian.us         1428                 :           1866 :             break;
                               1429                 :            394 :         case T_MergeJoin:
 5772                          1430                 :            394 :             pname = "Merge";  /* "Join" gets added by jointype switch */
 5972 tgl@sss.pgh.pa.us        1431                 :            394 :             sname = "Merge Join";
10326 bruce@momjian.us         1432                 :            394 :             break;
                               1433                 :           2143 :         case T_HashJoin:
 5772                          1434                 :           2143 :             pname = "Hash";       /* "Join" gets added by jointype switch */
 5972 tgl@sss.pgh.pa.us        1435                 :           2143 :             sname = "Hash Join";
10326 bruce@momjian.us         1436                 :           2143 :             break;
                               1437                 :          13455 :         case T_SeqScan:
 5972 tgl@sss.pgh.pa.us        1438                 :          13455 :             pname = sname = "Seq Scan";
10326 bruce@momjian.us         1439                 :          13455 :             break;
 3797 tgl@sss.pgh.pa.us        1440                 :             60 :         case T_SampleScan:
                               1441                 :             60 :             pname = sname = "Sample Scan";
                               1442                 :             60 :             break;
 3730 rhaas@postgresql.org     1443                 :            328 :         case T_Gather:
                               1444                 :            328 :             pname = sname = "Gather";
                               1445                 :            328 :             break;
 3204                          1446                 :            114 :         case T_GatherMerge:
                               1447                 :            114 :             pname = sname = "Gather Merge";
                               1448                 :            114 :             break;
10326 bruce@momjian.us         1449                 :           2081 :         case T_IndexScan:
 5180 tgl@sss.pgh.pa.us        1450                 :           2081 :             pname = sname = "Index Scan";
                               1451                 :           2081 :             break;
                               1452                 :           1412 :         case T_IndexOnlyScan:
                               1453                 :           1412 :             pname = sname = "Index Only Scan";
10326 bruce@momjian.us         1454                 :           1412 :             break;
 7546 tgl@sss.pgh.pa.us        1455                 :           2164 :         case T_BitmapIndexScan:
 5972                          1456                 :           2164 :             pname = sname = "Bitmap Index Scan";
 7546                          1457                 :           2164 :             break;
                               1458                 :           2067 :         case T_BitmapHeapScan:
 5972                          1459                 :           2067 :             pname = sname = "Bitmap Heap Scan";
 7546                          1460                 :           2067 :             break;
 9209                          1461                 :             33 :         case T_TidScan:
 5972                          1462                 :             33 :             pname = sname = "Tid Scan";
 9209                          1463                 :             33 :             break;
 1753 drowley@postgresql.o     1464                 :             57 :         case T_TidRangeScan:
                               1465                 :             57 :             pname = sname = "Tid Range Scan";
                               1466                 :             57 :             break;
 9209 tgl@sss.pgh.pa.us        1467                 :            217 :         case T_SubqueryScan:
 5972                          1468                 :            217 :             pname = sname = "Subquery Scan";
 9209                          1469                 :            217 :             break;
 8619                          1470                 :            288 :         case T_FunctionScan:
 5972                          1471                 :            288 :             pname = sname = "Function Scan";
 8619                          1472                 :            288 :             break;
 3205 alvherre@alvh.no-ip.     1473                 :             39 :         case T_TableFuncScan:
                               1474                 :             39 :             pname = sname = "Table Function Scan";
                               1475                 :             39 :             break;
 7076 mail@joeconway.com       1476                 :            282 :         case T_ValuesScan:
 5972 tgl@sss.pgh.pa.us        1477                 :            282 :             pname = sname = "Values Scan";
 7076 mail@joeconway.com       1478                 :            282 :             break;
 6282 tgl@sss.pgh.pa.us        1479                 :            125 :         case T_CteScan:
 5972                          1480                 :            125 :             pname = sname = "CTE Scan";
 6282                          1481                 :            125 :             break;
 3182 kgrittn@postgresql.o     1482                 :             12 :         case T_NamedTuplestoreScan:
                               1483                 :             12 :             pname = sname = "Named Tuplestore Scan";
                               1484                 :             12 :             break;
 6282 tgl@sss.pgh.pa.us        1485                 :             27 :         case T_WorkTableScan:
 5972                          1486                 :             27 :             pname = sname = "WorkTable Scan";
 6282                          1487                 :             27 :             break;
 5413                          1488                 :            431 :         case T_ForeignScan:
 3560 rhaas@postgresql.org     1489                 :            431 :             sname = "Foreign Scan";
                               1490   [ +  -  +  +  :            431 :             switch (((ForeignScan *) plan)->operation)
                                                 - ]
                               1491                 :                :             {
                               1492                 :            399 :                 case CMD_SELECT:
                               1493                 :            399 :                     pname = "Foreign Scan";
                               1494                 :            399 :                     operation = "Select";
                               1495                 :            399 :                     break;
 3560 rhaas@postgresql.org     1496                 :UBC           0 :                 case CMD_INSERT:
                               1497                 :              0 :                     pname = "Foreign Insert";
                               1498                 :              0 :                     operation = "Insert";
                               1499                 :              0 :                     break;
 3560 rhaas@postgresql.org     1500                 :CBC          18 :                 case CMD_UPDATE:
                               1501                 :             18 :                     pname = "Foreign Update";
                               1502                 :             18 :                     operation = "Update";
                               1503                 :             18 :                     break;
                               1504                 :             14 :                 case CMD_DELETE:
                               1505                 :             14 :                     pname = "Foreign Delete";
                               1506                 :             14 :                     operation = "Delete";
                               1507                 :             14 :                     break;
 3560 rhaas@postgresql.org     1508                 :UBC           0 :                 default:
                               1509                 :              0 :                     pname = "???";
                               1510                 :              0 :                     break;
                               1511                 :                :             }
 5413 tgl@sss.pgh.pa.us        1512                 :CBC         431 :             break;
 4057 rhaas@postgresql.org     1513                 :UBC           0 :         case T_CustomScan:
                               1514                 :              0 :             sname = "Custom Scan";
                               1515                 :              0 :             custom_name = ((CustomScan *) plan)->methods->CustomName;
                               1516         [ #  # ]:              0 :             if (custom_name)
                               1517                 :              0 :                 pname = psprintf("Custom Scan (%s)", custom_name);
                               1518                 :                :             else
                               1519                 :              0 :                 pname = sname;
                               1520                 :              0 :             break;
 9619 tgl@sss.pgh.pa.us        1521                 :CBC         553 :         case T_Material:
 5972                          1522                 :            553 :             pname = sname = "Materialize";
 9619                          1523                 :            553 :             break;
 1616 drowley@postgresql.o     1524                 :            177 :         case T_Memoize:
                               1525                 :            177 :             pname = sname = "Memoize";
 1719                          1526                 :            177 :             break;
10326 bruce@momjian.us         1527                 :           2379 :         case T_Sort:
 5972 tgl@sss.pgh.pa.us        1528                 :           2379 :             pname = sname = "Sort";
10326 bruce@momjian.us         1529                 :           2379 :             break;
 2080 tomas.vondra@postgre     1530                 :            196 :         case T_IncrementalSort:
                               1531                 :            196 :             pname = sname = "Incremental Sort";
                               1532                 :            196 :             break;
10326 bruce@momjian.us         1533                 :             48 :         case T_Group:
 5972 tgl@sss.pgh.pa.us        1534                 :             48 :             pname = sname = "Group";
10326 bruce@momjian.us         1535                 :             48 :             break;
                               1536                 :           5230 :         case T_Agg:
                               1537                 :                :             {
 3618 rhaas@postgresql.org     1538                 :           5230 :                 Agg        *agg = (Agg *) plan;
                               1539                 :                : 
 3457 tgl@sss.pgh.pa.us        1540                 :           5230 :                 sname = "Aggregate";
 3618 rhaas@postgresql.org     1541   [ +  +  +  +  :           5230 :                 switch (agg->aggstrategy)
                                                 - ]
                               1542                 :                :                 {
                               1543                 :           3700 :                     case AGG_PLAIN:
                               1544                 :           3700 :                         pname = "Aggregate";
                               1545                 :           3700 :                         strategy = "Plain";
                               1546                 :           3700 :                         break;
                               1547                 :            310 :                     case AGG_SORTED:
                               1548                 :            310 :                         pname = "GroupAggregate";
                               1549                 :            310 :                         strategy = "Sorted";
                               1550                 :            310 :                         break;
                               1551                 :           1170 :                     case AGG_HASHED:
                               1552                 :           1170 :                         pname = "HashAggregate";
                               1553                 :           1170 :                         strategy = "Hashed";
                               1554                 :           1170 :                         break;
 3186 rhodiumtoad@postgres     1555                 :             50 :                     case AGG_MIXED:
                               1556                 :             50 :                         pname = "MixedAggregate";
                               1557                 :             50 :                         strategy = "Mixed";
                               1558                 :             50 :                         break;
 3618 rhaas@postgresql.org     1559                 :UBC           0 :                     default:
                               1560                 :              0 :                         pname = "Aggregate ???";
                               1561                 :              0 :                         strategy = "???";
                               1562                 :              0 :                         break;
                               1563                 :                :                 }
                               1564                 :                : 
 3457 tgl@sss.pgh.pa.us        1565         [ +  + ]:CBC        5230 :                 if (DO_AGGSPLIT_SKIPFINAL(agg->aggsplit))
                               1566                 :                :                 {
                               1567                 :            493 :                     partialmode = "Partial";
                               1568                 :            493 :                     pname = psprintf("%s %s", partialmode, pname);
                               1569                 :                :                 }
                               1570         [ +  + ]:           4737 :                 else if (DO_AGGSPLIT_COMBINE(agg->aggsplit))
                               1571                 :                :                 {
                               1572                 :            368 :                     partialmode = "Finalize";
                               1573                 :            368 :                     pname = psprintf("%s %s", partialmode, pname);
                               1574                 :                :                 }
                               1575                 :                :                 else
                               1576                 :           4369 :                     partialmode = "Simple";
                               1577                 :                :             }
10326 bruce@momjian.us         1578                 :           5230 :             break;
 6197 tgl@sss.pgh.pa.us        1579                 :            235 :         case T_WindowAgg:
 5972                          1580                 :            235 :             pname = sname = "WindowAgg";
 6197                          1581                 :            235 :             break;
10326 bruce@momjian.us         1582                 :            221 :         case T_Unique:
 5972 tgl@sss.pgh.pa.us        1583                 :            221 :             pname = sname = "Unique";
10326 bruce@momjian.us         1584                 :            221 :             break;
 9203 tgl@sss.pgh.pa.us        1585                 :             57 :         case T_SetOp:
 5972                          1586                 :             57 :             sname = "SetOp";
 6340                          1587      [ +  +  - ]:             57 :             switch (((SetOp *) plan)->strategy)
                               1588                 :                :             {
                               1589                 :             33 :                 case SETOP_SORTED:
 5972                          1590                 :             33 :                     pname = "SetOp";
                               1591                 :             33 :                     strategy = "Sorted";
 9203                          1592                 :             33 :                     break;
 6340                          1593                 :             24 :                 case SETOP_HASHED:
 5972                          1594                 :             24 :                     pname = "HashSetOp";
                               1595                 :             24 :                     strategy = "Hashed";
 9203                          1596                 :             24 :                     break;
 9203 tgl@sss.pgh.pa.us        1597                 :UBC           0 :                 default:
                               1598                 :              0 :                     pname = "SetOp ???";
 5972                          1599                 :              0 :                     strategy = "???";
 9203                          1600                 :              0 :                     break;
                               1601                 :                :             }
 9203 tgl@sss.pgh.pa.us        1602                 :CBC          57 :             break;
 5909                          1603                 :            211 :         case T_LockRows:
                               1604                 :            211 :             pname = sname = "LockRows";
                               1605                 :            211 :             break;
 9182                          1606                 :            543 :         case T_Limit:
 5972                          1607                 :            543 :             pname = sname = "Limit";
 9182                          1608                 :            543 :             break;
10326 bruce@momjian.us         1609                 :           2143 :         case T_Hash:
 5972 tgl@sss.pgh.pa.us        1610                 :           2143 :             pname = sname = "Hash";
10326 bruce@momjian.us         1611                 :           2143 :             break;
10326 bruce@momjian.us         1612                 :UBC           0 :         default:
 5972 tgl@sss.pgh.pa.us        1613                 :              0 :             pname = sname = "???";
10326 bruce@momjian.us         1614                 :              0 :             break;
                               1615                 :                :     }
                               1616                 :                : 
 5972 tgl@sss.pgh.pa.us        1617         [ +  + ]:CBC       43858 :     ExplainOpenGroup("Plan",
                               1618                 :                :                      relationship ? NULL : "Plan",
                               1619                 :                :                      true, es);
                               1620                 :                : 
                               1621         [ +  + ]:          43858 :     if (es->format == EXPLAIN_FORMAT_TEXT)
                               1622                 :                :     {
                               1623         [ +  + ]:          43314 :         if (plan_name)
                               1624                 :                :         {
 2152                          1625                 :            951 :             ExplainIndentText(es);
 5972                          1626                 :            951 :             appendStringInfo(es->str, "%s\n", plan_name);
                               1627                 :            951 :             es->indent++;
                               1628                 :                :         }
                               1629         [ +  + ]:          43314 :         if (es->indent)
                               1630                 :                :         {
 2152                          1631                 :          31199 :             ExplainIndentText(es);
 5972                          1632                 :          31199 :             appendStringInfoString(es->str, "->  ");
                               1633                 :          31199 :             es->indent += 2;
                               1634                 :                :         }
 3688 rhaas@postgresql.org     1635         [ +  + ]:          43314 :         if (plan->parallel_aware)
                               1636                 :            693 :             appendStringInfoString(es->str, "Parallel ");
 1721 efujita@postgresql.o     1637         [ +  + ]:          43314 :         if (plan->async_capable)
                               1638                 :             51 :             appendStringInfoString(es->str, "Async ");
 5972 tgl@sss.pgh.pa.us        1639                 :          43314 :         appendStringInfoString(es->str, pname);
                               1640                 :          43314 :         es->indent++;
                               1641                 :                :     }
                               1642                 :                :     else
                               1643                 :                :     {
                               1644                 :            544 :         ExplainPropertyText("Node Type", sname, es);
                               1645         [ +  + ]:            544 :         if (strategy)
                               1646                 :             79 :             ExplainPropertyText("Strategy", strategy, es);
 3457                          1647         [ +  + ]:            544 :         if (partialmode)
                               1648                 :             79 :             ExplainPropertyText("Partial Mode", partialmode, es);
 5911                          1649         [ +  + ]:            544 :         if (operation)
                               1650                 :              3 :             ExplainPropertyText("Operation", operation, es);
 5972                          1651         [ +  + ]:            544 :         if (relationship)
                               1652                 :            391 :             ExplainPropertyText("Parent Relationship", relationship, es);
                               1653         [ -  + ]:            544 :         if (plan_name)
 5972 tgl@sss.pgh.pa.us        1654                 :UBC           0 :             ExplainPropertyText("Subplan Name", plan_name, es);
 4057 rhaas@postgresql.org     1655         [ -  + ]:CBC         544 :         if (custom_name)
 4057 rhaas@postgresql.org     1656                 :UBC           0 :             ExplainPropertyText("Custom Plan Provider", custom_name, es);
 3457 tgl@sss.pgh.pa.us        1657                 :CBC         544 :         ExplainPropertyBool("Parallel Aware", plan->parallel_aware, es);
 1721 efujita@postgresql.o     1658                 :            544 :         ExplainPropertyBool("Async Capable", plan->async_capable, es);
                               1659                 :                :     }
                               1660                 :                : 
10327 bruce@momjian.us         1661   [ +  +  +  +  :          43858 :     switch (nodeTag(plan))
                                        +  +  +  +  
                                                 + ]
                               1662                 :                :     {
10095 scrappy@hub.org          1663                 :          16650 :         case T_SeqScan:
                               1664                 :                :         case T_SampleScan:
                               1665                 :                :         case T_BitmapHeapScan:
                               1666                 :                :         case T_TidScan:
                               1667                 :                :         case T_TidRangeScan:
                               1668                 :                :         case T_SubqueryScan:
                               1669                 :                :         case T_FunctionScan:
                               1670                 :                :         case T_TableFuncScan:
                               1671                 :                :         case T_ValuesScan:
                               1672                 :                :         case T_CteScan:
                               1673                 :                :         case T_WorkTableScan:
 3882 rhaas@postgresql.org     1674                 :          16650 :             ExplainScanTarget((Scan *) plan, es);
                               1675                 :          16650 :             break;
 5413 tgl@sss.pgh.pa.us        1676                 :            431 :         case T_ForeignScan:
                               1677                 :                :         case T_CustomScan:
 3882 rhaas@postgresql.org     1678         [ +  + ]:            431 :             if (((Scan *) plan)->scanrelid > 0)
                               1679                 :            304 :                 ExplainScanTarget((Scan *) plan, es);
 5989 tgl@sss.pgh.pa.us        1680                 :            431 :             break;
 5180                          1681                 :           2081 :         case T_IndexScan:
                               1682                 :                :             {
                               1683                 :           2081 :                 IndexScan  *indexscan = (IndexScan *) plan;
                               1684                 :                : 
                               1685                 :           2081 :                 ExplainIndexScanDetails(indexscan->indexid,
                               1686                 :                :                                         indexscan->indexorderdir,
                               1687                 :                :                                         es);
                               1688                 :           2081 :                 ExplainScanTarget((Scan *) indexscan, es);
                               1689                 :                :             }
                               1690                 :           2081 :             break;
                               1691                 :           1412 :         case T_IndexOnlyScan:
                               1692                 :                :             {
                               1693                 :           1412 :                 IndexOnlyScan *indexonlyscan = (IndexOnlyScan *) plan;
                               1694                 :                : 
                               1695                 :           1412 :                 ExplainIndexScanDetails(indexonlyscan->indexid,
                               1696                 :                :                                         indexonlyscan->indexorderdir,
                               1697                 :                :                                         es);
                               1698                 :           1412 :                 ExplainScanTarget((Scan *) indexonlyscan, es);
                               1699                 :                :             }
                               1700                 :           1412 :             break;
 5989                          1701                 :           2164 :         case T_BitmapIndexScan:
                               1702                 :                :             {
 5972                          1703                 :           2164 :                 BitmapIndexScan *bitmapindexscan = (BitmapIndexScan *) plan;
                               1704                 :                :                 const char *indexname =
  942                          1705                 :           2164 :                     explain_get_index_name(bitmapindexscan->indexid);
                               1706                 :                : 
 5972                          1707         [ +  + ]:           2164 :                 if (es->format == EXPLAIN_FORMAT_TEXT)
 2003                          1708                 :           2134 :                     appendStringInfo(es->str, " on %s",
                               1709                 :                :                                      quote_identifier(indexname));
                               1710                 :                :                 else
 5972                          1711                 :             30 :                     ExplainPropertyText("Index Name", indexname, es);
                               1712                 :                :             }
                               1713                 :           2164 :             break;
 5404                          1714                 :            556 :         case T_ModifyTable:
                               1715                 :            556 :             ExplainModifyTarget((ModifyTable *) plan, es);
                               1716                 :            556 :             break;
 5972                          1717                 :           4403 :         case T_NestLoop:
                               1718                 :                :         case T_MergeJoin:
                               1719                 :                :         case T_HashJoin:
                               1720                 :                :             {
                               1721                 :                :                 const char *jointype;
                               1722                 :                : 
                               1723   [ +  +  +  +  :           4403 :                 switch (((Join *) plan)->jointype)
                                        +  +  +  +  
                                                 - ]
                               1724                 :                :                 {
                               1725                 :           2582 :                     case JOIN_INNER:
                               1726                 :           2582 :                         jointype = "Inner";
                               1727                 :           2582 :                         break;
                               1728                 :            902 :                     case JOIN_LEFT:
                               1729                 :            902 :                         jointype = "Left";
                               1730                 :            902 :                         break;
                               1731                 :            269 :                     case JOIN_FULL:
                               1732                 :            269 :                         jointype = "Full";
                               1733                 :            269 :                         break;
                               1734                 :            321 :                     case JOIN_RIGHT:
                               1735                 :            321 :                         jointype = "Right";
                               1736                 :            321 :                         break;
                               1737                 :            162 :                     case JOIN_SEMI:
                               1738                 :            162 :                         jointype = "Semi";
                               1739                 :            162 :                         break;
                               1740                 :             40 :                     case JOIN_ANTI:
                               1741                 :             40 :                         jointype = "Anti";
                               1742                 :             40 :                         break;
  529 rguo@postgresql.org      1743                 :             67 :                     case JOIN_RIGHT_SEMI:
                               1744                 :             67 :                         jointype = "Right Semi";
                               1745                 :             67 :                         break;
  986 tgl@sss.pgh.pa.us        1746                 :             60 :                     case JOIN_RIGHT_ANTI:
                               1747                 :             60 :                         jointype = "Right Anti";
                               1748                 :             60 :                         break;
 5972 tgl@sss.pgh.pa.us        1749                 :UBC           0 :                     default:
                               1750                 :              0 :                         jointype = "???";
                               1751                 :              0 :                         break;
                               1752                 :                :                 }
 5972 tgl@sss.pgh.pa.us        1753         [ +  + ]:CBC        4403 :                 if (es->format == EXPLAIN_FORMAT_TEXT)
                               1754                 :                :                 {
                               1755                 :                :                     /*
                               1756                 :                :                      * For historical reasons, the join type is interpolated
                               1757                 :                :                      * into the node type name...
                               1758                 :                :                      */
                               1759         [ +  + ]:           4334 :                     if (((Join *) plan)->jointype != JOIN_INNER)
                               1760                 :           1806 :                         appendStringInfo(es->str, " %s Join", jointype);
                               1761         [ +  + ]:           2528 :                     else if (!IsA(plan, NestLoop))
 4429 rhaas@postgresql.org     1762                 :           1324 :                         appendStringInfoString(es->str, " Join");
                               1763                 :                :                 }
                               1764                 :                :                 else
 5972 tgl@sss.pgh.pa.us        1765                 :             69 :                     ExplainPropertyText("Join Type", jointype, es);
                               1766                 :                :             }
                               1767                 :           4403 :             break;
                               1768                 :             57 :         case T_SetOp:
                               1769                 :                :             {
                               1770                 :                :                 const char *setopcmd;
                               1771                 :                : 
                               1772   [ +  -  +  +  :             57 :                 switch (((SetOp *) plan)->cmd)
                                                 - ]
                               1773                 :                :                 {
                               1774                 :             27 :                     case SETOPCMD_INTERSECT:
                               1775                 :             27 :                         setopcmd = "Intersect";
                               1776                 :             27 :                         break;
 5972 tgl@sss.pgh.pa.us        1777                 :UBC           0 :                     case SETOPCMD_INTERSECT_ALL:
                               1778                 :              0 :                         setopcmd = "Intersect All";
                               1779                 :              0 :                         break;
 5972 tgl@sss.pgh.pa.us        1780                 :CBC          27 :                     case SETOPCMD_EXCEPT:
                               1781                 :             27 :                         setopcmd = "Except";
                               1782                 :             27 :                         break;
                               1783                 :              3 :                     case SETOPCMD_EXCEPT_ALL:
                               1784                 :              3 :                         setopcmd = "Except All";
                               1785                 :              3 :                         break;
 5972 tgl@sss.pgh.pa.us        1786                 :UBC           0 :                     default:
                               1787                 :              0 :                         setopcmd = "???";
                               1788                 :              0 :                         break;
                               1789                 :                :                 }
 5972 tgl@sss.pgh.pa.us        1790         [ +  - ]:CBC          57 :                 if (es->format == EXPLAIN_FORMAT_TEXT)
                               1791                 :             57 :                     appendStringInfo(es->str, " %s", setopcmd);
                               1792                 :                :                 else
 5972 tgl@sss.pgh.pa.us        1793                 :UBC           0 :                     ExplainPropertyText("Command", setopcmd, es);
                               1794                 :                :             }
 6282 tgl@sss.pgh.pa.us        1795                 :CBC          57 :             break;
10326 bruce@momjian.us         1796                 :          16104 :         default:
                               1797                 :          16104 :             break;
                               1798                 :                :     }
                               1799                 :                : 
 5987 tgl@sss.pgh.pa.us        1800         [ +  + ]:          43858 :     if (es->costs)
                               1801                 :                :     {
 5972                          1802         [ +  + ]:          12615 :         if (es->format == EXPLAIN_FORMAT_TEXT)
                               1803                 :                :         {
                               1804                 :          12142 :             appendStringInfo(es->str, "  (cost=%.2f..%.2f rows=%.0f width=%d)",
                               1805                 :                :                              plan->startup_cost, plan->total_cost,
                               1806                 :                :                              plan->plan_rows, plan->plan_width);
                               1807                 :                :         }
                               1808                 :                :         else
                               1809                 :                :         {
 2832 andres@anarazel.de       1810                 :            473 :             ExplainPropertyFloat("Startup Cost", NULL, plan->startup_cost,
                               1811                 :                :                                  2, es);
                               1812                 :            473 :             ExplainPropertyFloat("Total Cost", NULL, plan->total_cost,
                               1813                 :                :                                  2, es);
                               1814                 :            473 :             ExplainPropertyFloat("Plan Rows", NULL, plan->plan_rows,
                               1815                 :                :                                  0, es);
                               1816                 :            473 :             ExplainPropertyInteger("Plan Width", NULL, plan->plan_width,
                               1817                 :                :                                    es);
                               1818                 :                :         }
                               1819                 :                :     }
                               1820                 :                : 
                               1821                 :                :     /*
                               1822                 :                :      * We have to forcibly clean up the instrumentation state because we
                               1823                 :                :      * haven't done ExecutorEnd yet.  This is pretty grotty ...
                               1824                 :                :      *
                               1825                 :                :      * Note: contrib/auto_explain could cause instrumentation to be set up
                               1826                 :                :      * even though we didn't ask for it here.  Be careful not to print any
                               1827                 :                :      * instrumentation results the user didn't ask for.  But we do the
                               1828                 :                :      * InstrEndLoop call anyway, if possible, to reduce the number of cases
                               1829                 :                :      * auto_explain has to contend with.
                               1830                 :                :      */
 7500 neilc@samurai.com        1831         [ +  + ]:          43858 :     if (planstate->instrument)
                               1832                 :           4025 :         InstrEndLoop(planstate->instrument);
                               1833                 :                : 
 4228 tgl@sss.pgh.pa.us        1834         [ +  + ]:          43858 :     if (es->analyze &&
                               1835   [ +  -  +  + ]:           4019 :         planstate->instrument && planstate->instrument->nloops > 0)
 7500 neilc@samurai.com        1836                 :           3648 :     {
                               1837                 :           3648 :         double      nloops = planstate->instrument->nloops;
 2832 andres@anarazel.de       1838                 :           3648 :         double      startup_ms = 1000.0 * planstate->instrument->startup / nloops;
                               1839                 :           3648 :         double      total_ms = 1000.0 * planstate->instrument->total / nloops;
 5972 tgl@sss.pgh.pa.us        1840                 :           3648 :         double      rows = planstate->instrument->ntuples / nloops;
                               1841                 :                : 
                               1842         [ +  + ]:           3648 :         if (es->format == EXPLAIN_FORMAT_TEXT)
                               1843                 :                :         {
  249 drowley@postgresql.o     1844                 :           3136 :             appendStringInfoString(es->str, " (actual ");
                               1845                 :                : 
 4228 tgl@sss.pgh.pa.us        1846         [ +  + ]:           3136 :             if (es->timing)
  298 rhaas@postgresql.org     1847                 :           1675 :                 appendStringInfo(es->str, "time=%.3f..%.3f ", startup_ms, total_ms);
                               1848                 :                : 
  292                          1849                 :           3136 :             appendStringInfo(es->str, "rows=%.2f loops=%.0f)", rows, nloops);
                               1850                 :                :         }
                               1851                 :                :         else
                               1852                 :                :         {
 4228 tgl@sss.pgh.pa.us        1853         [ +  + ]:            512 :             if (es->timing)
                               1854                 :                :             {
 1705                          1855                 :            464 :                 ExplainPropertyFloat("Actual Startup Time", "ms", startup_ms,
                               1856                 :                :                                      3, es);
                               1857                 :            464 :                 ExplainPropertyFloat("Actual Total Time", "ms", total_ms,
                               1858                 :                :                                      3, es);
                               1859                 :                :             }
  292 rhaas@postgresql.org     1860                 :            512 :             ExplainPropertyFloat("Actual Rows", NULL, rows, 2, es);
                               1861                 :            512 :             ExplainPropertyFloat("Actual Loops", NULL, nloops, 0, es);
                               1862                 :                :         }
                               1863                 :                :     }
 5987 tgl@sss.pgh.pa.us        1864         [ +  + ]:          40210 :     else if (es->analyze)
                               1865                 :                :     {
 5972                          1866         [ +  - ]:            371 :         if (es->format == EXPLAIN_FORMAT_TEXT)
 4429 rhaas@postgresql.org     1867                 :            371 :             appendStringInfoString(es->str, " (never executed)");
                               1868                 :                :         else
                               1869                 :                :         {
 4228 tgl@sss.pgh.pa.us        1870         [ #  # ]:UBC           0 :             if (es->timing)
                               1871                 :                :             {
 2832 andres@anarazel.de       1872                 :              0 :                 ExplainPropertyFloat("Actual Startup Time", "ms", 0.0, 3, es);
                               1873                 :              0 :                 ExplainPropertyFloat("Actual Total Time", "ms", 0.0, 3, es);
                               1874                 :                :             }
                               1875                 :              0 :             ExplainPropertyFloat("Actual Rows", NULL, 0.0, 0, es);
                               1876                 :              0 :             ExplainPropertyFloat("Actual Loops", NULL, 0.0, 0, es);
                               1877                 :                :         }
                               1878                 :                :     }
                               1879                 :                : 
                               1880                 :                :     /* in text format, first line ends here */
 5972 tgl@sss.pgh.pa.us        1881         [ +  + ]:CBC       43858 :     if (es->format == EXPLAIN_FORMAT_TEXT)
                               1882                 :          43314 :         appendStringInfoChar(es->str, '\n');
                               1883                 :                : 
                               1884                 :                : 
  431 drowley@postgresql.o     1885                 :          43858 :     isdisabled = plan_is_disabled(plan);
                               1886   [ +  +  +  + ]:          43858 :     if (es->format != EXPLAIN_FORMAT_TEXT || isdisabled)
                               1887                 :            594 :         ExplainPropertyBool("Disabled", isdisabled, es);
                               1888                 :                : 
                               1889                 :                :     /* prepare per-worker general execution details */
 2152 tgl@sss.pgh.pa.us        1890   [ +  +  +  + ]:          43858 :     if (es->workers_state && es->verbose)
                               1891                 :                :     {
                               1892                 :              6 :         WorkerInstrumentation *w = planstate->worker_instrument;
                               1893                 :                : 
                               1894         [ +  + ]:             30 :         for (int n = 0; n < w->num_workers; n++)
                               1895                 :                :         {
                               1896                 :             24 :             Instrumentation *instrument = &w->instrument[n];
                               1897                 :             24 :             double      nloops = instrument->nloops;
                               1898                 :                :             double      startup_ms;
                               1899                 :                :             double      total_ms;
                               1900                 :                :             double      rows;
                               1901                 :                : 
                               1902         [ -  + ]:             24 :             if (nloops <= 0)
 2152 tgl@sss.pgh.pa.us        1903                 :UBC           0 :                 continue;
 2152 tgl@sss.pgh.pa.us        1904                 :CBC          24 :             startup_ms = 1000.0 * instrument->startup / nloops;
                               1905                 :             24 :             total_ms = 1000.0 * instrument->total / nloops;
                               1906                 :             24 :             rows = instrument->ntuples / nloops;
                               1907                 :                : 
                               1908                 :             24 :             ExplainOpenWorker(n, es);
                               1909                 :                : 
                               1910         [ -  + ]:             24 :             if (es->format == EXPLAIN_FORMAT_TEXT)
                               1911                 :                :             {
 2152 tgl@sss.pgh.pa.us        1912                 :UBC           0 :                 ExplainIndentText(es);
  249 drowley@postgresql.o     1913                 :              0 :                 appendStringInfoString(es->str, "actual ");
 2152 tgl@sss.pgh.pa.us        1914         [ #  # ]:              0 :                 if (es->timing)
  291 rhaas@postgresql.org     1915                 :              0 :                     appendStringInfo(es->str, "time=%.3f..%.3f ", startup_ms, total_ms);
                               1916                 :                : 
  292                          1917                 :              0 :                 appendStringInfo(es->str, "rows=%.2f loops=%.0f\n", rows, nloops);
                               1918                 :                :             }
                               1919                 :                :             else
                               1920                 :                :             {
 2152 tgl@sss.pgh.pa.us        1921         [ +  - ]:CBC          24 :                 if (es->timing)
                               1922                 :                :                 {
                               1923                 :             24 :                     ExplainPropertyFloat("Actual Startup Time", "ms",
                               1924                 :                :                                          startup_ms, 3, es);
                               1925                 :             24 :                     ExplainPropertyFloat("Actual Total Time", "ms",
                               1926                 :                :                                          total_ms, 3, es);
                               1927                 :                :                 }
                               1928                 :                : 
  292 rhaas@postgresql.org     1929                 :             24 :                 ExplainPropertyFloat("Actual Rows", NULL, rows, 2, es);
                               1930                 :             24 :                 ExplainPropertyFloat("Actual Loops", NULL, nloops, 0, es);
                               1931                 :                :             }
                               1932                 :                : 
 2152 tgl@sss.pgh.pa.us        1933                 :             24 :             ExplainCloseWorker(n, es);
                               1934                 :                :         }
                               1935                 :                :     }
                               1936                 :                : 
                               1937                 :                :     /* target list */
 5987                          1938         [ +  + ]:          43858 :     if (es->verbose)
 5635                          1939                 :           5898 :         show_plan_tlist(planstate, ancestors, es);
                               1940                 :                : 
                               1941                 :                :     /* unique join */
 3175                          1942         [ +  + ]:          43858 :     switch (nodeTag(plan))
                               1943                 :                :     {
                               1944                 :           4403 :         case T_NestLoop:
                               1945                 :                :         case T_MergeJoin:
                               1946                 :                :         case T_HashJoin:
                               1947                 :                :             /* try not to be too chatty about this in text mode */
                               1948         [ +  + ]:           4403 :             if (es->format != EXPLAIN_FORMAT_TEXT ||
                               1949   [ +  +  +  + ]:           4334 :                 (es->verbose && ((Join *) plan)->inner_unique))
                               1950                 :            127 :                 ExplainPropertyBool("Inner Unique",
                               1951                 :            127 :                                     ((Join *) plan)->inner_unique,
                               1952                 :                :                                     es);
                               1953                 :           4403 :             break;
                               1954                 :          39455 :         default:
                               1955                 :          39455 :             break;
                               1956                 :                :     }
                               1957                 :                : 
                               1958                 :                :     /* quals, sort keys, etc */
 8680                          1959   [ +  +  +  +  :          43858 :     switch (nodeTag(plan))
                                     +  +  +  +  +  
                                     +  +  +  +  -  
                                     +  +  +  +  +  
                                     +  +  +  +  +  
                                     +  +  +  +  +  
                                                 + ]
                               1960                 :                :     {
                               1961                 :           2081 :         case T_IndexScan:
 7540                          1962                 :           2081 :             show_scan_qual(((IndexScan *) plan)->indexqualorig,
                               1963                 :                :                            "Index Cond", planstate, ancestors, es);
 5199                          1964         [ +  + ]:           2081 :             if (((IndexScan *) plan)->indexqualorig)
                               1965                 :           1559 :                 show_instrumentation_count("Rows Removed by Index Recheck", 2,
                               1966                 :                :                                            planstate, es);
 5493                          1967                 :           2081 :             show_scan_qual(((IndexScan *) plan)->indexorderbyorig,
                               1968                 :                :                            "Order By", planstate, ancestors, es);
 5635                          1969                 :           2081 :             show_scan_qual(plan->qual, "Filter", planstate, ancestors, es);
 5199                          1970         [ +  + ]:           2081 :             if (plan->qual)
                               1971                 :            320 :                 show_instrumentation_count("Rows Removed by Filter", 1,
                               1972                 :                :                                            planstate, es);
  280 pg@bowt.ie               1973                 :           2081 :             show_indexsearches_info(planstate, es);
 8680 tgl@sss.pgh.pa.us        1974                 :           2081 :             break;
 5180                          1975                 :           1412 :         case T_IndexOnlyScan:
                               1976                 :           1412 :             show_scan_qual(((IndexOnlyScan *) plan)->indexqual,
                               1977                 :                :                            "Index Cond", planstate, ancestors, es);
 1443                          1978         [ +  + ]:           1412 :             if (((IndexOnlyScan *) plan)->recheckqual)
 5180                          1979                 :            913 :                 show_instrumentation_count("Rows Removed by Index Recheck", 2,
                               1980                 :                :                                            planstate, es);
                               1981                 :           1412 :             show_scan_qual(((IndexOnlyScan *) plan)->indexorderby,
                               1982                 :                :                            "Order By", planstate, ancestors, es);
                               1983                 :           1412 :             show_scan_qual(plan->qual, "Filter", planstate, ancestors, es);
                               1984         [ +  + ]:           1412 :             if (plan->qual)
                               1985                 :             66 :                 show_instrumentation_count("Rows Removed by Filter", 1,
                               1986                 :                :                                            planstate, es);
 5074 rhaas@postgresql.org     1987         [ +  + ]:           1412 :             if (es->analyze)
 2807 alvherre@alvh.no-ip.     1988                 :             68 :                 ExplainPropertyFloat("Heap Fetches", NULL,
                               1989                 :             68 :                                      planstate->instrument->ntuples2, 0, es);
  280 pg@bowt.ie               1990                 :           1412 :             show_indexsearches_info(planstate, es);
 5180 tgl@sss.pgh.pa.us        1991                 :           1412 :             break;
 7546                          1992                 :           2164 :         case T_BitmapIndexScan:
 7540                          1993                 :           2164 :             show_scan_qual(((BitmapIndexScan *) plan)->indexqualorig,
                               1994                 :                :                            "Index Cond", planstate, ancestors, es);
  280 pg@bowt.ie               1995                 :           2164 :             show_indexsearches_info(planstate, es);
 7546 tgl@sss.pgh.pa.us        1996                 :           2164 :             break;
                               1997                 :           2067 :         case T_BitmapHeapScan:
 7540                          1998                 :           2067 :             show_scan_qual(((BitmapHeapScan *) plan)->bitmapqualorig,
                               1999                 :                :                            "Recheck Cond", planstate, ancestors, es);
 5199                          2000         [ +  + ]:           2067 :             if (((BitmapHeapScan *) plan)->bitmapqualorig)
                               2001                 :           2028 :                 show_instrumentation_count("Rows Removed by Index Recheck", 2,
                               2002                 :                :                                            planstate, es);
 4355 rhaas@postgresql.org     2003                 :           2067 :             show_scan_qual(plan->qual, "Filter", planstate, ancestors, es);
                               2004         [ +  + ]:           2067 :             if (plan->qual)
                               2005                 :            174 :                 show_instrumentation_count("Rows Removed by Filter", 1,
                               2006                 :                :                                            planstate, es);
  525 drowley@postgresql.o     2007                 :           2067 :             show_tidbitmap_info((BitmapHeapScanState *) planstate, es);
 4355 rhaas@postgresql.org     2008                 :           2067 :             break;
 3797 tgl@sss.pgh.pa.us        2009                 :             60 :         case T_SampleScan:
                               2010                 :             60 :             show_tablesample(((SampleScan *) plan)->tablesample,
                               2011                 :                :                              planstate, ancestors, es);
                               2012                 :                :             /* fall through to print additional fields the same as SeqScan */
                               2013                 :                :             /* FALLTHROUGH */
 8680                          2014                 :          14178 :         case T_SeqScan:
                               2015                 :                :         case T_ValuesScan:
                               2016                 :                :         case T_CteScan:
                               2017                 :                :         case T_NamedTuplestoreScan:
                               2018                 :                :         case T_WorkTableScan:
                               2019                 :                :         case T_SubqueryScan:
 5635                          2020                 :          14178 :             show_scan_qual(plan->qual, "Filter", planstate, ancestors, es);
 5199                          2021         [ +  + ]:          14178 :             if (plan->qual)
                               2022                 :           7088 :                 show_instrumentation_count("Rows Removed by Filter", 1,
                               2023                 :                :                                            planstate, es);
  449 ishii@postgresql.org     2024         [ +  + ]:          14178 :             if (IsA(plan, CteScan))
                               2025                 :            125 :                 show_ctescan_info(castNode(CteScanState, planstate), es);
 8680 tgl@sss.pgh.pa.us        2026                 :          14178 :             break;
 3730 rhaas@postgresql.org     2027                 :            328 :         case T_Gather:
                               2028                 :                :             {
 3541 tgl@sss.pgh.pa.us        2029                 :            328 :                 Gather     *gather = (Gather *) plan;
                               2030                 :                : 
 3730 rhaas@postgresql.org     2031                 :            328 :                 show_scan_qual(plan->qual, "Filter", planstate, ancestors, es);
                               2032         [ -  + ]:            328 :                 if (plan->qual)
 3730 rhaas@postgresql.org     2033                 :UBC           0 :                     show_instrumentation_count("Rows Removed by Filter", 1,
                               2034                 :                :                                                planstate, es);
 2832 andres@anarazel.de       2035                 :CBC         328 :                 ExplainPropertyInteger("Workers Planned", NULL,
 3730 rhaas@postgresql.org     2036                 :            328 :                                        gather->num_workers, es);
                               2037                 :                : 
 3532                          2038         [ +  + ]:            328 :                 if (es->analyze)
                               2039                 :                :                 {
                               2040                 :                :                     int         nworkers;
                               2041                 :                : 
                               2042                 :             84 :                     nworkers = ((GatherState *) planstate)->nworkers_launched;
 2832 andres@anarazel.de       2043                 :             84 :                     ExplainPropertyInteger("Workers Launched", NULL,
                               2044                 :                :                                            nworkers, es);
                               2045                 :                :                 }
                               2046                 :                : 
 3457 tgl@sss.pgh.pa.us        2047   [ +  +  +  + ]:            328 :                 if (gather->single_copy || es->format != EXPLAIN_FORMAT_TEXT)
                               2048                 :             52 :                     ExplainPropertyBool("Single Copy", gather->single_copy, es);
                               2049                 :                :             }
 3730 rhaas@postgresql.org     2050                 :            328 :             break;
 3204                          2051                 :            114 :         case T_GatherMerge:
                               2052                 :                :             {
                               2053                 :            114 :                 GatherMerge *gm = (GatherMerge *) plan;
                               2054                 :                : 
                               2055                 :            114 :                 show_scan_qual(plan->qual, "Filter", planstate, ancestors, es);
                               2056         [ -  + ]:            114 :                 if (plan->qual)
 3204 rhaas@postgresql.org     2057                 :UBC           0 :                     show_instrumentation_count("Rows Removed by Filter", 1,
                               2058                 :                :                                                planstate, es);
 2832 andres@anarazel.de       2059                 :CBC         114 :                 ExplainPropertyInteger("Workers Planned", NULL,
 3204 rhaas@postgresql.org     2060                 :            114 :                                        gm->num_workers, es);
                               2061                 :                : 
                               2062         [ +  + ]:            114 :                 if (es->analyze)
                               2063                 :                :                 {
                               2064                 :                :                     int         nworkers;
                               2065                 :                : 
                               2066                 :              6 :                     nworkers = ((GatherMergeState *) planstate)->nworkers_launched;
 2832 andres@anarazel.de       2067                 :              6 :                     ExplainPropertyInteger("Workers Launched", NULL,
                               2068                 :                :                                            nworkers, es);
                               2069                 :                :                 }
                               2070                 :                :             }
 3204 rhaas@postgresql.org     2071                 :            114 :             break;
 5593 tgl@sss.pgh.pa.us        2072                 :            288 :         case T_FunctionScan:
                               2073         [ +  + ]:            288 :             if (es->verbose)
                               2074                 :                :             {
 4408                          2075                 :            100 :                 List       *fexprs = NIL;
                               2076                 :                :                 ListCell   *lc;
                               2077                 :                : 
                               2078   [ +  -  +  +  :            200 :                 foreach(lc, ((FunctionScan *) plan)->functions)
                                              +  + ]
                               2079                 :                :                 {
                               2080                 :            100 :                     RangeTblFunction *rtfunc = (RangeTblFunction *) lfirst(lc);
                               2081                 :                : 
                               2082                 :            100 :                     fexprs = lappend(fexprs, rtfunc->funcexpr);
                               2083                 :                :                 }
                               2084                 :                :                 /* We rely on show_expression to insert commas as needed */
                               2085                 :            100 :                 show_expression((Node *) fexprs,
                               2086                 :                :                                 "Function Call", planstate, ancestors,
 5593                          2087                 :            100 :                                 es->verbose, es);
                               2088                 :                :             }
                               2089                 :            288 :             show_scan_qual(plan->qual, "Filter", planstate, ancestors, es);
 5199                          2090         [ +  + ]:            288 :             if (plan->qual)
                               2091                 :             11 :                 show_instrumentation_count("Rows Removed by Filter", 1,
                               2092                 :                :                                            planstate, es);
 5593                          2093                 :            288 :             break;
 3205 alvherre@alvh.no-ip.     2094                 :             39 :         case T_TableFuncScan:
                               2095         [ +  + ]:             39 :             if (es->verbose)
                               2096                 :                :             {
                               2097                 :             36 :                 TableFunc  *tablefunc = ((TableFuncScan *) plan)->tablefunc;
                               2098                 :                : 
                               2099                 :             36 :                 show_expression((Node *) tablefunc,
                               2100                 :                :                                 "Table Function Call", planstate, ancestors,
                               2101                 :             36 :                                 es->verbose, es);
                               2102                 :                :             }
                               2103                 :             39 :             show_scan_qual(plan->qual, "Filter", planstate, ancestors, es);
                               2104         [ +  + ]:             39 :             if (plan->qual)
                               2105                 :              9 :                 show_instrumentation_count("Rows Removed by Filter", 1,
                               2106                 :                :                                            planstate, es);
  449 ishii@postgresql.org     2107                 :             39 :             show_table_func_scan_info(castNode(TableFuncScanState,
                               2108                 :                :                                                planstate), es);
 3205 alvherre@alvh.no-ip.     2109                 :             39 :             break;
 7325 tgl@sss.pgh.pa.us        2110                 :             33 :         case T_TidScan:
                               2111                 :                :             {
                               2112                 :                :                 /*
                               2113                 :                :                  * The tidquals list has OR semantics, so be sure to show it
                               2114                 :                :                  * as an OR condition.
                               2115                 :                :                  */
 7013 bruce@momjian.us         2116                 :             33 :                 List       *tidquals = ((TidScan *) plan)->tidquals;
                               2117                 :                : 
 7325 tgl@sss.pgh.pa.us        2118         [ +  + ]:             33 :                 if (list_length(tidquals) > 1)
                               2119                 :              6 :                     tidquals = list_make1(make_orclause(tidquals));
 5635                          2120                 :             33 :                 show_scan_qual(tidquals, "TID Cond", planstate, ancestors, es);
                               2121                 :             33 :                 show_scan_qual(plan->qual, "Filter", planstate, ancestors, es);
 5199                          2122         [ +  + ]:             33 :                 if (plan->qual)
                               2123                 :              9 :                     show_instrumentation_count("Rows Removed by Filter", 1,
                               2124                 :                :                                                planstate, es);
                               2125                 :                :             }
 7325                          2126                 :             33 :             break;
 1753 drowley@postgresql.o     2127                 :             57 :         case T_TidRangeScan:
                               2128                 :                :             {
                               2129                 :                :                 /*
                               2130                 :                :                  * The tidrangequals list has AND semantics, so be sure to
                               2131                 :                :                  * show it as an AND condition.
                               2132                 :                :                  */
                               2133                 :             57 :                 List       *tidquals = ((TidRangeScan *) plan)->tidrangequals;
                               2134                 :                : 
                               2135         [ +  + ]:             57 :                 if (list_length(tidquals) > 1)
                               2136                 :              9 :                     tidquals = list_make1(make_andclause(tidquals));
                               2137                 :             57 :                 show_scan_qual(tidquals, "TID Cond", planstate, ancestors, es);
                               2138                 :             57 :                 show_scan_qual(plan->qual, "Filter", planstate, ancestors, es);
                               2139         [ -  + ]:             57 :                 if (plan->qual)
 1753 drowley@postgresql.o     2140                 :UBC           0 :                     show_instrumentation_count("Rows Removed by Filter", 1,
                               2141                 :                :                                                planstate, es);
                               2142                 :                :             }
 1753 drowley@postgresql.o     2143                 :CBC          57 :             break;
 5413 tgl@sss.pgh.pa.us        2144                 :            431 :         case T_ForeignScan:
                               2145                 :            431 :             show_scan_qual(plan->qual, "Filter", planstate, ancestors, es);
 5199                          2146         [ +  + ]:            431 :             if (plan->qual)
                               2147                 :             52 :                 show_instrumentation_count("Rows Removed by Filter", 1,
                               2148                 :                :                                            planstate, es);
 5413                          2149                 :            431 :             show_foreignscan_info((ForeignScanState *) planstate, es);
                               2150                 :            431 :             break;
 4057 rhaas@postgresql.org     2151                 :UBC           0 :         case T_CustomScan:
                               2152                 :                :             {
                               2153                 :              0 :                 CustomScanState *css = (CustomScanState *) planstate;
                               2154                 :                : 
                               2155                 :              0 :                 show_scan_qual(plan->qual, "Filter", planstate, ancestors, es);
                               2156         [ #  # ]:              0 :                 if (plan->qual)
                               2157                 :              0 :                     show_instrumentation_count("Rows Removed by Filter", 1,
                               2158                 :                :                                                planstate, es);
                               2159         [ #  # ]:              0 :                 if (css->methods->ExplainCustomScan)
                               2160                 :              0 :                     css->methods->ExplainCustomScan(css, ancestors, es);
                               2161                 :                :             }
                               2162                 :              0 :             break;
 8680 tgl@sss.pgh.pa.us        2163                 :CBC        1866 :         case T_NestLoop:
 8670                          2164                 :           1866 :             show_upper_qual(((NestLoop *) plan)->join.joinqual,
                               2165                 :                :                             "Join Filter", planstate, ancestors, es);
 5199                          2166         [ +  + ]:           1866 :             if (((NestLoop *) plan)->join.joinqual)
                               2167                 :            577 :                 show_instrumentation_count("Rows Removed by Join Filter", 1,
                               2168                 :                :                                            planstate, es);
 5635                          2169                 :           1866 :             show_upper_qual(plan->qual, "Filter", planstate, ancestors, es);
 5199                          2170         [ +  + ]:           1866 :             if (plan->qual)
                               2171                 :             45 :                 show_instrumentation_count("Rows Removed by Filter", 2,
                               2172                 :                :                                            planstate, es);
 8680                          2173                 :           1866 :             break;
                               2174                 :            394 :         case T_MergeJoin:
 8670                          2175                 :            394 :             show_upper_qual(((MergeJoin *) plan)->mergeclauses,
                               2176                 :                :                             "Merge Cond", planstate, ancestors, es);
                               2177                 :            394 :             show_upper_qual(((MergeJoin *) plan)->join.joinqual,
                               2178                 :                :                             "Join Filter", planstate, ancestors, es);
 5199                          2179         [ +  + ]:            394 :             if (((MergeJoin *) plan)->join.joinqual)
                               2180                 :             13 :                 show_instrumentation_count("Rows Removed by Join Filter", 1,
                               2181                 :                :                                            planstate, es);
 5635                          2182                 :            394 :             show_upper_qual(plan->qual, "Filter", planstate, ancestors, es);
 5199                          2183         [ +  + ]:            394 :             if (plan->qual)
                               2184                 :             12 :                 show_instrumentation_count("Rows Removed by Filter", 2,
                               2185                 :                :                                            planstate, es);
 8680                          2186                 :            394 :             break;
                               2187                 :           2143 :         case T_HashJoin:
 8670                          2188                 :           2143 :             show_upper_qual(((HashJoin *) plan)->hashclauses,
                               2189                 :                :                             "Hash Cond", planstate, ancestors, es);
                               2190                 :           2143 :             show_upper_qual(((HashJoin *) plan)->join.joinqual,
                               2191                 :                :                             "Join Filter", planstate, ancestors, es);
 5199                          2192         [ +  + ]:           2143 :             if (((HashJoin *) plan)->join.joinqual)
                               2193                 :             12 :                 show_instrumentation_count("Rows Removed by Join Filter", 1,
                               2194                 :                :                                            planstate, es);
 5635                          2195                 :           2143 :             show_upper_qual(plan->qual, "Filter", planstate, ancestors, es);
 5199                          2196         [ +  + ]:           2143 :             if (plan->qual)
                               2197                 :            134 :                 show_instrumentation_count("Rows Removed by Filter", 2,
                               2198                 :                :                                            planstate, es);
 8680                          2199                 :           2143 :             break;
                               2200                 :           5230 :         case T_Agg:
 3246 andres@anarazel.de       2201                 :           5230 :             show_agg_keys(castNode(AggState, planstate), ancestors, es);
 4387 tgl@sss.pgh.pa.us        2202                 :           5230 :             show_upper_qual(plan->qual, "Filter", planstate, ancestors, es);
 2099 jdavis@postgresql.or     2203                 :           5230 :             show_hashagg_info((AggState *) planstate, es);
 4387 tgl@sss.pgh.pa.us        2204         [ +  + ]:           5230 :             if (plan->qual)
                               2205                 :            184 :                 show_instrumentation_count("Rows Removed by Filter", 1,
                               2206                 :                :                                            planstate, es);
                               2207                 :           5230 :             break;
 1348 drowley@postgresql.o     2208                 :            235 :         case T_WindowAgg:
  280 tgl@sss.pgh.pa.us        2209                 :            235 :             show_window_def(castNode(WindowAggState, planstate), ancestors, es);
                               2210                 :            235 :             show_upper_qual(((WindowAgg *) plan)->runConditionOrig,
                               2211                 :                :                             "Run Condition", planstate, ancestors, es);
 1348 drowley@postgresql.o     2212                 :            235 :             show_upper_qual(plan->qual, "Filter", planstate, ancestors, es);
                               2213         [ +  + ]:            235 :             if (plan->qual)
                               2214                 :              3 :                 show_instrumentation_count("Rows Removed by Filter", 1,
                               2215                 :                :                                            planstate, es);
  455 ishii@postgresql.org     2216                 :            235 :             show_windowagg_info(castNode(WindowAggState, planstate), es);
 1348 drowley@postgresql.o     2217                 :            235 :             break;
 8680 tgl@sss.pgh.pa.us        2218                 :             48 :         case T_Group:
 3246 andres@anarazel.de       2219                 :             48 :             show_group_keys(castNode(GroupState, planstate), ancestors, es);
 5635 tgl@sss.pgh.pa.us        2220                 :             48 :             show_upper_qual(plan->qual, "Filter", planstate, ancestors, es);
 5199                          2221         [ -  + ]:             48 :             if (plan->qual)
 5199 tgl@sss.pgh.pa.us        2222                 :UBC           0 :                 show_instrumentation_count("Rows Removed by Filter", 1,
                               2223                 :                :                                            planstate, es);
 8680 tgl@sss.pgh.pa.us        2224                 :CBC          48 :             break;
 8613                          2225                 :           2379 :         case T_Sort:
 3246 andres@anarazel.de       2226                 :           2379 :             show_sort_keys(castNode(SortState, planstate), ancestors, es);
                               2227                 :           2379 :             show_sort_info(castNode(SortState, planstate), es);
 8613 tgl@sss.pgh.pa.us        2228                 :           2379 :             break;
 2080 tomas.vondra@postgre     2229                 :            196 :         case T_IncrementalSort:
                               2230                 :            196 :             show_incremental_sort_keys(castNode(IncrementalSortState, planstate),
                               2231                 :                :                                        ancestors, es);
                               2232                 :            196 :             show_incremental_sort_info(castNode(IncrementalSortState, planstate),
                               2233                 :                :                                        es);
                               2234                 :            196 :             break;
 5542 tgl@sss.pgh.pa.us        2235                 :            174 :         case T_MergeAppend:
 3246 andres@anarazel.de       2236                 :            174 :             show_merge_append_keys(castNode(MergeAppendState, planstate),
                               2237                 :                :                                    ancestors, es);
 5542 tgl@sss.pgh.pa.us        2238                 :            174 :             break;
 8680                          2239                 :           1540 :         case T_Result:
   84 rhaas@postgresql.org     2240                 :GNC        1540 :             show_result_replacement_info(castNode(Result, plan), es);
 8680 tgl@sss.pgh.pa.us        2241                 :CBC        1540 :             show_upper_qual((List *) ((Result *) plan)->resconstantqual,
                               2242                 :                :                             "One-Time Filter", planstate, ancestors, es);
 5635                          2243                 :           1540 :             show_upper_qual(plan->qual, "Filter", planstate, ancestors, es);
 5199                          2244         [ -  + ]:           1540 :             if (plan->qual)
 5199 tgl@sss.pgh.pa.us        2245                 :UBC           0 :                 show_instrumentation_count("Rows Removed by Filter", 1,
                               2246                 :                :                                            planstate, es);
 8680 tgl@sss.pgh.pa.us        2247                 :CBC        1540 :             break;
 4664                          2248                 :            556 :         case T_ModifyTable:
 3246 andres@anarazel.de       2249                 :            556 :             show_modifytable_info(castNode(ModifyTableState, planstate), ancestors,
                               2250                 :                :                                   es);
 4664 tgl@sss.pgh.pa.us        2251                 :            556 :             break;
 5797 rhaas@postgresql.org     2252                 :           2143 :         case T_Hash:
 3246 andres@anarazel.de       2253                 :           2143 :             show_hash_info(castNode(HashState, planstate), es);
 5797 rhaas@postgresql.org     2254                 :           2143 :             break;
  529 drowley@postgresql.o     2255                 :            553 :         case T_Material:
                               2256                 :            553 :             show_material_info(castNode(MaterialState, planstate), es);
                               2257                 :            553 :             break;
 1616                          2258                 :            177 :         case T_Memoize:
                               2259                 :            177 :             show_memoize_info(castNode(MemoizeState, planstate), ancestors,
                               2260                 :                :                               es);
 1719                          2261                 :            177 :             break;
  449 ishii@postgresql.org     2262                 :             27 :         case T_RecursiveUnion:
                               2263                 :             27 :             show_recursive_union_info(castNode(RecursiveUnionState,
                               2264                 :                :                                                planstate), es);
                               2265                 :             27 :             break;
 8680 tgl@sss.pgh.pa.us        2266                 :           3005 :         default:
                               2267                 :           3005 :             break;
                               2268                 :                :     }
                               2269                 :                : 
                               2270                 :                :     /*
                               2271                 :                :      * Prepare per-worker JIT instrumentation.  As with the overall JIT
                               2272                 :                :      * summary, this is printed only if printing costs is enabled.
                               2273                 :                :      */
 2152                          2274   [ +  +  +  +  :          43858 :     if (es->workers_state && es->costs && es->verbose)
                                              +  + ]
                               2275                 :                :     {
                               2276                 :              6 :         SharedJitInstrumentation *w = planstate->worker_jit_instrument;
                               2277                 :                : 
                               2278         [ -  + ]:              6 :         if (w)
                               2279                 :                :         {
 2152 tgl@sss.pgh.pa.us        2280         [ #  # ]:UBC           0 :             for (int n = 0; n < w->num_workers; n++)
                               2281                 :                :             {
                               2282                 :              0 :                 ExplainOpenWorker(n, es);
                               2283                 :              0 :                 ExplainPrintJIT(es, planstate->state->es_jit_flags,
                               2284                 :                :                                 &w->jit_instr[n]);
                               2285                 :              0 :                 ExplainCloseWorker(n, es);
                               2286                 :                :             }
                               2287                 :                :         }
                               2288                 :                :     }
                               2289                 :                : 
                               2290                 :                :     /* Show buffer/WAL usage */
 4228 tgl@sss.pgh.pa.us        2291   [ +  +  +  - ]:CBC       43858 :     if (es->buffers && planstate->instrument)
  687 alvherre@alvh.no-ip.     2292                 :           2105 :         show_buffer_usage(es, &planstate->instrument->bufusage);
 2080 akapila@postgresql.o     2293   [ -  +  -  - ]:          43858 :     if (es->wal && planstate->instrument)
 2080 akapila@postgresql.o     2294                 :UBC           0 :         show_wal_usage(es, &planstate->instrument->walusage);
                               2295                 :                : 
                               2296                 :                :     /* Prepare per-worker buffer/WAL usage */
 2080 akapila@postgresql.o     2297   [ +  +  +  +  :CBC       43858 :     if (es->workers_state && (es->buffers || es->wal) && es->verbose)
                                        -  +  +  + ]
                               2298                 :                :     {
 3660 rhaas@postgresql.org     2299                 :              6 :         WorkerInstrumentation *w = planstate->worker_instrument;
                               2300                 :                : 
 2152 tgl@sss.pgh.pa.us        2301         [ +  + ]:             30 :         for (int n = 0; n < w->num_workers; n++)
                               2302                 :                :         {
 3660 rhaas@postgresql.org     2303                 :             24 :             Instrumentation *instrument = &w->instrument[n];
                               2304                 :             24 :             double      nloops = instrument->nloops;
                               2305                 :                : 
                               2306         [ -  + ]:             24 :             if (nloops <= 0)
 3660 rhaas@postgresql.org     2307                 :UBC           0 :                 continue;
                               2308                 :                : 
 2152 tgl@sss.pgh.pa.us        2309                 :CBC          24 :             ExplainOpenWorker(n, es);
 2080 akapila@postgresql.o     2310         [ +  - ]:             24 :             if (es->buffers)
  687 alvherre@alvh.no-ip.     2311                 :             24 :                 show_buffer_usage(es, &instrument->bufusage);
 2080 akapila@postgresql.o     2312         [ -  + ]:             24 :             if (es->wal)
 2080 akapila@postgresql.o     2313                 :UBC           0 :                 show_wal_usage(es, &instrument->walusage);
 2152 tgl@sss.pgh.pa.us        2314                 :CBC          24 :             ExplainCloseWorker(n, es);
                               2315                 :                :         }
                               2316                 :                :     }
                               2317                 :                : 
                               2318                 :                :     /* Show per-worker details for this plan node, then pop that stack */
                               2319         [ +  + ]:          43858 :     if (es->workers_state)
                               2320                 :            513 :         ExplainFlushWorkersState(es);
                               2321                 :          43858 :     es->workers_state = save_workers_state;
                               2322                 :                : 
                               2323                 :                :     /* Allow plugins to print additional information */
  273 rhaas@postgresql.org     2324         [ +  + ]:          43858 :     if (explain_per_node_hook)
                               2325                 :             30 :         (*explain_per_node_hook) (planstate, ancestors, relationship,
                               2326                 :                :                                   plan_name, es);
                               2327                 :                : 
                               2328                 :                :     /*
                               2329                 :                :      * If partition pruning was done during executor initialization, the
                               2330                 :                :      * number of child plans we'll display below will be less than the number
                               2331                 :                :      * of subplans that was specified in the plan.  To make this a bit less
                               2332                 :                :      * mysterious, emit an indication that this happened.  Note that this
                               2333                 :                :      * field is emitted now because we want it to be a property of the parent
                               2334                 :                :      * node; it *cannot* be emitted within the Plans sub-node we'll open next.
                               2335                 :                :      */
 2142 tgl@sss.pgh.pa.us        2336      [ +  +  + ]:          43858 :     switch (nodeTag(plan))
                               2337                 :                :     {
                               2338                 :           1777 :         case T_Append:
                               2339                 :           1777 :             ExplainMissingMembers(((AppendState *) planstate)->as_nplans,
                               2340                 :           1777 :                                   list_length(((Append *) plan)->appendplans),
                               2341                 :                :                                   es);
                               2342                 :           1777 :             break;
                               2343                 :            174 :         case T_MergeAppend:
                               2344                 :            174 :             ExplainMissingMembers(((MergeAppendState *) planstate)->ms_nplans,
                               2345                 :            174 :                                   list_length(((MergeAppend *) plan)->mergeplans),
                               2346                 :                :                                   es);
                               2347                 :            174 :             break;
                               2348                 :          41907 :         default:
                               2349                 :          41907 :             break;
                               2350                 :                :     }
                               2351                 :                : 
                               2352                 :                :     /* Get ready to display the child plans */
 5635                          2353                 :         130983 :     haschildren = planstate->initPlan ||
                               2354         [ +  + ]:          43267 :         outerPlanState(planstate) ||
                               2355         [ +  - ]:          23743 :         innerPlanState(planstate) ||
 5972                          2356         [ +  + ]:          23743 :         IsA(plan, Append) ||
 5542                          2357         [ +  + ]:          22023 :         IsA(plan, MergeAppend) ||
 5972                          2358         [ +  + ]:          21852 :         IsA(plan, BitmapAnd) ||
                               2359         [ +  + ]:          21831 :         IsA(plan, BitmapOr) ||
                               2360         [ +  + ]:          21758 :         IsA(plan, SubqueryScan) ||
 3826 rhaas@postgresql.org     2361         [ -  + ]:          21541 :         (IsA(planstate, CustomScanState) &&
                               2362   [ +  +  -  - ]:          87125 :          ((CustomScanState *) planstate)->custom_ps != NIL) ||
 5972 tgl@sss.pgh.pa.us        2363         [ +  + ]:          21541 :         planstate->subPlan;
                               2364         [ +  + ]:          43858 :     if (haschildren)
                               2365                 :                :     {
                               2366                 :          22505 :         ExplainOpenGroup("Plans", "Plans", false, es);
                               2367                 :                :         /* Pass current Plan as head of ancestors list for children */
 2197                          2368                 :          22505 :         ancestors = lcons(plan, ancestors);
                               2369                 :                :     }
                               2370                 :                : 
                               2371                 :                :     /* initPlan-s */
 5635                          2372         [ +  + ]:          43858 :     if (planstate->initPlan)
                               2373                 :            591 :         ExplainSubPlans(planstate->initPlan, ancestors, "InitPlan", es);
                               2374                 :                : 
                               2375                 :                :     /* lefttree */
                               2376         [ +  + ]:          43858 :     if (outerPlanState(planstate))
                               2377                 :          19724 :         ExplainNode(outerPlanState(planstate), ancestors,
                               2378                 :                :                     "Outer", NULL, es);
                               2379                 :                : 
                               2380                 :                :     /* righttree */
                               2381         [ +  + ]:          43858 :     if (innerPlanState(planstate))
                               2382                 :           4487 :         ExplainNode(innerPlanState(planstate), ancestors,
                               2383                 :                :                     "Inner", NULL, es);
                               2384                 :                : 
                               2385                 :                :     /* special child plans */
 5989                          2386   [ +  +  +  +  :          43858 :     switch (nodeTag(plan))
                                           +  -  + ]
                               2387                 :                :     {
                               2388                 :           1777 :         case T_Append:
 2810 alvherre@alvh.no-ip.     2389                 :           1777 :             ExplainMemberNodes(((AppendState *) planstate)->appendplans,
                               2390                 :                :                                ((AppendState *) planstate)->as_nplans,
                               2391                 :                :                                ancestors, es);
 5989 tgl@sss.pgh.pa.us        2392                 :           1777 :             break;
 5542                          2393                 :            174 :         case T_MergeAppend:
 2810 alvherre@alvh.no-ip.     2394                 :            174 :             ExplainMemberNodes(((MergeAppendState *) planstate)->mergeplans,
                               2395                 :                :                                ((MergeAppendState *) planstate)->ms_nplans,
                               2396                 :                :                                ancestors, es);
 5542 tgl@sss.pgh.pa.us        2397                 :            174 :             break;
 5989                          2398                 :             21 :         case T_BitmapAnd:
 2810 alvherre@alvh.no-ip.     2399                 :             21 :             ExplainMemberNodes(((BitmapAndState *) planstate)->bitmapplans,
                               2400                 :                :                                ((BitmapAndState *) planstate)->nplans,
                               2401                 :                :                                ancestors, es);
 5989 tgl@sss.pgh.pa.us        2402                 :             21 :             break;
                               2403                 :             73 :         case T_BitmapOr:
 2810 alvherre@alvh.no-ip.     2404                 :             73 :             ExplainMemberNodes(((BitmapOrState *) planstate)->bitmapplans,
                               2405                 :                :                                ((BitmapOrState *) planstate)->nplans,
                               2406                 :                :                                ancestors, es);
 5989 tgl@sss.pgh.pa.us        2407                 :             73 :             break;
                               2408                 :            217 :         case T_SubqueryScan:
 5635                          2409                 :            217 :             ExplainNode(((SubqueryScanState *) planstate)->subplan, ancestors,
                               2410                 :                :                         "Subquery", NULL, es);
 5989                          2411                 :            217 :             break;
 3826 rhaas@postgresql.org     2412                 :UBC           0 :         case T_CustomScan:
                               2413                 :              0 :             ExplainCustomChildren((CustomScanState *) planstate,
                               2414                 :                :                                   ancestors, es);
                               2415                 :              0 :             break;
 5989 tgl@sss.pgh.pa.us        2416                 :CBC       41596 :         default:
                               2417                 :          41596 :             break;
                               2418                 :                :     }
                               2419                 :                : 
                               2420                 :                :     /* subPlan-s */
 8412                          2421         [ +  + ]:          43858 :     if (planstate->subPlan)
 5635                          2422                 :            303 :         ExplainSubPlans(planstate->subPlan, ancestors, "SubPlan", es);
                               2423                 :                : 
                               2424                 :                :     /* end of child plans */
 5972                          2425         [ +  + ]:          43858 :     if (haschildren)
                               2426                 :                :     {
 5502 peter_e@gmx.net          2427                 :          22505 :         ancestors = list_delete_first(ancestors);
 5972 tgl@sss.pgh.pa.us        2428                 :          22505 :         ExplainCloseGroup("Plans", "Plans", false, es);
                               2429                 :                :     }
                               2430                 :                : 
                               2431                 :                :     /* in text format, undo whatever indentation we added */
                               2432         [ +  + ]:          43858 :     if (es->format == EXPLAIN_FORMAT_TEXT)
                               2433                 :          43314 :         es->indent = save_indent;
                               2434                 :                : 
                               2435         [ +  + ]:          43858 :     ExplainCloseGroup("Plan",
                               2436                 :                :                       relationship ? NULL : "Plan",
                               2437                 :                :                       true, es);
10752 scrappy@hub.org          2438                 :          43858 : }
                               2439                 :                : 
                               2440                 :                : /*
                               2441                 :                :  * Show the targetlist of a plan node
                               2442                 :                :  */
                               2443                 :                : static void
 5635 tgl@sss.pgh.pa.us        2444                 :           5898 : show_plan_tlist(PlanState *planstate, List *ancestors, ExplainState *es)
                               2445                 :                : {
                               2446                 :           5898 :     Plan       *plan = planstate->plan;
                               2447                 :                :     List       *context;
 5972                          2448                 :           5898 :     List       *result = NIL;
                               2449                 :                :     bool        useprefix;
                               2450                 :                :     ListCell   *lc;
                               2451                 :                : 
                               2452                 :                :     /* No work if empty tlist (this occurs eg in bitmap indexscans) */
 6452                          2453         [ +  + ]:           5898 :     if (plan->targetlist == NIL)
                               2454                 :            259 :         return;
                               2455                 :                :     /* The tlist of an Append isn't real helpful, so suppress it */
                               2456         [ +  + ]:           5639 :     if (IsA(plan, Append))
                               2457                 :            153 :         return;
                               2458                 :                :     /* Likewise for MergeAppend and RecursiveUnion */
 5542                          2459         [ +  + ]:           5486 :     if (IsA(plan, MergeAppend))
                               2460                 :             20 :         return;
 6282                          2461         [ +  + ]:           5466 :     if (IsA(plan, RecursiveUnion))
                               2462                 :             24 :         return;
                               2463                 :                : 
                               2464                 :                :     /*
                               2465                 :                :      * Likewise for ForeignScan that executes a direct INSERT/UPDATE/DELETE
                               2466                 :                :      *
                               2467                 :                :      * Note: the tlist for a ForeignScan that executes a direct INSERT/UPDATE
                               2468                 :                :      * might contain subplan output expressions that are confusing in this
                               2469                 :                :      * context.  The tlist for a ForeignScan that executes a direct UPDATE/
                               2470                 :                :      * DELETE always contains "junk" target columns to identify the exact row
                               2471                 :                :      * to update or delete, which would be confusing in this context.  So, we
                               2472                 :                :      * suppress it in all the cases.
                               2473                 :                :      */
 3560 rhaas@postgresql.org     2474         [ +  + ]:           5442 :     if (IsA(plan, ForeignScan) &&
                               2475         [ +  + ]:            391 :         ((ForeignScan *) plan)->operation != CMD_SELECT)
                               2476                 :             32 :         return;
                               2477                 :                : 
                               2478                 :                :     /* Set up deparsing context */
 2197 tgl@sss.pgh.pa.us        2479                 :           5410 :     context = set_deparse_context_plan(es->deparse_cxt,
                               2480                 :                :                                        plan,
                               2481                 :                :                                        ancestors);
  462 rguo@postgresql.org      2482                 :           5410 :     useprefix = es->rtable_size > 1;
                               2483                 :                : 
                               2484                 :                :     /* Deparse each result column (we now include resjunk ones) */
 6452 tgl@sss.pgh.pa.us        2485   [ +  -  +  +  :          19546 :     foreach(lc, plan->targetlist)
                                              +  + ]
                               2486                 :                :     {
                               2487                 :          14136 :         TargetEntry *tle = (TargetEntry *) lfirst(lc);
                               2488                 :                : 
 5972                          2489                 :          14136 :         result = lappend(result,
 5772 bruce@momjian.us         2490                 :          14136 :                          deparse_expression((Node *) tle->expr, context,
                               2491                 :                :                                             useprefix, false));
                               2492                 :                :     }
                               2493                 :                : 
                               2494                 :                :     /* Print results */
 5972 tgl@sss.pgh.pa.us        2495                 :           5410 :     ExplainPropertyList("Output", result, es);
                               2496                 :                : }
                               2497                 :                : 
                               2498                 :                : /*
                               2499                 :                :  * Show a generic expression
                               2500                 :                :  */
                               2501                 :                : static void
 5593                          2502                 :          18656 : show_expression(Node *node, const char *qlabel,
                               2503                 :                :                 PlanState *planstate, List *ancestors,
                               2504                 :                :                 bool useprefix, ExplainState *es)
                               2505                 :                : {
                               2506                 :                :     List       *context;
                               2507                 :                :     char       *exprstr;
                               2508                 :                : 
                               2509                 :                :     /* Set up deparsing context */
 2197                          2510                 :          18656 :     context = set_deparse_context_plan(es->deparse_cxt,
 2197 tgl@sss.pgh.pa.us        2511                 :ECB     (18206) :                                        planstate->plan,
                               2512                 :                :                                        ancestors);
                               2513                 :                : 
                               2514                 :                :     /* Deparse the expression */
 6871 tgl@sss.pgh.pa.us        2515                 :CBC       18656 :     exprstr = deparse_expression(node, context, useprefix, false);
                               2516                 :                : 
                               2517                 :                :     /* And add to es->str */
 5972                          2518                 :          18656 :     ExplainPropertyText(qlabel, exprstr, es);
 8680                          2519                 :          18656 : }
                               2520                 :                : 
                               2521                 :                : /*
                               2522                 :                :  * Show a qualifier expression (which is a List with implicit AND semantics)
                               2523                 :                :  */
                               2524                 :                : static void
 5593                          2525                 :          52533 : show_qual(List *qual, const char *qlabel,
                               2526                 :                :           PlanState *planstate, List *ancestors,
                               2527                 :                :           bool useprefix, ExplainState *es)
                               2528                 :                : {
                               2529                 :                :     Node       *node;
                               2530                 :                : 
                               2531                 :                :     /* No work if empty qual */
                               2532         [ +  + ]:          52533 :     if (qual == NIL)
                               2533                 :          34013 :         return;
                               2534                 :                : 
                               2535                 :                :     /* Convert AND list to explicit AND */
                               2536                 :          18520 :     node = (Node *) make_ands_explicit(qual);
                               2537                 :                : 
                               2538                 :                :     /* And show it */
                               2539                 :          18520 :     show_expression(node, qlabel, planstate, ancestors, useprefix, es);
                               2540                 :                : }
                               2541                 :                : 
                               2542                 :                : /*
                               2543                 :                :  * Show a qualifier expression for a scan plan node
                               2544                 :                :  */
                               2545                 :                : static void
 5989                          2546                 :          32335 : show_scan_qual(List *qual, const char *qlabel,
                               2547                 :                :                PlanState *planstate, List *ancestors,
                               2548                 :                :                ExplainState *es)
                               2549                 :                : {
                               2550                 :                :     bool        useprefix;
                               2551                 :                : 
 2040                          2552   [ +  +  +  + ]:          32335 :     useprefix = (IsA(planstate->plan, SubqueryScan) || es->verbose);
 5635                          2553                 :          32335 :     show_qual(qual, qlabel, planstate, ancestors, useprefix, es);
 5989                          2554                 :          32335 : }
                               2555                 :                : 
                               2556                 :                : /*
                               2557                 :                :  * Show a qualifier expression for an upper-level plan node
                               2558                 :                :  */
                               2559                 :                : static void
 5635                          2560                 :          20198 : show_upper_qual(List *qual, const char *qlabel,
                               2561                 :                :                 PlanState *planstate, List *ancestors,
                               2562                 :                :                 ExplainState *es)
                               2563                 :                : {
                               2564                 :                :     bool        useprefix;
                               2565                 :                : 
  462 rguo@postgresql.org      2566   [ +  +  +  + ]:          20198 :     useprefix = (es->rtable_size > 1 || es->verbose);
 5635 tgl@sss.pgh.pa.us        2567                 :          20198 :     show_qual(qual, qlabel, planstate, ancestors, useprefix, es);
 8680                          2568                 :          20198 : }
                               2569                 :                : 
                               2570                 :                : /*
                               2571                 :                :  * Show the sort keys for a Sort node.
                               2572                 :                :  */
                               2573                 :                : static void
 5635                          2574                 :           2379 : show_sort_keys(SortState *sortstate, List *ancestors, ExplainState *es)
                               2575                 :                : {
                               2576                 :           2379 :     Sort       *plan = (Sort *) sortstate->ss.ps.plan;
                               2577                 :                : 
 4387                          2578                 :           2379 :     show_sort_group_keys((PlanState *) sortstate, "Sort Key",
                               2579                 :                :                          plan->numCols, 0, plan->sortColIdx,
                               2580                 :                :                          plan->sortOperators, plan->collations,
                               2581                 :                :                          plan->nullsFirst,
                               2582                 :                :                          ancestors, es);
 5542                          2583                 :           2379 : }
                               2584                 :                : 
                               2585                 :                : /*
                               2586                 :                :  * Show the sort keys for an IncrementalSort node.
                               2587                 :                :  */
                               2588                 :                : static void
 2080 tomas.vondra@postgre     2589                 :            196 : show_incremental_sort_keys(IncrementalSortState *incrsortstate,
                               2590                 :                :                            List *ancestors, ExplainState *es)
                               2591                 :                : {
                               2592                 :            196 :     IncrementalSort *plan = (IncrementalSort *) incrsortstate->ss.ps.plan;
                               2593                 :                : 
                               2594                 :            196 :     show_sort_group_keys((PlanState *) incrsortstate, "Sort Key",
                               2595                 :                :                          plan->sort.numCols, plan->nPresortedCols,
                               2596                 :                :                          plan->sort.sortColIdx,
                               2597                 :                :                          plan->sort.sortOperators, plan->sort.collations,
                               2598                 :                :                          plan->sort.nullsFirst,
                               2599                 :                :                          ancestors, es);
                               2600                 :            196 : }
                               2601                 :                : 
                               2602                 :                : /*
                               2603                 :                :  * Likewise, for a MergeAppend node.
                               2604                 :                :  */
                               2605                 :                : static void
 5542 tgl@sss.pgh.pa.us        2606                 :            174 : show_merge_append_keys(MergeAppendState *mstate, List *ancestors,
                               2607                 :                :                        ExplainState *es)
                               2608                 :                : {
                               2609                 :            174 :     MergeAppend *plan = (MergeAppend *) mstate->ps.plan;
                               2610                 :                : 
 4387                          2611                 :            174 :     show_sort_group_keys((PlanState *) mstate, "Sort Key",
                               2612                 :                :                          plan->numCols, 0, plan->sortColIdx,
                               2613                 :                :                          plan->sortOperators, plan->collations,
                               2614                 :                :                          plan->nullsFirst,
                               2615                 :                :                          ancestors, es);
 5542                          2616                 :            174 : }
                               2617                 :                : 
                               2618                 :                : /*
                               2619                 :                :  * Show the grouping keys for an Agg node.
                               2620                 :                :  */
                               2621                 :                : static void
 4387                          2622                 :           5230 : show_agg_keys(AggState *astate, List *ancestors,
                               2623                 :                :               ExplainState *es)
                               2624                 :                : {
                               2625                 :           5230 :     Agg        *plan = (Agg *) astate->ss.ps.plan;
                               2626                 :                : 
 3867 andres@anarazel.de       2627   [ +  +  +  + ]:           5230 :     if (plan->numCols > 0 || plan->groupingSets)
                               2628                 :                :     {
                               2629                 :                :         /* The key columns refer to the tlist of the child plan */
 2197 tgl@sss.pgh.pa.us        2630                 :           1531 :         ancestors = lcons(plan, ancestors);
                               2631                 :                : 
 3867 andres@anarazel.de       2632         [ +  + ]:           1531 :         if (plan->groupingSets)
                               2633                 :            135 :             show_grouping_sets(outerPlanState(astate), plan, ancestors, es);
                               2634                 :                :         else
                               2635                 :           1396 :             show_sort_group_keys(outerPlanState(astate), "Group Key",
                               2636                 :                :                                  plan->numCols, 0, plan->grpColIdx,
                               2637                 :                :                                  NULL, NULL, NULL,
                               2638                 :                :                                  ancestors, es);
                               2639                 :                : 
 4387 tgl@sss.pgh.pa.us        2640                 :           1531 :         ancestors = list_delete_first(ancestors);
                               2641                 :                :     }
                               2642                 :           5230 : }
                               2643                 :                : 
                               2644                 :                : static void
 3867 andres@anarazel.de       2645                 :            135 : show_grouping_sets(PlanState *planstate, Agg *agg,
                               2646                 :                :                    List *ancestors, ExplainState *es)
                               2647                 :                : {
                               2648                 :                :     List       *context;
                               2649                 :                :     bool        useprefix;
                               2650                 :                :     ListCell   *lc;
                               2651                 :                : 
                               2652                 :                :     /* Set up deparsing context */
 2197 tgl@sss.pgh.pa.us        2653                 :            135 :     context = set_deparse_context_plan(es->deparse_cxt,
 2197 tgl@sss.pgh.pa.us        2654                 :ECB       (129) :                                        planstate->plan,
                               2655                 :                :                                        ancestors);
  462 rguo@postgresql.org      2656   [ +  +  +  + ]:CBC         135 :     useprefix = (es->rtable_size > 1 || es->verbose);
                               2657                 :                : 
 3867 andres@anarazel.de       2658                 :            135 :     ExplainOpenGroup("Grouping Sets", "Grouping Sets", false, es);
                               2659                 :                : 
                               2660                 :            135 :     show_grouping_set_keys(planstate, agg, NULL,
                               2661                 :                :                            context, useprefix, ancestors, es);
                               2662                 :                : 
                               2663   [ +  +  +  +  :            330 :     foreach(lc, agg->chain)
                                              +  + ]
                               2664                 :                :     {
 3860 bruce@momjian.us         2665                 :            195 :         Agg        *aggnode = lfirst(lc);
                               2666                 :            195 :         Sort       *sortnode = (Sort *) aggnode->plan.lefttree;
                               2667                 :                : 
 3867 andres@anarazel.de       2668                 :            195 :         show_grouping_set_keys(planstate, aggnode, sortnode,
                               2669                 :                :                                context, useprefix, ancestors, es);
                               2670                 :                :     }
                               2671                 :                : 
                               2672                 :            135 :     ExplainCloseGroup("Grouping Sets", "Grouping Sets", false, es);
                               2673                 :            135 : }
                               2674                 :                : 
                               2675                 :                : static void
                               2676                 :            330 : show_grouping_set_keys(PlanState *planstate,
                               2677                 :                :                        Agg *aggnode, Sort *sortnode,
                               2678                 :                :                        List *context, bool useprefix,
                               2679                 :                :                        List *ancestors, ExplainState *es)
                               2680                 :                : {
                               2681                 :            330 :     Plan       *plan = planstate->plan;
                               2682                 :                :     char       *exprstr;
                               2683                 :                :     ListCell   *lc;
                               2684                 :            330 :     List       *gsets = aggnode->groupingSets;
                               2685                 :            330 :     AttrNumber *keycols = aggnode->grpColIdx;
                               2686                 :                :     const char *keyname;
                               2687                 :                :     const char *keysetname;
                               2688                 :                : 
 3186 rhodiumtoad@postgres     2689   [ +  +  +  + ]:            330 :     if (aggnode->aggstrategy == AGG_HASHED || aggnode->aggstrategy == AGG_MIXED)
                               2690                 :                :     {
                               2691                 :            202 :         keyname = "Hash Key";
                               2692                 :            202 :         keysetname = "Hash Keys";
                               2693                 :                :     }
                               2694                 :                :     else
                               2695                 :                :     {
                               2696                 :            128 :         keyname = "Group Key";
                               2697                 :            128 :         keysetname = "Group Keys";
                               2698                 :                :     }
                               2699                 :                : 
 3867 andres@anarazel.de       2700                 :            330 :     ExplainOpenGroup("Grouping Set", NULL, true, es);
                               2701                 :                : 
                               2702         [ +  + ]:            330 :     if (sortnode)
                               2703                 :                :     {
                               2704                 :             30 :         show_sort_group_keys(planstate, "Sort Key",
                               2705                 :                :                              sortnode->numCols, 0, sortnode->sortColIdx,
                               2706                 :                :                              sortnode->sortOperators, sortnode->collations,
                               2707                 :                :                              sortnode->nullsFirst,
                               2708                 :                :                              ancestors, es);
                               2709         [ +  - ]:             30 :         if (es->format == EXPLAIN_FORMAT_TEXT)
                               2710                 :             30 :             es->indent++;
                               2711                 :                :     }
                               2712                 :                : 
 3186 rhodiumtoad@postgres     2713                 :            330 :     ExplainOpenGroup(keysetname, keysetname, false, es);
                               2714                 :                : 
 3867 andres@anarazel.de       2715   [ +  -  +  +  :            717 :     foreach(lc, gsets)
                                              +  + ]
                               2716                 :                :     {
                               2717                 :            387 :         List       *result = NIL;
                               2718                 :                :         ListCell   *lc2;
                               2719                 :                : 
                               2720   [ +  +  +  +  :            793 :         foreach(lc2, (List *) lfirst(lc))
                                              +  + ]
                               2721                 :                :         {
                               2722                 :            406 :             Index       i = lfirst_int(lc2);
                               2723                 :            406 :             AttrNumber  keyresno = keycols[i];
                               2724                 :            406 :             TargetEntry *target = get_tle_by_resno(plan->targetlist,
                               2725                 :                :                                                    keyresno);
                               2726                 :                : 
                               2727         [ -  + ]:            406 :             if (!target)
 3867 andres@anarazel.de       2728         [ #  # ]:UBC           0 :                 elog(ERROR, "no tlist entry for key %d", keyresno);
                               2729                 :                :             /* Deparse the expression, showing any top-level cast */
 3867 andres@anarazel.de       2730                 :CBC         406 :             exprstr = deparse_expression((Node *) target->expr, context,
                               2731                 :                :                                          useprefix, true);
                               2732                 :                : 
                               2733                 :            406 :             result = lappend(result, exprstr);
                               2734                 :                :         }
                               2735                 :                : 
                               2736   [ +  +  +  - ]:            387 :         if (!result && es->format == EXPLAIN_FORMAT_TEXT)
 3186 rhodiumtoad@postgres     2737                 :             77 :             ExplainPropertyText(keyname, "()", es);
                               2738                 :                :         else
                               2739                 :            310 :             ExplainPropertyListNested(keyname, result, es);
                               2740                 :                :     }
                               2741                 :                : 
                               2742                 :            330 :     ExplainCloseGroup(keysetname, keysetname, false, es);
                               2743                 :                : 
 3867 andres@anarazel.de       2744   [ +  +  +  - ]:            330 :     if (sortnode && es->format == EXPLAIN_FORMAT_TEXT)
                               2745                 :             30 :         es->indent--;
                               2746                 :                : 
                               2747                 :            330 :     ExplainCloseGroup("Grouping Set", NULL, true, es);
                               2748                 :            330 : }
                               2749                 :                : 
                               2750                 :                : /*
                               2751                 :                :  * Show the grouping keys for a Group node.
                               2752                 :                :  */
                               2753                 :                : static void
 4387 tgl@sss.pgh.pa.us        2754                 :             48 : show_group_keys(GroupState *gstate, List *ancestors,
                               2755                 :                :                 ExplainState *es)
                               2756                 :                : {
                               2757                 :             48 :     Group      *plan = (Group *) gstate->ss.ps.plan;
                               2758                 :                : 
                               2759                 :                :     /* The key columns refer to the tlist of the child plan */
 2197                          2760                 :             48 :     ancestors = lcons(plan, ancestors);
 4387                          2761                 :             48 :     show_sort_group_keys(outerPlanState(gstate), "Group Key",
                               2762                 :                :                          plan->numCols, 0, plan->grpColIdx,
                               2763                 :                :                          NULL, NULL, NULL,
                               2764                 :                :                          ancestors, es);
                               2765                 :             48 :     ancestors = list_delete_first(ancestors);
                               2766                 :             48 : }
                               2767                 :                : 
                               2768                 :                : /*
                               2769                 :                :  * Common code to show sort/group keys, which are represented in plan nodes
                               2770                 :                :  * as arrays of targetlist indexes.  If it's a sort key rather than a group
                               2771                 :                :  * key, also pass sort operators/collations/nullsFirst arrays.
                               2772                 :                :  */
                               2773                 :                : static void
                               2774                 :           4223 : show_sort_group_keys(PlanState *planstate, const char *qlabel,
                               2775                 :                :                      int nkeys, int nPresortedKeys, AttrNumber *keycols,
                               2776                 :                :                      Oid *sortOperators, Oid *collations, bool *nullsFirst,
                               2777                 :                :                      List *ancestors, ExplainState *es)
                               2778                 :                : {
 5542                          2779                 :           4223 :     Plan       *plan = planstate->plan;
                               2780                 :                :     List       *context;
 5972                          2781                 :           4223 :     List       *result = NIL;
 2080 tomas.vondra@postgre     2782                 :           4223 :     List       *resultPresorted = NIL;
                               2783                 :                :     StringInfoData sortkeybuf;
                               2784                 :                :     bool        useprefix;
                               2785                 :                :     int         keyno;
                               2786                 :                : 
 8613 tgl@sss.pgh.pa.us        2787         [ -  + ]:           4223 :     if (nkeys <= 0)
 8613 tgl@sss.pgh.pa.us        2788                 :UBC           0 :         return;
                               2789                 :                : 
 3987 tgl@sss.pgh.pa.us        2790                 :CBC        4223 :     initStringInfo(&sortkeybuf);
                               2791                 :                : 
                               2792                 :                :     /* Set up deparsing context */
 2197                          2793                 :           4223 :     context = set_deparse_context_plan(es->deparse_cxt,
                               2794                 :                :                                        plan,
                               2795                 :                :                                        ancestors);
  462 rguo@postgresql.org      2796   [ +  +  +  + ]:           4223 :     useprefix = (es->rtable_size > 1 || es->verbose);
                               2797                 :                : 
 8260 tgl@sss.pgh.pa.us        2798         [ +  + ]:          10492 :     for (keyno = 0; keyno < nkeys; keyno++)
                               2799                 :                :     {
                               2800                 :                :         /* find key expression in tlist */
                               2801                 :           6269 :         AttrNumber  keyresno = keycols[keyno];
 5542                          2802                 :           6269 :         TargetEntry *target = get_tle_by_resno(plan->targetlist,
                               2803                 :                :                                                keyresno);
                               2804                 :                :         char       *exprstr;
                               2805                 :                : 
 8163                          2806         [ -  + ]:           6269 :         if (!target)
 8185 tgl@sss.pgh.pa.us        2807         [ #  # ]:UBC           0 :             elog(ERROR, "no tlist entry for key %d", keyresno);
                               2808                 :                :         /* Deparse the expression, showing any top-level cast */
 8163 tgl@sss.pgh.pa.us        2809                 :CBC        6269 :         exprstr = deparse_expression((Node *) target->expr, context,
                               2810                 :                :                                      useprefix, true);
 3987                          2811                 :           6269 :         resetStringInfo(&sortkeybuf);
                               2812                 :           6269 :         appendStringInfoString(&sortkeybuf, exprstr);
                               2813                 :                :         /* Append sort order information, if relevant */
                               2814         [ +  + ]:           6269 :         if (sortOperators != NULL)
                               2815                 :           4017 :             show_sortorder_options(&sortkeybuf,
                               2816                 :           4017 :                                    (Node *) target->expr,
                               2817                 :           4017 :                                    sortOperators[keyno],
                               2818                 :           4017 :                                    collations[keyno],
                               2819                 :           4017 :                                    nullsFirst[keyno]);
                               2820                 :                :         /* Emit one property-list item per sort key */
                               2821                 :           6269 :         result = lappend(result, pstrdup(sortkeybuf.data));
 2080 tomas.vondra@postgre     2822         [ +  + ]:           6269 :         if (keyno < nPresortedKeys)
                               2823                 :            214 :             resultPresorted = lappend(resultPresorted, exprstr);
                               2824                 :                :     }
                               2825                 :                : 
 4387 tgl@sss.pgh.pa.us        2826                 :           4223 :     ExplainPropertyList(qlabel, result, es);
 2080 tomas.vondra@postgre     2827         [ +  + ]:           4223 :     if (nPresortedKeys > 0)
                               2828                 :            196 :         ExplainPropertyList("Presorted Key", resultPresorted, es);
                               2829                 :                : }
                               2830                 :                : 
                               2831                 :                : /*
                               2832                 :                :  * Append nondefault characteristics of the sort ordering of a column to buf
                               2833                 :                :  * (collation, direction, NULLS FIRST/LAST)
                               2834                 :                :  */
                               2835                 :                : static void
 3987 tgl@sss.pgh.pa.us        2836                 :           4017 : show_sortorder_options(StringInfo buf, Node *sortexpr,
                               2837                 :                :                        Oid sortOperator, Oid collation, bool nullsFirst)
                               2838                 :                : {
                               2839                 :           4017 :     Oid         sortcoltype = exprType(sortexpr);
                               2840                 :           4017 :     bool        reverse = false;
                               2841                 :                :     TypeCacheEntry *typentry;
                               2842                 :                : 
                               2843                 :           4017 :     typentry = lookup_type_cache(sortcoltype,
                               2844                 :                :                                  TYPECACHE_LT_OPR | TYPECACHE_GT_OPR);
                               2845                 :                : 
                               2846                 :                :     /*
                               2847                 :                :      * Print COLLATE if it's not default for the column's type.  There are
                               2848                 :                :      * some cases where this is redundant, eg if expression is a column whose
                               2849                 :                :      * declared collation is that collation, but it's hard to distinguish that
                               2850                 :                :      * here (and arguably, printing COLLATE explicitly is a good idea anyway
                               2851                 :                :      * in such cases).
                               2852                 :                :      */
 2554                          2853   [ +  +  +  + ]:           4017 :     if (OidIsValid(collation) && collation != get_typcollation(sortcoltype))
                               2854                 :                :     {
 3987                          2855                 :             55 :         char       *collname = get_collation_name(collation);
                               2856                 :                : 
                               2857         [ -  + ]:             55 :         if (collname == NULL)
 3987 tgl@sss.pgh.pa.us        2858         [ #  # ]:UBC           0 :             elog(ERROR, "cache lookup failed for collation %u", collation);
 3987 tgl@sss.pgh.pa.us        2859                 :CBC          55 :         appendStringInfo(buf, " COLLATE %s", quote_identifier(collname));
                               2860                 :                :     }
                               2861                 :                : 
                               2862                 :                :     /* Print direction if not ASC, or USING if non-default sort operator */
                               2863         [ +  + ]:           4017 :     if (sortOperator == typentry->gt_opr)
                               2864                 :                :     {
                               2865                 :            125 :         appendStringInfoString(buf, " DESC");
                               2866                 :            125 :         reverse = true;
                               2867                 :                :     }
                               2868         [ +  + ]:           3892 :     else if (sortOperator != typentry->lt_opr)
                               2869                 :                :     {
                               2870                 :             17 :         char       *opname = get_opname(sortOperator);
                               2871                 :                : 
                               2872         [ -  + ]:             17 :         if (opname == NULL)
 3987 tgl@sss.pgh.pa.us        2873         [ #  # ]:UBC           0 :             elog(ERROR, "cache lookup failed for operator %u", sortOperator);
 3987 tgl@sss.pgh.pa.us        2874                 :CBC          17 :         appendStringInfo(buf, " USING %s", opname);
                               2875                 :                :         /* Determine whether operator would be considered ASC or DESC */
                               2876                 :             17 :         (void) get_equality_op_for_ordering_op(sortOperator, &reverse);
                               2877                 :                :     }
                               2878                 :                : 
                               2879                 :                :     /* Add NULLS FIRST/LAST only if it wouldn't be default */
                               2880   [ +  +  +  + ]:           4017 :     if (nullsFirst && !reverse)
                               2881                 :                :     {
                               2882                 :             18 :         appendStringInfoString(buf, " NULLS FIRST");
                               2883                 :                :     }
                               2884   [ +  +  -  + ]:           3999 :     else if (!nullsFirst && reverse)
                               2885                 :                :     {
 3987 tgl@sss.pgh.pa.us        2886                 :UBC           0 :         appendStringInfoString(buf, " NULLS LAST");
                               2887                 :                :     }
 3987 tgl@sss.pgh.pa.us        2888                 :CBC        4017 : }
                               2889                 :                : 
                               2890                 :                : /*
                               2891                 :                :  * Show the window definition for a WindowAgg node.
                               2892                 :                :  */
                               2893                 :                : static void
  280                          2894                 :            235 : show_window_def(WindowAggState *planstate, List *ancestors, ExplainState *es)
                               2895                 :                : {
                               2896                 :            235 :     WindowAgg  *wagg = (WindowAgg *) planstate->ss.ps.plan;
                               2897                 :                :     StringInfoData wbuf;
                               2898                 :            235 :     bool        needspace = false;
                               2899                 :                : 
                               2900                 :            235 :     initStringInfo(&wbuf);
                               2901                 :            235 :     appendStringInfo(&wbuf, "%s AS (", quote_identifier(wagg->winname));
                               2902                 :                : 
                               2903                 :                :     /* The key columns refer to the tlist of the child plan */
                               2904                 :            235 :     ancestors = lcons(wagg, ancestors);
                               2905         [ +  + ]:            235 :     if (wagg->partNumCols > 0)
                               2906                 :                :     {
                               2907                 :            123 :         appendStringInfoString(&wbuf, "PARTITION BY ");
                               2908                 :            123 :         show_window_keys(&wbuf, outerPlanState(planstate),
                               2909                 :                :                          wagg->partNumCols, wagg->partColIdx,
                               2910                 :                :                          ancestors, es);
                               2911                 :            123 :         needspace = true;
                               2912                 :                :     }
                               2913         [ +  + ]:            235 :     if (wagg->ordNumCols > 0)
                               2914                 :                :     {
                               2915         [ +  + ]:            164 :         if (needspace)
                               2916                 :             80 :             appendStringInfoChar(&wbuf, ' ');
                               2917                 :            164 :         appendStringInfoString(&wbuf, "ORDER BY ");
                               2918                 :            164 :         show_window_keys(&wbuf, outerPlanState(planstate),
                               2919                 :                :                          wagg->ordNumCols, wagg->ordColIdx,
                               2920                 :                :                          ancestors, es);
                               2921                 :            164 :         needspace = true;
                               2922                 :                :     }
                               2923                 :            235 :     ancestors = list_delete_first(ancestors);
                               2924         [ +  + ]:            235 :     if (wagg->frameOptions & FRAMEOPTION_NONDEFAULT)
                               2925                 :                :     {
                               2926                 :                :         List       *context;
                               2927                 :                :         bool        useprefix;
                               2928                 :                :         char       *framestr;
                               2929                 :                : 
                               2930                 :                :         /* Set up deparsing context for possible frame expressions */
                               2931                 :             98 :         context = set_deparse_context_plan(es->deparse_cxt,
                               2932                 :                :                                            (Plan *) wagg,
                               2933                 :                :                                            ancestors);
                               2934   [ +  +  +  + ]:             98 :         useprefix = (es->rtable_size > 1 || es->verbose);
                               2935                 :             98 :         framestr = get_window_frame_options_for_explain(wagg->frameOptions,
                               2936                 :                :                                                         wagg->startOffset,
                               2937                 :                :                                                         wagg->endOffset,
                               2938                 :                :                                                         context,
                               2939                 :                :                                                         useprefix);
                               2940         [ +  + ]:             98 :         if (needspace)
                               2941                 :             91 :             appendStringInfoChar(&wbuf, ' ');
                               2942                 :             98 :         appendStringInfoString(&wbuf, framestr);
                               2943                 :             98 :         pfree(framestr);
                               2944                 :                :     }
                               2945                 :            235 :     appendStringInfoChar(&wbuf, ')');
                               2946                 :            235 :     ExplainPropertyText("Window", wbuf.data, es);
                               2947                 :            235 :     pfree(wbuf.data);
                               2948                 :            235 : }
                               2949                 :                : 
                               2950                 :                : /*
                               2951                 :                :  * Append the keys of a window's PARTITION BY or ORDER BY clause to buf.
                               2952                 :                :  * We can't use show_sort_group_keys for this because that's too opinionated
                               2953                 :                :  * about how the result will be displayed.
                               2954                 :                :  * Note that the "planstate" node should be the WindowAgg's child.
                               2955                 :                :  */
                               2956                 :                : static void
                               2957                 :            287 : show_window_keys(StringInfo buf, PlanState *planstate,
                               2958                 :                :                  int nkeys, AttrNumber *keycols,
                               2959                 :                :                  List *ancestors, ExplainState *es)
                               2960                 :                : {
                               2961                 :            287 :     Plan       *plan = planstate->plan;
                               2962                 :                :     List       *context;
                               2963                 :                :     bool        useprefix;
                               2964                 :                : 
                               2965                 :                :     /* Set up deparsing context */
                               2966                 :            287 :     context = set_deparse_context_plan(es->deparse_cxt,
                               2967                 :                :                                        plan,
                               2968                 :                :                                        ancestors);
                               2969   [ +  +  +  + ]:            287 :     useprefix = (es->rtable_size > 1 || es->verbose);
                               2970                 :                : 
                               2971         [ +  + ]:            589 :     for (int keyno = 0; keyno < nkeys; keyno++)
                               2972                 :                :     {
                               2973                 :                :         /* find key expression in tlist */
                               2974                 :            302 :         AttrNumber  keyresno = keycols[keyno];
                               2975                 :            302 :         TargetEntry *target = get_tle_by_resno(plan->targetlist,
                               2976                 :                :                                                keyresno);
                               2977                 :                :         char       *exprstr;
                               2978                 :                : 
                               2979         [ -  + ]:            302 :         if (!target)
  280 tgl@sss.pgh.pa.us        2980         [ #  # ]:UBC           0 :             elog(ERROR, "no tlist entry for key %d", keyresno);
                               2981                 :                :         /* Deparse the expression, showing any top-level cast */
  280 tgl@sss.pgh.pa.us        2982                 :CBC         302 :         exprstr = deparse_expression((Node *) target->expr, context,
                               2983                 :                :                                      useprefix, true);
                               2984         [ +  + ]:            302 :         if (keyno > 0)
                               2985                 :             15 :             appendStringInfoString(buf, ", ");
                               2986                 :            302 :         appendStringInfoString(buf, exprstr);
                               2987                 :            302 :         pfree(exprstr);
                               2988                 :                : 
                               2989                 :                :         /*
                               2990                 :                :          * We don't attempt to provide sort order information because
                               2991                 :                :          * WindowAgg carries equality operators not comparison operators;
                               2992                 :                :          * compare show_agg_keys.
                               2993                 :                :          */
                               2994                 :                :     }
                               2995                 :            287 : }
                               2996                 :                : 
                               2997                 :                : /*
                               2998                 :                :  * Show information on storage method and maximum memory/disk space used.
                               2999                 :                :  */
                               3000                 :                : static void
  449 ishii@postgresql.org     3001                 :             15 : show_storage_info(char *maxStorageType, int64 maxSpaceUsed, ExplainState *es)
                               3002                 :                : {
                               3003                 :             15 :     int64       maxSpaceUsedKB = BYTES_TO_KILOBYTES(maxSpaceUsed);
                               3004                 :                : 
  455                          3005         [ -  + ]:             15 :     if (es->format != EXPLAIN_FORMAT_TEXT)
                               3006                 :                :     {
  455 ishii@postgresql.org     3007                 :UBC           0 :         ExplainPropertyText("Storage", maxStorageType, es);
                               3008                 :              0 :         ExplainPropertyInteger("Maximum Storage", "kB", maxSpaceUsedKB, es);
                               3009                 :                :     }
                               3010                 :                :     else
                               3011                 :                :     {
  455 ishii@postgresql.org     3012                 :CBC          15 :         ExplainIndentText(es);
                               3013                 :             15 :         appendStringInfo(es->str,
                               3014                 :                :                          "Storage: %s  Maximum Storage: " INT64_FORMAT "kB\n",
                               3015                 :                :                          maxStorageType,
                               3016                 :                :                          maxSpaceUsedKB);
                               3017                 :                :     }
                               3018                 :             15 : }
                               3019                 :                : 
                               3020                 :                : /*
                               3021                 :                :  * Show TABLESAMPLE properties
                               3022                 :                :  */
                               3023                 :                : static void
 3797 tgl@sss.pgh.pa.us        3024                 :             60 : show_tablesample(TableSampleClause *tsc, PlanState *planstate,
                               3025                 :                :                  List *ancestors, ExplainState *es)
                               3026                 :                : {
                               3027                 :                :     List       *context;
                               3028                 :                :     bool        useprefix;
                               3029                 :                :     char       *method_name;
                               3030                 :             60 :     List       *params = NIL;
                               3031                 :                :     char       *repeatable;
                               3032                 :                :     ListCell   *lc;
                               3033                 :                : 
                               3034                 :                :     /* Set up deparsing context */
 2197                          3035                 :             60 :     context = set_deparse_context_plan(es->deparse_cxt,
 2197 tgl@sss.pgh.pa.us        3036                 :ECB        (60) :                                        planstate->plan,
                               3037                 :                :                                        ancestors);
  462 rguo@postgresql.org      3038                 :CBC          60 :     useprefix = es->rtable_size > 1;
                               3039                 :                : 
                               3040                 :                :     /* Get the tablesample method name */
 3797 tgl@sss.pgh.pa.us        3041                 :             60 :     method_name = get_func_name(tsc->tsmhandler);
                               3042                 :                : 
                               3043                 :                :     /* Deparse parameter expressions */
                               3044   [ +  -  +  +  :            120 :     foreach(lc, tsc->args)
                                              +  + ]
                               3045                 :                :     {
                               3046                 :             60 :         Node       *arg = (Node *) lfirst(lc);
                               3047                 :                : 
                               3048                 :             60 :         params = lappend(params,
                               3049                 :             60 :                          deparse_expression(arg, context,
                               3050                 :                :                                             useprefix, false));
                               3051                 :                :     }
                               3052         [ +  + ]:             60 :     if (tsc->repeatable)
                               3053                 :             30 :         repeatable = deparse_expression((Node *) tsc->repeatable, context,
                               3054                 :                :                                         useprefix, false);
                               3055                 :                :     else
                               3056                 :             30 :         repeatable = NULL;
                               3057                 :                : 
                               3058                 :                :     /* Print results */
                               3059         [ +  - ]:             60 :     if (es->format == EXPLAIN_FORMAT_TEXT)
                               3060                 :                :     {
                               3061                 :             60 :         bool        first = true;
                               3062                 :                : 
 2152                          3063                 :             60 :         ExplainIndentText(es);
 3797                          3064                 :             60 :         appendStringInfo(es->str, "Sampling: %s (", method_name);
                               3065   [ +  -  +  +  :            120 :         foreach(lc, params)
                                              +  + ]
                               3066                 :                :         {
                               3067         [ -  + ]:             60 :             if (!first)
 3797 tgl@sss.pgh.pa.us        3068                 :UBC           0 :                 appendStringInfoString(es->str, ", ");
 3797 tgl@sss.pgh.pa.us        3069                 :CBC          60 :             appendStringInfoString(es->str, (const char *) lfirst(lc));
                               3070                 :             60 :             first = false;
                               3071                 :                :         }
                               3072                 :             60 :         appendStringInfoChar(es->str, ')');
                               3073         [ +  + ]:             60 :         if (repeatable)
                               3074                 :             30 :             appendStringInfo(es->str, " REPEATABLE (%s)", repeatable);
                               3075                 :             60 :         appendStringInfoChar(es->str, '\n');
                               3076                 :                :     }
                               3077                 :                :     else
                               3078                 :                :     {
 3797 tgl@sss.pgh.pa.us        3079                 :UBC           0 :         ExplainPropertyText("Sampling Method", method_name, es);
                               3080                 :              0 :         ExplainPropertyList("Sampling Parameters", params, es);
                               3081         [ #  # ]:              0 :         if (repeatable)
                               3082                 :              0 :             ExplainPropertyText("Repeatable Seed", repeatable, es);
                               3083                 :                :     }
 3797 tgl@sss.pgh.pa.us        3084                 :CBC          60 : }
                               3085                 :                : 
                               3086                 :                : /*
                               3087                 :                :  * If it's EXPLAIN ANALYZE, show tuplesort stats for a sort node
                               3088                 :                :  */
                               3089                 :                : static void
 5972                          3090                 :           2379 : show_sort_info(SortState *sortstate, ExplainState *es)
                               3091                 :                : {
 3031 rhaas@postgresql.org     3092         [ +  + ]:           2379 :     if (!es->analyze)
                               3093                 :           2298 :         return;
                               3094                 :                : 
                               3095   [ +  +  +  - ]:             81 :     if (sortstate->sort_Done && sortstate->tuplesortstate != NULL)
                               3096                 :                :     {
 5772 bruce@momjian.us         3097                 :             78 :         Tuplesortstate *state = (Tuplesortstate *) sortstate->tuplesortstate;
                               3098                 :                :         TuplesortInstrumentation stats;
                               3099                 :                :         const char *sortMethod;
                               3100                 :                :         const char *spaceType;
                               3101                 :                :         int64       spaceUsed;
                               3102                 :                : 
 3031 rhaas@postgresql.org     3103                 :             78 :         tuplesort_get_stats(state, &stats);
                               3104                 :             78 :         sortMethod = tuplesort_method_name(stats.sortMethod);
                               3105                 :             78 :         spaceType = tuplesort_space_type_name(stats.spaceType);
                               3106                 :             78 :         spaceUsed = stats.spaceUsed;
                               3107                 :                : 
 5972 tgl@sss.pgh.pa.us        3108         [ +  + ]:             78 :         if (es->format == EXPLAIN_FORMAT_TEXT)
                               3109                 :                :         {
 2152                          3110                 :             63 :             ExplainIndentText(es);
 1962 drowley@postgresql.o     3111                 :             63 :             appendStringInfo(es->str, "Sort Method: %s  %s: " INT64_FORMAT "kB\n",
                               3112                 :                :                              sortMethod, spaceType, spaceUsed);
                               3113                 :                :         }
                               3114                 :                :         else
                               3115                 :                :         {
 5972 tgl@sss.pgh.pa.us        3116                 :             15 :             ExplainPropertyText("Sort Method", sortMethod, es);
 2832 andres@anarazel.de       3117                 :             15 :             ExplainPropertyInteger("Sort Space Used", "kB", spaceUsed, es);
 5972 tgl@sss.pgh.pa.us        3118                 :             15 :             ExplainPropertyText("Sort Space Type", spaceType, es);
                               3119                 :                :         }
                               3120                 :                :     }
                               3121                 :                : 
                               3122                 :                :     /*
                               3123                 :                :      * You might think we should just skip this stanza entirely when
                               3124                 :                :      * es->hide_workers is true, but then we'd get no sort-method output at
                               3125                 :                :      * all.  We have to make it look like worker 0's data is top-level data.
                               3126                 :                :      * This is easily done by just skipping the OpenWorker/CloseWorker calls.
                               3127                 :                :      * Currently, we don't worry about the possibility that there are multiple
                               3128                 :                :      * workers in such a case; if there are, duplicate output fields will be
                               3129                 :                :      * emitted.
                               3130                 :                :      */
 3031 rhaas@postgresql.org     3131         [ +  + ]:             81 :     if (sortstate->shared_info != NULL)
                               3132                 :                :     {
                               3133                 :                :         int         n;
                               3134                 :                : 
                               3135         [ +  + ]:             30 :         for (n = 0; n < sortstate->shared_info->num_workers; n++)
                               3136                 :                :         {
                               3137                 :                :             TuplesortInstrumentation *sinstrument;
                               3138                 :                :             const char *sortMethod;
                               3139                 :                :             const char *spaceType;
                               3140                 :                :             int64       spaceUsed;
                               3141                 :                : 
                               3142                 :             24 :             sinstrument = &sortstate->shared_info->sinstrument[n];
                               3143         [ -  + ]:             24 :             if (sinstrument->sortMethod == SORT_TYPE_STILL_IN_PROGRESS)
 3031 rhaas@postgresql.org     3144                 :UBC           0 :                 continue;       /* ignore any unfilled slots */
 3031 rhaas@postgresql.org     3145                 :CBC          24 :             sortMethod = tuplesort_method_name(sinstrument->sortMethod);
                               3146                 :             24 :             spaceType = tuplesort_space_type_name(sinstrument->spaceType);
                               3147                 :             24 :             spaceUsed = sinstrument->spaceUsed;
                               3148                 :                : 
 2152 tgl@sss.pgh.pa.us        3149         [ +  - ]:             24 :             if (es->workers_state)
                               3150                 :             24 :                 ExplainOpenWorker(n, es);
                               3151                 :                : 
 3031 rhaas@postgresql.org     3152         [ +  + ]:             24 :             if (es->format == EXPLAIN_FORMAT_TEXT)
                               3153                 :                :             {
 2152 tgl@sss.pgh.pa.us        3154                 :             12 :                 ExplainIndentText(es);
 3031 rhaas@postgresql.org     3155                 :             12 :                 appendStringInfo(es->str,
                               3156                 :                :                                  "Sort Method: %s  %s: " INT64_FORMAT "kB\n",
                               3157                 :                :                                  sortMethod, spaceType, spaceUsed);
                               3158                 :                :             }
                               3159                 :                :             else
                               3160                 :                :             {
                               3161                 :             12 :                 ExplainPropertyText("Sort Method", sortMethod, es);
 2832 andres@anarazel.de       3162                 :             12 :                 ExplainPropertyInteger("Sort Space Used", "kB", spaceUsed, es);
 3031 rhaas@postgresql.org     3163                 :             12 :                 ExplainPropertyText("Sort Space Type", spaceType, es);
                               3164                 :                :             }
                               3165                 :                : 
 2152 tgl@sss.pgh.pa.us        3166         [ +  - ]:             24 :             if (es->workers_state)
                               3167                 :             24 :                 ExplainCloseWorker(n, es);
                               3168                 :                :         }
                               3169                 :                :     }
                               3170                 :                : }
                               3171                 :                : 
                               3172                 :                : /*
                               3173                 :                :  * Incremental sort nodes sort in (a potentially very large number of) batches,
                               3174                 :                :  * so EXPLAIN ANALYZE needs to roll up the tuplesort stats from each batch into
                               3175                 :                :  * an intelligible summary.
                               3176                 :                :  *
                               3177                 :                :  * This function is used for both a non-parallel node and each worker in a
                               3178                 :                :  * parallel incremental sort node.
                               3179                 :                :  */
                               3180                 :                : static void
 2080 tomas.vondra@postgre     3181                 :             27 : show_incremental_sort_group_info(IncrementalSortGroupInfo *groupInfo,
                               3182                 :                :                                  const char *groupLabel, bool indent, ExplainState *es)
                               3183                 :                : {
                               3184                 :                :     ListCell   *methodCell;
                               3185                 :             27 :     List       *methodNames = NIL;
                               3186                 :                : 
                               3187                 :                :     /* Generate a list of sort methods used across all groups. */
      tgl@sss.pgh.pa.us        3188         [ +  + ]:            135 :     for (int bit = 0; bit < NUM_TUPLESORTMETHODS; bit++)
                               3189                 :                :     {
                               3190                 :            108 :         TuplesortMethod sortMethod = (1 << bit);
                               3191                 :                : 
                               3192         [ +  + ]:            108 :         if (groupInfo->sortMethods & sortMethod)
                               3193                 :                :         {
                               3194                 :             45 :             const char *methodName = tuplesort_method_name(sortMethod);
                               3195                 :                : 
      tomas.vondra@postgre     3196                 :             45 :             methodNames = lappend(methodNames, unconstify(char *, methodName));
                               3197                 :                :         }
                               3198                 :                :     }
                               3199                 :                : 
                               3200         [ +  + ]:             27 :     if (es->format == EXPLAIN_FORMAT_TEXT)
                               3201                 :                :     {
                               3202         [ +  - ]:              9 :         if (indent)
                               3203                 :              9 :             appendStringInfoSpaces(es->str, es->indent * 2);
 2044                          3204                 :              9 :         appendStringInfo(es->str, "%s Groups: " INT64_FORMAT "  Sort Method", groupLabel,
                               3205                 :                :                          groupInfo->groupCount);
                               3206                 :                :         /* plural/singular based on methodNames size */
 2080                          3207         [ +  + ]:              9 :         if (list_length(methodNames) > 1)
 1888 drowley@postgresql.o     3208                 :              6 :             appendStringInfoString(es->str, "s: ");
                               3209                 :                :         else
                               3210                 :              3 :             appendStringInfoString(es->str, ": ");
 2080 tomas.vondra@postgre     3211   [ +  -  +  +  :             24 :         foreach(methodCell, methodNames)
                                              +  + ]
                               3212                 :                :         {
 1888 drowley@postgresql.o     3213                 :             15 :             appendStringInfoString(es->str, (char *) methodCell->ptr_value);
 2080 tomas.vondra@postgre     3214         [ +  + ]:             15 :             if (foreach_current_index(methodCell) < list_length(methodNames) - 1)
 1888 drowley@postgresql.o     3215                 :              6 :                 appendStringInfoString(es->str, ", ");
                               3216                 :                :         }
                               3217                 :                : 
 2080 tomas.vondra@postgre     3218         [ +  - ]:              9 :         if (groupInfo->maxMemorySpaceUsed > 0)
                               3219                 :                :         {
 1962 drowley@postgresql.o     3220                 :              9 :             int64       avgSpace = groupInfo->totalMemorySpaceUsed / groupInfo->groupCount;
                               3221                 :                :             const char *spaceTypeName;
                               3222                 :                : 
 2080 tomas.vondra@postgre     3223                 :              9 :             spaceTypeName = tuplesort_space_type_name(SORT_SPACE_TYPE_MEMORY);
 1962 drowley@postgresql.o     3224                 :              9 :             appendStringInfo(es->str, "  Average %s: " INT64_FORMAT "kB  Peak %s: " INT64_FORMAT "kB",
                               3225                 :                :                              spaceTypeName, avgSpace,
                               3226                 :                :                              spaceTypeName, groupInfo->maxMemorySpaceUsed);
                               3227                 :                :         }
                               3228                 :                : 
 2080 tomas.vondra@postgre     3229         [ -  + ]:              9 :         if (groupInfo->maxDiskSpaceUsed > 0)
                               3230                 :                :         {
 1962 drowley@postgresql.o     3231                 :UBC           0 :             int64       avgSpace = groupInfo->totalDiskSpaceUsed / groupInfo->groupCount;
                               3232                 :                : 
                               3233                 :                :             const char *spaceTypeName;
                               3234                 :                : 
 2080 tomas.vondra@postgre     3235                 :              0 :             spaceTypeName = tuplesort_space_type_name(SORT_SPACE_TYPE_DISK);
 1962 drowley@postgresql.o     3236                 :              0 :             appendStringInfo(es->str, "  Average %s: " INT64_FORMAT "kB  Peak %s: " INT64_FORMAT "kB",
                               3237                 :                :                              spaceTypeName, avgSpace,
                               3238                 :                :                              spaceTypeName, groupInfo->maxDiskSpaceUsed);
                               3239                 :                :         }
                               3240                 :                :     }
                               3241                 :                :     else
                               3242                 :                :     {
                               3243                 :                :         StringInfoData groupName;
                               3244                 :                : 
 2080 tomas.vondra@postgre     3245                 :CBC          18 :         initStringInfo(&groupName);
                               3246                 :             18 :         appendStringInfo(&groupName, "%s Groups", groupLabel);
                               3247                 :             18 :         ExplainOpenGroup("Incremental Sort Groups", groupName.data, true, es);
                               3248                 :             18 :         ExplainPropertyInteger("Group Count", NULL, groupInfo->groupCount, es);
                               3249                 :                : 
                               3250                 :             18 :         ExplainPropertyList("Sort Methods Used", methodNames, es);
                               3251                 :                : 
                               3252         [ +  - ]:             18 :         if (groupInfo->maxMemorySpaceUsed > 0)
                               3253                 :                :         {
 1962 drowley@postgresql.o     3254                 :             18 :             int64       avgSpace = groupInfo->totalMemorySpaceUsed / groupInfo->groupCount;
                               3255                 :                :             const char *spaceTypeName;
                               3256                 :                :             StringInfoData memoryName;
                               3257                 :                : 
 2080 tomas.vondra@postgre     3258                 :             18 :             spaceTypeName = tuplesort_space_type_name(SORT_SPACE_TYPE_MEMORY);
                               3259                 :             18 :             initStringInfo(&memoryName);
                               3260                 :             18 :             appendStringInfo(&memoryName, "Sort Space %s", spaceTypeName);
                               3261                 :             18 :             ExplainOpenGroup("Sort Space", memoryName.data, true, es);
                               3262                 :                : 
                               3263                 :             18 :             ExplainPropertyInteger("Average Sort Space Used", "kB", avgSpace, es);
 2079                          3264                 :             18 :             ExplainPropertyInteger("Peak Sort Space Used", "kB",
                               3265                 :                :                                    groupInfo->maxMemorySpaceUsed, es);
                               3266                 :                : 
 1880 tgl@sss.pgh.pa.us        3267                 :             18 :             ExplainCloseGroup("Sort Space", memoryName.data, true, es);
                               3268                 :                :         }
 2080 tomas.vondra@postgre     3269         [ -  + ]:             18 :         if (groupInfo->maxDiskSpaceUsed > 0)
                               3270                 :                :         {
 1962 drowley@postgresql.o     3271                 :UBC           0 :             int64       avgSpace = groupInfo->totalDiskSpaceUsed / groupInfo->groupCount;
                               3272                 :                :             const char *spaceTypeName;
                               3273                 :                :             StringInfoData diskName;
                               3274                 :                : 
 2080 tomas.vondra@postgre     3275                 :              0 :             spaceTypeName = tuplesort_space_type_name(SORT_SPACE_TYPE_DISK);
                               3276                 :              0 :             initStringInfo(&diskName);
                               3277                 :              0 :             appendStringInfo(&diskName, "Sort Space %s", spaceTypeName);
                               3278                 :              0 :             ExplainOpenGroup("Sort Space", diskName.data, true, es);
                               3279                 :                : 
                               3280                 :              0 :             ExplainPropertyInteger("Average Sort Space Used", "kB", avgSpace, es);
 2079                          3281                 :              0 :             ExplainPropertyInteger("Peak Sort Space Used", "kB",
                               3282                 :                :                                    groupInfo->maxDiskSpaceUsed, es);
                               3283                 :                : 
 1880 tgl@sss.pgh.pa.us        3284                 :              0 :             ExplainCloseGroup("Sort Space", diskName.data, true, es);
                               3285                 :                :         }
                               3286                 :                : 
 2080 tomas.vondra@postgre     3287                 :CBC          18 :         ExplainCloseGroup("Incremental Sort Groups", groupName.data, true, es);
                               3288                 :                :     }
                               3289                 :             27 : }
                               3290                 :                : 
                               3291                 :                : /*
                               3292                 :                :  * If it's EXPLAIN ANALYZE, show tuplesort stats for an incremental sort node
                               3293                 :                :  */
                               3294                 :                : static void
                               3295                 :            196 : show_incremental_sort_info(IncrementalSortState *incrsortstate,
                               3296                 :                :                            ExplainState *es)
                               3297                 :                : {
                               3298                 :                :     IncrementalSortGroupInfo *fullsortGroupInfo;
                               3299                 :                :     IncrementalSortGroupInfo *prefixsortGroupInfo;
                               3300                 :                : 
                               3301                 :            196 :     fullsortGroupInfo = &incrsortstate->incsort_info.fullsortGroupInfo;
                               3302                 :                : 
                               3303         [ +  + ]:            196 :     if (!es->analyze)
                               3304                 :            178 :         return;
                               3305                 :                : 
                               3306                 :                :     /*
                               3307                 :                :      * Since we never have any prefix groups unless we've first sorted a full
                               3308                 :                :      * groups and transitioned modes (copying the tuples into a prefix group),
                               3309                 :                :      * we don't need to do anything if there were 0 full groups.
                               3310                 :                :      *
                               3311                 :                :      * We still have to continue after this block if there are no full groups,
                               3312                 :                :      * though, since it's possible that we have workers that did real work
                               3313                 :                :      * even if the leader didn't participate.
                               3314                 :                :      */
                               3315         [ +  - ]:             18 :     if (fullsortGroupInfo->groupCount > 0)
                               3316                 :                :     {
                               3317                 :             18 :         show_incremental_sort_group_info(fullsortGroupInfo, "Full-sort", true, es);
                               3318                 :             18 :         prefixsortGroupInfo = &incrsortstate->incsort_info.prefixsortGroupInfo;
                               3319         [ +  + ]:             18 :         if (prefixsortGroupInfo->groupCount > 0)
                               3320                 :                :         {
                               3321         [ +  + ]:              9 :             if (es->format == EXPLAIN_FORMAT_TEXT)
 1888 drowley@postgresql.o     3322                 :              3 :                 appendStringInfoChar(es->str, '\n');
 2044 tomas.vondra@postgre     3323                 :              9 :             show_incremental_sort_group_info(prefixsortGroupInfo, "Pre-sorted", true, es);
                               3324                 :                :         }
 2080                          3325         [ +  + ]:             18 :         if (es->format == EXPLAIN_FORMAT_TEXT)
 1888 drowley@postgresql.o     3326                 :              6 :             appendStringInfoChar(es->str, '\n');
                               3327                 :                :     }
                               3328                 :                : 
 2080 tomas.vondra@postgre     3329         [ -  + ]:             18 :     if (incrsortstate->shared_info != NULL)
                               3330                 :                :     {
                               3331                 :                :         int         n;
                               3332                 :                :         bool        indent_first_line;
                               3333                 :                : 
 2080 tomas.vondra@postgre     3334         [ #  # ]:UBC           0 :         for (n = 0; n < incrsortstate->shared_info->num_workers; n++)
                               3335                 :                :         {
                               3336                 :              0 :             IncrementalSortInfo *incsort_info =
  942 tgl@sss.pgh.pa.us        3337                 :              0 :                 &incrsortstate->shared_info->sinfo[n];
                               3338                 :                : 
                               3339                 :                :             /*
                               3340                 :                :              * If a worker hasn't processed any sort groups at all, then
                               3341                 :                :              * exclude it from output since it either didn't launch or didn't
                               3342                 :                :              * contribute anything meaningful.
                               3343                 :                :              */
 2080 tomas.vondra@postgre     3344                 :              0 :             fullsortGroupInfo = &incsort_info->fullsortGroupInfo;
                               3345                 :                : 
                               3346                 :                :             /*
                               3347                 :                :              * Since we never have any prefix groups unless we've first sorted
                               3348                 :                :              * a full groups and transitioned modes (copying the tuples into a
                               3349                 :                :              * prefix group), we don't need to do anything if there were 0
                               3350                 :                :              * full groups.
                               3351                 :                :              */
 2047                          3352         [ #  # ]:              0 :             if (fullsortGroupInfo->groupCount == 0)
 2080                          3353                 :              0 :                 continue;
                               3354                 :                : 
                               3355         [ #  # ]:              0 :             if (es->workers_state)
                               3356                 :              0 :                 ExplainOpenWorker(n, es);
                               3357                 :                : 
                               3358   [ #  #  #  # ]:              0 :             indent_first_line = es->workers_state == NULL || es->verbose;
                               3359                 :              0 :             show_incremental_sort_group_info(fullsortGroupInfo, "Full-sort",
                               3360                 :                :                                              indent_first_line, es);
 2047                          3361                 :              0 :             prefixsortGroupInfo = &incsort_info->prefixsortGroupInfo;
 2080                          3362         [ #  # ]:              0 :             if (prefixsortGroupInfo->groupCount > 0)
                               3363                 :                :             {
                               3364         [ #  # ]:              0 :                 if (es->format == EXPLAIN_FORMAT_TEXT)
 1888 drowley@postgresql.o     3365                 :              0 :                     appendStringInfoChar(es->str, '\n');
 2044 tomas.vondra@postgre     3366                 :              0 :                 show_incremental_sort_group_info(prefixsortGroupInfo, "Pre-sorted", true, es);
                               3367                 :                :             }
 2080                          3368         [ #  # ]:              0 :             if (es->format == EXPLAIN_FORMAT_TEXT)
 1888 drowley@postgresql.o     3369                 :              0 :                 appendStringInfoChar(es->str, '\n');
                               3370                 :                : 
 2080 tomas.vondra@postgre     3371         [ #  # ]:              0 :             if (es->workers_state)
                               3372                 :              0 :                 ExplainCloseWorker(n, es);
                               3373                 :                :         }
                               3374                 :                :     }
                               3375                 :                : }
                               3376                 :                : 
                               3377                 :                : /*
                               3378                 :                :  * Show information on hash buckets/batches.
                               3379                 :                :  */
                               3380                 :                : static void
 5797 rhaas@postgresql.org     3381                 :CBC        2143 : show_hash_info(HashState *hashstate, ExplainState *es)
                               3382                 :                : {
 2906 andres@anarazel.de       3383                 :           2143 :     HashInstrumentation hinstrument = {0};
                               3384                 :                : 
                               3385                 :                :     /*
                               3386                 :                :      * Collect stats from the local process, even when it's a parallel query.
                               3387                 :                :      * In a parallel query, the leader process may or may not have run the
                               3388                 :                :      * hash join, and even if it did it may not have built a hash table due to
                               3389                 :                :      * timing (if it started late it might have seen no tuples in the outer
                               3390                 :                :      * relation and skipped building the hash table).  Therefore we have to be
                               3391                 :                :      * prepared to get instrumentation data from all participants.
                               3392                 :                :      */
 2075 tgl@sss.pgh.pa.us        3393         [ +  + ]:           2143 :     if (hashstate->hinstrument)
                               3394                 :             60 :         memcpy(&hinstrument, hashstate->hinstrument,
                               3395                 :                :                sizeof(HashInstrumentation));
                               3396                 :                : 
                               3397                 :                :     /*
                               3398                 :                :      * Merge results from workers.  In the parallel-oblivious case, the
                               3399                 :                :      * results from all participants should be identical, except where
                               3400                 :                :      * participants didn't run the join at all so have no data.  In the
                               3401                 :                :      * parallel-aware case, we need to consider all the results.  Each worker
                               3402                 :                :      * may have seen a different subset of batches and we want to report the
                               3403                 :                :      * highest memory usage across all batches.  We take the maxima of other
                               3404                 :                :      * values too, for the same reasons as in ExecHashAccumInstrumentation.
                               3405                 :                :      */
 2906 andres@anarazel.de       3406         [ +  + ]:           2143 :     if (hashstate->shared_info)
                               3407                 :                :     {
 2933                          3408                 :             42 :         SharedHashInfo *shared_info = hashstate->shared_info;
                               3409                 :                :         int         i;
                               3410                 :                : 
                               3411         [ +  + ]:            120 :         for (i = 0; i < shared_info->num_workers; ++i)
                               3412                 :                :         {
 2906                          3413                 :             78 :             HashInstrumentation *worker_hi = &shared_info->hinstrument[i];
                               3414                 :                : 
 2075 tgl@sss.pgh.pa.us        3415                 :             78 :             hinstrument.nbuckets = Max(hinstrument.nbuckets,
                               3416                 :                :                                        worker_hi->nbuckets);
                               3417                 :             78 :             hinstrument.nbuckets_original = Max(hinstrument.nbuckets_original,
                               3418                 :                :                                                 worker_hi->nbuckets_original);
                               3419                 :             78 :             hinstrument.nbatch = Max(hinstrument.nbatch,
                               3420                 :                :                                      worker_hi->nbatch);
                               3421                 :             78 :             hinstrument.nbatch_original = Max(hinstrument.nbatch_original,
                               3422                 :                :                                               worker_hi->nbatch_original);
                               3423                 :             78 :             hinstrument.space_peak = Max(hinstrument.space_peak,
                               3424                 :                :                                          worker_hi->space_peak);
                               3425                 :                :         }
                               3426                 :                :     }
                               3427                 :                : 
 2906 andres@anarazel.de       3428         [ +  + ]:           2143 :     if (hinstrument.nbatch > 0)
                               3429                 :                :     {
  579 drowley@postgresql.o     3430                 :             60 :         uint64      spacePeakKb = BYTES_TO_KILOBYTES(hinstrument.space_peak);
                               3431                 :                : 
 5797 rhaas@postgresql.org     3432         [ +  + ]:             60 :         if (es->format != EXPLAIN_FORMAT_TEXT)
                               3433                 :                :         {
 2832 andres@anarazel.de       3434                 :             54 :             ExplainPropertyInteger("Hash Buckets", NULL,
                               3435                 :             54 :                                    hinstrument.nbuckets, es);
                               3436                 :             54 :             ExplainPropertyInteger("Original Hash Buckets", NULL,
                               3437                 :             54 :                                    hinstrument.nbuckets_original, es);
                               3438                 :             54 :             ExplainPropertyInteger("Hash Batches", NULL,
                               3439                 :             54 :                                    hinstrument.nbatch, es);
                               3440                 :             54 :             ExplainPropertyInteger("Original Hash Batches", NULL,
                               3441                 :             54 :                                    hinstrument.nbatch_original, es);
  579 drowley@postgresql.o     3442                 :             54 :             ExplainPropertyUInteger("Peak Memory Usage", "kB",
                               3443                 :                :                                     spacePeakKb, es);
                               3444                 :                :         }
 2906 andres@anarazel.de       3445         [ +  - ]:              6 :         else if (hinstrument.nbatch_original != hinstrument.nbatch ||
                               3446         [ -  + ]:              6 :                  hinstrument.nbuckets_original != hinstrument.nbuckets)
                               3447                 :                :         {
 2152 tgl@sss.pgh.pa.us        3448                 :UBC           0 :             ExplainIndentText(es);
 5797 rhaas@postgresql.org     3449                 :              0 :             appendStringInfo(es->str,
                               3450                 :                :                              "Buckets: %d (originally %d)  Batches: %d (originally %d)  Memory Usage: " UINT64_FORMAT "kB\n",
                               3451                 :                :                              hinstrument.nbuckets,
                               3452                 :                :                              hinstrument.nbuckets_original,
                               3453                 :                :                              hinstrument.nbatch,
                               3454                 :                :                              hinstrument.nbatch_original,
                               3455                 :                :                              spacePeakKb);
                               3456                 :                :         }
                               3457                 :                :         else
                               3458                 :                :         {
 2152 tgl@sss.pgh.pa.us        3459                 :CBC           6 :             ExplainIndentText(es);
 5797 rhaas@postgresql.org     3460                 :              6 :             appendStringInfo(es->str,
                               3461                 :                :                              "Buckets: %d  Batches: %d  Memory Usage: " UINT64_FORMAT "kB\n",
                               3462                 :                :                              hinstrument.nbuckets, hinstrument.nbatch,
                               3463                 :                :                              spacePeakKb);
                               3464                 :                :         }
                               3465                 :                :     }
                               3466                 :           2143 : }
                               3467                 :                : 
                               3468                 :                : /*
                               3469                 :                :  * Show information on material node, storage method and maximum memory/disk
                               3470                 :                :  * space used.
                               3471                 :                :  */
                               3472                 :                : static void
  529 drowley@postgresql.o     3473                 :            553 : show_material_info(MaterialState *mstate, ExplainState *es)
                               3474                 :                : {
                               3475                 :                :     char       *maxStorageType;
                               3476                 :                :     int64       maxSpaceUsed;
                               3477                 :                : 
                               3478                 :            553 :     Tuplestorestate *tupstore = mstate->tuplestorestate;
                               3479                 :                : 
                               3480                 :                :     /*
                               3481                 :                :      * Nothing to show if ANALYZE option wasn't used or if execution didn't
                               3482                 :                :      * get as far as creating the tuplestore.
                               3483                 :                :      */
                               3484   [ +  +  -  + ]:            553 :     if (!es->analyze || tupstore == NULL)
                               3485                 :            547 :         return;
                               3486                 :                : 
  449 ishii@postgresql.org     3487                 :              6 :     tuplestore_get_stats(tupstore, &maxStorageType, &maxSpaceUsed);
                               3488                 :              6 :     show_storage_info(maxStorageType, maxSpaceUsed, es);
                               3489                 :                : }
                               3490                 :                : 
                               3491                 :                : /*
                               3492                 :                :  * Show information on WindowAgg node, storage method and maximum memory/disk
                               3493                 :                :  * space used.
                               3494                 :                :  */
                               3495                 :                : static void
  455                          3496                 :            235 : show_windowagg_info(WindowAggState *winstate, ExplainState *es)
                               3497                 :                : {
                               3498                 :                :     char       *maxStorageType;
                               3499                 :                :     int64       maxSpaceUsed;
                               3500                 :                : 
                               3501                 :            235 :     Tuplestorestate *tupstore = winstate->buffer;
                               3502                 :                : 
                               3503                 :                :     /*
                               3504                 :                :      * Nothing to show if ANALYZE option wasn't used or if execution didn't
                               3505                 :                :      * get as far as creating the tuplestore.
                               3506                 :                :      */
                               3507   [ +  +  -  + ]:            235 :     if (!es->analyze || tupstore == NULL)
                               3508                 :            226 :         return;
                               3509                 :                : 
  449                          3510                 :              9 :     tuplestore_get_stats(tupstore, &maxStorageType, &maxSpaceUsed);
                               3511                 :              9 :     show_storage_info(maxStorageType, maxSpaceUsed, es);
                               3512                 :                : }
                               3513                 :                : 
                               3514                 :                : /*
                               3515                 :                :  * Show information on CTE Scan node, storage method and maximum memory/disk
                               3516                 :                :  * space used.
                               3517                 :                :  */
                               3518                 :                : static void
                               3519                 :            125 : show_ctescan_info(CteScanState *ctescanstate, ExplainState *es)
                               3520                 :                : {
                               3521                 :                :     char       *maxStorageType;
                               3522                 :                :     int64       maxSpaceUsed;
                               3523                 :                : 
                               3524                 :            125 :     Tuplestorestate *tupstore = ctescanstate->leader->cte_table;
                               3525                 :                : 
                               3526   [ -  +  -  - ]:            125 :     if (!es->analyze || tupstore == NULL)
                               3527                 :            125 :         return;
                               3528                 :                : 
  449 ishii@postgresql.org     3529                 :UBC           0 :     tuplestore_get_stats(tupstore, &maxStorageType, &maxSpaceUsed);
                               3530                 :              0 :     show_storage_info(maxStorageType, maxSpaceUsed, es);
                               3531                 :                : }
                               3532                 :                : 
                               3533                 :                : /*
                               3534                 :                :  * Show information on Table Function Scan node, storage method and maximum
                               3535                 :                :  * memory/disk space used.
                               3536                 :                :  */
                               3537                 :                : static void
  449 ishii@postgresql.org     3538                 :CBC          39 : show_table_func_scan_info(TableFuncScanState *tscanstate, ExplainState *es)
                               3539                 :                : {
                               3540                 :                :     char       *maxStorageType;
                               3541                 :                :     int64       maxSpaceUsed;
                               3542                 :                : 
                               3543                 :             39 :     Tuplestorestate *tupstore = tscanstate->tupstore;
                               3544                 :                : 
                               3545   [ -  +  -  - ]:             39 :     if (!es->analyze || tupstore == NULL)
                               3546                 :             39 :         return;
                               3547                 :                : 
  449 ishii@postgresql.org     3548                 :UBC           0 :     tuplestore_get_stats(tupstore, &maxStorageType, &maxSpaceUsed);
                               3549                 :              0 :     show_storage_info(maxStorageType, maxSpaceUsed, es);
                               3550                 :                : }
                               3551                 :                : 
                               3552                 :                : /*
                               3553                 :                :  * Show information on Recursive Union node, storage method and maximum
                               3554                 :                :  * memory/disk space used.
                               3555                 :                :  */
                               3556                 :                : static void
  449 ishii@postgresql.org     3557                 :CBC          27 : show_recursive_union_info(RecursiveUnionState *rstate, ExplainState *es)
                               3558                 :                : {
                               3559                 :                :     char       *maxStorageType,
                               3560                 :                :                *tempStorageType;
                               3561                 :                :     int64       maxSpaceUsed,
                               3562                 :                :                 tempSpaceUsed;
                               3563                 :                : 
                               3564         [ +  - ]:             27 :     if (!es->analyze)
                               3565                 :             27 :         return;
                               3566                 :                : 
                               3567                 :                :     /*
                               3568                 :                :      * Recursive union node uses two tuplestores.  We employ the storage type
                               3569                 :                :      * from one of them which consumed more memory/disk than the other.  The
                               3570                 :                :      * storage size is sum of the two.
                               3571                 :                :      */
  449 ishii@postgresql.org     3572                 :UBC           0 :     tuplestore_get_stats(rstate->working_table, &tempStorageType,
                               3573                 :                :                          &tempSpaceUsed);
                               3574                 :              0 :     tuplestore_get_stats(rstate->intermediate_table, &maxStorageType,
                               3575                 :                :                          &maxSpaceUsed);
                               3576                 :                : 
                               3577         [ #  # ]:              0 :     if (tempSpaceUsed > maxSpaceUsed)
                               3578                 :              0 :         maxStorageType = tempStorageType;
                               3579                 :                : 
                               3580                 :              0 :     maxSpaceUsed += tempSpaceUsed;
                               3581                 :              0 :     show_storage_info(maxStorageType, maxSpaceUsed, es);
                               3582                 :                : }
                               3583                 :                : 
                               3584                 :                : /*
                               3585                 :                :  * Show information on memoize hits/misses/evictions and memory usage.
                               3586                 :                :  */
                               3587                 :                : static void
 1616 drowley@postgresql.o     3588                 :CBC         177 : show_memoize_info(MemoizeState *mstate, List *ancestors, ExplainState *es)
                               3589                 :                : {
                               3590                 :            177 :     Plan       *plan = ((PlanState *) mstate)->plan;
  140 drowley@postgresql.o     3591                 :GNC         177 :     Memoize    *mplan = (Memoize *) plan;
                               3592                 :                :     ListCell   *lc;
                               3593                 :                :     List       *context;
                               3594                 :                :     StringInfoData keystr;
 1372 michael@paquier.xyz      3595                 :CBC         177 :     char       *separator = "";
                               3596                 :                :     bool        useprefix;
                               3597                 :                :     int64       memPeakKb;
                               3598                 :                : 
 1719 drowley@postgresql.o     3599                 :            177 :     initStringInfo(&keystr);
                               3600                 :                : 
                               3601                 :                :     /*
                               3602                 :                :      * It's hard to imagine having a memoize node with fewer than 2 RTEs, but
                               3603                 :                :      * let's just keep the same useprefix logic as elsewhere in this file.
                               3604                 :                :      */
  462 rguo@postgresql.org      3605   [ -  +  -  - ]:            177 :     useprefix = es->rtable_size > 1 || es->verbose;
                               3606                 :                : 
                               3607                 :                :     /* Set up deparsing context */
 1719 drowley@postgresql.o     3608                 :            177 :     context = set_deparse_context_plan(es->deparse_cxt,
                               3609                 :                :                                        plan,
                               3610                 :                :                                        ancestors);
                               3611                 :                : 
  140 drowley@postgresql.o     3612   [ +  -  +  +  :GNC         372 :     foreach(lc, mplan->param_exprs)
                                              +  + ]
                               3613                 :                :     {
 1719 drowley@postgresql.o     3614                 :CBC         195 :         Node       *expr = (Node *) lfirst(lc);
                               3615                 :                : 
 1372 michael@paquier.xyz      3616                 :            195 :         appendStringInfoString(&keystr, separator);
                               3617                 :                : 
 1719 drowley@postgresql.o     3618                 :            195 :         appendStringInfoString(&keystr, deparse_expression(expr, context,
                               3619                 :                :                                                            useprefix, false));
 1372 michael@paquier.xyz      3620                 :            195 :         separator = ", ";
                               3621                 :                :     }
                               3622                 :                : 
  270 drowley@postgresql.o     3623                 :            177 :     ExplainPropertyText("Cache Key", keystr.data, es);
                               3624         [ +  + ]:            177 :     ExplainPropertyText("Cache Mode", mstate->binary_mode ? "binary" : "logical", es);
                               3625                 :                : 
 1719                          3626                 :            177 :     pfree(keystr.data);
                               3627                 :                : 
  140 drowley@postgresql.o     3628         [ +  + ]:GNC         177 :     if (es->costs)
                               3629                 :                :     {
                               3630         [ +  - ]:             58 :         if (es->format == EXPLAIN_FORMAT_TEXT)
                               3631                 :                :         {
                               3632                 :             58 :             ExplainIndentText(es);
                               3633                 :             58 :             appendStringInfo(es->str, "Estimates: capacity=%u distinct keys=%.0f lookups=%.0f hit percent=%.2f%%\n",
                               3634                 :                :                              mplan->est_entries, mplan->est_unique_keys,
                               3635                 :             58 :                              mplan->est_calls, mplan->est_hit_ratio * 100.0);
                               3636                 :                :         }
                               3637                 :                :         else
                               3638                 :                :         {
  140 drowley@postgresql.o     3639                 :UNC           0 :             ExplainPropertyUInteger("Estimated Capacity", NULL, mplan->est_entries, es);
                               3640                 :              0 :             ExplainPropertyFloat("Estimated Distinct Lookup Keys", NULL, mplan->est_unique_keys, 0, es);
                               3641                 :              0 :             ExplainPropertyFloat("Estimated Lookups", NULL, mplan->est_calls, 0, es);
                               3642                 :              0 :             ExplainPropertyFloat("Estimated Hit Percent", NULL, mplan->est_hit_ratio * 100.0, 2, es);
                               3643                 :                :         }
                               3644                 :                :     }
                               3645                 :                : 
 1719 drowley@postgresql.o     3646         [ +  + ]:CBC         177 :     if (!es->analyze)
                               3647                 :            177 :         return;
                               3648                 :                : 
 1616                          3649         [ +  - ]:             45 :     if (mstate->stats.cache_misses > 0)
                               3650                 :                :     {
                               3651                 :                :         /*
                               3652                 :                :          * mem_peak is only set when we freed memory, so we must use mem_used
                               3653                 :                :          * when mem_peak is 0.
                               3654                 :                :          */
                               3655         [ +  + ]:             45 :         if (mstate->stats.mem_peak > 0)
  579                          3656                 :              3 :             memPeakKb = BYTES_TO_KILOBYTES(mstate->stats.mem_peak);
                               3657                 :                :         else
                               3658                 :             42 :             memPeakKb = BYTES_TO_KILOBYTES(mstate->mem_used);
                               3659                 :                : 
 1719                          3660         [ -  + ]:             45 :         if (es->format != EXPLAIN_FORMAT_TEXT)
                               3661                 :                :         {
 1616 drowley@postgresql.o     3662                 :UBC           0 :             ExplainPropertyInteger("Cache Hits", NULL, mstate->stats.cache_hits, es);
                               3663                 :              0 :             ExplainPropertyInteger("Cache Misses", NULL, mstate->stats.cache_misses, es);
                               3664                 :              0 :             ExplainPropertyInteger("Cache Evictions", NULL, mstate->stats.cache_evictions, es);
                               3665                 :              0 :             ExplainPropertyInteger("Cache Overflows", NULL, mstate->stats.cache_overflows, es);
 1719                          3666                 :              0 :             ExplainPropertyInteger("Peak Memory Usage", "kB", memPeakKb, es);
                               3667                 :                :         }
                               3668                 :                :         else
                               3669                 :                :         {
 1719 drowley@postgresql.o     3670                 :CBC          45 :             ExplainIndentText(es);
                               3671                 :             45 :             appendStringInfo(es->str,
                               3672                 :                :                              "Hits: " UINT64_FORMAT "  Misses: " UINT64_FORMAT "  Evictions: " UINT64_FORMAT "  Overflows: " UINT64_FORMAT "  Memory Usage: " INT64_FORMAT "kB\n",
                               3673                 :                :                              mstate->stats.cache_hits,
                               3674                 :                :                              mstate->stats.cache_misses,
                               3675                 :                :                              mstate->stats.cache_evictions,
                               3676                 :                :                              mstate->stats.cache_overflows,
                               3677                 :                :                              memPeakKb);
                               3678                 :                :         }
                               3679                 :                :     }
                               3680                 :                : 
 1616                          3681         [ +  - ]:             45 :     if (mstate->shared_info == NULL)
 1719                          3682                 :             45 :         return;
                               3683                 :                : 
                               3684                 :                :     /* Show details from parallel workers */
 1616 drowley@postgresql.o     3685         [ #  # ]:UBC           0 :     for (int n = 0; n < mstate->shared_info->num_workers; n++)
                               3686                 :                :     {
                               3687                 :                :         MemoizeInstrumentation *si;
                               3688                 :                : 
                               3689                 :              0 :         si = &mstate->shared_info->sinstrument[n];
                               3690                 :                : 
                               3691                 :                :         /*
                               3692                 :                :          * Skip workers that didn't do any work.  We needn't bother checking
                               3693                 :                :          * for cache hits as a miss will always occur before a cache hit.
                               3694                 :                :          */
 1691                          3695         [ #  # ]:              0 :         if (si->cache_misses == 0)
                               3696                 :              0 :             continue;
                               3697                 :                : 
 1719                          3698         [ #  # ]:              0 :         if (es->workers_state)
                               3699                 :              0 :             ExplainOpenWorker(n, es);
                               3700                 :                : 
                               3701                 :                :         /*
                               3702                 :                :          * Since the worker's MemoizeState.mem_used field is unavailable to
                               3703                 :                :          * us, ExecEndMemoize will have set the
                               3704                 :                :          * MemoizeInstrumentation.mem_peak field for us.  No need to do the
                               3705                 :                :          * zero checks like we did for the serial case above.
                               3706                 :                :          */
  579                          3707                 :              0 :         memPeakKb = BYTES_TO_KILOBYTES(si->mem_peak);
                               3708                 :                : 
 1719                          3709         [ #  # ]:              0 :         if (es->format == EXPLAIN_FORMAT_TEXT)
                               3710                 :                :         {
                               3711                 :              0 :             ExplainIndentText(es);
                               3712                 :              0 :             appendStringInfo(es->str,
                               3713                 :                :                              "Hits: " UINT64_FORMAT "  Misses: " UINT64_FORMAT "  Evictions: " UINT64_FORMAT "  Overflows: " UINT64_FORMAT "  Memory Usage: " INT64_FORMAT "kB\n",
                               3714                 :                :                              si->cache_hits, si->cache_misses,
                               3715                 :                :                              si->cache_evictions, si->cache_overflows,
                               3716                 :                :                              memPeakKb);
                               3717                 :                :         }
                               3718                 :                :         else
                               3719                 :                :         {
                               3720                 :              0 :             ExplainPropertyInteger("Cache Hits", NULL,
                               3721                 :              0 :                                    si->cache_hits, es);
                               3722                 :              0 :             ExplainPropertyInteger("Cache Misses", NULL,
                               3723                 :              0 :                                    si->cache_misses, es);
                               3724                 :              0 :             ExplainPropertyInteger("Cache Evictions", NULL,
                               3725                 :              0 :                                    si->cache_evictions, es);
                               3726                 :              0 :             ExplainPropertyInteger("Cache Overflows", NULL,
                               3727                 :              0 :                                    si->cache_overflows, es);
                               3728                 :              0 :             ExplainPropertyInteger("Peak Memory Usage", "kB", memPeakKb,
                               3729                 :                :                                    es);
                               3730                 :                :         }
                               3731                 :                : 
                               3732         [ #  # ]:              0 :         if (es->workers_state)
                               3733                 :              0 :             ExplainCloseWorker(n, es);
                               3734                 :                :     }
                               3735                 :                : }
                               3736                 :                : 
                               3737                 :                : /*
                               3738                 :                :  * Show information on hash aggregate memory usage and batches.
                               3739                 :                :  */
                               3740                 :                : static void
 2099 jdavis@postgresql.or     3741                 :CBC        5230 : show_hashagg_info(AggState *aggstate, ExplainState *es)
                               3742                 :                : {
 2042 tgl@sss.pgh.pa.us        3743                 :           5230 :     Agg        *agg = (Agg *) aggstate->ss.ps.plan;
  579 drowley@postgresql.o     3744                 :           5230 :     int64       memPeakKb = BYTES_TO_KILOBYTES(aggstate->hash_mem_peak);
                               3745                 :                : 
 2099 jdavis@postgresql.or     3746         [ +  + ]:           5230 :     if (agg->aggstrategy != AGG_HASHED &&
                               3747         [ +  + ]:           4060 :         agg->aggstrategy != AGG_MIXED)
                               3748                 :           4010 :         return;
                               3749                 :                : 
 2006 drowley@postgresql.o     3750         [ -  + ]:           1220 :     if (es->format != EXPLAIN_FORMAT_TEXT)
                               3751                 :                :     {
 1966 drowley@postgresql.o     3752         [ #  # ]:UBC           0 :         if (es->costs)
 2006                          3753                 :              0 :             ExplainPropertyInteger("Planned Partitions", NULL,
                               3754                 :              0 :                                    aggstate->hash_planned_partitions, es);
                               3755                 :                : 
                               3756                 :                :         /*
                               3757                 :                :          * During parallel query the leader may have not helped out.  We
                               3758                 :                :          * detect this by checking how much memory it used.  If we find it
                               3759                 :                :          * didn't do any work then we don't show its properties.
                               3760                 :                :          */
 1957                          3761   [ #  #  #  # ]:              0 :         if (es->analyze && aggstate->hash_mem_peak > 0)
                               3762                 :                :         {
                               3763                 :              0 :             ExplainPropertyInteger("HashAgg Batches", NULL,
                               3764                 :              0 :                                    aggstate->hash_batches_used, es);
                               3765                 :              0 :             ExplainPropertyInteger("Peak Memory Usage", "kB", memPeakKb, es);
                               3766                 :              0 :             ExplainPropertyInteger("Disk Usage", "kB",
                               3767                 :              0 :                                    aggstate->hash_disk_used, es);
                               3768                 :                :         }
                               3769                 :                :     }
                               3770                 :                :     else
                               3771                 :                :     {
 2006 drowley@postgresql.o     3772                 :CBC        1220 :         bool        gotone = false;
                               3773                 :                : 
                               3774   [ +  +  -  + ]:           1220 :         if (es->costs && aggstate->hash_planned_partitions > 0)
                               3775                 :                :         {
 2006 drowley@postgresql.o     3776                 :UBC           0 :             ExplainIndentText(es);
                               3777                 :              0 :             appendStringInfo(es->str, "Planned Partitions: %d",
                               3778                 :                :                              aggstate->hash_planned_partitions);
                               3779                 :              0 :             gotone = true;
                               3780                 :                :         }
                               3781                 :                : 
                               3782                 :                :         /*
                               3783                 :                :          * During parallel query the leader may have not helped out.  We
                               3784                 :                :          * detect this by checking how much memory it used.  If we find it
                               3785                 :                :          * didn't do any work then we don't show its properties.
                               3786                 :                :          */
 1957 drowley@postgresql.o     3787   [ +  +  +  - ]:CBC        1220 :         if (es->analyze && aggstate->hash_mem_peak > 0)
                               3788                 :                :         {
                               3789         [ +  - ]:            291 :             if (!gotone)
                               3790                 :            291 :                 ExplainIndentText(es);
                               3791                 :                :             else
 1061 drowley@postgresql.o     3792                 :UBC           0 :                 appendStringInfoSpaces(es->str, 2);
                               3793                 :                : 
 1957 drowley@postgresql.o     3794                 :CBC         291 :             appendStringInfo(es->str, "Batches: %d  Memory Usage: " INT64_FORMAT "kB",
                               3795                 :                :                              aggstate->hash_batches_used, memPeakKb);
                               3796                 :            291 :             gotone = true;
                               3797                 :                : 
                               3798                 :                :             /* Only display disk usage if we spilled to disk */
                               3799         [ -  + ]:            291 :             if (aggstate->hash_batches_used > 1)
                               3800                 :                :             {
 1957 drowley@postgresql.o     3801                 :UBC           0 :                 appendStringInfo(es->str, "  Disk Usage: " UINT64_FORMAT "kB",
                               3802                 :                :                                  aggstate->hash_disk_used);
                               3803                 :                :             }
                               3804                 :                :         }
                               3805                 :                : 
 1957 drowley@postgresql.o     3806         [ +  + ]:CBC        1220 :         if (gotone)
                               3807                 :            291 :             appendStringInfoChar(es->str, '\n');
                               3808                 :                :     }
                               3809                 :                : 
                               3810                 :                :     /* Display stats for each parallel worker */
 2006                          3811   [ +  +  -  + ]:           1220 :     if (es->analyze && aggstate->shared_info != NULL)
                               3812                 :                :     {
 2006 drowley@postgresql.o     3813         [ #  # ]:UBC           0 :         for (int n = 0; n < aggstate->shared_info->num_workers; n++)
                               3814                 :                :         {
                               3815                 :                :             AggregateInstrumentation *sinstrument;
                               3816                 :                :             uint64      hash_disk_used;
                               3817                 :                :             int         hash_batches_used;
                               3818                 :                : 
                               3819                 :              0 :             sinstrument = &aggstate->shared_info->sinstrument[n];
                               3820                 :                :             /* Skip workers that didn't do anything */
 1957                          3821         [ #  # ]:              0 :             if (sinstrument->hash_mem_peak == 0)
                               3822                 :              0 :                 continue;
 2006                          3823                 :              0 :             hash_disk_used = sinstrument->hash_disk_used;
                               3824                 :              0 :             hash_batches_used = sinstrument->hash_batches_used;
  579                          3825                 :              0 :             memPeakKb = BYTES_TO_KILOBYTES(sinstrument->hash_mem_peak);
                               3826                 :                : 
 2006                          3827         [ #  # ]:              0 :             if (es->workers_state)
                               3828                 :              0 :                 ExplainOpenWorker(n, es);
                               3829                 :                : 
                               3830         [ #  # ]:              0 :             if (es->format == EXPLAIN_FORMAT_TEXT)
                               3831                 :                :             {
                               3832                 :              0 :                 ExplainIndentText(es);
                               3833                 :                : 
 1966                          3834                 :              0 :                 appendStringInfo(es->str, "Batches: %d  Memory Usage: " INT64_FORMAT "kB",
                               3835                 :                :                                  hash_batches_used, memPeakKb);
                               3836                 :                : 
                               3837                 :                :                 /* Only display disk usage if we spilled to disk */
                               3838         [ #  # ]:              0 :                 if (hash_batches_used > 1)
                               3839                 :              0 :                     appendStringInfo(es->str, "  Disk Usage: " UINT64_FORMAT "kB",
                               3840                 :                :                                      hash_disk_used);
 2006                          3841                 :              0 :                 appendStringInfoChar(es->str, '\n');
                               3842                 :                :             }
                               3843                 :                :             else
                               3844                 :                :             {
 1966                          3845                 :              0 :                 ExplainPropertyInteger("HashAgg Batches", NULL,
                               3846                 :                :                                        hash_batches_used, es);
 2006                          3847                 :              0 :                 ExplainPropertyInteger("Peak Memory Usage", "kB", memPeakKb,
                               3848                 :                :                                        es);
 1994                          3849                 :              0 :                 ExplainPropertyInteger("Disk Usage", "kB", hash_disk_used, es);
                               3850                 :                :             }
                               3851                 :                : 
 2006                          3852         [ #  # ]:              0 :             if (es->workers_state)
                               3853                 :              0 :                 ExplainCloseWorker(n, es);
                               3854                 :                :         }
                               3855                 :                :     }
                               3856                 :                : }
                               3857                 :                : 
                               3858                 :                : /*
                               3859                 :                :  * Show the total number of index searches for a
                               3860                 :                :  * IndexScan/IndexOnlyScan/BitmapIndexScan node
                               3861                 :                :  */
                               3862                 :                : static void
  280 pg@bowt.ie               3863                 :CBC        5657 : show_indexsearches_info(PlanState *planstate, ExplainState *es)
                               3864                 :                : {
                               3865                 :           5657 :     Plan       *plan = planstate->plan;
                               3866                 :           5657 :     SharedIndexScanInstrumentation *SharedInfo = NULL;
                               3867                 :           5657 :     uint64      nsearches = 0;
                               3868                 :                : 
                               3869         [ +  + ]:           5657 :     if (!es->analyze)
                               3870                 :           4999 :         return;
                               3871                 :                : 
                               3872                 :                :     /* Initialize counters with stats from the local process first */
                               3873   [ +  +  +  - ]:            658 :     switch (nodeTag(plan))
                               3874                 :                :     {
                               3875                 :            336 :         case T_IndexScan:
                               3876                 :                :             {
                               3877                 :            336 :                 IndexScanState *indexstate = ((IndexScanState *) planstate);
                               3878                 :                : 
                               3879                 :            336 :                 nsearches = indexstate->iss_Instrument.nsearches;
                               3880                 :            336 :                 SharedInfo = indexstate->iss_SharedInfo;
                               3881                 :            336 :                 break;
                               3882                 :                :             }
                               3883                 :             68 :         case T_IndexOnlyScan:
                               3884                 :                :             {
                               3885                 :             68 :                 IndexOnlyScanState *indexstate = ((IndexOnlyScanState *) planstate);
                               3886                 :                : 
                               3887                 :             68 :                 nsearches = indexstate->ioss_Instrument.nsearches;
                               3888                 :             68 :                 SharedInfo = indexstate->ioss_SharedInfo;
                               3889                 :             68 :                 break;
                               3890                 :                :             }
                               3891                 :            254 :         case T_BitmapIndexScan:
                               3892                 :                :             {
                               3893                 :            254 :                 BitmapIndexScanState *indexstate = ((BitmapIndexScanState *) planstate);
                               3894                 :                : 
                               3895                 :            254 :                 nsearches = indexstate->biss_Instrument.nsearches;
                               3896                 :            254 :                 SharedInfo = indexstate->biss_SharedInfo;
                               3897                 :            254 :                 break;
                               3898                 :                :             }
  280 pg@bowt.ie               3899                 :UBC           0 :         default:
                               3900                 :              0 :             break;
                               3901                 :                :     }
                               3902                 :                : 
                               3903                 :                :     /* Next get the sum of the counters set within each and every process */
  280 pg@bowt.ie               3904         [ +  + ]:CBC         658 :     if (SharedInfo)
                               3905                 :                :     {
                               3906         [ +  + ]:            270 :         for (int i = 0; i < SharedInfo->num_workers; ++i)
                               3907                 :                :         {
                               3908                 :            135 :             IndexScanInstrumentation *winstrument = &SharedInfo->winstrument[i];
                               3909                 :                : 
                               3910                 :            135 :             nsearches += winstrument->nsearches;
                               3911                 :                :         }
                               3912                 :                :     }
                               3913                 :                : 
                               3914                 :            658 :     ExplainPropertyUInteger("Index Searches", NULL, nsearches, es);
                               3915                 :                : }
                               3916                 :                : 
                               3917                 :                : /*
                               3918                 :                :  * Show exact/lossy pages for a BitmapHeapScan node
                               3919                 :                :  */
                               3920                 :                : static void
 4355 rhaas@postgresql.org     3921                 :           2067 : show_tidbitmap_info(BitmapHeapScanState *planstate, ExplainState *es)
                               3922                 :                : {
  525 drowley@postgresql.o     3923         [ +  + ]:           2067 :     if (!es->analyze)
                               3924                 :           1816 :         return;
                               3925                 :                : 
 4355 rhaas@postgresql.org     3926         [ +  + ]:            251 :     if (es->format != EXPLAIN_FORMAT_TEXT)
                               3927                 :                :     {
  526 drowley@postgresql.o     3928                 :             30 :         ExplainPropertyUInteger("Exact Heap Blocks", NULL,
                               3929                 :                :                                 planstate->stats.exact_pages, es);
                               3930                 :             30 :         ExplainPropertyUInteger("Lossy Heap Blocks", NULL,
                               3931                 :                :                                 planstate->stats.lossy_pages, es);
                               3932                 :                :     }
                               3933                 :                :     else
                               3934                 :                :     {
  525                          3935   [ +  +  -  + ]:            221 :         if (planstate->stats.exact_pages > 0 || planstate->stats.lossy_pages > 0)
                               3936                 :                :         {
 2152 tgl@sss.pgh.pa.us        3937                 :            143 :             ExplainIndentText(es);
 4173 fujii@postgresql.org     3938                 :            143 :             appendStringInfoString(es->str, "Heap Blocks:");
  525 drowley@postgresql.o     3939         [ +  - ]:            143 :             if (planstate->stats.exact_pages > 0)
                               3940                 :            143 :                 appendStringInfo(es->str, " exact=" UINT64_FORMAT, planstate->stats.exact_pages);
                               3941         [ -  + ]:            143 :             if (planstate->stats.lossy_pages > 0)
  525 drowley@postgresql.o     3942                 :UBC           0 :                 appendStringInfo(es->str, " lossy=" UINT64_FORMAT, planstate->stats.lossy_pages);
 4173 fujii@postgresql.org     3943                 :CBC         143 :             appendStringInfoChar(es->str, '\n');
                               3944                 :                :         }
                               3945                 :                :     }
                               3946                 :                : 
                               3947                 :                :     /* Display stats for each parallel worker */
  525 drowley@postgresql.o     3948         [ -  + ]:            251 :     if (planstate->pstate != NULL)
                               3949                 :                :     {
  525 drowley@postgresql.o     3950         [ #  # ]:UBC           0 :         for (int n = 0; n < planstate->sinstrument->num_workers; n++)
                               3951                 :                :         {
                               3952                 :              0 :             BitmapHeapScanInstrumentation *si = &planstate->sinstrument->sinstrument[n];
                               3953                 :                : 
                               3954   [ #  #  #  # ]:              0 :             if (si->exact_pages == 0 && si->lossy_pages == 0)
                               3955                 :              0 :                 continue;
                               3956                 :                : 
                               3957         [ #  # ]:              0 :             if (es->workers_state)
                               3958                 :              0 :                 ExplainOpenWorker(n, es);
                               3959                 :                : 
                               3960         [ #  # ]:              0 :             if (es->format == EXPLAIN_FORMAT_TEXT)
                               3961                 :                :             {
                               3962                 :              0 :                 ExplainIndentText(es);
                               3963                 :              0 :                 appendStringInfoString(es->str, "Heap Blocks:");
                               3964         [ #  # ]:              0 :                 if (si->exact_pages > 0)
                               3965                 :              0 :                     appendStringInfo(es->str, " exact=" UINT64_FORMAT, si->exact_pages);
                               3966         [ #  # ]:              0 :                 if (si->lossy_pages > 0)
                               3967                 :              0 :                     appendStringInfo(es->str, " lossy=" UINT64_FORMAT, si->lossy_pages);
                               3968                 :              0 :                 appendStringInfoChar(es->str, '\n');
                               3969                 :                :             }
                               3970                 :                :             else
                               3971                 :                :             {
                               3972                 :              0 :                 ExplainPropertyUInteger("Exact Heap Blocks", NULL,
                               3973                 :                :                                         si->exact_pages, es);
                               3974                 :              0 :                 ExplainPropertyUInteger("Lossy Heap Blocks", NULL,
                               3975                 :                :                                         si->lossy_pages, es);
                               3976                 :                :             }
                               3977                 :                : 
                               3978         [ #  # ]:              0 :             if (es->workers_state)
                               3979                 :              0 :                 ExplainCloseWorker(n, es);
                               3980                 :                :         }
                               3981                 :                :     }
                               3982                 :                : }
                               3983                 :                : 
                               3984                 :                : /*
                               3985                 :                :  * If it's EXPLAIN ANALYZE, show instrumentation information for a plan node
                               3986                 :                :  *
                               3987                 :                :  * "which" identifies which instrumentation counter to print
                               3988                 :                :  */
                               3989                 :                : static void
 5199 tgl@sss.pgh.pa.us        3990                 :CBC       13236 : show_instrumentation_count(const char *qlabel, int which,
                               3991                 :                :                            PlanState *planstate, ExplainState *es)
                               3992                 :                : {
                               3993                 :                :     double      nfiltered;
                               3994                 :                :     double      nloops;
                               3995                 :                : 
                               3996   [ +  +  -  + ]:          13236 :     if (!es->analyze || !planstate->instrument)
                               3997                 :          11415 :         return;
                               3998                 :                : 
                               3999         [ +  + ]:           1821 :     if (which == 2)
                               4000                 :            585 :         nfiltered = planstate->instrument->nfiltered2;
                               4001                 :                :     else
                               4002                 :           1236 :         nfiltered = planstate->instrument->nfiltered1;
                               4003                 :           1821 :     nloops = planstate->instrument->nloops;
                               4004                 :                : 
                               4005                 :                :     /* In text mode, suppress zero counts; they're not interesting enough */
                               4006   [ +  +  +  + ]:           1821 :     if (nfiltered > 0 || es->format != EXPLAIN_FORMAT_TEXT)
                               4007                 :                :     {
                               4008         [ +  - ]:            850 :         if (nloops > 0)
 2832 andres@anarazel.de       4009                 :            850 :             ExplainPropertyFloat(qlabel, NULL, nfiltered / nloops, 0, es);
                               4010                 :                :         else
 2832 andres@anarazel.de       4011                 :UBC           0 :             ExplainPropertyFloat(qlabel, NULL, 0.0, 0, es);
                               4012                 :                :     }
                               4013                 :                : }
                               4014                 :                : 
                               4015                 :                : /*
                               4016                 :                :  * Show extra information for a ForeignScan node.
                               4017                 :                :  */
                               4018                 :                : static void
 5413 tgl@sss.pgh.pa.us        4019                 :CBC         431 : show_foreignscan_info(ForeignScanState *fsstate, ExplainState *es)
                               4020                 :                : {
                               4021                 :            431 :     FdwRoutine *fdwroutine = fsstate->fdwroutine;
                               4022                 :                : 
                               4023                 :                :     /* Let the FDW emit whatever fields it wants */
 3560 rhaas@postgresql.org     4024         [ +  + ]:            431 :     if (((ForeignScan *) fsstate->ss.ps.plan)->operation != CMD_SELECT)
                               4025                 :                :     {
                               4026         [ +  - ]:             32 :         if (fdwroutine->ExplainDirectModify != NULL)
                               4027                 :             32 :             fdwroutine->ExplainDirectModify(fsstate, es);
                               4028                 :                :     }
                               4029                 :                :     else
                               4030                 :                :     {
                               4031         [ +  - ]:            399 :         if (fdwroutine->ExplainForeignScan != NULL)
                               4032                 :            399 :             fdwroutine->ExplainForeignScan(fsstate, es);
                               4033                 :                :     }
 5413 tgl@sss.pgh.pa.us        4034                 :            431 : }
                               4035                 :                : 
                               4036                 :                : /*
                               4037                 :                :  * Fetch the name of an index in an EXPLAIN
                               4038                 :                :  *
                               4039                 :                :  * We allow plugins to get control here so that plans involving hypothetical
                               4040                 :                :  * indexes can be explained.
                               4041                 :                :  *
                               4042                 :                :  * Note: names returned by this function should be "raw"; the caller will
                               4043                 :                :  * apply quoting if needed.  Formerly the convention was to do quoting here,
                               4044                 :                :  * but we don't want that in non-text output formats.
                               4045                 :                :  */
                               4046                 :                : static const char *
 6780                          4047                 :           5657 : explain_get_index_name(Oid indexId)
                               4048                 :                : {
                               4049                 :                :     const char *result;
                               4050                 :                : 
                               4051         [ -  + ]:           5657 :     if (explain_get_index_name_hook)
 6780 tgl@sss.pgh.pa.us        4052                 :UBC           0 :         result = (*explain_get_index_name_hook) (indexId);
                               4053                 :                :     else
 6780 tgl@sss.pgh.pa.us        4054                 :CBC        5657 :         result = NULL;
                               4055         [ +  - ]:           5657 :     if (result == NULL)
                               4056                 :                :     {
                               4057                 :                :         /* default behavior: look it up in the catalogs */
                               4058                 :           5657 :         result = get_rel_name(indexId);
                               4059         [ -  + ]:           5657 :         if (result == NULL)
 6780 tgl@sss.pgh.pa.us        4060         [ #  # ]:UBC           0 :             elog(ERROR, "cache lookup failed for index %u", indexId);
                               4061                 :                :     }
 6780 tgl@sss.pgh.pa.us        4062                 :CBC        5657 :     return result;
                               4063                 :                : }
                               4064                 :                : 
                               4065                 :                : /*
                               4066                 :                :  * Return whether show_buffer_usage would have anything to print, if given
                               4067                 :                :  * the same 'usage' data.  Note that when the format is anything other than
                               4068                 :                :  * text, we print even if the counters are all zeroes.
                               4069                 :                :  */
                               4070                 :                : static bool
  687 alvherre@alvh.no-ip.     4071                 :          12264 : peek_buffer_usage(ExplainState *es, const BufferUsage *usage)
                               4072                 :                : {
                               4073                 :                :     bool        has_shared;
                               4074                 :                :     bool        has_local;
                               4075                 :                :     bool        has_temp;
                               4076                 :                :     bool        has_shared_timing;
                               4077                 :                :     bool        has_local_timing;
                               4078                 :                :     bool        has_temp_timing;
                               4079                 :                : 
                               4080         [ +  + ]:          12264 :     if (usage == NULL)
                               4081                 :          10966 :         return false;
                               4082                 :                : 
                               4083         [ +  + ]:           1298 :     if (es->format != EXPLAIN_FORMAT_TEXT)
                               4084                 :            122 :         return true;
                               4085                 :                : 
                               4086                 :           3258 :     has_shared = (usage->shared_blks_hit > 0 ||
                               4087         [ +  + ]:            906 :                   usage->shared_blks_read > 0 ||
                               4088   [ +  +  +  - ]:           2982 :                   usage->shared_blks_dirtied > 0 ||
                               4089         [ -  + ]:            900 :                   usage->shared_blks_written > 0);
                               4090                 :           3528 :     has_local = (usage->local_blks_hit > 0 ||
                               4091         [ +  - ]:           1176 :                  usage->local_blks_read > 0 ||
                               4092   [ +  -  +  - ]:           3528 :                  usage->local_blks_dirtied > 0 ||
                               4093         [ -  + ]:           1176 :                  usage->local_blks_written > 0);
                               4094         [ +  - ]:           2352 :     has_temp = (usage->temp_blks_read > 0 ||
                               4095         [ -  + ]:           1176 :                 usage->temp_blks_written > 0);
                               4096         [ +  - ]:           2352 :     has_shared_timing = (!INSTR_TIME_IS_ZERO(usage->shared_blk_read_time) ||
                               4097         [ -  + ]:           1176 :                          !INSTR_TIME_IS_ZERO(usage->shared_blk_write_time));
                               4098         [ +  - ]:           2352 :     has_local_timing = (!INSTR_TIME_IS_ZERO(usage->local_blk_read_time) ||
                               4099         [ -  + ]:           1176 :                         !INSTR_TIME_IS_ZERO(usage->local_blk_write_time));
                               4100         [ +  - ]:           2352 :     has_temp_timing = (!INSTR_TIME_IS_ZERO(usage->temp_blk_read_time) ||
                               4101         [ -  + ]:           1176 :                        !INSTR_TIME_IS_ZERO(usage->temp_blk_write_time));
                               4102                 :                : 
                               4103   [ +  -  +  -  :            900 :     return has_shared || has_local || has_temp || has_shared_timing ||
                                        +  -  +  - ]
                               4104   [ +  +  -  + ]:           2076 :         has_local_timing || has_temp_timing;
                               4105                 :                : }
                               4106                 :                : 
                               4107                 :                : /*
                               4108                 :                :  * Show buffer usage details.  This better be sync with peek_buffer_usage.
                               4109                 :                :  */
                               4110                 :                : static void
                               4111                 :           2530 : show_buffer_usage(ExplainState *es, const BufferUsage *usage)
                               4112                 :                : {
 3660 rhaas@postgresql.org     4113         [ +  + ]:           2530 :     if (es->format == EXPLAIN_FORMAT_TEXT)
                               4114                 :                :     {
                               4115                 :           3885 :         bool        has_shared = (usage->shared_blks_hit > 0 ||
                               4116         [ +  + ]:             51 :                                   usage->shared_blks_read > 0 ||
                               4117   [ +  +  +  - ]:           1986 :                                   usage->shared_blks_dirtied > 0 ||
                               4118         [ -  + ]:             18 :                                   usage->shared_blks_written > 0);
                               4119                 :           5751 :         bool        has_local = (usage->local_blks_hit > 0 ||
                               4120         [ +  - ]:           1917 :                                  usage->local_blks_read > 0 ||
                               4121   [ +  -  +  - ]:           5751 :                                  usage->local_blks_dirtied > 0 ||
                               4122         [ -  + ]:           1917 :                                  usage->local_blks_written > 0);
                               4123         [ +  - ]:           3834 :         bool        has_temp = (usage->temp_blks_read > 0 ||
                               4124         [ -  + ]:           1917 :                                 usage->temp_blks_written > 0);
  789 michael@paquier.xyz      4125         [ +  - ]:           3834 :         bool        has_shared_timing = (!INSTR_TIME_IS_ZERO(usage->shared_blk_read_time) ||
                               4126         [ -  + ]:           1917 :                                          !INSTR_TIME_IS_ZERO(usage->shared_blk_write_time));
                               4127         [ +  - ]:           3834 :         bool        has_local_timing = (!INSTR_TIME_IS_ZERO(usage->local_blk_read_time) ||
                               4128         [ -  + ]:           1917 :                                         !INSTR_TIME_IS_ZERO(usage->local_blk_write_time));
 1348                          4129         [ +  - ]:           3834 :         bool        has_temp_timing = (!INSTR_TIME_IS_ZERO(usage->temp_blk_read_time) ||
                               4130         [ -  + ]:           1917 :                                        !INSTR_TIME_IS_ZERO(usage->temp_blk_write_time));
                               4131                 :                : 
                               4132                 :                :         /* Show only positive counter values. */
 3660 rhaas@postgresql.org     4133   [ +  +  +  -  :           1917 :         if (has_shared || has_local || has_temp)
                                              -  + ]
                               4134                 :                :         {
 2152 tgl@sss.pgh.pa.us        4135                 :           1899 :             ExplainIndentText(es);
 3660 rhaas@postgresql.org     4136                 :           1899 :             appendStringInfoString(es->str, "Buffers:");
                               4137                 :                : 
                               4138         [ +  - ]:           1899 :             if (has_shared)
                               4139                 :                :             {
                               4140                 :           1899 :                 appendStringInfoString(es->str, " shared");
                               4141         [ +  + ]:           1899 :                 if (usage->shared_blks_hit > 0)
  262 peter@eisentraut.org     4142                 :           1866 :                     appendStringInfo(es->str, " hit=%" PRId64,
                               4143                 :           1866 :                                      usage->shared_blks_hit);
 3660 rhaas@postgresql.org     4144         [ +  + ]:           1899 :                 if (usage->shared_blks_read > 0)
  262 peter@eisentraut.org     4145                 :             87 :                     appendStringInfo(es->str, " read=%" PRId64,
                               4146                 :             87 :                                      usage->shared_blks_read);
 3660 rhaas@postgresql.org     4147         [ +  + ]:           1899 :                 if (usage->shared_blks_dirtied > 0)
  262 peter@eisentraut.org     4148                 :             10 :                     appendStringInfo(es->str, " dirtied=%" PRId64,
                               4149                 :             10 :                                      usage->shared_blks_dirtied);
 3660 rhaas@postgresql.org     4150         [ +  + ]:           1899 :                 if (usage->shared_blks_written > 0)
  262 peter@eisentraut.org     4151                 :             32 :                     appendStringInfo(es->str, " written=%" PRId64,
                               4152                 :             32 :                                      usage->shared_blks_written);
 3660 rhaas@postgresql.org     4153   [ +  -  -  + ]:           1899 :                 if (has_local || has_temp)
 3660 rhaas@postgresql.org     4154                 :UBC           0 :                     appendStringInfoChar(es->str, ',');
                               4155                 :                :             }
 3660 rhaas@postgresql.org     4156         [ -  + ]:CBC        1899 :             if (has_local)
                               4157                 :                :             {
 3660 rhaas@postgresql.org     4158                 :UBC           0 :                 appendStringInfoString(es->str, " local");
                               4159         [ #  # ]:              0 :                 if (usage->local_blks_hit > 0)
  262 peter@eisentraut.org     4160                 :              0 :                     appendStringInfo(es->str, " hit=%" PRId64,
                               4161                 :              0 :                                      usage->local_blks_hit);
 3660 rhaas@postgresql.org     4162         [ #  # ]:              0 :                 if (usage->local_blks_read > 0)
  262 peter@eisentraut.org     4163                 :              0 :                     appendStringInfo(es->str, " read=%" PRId64,
                               4164                 :              0 :                                      usage->local_blks_read);
 3660 rhaas@postgresql.org     4165         [ #  # ]:              0 :                 if (usage->local_blks_dirtied > 0)
  262 peter@eisentraut.org     4166                 :              0 :                     appendStringInfo(es->str, " dirtied=%" PRId64,
                               4167                 :              0 :                                      usage->local_blks_dirtied);
 3660 rhaas@postgresql.org     4168         [ #  # ]:              0 :                 if (usage->local_blks_written > 0)
  262 peter@eisentraut.org     4169                 :              0 :                     appendStringInfo(es->str, " written=%" PRId64,
                               4170                 :              0 :                                      usage->local_blks_written);
 3660 rhaas@postgresql.org     4171         [ #  # ]:              0 :                 if (has_temp)
                               4172                 :              0 :                     appendStringInfoChar(es->str, ',');
                               4173                 :                :             }
 3660 rhaas@postgresql.org     4174         [ -  + ]:CBC        1899 :             if (has_temp)
                               4175                 :                :             {
 3660 rhaas@postgresql.org     4176                 :UBC           0 :                 appendStringInfoString(es->str, " temp");
                               4177         [ #  # ]:              0 :                 if (usage->temp_blks_read > 0)
  262 peter@eisentraut.org     4178                 :              0 :                     appendStringInfo(es->str, " read=%" PRId64,
                               4179                 :              0 :                                      usage->temp_blks_read);
 3660 rhaas@postgresql.org     4180         [ #  # ]:              0 :                 if (usage->temp_blks_written > 0)
  262 peter@eisentraut.org     4181                 :              0 :                     appendStringInfo(es->str, " written=%" PRId64,
                               4182                 :              0 :                                      usage->temp_blks_written);
                               4183                 :                :             }
 3660 rhaas@postgresql.org     4184                 :CBC        1899 :             appendStringInfoChar(es->str, '\n');
                               4185                 :                :         }
                               4186                 :                : 
                               4187                 :                :         /* As above, show only positive counter values. */
  789 michael@paquier.xyz      4188   [ +  -  +  -  :           1917 :         if (has_shared_timing || has_local_timing || has_temp_timing)
                                              -  + ]
                               4189                 :                :         {
 2152 tgl@sss.pgh.pa.us        4190                 :UBC           0 :             ExplainIndentText(es);
 3660 rhaas@postgresql.org     4191                 :              0 :             appendStringInfoString(es->str, "I/O Timings:");
                               4192                 :                : 
  789 michael@paquier.xyz      4193         [ #  # ]:              0 :             if (has_shared_timing)
                               4194                 :                :             {
                               4195                 :              0 :                 appendStringInfoString(es->str, " shared");
                               4196         [ #  # ]:              0 :                 if (!INSTR_TIME_IS_ZERO(usage->shared_blk_read_time))
 1348                          4197                 :              0 :                     appendStringInfo(es->str, " read=%0.3f",
  789                          4198                 :              0 :                                      INSTR_TIME_GET_MILLISEC(usage->shared_blk_read_time));
                               4199         [ #  # ]:              0 :                 if (!INSTR_TIME_IS_ZERO(usage->shared_blk_write_time))
 1348                          4200                 :              0 :                     appendStringInfo(es->str, " write=%0.3f",
  789                          4201                 :              0 :                                      INSTR_TIME_GET_MILLISEC(usage->shared_blk_write_time));
                               4202   [ #  #  #  # ]:              0 :                 if (has_local_timing || has_temp_timing)
                               4203                 :              0 :                     appendStringInfoChar(es->str, ',');
                               4204                 :                :             }
                               4205         [ #  # ]:              0 :             if (has_local_timing)
                               4206                 :                :             {
                               4207                 :              0 :                 appendStringInfoString(es->str, " local");
                               4208         [ #  # ]:              0 :                 if (!INSTR_TIME_IS_ZERO(usage->local_blk_read_time))
                               4209                 :              0 :                     appendStringInfo(es->str, " read=%0.3f",
                               4210                 :              0 :                                      INSTR_TIME_GET_MILLISEC(usage->local_blk_read_time));
                               4211         [ #  # ]:              0 :                 if (!INSTR_TIME_IS_ZERO(usage->local_blk_write_time))
                               4212                 :              0 :                     appendStringInfo(es->str, " write=%0.3f",
                               4213                 :              0 :                                      INSTR_TIME_GET_MILLISEC(usage->local_blk_write_time));
 1348                          4214         [ #  # ]:              0 :                 if (has_temp_timing)
                               4215                 :              0 :                     appendStringInfoChar(es->str, ',');
                               4216                 :                :             }
                               4217         [ #  # ]:              0 :             if (has_temp_timing)
                               4218                 :                :             {
                               4219                 :              0 :                 appendStringInfoString(es->str, " temp");
                               4220         [ #  # ]:              0 :                 if (!INSTR_TIME_IS_ZERO(usage->temp_blk_read_time))
                               4221                 :              0 :                     appendStringInfo(es->str, " read=%0.3f",
                               4222                 :              0 :                                      INSTR_TIME_GET_MILLISEC(usage->temp_blk_read_time));
                               4223         [ #  # ]:              0 :                 if (!INSTR_TIME_IS_ZERO(usage->temp_blk_write_time))
                               4224                 :              0 :                     appendStringInfo(es->str, " write=%0.3f",
                               4225                 :              0 :                                      INSTR_TIME_GET_MILLISEC(usage->temp_blk_write_time));
                               4226                 :                :             }
 3660 rhaas@postgresql.org     4227                 :              0 :             appendStringInfoChar(es->str, '\n');
                               4228                 :                :         }
                               4229                 :                :     }
                               4230                 :                :     else
                               4231                 :                :     {
 2832 andres@anarazel.de       4232                 :CBC         613 :         ExplainPropertyInteger("Shared Hit Blocks", NULL,
                               4233                 :            613 :                                usage->shared_blks_hit, es);
                               4234                 :            613 :         ExplainPropertyInteger("Shared Read Blocks", NULL,
                               4235                 :            613 :                                usage->shared_blks_read, es);
                               4236                 :            613 :         ExplainPropertyInteger("Shared Dirtied Blocks", NULL,
                               4237                 :            613 :                                usage->shared_blks_dirtied, es);
                               4238                 :            613 :         ExplainPropertyInteger("Shared Written Blocks", NULL,
                               4239                 :            613 :                                usage->shared_blks_written, es);
                               4240                 :            613 :         ExplainPropertyInteger("Local Hit Blocks", NULL,
                               4241                 :            613 :                                usage->local_blks_hit, es);
                               4242                 :            613 :         ExplainPropertyInteger("Local Read Blocks", NULL,
                               4243                 :            613 :                                usage->local_blks_read, es);
                               4244                 :            613 :         ExplainPropertyInteger("Local Dirtied Blocks", NULL,
                               4245                 :            613 :                                usage->local_blks_dirtied, es);
                               4246                 :            613 :         ExplainPropertyInteger("Local Written Blocks", NULL,
                               4247                 :            613 :                                usage->local_blks_written, es);
                               4248                 :            613 :         ExplainPropertyInteger("Temp Read Blocks", NULL,
                               4249                 :            613 :                                usage->temp_blks_read, es);
                               4250                 :            613 :         ExplainPropertyInteger("Temp Written Blocks", NULL,
                               4251                 :            613 :                                usage->temp_blks_written, es);
 3413 tgl@sss.pgh.pa.us        4252         [ +  + ]:            613 :         if (track_io_timing)
                               4253                 :                :         {
  789 michael@paquier.xyz      4254                 :              6 :             ExplainPropertyFloat("Shared I/O Read Time", "ms",
                               4255                 :              6 :                                  INSTR_TIME_GET_MILLISEC(usage->shared_blk_read_time),
                               4256                 :                :                                  3, es);
                               4257                 :              6 :             ExplainPropertyFloat("Shared I/O Write Time", "ms",
                               4258                 :              6 :                                  INSTR_TIME_GET_MILLISEC(usage->shared_blk_write_time),
                               4259                 :                :                                  3, es);
                               4260                 :              6 :             ExplainPropertyFloat("Local I/O Read Time", "ms",
                               4261                 :              6 :                                  INSTR_TIME_GET_MILLISEC(usage->local_blk_read_time),
                               4262                 :                :                                  3, es);
                               4263                 :              6 :             ExplainPropertyFloat("Local I/O Write Time", "ms",
                               4264                 :              6 :                                  INSTR_TIME_GET_MILLISEC(usage->local_blk_write_time),
                               4265                 :                :                                  3, es);
 1348                          4266                 :              6 :             ExplainPropertyFloat("Temp I/O Read Time", "ms",
                               4267                 :              6 :                                  INSTR_TIME_GET_MILLISEC(usage->temp_blk_read_time),
                               4268                 :                :                                  3, es);
                               4269                 :              6 :             ExplainPropertyFloat("Temp I/O Write Time", "ms",
                               4270                 :              6 :                                  INSTR_TIME_GET_MILLISEC(usage->temp_blk_write_time),
                               4271                 :                :                                  3, es);
                               4272                 :                :         }
                               4273                 :                :     }
 3660 rhaas@postgresql.org     4274                 :           2530 : }
                               4275                 :                : 
                               4276                 :                : /*
                               4277                 :                :  * Show WAL usage details.
                               4278                 :                :  */
                               4279                 :                : static void
 2080 akapila@postgresql.o     4280                 :UBC           0 : show_wal_usage(ExplainState *es, const WalUsage *usage)
                               4281                 :                : {
                               4282         [ #  # ]:              0 :     if (es->format == EXPLAIN_FORMAT_TEXT)
                               4283                 :                :     {
                               4284                 :                :         /* Show only positive counter values. */
 2051                          4285   [ #  #  #  # ]:              0 :         if ((usage->wal_records > 0) || (usage->wal_fpi > 0) ||
   47 michael@paquier.xyz      4286   [ #  #  #  # ]:UNC           0 :             (usage->wal_bytes > 0) || (usage->wal_buffers_full > 0) ||
                               4287         [ #  # ]:              0 :             (usage->wal_fpi_bytes > 0))
                               4288                 :                :         {
 2080 akapila@postgresql.o     4289                 :UBC           0 :             ExplainIndentText(es);
                               4290                 :              0 :             appendStringInfoString(es->str, "WAL:");
                               4291                 :                : 
                               4292         [ #  # ]:              0 :             if (usage->wal_records > 0)
  262 peter@eisentraut.org     4293                 :              0 :                 appendStringInfo(es->str, " records=%" PRId64,
                               4294                 :              0 :                                  usage->wal_records);
 2051 akapila@postgresql.o     4295         [ #  # ]:              0 :             if (usage->wal_fpi > 0)
  262 peter@eisentraut.org     4296                 :              0 :                 appendStringInfo(es->str, " fpi=%" PRId64,
                               4297                 :              0 :                                  usage->wal_fpi);
 2080 akapila@postgresql.o     4298         [ #  # ]:              0 :             if (usage->wal_bytes > 0)
  262 peter@eisentraut.org     4299                 :              0 :                 appendStringInfo(es->str, " bytes=%" PRIu64,
 2080 akapila@postgresql.o     4300                 :              0 :                                  usage->wal_bytes);
   47 michael@paquier.xyz      4301         [ #  # ]:UNC           0 :             if (usage->wal_fpi_bytes > 0)
                               4302                 :              0 :                 appendStringInfo(es->str, " fpi bytes=%" PRIu64,
                               4303                 :              0 :                                  usage->wal_fpi_bytes);
  302 michael@paquier.xyz      4304         [ #  # ]:UBC           0 :             if (usage->wal_buffers_full > 0)
  262 peter@eisentraut.org     4305                 :              0 :                 appendStringInfo(es->str, " buffers full=%" PRId64,
                               4306                 :              0 :                                  usage->wal_buffers_full);
 2080 akapila@postgresql.o     4307                 :              0 :             appendStringInfoChar(es->str, '\n');
                               4308                 :                :         }
                               4309                 :                :     }
                               4310                 :                :     else
                               4311                 :                :     {
 2051                          4312                 :              0 :         ExplainPropertyInteger("WAL Records", NULL,
 2080                          4313                 :              0 :                                usage->wal_records, es);
 2051                          4314                 :              0 :         ExplainPropertyInteger("WAL FPI", NULL,
                               4315                 :              0 :                                usage->wal_fpi, es);
                               4316                 :              0 :         ExplainPropertyUInteger("WAL Bytes", NULL,
 2080                          4317                 :              0 :                                 usage->wal_bytes, es);
   47 michael@paquier.xyz      4318                 :UNC           0 :         ExplainPropertyUInteger("WAL FPI Bytes", NULL,
                               4319                 :              0 :                                 usage->wal_fpi_bytes, es);
  302 michael@paquier.xyz      4320                 :UBC           0 :         ExplainPropertyInteger("WAL Buffers Full", NULL,
                               4321                 :              0 :                                usage->wal_buffers_full, es);
                               4322                 :                :     }
 2080 akapila@postgresql.o     4323                 :              0 : }
                               4324                 :                : 
                               4325                 :                : /*
                               4326                 :                :  * Show memory usage details.
                               4327                 :                :  */
                               4328                 :                : static void
  687 alvherre@alvh.no-ip.     4329                 :CBC          15 : show_memory_counters(ExplainState *es, const MemoryContextCounters *mem_counters)
                               4330                 :                : {
  579 drowley@postgresql.o     4331                 :             15 :     int64       memUsedkB = BYTES_TO_KILOBYTES(mem_counters->totalspace -
                               4332                 :                :                                                mem_counters->freespace);
                               4333                 :             15 :     int64       memAllocatedkB = BYTES_TO_KILOBYTES(mem_counters->totalspace);
                               4334                 :                : 
  687 alvherre@alvh.no-ip.     4335         [ +  + ]:             15 :     if (es->format == EXPLAIN_FORMAT_TEXT)
                               4336                 :                :     {
                               4337                 :              9 :         ExplainIndentText(es);
                               4338                 :              9 :         appendStringInfo(es->str,
                               4339                 :                :                          "Memory: used=" INT64_FORMAT "kB  allocated=" INT64_FORMAT "kB",
                               4340                 :                :                          memUsedkB, memAllocatedkB);
                               4341                 :              9 :         appendStringInfoChar(es->str, '\n');
                               4342                 :                :     }
                               4343                 :                :     else
                               4344                 :                :     {
  579 drowley@postgresql.o     4345                 :              6 :         ExplainPropertyInteger("Memory Used", "kB", memUsedkB, es);
                               4346                 :              6 :         ExplainPropertyInteger("Memory Allocated", "kB", memAllocatedkB, es);
                               4347                 :                :     }
  687 alvherre@alvh.no-ip.     4348                 :             15 : }
                               4349                 :                : 
                               4350                 :                : 
                               4351                 :                : /*
                               4352                 :                :  * Add some additional details about an IndexScan or IndexOnlyScan
                               4353                 :                :  */
                               4354                 :                : static void
 5180 tgl@sss.pgh.pa.us        4355                 :           3493 : ExplainIndexScanDetails(Oid indexid, ScanDirection indexorderdir,
                               4356                 :                :                         ExplainState *es)
                               4357                 :                : {
                               4358                 :           3493 :     const char *indexname = explain_get_index_name(indexid);
                               4359                 :                : 
                               4360         [ +  + ]:           3493 :     if (es->format == EXPLAIN_FORMAT_TEXT)
                               4361                 :                :     {
                               4362         [ +  + ]:           3472 :         if (ScanDirectionIsBackward(indexorderdir))
                               4363                 :            136 :             appendStringInfoString(es->str, " Backward");
 2003                          4364                 :           3472 :         appendStringInfo(es->str, " using %s", quote_identifier(indexname));
                               4365                 :                :     }
                               4366                 :                :     else
                               4367                 :                :     {
                               4368                 :                :         const char *scandir;
                               4369                 :                : 
 5180                          4370      [ -  +  - ]:             21 :         switch (indexorderdir)
                               4371                 :                :         {
 5180 tgl@sss.pgh.pa.us        4372                 :UBC           0 :             case BackwardScanDirection:
                               4373                 :              0 :                 scandir = "Backward";
                               4374                 :              0 :                 break;
 5180 tgl@sss.pgh.pa.us        4375                 :CBC          21 :             case ForwardScanDirection:
                               4376                 :             21 :                 scandir = "Forward";
                               4377                 :             21 :                 break;
 5180 tgl@sss.pgh.pa.us        4378                 :UBC           0 :             default:
                               4379                 :              0 :                 scandir = "???";
                               4380                 :              0 :                 break;
                               4381                 :                :         }
 5180 tgl@sss.pgh.pa.us        4382                 :CBC          21 :         ExplainPropertyText("Scan Direction", scandir, es);
                               4383                 :             21 :         ExplainPropertyText("Index Name", indexname, es);
                               4384                 :                :     }
                               4385                 :           3493 : }
                               4386                 :                : 
                               4387                 :                : /*
                               4388                 :                :  * Show the target of a Scan node
                               4389                 :                :  */
                               4390                 :                : static void
 5989                          4391                 :          20447 : ExplainScanTarget(Scan *plan, ExplainState *es)
                               4392                 :                : {
 5404                          4393                 :          20447 :     ExplainTargetRel((Plan *) plan, plan->scanrelid, es);
                               4394                 :          20447 : }
                               4395                 :                : 
                               4396                 :                : /*
                               4397                 :                :  * Show the target of a ModifyTable node
                               4398                 :                :  *
                               4399                 :                :  * Here we show the nominal target (ie, the relation that was named in the
                               4400                 :                :  * original query).  If the actual target(s) is/are different, we'll show them
                               4401                 :                :  * in show_modifytable_info().
                               4402                 :                :  */
                               4403                 :                : static void
                               4404                 :            556 : ExplainModifyTarget(ModifyTable *plan, ExplainState *es)
                               4405                 :                : {
 3955                          4406                 :            556 :     ExplainTargetRel((Plan *) plan, plan->nominalRelation, es);
 5404                          4407                 :            556 : }
                               4408                 :                : 
                               4409                 :                : /*
                               4410                 :                :  * Show the target relation of a scan or modify node
                               4411                 :                :  */
                               4412                 :                : static void
                               4413                 :          21276 : ExplainTargetRel(Plan *plan, Index rti, ExplainState *es)
                               4414                 :                : {
 5989                          4415                 :          21276 :     char       *objectname = NULL;
 5972                          4416                 :          21276 :     char       *namespace = NULL;
                               4417                 :          21276 :     const char *objecttag = NULL;
                               4418                 :                :     RangeTblEntry *rte;
                               4419                 :                :     char       *refname;
                               4420                 :                : 
 5404                          4421                 :          21276 :     rte = rt_fetch(rti, es->rtable);
 4834                          4422                 :          21276 :     refname = (char *) list_nth(es->rtable_names, rti - 1);
 4733                          4423         [ -  + ]:          21276 :     if (refname == NULL)
 4733 tgl@sss.pgh.pa.us        4424                 :UBC           0 :         refname = rte->eref->aliasname;
                               4425                 :                : 
 5989 tgl@sss.pgh.pa.us        4426   [ +  +  +  +  :CBC       21276 :     switch (nodeTag(plan))
                                        +  -  +  + ]
                               4427                 :                :     {
                               4428                 :          20298 :         case T_SeqScan:
                               4429                 :                :         case T_SampleScan:
                               4430                 :                :         case T_IndexScan:
                               4431                 :                :         case T_IndexOnlyScan:
                               4432                 :                :         case T_BitmapHeapScan:
                               4433                 :                :         case T_TidScan:
                               4434                 :                :         case T_TidRangeScan:
                               4435                 :                :         case T_ForeignScan:
                               4436                 :                :         case T_CustomScan:
                               4437                 :                :         case T_ModifyTable:
                               4438                 :                :             /* Assert it's on a real relation */
                               4439         [ -  + ]:          20298 :             Assert(rte->rtekind == RTE_RELATION);
                               4440                 :          20298 :             objectname = get_rel_name(rte->relid);
 5972                          4441         [ +  + ]:          20298 :             if (es->verbose)
 1603                          4442                 :           2212 :                 namespace = get_namespace_name_or_temp(get_rel_namespace(rte->relid));
 5972                          4443                 :          20298 :             objecttag = "Relation Name";
 5989                          4444                 :          20298 :             break;
                               4445                 :            288 :         case T_FunctionScan:
                               4446                 :                :             {
 4408                          4447                 :            288 :                 FunctionScan *fscan = (FunctionScan *) plan;
                               4448                 :                : 
                               4449                 :                :                 /* Assert it's on a RangeFunction */
 5989                          4450         [ -  + ]:            288 :                 Assert(rte->rtekind == RTE_FUNCTION);
                               4451                 :                : 
                               4452                 :                :                 /*
                               4453                 :                :                  * If the expression is still a function call of a single
                               4454                 :                :                  * function, we can get the real name of the function.
                               4455                 :                :                  * Otherwise, punt.  (Even if it was a single function call
                               4456                 :                :                  * originally, the optimizer could have simplified it away.)
                               4457                 :                :                  */
 4408                          4458         [ +  - ]:            288 :                 if (list_length(fscan->functions) == 1)
                               4459                 :                :                 {
                               4460                 :            288 :                     RangeTblFunction *rtfunc = (RangeTblFunction *) linitial(fscan->functions);
                               4461                 :                : 
                               4462         [ +  + ]:            288 :                     if (IsA(rtfunc->funcexpr, FuncExpr))
                               4463                 :                :                     {
                               4464                 :            276 :                         FuncExpr   *funcexpr = (FuncExpr *) rtfunc->funcexpr;
                               4465                 :            276 :                         Oid         funcid = funcexpr->funcid;
                               4466                 :                : 
                               4467                 :            276 :                         objectname = get_func_name(funcid);
                               4468         [ +  + ]:            276 :                         if (es->verbose)
 1603                          4469                 :             88 :                             namespace = get_namespace_name_or_temp(get_func_namespace(funcid));
                               4470                 :                :                     }
                               4471                 :                :                 }
 5972                          4472                 :            288 :                 objecttag = "Function Name";
                               4473                 :                :             }
 5989                          4474                 :            288 :             break;
 3205 alvherre@alvh.no-ip.     4475                 :             39 :         case T_TableFuncScan:
                               4476                 :                :             {
  621 amitlan@postgresql.o     4477                 :             39 :                 TableFunc  *tablefunc = ((TableFuncScan *) plan)->tablefunc;
                               4478                 :                : 
                               4479         [ -  + ]:             39 :                 Assert(rte->rtekind == RTE_TABLEFUNC);
                               4480      [ +  +  - ]:             39 :                 switch (tablefunc->functype)
                               4481                 :                :                 {
                               4482                 :             18 :                     case TFT_XMLTABLE:
                               4483                 :             18 :                         objectname = "xmltable";
                               4484                 :             18 :                         break;
                               4485                 :             21 :                     case TFT_JSON_TABLE:
                               4486                 :             21 :                         objectname = "json_table";
                               4487                 :             21 :                         break;
  621 amitlan@postgresql.o     4488                 :UBC           0 :                     default:
                               4489         [ #  # ]:              0 :                         elog(ERROR, "invalid TableFunc type %d",
                               4490                 :                :                              (int) tablefunc->functype);
                               4491                 :                :                 }
  621 amitlan@postgresql.o     4492                 :CBC          39 :                 objecttag = "Table Function Name";
                               4493                 :                :             }
 3205 alvherre@alvh.no-ip.     4494                 :             39 :             break;
 5989 tgl@sss.pgh.pa.us        4495                 :            282 :         case T_ValuesScan:
                               4496         [ -  + ]:            282 :             Assert(rte->rtekind == RTE_VALUES);
                               4497                 :            282 :             break;
                               4498                 :            125 :         case T_CteScan:
                               4499                 :                :             /* Assert it's on a non-self-reference CTE */
                               4500         [ -  + ]:            125 :             Assert(rte->rtekind == RTE_CTE);
                               4501         [ -  + ]:            125 :             Assert(!rte->self_reference);
                               4502                 :            125 :             objectname = rte->ctename;
 5972                          4503                 :            125 :             objecttag = "CTE Name";
 5989                          4504                 :            125 :             break;
 3182 kgrittn@postgresql.o     4505                 :UBC           0 :         case T_NamedTuplestoreScan:
                               4506         [ #  # ]:              0 :             Assert(rte->rtekind == RTE_NAMEDTUPLESTORE);
                               4507                 :              0 :             objectname = rte->enrname;
                               4508                 :              0 :             objecttag = "Tuplestore Name";
                               4509                 :              0 :             break;
 5989 tgl@sss.pgh.pa.us        4510                 :CBC          27 :         case T_WorkTableScan:
                               4511                 :                :             /* Assert it's on a self-reference CTE */
                               4512         [ -  + ]:             27 :             Assert(rte->rtekind == RTE_CTE);
                               4513         [ -  + ]:             27 :             Assert(rte->self_reference);
                               4514                 :             27 :             objectname = rte->ctename;
 5972                          4515                 :             27 :             objecttag = "CTE Name";
 5989                          4516                 :             27 :             break;
                               4517                 :            217 :         default:
                               4518                 :            217 :             break;
                               4519                 :                :     }
                               4520                 :                : 
 5972                          4521         [ +  + ]:          21276 :     if (es->format == EXPLAIN_FORMAT_TEXT)
                               4522                 :                :     {
                               4523                 :          21053 :         appendStringInfoString(es->str, " on");
                               4524         [ +  + ]:          21053 :         if (namespace != NULL)
                               4525                 :           2294 :             appendStringInfo(es->str, " %s.%s", quote_identifier(namespace),
                               4526                 :                :                              quote_identifier(objectname));
                               4527         [ +  + ]:          18759 :         else if (objectname != NULL)
                               4528                 :          18248 :             appendStringInfo(es->str, " %s", quote_identifier(objectname));
 4733                          4529   [ +  +  +  + ]:          21053 :         if (objectname == NULL || strcmp(refname, objectname) != 0)
 4834                          4530                 :          12267 :             appendStringInfo(es->str, " %s", quote_identifier(refname));
                               4531                 :                :     }
                               4532                 :                :     else
                               4533                 :                :     {
 5972                          4534   [ +  -  +  - ]:            223 :         if (objecttag != NULL && objectname != NULL)
                               4535                 :            223 :             ExplainPropertyText(objecttag, objectname, es);
                               4536         [ +  + ]:            223 :         if (namespace != NULL)
                               4537                 :              6 :             ExplainPropertyText("Schema", namespace, es);
 4733                          4538                 :            223 :         ExplainPropertyText("Alias", refname, es);
                               4539                 :                :     }
 5989                          4540                 :          21276 : }
                               4541                 :                : 
                               4542                 :                : /*
                               4543                 :                :  * Show extra information for a ModifyTable node
                               4544                 :                :  *
                               4545                 :                :  * We have three objectives here.  First, if there's more than one target
                               4546                 :                :  * table or it's different from the nominal target, identify the actual
                               4547                 :                :  * target(s).  Second, give FDWs a chance to display extra info about foreign
                               4548                 :                :  * targets.  Third, show information about ON CONFLICT.
                               4549                 :                :  */
                               4550                 :                : static void
 3875 andres@anarazel.de       4551                 :            556 : show_modifytable_info(ModifyTableState *mtstate, List *ancestors,
                               4552                 :                :                       ExplainState *es)
                               4553                 :                : {
 3922 tgl@sss.pgh.pa.us        4554                 :            556 :     ModifyTable *node = (ModifyTable *) mtstate->ps.plan;
                               4555                 :                :     const char *operation;
                               4556                 :                :     const char *foperation;
                               4557                 :                :     bool        labeltargets;
                               4558                 :                :     int         j;
 3875 andres@anarazel.de       4559                 :            556 :     List       *idxNames = NIL;
                               4560                 :                :     ListCell   *lst;
                               4561                 :                : 
 3922 tgl@sss.pgh.pa.us        4562   [ +  +  +  +  :            556 :     switch (node->operation)
                                                 - ]
                               4563                 :                :     {
                               4564                 :            137 :         case CMD_INSERT:
                               4565                 :            137 :             operation = "Insert";
                               4566                 :            137 :             foperation = "Foreign Insert";
                               4567                 :            137 :             break;
                               4568                 :            224 :         case CMD_UPDATE:
                               4569                 :            224 :             operation = "Update";
                               4570                 :            224 :             foperation = "Foreign Update";
                               4571                 :            224 :             break;
                               4572                 :             93 :         case CMD_DELETE:
                               4573                 :             93 :             operation = "Delete";
                               4574                 :             93 :             foperation = "Foreign Delete";
                               4575                 :             93 :             break;
 1359 alvherre@alvh.no-ip.     4576                 :            102 :         case CMD_MERGE:
                               4577                 :            102 :             operation = "Merge";
                               4578                 :                :             /* XXX unsupported for now, but avoid compiler noise */
                               4579                 :            102 :             foperation = "Foreign Merge";
                               4580                 :            102 :             break;
 3922 tgl@sss.pgh.pa.us        4581                 :UBC           0 :         default:
                               4582                 :              0 :             operation = "???";
                               4583                 :              0 :             foperation = "Foreign ???";
                               4584                 :              0 :             break;
                               4585                 :                :     }
                               4586                 :                : 
                               4587                 :                :     /*
                               4588                 :                :      * Should we explicitly label target relations?
                               4589                 :                :      *
                               4590                 :                :      * If there's only one target relation, do not list it if it's the
                               4591                 :                :      * relation named in the query, or if it has been pruned.  (Normally
                               4592                 :                :      * mtstate->resultRelInfo doesn't include pruned relations, but a single
                               4593                 :                :      * pruned target relation may be present, if all other target relations
                               4594                 :                :      * have been pruned.  See ExecInitModifyTable().)
                               4595                 :                :      */
 1721 tgl@sss.pgh.pa.us        4596         [ +  + ]:CBC        1034 :     labeltargets = (mtstate->mt_nrels > 1 ||
                               4597         [ +  - ]:            478 :                     (mtstate->mt_nrels == 1 &&
  272 amitlan@postgresql.o     4598   [ +  +  +  + ]:            544 :                      mtstate->resultRelInfo[0].ri_RangeTableIndex != node->nominalRelation &&
                               4599                 :             66 :                      bms_is_member(mtstate->resultRelInfo[0].ri_RangeTableIndex,
                               4600                 :             66 :                                    mtstate->ps.state->es_unpruned_relids)));
                               4601                 :                : 
 3922 tgl@sss.pgh.pa.us        4602         [ +  + ]:            556 :     if (labeltargets)
                               4603                 :            132 :         ExplainOpenGroup("Target Tables", "Target Tables", false, es);
                               4604                 :                : 
 1721                          4605         [ +  + ]:           1253 :     for (j = 0; j < mtstate->mt_nrels; j++)
                               4606                 :                :     {
 3922                          4607                 :            697 :         ResultRelInfo *resultRelInfo = mtstate->resultRelInfo + j;
                               4608                 :            697 :         FdwRoutine *fdwroutine = resultRelInfo->ri_FdwRoutine;
                               4609                 :                : 
                               4610         [ +  + ]:            697 :         if (labeltargets)
                               4611                 :                :         {
                               4612                 :                :             /* Open a group for this target */
                               4613                 :            273 :             ExplainOpenGroup("Target Table", NULL, true, es);
                               4614                 :                : 
                               4615                 :                :             /*
                               4616                 :                :              * In text mode, decorate each target with operation type, so that
                               4617                 :                :              * ExplainTargetRel's output of " on foo" will read nicely.
                               4618                 :                :              */
                               4619         [ +  - ]:            273 :             if (es->format == EXPLAIN_FORMAT_TEXT)
                               4620                 :                :             {
 2152                          4621                 :            273 :                 ExplainIndentText(es);
 3922                          4622         [ +  + ]:            273 :                 appendStringInfoString(es->str,
                               4623                 :                :                                        fdwroutine ? foperation : operation);
                               4624                 :                :             }
                               4625                 :                : 
                               4626                 :                :             /* Identify target */
                               4627                 :            273 :             ExplainTargetRel((Plan *) node,
                               4628                 :                :                              resultRelInfo->ri_RangeTableIndex,
                               4629                 :                :                              es);
                               4630                 :                : 
                               4631         [ +  - ]:            273 :             if (es->format == EXPLAIN_FORMAT_TEXT)
                               4632                 :                :             {
                               4633                 :            273 :                 appendStringInfoChar(es->str, '\n');
                               4634                 :            273 :                 es->indent++;
                               4635                 :                :             }
                               4636                 :                :         }
                               4637                 :                : 
                               4638                 :                :         /* Give FDW a chance if needed */
 3560 rhaas@postgresql.org     4639   [ +  +  +  + ]:            697 :         if (!resultRelInfo->ri_usesFdwDirectModify &&
                               4640                 :             46 :             fdwroutine != NULL &&
                               4641         [ +  - ]:             46 :             fdwroutine->ExplainForeignModify != NULL)
                               4642                 :                :         {
 3922 tgl@sss.pgh.pa.us        4643                 :             46 :             List       *fdw_private = (List *) list_nth(node->fdwPrivLists, j);
                               4644                 :                : 
                               4645                 :             46 :             fdwroutine->ExplainForeignModify(mtstate,
                               4646                 :                :                                              resultRelInfo,
                               4647                 :                :                                              fdw_private,
                               4648                 :                :                                              j,
                               4649                 :                :                                              es);
                               4650                 :                :         }
                               4651                 :                : 
                               4652         [ +  + ]:            697 :         if (labeltargets)
                               4653                 :                :         {
                               4654                 :                :             /* Undo the indentation we added in text format */
                               4655         [ +  - ]:            273 :             if (es->format == EXPLAIN_FORMAT_TEXT)
                               4656                 :            273 :                 es->indent--;
                               4657                 :                : 
                               4658                 :                :             /* Close the group */
                               4659                 :            273 :             ExplainCloseGroup("Target Table", NULL, true, es);
                               4660                 :                :         }
                               4661                 :                :     }
                               4662                 :                : 
                               4663                 :                :     /* Gather names of ON CONFLICT arbiter indexes */
 3875 andres@anarazel.de       4664   [ +  +  +  +  :            655 :     foreach(lst, node->arbiterIndexes)
                                              +  + ]
                               4665                 :                :     {
                               4666                 :             99 :         char       *indexname = get_rel_name(lfirst_oid(lst));
                               4667                 :                : 
                               4668                 :             99 :         idxNames = lappend(idxNames, indexname);
                               4669                 :                :     }
                               4670                 :                : 
                               4671         [ +  + ]:            556 :     if (node->onConflictAction != ONCONFLICT_NONE)
                               4672                 :                :     {
 2832                          4673                 :             72 :         ExplainPropertyText("Conflict Resolution",
                               4674         [ +  + ]:             72 :                             node->onConflictAction == ONCONFLICT_NOTHING ?
                               4675                 :                :                             "NOTHING" : "UPDATE",
                               4676                 :                :                             es);
                               4677                 :                : 
                               4678                 :                :         /*
                               4679                 :                :          * Don't display arbiter indexes at all when DO NOTHING variant
                               4680                 :                :          * implicitly ignores all conflicts
                               4681                 :                :          */
 3875                          4682         [ +  - ]:             72 :         if (idxNames)
                               4683                 :             72 :             ExplainPropertyList("Conflict Arbiter Indexes", idxNames, es);
                               4684                 :                : 
                               4685                 :                :         /* ON CONFLICT DO UPDATE WHERE qual is specially displayed */
                               4686         [ +  + ]:             72 :         if (node->onConflictWhere)
                               4687                 :                :         {
                               4688                 :             27 :             show_upper_qual((List *) node->onConflictWhere, "Conflict Filter",
                               4689                 :                :                             &mtstate->ps, ancestors, es);
                               4690                 :             27 :             show_instrumentation_count("Rows Removed by Conflict Filter", 1, &mtstate->ps, es);
                               4691                 :                :         }
                               4692                 :                : 
                               4693                 :                :         /* EXPLAIN ANALYZE display of actual outcome for each tuple proposed */
                               4694   [ -  +  -  - ]:             72 :         if (es->analyze && mtstate->ps.instrument)
                               4695                 :                :         {
                               4696                 :                :             double      total;
                               4697                 :                :             double      insert_path;
                               4698                 :                :             double      other_path;
                               4699                 :                : 
 1721 tgl@sss.pgh.pa.us        4700                 :UBC           0 :             InstrEndLoop(outerPlanState(mtstate)->instrument);
                               4701                 :                : 
                               4702                 :                :             /* count the number of source rows */
                               4703                 :              0 :             total = outerPlanState(mtstate)->instrument->ntuples;
 2807 alvherre@alvh.no-ip.     4704                 :              0 :             other_path = mtstate->ps.instrument->ntuples2;
 3875 andres@anarazel.de       4705                 :              0 :             insert_path = total - other_path;
                               4706                 :                : 
 2832                          4707                 :              0 :             ExplainPropertyFloat("Tuples Inserted", NULL,
                               4708                 :                :                                  insert_path, 0, es);
                               4709                 :              0 :             ExplainPropertyFloat("Conflicting Tuples", NULL,
                               4710                 :                :                                  other_path, 0, es);
                               4711                 :                :         }
                               4712                 :                :     }
 1359 alvherre@alvh.no-ip.     4713         [ +  + ]:CBC         484 :     else if (node->operation == CMD_MERGE)
                               4714                 :                :     {
                               4715                 :                :         /* EXPLAIN ANALYZE display of tuples processed */
                               4716   [ +  +  +  - ]:            102 :         if (es->analyze && mtstate->ps.instrument)
                               4717                 :                :         {
                               4718                 :                :             double      total;
                               4719                 :                :             double      insert_path;
                               4720                 :                :             double      update_path;
                               4721                 :                :             double      delete_path;
                               4722                 :                :             double      skipped_path;
                               4723                 :                : 
                               4724                 :             27 :             InstrEndLoop(outerPlanState(mtstate)->instrument);
                               4725                 :                : 
                               4726                 :                :             /* count the number of source rows */
                               4727                 :             27 :             total = outerPlanState(mtstate)->instrument->ntuples;
                               4728                 :             27 :             insert_path = mtstate->mt_merge_inserted;
                               4729                 :             27 :             update_path = mtstate->mt_merge_updated;
                               4730                 :             27 :             delete_path = mtstate->mt_merge_deleted;
                               4731                 :             27 :             skipped_path = total - insert_path - update_path - delete_path;
                               4732         [ -  + ]:             27 :             Assert(skipped_path >= 0);
                               4733                 :                : 
 1308                          4734         [ +  - ]:             27 :             if (es->format == EXPLAIN_FORMAT_TEXT)
                               4735                 :                :             {
                               4736         [ +  + ]:             27 :                 if (total > 0)
                               4737                 :                :                 {
                               4738                 :             24 :                     ExplainIndentText(es);
                               4739                 :             24 :                     appendStringInfoString(es->str, "Tuples:");
                               4740         [ +  + ]:             24 :                     if (insert_path > 0)
                               4741                 :              8 :                         appendStringInfo(es->str, " inserted=%.0f", insert_path);
                               4742         [ +  + ]:             24 :                     if (update_path > 0)
                               4743                 :             15 :                         appendStringInfo(es->str, " updated=%.0f", update_path);
                               4744         [ +  + ]:             24 :                     if (delete_path > 0)
                               4745                 :              6 :                         appendStringInfo(es->str, " deleted=%.0f", delete_path);
                               4746         [ +  + ]:             24 :                     if (skipped_path > 0)
                               4747                 :             18 :                         appendStringInfo(es->str, " skipped=%.0f", skipped_path);
                               4748                 :             24 :                     appendStringInfoChar(es->str, '\n');
                               4749                 :                :                 }
                               4750                 :                :             }
                               4751                 :                :             else
                               4752                 :                :             {
 1308 alvherre@alvh.no-ip.     4753                 :UBC           0 :                 ExplainPropertyFloat("Tuples Inserted", NULL, insert_path, 0, es);
                               4754                 :              0 :                 ExplainPropertyFloat("Tuples Updated", NULL, update_path, 0, es);
                               4755                 :              0 :                 ExplainPropertyFloat("Tuples Deleted", NULL, delete_path, 0, es);
                               4756                 :              0 :                 ExplainPropertyFloat("Tuples Skipped", NULL, skipped_path, 0, es);
                               4757                 :                :             }
                               4758                 :                :         }
                               4759                 :                :     }
                               4760                 :                : 
 3922 tgl@sss.pgh.pa.us        4761         [ +  + ]:CBC         556 :     if (labeltargets)
                               4762                 :            132 :         ExplainCloseGroup("Target Tables", "Target Tables", false, es);
 4664                          4763                 :            556 : }
                               4764                 :                : 
                               4765                 :                : /*
                               4766                 :                :  * Explain what a "Result" node replaced.
                               4767                 :                :  */
                               4768                 :                : static void
   84 rhaas@postgresql.org     4769                 :GNC        1540 : show_result_replacement_info(Result *result, ExplainState *es)
                               4770                 :                : {
                               4771                 :                :     StringInfoData buf;
                               4772                 :           1540 :     int         nrels = 0;
                               4773                 :           1540 :     int         rti = -1;
                               4774                 :           1540 :     bool        found_non_result = false;
                               4775                 :           1540 :     char       *replacement_type = "???";
                               4776                 :                : 
                               4777                 :                :     /* If the Result node has a subplan, it didn't replace anything. */
                               4778         [ +  + ]:           1540 :     if (result->plan.lefttree != NULL)
                               4779                 :           1173 :         return;
                               4780                 :                : 
                               4781                 :                :     /* Gating result nodes should have a subplan, and we don't. */
                               4782         [ -  + ]:           1416 :     Assert(result->result_type != RESULT_TYPE_GATING);
                               4783                 :                : 
                               4784   [ -  +  +  +  :           1416 :     switch (result->result_type)
                                              +  - ]
                               4785                 :                :     {
   84 rhaas@postgresql.org     4786                 :UNC           0 :         case RESULT_TYPE_GATING:
                               4787                 :              0 :             replacement_type = "Gating";
                               4788                 :              0 :             break;
   84 rhaas@postgresql.org     4789                 :GNC        1245 :         case RESULT_TYPE_SCAN:
                               4790                 :           1245 :             replacement_type = "Scan";
                               4791                 :           1245 :             break;
                               4792                 :             60 :         case RESULT_TYPE_JOIN:
                               4793                 :             60 :             replacement_type = "Join";
                               4794                 :             60 :             break;
                               4795                 :             33 :         case RESULT_TYPE_UPPER:
                               4796                 :                :             /* a small white lie */
                               4797                 :             33 :             replacement_type = "Aggregate";
                               4798                 :             33 :             break;
                               4799                 :             78 :         case RESULT_TYPE_MINMAX:
                               4800                 :             78 :             replacement_type = "MinMaxAggregate";
                               4801                 :             78 :             break;
                               4802                 :                :     }
                               4803                 :                : 
                               4804                 :                :     /*
                               4805                 :                :      * Build up a comma-separated list of user-facing names for the range
                               4806                 :                :      * table entries in the relids set.
                               4807                 :                :      */
                               4808                 :           1416 :     initStringInfo(&buf);
                               4809         [ +  + ]:           2955 :     while ((rti = bms_next_member(result->relids, rti)) >= 0)
                               4810                 :                :     {
                               4811                 :           1539 :         RangeTblEntry *rte = rt_fetch(rti, es->rtable);
                               4812                 :                :         char       *refname;
                               4813                 :                : 
                               4814                 :                :         /*
                               4815                 :                :          * add_outer_joins_to_relids will add join RTIs to the relids set of a
                               4816                 :                :          * join; if that join is then replaced with a Result node, we may see
                               4817                 :                :          * such RTIs here. But we want to completely ignore those here,
                               4818                 :                :          * because "a LEFT JOIN b ON whatever" is a join between a and b, not
                               4819                 :                :          * a join between a, b, and an unnamed join.
                               4820                 :                :          */
                               4821         [ +  + ]:           1539 :         if (rte->rtekind == RTE_JOIN)
                               4822                 :             60 :             continue;
                               4823                 :                : 
                               4824                 :                :         /* Count the number of rels that aren't ignored completely. */
                               4825                 :           1479 :         ++nrels;
                               4826                 :                : 
                               4827                 :                :         /* Work out what reference name to use and add it to the string. */
                               4828                 :           1479 :         refname = (char *) list_nth(es->rtable_names, rti - 1);
                               4829         [ -  + ]:           1479 :         if (refname == NULL)
   84 rhaas@postgresql.org     4830                 :UNC           0 :             refname = rte->eref->aliasname;
   84 rhaas@postgresql.org     4831         [ +  + ]:GNC        1479 :         if (buf.len > 0)
                               4832                 :            159 :             appendStringInfoString(&buf, ", ");
                               4833                 :           1479 :         appendStringInfoString(&buf, refname);
                               4834                 :                : 
                               4835                 :                :         /* Keep track of whether we see anything other than RTE_RESULT. */
                               4836         [ +  + ]:           1479 :         if (rte->rtekind != RTE_RESULT)
                               4837                 :            427 :             found_non_result = true;
                               4838                 :                :     }
                               4839                 :                : 
                               4840                 :                :     /*
                               4841                 :                :      * If this Result node is because of a single RTE that is RTE_RESULT, it
                               4842                 :                :      * is not really replacing anything at all, because there's no other
                               4843                 :                :      * method for implementing a scan of such an RTE, so we don't display the
                               4844                 :                :      * Replaces line in such cases.
                               4845                 :                :      */
                               4846   [ +  +  +  + ]:           1416 :     if (nrels <= 1 && !found_non_result &&
                               4847         [ +  + ]:           1145 :         result->result_type == RESULT_TYPE_SCAN)
                               4848                 :           1049 :         return;
                               4849                 :                : 
                               4850                 :                :     /* Say what we replaced, with list of rels if available. */
                               4851         [ +  + ]:            367 :     if (buf.len == 0)
                               4852                 :             96 :         ExplainPropertyText("Replaces", replacement_type, es);
                               4853                 :                :     else
                               4854                 :                :     {
                               4855                 :            271 :         char       *s = psprintf("%s on %s", replacement_type, buf.data);
                               4856                 :                : 
                               4857                 :            271 :         ExplainPropertyText("Replaces", s, es);
                               4858                 :                :     }
                               4859                 :                : }
                               4860                 :                : 
                               4861                 :                : /*
                               4862                 :                :  * Explain the constituent plans of an Append, MergeAppend,
                               4863                 :                :  * BitmapAnd, or BitmapOr node.
                               4864                 :                :  *
                               4865                 :                :  * The ancestors list should already contain the immediate parent of these
                               4866                 :                :  * plans.
                               4867                 :                :  */
                               4868                 :                : static void
 2142 tgl@sss.pgh.pa.us        4869                 :CBC        2045 : ExplainMemberNodes(PlanState **planstates, int nplans,
                               4870                 :                :                    List *ancestors, ExplainState *es)
                               4871                 :                : {
                               4872                 :                :     int         j;
                               4873                 :                : 
                               4874         [ +  + ]:           8256 :     for (j = 0; j < nplans; j++)
 5635                          4875                 :           6211 :         ExplainNode(planstates[j], ancestors,
                               4876                 :                :                     "Member", NULL, es);
 5989                          4877                 :           2045 : }
                               4878                 :                : 
                               4879                 :                : /*
                               4880                 :                :  * Report about any pruned subnodes of an Append or MergeAppend node.
                               4881                 :                :  *
                               4882                 :                :  * nplans indicates the number of live subplans.
                               4883                 :                :  * nchildren indicates the original number of subnodes in the Plan;
                               4884                 :                :  * some of these may have been pruned by the run-time pruning code.
                               4885                 :                :  */
                               4886                 :                : static void
 2142                          4887                 :           1951 : ExplainMissingMembers(int nplans, int nchildren, ExplainState *es)
                               4888                 :                : {
                               4889   [ +  +  +  + ]:           1951 :     if (nplans < nchildren || es->format != EXPLAIN_FORMAT_TEXT)
                               4890                 :            126 :         ExplainPropertyInteger("Subplans Removed", NULL,
                               4891                 :            126 :                                nchildren - nplans, es);
                               4892                 :           1951 : }
                               4893                 :                : 
                               4894                 :                : /*
                               4895                 :                :  * Explain a list of SubPlans (or initPlans, which also use SubPlan nodes).
                               4896                 :                :  *
                               4897                 :                :  * The ancestors list should already contain the immediate parent of these
                               4898                 :                :  * SubPlans.
                               4899                 :                :  */
                               4900                 :                : static void
 5635                          4901                 :            894 : ExplainSubPlans(List *plans, List *ancestors,
                               4902                 :                :                 const char *relationship, ExplainState *es)
                               4903                 :                : {
                               4904                 :                :     ListCell   *lst;
                               4905                 :                : 
 5989                          4906   [ +  -  +  +  :           1890 :     foreach(lst, plans)
                                              +  + ]
                               4907                 :                :     {
                               4908                 :            996 :         SubPlanState *sps = (SubPlanState *) lfirst(lst);
 3199 andres@anarazel.de       4909                 :            996 :         SubPlan    *sp = sps->subplan;
                               4910                 :                :         char       *cooked_plan_name;
                               4911                 :                : 
                               4912                 :                :         /*
                               4913                 :                :          * There can be multiple SubPlan nodes referencing the same physical
                               4914                 :                :          * subplan (same plan_id, which is its index in PlannedStmt.subplans).
                               4915                 :                :          * We should print a subplan only once, so track which ones we already
                               4916                 :                :          * printed.  This state must be global across the plan tree, since the
                               4917                 :                :          * duplicate nodes could be in different plan nodes, eg both a bitmap
                               4918                 :                :          * indexscan's indexqual and its parent heapscan's recheck qual.  (We
                               4919                 :                :          * do not worry too much about which plan node we show the subplan as
                               4920                 :                :          * attached to in such cases.)
                               4921                 :                :          */
 3445 tgl@sss.pgh.pa.us        4922         [ +  + ]:            996 :         if (bms_is_member(sp->plan_id, es->printed_subplans))
                               4923                 :             45 :             continue;
                               4924                 :            951 :         es->printed_subplans = bms_add_member(es->printed_subplans,
                               4925                 :                :                                               sp->plan_id);
                               4926                 :                : 
                               4927                 :                :         /*
                               4928                 :                :          * Treat the SubPlan node as an ancestor of the plan node(s) within
                               4929                 :                :          * it, so that ruleutils.c can find the referents of subplan
                               4930                 :                :          * parameters.
                               4931                 :                :          */
 2197                          4932                 :            951 :         ancestors = lcons(sp, ancestors);
                               4933                 :                : 
                               4934                 :                :         /*
                               4935                 :                :          * The plan has a name like exists_1 or rowcompare_2, but here we want
                               4936                 :                :          * to prefix that with CTE, InitPlan, or SubPlan, as appropriate, for
                               4937                 :                :          * display purposes.
                               4938                 :                :          */
   70 rhaas@postgresql.org     4939         [ +  + ]:GNC         951 :         if (sp->subLinkType == CTE_SUBLINK)
                               4940                 :            119 :             cooked_plan_name = psprintf("CTE %s", sp->plan_name);
                               4941         [ +  + ]:            832 :         else if (sp->isInitPlan)
                               4942                 :            529 :             cooked_plan_name = psprintf("InitPlan %s", sp->plan_name);
                               4943                 :                :         else
                               4944                 :            303 :             cooked_plan_name = psprintf("SubPlan %s", sp->plan_name);
                               4945                 :                : 
 5635 tgl@sss.pgh.pa.us        4946                 :CBC         951 :         ExplainNode(sps->planstate, ancestors,
                               4947                 :                :                     relationship, cooked_plan_name, es);
                               4948                 :                : 
 2197                          4949                 :            951 :         ancestors = list_delete_first(ancestors);
                               4950                 :                :     }
 5972                          4951                 :            894 : }
                               4952                 :                : 
                               4953                 :                : /*
                               4954                 :                :  * Explain a list of children of a CustomScan.
                               4955                 :                :  */
                               4956                 :                : static void
 3826 rhaas@postgresql.org     4957                 :UBC           0 : ExplainCustomChildren(CustomScanState *css, List *ancestors, ExplainState *es)
                               4958                 :                : {
                               4959                 :                :     ListCell   *cell;
                               4960                 :              0 :     const char *label =
  942 tgl@sss.pgh.pa.us        4961         [ #  # ]:              0 :         (list_length(css->custom_ps) != 1 ? "children" : "child");
                               4962                 :                : 
 3797                          4963   [ #  #  #  #  :              0 :     foreach(cell, css->custom_ps)
                                              #  # ]
 3826 rhaas@postgresql.org     4964                 :              0 :         ExplainNode((PlanState *) lfirst(cell), ancestors, label, NULL, es);
                               4965                 :              0 : }
                               4966                 :                : 
                               4967                 :                : /*
                               4968                 :                :  * Create a per-plan-node workspace for collecting per-worker data.
                               4969                 :                :  *
                               4970                 :                :  * Output related to each worker will be temporarily "set aside" into a
                               4971                 :                :  * separate buffer, which we'll merge into the main output stream once
                               4972                 :                :  * we've processed all data for the plan node.  This makes it feasible to
                               4973                 :                :  * generate a coherent sub-group of fields for each worker, even though the
                               4974                 :                :  * code that produces the fields is in several different places in this file.
                               4975                 :                :  * Formatting of such a set-aside field group is managed by
                               4976                 :                :  * ExplainOpenSetAsideGroup and ExplainSaveGroup/ExplainRestoreGroup.
                               4977                 :                :  */
                               4978                 :                : static ExplainWorkersState *
 2152 tgl@sss.pgh.pa.us        4979                 :CBC         513 : ExplainCreateWorkersState(int num_workers)
                               4980                 :                : {
                               4981                 :                :     ExplainWorkersState *wstate;
                               4982                 :                : 
    6 michael@paquier.xyz      4983                 :GNC         513 :     wstate = palloc_object(ExplainWorkersState);
 2152 tgl@sss.pgh.pa.us        4984                 :CBC         513 :     wstate->num_workers = num_workers;
                               4985                 :            513 :     wstate->worker_inited = (bool *) palloc0(num_workers * sizeof(bool));
                               4986                 :            513 :     wstate->worker_str = (StringInfoData *)
                               4987                 :            513 :         palloc0(num_workers * sizeof(StringInfoData));
                               4988                 :            513 :     wstate->worker_state_save = (int *) palloc(num_workers * sizeof(int));
                               4989                 :            513 :     return wstate;
                               4990                 :                : }
                               4991                 :                : 
                               4992                 :                : /*
                               4993                 :                :  * Begin or resume output into the set-aside group for worker N.
                               4994                 :                :  */
                               4995                 :                : static void
                               4996                 :             72 : ExplainOpenWorker(int n, ExplainState *es)
                               4997                 :                : {
                               4998                 :             72 :     ExplainWorkersState *wstate = es->workers_state;
                               4999                 :                : 
                               5000         [ -  + ]:             72 :     Assert(wstate);
                               5001   [ +  -  -  + ]:             72 :     Assert(n >= 0 && n < wstate->num_workers);
                               5002                 :                : 
                               5003                 :                :     /* Save prior output buffer pointer */
                               5004                 :             72 :     wstate->prev_str = es->str;
                               5005                 :                : 
                               5006         [ +  + ]:             72 :     if (!wstate->worker_inited[n])
                               5007                 :                :     {
                               5008                 :                :         /* First time through, so create the buffer for this worker */
                               5009                 :             36 :         initStringInfo(&wstate->worker_str[n]);
                               5010                 :             36 :         es->str = &wstate->worker_str[n];
                               5011                 :                : 
                               5012                 :                :         /*
                               5013                 :                :          * Push suitable initial formatting state for this worker's field
                               5014                 :                :          * group.  We allow one extra logical nesting level, since this group
                               5015                 :                :          * will eventually be wrapped in an outer "Workers" group.
                               5016                 :                :          */
                               5017                 :             36 :         ExplainOpenSetAsideGroup("Worker", NULL, true, 2, es);
                               5018                 :                : 
                               5019                 :                :         /*
                               5020                 :                :          * In non-TEXT formats we always emit a "Worker Number" field, even if
                               5021                 :                :          * there's no other data for this worker.
                               5022                 :                :          */
                               5023         [ +  + ]:             36 :         if (es->format != EXPLAIN_FORMAT_TEXT)
                               5024                 :             24 :             ExplainPropertyInteger("Worker Number", NULL, n, es);
                               5025                 :                : 
                               5026                 :             36 :         wstate->worker_inited[n] = true;
                               5027                 :                :     }
                               5028                 :                :     else
                               5029                 :                :     {
                               5030                 :                :         /* Resuming output for a worker we've already emitted some data for */
                               5031                 :             36 :         es->str = &wstate->worker_str[n];
                               5032                 :                : 
                               5033                 :                :         /* Restore formatting state saved by last ExplainCloseWorker() */
                               5034                 :             36 :         ExplainRestoreGroup(es, 2, &wstate->worker_state_save[n]);
                               5035                 :                :     }
                               5036                 :                : 
                               5037                 :                :     /*
                               5038                 :                :      * In TEXT format, prefix the first output line for this worker with
                               5039                 :                :      * "Worker N:".  Then, any additional lines should be indented one more
                               5040                 :                :      * stop than the "Worker N" line is.
                               5041                 :                :      */
                               5042         [ +  + ]:             72 :     if (es->format == EXPLAIN_FORMAT_TEXT)
                               5043                 :                :     {
                               5044         [ +  - ]:             12 :         if (es->str->len == 0)
                               5045                 :                :         {
                               5046                 :             12 :             ExplainIndentText(es);
                               5047                 :             12 :             appendStringInfo(es->str, "Worker %d:  ", n);
                               5048                 :                :         }
                               5049                 :                : 
                               5050                 :             12 :         es->indent++;
                               5051                 :                :     }
                               5052                 :             72 : }
                               5053                 :                : 
                               5054                 :                : /*
                               5055                 :                :  * End output for worker N --- must pair with previous ExplainOpenWorker call
                               5056                 :                :  */
                               5057                 :                : static void
                               5058                 :             72 : ExplainCloseWorker(int n, ExplainState *es)
                               5059                 :                : {
                               5060                 :             72 :     ExplainWorkersState *wstate = es->workers_state;
                               5061                 :                : 
                               5062         [ -  + ]:             72 :     Assert(wstate);
                               5063   [ +  -  -  + ]:             72 :     Assert(n >= 0 && n < wstate->num_workers);
                               5064         [ -  + ]:             72 :     Assert(wstate->worker_inited[n]);
                               5065                 :                : 
                               5066                 :                :     /*
                               5067                 :                :      * Save formatting state in case we do another ExplainOpenWorker(), then
                               5068                 :                :      * pop the formatting stack.
                               5069                 :                :      */
                               5070                 :             72 :     ExplainSaveGroup(es, 2, &wstate->worker_state_save[n]);
                               5071                 :                : 
                               5072                 :                :     /*
                               5073                 :                :      * In TEXT format, if we didn't actually produce any output line(s) then
                               5074                 :                :      * truncate off the partial line emitted by ExplainOpenWorker.  (This is
                               5075                 :                :      * to avoid bogus output if, say, show_buffer_usage chooses not to print
                               5076                 :                :      * anything for the worker.)  Also fix up the indent level.
                               5077                 :                :      */
                               5078         [ +  + ]:             72 :     if (es->format == EXPLAIN_FORMAT_TEXT)
                               5079                 :                :     {
                               5080   [ +  -  -  + ]:             12 :         while (es->str->len > 0 && es->str->data[es->str->len - 1] != '\n')
 2152 tgl@sss.pgh.pa.us        5081                 :UBC           0 :             es->str->data[--(es->str->len)] = '\0';
                               5082                 :                : 
 2152 tgl@sss.pgh.pa.us        5083                 :CBC          12 :         es->indent--;
                               5084                 :                :     }
                               5085                 :                : 
                               5086                 :                :     /* Restore prior output buffer pointer */
                               5087                 :             72 :     es->str = wstate->prev_str;
                               5088                 :             72 : }
                               5089                 :                : 
                               5090                 :                : /*
                               5091                 :                :  * Print per-worker info for current node, then free the ExplainWorkersState.
                               5092                 :                :  */
                               5093                 :                : static void
                               5094                 :            513 : ExplainFlushWorkersState(ExplainState *es)
                               5095                 :                : {
                               5096                 :            513 :     ExplainWorkersState *wstate = es->workers_state;
                               5097                 :                : 
                               5098                 :            513 :     ExplainOpenGroup("Workers", "Workers", false, es);
                               5099         [ +  + ]:           1353 :     for (int i = 0; i < wstate->num_workers; i++)
                               5100                 :                :     {
                               5101         [ +  + ]:            840 :         if (wstate->worker_inited[i])
                               5102                 :                :         {
                               5103                 :                :             /* This must match previous ExplainOpenSetAsideGroup call */
                               5104                 :             36 :             ExplainOpenGroup("Worker", NULL, true, es);
                               5105                 :             36 :             appendStringInfoString(es->str, wstate->worker_str[i].data);
                               5106                 :             36 :             ExplainCloseGroup("Worker", NULL, true, es);
                               5107                 :                : 
                               5108                 :             36 :             pfree(wstate->worker_str[i].data);
                               5109                 :                :         }
                               5110                 :                :     }
                               5111                 :            513 :     ExplainCloseGroup("Workers", "Workers", false, es);
                               5112                 :                : 
                               5113                 :            513 :     pfree(wstate->worker_inited);
                               5114                 :            513 :     pfree(wstate->worker_str);
                               5115                 :            513 :     pfree(wstate->worker_state_save);
                               5116                 :            513 :     pfree(wstate);
                               5117                 :            513 : }
        

Generated by: LCOV version 2.4-beta