aboutsummaryrefslogtreecommitdiff
path: root/doc/src/sgml/ref/create_aggregate.sgml
blob: 44291458d60a9cfbe71ab93aa0cadd98335c9e88 (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
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
<!--
$Header: /cvsroot/pgsql/doc/src/sgml/ref/create_aggregate.sgml,v 1.9 2000/03/31 14:57:05 tgl Exp $
Postgres documentation
-->

<refentry id="SQL-CREATEAGGREGATE">
 <refmeta>
  <refentrytitle id="sql-createaggregate-title">
   CREATE AGGREGATE
  </refentrytitle>
  <refmiscinfo>SQL - Language Statements</refmiscinfo>
 </refmeta>

 <refnamediv>
  <refname>
   CREATE AGGREGATE
  </refname>
  <refpurpose>
   Defines a new aggregate function
  </refpurpose>
 </refnamediv>
 <refsynopsisdiv>
  <refsynopsisdivinfo>
   <date>1999-07-20</date>
  </refsynopsisdivinfo>
  <synopsis>
CREATE AGGREGATE <replaceable class="PARAMETER">name</replaceable> ( BASETYPE = <replaceable class="PARAMETER">input_data_type</replaceable>
    [ , SFUNC1 = <replaceable class="PARAMETER">sfunc1</replaceable>, STYPE1 = <replaceable class="PARAMETER">state1_type</replaceable> ]
    [ , SFUNC2 = <replaceable class="PARAMETER">sfunc2</replaceable>, STYPE2 = <replaceable class="PARAMETER">state2_type</replaceable> ]
    [ , FINALFUNC = <replaceable class="PARAMETER">ffunc</replaceable> ]
    [ , INITCOND1 = <replaceable class="PARAMETER">initial_condition1</replaceable> ]
    [ , INITCOND2 = <replaceable class="PARAMETER">initial_condition2</replaceable> ] )
  </synopsis>

  <refsect2 id="R2-SQL-CREATEAGGREGATE-1">
   <refsect2info>
    <date>1998-09-09</date>
   </refsect2info>
   <title>
    Inputs
   </title>
   <para>
    <variablelist>
     <varlistentry>
      <term><replaceable class="PARAMETER">name</replaceable></term>
      <listitem>
       <para>
	The name of an aggregate function to create.
       </para>
      </listitem>
     </varlistentry>

     <varlistentry>
      <term><replaceable class="PARAMETER">input_data_type</replaceable></term>
      <listitem>
       <para>
	The input data type on which this aggregate function operates.
       </para>
      </listitem>
     </varlistentry>

     <varlistentry>
      <term><replaceable class="PARAMETER">sfunc1</replaceable></term>
      <listitem>
       <para>
	A state transition function
	to be called for every non-NULL input data value.
	This must be a function of two arguments, the first being of
	type <replaceable class="PARAMETER">state1_type</replaceable>
	and the second of
	type <replaceable class="PARAMETER">input_data_type</replaceable>.
	The function must return a value of
	type <replaceable class="PARAMETER">state1_type</replaceable>.
	This function takes the current state value 1 and the current
	input data item, and returns the next state value 1.
       </para>
      </listitem>
     </varlistentry>

     <varlistentry>
      <term><replaceable class="PARAMETER">state1_type</replaceable></term>
      <listitem>
       <para>
	The data type for the first state value of the aggregate.
       </para>
      </listitem>
     </varlistentry>

     <varlistentry>
      <term><replaceable class="PARAMETER">sfunc2</replaceable></term>
      <listitem>
       <para>
	A state transition function
	to be called for every non-NULL input data value.
	This must be a function of one argument of
	type <replaceable class="PARAMETER">state2_type</replaceable>,
	returning a value of the same type.
	This function takes the current state value 2 and
	returns the next state value 2.
       </para>
      </listitem>
     </varlistentry>

     <varlistentry>
      <term><replaceable class="PARAMETER">state2_type</replaceable></term>
      <listitem>
       <para>
	The data type for the second state value of the aggregate.
       </para>
      </listitem>
     </varlistentry>

     <varlistentry>
      <term><replaceable class="PARAMETER">ffunc</replaceable></term>
      <listitem>
       <para>
	The final function called to compute the aggregate's result
	after all input data has been traversed.
	If both state values are used, the final function must
	take two arguments of types
	<replaceable class="PARAMETER">state1_type</replaceable>
	and
	<replaceable class="PARAMETER">state2_type</replaceable>.
	If only one state value is used, the final function must
	take a single argument of that state value's type.
	The output datatype of the aggregate is defined as the return
	type of this function.
       </para>
      </listitem>
     </varlistentry>

     <varlistentry>
      <term><replaceable class="PARAMETER">initial_condition1</replaceable></term>
      <listitem>
       <para>
	The initial value for state value 1.
       </para>
      </listitem>
     </varlistentry>

     <varlistentry>
      <term><replaceable class="PARAMETER">initial_condition2</replaceable></term>
      <listitem>
       <para>
	The initial value for state value 2.
       </para>
      </listitem>
     </varlistentry>
    </variablelist>
   </para>
  </refsect2>

  <refsect2 id="R2-SQL-CREATEAGGREGATE-2">
   <refsect2info>
    <date>1998-09-09</date>
   </refsect2info>
   <title>
    Outputs
   </title>
   <para>

    <variablelist>
     <varlistentry>
      <term><computeroutput>
CREATE
       </computeroutput></term>
      <listitem>
       <para>
	Message returned if the command completes successfully.
       </para>
      </listitem>
     </varlistentry>
    </variablelist>
   </para>
  </refsect2>
 </refsynopsisdiv>

 <refsect1 id="R1-SQL-CREATEAGGREGATE-1">
  <refsect1info>
   <date>1998-09-09</date>
  </refsect1info>
  <title>
   Description
  </title>
  <para>
   <command>CREATE AGGREGATE</command> 
   allows a user or programmer to extend <productname>Postgres</productname>
   functionality by defining new aggregate functions. Some aggregate functions
   for base types such as <function>min(int4)</function>
   and <function>avg(float8)</function> are already provided in the base
   distribution. If one defines new types or needs an aggregate function not
   already provided then <command>CREATE AGGREGATE</command>
   can be used to provide the desired features.
  </para>
  <para>
   An  aggregate  function is identified by its name and input data type.
   Two aggregates can have the same name if they operate on different
   input types.  To avoid confusion, do not make an ordinary function
   of the same name and input data type as an aggregate.
  </para>
  <para>
   An  aggregate  function is made from between one and three ordinary
   functions:
   two state transition functions, 
   <replaceable class="PARAMETER">sfunc1</replaceable>
   and <replaceable class="PARAMETER">sfunc2</replaceable>,
   and a final calculation function,
   <replaceable class="PARAMETER">ffunc</replaceable>.
   These are used as follows:
   <programlisting>
<replaceable class="PARAMETER">sfunc1</replaceable>( internal-state1, next-data-item ) ---> next-internal-state1
<replaceable class="PARAMETER">sfunc2</replaceable>( internal-state2 ) ---> next-internal-state2
<replaceable class="PARAMETER">ffunc</replaceable>(internal-state1, internal-state2) ---> aggregate-value
   </programlisting>
  </para>
  <para>
   <productname>Postgres</productname> creates one or two temporary variables
   (of data types <replaceable class="PARAMETER">stype1</replaceable> and/or
   <replaceable class="PARAMETER">stype2</replaceable>) to hold the
   current internal states of the aggregate.  At each input data item,
   the state transition function(s) are invoked to calculate new values
   for the internal state values.  After all the data has been processed,
   the final function is invoked once to calculate the aggregate's output
   value.
  </para>
  <para>
   <replaceable class="PARAMETER">ffunc</replaceable> must be specified if
   both transition functions are specified.  If only one transition function
   is used, then <replaceable class="PARAMETER">ffunc</replaceable> is
   optional.  The default behavior when
   <replaceable class="PARAMETER">ffunc</replaceable> is not provided is
   to return the ending value of the internal state value being used
   (and, therefore, the aggregate's output type is the same as that
   state value's type).
  </para>  
  
  <para>	
   An aggregate function may also provide one or two initial conditions,
   that is, initial values for the internal state values being used.
   These are specified and  stored in the database as fields of type
   <type>text</type>, but they must be valid external representations
   of constants of the state value datatypes.  If
   <replaceable class="PARAMETER">sfunc1</replaceable> is specified 
   without an <replaceable class="PARAMETER">initcond1</replaceable> value,
   then the system does not call
   <replaceable class="PARAMETER">sfunc1</replaceable> 
   at the first input item; instead, the internal state value 1 is
   initialized with the first input value, and
   <replaceable class="PARAMETER">sfunc1</replaceable> is called beginning 
   at the second input item.  This is useful for aggregates like MIN and
   MAX.  Note that an aggregate using this feature will return NULL when
   called with no input values.  There is no comparable provision for
   state value 2; if <replaceable class="PARAMETER">sfunc2</replaceable> is
   specified then an <replaceable class="PARAMETER">initcond2</replaceable> is
   required.
  </para>
  
  <refsect2 id="R2-SQL-CREATEAGGREGATE-3">
   <refsect2info>
    <date>1998-09-09</date>
   </refsect2info>
   <title>
    Notes
   </title>
   <para>
    Use <command>DROP AGGREGATE</command>
    to drop aggregate functions.
   </para>

   <para>
    The parameters of <command>CREATE AGGREGATE</command> can be written
    in any order, not just the order illustrated above.
   </para>

   <para>
    It  is possible to specify aggregate functions
    that have varying combinations of state  and  final  functions. 
    For example, the <function>count</function> aggregate requires
    <replaceable class="PARAMETER">sfunc2</replaceable> 
    (an incrementing function) but not
    <replaceable class="PARAMETER">sfunc1</replaceable>  or  
    <replaceable class="PARAMETER">ffunc</replaceable>,
    whereas  the  <function>sum</function> aggregate requires
    <replaceable class="PARAMETER">sfunc1</replaceable> (an addition
    function) but not <replaceable class="PARAMETER">sfunc2</replaceable> or
    <replaceable class="PARAMETER">ffunc</replaceable>,  and  the
    <function>avg</function>
    aggregate  requires 
    both state functions as
    well as a <replaceable class="PARAMETER">ffunc</replaceable> (a division
    function) to  produce  its 
    answer.   In any case, at least one state function must be
    defined, and any <replaceable class="PARAMETER">sfunc2</replaceable> must
    have  a  corresponding
    <replaceable class="PARAMETER">initcond2</replaceable>. 
   </para>

  </refsect2>
 </refsect1>

 <refsect1 id="R1-SQL-CREATEAGGREGATE-2">
  <title>
   Usage
  </title>
  <para>
   Refer to the chapter on aggregate functions
   in the <citetitle>PostgreSQL Programmer's Guide</citetitle> for
   complete examples of usage.
  </para>
 </refsect1>

 <refsect1 id="R1-SQL-CREATEAGGREGATE-3">
  <title>
   Compatibility
  </title>

  <refsect2 id="R2-SQL-CREATEAGGREGATE-4">
   <refsect2info>
    <date>1998-09-09</date>
   </refsect2info>
   <title>
    SQL92
   </title>
   <para>
    <command>CREATE AGGREGATE</command> 
    is a <productname>Postgres</productname> language extension.
    There is no <command>CREATE AGGREGATE</command> in SQL92.
   </para>
  </refsect2>
 </refsect1>
</refentry>

<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:nil
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
sgml-parent-document:nil
sgml-default-dtd-file:"../reference.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:"/usr/lib/sgml/catalog"
sgml-local-ecat-files:nil
End:
-->