Skip to content

Commit bd227fe

Browse files
authored
Merge pull request #86 from NikolayS/feat/s3-workload-profile
feat(s3): add workload profile by query type
2 parents 1c72614 + 714e5bc commit bd227fe

3 files changed

Lines changed: 80 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ Then connect to any Postgres server via psql and type `:dba` to open the interac
6565
|----|--------|
6666
| s1 | Slowest queries by total time (requires `pg_stat_statements`) |
6767
| s2 | Slowest queries report (requires `pg_stat_statements`) |
68+
| s3 | Workload profile by query type (requires `pg_stat_statements`) |
6869

6970
### Configuration and other
7071
| ID | Report |
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
-- Workload profile by query type (requires pg_stat_statements)
2+
3+
-- Groups pg_stat_statements entries by first SQL keyword.
4+
-- Strips block comments (/* ... */) and line comments (-- ...)
5+
-- before extracting the keyword.
6+
7+
\if :postgres_dba_pgvers_13plus
8+
with data as (
9+
select
10+
lower((regexp_match(
11+
regexp_replace(
12+
regexp_replace(query, '/\*.*?\*/', '', 'gs'),
13+
'^\s*(--[^\n]*\n\s*)*', '', 'g'
14+
),
15+
'^\s*(\w+)'
16+
))[1]) as word,
17+
calls,
18+
total_exec_time,
19+
total_plan_time,
20+
rows
21+
from pg_stat_statements
22+
)
23+
select
24+
coalesce(word, '???') as "Query Type",
25+
sum(calls) as "Calls",
26+
round(
27+
100.0 * sum(calls) / nullif(sum(sum(calls)) over (), 0), 1
28+
) as "Calls %",
29+
round(sum(total_exec_time)::numeric, 1) as "Exec (ms)",
30+
round(
31+
100.0 * sum(total_exec_time) / nullif(sum(sum(total_exec_time)) over (), 0), 1
32+
) as "Exec %",
33+
round(sum(total_plan_time)::numeric, 1) as "Plan (ms)",
34+
round(
35+
(sum(total_exec_time) / nullif(sum(calls), 0))::numeric, 3
36+
) as "Avg (ms/call)",
37+
sum(rows) as "Rows"
38+
from data
39+
group by word
40+
order by sum(total_exec_time) desc;
41+
\else
42+
with data as (
43+
select
44+
lower((regexp_match(
45+
regexp_replace(
46+
regexp_replace(query, '/\*.*?\*/', '', 'gs'),
47+
'^\s*(--[^\n]*\n\s*)*', '', 'g'
48+
),
49+
'^\s*(\w+)'
50+
))[1]) as word,
51+
calls,
52+
total_time,
53+
rows
54+
from pg_stat_statements
55+
)
56+
select
57+
coalesce(word, '???') as "Query Type",
58+
sum(calls) as "Calls",
59+
round(
60+
100.0 * sum(calls) / nullif(sum(sum(calls)) over (), 0), 1
61+
) as "Calls %",
62+
round(sum(total_time)::numeric, 1) as "Time (ms)",
63+
round(
64+
100.0 * sum(total_time) / nullif(sum(sum(total_time)) over (), 0), 1
65+
) as "Time %",
66+
round(
67+
(sum(total_time) / nullif(sum(calls), 0))::numeric, 3
68+
) as "Avg (ms/call)",
69+
sum(rows) as "Rows"
70+
from data
71+
group by word
72+
order by sum(total_time) desc;
73+
\endif

start.psql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
\echo ' r2 – Alter user with random password (interactive)'
2626
\echo ' s1 – Slowest queries, by total time (requires pg_stat_statements)'
2727
\echo ' s2 – Slowest queries report (requires pg_stat_statements)'
28+
\echo ' s3 – Workload profile by query type (requires pg_stat_statements)'
2829
\echo ' t1 – Postgres parameters tuning'
2930
\echo ' t2 – Objects with custom storage parameters'
3031
\echo ' v1 – Vacuum: current activity'
@@ -60,6 +61,7 @@ select
6061
:d_stp::text = 'r2' as d_step_is_r2,
6162
:d_stp::text = 's1' as d_step_is_s1,
6263
:d_stp::text = 's2' as d_step_is_s2,
64+
:d_stp::text = 's3' as d_step_is_s3,
6365
:d_stp::text = 't1' as d_step_is_t1,
6466
:d_stp::text = 't2' as d_step_is_t2,
6567
:d_stp::text = 'v1' as d_step_is_v1,
@@ -168,6 +170,10 @@ select
168170
\ir ./sql/s2_pg_stat_statements_report.sql
169171
\prompt 'Press <Enter> to continue…' d_dummy
170172
\ir ./start.psql
173+
\elif :d_step_is_s3
174+
\ir ./sql/s3_pg_stat_statements_workload.sql
175+
\prompt 'Press <Enter> to continue…' d_dummy
176+
\ir ./start.psql
171177
\elif :d_step_is_t1
172178
\ir ./sql/t1_tuning.sql
173179
\prompt 'Press <Enter> to continue…' d_dummy

0 commit comments

Comments
 (0)