Skip to content

Instantly share code, notes, and snippets.

@sreekanthsnair
Last active October 21, 2024 16:06
Show Gist options
  • Save sreekanthsnair/3f94f60969d29350144b4619e414db74 to your computer and use it in GitHub Desktop.
Save sreekanthsnair/3f94f60969d29350144b4619e414db74 to your computer and use it in GitHub Desktop.
Most useful PostgreSQL queries for analytics

Tested in Postgres version 9.4

Check postgres connection usage

select max_conn,used,res_for_super,max_conn-used-res_for_super res_for_normal 
from 
  (select count(*) used from pg_stat_activity) t1,
  (select setting::int res_for_super from pg_settings where name=$$superuser_reserved_connections$$) t2,
  (select setting::int max_conn from pg_settings where name=$$max_connections$$) t3;

Check idle connection ( > 5 min)

select * from pg_stat_activity where state = 'idle' 
and state_change < current_timestamp - INTERVAL '5' MINUTE order by query_start;

Check all connected client and its count

SELECT datname,usename,client_addr,count(*) connections FROM pg_stat_activity 
group by datname,usename,client_addr order by usename;

Check running query with its lock status

SELECT act.pid, age(clock_timestamp(), query_start), usename, 
query, loc.mode, loc.locktype, loc.granted
FROM pg_stat_activity act inner join pg_locks loc on act.pid = loc.pid 
order by loc.granted, loc.pid DESC

Check all databases and its current size

select datname, pg_size_pretty(pg_database_size(datname)) from pg_database 
order by pg_database_size(datname) desc;

Checking connection state

select state,count(*) from pg_stat_activity group by state;

Long running queries in idle state

select now()-query_start as Running_Since,datname, usename,state, left(query,60) 
from pg_stat_activity where state in ('idle', 'idle in transaction') order by 1 desc;

Check waiting or locked query

SELECT * FROM pg_stat_activity WHERE waiting = TRUE;

Find the blocking query

SELECT blocked_locks.pid     AS blocked_pid,
         blocked_activity.usename  AS blocked_user,
         blocking_locks.pid     AS blocking_pid,
         blocking_activity.usename AS blocking_user,
         blocked_activity.query    AS blocked_statement,
         blocking_activity.query   AS current_statement_in_blocking_process
   FROM  pg_catalog.pg_locks         blocked_locks
    JOIN pg_catalog.pg_stat_activity blocked_activity  ON blocked_activity.pid = blocked_locks.pid
    JOIN pg_catalog.pg_locks         blocking_locks 
        ON blocking_locks.locktype = blocked_locks.locktype
        AND blocking_locks.DATABASE IS NOT DISTINCT FROM blocked_locks.DATABASE
        AND blocking_locks.relation IS NOT DISTINCT FROM blocked_locks.relation
        AND blocking_locks.page IS NOT DISTINCT FROM blocked_locks.page
        AND blocking_locks.tuple IS NOT DISTINCT FROM blocked_locks.tuple
        AND blocking_locks.virtualxid IS NOT DISTINCT FROM blocked_locks.virtualxid
        AND blocking_locks.transactionid IS NOT DISTINCT FROM blocked_locks.transactionid
        AND blocking_locks.classid IS NOT DISTINCT FROM blocked_locks.classid
        AND blocking_locks.objid IS NOT DISTINCT FROM blocked_locks.objid
        AND blocking_locks.objsubid IS NOT DISTINCT FROM blocked_locks.objsubid
        AND blocking_locks.pid != blocked_locks.pid
    JOIN pg_catalog.pg_stat_activity blocking_activity ON blocking_activity.pid = blocking_locks.pid
   WHERE NOT blocked_locks.GRANTED;

Checking table bloat

SELECT
  current_database(), schemaname, tablename, /*reltuples::bigint, relpages::bigint, otta,*/
  ROUND((CASE WHEN otta=0 THEN 0.0 ELSE sml.relpages::FLOAT/otta END)::NUMERIC,1) AS tbloat,
  CASE WHEN relpages < otta THEN 0 ELSE bs*(sml.relpages-otta)::BIGINT END AS wastedbytes,
  iname, /*ituples::bigint, ipages::bigint, iotta,*/
  ROUND((CASE WHEN iotta=0 OR ipages=0 THEN 0.0 ELSE ipages::FLOAT/iotta END)::NUMERIC,1) AS ibloat,
  CASE WHEN ipages < iotta THEN 0 ELSE bs*(ipages-iotta) END AS wastedibytes
FROM (
  SELECT
    schemaname, tablename, cc.reltuples, cc.relpages, bs,
    CEIL((cc.reltuples*((datahdr+ma-
      (CASE WHEN datahdr%ma=0 THEN ma ELSE datahdr%ma END))+nullhdr2+4))/(bs-20::FLOAT)) AS otta,
    COALESCE(c2.relname,'?') AS iname, COALESCE(c2.reltuples,0) AS ituples, COALESCE(c2.relpages,0) AS ipages,
    COALESCE(CEIL((c2.reltuples*(datahdr-12))/(bs-20::FLOAT)),0) AS iotta -- very rough approximation, assumes all cols
  FROM (
    SELECT
      ma,bs,schemaname,tablename,
      (datawidth+(hdr+ma-(CASE WHEN hdr%ma=0 THEN ma ELSE hdr%ma END)))::NUMERIC AS datahdr,
      (maxfracsum*(nullhdr+ma-(CASE WHEN nullhdr%ma=0 THEN ma ELSE nullhdr%ma END))) AS nullhdr2
    FROM (
      SELECT
        schemaname, tablename, hdr, ma, bs,
        SUM((1-null_frac)*avg_width) AS datawidth,
        MAX(null_frac) AS maxfracsum,
        hdr+(
          SELECT 1+COUNT(*)/8
          FROM pg_stats s2
          WHERE null_frac<>0 AND s2.schemaname = s.schemaname AND s2.tablename = s.tablename
        ) AS nullhdr
      FROM pg_stats s, (
        SELECT
          (SELECT current_setting('block_size')::NUMERIC) AS bs,
          CASE WHEN SUBSTRING(v,12,3) IN ('8.0','8.1','8.2') THEN 27 ELSE 23 END AS hdr,
          CASE WHEN v ~ 'mingw32' THEN 8 ELSE 4 END AS ma
        FROM (SELECT version() AS v) AS foo
      ) AS constants
      GROUP BY 1,2,3,4,5
    ) AS foo
  ) AS rs
  JOIN pg_class cc ON cc.relname = rs.tablename
  JOIN pg_namespace nn ON cc.relnamespace = nn.oid AND nn.nspname = rs.schemaname AND nn.nspname <> 'information_schema'
  LEFT JOIN pg_index i ON indrelid = cc.oid
  LEFT JOIN pg_class c2 ON c2.oid = i.indexrelid
) AS sml
ORDER BY wastedbytes DESC

Find duplicate Indexes

SELECT pg_size_pretty(SUM(pg_relation_size(idx))::BIGINT) AS SIZE,
       (array_agg(idx))[1] AS idx1, (array_agg(idx))[2] AS idx2,
       (array_agg(idx))[3] AS idx3, (array_agg(idx))[4] AS idx4
FROM (
    SELECT indexrelid::regclass AS idx, 
    (indrelid::text ||E'\n'|| indclass::text ||E'\n'|| indkey::text ||E'\n'
    ||COALESCE(indexprs::text,'')||E'\n' || COALESCE(indpred::text,'')) AS KEY
    FROM pg_index) sub
GROUP BY KEY HAVING COUNT(*)>1
ORDER BY SUM(pg_relation_size(idx)) DESC;

Finding fully unused Indexes

SELECT relid::regclass as table, indexrelid::regclass as index
, pg_size_pretty(pg_relation_size(indexrelid)) 
 FROM pg_stat_user_indexes JOIN pg_index USING (indexrelid)
 WHERE idx_scan = 0 AND indisunique IS FALSE order by pg_relation_size(indexrelid)

Terminating an idle query

SELECT pg_terminate_backend(pid);

Cacelling a running query

SELECT pg_cancel_backend(pid);

Query explain analyze as JSON Format (TEXT | XML | JSON | YAML)

EXPLAIN (ANALYZE, COSTS, VERBOSE, BUFFERS, FORMAT JSON) <Your_Query>

Reload postgres config without restart

select pg_reload_conf();

Postgres wiki performance optimization

https://wiki.postgresql.org/wiki/Performance_Optimization

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