Skip to content

Commit 6eb3d0d

Browse files
author
Commitfest Bot
committed
[CF 5956] v3 - Enhance statistics reset functions to return reset timestamp
This branch was automatically generated by a robot using patches from an email thread registered at: https://commitfest.postgresql.org/patch/5956 The branch will be overwritten each time a new patch version is posted to the thread, and also periodically to check for bitrot caused by changes on the master branch. Patch(es): https://www.postgresql.org/message-id/CAOzEurTH9g9oirw7U8s4DrqKdr29D0z70QjSawY_F8EJ0-dgkw@mail.gmail.com Author(s): Shinya Kato
2 parents 371a302 + 1a80b71 commit 6eb3d0d

File tree

19 files changed

+307
-239
lines changed

19 files changed

+307
-239
lines changed

contrib/test_decoding/expected/stats.out

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ SELECT slot_name, spill_txns = 0 AS spill_txns, spill_count = 0 AS spill_count,
4747

4848
RESET logical_decoding_work_mem;
4949
-- reset stats for one slot, others should be unaffected
50-
SELECT pg_stat_reset_replication_slot('regression_slot_stats1');
51-
pg_stat_reset_replication_slot
52-
--------------------------------
53-
50+
SELECT pg_stat_reset_replication_slot('regression_slot_stats1') IS NOT NULL AS t;
51+
t
52+
---
53+
t
5454
(1 row)
5555

5656
SELECT slot_name, spill_txns = 0 AS spill_txns, spill_count = 0 AS spill_count, total_txns > 0 AS total_txns, total_bytes > 0 AS total_bytes, mem_exceeded_count = 0 AS mem_exceeded_count FROM pg_stat_replication_slots ORDER BY slot_name;
@@ -62,10 +62,10 @@ SELECT slot_name, spill_txns = 0 AS spill_txns, spill_count = 0 AS spill_count,
6262
(3 rows)
6363

6464
-- reset stats for all slots
65-
SELECT pg_stat_reset_replication_slot(NULL);
66-
pg_stat_reset_replication_slot
67-
--------------------------------
68-
65+
SELECT pg_stat_reset_replication_slot(NULL) IS NOT NULL AS t;
66+
t
67+
---
68+
t
6969
(1 row)
7070

7171
SELECT slot_name, spill_txns = 0 AS spill_txns, spill_count = 0 AS spill_count, total_txns > 0 AS total_txns, total_bytes > 0 AS total_bytes, mem_exceeded_count = 0 AS mem_exceeded_count FROM pg_stat_replication_slots ORDER BY slot_name;
@@ -91,6 +91,25 @@ SELECT * FROM pg_stat_get_replication_slot('do-not-exist');
9191
do-not-exist | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
9292
(1 row)
9393

94+
-- verify physical replication slot stats are not reset
95+
SELECT 'init' FROM pg_create_physical_replication_slot('regression_slot_stats_physical') ps;
96+
?column?
97+
----------
98+
init
99+
(1 row)
100+
101+
SELECT pg_stat_reset_replication_slot('regression_slot_stats_physical');
102+
pg_stat_reset_replication_slot
103+
--------------------------------
104+
105+
(1 row)
106+
107+
SELECT pg_drop_replication_slot('regression_slot_stats_physical');
108+
pg_drop_replication_slot
109+
--------------------------
110+
111+
(1 row)
112+
94113
-- spilling the xact
95114
BEGIN;
96115
INSERT INTO stats_test SELECT 'serialize-topbig--1:'||g.i FROM generate_series(1, 5000) g(i);

contrib/test_decoding/sql/stats.sql

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,23 @@ SELECT slot_name, spill_txns = 0 AS spill_txns, spill_count = 0 AS spill_count,
1919
RESET logical_decoding_work_mem;
2020

2121
-- reset stats for one slot, others should be unaffected
22-
SELECT pg_stat_reset_replication_slot('regression_slot_stats1');
22+
SELECT pg_stat_reset_replication_slot('regression_slot_stats1') IS NOT NULL AS t;
2323
SELECT slot_name, spill_txns = 0 AS spill_txns, spill_count = 0 AS spill_count, total_txns > 0 AS total_txns, total_bytes > 0 AS total_bytes, mem_exceeded_count = 0 AS mem_exceeded_count FROM pg_stat_replication_slots ORDER BY slot_name;
2424

2525
-- reset stats for all slots
26-
SELECT pg_stat_reset_replication_slot(NULL);
26+
SELECT pg_stat_reset_replication_slot(NULL) IS NOT NULL AS t;
2727
SELECT slot_name, spill_txns = 0 AS spill_txns, spill_count = 0 AS spill_count, total_txns > 0 AS total_txns, total_bytes > 0 AS total_bytes, mem_exceeded_count = 0 AS mem_exceeded_count FROM pg_stat_replication_slots ORDER BY slot_name;
2828

2929
-- verify accessing/resetting stats for non-existent slot does something reasonable
3030
SELECT * FROM pg_stat_get_replication_slot('do-not-exist');
3131
SELECT pg_stat_reset_replication_slot('do-not-exist');
3232
SELECT * FROM pg_stat_get_replication_slot('do-not-exist');
3333

34+
-- verify physical replication slot stats are not reset
35+
SELECT 'init' FROM pg_create_physical_replication_slot('regression_slot_stats_physical') ps;
36+
SELECT pg_stat_reset_replication_slot('regression_slot_stats_physical');
37+
SELECT pg_drop_replication_slot('regression_slot_stats_physical');
38+
3439
-- spilling the xact
3540
BEGIN;
3641
INSERT INTO stats_test SELECT 'serialize-topbig--1:'||g.i FROM generate_series(1, 5000) g(i);

doc/src/sgml/monitoring.sgml

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5055,10 +5055,11 @@ description | Waiting for a newly initialized WAL file to reach durable storage
50555055
<primary>pg_stat_clear_snapshot</primary>
50565056
</indexterm>
50575057
<function>pg_stat_clear_snapshot</function> ()
5058-
<returnvalue>void</returnvalue>
5058+
<returnvalue>timestamp with time zone</returnvalue>
50595059
</para>
50605060
<para>
5061-
Discards the current statistics snapshot or cached information.
5061+
Discards the current statistics snapshot or cached information, and
5062+
returns the time of the discard.
50625063
</para></entry>
50635064
</row>
50645065

@@ -5068,10 +5069,11 @@ description | Waiting for a newly initialized WAL file to reach durable storage
50685069
<primary>pg_stat_reset</primary>
50695070
</indexterm>
50705071
<function>pg_stat_reset</function> ()
5071-
<returnvalue>void</returnvalue>
5072+
<returnvalue>timestamp with time zone</returnvalue>
50725073
</para>
50735074
<para>
5074-
Resets all statistics counters for the current database to zero.
5075+
Resets all statistics counters for the current database to zero, and
5076+
returns the time of the reset.
50755077
</para>
50765078
<para>
50775079
This function is restricted to superusers by default, but other users
@@ -5085,11 +5087,12 @@ description | Waiting for a newly initialized WAL file to reach durable storage
50855087
<primary>pg_stat_reset_shared</primary>
50865088
</indexterm>
50875089
<function>pg_stat_reset_shared</function> ( [ <parameter>target</parameter> <type>text</type> <literal>DEFAULT</literal> <literal>NULL</literal> ] )
5088-
<returnvalue>void</returnvalue>
5090+
<returnvalue>timestamp with time zone</returnvalue>
50895091
</para>
50905092
<para>
50915093
Resets some cluster-wide statistics counters to zero, depending on the
5092-
argument. <parameter>target</parameter> can be:
5094+
argument, and returns the time of the reset.
5095+
<parameter>target</parameter> can be:
50935096
<itemizedlist>
50945097
<listitem>
50955098
<para>
@@ -5153,11 +5156,12 @@ description | Waiting for a newly initialized WAL file to reach durable storage
51535156
<primary>pg_stat_reset_single_table_counters</primary>
51545157
</indexterm>
51555158
<function>pg_stat_reset_single_table_counters</function> ( <type>oid</type> )
5156-
<returnvalue>void</returnvalue>
5159+
<returnvalue>timestamp with time zone</returnvalue>
51575160
</para>
51585161
<para>
51595162
Resets statistics for a single table or index in the current database
5160-
or shared across all databases in the cluster to zero.
5163+
or shared across all databases in the cluster to zero, and returns the
5164+
time of the reset.
51615165
</para>
51625166
<para>
51635167
This function is restricted to superusers by default, but other users
@@ -5171,11 +5175,12 @@ description | Waiting for a newly initialized WAL file to reach durable storage
51715175
<primary>pg_stat_reset_backend_stats</primary>
51725176
</indexterm>
51735177
<function>pg_stat_reset_backend_stats</function> ( <type>integer</type> )
5174-
<returnvalue>void</returnvalue>
5178+
<returnvalue>timestamp with time zone</returnvalue>
51755179
</para>
51765180
<para>
51775181
Resets statistics for a single backend with the specified process ID
5178-
to zero.
5182+
to zero, and returns the time of the reset or <literal>NULL</literal>
5183+
if an invalid backend process ID is specified.
51795184
</para>
51805185
<para>
51815186
This function is restricted to superusers by default, but other users
@@ -5189,11 +5194,11 @@ description | Waiting for a newly initialized WAL file to reach durable storage
51895194
<primary>pg_stat_reset_single_function_counters</primary>
51905195
</indexterm>
51915196
<function>pg_stat_reset_single_function_counters</function> ( <type>oid</type> )
5192-
<returnvalue>void</returnvalue>
5197+
<returnvalue>timestamp with time zone</returnvalue>
51935198
</para>
51945199
<para>
51955200
Resets statistics for a single function in the current database to
5196-
zero.
5201+
zero, and returns the time of the reset.
51975202
</para>
51985203
<para>
51995204
This function is restricted to superusers by default, but other users
@@ -5207,11 +5212,11 @@ description | Waiting for a newly initialized WAL file to reach durable storage
52075212
<primary>pg_stat_reset_slru</primary>
52085213
</indexterm>
52095214
<function>pg_stat_reset_slru</function> ( [ <parameter>target</parameter> <type>text</type> <literal>DEFAULT</literal> <literal>NULL</literal> ] )
5210-
<returnvalue>void</returnvalue>
5215+
<returnvalue>timestamp with time zone</returnvalue>
52115216
</para>
52125217
<para>
52135218
Resets statistics to zero for a single SLRU cache, or for all SLRUs in
5214-
the cluster. If <parameter>target</parameter> is
5219+
the cluster, and returns the time of the reset. If <parameter>target</parameter> is
52155220
<literal>NULL</literal> or is not specified, all the counters shown in
52165221
the <structname>pg_stat_slru</structname> view for all SLRU caches are
52175222
reset. The argument can be one of
@@ -5239,12 +5244,13 @@ description | Waiting for a newly initialized WAL file to reach durable storage
52395244
<primary>pg_stat_reset_replication_slot</primary>
52405245
</indexterm>
52415246
<function>pg_stat_reset_replication_slot</function> ( <type>text</type> )
5242-
<returnvalue>void</returnvalue>
5247+
<returnvalue>timestamp with time zone</returnvalue>
52435248
</para>
52445249
<para>
5245-
Resets statistics of the replication slot defined by the argument. If
5246-
the argument is <literal>NULL</literal>, resets statistics for all
5247-
the replication slots.
5250+
Resets statistics of the replication slot defined by the argument, and
5251+
returns the time of the reset or <literal>NULL</literal> if the replication
5252+
slot defined by the argument is a physical slot. If the argument is
5253+
<literal>NULL</literal>, resets statistics for all the replication slots.
52485254
</para>
52495255
<para>
52505256
This function is restricted to superusers by default, but other users
@@ -5258,13 +5264,13 @@ description | Waiting for a newly initialized WAL file to reach durable storage
52585264
<primary>pg_stat_reset_subscription_stats</primary>
52595265
</indexterm>
52605266
<function>pg_stat_reset_subscription_stats</function> ( <type>oid</type> )
5261-
<returnvalue>void</returnvalue>
5267+
<returnvalue>timestamp with time zone</returnvalue>
52625268
</para>
52635269
<para>
52645270
Resets statistics for a single subscription shown in the
5265-
<structname>pg_stat_subscription_stats</structname> view to zero. If
5266-
the argument is <literal>NULL</literal>, reset statistics for all
5267-
subscriptions.
5271+
<structname>pg_stat_subscription_stats</structname> view to zero, and
5272+
returns the time of the reset. If the argument is
5273+
<literal>NULL</literal>, reset statistics for all subscriptions.
52685274
</para>
52695275
<para>
52705276
This function is restricted to superusers by default, but other users

src/backend/access/transam/xlogprefetcher.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,16 +299,18 @@ XLogPrefetchShmemSize(void)
299299
/*
300300
* Reset all counters to zero.
301301
*/
302-
void
302+
TimestampTz
303303
XLogPrefetchResetStats(void)
304304
{
305-
pg_atomic_write_u64(&SharedStats->reset_time, GetCurrentTimestamp());
305+
TimestampTz ts = GetCurrentTimestamp();
306+
pg_atomic_write_u64(&SharedStats->reset_time, ts);
306307
pg_atomic_write_u64(&SharedStats->prefetch, 0);
307308
pg_atomic_write_u64(&SharedStats->hit, 0);
308309
pg_atomic_write_u64(&SharedStats->skip_init, 0);
309310
pg_atomic_write_u64(&SharedStats->skip_new, 0);
310311
pg_atomic_write_u64(&SharedStats->skip_fpw, 0);
311312
pg_atomic_write_u64(&SharedStats->skip_rep, 0);
313+
return ts;
312314
}
313315

314316
void

src/backend/catalog/system_functions.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -638,14 +638,14 @@ AS 'unicode_is_normalized';
638638

639639
CREATE OR REPLACE FUNCTION
640640
pg_stat_reset_shared(target text DEFAULT NULL)
641-
RETURNS void
641+
RETURNS timestamp with time zone
642642
LANGUAGE INTERNAL
643643
CALLED ON NULL INPUT VOLATILE PARALLEL SAFE
644644
AS 'pg_stat_reset_shared';
645645

646646
CREATE OR REPLACE FUNCTION
647647
pg_stat_reset_slru(target text DEFAULT NULL)
648-
RETURNS void
648+
RETURNS timestamp with time zone
649649
LANGUAGE INTERNAL
650650
CALLED ON NULL INPUT VOLATILE PARALLEL SAFE
651651
AS 'pg_stat_reset_slru';

src/backend/utils/activity/pgstat.c

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -832,14 +832,16 @@ match_db_entries(PgStatShared_HashEntry *entry, Datum match_data)
832832
* Permission checking for this function is managed through the normal
833833
* GRANT system.
834834
*/
835-
void
835+
TimestampTz
836836
pgstat_reset_counters(void)
837837
{
838838
TimestampTz ts = GetCurrentTimestamp();
839839

840840
pgstat_reset_matching_entries(match_db_entries,
841841
ObjectIdGetDatum(MyDatabaseId),
842842
ts);
843+
844+
return ts;
843845
}
844846

845847
/*
@@ -851,7 +853,7 @@ pgstat_reset_counters(void)
851853
* Permission checking for this function is managed through the normal
852854
* GRANT system.
853855
*/
854-
void
856+
TimestampTz
855857
pgstat_reset(PgStat_Kind kind, Oid dboid, uint64 objid)
856858
{
857859
const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
@@ -865,6 +867,8 @@ pgstat_reset(PgStat_Kind kind, Oid dboid, uint64 objid)
865867

866868
if (!kind_info->accessed_across_databases)
867869
pgstat_reset_database_timestamp(dboid, ts);
870+
871+
return ts;
868872
}
869873

870874
/*
@@ -873,7 +877,7 @@ pgstat_reset(PgStat_Kind kind, Oid dboid, uint64 objid)
873877
* Permission checking for this function is managed through the normal
874878
* GRANT system.
875879
*/
876-
void
880+
TimestampTz
877881
pgstat_reset_of_kind(PgStat_Kind kind)
878882
{
879883
const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
@@ -883,8 +887,9 @@ pgstat_reset_of_kind(PgStat_Kind kind)
883887
kind_info->reset_all_cb(ts);
884888
else
885889
pgstat_reset_entries_of_kind(kind, ts);
886-
}
887890

891+
return ts;
892+
}
888893

889894
/* ------------------------------------------------------------
890895
* Fetching of stats
@@ -899,7 +904,7 @@ pgstat_reset_of_kind(PgStat_Kind kind)
899904
* the no-longer-wanted snapshot. Updates of stats_fetch_consistency can
900905
* cause this routine to be called.
901906
*/
902-
void
907+
TimestampTz
903908
pgstat_clear_snapshot(void)
904909
{
905910
pgstat_assert_is_up();
@@ -929,6 +934,8 @@ pgstat_clear_snapshot(void)
929934

930935
/* Reset this flag, as it may be possible that a cleanup was forced. */
931936
force_stats_snapshot_clear = false;
937+
938+
return GetCurrentTimestamp();
932939
}
933940

934941
void *

src/backend/utils/activity/pgstat_replslot.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,14 @@ static int get_replslot_index(const char *name, bool need_lock);
3737
*
3838
* Permission checking for this function is managed through the normal
3939
* GRANT system.
40+
*
41+
* NB: Returns the time of the reset or 0 if it is a physical slot.
4042
*/
41-
void
43+
TimestampTz
4244
pgstat_reset_replslot(const char *name)
4345
{
4446
ReplicationSlot *slot;
47+
TimestampTz ts = 0;
4548

4649
Assert(name != NULL);
4750

@@ -61,10 +64,12 @@ pgstat_reset_replslot(const char *name)
6164
* as we collect stats only for logical slots.
6265
*/
6366
if (SlotIsLogical(slot))
64-
pgstat_reset(PGSTAT_KIND_REPLSLOT, InvalidOid,
67+
ts = pgstat_reset(PGSTAT_KIND_REPLSLOT, InvalidOid,
6568
ReplicationSlotIndex(slot));
6669

6770
LWLockRelease(ReplicationSlotControlLock);
71+
72+
return ts;
6873
}
6974

7075
/*

src/backend/utils/activity/pgstat_slru.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,16 @@ static bool have_slrustats = false;
4141
* Permission checking for this function is managed through the normal
4242
* GRANT system.
4343
*/
44-
void
44+
TimestampTz
4545
pgstat_reset_slru(const char *name)
4646
{
4747
TimestampTz ts = GetCurrentTimestamp();
4848

4949
Assert(name != NULL);
5050

5151
pgstat_reset_slru_counter_internal(pgstat_get_slru_index(name), ts);
52+
53+
return ts;
5254
}
5355

5456
/*

0 commit comments

Comments
 (0)