Home Explore Blog CI



postgresql

1st chunk of `doc/src/sgml/xoper.sgml`
ef61f65b3e3653017a784420565930aeade949d72d6988aa0000000100000fa7
<!-- doc/src/sgml/xoper.sgml -->

 <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 prefix
   and infix 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,
    function = 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 a prefix
   operator, just omit the <literal>leftarg</literal>.
   The <literal>function</literal>
   clause and the argument clauses are the only required items in
   <command>CREATE OPERATOR</command>.  The <literal>commutator</literal>
   clause shown in the example is an optional hint to the query
   optimizer.  Further details about <literal>commutator</literal> and other
   optimizer hints appear in the next section.
  </para>
 </sect1>

  <sect1 id="xoper-optimization">
   <title>Operator Optimization Information</title>

  <indexterm zone="xoper-optimization">
   <primary>optimization information</primary>
   <secondary>for operators</secondary>
  </indexterm>

   <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 slow queries, 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>

   <para>
    It is also possible to attach a planner support function to the function
    that underlies an operator, providing another way of telling the system
    about the behavior of the operator.
    See <xref linkend="xfunc-optimization"/> for more information.
   </para>

   <sect2 id="xoper-commutator">
    <title><literal>COMMUTATOR</literal></title>

Title: User-Defined Operators in PostgreSQL
Summary
This section explains how to create user-defined operators in PostgreSQL, including prefix and infix operators, and how to optimize their performance using optional clauses such as commutator, with examples of creating and using custom operators.