CREATE AGGREGATE name [ AS ] ( BASETYPE = data_type [ , SFUNC1 = sfunc1 , STYPE1 = sfunc1_return_type ] [ , SFUNC2 = sfunc2 , STYPE2 = sfunc2_return_type ] [ , FINALFUNC = ffunc ] [ , INITCOND1 = initial_condition1 ] [ , INITCOND2 = initial_condition2 ] )
The name of an aggregate function to create.
作成する集約関数の名前です。
The fundamental data type on which this aggregate function operates.
この集約関数が影響を及ぼす基準のデータ型です。
The state transition function to be called for every non-NULL field from the source column. It takes a variable of type sfunc1_return_type as the first argument and that field as the second argument.
元となるカラムからそれぞれの非 NULL フィールドにたいし 呼び出される状態推移関数です。 最初の引数として、 sfunc1_return_type 型 の変数を取り、そのフィールドを第二の引数として取ります。
The return type of the first transition function.
最初の推移関数の戻り型です。
The state transition function to be called for every non-NULL field from the source column. It takes a variable of type sfunc2_return_type as the only argument and returns a variable of the same type.
元となるカラムからそれぞれの非 NULL フィールドにたいし 呼び出される状態推移関数です。 たった一つの引数として、 sfunc1_return_type 型 の変数をとり、同じ型の変数を戻します。
The return type of the second transition function.
二番目の推移関数の戻り型です。
The final function called after traversing all input fields. This function must take two arguments of types sfunc1_return_type and sfunc2_return_type.
すべての入力フィールドが考察されたあとに呼ばれる最終の関数です。 この関数は sfunc1_return_type と sfunc2_return_type の二つの型の引数を取らなければなりません。
The initial value for the first transition function argument.
最初の推移関数の引数の初期値です。
The initial value for the second transition function argument.
二番目の推移関数の引数の初期値です。
CREATE AGGREGATE allows a user or programmer to extend Postgres functionality by defining new aggregate functions. Some aggregate functions for base types such as min(int4) and avg(float8) are already provided in the base distribution. If one defines new types or needs an aggregate function not already provided then CREATE AGGREGATE can be used to provide the desired features.
CREATE AGGREGATE は、ユーザまたはプログラマが 新規の集約関数を定義するこで Postgres の 機能を拡張できるようにします。 min(int4) や avg(float8) と いったいくつかの基本型用の集約関数は基本の配布物として提供されて います。新しい型を定義したい場合とか、用意されていない集約関数が 必要になった時には、CREATE AGGREGATE によって 欲しい機能を実現することができます。
An aggregate function can require up to three functions, two state transition functions, sfunc1 and sfunc2:
一つの集約関数には、最大で3つの関数を持つことができます。 内 2 つは状態推移関数である、次の sfunc1 と sfunc2 です。
sfunc1( internal-state1, next-data_item ) ---> next-internal-state1 sfunc2( internal-state2 ) ---> next-internal-state2and a final calculation function, ffunc: そして最後の 1 つは、演算関数
ffunc(internal-state1, internal-state2) ---> aggregate-valueとなります。
Postgres creates up to two temporary variables (referred to here as temp1 and temp2) to hold intermediate results used as arguments to the transition functions.
Postgres は (ここでは temp1 と temp2 になっている) 推移関数に対する引数として使用する中間結果を保持する最大 2 つの暫定 変数を生成します。
These transition functions are required to have the following properties:
これらの推移関数には以下の特性が必要です。
The arguments to sfunc1 must be temp1 of type sfunc1_return_type and column_value of type data_type. The return value must be of type sfunc1_return_type and will be used as the first argument in the next call to sfunc1.
sfunc1 の引数は 型 sfunc1_return_type の temp1 と 型 data_type の column_value で なくてはなりません。 戻り値は、型 sfunc1_return_type であって、次の sfunc1 呼び出しの 第一の引数でなければなりません。
The argument and return value of sfunc2 must be temp2 of type sfunc2_return_type.
sfunc2 の 引数と戻り値は、 型 sfunc2_return_type の temp2 でなくては なりません。
The arguments to the final-calculation-function must be temp1 and temp2 and its return value must be a Postgres base type (not necessarily data_type which had been specified for BASETYPE).
最終演算関数の引数は temp1 と temp2 であって、戻り値は Postgres の基準型でなければなりません。 (BASETYPE で指定される data_type である必要はありません。)
FINALFUNC should be specified if and only if both state-transition functions are specified.
2 つの状態推移関数が指定されたときのみ、 FINALFUNC が指定されなければなりません。
An aggregate function may also require one or two initial conditions, one for each transition function. These are specified and stored in the database as fields of type text.
また、集約関数はそれぞれの推移関数に対して 1 つづつ、全体として1 つ または 2 つの初期条件が必要なことがあります。これらはデータベースで型 text のフィールドとして指定され、記憶されます。
Use DROP AGGREGATE to drop aggregate functions.
集約関数を削除するのには、 DROP AGGREGATE を使用します。
It is possible to specify aggregate functions that have varying combinations of state and final functions. For example, the count aggregate requires SFUNC2 (an incrementing function) but not SFUNC1 or FINALFUNC, whereas the sum aggregate requires SFUNC1 (an addition function) but not SFUNC2 or FINALFUNC and the avg aggregate requires both of the above state functions as well as a FINALFUNC (a division function) to produce its answer. In any case, at least one state function must be defined, and any SFUNC2 must have a corresponding INITCOND2.
状態関数と最終関数の組合せが異なる集約関数を指定することが できます。 例えば、count 集約は SFUNC1 または FINALFUNC ではなく(増分関数である) SFUNC2 を必要とします。 一方、 sum 集約は SFUNC2 または FINALFUNC ではなく(加算関数である) SFUNC1 を必要とし、avg 集約は結果を引き出すのに、上記 2 つの関数と(除算関数である) FINALFUNC を必要とします。いずれの場合においても、最低 1 つの 状態関数を定義する必要があり、全ての SFUNC2 は、それに相応する INITCOND2 がなければなりません。
Refer to the chapter on aggregate functions in the PostgreSQL Programmer's Guide on aggregate functions for complete examples of usage.
集約関数に関しての完備した事例と使用法については PostgreSQL プログラマガイドの 集約関数の章を参照して下さい。