📄 xoper.sgml
字号:
operators that have very high or very low selectivity, even if they aren't really equality or inequality. For example, the approximate-equality geometric operators use <function>eqsel</function> on the assumption that they'll usually only match a small fraction of the entries in a table. </para> <para> You can use <function>scalarltsel</> and <function>scalargtsel</> for comparisons on data types that have some sensible means of being converted into numeric scalars for range comparisons. If possible, add the data type to those understood by the function <function>convert_to_scalar()</function> in <filename>src/backend/utils/adt/selfuncs.c</filename>. (Eventually, this function should be replaced by per-data-type functions identified through a column of the <classname>pg_type</> system catalog; but that hasn't happened yet.) If you do not do this, things will still work, but the optimizer's estimates won't be as good as they could be. </para> <para> There are additional selectivity estimation functions designed for geometric operators in <filename>src/backend/utils/adt/geo_selfuncs.c</filename>: <function>areasel</function>, <function>positionsel</function>, and <function>contsel</function>. At this writing these are just stubs, but you may want to use them (or even better, improve them) anyway. </para> </sect2> <sect2> <title><literal>JOIN</></title> <para> The <literal>JOIN</> clause, if provided, names a join selectivity estimation function for the operator. (Note that this is a function name, not an operator name.) <literal>JOIN</> clauses only make sense for binary operators that return <type>boolean</type>. The idea behind a join selectivity estimator is to guess what fraction of the rows in a pair of tables will satisfy a <literal>WHERE</>-clause condition of the form<programlisting>table1.column1 OP table2.column2</programlisting> for the current operator. As with the <literal>RESTRICT</literal> clause, this helps the optimizer very substantially by letting it figure out which of several possible join sequences is likely to take the least work. </para> <para> As before, this chapter will make no attempt to explain how to write a join selectivity estimator function, but will just suggest that you use one of the standard estimators if one is applicable: <simplelist> <member><function>eqjoinsel</> for <literal>=</></member> <member><function>neqjoinsel</> for <literal><></></member> <member><function>scalarltjoinsel</> for <literal><</> or <literal><=</></member> <member><function>scalargtjoinsel</> for <literal>></> or <literal>>=</></member> <member><function>areajoinsel</> for 2D area-based comparisons</member> <member><function>positionjoinsel</> for 2D position-based comparisons</member> <member><function>contjoinsel</> for 2D containment-based comparisons</member> </simplelist> </para> </sect2> <sect2> <title><literal>HASHES</></title> <para> The <literal>HASHES</literal> clause, if present, tells the system that it is permissible to use the hash join method for a join based on this operator. <literal>HASHES</> only makes sense for a binary operator that returns <literal>boolean</>, and in practice the operator had better be equality for some data type. </para> <para> The assumption underlying hash join is that the join operator can only return true for pairs of left and right values that hash to the same hash code. If two values get put in different hash buckets, the join will never compare them at all, implicitly assuming that the result of the join operator must be false. So it never makes sense to specify <literal>HASHES</literal> for operators that do not represent equality. </para> <para> To be marked <literal>HASHES</literal>, the join operator must appear in a hash index operator class. This is not enforced when you create the operator, since of course the referencing operator class couldn't exist yet. But attempts to use the operator in hash joins will fail at run time if no such operator class exists. The system needs the operator class to find the data-type-specific hash function for the operator's input data type. Of course, you must also supply a suitable hash function before you can create the operator class. </para> <para> Care should be exercised when preparing a hash function, because there are machine-dependent ways in which it might fail to do the right thing. For example, if your data type is a structure in which there may be uninteresting pad bits, you can't simply pass the whole structure to <function>hash_any</>. (Unless you write your other operators and functions to ensure that the unused bits are always zero, which is the recommended strategy.) Another example is that on machines that meet the <acronym>IEEE</> floating-point standard, negative zero and positive zero are different values (different bit patterns) but they are defined to compare equal. If a float value might contain negative zero then extra steps are needed to ensure it generates the same hash value as positive zero. </para> <note> <para> The function underlying a hash-joinable operator must be marked immutable or stable. If it is volatile, the system will never attempt to use the operator for a hash join. </para> </note> <note> <para> If a hash-joinable operator has an underlying function that is marked strict, the function must also be complete: that is, it should return true or false, never null, for any two nonnull inputs. If this rule is not followed, hash-optimization of <literal>IN</> operations may generate wrong results. (Specifically, <literal>IN</> might return false where the correct answer according to the standard would be null; or it might yield an error complaining that it wasn't prepared for a null result.) </para> </note> </sect2> <sect2> <title><literal>MERGES</> (<literal>SORT1</>, <literal>SORT2</>, <literal>LTCMP</>, <literal>GTCMP</>)</title> <para> The <literal>MERGES</literal> clause, if present, tells the system that it is permissible to use the merge-join method for a join based on this operator. <literal>MERGES</> only makes sense for a binary operator that returns <literal>boolean</>, and in practice the operator must represent equality for some data type or pair of data types. </para> <para> Merge join is based on the idea of sorting the left- and right-hand tables into order and then scanning them in parallel. So, both data types must be capable of being fully ordered, and the join operator must be one that can only succeed for pairs of values that fall at the <quote>same place</> in the sort order. In practice this means that the join operator must behave like equality. But unlike hash join, where the left and right data types had better be the same (or at least bitwise equivalent), it is possible to merge-join two distinct data types so long as they are logically compatible. For example, the <type>smallint</type>-versus-<type>integer</type> equality operator is merge-joinable. We only need sorting operators that will bring both data types into a logically compatible sequence. </para> <para> Execution of a merge join requires that the system be able to identify four operators related to the merge-join equality operator: less-than comparison for the left operand data type, less-than comparison for the right operand data type, less-than comparison between the two data types, and greater-than comparison between the two data types. (These are actually four distinct operators if the merge-joinable operator has two different operand data types; but when the operand types are the same the three less-than operators are all the same operator.) It is possible to specify these operators individually by name, as the <literal>SORT1</>, <literal>SORT2</>, <literal>LTCMP</>, and <literal>GTCMP</> options respectively. The system will fill in the default names <literal><</>, <literal><</>, <literal><</>, <literal>></> respectively if any of these are omitted when <literal>MERGES</> is specified. Also, <literal>MERGES</> will be assumed to be implied if any of these four operator options appear, so it is possible to specify just some of them and let the system fill in the rest. </para> <para> The operand data types of the four comparison operators can be deduced from the operand types of the merge-joinable operator, so just as with <literal>COMMUTATOR</>, only the operator names need be given in these clauses. Unless you are using peculiar choices of operator names, it's sufficient to write <literal>MERGES</> and let the system fill in the details. (As with <literal>COMMUTATOR</> and <literal>NEGATOR</>, the system is able to make dummy operator entries if you happen to define the equality operator before the other ones.) </para> <para> There are additional restrictions on operators that you mark merge-joinable. These restrictions are not currently checked by <command>CREATE OPERATOR</command>, but errors may occur when the operator is used if any are not true: <itemizedlist> <listitem> <para> A merge-joinable equality operator must have a merge-joinable commutator (itself if the two operand data types are the same, or a related equality operator if they are different). </para> </listitem> <listitem> <para> If there is a merge-joinable operator relating any two data types A and B, and another merge-joinable operator relating B to any third data type C, then A and C must also have a merge-joinable operator; in other words, having a merge-joinable operator must be transitive. </para> </listitem> <listitem> <para> Bizarre results will ensue at run time if the four comparison operators you name do not sort the data values compatibly. </para> </listitem> </itemizedlist> </para> <note> <para> The function underlying a merge-joinable operator must be marked immutable or stable. If it is volatile, the system will never attempt to use the operator for a merge join. </para> </note> <note> <para> In <productname>PostgreSQL</productname> versions before 7.3, the <literal>MERGES</> shorthand was not available: to make a merge-joinable operator one had to write both <literal>SORT1</> and <literal>SORT2</> explicitly. Also, the <literal>LTCMP</> and <literal>GTCMP</> options did not exist; the names of those operators were hardwired as <literal><</> and <literal>></> respectively. </para> </note> </sect2> </sect1><!-- Keep this comment at the end of the fileLocal variables:mode:sgmlsgml-omittag:nilsgml-shorttag:tsgml-minimize-attributes:nilsgml-always-quote-attributes:tsgml-indent-step:1sgml-indent-data:tsgml-parent-document:nilsgml-default-dtd-file:"./reference.ced"sgml-exposed-tags:nilsgml-local-catalogs:("/usr/lib/sgml/catalog")sgml-local-ecat-files:nilEnd:-->
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -