PostgreSQL查看資料庫,索引,資料表空間大小

來源:互聯網
上載者:User
 代碼如下 複製代碼

執行個體講解

3.1 查看儲存一個指定的數值需要的位元組數


david=# select pg_column_size(1);    
 pg_column_size
----------------
              4
(1 row)

david=# select pg_column_size(10000);
 pg_column_size
----------------
              4
(1 row)

david=# select pg_column_size('david');
 pg_column_size
----------------
              6
(1 row)

david=# select pg_column_size('hello,world');
 pg_column_size
----------------
             12
(1 row)

david=# select pg_column_size('2013-04-18 15:17:21.622885+08');
 pg_column_size
----------------
             30
(1 row)

david=# select pg_column_size('中國');                        
 pg_column_size
----------------
              7
(1 row)

david=# 3.2 查看資料庫大小

查看未經處理資料


david=# d test
              Table "public.test"
  Column   |         Type          | Modifiers
-----------+-----------------------+-----------
 id        | integer               |
 name      | character varying(20) |
 gender    | boolean               |
 join_date | date                  |
 dept      | character(4)          |
Indexes:
    "idx_join_date_test" btree (join_date)
    "idx_test" btree (id)

david=# select count(1) from test;
  count 
---------
 1835008
(1 row)

david=# 查看david 資料庫大小


david=# select pg_database_size('david');
 pg_database_size
------------------
        190534776
(1 row)

david=# 查看所有資料庫大小


david=# select pg_database.datname, pg_database_size(pg_database.datname) AS size from pg_database;               
  datname  |    size    
-----------+-------------
 template0 |     6513156
 postgres  |     6657144
 jboss     |     6521348
 bugs      |     6521348
 david     |   190534776
 BMCV3     | 28147135608
 mydb      |    10990712
 template1 |     6521348
(8 rows)

david=# 這樣查出來的結果,看上去太長了,不太容易讀數。

3.3 以人性化的方式顯示大小


david=# select pg_size_pretty(pg_database_size('david'));
 pg_size_pretty
----------------
 182 MB
(1 row)

david=# 3.4 查看單索引大小


david=# select pg_relation_size('idx_test');
 pg_relation_size
------------------
         41238528
(1 row)

david=# select pg_size_pretty(pg_relation_size('idx_test'));
 pg_size_pretty
----------------
 39 MB
(1 row)

david=#
david=# select pg_size_pretty(pg_relation_size('idx_join_date_test'));
 pg_size_pretty
----------------
 39 MB
(1 row)

david=# 3.5 查看指定表中所有索引大小


david=# select pg_indexes_size('test');                 
 pg_indexes_size
-----------------
        82477056
(1 row)

david=# select pg_size_pretty(pg_indexes_size('test'));
 pg_size_pretty
----------------
 79 MB
(1 row)

david=# idx_test 和idx_join_date_test 兩個索引大小加起來差不多等於上面pg_indexes_size() 查詢出來的索引大小。

3.6 查看指定schema 裡所有的索引大小,按從大到小的順序排列。


david=# select * from pg_namespace;
      nspname       | nspowner |               nspacl               
--------------------+----------+-------------------------------------
 pg_toast           |       10 |
 pg_temp_1          |       10 |
 pg_toast_temp_1    |       10 |
 pg_catalog         |       10 | {postgres=UC/postgres,=U/postgres}
 information_schema |       10 | {postgres=UC/postgres,=U/postgres}
 public             |       10 | {postgres=UC/postgres,=UC/postgres}
(6 rows)

david=# select indexrelname, pg_size_pretty(pg_relation_size(relid)) from pg_stat_user_indexes where schemaname='public' order by pg_relation_size(relid) desc;
         indexrelname          | pg_size_pretty
-------------------------------+----------------
 idx_join_date_test            | 91 MB
 idx_test                      | 91 MB
 testtable_idx                 | 1424 kB
 city_pkey                     | 256 kB
 city11                        | 256 kB
 countrylanguage_pkey          | 56 kB
 sale_pkey                     | 8192 bytes
 track_pkey                    | 8192 bytes
 tbl_partition_201211_joindate | 8192 bytes
 tbl_partition_201212_joindate | 8192 bytes
 tbl_partition_201301_joindate | 8192 bytes
 tbl_partition_201302_joindate | 8192 bytes
 tbl_partition_201303_joindate | 8192 bytes
 customer_pkey                 | 8192 bytes
 album_pkey                    | 8192 bytes
 item_pkey                     | 8192 bytes
 tbl_partition_201304_joindate | 8192 bytes
 tbl_partition_201307_joindate | 8192 bytes
 tbl_partition_201305_joindate | 0 bytes
 tbl_partition_201306_joindate | 0 bytes
(20 rows)

david=# 3.7 查看指定表大小


david=# select pg_relation_size('test');               
 pg_relation_size
------------------
         95748096
(1 row)

david=# select pg_size_pretty(pg_relation_size('test'));
 pg_size_pretty
----------------
 91 MB
(1 row)

david=# 使用pg_table_size() 函數查看


david=# select pg_table_size('test');                  
 pg_table_size
---------------
      95789056
(1 row)

david=# select pg_size_pretty(pg_table_size('test'));  
 pg_size_pretty
----------------
 91 MB
(1 row)

david=# 3.8 查看指定表的總大小


david=# select pg_total_relation_size('test');      
 pg_total_relation_size
------------------------
              178266112
(1 row)

david=# select pg_size_pretty(pg_total_relation_size('test'));
 pg_size_pretty
----------------
 170 MB
(1 row)

david=# 3.9 查看指定schema 裡所有的表大小,按從大到小的順序排列。


david=# select relname, pg_size_pretty(pg_relation_size(relid)) from pg_stat_user_tables where schemaname='public' order by pg_relation_size(relid) desc;
            relname            | pg_size_pretty
-------------------------------+----------------
 test                          | 91 MB
 testtable                     | 1424 kB
 city                          | 256 kB
 countrylanguage               | 56 kB
 country                       | 40 kB
 testcount                     | 8192 bytes
 tbl_partition_201302          | 8192 bytes
 tbl_partition_201303          | 8192 bytes
 person                        | 8192 bytes
 customer                      | 8192 bytes
 american_state                | 8192 bytes
 tbl_david                     | 8192 bytes
 emp                           | 8192 bytes
 tbl_partition_201212          | 8192 bytes
 tbl_partition_201304          | 8192 bytes
 tbl_partition_error_join_date | 8192 bytes
 tbl_partition_201211          | 8192 bytes
 album                         | 8192 bytes
 tbl_partition_201307          | 8192 bytes
 tbl_xulie                     | 8192 bytes
 tbl_partition_201301          | 8192 bytes
 sale                          | 8192 bytes
 item                          | 8192 bytes
 track                         | 8192 bytes
 tbl_partition_201306          | 0 bytes
 tbl_partition                 | 0 bytes
 tbl_partition_201305          | 0 bytes
 person2                       | 0 bytes
(28 rows)

david=# 3.10 查看錶空間大小


david=# select spcname from pg_tablespace;
  spcname  
------------
 pg_default
 pg_global
(2 rows)

david=# select pg_tablespace_size('pg_default');               
 pg_tablespace_size
--------------------
        28381579760
(1 row)

david=# select pg_size_pretty(pg_tablespace_size('pg_default'));
 pg_size_pretty
----------------
 26 GB
(1 row)

david=# 另一種查看方法:


david=# select pg_tablespace_size('pg_default')/1024/1024 as "SIZE M";    
 SIZE M
--------
  27066
(1 row)

david=# select pg_tablespace_size('pg_default')/1024/1024/1024 as "SIZE G";
 SIZE G
--------
     26
(1 row)

david=#

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.