title: Postgres performance queries date: “2024-09-10T22:19:32.12Z” description: A collection of usefull queries for performance analyzis slug: postgres-performance-SQLs

The size of things

select pg_relation_size('<TABLE>');

SELECT pg_size_pretty (pg_relation_size('<TABLE>'));

SELECT pg_size_pretty (pg_indexes_size('<INDEX>'));

Vaccum and analyze


VACUUM ANALYZE;

VACUUM (VERBOSE, ANALYZE) global_db;

SELECT relname,
   CASE idx_scan
     WHEN 0 THEN 'Insufficient data'
     ELSE (100 * idx_scan / (seq_scan + idx_scan))::text
   END percent_of_times_index_used,
   n_live_tup rows_in_table
 FROM
   pg_stat_user_tables
 ORDER BY
   n_live_tup DESC;
   

SELECT
  'index hit rate' AS name,
  (sum(idx_blks_hit)) / nullif(sum(idx_blks_hit + idx_blks_read),0) AS ratio
FROM pg_statio_user_indexes
UNION ALL
SELECT
 'table hit rate' AS name,
  sum(heap_blks_hit) / nullif(sum(heap_blks_hit) + sum(heap_blks_read),0) AS ratio
FROM pg_statio_user_tables;