1- <!-- $PostgreSQL: pgsql/doc/src/sgml/xoper.sgml,v 1.41 2007/02/01 19:10:24 momjian Exp $ -->
1+ <!-- $PostgreSQL: pgsql/doc/src/sgml/xoper.sgml,v 1.42 2007/02/06 04:38:31 tgl Exp $ -->
22
33 <sect1 id="xoper">
44 <title>User-Defined Operators</title>
@@ -85,7 +85,7 @@ SELECT (a + b) AS c FROM test_complex;
8585 appropriate, because they can make for considerable speedups in execution
8686 of queries that use the operator. But if you provide them, you must be
8787 sure that they are right! Incorrect use of an optimization clause can
88- result inserver process crashes , subtly wrong output, or other Bad Things.
88+ result inslow queries , subtly wrong output, or other Bad Things.
8989 You can always leave out an optimization clause if you are not sure
9090 about it; the only consequence is that queries might run slower than
9191 they need to.
@@ -326,8 +326,8 @@ table1.column1 OP table2.column2
326326 The <literal>HASHES</literal> clause, if present, tells the system that
327327 it is permissible to use the hash join method for a join based on this
328328 operator. <literal>HASHES</> only makes sense for a binary operator that
329- returns <literal>boolean</>, and in practice the operatorhad better be
330- equality for some data type.
329+ returns <literal>boolean</>, and in practice the operatormust represent
330+ equality for some data type or pair of data types .
331331 </para>
332332
333333 <para>
@@ -337,7 +337,13 @@ table1.column1 OP table2.column2
337337 join will never compare them at all, implicitly assuming that the
338338 result of the join operator must be false. So it never makes sense
339339 to specify <literal>HASHES</literal> for operators that do not represent
340- some form of equality.
340+ some form of equality. In most cases it is only practical to support
341+ hashing for operators that take the same data type on both sides.
342+ However, sometimes it is possible to design compatible hash functions
343+ for two or more datatypes; that is, functions that will generate the
344+ same hash codes for <quote>equal</> values, even though the values
345+ have different representations. For example, it's fairly simple
346+ to arrange this property when hashing integers of different widths.
341347 </para>
342348
343349 <para>
@@ -346,9 +352,9 @@ table1.column1 OP table2.column2
346352 the operator, since of course the referencing operator family couldn't
347353 exist yet. But attempts to use the operator in hash joins will fail
348354 at run time if no such operator family exists. The system needs the
349- operator family to find the data-type-specific hash function for the
350- operator's input data type. Of course, you must also create a suitable
351- hashfunction before you can create the operator family.
355+ operator family to find the data-type-specific hash function(s) for the
356+ operator's input data type(s) . Of course, you must also create suitable
357+ hashfunctions before you can create the operator family.
352358 </para>
353359
354360 <para>
@@ -366,6 +372,17 @@ table1.column1 OP table2.column2
366372 to ensure it generates the same hash value as positive zero.
367373 </para>
368374
375+ <para>
376+ A hash-joinable operator must have a commutator (itself if the two
377+ operand data types are the same, or a related equality operator
378+ if they are different) that appears in the same operator family.
379+ If this is not the case, planner errors might occur when the operator
380+ is used. Also, it is a good idea (but not strictly required) for
381+ a hash operator family that supports multiple datatypes to provide
382+ equality operators for every combination of the datatypes; this
383+ allows better optimization.
384+ </para>
385+
369386 <note>
370387 <para>
371388 The function underlying a hash-joinable operator must be marked