Skip to content

Commit 2fb7a75

Browse files
committed
Add pg_stat_get_snapshot_timestamp() to show statistics snapshot timestamp.
Per discussion, this could be useful for purposes such as programmatically detecting a nonresponding stats collector. We already have the timestamp anyway, it's just a matter of providing a SQL-accessible function to fetch it. Matt Kelly, reviewed by Jim Nasby
1 parent 634618e commit 2fb7a75

File tree

6 files changed

+33
-3
lines changed

6 files changed

+33
-3
lines changed

doc/src/sgml/monitoring.sgml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1709,6 +1709,14 @@ postgres 27093 0.0 0.0 30096 2752 ? Ss 11:34 0:00 postgres: ser
17091709
</entry>
17101710
</row>
17111711

1712+
<row>
1713+
<entry><literal><function>pg_stat_get_snapshot_timestamp()</function></literal><indexterm><primary>pg_stat_get_snapshot_timestamp</primary></indexterm></entry>
1714+
<entry><type>timestamp with time zone</type></entry>
1715+
<entry>
1716+
Returns the timestamp of the current statistics snapshot
1717+
</entry>
1718+
</row>
1719+
17121720
<row>
17131721
<entry><literal><function>pg_stat_clear_snapshot()</function></literal><indexterm><primary>pg_stat_clear_snapshot</primary></indexterm></entry>
17141722
<entry><type>void</type></entry>

src/backend/utils/adt/pgstatfuncs.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ extern Datum pg_stat_get_xact_function_calls(PG_FUNCTION_ARGS);
115115
extern Datum pg_stat_get_xact_function_total_time(PG_FUNCTION_ARGS);
116116
extern Datum pg_stat_get_xact_function_self_time(PG_FUNCTION_ARGS);
117117

118+
extern Datum pg_stat_get_snapshot_timestamp(PG_FUNCTION_ARGS);
118119
extern Datum pg_stat_clear_snapshot(PG_FUNCTION_ARGS);
119120
extern Datum pg_stat_reset(PG_FUNCTION_ARGS);
120121
extern Datum pg_stat_reset_shared(PG_FUNCTION_ARGS);
@@ -1682,6 +1683,13 @@ pg_stat_get_xact_function_self_time(PG_FUNCTION_ARGS)
16821683
}
16831684

16841685

1686+
/* Get the timestamp of the current statistics snapshot */
1687+
Datum
1688+
pg_stat_get_snapshot_timestamp(PG_FUNCTION_ARGS)
1689+
{
1690+
PG_RETURN_TIMESTAMPTZ(pgstat_fetch_global()->stats_timestamp);
1691+
}
1692+
16851693
/* Discard the active statistics snapshot */
16861694
Datum
16871695
pg_stat_clear_snapshot(PG_FUNCTION_ARGS)

src/include/catalog/catversion.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,6 @@
5353
*/
5454

5555
/* yyyymmddN */
56-
#define CATALOG_VERSION_NO 201502181
56+
#define CATALOG_VERSION_NO 201502191
5757

5858
#endif

src/include/catalog/pg_proc.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2852,6 +2852,8 @@ DESCR("statistics: total execution time of function in current transaction, in m
28522852
DATA(insert OID = 3048 ( pg_stat_get_xact_function_self_time PGNSP PGUID 12 1 0 0 0 f f f f t f v 1 0 701 "26" _null_ _null_ _null_ _null_ pg_stat_get_xact_function_self_time _null_ _null_ _null_ ));
28532853
DESCR("statistics: self execution time of function in current transaction, in msec");
28542854

2855+
DATA(insert OID = 3788 ( pg_stat_get_snapshot_timestamp PGNSP PGUID 12 1 0 0 0 f f f f t f s 0 0 1184 "" _null_ _null_ _null_ _null_ pg_stat_get_snapshot_timestamp _null_ _null_ _null_ ));
2856+
DESCR("statistics: timestamp of the current statistics snapshot");
28552857
DATA(insert OID = 2230 ( pg_stat_clear_snapshot PGNSP PGUID 12 1 0 0 0 f f f f f f v 0 0 2278 "" _null_ _null_ _null_ _null_ pg_stat_clear_snapshot _null_ _null_ _null_ ));
28562858
DESCR("statistics: discard current transaction's statistics snapshot");
28572859
DATA(insert OID = 2274 ( pg_stat_reset PGNSP PGUID 12 1 0 0 0 f f f f f f v 0 0 2278 "" _null_ _null_ _null_ _null_ pg_stat_reset _null_ _null_ _null_ ));

src/test/regress/expected/stats.out

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ SELECT pg_sleep_for('2 seconds');
2828
CREATE TEMP TABLE prevstats AS
2929
SELECT t.seq_scan, t.seq_tup_read, t.idx_scan, t.idx_tup_fetch,
3030
(b.heap_blks_read + b.heap_blks_hit) AS heap_blks,
31-
(b.idx_blks_read + b.idx_blks_hit) AS idx_blks
31+
(b.idx_blks_read + b.idx_blks_hit) AS idx_blks,
32+
pg_stat_get_snapshot_timestamp() as snap_ts
3233
FROM pg_catalog.pg_stat_user_tables AS t,
3334
pg_catalog.pg_statio_user_tables AS b
3435
WHERE t.relname='tenk2' AND b.relname='tenk2';
@@ -111,4 +112,11 @@ SELECT st.heap_blks_read + st.heap_blks_hit >= pr.heap_blks + cl.relpages,
111112
t | t
112113
(1 row)
113114

115+
SELECT pr.snap_ts < pg_stat_get_snapshot_timestamp() as snapshot_newer
116+
FROM prevstats AS pr;
117+
snapshot_newer
118+
----------------
119+
t
120+
(1 row)
121+
114122
-- End of Stats Test

src/test/regress/sql/stats.sql

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ SELECT pg_sleep_for('2 seconds');
2222
CREATE TEMP TABLE prevstats AS
2323
SELECT t.seq_scan, t.seq_tup_read, t.idx_scan, t.idx_tup_fetch,
2424
(b.heap_blks_read + b.heap_blks_hit) AS heap_blks,
25-
(b.idx_blks_read + b.idx_blks_hit) AS idx_blks
25+
(b.idx_blks_read + b.idx_blks_hit) AS idx_blks,
26+
pg_stat_get_snapshot_timestamp() as snap_ts
2627
FROM pg_catalog.pg_stat_user_tables AS t,
2728
pg_catalog.pg_statio_user_tables AS b
2829
WHERE t.relname='tenk2' AND b.relname='tenk2';
@@ -81,4 +82,7 @@ SELECT st.heap_blks_read + st.heap_blks_hit >= pr.heap_blks + cl.relpages,
8182
FROM pg_statio_user_tables AS st, pg_class AS cl, prevstats AS pr
8283
WHERE st.relname='tenk2' AND cl.relname='tenk2';
8384

85+
SELECT pr.snap_ts < pg_stat_get_snapshot_timestamp() as snapshot_newer
86+
FROM prevstats AS pr;
87+
8488
-- End of Stats Test

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy