4 CREATE OPERATOR — define a new operator
9 {FUNCTION|PROCEDURE} = function_name
10 [, LEFTARG = left_type ] [, RIGHTARG = right_type ]
11 [, COMMUTATOR = com_op ] [, NEGATOR = neg_op ]
12 [, RESTRICT = res_proc ] [, JOIN = join_proc ]
13 [, HASHES ] [, MERGES ]
18 CREATE OPERATOR defines a new operator, name. The user who defines an
19 operator becomes its owner. If a schema name is given then the operator
20 is created in the specified schema. Otherwise it is created in the
23 The operator name is a sequence of up to NAMEDATALEN-1 (63 by default)
24 characters from the following list:
26 + - * / < > = ~ ! @ # % ^ & | ` ?
28 There are a few restrictions on your choice of name:
29 * -- and /* cannot appear anywhere in an operator name, since they
30 will be taken as the start of a comment.
31 * A multicharacter operator name cannot end in + or -, unless the
32 name also contains at least one of these characters:
34 For example, @- is an allowed operator name, but *- is not. This
35 restriction allows PostgreSQL to parse SQL-compliant commands
36 without requiring spaces between tokens.
37 * The symbol => is reserved by the SQL grammar, so it cannot be used
40 The operator != is mapped to <> on input, so these two names are always
43 For binary operators, both LEFTARG and RIGHTARG must be defined. For
44 prefix operators only RIGHTARG should be defined. The function_name
45 function must have been previously defined using CREATE FUNCTION and
46 must be defined to accept the correct number of arguments (either one
47 or two) of the indicated types.
49 In the syntax of CREATE OPERATOR, the keywords FUNCTION and PROCEDURE
50 are equivalent, but the referenced function must in any case be a
51 function, not a procedure. The use of the keyword PROCEDURE here is
52 historical and deprecated.
54 The other clauses specify optional operator optimization attributes.
55 Their meaning is detailed in Section 36.15.
57 To be able to create an operator, you must have USAGE privilege on the
58 argument types and the return type, as well as EXECUTE privilege on the
59 underlying function. If a commutator or negator operator is specified,
60 you must own those operators.
65 The name of the operator to be defined. See above for allowable
66 characters. The name can be schema-qualified, for example CREATE
67 OPERATOR myschema.+ (...). If not, then the operator is created
68 in the current schema. Two operators in the same schema can have
69 the same name if they operate on different data types. This is
73 The function used to implement this operator.
76 The data type of the operator's left operand, if any. This
77 option would be omitted for a prefix operator.
80 The data type of the operator's right operand.
83 The commutator of this operator.
86 The negator of this operator.
89 The restriction selectivity estimator function for this
93 The join selectivity estimator function for this operator.
96 Indicates this operator can support a hash join.
99 Indicates this operator can support a merge join.
101 To give a schema-qualified operator name in com_op or the other
102 optional arguments, use the OPERATOR() syntax, for example:
103 COMMUTATOR = OPERATOR(myschema.===) ,
107 Refer to Section 36.14 and Section 36.15 for further information.
109 When you are defining a self-commutative operator, you just do it. When
110 you are defining a pair of commutative operators, things are a little
111 trickier: how can the first one to be defined refer to the other one,
112 which you haven't defined yet? There are three solutions to this
114 * One way is to omit the COMMUTATOR clause in the first operator that
115 you define, and then provide one in the second operator's
116 definition. Since PostgreSQL knows that commutative operators come
117 in pairs, when it sees the second definition it will automatically
118 go back and fill in the missing COMMUTATOR clause in the first
120 * Another, more straightforward way is just to include COMMUTATOR
121 clauses in both definitions. When PostgreSQL processes the first
122 definition and realizes that COMMUTATOR refers to a nonexistent
123 operator, the system will make a dummy entry for that operator in
124 the system catalog. This dummy entry will have valid data only for
125 the operator name, left and right operand types, and owner, since
126 that's all that PostgreSQL can deduce at this point. The first
127 operator's catalog entry will link to this dummy entry. Later, when
128 you define the second operator, the system updates the dummy entry
129 with the additional information from the second definition. If you
130 try to use the dummy operator before it's been filled in, you'll
131 just get an error message.
132 * Alternatively, both operators can be defined without COMMUTATOR
133 clauses and then ALTER OPERATOR can be used to set their commutator
134 links. It's sufficient to ALTER either one of the pair.
136 In all three cases, you must own both operators in order to mark them
139 Pairs of negator operators can be defined using the same methods as for
142 It is not possible to specify an operator's lexical precedence in
143 CREATE OPERATOR, because the parser's precedence behavior is
144 hard-wired. See Section 4.1.6 for precedence details.
146 The obsolete options SORT1, SORT2, LTCMP, and GTCMP were formerly used
147 to specify the names of sort operators associated with a merge-joinable
148 operator. This is no longer necessary, since information about
149 associated operators is found by looking at B-tree operator families
150 instead. If one of these options is given, it is ignored except for
151 implicitly setting MERGES true.
153 Use DROP OPERATOR to delete user-defined operators from a database. Use
154 ALTER OPERATOR to modify operators in a database.
158 The following command defines a new operator, area-equality, for the
160 CREATE OPERATOR === (
163 FUNCTION = area_equal_function,
166 RESTRICT = area_restriction_function,
167 JOIN = area_join_function,
173 CREATE OPERATOR is a PostgreSQL extension. There are no provisions for
174 user-defined operators in the SQL standard.
178 ALTER OPERATOR, CREATE OPERATOR CLASS, DROP OPERATOR