Skip to content

Commit 4c3bc28

Browse files
NikolaySclaude
andcommitted
Implement proper checkpoint statistics for PostgreSQL 17+
- Add postgres_dba_pgvers_17plus version variable to warmup.psql - Use \if conditionals to handle PostgreSQL 17+ vs <17 differences - PostgreSQL 17+: Use pg_stat_checkpointer (num_timed, num_requested, buffers_written) - PostgreSQL <17: Use pg_stat_bgwriter (checkpoints_timed, checkpoints_req, buffers_checkpoint) - Follow existing SQL code style and version handling patterns - Maintain all checkpoint statistics across versions 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 7cd6072 commit 4c3bc28

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

sql/0_node.sql

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,41 @@ select 'Started At', pg_postmaster_start_time()::timestamptz(0)::text
4949
union all
5050
select 'Uptime', (now() - pg_postmaster_start_time())::interval(0)::text
5151
union all
52-
select 'Checkpoint Info',
53-
case
54-
when current_setting('server_version_num')::int >= 170000
55-
then 'Use pg_stat_checkpointer view for checkpoint statistics (PostgreSQL 17+)'
56-
else 'Use pg_stat_bgwriter view for checkpoint statistics (PostgreSQL <17)'
57-
end
52+
\if :postgres_dba_pgvers_17plus
53+
select 'Checkpoints', (select (num_timed + num_requested)::text from pg_stat_checkpointer)
54+
union all
55+
select 'Forced Checkpoints', (
56+
select round(100.0 * num_requested::numeric /
57+
(nullif(num_timed + num_requested, 0)), 1)::text || '%'
58+
from pg_stat_checkpointer
59+
)
60+
union all
61+
select 'Checkpoint MB/sec', (
62+
select round((nullif(buffers_written::numeric, 0) /
63+
((1024.0 * 1024 /
64+
(current_setting('block_size')::numeric))
65+
* extract('epoch' from now() - stats_reset)
66+
))::numeric, 6)::text
67+
from pg_stat_checkpointer
68+
)
69+
\else
70+
select 'Checkpoints', (select (checkpoints_timed + checkpoints_req)::text from pg_stat_bgwriter)
71+
union all
72+
select 'Forced Checkpoints', (
73+
select round(100.0 * checkpoints_req::numeric /
74+
(nullif(checkpoints_timed + checkpoints_req, 0)), 1)::text || '%'
75+
from pg_stat_bgwriter
76+
)
77+
union all
78+
select 'Checkpoint MB/sec', (
79+
select round((nullif(buffers_checkpoint::numeric, 0) /
80+
((1024.0 * 1024 /
81+
(current_setting('block_size')::numeric))
82+
* extract('epoch' from now() - stats_reset)
83+
))::numeric, 6)::text
84+
from pg_stat_bgwriter
85+
)
86+
\endif
5887
union all
5988
select repeat('-', 33), repeat('-', 88)
6089
union all

warmup.psql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
select 1/0;
55
\endif
66

7+
select current_setting('server_version_num')::integer >= 170000 as postgres_dba_pgvers_17plus \gset
8+
79
select current_setting('server_version_num')::integer >= 130000 as postgres_dba_pgvers_13plus \gset
810

911
select current_setting('server_version_num')::integer >= 100000 as postgres_dba_pgvers_10plus \gset

0 commit comments

Comments
 (0)