Skip to content

Instantly share code, notes, and snippets.

@portnov
Created May 27, 2024 12:51
Show Gist options
  • Save portnov/581b30ee93fd75e7ab48d52b42934d6f to your computer and use it in GitHub Desktop.
Save portnov/581b30ee93fd75e7ab48d52b42934d6f to your computer and use it in GitHub Desktop.
postgres_exporter stat_statements query
explain (analyze, buffers)
SELECT
pg_get_userbyid(userid) as user,
pg_database.datname,
pg_stat_statements.queryid,
pg_stat_statements.calls as calls_total,
pg_stat_statements.total_exec_time / 1000.0 as seconds_total,
pg_stat_statements.rows as rows_total,
pg_stat_statements.blk_read_time / 1000.0 as block_read_seconds_total,
pg_stat_statements.blk_write_time / 1000.0 as block_write_seconds_total
FROM pg_stat_statements
JOIN pg_database
ON pg_database.oid = pg_stat_statements.dbid
WHERE
total_exec_time > (
SELECT percentile_cont(0.1)
WITHIN GROUP (ORDER BY total_exec_time)
FROM pg_stat_statements
)
ORDER BY seconds_total DESC
LIMIT 100;
QUERY PLAN |
-----------------------------------------------------------------------------------------------------------------------------------------------------------+
Limit (cost=27.41..27.44 rows=12 width=176) (actual time=33.296..33.304 rows=100 loops=1) |
Buffers: shared hit=1, temp read=1878 written=1878 |
I/O Timings: temp read=1.656 write=4.335 |
InitPlan 1 (returns $0) |
-> Aggregate (cost=12.50..12.51 rows=1 width=8) (actual time=13.352..13.352 rows=1 loops=1) |
Buffers: temp read=939 written=939 |
I/O Timings: temp read=0.714 write=2.136 |
-> Function Scan on pg_stat_statements pg_stat_statements_1 (cost=0.00..10.00 rows=1000 width=8) (actual time=9.578..10.939 rows=19403 loops=1)|
Buffers: temp read=939 written=939 |
I/O Timings: temp read=0.714 write=2.136 |
-> Sort (cost=14.89..14.92 rows=12 width=176) (actual time=33.295..33.299 rows=100 loops=1) |
Sort Key: ((pg_stat_statements.total_exec_time / '1000'::double precision)) DESC |
Sort Method: top-N heapsort Memory: 70kB |
Buffers: shared hit=1, temp read=1878 written=1878 |
I/O Timings: temp read=1.656 write=4.335 |
-> Hash Join (cost=1.16..14.68 rows=12 width=176) (actual time=27.245..32.039 rows=17462 loops=1) |
Hash Cond: (pg_stat_statements.dbid = pg_database.oid) |
Buffers: shared hit=1, temp read=1878 written=1878 |
I/O Timings: temp read=1.656 write=4.335 |
-> Function Scan on pg_stat_statements (cost=0.00..12.50 rows=333 width=56) (actual time=27.231..29.565 rows=17462 loops=1) |
Filter: (total_exec_time > $0) |
Rows Removed by Filter: 1941 |
Buffers: temp read=1878 written=1878 |
I/O Timings: temp read=1.656 write=4.335 |
-> Hash (cost=1.07..1.07 rows=7 width=68) (actual time=0.008..0.009 rows=7 loops=1) |
Buckets: 1024 Batches: 1 Memory Usage: 9kB |
Buffers: shared hit=1 |
-> Seq Scan on pg_database (cost=0.00..1.07 rows=7 width=68) (actual time=0.006..0.007 rows=7 loops=1) |
Buffers: shared hit=1 |
Planning Time: 0.306 ms |
Execution Time: 34.553 ms |
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment