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 $ -->
2
2
3
3
<sect1 id="xaggr">
4
4
<title>User-Defined Aggregates</title>
9
9
</indexterm>
10
10
11
11
<para>
12
- Aggregate functions in <productname>PostgreSQL</productname>
12
+ Aggregate functions in <productname>PostgreSQL</productname>
13
13
are expressed in terms of <firstterm>state values</firstterm>
14
14
and <firstterm>state transition functions</firstterm>.
15
15
That is, an aggregate operates using a state value that is updated
41
41
aggregate to work on a data type for complex numbers,
42
42
we only need the addition function for that data type.
43
43
The aggregate definition would be:
44
-
44
+
45
45
<screen>
46
46
CREATE AGGREGATE sum (complex)
47
47
(
@@ -80,7 +80,7 @@ SELECT sum(a) FROM test_complex;
80
80
the transition function is marked <quote>strict</> (i.e., not to be called
81
81
for null inputs).
82
82
</para>
83
-
83
+
84
84
<para>
85
85
Another bit of default behavior for a <quote>strict</> transition function
86
86
is that the previous state value is retained unchanged whenever a
@@ -89,7 +89,7 @@ SELECT sum(a) FROM test_complex;
89
89
transition function as strict; instead code it to test for null inputs and
90
90
do whatever is needed.
91
91
</para>
92
-
92
+
93
93
<para>
94
94
<function>avg</> (average) is a more complex example of an aggregate.
95
95
It requires
@@ -132,7 +132,10 @@ CREATE AGGREGATE array_accum (anyelement)
132
132
</programlisting>
133
133
134
134
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.)
136
139
</para>
137
140
138
141
<para>
@@ -149,14 +152,14 @@ SELECT attrelid::regclass, array_accum(attname)
149
152
pg_tablespace | {spcname,spcowner,spclocation,spcacl}
150
153
(1 row)
151
154
152
- SELECT attrelid::regclass, array_accum(atttypid)
155
+ SELECT attrelid::regclass, array_accum(atttypid::regtype )
153
156
FROM pg_attribute
154
157
WHERE attnum > 0 AND attrelid = 'pg_tablespace'::regclass
155
158
GROUP BY attrelid;
156
159
157
- attrelid | array_accum
158
- ---------------+-----------------
159
- pg_tablespace | {19,26,25,1034 }
160
+ attrelid | array_accum
161
+ ---------------+---------------------------
162
+ pg_tablespace | {name,oid,text,aclitem[] }
160
163
(1 row)
161
164
</programlisting>
162
165
</para>
0 commit comments