Efter valg af forespørgsel vil alle tabellen og dens størrelse returneres
SELECT
relname as mytable,
pg_size_pretty(pg_relation_size(relid)) As size
FROM pg_catalog.pg_statio_user_tables ORDER BY pg_total_relation_size(relid) DESC;
Opret en VIEW med dette valg
CREATE VIEW vTableAndSize AS
SELECT
relname as mytable,
pg_size_pretty(pg_relation_size(relid)) As size
FROM pg_catalog.pg_statio_user_tables ORDER BY pg_total_relation_size(relid) DESC;
og nu kan du forespørge på denne visning for at få størrelsen som denne
SELECT mytable,size
FROM vTableAndSize WHERE mytable in ('table1','table2')
Ifølge OPs kommentar
CREATE VIEW vTableAndSize_1 as
SELECT
relname as mytable,
(pg_relation_size(relid)) As size
FROM pg_catalog.pg_statio_user_tables ORDER BY pg_total_relation_size(relid) DESC;
og få størrelsessummen af flere kolonner ved hjælp af
/* Get sum of specific tables */
SELECT pg_size_pretty(sum(size)) tablesizesum
FROM vTableAndSize_1 WHERE mytable in ('table1','table2')
/* Get sum of all tables */
SELECT pg_size_pretty(sum(size)) tablesizesum
FROM vTableAndSize_1
Opret vTableAndSize_1
i din PostgreSQL
database og forespørgsel som nedenfor i din frontend (er ikke bekendt med Ruby
)
ActiveRecord::Base.connection.execute("SELECT pg_size_pretty(sum(size)) FROM vTableAndSize_1
WHERE mytable in ('table1','table2')")