File: xoper.html

package info (click to toggle)
pgadmin3 1.4.3-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 29,796 kB
  • ctags: 10,758
  • sloc: cpp: 55,356; sh: 6,164; ansic: 1,520; makefile: 576; sql: 482; xml: 100; perl: 18
file content (74 lines) | stat: -rw-r--r-- 3,506 bytes parent folder | download
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>32.12.User-Defined Operators</title>
<link rel="stylesheet" href="stylesheet.css" type="text/css">
<link rev="made" href="pgsql-docs@postgresql.org">
<meta name="generator" content="DocBook XSL Stylesheets V1.70.0">
<link rel="start" href="index.html" title="PostgreSQL 8.1.4 Documentation">
<link rel="up" href="extend.html" title="Chapter32.Extending SQL">
<link rel="prev" href="xtypes.html" title="32.11.User-Defined Types">
<link rel="next" href="xoper-optimization.html" title="32.13.Operator Optimization Information">
<link rel="copyright" href="ln-legalnotice.html" title="Legal Notice">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="sect1" lang="en">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="xoper"></a>32.12.User-Defined Operators</h2></div></div></div>
<a name="id711351"></a><p>   Every operator is &#8220;<span class="quote">syntactic sugar</span>&#8221; 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 left unary, right
   unary, and binary operators.  Operators can be
   overloaded;<a name="id711392"></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 href="xtypes.html" title="32.11.User-Defined Types">Section32.11, &#8220;User-Defined Types&#8221;</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,
    procedure = 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 unary
   operators, just omit one of <code class="literal">leftarg</code> (for left unary) or
   <code class="literal">rightarg</code> (for right unary).  The <code class="literal">procedure</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></body>
</html>