LCOV - differential code coverage report
Current view: top level - src/backend/commands - explain.c (source / functions) Coverage Total Hit UNC LBC UBC GIC GNC CBC ECB DUB DCB
Current: 0e5ff9b9b45a657aea12440478dc002e9b01f138 vs 0123ce131fca454009439dfa3b2266d1d40737d7 Lines: 79.7 % 2357 1878 24 7 448 81 1797 3 3 10
Current Date: 2026-03-14 14:10:32 -0400 Functions: 95.8 % 71 68 1 1 1 12 56
Baseline: lcov-20260315-024220-baseline Branches: 69.5 % 1543 1073 18 5 447 1 44 1028 5 7
Baseline Date: 2026-03-14 15:27:56 +0100 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(30,360] days: 69.7 % 145 101 24 20 81 20
(360..) days: 80.3 % 2212 1777 7 428 1777 3
Function coverage date bins:
(30,360] days: 100.0 % 2 2 1 1
(360..) days: 95.7 % 69 66 1 1 1 11 55
Branch coverage date bins:
(30,360] days: 74.3 % 70 52 18 44 8
(360..) days: 69.3 % 1473 1021 5 447 1 1020

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

Generated by: LCOV version 2.4-beta