简体   繁体   English

如何获取数据库中所有表的FreeSpace之和

[英]How to get the sum of FreeSpace of all the tables in a database

To Get all the user Table names in the database using: 使用以下命令获取数据库中的所有用户表名称:

select relname from pg_stat_user_tables;

relname
-------
Table1
Table2
Table3

To Get the free space marked by vacuum of one table using: 要使用一张桌子的真空标记可用空间,请使用:

SELECT sum(avail) FROM pg_freespace('Table1');

sum
-----
1728

I want to get the total free space of all the tables in single query. 我想在单个查询中获取所有表的总可用空间。

Say for example, 举例来说,

Table1 has 1728 freespace
Table2 has 100 freespace
Table3 has 100 freespace

How to get 1928 as answer with a query? 如何通过查询获取1928作为答案?

I suggest you can query it first through a table list all table in database such as pg_table and then sum it all. 我建议您首先通过一个表列出数据库中的所有表(例如pg_table来查询它,然后将它们加总。 Such like 2 query below: 1. list all table and its freespace: 比如下面的2个查询:1.列出所有表及其可用空间:

SELECT fulltablename, sum(avail) as freespace
FROM (
    SELECT concat(schemaname,'.',tablename) as fulltablename, (pg_freespace(concat(schemaname,'.',tablename))).* 
      FROM pg_tables 
    ) t
GROUP BY fulltablename
  1. sum all freespace in database: 对数据库中的所有可用空间求和:
SELECT sum(avail) as freespace
FROM (
    SELECT concat(schemaname,'.',tablename) as fulltablename, (pg_freespace(concat(schemaname,'.',tablename))).* 
      FROM pg_tables 
    ) t

I hope this answer will satisfy you. 我希望这个答案能使您满意。

I used the below query to solve my problem (To get the sum of freespace of all table of my schema) 我使用以下查询解决了我的问题(以获取架构所有表的自由空间之和)

SELECT sum(avail) as freespace
FROM (
    SELECT  (pg_freespace(concat(schemaname,'."',tablename,'"'))).* 
    FROM pg_tables where schemaname = 'mySchema'
) t
;

I have tweaked Mabu's sql to give a single total freespace value for all tables 我已经调整了Mabu的sql,以便为所有表提供一个总的自由空间值

SELECT sum(freespace) as TotalFreeSpace
FROM (
    SELECT fulltablename, sum(avail) as freespace
    FROM (
        SELECT concat(schemaname,'.',tablename) as fulltablename, 
        (pg_freespace(concat(schemaname,'.',tablename))).*
        FROM pg_tables
        ) t
    GROUP BY fulltablename) f

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM