Skip to content

Instantly share code, notes, and snippets.

@sjeandeaux
Last active July 20, 2021 17:41
Show Gist options
  • Save sjeandeaux/4967836eccfcaed6160d161154b2f363 to your computer and use it in GitHub Desktop.
Save sjeandeaux/4967836eccfcaed6160d161154b2f363 to your computer and use it in GitHub Desktop.
postgresql

Queries

SELECT state, 
       pid,
       timezone('Europe/Paris', query_start), 
       usename, 
       substring(query,1, 20)
FROM pg_stat_activity
WHERE 
   query != '<IDLE>' AND 
   query NOT ILIKE '%pg_stat_activity%'
ORDER BY state, query_start asc limit 10;
SELECT relname,
       n_tup_ins as "inserts",
       n_tup_upd as "updates", 
       n_tup_del as "deletes",
       n_live_tup as "live_tuples", 
       n_dead_tup as "dead_tuples" 
FROM pg_stat_user_tables;
SELECT substring(query, 1, 20), 
       calls, 
       mean_time, 
       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 10;
---- 13
SELECT substring(query, 1, 20), 
       calls, 
       mean_exec_time, 
       total_exec_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_exec_time DESC LIMIT 10;
SELECT pg_cancel_backend(<pid of the process>)
SELECT pg_terminate_backend(<pid of the process>)

References

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment