1 <refentry id="SQL-CREATEOPERATOR">
6 <refmiscinfo>SQL - Language Statements</refmiscinfo>
13 Defines a new user operator
18 <date>1999-04-14</date>
21 CREATE OPERATOR <replaceable>name</replaceable> (
22 PROCEDURE = <replaceable class="parameter">func_name</replaceable>
23 [, LEFTARG = <replaceable class="parameter">type1</replaceable> ]
24 [, RIGHTARG = <replaceable class="parameter">type2</replaceable> ]
25 [, COMMUTATOR = <replaceable class="parameter">com_op</replaceable> ]
26 [, NEGATOR = <replaceable class="parameter">neg_op</replaceable> ]
27 [, RESTRICT = <replaceable class="parameter">res_proc</replaceable> ]
28 [, JOIN = <replaceable class="parameter">join_proc</replaceable> ]
30 [, SORT1 = <replaceable class="parameter">left_sort_op</replaceable> ]
31 [, SORT2 = <replaceable class="parameter">right_sort_op</replaceable> ]
35 <refsect2 id="R2-SQL-CREATEOPERATOR-1">
37 <date>1999-04-14</date>
46 <term><replaceable class="parameter">name</replaceable></term>
49 The operator to be defined. See below for allowable characters.
54 <term><replaceable class="parameter">func_name</replaceable></term>
57 The function used to implement this operator.
62 <term><replaceable class="parameter">type1</replaceable></term>
65 The type for the left-hand side of the operator, if any. This option would be
66 omitted for a right-unary operator.
71 <term><replaceable class="parameter">type2</replaceable></term>
74 The type for the right-hand side of the operator, if any. This option would be
75 omitted for a left-unary operator.
80 <term><replaceable class="parameter">com_op</replaceable></term>
83 The commutator for this operator.
88 <term><replaceable class="parameter">neg_op</replaceable></term>
91 The negator of this operator.
96 <term><replaceable class="parameter">res_proc</replaceable></term>
99 The restriction selectivity estimator function for this operator.
104 <term><replaceable class="parameter">join_proc</replaceable></term>
107 The join selectivity estimator function for this operator.
115 Indicates this operator can support a hash-join algorithm.
120 <term><replaceable class="parameter">left_sort_op</replaceable></term>
123 Operator that sorts the left-hand data type of this operator.
128 <term><replaceable class="parameter">right_sort_op</replaceable></term>
131 Operator that sorts the right-hand data type of this operator.
139 <refsect2 id="R2-SQL-CREATEOPERATOR-2">
141 <date>1999-04-14</date>
149 <term><computeroutput>
151 </computeroutput></term>
154 Message returned if the operator is successfully created.
163 <refsect1 id="R1-SQL-CREATEOPERATOR-1">
165 <date>1999-04-14</date>
171 <command>CREATE OPERATOR</command> defines a new operator,
172 <replaceable class="parameter">name</replaceable>.
173 The user who defines an operator becomes its owner.
176 The operator <replaceable class="parameter">name</replaceable>
177 is a sequence of up to thirty two (32) characters in any combination
180 + - * / < > = ~ ! @ # % ^ & | ` ? $ :
184 No alphabetic characters are allowed in an operator name.
185 This enables <productname>Postgres</productname> to parse SQL input
186 into tokens without requiring spaces between each token.
191 The operator "!=" is mapped to "<>" on input, so they are
192 therefore equivalent.
195 At least one of LEFTARG and RIGHTARG must be defined. For
196 binary operators, both should be defined. For right unary
197 operators, only LEFTARG should be defined, while for left
198 unary operators only RIGHTARG should be defined.
202 <replaceable class="parameter">func_name</replaceable> procedure must have
203 been previously defined using <command>CREATE FUNCTION</command> and must
204 be defined to accept the correct number of arguments
208 The commutator operator should be identified if one exists,
209 so that <productname>Postgres</productname> can
210 reverse the order of the operands if it wishes.
211 For example, the operator area-less-than, <<<,
212 would probably have a commutator
213 operator, area-greater-than, >>>.
214 Hence, the query optimizer could freely convert:
217 "0,0,1,1"::box >>> MYBOXES.description
223 MYBOXES.description <<< "0,0,1,1"::box
227 This allows the execution code to always use the latter
228 representation and simplifies the query optimizer somewhat.
231 Similarly, if there is a negator operator then it should be
234 operator, area-equal, ===, exists, as well as an area not
236 The negator link allows the query optimizer to simplify
238 NOT MYBOXES.description === "0,0,1,1"::box
242 MYBOXES.description !== "0,0,1,1"::box
246 If a commutator operator name is supplied,
247 <productname>Postgres</productname>
248 searches for it in the catalog. If it is found and it
249 does not yet have a commutator itself, then the commutator's
250 entry is updated to have the newly created operator as its
251 commutator. This applies to the negator, as well.
254 This is to allow the definition of two operators that are
255 the commutators or the negators of each other. The first
256 operator should be defined without a commutator or negator
257 (as appropriate). When the second operator is defined,
258 name the first as the commutator or negator. The first
259 will be updated as a side effect. (As of Postgres 6.5,
260 it also works to just have both operators refer to each other.)
263 The next three specifications are present to support the
264 query optimizer in performing joins.
265 <productname>Postgres</productname> can always
266 evaluate a join (i.e., processing a clause with two tuple
267 variables separated by an operator that returns a boolean)
268 by iterative substitution [WONG76].
269 In addition, <productname>Postgres</productname>
270 can use a hash-join algorithm along
271 the lines of [SHAP86]; however, it must know whether this
272 strategy is applicable. The current hash-join algorithm
273 is only correct for operators that represent equality tests;
274 furthermore, equality of the datatype must mean bitwise equality
275 of the representation of the type. (For example, a datatype that
276 contains unused bits that don't matter for equality tests could
278 The HASHES flag indicates to the query optimizer that a hash join
279 may safely be used with this operator.</para>
281 Similarly, the two sort operators indicate to the query
282 optimizer whether merge-sort is a usable join strategy and
283 which operators should be used to sort the two operand
284 classes. Sort operators should only be provided for an equality
285 operator, and they should refer to less-than operators for the
286 left and right side data types respectively.
289 If other join strategies are found to be practical,
290 <productname>Postgres</productname>
291 will change the optimizer and run-time system to use
292 them and will require additional specification when an
293 operator is defined. Fortunately, the research community
294 invents new join strategies infrequently, and the added
295 generality of user-defined join strategies was not felt to
296 be worth the complexity involved.
299 The last two pieces of the specification are present so
300 the query optimizer can estimate result sizes. If a
303 MYBOXES.description <<< "0,0,1,1"::box
305 is present in the qualification,
306 then <productname>Postgres</productname> may have to
307 estimate the fraction of the instances in MYBOXES that
308 satisfy the clause. The function
309 <replaceable class="parameter">res_proc</replaceable>
310 must be a registered function (meaning it is already defined using
311 <command>CREATE FUNCTION</command>) which accepts arguments of the correct
312 data types and returns a floating point number. The
313 query optimizer simply calls this function, passing the
314 parameter "0,0,1,1" and multiplies the result by the relation
315 size to get the desired expected number of instances.
318 Similarly, when the operands of the operator both contain
319 instance variables, the query optimizer must estimate the
320 size of the resulting join. The function join_proc will
321 return another floating point number which will be multiplied
322 by the cardinalities of the two classes involved to
323 compute the desired expected result size.
326 The difference between the function
328 my_procedure_1 (MYBOXES.description, "0,0,1,1"::box)
332 MYBOXES.description === "0,0,1,1"::box
334 is that <productname>Postgres</productname>
335 attempts to optimize operators and can
336 decide to use an index to restrict the search space when
337 operators are involved. However, there is no attempt to
338 optimize functions, and they are performed by brute force.
339 Moreover, functions can have any number of arguments while
340 operators are restricted to one or two.
343 <refsect2 id="R2-SQL-CREATEOPERATOR-3">
345 <date>1999-04-14</date>
351 Refer to the chapter on operators in the
352 <citetitle>PostgreSQL User's Guide</citetitle>
353 for further information.
354 Refer to <command>DROP OPERATOR</command> to delete
355 user-defined operators from a database.
360 <refsect1 id="R1-SQL-CREATEOPERATOR-2">
364 <para>The following command defines a new operator,
365 area-equality, for the BOX data type.
368 CREATE OPERATOR === (
371 PROCEDURE = area_equal_procedure,
374 RESTRICT = area_restriction_procedure,
375 JOIN = area_join_procedure,
383 <refsect1 id="R1-SQL-CREATEOPERATOR-3">
388 CREATE OPERATOR is a <productname>Postgres</productname> extension.
391 <refsect2 id="R2-SQL-CREATEOPERATOR-4">
393 <date>1999-04-14</date>
399 There is no CREATE OPERATOR statement in <acronym>SQL92</acronym>.
405 <!-- Keep this comment at the end of the file
410 sgml-minimize-attributes:nil
411 sgml-always-quote-attributes:t
414 sgml-parent-document:nil
415 sgml-default-dtd-file:"../reference.ced"
416 sgml-exposed-tags:nil
417 sgml-local-catalogs:"/usr/lib/sgml/catalog"
418 sgml-local-ecat-files:nil