Skip to content

Instantly share code, notes, and snippets.

@svilen-ivanov
Created November 21, 2023 08:45
Show Gist options
  • Save svilen-ivanov/b41c7d4e6c48d09fba020b532ae796d6 to your computer and use it in GitHub Desktop.
Save svilen-ivanov/b41c7d4e6c48d09fba020b532ae796d6 to your computer and use it in GitHub Desktop.
Pretty `psql` output
-- \set QUIET 1
\pset linestyle unicode
\pset border 2
\pset unicode_header_linestyle double
\pset null NULL
\pset format aligned
-- Custom queries examples
-- number of connected backends
\set backends 'SELECT datname, numbackends FROM pg_catalog.pg_stat_database;'
-- buffer cache hit ratio
\set buffer_cache_hit_ratio 'select datname, blks_hit::float/(blks_hit+blks_read) as hit_ratio from pg_stat_database where blks_read+blks_hit <> 0;'
-- kill transactions that have been "idle in transaction" for more than 10 minutes
\set kill_old_transactions 'select count(pg_terminate_backend(pid)) as nb_killed_proc from pg_stat_activity where query = \'<IDLE> in transaction\' and current_timestamp - query_start > \'10 min\';'
-- running queries
\set queries 'select current_timestamp - query_start as runtime,pid,datname,usename,query from pg_stat_activity where query != \'<IDLE>\' order by 1 desc; '
-- server uptime
\set uptime 'select date_trunc(\'second\',current_timestamp - pg_postmaster_start_time()) as uptime;'
\echo '--------------------------------------'
\echo 'Custom queries:'
\echo '\t' ':backends'
\echo '\t' ':buffer_cache_hit_ratio'
\echo '\t' ':kill_old_transactions'
\echo '\t' ':queries'
\echo '\t' ':uptime'
\echo '--------------------------------------\n'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment