CREATE OPERATOR

Name

CREATE OPERATOR  --  Defines a new user operator 新しいユーザ演算子を定義します。

Synopsis

CREATE OPERATOR name (
        PROCEDURE  = func_name
     [, LEFTARG    = type1 ]
     [, RIGHTARG   = type2 ]
     [, COMMUTATOR = com_op ]
     [, NEGATOR    = neg_op ]
     [, RESTRICT   = res_proc ]
     [, JOIN       = join_proc ]
     [, HASHES ]
     [, SORT1      = left_sort_op ]
     [, SORT2      = right_sort_op ]
    )
  

入力

name

The operator to be defined. See below for allowable characters.

定義される演算子です。有効な文字については下記を参照して 下さい。

func_name

The function used to implement this operator.

この演算子を実装するために使用される関数です。

type1

The type for the left-hand side of the operator, if any. This option would be omitted for a right-unary operator.

もしあれば、演算子の左側の型です。このオプションは右単項演算子 では省略されます。

type2

The type for the right-hand side of the operator, if any. This option would be omitted for a left-unary operator.

もしあれば、演算子の右側の型です。このオプションは左単項演算子 では省略されます。

com_op

The commutator for this operator.

この演算子に対する交代演算子です。

neg_op

The negator of this operator.

この演算子に対する否定演算子です。

res_proc

The restriction selectivity estimator function for this operator.

この演算子に対する制限選択評価関数です。

join_proc

The join selectivity estimator function for this operator.

この演算子に対する結合選択評価関数です。

HASHES

Indicates this operator can support a hash-join algorithm.

この演算子がハッシュ結合アルゴリズムをサポートすることを 示します。

left_sort_op

Operator that sorts the left-hand data type of this operator.

この演算子の左側のデータ型で整列させる演算子です。

right_sort_op

Operator that sorts the right-hand data type of this operator.

この演算子の右側のデータ型で整列させる演算子です。

出力

CREATE

Message returned if the operator is successfully created.

演算子が正常に作成された場合に返るメッセージです。

説明

CREATE OPERATOR defines a new operator, name. The user who defines an operator becomes its owner.

CREATE OPERATOR は新しい演算子、 name を 定義します。 オペレータを定義したユーザはその所有者となります。

The operator name is a sequence of up to thirty two (32) characters in any combination from the following:

演算子 name は 以下の中からどんな組合せでも構わない 32 文字までの文字列です。

+ - * / < > = ~ ! @ # % ^ & | ` ? $ : 
   

Note: No alphabetic characters are allowed in an operator name. This enables Postgres to parse SQL input into tokens without requiring spaces between each token.

演算子の名前にはアルファベット文字が使えません。 そうすることで、Postgres が SQL 入力をトークンにトークン間にスペースを挿入せず、 解析することを可能にします。

The operator "!=" is mapped to "<>" on input, so they are therefore equivalent.

演算子 "!=" は入力の際 "<>" に射影されますので、二つは 等価です。

At least one of LEFTARG and RIGHTARG must be defined. For binary operators, both should be defined. For right unary operators, only LEFTARG should be defined, while for left unary operators only RIGHTARG should be defined.

少なくとも LEFTARG と RIGHTARG のうち一つを定義しなければ なりません。二項演算子の場合、二つとも定義しなければ なりません。右単項演算子の場合、LEFTARG だけが定義され、一方 左単項演算子の場合は RIGHTARG だけが定義されなければなりません。

Also, the func_name procedure must have been previously defined using CREATE FUNCTION and must be defined to accept the correct number of arguments (either one or two).

同時に、func_name プロシージャは前もって CREATE FUNCTION により 定義され、且つ(一つまたは二つの)正しい数の引数を受け入れるように 定義されていなければなりません。

The commutator operator should be identified if one exists, so that Postgres can reverse the order of the operands if it wishes. For example, the operator area-less-than, <<<, would probably have a commutator operator, area-greater-than, >>>. Hence, the query optimizer could freely convert:

Postgres が、必要な場合 演算項目の順序を逆さまに辿れるようにするため、 交代演算子が存在する場合には、それに相違ないことを 確認する必要があります。 例えば、演算子 area-less-than, <<< は 交代演算子 area-greater-than, >>> を持っていて 問合せオプティマイザは自由に変換することが出来ます。

"0,0,1,1"::box  >>> MYBOXES.description
   
to を
MYBOXES.description <<< "0,0,1,1"::box
   
に変換します。

This allows the execution code to always use the latter representation and simplifies the query optimizer somewhat.

これにより、実行コードが常に後者の表現形式を使用することが 出来るようにし、問合せオプティマイザを何がしか単純化します。

Similarly, if there is a negator operator then it should be identified. Suppose that an operator, area-equal, ===, exists, as well as an area not equal, !==. The negator link allows the query optimizer to simplify

NOT MYBOXES.description === "0,0,1,1"::box
   
to
MYBOXES.description !== "0,0,1,1"::box
   

同様にして、否定演算子が存在するときはその確認がなされ なければなりません。 演算子 area-equal, ===, が存在し、area が !== に等しくない 場合を想定します。 否定演算子のリンクは問合せオプティマイザが

NOT MYBOXES.description === "0,0,1,1"::box
   
から
MYBOXES.description !== "0,0,1,1"::box
   
に単純化することを許可します。

If a commutator operator name is supplied, Postgres searches for it in the catalog. If it is found and it does not yet have a commutator itself, then the commutator's entry is updated to have the newly created operator as its commutator. This applies to the negator, as well.

交代演算子の名前が与えられている場合、 Postgres は交代演算子を カタログ内で検索します。 検出され、なお交代そのものを自身が所持していない場合 には、交代の項目が、その交代として、新たに作成された演算子 を所有するように更新されます。 このことは否定演算子にも同様に適用されます。

This is to allow the definition of two operators that are the commutators or the negators of each other. The first operator should be defined without a commutator or negator (as appropriate). When the second operator is defined, name the first as the commutator or negator. The first will be updated as a side effect. (As of Postgres 6.5, it also works to just have both operators refer to each other.)

このことにより、それぞれが交代演算子または否定演算子である 二つの演算子を定義することが可能となります。第一の演算子 は(適切な処理として)交代演算子または否定演算子なしで定義 される必要があります。第二の演算子が定義されるとき、第一 の演算子を交代演算子または否定演算子と指定します。 初めの演算子は副作用により更新されることになります。 (Postgres 6.5 時点で、それぞれを参照する二つの演算子を 持つことが有効となっています。)

The next three specifications are present to support the query optimizer in performing joins. Postgres can always evaluate a join (i.e., processing a clause with two tuple variables separated by an operator that returns a boolean) by iterative substitution [WONG76]. In addition, Postgres can use a hash-join algorithm along the lines of [SHAP86]; however, it must know whether this strategy is applicable. The current hash-join algorithm is only correct for operators that represent equality tests; furthermore, equality of the datatype must mean bitwise equality of the representation of the type. (For example, a datatype that contains unused bits that don't matter for equality tests could not be hashjoined.) The HASHES flag indicates to the query optimizer that a hash join may safely be used with this operator.

問合せオプティマイザが結合を実行するのをサポートするため、 以下の三つの仕様が提供されています。 Postgres は常に反復した変換 [WONG76] (つまり、ブール値を返す演算子によって分離された二つのタプル 変数を持つ句を処理すること)により結合を評価します。 このほかに、Postgres はハッシュ・結合 [SHAP86] にしたがったアルゴリズムを使用することができます。 といっても、この戦略が適用できるかどうかを知る必要があります。 現在のハッシュ・結合アルゴリズムは等価性の評価を示す演算子のみ にたいして有効です。さらに、データ型の等価性は、型の表現形式 がビット単位で整合していることを意味していなければなりません。 (例えば、等価性の評価に関係のない使われないビットを含んでいる データ型はハッシュ・結合されません。) HASHES のフラグは問合せオプティマイザにたいし、ハッシュ結合 がこの演算子で安全に使用できることを示します。

Similarly, the two sort operators indicate to the query optimizer whether merge-sort is a usable join strategy and which operators should be used to sort the two operand classes. Sort operators should only be provided for an equality operator, and they should refer to less-than operators for the left and right side data types respectively.

同様にして、二つの整列演算子は問合せオプティマイザにたいし、 マージ・整列が結合戦略で有効かどうか、そしてどちらの演算子が 二つの演算項目クラスにたいして使用されるのかを示します。 整列演算子は等価演算子にたいしてのみ提供され、それぞれが右側と 左側のデータ型に対する less-than 演算子を参照しなければなり ません。

If other join strategies are found to be practical, Postgres will change the optimizer and run-time system to use them and will require additional specification when an operator is defined. Fortunately, the research community invents new join strategies infrequently, and the added generality of user-defined join strategies was not felt to be worth the complexity involved.

もし、他の結合戦略が実用的であった場合、 Postgres はその戦略を使用する ようにオプティマイザと実行時システムを変更し、ある演算子 が定義されたとき追加の仕様を求めることになります。 幸運なことに、研究団体がときたま新しい結合戦略を発明する ことがあって、付加されたユーザ定義の結合戦略の一般性は、 内包する複雑性にかかわらず、その価値が認められています。

The last two pieces of the specification are present so the query optimizer can estimate result sizes. If a clause of the form:

MYBOXES.description <<< "0,0,1,1"::box
   
is present in the qualification, then Postgres may have to estimate the fraction of the instances in MYBOXES that satisfy the clause. The function res_proc must be a registered function (meaning it is already defined using CREATE FUNCTION) which accepts arguments of the correct data types and returns a floating point number. The query optimizer simply calls this function, passing the parameter "0,0,1,1" and multiplies the result by the relation size to get the desired expected number of instances.

最後の二つの仕様によって問合せオプティマイザは結果の大きさを 推測できます。 もし、形状の句

MYBOXES.description <<< "0,0,1,1"::box
   
が条件の中にあれば、Postgres は この句を満足する MYBOXES にあるインスタンスの割合を推測 します。関数 res_proc は登録された(前もって CREATE FUNCTION に より定義された)関数で、正しいデータ型の引数を受けつけ、 そして浮動小数点の数値を返すものです。問合せオプティマイザは、 望ましい、そして期待されるインスタンスの数を取得するために、 単にこの関数を呼び出し、パラメータ "0,0,1,1" を引き渡して、 結果をリレーションの大きさで掛け算します。

Similarly, when the operands of the operator both contain instance variables, the query optimizer must estimate the size of the resulting join. The function join_proc will return another floating point number which will be multiplied by the cardinalities of the two classes involved to compute the desired expected result size.

同様、演算子の演算項目が二つともインスタンス変数を所有して いる場合、問合せオプティマイザは結果となる結合の大きさを推測 しなければなりません。関数 join_proc は望ましい期待される結果 の大きさを計算するために関係する二つのクラスの基数によって乗算 された別の浮動小数点数値を返します。

The difference between the function

my_procedure_1 (MYBOXES.description, "0,0,1,1"::box)
   
and the operator
MYBOXES.description === "0,0,1,1"::box
   
is that Postgres attempts to optimize operators and can decide to use an index to restrict the search space when operators are involved. However, there is no attempt to optimize functions, and they are performed by brute force. Moreover, functions can have any number of arguments while operators are restricted to one or two.

関数

my_procedure_1 (MYBOXES.description, "0,0,1,1"::box)
   
とその演算子
MYBOXES.description === "0,0,1,1"::box
   
との相違は Postgres が 演算子を最適化しようとし、演算子がかかわっている場合 検索空間を制限するためにインデックスの使用を決定 できることです。とはいっても、関数を最適化することは せず、強制的に実行されます。更には、演算子は一つまたは 二つまでと引数の数が制限されているにかかわらず、関数は どんな数の引数でも取ることが出来ます。

注意事項

Refer to the chapter on operators in the PostgreSQL User's Guide for further information. Refer to DROP OPERATOR to delete user-defined operators from a database.

詳細情報については PostgreSQL ユーザガイド の演算子の章を参照して下さい。 ユーザ定義の演算子をデータベースから削除するには DROP OPERATOR を参照して下さい。

使用法

The following command defines a new operator, area-equality, for the BOX data type.

つぎのコマンドは BOX データ型に対する 新しい演算子 area-equality を定義します。

CREATE OPERATOR === (
   LEFTARG = box,
   RIGHTARG = box,
   PROCEDURE = area_equal_procedure,
   COMMUTATOR = ===,
   NEGATOR = !==,
   RESTRICT = area_restriction_procedure,
   JOIN = area_join_procedure,
   HASHES,
   SORT1 = <<<,
   SORT2 = <<<
);
  

互換性

CREATE OPERATOR is a Postgres extension.

CREATE OPERATOR は Postgres の拡張です。

SQL92

There is no CREATE OPERATOR statement in SQL92.

SQL92 に CREATE OPERATOR 文はありません。