バージョンごとのドキュメント一覧

38.2. PostgreSQLの型システム #

<title>The <productname>PostgreSQL</productname> Type System</title>

<productname>PostgreSQL</productname> data types can be divided into base types, container types, domains, and pseudo-types. PostgreSQLのデータ型は、基本型、コンテナ型、ドメイン、疑似型に分類されます。

38.2.1. 基本型 #

<title>Base Types</title>

Base types are those, like <type>integer</type>, that are implemented below the level of the <acronym>SQL</acronym> language (typically in a low-level language such as C). They generally correspond to what are often known as abstract data types. <productname>PostgreSQL</productname> can only operate on such types through functions provided by the user and only understands the behavior of such types to the extent that the user describes them. The built-in base types are described in <xref linkend="datatype"/>. 基本型はintegerのように、SQL言語レベル以下で実装されたものです (通常はCのような低レベル言語で作成されます)。 一般的にこれらは抽象データ型とも呼ばれるものに対応します。 PostgreSQLは、ユーザによって提供された関数を通してのみ、こうした型に対する操作が可能で、また、こうした型の動作をユーザが記述する限りにおいてのみ理解します。 組み込みの基本型は、第8章に記載されています。

Enumerated (enum) types can be considered as a subcategory of base types. The main difference is that they can be created using just <acronym>SQL</acronym> commands, without any low-level programming. Refer to <xref linkend="datatype-enum"/> for more information. 列挙(enum)型は基本型の一種とみなすことができます。 主な違いは、列挙型は低レベルプログラミング無しに、SQLコマンドだけで作ることができることです。 より詳細については、8.7を参照してください。

38.2.2. コンテナ型 #

<title>Container Types</title>

<productname>PostgreSQL</productname> has three kinds of <quote>container</quote> types, which are types that contain multiple values of other types. These are arrays, composites, and ranges. PostgreSQLには三種類のコンテナ型があります。これは他の型の複数の値を含む型です。 配列、複合型、範囲型があります。

Arrays can hold multiple values that are all of the same type. An array type is automatically created for each base type, composite type, range type, and domain type. But there are no arrays of arrays. So far as the type system is concerned, multi-dimensional arrays are the same as one-dimensional arrays. Refer to <xref linkend="arrays"/> for more information. 配列は、全て同じ型の複数の値を保持できます。 配列型は各基本型、複合型、範囲型およびドメイン型に対して自動的に作られます。 しかし、配列の配列はありません。 この型システムにおいては多次元配列は一次元配列と同じです。 より詳細については、8.15を参照してください。

Composite types, or row types, are created whenever the user creates a table. It is also possible to use <xref linkend="sql-createtype"/> to define a <quote>stand-alone</quote> composite type with no associated table. A composite type is simply a list of types with associated field names. A value of a composite type is a row or record of field values. Refer to <xref linkend="rowtypes"/> for more information. ユーザがテーブルを作成すると、複合型、もしくは行型が作成されます。 関連するテーブルを持たないスタンドアローンの複合型をCREATE TYPEを使用して定義することもできます。 複合型は関連したフィールド名を持つ基本型の単なるリストです。 複合型の値は行もしくはフィールド値のレコードです。 より詳細については、8.16を参照してください。

A range type can hold two values of the same type, which are the lower and upper bounds of the range. Range types are user-created, although a few built-in ones exist. Refer to <xref linkend="rangetypes"/> for more information. 範囲型は同じ型の二つの値を保持できます。これらは範囲の下限と上限です。 範囲型はユーザによって作られますが、少数の組み込みの型もあります。 より詳細については、8.17を参照してください。

38.2.3. ドメイン #

<title>Domains</title>

A domain is based on a particular underlying type and for many purposes is interchangeable with its underlying type. However, a domain can have constraints that restrict its valid values to a subset of what the underlying type would allow. Domains are created using the <acronym>SQL</acronym> command <xref linkend="sql-createdomain"/>. Refer to <xref linkend="domains"/> for more information. ドメインは、特定の元となる型に基づいたもので、多くの目的では、その元となる型と交換可能です。 しかし、ドメインは元となる基本型で許可される範囲内で値の有効範囲を制限する制約を持つことができます。 ドメインはSQLコマンドのCREATE DOMAINを使って作られます。 より詳細については、8.18を参照してください。

38.2.4. 疑似データ型 #

<title>Pseudo-Types</title>

There are a few <quote>pseudo-types</quote> for special purposes. Pseudo-types cannot appear as columns of tables or components of container types, but they can be used to declare the argument and result types of functions. This provides a mechanism within the type system to identify special classes of functions. <xref linkend="datatype-pseudotypes-table"/> lists the existing pseudo-types. 特殊な目的用に数個の疑似型があります。 疑似型はテーブルの列やコンテナ型の構成要素として現れることはありません。 しかし、関数の引数や結果型を宣言する際に使用できます。 これは、型システム内で特殊な関数クラスを識別するための機構を提供します。 表 8.27に既存の疑似型を列挙します。

38.2.5. 多様型 #

<title>Polymorphic Types</title>

Some pseudo-types of special interest are the <firstterm>polymorphic types</firstterm>, which are used to declare <firstterm>polymorphic functions</firstterm>. This powerful feature allows a single function definition to operate on many different data types, with the specific data type(s) being determined by the data types actually passed to it in a particular call. The polymorphic types are shown in <xref linkend="extend-types-polymorphic-table"/>. Some examples of their use appear in <xref linkend="xfunc-sql-polymorphic-functions"/>. いくつかの特殊な用途を持つ疑似型は多様型で、多様関数を宣言するのに使われます。 この強力な機能により、特定の呼び出しで実際に渡されたデータ型で決定される具体的なデータ型について、一つの関数定義で多数の異なるデータ型を処理できるようになります。 多様型を表 38.1に示します。 これらの使用例は38.5.11にあります。

表38.1 多様型

<title>Polymorphic Types</title>
名前説明
anyelementSimple関数があらゆるデータ型を受け付けることを示します
anyarraySimple関数があらゆる配列データ型を受け付けることを示します
anynonarraySimple関数があらゆる配列でないデータ型を受け付けることを示します
anyenumSimple関数があらゆる列挙型(8.7を参照)を受け付けることを示します
anyrangeSimple関数があらゆる範囲データ型(8.17を参照)を受け付けることを示します
anymultirangeSimple関数があらゆる多重範囲データ型(8.17を参照)を受け付けることを示します
anycompatibleCommonさまざまな引数の共通データ型への自動昇格によって、関数があらゆるデータ型を受け付けることを示します
anycompatiblearrayCommonさまざまな引数の共通データ型への自動昇格によって、関数があらゆる配列データ型を受け付けることを示します
anycompatiblenonarrayCommonさまざまな引数の共通データ型への自動昇格によって、関数があらゆる配列でないデータ型を受け付けることを示します
anycompatiblerangeCommonさまざまな引数の共通データ型への自動昇格によって、配列があらゆる範囲データ型を受け付けることを示します
anycompatiblemultirangeCommonさまざまな引数の共通データ型への自動昇格によって、配列があらゆる多重範囲データ型を受け付けることを示します

Polymorphic arguments and results are tied to each other and are resolved to specific data types when a query calling a polymorphic function is parsed. When there is more than one polymorphic argument, the actual data types of the input values must match up as described below. If the function's result type is polymorphic, or it has output parameters of polymorphic types, the types of those results are deduced from the actual types of the polymorphic inputs as described below. 多様の引数と結果は互いに結びつけられていて、多様関数を呼ぶ問い合わせが解析されるときに特定のデータ型に決定されます。 2つ以上の多様引数がある時には、入力値の実データ型は後述するように合わせなければなりません。 関数の結果型が多様、あるいは、多様型の出力パラメータを持つ場合には、それらの結果の型は後述するように多様入力の実際の型から導出されます。

For the <quote>simple</quote> family of polymorphic types, the matching and deduction rules work like this: 多様型のsimple族では、一致と導出の規則は以下のように動作します。

Each position (either argument or return value) declared as <type>anyelement</type> is allowed to have any specific actual data type, but in any given call they must all be the <emphasis>same</emphasis> actual type. Each position declared as <type>anyarray</type> can have any array data type, but similarly they must all be the same type. And similarly, positions declared as <type>anyrange</type> must all be the same range type. Likewise for <type>anymultirange</type>. anyelementとして宣言された位置(引数もしくは戻り値)にはそれぞれ、任意の実データ型を指定できますが、1つの呼び出しでは、これらはすべて同一の実データ型でなければなりません。 anyarrayとして宣言された位置にはそれぞれ、任意の配列データ型を持つことができますが、同様にこれらはすべて同じデータ型でなければなりません。 anyrangeと宣言された位置にはそれぞれ、同様にすべて同じ範囲型でなければなりません。 anymultirangeについても同様です。

Furthermore, if there are positions declared <type>anyarray</type> and others declared <type>anyelement</type>, the actual array type in the <type>anyarray</type> positions must be an array whose elements are the same type appearing in the <type>anyelement</type> positions. <type>anynonarray</type> is treated exactly the same as <type>anyelement</type>, but adds the additional constraint that the actual type must not be an array type. <type>anyenum</type> is treated exactly the same as <type>anyelement</type>, but adds the additional constraint that the actual type must be an enum type. さらに、anyarrayと宣言された位置とanyelementと宣言された位置の両方がある場合、anyarrayの位置の実際の配列型は、その要素の型がanyelement位置に現れる型と同じでなければなりません。 anynonarrayは、実際の型が配列型であってはならないという制限が加わっている点を除き、anyelementとまったく同様に扱われます。 anyenumは、実際の型が列挙型でなければならないという制約が加わっている点を除き、anyelementとまったく同様に扱われます。

Similarly, if there are positions declared <type>anyrange</type> and others declared <type>anyelement</type> or <type>anyarray</type>, the actual range type in the <type>anyrange</type> positions must be a range whose subtype is the same type appearing in the <type>anyelement</type> positions and the same as the element type of the <type>anyarray</type> positions. If there are positions declared <type>anymultirange</type>, their actual multirange type must contain ranges matching parameters declared <type>anyrange</type> and base elements matching parameters declared <type>anyelement</type> and <type>anyarray</type>. 同様にanyrangeと宣言された位置とanyelementもしくはanyarrayと宣言された位置の両方がある場合、anyrangeの位置の実際の範囲型は、その範囲の派生元型がanyelement位置に現れる型と同じであり、anyarray位置の要素の型と同じでなければなりません。 anymultirangeと宣言された位置がある場合、実際の多重範囲型には、宣言された位置とanyrangeが一致する範囲と、宣言された位置anyelementおよびanyarrayと一致する基本要素が含まれている必要があります。

Thus, when more than one argument position is declared with a polymorphic type, the net effect is that only certain combinations of actual argument types are allowed. For example, a function declared as <literal>equal(anyelement, anyelement)</literal> will take any two input values, so long as they are of the same data type. このように、2つ以上の引数位置が多様型と宣言されると、全体の効果として、実引数型の特定の組み合わせのみが許されるようになります。 例えば、equal(anyelement, anyelement)と宣言された関数は、2つの引数が同じデータ型である限り、任意の入力値を2つ取ることになります。

When the return value of a function is declared as a polymorphic type, there must be at least one argument position that is also polymorphic, and the actual data type(s) supplied for the polymorphic arguments determine the actual result type for that call. For example, if there were not already an array subscripting mechanism, one could define a function that implements subscripting as <literal>subscript(anyarray, integer) returns anyelement</literal>. This declaration constrains the actual first argument to be an array type, and allows the parser to infer the correct result type from the actual first argument's type. Another example is that a function declared as <literal>f(anyarray) returns anyenum</literal> will only accept arrays of enum types. 関数の戻り値を多様型として宣言する時、少なくとも1つの引数位置も多様でなければなりません。 そして多様の引数として与えられる実データ型がその呼び出しの実結果型を決定します。 例えば、配列添字機構がなかったとすると、subscript(anyarray, integer) returns anyelementとして添字機構を実装する関数を定義できます。 この宣言で最初の実引数は配列型に制約され、パーサはこの最初の実引数の型から正しい結果型を推論できます。 他にも例えば、f(anyarray) returns anyenumと宣言された関数は列挙型の配列のみを受け付けます。

In most cases, the parser can infer the actual data type for a polymorphic result type from arguments that are of a different polymorphic type in the same family; for example <type>anyarray</type> can be deduced from <type>anyelement</type> or vice versa. An exception is that a polymorphic result of type <type>anyrange</type> requires an argument of type <type>anyrange</type>; it cannot be deduced from <type>anyarray</type> or <type>anyelement</type> arguments. This is because there could be multiple range types with the same subtype. ほとんどの場合、パーサは同じ族の異なる多様型の引数から多様結果型の実データ型を推論できます。 例えば、anyarrayanyelementから、もしくはその逆から推定できます。 例外はanyrange型の多様結果はanyrange型の引数を必要とします。 anyarrayもしくはanyelementの引数からは推定できません。 これは、同じ派生元型の複数の範囲型が存在する可能性があるためです。

Note that <type>anynonarray</type> and <type>anyenum</type> do not represent separate type variables; they are the same type as <type>anyelement</type>, just with an additional constraint. For example, declaring a function as <literal>f(anyelement, anyenum)</literal> is equivalent to declaring it as <literal>f(anyenum, anyenum)</literal>: both actual arguments have to be the same enum type. anynonarray型とanyenum型が、別個の型変数を表していないことに注意してください。 これはanyelementと同じ型で、追加の制約が付いているだけです。 例えば、f(anyelement, anyenum)として関数を宣言することは、f(anyenum, anyenum)と宣言することと同一です。 両方の実引数は同じ列挙型でなければなりません。

For the <quote>common</quote> family of polymorphic types, the matching and deduction rules work approximately the same as for the <quote>simple</quote> family, with one major difference: the actual types of the arguments need not be identical, so long as they can be implicitly cast to a single common type. The common type is selected following the same rules as for <literal>UNION</literal> and related constructs (see <xref linkend="typeconv-union-case"/>). Selection of the common type considers the actual types of <type>anycompatible</type> and <type>anycompatiblenonarray</type> inputs, the array element types of <type>anycompatiblearray</type> inputs, the range subtypes of <type>anycompatiblerange</type> inputs, and the multirange subtypes of <type>anycompatiblemultirange</type> inputs. If <type>anycompatiblenonarray</type> is present then the common type is required to be a non-array type. Once a common type is identified, arguments in <type>anycompatible</type> and <type>anycompatiblenonarray</type> positions are automatically cast to that type, and arguments in <type>anycompatiblearray</type> positions are automatically cast to the array type for that type. 多様型のcommon族については、一致と導出の規則は概ねsimpleと同じに動作しますが、一つの大きな違いがあります。 一つの共通型へ暗黙にキャスト可能である限り、引数の実際の型が同一である必要がありません。 共通型はUNIONや関連する構文と同じ規則(10.5を参照)に従って選択されます。 共通型の選択では、anycompatibleanycompatiblenonarrayの入力の実際の型、anycompatiblearray入力の配列要素型、anycompatiblerange入力の範囲型とanycompatiblemultirange入力の多重範囲型、anycompatiblerange入力の範囲の派生元型が、考慮されます。 anycompatiblenonarrayが存在する場合、共通型は配列でない型である必要があります。 共通データ型が特定されたなら、anycompatibleおよびanycompatiblenonarrayの位置の引数は自動的にその型にキャストされ、anycompatiblearray位置の引数は自動的にその型に対する配列にキャストされます。

Since there is no way to select a range type knowing only its subtype, use of <type>anycompatiblerange</type> and/or <type>anycompatiblemultirange</type> requires that all arguments declared with that type have the same actual range and/or multirange type, and that that type's subtype agree with the selected common type, so that no casting of the range values is required. As with <type>anyrange</type> and <type>anymultirange</type>, use of <type>anycompatiblerange</type> and <type>anymultirange</type> as a function result type requires that there be an <type>anycompatiblerange</type> or <type>anycompatiblemultirange</type> argument. その派生元型だけ分かっている範囲型を選択する方法がないため、anycompatiblerangeおよび/またはanycompatiblemultirangeの使用には、その型で宣言されているすべての引数が同じ実際の範囲型および/または多重範囲型を持ち、また、その型の派生元型は選択された共通型に即したものである必要があり、そのため、範囲値のキャストは必要ありません。 anyrangeおよびanymultirangeと同様に、anycompatiblerangeおよびanymultirangeを関数の結果型として使用するには、anycompatiblerangeまたはanycompatiblemultirangeの引数がある必要があります。

Notice that there is no <type>anycompatibleenum</type> type. Such a type would not be very useful, since there normally are not any implicit casts to enum types, meaning that there would be no way to resolve a common type for dissimilar enum inputs. anycompatibleenum型は無いことに注意してください。 通常、列挙型への暗黙キャストはありません。これは異なる列挙入力に対する共通型を決める方法が無いことを意味します。そのため、このような型はあまり有益ではないでしょう。

The <quote>simple</quote> and <quote>common</quote> polymorphic families represent two independent sets of type variables. Consider for example simplecommonの多様族は型変数の二つの独立したセットに相当します。 例えば以下を考えてください。

CREATE FUNCTION myfunc(a anyelement, b anyelement,
                       c anycompatible, d anycompatible)
RETURNS anycompatible AS ...

In an actual call of this function, the first two inputs must have exactly the same type. The last two inputs must be promotable to a common type, but this type need not have anything to do with the type of the first two inputs. The result will have the common type of the last two inputs. この関数の実際の呼び出しでは、最初の2つの入力は正確に同じ型を持たなければなりません。 最後の2つの入力は共通型に昇格できなければなりませんが、この型が最初の2つの入力型と何らか関係がある必要はありません。 結果は最後の2つの入力の共通型を持ちます。

A variadic function (one taking a variable number of arguments, as in <xref linkend="xfunc-sql-variadic-functions"/>) can be polymorphic: this is accomplished by declaring its last parameter as <literal>VARIADIC</literal> <type>anyarray</type> or <literal>VARIADIC</literal> <type>anycompatiblearray</type>. For purposes of argument matching and determining the actual result type, such a function behaves the same as if you had written the appropriate number of <type>anynonarray</type> or <type>anycompatiblenonarray</type> parameters. 可変長引数の関数(38.5.6で説明する可変個の引数を取る関数)を多様とすることができます。 最後のパラメータをVARIADIC anyarrayまたはVARIADIC anycompatiblearrayと宣言することで実現されます。 引数を一致させ、実際の結果型を決めるために、こうした関数はanynonarrayまたはanycompatiblenonarrayパラメータをあたかも適切な個数記述した場合と同様に動作します