Forked from rgreenjr/postgres_queries_and_commands.sql
Last active
September 22, 2021 19:36
-
-
Save marcomalva/c0daa7f9d1243ef4686fac6621d71b0a to your computer and use it in GitHub Desktop.
[PostgreSQL Query Snippets] Useful PostgreSQL Queries and Commands #SQL
This file contains 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 (pre 9.2) | |
SELECT procpid, age(clock_timestamp(), query_start), usename, current_query | |
FROM pg_stat_activity | |
WHERE current_query != '<IDLE>' AND current_query NOT ILIKE '%pg_stat_activity%' | |
ORDER BY query_start desc; | |
-- 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; | |
-- 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 | |
-- PostgreSQL uptime | |
SELECT now() as utc, now() at time zone 'America/Los_Angeles' as la, date_trunc('second', current_timestamp - pg_postmaster_start_time()) as uptime; | |
-- get number of connections by database instance + total | |
select datname, count(*) as cnt_conn, sum(count(*)) over () as tot_conn from pg_stat_activity group by datname order by datname; | |
-- get more details, see column numbackends for number of connections | |
select * from pg_stat_database order by datname; | |
-- show max number of connections, e.g. 600 | |
show max_connections; | |
-- show private memory per connect, the private memory available for each operation during query execution is limited by work_mem, e.g. 4 MB | |
show work_mem; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment