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 );