CREATE OPERATOR 中文man頁面
NAME
CREATE OPERATOR - 定義一個新的操作符
SYNOPSIS
CREATE OPERATOR name ( PROCEDURE = funcname [, LEFTARG = lefttype ] [, RIGHTARG = righttype ] [, COMMUTATOR = com_op ] [, NEGATOR = neg_op ] [, RESTRICT = res_proc ] [, JOIN = join_proc ] [, HASHES ] [, MERGES ] [, SORT1 = left_sort_op ] [, SORT2 = right_sort_op ] [, LTCMP = less_than_op ] [, GTCMP = greater_than_op ] )
DESCRIPTION 描述
CREATE OPERATOR 定義一個新的操作符, name。 定義該操作符的用戶成為其所有者。如果給出了一個模式名,那么該操作符將在指定的模式中創建。 否則它會在當前模式中創建。
操作符 name 是一個最多NAMEDATALEN-1 長的(缺省為 63 個)下列字符組成的字串:
+ - * / < > = ~ ! @ # % ^ & | ` ?
你選擇名字的時候有幾個限制:
- *
- -- 和 /* 不能在操作符名字的任何地方出現, 因為它們會被認為是一個注釋的開始。
- *
一個多字符的操作符名字不能以 + 或 - 結尾, 除非該名字還包含至少下面字符之一:~ ! @ # % ^ & | ` ?
例如, @- 是一個允許的操作符名, 但 *- 不是。 這個限制允許 PostgreSQL 分析 SQL-有問題的查詢而不要求在符號之間有空白。
操作符 != 在輸入時映射成 <>, 因此這兩個名稱總是相等的。
至少需要定義一個LEFTARG或RIGHTARG。 對于雙目操作符來說,兩者都需要定義。 對右目操作符來說,只需要定義LEFTARG, 而對于左目操作符來說,只需要定義RIGHTARG。
同樣,funcname 過程必須已經用 CREATE FUNCTION 定義過, 而且必須定義為接受正確數量的指定類型參數(一個或是兩個)。
其它子句聲明可選的操作符優化子句。他們的含義在 ``User-Defined Operators'' 里定義。
PARAMETERS 參數
- name
要定義的操作符名字。可用的字符見上文。 其名字可以用模式修飾,比如 CREATE OPERATOR myschema.+ (...)。 如果沒有模式,則在當前模式中創建操作符。同一個模式中的兩個操作符可以有一樣的名字,只要他們操作不同的數據類型。這叫做 重載。- funcname
用于實現該操作符的函數。- lefttype
如果存在的話,操作符左手邊的參數類型。 如果是左目操作符,這個參數可以省略。- righttype
如果存在的話,操作符右手邊的參數類型。 如果是右目操作符,這個參數可以省略。- com_op
該操作符對應的交換(commutator)操作符。- neg_op
對應的負操作符。- res_proc
此操作符約束選擇性計算函數。- join_proc
此操作符連接選擇性計算函數。- HASHES
表明此操作符支持哈希(散列)連接。- MERGES
表明此操作符可以支持一個融合連接。- left_sort_op
如果此操作符支持融合連接(join),此操作符的左手邊數據的排序操作符。- right_sort_op
如果此操作符支持融合連接(join),此操作符的右手邊數據的排序操作符。- less_than_op
如果這個操作符可以支持融合連接,那么這就是比較這個操作符的輸入數據類型的小于操作符。- greater_than_op
如果這個操作符不支持融合連接,那么這就是比較輸入這個操作符的數據類型的大于操作符。
要在 com_op 或者其它可選參數里給出一個模式修飾的操作符名,使用 OPERATOR() 語法,比如
COMMUTATOR = OPERATOR(myschema.===) ,
NOTES 注意
請參閱 ``User-Defined Operators'' 中操作符章節獲取更多信息。
請使用 DROP OPERATOR 從數據庫中刪除用戶定義操作符。
EXAMPLES 例子
下面命令定義一個新操作符,面積相等,用于 box 數據類型。
CREATE OPERATOR === ( LEFTARG = box, RIGHTARG = box, PROCEDURE = area_equal_procedure, COMMUTATOR = ===, NEGATOR = !==, RESTRICT = area_restriction_procedure, JOIN = area_join_procedure, HASHES, SORT1 = <<<, SORT2 = <<< -- 因為給出了排序操作符,索引隱含地有 MERGES。 -- LTCMP 和 GTCMP 分別假設是 < 和 > );
#p#
NAME
CREATE OPERATOR - define a new operator
SYNOPSIS
CREATE OPERATOR name ( PROCEDURE = funcname [, LEFTARG = lefttype ] [, RIGHTARG = righttype ] [, COMMUTATOR = com_op ] [, NEGATOR = neg_op ] [, RESTRICT = res_proc ] [, JOIN = join_proc ] [, HASHES ] [, MERGES ] [, SORT1 = left_sort_op ] [, SORT2 = right_sort_op ] [, LTCMP = less_than_op ] [, GTCMP = greater_than_op ] )
DESCRIPTION
CREATE OPERATOR defines a new operator, name. The user who defines an operator becomes its owner. If a schema name is given then the operator is created in the specified schema. Otherwise it is created in the current schema.
The operator name is a sequence of up to NAMEDATALEN-1 (63 by default) characters from the following list:
+ - * / < > = ~ ! @ # % ^ & | ` ?
There are a few restrictions on your choice of name:
- *
- -- and /* cannot appear anywhere in an operator name, since they will be taken as the start of a comment.
- *
- A multicharacter operator name cannot end in + or -, unless the name also contains at least one of these characters:
~ ! @ # % ^ & | ` ?
For example, @- is an allowed operator name, but *- is not. This restriction allows PostgreSQL to parse SQL-compliant commands without requiring spaces between tokens.
The operator != is mapped to <> on input, so these two names are always equivalent.
At least one of LEFTARG and RIGHTARG must be defined. For binary operators, both must be defined. For right unary operators, only LEFTARG should be defined, while for left unary operators only RIGHTARG should be defined.
The funcname procedure must have been previously defined using CREATE FUNCTION and must be defined to accept the correct number of arguments (either one or two) of the indicated types.
The other clauses specify optional operator optimization clauses. Their meaning is detailed in the section called ``User-Defined Operators'' in the documentation.
PARAMETERS
- name
- The name of the operator to be defined. See above for allowable characters. The name may be schema-qualified, for example CREATE OPERATOR myschema.+ (...). If not, then the operator is created in the current schema. Two operators in the same schema can have the same name if they operate on different data types. This is called overloading.
- funcname
- The function used to implement this operator.
- lefttype
- The type of the left-hand argument of the operator, if any. This option would be omitted for a left-unary operator.
- righttype
- The type of the right-hand argument of the operator, if any. This option would be omitted for a right-unary operator.
- com_op
- The commutator of 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.
- MERGES
- Indicates this operator can support a merge join.
- left_sort_op
- If this operator can support a merge join, the less-than operator that sorts the left-hand data type of this operator.
- right_sort_op
- If this operator can support a merge join, the less-than operator that sorts the right-hand data type of this operator.
- less_than_op
- If this operator can support a merge join, the less-than operator that compares the input data types of this operator.
- greater_than_op
- If this operator can support a merge join, the greater-than operator that compares the input data types of this operator.
To give a schema-qualified operator name in com_op or the other optional arguments, use the OPERATOR() syntax, for example
COMMUTATOR = OPERATOR(myschema.===) ,
NOTES
Refer to the section called ``User-Defined Operators'' in the documentation for further information.
Use DROP OPERATOR to delete user-defined operators from a database.
EXAMPLES
The following command defines a new operator, area-equality, for the data type box:
CREATE OPERATOR === ( LEFTARG = box, RIGHTARG = box, PROCEDURE = area_equal_procedure, COMMUTATOR = ===, NEGATOR = !==, RESTRICT = area_restriction_procedure, JOIN = area_join_procedure, HASHES, SORT1 = <<<, SORT2 = <<< -- Since sort operators were given, MERGES is implied. -- LTCMP and GTCMP are assumed to be < and > respectively );