]> granicus.if.org Git - postgresql/blob - doc/src/sgml/xoper.sgml
Minor cleanup in markup, especially in the Output section.
[postgresql] / doc / src / sgml / xoper.sgml
1 <Chapter Id="xoper">
2 <Title>Extending <Acronym>SQL</Acronym>: Operators</Title>
3
4 <Para>
5      <ProductName>Postgres</ProductName> supports left unary, right  unary  and  binary
6      operators.   Operators  can  be  overloaded, or re-used
7      with different numbers  and  types  of  arguments.   If
8      there  is  an ambiguous situation and the system cannot
9      determine the correct operator to use, it  will  return
10      an  error  and you may have to typecast the left and/or
11      right operands to help it understand which operator you
12      meant to use.
13      To  create  an  operator for adding two complex numbers
14      can be done as follows.  First  we  need  to  create  a
15      function  to add the new types. Then, we can create the
16      operator with the function.
17
18 <ProgramListing>
19          CREATE FUNCTION complex_add(complex, complex)
20             RETURNS complex
21             AS '$PWD/obj/complex.so'
22             LANGUAGE 'c';
23
24          CREATE OPERATOR + (
25             leftarg = complex,
26             rightarg = complex,
27             procedure = complex_add,
28             commutator = +
29          );
30 </ProgramListing>
31 </Para>
32
33 <Para>
34      We've shown how to create a binary  operator  here.  To
35      create  unary  operators, just omit one of leftarg (for
36      left unary) or rightarg (for right unary).
37      If we give the system enough type information,  it  can
38      automatically figure out which operators to use.
39      
40 <ProgramListing>
41          SELECT (a + b) AS c FROM test_complex;
42
43          +----------------+
44          |c               |
45          +----------------+
46          |(5.2,6.05)      |
47          +----------------+
48          |(133.42,144.95) |
49          +----------------+
50 </ProgramListing>
51 </Para>
52 </Chapter>