Guest User

Untitled

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