File: sql-createaggregate.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 (199 lines) | stat: -rw-r--r-- 11,244 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>CREATE AGGREGATE</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="sql-commands.html" title="SQL Commands">
<link rel="prev" href="sql-copy.html" title="COPY">
<link rel="next" href="sql-createcast.html" title="CREATE CAST">
<link rel="copyright" href="ln-legalnotice.html" title="Legal Notice">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="refentry" lang="en">
<a name="sql-createaggregate"></a><div class="titlepage"></div>
<div class="refnamediv">
<h2>Name</h2>
<p>CREATE AGGREGATE &#8212; define a new aggregate function</p>
</div>
<a name="id752318"></a><div class="refsynopsisdiv">
<h2>Synopsis</h2>
<pre class="synopsis">CREATE AGGREGATE <em class="replaceable"><code>name</code></em> (
    BASETYPE = <em class="replaceable"><code>input_data_type</code></em>,
    SFUNC = <em class="replaceable"><code>sfunc</code></em>,
    STYPE = <em class="replaceable"><code>state_data_type</code></em>
    [ , FINALFUNC = <em class="replaceable"><code>ffunc</code></em> ]
    [ , INITCOND = <em class="replaceable"><code>initial_condition</code></em> ]
    [ , SORTOP = <em class="replaceable"><code>sort_operator</code></em> ]
)</pre>
</div>
<div class="refsect1" lang="en">
<a name="id752384"></a><h2>Description</h2>
<p>   <code class="command">CREATE AGGREGATE</code> defines a new aggregate
   function. Some basic and commonly-used aggregate functions are
   included with the distribution; they are documented in <a href="functions-aggregate.html" title="9.15.Aggregate Functions">Section9.15, &#8220;Aggregate Functions&#8221;</a>. If one defines new types or needs
   an aggregate function not already provided, then <code class="command">CREATE
   AGGREGATE</code> can be used to provide the desired features.
  </p>
<p>   If a schema name is given (for example, <code class="literal">CREATE AGGREGATE
   myschema.myagg ...</code>) then the aggregate function is created in the
   specified schema.  Otherwise it is created in the current schema.
  </p>
<p>   An  aggregate  function is identified by its name and input data type.
   Two aggregates in the same schema can have the same name if they operate on
   different input types.  The
   name and input data type of an aggregate must also be distinct from
   the name and input data type(s) of every ordinary function in the same
   schema.
  </p>
<p>   An  aggregate function is made from one or two ordinary
   functions:
   a state transition function
   <em class="replaceable"><code>sfunc</code></em>,
   and an optional final calculation function
   <em class="replaceable"><code>ffunc</code></em>.
   These are used as follows:
</p>
<pre class="programlisting"><em class="replaceable"><code>sfunc</code></em>( internal-state, next-data-item ) ---&gt; next-internal-state
<em class="replaceable"><code>ffunc</code></em>( internal-state ) ---&gt; aggregate-value</pre>
<p>
  </p>
<p>   <span class="productname">PostgreSQL</span> creates a temporary variable
   of data type <em class="replaceable"><code>stype</code></em>
   to hold the current internal state of the aggregate.  At each input
   data item,
   the state transition function is invoked to calculate a new
   internal state value.  After all the data has been processed,
   the final function is invoked once to calculate the aggregate's return
   value.  If there is no final function then the ending state value
   is returned as-is.
  </p>
<p>   An aggregate function may provide an initial condition,
   that is, an initial value for the internal state value.
   This is specified and stored in the database as a column of type
   <code class="type">text</code>, but it must be a valid external representation
   of a constant of the state value data type.  If it is not supplied
   then the state value starts out null.
  </p>
<p>   If the state transition function is declared &#8220;<span class="quote">strict</span>&#8221;,
   then it cannot be called with null inputs.  With such a transition
   function, aggregate execution behaves as follows.  Null input values
   are ignored (the function is not called and the previous state value
   is retained).  If the initial state value is null, then the first
   nonnull input value replaces the state value, and the transition
   function is invoked beginning with the second nonnull input value.
   This is handy for implementing aggregates like <code class="function">max</code>.
   Note that this behavior is only available when
   <em class="replaceable"><code>state_data_type</code></em>
   is the same as
   <em class="replaceable"><code>input_data_type</code></em>.
   When these types are different, you must supply a nonnull initial
   condition or use a nonstrict transition function.
  </p>
<p>   If the state transition function is not strict, then it will be called
   unconditionally at each input value, and must deal with null inputs
   and null transition values for itself.  This allows the aggregate
   author to have full control over the aggregate's handling of null values.
  </p>
<p>   If the final function is declared &#8220;<span class="quote">strict</span>&#8221;, then it will not
   be called when the ending state value is null; instead a null result
   will be returned automatically.  (Of course this is just the normal
   behavior of strict functions.)  In any case the final function has
   the option of returning a null value.  For example, the final function for
   <code class="function">avg</code> returns null when it sees there were zero
   input rows.
  </p>
<p>   Aggregates that behave like <code class="function">MIN</code> or <code class="function">MAX</code> can
   sometimes be optimized by looking into an index instead of scanning every
   input row.  If this aggregate can be so optimized, indicate it by
   specifying a <em class="firstterm">sort operator</em>.  The basic requirement is that
   the aggregate must yield the first element in the sort ordering induced by
   the operator; in other words
</p>
<pre class="programlisting">SELECT agg(col) FROM tab;</pre>
<p>
   must be equivalent to
</p>
<pre class="programlisting">SELECT col FROM tab ORDER BY col USING sortop LIMIT 1;</pre>
<p>
   Further assumptions are that the aggregate ignores null inputs, and that
   it delivers a null result if and only if there were no non-null inputs.
   Ordinarily, a data type's <code class="literal">&lt;</code> operator is the proper sort
   operator for <code class="function">MIN</code>, and <code class="literal">&gt;</code> is the proper sort
   operator for <code class="function">MAX</code>.  Note that the optimization will never
   actually take effect unless the specified operator is the &#8220;<span class="quote">less than</span>&#8221; or
   &#8220;<span class="quote">greater than</span>&#8221; strategy member of a B-tree index operator class.
  </p>
</div>
<div class="refsect1" lang="en">
<a name="id752657"></a><h2>Parameters</h2>
<div class="variablelist"><dl>
<dt><span class="term"><em class="replaceable"><code>name</code></em></span></dt>
<dd><p>      The name (optionally schema-qualified) of the aggregate function
      to create.
     </p></dd>
<dt><span class="term"><em class="replaceable"><code>input_data_type</code></em></span></dt>
<dd><p>      The input data type on which this aggregate function operates.
      This can be specified as <code class="literal">"ANY"</code> for an aggregate that
      does not examine its input values (an example is
      <code class="function">count(*)</code>).
     </p></dd>
<dt><span class="term"><em class="replaceable"><code>sfunc</code></em></span></dt>
<dd><p>      The name of the state transition function to be called for each
      input data value.  This is normally a function of two arguments,
      the first being of type <em class="replaceable"><code>state_data_type</code></em> and the second
      of type <em class="replaceable"><code>input_data_type</code></em>.  Alternatively,
      for an aggregate that does not examine its input values, the
      function takes just one argument of type <em class="replaceable"><code>state_data_type</code></em>.  In either case
      the function must return a value of type <em class="replaceable"><code>state_data_type</code></em>.  This function
      takes the current state value and the current input data item,
      and returns the next state value.
     </p></dd>
<dt><span class="term"><em class="replaceable"><code>state_data_type</code></em></span></dt>
<dd><p>      The data type for the aggregate's state value.
     </p></dd>
<dt><span class="term"><em class="replaceable"><code>ffunc</code></em></span></dt>
<dd><p>      The name of the final function called to compute the aggregate's
      result after all input data has been traversed.  The function
      must take a single argument of type <em class="replaceable"><code>state_data_type</code></em>.  The return
      data type of the aggregate is defined as the return type of this
      function.  If <em class="replaceable"><code>ffunc</code></em>
      is not specified, then the ending state value is used as the
      aggregate's result, and the return type is <em class="replaceable"><code>state_data_type</code></em>.
     </p></dd>
<dt><span class="term"><em class="replaceable"><code>initial_condition</code></em></span></dt>
<dd><p>      The initial setting for the state value.  This must be a string
      constant in the form accepted for the data type <em class="replaceable"><code>state_data_type</code></em>.  If not
      specified, the state value starts out null.
     </p></dd>
<dt><span class="term"><em class="replaceable"><code>sort_operator</code></em></span></dt>
<dd><p>      The associated sort operator for a <code class="function">MIN</code>- or
      <code class="function">MAX</code>-like aggregate.
      This is just an operator name (possibly schema-qualified).
      The operator is assumed to have the same input data types as
      the aggregate.
     </p></dd>
</dl></div>
<p>   The parameters of <code class="command">CREATE AGGREGATE</code> can be
   written in any order, not just the order illustrated above.
  </p>
</div>
<div class="refsect1" lang="en">
<a name="id752862"></a><h2>Examples</h2>
<p>   See <a href="xaggr.html" title="32.10.User-Defined Aggregates">Section32.10, &#8220;User-Defined Aggregates&#8221;</a>.
  </p>
</div>
<div class="refsect1" lang="en">
<a name="id752875"></a><h2>Compatibility</h2>
<p>   <code class="command">CREATE AGGREGATE</code> is a
   <span class="productname">PostgreSQL</span> language extension.  The SQL
   standard does not provide for user-defined aggregate functions.
  </p>
</div>
<div class="refsect1" lang="en">
<a name="id752896"></a><h2>See Also</h2>
<span class="simplelist"><a href="sql-alteraggregate.html">ALTER AGGREGATE</a>, <a href="sql-dropaggregate.html">DROP AGGREGATE</a></span>
</div>
</div></body>
</html>