2012-09-12 52 views
1

我想弄清楚如何確定數據庫中特定列的大小,例如我有兩列稱爲sourceip,destinationip都是16字節的字段。計算Postgresql中的列類型的大小

我認爲這將在information_schema或\ d +中的某處,但我找不到特定的命令來隔離每個列類型的大小。

您可以計算數據庫中的列類型大小,還是隻需要在Postgresql文檔中引用每種類型的字節大小?

回答

9

pg中只有幾種類型具有固定長度 - 幾乎所有類型都是varlena類型 - 它具有動態長度。你可以檢查查詢,如

postgres=# select typlen from pg_type where oid = 'int'::regtype::oid; 
    typlen 
-------- 
     4 
(1 row) 


postgres=# select attlen from pg_attribute where attrelid = 'x'::regclass and attname = 'a'; 
    attlen 
-------- 
     4 
(1 row) 

當結果不爲-1,然後鍵入沒有固定長度

一個變長類型使用pg_column_size功能:

postgres=# \df *size* 
            List of functions 
    Schema |   Name   | Result data type | Argument data types | Type 
------------+------------------------+------------------+---------------------+-------- 
pg_catalog | pg_column_size   | integer   | "any"    | normal 
pg_catalog | pg_database_size  | bigint   | name    | normal 
pg_catalog | pg_database_size  | bigint   | oid     | normal 
pg_catalog | pg_indexes_size  | bigint   | regclass   | normal 
pg_catalog | pg_relation_size  | bigint   | regclass   | normal 
pg_catalog | pg_relation_size  | bigint   | regclass, text  | normal 
pg_catalog | pg_size_pretty   | text    | bigint    | normal 
pg_catalog | pg_size_pretty   | text    | numeric    | normal 
pg_catalog | pg_table_size   | bigint   | regclass   | normal 
pg_catalog | pg_tablespace_size  | bigint   | name    | normal 
pg_catalog | pg_tablespace_size  | bigint   | oid     | normal 
pg_catalog | pg_total_relation_size | bigint   | regclass   | normal 
(12 rows) 



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

postgres=# select pg_column_size(10); 
    pg_column_size 
---------------- 
       4 
(1 row) 

postgres=# select pg_column_size(now()); 
    pg_column_size 
---------------- 
       8 
+0

感謝您的幫助。輸出是否以字節爲單位? – user7980

+0

是的,它是ub字節 –