]> granicus.if.org Git - postgresql/blob - doc/src/sgml/ref/create_operator.sgml
More editing of reference pages.
[postgresql] / doc / src / sgml / ref / create_operator.sgml
1 <!--
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
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  <refsynopsisdiv>
18 <synopsis>
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> ]
27 )
28 </synopsis>
29  </refsynopsisdiv>
30
31  <refsect1>
32   <title>Description</title>
33
34   <para>
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.
40   </para>
41
42   <para>
43    The operator name is a sequence of up to <symbol>NAMEDATALEN</>-1
44    (63 by default) characters from the following list:
45 <literallayout>
46 + - * / &lt; &gt; = ~ ! @ # % ^ &amp; | ` ? $
47 </literallayout>
48
49    There are a few restrictions on your choice of name:
50    <itemizedlist>
51     <listitem>
52      <para>
53      <literal>$</literal> cannot be defined as a single-character operator,
54      although it can be part of a multicharacter operator name.
55      </para>
56     </listitem>
57     <listitem>
58      <para>
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.
61      </para>
62     </listitem>
63     <listitem>
64      <para>
65      A multicharacter operator name cannot end in <literal>+</literal> or
66      <literal>-</literal>,
67      unless the name also contains at least one of these characters:
68 <literallayout>
69 ~ ! @ # % ^ &amp; | ` ? $
70 </literallayout>
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.
75      </para>
76     </listitem>
77    </itemizedlist>
78   </para>
79
80   <para>
81    The operator <literal>!=</literal> is mapped to
82    <literal>&lt;&gt;</literal> on input, so these two names are always
83    equivalent.
84   </para>
85
86   <para>
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.
91   </para>
92
93   <para>
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.
98   </para>
99
100   <para>
101    The other clauses specify optional operator optimization clauses.
102    Their meaning is detailed in <xref linkend="xoper">.
103   </para>
104  </refsect1>
105
106  <refsect1>
107   <title>Parameters</title>
108
109     <variablelist>
110      <varlistentry>
111       <term><replaceable class="parameter">name</replaceable></term>
112       <listitem>
113        <para>
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</>.
121        </para>
122       </listitem>
123      </varlistentry>
124
125      <varlistentry>
126       <term><replaceable class="parameter">func_name</replaceable></term>
127       <listitem>
128        <para>
129         The function used to implement this operator.
130        </para>
131       </listitem>
132      </varlistentry>
133
134      <varlistentry>
135       <term><replaceable class="parameter">lefttype</replaceable></term>
136       <listitem>
137        <para>
138         The type of the left-hand argument of the operator, if any.
139         This option would be omitted for a left-unary operator.
140        </para>
141       </listitem>
142      </varlistentry>
143
144      <varlistentry>
145       <term><replaceable class="parameter">righttype</replaceable></term>
146       <listitem>
147        <para>
148         The type of the right-hand argument of the operator, if any.
149         This option would be omitted for a right-unary operator.
150        </para>
151       </listitem>
152      </varlistentry>
153
154      <varlistentry>
155       <term><replaceable class="parameter">com_op</replaceable></term>
156       <listitem>
157        <para>
158         The commutator of this operator.
159        </para>
160       </listitem>
161      </varlistentry>
162
163      <varlistentry>
164       <term><replaceable class="parameter">neg_op</replaceable></term>
165       <listitem>
166        <para>
167         The negator of this operator.
168        </para>
169       </listitem>
170      </varlistentry>
171
172      <varlistentry>
173       <term><replaceable class="parameter">res_proc</replaceable></term>
174       <listitem>
175        <para>
176         The restriction selectivity estimator function for this operator.
177        </para>
178       </listitem>
179      </varlistentry>
180
181      <varlistentry>
182       <term><replaceable class="parameter">join_proc</replaceable></term>
183       <listitem>
184        <para>
185         The join selectivity estimator function for this operator.
186        </para>
187       </listitem>
188      </varlistentry>
189
190      <varlistentry>
191       <term><literal>HASHES</literal></term>
192       <listitem>
193        <para>
194        Indicates this operator can support a hash join.
195        </para>
196       </listitem>
197      </varlistentry>
198
199      <varlistentry>
200       <term><literal>MERGES</literal></term>
201       <listitem>
202        <para>
203        Indicates this operator can support a merge join.
204        </para>
205       </listitem>
206      </varlistentry>
207
208      <varlistentry>
209       <term><replaceable class="parameter">left_sort_op</replaceable></term>
210       <listitem>
211        <para>
212         If this operator can support a merge join, the less-than
213         operator that sorts the left-hand data type of this operator.
214        </para>
215       </listitem>
216      </varlistentry>
217
218      <varlistentry>
219       <term><replaceable class="parameter">right_sort_op</replaceable></term>
220       <listitem>
221        <para>
222         If this operator can support a merge join, the less-than
223         operator that sorts the right-hand data type of this operator.
224        </para>
225       </listitem>
226      </varlistentry>
227
228      <varlistentry>
229       <term><replaceable class="parameter">less_than_op</replaceable></term>
230       <listitem>
231        <para>
232         If this operator can support a merge join, the less-than
233         operator that compares the input data types of this operator.
234        </para>
235       </listitem>
236      </varlistentry>
237
238      <varlistentry>
239       <term><replaceable class="parameter">greater_than_op</replaceable></term>
240       <listitem>
241        <para>
242         If this operator can support a merge join, the greater-than
243         operator that compares the input data types of this operator.
244        </para>
245       </listitem>
246      </varlistentry>
247     </variablelist>
248
249   <para>
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
253 <programlisting>
254 COMMUTATOR = OPERATOR(myschema.===) ,
255 </programlisting>  
256   </para>
257  </refsect1>
258
259  <refsect1>
260   <title>Diagnostics</title>
261
262   <variablelist>
263    <varlistentry>
264     <term><computeroutput>CREATE OPERATOR</computeroutput></term>
265     <listitem>
266      <para>
267       Message returned if the operator was successfully created.
268      </para>
269     </listitem>
270    </varlistentry>
271   </variablelist>
272  </refsect1>
273   
274  <refsect1>
275   <title>Notes</title>
276
277   <para>
278    Refer to <xref linkend="xoper"> for further information.
279   </para>
280
281   <para>
282    Use <command>DROP OPERATOR</command> to delete user-defined
283    operators from a database.
284   </para>
285  </refsect1>
286   
287  <refsect1>
288   <title>Examples</title>
289
290   <para>
291    The following command defines a new operator, area-equality, for
292    the data type <type>box</type>:
293 <programlisting>
294 CREATE OPERATOR === (
295     LEFTARG = box,
296     RIGHTARG = box,
297     PROCEDURE = area_equal_procedure,
298     COMMUTATOR = ===,
299     NEGATOR = !==,
300     RESTRICT = area_restriction_procedure,
301     JOIN = area_join_procedure,
302     HASHES,
303     SORT1 = &lt;&lt;&lt;,
304     SORT2 = &lt;&lt;&lt;
305     -- Since sort operators were given, MERGES is implied.
306     -- LTCMP and GTCMP are assumed to be &lt; and &gt; respectively
307 );
308 </programlisting>  
309   </para>
310  </refsect1>
311  
312  <refsect1>
313   <title>Compatibility</title>
314
315   <para>
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.
319   </para>
320  </refsect1>
321 </refentry>
322
323 <!-- Keep this comment at the end of the file
324 Local variables:
325 mode: sgml
326 sgml-omittag:nil
327 sgml-shorttag:t
328 sgml-minimize-attributes:nil
329 sgml-always-quote-attributes:t
330 sgml-indent-step:1
331 sgml-indent-data: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
337 End:
338 -->