Make LC_COLLATE and LC_CTYPE database-level settings. Collation and
[PostgreSQL.git] / doc / src / sgml / ref / create_operator.sgml
bloba60e488f06b0260337e784679de00a1b97043635
1 <!--
2 $PostgreSQL$
3 PostgreSQL documentation
4 -->
6 <refentry id="SQL-CREATEOPERATOR">
7 <refmeta>
8 <refentrytitle id="sql-createoperator-title">CREATE OPERATOR</refentrytitle>
9 <refmiscinfo>SQL - Language Statements</refmiscinfo>
10 </refmeta>
12 <refnamediv>
13 <refname>CREATE OPERATOR</refname>
14 <refpurpose>define a new operator</refpurpose>
15 </refnamediv>
17 <indexterm zone="sql-createoperator">
18 <primary>CREATE OPERATOR</primary>
19 </indexterm>
21 <refsynopsisdiv>
22 <synopsis>
23 CREATE OPERATOR <replaceable>name</replaceable> (
24 PROCEDURE = <replaceable class="parameter">funcname</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 ]
30 </synopsis>
31 </refsynopsisdiv>
33 <refsect1>
34 <title>Description</title>
36 <para>
37 <command>CREATE OPERATOR</command> defines a new operator,
38 <replaceable class="parameter">name</replaceable>. The user who
39 defines an operator becomes its owner. If a schema name is given
40 then the operator is created in the specified schema. Otherwise it
41 is created in the current schema.
42 </para>
44 <para>
45 The operator name is a sequence of up to <symbol>NAMEDATALEN</>-1
46 (63 by default) characters from the following list:
47 <literallayout>
48 + - * / &lt; &gt; = ~ ! @ # % ^ &amp; | ` ?
49 </literallayout>
51 There are a few restrictions on your choice of name:
52 <itemizedlist>
53 <listitem>
54 <para>
55 <literal>--</literal> and <literal>/*</literal> cannot appear anywhere in an operator name,
56 since they will be taken as the start of a comment.
57 </para>
58 </listitem>
59 <listitem>
60 <para>
61 A multicharacter operator name cannot end in <literal>+</literal> or
62 <literal>-</literal>,
63 unless the name also contains at least one of these characters:
64 <literallayout>
65 ~ ! @ # % ^ &amp; | ` ?
66 </literallayout>
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.
71 </para>
72 </listitem>
73 </itemizedlist>
74 </para>
76 <para>
77 The operator <literal>!=</literal> is mapped to
78 <literal>&lt;&gt;</literal> on input, so these two names are always
79 equivalent.
80 </para>
82 <para>
83 At least one of <literal>LEFTARG</> and <literal>RIGHTARG</> must be defined. For
84 binary operators, both must be defined. For right unary
85 operators, only <literal>LEFTARG</> should be defined, while for left
86 unary operators only <literal>RIGHTARG</> should be defined.
87 </para>
89 <para>
90 The <replaceable class="parameter">funcname</replaceable>
91 procedure must have been previously defined using <command>CREATE
92 FUNCTION</command> and must be defined to accept the correct number
93 of arguments (either one or two) of the indicated types.
94 </para>
96 <para>
97 The other clauses specify optional operator optimization clauses.
98 Their meaning is detailed in <xref linkend="xoper-optimization">.
99 </para>
100 </refsect1>
102 <refsect1>
103 <title>Parameters</title>
105 <variablelist>
106 <varlistentry>
107 <term><replaceable class="parameter">name</replaceable></term>
108 <listitem>
109 <para>
110 The name of the operator to be defined. See above for allowable
111 characters. The name can be schema-qualified, for example
112 <literal>CREATE OPERATOR myschema.+ (...)</>. If not, then
113 the operator is created in the current schema. Two operators
114 in the same schema can have the same name if they operate on
115 different data types. This is called
116 <firstterm>overloading</>.
117 </para>
118 </listitem>
119 </varlistentry>
121 <varlistentry>
122 <term><replaceable class="parameter">funcname</replaceable></term>
123 <listitem>
124 <para>
125 The function used to implement this operator.
126 </para>
127 </listitem>
128 </varlistentry>
130 <varlistentry>
131 <term><replaceable class="parameter">lefttype</replaceable></term>
132 <listitem>
133 <para>
134 The data type of the operator's left operand, if any.
135 This option would be omitted for a left-unary operator.
136 </para>
137 </listitem>
138 </varlistentry>
140 <varlistentry>
141 <term><replaceable class="parameter">righttype</replaceable></term>
142 <listitem>
143 <para>
144 The data type of the operator's right operand, if any.
145 This option would be omitted for a right-unary operator.
146 </para>
147 </listitem>
148 </varlistentry>
150 <varlistentry>
151 <term><replaceable class="parameter">com_op</replaceable></term>
152 <listitem>
153 <para>
154 The commutator of this operator.
155 </para>
156 </listitem>
157 </varlistentry>
159 <varlistentry>
160 <term><replaceable class="parameter">neg_op</replaceable></term>
161 <listitem>
162 <para>
163 The negator of this operator.
164 </para>
165 </listitem>
166 </varlistentry>
168 <varlistentry>
169 <term><replaceable class="parameter">res_proc</replaceable></term>
170 <listitem>
171 <para>
172 The restriction selectivity estimator function for this operator.
173 </para>
174 </listitem>
175 </varlistentry>
177 <varlistentry>
178 <term><replaceable class="parameter">join_proc</replaceable></term>
179 <listitem>
180 <para>
181 The join selectivity estimator function for this operator.
182 </para>
183 </listitem>
184 </varlistentry>
186 <varlistentry>
187 <term><literal>HASHES</literal></term>
188 <listitem>
189 <para>
190 Indicates this operator can support a hash join.
191 </para>
192 </listitem>
193 </varlistentry>
195 <varlistentry>
196 <term><literal>MERGES</literal></term>
197 <listitem>
198 <para>
199 Indicates this operator can support a merge join.
200 </para>
201 </listitem>
202 </varlistentry>
203 </variablelist>
205 <para>
206 To give a schema-qualified operator name in <replaceable
207 class="parameter">com_op</replaceable> or the other optional
208 arguments, use the <literal>OPERATOR()</> syntax, for example:
209 <programlisting>
210 COMMUTATOR = OPERATOR(myschema.===) ,
211 </programlisting>
212 </para>
213 </refsect1>
215 <refsect1>
216 <title>Notes</title>
218 <para>
219 Refer to <xref linkend="xoper"> for further information.
220 </para>
222 <para>
223 The obsolete options <literal>SORT1</>, <literal>SORT2</>,
224 <literal>LTCMP</>, and <literal>GTCMP</> were formerly used to
225 specify the names of sort operators associated with a merge-joinable
226 operator. This is no longer necessary, since information about
227 associated operators is found by looking at B-tree operator families
228 instead. If one of these options is given, it is ignored except
229 for implicitly setting <literal>MERGES</> true.
230 </para>
232 <para>
233 Use <xref linkend="sql-dropoperator"
234 endterm="sql-dropoperator-title"> to delete user-defined operators
235 from a database. Use <xref linkend="sql-alteroperator"
236 endterm="sql-alteroperator-title"> to modify operators in a
237 database.
238 </para>
239 </refsect1>
241 <refsect1>
242 <title>Examples</title>
244 <para>
245 The following command defines a new operator, area-equality, for
246 the data type <type>box</type>:
247 <programlisting>
248 CREATE OPERATOR === (
249 LEFTARG = box,
250 RIGHTARG = box,
251 PROCEDURE = area_equal_procedure,
252 COMMUTATOR = ===,
253 NEGATOR = !==,
254 RESTRICT = area_restriction_procedure,
255 JOIN = area_join_procedure,
256 HASHES, MERGES
258 </programlisting>
259 </para>
260 </refsect1>
262 <refsect1>
263 <title>Compatibility</title>
265 <para>
266 <command>CREATE OPERATOR</command> is a
267 <productname>PostgreSQL</productname> extension. There are no
268 provisions for user-defined operators in the SQL standard.
269 </para>
270 </refsect1>
272 <refsect1>
273 <title>See Also</title>
275 <simplelist type="inline">
276 <member><xref linkend="sql-alteroperator" endterm="sql-alteroperator-title"></member>
277 <member><xref linkend="sql-createopclass" endterm="sql-createopclass-title"></member>
278 <member><xref linkend="sql-dropoperator" endterm="sql-dropoperator-title"></member>
279 </simplelist>
280 </refsect1>
281 </refentry>