⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xoper.sgml

📁 PostgreSQL 8.1.4的源码 适用于Linux下的开源数据库系统
💻 SGML
📖 第 1 页 / 共 2 页
字号:
<!--$PostgreSQL: pgsql/doc/src/sgml/xoper.sgml,v 1.34 2005/11/04 23:14:02 petere Exp $--> <sect1 id="xoper">  <title>User-Defined Operators</title>  <indexterm zone="xoper">   <primary>operator</primary>   <secondary>user-defined</secondary>  </indexterm>  <para>   Every operator is <quote>syntactic sugar</quote> for a call to an   underlying function that does the real work; so you must   first create the underlying function before you can create   the operator.  However, an operator is <emphasis>not merely</emphasis>   syntactic sugar, because it carries additional information   that helps the query planner optimize queries that use the   operator.  The next section will be devoted to explaining   that additional information.  </para>  <para>   <productname>PostgreSQL</productname> supports left unary, right   unary, and binary operators.  Operators can be   overloaded;<indexterm><primary>overloading</primary><secondary>operators</secondary></indexterm>   that is, the same operator name can be used for different operators   that have different numbers and types of operands.  When a query is   executed, the system determines the operator to call from the   number and types of the provided operands.  </para>  <para>   Here is an example of creating an operator for adding two complex   numbers.  We assume we've already created the definition of type   <type>complex</type> (see <xref linkend="xtypes">).  First we need a   function that does the work, then we can define the operator:<programlisting>CREATE FUNCTION complex_add(complex, complex)    RETURNS complex    AS '<replaceable>filename</replaceable>', 'complex_add'    LANGUAGE C IMMUTABLE STRICT;CREATE OPERATOR + (    leftarg = complex,    rightarg = complex,    procedure = complex_add,    commutator = +);</programlisting>  </para>  <para>   Now we could execute a query like this:     <screen>SELECT (a + b) AS c FROM test_complex;        c----------------- (5.2,6.05) (133.42,144.95)</screen>  </para>  <para>   We've shown how to create a binary operator here.  To create unary   operators, just omit one of <literal>leftarg</> (for left unary) or   <literal>rightarg</> (for right unary).  The <literal>procedure</>   clause and the argument clauses are the only required items in   <command>CREATE OPERATOR</command>.  The <literal>commutator</>   clause shown in the example is an optional hint to the query   optimizer.  Further details about <literal>commutator</> and other   optimizer hints appear in the next section.  </para> </sect1>  <sect1 id="xoper-optimization">   <title>Operator Optimization Information</title>   <para>    A <productname>PostgreSQL</productname> operator definition can include    several optional clauses that tell the system useful things about how    the operator behaves.  These clauses should be provided whenever    appropriate, because they can make for considerable speedups in execution    of queries that use the operator.  But if you provide them, you must be    sure that they are right!  Incorrect use of an optimization clause can    result in server process crashes, subtly wrong output, or other Bad Things.    You can always leave out an optimization clause if you are not sure    about it; the only consequence is that queries might run slower than    they need to.   </para>   <para>    Additional optimization clauses might be added in future versions of    <productname>PostgreSQL</productname>.  The ones described here are all    the ones that release &version; understands.   </para>   <sect2>    <title><literal>COMMUTATOR</></title>    <para>     The <literal>COMMUTATOR</> clause, if provided, names an operator that is the     commutator of the operator being defined.  We say that operator A is the     commutator of operator B if (x A y) equals (y B x) for all possible input     values x, y.  Notice that B is also the commutator of A.  For example,     operators <literal>&lt;</> and <literal>&gt;</> for a particular data type are usually each others'     commutators, and operator <literal>+</> is usually commutative with itself.     But operator <literal>-</> is usually not commutative with anything.    </para>    <para>     The left operand type of a commutable operator is the same as the     right operand type of its commutator, and vice versa.  So the name of     the commutator operator is all that <productname>PostgreSQL</productname>     needs to be given to look up the commutator, and that's all that needs to     be provided in the <literal>COMMUTATOR</> clause.    </para>    <para>     It's critical to provide commutator information for operators that     will be used in indexes and join clauses, because this allows the     query optimizer to <quote>flip around</> such a clause to the forms     needed for different plan types.  For example, consider a query with     a WHERE clause like <literal>tab1.x = tab2.y</>, where <literal>tab1.x</>     and <literal>tab2.y</> are of a user-defined type, and suppose that     <literal>tab2.y</> is indexed.  The optimizer cannot generate an     index scan unless it can determine how to flip the clause around to     <literal>tab2.y = tab1.x</>, because the index-scan machinery expects     to see the indexed column on the left of the operator it is given.     <productname>PostgreSQL</productname> will <emphasis>not</> simply     assume that this is a valid transformation &mdash; the creator of the     <literal>=</> operator must specify that it is valid, by marking the     operator with commutator information.    </para>    <para>     When you are defining a self-commutative operator, you just do it.     When you are defining a pair of commutative operators, things are     a little trickier: how can the first one to be defined refer to the     other one, which you haven't defined yet?  There are two solutions     to this problem:     <itemizedlist>      <listitem>       <para>	One way is to omit the <literal>COMMUTATOR</> clause in the first operator that	you define, and then provide one in the second operator's definition.	Since <productname>PostgreSQL</productname> knows that commutative	operators come in pairs, when it sees the second definition it will	automatically go back and fill in the missing <literal>COMMUTATOR</> clause in	the first definition.       </para>      </listitem>      <listitem>       <para>	The other, more straightforward way is just to include <literal>COMMUTATOR</> clauses	in both definitions.  When <productname>PostgreSQL</productname> processes	the first definition and realizes that <literal>COMMUTATOR</> refers to a nonexistent	operator, the system will make a dummy entry for that operator in the	system catalog.  This dummy entry will have valid data only	for the operator name, left and right operand types, and result type,	since that's all that <productname>PostgreSQL</productname> can deduce	at this point.  The first operator's catalog entry will link to this	dummy entry.  Later, when you define the second operator, the system	updates the dummy entry with the additional information from the second	definition.  If you try to use the dummy operator before it's been filled	in, you'll just get an error message.       </para>      </listitem>     </itemizedlist>    </para>   </sect2>   <sect2>    <title><literal>NEGATOR</></title>    <para>     The <literal>NEGATOR</> clause, if provided, names an operator that is the     negator of the operator being defined.  We say that operator A     is the negator of operator B if both return Boolean results and     (x A y) equals NOT (x B y) for all possible inputs x, y.     Notice that B is also the negator of A.     For example, <literal>&lt;</> and <literal>&gt;=</> are a negator pair for most data types.     An operator can never validly be its own negator.    </para>   <para>    Unlike commutators, a pair of unary operators could validly be marked    as each others' negators; that would mean (A x) equals NOT (B x)    for all x, or the equivalent for right unary operators.   </para>   <para>    An operator's negator must have the same left and/or right operand types    as the operator to be defined, so just as with <literal>COMMUTATOR</>, only the operator    name need be given in the <literal>NEGATOR</> clause.   </para>   <para>    Providing a negator is very helpful to the query optimizer since    it allows expressions like <literal>NOT (x = y)</> to be simplified into    <literal>x &lt;&gt; y</>.  This comes up more often than you might think, because    <literal>NOT</> operations can be inserted as a consequence of other rearrangements.   </para>   <para>    Pairs of negator operators can be defined using the same methods    explained above for commutator pairs.   </para>  </sect2>  <sect2>   <title><literal>RESTRICT</></title>   <para>    The <literal>RESTRICT</> clause, if provided, names a restriction selectivity    estimation function for the operator.  (Note that this is a function    name, not an operator name.)  <literal>RESTRICT</> clauses only make sense for    binary operators that return <type>boolean</>.  The idea behind a restriction    selectivity estimator is to guess what fraction of the rows in a    table will satisfy a <literal>WHERE</literal>-clause condition of the form<programlisting>column OP constant</programlisting>    for the current operator and a particular constant value.    This assists the optimizer by    giving it some idea of how many rows will be eliminated by <literal>WHERE</>    clauses that have this form.  (What happens if the constant is on    the left, you may be wondering?  Well, that's one of the things that    <literal>COMMUTATOR</> is for...)   </para>   <para>    Writing new restriction selectivity estimation functions is far beyond    the scope of this chapter, but fortunately you can usually just use    one of the system's standard estimators for many of your own operators.    These are the standard restriction estimators:    <simplelist>     <member><function>eqsel</>	for <literal>=</></member>     <member><function>neqsel</> for <literal>&lt;&gt;</></member>     <member><function>scalarltsel</> for <literal>&lt;</> or <literal>&lt;=</></member>     <member><function>scalargtsel</> for <literal>&gt;</> or <literal>&gt;=</></member>   </simplelist>    It might seem a little odd that these are the categories, but they    make sense if you think about it.  <literal>=</> will typically accept only    a small fraction of the rows in a table; <literal>&lt;&gt;</> will typically reject    only a small fraction.  <literal>&lt;</> will accept a fraction that depends on    where the given constant falls in the range of values for that table    column (which, it just so happens, is information collected by    <command>ANALYZE</command> and made available to the selectivity estimator).    <literal>&lt;=</> will accept a slightly larger fraction than <literal>&lt;</> for the same    comparison constant, but they're close enough to not be worth    distinguishing, especially since we're not likely to do better than a    rough guess anyhow.  Similar remarks apply to <literal>&gt;</> and <literal>&gt;=</>.   </para>   <para>    You can frequently get away with using either <function>eqsel</function> or <function>neqsel</function> for

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -