2 doc/src/sgml/ref/create_operator.sgml
3 PostgreSQL documentation
6 <refentry id="SQL-CREATEOPERATOR">
8 <refentrytitle>CREATE OPERATOR</refentrytitle>
9 <manvolnum>7</manvolnum>
10 <refmiscinfo>SQL - Language Statements</refmiscinfo>
14 <refname>CREATE OPERATOR</refname>
15 <refpurpose>define a new operator</refpurpose>
18 <indexterm zone="sql-createoperator">
19 <primary>CREATE OPERATOR</primary>
24 CREATE OPERATOR <replaceable>name</replaceable> (
25 PROCEDURE = <replaceable class="parameter">function_name</replaceable>
26 [, LEFTARG = <replaceable class="parameter">left_type</replaceable> ] [, RIGHTARG = <replaceable class="parameter">right_type</replaceable> ]
27 [, COMMUTATOR = <replaceable class="parameter">com_op</replaceable> ] [, NEGATOR = <replaceable class="parameter">neg_op</replaceable> ]
28 [, RESTRICT = <replaceable class="parameter">res_proc</replaceable> ] [, JOIN = <replaceable class="parameter">join_proc</replaceable> ]
29 [, HASHES ] [, MERGES ]
35 <title>Description</title>
38 <command>CREATE OPERATOR</command> defines a new operator,
39 <replaceable class="parameter">name</replaceable>. The user who
40 defines an operator becomes its owner. If a schema name is given
41 then the operator is created in the specified schema. Otherwise it
42 is created in the current schema.
46 The operator name is a sequence of up to <symbol>NAMEDATALEN</>-1
47 (63 by default) characters from the following list:
49 + - * / < > = ~ ! @ # % ^ & | ` ?
52 There are a few restrictions on your choice of name:
55 <para><literal>--</literal> and <literal>/*</literal> cannot appear anywhere in an operator name,
56 since they will be taken as the start of a comment.
61 A multicharacter operator name cannot end in <literal>+</literal> or
63 unless the name also contains at least one of these characters:
65 ~ ! @ # % ^ & | ` ?
67 For example, <literal>@-</literal> is an allowed operator name,
68 but <literal>*-</literal> is not.
69 This restriction allows <productname>PostgreSQL</productname> to
70 parse SQL-compliant commands without requiring spaces between tokens.
75 The use of <literal>=></> as an operator name is deprecated. It may
76 be disallowed altogether in a future release.
83 The operator <literal>!=</literal> is mapped to
84 <literal><></literal> on input, so these two names are always
89 At least one of <literal>LEFTARG</> and <literal>RIGHTARG</> must be defined. For
90 binary operators, both must be defined. For right unary
91 operators, only <literal>LEFTARG</> should be defined, while for left
92 unary operators only <literal>RIGHTARG</> should be defined.
96 The <replaceable class="parameter">function_name</replaceable>
97 procedure must have been previously defined using <command>CREATE
98 FUNCTION</command> and must be defined to accept the correct number
99 of arguments (either one or two) of the indicated types.
103 The other clauses specify optional operator optimization clauses.
104 Their meaning is detailed in <xref linkend="xoper-optimization">.
109 <title>Parameters</title>
113 <term><replaceable class="parameter">name</replaceable></term>
116 The name of the operator to be defined. See above for allowable
117 characters. The name can be schema-qualified, for example
118 <literal>CREATE OPERATOR myschema.+ (...)</>. If not, then
119 the operator is created in the current schema. Two operators
120 in the same schema can have the same name if they operate on
121 different data types. This is called
122 <firstterm>overloading</>.
128 <term><replaceable class="parameter">function_name</replaceable></term>
131 The function used to implement this operator.
137 <term><replaceable class="parameter">left_type</replaceable></term>
140 The data type of the operator's left operand, if any.
141 This option would be omitted for a left-unary operator.
147 <term><replaceable class="parameter">right_type</replaceable></term>
150 The data type of the operator's right operand, if any.
151 This option would be omitted for a right-unary operator.
157 <term><replaceable class="parameter">com_op</replaceable></term>
160 The commutator of this operator.
166 <term><replaceable class="parameter">neg_op</replaceable></term>
169 The negator of this operator.
175 <term><replaceable class="parameter">res_proc</replaceable></term>
178 The restriction selectivity estimator function for this operator.
184 <term><replaceable class="parameter">join_proc</replaceable></term>
187 The join selectivity estimator function for this operator.
193 <term><literal>HASHES</literal></term>
196 Indicates this operator can support a hash join.
202 <term><literal>MERGES</literal></term>
205 Indicates this operator can support a merge join.
212 To give a schema-qualified operator name in <replaceable
213 class="parameter">com_op</replaceable> or the other optional
214 arguments, use the <literal>OPERATOR()</> syntax, for example:
216 COMMUTATOR = OPERATOR(myschema.===) ,
217 </programlisting></para>
224 Refer to <xref linkend="xoper"> for further information.
228 It is not possible to specify an operator's lexical precedence in
229 <command>CREATE OPERATOR</>, because the parser's precedence behavior
230 is hard-wired. See <xref linkend="sql-precedence"> for precedence details.
234 The obsolete options <literal>SORT1</>, <literal>SORT2</>,
235 <literal>LTCMP</>, and <literal>GTCMP</> were formerly used to
236 specify the names of sort operators associated with a merge-joinable
237 operator. This is no longer necessary, since information about
238 associated operators is found by looking at B-tree operator families
239 instead. If one of these options is given, it is ignored except
240 for implicitly setting <literal>MERGES</> true.
244 Use <xref linkend="sql-dropoperator"> to delete user-defined operators
245 from a database. Use <xref linkend="sql-alteroperator"> to modify operators in a
251 <title>Examples</title>
254 The following command defines a new operator, area-equality, for
255 the data type <type>box</type>:
257 CREATE OPERATOR === (
260 PROCEDURE = area_equal_procedure,
263 RESTRICT = area_restriction_procedure,
264 JOIN = area_join_procedure,
267 </programlisting></para>
271 <title>Compatibility</title>
274 <command>CREATE OPERATOR</command> is a
275 <productname>PostgreSQL</productname> extension. There are no
276 provisions for user-defined operators in the SQL standard.
281 <title>See Also</title>
283 <simplelist type="inline">
284 <member><xref linkend="sql-alteroperator"></member>
285 <member><xref linkend="sql-createopclass"></member>
286 <member><xref linkend="sql-dropoperator"></member>