2 $Header: /cvsroot/pgsql/doc/src/sgml/ref/create_operator.sgml,v 1.36 2003/08/17 22:09:00 tgl Exp $
3 PostgreSQL documentation
6 <refentry id="SQL-CREATEOPERATOR">
8 <refentrytitle id="sql-createoperator-title">CREATE OPERATOR</refentrytitle>
9 <refmiscinfo>SQL - Language Statements</refmiscinfo>
13 <refname>CREATE OPERATOR</refname>
14 <refpurpose>define a new operator</refpurpose>
19 CREATE OPERATOR <replaceable>name</replaceable> (
20 PROCEDURE = <replaceable class="parameter">func_name</replaceable>
21 [, LEFTARG = <replaceable class="parameter">lefttype</replaceable> ] [, RIGHTARG = <replaceable class="parameter">righttype</replaceable> ]
22 [, COMMUTATOR = <replaceable class="parameter">com_op</replaceable> ] [, NEGATOR = <replaceable class="parameter">neg_op</replaceable> ]
23 [, RESTRICT = <replaceable class="parameter">res_proc</replaceable> ] [, JOIN = <replaceable class="parameter">join_proc</replaceable> ]
24 [, HASHES ] [, MERGES ]
25 [, SORT1 = <replaceable class="parameter">left_sort_op</replaceable> ] [, SORT2 = <replaceable class="parameter">right_sort_op</replaceable> ]
26 [, LTCMP = <replaceable class="parameter">less_than_op</replaceable> ] [, GTCMP = <replaceable class="parameter">greater_than_op</replaceable> ]
32 <title>Description</title>
35 <command>CREATE OPERATOR</command> defines a new operator,
36 <replaceable class="parameter">name</replaceable>. The user who
37 defines an operator becomes its owner. If a schema name is given
38 then the operator is created in the specified schema. Otherwise it
39 is created in the current schema.
43 The operator name is a sequence of up to <symbol>NAMEDATALEN</>-1
44 (63 by default) characters from the following list:
46 + - * / < > = ~ ! @ # % ^ & | ` ?
49 There are a few restrictions on your choice of name:
53 <literal>--</literal> and <literal>/*</literal> cannot appear anywhere in an operator name,
54 since they will be taken as the start of a comment.
59 A multicharacter operator name cannot end in <literal>+</literal> or
61 unless the name also contains at least one of these characters:
63 ~ ! @ # % ^ & | ` ?
65 For example, <literal>@-</literal> is an allowed operator name,
66 but <literal>*-</literal> is not.
67 This restriction allows <productname>PostgreSQL</productname> to
68 parse SQL-compliant commands without requiring spaces between tokens.
75 The operator <literal>!=</literal> is mapped to
76 <literal><></literal> on input, so these two names are always
81 At least one of <literal>LEFTARG</> and <literal>RIGHTARG</> must be defined. For
82 binary operators, both must be defined. For right unary
83 operators, only <literal>LEFTARG</> should be defined, while for left
84 unary operators only <literal>RIGHTARG</> should be defined.
88 The <replaceable class="parameter">func_name</replaceable>
89 procedure must have been previously defined using <command>CREATE
90 FUNCTION</command> and must be defined to accept the correct number
91 of arguments (either one or two) of the indicated types.
95 The other clauses specify optional operator optimization clauses.
96 Their meaning is detailed in <xref linkend="xoper">.
101 <title>Parameters</title>
105 <term><replaceable class="parameter">name</replaceable></term>
108 The name of the operator to be defined. See above for allowable
109 characters. The name may be schema-qualified, for example
110 <literal>CREATE OPERATOR myschema.+ (...)</>. If not, then
111 the operator is created in the current schema. Two operators
112 in the same schema can have the same name if they operate on
113 different data types. This is called
114 <firstterm>overloading</>.
120 <term><replaceable class="parameter">func_name</replaceable></term>
123 The function used to implement this operator.
129 <term><replaceable class="parameter">lefttype</replaceable></term>
132 The type of the left-hand argument of the operator, if any.
133 This option would be omitted for a left-unary operator.
139 <term><replaceable class="parameter">righttype</replaceable></term>
142 The type of the right-hand argument of the operator, if any.
143 This option would be omitted for a right-unary operator.
149 <term><replaceable class="parameter">com_op</replaceable></term>
152 The commutator of this operator.
158 <term><replaceable class="parameter">neg_op</replaceable></term>
161 The negator of this operator.
167 <term><replaceable class="parameter">res_proc</replaceable></term>
170 The restriction selectivity estimator function for this operator.
176 <term><replaceable class="parameter">join_proc</replaceable></term>
179 The join selectivity estimator function for this operator.
185 <term><literal>HASHES</literal></term>
188 Indicates this operator can support a hash join.
194 <term><literal>MERGES</literal></term>
197 Indicates this operator can support a merge join.
203 <term><replaceable class="parameter">left_sort_op</replaceable></term>
206 If this operator can support a merge join, the less-than
207 operator that sorts the left-hand data type of this operator.
213 <term><replaceable class="parameter">right_sort_op</replaceable></term>
216 If this operator can support a merge join, the less-than
217 operator that sorts the right-hand data type of this operator.
223 <term><replaceable class="parameter">less_than_op</replaceable></term>
226 If this operator can support a merge join, the less-than
227 operator that compares the input data types of this operator.
233 <term><replaceable class="parameter">greater_than_op</replaceable></term>
236 If this operator can support a merge join, the greater-than
237 operator that compares the input data types of this operator.
244 To give a schema-qualified operator name in <replaceable
245 class="parameter">com_op</replaceable> or the other optional
246 arguments, use the <literal>OPERATOR()</> syntax, for example
248 COMMUTATOR = OPERATOR(myschema.===) ,
254 <title>Diagnostics</title>
258 <term><computeroutput>CREATE OPERATOR</computeroutput></term>
261 Message returned if the operator was successfully created.
272 Refer to <xref linkend="xoper"> for further information.
276 Use <command>DROP OPERATOR</command> to delete user-defined
277 operators from a database.
282 <title>Examples</title>
285 The following command defines a new operator, area-equality, for
286 the data type <type>box</type>:
288 CREATE OPERATOR === (
291 PROCEDURE = area_equal_procedure,
294 RESTRICT = area_restriction_procedure,
295 JOIN = area_join_procedure,
297 SORT1 = <<<,
299 -- Since sort operators were given, MERGES is implied.
300 -- LTCMP and GTCMP are assumed to be < and > respectively
307 <title>Compatibility</title>
310 <command>CREATE OPERATOR</command> is a
311 <productname>PostgreSQL</productname> extension. There are no
312 provisions for user-defined operators in the SQL standard.
317 <!-- Keep this comment at the end of the file
322 sgml-minimize-attributes:nil
323 sgml-always-quote-attributes:t
326 sgml-parent-document:nil
327 sgml-default-dtd-file:"../reference.ced"
328 sgml-exposed-tags:nil
329 sgml-local-catalogs:"/usr/lib/sgml/catalog"
330 sgml-local-ecat-files:nil