Guest User

Untitled

a guest
Mar 17th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. -- show running queries (pre 9.2)
  2. SELECT procpid, age(query_start, clock_timestamp()), usename, current_query
  3. FROM pg_stat_activity
  4. WHERE current_query != '<IDLE>' AND current_query NOT ILIKE '%pg_stat_activity%'
  5. ORDER BY query_start desc;
  6.  
  7. -- show running queries (9.2)
  8. SELECT pid, age(query_start, clock_timestamp()), usename, query
  9. FROM pg_stat_activity
  10. WHERE query != '<IDLE>' AND query NOT ILIKE '%pg_stat_activity%'
  11. and state != 'idle'
  12. ORDER BY query_start desc;
  13.  
  14. -- kill running query
  15. SELECT pg_cancel_backend(procpid);
  16.  
  17. -- kill idle query
  18. SELECT pg_terminate_backend(procpid);
  19.  
  20. -- vacuum command
  21. VACUUM (VERBOSE, ANALYZE);
  22.  
  23. -- all database users
  24. select * from pg_stat_activity where current_query not like '<%';
  25.  
  26. -- all databases and their sizes
  27. select * from pg_user;
  28.  
  29. -- all tables and their size, with/without indexes
  30. select datname, pg_size_pretty(pg_database_size(datname))
  31. from pg_database
  32. order by pg_database_size(datname) desc;
  33.  
  34. -- cache hit rates (should not be less than 0.99)
  35. 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
  36. FROM pg_statio_user_tables;
  37.  
  38. -- table index usage rates (should not be less than 0.99)
  39. SELECT relname, 100 * idx_scan / (seq_scan + idx_scan) percent_of_times_index_used, n_live_tup rows_in_table
  40. FROM pg_stat_user_tables
  41. ORDER BY n_live_tup DESC;
  42.  
  43. -- how many indexes are in cache
  44. 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
  45. FROM pg_statio_user_indexes;
  46.  
  47. -- Dump database on remote host to file
  48. $ pg_dump -U username -h hostname databasename > dump.sql
  49.  
  50. -- Import dump into existing database
  51. $ psql -d newdb -f dump.sql
Add Comment
Please, Sign In to add comment