Forked from rgreenjr/postgres_queries_and_commands.sql
Created
July 11, 2019 01:41
-
-
Save mgiay/b0ea3b3b67b060b55fff694b5e1e4464 to your computer and use it in GitHub Desktop.
Useful PostgreSQL Queries and Commands
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- show running queries | |
| SELECT procpid, age(query_start, clock_timestamp()), usename, current_query | |
| FROM pg_stat_activity | |
| WHERE current_query != '<IDLE>' AND current_query NOT ILIKE '%pg_stat_activity%' | |
| ORDER BY query_start desc; | |
| -- kill running query | |
| SELECT pg_cancel_backend(procpid); | |
| -- all database users | |
| select * from pg_stat_activity where current_query not like '<%'; | |
| -- all databases and their sizes | |
| select * from pg_user; | |
| -- all tables and their size, with/without indexes | |
| select datname, | |
| pg_size_pretty(pg_database_size(datname)) | |
| from pg_database | |
| order by pg_database_size(datname) desc; | |
| -- cache hit rates (should not be less than 0.99) | |
| SELECT | |
| sum(heap_blks_read) as heap_read, | |
| sum(heap_blks_hit) as heap_hit, | |
| (sum(heap_blks_hit) - sum(heap_blks_read)) / sum(heap_blks_hit) as ratio | |
| FROM | |
| pg_statio_user_tables; | |
| -- table index usage rates (should not be less than 0.99) | |
| SELECT | |
| relname, | |
| 100 * idx_scan / (seq_scan + idx_scan) percent_of_times_index_used, | |
| n_live_tup rows_in_table | |
| FROM | |
| pg_stat_user_tables | |
| ORDER BY | |
| n_live_tup DESC; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment