Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit2fb7a75

Browse files
committed
Add pg_stat_get_snapshot_timestamp() to show statistics snapshot timestamp.
Per discussion, this could be useful for purposes such as programmaticallydetecting a nonresponding stats collector. We already have the timestampanyway, it's just a matter of providing a SQL-accessible function to fetchit.Matt Kelly, reviewed by Jim Nasby
1 parent634618e commit2fb7a75

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
externDatumpg_stat_get_xact_function_total_time(PG_FUNCTION_ARGS);
116116
externDatumpg_stat_get_xact_function_self_time(PG_FUNCTION_ARGS);
117117

118+
externDatumpg_stat_get_snapshot_timestamp(PG_FUNCTION_ARGS);
118119
externDatumpg_stat_clear_snapshot(PG_FUNCTION_ARGS);
119120
externDatumpg_stat_reset(PG_FUNCTION_ARGS);
120121
externDatumpg_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-
#defineCATALOG_VERSION_NO201502181
56+
#defineCATALOG_VERSION_NO201502191
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_timePGNSP 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_snapshotPGNSP 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_resetPGNSP 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 prevstatsAS
2323
SELECTt.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
FROMpg_catalog.pg_stat_user_tablesAS t,
2728
pg_catalog.pg_statio_user_tablesAS b
2829
WHEREt.relname='tenk2'ANDb.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_tablesAS st, pg_classAS cl, prevstatsAS pr
8283
WHEREst.relname='tenk2'ANDcl.relname='tenk2';
8384

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

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp