2 $Header: /cvsroot/pgsql/doc/src/sgml/ref/create_operator.sgml,v 1.19 2001/05/08 17:51:30 momjian Exp $
6 <refentry id="SQL-CREATEOPERATOR">
8 <refentrytitle id="sql-createoperator-title">
11 <refmiscinfo>SQL - Language Statements</refmiscinfo>
18 Defines a new user operator
23 <date>2000-03-25</date>
26 CREATE OPERATOR <replaceable>name</replaceable> ( PROCEDURE = <replaceable class="parameter">func_name</replaceable>
27 [, LEFTARG = <replaceable class="parameter">lefttype</replaceable>
28 ] [, RIGHTARG = <replaceable class="parameter">righttype</replaceable> ]
29 [, COMMUTATOR = <replaceable class="parameter">com_op</replaceable> ] [, NEGATOR = <replaceable class="parameter">neg_op</replaceable> ]
30 [, RESTRICT = <replaceable class="parameter">res_proc</replaceable> ] [, JOIN = <replaceable class="parameter">join_proc</replaceable> ]
31 [, HASHES ] [, SORT1 = <replaceable class="parameter">left_sort_op</replaceable> ] [, SORT2 = <replaceable class="parameter">right_sort_op</replaceable> ] )
34 <refsect2 id="R2-SQL-CREATEOPERATOR-1">
36 <date>2000-03-25</date>
45 <term><replaceable class="parameter">name</replaceable></term>
48 The operator to be defined. See below for allowable characters.
53 <term><replaceable class="parameter">func_name</replaceable></term>
56 The function used to implement this operator.
61 <term><replaceable class="parameter">lefttype</replaceable></term>
64 The type of the left-hand argument of the operator, if any.
65 This option would be omitted for a left-unary operator.
70 <term><replaceable class="parameter">righttype</replaceable></term>
73 The type of the right-hand argument of the operator, if any.
74 This option would be omitted for a right-unary operator.
79 <term><replaceable class="parameter">com_op</replaceable></term>
82 The commutator of this operator.
87 <term><replaceable class="parameter">neg_op</replaceable></term>
90 The negator of this operator.
95 <term><replaceable class="parameter">res_proc</replaceable></term>
98 The restriction selectivity estimator function for this operator.
103 <term><replaceable class="parameter">join_proc</replaceable></term>
106 The join selectivity estimator function for this operator.
114 Indicates this operator can support a hash join.
119 <term><replaceable class="parameter">left_sort_op</replaceable></term>
122 If this operator can support a merge join, the
123 operator that sorts the left-hand data type of this operator.
128 <term><replaceable class="parameter">right_sort_op</replaceable></term>
131 If this operator can support a merge join, the
132 operator that sorts the right-hand data type of this operator.
140 <refsect2 id="R2-SQL-CREATEOPERATOR-2">
142 <date>2000-03-25</date>
150 <term><computeroutput>
152 </computeroutput></term>
155 Message returned if the operator is successfully created.
164 <refsect1 id="R1-SQL-CREATEOPERATOR-1">
166 <date>2000-03-25</date>
172 <command>CREATE OPERATOR</command> defines a new operator,
173 <replaceable class="parameter">name</replaceable>.
174 The user who defines an operator becomes its owner.
177 The operator <replaceable class="parameter">name</replaceable>
178 is a sequence of up to NAMEDATALEN-1 (31 by default) characters
179 from the following list:
181 + - * / < > = ~ ! @ # % ^ & | ` ? $
184 There are a few restrictions on your choice of name:
188 "$" cannot be defined as a single-character operator,
189 although it can be part of a multi-character operator name.
194 "--" and "/*" cannot appear anywhere in an operator name,
195 since they will be taken as the start of a comment.
200 A multi-character operator name cannot end in "+" or "-",
201 unless the name also contains at least one of these characters:
203 ~ ! @ # % ^ & | ` ? $
205 For example, <literal>@-</literal> is an allowed operator name,
206 but <literal>*-</literal> is not.
207 This restriction allows <productname>Postgres</productname> to
208 parse SQL-compliant queries without requiring spaces between tokens.
215 When working with non-SQL-standard operator names, you will usually
216 need to separate adjacent operators with spaces to avoid ambiguity.
217 For example, if you have defined a left-unary operator named "@",
218 you cannot write <literal>X*@Y</literal>; you must write
219 <literal>X* @Y</literal> to ensure that
220 <productname>Postgres</productname> reads it as two operator names
226 The operator "!=" is mapped to "<>" on input, so these two names
227 are always equivalent.
230 At least one of LEFTARG and RIGHTARG must be defined. For
231 binary operators, both should be defined. For right unary
232 operators, only LEFTARG should be defined, while for left
233 unary operators only RIGHTARG should be defined.
237 <replaceable class="parameter">func_name</replaceable> procedure must have
238 been previously defined using <command>CREATE FUNCTION</command> and must
239 be defined to accept the correct number of arguments
240 (either one or two) of the indicated types.
243 The commutator operator should be identified if one exists,
244 so that <productname>Postgres</productname> can
245 reverse the order of the operands if it wishes.
246 For example, the operator area-less-than, <<<,
247 would probably have a commutator
248 operator, area-greater-than, >>>.
249 Hence, the query optimizer could freely convert:
252 box '((0,0), (1,1))' >>> MYBOXES.description
258 MYBOXES.description <<< box '((0,0), (1,1))'
262 This allows the execution code to always use the latter
263 representation and simplifies the query optimizer somewhat.
266 Similarly, if there is a negator operator then it should be
269 operator, area-equal, ===, exists, as well as an area not
271 The negator link allows the query optimizer to simplify
273 NOT MYBOXES.description === box '((0,0), (1,1))'
277 MYBOXES.description !== box '((0,0), (1,1))'
281 If a commutator operator name is supplied,
282 <productname>Postgres</productname>
283 searches for it in the catalog. If it is found and it
284 does not yet have a commutator itself, then the commutator's
285 entry is updated to have the newly created operator as its
286 commutator. This applies to the negator, as well.
287 This is to allow the definition of two operators that are
288 the commutators or the negators of each other. The first
289 operator should be defined without a commutator or negator
290 (as appropriate). When the second operator is defined,
291 name the first as the commutator or negator. The first
292 will be updated as a side effect. (As of Postgres 6.5,
293 it also works to just have both operators refer to each other.)
296 The HASHES, SORT1, and SORT2 options are present to support the
297 query optimizer in performing joins.
298 <productname>Postgres</productname> can always
299 evaluate a join (i.e., processing a clause with two tuple
300 variables separated by an operator that returns a boolean)
301 by iterative substitution [WONG76].
302 In addition, <productname>Postgres</productname>
303 can use a hash-join algorithm along
304 the lines of [SHAP86]; however, it must know whether this
305 strategy is applicable. The current hash-join algorithm
306 is only correct for operators that represent equality tests;
307 furthermore, equality of the data type must mean bitwise equality
308 of the representation of the type. (For example, a data type that
309 contains unused bits that don't matter for equality tests could
311 The HASHES flag indicates to the query optimizer that a hash join
312 may safely be used with this operator.</para>
314 Similarly, the two sort operators indicate to the query
315 optimizer whether merge-sort is a usable join strategy and
316 which operators should be used to sort the two operand
317 classes. Sort operators should only be provided for an equality
318 operator, and they should refer to less-than operators for the
319 left and right side data types respectively.
322 If other join strategies are found to be practical,
323 <productname>Postgres</productname>
324 will change the optimizer and run-time system to use
325 them and will require additional specification when an
326 operator is defined. Fortunately, the research community
327 invents new join strategies infrequently, and the added
328 generality of user-defined join strategies was not felt to
329 be worth the complexity involved.
332 The RESTRICT and JOIN options assist the query optimizer in estimating
333 result sizes. If a clause of the form:
335 MYBOXES.description <<< box '((0,0), (1,1))'
337 is present in the qualification,
338 then <productname>Postgres</productname> may have to
339 estimate the fraction of the instances in MYBOXES that
340 satisfy the clause. The function
341 <replaceable class="parameter">res_proc</replaceable>
342 must be a registered function (meaning it is already defined using
343 <command>CREATE FUNCTION</command>) which accepts arguments of the correct
344 data types and returns a floating point number. The
345 query optimizer simply calls this function, passing the
346 parameter <literal>((0,0), (1,1))</literal> and multiplies the result by the relation
347 size to get the expected number of instances.
350 Similarly, when the operands of the operator both contain
351 instance variables, the query optimizer must estimate the
352 size of the resulting join. The function join_proc will
353 return another floating point number which will be multiplied
354 by the cardinalities of the two tables involved to
355 compute the expected result size.
358 The difference between the function
360 my_procedure_1 (MYBOXES.description, box '((0,0), (1,1))')
364 MYBOXES.description === box '((0,0), (1,1))'
366 is that <productname>Postgres</productname>
367 attempts to optimize operators and can
368 decide to use an index to restrict the search space when
369 operators are involved. However, there is no attempt to
370 optimize functions, and they are performed by brute force.
371 Moreover, functions can have any number of arguments while
372 operators are restricted to one or two.
375 <refsect2 id="R2-SQL-CREATEOPERATOR-3">
377 <date>2000-03-25</date>
383 Refer to the chapter on operators in the
384 <citetitle>PostgreSQL User's Guide</citetitle>
385 for further information.
386 Refer to <command>DROP OPERATOR</command> to delete
387 user-defined operators from a database.
392 <refsect1 id="R1-SQL-CREATEOPERATOR-2">
396 <para>The following command defines a new operator,
397 area-equality, for the BOX data type:
400 CREATE OPERATOR === (
403 PROCEDURE = area_equal_procedure,
406 RESTRICT = area_restriction_procedure,
407 JOIN = area_join_procedure,
415 <refsect1 id="R1-SQL-CREATEOPERATOR-3">
420 <refsect2 id="R2-SQL-CREATEOPERATOR-4">
422 <date>2000-03-25</date>
429 <command>CREATE OPERATOR</command>
430 is a <productname>Postgres</productname> extension.
431 There is no <command>CREATE OPERATOR</command>
432 statement in <acronym>SQL92</acronym>.
438 <!-- Keep this comment at the end of the file
443 sgml-minimize-attributes:nil
444 sgml-always-quote-attributes:t
447 sgml-parent-document:nil
448 sgml-default-dtd-file:"../reference.ced"
449 sgml-exposed-tags:nil
450 sgml-local-catalogs:"/usr/lib/sgml/catalog"
451 sgml-local-ecat-files:nil