From 8fe315f18d4181d37385c8e62ea3009084ba303a Mon Sep 17 00:00:00 2001 From: Fujii Masao Date: Mon, 16 Mar 2026 17:24:08 +0900 Subject: [PATCH] Add stats_reset column to pg_statio_all_sequences 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 Co-authored-by: Shihao Zhong Reviewed-by: Fujii Masao Discussion: https://postgr.es/m/CAA5RZ0v0OPGyDpwxkX81CtTt9xsj9-TNxhm=8JdOvEKPsVVFNg@mail.gmail.com --- doc/src/sgml/monitoring.sgml | 11 ++++++++++ src/backend/catalog/system_views.sql | 3 ++- src/include/catalog/catversion.h | 2 +- src/test/regress/expected/rules.out | 9 +++++--- src/test/regress/expected/stats.out | 33 ++++++++++++++++++++++++++++ src/test/regress/sql/stats.sql | 15 +++++++++++++ 6 files changed, 68 insertions(+), 5 deletions(-) diff --git a/doc/src/sgml/monitoring.sgml b/doc/src/sgml/monitoring.sgml index 9c5c6dc490f..462019a972c 100644 --- a/doc/src/sgml/monitoring.sgml +++ b/doc/src/sgml/monitoring.sgml @@ -4909,6 +4909,15 @@ description | Waiting for a newly initialized WAL file to reach durable storage Number of buffer hits in this sequence + + + + stats_reset timestamp with time zone + + + Time at which these statistics were last reset + + @@ -5406,6 +5415,8 @@ description | Waiting for a newly initialized WAL file to reach durable storage 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. This function is restricted to superusers by default, but other users diff --git a/src/backend/catalog/system_views.sql b/src/backend/catalog/system_views.sql index 90d48bc9c80..6d6dce18fa3 100644 --- a/src/backend/catalog/system_views.sql +++ b/src/backend/catalog/system_views.sql @@ -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'; diff --git a/src/include/catalog/catversion.h b/src/include/catalog/catversion.h index 3da6a75ff87..aa8708b195d 100644 --- a/src/include/catalog/catversion.h +++ b/src/include/catalog/catversion.h @@ -57,6 +57,6 @@ */ /* yyyymmddN */ -#define CATALOG_VERSION_NO 202603131 +#define CATALOG_VERSION_NO 202603161 #endif diff --git a/src/test/regress/expected/rules.out b/src/test/regress/expected/rules.out index 71d7262049e..9ed0a1756c0 100644 --- a/src/test/regress/expected/rules.out +++ b/src/test/regress/expected/rules.out @@ -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, diff --git a/src/test/regress/expected/stats.out b/src/test/regress/expected/stats.out index 981d7c90822..8a31521fba2 100644 --- a/src/test/regress/expected/stats.out +++ b/src/test/regress/expected/stats.out @@ -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 ---- diff --git a/src/test/regress/sql/stats.sql b/src/test/regress/sql/stats.sql index 70af96f739f..15add71f734 100644 --- a/src/test/regress/sql/stats.sql +++ b/src/test/regress/sql/stats.sql @@ -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