Skip to content

Commit 47332d9

Browse files
committed
slow queries, by total time
1 parent 64adbf8 commit 47332d9

File tree

3 files changed

+51
-41
lines changed

3 files changed

+51
-41
lines changed

bloat/index_pgstattuple.sql

Lines changed: 0 additions & 31 deletions
This file was deleted.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
-- In pg_stat_statements, there is a problem: sometimes (quite often), it registers the same query twice (or even more).
2+
-- It's easy to check in your DB:
3+
--
4+
-- with heh as (
5+
-- select userid, dbid, query, count(*), array_agg(queryid) queryids
6+
-- from pg_stat_statements group by 1, 2, 3 having count(*) > 1
7+
-- ) select left(query, 85) || '...', userid, dbid, count, queryids from heh;
8+
--
9+
-- This query gives you "full picture", aggregating stats for each query-database-username ternary
10+
11+
-- Works with Postgres 9.6
12+
13+
select
14+
sum(calls) as calls,
15+
sum(total_time) as total_time,
16+
sum(mean_time * calls) / sum(calls) as mean_time,
17+
max(max_time) as max_time,
18+
min(min_time) as min_time,
19+
-- stddev_time, -- https://stats.stackexchange.com/questions/55999/is-it-possible-to-find-the-combined-standard-deviation
20+
sum(rows) as rows,
21+
userid,
22+
dbid,
23+
query,
24+
sum(shared_blks_hit) as shared_blks_hit,
25+
sum(shared_blks_read) as shared_blks_read,
26+
sum(shared_blks_dirtied) as shared_blks_dirtied,
27+
sum(shared_blks_written) as shared_blks_written,
28+
sum(local_blks_hit) as local_blks_hit,
29+
sum(local_blks_read) as local_blks_read,
30+
sum(local_blks_dirtied) as local_blks_dirtied,
31+
sum(local_blks_written) as local_blks_written,
32+
sum(temp_blks_read) as temp_blks_read,
33+
sum(temp_blks_written) as temp_blks_written,
34+
sum(blk_read_time) as blk_read_time,
35+
sum(blk_write_time) as blk_write_time,
36+
array_agg(queryid) as queryids -- 9.4+
37+
from pg_stat_statements
38+
group by userid, dbid, query
39+
order by sum(total_time) desc
40+
limit 50;
41+

start.psql

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
\echo Menu:
22
\echo ' 1 – Basic Node Information (master/replica, lag, DB size, tmp files)'
33
\echo ' 2 – Sizes of tables'
4-
\echo ' b1 – Table bloat, rough estimation'
4+
\echo ' b1 – Tables bloat, rough estimation'
55
\echo ' b2 – B-tree indexes bloat, rough estimation'
6-
\echo ' b3 – Table bloat, more precise (requires pgstattuple extension; expensive)'
7-
\echo ' b4 – Index bloat, more precise (requires pgstattuple extension; expensive)'
8-
\echo ' 3 - Slowest Queries'
6+
\echo ' b3 – Tables bloat, more precise (requires pgstattuple extension; expensive)'
7+
\echo ' b4 – B-tree indexes bloat, more precise (requires pgstattuple extension; expensive)'
98
\echo ' i1 - Unused/Redundant Indexes'
109
\echo ' i2 - Rarely Used Indexes'
10+
\echo ' s1 - Slowest Queries, by Total Time (requires pg_stat_statements extension)'
1111
\echo ' q – Quit'
1212
\echo
1313
\echo 'Type your choice and press <Enter>:'
@@ -20,9 +20,9 @@ select
2020
:d_stp::text = 'b2' as d_step_is_b2,
2121
:d_stp::text = 'b3' as d_step_is_b3,
2222
:d_stp::text = 'b4' as d_step_is_b4,
23-
:d_stp::text = '3' as d_step_is_3,
2423
:d_stp::text = 'i1' as d_step_is_i1,
2524
:d_stp::text = 'i2' as d_step_is_i2,
25+
:d_stp::text = 's1' as d_step_is_s1,
2626
:d_stp::text = 'q' as d_step_is_q \gset
2727
\if :d_step_is_1
2828
\i ./general/basic.sql
@@ -45,11 +45,7 @@ select
4545
\prompt 'Press <Enter> to continue…' d_dummy
4646
\i ./start.psql
4747
\elif :d_step_is_b4
48-
\i ./bloat/index_pgstattuple.sql
49-
\prompt 'Press <Enter> to continue…' d_dummy
50-
\i ./start.psql
51-
\elif :d_step_is_3
52-
\echo ' 3 is chosen!'
48+
\i ./bloat/btree_pgstattuple.sql
5349
\prompt 'Press <Enter> to continue…' d_dummy
5450
\i ./start.psql
5551
\elif :d_step_is_i1
@@ -60,6 +56,10 @@ select
6056
\i ./indexes/rare.sql
6157
\prompt 'Press <Enter> to continue…' d_dummy
6258
\i ./start.psql
59+
\elif :d_step_is_s1
60+
\i ./slow/pg_stat_statements_top_total.sql
61+
\prompt 'Press <Enter> to continue…' d_dummy
62+
\i ./start.psql
6363
\elif :d_step_is_q
6464
\echo 'Bye!'
6565
\echo

0 commit comments

Comments
 (0)