1
1
<!--
2
- $PostgreSQL: pgsql/doc/src/sgml/ref/create_aggregate.sgml,v 1.34 2006/04/15 17:45:18 tgl Exp $
2
+ $PostgreSQL: pgsql/doc/src/sgml/ref/create_aggregate.sgml,v 1.35 2006/07/27 19:52:04 tgl Exp $
3
3
PostgreSQL documentation
4
4
-->
5
5
@@ -20,7 +20,7 @@ PostgreSQL documentation
20
20
21
21
<refsynopsisdiv>
22
22
<synopsis>
23
- CREATE AGGREGATE <replaceable class="PARAMETER">name</replaceable> ( <replaceable class="PARAMETER">input_data_type</replaceable> ) (
23
+ CREATE AGGREGATE <replaceable class="PARAMETER">name</replaceable> ( <replaceable class="PARAMETER">input_data_type</replaceable> [ , ... ] ) (
24
24
SFUNC = <replaceable class="PARAMETER">sfunc</replaceable>,
25
25
STYPE = <replaceable class="PARAMETER">state_data_type</replaceable>
26
26
[ , FINALFUNC = <replaceable class="PARAMETER">ffunc</replaceable> ]
@@ -60,35 +60,36 @@ CREATE AGGREGATE <replaceable class="PARAMETER">name</replaceable> (
60
60
</para>
61
61
62
62
<para>
63
- An aggregate function is identified by its name and input data type.
63
+ An aggregate function is identified by its name and input data type(s) .
64
64
Two aggregates in the same schema can have the same name if they operate on
65
65
different input types. The
66
- name and input data type of an aggregate must also be distinct from
66
+ name and input data type(s) of an aggregate must also be distinct from
67
67
the name and input data type(s) of every ordinary function in the same
68
68
schema.
69
69
</para>
70
70
71
71
<para>
72
- An aggregate function is made from one or two ordinary
72
+ An aggregate function is made from one or two ordinary
73
73
functions:
74
74
a state transition function
75
75
<replaceable class="PARAMETER">sfunc</replaceable>,
76
76
and an optional final calculation function
77
77
<replaceable class="PARAMETER">ffunc</replaceable>.
78
78
These are used as follows:
79
79
<programlisting>
80
- <replaceable class="PARAMETER">sfunc</replaceable>( internal-state, next-data-item ) ---> next-internal-state
80
+ <replaceable class="PARAMETER">sfunc</replaceable>( internal-state, next-data-values ) ---> next-internal-state
81
81
<replaceable class="PARAMETER">ffunc</replaceable>( internal-state ) ---> aggregate-value
82
82
</programlisting>
83
83
</para>
84
84
85
85
<para>
86
86
<productname>PostgreSQL</productname> creates a temporary variable
87
87
of data type <replaceable class="PARAMETER">stype</replaceable>
88
- to hold the current internal state of the aggregate. At each input
89
- data item,
90
- the state transition function is invoked to calculate a new
91
- internal state value. After all the data has been processed,
88
+ to hold the current internal state of the aggregate. At each input row,
89
+ the aggregate argument value(s) are calculated and
90
+ the state transition function is invoked with the current state value
91
+ and the new argument value(s) to calculate a new
92
+ internal state value. After all the rows have been processed,
92
93
the final function is invoked once to calculate the aggregate's return
93
94
value. If there is no final function then the ending state value
94
95
is returned as-is.
@@ -106,23 +107,24 @@ CREATE AGGREGATE <replaceable class="PARAMETER">name</replaceable> (
106
107
<para>
107
108
If the state transition function is declared <quote>strict</quote>,
108
109
then it cannot be called with null inputs. With such a transition
109
- function, aggregate execution behaves as follows. Null input values
110
- are ignored (the function is not called and the previous state value
111
- is retained). If the initial state value is null, then the first
112
- nonnull input value replaces the state value, and the transition
113
- function is invoked beginning with the second nonnull input value.
110
+ function, aggregate execution behaves as follows. Rows with any null input
111
+ values are ignored (the function is not called and the previous state value
112
+ is retained). If the initial state value is null, then at the first row
113
+ with all-nonnull input values, the first argument value replaces the state
114
+ value, and the transition function is invoked at subsequent rows with
115
+ all-nonnull input values.
114
116
This is handy for implementing aggregates like <function>max</function>.
115
117
Note that this behavior is only available when
116
118
<replaceable class="PARAMETER">state_data_type</replaceable>
117
- is the same as
119
+ is the same as the first
118
120
<replaceable class="PARAMETER">input_data_type</replaceable>.
119
121
When these types are different, you must supply a nonnull initial
120
122
condition or use a nonstrict transition function.
121
123
</para>
122
124
123
125
<para>
124
126
If the state transition function is not strict, then it will be called
125
- unconditionally at each input value , and must deal with null inputs
127
+ unconditionally at each input row , and must deal with null inputs
126
128
and null transition values for itself. This allows the aggregate
127
129
author to have full control over the aggregate's handling of null values.
128
130
</para>
@@ -180,10 +182,10 @@ SELECT col FROM tab ORDER BY col USING sortop LIMIT 1;
180
182
<term><replaceable class="PARAMETER">input_data_type</replaceable></term>
181
183
<listitem>
182
184
<para>
183
- The input data type on which this aggregate function operates.
184
- This can be specified as <literal>*</> for an aggregate that
185
- does not examine its input values (an example is
186
- <function>count(*)</function>).
185
+ An input data type on which this aggregate function operates.
186
+ To create a zero-argument aggregate function, write <literal>*</>
187
+ in place of the list of input data types. (An example of such an
188
+ aggregate is <function>count(*)</function>.)
187
189
</para>
188
190
</listitem>
189
191
</varlistentry>
@@ -195,8 +197,8 @@ SELECT col FROM tab ORDER BY col USING sortop LIMIT 1;
195
197
In the old syntax for <command>CREATE AGGREGATE</>, the input data type
196
198
is specified by a <literal>basetype</> parameter rather than being
197
199
written next to the aggregate name. Note that this syntax allows
198
- only one input parameter. To define an aggregate that does not examine
199
- its input values, specify the <literal>basetype</> as
200
+ only one input parameter. To define a zero-argument aggregate function,
201
+ specify the <literal>basetype</> as
200
202
<literal>"ANY"</> (not <literal>*</>).
201
203
</para>
202
204
</listitem>
@@ -207,17 +209,15 @@ SELECT col FROM tab ORDER BY col USING sortop LIMIT 1;
207
209
<listitem>
208
210
<para>
209
211
The name of the state transition function to be called for each
210
- input data value. This is normally a function of two arguments,
212
+ input row. For an <replaceable class="PARAMETER">N</>-argument
213
+ aggregate function, the <replaceable class="PARAMETER">sfunc</>
214
+ must take <replaceable class="PARAMETER">N</>+1 arguments,
211
215
the first being of type <replaceable
212
- class="PARAMETER">state_data_type</replaceable> and the second
213
- of type <replaceable
214
- class="PARAMETER">input_data_type</replaceable>. Alternatively,
215
- for an aggregate that does not examine its input values, the
216
- function takes just one argument of type <replaceable
217
- class="PARAMETER">state_data_type</replaceable>. In either case
218
- the function must return a value of type <replaceable
216
+ class="PARAMETER">state_data_type</replaceable> and the rest
217
+ matching the declared input data type(s) of the aggregate.
218
+ The function must return a value of type <replaceable
219
219
class="PARAMETER">state_data_type</replaceable>. This function
220
- takes the current state value and the current input data item ,
220
+ takes the current state value and the current input data value(s) ,
221
221
and returns the next state value.
222
222
</para>
223
223
</listitem>
@@ -237,7 +237,7 @@ SELECT col FROM tab ORDER BY col USING sortop LIMIT 1;
237
237
<listitem>
238
238
<para>
239
239
The name of the final function called to compute the aggregate's
240
- result after all input data has been traversed. The function
240
+ result after all input rows have been traversed. The function
241
241
must take a single argument of type <replaceable
242
242
class="PARAMETER">state_data_type</replaceable>. The return
243
243
data type of the aggregate is defined as the return type of this
@@ -269,7 +269,7 @@ SELECT col FROM tab ORDER BY col USING sortop LIMIT 1;
269
269
<function>MAX</>-like aggregate.
270
270
This is just an operator name (possibly schema-qualified).
271
271
The operator is assumed to have the same input data types as
272
- the aggregate.
272
+ the aggregate (which must be a single-argument aggregate) .
273
273
</para>
274
274
</listitem>
275
275
</varlistentry>
0 commit comments