From a6bc82b5112cb424058b985c3028ca6b61d1c831 Mon Sep 17 00:00:00 2001
From: Tatsuro Yamada <yamatattsu@gmail.com>
Date: Mon, 10 Feb 2025 08:40:57 +0900
Subject: [PATCH 2/3] Add a new option auto_explain.log_stats to auto_explain
 (T9)

---
 contrib/auto_explain/auto_explain.c | 13 +++++++++++++
 doc/src/sgml/auto-explain.sgml      | 18 ++++++++++++++++++
 2 files changed, 31 insertions(+)

diff --git a/contrib/auto_explain/auto_explain.c b/contrib/auto_explain/auto_explain.c
index f1ad876e821..564b7b5cdbb 100644
--- a/contrib/auto_explain/auto_explain.c
+++ b/contrib/auto_explain/auto_explain.c
@@ -29,6 +29,7 @@ static bool auto_explain_log_analyze = false;
 static bool auto_explain_log_verbose = false;
 static bool auto_explain_log_buffers = false;
 static bool auto_explain_log_wal = false;
+static bool auto_explain_log_stats = false;
 static bool auto_explain_log_triggers = false;
 static bool auto_explain_log_timing = true;
 static bool auto_explain_log_settings = false;
@@ -170,6 +171,17 @@ _PG_init(void)
 							 NULL,
 							 NULL);
 
+	DefineCustomBoolVariable("auto_explain.log_stats",
+							 "Use EXPLAIN STATS for plan logging.",
+							 NULL,
+							 &auto_explain_log_stats,
+							 false,
+							 PGC_SUSET,
+							 0,
+							 NULL,
+							 NULL,
+							 NULL);
+
 	DefineCustomBoolVariable("auto_explain.log_triggers",
 							 "Include trigger statistics in plans.",
 							 "This has no effect unless log_analyze is also set.",
@@ -396,6 +408,7 @@ explain_ExecutorEnd(QueryDesc *queryDesc)
 			es->summary = es->analyze;
 			/* No support for MEMORY */
 			/* es->memory = false; */
+			es->stats = auto_explain_log_stats;
 			es->format = auto_explain_log_format;
 			es->settings = auto_explain_log_settings;
 
diff --git a/doc/src/sgml/auto-explain.sgml b/doc/src/sgml/auto-explain.sgml
index 0c4656ee302..a3be3f38adc 100644
--- a/doc/src/sgml/auto-explain.sgml
+++ b/doc/src/sgml/auto-explain.sgml
@@ -148,6 +148,24 @@ LOAD 'auto_explain';
     </listitem>
    </varlistentry>
 
+   <varlistentry id="auto-explain-configuration-parameters-log-stats">
+    <term>
+     <varname>auto_explain.log_stats</varname> (<type>boolean</type>)
+     <indexterm>
+      <primary><varname>auto_explain.log_stats</varname> configuration parameter</primary>
+     </indexterm>
+    </term>
+    <listitem>
+     <para>
+      <varname>auto_explain.log_stats</varname> controls whether applied
+      Extended Statistic are printed when an execution plan is logged; it's
+      equivalent to the <literal>STATS</literal> option of <command>EXPLAIN</command>.
+      This parameter is off by default.
+      Only superusers can change this setting.
+     </para>
+    </listitem>
+   </varlistentry>
+
    <varlistentry id="auto-explain-configuration-parameters-log-timing">
     <term>
      <varname>auto_explain.log_timing</varname> (<type>boolean</type>)
-- 
2.43.5

