]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Add stats_reset column to pg_statio_all_sequences
authorFujii Masao <fujii@postgresql.org>
Mon, 16 Mar 2026 08:24:08 +0000 (17:24 +0900)
committerFujii Masao <fujii@postgresql.org>
Mon, 16 Mar 2026 08:24:08 +0000 (17:24 +0900)
pg_statio_all_sequences lacked a stats_reset column, unlike the other
pg_statio_* views that already expose it. This commit adds the column so
users can see when the statistics in this view were last reset.

Also this commit updates the documentation for
pg_stat_reset_single_table_counters() to clarify that it can reset statistics
for sequences and materialized views as well.

Catalog version bumped.

Author: Sami Imseih <samimseih@gmail.com>
Co-authored-by: Shihao Zhong <zhong950419@gmail.com>
Reviewed-by: Fujii Masao <masao.fujii@gmail.com>
Discussion: https://postgr.es/m/CAA5RZ0v0OPGyDpwxkX81CtTt9xsj9-TNxhm=8JdOvEKPsVVFNg@mail.gmail.com

doc/src/sgml/monitoring.sgml
src/backend/catalog/system_views.sql
src/include/catalog/catversion.h
src/test/regress/expected/rules.out
src/test/regress/expected/stats.out
src/test/regress/sql/stats.sql

index 9c5c6dc490f4315e3273e124d59e4491fc2ccd3b..462019a972c64e4f5f4d998488ae989e8592ae91 100644 (file)
@@ -4909,6 +4909,15 @@ description | Waiting for a newly initialized WAL file to reach durable storage
        Number of buffer hits in this sequence
       </para></entry>
      </row>
+
+     <row>
+      <entry role="catalog_table_entry"><para role="column_definition">
+       <structfield>stats_reset</structfield> <type>timestamp with time zone</type>
+      </para>
+      <para>
+       Time at which these statistics were last reset
+      </para></entry>
+     </row>
     </tbody>
    </tgroup>
   </table>
@@ -5406,6 +5415,8 @@ description | Waiting for a newly initialized WAL file to reach durable storage
        <para>
         Resets statistics for a single table or index in the current database
         or shared across all databases in the cluster to zero.
+        It also resets statistics for a single sequence or materialized view
+        in the current database.
        </para>
        <para>
         This function is restricted to superusers by default, but other users
index 90d48bc9c802956d5cdbeed2c1f3a19983321e4d..6d6dce18fa3fdbf6b431b286b4feab3b59a28f07 100644 (file)
@@ -896,7 +896,8 @@ CREATE VIEW pg_statio_all_sequences AS
             C.relname AS relname,
             pg_stat_get_blocks_fetched(C.oid) -
                     pg_stat_get_blocks_hit(C.oid) AS blks_read,
-            pg_stat_get_blocks_hit(C.oid) AS blks_hit
+            pg_stat_get_blocks_hit(C.oid) AS blks_hit,
+            pg_stat_get_stat_reset_time(C.oid) AS stats_reset
     FROM pg_class C
             LEFT JOIN pg_namespace N ON (N.oid = C.relnamespace)
     WHERE C.relkind = 'S';
index 3da6a75ff87eba78042ee7f86e4bf729b2084814..aa8708b195dc9271e9cf3d94bc30bf1185a976f6 100644 (file)
@@ -57,6 +57,6 @@
  */
 
 /*                                                     yyyymmddN */
-#define CATALOG_VERSION_NO     202603131
+#define CATALOG_VERSION_NO     202603161
 
 #endif
index 71d7262049e77a8e095e45dfbd8fdbb89988e0dc..9ed0a1756c008ed9308596a468de4e344f634395 100644 (file)
@@ -2451,7 +2451,8 @@ pg_statio_all_sequences| SELECT c.oid AS relid,
     n.nspname AS schemaname,
     c.relname,
     (pg_stat_get_blocks_fetched(c.oid) - pg_stat_get_blocks_hit(c.oid)) AS blks_read,
-    pg_stat_get_blocks_hit(c.oid) AS blks_hit
+    pg_stat_get_blocks_hit(c.oid) AS blks_hit,
+    pg_stat_get_stat_reset_time(c.oid) AS stats_reset
    FROM (pg_class c
      LEFT JOIN pg_namespace n ON ((n.oid = c.relnamespace)))
   WHERE (c.relkind = 'S'::"char");
@@ -2493,7 +2494,8 @@ pg_statio_sys_sequences| SELECT relid,
     schemaname,
     relname,
     blks_read,
-    blks_hit
+    blks_hit,
+    stats_reset
    FROM pg_statio_all_sequences
   WHERE ((schemaname = ANY (ARRAY['pg_catalog'::name, 'information_schema'::name])) OR (schemaname ~ '^pg_toast'::text));
 pg_statio_sys_tables| SELECT relid,
@@ -2524,7 +2526,8 @@ pg_statio_user_sequences| SELECT relid,
     schemaname,
     relname,
     blks_read,
-    blks_hit
+    blks_hit,
+    stats_reset
    FROM pg_statio_all_sequences
   WHERE ((schemaname <> ALL (ARRAY['pg_catalog'::name, 'information_schema'::name])) AND (schemaname !~ '^pg_toast'::text));
 pg_statio_user_tables| SELECT relid,
index 981d7c9082200b4acc7be9aa8f7b3a177a218ab9..8a31521fba2d4fe72f91861d55b557d2b6b3bce5 100644 (file)
@@ -1167,6 +1167,39 @@ SELECT stats_reset > :'dbc_reset_ts'::timestamptz FROM pg_stat_database_conflict
  t
 (1 row)
 
+-- Test that reset works for pg_statio_all_sequences
+-- Use the sequence to accumulate its stats, and reset them once first
+-- so that we have a baseline for comparison, similar to the previous test.
+-- stats_reset to compare to.
+CREATE SEQUENCE test_seq1;
+SELECT nextval('test_seq1');
+ nextval 
+---------
+       1
+(1 row)
+
+SELECT pg_stat_reset_single_table_counters('test_seq1'::regclass);
+ pg_stat_reset_single_table_counters 
+-------------------------------------
+(1 row)
+
+SELECT stats_reset AS seq_reset_ts
+  FROM pg_statio_all_sequences WHERE relname ='test_seq1' \gset
+SELECT pg_stat_reset_single_table_counters('test_seq1'::regclass);
+ pg_stat_reset_single_table_counters 
+-------------------------------------
+(1 row)
+
+SELECT stats_reset > :'seq_reset_ts'::timestamptz, blks_read + blks_hit
+  FROM pg_statio_all_sequences WHERE relname ='test_seq1';
+ ?column? | ?column? 
+----------+----------
+ t        |        0
+(1 row)
+
+DROP SEQUENCE test_seq1;
 ----
 -- pg_stat_get_snapshot_timestamp behavior
 ----
index 70af96f739f93b8b3584993b2ae36da8d7a71338..15add71f734684570bde1adb45785585cccb1173 100644 (file)
@@ -536,6 +536,21 @@ SELECT pg_stat_reset();
 SELECT stats_reset > :'db_reset_ts'::timestamptz FROM pg_stat_database WHERE datname = (SELECT current_database());
 SELECT stats_reset > :'dbc_reset_ts'::timestamptz FROM pg_stat_database_conflicts WHERE datname = (SELECT current_database());
 
+-- Test that reset works for pg_statio_all_sequences
+
+-- Use the sequence to accumulate its stats, and reset them once first
+-- so that we have a baseline for comparison, similar to the previous test.
+-- stats_reset to compare to.
+CREATE SEQUENCE test_seq1;
+SELECT nextval('test_seq1');
+SELECT pg_stat_reset_single_table_counters('test_seq1'::regclass);
+SELECT stats_reset AS seq_reset_ts
+  FROM pg_statio_all_sequences WHERE relname ='test_seq1' \gset
+SELECT pg_stat_reset_single_table_counters('test_seq1'::regclass);
+SELECT stats_reset > :'seq_reset_ts'::timestamptz, blks_read + blks_hit
+  FROM pg_statio_all_sequences WHERE relname ='test_seq1';
+DROP SEQUENCE test_seq1;
+
 
 ----
 -- pg_stat_get_snapshot_timestamp behavior