2 $Header: /cvsroot/pgsql/doc/src/sgml/ref/create_operator.sgml,v 1.35 2003/04/22 10:08:08 petere 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> cannot be defined as a single-character operator,
54 although it can be part of a multicharacter operator name.
59 <literal>--</literal> and <literal>/*</literal> cannot appear anywhere in an operator name,
60 since they will be taken as the start of a comment.
65 A multicharacter operator name cannot end in <literal>+</literal> or
67 unless the name also contains at least one of these characters:
69 ~ ! @ # % ^ & | ` ? $
71 For example, <literal>@-</literal> is an allowed operator name,
72 but <literal>*-</literal> is not.
73 This restriction allows <productname>PostgreSQL</productname> to
74 parse SQL-compliant commands without requiring spaces between tokens.
81 The operator <literal>!=</literal> is mapped to
82 <literal><></literal> on input, so these two names are always
87 At least one of <literal>LEFTARG</> and <literal>RIGHTARG</> must be defined. For
88 binary operators, both must be defined. For right unary
89 operators, only <literal>LEFTARG</> should be defined, while for left
90 unary operators only <literal>RIGHTARG</> should be defined.
94 The <replaceable class="parameter">func_name</replaceable>
95 procedure must have been previously defined using <command>CREATE
96 FUNCTION</command> and must be defined to accept the correct number
97 of arguments (either one or two) of the indicated types.
101 The other clauses specify optional operator optimization clauses.
102 Their meaning is detailed in <xref linkend="xoper">.
107 <title>Parameters</title>
111 <term><replaceable class="parameter">name</replaceable></term>
114 The name of the operator to be defined. See above for allowable
115 characters. The name may be schema-qualified, for example
116 <literal>CREATE OPERATOR myschema.+ (...)</>. If not, then
117 the operator is created in the current schema. Two operators
118 in the same schema can have the same name if they operate on
119 different data types. This is called
120 <firstterm>overloading</>.
126 <term><replaceable class="parameter">func_name</replaceable></term>
129 The function used to implement this operator.
135 <term><replaceable class="parameter">lefttype</replaceable></term>
138 The type of the left-hand argument of the operator, if any.
139 This option would be omitted for a left-unary operator.
145 <term><replaceable class="parameter">righttype</replaceable></term>
148 The type of the right-hand argument of the operator, if any.
149 This option would be omitted for a right-unary operator.
155 <term><replaceable class="parameter">com_op</replaceable></term>
158 The commutator of this operator.
164 <term><replaceable class="parameter">neg_op</replaceable></term>
167 The negator of this operator.
173 <term><replaceable class="parameter">res_proc</replaceable></term>
176 The restriction selectivity estimator function for this operator.
182 <term><replaceable class="parameter">join_proc</replaceable></term>
185 The join selectivity estimator function for this operator.
191 <term><literal>HASHES</literal></term>
194 Indicates this operator can support a hash join.
200 <term><literal>MERGES</literal></term>
203 Indicates this operator can support a merge join.
209 <term><replaceable class="parameter">left_sort_op</replaceable></term>
212 If this operator can support a merge join, the less-than
213 operator that sorts the left-hand data type of this operator.
219 <term><replaceable class="parameter">right_sort_op</replaceable></term>
222 If this operator can support a merge join, the less-than
223 operator that sorts the right-hand data type of this operator.
229 <term><replaceable class="parameter">less_than_op</replaceable></term>
232 If this operator can support a merge join, the less-than
233 operator that compares the input data types of this operator.
239 <term><replaceable class="parameter">greater_than_op</replaceable></term>
242 If this operator can support a merge join, the greater-than
243 operator that compares the input data types of this operator.
250 To give a schema-qualified operator name in <replaceable
251 class="parameter">com_op</replaceable> or the other optional
252 arguments, use the <literal>OPERATOR()</> syntax, for example
254 COMMUTATOR = OPERATOR(myschema.===) ,
260 <title>Diagnostics</title>
264 <term><computeroutput>CREATE OPERATOR</computeroutput></term>
267 Message returned if the operator was successfully created.
278 Refer to <xref linkend="xoper"> for further information.
282 Use <command>DROP OPERATOR</command> to delete user-defined
283 operators from a database.
288 <title>Examples</title>
291 The following command defines a new operator, area-equality, for
292 the data type <type>box</type>:
294 CREATE OPERATOR === (
297 PROCEDURE = area_equal_procedure,
300 RESTRICT = area_restriction_procedure,
301 JOIN = area_join_procedure,
303 SORT1 = <<<,
305 -- Since sort operators were given, MERGES is implied.
306 -- LTCMP and GTCMP are assumed to be < and > respectively
313 <title>Compatibility</title>
316 <command>CREATE OPERATOR</command> is a
317 <productname>PostgreSQL</productname> extension. There are no
318 provisions for user-defined operators in the SQL standard.
323 <!-- Keep this comment at the end of the file
328 sgml-minimize-attributes:nil
329 sgml-always-quote-attributes:t
332 sgml-parent-document:nil
333 sgml-default-dtd-file:"../reference.ced"
334 sgml-exposed-tags:nil
335 sgml-local-catalogs:"/usr/lib/sgml/catalog"
336 sgml-local-ecat-files:nil