Skip to main content
Added a word to the purpose of the posted PL/SQL script
Source Link

I just recently run in to the same problem. So i wrote PL/SQL script to print the index data size for all the tables which contains some indexes. Maybe someone will find this script helpful:

BEGIN;
DO $$
DECLARE

    _schemaTable VARCHAR;
    _schemaTables VARCHAR[];

    _indexSize VARCHAR;
    _relationSize VARCHAR;
    _totalRelationSize VARCHAR;

BEGIN

    -- Find all tables (include schema name) which contains some indexes
    SELECT ARRAY(
        SELECT concat_ws('.', schemaname, tablename) FROM pg_indexes
    ) INTO _schemaTables;

    -- Read and print the size of indexes for given tables
    FOREACH _schemaTable IN ARRAY _schemaTables LOOP
        SELECT pg_size_pretty(pg_indexes_size(_schemaTable)),
               pg_size_pretty(pg_relation_size(_schemaTable)),
               pg_size_pretty(pg_total_relation_size(_schemaTable))
        INTO _indexSize, _relationSize, _totalRelationSize;

        RAISE NOTICE 'table: %, indexes size: %, relation size: %, total relation size: %', _schemaTable, _indexSize, _relationSize, _totalRelationSize;
    END LOOP;
    
    -- https://www.postgresql.org/docs/9.4/functions-admin.html#FUNCTIONS-ADMIN-DBSIZE
    -- pg_indexes_size: Total disk space used by indexes attached to the specified table
    -- pg_relation_size shows: disk space used by the specified fork (if nothing specified, 'main' fork will be used)
    -- pg_total_relation_size: Total disk space used by the specified table, including all indexes and TOAST data

END $$;
COMMIT;

Basically the purpose of this script was to find out on which table the index produce such a huge disc usage.

I just recently run in to the same problem. So i wrote PL/SQL script to print the index data size for all the tables which contains some indexes. Maybe someone will find this script helpful:

BEGIN;
DO $$
DECLARE

    _schemaTable VARCHAR;
    _schemaTables VARCHAR[];

    _indexSize VARCHAR;
    _relationSize VARCHAR;
    _totalRelationSize VARCHAR;

BEGIN

    -- Find all tables (include schema name) which contains some indexes
    SELECT ARRAY(
        SELECT concat_ws('.', schemaname, tablename) FROM pg_indexes
    ) INTO _schemaTables;

    -- Read and print the size of indexes for given tables
    FOREACH _schemaTable IN ARRAY _schemaTables LOOP
        SELECT pg_size_pretty(pg_indexes_size(_schemaTable)),
               pg_size_pretty(pg_relation_size(_schemaTable)),
               pg_size_pretty(pg_total_relation_size(_schemaTable))
        INTO _indexSize, _relationSize, _totalRelationSize;

        RAISE NOTICE 'table: %, indexes size: %, relation size: %, total relation size: %', _schemaTable, _indexSize, _relationSize, _totalRelationSize;
    END LOOP;
    
    -- https://www.postgresql.org/docs/9.4/functions-admin.html#FUNCTIONS-ADMIN-DBSIZE
    -- pg_indexes_size: Total disk space used by indexes attached to the specified table
    -- pg_relation_size shows: disk space used by the specified fork (if nothing specified, 'main' fork will be used)
    -- pg_total_relation_size: Total disk space used by the specified table, including all indexes and TOAST data

END $$;
COMMIT;

I just recently run in to the same problem. So i wrote PL/SQL script to print the index data size for all the tables which contains some indexes. Maybe someone will find this script helpful:

BEGIN;
DO $$
DECLARE

    _schemaTable VARCHAR;
    _schemaTables VARCHAR[];

    _indexSize VARCHAR;
    _relationSize VARCHAR;
    _totalRelationSize VARCHAR;

BEGIN

    -- Find all tables (include schema name) which contains some indexes
    SELECT ARRAY(
        SELECT concat_ws('.', schemaname, tablename) FROM pg_indexes
    ) INTO _schemaTables;

    -- Read and print the size of indexes for given tables
    FOREACH _schemaTable IN ARRAY _schemaTables LOOP
        SELECT pg_size_pretty(pg_indexes_size(_schemaTable)),
               pg_size_pretty(pg_relation_size(_schemaTable)),
               pg_size_pretty(pg_total_relation_size(_schemaTable))
        INTO _indexSize, _relationSize, _totalRelationSize;

        RAISE NOTICE 'table: %, indexes size: %, relation size: %, total relation size: %', _schemaTable, _indexSize, _relationSize, _totalRelationSize;
    END LOOP;
    
    -- https://www.postgresql.org/docs/9.4/functions-admin.html#FUNCTIONS-ADMIN-DBSIZE
    -- pg_indexes_size: Total disk space used by indexes attached to the specified table
    -- pg_relation_size shows: disk space used by the specified fork (if nothing specified, 'main' fork will be used)
    -- pg_total_relation_size: Total disk space used by the specified table, including all indexes and TOAST data

END $$;
COMMIT;

Basically the purpose of this script was to find out on which table the index produce such a huge disc usage.

Source Link

I just recently run in to the same problem. So i wrote PL/SQL script to print the index data size for all the tables which contains some indexes. Maybe someone will find this script helpful:

BEGIN;
DO $$
DECLARE

    _schemaTable VARCHAR;
    _schemaTables VARCHAR[];

    _indexSize VARCHAR;
    _relationSize VARCHAR;
    _totalRelationSize VARCHAR;

BEGIN

    -- Find all tables (include schema name) which contains some indexes
    SELECT ARRAY(
        SELECT concat_ws('.', schemaname, tablename) FROM pg_indexes
    ) INTO _schemaTables;

    -- Read and print the size of indexes for given tables
    FOREACH _schemaTable IN ARRAY _schemaTables LOOP
        SELECT pg_size_pretty(pg_indexes_size(_schemaTable)),
               pg_size_pretty(pg_relation_size(_schemaTable)),
               pg_size_pretty(pg_total_relation_size(_schemaTable))
        INTO _indexSize, _relationSize, _totalRelationSize;

        RAISE NOTICE 'table: %, indexes size: %, relation size: %, total relation size: %', _schemaTable, _indexSize, _relationSize, _totalRelationSize;
    END LOOP;
    
    -- https://www.postgresql.org/docs/9.4/functions-admin.html#FUNCTIONS-ADMIN-DBSIZE
    -- pg_indexes_size: Total disk space used by indexes attached to the specified table
    -- pg_relation_size shows: disk space used by the specified fork (if nothing specified, 'main' fork will be used)
    -- pg_total_relation_size: Total disk space used by the specified table, including all indexes and TOAST data

END $$;
COMMIT;