7

たとえば、データベース内の特定の列のサイズを決定する方法を見つけようとしています。たとえば、sourceip、destinationip という 2 つの列があり、どちらも 16 バイト フィールドです。

これは information_schema または \d+ のどこかにあると思いましたが、各列タイプのサイズを分離するための特定のコマンドが見つかりません。

データベースで列の型のサイズを計算できますか、それとも Postgresql のドキュメントで各型のバイト サイズを参照する必要がありますか?

4

1 に答える 1

15

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
于 2012-09-12T20:26:37.747 に答える