Skip to content

Commit ac922ea

Browse files
authored
fix(inspect): update queries to handle system table column names for v15 & 17 (#4041)
2 parents ae830d2 + 6e86df1 commit ac922ea

File tree

3 files changed

+76
-11
lines changed

3 files changed

+76
-11
lines changed

internal/inspect/calls/calls.sql

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,24 @@ SELECT
33
(interval '1 millisecond' * total_exec_time)::text AS total_exec_time,
44
to_char((total_exec_time/sum(total_exec_time) OVER()) * 100, 'FM90D0') || '%' AS prop_exec_time,
55
to_char(calls, 'FM999G999G990') AS ncalls,
6-
(interval '1 millisecond' * (blk_read_time + blk_write_time))::text AS sync_io_time
7-
FROM pg_stat_statements
6+
/*
7+
Handle column names for 15 and 17
8+
*/
9+
(
10+
interval '1 millisecond' * (
11+
COALESCE(
12+
(to_jsonb(s) ->> 'shared_blk_read_time')::double precision,
13+
(to_jsonb(s) ->> 'blk_read_time')::double precision,
14+
0
15+
)
16+
+
17+
COALESCE(
18+
(to_jsonb(s) ->> 'shared_blk_write_time')::double precision,
19+
(to_jsonb(s) ->> 'blk_write_time')::double precision,
20+
0
21+
)
22+
)
23+
)::text AS sync_io_time
24+
FROM pg_stat_statements s
825
ORDER BY calls DESC
926
LIMIT 10

internal/inspect/db_stats/db_stats.sql

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,47 @@ WITH total_objects AS (
1212
UNION
1313
SELECT
1414
't' AS relkind,
15-
ROUND(SUM(heap_blks_hit)::numeric / nullif(SUM(heap_blks_hit + heap_blks_read), 0), 2) AS ratio
16-
FROM pg_statio_user_tables
15+
/*
16+
Handle column names for both PG15 and 17
17+
*/
18+
ROUND(
19+
(
20+
SUM(
21+
COALESCE(
22+
(to_jsonb(s) ->> 'rel_blks_hit')::bigint,
23+
(to_jsonb(s) ->> 'heap_blks_hit')::bigint,
24+
0
25+
)
26+
)::numeric
27+
/
28+
nullif(
29+
SUM(
30+
COALESCE(
31+
(to_jsonb(s) ->> 'rel_blks_hit')::bigint,
32+
(to_jsonb(s) ->> 'heap_blks_hit')::bigint,
33+
0
34+
)
35+
+
36+
COALESCE(
37+
(to_jsonb(s) ->> 'rel_blks_read')::bigint,
38+
(to_jsonb(s) ->> 'heap_blks_read')::bigint,
39+
0
40+
)
41+
),
42+
0
43+
)
44+
),
45+
2
46+
) AS ratio
47+
FROM pg_statio_user_tables s
1748
WHERE NOT schemaname LIKE ANY($1)
1849
)
1950
SELECT
2051
pg_size_pretty(pg_database_size($2)) AS database_size,
21-
(SELECT size FROM total_objects WHERE relkind = 'i') AS total_index_size,
22-
(SELECT size FROM total_objects WHERE relkind = 'r') AS total_table_size,
23-
(SELECT size FROM total_objects WHERE relkind = 't') AS total_toast_size,
24-
(SELECT (now() - stats_reset)::text FROM pg_stat_statements_info) AS time_since_stats_reset,
52+
COALESCE((SELECT size FROM total_objects WHERE relkind = 'i'), '0 bytes') AS total_index_size,
53+
COALESCE((SELECT size FROM total_objects WHERE relkind = 'r'), '0 bytes') AS total_table_size,
54+
COALESCE((SELECT size FROM total_objects WHERE relkind = 't'), '0 bytes') AS total_toast_size,
55+
COALESCE((SELECT (now() - stats_reset)::text FROM pg_stat_statements_info), 'N/A') AS time_since_stats_reset,
2556
(SELECT COALESCE(ratio::text, 'N/A') FROM cache_hit WHERE relkind = 'i') AS index_hit_rate,
2657
(SELECT COALESCE(ratio::text, 'N/A') FROM cache_hit WHERE relkind = 't') AS table_hit_rate,
27-
(SELECT pg_size_pretty(SUM(size)) FROM pg_ls_waldir()) AS wal_size
58+
COALESCE((SELECT pg_size_pretty(SUM(size)) FROM pg_ls_waldir()), '0 bytes') AS wal_size

internal/inspect/outliers/outliers.sql

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,25 @@ SELECT
22
(interval '1 millisecond' * total_exec_time)::text AS total_exec_time,
33
to_char((total_exec_time/sum(total_exec_time) OVER()) * 100, 'FM90D0') || '%' AS prop_exec_time,
44
to_char(calls, 'FM999G999G999G990') AS ncalls,
5-
(interval '1 millisecond' * (blk_read_time + blk_write_time))::text AS sync_io_time,
5+
/*
6+
Handle column names for 15 and 17
7+
*/
8+
(
9+
interval '1 millisecond' * (
10+
COALESCE(
11+
(to_jsonb(s) ->> 'shared_blk_read_time')::double precision,
12+
(to_jsonb(s) ->> 'blk_read_time')::double precision,
13+
0
14+
)
15+
+
16+
COALESCE(
17+
(to_jsonb(s) ->> 'shared_blk_write_time')::double precision,
18+
(to_jsonb(s) ->> 'blk_write_time')::double precision,
19+
0
20+
)
21+
)
22+
)::text AS sync_io_time,
623
query
7-
FROM pg_stat_statements WHERE userid = (SELECT usesysid FROM pg_user WHERE usename = current_user LIMIT 1)
24+
FROM pg_stat_statements s WHERE userid = (SELECT usesysid FROM pg_user WHERE usename = current_user LIMIT 1)
825
ORDER BY total_exec_time DESC
926
LIMIT 10

0 commit comments

Comments
 (0)