Chapter 3. データ型

Table of Contents
数値型
貨幣型
文字データ型
日付/時刻型
ブール値型
地理データ型
インターネットプロトコル バージョン4 上の ネットワークアドレスとホストアドレス

Describes the built-in data types available in Postgres.

Postgres で使用可能なデータ型に ついて説明します。

Postgres has a rich set of native data types available to users. Users may add new types to Postgres using the DEFINE TYPE command described elsewhere.

Postgres は、多くのネイティブな データ型を提供してしています。 また、別で解説している DEFINE TYPE コマンドを使えば、Postgres に新しい型 を追加することもできます。

In the context of data types, the following sections will discuss SQL standards compliance, porting issues, and usage.

データ型の内容について、以下のセクションでSQL標準への準拠や 移植状況、および使用法について解説しています。 Some Postgres types correspond directly to SQL92-compatible types. In other cases, data types defined by SQL92 syntax are mapped directly into native Postgres types. いくつかの Postgres データ型は直接 SQL92 互換のデータ型に対応しています。その他の 場合、SQL92 文法で定義されているデータ型は、 直接ネイティブな Postgres のデータ型 にマッピングされています。 Many of the built-in types have obvious external formats. However, several types are either unique to Postgres, such as open and closed paths, or have several possibilities for formats, such as the date and time types. 組み込み済みのデータ型の多くは明示的な外部フォーマットを 持っています。しかし、"開いたパス"あるいは"閉じたパス"を保存する path 型のように Postgres 固有のものも あれば、data 型や time 型のように、複数のフォーマットを持つものも あります。

Table 3-1. Postgres のデータ型

Postgres のデータ型 SQL92 又は SQL3 のデータ型説明
boolboolean論理ブール型(真/偽)
box 2次平面における矩形
char(n)character(n)固定長文字列
cidr インターネットプロトコル バージョン4 の ネットワークアドレスまたはホストアドレス
circle 2次平面における円形
datedate時刻を含まない日付
float4/8float(p)精度pを持つ浮動小数点数
float8real, double precision倍精度浮動小数点数
inet インターネットプロトコル バージョン4 における ネットワークアドレスまたはホストアドレス
int2smallint符号付き2バイト整数
int4int, integer符号付き4バイト整数
int4decimal(p,s)p <= 9, s = 0 な正確な数値
int4numeric(p,s)p == 9, s = 0 な正確な数値
int8 符号付き8バイト整数
line 2次平面における直線
lseg 2次平面における線分
moneydecimal(9,2)米国通貨
path 2次平面における開いた、および閉じた座標パス
point 2次平面における座標点
polygon 2次平面における閉じた座標パス(多角形)
serial インデックスや相互参照に使用される重複しない番号
timetime時刻
timespaninterval汎用的な時間の間隔
timestamptimestamp with time zone日付/時間
varchar(n)character varying(n)可変長文字列

Note: The cidr and inet types are designed to handle any IP type but only ipv4 is handled in the current implementation. Everything here that talks about ipv4 will apply to ipv6 in a future release.

cidr 型と inet 型は、すべての インターネットプロトコルの種類を扱えるように設計されて いましたが、現在の実装では、インターネットプロトコル バージョン4しか扱うことが出来ません。 ここで説明するバージョン4の内容は、将来のバージョンで サポートされるはずのバージョン6にも当てはまるはずです。

Table 3-2. Postgres 関数定数

Postgres 関数SQL92 定数説明
getpgusername()current_user現在のセッションにおけるユーザ名
date('now')current_date現在のトランザクションにおける日付
time('now')current_time現在のトランザクションにおける時刻
timestamp('now')current_timestamp現在のトランザクションにおける日付と時刻

Postgres has features at the forefront of ORDBMS development. In addition to SQL3 conformance, substantial portions of SQL92 are also supported. Although we strive for SQL92 compliance, there are some aspects of the standard which are ill considered and which should not live through subsequent standards. Postgres will not make great efforts to conform to these features; however, these tend to apply in little-used or obsure cases, and a typical user is not likely to run into them.

Postgres は、ORDBMS 開発の最先端 の機能を持っています。SQL3 対応に加え、 SQL92 の相当の部分がサポートされています。 SQL92 の準拠に最大限の努力をしてはいます が、標準案の中にはまずい考え方や未来の標準としては生き残れ ないような物もあります。 Postgres では、それらについて 積極的に対応する考えはありません。しかしながら、これら はあまり使用されないで廃れてゆく傾向にあり、通常のユーザは それらをまず使うことはないようです。

Most of the input and output functions corresponding to the base types (e.g., integers and floating point numbers) do some error-checking. Some of the operators and functions (e.g., addition and multiplication) do not perform run-time error-checking in the interests of improving execution speed. On some systems, for example, the numeric operators for some data types may silently underflow or overflow.

ベースとなる型(整数や浮動小数点数など)に対応しているほとんど の入出力関数は、いくつかのエラーチェックをしますが、特別に 厳密なチェックを行わないものもあります。より重要なのは、 演算子や関数 (たとえば加算や乗算) によっては、全くエラー チェックを行わないものがあることです。このため、多くの数値 演算子が(たとえば)アンダーフローやオーバーフローを起こしたと しても、それをユーザに通知することはありません。

Note that some of the input and output functions are not invertible. That is, the result of an output function may lose precision when compared to the original input.

入出力関数の中には可逆的でないものもあります。つまり、ある 出力関数の結果をオリジナルの入力と比較した場合、有効桁数を 失うことがありうるということです。

Note: The original Postgres v4.2 code received from Berkeley rounded all double precision floating point results to six digits for output. Starting with v6.1, floating point numbers are allowed to retain most of the intrinsic precision of the type (typically 15 digits for doubles, 6 digits for 4-byte floats). Other types with underlying floating point fields (e.g. geometric types) carry similar precision.

Berkeley 校から受け取った Postgres v4.2 のオリジナル コードでは、全ての倍精度浮動小数点数を全て6桁に四捨五入 していました。 v6.1から、浮動小数点数はそのデータ型の最も本質的な精度 (一般には double 型では15桁、float 型では6桁)を保持する ことが出来るようになりました。 geometric 型のような存在的に浮動小数点をもつ他のデータ型 でも、似たような精度を伴っています。

数値型

Numeric types consist of two- and four-byte integers and four- and eight-byte floating point numbers.

数値型には、2バイトと4バイトの整数と、4バイトと8バイトの 浮動小数点数が在ります。

Table 3-3. Postgres の数値型

数値型記憶領域説明有効範囲
float44 bytes可変精度小数位6桁
float88 bytes可変精度小数位15桁
int22 bytes固定精度-32768 から +32767
int44 bytes固定精度。通常これを選択-2147483648 から +2147483647
int88 bytes固定精度。非常に大きな有効範囲を持つ+/- > 18 桁の整数
serial4 bytesインデックスや相互参照に使用する0 から +2147483647

The numeric types have a full set of corresponding arithmetic operators and functions. Refer to 算術演算子 and 算術関数 for more information.

数値型は対応する算術演算子と関数をフルセットで持っています。 詳細は 算術演算子算術関数 を参照して ください。

The serial type is a special-case type constructed by Postgres from other existing components. It is typically used to create unique identifiers for table entries. In the current implementation, specifying

serial 型は、Postgres が既に在るコンポーネントの組み合わせで作る特別なデータ型で、 一般的にはテーブルの項目に一意な識別子(id)を作るため使われます。 現在の実装では serial 型を使用する時に、

CREATE TABLE tablename
(colname SERIAL);
is equivalent to specifying: と指定するのは、以下と同等です:
CREATE SEQUENCE tablename_colname_seq;
CREATE TABLE tablename
    (colname INT4 DEFAULT nextval('tablename_colname_seq');
CREATE UNIQUE INDEX tablename_colname_key on tablename (colname);

Caution

The implicit sequence created for the serial type will not be automatically removed when the table is dropped. So, the following commands executed in order will likely fail:

serial 型を含んでいるテーブルが削除されても、 serial 型のために作られた暗示的なシーケンスは、 自動的に削除されません。 そのため、次のようなコマンド操作は多分失敗します:

CREATE TABLE tablename
(colname SERIAL);
DROP TABLE tablename;
CREATE TABLE tablename
(colname SERIAL);
The sequence will remain in the database until explicitly dropped using DROP SEQUENCE. シーケンスは DROP SEQUENCE コマンドを使って 明示的に削除しない限りデータベース内に残っています。

The int8 type may not be available on all platforms since it relies on compiler support for this.

int8 型は、コンパイラに依存することから 全てのプラットフォーム上で使用可能と言う訳では有りません。