CREATE OPERATOR
Defines 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] )
Description
CREATE OPERATOR defines a new operator.
The user who defines an operator becomes its owner.
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 Greengage DB to parse SQL-compliant commands without requiring spaces between tokens.
The use of => as an operator name is deprecated.
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, must be IMMUTABLE, 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. These clauses should be provided whenever appropriate to speed up queries that use the operator. But if you provide them, you must be sure that they are correct. Incorrect use of an optimization clause can result in server process crashes, subtly wrong output, or other unexpected results. You can always leave out an optimization clause if you are not sure about it.
To be able to create an operator, you must have the USAGE privilege on the argument types and the return type, as well as the EXECUTE privilege on the underlying function.
If a commutator or negator operator is specified, you must own these operators.
Parameters
| Parameter | Description |
|---|---|
name |
The name (optionally schema-qualified) of the operator to be defined. Two operators in the same schema can have the same name if they operate on different data types |
funcname |
The function used to implement this operator (must be an |
lefttype |
The data type of the operator’s left operand, if any. This option would be omitted for a left-unary operator |
righttype |
The data type of the operator’s right operand, if any. This option would be omitted for a right-unary operator |
com_op |
The optional |
neg_op |
The optional |
res_proc |
The optional
This assists the optimizer by giving it some idea of how many rows will be eliminated by
|
join_proc |
The optional
This helps the optimizer by letting it figure out which of several possible join sequences is likely to take the least work. You can usually just use one of the following system standard join selectivity estimator functions for many of your own operators:
|
HASHES |
The optional In most cases, it is only practical to support hashing for operators that take the same data type on both sides. However, you can design compatible hash functions for two or more data types, which are functions that will generate the same hash codes for "equal" values, even if the values are differently represented. To be marked A hash-joinable operator must have a commutator (itself, if the two operand data types are the same, or a related equality operator if they are different) that appears in the same operator family. Otherwise, planner errors can occur when the operator is used. For better optimization, a hash operator family that supports multiple data types should provide equality operators for every combination of the data types. NOTE
The function underlying a hash-joinable operator must be marked immutable or stable.
An operator marked as volatile will not be used.
If a hash-joinable operator has an underlying function that is marked strict, the function must also be complete, returning |
MERGES |
The Merge join is based on the idea of sorting the left- and right-hand tables into order and then scanning them in parallel.
This means both data types must be capable of being fully ordered, and the join operator must be one that can only succeed for pairs of values that fall at equivalent places in the sort order.
In practice, this means that the join operator must behave like an equality operator.
However, you can merge-join two distinct data types so long as they are logically compatible.
For example, the To be marked NOTE
|
Notes
Any functions used to implement the operator must be defined as IMMUTABLE.
It is not possible to specify an operator’s lexical precedence in CREATE OPERATOR, because the parser’s precedence behavior is hard-wired.
See Operator Precedence in the PostgreSQL documentation for precedence details.
Use DROP OPERATOR to delete user-defined operators from a database. Use ALTER OPERATOR to modify operators in a database.
Examples
Here is an example of creating an operator for adding two complex numbers, assuming we have already created the definition of type complex.
First define the function that does the work, then define the operator:
CREATE FUNCTION complex_add(complex, complex)
RETURNS complex
AS
'filename',
'complex_add'
LANGUAGE C IMMUTABLE
STRICT;
CREATE OPERATOR + (
leftarg = complex,
rightarg = complex,
procedure = complex_add,
commutator = +
);
To use this operator in a query, run:
SELECT (a + b) AS c FROM test_complex;
Compatibility
CREATE OPERATOR is a Greengage DB language extension.
The SQL standard does not provide for user-defined operators.