Skip to content

Instantly share code, notes, and snippets.

@Erushenko
Forked from nesquena/basic.sql
Created November 1, 2017 14:35
Show Gist options
  • Save Erushenko/b32c25fc3fd7c4364f1c52e780e108ef to your computer and use it in GitHub Desktop.
Save Erushenko/b32c25fc3fd7c4364f1c52e780e108ef to your computer and use it in GitHub Desktop.

Revisions

  1. @nesquena nesquena revised this gist Jan 30, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion basic.sql
    Original file line number Diff line number Diff line change
    @@ -7,4 +7,4 @@ SELECT pg_size_pretty(pg_database_size('thedbname'));
    SELECT pg_size_pretty(pg_total_relation_size('big_table'));
    SELECT pg_size_pretty(pg_relation_size('big_table')); /* without index */

    /* See indexes on a table with `\d feed_items` */
    /* See indexes on a table with `\d tablename` */
  2. @nesquena nesquena revised this gist Jan 30, 2012. 1 changed file with 1 addition and 3 deletions.
    4 changes: 1 addition & 3 deletions basic.sql
    Original file line number Diff line number Diff line change
    @@ -7,6 +7,4 @@ SELECT pg_size_pretty(pg_database_size('thedbname'));
    SELECT pg_size_pretty(pg_total_relation_size('big_table'));
    SELECT pg_size_pretty(pg_relation_size('big_table')); /* without index */

    /* See indexes on a table */

    \d feed_items
    /* See indexes on a table with `\d feed_items` */
  3. @nesquena nesquena renamed this gist Jan 30, 2012. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. @nesquena nesquena revised this gist Jan 30, 2012. 1 changed file with 12 additions and 0 deletions.
    12 changes: 12 additions & 0 deletions basic
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    /* How to calculate postgreSQL database size in disk ? */

    SELECT pg_size_pretty(pg_database_size('thedbname'));

    /* Calculate size of a table including or excluding the index */

    SELECT pg_size_pretty(pg_total_relation_size('big_table'));
    SELECT pg_size_pretty(pg_relation_size('big_table')); /* without index */

    /* See indexes on a table */

    \d feed_items
  5. @nesquena nesquena revised this gist Sep 7, 2011. 4 changed files with 5 additions and 3 deletions.
    2 changes: 1 addition & 1 deletion biggest_tables.txt → biggest_tables.sql
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    # Finding the total size of your biggest tables
    /* Finding the total size of your biggest tables */

    SELECT nspname || '.' || relname AS "relation",
    pg_size_pretty(pg_total_relation_size(C.oid)) AS "total_size"
    2 changes: 1 addition & 1 deletion index_size.txt → index_size.sql
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    # Table & index sizes along which indexes are being scanned and how many tuples are fetched
    /* Table & index sizes along which indexes are being scanned and how many tuples are fetched */

    SELECT
    t.tablename,
    2 changes: 1 addition & 1 deletion index_summary.txt → index_summary.sql
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    # Pull the number of rows, indexes, and some info about those indexes for each table
    /* Pull the number of rows, indexes, and some info about those indexes for each table */

    SELECT
    pg_class.relname,
    2 changes: 2 additions & 0 deletions index_useless.txt → index_useless.sql
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    /* Finds indices that have not been used (the ones with 0 scans) */

    SELECT
    schemaname,
    relname,
  6. @nesquena nesquena revised this gist Sep 7, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion biggest_relations.sql
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    // Finding the total size of your biggest tables
    /* Finding the total size of your biggest tables */

    SELECT nspname || '.' || relname AS "relation",
    pg_size_pretty(pg_relation_size(C.oid)) AS "size"
  7. @nesquena nesquena revised this gist Sep 7, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion biggest_relations.sql
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    # Finding the total size of your biggest tables
    // Finding the total size of your biggest tables

    SELECT nspname || '.' || relname AS "relation",
    pg_size_pretty(pg_relation_size(C.oid)) AS "size"
  8. @nesquena nesquena renamed this gist Sep 7, 2011. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  9. @nesquena nesquena revised this gist Sep 7, 2011. 1 changed file with 12 additions and 21 deletions.
    33 changes: 12 additions & 21 deletions index_useless.txt
    Original file line number Diff line number Diff line change
    @@ -1,21 +1,12 @@
    SELECT idstat.relname AS table_name,

    indexrelname AS index_name,

    idstat.idx_scan AS times_used,

    pg_size_pretty(pg_table_size(idstat.relname)) AS table_size, pg_size_pretty(pg_table_size(indexrelname)) AS index_size,

    n_tup_upd + n_tup_ins + n_tup_del as num_writes,

    indexdef AS definition

    FROM pg_stat_user_indexes AS idstat JOIN pg_indexes ON indexrelname = indexname

    JOIN pg_stat_user_tables AS tabstat ON idstat.relname = tabstat.relname

    WHERE idstat.idx_scan < 200

    AND indexdef !~* 'unique'

    ORDER BY idstat.relname, indexrelname;
    SELECT
    schemaname,
    relname,
    indexrelname,
    idx_scan,
    pg_size_pretty(pg_relation_size(i.indexrelid)) AS index_size
    FROM
    pg_stat_user_indexes i
    JOIN pg_index USING (indexrelid)
    WHERE
    indisunique IS false
    ORDER BY idx_scan,relname;
  10. @nesquena nesquena revised this gist Sep 7, 2011. 1 changed file with 21 additions and 9 deletions.
    30 changes: 21 additions & 9 deletions index_useless.txt
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,21 @@
    # Finding unused indexes

    select
    indexrelid::regclass as index, relid::regclass as table
    from
    pg_stat_user_indexes
    JOIN pg_index USING (indexrelid)
    where
    idx_scan = 0 and indisunique is false;
    SELECT idstat.relname AS table_name,

    indexrelname AS index_name,

    idstat.idx_scan AS times_used,

    pg_size_pretty(pg_table_size(idstat.relname)) AS table_size, pg_size_pretty(pg_table_size(indexrelname)) AS index_size,

    n_tup_upd + n_tup_ins + n_tup_del as num_writes,

    indexdef AS definition

    FROM pg_stat_user_indexes AS idstat JOIN pg_indexes ON indexrelname = indexname

    JOIN pg_stat_user_tables AS tabstat ON idstat.relname = tabstat.relname

    WHERE idstat.idx_scan < 200

    AND indexdef !~* 'unique'

    ORDER BY idstat.relname, indexrelname;
  11. @nesquena nesquena revised this gist Sep 7, 2011. 1 changed file with 9 additions and 0 deletions.
    9 changes: 9 additions & 0 deletions index_useless.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    # Finding unused indexes

    select
    indexrelid::regclass as index, relid::regclass as table
    from
    pg_stat_user_indexes
    JOIN pg_index USING (indexrelid)
    where
    idx_scan = 0 and indisunique is false;
  12. @nesquena nesquena created this gist Sep 7, 2011.
    9 changes: 9 additions & 0 deletions biggest_relations.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    # Finding the total size of your biggest tables

    SELECT nspname || '.' || relname AS "relation",
    pg_size_pretty(pg_relation_size(C.oid)) AS "size"
    FROM pg_class C
    LEFT JOIN pg_namespace N ON (N.oid = C.relnamespace)
    WHERE nspname NOT IN ('pg_catalog', 'information_schema')
    ORDER BY pg_relation_size(C.oid) DESC
    LIMIT 20;
    11 changes: 11 additions & 0 deletions biggest_tables.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    # Finding the total size of your biggest tables

    SELECT nspname || '.' || relname AS "relation",
    pg_size_pretty(pg_total_relation_size(C.oid)) AS "total_size"
    FROM pg_class C
    LEFT JOIN pg_namespace N ON (N.oid = C.relnamespace)
    WHERE nspname NOT IN ('pg_catalog', 'information_schema')
    AND C.relkind <> 'i'
    AND nspname !~ '^pg_toast'
    ORDER BY pg_total_relation_size(C.oid) DESC
    LIMIT 20;
    31 changes: 31 additions & 0 deletions index_size.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    # Table & index sizes along which indexes are being scanned and how many tuples are fetched

    SELECT
    t.tablename,
    indexname,
    c.reltuples AS num_rows,
    pg_size_pretty(pg_relation_size(t.tablename::text)) AS table_size,
    pg_size_pretty(pg_relation_size(indexrelname::text)) AS index_size,
    CASE WHEN x.is_unique = 1 THEN 'Y'
    ELSE 'N'
    END AS UNIQUE,
    idx_scan AS number_of_scans,
    idx_tup_read AS tuples_read,
    idx_tup_fetch AS tuples_fetched
    FROM pg_tables t
    LEFT OUTER JOIN pg_class c ON t.tablename=c.relname
    LEFT OUTER JOIN
    (SELECT indrelid,
    max(CAST(indisunique AS integer)) AS is_unique
    FROM pg_index
    GROUP BY indrelid) x
    ON c.oid = x.indrelid
    LEFT OUTER JOIN
    ( SELECT c.relname AS ctablename, ipg.relname AS indexname, x.indnatts AS number_of_columns, idx_scan, idx_tup_read, idx_tup_fetch,indexrelname FROM pg_index x
    JOIN pg_class c ON c.oid = x.indrelid
    JOIN pg_class ipg ON ipg.oid = x.indexrelid
    JOIN pg_stat_all_indexes psai ON x.indexrelid = psai.indexrelid )
    AS foo
    ON t.tablename = foo.ctablename
    WHERE t.schemaname='public'
    ORDER BY 1,2;
    36 changes: 36 additions & 0 deletions index_summary.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    # Pull the number of rows, indexes, and some info about those indexes for each table

    SELECT
    pg_class.relname,
    pg_size_pretty(pg_class.reltuples::bigint) AS rows_in_bytes,
    pg_class.reltuples AS num_rows,
    count(indexname) AS number_of_indexes,
    CASE WHEN x.is_unique = 1 THEN 'Y'
    ELSE 'N'
    END AS UNIQUE,
    SUM(case WHEN number_of_columns = 1 THEN 1
    ELSE 0
    END) AS single_column,
    SUM(case WHEN number_of_columns IS NULL THEN 0
    WHEN number_of_columns = 1 THEN 0
    ELSE 1
    END) AS multi_column
    FROM pg_namespace
    LEFT OUTER JOIN pg_class ON pg_namespace.oid = pg_class.relnamespace
    LEFT OUTER JOIN
    (SELECT indrelid,
    max(CAST(indisunique AS integer)) AS is_unique
    FROM pg_index
    GROUP BY indrelid) x
    ON pg_class.oid = x.indrelid
    LEFT OUTER JOIN
    ( SELECT c.relname AS ctablename, ipg.relname AS indexname, x.indnatts AS number_of_columns FROM pg_index x
    JOIN pg_class c ON c.oid = x.indrelid
    JOIN pg_class ipg ON ipg.oid = x.indexrelid )
    AS foo
    ON pg_class.relname = foo.ctablename
    WHERE
    pg_namespace.nspname='public'
    AND pg_class.relkind = 'r'
    GROUP BY pg_class.relname, pg_class.reltuples, x.is_unique
    ORDER BY 2;