Skip to content

Commit 364184a

Browse files
NikolaySclaude
andcommitted
Fix PostgreSQL 17/18 compatibility using WHERE clauses
- Use WHERE clauses to completely exclude checkpoint queries for PG17+ - Avoids query parsing errors with non-existent columns - For PG17+: Shows informational note about pg_stat_checkpointer - For PG<17: Shows normal checkpoint statistics from pg_stat_bgwriter - No DDL required, purely conditional query execution 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent 856941a commit 364184a

File tree

1 file changed

+20
-30
lines changed

1 file changed

+20
-30
lines changed

sql/0_node.sql

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -49,40 +49,30 @@ 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
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
59-
union all
60-
select
61-
'Forced Checkpoints',
52+
select 'Checkpoints Note',
6253
case
6354
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-
)
55+
then 'Use pg_stat_checkpointer view for checkpoint statistics (PostgreSQL 17+)'
56+
else 'Checkpoint statistics from pg_stat_bgwriter'
7057
end
7158
union all
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
59+
select 'Checkpoints', (select (checkpoints_timed + checkpoints_req)::text from pg_stat_bgwriter)
60+
where current_setting('server_version_num')::int < 170000
61+
union all
62+
select 'Forced Checkpoints', (
63+
select round(100.0 * checkpoints_req::numeric /
64+
(nullif(checkpoints_timed + checkpoints_req, 0)), 1)::text || '%'
65+
from pg_stat_bgwriter
66+
) where current_setting('server_version_num')::int < 170000
67+
union all
68+
select 'Checkpoint MB/sec', (
69+
select round((nullif(buffers_checkpoint::numeric, 0) /
70+
((1024.0 * 1024 /
71+
(current_setting('block_size')::numeric))
72+
* extract('epoch' from now() - stats_reset)
73+
))::numeric, 6)::text
74+
from pg_stat_bgwriter
75+
) where current_setting('server_version_num')::int < 170000
8676
union all
8777
select repeat('-', 33), repeat('-', 88)
8878
union all

0 commit comments

Comments
 (0)