Skip to content

Instantly share code, notes, and snippets.

@slardiere
Created October 20, 2020 15:06
Show Gist options
  • Save slardiere/ca477fa80ac92282f35d15569682ab6f to your computer and use it in GitHub Desktop.
Save slardiere/ca477fa80ac92282f35d15569682ab6f to your computer and use it in GitHub Desktop.
Get Size of SeqScan data
-- 1- Data collect in a database, 2 times with few hours lag
-- 2- 'Create table as' in another database
-- CREATE TABLE stats_tables AS
\o tables.out
COPY (
SELECT relid , schemaname , relname , seq_scan
, seq_tup_read , idx_scan , idx_tup_fetch
, pg_table_size(relid) as table_size
, now() AS date_ev
FROM pg_stat_user_tables
WHERE pg_table_size(relid) > 8192 * 1024
ORDER BY seq_scan DESC
) TO STDOUT;
\o
-- 3- COPY data in the table, 2 times
-- \copy stats_tables FROM 'tables.out'
-- 4- Run query
SELECT tablename , nb_seq_scan_hours
, pg_size_pretty((nb_seq_scan_hours * table_size )::numeric) AS total
, pg_size_pretty((sum( nb_seq_scan_hours * table_size ) OVER ())::numeric) AS sum_size
FROM (
SELECT tablename
, trunc((seq_scan - prev_seq_scan)::float / lag_hours ) AS nb_seq_scan_hours
, table_size
FROM (
SELECT schemaname || '.' || relname AS tablename , seq_scan
, lag(seq_scan) OVER (PARTITION BY schemaname || '.' || relname ORDER BY date_ev) AS prev_seq_scan
, date_ev
, lag(date_ev) OVER (PARTITION BY schemaname || '.' || relname ORDER BY date_ev) AS prev_date_ev
, extract('hours' from date_ev - lag(date_ev) OVER (PARTITION BY schemaname || '.' || relname ORDER BY date_ev) )
+ extract('minute' from date_ev - lag(date_ev) OVER (PARTITION BY schemaname || '.' || relname ORDER BY date_ev) ) / 60 as lag_hours
, table_size
FROM stats_tables) x
WHERE prev_seq_scan IS NOT NULL
AND seq_scan - prev_seq_scan > 0) y
ORDER BY 2 DESC;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment