@@ -8,58 +8,6 @@ For Postgres versions older than 10, run this first:
88 \set postgres_dba_is_wal_replay_paused pg_is_xlog_replay_paused
99*/
1010
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- $$;
6311
6412with data as (
6513 select s.*
@@ -101,11 +49,40 @@ select 'Started At', pg_postmaster_start_time()::timestamptz(0)::text
10149union all
10250select ' Uptime' , (now() - pg_postmaster_start_time())::interval (0 )::text
10351union all
104- select ' Checkpoints' , pg_checkpoints()
52+ select
53+ ' Checkpoints' ,
54+ case
55+ when current_setting(' server_version_num' )::int >= 170000
56+ then ' See pg_stat_checkpointer (PG17+)'
57+ else (select (checkpoints_timed + checkpoints_req)::text from pg_stat_bgwriter)
58+ end
10559union all
106- select ' Forced Checkpoints' , pg_forced_checkpoints()
60+ select
61+ ' Forced Checkpoints' ,
62+ case
63+ when current_setting(' server_version_num' )::int >= 170000
64+ then ' See pg_stat_checkpointer (PG17+)'
65+ else (
66+ select round(100 .0 * checkpoints_req::numeric /
67+ (nullif(checkpoints_timed + checkpoints_req, 0 )), 1 )::text || ' %'
68+ from pg_stat_bgwriter
69+ )
70+ end
10771union all
108- select ' Checkpoint MB/sec' , pg_checkpoint_mbps()
72+ select
73+ ' Checkpoint MB/sec' ,
74+ case
75+ when current_setting(' server_version_num' )::int >= 170000
76+ then ' See pg_stat_checkpointer (PG17+)'
77+ else (
78+ select round((nullif(buffers_checkpoint::numeric , 0 ) /
79+ ((1024 .0 * 1024 /
80+ (current_setting(' block_size' )::numeric ))
81+ * extract(' epoch' from now() - stats_reset)
82+ ))::numeric , 6 )::text
83+ from pg_stat_bgwriter
84+ )
85+ end
10986union all
11087select repeat(' -' , 33 ), repeat(' -' , 88 )
11188union all
0 commit comments