]> granicus.if.org Git - postgresql/blob - doc/src/sgml/ref/create_operator.sgml
d806bdd9d6d66e1c166a51af3d033858b25a6a24
[postgresql] / doc / src / sgml / ref / create_operator.sgml
1 <!--
2 $Header: /cvsroot/pgsql/doc/src/sgml/ref/create_operator.sgml,v 1.38 2003/09/09 18:28:52 tgl Exp $
3 PostgreSQL documentation
4 -->
5
6 <refentry id="SQL-CREATEOPERATOR">
7  <refmeta>
8   <refentrytitle id="sql-createoperator-title">CREATE OPERATOR</refentrytitle>
9   <refmiscinfo>SQL - Language Statements</refmiscinfo>
10  </refmeta>
11
12  <refnamediv>
13   <refname>CREATE OPERATOR</refname>
14   <refpurpose>define a new operator</refpurpose>
15  </refnamediv>
16
17  <indexterm zone="sql-createoperator">
18   <primary>CREATE OPERATOR</primary>
19  </indexterm>
20
21  <refsynopsisdiv>
22 <synopsis>
23 CREATE OPERATOR <replaceable>name</replaceable> (
24     PROCEDURE = <replaceable class="parameter">func_name</replaceable>
25     [, LEFTARG = <replaceable class="parameter">lefttype</replaceable> ] [, RIGHTARG = <replaceable class="parameter">righttype</replaceable> ]
26     [, COMMUTATOR = <replaceable class="parameter">com_op</replaceable> ] [, NEGATOR = <replaceable class="parameter">neg_op</replaceable> ]
27     [, RESTRICT = <replaceable class="parameter">res_proc</replaceable> ] [, JOIN = <replaceable class="parameter">join_proc</replaceable> ]
28     [, HASHES ] [, MERGES ]
29     [, SORT1 = <replaceable class="parameter">left_sort_op</replaceable> ] [, SORT2 = <replaceable class="parameter">right_sort_op</replaceable> ]
30     [, LTCMP = <replaceable class="parameter">less_than_op</replaceable> ] [, GTCMP = <replaceable class="parameter">greater_than_op</replaceable> ]
31 )
32 </synopsis>
33  </refsynopsisdiv>
34
35  <refsect1>
36   <title>Description</title>
37
38   <para>
39    <command>CREATE OPERATOR</command> defines a new operator,
40    <replaceable class="parameter">name</replaceable>.  The user who
41    defines an operator becomes its owner.  If a schema name is given
42    then the operator is created in the specified schema.  Otherwise it
43    is created in the current schema.
44   </para>
45
46   <para>
47    The operator name is a sequence of up to <symbol>NAMEDATALEN</>-1
48    (63 by default) characters from the following list:
49 <literallayout>
50 + - * / &lt; &gt; = ~ ! @ # % ^ &amp; | ` ?
51 </literallayout>
52
53    There are a few restrictions on your choice of name:
54    <itemizedlist>
55     <listitem>
56      <para>
57      <literal>--</literal> and <literal>/*</literal> cannot appear anywhere in an operator name,
58      since they will be taken as the start of a comment.
59      </para>
60     </listitem>
61     <listitem>
62      <para>
63      A multicharacter operator name cannot end in <literal>+</literal> or
64      <literal>-</literal>,
65      unless the name also contains at least one of these characters:
66 <literallayout>
67 ~ ! @ # % ^ &amp; | ` ?
68 </literallayout>
69      For example, <literal>@-</literal> is an allowed operator name,
70      but <literal>*-</literal> is not.
71      This restriction allows <productname>PostgreSQL</productname> to
72      parse SQL-compliant commands without requiring spaces between tokens.
73      </para>
74     </listitem>
75    </itemizedlist>
76   </para>
77
78   <para>
79    The operator <literal>!=</literal> is mapped to
80    <literal>&lt;&gt;</literal> on input, so these two names are always
81    equivalent.
82   </para>
83
84   <para>
85    At least one of <literal>LEFTARG</> and <literal>RIGHTARG</> must be defined.  For
86    binary operators, both must be defined. For right  unary
87    operators, only <literal>LEFTARG</> should be defined, while for left
88    unary operators only <literal>RIGHTARG</> should be defined.
89   </para>
90
91   <para>
92    The <replaceable class="parameter">func_name</replaceable>
93    procedure must have been previously defined using <command>CREATE
94    FUNCTION</command> and must be defined to accept the correct number
95    of arguments (either one or two) of the indicated types.
96   </para>
97
98   <para>
99    The other clauses specify optional operator optimization clauses.
100    Their meaning is detailed in <xref linkend="xoper">.
101   </para>
102  </refsect1>
103
104  <refsect1>
105   <title>Parameters</title>
106
107     <variablelist>
108      <varlistentry>
109       <term><replaceable class="parameter">name</replaceable></term>
110       <listitem>
111        <para>
112         The name of the operator to be defined. See above for allowable
113         characters.  The name may be schema-qualified, for example
114         <literal>CREATE OPERATOR myschema.+ (...)</>.  If not, then
115         the operator is created in the current schema.  Two operators
116         in the same schema can have the same name if they operate on
117         different data types.  This is called
118         <firstterm>overloading</>.
119        </para>
120       </listitem>
121      </varlistentry>
122
123      <varlistentry>
124       <term><replaceable class="parameter">func_name</replaceable></term>
125       <listitem>
126        <para>
127         The function used to implement this operator.
128        </para>
129       </listitem>
130      </varlistentry>
131
132      <varlistentry>
133       <term><replaceable class="parameter">lefttype</replaceable></term>
134       <listitem>
135        <para>
136         The type of the left-hand argument of the operator, if any.
137         This option would be omitted for a left-unary operator.
138        </para>
139       </listitem>
140      </varlistentry>
141
142      <varlistentry>
143       <term><replaceable class="parameter">righttype</replaceable></term>
144       <listitem>
145        <para>
146         The type of the right-hand argument of the operator, if any.
147         This option would be omitted for a right-unary operator.
148        </para>
149       </listitem>
150      </varlistentry>
151
152      <varlistentry>
153       <term><replaceable class="parameter">com_op</replaceable></term>
154       <listitem>
155        <para>
156         The commutator of this operator.
157        </para>
158       </listitem>
159      </varlistentry>
160
161      <varlistentry>
162       <term><replaceable class="parameter">neg_op</replaceable></term>
163       <listitem>
164        <para>
165         The negator of this operator.
166        </para>
167       </listitem>
168      </varlistentry>
169
170      <varlistentry>
171       <term><replaceable class="parameter">res_proc</replaceable></term>
172       <listitem>
173        <para>
174         The restriction selectivity estimator function for this operator.
175        </para>
176       </listitem>
177      </varlistentry>
178
179      <varlistentry>
180       <term><replaceable class="parameter">join_proc</replaceable></term>
181       <listitem>
182        <para>
183         The join selectivity estimator function for this operator.
184        </para>
185       </listitem>
186      </varlistentry>
187
188      <varlistentry>
189       <term><literal>HASHES</literal></term>
190       <listitem>
191        <para>
192        Indicates this operator can support a hash join.
193        </para>
194       </listitem>
195      </varlistentry>
196
197      <varlistentry>
198       <term><literal>MERGES</literal></term>
199       <listitem>
200        <para>
201        Indicates this operator can support a merge join.
202        </para>
203       </listitem>
204      </varlistentry>
205
206      <varlistentry>
207       <term><replaceable class="parameter">left_sort_op</replaceable></term>
208       <listitem>
209        <para>
210         If this operator can support a merge join, the less-than
211         operator that sorts the left-hand data type of this operator.
212        </para>
213       </listitem>
214      </varlistentry>
215
216      <varlistentry>
217       <term><replaceable class="parameter">right_sort_op</replaceable></term>
218       <listitem>
219        <para>
220         If this operator can support a merge join, the less-than
221         operator that sorts the right-hand data type of this operator.
222        </para>
223       </listitem>
224      </varlistentry>
225
226      <varlistentry>
227       <term><replaceable class="parameter">less_than_op</replaceable></term>
228       <listitem>
229        <para>
230         If this operator can support a merge join, the less-than
231         operator that compares the input data types of this operator.
232        </para>
233       </listitem>
234      </varlistentry>
235
236      <varlistentry>
237       <term><replaceable class="parameter">greater_than_op</replaceable></term>
238       <listitem>
239        <para>
240         If this operator can support a merge join, the greater-than
241         operator that compares the input data types of this operator.
242        </para>
243       </listitem>
244      </varlistentry>
245     </variablelist>
246
247   <para>
248    To give a schema-qualified operator name in <replaceable
249    class="parameter">com_op</replaceable> or the other optional
250    arguments, use the <literal>OPERATOR()</> syntax, for example
251 <programlisting>
252 COMMUTATOR = OPERATOR(myschema.===) ,
253 </programlisting>  
254   </para>
255  </refsect1>
256   
257  <refsect1>
258   <title>Notes</title>
259
260   <para>
261    Refer to <xref linkend="xoper"> for further information.
262   </para>
263
264   <para>
265    Use <command>DROP OPERATOR</command> to delete user-defined
266    operators from a database.
267   </para>
268  </refsect1>
269   
270  <refsect1>
271   <title>Examples</title>
272
273   <para>
274    The following command defines a new operator, area-equality, for
275    the data type <type>box</type>:
276 <programlisting>
277 CREATE OPERATOR === (
278     LEFTARG = box,
279     RIGHTARG = box,
280     PROCEDURE = area_equal_procedure,
281     COMMUTATOR = ===,
282     NEGATOR = !==,
283     RESTRICT = area_restriction_procedure,
284     JOIN = area_join_procedure,
285     HASHES,
286     SORT1 = &lt;&lt;&lt;,
287     SORT2 = &lt;&lt;&lt;
288     -- Since sort operators were given, MERGES is implied.
289     -- LTCMP and GTCMP are assumed to be &lt; and &gt; respectively
290 );
291 </programlisting>  
292   </para>
293  </refsect1>
294  
295  <refsect1>
296   <title>Compatibility</title>
297
298   <para>
299    <command>CREATE OPERATOR</command> is a
300    <productname>PostgreSQL</productname> extension.  There are no
301    provisions for user-defined operators in the SQL standard.
302   </para>
303  </refsect1>
304 </refentry>
305
306 <!-- Keep this comment at the end of the file
307 Local variables:
308 mode: sgml
309 sgml-omittag:nil
310 sgml-shorttag:t
311 sgml-minimize-attributes:nil
312 sgml-always-quote-attributes:t
313 sgml-indent-step:1
314 sgml-indent-data:t
315 sgml-parent-document:nil
316 sgml-default-dtd-file:"../reference.ced"
317 sgml-exposed-tags:nil
318 sgml-local-catalogs:"/usr/lib/sgml/catalog"
319 sgml-local-ecat-files:nil
320 End:
321 -->