From 253a6906eb3ed19d8de2b089fa581710fd9886a5 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 1f4badb4928..20fbcc69025 100644
--- a/contrib/auto_explain/auto_explain.c
+++ b/contrib/auto_explain/auto_explain.c
@@ -34,6 +34,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;
@@ -175,6 +176,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.",
@@ -401,6 +413,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 15c868021e6..96599b12f3b 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.39.5

