From c62932bfe13e3f13cc5b8d1b78bedaf379f2a954 Mon Sep 17 00:00:00 2001
From: Sami Imseih <samimseih@gmail.com>
Date: Thu, 14 May 2026 12:39:23 -0500
Subject: [PATCH v1 4/4] pg_stat_statements: extend pg_stat_statements_info
 view

Add new columns to the pg_stat_statements_info view:

  - query_text_memory_bytes: total DSA memory used for query texts
  - query_text_file_bytes: size of the external query text overflow file
  - num_entries: current number of tracked statement entries
  - last_eviction_time: timestamp of the most recent eviction cycle
---
 .../pg_stat_statements--1.12--1.13.sql        | 23 ++++++++++++
 .../pg_stat_statements/pg_stat_statements.c   | 17 ++++++++-
 doc/src/sgml/pgstatstatements.sgml            | 36 +++++++++++++++++++
 3 files changed, 75 insertions(+), 1 deletion(-)

diff --git a/contrib/pg_stat_statements/pg_stat_statements--1.12--1.13.sql b/contrib/pg_stat_statements/pg_stat_statements--1.12--1.13.sql
index 2f0eaf14ec3..7d1cbfcc701 100644
--- a/contrib/pg_stat_statements/pg_stat_statements--1.12--1.13.sql
+++ b/contrib/pg_stat_statements/pg_stat_statements--1.12--1.13.sql
@@ -76,3 +76,26 @@ CREATE VIEW pg_stat_statements AS
   SELECT * FROM pg_stat_statements(true);
 
 GRANT SELECT ON pg_stat_statements TO PUBLIC;
+
+ALTER EXTENSION pg_stat_statements DROP VIEW pg_stat_statements_info;
+ALTER EXTENSION pg_stat_statements DROP FUNCTION pg_stat_statements_info();
+
+DROP VIEW pg_stat_statements_info;
+DROP FUNCTION pg_stat_statements_info();
+
+CREATE FUNCTION pg_stat_statements_info(
+    OUT dealloc bigint,
+    OUT stats_reset timestamp with time zone,
+    OUT query_text_memory_bytes bigint,
+    OUT query_text_file_bytes bigint,
+    OUT num_entries bigint,
+    OUT last_eviction_time timestamp with time zone
+)
+RETURNS record
+AS 'MODULE_PATHNAME'
+LANGUAGE C STRICT VOLATILE PARALLEL SAFE;
+
+CREATE VIEW pg_stat_statements_info AS
+  SELECT * FROM pg_stat_statements_info();
+
+GRANT SELECT ON pg_stat_statements_info TO PUBLIC;
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index af7637b25b9..6c8dd695ed6 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -1976,7 +1976,7 @@ pg_stat_statements_internal(FunctionCallInfo fcinfo,
 }
 
 /* Number of output arguments (columns) for pg_stat_statements_info */
-#define PG_STAT_STATEMENTS_INFO_COLS	2
+#define PG_STAT_STATEMENTS_INFO_COLS	6
 
 /*
  * Return statistics of pg_stat_statements.
@@ -2006,6 +2006,21 @@ pg_stat_statements_info(PG_FUNCTION_ARGS)
 
 	values[i++] = Int64GetDatum(stats.dealloc);
 	values[i++] = TimestampTzGetDatum(stats.stats_reset);
+	/* query_text_memory_bytes: DSA memory used for query texts */
+	pgss_qtext_dsa_attach();
+	if (pgss_qtext_dsa)
+		values[i++] = Int64GetDatum(dsa_get_total_size(pgss_qtext_dsa));
+	else
+		nulls[i++] = true;
+	/* query_text_file_bytes: overflow file size */
+	values[i++] = Int64GetDatum(pgss->extent);
+	/* num_entries: current entry count */
+	values[i++] = Int64GetDatum(pgstat_get_entry_count(PGSTAT_KIND_PGSS));
+	/* last eviction time */
+	if (pgss->last_eviction_time != 0)
+		values[i++] = TimestampTzGetDatum(pgss->last_eviction_time);
+	else
+		nulls[i++] = true;
 
 	PG_RETURN_DATUM(HeapTupleGetDatum(heap_form_tuple(tupdesc, values, nulls)));
 }
diff --git a/doc/src/sgml/pgstatstatements.sgml b/doc/src/sgml/pgstatstatements.sgml
index 0f4ab34965b..2221212574d 100644
--- a/doc/src/sgml/pgstatstatements.sgml
+++ b/doc/src/sgml/pgstatstatements.sgml
@@ -809,6 +809,42 @@ calls | 2
        <structname>pg_stat_statements</structname> view were last reset.
       </para></entry>
      </row>
+     <row>
+      <entry role="catalog_table_entry"><para role="column_definition">
+       <structfield>query_text_memory_bytes</structfield> <type>bigint</type>
+      </para>
+      <para>
+       Total bytes of dynamic shared memory used for query text storage.
+      </para></entry>
+     </row>
+     <row>
+      <entry role="catalog_table_entry"><para role="column_definition">
+       <structfield>query_text_file_bytes</structfield> <type>bigint</type>
+      </para>
+      <para>
+       Size in bytes of the external query text file used for overflow
+       storage.
+      </para></entry>
+     </row>
+     <row>
+      <entry role="catalog_table_entry"><para role="column_definition">
+       <structfield>num_entries</structfield> <type>bigint</type>
+      </para>
+      <para>
+       Current number of entries tracked by
+       <structname>pg_stat_statements</structname>.
+      </para></entry>
+     </row>
+     <row>
+      <entry role="catalog_table_entry"><para role="column_definition">
+       <structfield>last_eviction_time</structfield> <type>timestamp with time zone</type>
+      </para>
+      <para>
+       Time of the most recent entry eviction cycle, or null if no eviction
+       has occurred since server start.
+      </para></entry>
+     </row>
+
     </tbody>
    </tgroup>
   </table>
-- 
2.50.1 (Apple Git-155)

