1- <!-- $PostgreSQL: pgsql/doc/src/sgml/xaggr.sgml,v 1.35 2007/02/01 00:28:18 momjian Exp $ -->
1+ <!-- $PostgreSQL: pgsql/doc/src/sgml/xaggr.sgml,v 1.36 2008/11/20 21:10:44 tgl Exp $ -->
22
33 <sect1 id="xaggr">
44 <title>User-Defined Aggregates</title>
99 </indexterm>
1010
1111 <para>
12- Aggregate functions in <productname>PostgreSQL</productname>
12+ Aggregate functions in <productname>PostgreSQL</productname>
1313 are expressed in terms of <firstterm>state values</firstterm>
1414 and <firstterm>state transition functions</firstterm>.
1515 That is, an aggregate operates using a state value that is updated
4141 aggregate to work on a data type for complex numbers,
4242 we only need the addition function for that data type.
4343 The aggregate definition would be:
44-
44+
4545<screen>
4646CREATE AGGREGATE sum (complex)
4747(
@@ -80,7 +80,7 @@ SELECT sum(a) FROM test_complex;
8080 the transition function is marked <quote>strict</> (i.e., not to be called
8181 for null inputs).
8282 </para>
83-
83+
8484 <para>
8585 Another bit of default behavior for a <quote>strict</> transition function
8686 is that the previous state value is retained unchanged whenever a
@@ -89,7 +89,7 @@ SELECT sum(a) FROM test_complex;
8989 transition function as strict; instead code it to test for null inputs and
9090 do whatever is needed.
9191 </para>
92-
92+
9393 <para>
9494 <function>avg</> (average) is a more complex example of an aggregate.
9595 It requires
@@ -132,7 +132,10 @@ CREATE AGGREGATE array_accum (anyelement)
132132</programlisting>
133133
134134 Here, the actual state type for any aggregate call is the array type
135- having the actual input type as elements.
135+ having the actual input type as elements. The behavior of the aggregate
136+ is to concatenate all the inputs into an array of that type.
137+ (Note: the built-in aggregate <function>array_agg</> provides similar
138+ functionality, with better performance than this definition would have.)
136139 </para>
137140
138141 <para>
@@ -149,14 +152,14 @@ SELECT attrelid::regclass, array_accum(attname)
149152 pg_tablespace | {spcname,spcowner,spclocation,spcacl}
150153(1 row)
151154
152- SELECT attrelid::regclass, array_accum(atttypid)
155+ SELECT attrelid::regclass, array_accum(atttypid::regtype )
153156 FROM pg_attribute
154157 WHERE attnum > 0 AND attrelid = 'pg_tablespace'::regclass
155158 GROUP BY attrelid;
156159
157- attrelid | array_accum
158- ---------------+-----------------
159- pg_tablespace | {19,26,25,1034 }
160+ attrelid | array_accum
161+ ---------------+---------------------------
162+ pg_tablespace | {name,oid,text,aclitem[] }
160163(1 row)
161164</programlisting>
162165 </para>