2 $Header: /cvsroot/pgsql/doc/src/sgml/ref/create_operator.sgml,v 1.26 2002/04/16 23:08:10 tgl Exp $
3 PostgreSQL documentation
6 <refentry id="SQL-CREATEOPERATOR">
8 <refentrytitle id="sql-createoperator-title">
11 <refmiscinfo>SQL - Language Statements</refmiscinfo>
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 ] [, MERGES ]
32 [, SORT1 = <replaceable class="parameter">left_sort_op</replaceable> ] [, SORT2 = <replaceable class="parameter">right_sort_op</replaceable> ]
33 [, LTCMP = <replaceable class="parameter">less_than_op</replaceable> ] [, GTCMP = <replaceable class="parameter">greater_than_op</replaceable> ] )
36 <refsect2 id="R2-SQL-CREATEOPERATOR-1">
38 <date>2000-03-25</date>
47 <term><replaceable class="parameter">name</replaceable></term>
50 The operator to be defined. See below for allowable characters.
55 <term><replaceable class="parameter">func_name</replaceable></term>
58 The function used to implement this operator.
63 <term><replaceable class="parameter">lefttype</replaceable></term>
66 The type of the left-hand argument of the operator, if any.
67 This option would be omitted for a left-unary operator.
72 <term><replaceable class="parameter">righttype</replaceable></term>
75 The type of the right-hand argument of the operator, if any.
76 This option would be omitted for a right-unary operator.
81 <term><replaceable class="parameter">com_op</replaceable></term>
84 The commutator of this operator.
89 <term><replaceable class="parameter">neg_op</replaceable></term>
92 The negator of this operator.
97 <term><replaceable class="parameter">res_proc</replaceable></term>
100 The restriction selectivity estimator function for this operator.
105 <term><replaceable class="parameter">join_proc</replaceable></term>
108 The join selectivity estimator function for this operator.
116 Indicates this operator can support a hash join.
124 Indicates this operator can support a merge join.
129 <term><replaceable class="parameter">left_sort_op</replaceable></term>
132 If this operator can support a merge join, the less-than
133 operator that sorts the left-hand data type of this operator.
138 <term><replaceable class="parameter">right_sort_op</replaceable></term>
141 If this operator can support a merge join, the less-than
142 operator that sorts the right-hand data type of this operator.
147 <term><replaceable class="parameter">less_than_op</replaceable></term>
150 If this operator can support a merge join, the less-than
151 operator that compares the input data types of this operator.
156 <term><replaceable class="parameter">greater_than_op</replaceable></term>
159 If this operator can support a merge join, the greater-than
160 operator that compares the input data types of this operator.
168 <refsect2 id="R2-SQL-CREATEOPERATOR-2">
170 <date>2000-03-25</date>
178 <term><computeroutput>
180 </computeroutput></term>
183 Message returned if the operator is successfully created.
192 <refsect1 id="R1-SQL-CREATEOPERATOR-1">
194 <date>2000-03-25</date>
200 <command>CREATE OPERATOR</command> defines a new operator,
201 <replaceable class="parameter">name</replaceable>.
202 The user who defines an operator becomes its owner.
205 The operator <replaceable class="parameter">name</replaceable>
206 is a sequence of up to <symbol>NAMEDATALEN</>-1 (31 by default) characters
207 from the following list:
209 + - * / < > = ~ ! @ # % ^ & | ` ? $
212 There are a few restrictions on your choice of name:
216 <literal>$</literal> cannot be defined as a single-character operator,
217 although it can be part of a multicharacter operator name.
222 <literal>--</literal> and <literal>/*</literal> cannot appear anywhere in an operator name,
223 since they will be taken as the start of a comment.
228 A multicharacter operator name cannot end in <literal>+</literal> or
229 <literal>-</literal>,
230 unless the name also contains at least one of these characters:
232 ~ ! @ # % ^ & | ` ? $
234 For example, <literal>@-</literal> is an allowed operator name,
235 but <literal>*-</literal> is not.
236 This restriction allows <productname>PostgreSQL</productname> to
237 parse SQL-compliant queries without requiring spaces between tokens.
244 When working with non-SQL-standard operator names, you will usually
245 need to separate adjacent operators with spaces to avoid ambiguity.
246 For example, if you have defined a left-unary operator named <literal>@</literal>,
247 you cannot write <literal>X*@Y</literal>; you must write
248 <literal>X* @Y</literal> to ensure that
249 <productname>PostgreSQL</productname> reads it as two operator names
255 The operator <literal>!=</literal> is mapped to <literal><></literal> on input, so these two names
256 are always equivalent.
259 At least one of LEFTARG and RIGHTARG must be defined. For
260 binary operators, both should be defined. For right unary
261 operators, only LEFTARG should be defined, while for left
262 unary operators only RIGHTARG should be defined.
266 <replaceable class="parameter">func_name</replaceable> procedure must have
267 been previously defined using <command>CREATE FUNCTION</command> and must
268 be defined to accept the correct number of arguments
269 (either one or two) of the indicated types.
272 The commutator operator should be identified if one exists,
273 so that <productname>PostgreSQL</productname> can
274 reverse the order of the operands if it wishes.
275 For example, the operator area-less-than, <<<,
276 would probably have a commutator
277 operator, area-greater-than, >>>.
278 Hence, the query optimizer could freely convert:
281 box '((0,0), (1,1))' >>> MYBOXES.description
287 MYBOXES.description <<< box '((0,0), (1,1))'
291 This allows the execution code to always use the latter
292 representation and simplifies the query optimizer somewhat.
295 Similarly, if there is a negator operator then it should be
298 operator, area-equal, ===, exists, as well as an area not
300 The negator link allows the query optimizer to simplify
302 NOT MYBOXES.description === box '((0,0), (1,1))'
306 MYBOXES.description !== box '((0,0), (1,1))'
310 If a commutator operator name is supplied,
311 <productname>PostgreSQL</productname>
312 searches for it in the catalog. If it is found and it
313 does not yet have a commutator itself, then the commutator's
314 entry is updated to have the newly created operator as its
315 commutator. This applies to the negator, as well.
316 This is to allow the definition of two operators that are
317 the commutators or the negators of each other. The first
318 operator should be defined without a commutator or negator
319 (as appropriate). When the second operator is defined,
320 name the first as the commutator or negator. The first
321 will be updated as a side effect. (As of
322 <application>PostgreSQL</application> <literal>6.5</literal>,
323 it also works to just have both operators refer to each other.)
326 The HASHES, MERGES, SORT1, SORT2, LTCMP, and GTCMP options are present to
327 support the query optimizer in performing joins.
328 <productname>PostgreSQL</productname> can always evaluate a join (i.e.,
329 processing a clause with two tuple variables separated by an operator that
330 returns a <type>boolean</type>) by iterative substitution [WONG76]. In
331 addition, <productname>PostgreSQL</productname> can use a hash-join
332 algorithm along the lines of [SHAP86]; however, it must know whether this
333 strategy is applicable. The current hash-join algorithm is only correct
334 for operators that represent equality tests; furthermore, equality of the
335 data type must mean bitwise equality of the representation of the type.
336 (For example, a data type that contains unused bits that don't matter for
337 equality tests could not be hash-joined.) The HASHES flag indicates to the
338 query optimizer that a hash join may safely be used with this
342 Similarly, the MERGES flag indicates whether merge-sort is a usable join
343 strategy for this operator. A merge join requires that the two input
344 datatypes have consistent orderings, and that the mergejoin operator
345 behave like equality with respect to that ordering. For example, it is
346 possible to merge-join equality between an integer and a float variable by
347 sorting both inputs in ordinary
348 numeric order. Execution of a merge join requires that the system be
349 able to identify four operators related to the mergejoin equality operator:
350 less-than comparison for the left input datatype,
351 less-than comparison for the right input datatype,
352 less-than comparison between the two datatypes, and
353 greater-than comparison between the two datatypes. It is possible to
354 specify these by name, as the SORT1, SORT2, LTCMP, and GTCMP options
355 respectively. The system will fill in the default names <literal><</>,
356 <literal><</>, <literal><</>, <literal>></> respectively if
357 any of these are omitted when MERGES is specified. Also, MERGES will
358 be assumed to be implied if any of these four operator options appear.
361 If other join strategies are found to be practical,
362 <productname>PostgreSQL</productname>
363 will change the optimizer and run-time system to use
364 them and will require additional specification when an
365 operator is defined. Fortunately, the research community
366 invents new join strategies infrequently, and the added
367 generality of user-defined join strategies was not felt to
368 be worth the complexity involved.
371 The RESTRICT and JOIN options assist the query optimizer in estimating
372 result sizes. If a clause of the form:
374 MYBOXES.description <<< box '((0,0), (1,1))'
376 is present in the qualification,
377 then <productname>PostgreSQL</productname> may have to
378 estimate the fraction of the instances in MYBOXES that
379 satisfy the clause. The function
380 <replaceable class="parameter">res_proc</replaceable>
381 must be a registered function (meaning it is already defined using
382 <command>CREATE FUNCTION</command>) which accepts arguments of the correct
383 data types and returns a floating-point number. The
384 query optimizer simply calls this function, passing the
385 parameter <literal>((0,0), (1,1))</literal> and multiplies the result by the relation
386 size to get the expected number of instances.
389 Similarly, when the operands of the operator both contain
390 instance variables, the query optimizer must estimate the
391 size of the resulting join. The function join_proc will
392 return another floating-point number which will be multiplied
393 by the cardinalities of the two tables involved to
394 compute the expected result size.
397 The difference between the function
399 my_procedure_1 (MYBOXES.description, box '((0,0), (1,1))')
403 MYBOXES.description === box '((0,0), (1,1))'
405 is that <productname>PostgreSQL</productname>
406 attempts to optimize operators and can
407 decide to use an index to restrict the search space when
408 operators are involved. However, there is no attempt to
409 optimize functions, and they are performed by brute force.
410 Moreover, functions can have any number of arguments while
411 operators are restricted to one or two.
414 <refsect2 id="R2-SQL-CREATEOPERATOR-3">
416 <date>2000-03-25</date>
422 Refer to the chapter on operators in the
423 <citetitle>PostgreSQL User's Guide</citetitle>
424 for further information.
425 Refer to <command>DROP OPERATOR</command> to delete
426 user-defined operators from a database.
431 <refsect1 id="R1-SQL-CREATEOPERATOR-2">
435 <para>The following command defines a new operator,
436 area-equality, for the BOX data type:
439 CREATE OPERATOR === (
442 PROCEDURE = area_equal_procedure,
445 RESTRICT = area_restriction_procedure,
446 JOIN = area_join_procedure,
448 SORT1 = <<<,
450 -- Since sort operators were given, MERGES is implied.
451 -- LTCMP and GTCMP are assumed to be < and > respectively
456 <refsect1 id="R1-SQL-CREATEOPERATOR-3">
461 <refsect2 id="R2-SQL-CREATEOPERATOR-4">
463 <date>2000-03-25</date>
470 <command>CREATE OPERATOR</command>
471 is a <productname>PostgreSQL</productname> extension.
472 There is no <command>CREATE OPERATOR</command>
473 statement in <acronym>SQL92</acronym>.
479 <!-- Keep this comment at the end of the file
484 sgml-minimize-attributes:nil
485 sgml-always-quote-attributes:t
488 sgml-parent-document:nil
489 sgml-default-dtd-file:"../reference.ced"
490 sgml-exposed-tags:nil
491 sgml-local-catalogs:"/usr/lib/sgml/catalog"
492 sgml-local-ecat-files:nil