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 <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
This commit is contained in:
Fujii Masao 2026-03-16 17:24:08 +09:00
parent a41bc38439
commit 8fe315f18d
6 changed files with 68 additions and 5 deletions

View 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

View 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';

View file

@ -57,6 +57,6 @@
*/
/* yyyymmddN */
#define CATALOG_VERSION_NO 202603131
#define CATALOG_VERSION_NO 202603161
#endif

View 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,

View 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
----

View 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