sql - 计算Postgresql中列类型的大小

标签 sql postgresql

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

我认为这会在 information_schema 或\d+ 中的某处,但我找不到特定的命令来隔离每个列类型的大小。

Can you calculate column type size in database or do you just have to reference the byte size for each type in the Postgresql documentation?

最佳答案

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

关于sql - 计算Postgresql中列类型的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12394538/

相关文章:

sql - 混合字数串的人性化或自然数排序

database - 如何在我的表中找到丢失的 ID?

sql - 难以定义分区的平均值

Django 应用程序未连接远程数据库

sql - 如何安全地允许用户定义的 SQL 查询?

android - 将 Android 应用程序与 SQLite 数据库连接时找不到表名

php - PostgreSQL 中的循环 ORDER BY 排序

postgresql - 从平面文件插入 GPFdist 在插入时抛出错误 'invalid byte sequence for encoding "UTF 8": 0x00'

sql - PostgreSQL 增量日期?

SQL查询删除超过特定行数的最旧行?