11<!--
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 $
33PostgreSQL documentation
44-->
55
@@ -20,7 +20,7 @@ PostgreSQL documentation
2020
2121 <refsynopsisdiv>
2222<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>[ , ... ] ) (
2424 SFUNC = <replaceable class="PARAMETER">sfunc</replaceable>,
2525 STYPE = <replaceable class="PARAMETER">state_data_type</replaceable>
2626 [ , FINALFUNC = <replaceable class="PARAMETER">ffunc</replaceable> ]
@@ -60,35 +60,36 @@ CREATE AGGREGATE <replaceable class="PARAMETER">name</replaceable> (
6060 </para>
6161
6262 <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) .
6464 Two aggregates in the same schema can have the same name if they operate on
6565 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
6767 the name and input data type(s) of every ordinary function in the same
6868 schema.
6969 </para>
7070
7171 <para>
72- An aggregate function is made from one or two ordinary
72+ An aggregate function is made from one or two ordinary
7373 functions:
7474 a state transition function
7575 <replaceable class="PARAMETER">sfunc</replaceable>,
7676 and an optional final calculation function
7777 <replaceable class="PARAMETER">ffunc</replaceable>.
7878 These are used as follows:
7979<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
8181<replaceable class="PARAMETER">ffunc</replaceable>( internal-state ) ---> aggregate-value
8282</programlisting>
8383 </para>
8484
8585 <para>
8686 <productname>PostgreSQL</productname> creates a temporary variable
8787 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,
9293 the final function is invoked once to calculate the aggregate's return
9394 value. If there is no final function then the ending state value
9495 is returned as-is.
@@ -106,23 +107,24 @@ CREATE AGGREGATE <replaceable class="PARAMETER">name</replaceable> (
106107 <para>
107108 If the state transition function is declared <quote>strict</quote>,
108109 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.
114116 This is handy for implementing aggregates like <function>max</function>.
115117 Note that this behavior is only available when
116118 <replaceable class="PARAMETER">state_data_type</replaceable>
117- is the same as
119+ is the same as the first
118120 <replaceable class="PARAMETER">input_data_type</replaceable>.
119121 When these types are different, you must supply a nonnull initial
120122 condition or use a nonstrict transition function.
121123 </para>
122124
123125 <para>
124126 If the state transition function is not strict, then it will be called
125- unconditionally at each inputvalue , and must deal with null inputs
127+ unconditionally at each inputrow , and must deal with null inputs
126128 and null transition values for itself. This allows the aggregate
127129 author to have full control over the aggregate's handling of null values.
128130 </para>
@@ -180,10 +182,10 @@ SELECT col FROM tab ORDER BY col USING sortop LIMIT 1;
180182 <term><replaceable class="PARAMETER">input_data_type</replaceable></term>
181183 <listitem>
182184 <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 inputvalues (an exampleis
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 inputdata types. (An exampleof such an
188+ aggregate is <function>count(*)</function>.)
187189 </para>
188190 </listitem>
189191 </varlistentry>
@@ -195,8 +197,8 @@ SELECT col FROM tab ORDER BY col USING sortop LIMIT 1;
195197 In the old syntax for <command>CREATE AGGREGATE</>, the input data type
196198 is specified by a <literal>basetype</> parameter rather than being
197199 written next to the aggregate name. Note that this syntax allows
198- only one input parameter. To definean aggregatethat does not examine
199- its input values, specify the <literal>basetype</> as
200+ only one input parameter. To definea zero-argument aggregatefunction,
201+ specify the <literal>basetype</> as
200202 <literal>"ANY"</> (not <literal>*</>).
201203 </para>
202204 </listitem>
@@ -207,17 +209,15 @@ SELECT col FROM tab ORDER BY col USING sortop LIMIT 1;
207209 <listitem>
208210 <para>
209211 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,
211215 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
219219 class="PARAMETER">state_data_type</replaceable>. This function
220- takes the current state value and the current input dataitem ,
220+ takes the current state value and the current input datavalue(s) ,
221221 and returns the next state value.
222222 </para>
223223 </listitem>
@@ -237,7 +237,7 @@ SELECT col FROM tab ORDER BY col USING sortop LIMIT 1;
237237 <listitem>
238238 <para>
239239 The name of the final function called to compute the aggregate's
240- result after all inputdata has been traversed. The function
240+ result after all inputrows have been traversed. The function
241241 must take a single argument of type <replaceable
242242 class="PARAMETER">state_data_type</replaceable>. The return
243243 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;
269269 <function>MAX</>-like aggregate.
270270 This is just an operator name (possibly schema-qualified).
271271 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) .
273273 </para>
274274 </listitem>
275275 </varlistentry>