小编典典

计算PostgreSQL中列类型的大小

sql

我试图弄清楚如何确定数据库中特定列的大小,例如,我有两列称为sourceip,destinationip的列,它们都是16字节字段。

我以为这将是在information_schema或\ d +中的某个位置,但是我找不到用于隔离每种列类型的大小的特定命令。

您可以在数据库中计算列类型的大小,还是只需要在Postgresql文档中引用每种类型的字节大小?


阅读 358

收藏
2021-03-23

共1个答案

小编典典

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时,类型没有固定长度

对于varlena类型,请使用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
2021-03-23