Forked from rgreenjr/postgres_queries_and_commands.sql
Last active
April 20, 2020 11:12
-
-
Save Rajaneeshs/6ef3a50383c57df33dddfdc21eab0d82 to your computer and use it in GitHub Desktop.
Useful PostgreSQL Queries and Commands
This file contains 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 (pre 9.2) | |
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; | |
-- show running queries (9.2) | |
SELECT pid, age(query_start, clock_timestamp()), usename, query | |
FROM pg_stat_activity | |
WHERE query != '<IDLE>' AND query NOT ILIKE '%pg_stat_activity%' | |
ORDER BY query_start desc; | |
SELECT substring(query, 1, 50) AS short_query,round(total_time::numeric, 2) AS total_time,calls,round(mean_time::numeric, 2) AS mean,round((100 * total_time/sum(total_time::numeric) OVER ())::numeric, 2) AS percentage_cpu | |
FROM pg_stat_statements | |
ORDER BY total_time DESC | |
LIMIT 10; | |
--- how-to-detect-query-which-holds-the-lock-in-postgres | |
select pid, | |
usename, | |
pg_blocking_pids(pid) as blocked_by, | |
query as blocked_query | |
from pg_stat_activity | |
where cardinality(pg_blocking_pids(pid)) > 0; | |
SELECT pid, age(query_start, clock_timestamp()), usename, query , state FROM pg_stat_activity | |
-- WHERE query != '' AND query NOT ILIKE '%pg_stat_activity%' | |
ORDER BY query_start desc; | |
find SQL/activity that is mostly impacting CPU by: | |
Listing queries by total_time and see which query spends the most time in the database: | |
SELECT round(total_time*1000)/1000 AS total_time,query | |
FROM pg_stat_statements | |
ORDER BY total_time DESC; | |
or | |
Listing queries with total number of calls, total rows & rows returned etc: | |
SELECT query, calls, total_time, rows, 100.0 * shared_blks_hit / nullif(shared_blks_hit + shared_blks_read, 0) AS hit_percent | |
FROM pg_stat_statements ORDER BY total_time DESC LIMIT 5; | |
or even | |
Listing queries on 'per execution' basis & try to sample them over time, like: | |
SELECT queryid, query, calls, total_time/calls, rows/calls, temp_blks_read/calls, temp_blks_written/calls | |
FROM pg_stat_statements | |
WHERE calls != 0 | |
ORDER BY total_time DESC LIMIT 10; | |
-- kill running query | |
SELECT pg_cancel_backend(procpid); | |
-- kill idle query | |
SELECT pg_terminate_backend(procpid); | |
-- vacuum command | |
VACUUM (VERBOSE, ANALYZE); | |
-- 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 nspname || '.' || relname AS "relation", | |
pg_size_pretty(pg_total_relation_size(C.oid)) AS "total_size" | |
FROM pg_class C | |
LEFT JOIN pg_namespace N ON (N.oid = C.relnamespace) | |
WHERE nspname NOT IN ('pg_catalog', 'information_schema') | |
AND C.relkind <> 'i' | |
AND nspname !~ '^pg_toast' | |
ORDER BY pg_total_relation_size(C.oid) 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; | |
-- table missing index and pointless operations | |
select schemaname, relname,seq_scan, seq_tup_read, idx_scan, seq_tup_read / seq_scan AS avg | |
FROM pg_stat_user_tables | |
where seq_scan > 0 | |
Order by seq_tup_read desc; | |
--- Show unused indexes: | |
SELECT relname AS table_name, indexrelname AS index_name, idx_scan, idx_tup_read, idx_tup_fetch, pg_size_pretty(pg_relation_size(indexrelname::regclass)) | |
FROM pg_stat_all_indexes | |
WHERE schemaname = 'public' | |
AND idx_scan = 0 | |
AND idx_tup_read = 0 | |
AND idx_tup_fetch = 0 | |
ORDER BY pg_relation_size(indexrelname::regclass) DESC; | |
-- how many indexes are in cache | |
SELECT sum(idx_blks_read) as idx_read, sum(idx_blks_hit) as idx_hit, (sum(idx_blks_hit) - sum(idx_blks_read)) / sum(idx_blks_hit) as ratio | |
FROM pg_statio_user_indexes; | |
-- Dump database on remote host to file | |
$ pg_dump -U username -h hostname databasename > dump.sql | |
-- Import dump into existing database | |
$ psql -d newdb -f dump.sql |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment