Skip to content

Instantly share code, notes, and snippets.

View pshen's full-sized avatar

Chenjun Shen pshen

View GitHub Profile
@pshen
pshen / gist:5a6e92aa20d8c091821d
Last active August 29, 2015 14:01
Who is blocking? PG>9.2
SELECT
w.query as waiting_query,
w.pid as w_pid,
w.usename as w_user,
l.query as locking_query,
l.pid as l_pid,
l.usename as l_user,
t.schemaname || '.' || t.relname as tablename
from pg_stat_activity w
join pg_locks l1 on w.pid = l1.pid and not l1.granted
@pshen
pshen / gist:4dd92177149c256c6337
Last active August 29, 2015 14:01 — forked from jberkus/gist:6b1bcaf7724dfc2a54f3
Finding unused indexes
WITH table_scans as (
SELECT relid,
tables.idx_scan + tables.seq_scan as all_scans,
( tables.n_tup_ins + tables.n_tup_upd + tables.n_tup_del ) as writes,
pg_relation_size(relid) as table_size
FROM pg_stat_user_tables as tables
),
all_writes as (
SELECT sum(writes) as total_writes
FROM table_scans