CREATE OPERATOR 中文man頁面
NAME
CREATE OPERATOR - 定義一個(gè)新的操作符
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 定義一個(gè)新的操作符, name。 定義該操作符的用戶成為其所有者。如果給出了一個(gè)模式名,那么該操作符將在指定的模式中創(chuàng)建。 否則它會(huì)在當(dāng)前模式中創(chuàng)建。
操作符 name 是一個(gè)最多NAMEDATALEN-1 長的(缺省為 63 個(gè))下列字符組成的字串:
+ - * / < > = ~ ! @ # % ^ & | ` ?
你選擇名字的時(shí)候有幾個(gè)限制:
- *
- -- 和 /* 不能在操作符名字的任何地方出現(xiàn), 因?yàn)樗鼈儠?huì)被認(rèn)為是一個(gè)注釋的開始。
- *
一個(gè)多字符的操作符名字不能以 + 或 - 結(jié)尾, 除非該名字還包含至少下面字符之一:~ ! @ # % ^ & | ` ?
例如, @- 是一個(gè)允許的操作符名, 但 *- 不是。 這個(gè)限制允許 PostgreSQL 分析 SQL-有問題的查詢而不要求在符號(hào)之間有空白。
操作符 != 在輸入時(shí)映射成 <>, 因此這兩個(gè)名稱總是相等的。
至少需要定義一個(gè)LEFTARG或RIGHTARG。 對(duì)于雙目操作符來說,兩者都需要定義。 對(duì)右目操作符來說,只需要定義LEFTARG, 而對(duì)于左目操作符來說,只需要定義RIGHTARG。
同樣,funcname 過程必須已經(jīng)用 CREATE FUNCTION 定義過, 而且必須定義為接受正確數(shù)量的指定類型參數(shù)(一個(gè)或是兩個(gè))。
其它子句聲明可選的操作符優(yōu)化子句。他們的含義在 ``User-Defined Operators'' 里定義。
PARAMETERS 參數(shù)
- name
要定義的操作符名字??捎玫淖址娚衔摹?nbsp;其名字可以用模式修飾,比如 CREATE OPERATOR myschema.+ (...)。 如果沒有模式,則在當(dāng)前模式中創(chuàng)建操作符。同一個(gè)模式中的兩個(gè)操作符可以有一樣的名字,只要他們操作不同的數(shù)據(jù)類型。這叫做 重載。- funcname
用于實(shí)現(xiàn)該操作符的函數(shù)。- lefttype
如果存在的話,操作符左手邊的參數(shù)類型。 如果是左目操作符,這個(gè)參數(shù)可以省略。- righttype
如果存在的話,操作符右手邊的參數(shù)類型。 如果是右目操作符,這個(gè)參數(shù)可以省略。- com_op
該操作符對(duì)應(yīng)的交換(commutator)操作符。- neg_op
對(duì)應(yīng)的負(fù)操作符。- res_proc
此操作符約束選擇性計(jì)算函數(shù)。- join_proc
此操作符連接選擇性計(jì)算函數(shù)。- HASHES
表明此操作符支持哈希(散列)連接。- MERGES
表明此操作符可以支持一個(gè)融合連接。- left_sort_op
如果此操作符支持融合連接(join),此操作符的左手邊數(shù)據(jù)的排序操作符。- right_sort_op
如果此操作符支持融合連接(join),此操作符的右手邊數(shù)據(jù)的排序操作符。- less_than_op
如果這個(gè)操作符可以支持融合連接,那么這就是比較這個(gè)操作符的輸入數(shù)據(jù)類型的小于操作符。- greater_than_op
如果這個(gè)操作符不支持融合連接,那么這就是比較輸入這個(gè)操作符的數(shù)據(jù)類型的大于操作符。
要在 com_op 或者其它可選參數(shù)里給出一個(gè)模式修飾的操作符名,使用 OPERATOR() 語法,比如
COMMUTATOR = OPERATOR(myschema.===) ,
NOTES 注意
請(qǐng)參閱 ``User-Defined Operators'' 中操作符章節(jié)獲取更多信息。
請(qǐng)使用 DROP OPERATOR 從數(shù)據(jù)庫中刪除用戶定義操作符。
EXAMPLES 例子
下面命令定義一個(gè)新操作符,面積相等,用于 box 數(shù)據(jù)類型。
CREATE OPERATOR === ( LEFTARG = box, RIGHTARG = box, PROCEDURE = area_equal_procedure, COMMUTATOR = ===, NEGATOR = !==, RESTRICT = area_restriction_procedure, JOIN = area_join_procedure, HASHES, SORT1 = <<<, SORT2 = <<< -- 因?yàn)榻o出了排序操作符,索引隱含地有 MERGES。 -- LTCMP 和 GTCMP 分別假設(shè)是 < 和 > );
#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 );