@@ -8,6 +8,59 @@ For Postgres versions older than 10, run this first:
8
8
\set postgres_dba_is_wal_replay_paused pg_is_xlog_replay_paused
9
9
*/
10
10
11
+ -- Functions to handle PostgreSQL version differences
12
+ create or replace function pg_checkpoints () returns text language plpgsql as $$
13
+ begin
14
+ if current_setting(' server_version_num' )::int >= 170000 then
15
+ return (select (num_timed + num_requested)::text from pg_stat_checkpointer);
16
+ else
17
+ return (select (checkpoints_timed + checkpoints_req)::text from pg_stat_bgwriter);
18
+ end if;
19
+ end;
20
+ $$;
21
+
22
+ create or replace function pg_forced_checkpoints () returns text language plpgsql as $$
23
+ begin
24
+ if current_setting(' server_version_num' )::int >= 170000 then
25
+ return (
26
+ select round(100 .0 * num_requested::numeric /
27
+ (nullif(num_timed + num_requested, 0 )), 1 )::text || ' %'
28
+ from pg_stat_checkpointer
29
+ );
30
+ else
31
+ return (
32
+ select round(100 .0 * checkpoints_req::numeric /
33
+ (nullif(checkpoints_timed + checkpoints_req, 0 )), 1 )::text || ' %'
34
+ from pg_stat_bgwriter
35
+ );
36
+ end if;
37
+ end;
38
+ $$;
39
+
40
+ create or replace function pg_checkpoint_mbps () returns text language plpgsql as $$
41
+ begin
42
+ if current_setting(' server_version_num' )::int >= 170000 then
43
+ return (
44
+ select round((nullif(buffers_written::numeric , 0 ) /
45
+ ((1024 .0 * 1024 /
46
+ (current_setting(' block_size' )::numeric ))
47
+ * extract(' epoch' from now() - stats_reset)
48
+ ))::numeric , 6 )::text
49
+ from pg_stat_checkpointer
50
+ );
51
+ else
52
+ return (
53
+ select round((nullif(buffers_checkpoint::numeric , 0 ) /
54
+ ((1024 .0 * 1024 /
55
+ (current_setting(' block_size' )::numeric ))
56
+ * extract(' epoch' from now() - stats_reset)
57
+ ))::numeric , 6 )::text
58
+ from pg_stat_bgwriter
59
+ );
60
+ end if;
61
+ end;
62
+ $$;
63
+
11
64
with data as (
12
65
select s.*
13
66
from pg_stat_database s
@@ -48,57 +101,11 @@ select 'Started At', pg_postmaster_start_time()::timestamptz(0)::text
48
101
union all
49
102
select ' Uptime' , (now() - pg_postmaster_start_time())::interval (0 )::text
50
103
union all
51
- select
52
- ' Checkpoints' ,
53
- (
54
- case
55
- when current_setting(' server_version_num' )::int >= 170000
56
- then (select (num_timed + num_requested)::text from pg_stat_checkpointer)
57
- else (select (checkpoints_timed + checkpoints_req)::text from pg_stat_bgwriter)
58
- end
59
- )
104
+ select ' Checkpoints' , pg_checkpoints()
60
105
union all
61
- select
62
- ' Forced Checkpoints' ,
63
- (
64
- case
65
- when current_setting(' server_version_num' )::int >= 170000
66
- then (
67
- select round(100 .0 * num_requested::numeric /
68
- (nullif(num_timed + num_requested, 0 )), 1 )::text || ' %'
69
- from pg_stat_checkpointer
70
- )
71
- else (
72
- select round(100 .0 * checkpoints_req::numeric /
73
- (nullif(checkpoints_timed + checkpoints_req, 0 )), 1 )::text || ' %'
74
- from pg_stat_bgwriter
75
- )
76
- end
77
- )
106
+ select ' Forced Checkpoints' , pg_forced_checkpoints()
78
107
union all
79
- select
80
- ' Checkpoint MB/sec' ,
81
- (
82
- case
83
- when current_setting(' server_version_num' )::int >= 170000
84
- then (
85
- select round((nullif(buffers_written::numeric , 0 ) /
86
- ((1024 .0 * 1024 /
87
- (current_setting(' block_size' )::numeric ))
88
- * extract(' epoch' from now() - stats_reset)
89
- ))::numeric , 6 )::text
90
- from pg_stat_checkpointer
91
- )
92
- else (
93
- select round((nullif(buffers_checkpoint::numeric , 0 ) /
94
- ((1024 .0 * 1024 /
95
- (current_setting(' block_size' )::numeric ))
96
- * extract(' epoch' from now() - stats_reset)
97
- ))::numeric , 6 )::text
98
- from pg_stat_bgwriter
99
- )
100
- end
101
- )
108
+ select ' Checkpoint MB/sec' , pg_checkpoint_mbps()
102
109
union all
103
110
select repeat(' -' , 33 ), repeat(' -' , 88 )
104
111
union all
0 commit comments