Forked from rgreenjr/postgres_queries_and_commands.sql
Last active
April 24, 2022 13:32
-
-
Save johnjreiser/240b71468f07462c83eafbdf2cd52eb1 to your computer and use it in GitHub Desktop.
Useful PostgreSQL Queries and Commands
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- show running queries (9.2) | |
SELECT pid, age(query_start, clock_timestamp()), usename, query | |
FROM pg_stat_activity | |
WHERE query != '<IDLE>' AND query NOT ILIKE '%pg_stat_activity%' | |
ORDER BY query_start desc; | |
-- kill running query | |
SELECT pg_cancel_backend(procpid); | |
-- kill idle query | |
SELECT pg_terminate_backend(procpid); | |
-- vacuum command | |
VACUUM (VERBOSE, ANALYZE); | |
-- all database users | |
select * from pg_stat_activity where current_query not like '<%'; | |
-- all databases and their sizes | |
select * from pg_user; | |
-- all tables and their size, with/without indexes | |
select datname, pg_size_pretty(pg_database_size(datname)) | |
from pg_database | |
order by pg_database_size(datname) 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; | |
-- table index usage rates (should not be less than 0.99) | |
SELECT relname, 100 * idx_scan / (seq_scan + idx_scan) percent_of_times_index_used, n_live_tup rows_in_table | |
FROM pg_stat_user_tables | |
ORDER BY n_live_tup DESC; | |
-- how many indexes are in cache | |
SELECT sum(idx_blks_read) as idx_read, sum(idx_blks_hit) as idx_hit, (sum(idx_blks_hit) - sum(idx_blks_read)) / sum(idx_blks_hit) as ratio | |
FROM pg_statio_user_indexes; | |
-- Dump database on remote host to file | |
$ pg_dump -U username -h hostname databasename > dump.sql | |
-- Import dump into existing database | |
$ psql -d newdb -f dump.sql | |
-- check streaming replication (on hot standby) | |
SELECT | |
CASE | |
WHEN pg_last_xlog_receive_location() = pg_last_xlog_replay_location() THEN 0 | |
ELSE EXTRACT (EPOCH FROM now() - pg_last_xact_replay_timestamp())::INTEGER | |
END | |
AS replication_lag; | |
-- view dependencies (tables and views) | |
SELECT DISTINCT nd.nspname||'.'||cl_d.relname AS ref_table | |
, n.nspname||'.'||cl_r.relname as ref_view | |
FROM pg_rewrite AS r | |
JOIN pg_class AS cl_r ON r.ev_class=cl_r.oid | |
JOIN pg_depend AS d ON r.oid=d.objid | |
JOIN pg_namespace AS n ON cl_r.relnamespace = n.oid | |
JOIN pg_class AS cl_d ON d.refobjid=cl_d.oid | |
JOIN pg_namespace AS nd ON cl_d.relnamespace = nd.oid | |
WHERE cl_d.relkind IN ('r','v','c','m') | |
AND not 'information_schema' in (n.nspname, nd.nspname) | |
AND not 'pg_catalog' in (n.nspname, nd.nspname) | |
AND cl_d.relname <> cl_r.relname | |
ORDER BY 1; | |
-- view dependencies (columns) | |
SELECT dependent_ns.nspname as dependent_schema | |
, dependent_view.relname as dependent_view | |
, source_ns.nspname as source_schema | |
, source_table.relname as source_table | |
, string_agg(pg_attribute.attname, ',') as column_names | |
FROM pg_depend | |
JOIN pg_rewrite ON pg_depend.objid = pg_rewrite.oid | |
JOIN pg_class as dependent_view ON pg_rewrite.ev_class = dependent_view.oid | |
JOIN pg_class as source_table ON pg_depend.refobjid = source_table.oid | |
JOIN pg_attribute ON pg_depend.refobjid = pg_attribute.attrelid | |
AND pg_depend.refobjsubid = pg_attribute.attnum | |
JOIN pg_namespace dependent_ns ON dependent_ns.oid = dependent_view.relnamespace | |
JOIN pg_namespace source_ns ON source_ns.oid = source_table.relnamespace | |
WHERE | |
source_ns.nspname = 'pubdata' | |
and pg_attribute.attnum > 0 | |
group by dependent_ns.nspname , dependent_view.relname , source_ns.nspname , source_table.relname | |
order by 1,2,3,4 | |
; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment