gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 11:30:48 27 4
gpt4 key购买 nike

我想弄清楚如何确定数据库中特定列的大小,例如我有两列分别称为 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com