You are on page 1of 2

# Queries running

docker exec -it stackdevelop_dbhost_1 psql -U postgres -c "select


datname,pid,user,state,query from pg_stat_activity where state !='idle';"

# Number of rows of table


docker exec -it stackmaster_dbhost_1 psql -U postgres -d camunda -c "SELECT
reltuples as approximate_row_count FROM pg_class WHERE relname =
'act_ru_meter_log';"

# Size of table
docker exec -it stackmaster_dbhost_1 psql -U postgres -d camunda -c "SELECT
pg_size_pretty( pg_total_relation_size('act_ru_meter_log') );"

# Show rows from table


docker exec -it stackproduction_dbhost_1 psql -U postgres -d camunda -c "SELECT *
FROM act_ru_meter_log;"

-- show running queries (9.2)


SELECT pid, age(clock_timestamp(), query_start), usename, query
FROM pg_stat_activity
WHERE query != '<IDLE>' AND query NOT ILIKE '%pg_stat_activity%'
ORDER BY query_start desc;

-- cache hit rates (should not be less than 0.99)


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
FROM pg_statio_user_tables;

# Another of my favourite queries is to show a top-like view of current queries


docker exec -it stackdevelop_dbhost_1 psql -U postgres -c "SELECT count(*) as cnt,
user,query FROM pg_stat_activity GROUP BY user,query ORDER BY cnt DESC;"

# This is useful for analysing queries for non idle connections that have been
running since a long time that may require some tuning.
SELECT datname,
pid,
usename,
client_addr,
client_port,
xact_start,
backend_start,
query_start,
state,
query
FROM pg_stat_activity
WHERE state != 'idle'
ORDER BY query_start ASC

# Long running queries


SELECT
pid,
now() - pg_stat_activity.query_start AS duration,
query,
state
FROM pg_stat_activity where now() - query_start > interval '5 minute'
AND state != 'idle'

#Get Running Queries (And Lock statuses) in PostgreSQL

You might also like