summaryrefslogtreecommitdiffstats
path: root/doc/src/sgml/html/xoper.html
blob: ebd0f83c28fa196fbe823c2b21c21d5ea0f8afe7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>38.14. User-Defined Operators</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><link rel="prev" href="xtypes.html" title="38.13. User-Defined Types" /><link rel="next" href="xoper-optimization.html" title="38.15. Operator Optimization Information" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">38.14. User-Defined Operators</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="xtypes.html" title="38.13. User-Defined Types">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="extend.html" title="Chapter 38. Extending SQL">Up</a></td><th width="60%" align="center">Chapter 38. Extending <acronym class="acronym">SQL</acronym></th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 15.5 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="xoper-optimization.html" title="38.15. Operator Optimization Information">Next</a></td></tr></table><hr /></div><div class="sect1" id="XOPER"><div class="titlepage"><div><div><h2 class="title" style="clear: both">38.14. User-Defined Operators</h2></div></div></div><a id="id-1.8.3.17.2" class="indexterm"></a><p>
   Every operator is <span class="quote"><span class="quote">syntactic sugar</span></span> 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 <span class="emphasis"><em>not merely</em></span>
   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.
  </p><p>
   <span class="productname">PostgreSQL</span> supports prefix
   and infix operators.  Operators can be
   overloaded;<a id="id-1.8.3.17.4.2" class="indexterm"></a>
   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.
  </p><p>
   Here is an example of creating an operator for adding two complex
   numbers.  We assume we've already created the definition of type
   <code class="type">complex</code> (see <a class="xref" href="xtypes.html" title="38.13. User-Defined Types">Section 38.13</a>).  First we need a
   function that does the work, then we can define the operator:

</p><pre class="programlisting">
CREATE FUNCTION complex_add(complex, complex)
    RETURNS complex
    AS '<em class="replaceable"><code>filename</code></em>', 'complex_add'
    LANGUAGE C IMMUTABLE STRICT;

CREATE OPERATOR + (
    leftarg = complex,
    rightarg = complex,
    function = complex_add,
    commutator = +
);
</pre><p>
  </p><p>
   Now we could execute a query like this:

</p><pre class="screen">
SELECT (a + b) AS c FROM test_complex;

        c
-----------------
 (5.2,6.05)
 (133.42,144.95)
</pre><p>
  </p><p>
   We've shown how to create a binary operator here.  To create a prefix
   operator, just omit the <code class="literal">leftarg</code>.
   The <code class="literal">function</code>
   clause and the argument clauses are the only required items in
   <code class="command">CREATE OPERATOR</code>.  The <code class="literal">commutator</code>
   clause shown in the example is an optional hint to the query
   optimizer.  Further details about <code class="literal">commutator</code> and other
   optimizer hints appear in the next section.
  </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="xtypes.html" title="38.13. User-Defined Types">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="extend.html" title="Chapter 38. Extending SQL">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="xoper-optimization.html" title="38.15. Operator Optimization Information">Next</a></td></tr><tr><td width="40%" align="left" valign="top">38.13. User-Defined Types </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 15.5 Documentation">Home</a></td><td width="40%" align="right" valign="top"> 38.15. Operator Optimization Information</td></tr></table></div></body></html>