Skip to content

fix(inspect): update queries to handle system table column names for v15 & 17 #4041

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions internal/inspect/calls/calls.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,24 @@ SELECT
(interval '1 millisecond' * total_exec_time)::text AS total_exec_time,
to_char((total_exec_time/sum(total_exec_time) OVER()) * 100, 'FM90D0') || '%' AS prop_exec_time,
to_char(calls, 'FM999G999G990') AS ncalls,
(interval '1 millisecond' * (blk_read_time + blk_write_time))::text AS sync_io_time
FROM pg_stat_statements
/*
Handle column names for 15 and 17
*/
(
interval '1 millisecond' * (
COALESCE(
(to_jsonb(s) ->> 'shared_blk_read_time')::double precision,
(to_jsonb(s) ->> 'blk_read_time')::double precision,
0
)
+
COALESCE(
(to_jsonb(s) ->> 'shared_blk_write_time')::double precision,
(to_jsonb(s) ->> 'blk_write_time')::double precision,
0
)
)
)::text AS sync_io_time
FROM pg_stat_statements s
ORDER BY calls DESC
LIMIT 10
45 changes: 38 additions & 7 deletions internal/inspect/db_stats/db_stats.sql
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,47 @@ WITH total_objects AS (
UNION
SELECT
't' AS relkind,
ROUND(SUM(heap_blks_hit)::numeric / nullif(SUM(heap_blks_hit + heap_blks_read), 0), 2) AS ratio
FROM pg_statio_user_tables
/*
Handle column names for both PG15 and 17
*/
ROUND(
(
SUM(
COALESCE(
(to_jsonb(s) ->> 'rel_blks_hit')::bigint,
(to_jsonb(s) ->> 'heap_blks_hit')::bigint,
0
)
)::numeric
/
nullif(
SUM(
COALESCE(
(to_jsonb(s) ->> 'rel_blks_hit')::bigint,
(to_jsonb(s) ->> 'heap_blks_hit')::bigint,
0
)
+
COALESCE(
(to_jsonb(s) ->> 'rel_blks_read')::bigint,
(to_jsonb(s) ->> 'heap_blks_read')::bigint,
0
)
),
0
)
),
2
) AS ratio
FROM pg_statio_user_tables s
WHERE NOT schemaname LIKE ANY($1)
)
SELECT
pg_size_pretty(pg_database_size($2)) AS database_size,
(SELECT size FROM total_objects WHERE relkind = 'i') AS total_index_size,
(SELECT size FROM total_objects WHERE relkind = 'r') AS total_table_size,
(SELECT size FROM total_objects WHERE relkind = 't') AS total_toast_size,
(SELECT (now() - stats_reset)::text FROM pg_stat_statements_info) AS time_since_stats_reset,
COALESCE((SELECT size FROM total_objects WHERE relkind = 'i'), '0 bytes') AS total_index_size,
COALESCE((SELECT size FROM total_objects WHERE relkind = 'r'), '0 bytes') AS total_table_size,
COALESCE((SELECT size FROM total_objects WHERE relkind = 't'), '0 bytes') AS total_toast_size,
COALESCE((SELECT (now() - stats_reset)::text FROM pg_stat_statements_info), 'N/A') AS time_since_stats_reset,
(SELECT COALESCE(ratio::text, 'N/A') FROM cache_hit WHERE relkind = 'i') AS index_hit_rate,
(SELECT COALESCE(ratio::text, 'N/A') FROM cache_hit WHERE relkind = 't') AS table_hit_rate,
(SELECT pg_size_pretty(SUM(size)) FROM pg_ls_waldir()) AS wal_size
COALESCE((SELECT pg_size_pretty(SUM(size)) FROM pg_ls_waldir()), '0 bytes') AS wal_size
21 changes: 19 additions & 2 deletions internal/inspect/outliers/outliers.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,25 @@ SELECT
(interval '1 millisecond' * total_exec_time)::text AS total_exec_time,
to_char((total_exec_time/sum(total_exec_time) OVER()) * 100, 'FM90D0') || '%' AS prop_exec_time,
to_char(calls, 'FM999G999G999G990') AS ncalls,
(interval '1 millisecond' * (blk_read_time + blk_write_time))::text AS sync_io_time,
/*
Handle column names for 15 and 17
*/
(
interval '1 millisecond' * (
COALESCE(
(to_jsonb(s) ->> 'shared_blk_read_time')::double precision,
(to_jsonb(s) ->> 'blk_read_time')::double precision,
0
)
+
COALESCE(
(to_jsonb(s) ->> 'shared_blk_write_time')::double precision,
(to_jsonb(s) ->> 'blk_write_time')::double precision,
0
)
)
)::text AS sync_io_time,
query
FROM pg_stat_statements WHERE userid = (SELECT usesysid FROM pg_user WHERE usename = current_user LIMIT 1)
FROM pg_stat_statements s WHERE userid = (SELECT usesysid FROM pg_user WHERE usename = current_user LIMIT 1)
ORDER BY total_exec_time DESC
LIMIT 10