LCOV - differential code coverage report
Current view: top level - src/backend/commands - explain.c (source / functions) Coverage Total Hit UNC LBC UBC GBC GNC CBC DUB DCB
Current: 4b3c9325757dfd8f2312e9f1a8decf4431f344ae vs e87b024bfb8dd55e3049b1b6e6ab4e810cf1abc8 Lines: 79.4 % 2484 1973 27 484 27 44 1902 9 18
Current Date: 2026-11-28 16:48:44 +0900 Functions: 97.3 % 74 72 2 1 11 60 2
Baseline: lcov-20261128-baseline Branches: 68.8 % 1621 1116 21 2 482 15 17 1084
Baseline Date: 2026-11-28 16:48:48 +0900 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(7,30] days: 73.5 % 34 25 9 19 6
(30,360] days: 54.7 % 172 94 18 60 25 69
(360..) days: 81.4 % 2278 1854 424 27 1827
Function coverage date bins:
(7,30] days: 100.0 % 1 1 1
(30,360] days: 100.0 % 4 4 2 2
(360..) days: 97.1 % 69 67 2 1 8 58
Branch coverage date bins:
(7,30] days: 45.8 % 24 11 11 2 7 4
(30,360] days: 40.8 % 76 31 10 2 33 10 21
(360..) days: 70.6 % 1521 1074 447 15 1059

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

Generated by: LCOV version 2.0-1